// ------------------------------------------------------------------------------------------------------------------------------------
// Aonirs Blade

// Documentation can be found at:
// 		http://cp-srv-01/wiki
//		file://///CP-SRV-01/Projects/SF3/LevelDesign/ScriptDokumentation/html/index.html

// includes:
#include "../../basicScripts/LevelBase.as"


class Level: LevelBase
{
	string m_stgVersion = "0.163 - 07.12.2017 - 21:10";
	//string m_stgVersion = "0.15 - 05.12.2017 - 21:28";
	//string m_stgVersion = "0.14 - 03.12.2017 - 01:07";
	//string m_stgVersion = "0.13 - 01.08.2017 - 15:30";
	//string m_stgVersion = "0.12 - 19.05.2017 - 20:20";


	// --------------------------------------------------------------------------------------------------------------------
	// --- constructor --- dont make any changes here
	Level (LevelReference@ _Reference)
	{
		super(_Reference);
	}

	// --- A O N I R S   B L A D E   0  ----------------------------------------------------------------------------------

	string m_stgSecretTreasure = 			"AB_SecretTreasure";

	string m_stgTreasure_Plate = 			"AB_Treasure_PlatformPlate_";
	string m_stgTreasure_PlateTrg = 		"AB_Treasure_PlatformPlateTrigger_";
	string m_stgTreasure_PlateEffect = 		"AB_Treasure_PlatformPlateEffect_";
	string m_stgTreasure_ChestEffect = 		"AB_Treasure_ChestEffect_";

	// --- A O N I R S   B L A D E   1  ----------------------------------------------------------------------------------
	bool m_bStartedRTSpart = 		false;

	bool m_bKilledAmbush01 =		false;
	bool m_bKilledAmbush02 =		false;

	// factions
	int m_iFaction_IronFalcons =  	1;
	int m_iFaction_Deathman = 		2;
	int m_iFaction_Neutral = 		3;
	int m_iFaction_Mentalists = 	4;
	int m_iFaction_DeathmenRTS = 	5;
	int m_iFaction_DeathmenSiege = 	6;

	// Entrances
	string m_stgEntrance_AB_TFD = 			"PlayerSpawn_AB_ToFD";

	// --- Strings of all the quests related Creatures
	// NPCs, Enemies, Bosses and so on.
	string m_stgCrt_KiroyTeranor = 			"Kiroy_Teranor";
	string m_stgCrt_ArenorArandir = 		"Arenor_Arandir";
	string m_stgCrt_KaylenVirTrath = 		"Katras";
	string m_stgCrt_DeadScout = 			"AB1_Dead_Scout";
	string m_stgCrt_ScoutFalcons = 			"AB1_Scout_Falcons";
	string m_stgCrt_ScoutDeathman = 		"AB1_Scout_Deathman";
	string m_stgCrt_FalconTraitor = 		"AB1_Traitor_Falcon";
	string m_stgCrt_Necromancer = 			"AB1_Necromancer";
	string m_stgCrt_SummonedMiniboss = 		"AB1_SummonedMiniboss";
	string m_stgSpawn_SumminedMinions_01 = 	"Undead_Encounter_Summoning_01";
	string m_stgSpawn_SumminedMinions_02 = 	"Undead_Encounter_Summoning_02";
	string m_stgSpawn_SumminedMinions_03 = 	"Undead_Encounter_Summoning_03";


	string m_stgPath_TraitorWalkRoute = 	"TraitorWalkPath_Falcons";
	string m_stgPath_TraitorWalkRoute_Deathmen = "TraitorWalkPath_Deathmen";
	bool m_bTraitorFriendly = 				false;

	string m_stgCrt_Soldier_Falcons = 		"AB1_Soldier_Falcons";
	//string m_stgCrt_Soldier_Deathmen = 	"AB1_Soldier_Deathman";
	string m_stgCrt_Soldier_Deathmen = 		"Katras";
	string m_stgTrg_FalconCamp = 			"Enter_FalconCamp";
	string m_stgTrg_DeathmanCamp = 			"Enter_DeathmanCamp";

	string m_stgPos_DwarfCamp = 			"DwarfBrothers";
	string m_stgCrt_DwarfBrother_Caravan = 	"AB1_Dwarf_Brothers_Caravan";
	string m_stgCrt_DwarfBrother_01 = 		"AB1_Dwarf_Brother_1";
	string m_stgCrt_DwarfBrother_02 = 		"AB1_Dwarf_Brother_2";
	string m_stgCrt_DwarfBrother_03 = 		"AB1_Dwarf_Brother_3";

	bool m_bFinishedDebating = 				false;
	bool m_bNextStep = 						false;

	// Intro
	string m_stgSpawn_StagedBattle_UD =  	"FightScene_Undead";
	string m_stdSpawn_UndeadFortBoss =		"Undead_Fort_Boss";
	string m_stgSpawn_StagedBattle_DM =  	"FightScene_Deathman";
	string m_stgSpawn_StagedBattle_IF =  	"FightScene_Falcons";
	string m_stgTrg_DebateArea = 			"DebateArea";
	string m_stgTrg_SoldierTalk = 			"AB1_SoldierTalk";
	string m_stgSpawn_DeathmanPatrol = 		"AB1_DeathmanPatrolAtBorder";
	string m_stgTrg_DeathmanPatrol = 		"AB1_SoldierTalk_Patrol";

	// All the Wave stuff from the undead, the iron falcons and aonirs deathmen
	string m_stgBldg_Wave_PlayerBase = 				"AB1_UndeadWaves_PlayerBase";
	string m_stgBldgDes_Wave_PlayerBase_Inactive = 	"S_AB1_UndeadWaves_PlayerBase_Inactive";
	string m_stgBldgDes_Wave_PlayerBase_01 = 		"S_AB1_UndeadWaves_PlayerBase_01";
	string m_stgBldgDes_Wave_PlayerBase_02a = 		"S_AB1_UndeadWaves_PlayerBase_02-a";
	string m_stgBldgDes_Wave_PlayerBase_02b = 		"S_AB1_UndeadWaves_PlayerBase_02-b";

	string m_stgBldg_Wave_Trebuchet = 			"AB1_UndeadWaves_Trebuchet";
	string m_stgBldg_Trebuchet = 				"AB1_S_Trebuchet_Building";
	string m_stgBldgDes_Wave_Trebuchet = 		"S_AB1_UndeadWaves_Trebuchet";

	// The Pathes for the waves and stuff
	string m_stgPath_AttackTheUndead = 		"Path_AttackTheUndead";
	string m_stgPath_BossWalkHome = 		"Path_BossWalk";
	string m_stgPath_Wave_PlayerBase =		"Path_UndeadWaves_PlayerBase";
	string m_stgPath_Wave_Trebuchet =		"Path_UndeadWaves_Trebuchet";

	bool m_bOneLeftOneRight = 				false;

	// All the waves
	string m_stgPos_WaveWaitFalcons = 		"FalconWaves_Wait";
	string m_stgBldg_WaveFalcons = 			"FalconWaves";
	string m_stgBldg_WaveFalcons_BigWave = 	"FalconWaves_MainWave";
	string m_stgBldg_WaveFalcons_Connect = 	"FalconWaves_Connection";
	string m_stgBldgDes_Falcons_connect = 	"S_AB1_FalconWaves_connectedTent";
	string m_stgBldgDes_Falcons_Main = 		"S_AB1_FalconWaves_MainWave"; // doesnt send waves
	string m_stgBldgDes_Falcons_0 = 		"S_AB1_FalconWaves_00"; // doesnt send waves
	string m_stgBldgDes_Falcons_1 = 		"S_AB1_FalconWaves_01";	// sends weak waves
	string m_stgBldgDes_Falcons_2 = 		"S_AB1_FalconWaves_02"; // sends the strong waves

	string m_stgPos_WaveWaitDeathman = 		"DeathmenWaves_Wait";
	string m_stgBldg_WaveDeathmen = 		"DeathmenWaves";
	string m_stgBldg_WaveDeathmen_BigWave = "DeathmenWaves_MainWave";
	string m_stgBldg_WaveDeathmen_Connect = "DeathmenWaves_Connection";
	string m_stgBldgDes_Deathmen_connect = 	"S_AB1_DeathmenWaves_connectedTent";
	string m_stgBldgDes_Deathmen_Main = 	"S_AB1_DeathmenWaves_MainWave"; // doesnt send waves
	string m_stgBldgDes_Deathmen_0 = 		"S_AB1_DeathmenWaves_00"; // doesnt send waves
	string m_stgBldgDes_Deathmen_1 = 		"S_AB1_DeathmenWaves_01"; // sends weak waves
	string m_stgBldgDes_Deathmen_2 = 		"S_AB1_DeathmenWaves_02"; // sends the strong waves

	int m_bNumUpgraded =					0;
	array <uint> m_arrWavesWaiting; // collecting ID's from the MainWaves, that are waiting for the normal waves to start

	// Choosing the Faction
	string m_stgObj_Camp_Falcons = 			"StartRTS_Camp_01";
	string m_stgObj_Camp_Deathman = 		"StartRTS_Camp_02";
	string m_stgSect_Falcons = 				"Sector_Player_01";
	string m_stgSect_Deathman = 			"Sector_Player_02";
	string m_stgTrg_SectorCamp_Falcons = 	"SO_Scout_01";
	string m_stgTrg_SectorCamp_Deathmen = 	"SO_Scout_02";
	string m_stgPos_SectorCamp = 			""; //<- it's wanted, that this string is empty here
	string m_stgPos_SectorCamp_Falcons = 	"ScoutPosition_01";
	string m_stgPos_SectorCamp_Deathmen = 	"ScoutPosition_02";
	bool m_bRessourceAlreadyTriggered = 	false;
	int m_iWoodcutterNumber = 				0;
	int m_iFoodCabinNumber = 				0;


	// the trebuchet, ToDo
	int m_iTimesTrebuchetTaken = 			0;
	string m_stgCrtTrebuchet = 				"AB1_Trebuchet";
	string m_stgSectTrebuchet = 			"Sector_Trebuchet";
	string m_stgBldg_TrebuchetAttack = 		"AB1_UndeadWaves_Target";

	// The ritual, ToDo
	// - building needs to be usable for every faction
	int m_iStartSummoningMiniboss = 		1;
	bool m_bSummonMiniboss = 				true;
	int m_iMinibossSummoningTime = 			300;
	int m_iSummoningEffects = 				12;
	int m_iSummoningEffectsCurrent = 		0;
	string m_stgObjSummoningEffect = 		"SummonEffect_";
	string m_stgItem_Necromancer =			"AB1_MageStaff_Nekromancer_Q?";
	string m_stgSpawn_Miniboss = 			"SpawnMiniboss";
	string m_stgBldg_Summoning = 			"AB1_Summoning_Staff";
	bool m_bSpawnedMiniBoss = 				false;

	// Dwarf Brothers
	//int m_iDwarfBrothers_Saved = 			0;
	bool m_bDwarfAttackStarted = 			false;
	bool m_bDwarfSaved_01 = 				false;
	bool m_bDwarfSaved_02 = 				false;
	bool m_bDwarfSaved_03 = 				false;
	int m_iDwarfsArrived = 					1;
	int m_iDwarfBrothers_Alive = 			3;

	// Final Area
	string m_stgTrg_FinalArea = 			"EnterFinalArea";
	string m_stgPos_FinalArea_army = 		"FinalFrontier_army";
	string m_stgPos_FinalArea_commanders = 	"FinalFrontier_commanders";
	bool m_bPlayedJoinGroupCutscene =		false;
	array<uint> m_arr_UndeadWave;
	array<uint> m_arr_DeathmenFalconWave;

	// Variables
	bool m_bChoiceIronFaloncs = 			false;
	bool m_bChoiceAonirsDeathman = 			false;
	bool m_bChoosedIronFaloncs = 			false;
	bool m_bChoosedAonirsDeathman = 		false;
	bool m_bPlayFortressCutscene = 			false;

	bool m_bAttackSuccess = 				false;
	int m_iWavesThatMadeIt = 				0;
	int m_iWavesThatHaveToMakeIt = 			3;
	int m_iNumberMainCharsInArea = 			0;
	int m_iUndeadTowersDestroyed =			0;
	int m_iNumUndeadTowers =				3;

	int m_iTimerForTrueSight = 				60;
	string m_sRevealedSector = 				"";


	int m_iFinalWave =						0;
	int m_iFinalWavesInterval = 			60; 

	// --- A O N I R S   B L A D E   2  ----------------------------------------------------------------------------------

	bool m_bDeathmanWeakend =				false;
	bool m_bArenorOnTheMove =				false;

	int m_iKilledUndeadDuringRitualCount = 	0;
	int m_iUndeadRitualSpawns =		 		2;

	bool m_bLootedEngineerTools =			false;

	int m_iTimerDeathmenAI = 				60;

	// -------------------------------------------------------------------------------------------------------------------
	// overriden:

	// Function that is called when the Level is created for the first time - only called once within the campaign
	void OnCreated () override
	{
		printImportant("V E R S I O N : LevelBase: "+m_LevelBaseVersion+", Map Script: " + m_stgVersion+" (Level was just created)");
		printImportant("M A P : Map: "+ m_Reference.GetMapId() +", setting: " + m_Reference.GetWorld().GetCurrentMapSetting( m_Reference.GetMapId() ) +".");

		LevelBase::OnCreated();

		// Calling the final functions
		InitEvents(); // register the Events
		InitCommon(); // Start the Level

		// setting specific calls
		if ( m_Reference.GetWorld().GetCurrentMapSetting( m_Reference.GetMapId() ) == 0 ) // First setting
		{
			InitEvents_0(); // Register the Events
			InitCommon_0(); // Start the Level
		}
		else if ( m_Reference.GetWorld().GetCurrentMapSetting( m_Reference.GetMapId() ) == 1 ) // Second setting
		{
			InitEvents_1(); // Register the Events
			InitCommon_1(); // Start the Level
		}
		m_Reference.GetWorld().SetMapAccessible( m_iMap_GK, false, m_Reference.GetWorld().GetCurrentMapSetting(m_iMap_GK));
	}

