function createLastFightArmy()
	local EndBossFirstRoom = {}
	EndBossFirstRoom[1] = GetPosition("endfightfirstpoint1")
	EndBossFirstRoom[1].Generator = {"endfightfirstpointTower1"} 
	EndBossFirstRoom[2] = GetPosition("endfightfirstpoint2")
	EndBossFirstRoom[2].Generator = {"endfightfirstpointTower2"} 
	UAFirstRoomDef = UnlimitedArmy:New({
					Player = 6,
					Area = 4200,
					AutoDestroyIfEmpty = true,
					TransitAttackMove = true,
					Formation = UnlimitedArmy.Formations.Chaotic,
					LeaderFormation = FormationFunktion,
					AIActive = true,
					AutoRotateRange = true,
					DoNotNormalizeSpeed = true
					})
	UAFirstRoomDefSpawner = UnlimitedArmySpawnGenerator:New(UAFirstRoomDef,{
					Position = EndBossFirstRoom,
					ArmySize = 20,
					SpawnCounter = 5,
					SpawnLeaders = 15,
					LeaderDesc = {
								{LeaderType = Entities.PU_LeaderSword4, SoldierNum = 8, SpawnNum = 5, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderBow4, SoldierNum = 8,SpawnNum = 4, Looped = true, Experience = 3},
								{LeaderType = Entities.PV_Cannon4, SoldierNum = 0, SpawnNum = 3, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderHeavyCavalry2, SoldierNum = 3, SpawnNum = 6, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderBow4, SoldierNum = 8,SpawnNum = 4, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderPoleArm4, SoldierNum = 8, SpawnNum = 5, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderCavalry2, SoldierNum = 3, SpawnNum = 5, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderSword1, SoldierNum = 0, SpawnNum = 1, Looped = true, Experience = 3},
								{LeaderType = Entities.PV_Cannon3, SoldierNum = 0, SpawnNum = 3, Looped = true, Experience = 3}
								},
								RandomizeSpawn = true,
								RandomizeSpawnPoint = true,
					})
	UAFirstRoomDef:AddCommandDefend(GetPosition("firstdefense"), 7500, true)	
	UAFirstRoomAttack = UnlimitedArmy:New({
						Player = 6,
						Area = 4200,
						AutoDestroyIfEmpty = true,
						TransitAttackMove = true,
						Formation = UnlimitedArmy.Formations.Chaotic,
						LeaderFormation = FormationFunktion,
						AIActive = true,
						AutoRotateRange = true,
						DoNotNormalizeSpeed = true,
						})
	UAFirstRoomAttackSpawner = UnlimitedArmySpawnGenerator:New(UAFirstRoomAttack,{
					Position = EndBossFirstRoom,
					ArmySize = 30,
					SpawnCounter = 5,
					SpawnLeaders = 15,
					LeaderDesc = {
								{LeaderType = Entities.PU_LeaderSword4, SoldierNum = 8, SpawnNum = 5, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderBow4, SoldierNum = 8,SpawnNum = 4, Looped = true, Experience = 3},
								{LeaderType = Entities.PV_Cannon4, SoldierNum = 0, SpawnNum = 3, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderHeavyCavalry2, SoldierNum = 3, SpawnNum = 6, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderBow4, SoldierNum = 8,SpawnNum = 4, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderPoleArm4, SoldierNum = 8, SpawnNum = 5, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderCavalry2, SoldierNum = 3, SpawnNum = 5, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderSword1, SoldierNum = 0, SpawnNum = 1, Looped = true, Experience = 3},
								{LeaderType = Entities.PV_Cannon3, SoldierNum = 0, SpawnNum = 3, Looped = true, Experience = 3}
								},
								RandomizeSpawn = true,
								RandomizeSpawnPoint = true,
					})
	UAFirstRoomAttack:AddCommandLuaFunc(UAAttackersLuaFunc,true)
	local EndBossSecondRoomBarracks = {}
	EndBossSecondRoomBarracks[1] = GetPosition("GarnisonPos1")
	EndBossSecondRoomBarracks[1].Generator = "Barracks2"
	EndBossSecondRoomBarracks[2] = GetPosition("GarnisonPos2")
	EndBossSecondRoomBarracks[2].Generator = "Barracks3"
	EndBossSecondRoomBarracks[3] = GetPosition("GarnisonPos3")
	EndBossSecondRoomBarracks[3].Generator = "Barracks1"
	UASecondRoomAttackBarracks = UnlimitedArmy:New({
						Player = 6,
						Area = 3000,
						AutoDestroyIfEmpty = true,
						TransitAttackmove = true,
						Formation = UnlimitedArmy.Formations.Chaotic,
						LeaderFormation = FormationFunktion,
						AIActive = true,
						AutoRotateRange = true,
						DoNotNormalizeSpeed = true,
						})
	UASecondRoomAttackBarracksSpawner = UnlimitedArmySpawnGenerator:New(UASecondRoomAttackBarracks,{
						Position = EndBossSecondRoomBarracks,
						ArmySize = 12,
						SpawnCounter = 5,
						SpawnLeaders = 10,
						LeaderDesc = {
									{LeaderType = Entities.PU_LeaderSword4, SoldierNum = 8, SpawnNum = 2, Looped = true, Experience = 3},
									{LeaderType = Entities.PU_LeaderPoleArm4, SoldierNum = 8, SpawnNum = 2, Looped = true, Experience = 3},
									{LeaderType = Entities.CU_BlackKnight_LeaderMace1, SoldierNum = 4, SpawnNum = 1, Looped = true, Experience = 3}
									},
									RandomizeSpawn = true,
									RandomizeSpawnPoint = true
									})
	Trigger.DisableTrigger(UASecondRoomAttackBarracks.Trigger)
	
	local EndBossSecondRoomArchers = {}
	EndBossSecondRoomArchers[1] = GetPosition("ArcheryPos1")
	EndBossSecondRoomArchers[1].Generator = "Archers3"
	EndBossSecondRoomArchers[2] = GetPosition("ArcheryPos2")
	EndBossSecondRoomArchers[2].Generator = "Archers1"
	EndBossSecondRoomArchers[3] = GetPosition("ArcheryPos3")
	EndBossSecondRoomArchers[3].Generator = "Archers2"
	UASecondRoomAttackArchers = UnlimitedArmy:New({
						Player = 6,
						Area = 4200,
						AutoDestroyIfEmpty = true,
						TransitAttackmove = true,
						Formation = UnlimitedArmy.Formations.Chaotic,
						LeaderFormation = FormationFunktion,
						AIActive = true,
						AutoRotateRange = true,
						DoNotNormalizeSpeed = true,
						})
	UASecondRoomAttackArchersSpawner = UnlimitedArmySpawnGenerator:New(UASecondRoomAttackArchers,{
						Position = EndBossSecondRoomArchers,
						ArmySize = 10,
						SpawnCounter = 5,
						SpawnLeaders = 10,
						LeaderDesc = {
									{LeaderType = Entities.PU_LeaderBow4, SoldierNum = 8, SpawnNum = 10, Looped = true, Experience = 3}
									},
						RandomizeSpawnPoint = true
						})
	Trigger.DisableTrigger(UASecondRoomAttackArchers.Trigger)
	
	local EndBossSecondRoomStables = {}
	EndBossSecondRoomStables[1] = GetPosition("Stables1Pos")
	EndBossSecondRoomStables[1].Generator = "Stables1"
	EndBossSecondRoomStables[2] = GetPosition("Stables2Pos")
	EndBossSecondRoomStables[2].Generator = "Stables2"
	UASecondRoomAttackStables = UnlimitedArmy:New({
						Player = 6,
						Area = 3800,
						AutoDestroyIfEmpty = true,
						TransitAttackmove = true,
						Formation = UnlimitedArmy.Formations.Chaotic,
						LeaderFormation = FormationFunktion,
						AIActive = true,
						AutoRotateRange = true,
						DoNotNormalizeSpeed = true,
						})
	UASecondRoomAttackStablesSpawner = UnlimitedArmySpawnGenerator:New(UASecondRoomAttackStables,{
						Position = EndBossSecondRoomStables,
						ArmySize = 12,
						SpawnCounter = 5,
						SpawnLeaders = 10,
						LeaderDesc = {
									{LeaderType = Entities.PU_LeaderHeavyCavalry2, SoldierNum = 3, SpawnNum = 3, Looped = true, Experience = 3},
									{LeaderType = Entities.PU_LeaderCavalry2, SoldierNum = 3, SpawnNum = 3, Looped = true, Experience = 3}
									},
						RandomizeSpawn = true,
						RandomizeSpawnPoint = true
						})
	Trigger.DisableTrigger(UASecondRoomAttackStables.Trigger)
	UASecondRoomAttackTower = UnlimitedArmy:New({
						Player = 6,
						Area = 4500,
						AutoDestroyIfEmpty = true,
						TransitAttackmove = true,
						Formation = UnlimitedArmy.Formations.Chaotic,
						LeaderFormation = FormationFunktion,
						AIActive = true,
						AutoRotateRange = true,
						DoNotNormalizeSpeed = true,
						})
	UASecondRoomAttackTowerSpawner = UnlimitedArmySpawnGenerator:New(UASecondRoomAttackTower,{
						Position = GetPosition("MidRoomCannonSpawn"),
						ArmySize = 6,
						SpawnCounter = 5,
						SpawnLeaders = 6,
						LeaderDesc = {
									{LeaderType = Entities.PV_Cannon3, SoldierNum = 0, SpawnNum = 2, Looped = true, Experience = 3},
									{LeaderType = Entities.PV_Cannon4, SoldierNum = 0, SpawnNum = 2, Looped = true, Experience = 3}
									},
						Generator = "TowerBanditTower",
						RandomizeSpawn = true,
						})
	Trigger.DisableTrigger(UASecondRoomAttackTower.Trigger)
	local EndBossRoomDef = {}
	EndBossRoomDef[1] = GetPosition("GarnisonPos1")
	EndBossRoomDef[1].Generator = "Barracks2"
	EndBossRoomDef[2] = GetPosition("GarnisonPos2")
	EndBossRoomDef[2].Generator = "Barracks3"
	EndBossRoomDef[3] = GetPosition("GarnisonPos3")
	EndBossRoomDef[3].Generator = "Barracks1"
	EndBossRoomDef[4] = GetPosition("ArcheryPos1")
	EndBossRoomDef[4].Generator = "Archers3"
	EndBossRoomDef[5] = GetPosition("ArcheryPos2")
	EndBossRoomDef[5].Generator = "Archers1"
	EndBossRoomDef[6] = GetPosition("ArcheryPos3")
	EndBossRoomDef[6].Generator = "Archers2"
	EndBossRoomDef[7] = GetPosition("Stables1Pos")
	EndBossRoomDef[7].Generator = "Stables1"
	EndBossRoomDef[8] = GetPosition("Stables2Pos")
	EndBossRoomDef[8].Generator = "Stables2"
	EndBossRoomDef[9] = GetPosition("MidRoomCannonSpawn")
	EndBossRoomDef[9].Generator = "TowerBanditTower"
	UASecondRoomDef = UnlimitedArmy:New({
						Player = 6,
						Area = 3800,
						AutoDestroyIfEmpty = true,
						TransitAttackMove = true,
						Formation = UnlimitedArmy.Formations.Circle,
						LeaderFormation = FormationFunktion,
						AIActive = true,
						AutoRotateRange = true,
						DoNotNormalizeSpeed = true
						})
	UASecondRoomDefSpawner = UnlimitedArmySpawnGenerator:New(UASecondRoomDef,{
						Position = EndBossRoomDef,
						ArmySize = 15,
						SpawnCounter = 30,
						SpawnLeaders = 7,
						LeaderDesc = {
								{LeaderType = Entities.PU_LeaderSword4, SoldierNum = 8, SpawnNum = 5, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderBow4, SoldierNum = 8,SpawnNum = 4, Looped = true, Experience = 3},
								{LeaderType = Entities.PV_Cannon4, SoldierNum = 0, SpawnNum = 3, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderHeavyCavalry2, SoldierNum = 3, SpawnNum = 6, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderBow4, SoldierNum = 8,SpawnNum = 4, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderPoleArm4, SoldierNum = 8, SpawnNum = 5, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderCavalry2, SoldierNum = 3, SpawnNum = 5, Looped = true, Experience = 3},
								{LeaderType = Entities.PU_LeaderSword1, SoldierNum = 0, SpawnNum = 1, Looped = true, Experience = 3},
								{LeaderType = Entities.PV_Cannon3, SoldierNum = 0, SpawnNum = 3, Looped = true, Experience = 3}
								},
						RandomizeSpawn = true,
						RandomizeSpawnPoint = true
						})
	Trigger.DisableTrigger(UASecondRoomDef.Trigger)
	UASecondRoomAttackBarracks:AddCommandLuaFunc(UAAttackersLuaFunc,true)
	UASecondRoomAttackArchers:AddCommandLuaFunc(UAAttackersLuaFunc,true)
	UASecondRoomAttackStables:AddCommandLuaFunc(UAAttackersLuaFunc,true)
	UASecondRoomAttackTower:AddCommandLuaFunc(UAAttackersLuaFunc,true)
	UASecondRoomDef:AddCommandDefend(GetPosition("TruppenMassen"),3800,true)
	FinalRoomUADef = UnlimitedArmy:New({
					Player = 6,
					Area = 4200,
					AutoDestroyIfEmpty = false,
					TransitAttackMove = true,
					Formation = UnlimitedArmy.Formations.Circle,
					LeaderFormation = FormationFunktion,
					AIActive = true,
					AutoRotateRange = true,
					DoNotNormalizeSpeed = true
					})
	Trigger.DisableTrigger(FinalRoomUADef.Trigger)
	FinalRoomUAAttack = UnlimitedArmy:New({
					Player = 6,
					Area = 4200,
					AutoDestroyIfEmpty = false,
					TransitAttackMove = true,
					Formation = UnlimitedArmy.Formations.Circle,
					LeaderFormation = FormationFunktion,
					AIActive = true,
					AutoRotateRange = true,
					DoNotNormalizeSpeed = true
					})
	Trigger.DisableTrigger(FinalRoomUAAttack.Trigger)
	CheckpointHandler()
	FinalRoomUADef:AddCommandLuaFunc(UADeffers,true)
	FinalRoomUAAttack:AddCommandLuaFunc(UAAttackers,true)
	LastRoomSpawnTable = {
	{LeaderType = Entities.PU_LeaderSword1, MaxNumber = 1, SoldierNum = 0, Pos = GetPosition("EndBossMiddleSpawn")},
	{LeaderType = Entities.PU_LeaderSword4, MaxNumber = 1, SoldierNum = 8},
	{LeaderType = Entities.PU_LeaderPoleArm4, MaxNumber = 1, SoldierNum = 8},
	{LeaderType = Entities.PU_LeaderHeavyCavalry2, MaxNumber = 1, SoldierNum = 3},
	{LeaderType = Entities.PU_LeaderBow4, MaxNumber = 1, SoldierNum = 8},
	{LeaderType = Entities.PU_LeaderCavalry2, MaxNumber = 1, SoldierNum = 3},
	{LeaderType = Entities.PV_Cannon4, MaxNumber = 1, SoldierNum = 0}
	}
