---------------------------------------------------------------------------------------------
function createPlayer2()

	SetPlayerName(2, String.Key("_Player2Name"))
	--SetPlayerName(2, "Boeser Gouverneur")

	player2 	= {}
	player2.id 	= 2

		local description = {

			serfLimit				=	12,
			--------------------------------------------------
			extracting				=	false,
			--------------------------------------------------
			resources = {
				gold				=	10000,
				clay				=	10000,
				iron				=	10000,
				sulfur				=	10000,
				stone				=	10000,
				wood				=	10000
			},
			--------------------------------------------------
			refresh = {
				gold				=	10,
				clay				=	10,
				iron				=	10,
				sulfur				=	10,
				stone				=	10,
				wood				=	10,
				updateTime			=	60	
			},
		}
	
	Logic.SetTechnologyState(2, Technologies.T_LeatherMailArmor, 3)
	Logic.SetTechnologyState(2, Technologies.T_ChainMailArmor, 3)
	Logic.SetTechnologyState(2, Technologies.T_PlateMailArmor, 3)
	Logic.SetTechnologyState(2, Technologies.T_SoftArcherArmor, 3)
	Logic.SetTechnologyState(2, Technologies.T_PaddedArcherArmor, 3)
	Logic.SetTechnologyState(2, Technologies.T_LeatherArcherArmor, 3)
	Logic.SetTechnologyState(2, Technologies.T_MasterOfSmithery, 3)
	Logic.SetTechnologyState(2, Technologies.T_IronCasting, 3)
	Logic.SetTechnologyState(2, Technologies.T_Fletching, 3)
	Logic.SetTechnologyState(2, Technologies.T_BodkinArrow, 3)
	Logic.SetTechnologyState(2, Technologies.T_WoodAging, 3)
	Logic.SetTechnologyState(2, Technologies.T_Turnery, 3)
	Logic.SetTechnologyState(2, Technologies.T_EnhancedGunPowder, 3)
	Logic.SetTechnologyState(2, Technologies.T_BlisteringCannonballs, 3)
	Logic.SetTechnologyState(2, Technologies.T_BetterTrainingBarracks, 3)
	Logic.SetTechnologyState(2, Technologies.T_BetterTrainingArchery, 3)
	Logic.SetTechnologyState(2, Technologies.T_Shoeing, 3)
	Logic.SetTechnologyState(2, Technologies.T_Masonry, 3)
	Logic.SetTechnologyState(2, Technologies.T_SuperTechnology, 3)
	
	Logic.SetTechnologyState(2, Technologies.T_FleeceArmor, 3)
	Logic.SetTechnologyState(2, Technologies.T_FleeceLinedLeatherArmor, 3)
	Logic.SetTechnologyState(2, Technologies.T_LeadShot, 3)
	Logic.SetTechnologyState(2, Technologies.T_Sights, 3)
	
	for i=1,3 do
		Logic.UpgradeSettlerCategory(UpgradeCategories.LeaderBow, 2)
		Logic.UpgradeSettlerCategory(UpgradeCategories.SoldierBow, 2)
		Logic.UpgradeSettlerCategory(UpgradeCategories.LeaderSword, 2)
		Logic.UpgradeSettlerCategory(UpgradeCategories.SoldierSword, 2)
		Logic.UpgradeSettlerCategory(UpgradeCategories.LeaderPoleArm, 2)
		Logic.UpgradeSettlerCategory(UpgradeCategories.SoldierPoleArm, 2)
	end

	Logic.UpgradeSettlerCategory(UpgradeCategories.LeaderCavalry, 2)
	Logic.UpgradeSettlerCategory(UpgradeCategories.LeaderHeavyCavalry, 2)
	Logic.UpgradeSettlerCategory(UpgradeCategories.SoldierCavalry, 2)
	Logic.UpgradeSettlerCategory(UpgradeCategories.SoldierHeavyCavalry, 2)
	
	Logic.CreateEntity(Entities.PU_LeaderSword1, 37869.93, 55617.82, 275.00, 2)

	Logic.SetTechnologyState(7, Technologies.T_LeatherMailArmor, 3)
	Logic.SetTechnologyState(7, Technologies.T_ChainMailArmor, 3)
	Logic.SetTechnologyState(7, Technologies.T_PlateMailArmor, 3)
	Logic.SetTechnologyState(7, Technologies.T_SoftArcherArmor, 3)
	Logic.SetTechnologyState(7, Technologies.T_PaddedArcherArmor, 3)
	Logic.SetTechnologyState(7, Technologies.T_LeatherArcherArmor, 3)
	Logic.SetTechnologyState(7, Technologies.T_MasterOfSmithery, 3)
	Logic.SetTechnologyState(7, Technologies.T_IronCasting, 3)
	Logic.SetTechnologyState(7, Technologies.T_Fletching, 3)
	Logic.SetTechnologyState(7, Technologies.T_BodkinArrow, 3)
	Logic.SetTechnologyState(7, Technologies.T_WoodAging, 3)
	Logic.SetTechnologyState(7, Technologies.T_Turnery, 3)
	Logic.SetTechnologyState(7, Technologies.T_EnhancedGunPowder, 3)
	Logic.SetTechnologyState(7, Technologies.T_BlisteringCannonballs, 3)
	Logic.SetTechnologyState(7, Technologies.T_BetterTrainingBarracks, 3)
	Logic.SetTechnologyState(7, Technologies.T_BetterTrainingArchery, 3)
	Logic.SetTechnologyState(7, Technologies.T_Shoeing, 3)
	Logic.SetTechnologyState(7, Technologies.T_Masonry, 3)
	Logic.SetTechnologyState(7, Technologies.T_SuperTechnology, 3)
	
	Logic.SetTechnologyState(7, Technologies.T_FleeceArmor, 3)
	Logic.SetTechnologyState(7, Technologies.T_FleeceLinedLeatherArmor, 3)
	Logic.SetTechnologyState(7, Technologies.T_LeadShot, 3)
	Logic.SetTechnologyState(7, Technologies.T_Sights, 3)

	SetupPlayerAi(player2.id,description)
	
	Logic.SetPlayerPaysLeaderFlag(2, 0)
	AI.Village_EnableConstructing(2,1)
	AI.Village_EnableRepairing(2,1)

	Trigger.RequestTrigger(Events.LOGIC_EVENT_ENTITY_DESTROYED,nil,"EntitiesForTechs",1,nil,nil)
	
	TechEntities = {}
	TechEntities[Entities.PB_Blacksmith3] = {Technologies.T_LeatherMailArmor,
											Technologies.T_ChainMailArmor,
											Technologies.T_PlateMailArmor,
											Technologies.T_SoftArcherArmor,
											Technologies.T_PaddedArcherArmor,
											Technologies.T_LeatherArcherArmor,
											Technologies.T_MasterOfSmithery,
											Technologies.T_IronCasting
											}
	TechEntities[Entities.PB_Sawmill2] = {Technologies.T_Fletching,
											Technologies.T_BodkinArrow,
											Technologies.T_WoodAging,
											Technologies.T_Turnery}
	TechEntities[Entities.PB_StoneMason2] = {Technologies.T_Masonry}
	TechEntities[Entities.PB_GunsmithWorkshop2] = {Technologies.T_FleeceArmor,
													Technologies.T_FleeceLinedLeatherArmor,
													Technologies.T_LeadShot,
													Technologies.T_Sights}
	TechEntities[Entities.PB_Alchemist2] = {Technologies.T_EnhancedGunPowder,
											Technologies.T_BlisteringCannonballs}
	TechEntities[Entities.CB_SteamMashine] = SteamMachineDestroied
	TechEntities[Entities.PB_Bank2] = BankDestroied
	
	local SingleRifles = {Logic.GetEntities(Entities.PU_LeaderRifle1,16)}
	table.remove(SingleRifles,1)
	for i = 1,table.getn(SingleRifles),1 do
		Tools.CreateSoldiersForLeader(SingleRifles[i], 4)
		Logic.GroupStand(SingleRifles[i])
	end
	
	schmiedsichtbar = StartSimpleJob("SeeFeinschmied")
	
    end
	