	// level data had been restored from savegame - ensure version compability here
	void OnLoaded (const uint _uVersion) override
	{
		printImportant("V E R S I O N : LevelBase: "+m_LevelBaseVersion+", Map Script: " + m_stgVersion+" (Level was loaded)");
		LevelBase::OnLoaded(_uVersion);

		if ( m_Reference.GetWorld().GetCurrentMapSetting( m_Reference.GetMapId() ) == 0 )
		{
			if(m_Reference.GetJournal(m_iPlayerFaction).GetTaskState("P3_MAP016_Deathmen", "P3_MAP016_Deathmen_TalkToKatras") != Completed &&
				m_Reference.GetJournal(m_iPlayerFaction).IsTaskActive("P3_MAP016_Deathmen", "P3_MAP016_Deathmen_TalkToKatras") || 
				m_Reference.GetJournal(m_iPlayerFaction).GetTaskState("P3_MAP016_Falcons", "P3_MAP016_Falcons_TalkToKiroy") != Completed &&
				m_Reference.GetJournal(m_iPlayerFaction).IsTaskActive("P3_MAP016_Falcons", "P3_MAP016_Falcons_TalkToKiroy"))
			{
				FireNextStep();
			}	

			// savegame comp
			if(m_bStartedRTSpart)
			{
				EnableWorldMap(false);
				m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).ApplyCondition(27, -1);
			}
			
			// savegame comp
			if(m_Reference.GetJournal(m_iPlayerFaction).GetTaskState("P3_Q4_GetStones", "P3_Q4_GetSecondStone") == Completed) 
			{
				if(m_bChoiceAonirsDeathman)
				{
					TransferRTS(0, 2);
				}
				else
				{
					TransferRTS(0, 1);
				}
				EnableWorldMap( true );
			}
		}
		// savegame comp
		if ( m_Reference.GetWorld().GetCurrentMapSetting( m_Reference.GetMapId() ) == 1 )
		{
			if(m_Reference.GetJournal(m_iPlayerFaction).GetTaskState("P4_MAP016_StrengthenHumans", "P4_MAP016_DefeatDeathmenHQ") == Completed )
			{
				TransferRTS(0, kFactionNeutral);
			}
		}
	
	}

	// level has been entered
	void OnEntered () override
	{
		printImportant("V E R S I O N : LevelBase: "+m_LevelBaseVersion+", Map Script: " + m_stgVersion+" (Level was entered)");
		LevelBase::OnEntered();
	}

	// -------------------------------------------------------------------------------------------------------------------
	// ---- CUSTOM FUNCTIONS AND EVENTS ----------------------------------------------------------------------------------
	/*
		All events are registered in this function.
		There soudn't be any variable declarations or initialitations
	*/
	void InitEvents ()
	{
		// Dwarf Brothers
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnDwarfBrothers_01_attack), "AB1_DwarfBrotherAttack_01", m_iPlayerFaction);
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.OnDwarfBrothers_01_saved), m_Reference.GetCreaturesFromSpawn("Undead_DB_Attack_01"), false, "");
		m_Reference.RegisterCreatureEventByIndividual(RevivePeriodEnded, TOnCreatureEvent(@this.OnDwarfBrothers_01_killed), m_stgCrt_DwarfBrother_01, "" );

		// Pressure Plates. Wuhuuu
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnStepOnPressurePlate_01), (m_stgTreasure_PlateTrg+1), m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnStepOnPressurePlate_02), (m_stgTreasure_PlateTrg+2), m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnStepOnPressurePlate_03), (m_stgTreasure_PlateTrg+3), m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnStepOnPressurePlate_04), (m_stgTreasure_PlateTrg+4), m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AllOutside, TOnHeroPartyEvent(@this.OnStepOffPressurePlate_01), (m_stgTreasure_PlateTrg+1), m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AllOutside, TOnHeroPartyEvent(@this.OnStepOffPressurePlate_02), (m_stgTreasure_PlateTrg+2), m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AllOutside, TOnHeroPartyEvent(@this.OnStepOffPressurePlate_03), (m_stgTreasure_PlateTrg+3), m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AllOutside, TOnHeroPartyEvent(@this.OnStepOffPressurePlate_04), (m_stgTreasure_PlateTrg+4), m_iPlayerFaction);

		m_Reference.RegisterHeroPartyEvent(Interact, TOnHeroPartyEvent(@this.AB1_OnInteract_PhoenixShrine), "AB_Artifact_Phoenix_Shrine", m_iPlayerFaction);
	}

	// for the first map setting
	void InitEvents_0 ()
	{
		// --- All the map intro stuff ---
		m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnSO_Environment_01), m_stgAvatar, "SO_Waterfall" );
		m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnSO_DeadScout), m_stgAvatar, "SO_DeadScout" );
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnSO_WhyUndead), "SO_WhyUndead", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnStartStagedBattle), "SO_FightGoingOn_(start)", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnSO_FightGoingOn), "SO_FightGoingOn", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnSO_DontWalkIn), "SO_DontWalkIntoWave", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnSO_DontWalkIntoFort), "SO_DontWalkIntoTheFort", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnSO_HelpThePeople), "SO_HelpPeople_02", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnSO_Trebuchet), "SO_Trebuchet", m_iPlayerFaction);

		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnSO_StartStart), m_stgTrg_SoldierTalk, m_iPlayerFaction);

		// Traitor
		m_Reference.RegisterCreatureEventByIndividual(Killed, TOnCreatureEvent(@this.OnTraitorKilled), m_stgCrt_FalconTraitor, "");
		m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnTraitorSaved), m_stgCrt_FalconTraitor, m_stgTrg_DeathmanCamp );
		m_Reference.RegisterInventoryEvent(Added, TOnInventoryEvent(@this.OnTraitorGotLoot), "AB1_TraitorsProof", m_iPlayerFaction);

		// --- Events for choosing the faction ---
		//m_Reference.RegisterCreatureEventByIndividual(LeftArea, TOnCreatureEvent(@this.OnLeaveDebateCircle), m_stgAvatar, m_stgTrg_DebateArea );
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnShoutoutBuiltSectorHere_Falcons), m_stgTrg_SectorCamp_Falcons, m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnShoutoutBuiltSectorHere_Deathmen), m_stgTrg_SectorCamp_Deathmen, m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(Interact, TOnHeroPartyEvent(@this.OnStartRTS_Falcons), m_stgObj_Camp_Falcons, m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(Interact, TOnHeroPartyEvent(@this.OnStartRTS_Deathman), m_stgObj_Camp_Deathman, m_iPlayerFaction);

		// Step 1: RTS and resources
		m_Reference.RegisterBuildingEventByDescription(Placed, TOnBuildingEvent(@this.OnEconomyThere_Wood), "H_Woodcutter", m_iPlayerFaction );
		m_Reference.RegisterBuildingEventByDescription(Completed, TOnBuildingEvent(@this.OnEconomyThere_Hunting), "H_Fishing_Cabin", m_iPlayerFaction );

		// Step 2: Stronger waves and stuff
		m_Reference.RegisterBuildingEventByDescription(Placed, TOnBuildingEvent(@this.OnNextStep), "H_Capital_I", m_iPlayerFaction );
		m_Reference.RegisterBuildingEventByDescription(Placed, TOnBuildingEvent(@this.OnNextStep), "E_Capital_I", m_iPlayerFaction );
		m_Reference.RegisterBuildingEventByDescription(Placed, TOnBuildingEvent(@this.OnNextStep), "O_Capital_I", m_iPlayerFaction );

		// --- Friendly Creep Waves ---
		m_Reference.RegisterBuildingCreatureEventByIndividual(CreaturesSpawned, TOnBuildingCreatureEvent(@this.OnSendAttack), m_stgBldg_WaveFalcons);
		m_Reference.RegisterBuildingCreatureEventByIndividual(CreaturesSpawned, TOnBuildingCreatureEvent(@this.OnSendAttack), m_stgBldg_WaveFalcons_BigWave);
		m_Reference.RegisterBuildingCreatureEventByIndividual(CreaturesSpawned, TOnBuildingCreatureEvent(@this.OnSendAttack), m_stgBldg_WaveDeathmen);
		m_Reference.RegisterBuildingCreatureEventByIndividual(CreaturesSpawned, TOnBuildingCreatureEvent(@this.OnSendAttack), m_stgBldg_WaveDeathmen_BigWave);

		// Waves from the Undead
		m_Reference.RegisterBuildingCreatureEventByIndividual(CreaturesSpawned, TOnBuildingCreatureEvent(@this.OnCreepWave_AttackPlayer), m_stgBldg_Wave_PlayerBase);
		m_Reference.RegisterBuildingCreatureEventByIndividual(CreaturesSpawned, TOnBuildingCreatureEvent(@this.OnCreepWave_AttackTrebuchet), m_stgBldg_Wave_Trebuchet);

		// --- Trebuchet ---
 		m_Reference.RegisterSectorEvent(OwnerChanged, TOnSectorEvent(@this.OnChangeOwnerTrebuchet), m_stgSectTrebuchet);
 		m_Reference.RegisterHeroPartyEvent(AnyInsideSector, TOnHeroPartyEvent(@this.AB1_OnEnteredSector_CatapultSector), "m_stgSectTrebuchet", m_iPlayerFaction);


		// Andra Ritual Site / (old: necromancer)
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnSO_NecromancerAttack), "SO_Necromancer", m_iPlayerFaction);
		m_Reference.RegisterBuildingEventByIndividual(Charged, TOnBuildingEvent(@this.OnSacrificedUnits), "AB1_Summoning_Staff");

		// Seer events
		m_Reference.RegisterHeroPartyEvent(Interact, TOnHeroPartyEvent(@this.AB1_OnInteractSeerBones), "AB1_SeerBones", m_iPlayerFaction);


		m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).SetHelpUpAllowed(true);
		m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).EnableTrade(false);
		m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).EnableTrade(false);
		m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).SetHelpUpAllowed(true);
		m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).SetHelpUpAllowed(true);

		// final stuff
		m_Reference.RegisterBuildingEventByIndividual (Destroyed, TOnBuildingEvent(@this.AB1_OnDestroyed_UndeadTower), "S_AB1_UndeadTower_01");
		m_Reference.RegisterBuildingEventByIndividual (Destroyed, TOnBuildingEvent(@this.AB1_OnDestroyed_UndeadTower), "S_AB1_UndeadTower_02");
		m_Reference.RegisterBuildingEventByIndividual (Destroyed, TOnBuildingEvent(@this.AB1_OnDestroyed_UndeadTower), "S_AB1_UndeadTower_03");

		// cleanse undead quest
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB1_OnKilled_UndeadFortress), m_Reference.GetCreaturesFromSpawn("Undead_Fort_02b"), true, "");
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB1_OnKilled_UndeadFortress), m_Reference.GetCreaturesFromSpawn("Undead_Fort_03"), true, "");
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB1_OnKilled_UndeadFortress), m_Reference.GetCreaturesFromSpawn("Undead_Fort_04a"), true, "");
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB1_OnKilled_UndeadFortress), m_Reference.GetCreaturesFromSpawn("Undead_Fort_04b"), true, "");
	}

	// for the second map setting
	void InitEvents_1 ()
	{
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnAngarWarning), "AB2_AngarWarning", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnAmbush), "AB2_AmbushTrigger", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnSeeCapturedMentalist), "AB2_TriggerCaptiveSO", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnEnterCamp), "AB2_CampDestroyedSO", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnDiscoverFalconSideCamp), "AB2_DiscoverFalconSideOutpost", m_iPlayerFaction);
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnDiscoverDeathmenSideCamp), "AB2_DiscoverDeathmenSideOutpost", m_iPlayerFaction);

		//m_Reference.RegisterSectorEvent(OwnerChanged, TOnSectorEvent(@this.OnPlayerCapturedCatapult), "Sector_Trebuchet");
		m_Reference.RegisterSectorEvent(OwnerChanged, TOnSectorEvent(@this.AB2_OnOwnerChanged_TrebuchetSector), m_stgSectTrebuchet);
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB2_OnKilledEngineerGuards), m_Reference.GetCreaturesFromSpawn("AB2_DeathMenEngineerGuards"), false, "");
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnEncounterEngineer), "AB2_EncounterEngineer", m_iPlayerFaction);

		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.OnKilledNecromancers), m_Reference.GetCreaturesFromSpawn("AB2_Necromancers"), false, "");

		// falcon camp
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.AB2_OnAnyInside_TrgFalconSurvivor), "AB2_Trg_FalconSurvivor", m_iPlayerFaction);

		array<string> ArchfireSpawns = {"AB2_ArchfireMonsters", "AB2_ArchfireMonsters2"};
		array<string> ArchfireCreaturesNames = GetCreaturesFromSpawns_stg( ArchfireSpawns );
		for(uint i = 0; i < ArchfireCreaturesNames.size(); i++)
		{
			m_Reference.RegisterHeroPartyEvent(AnyCanSee, TOnHeroPartyEvent(@this.OnSeeArchfireBeast), ArchfireCreaturesNames[i], m_iPlayerFaction);
		}

		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.AB2_OnEnterMentalistCamp), "AB2_EnterMentalistCamp", m_iPlayerFaction);

		

		m_Reference.RegisterBuildingEventByIndividual(Charged, TOnBuildingEvent(@this.AB2_OnChargedHoleInTheWall), "AB2_HoleInTheWall");

		m_Reference.RegisterBuildingEventByIndividual(Destroyed, TOnBuildingEvent(@this.AB2_OnDestroyDeathmenSector), "AB2_DeathmenMainHQ_Sector7");
		m_Reference.RegisterBuildingEventByIndividual(Destroyed, TOnBuildingEvent(@this.AB2_OnDestroyDeathmenSector), "AB2_DeathmenMainHQ_Sector6");
		print("Sector 6 has " + m_Reference.GetSectorByName("Sector_06").GetBuildings().size() + " Buildings");
		for(uint i = 0; i < m_Reference.GetSectorByName("Sector_06").GetBuildings().size(); i++)
		{
			print("Buildings in Sector 06: " + i + ".   " + m_Reference.GetSectorByName("Sector_06").GetBuildings()[i].GetDescriptionName());
			m_Reference.RegisterBuildingEventByIndividual(Destroyed, TOnBuildingEvent(@this.AB2_OnDestroyFirstDeathmenBuilding), m_Reference.GetSectorByName("Sector_06").GetBuildings()[i].GetDescriptionName()); //First pre placed Building in the Main Deathmen Sectors
		}
	}

	// for the third map setting
	void InitEvents_2 ()
	{

	}

	/*
		Is called right at the beginning of the level. Should only be used to start a dialoge, or directly stage an event and so on.
	*/
	void InitCommon ()
	{
		// Anja: bug-fix for the invisible elf tower .... ? 
		m_Reference.GetBuildingByName("Entity_Elf_Wooden_Tower_75281").SetSelectable( false ); // this should be enough
		m_Reference.GetBuildingByName("Entity_Elf_Wooden_Tower_75281").SetAttackable( false );
		m_Reference.GetLogicObjectByName("Entity_Elf_Wooden_Tower_75281").SetSelectable( false ); // this should be enough
		

		m_Reference.GetLogicObjectByName("AB_Artifact_Phoenix_LootChest").Enable(false);
		m_Reference.GetLogicObjectByName("AB_Artifact_Phoenix_LootChest_FX").Enable(false);
		m_Reference.GetLogicObjectByName("AB_Artifact_Phoenix_Shrine").SetInspectionText(16000);

		// Dwarf Brothers
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_Caravan ).SetAttackable( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_Caravan ).AllowAttacks( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_Caravan ).SetImmovable( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_01 ).SetAttackable( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_02 ).SetAttackable( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_03 ).SetAttackable( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_01 ).AllowAttacks( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_02 ).AllowAttacks( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_03 ).AllowAttacks( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_Caravan ).SetImmovable( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_01 ).SetImmovable( true );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_02 ).SetImmovable( true );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_03 ).SetImmovable( true );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_01 ).EnablePOIVisitingBehavior( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_02 ).EnablePOIVisitingBehavior( false );
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_03 ).EnablePOIVisitingBehavior( false );
		m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Enable( false );
		m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Enable( false );


		// INSPECTING
		m_Reference.GetLogicObjectByName("Inspect_Grave_01").SetInspectionText(16010);
		m_Reference.GetLogicObjectByName("Inspect_Grave_02").SetInspectionText(16010);
		m_Reference.GetLogicObjectByName("Inspect_Grave_03").SetInspectionText(16010);
		m_Reference.GetLogicObjectByName("Inspect_Grave_04").SetInspectionText(16010);
		m_Reference.GetLogicObjectByName("Inspect_Grave_05").SetInspectionText(16010);
		m_Reference.GetLogicObjectByName("Inspect_Grave_06").SetInspectionText(16010);
		m_Reference.GetLogicObjectByName("Inspect_Grave_07").SetInspectionText(16010);

		m_Reference.GetLogicObjectByName(m_stgTreasure_PlateEffect+1).SetInspectionText(0);
		m_Reference.GetLogicObjectByName(m_stgTreasure_PlateEffect+2).SetInspectionText(0);
		m_Reference.GetLogicObjectByName(m_stgTreasure_PlateEffect+3).SetInspectionText(0);
		m_Reference.GetLogicObjectByName(m_stgTreasure_PlateEffect+4).SetInspectionText(0);

		m_Reference.SetFactionRelation(kFactionMob, m_iFaction_Neutral, Hostile);

		// Inspections
		//m_Reference.GetLogicObjectByName("Inspect_Grave_00").SetInspectionText(0);
		m_Reference.GetLogicObjectByName("AB_Inspect_DesertPits").SetInspectionText(16001);
		m_Reference.GetLogicObjectByName("AB_Inspect_Templewall_02").SetInspectionText(16002);
		m_Reference.GetLogicObjectByName("AB_Inspect_Templewall").SetInspectionText(16003);
		m_Reference.GetLogicObjectByName("AB_Inspect_BuriedGoods").SetInspectionText(16004);
		m_Reference.GetLogicObjectByName("AB_Inspect_CanopyConstruct").SetInspectionText(16005);
		m_Reference.GetLogicObjectByName("AB1_Inspect_DeadIronFalconVillager").SetInspectionText(16006);
		m_Reference.GetLogicObjectByName("AB1_Inspect_DeadTroup").SetInspectionText(16007);
		m_Reference.GetLogicObjectByName("AB2_DeadIronFalconRemains").SetInspectionText(16008);
		m_Reference.GetLogicObjectByName("Inspect_GraveOpen_01").SetInspectionText(16011);
		m_Reference.GetLogicObjectByName("Inspect_GraveOpen_02").SetInspectionText(16011);
		m_Reference.GetLogicObjectByName("Inspect_GraveOpen_03").SetInspectionText(16011);
		m_Reference.GetLogicObjectByName("Inspect_GraveOpen_04").SetInspectionText(16011);
		m_Reference.GetLogicObjectByName("Inspect_GraveOpen_05").SetInspectionText(16011);
		m_Reference.GetLogicObjectByName("Inspect_GraveOpen_06").SetInspectionText(16011);
		m_Reference.GetLogicObjectByName("Inspect_GraveOpen_07").SetInspectionText(16011);


		// the Pressure Plate thingy
		m_Reference.GetLogicObjectByName( m_stgSecretTreasure ).Enable( false );

		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + 1 ).Enable( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + 2 ).Enable( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + 3 ).Enable( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + 4 ).Enable( false );

		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + 1 ).Enable( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + 2 ).Enable( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + 3 ).Enable( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + 4 ).Enable( false );

		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + 1 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + 2 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + 3 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + 4 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + 1 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + 2 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + 3 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + 4 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_Plate + 1 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_Plate + 2 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_Plate + 3 ).SetInteractive( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_Plate + 4 ).SetInteractive( false );

		// disable light at ritual thing
		m_Reference.EnableLight("AB_KiroyCast_Light_1", false);
		m_Reference.EnableLight("AB_KiroyCast_Light_2", false);
	}

	// for the first map version
	void InitCommon_0 ()
	{
		AB1_CutsceneMapIntro();

		//m_Reference.GetCreatureByName("AB1_Trebuchet").ApplyCondition(10201, -1);

		//m_Reference.GetLogicObjectByName("Godstone_Start").SetFaction(m_iPlayerFaction);
		m_Reference.GetBuildingByName("Godstone_Start").SetFaction(m_iPlayerFaction);

		for (int _iTmp = 1; _iTmp <= m_iSummoningEffects ;_iTmp++)
		{
			m_Reference.GetLogicObjectByName( m_stgObjSummoningEffect + _iTmp ).Enable(false);
			m_Reference.GetLogicObjectByName( m_stgObjSummoningEffect + _iTmp ).SetInteractive(false);
		}

		// Faction buildings
		m_Reference.GetBuildingByName(m_stgBldg_WaveFalcons_Connect).SetInteractive(false);
		m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen_Connect).SetInteractive(false);

		// Trebuchet
		m_Reference.GetCreatureByName(m_stgCrtTrebuchet).SetImmovable(true);
		m_Reference.GetCreatureByName(m_stgCrtTrebuchet).AllowAttacks(false);
		m_Reference.GetCreatureByName(m_stgCrtTrebuchet).SetAttackable(false);
		m_Reference.GetCreatureByName(m_stgCrtTrebuchet).SetPlayerControllable(false);

		m_Reference.GetLogicObjectByName(m_stgObj_Camp_Falcons).SetInteractive(false);
		m_Reference.GetLogicObjectByName(m_stgObj_Camp_Deathman).SetInteractive(false);
		m_Reference.GetCreatureByName( m_stgCrt_Soldier_Deathmen ).EnablePOIVisitingBehavior( false );
		m_Reference.GetCreatureByName( m_stgCrt_FalconTraitor ).EnablePOIVisitingBehavior( false );
		m_Reference.GetCreatureByName( m_stgCrt_KiroyTeranor ).EnablePOIVisitingBehavior( false );
		m_Reference.GetCreatureByName( m_stgCrt_ArenorArandir ).EnablePOIVisitingBehavior( false );

		// Spawning Miniboss
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).SetSelectable(false);

		// setting up the undead sector.
		m_Reference.GetSectorByName("Sector_Undead").SetOwner(kFactionMob);

		// All the spawn buildings.
		// there are quite some, being lazy here, and streamlining them via an array and for loop. Dont want to copy paste these lines
		array <string> _arrStg_BldgsSpawners;
		_arrStg_BldgsSpawners.insertLast(m_stgBldg_Wave_PlayerBase);
		_arrStg_BldgsSpawners.insertLast(m_stgBldg_Wave_Trebuchet);
		_arrStg_BldgsSpawners.insertLast(m_stgBldg_WaveFalcons);
		_arrStg_BldgsSpawners.insertLast(m_stgBldg_WaveFalcons_BigWave);
		_arrStg_BldgsSpawners.insertLast(m_stgBldg_WaveFalcons_Connect);
		_arrStg_BldgsSpawners.insertLast(m_stgBldg_WaveDeathmen);
		_arrStg_BldgsSpawners.insertLast(m_stgBldg_WaveDeathmen_BigWave);
		_arrStg_BldgsSpawners.insertLast(m_stgBldg_WaveDeathmen_Connect);
		_arrStg_BldgsSpawners.insertLast(m_stgBldg_TrebuchetAttack);
		m_Reference.GetBuildingByName("DeathmenWaves_MainWave").Enable(false);
		m_Reference.GetBuildingByName("FalconWaves_MainWave").Enable(false);

		for ( uint _iTmp = 0 ; _iTmp < _arrStg_BldgsSpawners.length() ; _iTmp++)
		{
			Building _BlgdTmp = m_Reference.GetBuildingByName(_arrStg_BldgsSpawners[_iTmp]);
			_BlgdTmp.SetInteractive(false);

			if ( _arrStg_BldgsSpawners[_iTmp] != m_stgBldg_TrebuchetAttack)
				_BlgdTmp.SetAttackable(false);

			if (_arrStg_BldgsSpawners[_iTmp] == m_stgBldg_Wave_PlayerBase || _arrStg_BldgsSpawners[_iTmp] == m_stgBldg_Wave_Trebuchet || _arrStg_BldgsSpawners[_iTmp] == m_stgBldg_TrebuchetAttack)
			{
				_BlgdTmp.Enable(false);
				_BlgdTmp.SetFaction(kFactionMob);
			}
		}

		m_Reference.GetCreatureByName( m_stgCrt_KiroyTeranor ).SetImmovable( true );
		m_Reference.GetCreatureByName( m_stgCrt_ArenorArandir ).SetImmovable( true );
		m_Reference.GetCreatureByName( m_stgCrt_KaylenVirTrath ).SetImmovable( true );
		m_Reference.GetCreatureByName( m_stgCrt_FalconTraitor ).SetImmovable( true );

		//m_Reference.GetCreatureByName( m_stgCrt_KiroyTeranor ).SetHelpUpAllowed( true );
		//m_Reference.GetCreatureByName( m_stgCrt_KiroyTeranor ).SetImmortal( true );

		m_Reference.GetCreatureByName( m_stgCrt_ArenorArandir ).SetHelpUpAllowed( true );
		m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).SetAutoRevive(true);

		m_Reference.GetCreatureByName( "Katras" ).SetHelpUpAllowed( true );
		m_Reference.GetCreatureByName("Katras").SetAutoRevive(true);

		m_Reference.GetCreatureByName( m_stgCrt_DeadScout ).SetImmovable( true );
		m_Reference.GetCreatureByName( m_stgCrt_DeadScout ).SetPreserveBody(true);

		m_Reference.GetCreatureByName( m_stgCrt_Soldier_Deathmen).SetFaction( m_iFaction_Deathman );
		m_Reference.GetCreatureByName( m_stgCrt_Soldier_Deathmen).SetImmovable( true );

		m_Reference.GetCreatureByName( m_stgCrt_ScoutDeathman ).EnablePOIVisitingBehavior( false );
		m_Reference.GetCreatureByName( m_stgCrt_ScoutDeathman ).SetImmovable( true );

		m_Reference.GetCreatureByName( m_stgCrt_ScoutFalcons ).EnablePOIVisitingBehavior( false );
		m_Reference.GetCreatureByName( m_stgCrt_ScoutFalcons ).SetImmovable( true );

		array <Creature>@ _entPatrol = m_Reference.GetCreaturesFromSpawn( m_stgSpawn_DeathmanPatrol );
		for ( uint _iTmp = 0 ; _iTmp < _entPatrol.length() ; _iTmp++ )
		{
			_entPatrol[_iTmp].EnablePOIVisitingBehavior ( false );
		}


		m_Reference.BlockNavMesh("AB1_PhysicBlocker_KiroyCamp", true);

		// enable spawns for undead so that they kill the first waves of falcons/deathmen
		//m_Reference.ActivateSpawn( m_stgSpawn_StagedBattle_UD, true);
		//m_Reference.ActivateSpawn( m_stdSpawn_UndeadFortBoss, true);

		// disable barrierr groups
		m_Reference.EnableGroup("AB1_KiroyCastStart", false);
		m_Reference.EnableGroup("AB1_KiroyCastOnce", false);
		m_Reference.EnableGroup("AB1_KiroyCastEnd", false);

		// disable fial darg tomb entrance physical blocker
		m_Reference.BlockNavMesh( "AB_FialDargTomb_PhysicBlocker", false );

		// prepare trebuchet building
		m_Reference.GetBuildingByName(m_stgBldg_Trebuchet).ProhibitProduction(Spell, "AB1_TrebuchetShot", true);
		m_Reference.RegisterBuildingEventByIndividual(ProductionFinished, TOnBuildingExtendedEvent(@this.AB1_OnProductionFinished_Trebuchet), m_stgBldg_Trebuchet);

		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.AB1_OnAnyInside_EnterFinalAreaFinishGatherStrength), "EnterFinalArea", m_iPlayerFaction);
		// let katras/arenor join the fight when entering fortress
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.AB1_OnAnyInside_FinalAreaAllyJoin), "EnterFinalArea", m_iPlayerFaction);

		// final stuff
		// set towers enemys and register their events
		m_Reference.GetBuildingByName("S_AB1_UndeadTower_02").SetFaction(kFactionMob);
		m_Reference.GetBuildingByName("S_AB1_UndeadTower_01").SetFaction(kFactionMob);
		m_Reference.GetBuildingByName("S_AB1_UndeadTower_03").SetFaction(kFactionMob);

	}

	// for the second map version
	void InitCommon_1 ()
	{
		AB2_CutsceneMapIntro();
		m_Reference.SetFactionRelation(m_iFaction_Deathman, m_iPlayerFaction, Hostile);
		m_Reference.SetFactionRelation(m_iFaction_Deathman, m_iFaction_Mentalists, Neutral);
		m_Reference.SetFactionRelation(m_iFaction_Mentalists, m_iPlayerFaction, Neutral);
		m_Reference.SetFactionRelation(m_iFaction_DeathmenSiege, kFactionMob, Neutral);
		m_Reference.SetFactionRelation(m_iFaction_DeathmenRTS, kFactionMob, Neutral);

		m_Reference.GetSectorByName("Sector_07").SetOwner(m_iFaction_DeathmenRTS);
		m_Reference.GetSectorByName("Sector_07").CreateCapital(m_iFaction_DeathmenRTS, false, true);
		m_Reference.GetSectorByName("Sector_06").SetOwner(m_iFaction_DeathmenRTS);
		m_Reference.GetSectorByName("Sector_06").CreateCapital(m_iFaction_DeathmenRTS, false, true);


		m_Reference.GetSectorByName("Sector_AonirsDeathman").SetOwner(m_iFaction_Mentalists);
		m_Reference.GetSectorByName("Sector_AonirsDeathman").CreateCapital(m_iFaction_Mentalists, false, true);

		m_Reference.GetCreatureByName("AB2_Engineer").EnableTrade(false);

		m_Reference.BlockNavMesh("AB2_MainGate_Blocker", true);
		m_Reference.BlockNavMesh("AB2_WesternGate_Blocker", true);

		m_Reference.GetCreatureByName("Arenor_Arandir").EnableTrade(false);

		// disable barrierr groups
		m_Reference.EnableGroup("AB1_KiroyCastStart", false);
		m_Reference.EnableGroup("AB1_KiroyCastOnce", false);
		//m_Reference.EnableGroup("AB1_KiroyCastEnd", false);

		m_Reference.GetCreatureByName("AB2_Mentalist_Captive").PlayAnimation(m_Reference, BoundOnKneesLoop , -1, 999999999, true, false, Entity());

		// engineer quest
		m_Reference.GetLogicObjectByName("AB2_ToolBox").Enable(false);

		// reset godstones
		m_Reference.GetBuildingByName("Godstone_Falcons").SetFaction(kFactionNeutral);
		m_Reference.GetBuildingByName("Godstone_Deathman").SetFaction(kFactionNeutral);
		m_Reference.GetBuildingByName("Godstone_North").SetFaction(kFactionNeutral);
		m_Reference.GetBuildingByName("Godstone_Palmen").SetFaction(kFactionNeutral);
		m_Reference.GetBuildingByName("Entity_Godstone_71806").SetFaction(kFactionNeutral);

	}

	// for the third map version
	void InitCommon_2 ()
	{

	}

	// -------------------------------------------------------------------------------------------------------------------
	// ---- E V E N T S --------------------------------------------------------------------------------------------------
	// -------------------------------------------------------------------------------------------------------------------

	// first meeting with kiroy, katras and arenor
	bool AB1_OnAnyOutsideAggroRange_KiroyStartTalking(Creature& in _Creature)
	{
		DialogShoutout( m_stgCrt_KiroyTeranor, m_stgCrt_KiroyTeranor, "AB1_SO_Wait", true );
		m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_KiroyTeranor, "AB1_FalconQuest", true);

		return true;
	}

	bool AB1_OnAnyInside_EnterFinalAreaFinishGatherStrength(Creature& in _Creature)
	{
		if(m_bChoiceAonirsDeathman)
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Deathmen", "P3_MAP016_Deathmen_GatherStrength", Completed );
		}
		else
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Falcons", "P3_MAP016_Falcons_GatherStrength", Completed );
		}

		return true;
	}

	// traitor quest

	bool AB1_OnAnyInside_FindTraitorSO(Creature& in _Creature)
	{
		DialogShoutout(m_stgCrt_FalconTraitor, m_stgCrt_FalconTraitor, "AB1_FoundTraitor", false);
		return true;
	}

	bool AB1_OnAnyInside_FalconQuestComplete(Creature& in _Creature)
	{
		DialogShoutoutReveal(m_stgCrt_KiroyTeranor, m_stgCrt_KiroyTeranor, "AB1_SO_DiscussionWithArenor");
		m_Reference.GetCreatureByName( m_stgCrt_KiroyTeranor ).EnablePOIVisitingBehavior( false );
		m_Reference.GetCreatureByName( m_stgCrt_KiroyTeranor ).Teleport(m_Reference, m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir ));
		return true;
	}