end
function UAAttackersLuaFunc(self)
	if self:IsIdle() then
		return GetNextDZ(self)
	end
	return false
end
function CheckpointHandler()
	local Quest		=	{}
	Quest.Targets	=	{"endfightfirstpointTower1","endfightfirstpointTower2"}
	
	Quest.Callback	=	OpenNextRoom

	SetupDestroy(Quest)
end
function OpenNextRoom()
	LastFightPart = 2
	ReplaceEntity("firstgatesClosed1", Entities.XD_DarkWallStraightGate)
	ReplaceEntity("firstgatesClosed2", Entities.XD_DarkWallStraightGate)
	local entitiypos = GetPosition("firstdefense")
	local wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,4500,1)
	while wallnumbers > 0 do
		Logic.ChangeEntityPlayerID(wallentID, 4)
		wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,4500,1)
	end
	
	Trigger.EnableTrigger(UASecondRoomDef.Trigger)
	Trigger.EnableTrigger(UASecondRoomAttackTower.Trigger)
	Trigger.EnableTrigger(UASecondRoomAttackBarracks.Trigger)
	Trigger.EnableTrigger(UASecondRoomAttackArchers.Trigger)
	Trigger.EnableTrigger(UASecondRoomAttackStables.Trigger)
	
	local Towerdown = {}
	Towerdown.Target = "TowerBanditTower"
	Towerdown.Callback = BiggerCannonTowerDown
	SetupDestroy(Towerdown)
	local Quest = {}
	Quest.Targets = {"Barracks1",
					"Barracks2",
					"Barracks3",
					"Archers1",
					"Archers2",
					"Archers3",
					"Stables1",
					"Stables2",
					"TowerBanditTower"
					}
	
	Quest.Callback = EndBossOpen
	
	SetupDestroy(Quest)