function SeeFeinschmied()
	local schmiedpos = GetPosition("feinschmid")
	if Logic.IsMapPositionExplored(1, schmiedpos.X, schmiedpos.Y) == 1 then
		createSchmiedBriefing()
		return true
	end
	return false
end
	
function BankDestroied()
	if not IsDead("LkavDef") then
		createSoeldnerBriefing()
	end
	--[[
	LkavBaseDef:ClearCommandQueue()
	LkavBaseDef:AddCommandMove(GetPosition("guards"),false)
	LkavBaseDef:AddCommandLuaFunc(makeNeutral,false)
	LkavBaseDef:AddCommandLuaFunc(MoveToBishop,false)
	LkavBaseDef:AddCommandMove(GetPosition("guards"),true)
	LkavBaseDef:AddCommandWaitForIdle(true)
	BankDestroiedAfter = true
	InitPlayerColorMapping()
	]]
	return false
end

function makeNeutral(self)
	SetNeutral(1,7)
	SetNeutral(3,7)
	return true
end
function MoveToBishop(self)
	if GetDistance(self:GetPosition(),GetPosition("guards")) <= 5500 then
		SetHostile(2,7)
		return true, UnlimitedArmy.CreateCommandSetSpawnerStatus(false)
	end
	return false
end
	
function SteamMachineDestroied(_SteamID)
	local SteamMashinePos = GetPosition(_SteamID)
	buildingsTable = {Logic.GetPlayerEntitiesInArea(2, 0, SteamMashinePos.X, SteamMashinePos.Y, 2000, 6, 8)}
	table.remove(buildingsTable,1)
	StartSimpleJob("TickDownBuildings",buildingsTable)
	return false
end

function TickDownBuildings(_table)
	local i = table.getn(_table)
	if i <= 0 then
		return true
	end
	for _var = i,0,-1 do
		if IsDead(_table[_var]) then
			table.remove(_table,_var)
		else
			Logic.HurtEntity(_table[_var], 200)
		end
	end
	return false
end
	
function EntitiesForTechs()
	local _entID = Event.GetEntityID()
	if Logic.EntityGetPlayer(_entID) ~= 2 then
		return false
	end
	local _enttype = Logic.GetEntityType(_entID)
	if TechEntities[_enttype] then
		if type(TechEntities[_enttype]) == "function" then
			TechEntities[_enttype](_entID)
			return TableContentOrder(_enttype)
		end
		local _numberOf = {Logic.GetPlayerEntities(2, _enttype, 1)}
		if _numberOf[1] <= 0 then
			for _,v in pairs(TechEntities[_enttype]) do
				Logic.SetTechnologyState(2, v, 2)
			end
			return TableContentOrder(_enttype)
		end
	end
	return false
end

function TableContentOrder(_type)
	TechEntities[_type] = nil
	for _,v in pairs(TechEntities) do
		return false
	end
	return true
end