// --- Version 1 ---
	bool OnStepOnPressurePlate_01(Creature& in _Creature)
	{
		StepOnPressurePlate( 1 );
		return false;
	}

	bool OnStepOnPressurePlate_02(Creature& in _Creature)
	{
		StepOnPressurePlate( 2 );
		return false;
	}

	bool OnStepOnPressurePlate_03(Creature& in _Creature)
	{
		StepOnPressurePlate( 3 );
		return false;
	}

	bool OnStepOnPressurePlate_04(Creature& in _Creature)
	{
		StepOnPressurePlate( 4 );
			return false;
	}

	bool OnStepOffPressurePlate_01(Creature& in _Creature)
	{
		StepOffPressurePlate( 1 );
		return false;
	}
	bool OnStepOffPressurePlate_02(Creature& in _Creature)
	{
		StepOffPressurePlate( 2 );
		return false;
	}
	bool OnStepOffPressurePlate_03(Creature& in _Creature)
	{
		StepOffPressurePlate( 3 );
		return false;
	}
	bool OnStepOffPressurePlate_04(Creature& in _Creature)
	{
		StepOffPressurePlate( 4 );
		return false;
	}

	bool StepOnPressurePlate ( int _iNmbr )
	{
		print("[SCRIPT] - NOTE: Stepped on the pressure plate "+_iNmbr+".");

		if (m_Reference.GetLogicObjectByName( m_stgSecretTreasure ).IsEnabled() == true)
			return true;

		// toggle the pressure plate
		m_Reference.GetLogicObjectByName( m_stgTreasure_Plate + _iNmbr ).SetToggleState( true );

		// enable the fires
		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + _iNmbr ).Enable( true );
		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + _iNmbr ).Enable( true );

		// check if all pressure plates are pressed
		if ( m_Reference.GetLogicObjectByName( m_stgTreasure_Plate+1 ).GetToggleState() == true
			&& m_Reference.GetLogicObjectByName( m_stgTreasure_Plate+2 ).GetToggleState() == true
			&& m_Reference.GetLogicObjectByName( m_stgTreasure_Plate+3 ).GetToggleState() == true
			&& m_Reference.GetLogicObjectByName( m_stgTreasure_Plate+4 ).GetToggleState() == true
		)
		{
			m_Reference.GetLogicObjectByName( m_stgSecretTreasure ).Enable( true );
			MarkerLogic( "PosSecretTreasure", "POIMarker", "Show", true, m_iPlayerFaction );

			return true;
		}

		return false;
	}
	bool StepOffPressurePlate ( int _iNmbr )
	{
		print("[SCRIPT] - NOTE: Stepped off the pressure plate "+_iNmbr+".");

		if (m_Reference.GetLogicObjectByName( m_stgSecretTreasure ).IsEnabled() == true)
			return true;

		// toggle the pressure plate
		m_Reference.GetLogicObjectByName( m_stgTreasure_Plate + _iNmbr ).SetToggleState( false );

		// enable the fires
		m_Reference.GetLogicObjectByName( m_stgTreasure_ChestEffect + _iNmbr ).Enable( false );
		m_Reference.GetLogicObjectByName( m_stgTreasure_PlateEffect + _iNmbr ).Enable( false );

		return false;
	}


	// Shoutout about the waterfall
	bool OnSO_Environment_01(Creature& in _Creature)
	{
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_WaterfallRemark", false);
		return true;
	}

	bool OnSO_DeadScout(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Meeting the DeadScout");
		DialogShoutout(m_stgCrt_DeadScout, m_stgCrt_DeadScout, "AB1_FirstContact", true);
		return true;
	}

	bool OnSO_WhyUndead(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Seeing the first undead -> Shoutout 'SO_UndeadAttack'");
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_UndeadAttack", true);
		return true;
	}

	bool AB2_OnAnyInside_TrgFalconSurvivor(Creature& in _Creature)
	{
		printNote("player walked near falcon survivor");
		DialogShoutoutWhenReady("AB2_Falcon_Survivor", "AB2_Falcon_Survivor", "AB2_08_FirstContact", true);
		return true;
	}

	bool OnStartStagedBattle(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: starting the semi-staged battle in the valley.");

		// spawn IF/DM soldiers for cinematic purposes
		m_Reference.ActivateSpawn( m_stgSpawn_StagedBattle_IF, true);
		m_Reference.ActivateSpawn( m_stgSpawn_StagedBattle_DM, true);
		m_Reference.ActivateSpawn( m_stgSpawn_StagedBattle_UD, true);

		// sending these spawns to each other for the attack
		CreatureGroup _crtUndead ( m_Reference );
		_crtUndead.Set( m_Reference.GetCreaturesFromSpawn( m_stgSpawn_StagedBattle_UD ) );
		CreatureGroup _crtFalcons ( m_Reference );
		_crtFalcons.Set( m_Reference.GetCreaturesFromSpawn( m_stgSpawn_StagedBattle_IF ) );
		CreatureGroup _crtDeathmen ( m_Reference );
		_crtDeathmen.Set( m_Reference.GetCreaturesFromSpawn( m_stgSpawn_StagedBattle_DM ) );

		_crtUndead.FollowPath(m_stgPath_Wave_PlayerBase, true, true);
		_crtFalcons.FollowPath(m_stgPath_AttackTheUndead, true, true);
		_crtDeathmen.FollowPath(m_stgPath_AttackTheUndead, true, true);


		// kill all units, when they are finished
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.OnKillStartingStageFight), _crtFalcons.GetCreatures(), false, "");
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.OnKillStartingStageFight), _crtDeathmen.GetCreatures(), false, "");

		// enablecreepstuff
		m_Reference.GetBuildingByName(m_stgBldg_Wave_PlayerBase).Enable(true);
		m_Reference.GetBuildingByName("DeathmenWaves_MainWave").Enable(true);
		m_Reference.GetBuildingByName("FalconWaves_MainWave").Enable(true);
		m_Reference.GetBuildingByName(m_stgBldg_WaveFalcons).Upgrade(m_Reference, m_stgBldgDes_Falcons_0, true); // activate the normal waves
		m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen).Upgrade(m_Reference, m_stgBldgDes_Deathmen_0, true);  // activate the normal waves

		return true;
	}
	bool OnKillStartingStageFight(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Ending the battle in the valley.");

		CreatureGroup _crtUndead ( m_Reference );
		CreatureGroup _crtFalcons ( m_Reference );
		CreatureGroup _crtDeathmen ( m_Reference );
		_crtUndead.Set( m_Reference.GetCreaturesFromSpawn( m_stgSpawn_StagedBattle_UD ) );
		_crtFalcons.Set( m_Reference.GetCreaturesFromSpawn( m_stgSpawn_StagedBattle_IF ) );
		_crtDeathmen.Set( m_Reference.GetCreaturesFromSpawn( m_stgSpawn_StagedBattle_DM ) );
		_crtUndead.Die(false);
		_crtFalcons.Die(false);
		_crtDeathmen.Die(false);
		return true;
	}

	bool OnSO_FightGoingOn(Creature& in _Creature)
	{
		if (_Creature == m_Reference.GetCreatureByName(m_stgAvatar))
			return false;

		print("[SCRIPT] - NOTE: Seeing the battle in the valley.");
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_Battle", false);
		MarkerLogic( "BattleCliff_Valley", "POIMarker", "Show", true, 0 );

		return true;
	}
	bool OnSO_DontWalkIn(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Seeing the battle in the valley.");
		if(!m_bStartedRTSpart)
		{
			DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_DontWalkIntoWave", false);
			return false;
		}
		return true;
	}
	bool OnSO_DontWalkIntoFort(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Seeing the battle in the valley.");
		if(!m_bStartedRTSpart)
		{
			DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_DontWalkIntoWave", false);
			return false;
		}
		return true;
	}
	bool OnSO_HelpThePeople(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Shoutout, when the player is about to stroll to the RTS parts.");
		if(!m_bStartedRTSpart)
		{
			DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_DontWalkIntoWave", false);
		}

		return true;
	}
	bool OnSO_Trebuchet(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Seeing the battle in the valley.");
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_TakeCatapult", true);

		return true;
	}

	bool OnSO_StartStart (Creature& in _Creature)
	{
		if (!m_Reference.GetHeroParty(m_iPlayerFaction).IsGathered( true ) ||  !HeroPartyReadyForDialogue() || _Creature.GetDescriptionName() != m_stgAvatar)
		{
			return false;
		}

		print("[SCRIPT] - NOTE: The Falcon Soldier comes and talkts to the player.");

		PlayerHeroControlDisable();
		m_Reference.GetHeroParty(m_iPlayerFaction).Stop();
		HeroPartyGatherAtAvatar();

		DialogTalkTo( m_stgCrt_KiroyTeranor, m_stgAvatar, m_stgCrt_KiroyTeranor, "AB1_FoundTheAvatar");
		return true;
	}

	bool OnSO_StartDebating (Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Arenor and Kaylen are starting their bedating.");
		DialogShoutout( m_stgCrt_Soldier_Falcons, m_stgCrt_Soldier_Falcons, "FalconQuest", false);

		return true;
	}
	
	bool OnTraitorKilled (Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Traitor was killed.");
		m_Reference.RegisterHeroPartyEvent(Interact, TOnHeroPartyEvent(@this.OnTraitorLoot), m_stgObj_Camp_Falcons, m_iPlayerFaction);

		m_Reference.GetCreatureByName(m_stgCrt_FalconTraitor).DropItem(m_Reference, "AB1_TraitorsProof", 1);

		return true;
	}

	bool OnTraitorSaved(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Traitor entered the Deathman camp and is now saved.");
		m_Reference.GetCreatureByName(m_stgCrt_FalconTraitor).SetFaction(m_iFaction_Deathman);
		m_Reference.GetCreatureByName( m_stgCrt_FalconTraitor ).EnablePOIVisitingBehavior( true );
		m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_KaylenVirTrath, "DeathmanSide", true);

		return true;
	}

	bool OnTraitorLoot (Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Traitor was looted.");
		HeroParty@ _HeroParty = m_Reference.GetHeroParty(m_iPlayerFaction);
		_HeroParty.AddItems("AB1_TraitorsProof", 1);

		return true;
	}
	bool OnTraitorGotLoot ( const uint8 _uPlayerFaction, const string& in _ItemName, const uint _ItemCount )
	{
		print("[SCRIPT] - NOTE: Party got Traitor Loot");
		m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_ArenorArandir, "IronFalconSide", true);
		return true;
	}

	// --- CHOOSING FACTION ----------------------------------------------------------------------------------------------
	bool OnShoutoutBuiltSectorHere_Falcons (Creature& in _Creature)
	{
		if (m_bChoiceIronFaloncs == false)
			return false;

		print("[SCRIPT] - NOTE: Shoutout:SO_JoinFalconOption");
		DialogShoutoutWhenReady(m_stgCrt_KiroyTeranor, m_stgCrt_KiroyTeranor, "AB1_SO_JoinFalconOption", false);

		return true;
	}
	bool OnShoutoutBuiltSectorHere_Deathmen (Creature& in _Creature)
	{
		if (m_bChoiceAonirsDeathman == false)
			return false;

		print("[SCRIPT] - NOTE: Shoutout: SO_JoinDeathmanOption");
		DialogShoutoutWhenReady(m_stgCrt_KaylenVirTrath, m_stgCrt_KaylenVirTrath, "AB1_SO_JoinDeathmanOption", false);

		return true;
	}

	bool OnStartRTS_Falcons(Creature& in _Creature)
	{
		// setting the camps to not interactable
		m_Reference.GetLogicObjectByName(m_stgObj_Camp_Falcons).SetInteractive(false);
		m_Reference.GetLogicObjectByName(m_stgObj_Camp_Deathman).SetInteractive(false);

		m_bChoosedIronFaloncs = true;
		m_bChoosedAonirsDeathman = false;
		m_bStartedRTSpart = true;
		EnableWorldMap(false);
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).ApplyCondition(27, -1);
		print("[SCRIPT] - NOTE: Start the RTS Gameplay - with the Falcon Camp. Falcon: "+m_bChoosedIronFaloncs+", Deathmen: "+m_bChoosedAonirsDeathman);

		// starting the RTS stuff
		m_Reference.GetSectorByName( m_stgSect_Falcons ).SetOwner( m_iPlayerFaction );
		m_Reference.GetSectorByName( m_stgSect_Falcons ).CreateCapital( m_iPlayerFaction, true, false );

		m_stgPos_SectorCamp = m_stgPos_SectorCamp_Falcons;
		m_Reference.SetFactionRelation( m_iPlayerFaction, m_iFaction_IronFalcons, Allied);
		m_Reference.GetSectorByName( m_stgSect_Deathman ).SetBlocked(true);

		// quest update
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Falcons", "P3_MAP016_Falcons_SetupCamp", Completed );
		m_Reference.RegisterBuildingEventByIndividual (Charged, TOnBuildingEvent(@this.AB1_OnCharged_WavesFalconConnect), m_stgBldg_WaveFalcons_Connect);
		return true;
	}

	bool OnStartRTS_Deathman(Creature& in _Creature)
	{
		// setting the camps to not interactable
		m_Reference.GetLogicObjectByName(m_stgObj_Camp_Falcons).SetInteractive(false);
		m_Reference.GetLogicObjectByName(m_stgObj_Camp_Deathman).SetInteractive(false);

		m_bChoosedAonirsDeathman = true;
		m_bChoosedIronFaloncs = false;
		m_bStartedRTSpart = true;
		EnableWorldMap(false);
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).ApplyCondition(27, -1);
		print("[SCRIPT] - NOTE: Start the RTS Gameplay - with the Deathman Camp."+m_bChoosedIronFaloncs+", Deathmen: "+m_bChoosedAonirsDeathman);

		// starting the RTS stuff
		m_Reference.GetSectorByName( m_stgSect_Deathman ).SetOwner( m_iPlayerFaction );
		m_Reference.GetSectorByName( m_stgSect_Deathman ).CreateCapital( m_iPlayerFaction, true, false );

		m_stgPos_SectorCamp = m_stgPos_SectorCamp_Deathmen;
		m_Reference.SetFactionRelation( m_iPlayerFaction, m_iFaction_Deathman, Allied);
		m_Reference.GetSectorByName( m_stgSect_Falcons ).SetBlocked(true);
		// quest update
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Deathmen", "P3_MAP016_Deathmen_SetupCamp", Completed );

		// activate the wave-stuff
		m_Reference.GetBuildingByName(m_stgBldg_Wave_PlayerBase).Enable(true); // enable the enemy spawner

		m_Reference.RegisterBuildingEventByIndividual (Charged, TOnBuildingEvent(@this.AB1_OnCharged_WavesDeathmenConnect), m_stgBldg_WaveDeathmen_Connect);
		return true;
	}

	bool AB1_OnCharged_WavesFalconConnect(Building& in _Building)
	{
		printNote("Upgraded falcon building!");
		if(m_bNumUpgraded == 0)
		{
			_Building.Upgrade(m_Reference, "S_AB1_FalconWaves_connectedTent_01", true);
			m_Reference.GetBuildingByName(m_stgBldg_WaveFalcons).Upgrade(m_Reference, m_stgBldgDes_Falcons_1, true);
			m_bNumUpgraded++;
		}
		else
		{
			m_Reference.GetBuildingByName(m_stgBldg_WaveFalcons).Upgrade(m_Reference, m_stgBldgDes_Falcons_2, true);
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ03_AllyProvisions", "P3_MAP016_SQ03_ProvideProvisions", Completed );
			return true;
		}

		return false;
	}

	bool AB1_OnCharged_WavesDeathmenConnect(Building& in _Building)
	{
		printNote("Upgraded deathmen building!");
		if(m_bNumUpgraded == 0)
		{
			_Building.Upgrade(m_Reference, "S_AB1_DeathmenWaves_connectedTent_01", true);
			m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen).Upgrade(m_Reference, m_stgBldgDes_Deathmen_1, true);
			m_bNumUpgraded++;
		}
		else
		{
			m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen).Upgrade(m_Reference, m_stgBldgDes_Deathmen_2, true);
			return true;
		}

		return false;
	}

	bool OnEconomyThere_Wood(Building&in _Building)
	{
		m_iWoodcutterNumber++;
		print("[SCRIPT] - NOTE: The player built woodcutter. Now we have "+m_iWoodcutterNumber+" of it.");

		return CheckEconomy();
	}

	bool OnEconomyThere_Hunting(Building&in _Building)
	{
		m_iFoodCabinNumber++;
		print("[SCRIPT] - NOTE: The player built hunting cabin. Now we have "+m_iFoodCabinNumber+" of it.");

		return CheckEconomy();
	}

	// --- WAVES ---------------------------------------------------------------------------------------------------------
	bool OnCreepWave_AttackPlayer (Building&in _Building, Creature[]&in _Creatures)
	{
		for (uint _iTmp = 0; _iTmp < _Creatures.length(); _iTmp++ )
		{
			_Creatures[_iTmp].FollowPath(m_Reference, m_stgPath_Wave_PlayerBase, true, true);
			m_arr_UndeadWave.insertLast(_Creatures[_iTmp].GetId());

			//m_Reference.RegisterCreatureEventByIndividual(FollowPathFinished  , TOnCreatureEvent(@this.OnWaveAttack), _Creatures[_iTmp], "");
		}
		printNote("Registering onwaveattack event here");
		m_Reference.RegisterCreatureEventByIndividuals(FollowPathFinished, TOnCreatureEvent(@this.OnWaveAttack), _Creatures, true, "" );

		return false;
	}

	bool OnWaveAttack(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Undead Wave - deciding where to go");
		if (m_stgPos_SectorCamp != "")
		{
			_Creature.GoTo( m_Reference, ( m_Reference.GetEntitiesByName(m_stgPos_SectorCamp)[0]), 10, 15, false );
			return true;
		}

		if (m_stgPos_SectorCamp == m_stgPos_SectorCamp_Falcons )
		{
			_Creature.GoTo( m_Reference, ( m_Reference.GetLogicObjectByName("Iron_Falcons_Outpost") ), 10, 15, false );
		}
		else
		{
			_Creature.GoTo( m_Reference, ( m_Reference.GetLogicObjectByName("Aonirs_Deathman_Outpost")), 10, 15, false );
		}

		return false;
	}

	bool OnCreepWave_AttackTrebuchet(Building&in _Building, Creature[]&in _Creatures)
	{
		for (uint _iTmp = 0; _iTmp < _Creatures.length(); _iTmp++ )
			_Creatures[_iTmp].FollowPath(m_Reference, m_stgPath_Wave_Trebuchet, true, true);

		return false;
	}

	// Waves from the Falcons and Deathmen
	bool OnSendAttack(Building&in _Building, Creature[]&in _Creatures)
	{
		print("[SCRIPT] - NOTE: sending Wave from " + _Building.GetDescriptionName());
		for (uint _iTmp = 0; _iTmp < _Creatures.length(); _iTmp++ )
		{
			_Creatures[_iTmp].FollowPath(m_Reference, m_stgPath_AttackTheUndead, true, true);
			m_arr_DeathmenFalconWave.insertLast(_Creatures[_iTmp].GetId());
		}

		// check, if there are other waves waiting - if yes, take them with
		if (m_arrWavesWaiting.length() > 0)
		{
			for (uint _iTmp = 0; _iTmp < m_arrWavesWaiting.length(); _iTmp++ )
				m_Reference.GetCreatureById(m_arrWavesWaiting[_iTmp]).FollowPath(m_Reference, m_stgPath_AttackTheUndead, true, true);

			for (uint _iTmp = 0; _iTmp < m_arrWavesWaiting.length(); _iTmp++ )
				m_arrWavesWaiting.removeLast(); // clear the ID-Array
		}

		return false;
	}

	bool OnSendAttack_Wait(Building&in _Building, Creature[]&in _Creatures)
	{
		print("[SCRIPT] - NOTE: waiting Wave from " + _Building.GetDescriptionName());
		// fill the ID-Array
		for (uint _iTmp = 0; _iTmp < _Creatures.length(); _iTmp++ )
		{
			m_arrWavesWaiting.insertLast( _Creatures[_iTmp].GetId() ); // fill the ID-Array
		}

		// let them walk to a good wait position
		CreatureGroup _crtWave ( m_Reference );
		_crtWave.Set( _Creatures );
		if (m_bChoosedIronFaloncs)
			_crtWave.Move( m_Reference.GetLogicObjectByName( m_stgPos_WaveWaitFalcons ) );
		else if (m_bChoosedAonirsDeathman)
			_crtWave.Move( m_Reference.GetLogicObjectByName( m_stgPos_WaveWaitDeathman ) );

		return false;
	}

	bool OnAttackSuccess(Creature&in _Creature)
	{
		print("[SCRIPT] - NOTE: WHOOP WHOOP - Game is won - stopping all the enemy and wave activity.");

		//Kill every Undead Left in the Fortress
		//Hacky but works
		CreatureGroup _crtGrpUndead ( m_Reference );
		_crtGrpUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_Fort_00") );
		_crtGrpUndead.Die();
		_crtGrpUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_Fort_01a") );
		_crtGrpUndead.Die();
		_crtGrpUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_Fort_01b") );
		_crtGrpUndead.Die();
		_crtGrpUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_Fort_02a") );
		_crtGrpUndead.Die();
		_crtGrpUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_Fort_02b") );
		_crtGrpUndead.Die();
		_crtGrpUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_Fort_03") );
		_crtGrpUndead.Die();
		_crtGrpUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_Fort_04b") );
		_crtGrpUndead.Die();
		_crtGrpUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_Fort_04a") );
		_crtGrpUndead.Die();

		m_Reference.GetSectorByName("Sector_Undead").SetOwner(m_iPlayerFaction);

		// destroy the miniboss building, and let the Necromancer flee from the map
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).Damage( Siege, m_Reference.GetBuildingByName(m_stgBldg_Summoning).GetMaxHP() );
		m_Reference.GetCreatureByName(m_stgCrt_Necromancer).Enable(false);
		// Stopping the waves
		m_Reference.GetBuildingByName( m_stgBldg_Wave_PlayerBase ).Enable(false);
		m_Reference.GetBuildingByName( m_stgBldg_Wave_Trebuchet ).Enable(false);
		m_Reference.GetBuildingByName(m_stgBldg_WaveFalcons).Upgrade(m_Reference, m_stgBldgDes_Falcons_0, true); // deactivate the Wave
		m_Reference.GetBuildingByName( m_stgBldg_WaveFalcons_BigWave ).AssignWorkers(0);

		m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen).Upgrade(m_Reference, m_stgBldgDes_Deathmen_0, true); // deactivate the Wave
		m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen_BigWave).AssignWorkers(0);

		//Revive HeroParty
		for(uint i = 0; i < m_Reference.GetHeroParty(0).GetMembers().length(); i++)
		{
			m_Reference.GetHeroParty(0).GetMembers()[i].Revive();
			m_Reference.GetHeroParty(0).GetMembers()[i].Teleport(m_Reference, m_Reference.GetLogicObjectByName(m_stgPos_FinalArea_commanders));
		}
		//Make Protagonists immortal
		//m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).SetImmortal(true);
		m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).SetHelpUpAllowed(true);
		m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).SetAutoRevive(true);
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).SetHelpUpAllowed(true);
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).SetAutoRevive(true);
		m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).Teleport(m_Reference, m_Reference.GetLogicObjectByName(m_stgPos_FinalArea_commanders));
		m_Reference.GetCreatureByName(m_stgCrt_KaylenVirTrath).Teleport(m_Reference, m_Reference.GetLogicObjectByName(m_stgPos_FinalArea_commanders));
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).Teleport(m_Reference, m_Reference.GetLogicObjectByName(m_stgPos_FinalArea_commanders));

		m_bAttackSuccess = true;


		return true;
	}

	// --- TREBUCHET -----------------------------------------------------------------------------------------------------
	bool OnChangeOwnerTrebuchet(Sector@ _Sector, const uint8 _iPreviousOwner, const uint8 _iNewOwner)
	{
		if (m_bAttackSuccess)
			return true;

		int _iFactionTrebuchet = 0;

		if (_iNewOwner == kFactionNeutral)
		{
			_iFactionTrebuchet = kFactionNeutral;
			m_Reference.GetCreatureByName(m_stgCrtTrebuchet).AllowAttacks(false);

			// changing the undead waves
			m_Reference.GetBuildingByName(m_stgBldg_Wave_Trebuchet).Enable(false);
			m_Reference.GetBuildingByName(m_stgBldg_Wave_PlayerBase).Upgrade(m_Reference, m_stgBldgDes_Wave_PlayerBase_02a, true);
		}
		else if (_iNewOwner == m_iPlayerFaction)
		{
			// only play this shoutout once.
			if (m_iTimesTrebuchetTaken == 0)
				DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_CatapultFeature", true);
			m_iTimesTrebuchetTaken++;

			// activate the trebuchet
			_iFactionTrebuchet = m_iPlayerFaction;
			m_Reference.SetTimer( TOnTimerEvent(@this.StartTrebuchetAttacking), 3 );

			// changing the undead waves
			m_Reference.GetBuildingByName(m_stgBldg_Wave_Trebuchet).Enable(true);
			m_Reference.GetBuildingByName(m_stgBldg_Wave_PlayerBase).Upgrade(m_Reference, m_stgBldgDes_Wave_PlayerBase_02b, true);

			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ01_Catapult", "P3_MAP016_SQ01_SecureCatapult", Completed );
		}

		Creature _CrtTrebuchet = m_Reference.GetCreatureByName(m_stgCrtTrebuchet);
		_CrtTrebuchet.SetFaction(_iFactionTrebuchet);

		print("[SCRIPT] - NOTE: trebuchet was taken by the faction "+_iNewOwner);
		return false;
	}

	bool AB1_OnEnteredSector_CatapultSector(Creature& in _Creature)
	{
		// changing the undead waves
		m_Reference.GetBuildingByName(m_stgBldg_Wave_Trebuchet).Enable(true);
		m_Reference.GetBuildingByName(m_stgBldg_Wave_PlayerBase).Upgrade(m_Reference, m_stgBldgDes_Wave_PlayerBase_02b, true);

		return true;
	}

	// --- SUMMONING MINIBOSS --------------------------------------------------------------------------------------------
	bool OnWarnMiniboss(Creature& in _Creature)
	{
		if (m_bAttackSuccess)
			return true;

		if (m_bChoosedIronFaloncs)
		{
			DialogShoutoutWhenReady( m_stgCrt_ScoutFalcons, m_stgCrt_ScoutFalcons, "AB1_SO_ScoutAttentionDeathman", true);

			m_Reference.GetCreatureByName( m_stgCrt_ScoutFalcons ).EnablePOIVisitingBehavior( true );

		}
		else if (m_bChoosedAonirsDeathman)
		{
			DialogShoutoutWhenReady( m_stgCrt_ScoutDeathman, m_stgCrt_ScoutDeathman, "AB1_SO_ScoutAttentionDeathman", true);

			m_Reference.GetCreatureByName( m_stgCrt_ScoutDeathman ).EnablePOIVisitingBehavior( true );
		}

		
		return true;
	}

	bool OnNextStep(Building& in _Building)
	{
		if(m_bNextStep)
		{
			return true;
		}
		print("[SCRIPT] - NOTE: Next Step");
		if (m_bChoosedIronFaloncs)
		{
			DialogShoutout( m_stgCrt_KiroyTeranor, m_stgCrt_KiroyTeranor, "AB1_SO_JoinedFalcons", true);
			m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_KiroyTeranor, "AB1_SO_FalconsPlan", true);
			m_Reference.GetBuildingByName(m_stgBldg_WaveFalcons_Connect).SetInteractive(true);
		}
		else if (m_bChoosedAonirsDeathman)
		{
			DialogShoutout( m_stgCrt_KaylenVirTrath, m_stgCrt_KaylenVirTrath, "AB1_SO_JoinedDeathman", true);
			m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_KaylenVirTrath, "AB1_DeathmanPlan", true);
			m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen_Connect).SetInteractive(true);
		}

		// strengthen the waves from the undead
		m_Reference.GetBuildingByName(m_stgBldg_Wave_PlayerBase).Upgrade(m_Reference, m_stgBldgDes_Wave_PlayerBase_02a, true);
		m_bNextStep = true;

		//Start Wave Spawing
		if (m_bChoosedIronFaloncs)
			m_Reference.GetBuildingByName(m_stgBldg_WaveFalcons_BigWave).AssignWorkers( 5 );
		else if (m_bChoosedAonirsDeathman)
			m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen_BigWave).AssignWorkers( 5 );

		return true;
	}

	bool FireNextStep()
	{
		if(m_bNextStep)
		{
			return true;
		}
		print("[SCRIPT] - NOTE: Next Step");
		if (m_bChoosedIronFaloncs)
		{
			DialogShoutout( m_stgCrt_KiroyTeranor, m_stgCrt_KiroyTeranor, "AB1_SO_JoinedFalcons", true);
			m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_KiroyTeranor, "AB1_SO_FalconsPlan", true);
			m_Reference.GetBuildingByName(m_stgBldg_WaveFalcons_Connect).SetInteractive(true);
		}
		else if (m_bChoosedAonirsDeathman)
		{
			DialogShoutout( m_stgCrt_KaylenVirTrath, m_stgCrt_KaylenVirTrath, "AB1_SO_JoinedDeathman", true);
			m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_KaylenVirTrath, "AB1_DeathmanPlan", true);
			m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen_Connect).SetInteractive(true);
		}

		// strengthen the waves from the undead
		m_Reference.GetBuildingByName(m_stgBldg_Wave_PlayerBase).Upgrade(m_Reference, m_stgBldgDes_Wave_PlayerBase_02a, true);
		m_bNextStep = true;

		//Start Wave Spawing
		if (m_bChoosedIronFaloncs)
			m_Reference.GetBuildingByName(m_stgBldg_WaveFalcons_BigWave).AssignWorkers( 5 );
		else if (m_bChoosedAonirsDeathman)
			m_Reference.GetBuildingByName(m_stgBldg_WaveDeathmen_BigWave).AssignWorkers( 5 );

		return true;
	}


	bool OnPickUpMageStaff(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Shootout from Arenor Arandir (Iron Falcons).");
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).Damage( Siege, m_Reference.GetBuildingByName(m_stgBldg_Summoning).GetMaxHP() );
		HeroParty@ _HeroParty = m_Reference.GetHeroParty(0);
		_HeroParty.AddItems( m_stgItem_Necromancer, 1 );

		return true;
	}

	bool OnMinibossAttacks_Deathmen(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Miniboss arrives, and shit is happening.");

		// Shoutout
		DialogShoutoutReveal( m_stgCrt_Soldier_Deathmen, m_stgCrt_Soldier_Deathmen, "AB1_SO_BossArrives");

		// send the Miniboss to Deathman Camp
		CreatureGroup _crtGrpMiniboss ( m_Reference );
		_crtGrpMiniboss.Set( m_Reference.GetCreaturesFromSpawn(m_stgSpawn_Miniboss) );
		_crtGrpMiniboss.AttackPosition(m_Reference.GetEntitiesByName(m_stgCrt_Soldier_Deathmen)[0], true);

		return true;
	}

	bool OnMinibossAttacks_Falcon(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Miniboss destroyed the Deathmen Camp, and now comes to the Falcons. ("+m_Reference.GetEntitiesByName(m_stgPos_SectorCamp_Deathmen)[0].IsEnabled()+")");

		// Shoutout
		DialogShoutoutReveal( m_stgCrt_Soldier_Falcons, m_stgCrt_Soldier_Falcons, "AB1_SO_BossAttacks"	);

		// send the Miniboss to Deathman Camp
		CreatureGroup _crtGrpMiniboss ( m_Reference );
		_crtGrpMiniboss.Set( m_Reference.GetCreaturesFromSpawn(m_stgSpawn_Miniboss) );
		_crtGrpMiniboss.AttackPosition(m_Reference.GetEntitiesByName(m_stgPos_SectorCamp_Falcons)[0], true);

		return true;
	}

	// andra ritual site stuff
	bool AB1_OnAnyInside_RitualSiteEntered(Creature& in _Creature)
	{
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_RitualSideQuest", false);

		return true;
	}

	bool AB1_OnKilled_UndeadRitualSite(Creature& in _Creature)
	{
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).SetSelectable(false);
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).SetInteractive(false);
		DialogShoutoutWhenReady(m_stgAvatar, m_stgAvatar, "AB1_DefeatedNecromancer", true);
		
		return true;
	}

	bool OnSacrificedUnits(Building& in _Building)
	{
		print("[SCRIPT] - NOTE: Summoning altair was charged, and the boss is not going to spawn.");

		m_bSummonMiniboss = false;
		m_Reference.GetLogicObjectByName(m_stgBldg_Summoning).SetInteractive(true);

		for (int _iTmp0 = 1; _iTmp0 <= m_iSummoningEffects ;_iTmp0++)
		{
			m_Reference.GetLogicObjectByName( m_stgObjSummoningEffect + _iTmp0 ).Enable(false);
		}
		m_Reference.GetLogicObjectByName("S_AB1_SummoningUndeadMiniboss").Enable(false);
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_SacrificeUnit", Completed );
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_SacrificeUnitComplete", Completed );
		return true;
	}

	bool OnSO_NecromancerAttack(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Let the Zombie-Minions from the Necromancer attack.");
		if(m_bSpawnedMiniBoss != true)
		{
			CreatureGroup _crtGrpMiniboss ( m_Reference );
			_crtGrpMiniboss.Set( m_Reference.GetCreaturesFromSpawn(m_stgSpawn_SumminedMinions_01) );
			_crtGrpMiniboss.AttackEntity(m_Reference.GetEntityById( _Creature.GetId() ), false);
			m_Reference.GetBuildingByName("AB1_Summoning_Staff").SetFaction(m_iPlayerFaction);
		}
		return true;
	}

	// D W A R F   B R O T H E R S
	bool OnDwarfBrothers_01_attack(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Trigger for the 1st Dwarf Brother");

		// Start all the attacks
		if (m_bDwarfAttackStarted == false)
		{
			m_Reference.GetCreatureByName("AB1_Dwarf_Brother_1").SetPreserveBody(true);
			m_Reference.GetCreatureByName("AB1_Dwarf_Brother_2").SetPreserveBody(true);
			m_Reference.GetCreatureByName("AB1_Dwarf_Brother_3").SetPreserveBody(true);
			DwarfBrothers_StartAttack_01();

			// enable the other two brothers
			m_Reference.ActivateSpawn( "Undead_DB_Attack_02", true);
			m_Reference.ActivateSpawn( "Undead_DB_Attack_03", true);
			m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.OnDwarfBrothers_02_saved), m_Reference.GetCreaturesFromSpawn("Undead_DB_Attack_02"), false, "");
			m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.OnDwarfBrothers_03_saved), m_Reference.GetCreaturesFromSpawn("Undead_DB_Attack_03"), false, "");

			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Enable( true );
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Enable( true );


			m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnDwarfBrothers_02_attack), "AB1_DwarfBrotherAttack_02", m_iPlayerFaction);
			m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.OnDwarfBrothers_03_attack), "AB1_DwarfBrotherAttack_02", m_iPlayerFaction);
			m_Reference.RegisterCreatureEventByIndividual(RevivePeriodEnded, TOnCreatureEvent(@this.OnDwarfBrothers_02_killed), m_stgCrt_DwarfBrother_02, "" );
			m_Reference.RegisterCreatureEventByIndividual(RevivePeriodEnded, TOnCreatureEvent(@this.OnDwarfBrothers_03_killed), m_stgCrt_DwarfBrother_03, "" );
			m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnDwarfBrother_Dialogue), m_stgCrt_DwarfBrother_02, "AB1_DwarfBrothers" );
			m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnDwarfBrother_Dialogue), m_stgCrt_DwarfBrother_03, "AB1_DwarfBrothers" );

			// start the countdown of their DEATH! MUHA HA HA HA HAAHAHAJDAKSD *cough* ...
			//m_Reference.SetTimer( TOnTimerEvent(@this.DwarfBrothers_StartAttack_02), 120 );
			//m_Reference.SetTimer( TOnTimerEvent(@this.DwarfBrothers_StartAttack_03), 240 );

				m_bDwarfAttackStarted = true;
			}

			// Shoutout from the dwarf
		if (m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).Exists() && m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).GetCurrentHP() > 0)
			DialogShoutout(m_stgCrt_DwarfBrother_01, m_stgCrt_DwarfBrother_01, "AB1_SO_AttackedByUndead", false);

		return true;
	}
	bool OnDwarfBrothers_02_attack(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Trigger for the 2nd Dwarf Brother");

		DwarfBrothers_StartAttack_02();
		if (m_bDwarfAttackStarted == true)
		{
			if (m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Exists() && m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).GetCurrentHP() > 0)
				DialogShoutout(m_stgCrt_DwarfBrother_02, m_stgCrt_DwarfBrother_02, "AB1_SO_Dwarf2Attacked", false);
			return true;
		}
		return false;
	}

	bool OnDwarfBrothers_03_attack(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: Trigger for the 3rd Dwarf Brother");

		DwarfBrothers_StartAttack_03();
		if (m_bDwarfAttackStarted == true)
		{
			if (m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Exists() && m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).GetCurrentHP() > 0)
				DialogShoutout(m_stgCrt_DwarfBrother_03, m_stgCrt_DwarfBrother_03, "AB1_SO_Dwarf3Attacked", false);
			return true;
		}
		return false ;
	}


	bool OnDwarfBrothers_01_saved(Creature& in _Creature)
	{
		m_bDwarfSaved_01 = true;

		// check, if the first dwarf is still alive
		if (m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).Exists())
		{
			print("[SCRIPT] - NOTE: 1st Dwarf Brother was saved");
			m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_DwarfBrother_01, "AB1_Dwarf1Saved", true);
			m_Reference.GetWorld().SetGlobalBool("AB_SavedDwarfBrother_1", true);

		}
		else
		{
			print("[SCRIPT] - NOTE: 1st Dwarf Brother could not be saved");
		}

		return true;
	}

	bool OnDwarfBrothers_02_saved(Creature& in _Creature)
	{
		m_bDwarfSaved_02 = true;
		CheckDwarfBrothers();

		return true;
	}
	bool OnDwarfBrothers_03_saved(Creature& in _Creature)
	{
		m_bDwarfSaved_03 = true;
		CheckDwarfBrothers();

		return true;
	}

	bool OnDwarfBrothers_01_killed(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: 1st Dwarf Brother died");
		m_Reference.GetCreatureByName( m_stgCrt_DwarfBrother_Caravan ).Kill();
		DwarfBrothers_StartAttack_02();
		DwarfBrothers_StartAttack_03();

		m_Reference.GetJournal(m_iPlayerFaction).SetQuestState("SQ_016_Dwarves", Failed);
		return true;
	}

	bool OnDwarfBrothers_02_killed(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: 2nd Dwarf Brother died");

		CheckDwarfBrothers();
		return true;
	}

	bool OnDwarfBrothers_03_killed(Creature& in _Creature)
	{
		print("[SCRIPT] - NOTE: 3rd Dwarf Brother died");

		CheckDwarfBrothers();
		
		printNote("unblocking proper dialogue options");
		m_Reference.GetWorld().BlockDialogueTopicNode("AB1_Dwarf_Brother_2", "29B924C9");
		m_Reference.GetWorld().UnblockDialogueTopicNode("AB1_Dwarf_Brother_2", "32B2C57E");
		return true;
	}

	bool OnDwarfBrother_Dialogue(Creature& in _Creature)
	{
		m_iDwarfsArrived++;
		print("[SCRIPT] - NOTE: "+m_iDwarfsArrived+" Dwarvens from "+m_iDwarfBrothers_Alive+" have arrived.");

		CheckDwarfBrothers();

		_Creature.EnablePOIVisitingBehavior(true);

		return true;
	}

	bool AB1_OnSeeSeerBones(Creature& in _Creature)
	{
		print("See Seer Bones");
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_VisionBones", true);
		return true;
	}

	bool AB1_OnInteractSeerBones(Creature& in Creatures)
	{
		print("Touched the bones");
		DialogTalkTo(m_Reference.GetHeroParty(m_iPlayerFaction).GetMembers()[1].GetDescriptionName(), m_stgAvatar, m_stgAvatar, "AB1_BonesDialogue");
		return false;
	}

	bool AB1_OnDestroyed_UndeadTower(Building&in _Building)
	{
		m_iUndeadTowersDestroyed++;

		m_Reference.GetWorld().SetGlobalInt("MapVariables.AB1_UndeadTowersDestroyed", m_iUndeadTowersDestroyed);
		if(m_iUndeadTowersDestroyed >= m_iNumUndeadTowers)
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ02_Towers", "P3_MAP016_SQ02_DestroyTowers", Completed );
		}

		return true;
	}

	bool AB1_OnKilled_UndeadFortress(Creature& in _Creature)
	{
		printNote("AB1_OnKilled_UndeadFortress");
		if(IsSpawnAlive("Undead_Fort_02b"))
		{
			return false;
		}
		if(IsSpawnAlive("Undead_Fort_03"))
		{
			return false;
		}
		if(IsSpawnAlive("Undead_Fort_04a"))
		{
			return false;
		}
		if(IsSpawnAlive("Undead_Fort_04b"))
		{
			return false;
		}

		// register event
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.AB1_OnUndeadFortressMeeting), "AB1_Trg_FinalMeeting", m_iPlayerFaction);

		// complete quest
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Falcons", "P3_MAP016_Falcons_KillTheUndead", Completed );
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Deathmen", "P3_MAP016_Deathmen_KillTheUndead", Completed );

		return true;
	}

	bool AB1_OnKilled_UndeadFortressBoss(Creature& in _Creature)
	{
		printNote("playing final cutscene!");
		if(IsSpawnAlive("AB1_FinalWaveSpawn03") ||
			IsSpawnAlive("Undead_Fort_Boss"))
		{
			return false;
		}

		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).EnablePOIVisitingBehavior(false);

		HeroPartyReviveMembers_NotCutscene();
		m_Reference.PlayCutScene("AB1_04_KiroyDies");
		m_Reference.GetWorld().EnableDialogueTopic("Arenor_Arandir", "AB1_Epilogue", true);

		if(m_bChoiceAonirsDeathman)
		{
			TransferRTS(0, 2);
		}
		else
		{
			TransferRTS(0, 1);
		}
		return true;
	}

	bool AB1_OnUndeadFortressMeeting(Creature& in _Creature)
	{
		printNote("playing fortress meeting cutscene");

		if(m_iUndeadTowersDestroyed < m_iNumUndeadTowers)
		{
			return false;
		}
		if(!m_bPlayFortressCutscene)
		{
			m_bPlayFortressCutscene = true;
			// play the cutscene
			HeroPartyReviveMembers_NotCutscene();
			m_Reference.GetCreatureByName("Arenor_Arandir").EnablePOIVisitingBehavior(false);
			if(m_bChoiceAonirsDeathman)
			{
				m_Reference.PlayCutScene("AB1_02_JoinGroup_SidedWithDeathmen");
			}
			else
			{
				m_Reference.PlayCutScene("AB1_02_JoinGroup_SidedWithFalcons");
			}


			// enable waves after cutscene
			m_Reference.RegisterUIEvent(CutsceneFinished, TOnUIEvent(@this.AB1_OnCutsceneFinished_FortressMeeting), m_iPlayerFaction);

			// disable spawner logic
			m_Reference.GetBuildingByName(m_stgBldg_Wave_Trebuchet).Enable(false);
			m_Reference.GetBuildingByName(m_stgBldg_Wave_PlayerBase).Upgrade(m_Reference, m_stgBldgDes_Wave_PlayerBase_Inactive, true);
			m_Reference.GetBuildingByName(m_stgBldg_Wave_PlayerBase).Destroy();

			// complete questt
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Falcons", "P3_MAP016_Falcons_WaitKatras", Completed );
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Deathmen", "P3_MAP016_Deathmen_WaitArenor", Completed );
			m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).SetImmovable(true);
			string[] POICategories_KiroyRitual = {"Kiroy_Ritual"};
			m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).SetPOICategories (m_Reference, POICategories_KiroyRitual);
			m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).AllowAttacks(false);

			m_Reference.GetCreatureByName( m_stgCrt_KiroyTeranor ).SetImmortal( false );
			m_Reference.GetCreatureByName( m_stgCrt_KiroyTeranor ).SetImmovable( true );
			m_Reference.RegisterCreatureEventByIndividual(Killed, TOnCreatureEvent(@this.AB1_OnKilled_Kiroy), m_stgCrt_KiroyTeranor, "");
			// kill all spawned undeads
			for(uint i = 0; i < m_arr_UndeadWave.length(); i++)
			{
				//m_arr_UndeadWave[i].Kill();
				m_Reference.GetCreatureById(m_arr_UndeadWave[i]).Kill();
			}

			for(uint i = 0; i < m_arr_DeathmenFalconWave.length(); i++)
			{
				m_Reference.GetCreatureById(m_arr_DeathmenFalconWave[i]).Kill();
			}
		}
		return true;
	}

	bool AB1_OnKilled_Kiroy(Creature& in _Creature)
	{
		if(m_iFinalWave >= 2 && !IsSpawnAlive("Undead_Fort_Boss"))
		{
			return true;
		}

		m_Reference.LoseGame(m_iPlayerFaction);
		return true;
	}

	bool AB1_OnCutsceneFinished_FortressMeeting(const uint8 _uPlayerFaction)
	{
		m_Reference.SetTimer(TOnTimerEvent(@this.AB1_OnTimer_FinalWaves), m_iFinalWavesInterval);

		return true;
	}

	bool AB1_OnTimer_FinalWaves()
	{
		printNote("spawning final wave " + m_iFinalWave);
		if(m_iFinalWave == 0)	
		{
			string ToSpawn = "AB1_FinalWaveSpawn01";
			m_Reference.ActivateSpawn(ToSpawn, true);
			for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn(ToSpawn).size(); i++)
			{
				m_Reference.GetCreaturesFromSpawn(ToSpawn)[i].AttackPosition(m_Reference,  m_Reference.GetCreatureByName(m_stgAvatar), false);
			}
			m_Reference.SetTimer(TOnTimerEvent(@this.AB1_OnTimer_FinalWaves), m_iFinalWavesInterval);
		}
		else if(m_iFinalWave == 1)
		{
			string ToSpawn = "AB1_FinalWaveSpawn02";
			m_Reference.ActivateSpawn(ToSpawn, true);
			for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn(ToSpawn).size(); i++)
			{
				m_Reference.GetCreaturesFromSpawn(ToSpawn)[i].AttackPosition(m_Reference,  m_Reference.GetCreatureByName(m_stgAvatar), false);
			}
			m_Reference.SetTimer(TOnTimerEvent(@this.AB1_OnTimer_FinalWaves), m_iFinalWavesInterval);
		}
		else if(m_iFinalWave == 2)
		{
			string ToSpawn = "AB1_FinalWaveSpawn03";
			m_Reference.ActivateSpawn(ToSpawn, true);
			for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn(ToSpawn).size(); i++)
			{
				m_Reference.GetCreaturesFromSpawn(ToSpawn)[i].AttackPosition(m_Reference,  m_Reference.GetCreatureByName(m_stgAvatar), false);
			}
			m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB1_OnKilled_UndeadFortressBoss), m_Reference.GetCreaturesFromSpawn(ToSpawn), false, "");
			printNote("spawning final wave boss");
			DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_Final_Wave", true);
			ToSpawn = "Undead_Fort_Boss";
			m_Reference.ActivateSpawn(ToSpawn, true);
			for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn(ToSpawn).size(); i++)
			{	
				m_Reference.GetCreaturesFromSpawn(ToSpawn)[i].AttackPosition(m_Reference,  m_Reference.GetCreatureByName(m_stgAvatar), false);
			}
			m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB1_OnKilled_UndeadFortressBoss), m_Reference.GetCreaturesFromSpawn(ToSpawn), false, "");
		}

		m_iFinalWave++;

		return true;
	}

	// trebuchet catapult stuff
	bool AB1_OnProductionFinished_Trebuchet(Building& in _Building, string[]& in sParams)
	{
		printNote("catapult finished produciton of:" + sParams[0]);
		if(sParams[0] == "AB1_TrebuchetShot")
		{
			m_Reference.GetBuildingByName(m_stgBldg_Trebuchet).Charge(-25);
		}
		else
		{
			m_Reference.GetBuildingByName(m_stgBldg_Trebuchet).Charge(25);
		}

		if(m_Reference.GetBuildingByName(m_stgBldg_Trebuchet).GetCharge() >= 25)
		{
			m_Reference.GetBuildingByName(m_stgBldg_Trebuchet).ProhibitProduction(Spell, "AB1_TrebuchetShot", false);
			if(m_Reference.GetBuildingByName(m_stgBldg_Trebuchet).GetCharge() >= 100)
			{
				m_Reference.GetBuildingByName(m_stgBldg_Trebuchet).ProhibitProduction(Spell, "AB1_LoadTrebuchetShot", true);
			}
			else
			{
				m_Reference.GetBuildingByName(m_stgBldg_Trebuchet).ProhibitProduction(Spell, "AB1_LoadTrebuchetShot", false);
			}
		}
		else
		{
			m_Reference.GetBuildingByName(m_stgBldg_Trebuchet).ProhibitProduction(Spell, "AB1_TrebuchetShot", true);
		}

		return false;
	}

	// final area
	bool AB1_OnAnyInside_FinalAreaAllyJoin(Creature& in _Creature)
	{
		if(m_bChoiceAonirsDeathman)
		{
			m_Reference.GetCreatureByName("Katras").Teleport(m_Reference, m_Reference.GetLogicObjectByName("AB1_Pos_AllyJoinForces"));
			m_Reference.GetCreatureByName("Katras").AttackPosition(m_Reference, m_Reference.GetLogicObjectByName("FinalFrontier_army"), false);
			m_Reference.ActivateSpawn("AB1_Spawn_AllyDeathmen", true);
			for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB1_Spawn_AllyDeathmen").length; i++)
			{
				m_Reference.GetCreaturesFromSpawn("AB1_Spawn_AllyDeathmen")[i].AttackPosition(m_Reference, m_Reference.GetLogicObjectByName("FinalFrontier_army"), false);
			}
		}
		else
		{
			m_Reference.GetCreatureByName("Arenor_Arandir").Teleport(m_Reference, m_Reference.GetLogicObjectByName("AB1_Pos_AllyJoinForces"));
			m_Reference.GetCreatureByName("Arenor_Arandir").AttackPosition(m_Reference, m_Reference.GetLogicObjectByName("FinalFrontier_army"), false);
			m_Reference.ActivateSpawn("AB1_Spawn_AllyFalcons", true);
			for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB1_Spawn_AllyFalcons").length; i++)
			{
				m_Reference.GetCreaturesFromSpawn("AB1_Spawn_AllyFalcons")[i].AttackPosition(m_Reference, m_Reference.GetLogicObjectByName("FinalFrontier_army"), false);
			}
			
		}

		DialogShoutoutReveal(m_stgAvatar, m_stgAvatar, "AB1_SO_StormFortress");
		return true;
	}