end
function BiggerCannonTowerDown()
	Logic.DestroyEntity(Logic.GetEntityIDByName("testtower"))
end
function EndBossOpen()
	ReplaceEntity("LastDoorClosed", Entities.XD_DarkWallStraightGate)
	local entitiypos = GetPosition("firstgatesClosed1")
	local wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,4000,1)
	while wallnumbers > 0 do
		Logic.ChangeEntityPlayerID(wallentID, 4)
		wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,4000,1)
	end
	
	entitiypos = GetPosition("LastDoorClosed")
	local wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,1500,1)
	while wallnumbers > 0 do
		Logic.ChangeEntityPlayerID(wallentID, 4)
		wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,1500,1)
	end
	
	entitiypos.Y = entitiypos.Y + 2600
	local wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,1500,1)
	while wallnumbers > 0 do
		Logic.ChangeEntityPlayerID(wallentID, 4)
		wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,1500,1)
	end
	
	entitiypos.Y = entitiypos.Y - 5200
	local wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,1500,1)
	while wallnumbers > 0 do
		Logic.ChangeEntityPlayerID(wallentID, 4)
		wallnumbers,wallentID = Logic.GetPlayerEntitiesInArea(3,0,entitiypos.X,entitiypos.Y,1500,1)
	end
	
	
	LastFightPart = 3
	InitFinalBattle()
	Trigger.EnableTrigger(FinalRoomUAAttack.Trigger)
	Trigger.EnableTrigger(FinalRoomUADef.Trigger)
