include("SharedFunctions.lua")

local bCityTransferRun = false

function CityTransfer(hexPos,playerID,cityID,newPlayerID)
if bCityTransferRun == true then
	print("长平之战已经发生过")
	return
end
print("长平待机")

local playerZhao=Players[2]
if not playerZhao or not playerZhao:IsAlive() then
print("赵不在")
end
local playerQin = Players[newPlayerID]
local playerHan = Players[playerID]
	if not (playerQin:GetCivilizationType()==GameInfoTypes.CIVILIZATION_QIN) or not (playerHan:GetCivilizationType()==GameInfoTypes.CIVILIZATION_HAN) then
		return
	end

local ZhaoPower=EvaluePower(playerZhao)
local QinPower=EvaluePower(playerQin)
if (ZhaoPower)/(QinPower) < 0.7 then
print ("赵 担心生命安全不肯出演")
	return
end
local plot = Map.GetPlot(ToGridFromHex(hexPos))
local pCity = plot:GetPlotCity()
if (plot:GetX() == 21) and (plot:GetY() == 38) then
	playerZhao:AcquireCity(pCity, false, true)
	pCity:DoTask(TaskTypes.TASK_CREATE_PUPPET)
	print("Transferring...", pCity:GetName(), "to" , playerZhao:GetName(), "from", playerQin:GetName() )
	local QinTeam=Teams[playerQin:GetTeam()]
	local ZhaoTeamID=playerZhao:GetTeam()
	if not QinTeam:IsAtWar(ZhaoTeamID) then
		QinTeam:DeclareWar(ZhaoTeamID)
		print( playerQin:GetName(),"因为", pCity:GetName(), "归属之争,向", playerZhao:GetName(), "宣战" )
		bCityTransferRun = true
	end
	local heading = Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_SHANGDANG_SHORT")
	local text = Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_SHANGDANG")
	for _, player in pairs(Players) do
		if player:IsHuman() then
			player:AddNotification(NotificationTypes.NOTIFICATION_GENERIC ,text,heading,21,38)
		end
	end
end

end

Events.SerialEventCityCaptured.Add(CityTransfer)