// -------------Version2--------------------
	bool OnAngarWarning(Creature& in Creatures)
	{
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB2_03_SO_Ambush", true);
		return true;
	}

	bool OnAmbush(Creature& in Creatures)
	{
		m_Reference.ActivateSpawn("AB2_Ambush01", true);
		for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB2_Ambush01").size(); i++)
		{
			m_Reference.GetCreaturesFromSpawn("AB2_Ambush01")[i].GoTo(m_Reference, m_Reference.GetHeroParty(m_iPlayerFaction).GetMembers()[0], 5, 7, true, false);
		}
		for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB2_Ambush02").size(); i++)
		{
			m_Reference.GetCreaturesFromSpawn("AB2_Ambush02")[i].GoTo(m_Reference, m_Reference.GetHeroParty(m_iPlayerFaction).GetMembers()[0], 5, 7, true, false);
		}

		if(IsSpawnAlive("AB2_Ambush01"))
		{
			DialogShoutout("AB2_Bane_Fanatic_1", "AB2_Bane_Fanatic_1", "AB2_02_SO_Bane_Ambush", false);

			m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.OnKilledAmbush01), m_Reference.GetCreaturesFromSpawn("AB2_Ambush01"), false, "");
		}
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.OnKilledAmbush02), m_Reference.GetCreaturesFromSpawn("AB2_Ambush02"), false, "");

		return true;
	}

	bool OnKilledAmbush01(Creature& in Creatures)
	{
		AB2_CheckAmbush();
		return true;
	}

	bool OnKilledAmbush02(Creature& in Creatures)
	{
		AB2_CheckAmbush();
		return true;
	}

	bool OnSeeCapturedMentalist(Creature& in Creatures)
	{
		DialogShoutout("AB2_Mentalist_Captive", "AB2_Mentalist_Captive", "AB2_06_SO_Battle", false);
		m_Reference.RegisterHeroPartyEvent(AnyInAggroRange, TOnHeroPartyEvent(@this.OnAggroGuards), "AB2_DeatmanGuard_Mentalist", m_iPlayerFaction);
		return true;
	}

	bool OnAggroGuards(Creature& in Creatures)
	{
		DialogShoutout("AB2_Mentalist_Captive", "AB2_Mentalist_Captive", "AB2_05_SO_HelpMe", false);
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.OnKilledGuards), m_Reference.GetCreaturesFromSpawn("AB2_DeathMenGuards"), false, "");
		m_Reference.SetFactionRelation(m_iFaction_Deathman, m_iFaction_Mentalists, Hostile);

		m_Reference.GetCreatureByName("AB2_Mentalist_Captive").SetHelpUpAllowed(true);
		m_Reference.GetCreatureByName("AB2_Mentalist_Captive").SetAutoRevive(true);
		return true;
	}

	bool OnKilledGuards(Creature& in Creatures)
	{
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_StrengthenHumans", "P4_MAP016_MentalistTrouble", Completed );
		m_Reference.GetCreatureByName("AB2_Mentalist_Captive").PlayAnimation(m_Reference, Idle, -1, 1, false, false, Entity ( ));
		m_Reference.GetWorld().EnableDialogueTopic("AB2_Mentalist_Captive", "AB2_07_FirstContact", true);
		DialogShoutoutWhenReady("AB2_Mentalist_Captive", "AB2_Mentalist_Captive", "AB2_07_FirstContact", false);
		return true;
	}

	bool OnEnterCamp(Creature& in Creatures)
	{
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB2_09_SO_FalconCamp", false);
		return true;
	}

	bool OnDiscoverFalconSideCamp(Creature& in Creatures)
	{
		if(m_bChoiceIronFaloncs)
			m_Reference.GetWorld().SetGlobalBool("AB2_VisitedCamp", true);
		return true;
	}

	bool OnDiscoverDeathmenSideCamp(Creature& in Creatures)
	{
		if(m_bChoiceAonirsDeathman)
			m_Reference.GetWorld().SetGlobalBool("AB2_VisitedCamp", true);
		return true;
	}

	bool AB2_OnKilledEngineerGuards(Creature& in Creatures)
	{
		m_Reference.GetWorld().EnableDialogueTopic("AB2_Engineer", "AB2_27_FirstContact", true);
		return true;
	}

	bool OnEncounterEngineer(Creature& in Creatures)
	{
		DialogShoutoutReveal("AB2_Engineer", "AB2_Engineer", "AB2_26B_SO_Help");
		return true;
	}

	bool OnKilledNecromancers(Creature& in Creatures)
	{
		if (m_Reference.GetJournal(m_iPlayerFaction).IsTaskActive("P4_MAP016_SQ03_Paladins","P4_MAP016_SQ03_KillNecromancers"))
			{
				m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_SQ03_Paladins", "P4_MAP016_SQ03_KillNecromancers", Completed );
			}

		m_Reference.GetWorld().SetGlobalBool("AB2_KilledNecromancers", true);
		return true;
	}

	bool OnSeeArchfireBeast(Creature& in Creatures)
	{
		print(Creatures.GetDescriptionName() + "!!!!!!!");
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB2_30_SO_ArchfireBeast", false);
		m_Reference.GetWorld().UnblockDialogueTopicNode("Leyna", "3D106E52"); //Unlock one Question in the Dialogue with Leyna
		return true;
	}

	bool AB2_OnEnterMentalistCamp(Creature& in Creatures)
	{
		if(m_Reference.GetWorld().GetGlobalBool("AB2_DeathmenDefeated", false) == false)
		{
			print("SO from Mentalists"); // Doesn´t matter if party is complete
			DialogShoutout("AB2_Mentalist_1", "AB2_Mentalist_1", "AB2_12_SO_Approach_1", true);
			return true;
		}
		else
		{
			//m_Reference.GetWorld().EnableDialogueTopic("", "FailSafe", true);    // Dialogue missing
			return true;
		}
	}

	bool AB2_OnMentalistDamaged(Creature& in Creatures)
	{
		if(Creatures.GetCurrentHP() < 10)
		{
			print("Abort the Ritual");
			AB2_MentalistsRelocateToBase();
		}
		return false;
	}

	bool AB2_OnKilledUndeadDuringRitual(Creature& in Creatures)
	{
		print("+1");
		m_iKilledUndeadDuringRitualCount++;
		if(m_iKilledUndeadDuringRitualCount >= m_iUndeadRitualSpawns)
		{
			print("End");
			m_Reference.ActivateSpawn("AB2_UndeadAttackOnRitual01", false);
			m_Reference.ActivateSpawn("AB2_UndeadAttackOnRitual02", false);
			//LIGHT APPEARS IN THE MIDDLE OF THE RITUAL
			CreatureGroup _crtMentalists ( m_Reference );
			_crtMentalists.Set( m_Reference.GetCreaturesFromSpawn( "AB2_Mentalists" ) );
			_crtMentalists.Add(m_Reference.GetCreatureByName("AB2_Mentalist_Captive"));
			_crtMentalists.Move(m_Reference.GetEntitiesByName("AB_SecretTreasure")[0], false, false, false);
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_SQ01_Mentalists", "P4_MAP016_SQ01_Ritual", Completed );
			m_Reference.GetWorld().EnableDialogueTopic("Leyna", "AB2_20_QuestComplete", true);

			A_UnlockAchievement("SF3_SP_AB_Ritualist");

			return true;
		}
		return false;
	}

	bool AB2_OnOwnerChanged_TrebuchetSector(Sector@ _Sector, const uint8 _iPreviousOwner, const uint8 _iNewOwner)
	{
		printNote("Sector owner changed");
		if(_iNewOwner == m_iPlayerFaction)
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_StrengthenHumans", "P4_MAP016_TakeTrebuchetSector", Completed);
		}

		return false;
	}

	bool AB2_OnLootedEngineerTools(LogicObject& in _Object, uint8 _uFaction)
	{
		m_bLootedEngineerTools = true;
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_SQ02_Engineer", "P4_MAP016_SQ02_RetrieveTools", Completed );
		m_Reference.GetWorld().EnableDialogueTopic("AB2_Engineer", "AB2_27_FirstContact", false);
		m_Reference.GetWorld().EnableDialogueTopic("AB2_Engineer", "AB2_28_SO_Hurry", false);
		m_Reference.GetWorld().EnableDialogueTopic("AB2_Engineer", "AB2_31_HaveTools", true);

		return true;
	}

	bool AB2_OnDestroyDeathmenSector(Building& in Buildings)
	{
		if(m_bDeathmanWeakend == false)
		{
			print("First HQ destroyed " + Buildings.GetDescriptionName());
			m_bDeathmanWeakend = true;

			CreatureGroup crt_BigArmy (m_Reference);
			crt_BigArmy.Set(m_Reference.GetCreaturesFromSpawn("AB2_BigArmy01"));
			crt_BigArmy.AttackPosition(Buildings, false, false);
			crt_BigArmy.Set(m_Reference.GetCreaturesFromSpawn("AB2_BigArmy"));
			crt_BigArmy.AttackPosition(Buildings, false, false);

			return false;
		}

		print("Deathmen defeated");
		m_Reference.LoseGame(m_iFaction_DeathmenRTS);
		TransferRTS(0, kFactionNeutral);
		m_Reference.GetWorld().SetGlobalBool("AB2_DeathmenDefeated", true);
		if (m_Reference.GetJournal(m_iPlayerFaction).IsTaskActive("P4_MAP016_StrengthenHumans","P4_MAP016_InvTriggerArenorDeathmen"))
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_StrengthenHumans", "P4_MAP016_InvTriggerArenorDeathmen", Completed );
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_StrengthenHumans", "P4_MAP016_InvTriggerDeathmenDefeated", Completed );
		}
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_StrengthenHumans", "P4_MAP016_DefeatDeathmenHQ", Completed );
		if(m_bArenorOnTheMove == false)
		{
			m_Reference.BlockNavMesh("AB2_MainGate_Blocker", false);
			m_Reference.GetEntitiesByName("AB2_MainGate")[0].Enable(false);

			m_Reference.GetWorld().EnableDialogueTopic("Arenor_Arandir", "AB2_35_ArenorFreed", true);
		}
		else
		{
			
			DialogTalkTo("Arenor_Arandir", m_stgAvatar, "Arenor_Arandir", "AB2_36_Reconcile");
			m_Reference.GetWorld().EnableDialogueTopic("Arenor_Arandir", "AB2_36_Reconcile", true);
		}
		return true;
	}

	bool AB2_OnChargedHoleInTheWall(Building& in Buildings)
	{
		print("Wohoo Food.");
		//Soundeffect "Fierce Shouting" should play
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_StrengthenHumans", "P4_MAP016_FalconResources", Completed );
		DialogShoutoutReveal("Arenor_Arandir", "Arenor_Arandir", "AB2_34_SO_Charge");
		m_bArenorOnTheMove = true;
		m_Reference.BlockNavMesh("AB2_WesternGate_Blocker", false);
		m_Reference.GetLogicObjectByName("AB2_WesternGate").Enable(false);

		//CreatureGroup crt_TrappedFalcons(m_Reference);
		//crt_TrappedFalcons.Set(m_Reference.GetCreaturesFromSpawn("AB2_TrappedFalcons"));
		//crt_TrappedFalcons.Goto(m_Reference.GetSectorByName("Sector_Trebuchet").GetMainBuilding(), false, false);
		for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB2_TrappedFalcons").size(); i++)
		{
			m_Reference.GetCreaturesFromSpawn("AB2_TrappedFalcons")[i].GoTo(m_Reference, m_Reference.GetSectorByName("Sector_Trebuchet").GetMainBuilding(), 5, 5, false, false);
		}

		m_Reference.GetCreatureByName("AB2_Falcon_Survivor_02").GoTo(m_Reference, m_Reference.GetSectorsByOwner(m_iPlayerFaction)[0].GetMainBuilding(), 5, 5, true, false);
		m_Reference.GetWorld().EnableDialogueTopic("Arenor_Arandir", "AB2_35_ArenorFreed", true);

		return true;
	}

	bool AB2_OnDestroyFirstDeathmenBuilding(Building& in Buildings)
	{
		print("Player is Attacking Main Deathmen Sector; Moving First Half of Siege Army to help out");
		CreatureGroup crt_BigArmy (m_Reference);
		crt_BigArmy.Set(m_Reference.GetCreaturesFromSpawn("AB2_BigArmy01"));
		crt_BigArmy.AttackPosition(Buildings, false, false);
		return true;
	}

	bool AB2_OnMentalistEnteredClairvoyanceSpot(Creature& in Creatures)
	{
		print("I see!!!! I seee!");
		return true;
	}

	// -------------------------------------------------------------------------------------------------------------------
	// ---- A R T I C Y   F U N C T I O N S ------------------------------------------------------------------------------

	// -- Version 1 --

	bool ArticyKillDeadScout ()
	{
		print("[SCRIPT] - NOTE: Kill the dead scout completely.");
		m_Reference.GetCreatureByName(m_stgCrt_DeadScout).KillAndPreserve(m_Reference);
		m_Reference.GetLogicObjectByName(m_stgCrt_DeadScout).SetInspectionText(0);

		return true;
	}

	bool ArticyFinishedBedating ()
	{
		print("[SCRIPT] - NOTE: finish debating, going back to their camps.");
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).EnablePOIVisitingBehavior ( true );
		m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).EnablePOIVisitingBehavior ( true );
		m_Reference.GetCreatureByName(m_stgCrt_KaylenVirTrath).EnablePOIVisitingBehavior ( true );
		m_bFinishedDebating = true;

		return true;
	}

	bool ArticyKiroyBackToPost()
	{
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).GoTo( m_Reference, m_Reference.GetLogicObjectByName("AB1_Spot_Arenor01"), 15, 25, false );

		return true;
	}

	bool ArticyLetDeathmanSoldierWalkHere ()
	{
		print("[SCRIPT] - NOTE: let the Deathman Soldier walk up the discussion");

		// getting
		m_Reference.GetCreatureByName(m_stgCrt_Soldier_Deathmen).Stop (m_Reference );
		m_Reference.GetCreatureByName(m_stgCrt_Soldier_Deathmen).GoTo( m_Reference, m_Reference.GetLogicObjectByName("AB1_Spot_KatrasTalk"), 3, 12, false );

		m_Reference.ActivateSpawn(m_stgSpawn_DeathmanPatrol, true);
		array <Creature>@ _entPatrol = m_Reference.GetCreaturesFromSpawn( m_stgSpawn_DeathmanPatrol );
		for ( uint _iTmp = 0 ; _iTmp < _entPatrol.length() ; _iTmp++ )
		{
			_entPatrol[_iTmp].FollowCreature(m_Reference, m_Reference.GetCreatureByName(m_stgCrt_Soldier_Deathmen), 1, 1, false, true);
		}

		return true;
	}

	bool ArticyArenorJoinConversation()
	{
		printNote("Arenor Arandir joins the conversation");
		m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).Stop (m_Reference );
		m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).GoTo( m_Reference, m_Reference.GetLogicObjectByName("AB1_Spot_Arenor01"), 3, 12, false );
		return true;
	}

	bool ArticyFoundAvatarFinished ()
	{
		printNote("Found Avatar stuff is finished - all should go back now.");
		PlayerHeroControlEnable();
			
		CreatureGroup _crtPatrol ( m_Reference );
		_crtPatrol.Set( m_Reference.GetCreaturesFromSpawn( m_stgSpawn_DeathmanPatrol ) );
		_crtPatrol.Patrol( "AB1_DeathmenPatrolPath", false, true, true, true );

		m_Reference.BlockNavMesh("AB1_PhysicBlocker_KiroyCamp", false);

		m_Reference.GetCreatureByName(m_stgCrt_Soldier_Deathmen).Stop (m_Reference );
		m_Reference.GetCreatureByName(m_stgCrt_Soldier_Deathmen).EnablePOIVisitingBehavior ( true );
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).EnablePOIVisitingBehavior ( true );
		m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).EnablePOIVisitingBehavior ( true );

		array <Creature>@ _entPatrol = m_Reference.GetCreaturesFromSpawn( m_stgSpawn_DeathmanPatrol );
		for ( uint _iTmp = 0 ; _iTmp < _entPatrol.length() ; _iTmp++ )
		{
			_entPatrol[_iTmp].EnablePOIVisitingBehavior ( true );
		}

		m_Reference.RegisterHeroPartyEvent(AnyOutsideAggroRange, TOnHeroPartyEvent(@this.AB1_OnAnyOutsideAggroRange_KiroyStartTalking), m_stgCrt_KiroyTeranor, m_iPlayerFaction);
		return true;
	}

	void ArticyMoveSoldiers()
	{
		for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB1_Falcons_Outpost").length(); i++)
		{
			uint j = i+1;
			m_Reference.GetCreaturesFromSpawn("AB1_Falcons_Outpost")[i].EnablePOIVisitingBehavior(true);
		}
	}

	bool ArticyKatrasWalkBack()
	{
		m_Reference.GetCreatureByName(m_stgCrt_Soldier_Deathmen).EnablePOIVisitingBehavior(true);
		return true;
	}	

	bool ArticyTraitorWalkBack()
	{
		print("[SCRIPT] - NOTE: Player lets the Traitor walk back to Deathman Camp - with free will.");
		m_bTraitorFriendly = true;

		CreatureGroup _crtTraitor ( m_Reference );
		_crtTraitor.Add(m_Reference.GetCreatureByName(m_stgCrt_FalconTraitor));
		_crtTraitor.FollowPath( m_stgPath_TraitorWalkRoute_Deathmen, true, true);

		m_Reference.GetWorld().EnableDialogueTopic("Katras", "AB1_DeathmanSide", true);
		return true;
	}

	bool ArticyTraitorWalkBackForced()
	{
		print("[SCRIPT] - NOTE: Player lets the Traitor walk back to Deathman Camp - against his will.");
		m_bTraitorFriendly = false;

		CreatureGroup _crtTraitor ( m_Reference );
		_crtTraitor.Add(m_Reference.GetCreatureByName(m_stgCrt_FalconTraitor));
		_crtTraitor.FollowPath( m_stgPath_TraitorWalkRoute, true, true);

		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.AB1_OnAnyInside_FalconQuestComplete), "AB1_Trg_FalconQuest", m_iPlayerFaction);
		return true;
	}

	bool ArticyTraitorAttack()
	{
		print("[SCRIPT] - NOTE: Playere wants to kill the traitor.");
		m_bTraitorFriendly = false;
		m_Reference.GetCreatureByName(m_stgCrt_FalconTraitor).SetFaction(kFactionMob);
		m_Reference.GetCreatureByName(m_stgCrt_FalconTraitor).SetPreserveBody(true);

		return true;
	}

	bool ArticyActivatePOIBehavior_SoldiersFalcon()
	{
		print("[SCRIPT] - NOTE: "+m_stgCrt_Soldier_Falcons+", POI: "+m_Reference.GetCreatureByName(m_stgCrt_Soldier_Falcons).IsPOIVisitingBehaviorEnabled ());
		m_Reference.GetCreatureByName(m_stgCrt_Soldier_Falcons).EnablePOIVisitingBehavior ( true );

		DialogShoutoutWhenReady ( m_stgAvatar, m_stgAvatar, "AB1_SO_WhoToSideWith", false);
		// enable traitor quest
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.AB1_OnAnyInside_FindTraitorSO), "AB1_Trg_FindTraitor", m_iPlayerFaction);
		return true;
	}

	bool ArticyActivatePOIBehavior_SoldiersDeathman()
	{
		print("[SCRIPT] - NOTE: "+m_stgCrt_Soldier_Deathmen+", POI: "+m_Reference.GetCreatureByName(m_stgCrt_Soldier_Deathmen).IsPOIVisitingBehaviorEnabled ());
		m_Reference.GetCreatureByName(m_stgCrt_Soldier_Deathmen).EnablePOIVisitingBehavior ( true );
		return true;
	}

	bool ArticyActivatePOIBehavior_Arenor()
	{
		m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir).EnablePOIVisitingBehavior ( true );
		return true;
	}

	bool ArticyActivatePOIBehavior_Kaylen ()
	{
		m_Reference.GetCreatureByName(m_stgCrt_KaylenVirTrath).EnablePOIVisitingBehavior ( true );
		return true;
	}

	bool ArticyActivatePOIBehavior_Kiroy ()
	{
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).EnablePOIVisitingBehavior ( true );
		return true;
	}

	bool ArticyStartRTSChoice_Falcons ()
	{
		print("[SCRIPT] - NOTE: Choice for the Falcons.");
		m_Reference.GetLogicObjectByName(m_stgObj_Camp_Falcons).SetInteractive(true);
		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).GoTo( m_Reference, ( m_Reference.GetLogicObjectByName(m_stgObj_Camp_Falcons)), 10, 15, false );
		m_bChoiceIronFaloncs = true;

		A_UnlockAchievement("SF3_SP_AB_Falcons");

		m_Reference.GetCreatureByName(m_stgCrt_KiroyTeranor).EnablePOIVisitingBehavior ( false );

		return true;
	}

	bool ArticyStartRTSChoice_Deathman ()
	{
		print("[SCRIPT] - NOTE: Choice for the Deathmans.");
		m_Reference.GetLogicObjectByName(m_stgObj_Camp_Deathman).SetInteractive(true);

		A_UnlockAchievement("SF3_SP_AB_Deathmen");

		m_bChoiceAonirsDeathman = true;

		return true;
	}

	bool ArticyStartRTSChoice_Deathman_Go()
	{
		m_Reference.GetCreatureByName(m_stgCrt_KaylenVirTrath).GoTo( m_Reference, ( m_Reference.GetLogicObjectByName(m_stgObj_Camp_Deathman)), 10, 15, false );
		m_Reference.GetCreatureByName( m_stgCrt_Soldier_Deathmen ).EnablePOIVisitingBehavior( false );
		return true;
	}

	bool ArticyStartRTSQuest()
	{
		printNote("starting RTS quests");

		// --- The Final stuff

		

		// unlock for andra/necromancer dialigue
		m_Reference.GetWorld().BlockDialogueTopicNode(m_stgAvatar, "4A8CEA93");
		m_Reference.GetWorld().UnblockDialogueTopicNode(m_stgAvatar, "D9A4AF60");

		// check race of player and enable/disable dialogue accordingly
		if(m_Reference.GetFactionRace(m_iPlayerFaction) == Human)
		{
			m_Reference.GetWorld().UnblockDialogueTopicNode("Kiroy_Teranor", "E15DF2C6");
			m_Reference.GetWorld().UnblockDialogueTopicNode("Katras", "EE65FB01");
		}
		else if(m_Reference.GetFactionRace(m_iPlayerFaction) == Orc)
		{
			m_Reference.GetWorld().UnblockDialogueTopicNode("Kiroy_Teranor", "3F686372");
			m_Reference.GetWorld().UnblockDialogueTopicNode("Katras", "47CAB029");
		}
		else if(m_Reference.GetFactionRace(m_iPlayerFaction) == Elf)
		{
			m_Reference.GetWorld().UnblockDialogueTopicNode("Kiroy_Teranor", "D5C8F916");
			m_Reference.GetWorld().UnblockDialogueTopicNode("Katras", "10E25E32");
		}
		return true;
	}

	bool ArticyActivateRitualSite()
	{
		printNote("registering event for ritual site");
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.AB1_OnAnyInside_RitualSiteEntered), "AB1_Trg_RitualSite", m_iPlayerFaction);

		return true;
	}

	void ArticyStartSummoning_Normal()
	{
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).SetUsableByAllFactions( true );
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).SetSelectable(true);
		StartSummoning();
	}

	void ArticyStartSummoning_Fast()
	{
		m_iMinibossSummoningTime = m_iMinibossSummoningTime/5;
		StartSummoning();
	}

	bool ArticyPlayAndraSong()
	{
		printNote("Playing andra's song");
		m_Reference.SetTimer( TOnTimerEvent( @this.AB1_OnTimer_ContinueWightSongDialogue), 15);
		PlayTrack_CharAndrasSong_1_short();
		return true;
	}

	bool AB1_OnTimer_ContinueWightSongDialogue()
	{
		printNote("TIMER FIRED!");
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_RitualSite", false);
		return true;
	}

	bool ArticySpawnZombies()
	{
		printNote("spawning zombies in ritual site");
		//m_Reference.ActivateSpawn("Undead_Encounter_Summoning_01", true);
		m_Reference.CastSpell("AB1_SummonUndead", kFactionMob, m_Reference.GetLogicObjectByName("AB1_Spot_CorDwellerZombies"));
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).SetInteractive(true);
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).SetSelectable(true);

		m_Reference.RegisterHeroPartyEvent(Interact, TOnHeroPartyEvent(@this.AB1_OnKilled_UndeadRitualSite), m_stgBldg_Summoning, m_iPlayerFaction);

		//
		//AB1_SummonUndead und Summon_CoreDweller_AB1
		///m_Reference.GetSummonedCreatures(m_Reference);
		//m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB1_OnKilled_UndeadRitualSite), m_Reference.GetSummonedCreatures(m_Reference), false, "");
		return true;
	}

	bool ArticySecondBrotherWalk()
	{
		m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).GoTo( m_Reference, ( m_Reference.GetEntitiesByName(m_stgCrt_DwarfBrother_01)[0]), 10, 15, false );
		return true;
	}

	bool ArticyThirdBrotherWalk()
	{
		m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).GoTo( m_Reference, ( m_Reference.GetEntitiesByName(m_stgCrt_DwarfBrother_01)[0]), 10, 15, false );
		return true;
	}

	//FalconPriestStuff
	void AB1_ArticyFirstTimeUsedSeerBone()
	{
		print("First time use");
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_AfterBones", false);
		m_Reference.GetWorld().SetGlobalBool("MapVariables.AB1_SeerBoneFirstTimeUse", false);
		m_Reference.GetWorld().SetGlobalBool("MapVariables.AB1_SeerBoneSecondTime", true);
	}

	void AB1_ArticySecondTimeUsedSeerBone()
	{
		print("Second time use");
		m_Reference.GetWorld().SetGlobalBool("MapVariables.AB1_SeerBoneSecondTime", false);
	}

	void AB1_ArticyRevealSector(string _Sector)
	{
		if(m_sRevealedSector != "")
			m_Reference.RemoveVisibleSector(m_iPlayerFaction, m_Reference.GetSectorByName( m_sRevealedSector ).GetIndex());
		print("Revealing: " + _Sector);
		m_sRevealedSector = _Sector;
		print(m_sRevealedSector + "!!!");
		m_Reference.AddVisibleSector(m_iPlayerFaction, m_Reference.GetSectorByName( _Sector ).GetIndex());
		m_Reference.SetTimer(TOnTimerEvent(@this.RevokeReveal), m_iTimerForTrueSight);
	}

	void ArticyGetFinalStone()
	{
		printNote("player got second stone from arenor");
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_Q4_GetStones", "P3_Q4_GetSecondStone", Completed );

		if(m_Reference.GetJournal(m_iPlayerFaction).GetTaskState("P3_Q4_GetStones", "P3_Q4_GetFirstStone") == Completed)
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_Q4_GetStones", "P3_Q4_InvTriggerStonesFinished", Completed );
		}
		// todo: add correct item name
		//m_Reference.GetHeroParty(m_iPlayerFaction).AddItems(, 1);
	}

	// -- Version 2 --
	void Articy_AB2_MentalistWalkToCamp()
	{
		print("Mentalist walking to camp");
		m_Reference.GetWorld().EnableDialogueTopic("Leyna", "AB2_14_FirstContact", true);
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB2_25A_SO_GoToFalcons", false);

		m_Reference.GetCreatureByName("AB2_Mentalist_Captive").GoTo(m_Reference, m_Reference.GetSectorByName("Sector_AonirsDeathman").GetMainBuilding(), 5, 15, true, false);
	}

	void Articy_AB2_GetOldOutpost()
	{
		if(!m_bChoiceAonirsDeathman)
		{
			m_Reference.GetSectorByName(m_stgSect_Deathman).SetOwner(m_iPlayerFaction);
			m_Reference.GetSectorByName(m_stgSect_Deathman).CreateCapital( m_iPlayerFaction, true, false );
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_StrengthenHumans", "P4_MAP016_EstablishBase", Completed );
		}
		else
		{
			m_Reference.GetSectorByName(m_stgSect_Falcons).SetOwner(m_iPlayerFaction);
			m_Reference.GetSectorByName(m_stgSect_Falcons).CreateCapital( m_iPlayerFaction, true, false );
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P4_MAP016_StrengthenHumans", "P4_MAP016_EstablishBase", Completed );
		}


		m_Reference.SetTimer(TOnTimerEvent(@this.AB1_OnTimer_ActivateDeathmenAI), m_iTimerDeathmenAI);

		// .. player start army to balance out RTS party, adjust with following iterations!
		m_Reference.ActivateSpawn("AB2_Sp_PlayerStartArmy", true);
		for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB2_Sp_PlayerStartArmy").size(); i++)
		{
			m_Reference.GetCreaturesFromSpawn("AB2_Sp_PlayerStartArmy")[i].GoTo(m_Reference, m_Reference.GetCreatureByName(m_stgAvatar), 5, 7, false, false);
		}

		for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB2_FalconCampSurvivor2").size(); i++)
		{
			m_Reference.GetCreaturesFromSpawn("AB2_FalconCampSurvivor2")[i].SetFaction(m_iPlayerFaction);
			m_Reference.GetCreaturesFromSpawn("AB2_FalconCampSurvivor2")[i].GoTo(m_Reference, m_Reference.GetCreatureByName(m_stgAvatar), 5, 7, false, false);
		}

		m_Reference.GetCreatureByName("AB2_Falcon_Survivor_02").GoTo(m_Reference, m_Reference.GetSectorsByOwner(m_iPlayerFaction)[0].GetMainBuilding(), 5, 5, true, false);
		m_Reference.GetCreatureByName("AB2_Falcon_Survivor_02").SetFaction(m_iPlayerFaction);
		m_Reference.GetCreatureByName("AB2_Falcon_Survivor").GoTo(m_Reference, m_Reference.GetSectorsByOwner(m_iPlayerFaction)[0].GetMainBuilding(), 5, 5, true, false);
		m_Reference.GetCreatureByName("AB2_Falcon_Survivor").SetFaction(m_iPlayerFaction);

		//m_Reference.GetHeroParty(m_iPlayerFaction).AddItems("BP_H_Armsman", 1);
		m_Reference.GetWorld().UnlockBlueprint("BP_H_Armsman", false);

		m_Reference.GetWorld().BlockDialogueTopicNode("Leyna", "3CEC7D68"); //Unlock one Question in the Dialogue with Leyna
		m_Reference.GetWorld().UnblockDialogueTopicNode("Leyna", "05C128C6"); //Unlock one Question in the Dialogue with Leyna

		OnAB2_GetBlueprintsUnlock();
	}

	bool AB1_OnTimer_ActivateDeathmenAI()
	{
		printNote("activating deathmen AI");
		EnemyAI_Start(m_iFaction_DeathmenRTS);
		return true;
	}

	void Articy_AB2_PaladinsJoin()
	{
		for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB2_FalconPaladins").size(); i++)
		{
			m_Reference.GetCreaturesFromSpawn("AB2_FalconPaladins")[i].SetFaction(m_iPlayerFaction);
		}
	}

	void Articy_AB2_StartMentalistQuest()
	{
		m_Reference.SetTimer(TOnTimerEvent(@this.AB2_MentalistsRelocateToRitual), 3);
	}

	void Articy_AB2_StartRitual()
	{
		print("StartRitual");
		m_Reference.GetCreatureByName("Leyna").Teleport(m_Reference, m_Reference.GetLogicObjectByName( "AB2_Ruin_East" ));
		m_Reference.GetCreatureByName("AB2_Mentalist_Captive").Teleport(m_Reference, m_Reference.GetLogicObjectByName( "AB2_Ruin_West" ));
		m_Reference.GetCreatureByName("AB2_Mentalist_1").Teleport(m_Reference, m_Reference.GetLogicObjectByName( "AB2_Ruin_North" ));
		m_Reference.GetCreatureByName("AB2_Mentalist_2").Teleport(m_Reference, m_Reference.GetLogicObjectByName( "AB2_Ruin_South" ));
		//m_Reference.GetCreatureByName("Leyna").GoTo(m_Reference, m_Reference.GetEntitiesByName("AB2_Ruin_North")[0], 0, 1, false, false);
		//m_Reference.GetCreatureByName("AB2_Mentalist_Captive").GoTo(m_Reference, m_Reference.GetLogicObjectByName( "AB2_Ruin_East" ), 0, 1, false, false);
		//m_Reference.GetCreatureByName("AB2_Mentalist_1").GoTo(m_Reference, m_Reference.GetLogicObjectByName( "AB2_Ruin_South" ), 0, 1, false, false);
		//m_Reference.GetCreatureByName("AB2_Mentalist_2").GoTo(m_Reference, m_Reference.GetLogicObjectByName( "AB2_Ruin_West" ), 0, 1, false, false); //TODO: They are not moving...

		m_Reference.SetTimer(TOnTimerEvent(@this.AB2_StartChanting), 4);
	}

	void Articy_AB2_MentalistQuestComplete()
	{
		m_Reference.GetCreatureByName("Leyna").Enable(false);
		m_Reference.GetWorld().EnableDialogueTopic("AB2_Mentalist_2", "AB2_22_FogOfWar", true);
	}

	void Articy_AB2_ControllMentalists()
	{
		for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB2_Mentalists").size(); i++)
		{
			if(m_Reference.GetCreaturesFromSpawn("AB2_Mentalists")[i].IsEnabled())
			{
				m_Reference.GetCreaturesFromSpawn("AB2_Mentalists")[i].SetFaction(m_iPlayerFaction);
				m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.AB2_OnMentalistEnteredClairvoyanceSpot), m_Reference.GetCreaturesFromSpawn("AB2_Mentalists")[i].GetDescriptionName(), "AB2_ClairvoyanceSpot");
			}
		}
		//m_Reference.GetHeroParty(m_iPlayerFaction).AddItems("BP_H_Mentalist", 1);
		m_Reference.GetWorld().UnlockBlueprint("BP_H_Mentalist", false);
		m_Reference.GetWorld().SetGlobalBool("AB2_MentalistsUnlocked", true);

		m_Reference.RegisterBuildingCreatureEventByDescription(CreaturesSpawned, TOnBuildingCreatureEvent(@this.AB2_OnMentalistSpawned), "H_Mentalist", m_iPlayerFaction);
	}

	void Articy_AB2_UnlockCatapults()
	{
		m_Reference.GetWorld().SetGlobalBool("AB2_CatapultUnlocked", true);
		//m_Reference.GetWorld().UnlockBlueprint("BP_H_Catapult", false);
		//m_Reference.GetHeroParty(m_iPlayerFaction).AddItems("BP_H_Catapult", 1);
		m_Reference.GetWorld().UnlockBlueprint("BP_H_Catapult", false);
	}

	void ArticyEngineerMoveToPlayerCamp()
	{
		m_Reference.GetCreatureByName("AB2_Engineer").FollowPath(m_Reference, "AB2_EngineerPath", true, false, false);
	}

	void ArticyActivateEngineerQuest()
	{
		m_Reference.GetLogicObjectByName("AB2_ToolBox").Enable(true);
		m_Reference.RegisterLogicObjectEvent(Looted, TOnLogicObjectEvent(@this.AB2_OnLootedEngineerTools), "AB2_ToolBox", m_iPlayerFaction);

	}

	void Articy_AB2_UnlockPaladins()
	{
		//m_Reference.GetWorld().UnlockBlueprint("BP_H_Paladin", false);
		//m_Reference.GetHeroParty(m_iPlayerFaction).AddItems("BP_H_Paladin", 1);
		m_Reference.GetWorld().UnlockBlueprint("BP_H_Paladin", false);
		SetFactionSpawn("AB2_FalconPaladins", m_iPlayerFaction);
	}

	void Articy_AB2_ArenorFreed()
	{
		if(m_Reference.GetWorld().GetGlobalBool("AB2_DeathmenDefeated", false) == false)
		{
			m_Reference.GetCreatureByName("Arenor_Arandir").ForceGoTo(m_Reference, m_Reference.GetSectorByName("Sector_IronFalcons").GetMainBuilding(), 5, 10, false, false);
		}
		else
		{
			m_Reference.GetCreatureByName("Arenor_Arandir").GoTo(m_Reference, m_Reference.GetSectorByName("Sector_IronFalcons").GetMainBuilding(), 5, 10, false, false);
			m_Reference.GetWorld().EnableDialogueTopic("Arenor_Arandir", "AB2_36_Reconcile", true);
		}
	}

	void Articy_AB2_FailSafe()
	{
		if(m_Reference.GetWorld().IsBlueprintUnlocked("BP_H_Catapult") == false)
		{
			m_Reference.GetCreatureByName("AB2_Engineer").GoTo(m_Reference, m_Reference.GetSectorByName("Sector_IronFalcons").GetMainBuilding(), 5, 10, false, false);
		}	
		if(m_Reference.GetWorld().IsBlueprintUnlocked("BP_H_Catapult") == false)
		{
			AB2_MentalistsRelocateToBase();
		}
		m_Reference.GetWorld().EnableDialogueTopic("Angar_Arandir", "AB2_37_UnlockFailSave", true);
		EnableWorldMap( true );
	}

	void ArticyStopArenorDialogue()
	{
		m_Reference.FadeToBlack(m_iPlayerFaction, uint8(-1), 1000);
		m_Reference.SetTimer(TOnTimerEvent(@this.AB1_OnTimer_ContinueArenorDialogue), 3);
	}

	bool AB1_OnTimer_ContinueArenorDialogue()
	{
		m_Reference.FadeFromBlack(m_iPlayerFaction, uint8(-1), 1000);
		DialogShoutout("Arenor_Arandir", "Arenor_Arandir", "AB1_Epilogue", false);

		return true;
	}

	void ArticyFalconSoldiersPosition()
	{
		m_Reference.SetTimer(TOnTimerEvent(@this.AB1_OnTimer_FalconSoldiersMoveToPosition), 3);
	}

	bool AB1_OnTimer_FalconSoldiersMoveToPosition()
	{
		printNote("moving soliders to position");
		for(uint i = 0; i < m_Reference.GetCreaturesFromSpawn("AB1_Falcons_Outpost").length(); i++)
		{
			uint j = i+1;
			m_Reference.GetCreaturesFromSpawn("AB1_Falcons_Outpost")[i].EnablePOIVisitingBehavior(false);
			m_Reference.GetCreaturesFromSpawn("AB1_Falcons_Outpost")[i].GoTo(m_Reference, m_Reference.GetLogicObjectByName("AB1_Spot_Tower0" + j), 0, 1, false);
		}

		return true;
	}

	// ---- C U S T U M  F U N C T I O N S --------------------------------------------------------------------------------
	bool RevokeReveal()
	{
		print("Time is up. Getting Dark gain");
		if(m_sRevealedSector != "")
		{
			m_Reference.RemoveVisibleSector(m_iPlayerFaction, m_Reference.GetSectorByName( m_sRevealedSector ).GetIndex());
			m_sRevealedSector = "";
		}

		return true;
	}

	bool CheckEconomy()
	{
		if (m_bRessourceAlreadyTriggered)
			return true;	

		if (m_iWoodcutterNumber >= 1 && m_iFoodCabinNumber >= 1)
		{
			print("[SCRIPT] - NOTE: checked economy - the next dialogue is triggered.");
			Creature _crtCommander;
			if (m_bChoosedIronFaloncs)
				_crtCommander = m_Reference.GetCreatureByName(m_stgCrt_ArenorArandir);
			else if (m_bChoosedAonirsDeathman)
				_crtCommander = m_Reference.GetCreatureByName(m_stgCrt_KaylenVirTrath);

			_crtCommander.EnablePOIVisitingBehavior(false);
			_crtCommander.GoTo( m_Reference, ( m_Reference.GetEntitiesByName(m_stgPos_SectorCamp)[0]), 10, 15, false );

			m_bRessourceAlreadyTriggered = true;
			if(m_bChoiceAonirsDeathman)
			{
				m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Deathmen", "P3_MAP016_Deathmen_GatherStrength", Completed );
			}
			else
			{
				m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_Falcons", "P3_MAP016_Falcons_GatherStrength", Completed );
			}
			return true;
		}
		print("[SCRIPT] - NOTE: checked economy - not ready yet");

		return false;
	}

	bool CheckDwarfBrothers()
	{
		print("[SCIPT] - NOTE: Checking Alive Brothers");

		if(m_bDwarfSaved_03 && m_bDwarfSaved_02 && m_bDwarfSaved_01)
		{
			m_Reference.GetCreatureByName("AB1_Dwarf_Brother_2").Revive();
			m_Reference.GetCreatureByName("AB1_Dwarf_Brother_2").SetAutoRevive(true);
			m_Reference.GetCreatureByName("AB1_Dwarf_Brother_1").Revive();
			m_Reference.GetCreatureByName("AB1_Dwarf_Brother_1").SetAutoRevive(true);
			m_Reference.GetCreatureByName("AB1_Dwarf_Brother_3").Revive();
			m_Reference.GetCreatureByName("AB1_Dwarf_Brother_3").SetAutoRevive(true);
		}


		if(!IsSpawnAlive("Undead_DB_Attack_01") && !IsSpawnAlive("Undead_DB_Attack_02") && !IsSpawnAlive("Undead_DB_Attack_03") && !IsSpawnAlive("Undead_DB_Attack_04"))
		{
			// only check, if all three Dwarf Brothers where visited by the player
			if (IsSpawnAlive("AB1_SpawnDwarf01") && !IsSpawnAlive("AB1_SpawnDwarf02") && !IsSpawnAlive("AB1_SpawnDwarf03"))
			{
				m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_DwarfBrother_01, "AB1_NoDwarvesSaved", true);
			}
			else if (IsSpawnAlive("AB1_SpawnDwarf01") && !IsSpawnAlive("AB1_SpawnDwarf02") && IsSpawnAlive("AB1_SpawnDwarf03") ||
				IsSpawnAlive("AB1_SpawnDwarf01") && IsSpawnAlive("AB1_SpawnDwarf02") && !IsSpawnAlive("AB1_SpawnDwarf03"))
			{
				m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_DwarfBrother_01, "AB1_OneDwarfSaved", true);
				m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).EnableTrade(true);
			}
			else if (IsSpawnAlive("AB1_SpawnDwarf01") && IsSpawnAlive("AB1_SpawnDwarf02") && IsSpawnAlive("AB1_SpawnDwarf03"))
			{
				m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_DwarfBrother_01, "AB1_AllDwarvesSaved", true);
				m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).EnableTrade(true);
			}


			if(!m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Exists() &&
				!m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Exists())
			{
				m_Reference.GetJournal(m_iPlayerFaction).ActivateTask("SQ_016_Dwarves", "SQ_016_Dwarves_TalkToFirstBrother_NoSaved", true);
				m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("SQ_016_Dwarves", "SQ_016_Dwarves_TalkToFirstBrother_OneSaved", Failed);
			}
			else if(!m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Exists() ||
				!m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Exists())
			{
				m_Reference.GetJournal(m_iPlayerFaction).ActivateTask("SQ_016_Dwarves", "SQ_016_Dwarves_TalkToFirstBrother_OneSaved", true);
				m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("SQ_016_Dwarves", "SQ_016_Dwarves_TalkToFirstBrother_AllSaved", Failed);
			}
			else if(m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Exists() &&
				m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Exists())
			{
				m_Reference.GetJournal(m_iPlayerFaction).ActivateTask("SQ_016_Dwarves", "SQ_016_Dwarves_TalkToFirstBrother_AllSaved", true);
			}


			// check all dialogue options

			// check, if the third dwarf is still alive
			if (m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Exists())
			{
				print("[SCRIPT] - NOTE: 3rd Dwarf Brother was saved");
				if(!m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Exists())
				{
					m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_DwarfBrother_03, "AB1_Dwarf3Saved", true);
				}
				else
				{
					// goto brother
					m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).FollowCreature (m_Reference, m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02), 0,50, false);
				}
				m_Reference.GetWorld().SetGlobalBool("AB_SavedDwarfBrother_3", true);
			}
			else
			{
				print("[SCRIPT] - NOTE: 3rd Dwarf Brother could not be saved");
			}

			// check, if the second dwarf is still alive
			if (m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Exists())
			{
				print("[SCRIPT] - NOTE: 2nd Dwarf Brother was saved");
				m_Reference.GetWorld().EnableDialogueTopic( m_stgCrt_DwarfBrother_02, "AB1_Dwarf2Saved", true);
				m_Reference.GetWorld().SetGlobalBool("AB_SavedDwarfBrother_2", true);
			}
			else
			{
				print("[SCRIPT] - NOTE: 2nd Dwarf Brother could not be saved");
			}
		}

		return false;
	}

	bool DwarfBrothers_StartAttack_01()
	{
		// start the Undead Attack
		if (m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).GetCurrentHP() > 0 )
		{
			
			// let the brother be attacked
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).SetAttackable(true);
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_01).AllowAttacks(true);

			CreatureGroup _crtUndead ( m_Reference );
			_crtUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_DB_Attack_01") );
			_crtUndead.AttackEntity(m_Reference.GetEntitiesByName(m_stgCrt_DwarfBrother_01)[0], false);
		}

		return true;
	}

	bool DwarfBrothers_StartAttack_02()
	{
		// start the Undead Attack
		if (m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).GetCurrentHP() > 0 )
		{
			m_Reference.SetFactionRelation(m_iPlayerFaction, 8, Allied);
			// let the brother be attacked
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).SetAttackable(true);
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).AllowAttacks(true);

			CreatureGroup _crtUndead ( m_Reference );
			_crtUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_DB_Attack_02") );
			_crtUndead.AttackEntity(m_Reference.GetEntitiesByName(m_stgCrt_DwarfBrother_02)[0], false);
		}

		return true;
	}

	bool DwarfBrothers_StartAttack_03()
	{
		// start the Undead Attack
		if (m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).GetCurrentHP() > 0 )
		{
			// let the brother be attacked
			m_Reference.SetFactionRelation(m_iPlayerFaction, 8, Allied);
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).SetAttackable(true);
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).AllowAttacks(true);

			CreatureGroup _crtUndead ( m_Reference );
			_crtUndead.Set( m_Reference.GetCreaturesFromSpawn("Undead_DB_Attack_03") );
			_crtUndead.AttackEntity(m_Reference.GetEntitiesByName(m_stgCrt_DwarfBrother_03)[0], false);
		}

		return true;
	}

	bool StartTrebuchetAttacking ()
	{
		m_Reference.GetCreatureByName(m_stgCrtTrebuchet).AllowAttacks(true);
		CreatureGroup _crtTrebuchet ( m_Reference );
		_crtTrebuchet.Add(m_Reference.GetCreatureByName(m_stgCrtTrebuchet));
		_crtTrebuchet.AttackPosition(m_Reference.GetBuildingByName(m_stgBldg_TrebuchetAttack), false, false);
		m_Reference.GetCreatureByName(m_stgCrtTrebuchet).SetPlayerControllable(true);
		m_Reference.GetCreatureByName("AB1_Trebuchet").RemoveCondition(10201);
		return true;
	}

	bool ActivateSummoningMiniboss ()
	{
		print("[SCRIPT] - NOTE: Undead Miniboss stuff is starting now - sending the scout to inform the player");

		if (m_bChoosedIronFaloncs == true)
		{
			m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnWarnMiniboss), m_stgCrt_ScoutFalcons, m_stgTrg_SectorCamp_Falcons );
			m_Reference.GetCreatureByName( m_stgCrt_ScoutFalcons ).GoTo( m_Reference, ( m_Reference.GetLogicObjectByName(m_stgObj_Camp_Falcons)), 10, 15, false );
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_Undead_Falcons", Completed );
		}
		else if (m_bChoosedAonirsDeathman == true)
		{
			m_Reference.GetCreatureByName( m_stgCrt_ScoutDeathman ).GoTo( m_Reference, ( m_Reference.GetLogicObjectByName(m_stgObj_Camp_Deathman)), 10, 15, false );
			m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnWarnMiniboss), m_stgCrt_ScoutDeathman, m_stgTrg_SectorCamp_Deathmen );
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_TalkScout_Deathman", Completed );
		}

		return true;
	}

	bool StartSummoning ()
	{
		if (m_bSummonMiniboss == false || m_bAttackSuccess == true)
			return true;
		
		m_iSummoningEffectsCurrent++;
		print("[SCRIPT] - NOTE: Summon the miniboss, Phase: "+m_iSummoningEffectsCurrent+" of "+m_iSummoningEffects+" next phase in "+(m_iMinibossSummoningTime / m_iSummoningEffects)+" seconds");
		m_Reference.GetLogicObjectByName( m_stgObjSummoningEffect + m_iSummoningEffectsCurrent ).Enable(true);
		PlaySound (  "AB1_Summoning_Staff", "events/ab1_tribeeater_circle" ); 

		if (m_iSummoningEffectsCurrent == m_iSummoningEffects)
			SpawnMiniboss();
		else
		{
			m_Reference.CreateCountdownTimer(0, m_iMinibossSummoningTime, TOnTimerEvent(@this.StopSummoningTimer), m_iPlayerFaction , "AB1_Timer_UndeadSQ");
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_Undead_InvTriggerTimer", Completed);
			m_Reference.SetTimer( TOnTimerEvent(@this.StartSummoning), (m_iMinibossSummoningTime / m_iSummoningEffects) );
		}


		return true;
	}

	bool StopSummoningTimer()
	{

		return true;
	}

	bool SpawnMiniboss ()
	{
		print("[SCRIPT] - NOTE: RELEASE THE ZOMBIE (the Undead Miniboss with the necromancer) ");
		
		//m_Reference.ActivateSpawn( m_stgSpawn_Miniboss, true);
		m_Reference.CastSpell("Summon_CoreDweller_AB1", kFactionMob, m_Reference.GetLogicObjectByName("AB1_Spot_CoreDweller"));
		
		m_Reference.SetTimer(TOnTimerEvent(@this.AB1_SetupCoreDwoller), 3);

		/*

		m_Reference.GetBuildingByName(m_stgBldg_Summoning).Damage( Siege, m_Reference.GetBuildingByName(m_stgBldg_Summoning).GetMaxHP() );

		// handle the Miniboss
		Creature _CrtMiniboss = m_Reference.GetCreatureByName("AB1_BlackDemon_RitualBoss");
		//Creature _CrtMiniboss = m_Refer//m_Reference.GetCreaturesFromSpawn(m_stgSpawn_Miniboss)[0];
		CreatureGroup _crtGrpMiniboss ( m_Reference );
		_crtGrpMiniboss.Add(_CrtMiniboss);

		_crtGrpMiniboss.AttackPosition(m_Reference.GetEntitiesByName(m_stgPos_SectorCamp_Deathmen)[0], true);

		m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnMinibossAttacks_Deathmen), _CrtMiniboss, "SO_MinibossIsComing");
		m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnMinibossAttacks_Falcon), _CrtMiniboss, "SO_DeathmenUnderAttack" );

		// handle the Zombie Minions, let all of them follow the big boss
		array <Creature>@ _entMinions_01 = m_Reference.GetCreaturesFromSpawn( m_stgSpawn_SumminedMinions_01 );
		for (uint _iTmp1 = 0; _iTmp1 < _entMinions_01.length(); _iTmp1++ )
			_entMinions_01[_iTmp1].FollowCreature(m_Reference, _CrtMiniboss, 30, 50, false);
		array <Creature>@ _entMinions_02 = m_Reference.GetCreaturesFromSpawn( m_stgSpawn_SumminedMinions_02 );
		for (uint _iTmp2 = 0; _iTmp2 < _entMinions_02.length(); _iTmp2++ )
			_entMinions_02[_iTmp2].FollowCreature(m_Reference, _CrtMiniboss, 50, 60, false);
		array <Creature>@ _entMinions_03 = m_Reference.GetCreaturesFromSpawn( m_stgSpawn_SumminedMinions_03 );
		for (uint _iTmp3 = 0; _iTmp3 < _entMinions_03.length(); _iTmp3++ )
			_entMinions_03[_iTmp3].FollowCreature(m_Reference, _CrtMiniboss, 30, 50, false);

		// Handle the summoning circle and building
		for (int _iTmp0 = 1; _iTmp0 <= m_iSummoningEffects ;_iTmp0++)
			m_Reference.GetLogicObjectByName( m_stgObjSummoningEffect + _iTmp0 ).Enable(false);

		if(m_bChoiceIronFaloncs)
		{
			//_CrtMiniboss.ForceGoTo(m_Reference, m_Reference.GetLogicObjectByName("ScoutPosition_01"), 10, 15, false, false);
			_CrtMiniboss.AttackPosition(m_Reference, m_Reference.GetLogicObjectByName("ScoutPosition_01"), false, false);
		}
		else
		{
			//_CrtMiniboss.ForceGoTo(m_Reference, m_Reference.GetLogicObjectByName("ScoutPosition_02"), 10, 15, false, false);
			_CrtMiniboss.AttackPosition(m_Reference, m_Reference.GetLogicObjectByName("ScoutPosition_02"), false, false);
		}

		// fail sacrifice state
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_SacrificeUnit", Failed );

		// fail previous states if not completed
		if(m_Reference.GetJournal(m_iPlayerFaction).GetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_TalkScout") != Completed)
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_TalkScout", Completed );
		}
		if(m_Reference.GetJournal(m_iPlayerFaction).GetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_InvestigateSite") != Completed)
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_InvestigateSite", Completed );
		}

		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_DoNothing", Completed);
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB1_OnKilled_SpawnedMiniBoss), m_Reference.GetCreaturesFromSpawn(m_stgSpawn_Miniboss), true, "");
		m_bSpawnedMiniBoss = true;


		m_Reference.RegisterHeroPartyEvent(AnyCanSee, TOnHeroPartyEvent(@this.AB1_OnAnyCanSee_HeroPartySeesMiniBoss), "AB1_BlackDemon_RitualBoss", m_iPlayerFaction);*/
		return true;
	}


	bool AB1_SetupCoreDwoller()
	{
		m_Reference.GetBuildingByName(m_stgBldg_Summoning).Damage( Siege, m_Reference.GetBuildingByName(m_stgBldg_Summoning).GetMaxHP() );

		// handle the Miniboss
		Creature _CrtMiniboss = m_Reference.GetCreatureByName("AB1_BlackDemon_RitualBoss");
		//Creature _CrtMiniboss = m_Refer//m_Reference.GetCreaturesFromSpawn(m_stgSpawn_Miniboss)[0];
		CreatureGroup _crtGrpMiniboss ( m_Reference );
		_crtGrpMiniboss.Add(_CrtMiniboss);

		_crtGrpMiniboss.AttackPosition(m_Reference.GetEntitiesByName(m_stgPos_SectorCamp_Deathmen)[0], true);

		m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnMinibossAttacks_Deathmen), "AB1_BlackDemon_RitualBoss", "SO_MinibossIsComing");
		m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.OnMinibossAttacks_Falcon), "AB1_BlackDemon_RitualBoss", "SO_DeathmenUnderAttack" );
													// (const ECreatureEvent _EventType, TOnCreatureEvent@ _pFunction, const string&in _sCreatureName, const string&in _sParam)

		// handle the Zombie Minions, let all of them follow the big boss
		array <Creature>@ _entMinions_01 = m_Reference.GetCreaturesFromSpawn( m_stgSpawn_SumminedMinions_01 );
		for (uint _iTmp1 = 0; _iTmp1 < _entMinions_01.length(); _iTmp1++ )
			_entMinions_01[_iTmp1].FollowCreature(m_Reference, _CrtMiniboss, 30, 50, false);
		array <Creature>@ _entMinions_02 = m_Reference.GetCreaturesFromSpawn( m_stgSpawn_SumminedMinions_02 );
		for (uint _iTmp2 = 0; _iTmp2 < _entMinions_02.length(); _iTmp2++ )
			_entMinions_02[_iTmp2].FollowCreature(m_Reference, _CrtMiniboss, 50, 60, false);
		array <Creature>@ _entMinions_03 = m_Reference.GetCreaturesFromSpawn( m_stgSpawn_SumminedMinions_03 );
		for (uint _iTmp3 = 0; _iTmp3 < _entMinions_03.length(); _iTmp3++ )
			_entMinions_03[_iTmp3].FollowCreature(m_Reference, _CrtMiniboss, 30, 50, false);

		// Handle the summoning circle and building
		for (int _iTmp0 = 1; _iTmp0 <= m_iSummoningEffects ;_iTmp0++)
			m_Reference.GetLogicObjectByName( m_stgObjSummoningEffect + _iTmp0 ).Enable(false);

		if(m_bChoiceIronFaloncs)
		{
			//_CrtMiniboss.ForceGoTo(m_Reference, m_Reference.GetLogicObjectByName("ScoutPosition_01"), 10, 15, false, false);
			_CrtMiniboss.AttackPosition(m_Reference, m_Reference.GetLogicObjectByName("ScoutPosition_01"), false, false);
		}
		else
		{
			//_CrtMiniboss.ForceGoTo(m_Reference, m_Reference.GetLogicObjectByName("ScoutPosition_02"), 10, 15, false, false);
			_CrtMiniboss.AttackPosition(m_Reference, m_Reference.GetLogicObjectByName("ScoutPosition_02"), false, false);
		}

		// fail sacrifice state
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_SacrificeUnit", Failed );

		// fail previous states if not completed
		if(m_Reference.GetJournal(m_iPlayerFaction).GetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_TalkScout") != Completed)
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_TalkScout", Completed );
		}
		if(m_Reference.GetJournal(m_iPlayerFaction).GetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_InvestigateSite") != Completed)
		{
			m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_InvestigateSite", Completed );
		}

		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_DoNothing", Completed);
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB1_OnKilled_SpawnedMiniBoss), m_Reference.GetCreaturesFromSpawn(m_stgSpawn_Miniboss), true, "");
		m_bSpawnedMiniBoss = true;


		m_Reference.RegisterHeroPartyEvent(AnyCanSee, TOnHeroPartyEvent(@this.AB1_OnAnyCanSee_HeroPartySeesMiniBoss), "AB1_BlackDemon_RitualBoss", m_iPlayerFaction);

		return true;
	}

	bool AB1_OnAnyCanSee_HeroPartySeesMiniBoss(Creature& in _Creature)
	{
		DialogShoutout(m_stgAvatar, m_stgAvatar, "AB1_SO_BossSpawned", true);
		return true;
	}

	bool AB1_OnKilled_SpawnedMiniBoss(Creature& in _Creature)
	{
		printNote("Player killed the spawned miniboss");
		m_Reference.GetJournal(m_iPlayerFaction).SetTaskState("P3_MAP016_SQ04_Undead", "P3_MAP016_SQ04_DefeatAbomination", Completed);
		return true;
	}

	//-- Version 2 --
	void AB2_CheckAmbush()
	{
		if(!IsSpawnAlive("AB2_Ambush01") && !IsSpawnAlive("AB2_Ambush02"))
		{
			DialogShoutout(m_stgAvatar, m_stgAvatar, "AB2_04_AfterAmbush", false);
		}
	}

	bool AB2_MentalistsRelocateToRitual()
	{
		print("mentalists go to ritual site");
		CreatureGroup _crtMentalists ( m_Reference );
		_crtMentalists.Set( m_Reference.GetCreaturesFromSpawn( "AB2_Mentalists" ) );
		_crtMentalists.Add(m_Reference.GetCreatureByName("AB2_Mentalist_Captive"));
		_crtMentalists.Teleport(m_Reference.GetEntitiesByName("AB_SecretTreasure")[0], false);

		m_Reference.GetWorld().EnableDialogueTopic("Leyna", "AB2_15_WaitingForStart", true);
		return true;
	}

	bool AB2_MentalistsRelocateToBase()
	{
		printNote("Mentalists go back to camp");
		CreatureGroup _crtMentalists ( m_Reference );
		_crtMentalists.Set( m_Reference.GetCreaturesFromSpawn( "AB2_Mentalists" ) );
		_crtMentalists.Add(m_Reference.GetCreatureByName("AB2_Mentalist_Captive"));
		_crtMentalists.Teleport(m_Reference.GetSectorByName("Sector_AonirsDeathman").GetMainBuilding(), false);
		return true;
	}

	bool AB2_StartChanting()
	{
		print("Start Chanting");
		DialogShoutout("Leyna", "Leyna", "AB2_16_SO_RitualChant", false);
		DialogShoutout("AB2_Mentalist_1", "AB2_Mentalist_1", "AB2_18_SO_RitualChant", false);
		DialogShoutout("AB2_Mentalist_2", "AB2_Mentalist_2", "AB2_17_SO_RitualChant", false);

		m_Reference.SetTimer(TOnTimerEvent(@this.AB2_StartUndeadAttackOnMentalists), 2);
		return true;
	}

	bool AB2_StartUndeadAttackOnMentalists()
	{
		print("Start Attack");
		m_Reference.ActivateSpawn("AB2_UndeadAttackOnRitual01", true);
		m_Reference.ActivateSpawn("AB2_UndeadAttackOnRitual02", true);

		m_Reference.RegisterCreatureEventByIndividual(Damaged, TOnCreatureEvent(@this.AB2_OnMentalistDamaged), "Leyna", "");
		m_Reference.RegisterCreatureEventByIndividual(Damaged, TOnCreatureEvent(@this.AB2_OnMentalistDamaged), "AB2_Mentalist_1", "");
		m_Reference.RegisterCreatureEventByIndividual(Damaged, TOnCreatureEvent(@this.AB2_OnMentalistDamaged), "AB2_Mentalist_2", "");
		m_Reference.RegisterCreatureEventByIndividual(Damaged, TOnCreatureEvent(@this.AB2_OnMentalistDamaged), "AB2_Mentalist_Captive", "");

		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB2_OnKilledUndeadDuringRitual), m_Reference.GetCreaturesFromSpawn("AB2_UndeadAttackOnRitual01"), false, "");
		m_Reference.RegisterCreatureEventByIndividuals(Killed, TOnCreatureEvent(@this.AB2_OnKilledUndeadDuringRitual), m_Reference.GetCreaturesFromSpawn("AB2_UndeadAttackOnRitual02"), false, "");
		return true;
	}

	bool AB2_OnMentalistSpawned(Building&in _Building, Creature[]&in _Creatures)
	{
		print("A Mentalist Spawned");
		for(uint i = 0; i < _Creatures.size(); i++)
		{
			m_Reference.RegisterCreatureEventByIndividual(EnteredArea, TOnCreatureEvent(@this.AB2_OnMentalistEnteredClairvoyanceSpot), _Creatures[i].GetDescriptionName(), "AB2_ClairvoyanceSpot");
		}
		return false;
	}


	void ArticyAB1_RemainingDwarvesStartFollowingPlayer()
	{
		if(m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Exists())
		{
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).FollowCreature (m_Reference, m_Reference.GetCreatureByName(m_stgAvatar), 0,5, false);
		}

		if(m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Exists())
		{
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).FollowCreature (m_Reference, m_Reference.GetCreatureByName(m_stgAvatar), 0,5, false);
		}
	}

	void ArticyAB1_RemainingDwarvesStopFollowingPlayer()
	{
		if(m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Exists())
		{
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_03).Stop(m_Reference);
		}

		if(m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Exists())
		{
			m_Reference.GetCreatureByName(m_stgCrt_DwarfBrother_02).Stop(m_Reference);
		}
	}






	// -------------------------------------------------------------------------------------------------------------------
	// --- C U S T O M  F U N C T I O N S --------------------------------------------------------------------------------
	// -------------------------------------------------------------------------------------------------------------------
	void AB1_Cutscene_PlayFialDargCombatSoundtrack( )
	{
		PlaySetting_desert_high( );
		//m_Reference.SuspendAutomaticCombatMusic( m_iPlayerFaction, uint8( -1 ), true );

		//m_Reference.SetGlobalSoundParameter( m_iPlayerFaction, uint8( -1 ), "combat", 1 );
		//m_Reference.SetGlobalSoundParameter( m_iPlayerFaction, uint8( -1 ), "intro", 1 );
		//m_Reference.SetGlobalSoundParameter( m_iPlayerFaction, uint8( -1 ), "intensity", 1000 );
	}

	void AB1_Cutscene_EndFialDargCombatSoundtrack( )
	{
		PlaySetting_Reset( "desert" );
		//m_Reference.SetGlobalSoundParameter( m_iPlayerFaction, uint8( -1 ), "combat", 0 );
		//m_Reference.SetGlobalSoundParameter( m_iPlayerFaction, uint8( -1 ), "intro", 0 );
		//m_Reference.SetGlobalSoundParameter( m_iPlayerFaction, uint8( -1 ), "intensity", 0 );

		//m_Reference.SuspendAutomaticCombatMusic( m_iPlayerFaction, uint8( -1 ), false );
	}

	void AB1_Cutscene_KiroyStartRitual( )
	{
		m_Reference.EnableGroup("AB1_KiroyCastStart", true);
	}

	void AB1_Cutscene_KiroyFinishRitual()
	{
		// activate visual effects
		m_Reference.EnableGroup("AB1_KiroyCastStart", false);
		m_Reference.EnableGroup("AB1_KiroyCastOnce", true);
		m_Reference.EnableGroup("AB1_KiroyCastEnd", true);
		m_Reference.EnableLight("AB_KiroyCast_Light_1", true);
		m_Reference.EnableLight("AB_KiroyCast_Light_2", true);

		// block entrance to the fial darg tomb
		m_Reference.BlockNavMesh( "AB_FialDargTomb_PhysicBlocker", true );

		// kill Kiroy_Teranor
		m_Reference.GetCreatureByName( "Kiroy_Teranor" ).KillAndPreserve( m_Reference );

		m_Reference.SetTimer( TOnTimerEvent(@this.AB1_Cutscene_KiroyFinishRitual_DisableEffect), 2);
	}

	bool AB1_Cutscene_KiroyFinishRitual_DisableEffect()
	{
		// disable one shot effects
		m_Reference.EnableGroup( "AB1_KiroyCastOnce", false );

		return true;
	}

	void AB1_Cutscene_KiroyToggleAnimation( )
	{
		m_Reference.GetCreatureByName( "Kiroy_Teranor" ).PlayAnimation( m_Reference, CastLoop, -1, 999999999, true, false, Entity( ) );
	}

	void AB1_Cutscene_PlayFialDargScreamSound( )
	{
		//m_Reference.PlaySound( m_iPlayerFaction, uint8( -1 ), "events/ab1_fialdarg_scream", "" );
	}

	void AB1_Cutscene_PlayFialDargDeathSound( )
	{
		//m_Reference.PlaySound( m_iPlayerFaction, uint8( -1 ), "events/ab1_fialdarg_tomb_sealed" );
	}

	void AB1_CutsceneMapIntro()
	{
		//Trigger "MapIntro_1" Dialogue
		m_Reference.PlayCutScene("AB1_01_MapIntro");
		m_Reference.RegisterUIEvent(CutsceneFinished, TOnUIEvent(@this.AB1_OnCutsceneFinished_MapIntro1), m_iPlayerFaction);
	}

	bool AB1_OnCutsceneFinished_MapIntro1(const uint8 _uPlayerFaction)
	{
		m_Reference.RegisterHeroPartyEvent(AnyInside, TOnHeroPartyEvent(@this.AB1_OnSeeSeerBones), "AB1_SeeSeerBones", m_iPlayerFaction);
		AB1_OnTimer_FalconSoldiersMoveToPosition();
		return true;
	}

	void AB2_CutsceneMapIntro()
	{
		//Trigger "MapIntro_2" Dialogue
		m_Reference.PlayCutScene("AB2_01_MapIntro");
	}

	void OnAB2_GetBlueprintsUnlock()
	{
		OnAB2_GetBlueprintsUnlock_Add("BP_HumanRace");

		// Human T1/T2 - Buildings
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Barracks");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Capital_I");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Capital_II");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Expansion_0");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Expansion_I");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Expansion_II");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_WoodenTower_I");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_WoodenTower_II");
		// .. orcs have no towers to build anymore due to mp changes

		OnAB2_GetBlueprintsUnlock_Add("BP_H_Farm");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Woodcutter");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Stonecutter");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_HuntingCabin");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_FishingCabin");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_IronMine");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_IronSmeltery");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Smith");

		// Human T1/T2 - Units
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Pillager");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Recruit");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Soldier");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Helebardier");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Scout");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Marksman");

		// Human T3 - Buildings
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Capital_III");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Expansion_III");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_Academy");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_AriaCollector");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_AriaDestillery");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_WoodenTower_III");
		OnAB2_GetBlueprintsUnlock_Add("BP_H_StoneTower");
	}

	void OnAB2_GetBlueprintsUnlock_Add( string _stgBpUnlock)
	{
		// Permanent
		m_Reference.GetWorld().UnlockBlueprint(_stgBpUnlock, false);
	}


	bool AB1_OnInteract_PhoenixShrine(Creature& in _Creature)
	{
		if(m_Reference.GetHeroParty(m_iPlayerFaction).HasItems("AP_FragmentOfPhoenixShield", 3))
		{
			m_Reference.GetLogicObjectByName("AB_Artifact_Phoenix_LootChest").Enable(true);
			m_Reference.GetLogicObjectByName("AB_Artifact_Phoenix_LootChest_FX").Enable(true);
			m_Reference.GetHeroParty(m_iPlayerFaction).RemoveItems ("AP_FragmentOfPhoenixShield", 3);
			m_Reference.SetTimer(TOnTimerEvent(@this.AB1_OnTimer_DisableArtifactFX_Phoenix), 2);
			DialogShoutout_Generic_ValuableLoot();
			return true;
		}
		return false;
	}

	bool AB1_OnTimer_DisableArtifactFX_Phoenix()
	{
		m_Reference.GetLogicObjectByName("AB_Artifact_Phoenix_LootChest_FX").Enable(false);
		return true;
	}
}