end

function InitFinalBattle()
	tower1HP = 100000 --100k
	tower2HP = 100000 --100k
	FinalBaseHP = 500000 -- 500k
	
	function GUIUpdate_VCTechRaceProgress() end
	function GUIUpdate_GetTeamPoints() end
	function GUIUpdate_VCTechRaceColor(_Player) end
	XGUIEng.ShowWidget("VCMP_Window", 1)
	XGUIEng.ShowWidget("VCMP_Team2", 0)
	XGUIEng.ShowWidget("VCMP_Team3", 0)
	XGUIEng.ShowWidget("VCMP_Team4", 0)
	XGUIEng.ShowWidget("VCMP_Team5", 0)
	XGUIEng.ShowWidget("VCMP_Team6", 0)
	XGUIEng.ShowWidget("VCMP_Team7", 0)
	XGUIEng.ShowWidget("VCMP_Team8", 0)
	--XGUIEng.ShowWidget("VCMP_Team1_Shade",0)
	XGUIEng.ShowWidget("VCMP_Team2_Shade", 0)
	XGUIEng.ShowWidget("VCMP_Team3_Shade", 0)
	XGUIEng.ShowWidget("VCMP_Team4_Shade", 0)
	XGUIEng.ShowWidget("VCMP_Team5_Shade", 0)
	XGUIEng.ShowWidget("VCMP_Team6_Shade", 0)
	XGUIEng.ShowWidget("VCMP_Team7_Shade", 0)
	XGUIEng.ShowWidget("VCMP_Team8_Shade", 0)
	XGUIEng.ShowWidget("VCMP_Team1Name",0)
	XGUIEng.ShowWidget("VCMP_Team1Player1", 0)
	XGUIEng.ShowWidget("VCMP_Team1Player2", 0)
	XGUIEng.ShowWidget("VCMP_Team1Player3", 0)
	XGUIEng.ShowWidget("VCMP_Team1Player4", 0)
	XGUIEng.ShowWidget("VCMP_Team1Player5", 0)
	XGUIEng.ShowWidget("VCMP_Team1Player6", 0)
	XGUIEng.ShowWidget("VCMP_Team1Player7", 0)
	XGUIEng.ShowWidget("VCMP_Team1Player8", 0)
	XGUIEng.SetWidgetPositionAndSize(XGUIEng.GetWidgetID("VCMP_Window"), 256, 80, 522, 300)
	XGUIEng.ShowWidget("VCMP_Team1TechRace",1)
	XGUIEng.SetWidgetPositionAndSize(XGUIEng.GetWidgetID("VCMP_Team1"),0,0,522,30)
	XGUIEng.SetWidgetPositionAndSize(XGUIEng.GetWidgetID("VCMP_Team1TechRace"),0,0,522,48)
	XGUIEng.SetWidgetPositionAndSize(XGUIEng.GetWidgetID("VCMP_Team1Progress"),12,19,498,10)
	XGUIEng.SetWidgetPositionAndSize(XGUIEng.GetWidgetID("VCMP_Team1ProgressBG"),12,19,498,10)
	XGUIEng.SetWidgetPositionAndSize(XGUIEng.GetWidgetID("VCMP_Team1_Shade"),0,0,522,48)
	XGUIEng.SetMaterialColor(XGUIEng.GetWidgetID("VCMP_Team1Progress"), 0, 120, 0, 0, 255)
	for i = 0,5,1 do XGUIEng.SetMaterialColor(XGUIEng.GetWidgetID("VCMP_Team1_Shade"), i, 255, 255, 255, 255) end
	for i = 0,5,1 do XGUIEng.SetMaterialColor(XGUIEng.GetWidgetID("VCMP_Team1ProgressBG"), i, 50, 50, 50, 255) end
	for i = 0,5,1 do XGUIEng.SetMaterialTexture(XGUIEng.GetWidgetID("VCMP_Team1_Shade"), i, Folders.Map.."testobjekt10.png") end
	
	XGUIEng.SetProgressBarValues(XGUIEng.GetWidgetID("VCMP_Team1Progress"),tower1HP+tower2HP, tower1HP+tower2HP)

	importantTargetTable = {Logic.GetEntityIDByName("Bosscannontower"),
							Logic.GetEntityIDByName("BossTower1"),
							Logic.GetEntityIDByName("FinalBase")
							}
	HurtTriggerID = Trigger.RequestTrigger(Events.LOGIC_EVENT_ENTITY_HURT_ENTITY, nil, "Bossbalkenfunc",1,nil,nil)
	Logic.SuspendEntity(Tower1TurretID)
	towerwaiter = 0
	SubIndexTable = {[0] = true,[2] = true,[6] = true}
	RapidfireTriggerID = Trigger.RequestTrigger(Events.LOGIC_EVENT_EVERY_TURN,nil,"TowerController",1,nil,nil)
end

function GetTaskSubIndexNumberViaSV(_id)
	if HistoryFlag == 1 then
		return Logic.GetEntityScriptingValue(_id,-18)
	else
		return Logic.GetEntityScriptingValue(_id,-21)
	end
end

function SetTaskSubIndexNumberViaSV(_id)
	if HistoryFlag == 1 then
		return Logic.SetEntityScriptingValue(_id,-18,6)
	else
		return Logic.SetEntityScriptingValue(_id,-21,6)
	end
end

function SetTurretOrientationOfFirstTurret(_id)
	local orientate = Logic.GetEntityOrientation(_id)
	Logic.ResumeEntity(Tower1TurretID)
	if HistoryFlag == 1 then
		Logic.SetEntityScriptingValue(Tower1TurretID,-30,Float2Int(math.rad(orientate)))
	else
		Logic.SetEntityScriptingValue(Tower1TurretID,-34,Float2Int(math.rad(orientate)))
	end
	Logic.SetTaskList(Tower1TurretID,TaskLists.TL_BATTLE_BALISTATOWER)
	Logic.SuspendEntity(Tower1TurretID)
end

function TowerController()
	if towerwaiter < 12 then
		if math.mod(towerwaiter,2) == 0 then
			--setting up and fire
			if Counter.Tick2("ReloadTime",3) then
				SetTaskSubIndexNumberViaSV(Tower1TurretID)
				Logic.ResumeEntity(Tower1TurretID)
				towerwaiter = towerwaiter + 1
			end
		else
			--recharge
			Logic.SuspendEntity(Tower1TurretID)
			towerwaiter = towerwaiter + 1
		end
	else
		if Counter.Tick2("TowerCooldown", 45) then
			towerwaiter = 0
		end
	end
	return false
end

function Bossbalkenfunc()
	local towerID = Event.GetEntityID2()
	if Logic.EntityGetPlayer(towerID) == 6 then
		if LastFightPart == 3 then
			if towerID == importantTargetTable[1] then
				tower1HP = tower1HP - Logic.GetEntityDamage(Event.GetEntityID1())
				if tower1HP <= 0 then
					Trigger.RequestTrigger(Events.LOGIC_EVENT_EVERY_TURN,nil,"DoAvoidHurtTriggerCrash",1,nil,{towerID})
					tower1HP = 0
 				end
				BossbalkenCalc(LastFightPart)
				local prozentualanteil = tower1HP/100000
				HPBerechnung(towerID,prozentualanteil)
				return false
			end
			if towerID == importantTargetTable[2] then
				tower2HP = tower2HP - Logic.GetEntityDamage(Event.GetEntityID1())
				if tower2HP <= 0 then
					Trigger.RequestTrigger(Events.LOGIC_EVENT_EVERY_TURN,nil,"DoAvoidHurtTriggerCrash",1,nil,{towerID})
					tower2HP = 0
				end
				BossbalkenCalc(LastFightPart)
				local prozentualanteil = tower2HP/100000
				HPBerechnung(towerID,prozentualanteil)
				return false
			end
		elseif LastFightPart == 4 then
			if towerID == importantTargetTable[3] then
				FinalBaseHP = FinalBaseHP - Logic.GetEntityDamage(Event.GetEntityID1())
				if FinalBaseHP <= 0 then
						Trigger.RequestTrigger(Events.LOGIC_EVENT_EVERY_TURN,nil,"DoAvoidHurtTriggerCrash",1,nil,{towerID})
						FinalBaseHP = 0
				end
				BossbalkenCalc(LastFightPart)
				local prozentualanteil = FinalBaseHP/500000
				HPBaseBerechnung(towerID,prozentualanteil)
			end
		end
	end
	return false
end

function HPBaseBerechnung(entityasID,_HealthInPercent)
	local health = Logic.GetEntityMaxHealth(entityasID)
	health = health * _HealthInPercent
	if health <= 1 then
		Logic.SetEntityScriptingValue(entityasID,-8,1)
	else
		Logic.SetEntityScriptingValue(entityasID,-8,health)
	end
end

function DoAvoidHurtTriggerCrash(entityasID)
	local entID = entityasID
	if LastFightPart == 3 then
		if tower1HP == 0 and entID == importantTargetTable[1] then
			Logic.ChangeEntityPlayerID(entID, 3)
		end
		if tower2HP == 0 and entID == importantTargetTable[2] then
			Trigger.UnrequestTrigger(RapidfireTriggerID)
			Logic.ChangeEntityPlayerID(entID, 3)
		end
		if tower1HP == 0 and tower2HP == 0 then
			LastFightPart = 4
			FinalRoomUAAttack:AddLeader(CreateEntity(5, Entities.CU_BlackKnight, GetPosition("EndBossMiddleSpawn"), "Kerberos2" ))
			BossbalkenCalc(LastFightPart)
			return true
		end
	elseif LastFightPart == 4 and FinalBaseHP <= 0 then
		if OnceExecuted == nil then
			OnceExecuted = true
			Trigger.UnrequestTrigger(HurtTriggerID)
			Logic.ChangeEntityPlayerID(entID, 3)
			EndFightEnd()
			return true
		end
	end
	return true
end

function BossbalkenCalc(TowerOrBase)
	if TowerOrBase == 3 then
		XGUIEng.SetProgressBarValues(XGUIEng.GetWidgetID("VCMP_Team1Progress"),tower1HP+tower2HP, 200000)
	else
		XGUIEng.SetProgressBarValues(XGUIEng.GetWidgetID("VCMP_Team1Progress"),FinalBaseHP, 500000)
	end
end

--[[function HPBerechnung(id, perc)
  perc = 20 + perc*0.8 -- [0-100] zu 20 + [0-80]
  SetHealth(id, perc)
end]]

function HPBerechnung(entityasID, _HealthInPercent)
	local Health = Logic.GetEntityMaxHealth(entityasID)
	local GrenzHealth = Health*0.2
	Health = Health - GrenzHealth	
	Health =  Health*_HealthInPercent
	local CurHealth = Logic.GetEntityHealth(entityasID)
	local DeltaHealth = CurHealth-GrenzHealth
	DeltaHealth = Health - DeltaHealth
	if DeltaHealth < 0 then
		Logic.SetEntityScriptingValue(entityasID,-8,CurHealth-(-DeltaHealth))
	end
end

function UAAttackers(self)
	local size = SizeOfUA()
	local OPdudes = 0
	if self:GetSize(true,false) < size then
		for id in self:Iterator(true) do
			if Logic.GetEntityTypeName(Logic.GetEntityType(id)) == "PU_LeaderSword1" then
				OPdudes = OPdudes + 1
			end
		end
		for i = 1,math.min(size-self:GetSize(true,false),13),1 do
			local spot = (OPdudes ~= LastRoomSpawnTable[1].MaxNumber) and math.random(1,7) or math.random(2,7)
			if LastRoomSpawnTable[spot].Pos then
					self:CreateLeaderForArmy(LastRoomSpawnTable[spot].LeaderType, LastRoomSpawnTable[spot].SoldierNum, LastRoomSpawnTable[spot].Pos, 3)
					OPdudes = OPdudes + 1
			else
				local randomplace = (LastFightPart == 4) and math.random(3,6) or math.random(1,4)
				self:CreateLeaderForArmy(LastRoomSpawnTable[spot].LeaderType, LastRoomSpawnTable[spot].SoldierNum, GetPosition("BossSpawner"..randomplace), 3)
			end
		end
	end
	if self:IsIdle() then
		return GetNextDZ(self)
	end
	return false
end

function SizeOfUA()
	if LastFightPart == 3 then
		return 28
	else
		return 35
	end
end

function UADeffers(self)
	local size = SizeOfUA()
	local OPdudes = 0
	if self:GetSize(true,false) < size then
		for id in self:Iterator(true) do
			if Logic.GetEntityTypeName(Logic.GetEntityType(id)) == "PU_LeaderSword1" then
				OPdudes = OPdudes + 1
			end
		end
		for i = 1,math.min(size-self:GetSize(true,false),13),1 do
			local spot = (OPdudes ~= LastRoomSpawnTable[1].MaxNumber) and math.random(1,7) or math.random(2,7)
			if LastRoomSpawnTable[spot].Pos then
					self:CreateLeaderForArmy(LastRoomSpawnTable[spot].LeaderType, LastRoomSpawnTable[spot].SoldierNum, LastRoomSpawnTable[spot].Pos, 3)
					OPdudes = OPdudes + 1
			else
				local randomplace = (LastFightPart == 4) and math.random(3,6) or math.random(1,4)
				self:CreateLeaderForArmy(LastRoomSpawnTable[spot].LeaderType, LastRoomSpawnTable[spot].SoldierNum, GetPosition("BossSpawner"..randomplace), 3)
			end
		end
	end
--	if self:IsIdle() then
		return true,UnlimitedArmy.CreateCommandDefend(GetPosition("EndBossMiddleSpawn"),3800,false)
--	end
--	return false
end