p0 = Player.GetPlayer("Multi0") 
p1 = Player.GetPlayer("Multi1")
p2 = Player.GetPlayer("Multi2")
p3 = Player.GetPlayer("Multi3")
p4 = Player.GetPlayer("Multi4")
p5 = Player.GetPlayer("Multi5")
p6 = Player.GetPlayer("Multi6")
p7 = Player.GetPlayer("Multi7")
mainfactions = {p0,p1,p2,p3,p4,p5,p6,p7}
p0target = p0
p1target = p1
p2target = p2
p3target = p3
p4target = p4
p5target = p5
p6target = p6
p7target = p7
p0got = 0
p1got = 0
p2got = 0
p3got = 0
p4got = 0
p5got = 0
p6got = 0
p7got = 0
p0msg = {}
p1msg = {}
p2msg = {}
p3msg = {}
p4msg = {}
p5msg = {}
p6msg = {}
p7msg = {}
p0gold = 0
p1gold = 0
p2gold = 0
p3gold = 0
p4gold = 0
p5gold = 0
p6gold = 0
p7gold = 0
p0score = 0
p1score = 0
p2score = 0
p3score = 0
p4score = 0
p5score = 0
p6score = 0
p7score = 0
winners = {}
gottimer = 0
winpoints = 0
p0debt = 0
p1debt = 0
p2debt = 0
p3debt = 0
p4debt = 0
p5debt = 0
p6debt = 0
p7debt = 0
p8debt = 0
debts = {p0debt,p1debt,p2debt,p3debt,p4debt,p5debt,p6debt,p7debt}
p0costs = 0
p1costs = 0
p2costs = 0
p3costs = 0
p4costs = 0
p5costs = 0
p6costs = 0
p7costs = 0
p0income = 0
p1income = 0
p2income = 0
p3income = 0
p4income = 0
p5income = 0
p6income = 0
p7income = 0
incomes = {p0income,p1income,p2income,p3income,p4income,p5income,p6income,p7income}
incomemsg = {"","","","","","","",""}
delayed = 0
neutmoney = 0
neutincome = 0

Produces = function(producer,produced,productionType)
	if produced.Type == "thronepin-informants" then
			local p0gottemp = p0got
			local p1gottemp = p1got
			local p2gottemp = p2got
			local p3gottemp = p3got
			local p4gottemp = p4got
			local p5gottemp = p5got
			local p6gottemp = p6got
			local p7gottemp = p7got
			local allgots = {p0gottemp, p1gottemp, p2gottemp, p3gottemp, p4gottemp, p5gottemp, p6gottemp, p7gottemp}
			for i = 1, #allgots do
				allgots[i] = allgots[i] + Utils.RandomInteger(1,3)
			end
			local minimumgot = Utils.RandomInteger(1,2)
			local maximumgot = Utils.RandomInteger(1,4)
		if produced.Owner.IsLocalPlayer == true then
			Media.DisplayMessage("Influence by faction:\nStark: " .. allgots[1] - minimumgot .. " to "  .. allgots[1] + maximumgot .. "\nTully: " .. allgots[2] - minimumgot .. " to "  .. allgots[2] + maximumgot .. "\nArryn: " .. allgots[3] - minimumgot .. " to "  .. allgots[3] + maximumgot .. "\nLannister: " .. allgots[4] - minimumgot .. " to "  .. allgots[4] + maximumgot .. "\nGreyjoy: " .. allgots[5] - minimumgot .. " to "  .. allgots[5] + maximumgot .. "\nBaratheon: " .. allgots[6] - minimumgot .. " to "  .. allgots[6] + maximumgot .. "\nMartell: " .. allgots[7] - minimumgot .. " to "  .. allgots[7] + maximumgot .. "\nTyrell: " .. allgots[8] - minimumgot .. " to "  .. allgots[8] + maximumgot .. "", "Informant")
		end
	elseif produced.Type == "thronepin-commons" then
		if produced.Owner == p0 then
			p0got = p0got + 2
		elseif produced.Owner == p1 then
			p1got = p1got + 2
		elseif produced.Owner == p2 then
			p2got = p2got + 2
		elseif produced.Owner == p3 then
			p3got = p3got + 2
		elseif produced.Owner == p4 then
			p4got = p4got + 2
		elseif produced.Owner == p5 then
			p5got = p5got + 2
		elseif produced.Owner == p6 then
			p6got = p6got + 2
		elseif produced.Owner == p7 then
			p7got = p7got + 2
		end
	elseif produced.Type == "thronepin-espionage" then
		local points = Utils.RandomInteger(0,5)
		if produced.Owner == p0 then
			p0got = p0got + points
		elseif produced.Owner == p1 then
			p1got = p1got + points
		elseif produced.Owner == p2 then
			p2got = p2got + points
		elseif produced.Owner == p3 then
			p3got = p3got + points
		elseif produced.Owner == p4 then
			p4got = p4got + points
		elseif produced.Owner == p5 then
			p5got = p5got + points
		elseif produced.Owner == p6 then
			p6got = p6got + points
		elseif produced.Owner == p7 then
			p7got = p7got + points
		end
	elseif produced.Type == "thronepin-bribery" then
		local stealfrom = Utils.RandomInteger(0,8)
		local points = Utils.RandomInteger(0,4)
		if stealfrom == 0 then
			p0got = p0got - points
		elseif stealfrom == 1 then
			p1got = p1got - points
		elseif stealfrom == 2 then
			p2got = p2got - points
		elseif stealfrom == 3 then
			p3got = p3got - points
		elseif stealfrom == 4 then
			p4got = p4got - points
		elseif stealfrom == 5 then
			p5got = p5got - points
		elseif stealfrom == 6 then
			p6got = p6got - points
		elseif stealfrom == 7 then
			p7got = p7got - points
		end
		if produced.Owner == p0 then
			p0got = p0got + points
		elseif produced.Owner == p1 then
			p1got = p1got + points
		elseif produced.Owner == p2 then
			p2got = p2got + points
		elseif produced.Owner == p3 then
			p3got = p3got + points
		elseif produced.Owner == p4 then
			p4got = p4got + points
		elseif produced.Owner == p5 then
			p5got = p5got + points
		elseif produced.Owner == p6 then
			p6got = p6got + points
		elseif produced.Owner == p7 then
			p7got = p7got + points
		end
	elseif produced.Type == "thronepin-piety" then
		if produced.Owner == p0 then
			p1got = p1got - 1
			p2got = p2got - 1
			p3got = p3got - 1
			p4got = p4got - 1
			p5got = p5got - 1
			p6got = p6got - 1
			p7got = p7got - 1
		elseif produced.Owner == p1 then
			p0got = p0got - 1
			p2got = p2got - 1
			p3got = p3got - 1
			p4got = p4got - 1
			p5got = p5got - 1
			p6got = p6got - 1
			p7got = p7got - 1
		elseif produced.Owner == p2 then
			p0got = p0got - 1
			p1got = p1got - 1
			p3got = p3got - 1
			p4got = p4got - 1
			p5got = p5got - 1
			p6got = p6got - 1
			p7got = p7got - 1
		elseif produced.Owner == p3 then
			p0got = p0got - 1
			p1got = p1got - 1
			p2got = p2got - 1
			p4got = p4got - 1
			p5got = p5got - 1
			p6got = p6got - 1
			p7got = p7got - 1
		elseif produced.Owner == p4 then
			p0got = p0got - 1
			p1got = p1got - 1
			p2got = p2got - 1
			p3got = p3got - 1
			p5got = p5got - 1
			p6got = p6got - 1
			p7got = p7got - 1
		elseif produced.Owner == p5 then
			p0got = p0got - 1
			p1got = p1got - 1
			p2got = p2got - 1
			p3got = p3got - 1
			p4got = p4got - 1
			p6got = p6got - 1
			p7got = p7got - 1
		elseif produced.Owner == p6 then
			p0got = p0got - 1
			p1got = p1got - 1
			p2got = p2got - 1
			p3got = p3got - 1
			p4got = p4got - 1
			p5got = p5got - 1
			p7got = p7got - 1
		elseif produced.Owner == p7 then
			p0got = p0got - 1
			p1got = p1got - 1
			p2got = p2got - 1
			p3got = p3got - 1
			p4got = p4got - 1
			p5got = p5got - 1
			p6got = p6got - 1
		end
	elseif produced.Type == "thronepin-goldcloaks" then
		if produced.Owner == p0 then
			p0gold = p0gold + 1
		elseif produced.Owner == p1 then
			p1gold = p1gold + 1
		elseif produced.Owner == p2 then
			p2gold = p2gold + 1
		elseif produced.Owner == p3 then
			p3gold = p3gold + 1
		elseif produced.Owner == p4 then
			p4gold = p4gold + 1
		elseif produced.Owner == p5 then
			p5gold = p5gold + 1
		elseif produced.Owner == p6 then
			p6gold = p6gold + 1
		elseif produced.Owner == p7 then
			p7gold = p7gold + 1
		end
	elseif produced.Type == "thronepin-foil-stark" then
		local foilpoints = Utils.RandomInteger(0,4)
		p0got = p0got - foilpoints
	elseif produced.Type == "thronepin-foil-tully" then
		local foilpoints = Utils.RandomInteger(0,4)
		p1got = p1got - foilpoints
	elseif produced.Type == "thronepin-foil-arryn" then
		local foilpoints = Utils.RandomInteger(0,4)
		p2got = p2got - foilpoints
	elseif produced.Type == "thronepin-foil-lannister" then
		local foilpoints = Utils.RandomInteger(0,4)
		p3got = p3got - foilpoints
	elseif produced.Type == "thronepin-foil-greyjoy" then
		local foilpoints = Utils.RandomInteger(0,4)
		p4got = p4got - foilpoints
	elseif produced.Type == "thronepin-foil-baratheon" then
		local foilpoints = Utils.RandomInteger(0,4)
		p5got = p5got - foilpoints
	elseif produced.Type == "thronepin-foil-martell" then
		local foilpoints = Utils.RandomInteger(0,4)
		p6got = p6got - foilpoints
	elseif produced.Type == "thronepin-foil-tyrell" then
		local foilpoints = Utils.RandomInteger(0,4)
		p7got = p7got - foilpoints
	end
	local name = ""
	if producer.Owner == p0 then
		name = "Stark"
	elseif producer.Owner == p1 then
		name = "Tully"
	elseif producer.Owner == p2 then
		name = "Arryn"
	elseif producer.Owner == p3 then
		name = "Lannister"
	elseif producer.Owner == p4 then
		name = "Greyjoy"
	elseif producer.Owner == p5 then
		name = "Baratheon"
	elseif producer.Owner == p6 then
		name = "Martell"
	elseif producer.Owner == p7 then
		name = "Tyrell"
	end
	if produced.Type == "diplomacypin-war-lannister" then
		Media.DisplayMessage("The " .. name .. "s have declared war on the Lannisters")
		KeepAlive(produced)
		DeclareWar(produced.Owner,4)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-ally-lannister" then
		Media.DisplayMessage("The " .. name .. "s have declared an alliance with the Lannisters")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-neutral-lannister" then
		Media.DisplayMessage("The " .. name .. "s have declared neutrality with the Lannisters")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-war-stark" then
		Media.DisplayMessage("The " .. name .. "s have declared war on the Starks")
		KeepAlive(produced)
		DeclareWar(produced.Owner,1)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-ally-stark" then
		Media.DisplayMessage("The " .. name .. "s have declared an alliance with the Starks")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-neutral-stark" then
		Media.DisplayMessage("The " .. name .. "s have declared neutrality with the Starks")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-war-tully" then
		Media.DisplayMessage("The " .. name .. "s have declared war on the Tullys")
		KeepAlive(produced)
		DeclareWar(produced.Owner,2)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-ally-tully" then
		Media.DisplayMessage("The " .. name .. "s have declared an alliance with the Tullys")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-neutral-tully" then
		Media.DisplayMessage("The " .. name .. "s have declared neutrality with the Tullys")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-war-arryn" then
		Media.DisplayMessage("The " .. name .. "s have declared war on the Arryns")
		KeepAlive(produced)
		DeclareWar(produced.Owner,3)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-ally-arryn" then
		Media.DisplayMessage("The " .. name .. "s have declared an alliance with the Arryns")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-neutral-arryn" then
		Media.DisplayMessage("The " .. name .. "s have declared neutrality with the Arryns")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-war-baratheon" then
		Media.DisplayMessage("The " .. name .. "s have declared war on the Baratheons")
		KeepAlive(produced)
		DeclareWar(produced.Owner,6)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-ally-baratheon" then
		Media.DisplayMessage("The " .. name .. "s have declared an alliance with the Baratheons")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-neutral-baratheon" then
		Media.DisplayMessage("The " .. name .. "s have declared neutrality with the Baratheons")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-war-greyjoy" then
		Media.DisplayMessage("The " .. name .. "s have declared war on the Greyjoys")
		KeepAlive(produced)
		DeclareWar(produced.Owner,5)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-ally-greyjoy" then
		Media.DisplayMessage("The " .. name .. "s have declared an alliance with the Greyjoys")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-neutral-greyjoy" then
		Media.DisplayMessage("The " .. name .. "s have declared neutrality with the Greyjoys")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-war-martell" then
		Media.DisplayMessage("The " .. name .. "s have declared war on the Martells")
		KeepAlive(produced)
		DeclareWar(produced.Owner,7)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-ally-martell" then
		Media.DisplayMessage("The " .. name .. "s have declared an alliance with the Martells")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-neutral-martell" then
		Media.DisplayMessage("The " .. name .. "s have declared neutrality with the Martells")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-war-tyrell" then
		Media.DisplayMessage("The " .. name .. "s have declared war on the Tyrells")
		KeepAlive(produced)
		DeclareWar(produced.Owner,8)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-ally-tyrell" then
		Media.DisplayMessage("The " .. name .. "s have declared an alliance with the Tyrells")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	elseif produced.Type == "diplomacypin-neutral-tyrell" then
		Media.DisplayMessage("The " .. name .. "s have declared neutrality with the Tyrells")
		KeepAlive(produced)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	end
	if produced.Type == "message_target_lannister" or produced.Type == "message_target_stark" or produced.Type == "message_target_tully" or produced.Type == "message_target_arryn" or produced.Type == "message_target_greyjoy" or produced.Type == "message_target_baratheon" or produced.Type == "message_target_martell" or produced.Type == "message_target_tyrell" then
		produced.GrantCondition("keepalive",5)
		local messageTarget = nil
		if produced.Type == "message_target_stark" then
			messageTarget = p0
		elseif produced.Type == "message_target_tully" then
			messageTarget = p1
		elseif produced.Type == "message_target_arryn" then
			messageTarget = p2
		elseif produced.Type == "message_target_lannister" then
			messageTarget = p3
		elseif produced.Type == "message_target_greyjoy" then
			messageTarget = p4
		elseif produced.Type == "message_target_baratheon" then
			messageTarget = p5
		elseif produced.Type == "message_target_martell" then
			messageTarget = p6
		elseif produced.Type == "message_target_tyrell" then
			messageTarget = p7
		end
		if produced.Owner == p0 then
			p0target = messageTarget
		elseif produced.Owner == p1 then
			p1target = messageTarget
		elseif produced.Owner == p2 then
			p2target = messageTarget
		elseif produced.Owner == p3 then
			p3target = messageTarget
		elseif produced.Owner == p4 then
			p4target = messageTarget
		elseif produced.Owner == p5 then
			p5target = messageTarget
		elseif produced.Owner == p6 then
			p6target = messageTarget
		elseif produced.Owner == p7 then
			p7target = messageTarget
		end
		local targetFactionName
		if messageTarget == Player.GetPlayer("Multi0") then
			targetFactionName = "Starks"
		elseif messageTarget == Player.GetPlayer("Multi1") then
			targetFactionName = "Tullys"
		elseif messageTarget == Player.GetPlayer("Multi2") then
			targetFactionName = "Arryns"
		elseif messageTarget == Player.GetPlayer("Multi3") then
			targetFactionName = "Lannisters"
		elseif messageTarget == Player.GetPlayer("Multi4") then
			targetFactionName = "Greyjoys"
		elseif messageTarget == Player.GetPlayer("Multi5") then
			targetFactionName = "Baratheons"
		elseif messageTarget == Player.GetPlayer("Multi6") then
			targetFactionName = "Martells"
		elseif messageTarget == Player.GetPlayer("Multi7") then
			targetFactionName = "Tyrells"
		end
		if produced.Owner.IsLocalPlayer then
			Media.DisplayMessage("Now sending messages to the " .. targetFactionName .. ".")
		end
	end
	if produced.Type == "message_write" then
		if produced.Owner == p0 then
			p0msg = {}
			p0msg[1] = "Start"
		elseif produced.Owner == p1 then
			p1msg = {}
			p1msg[1] ="Start"
		elseif produced.Owner == p2 then
			p2msg = {}
			p2msg[1] = "Start"
		elseif produced.Owner == p3 then
			p3msg = {}
			p3msg[1] = "Start"
		elseif produced.Owner == p4 then
			p4msg = {}
			p4msg[1] = "Start"
		elseif produced.Owner == p5 then
			p5msg = {}
			p5msg[1] = "Start"
		elseif produced.Owner == p6 then
			p6msg = {}
			p6msg[1] = "Start"
		elseif produced.Owner == p7 then
			p7msg = {}
			p7msg[1] = "Start"
		end
	elseif produced.Type == "message_send" then
		if produced.Owner == p0 then
			if #p0msg > 1 then
				SendMessage(p0,p0msg,p0target)
			end
		elseif produced.Owner == p1 then
			if #p1msg > 1 then
				SendMessage(p1,p1msg,p1target)
			end
		elseif produced.Owner == p2 then
			if #p2msg > 1 then
				SendMessage(p2,p2msg,p2target)
			end
		elseif produced.Owner == p3 then
			if #p3msg > 1 then
				SendMessage(p3,p3msg,p3target)
			end
		elseif produced.Owner == p4 then
			if #p4msg > 1 then
				SendMessage(p4,p4msg,p4target)
			end
		elseif produced.Owner == p5 then
			if #p5msg > 1 then
				SendMessage(p5,p5msg,p5target)
			end
		elseif produced.Owner == p6 then
			if #p6msg > 1 then
				SendMessage(p6,p6msg,p6target)
			end
		elseif produced.Owner == p7 then
			if #p7msg > 1 then
				SendMessage(p7,p7msg,p7target)
			end
		end
	elseif produced.Type == "message_you" or produced.Type == "message_me" or produced.Type == "message_against" or produced.Type == "message_war" or produced.Type == "message_we" or produced.Type == "message_yes" or produced.Type == "message_no" or produced.Type == "message_and" or produced.Type == "message_peace" or produced.Type == "message_betray" or produced.Type == "message_with" or produced.Type == "message_alliance" or produced.Type == "message_move_here" or produced.Type == "message_attack_here" or produced.Type == "message_gather_here" or produced.Type == "message_stark" or produced.Type == "message_tully" or produced.Type == "message_arryn" or produced.Type == "message_lannister" or produced.Type == "message_greyjoy" or produced.Type == "message_baratheon" or produced.Type == "message_martell" or produced.Type == "message_tyrell" or produced.Type == "message_one" or produced.Type == "message_two" or produced.Type == "message_three" or produced.Type == "message_four" or produced.Type == "message_five" or produced.Type == "message_six" or produced.Type == "message_seven" or produced.Type == "message_eight" or produced.Type == "message_nine" or produced.Type == "message_ten" or produced.Type == "message_what" or produced.Type == "message_send_gold" or produced.Type == "message_send_food" or produced.Type == "message_gold" or produced.Type == "message_food" or produced.Type == "message_give" or produced.Type == "message_here" or produced.Type == "message_if" or produced.Type == "message_and" or produced.Type == "message_but" or produced.Type == "message_then" or produced.Type == "message_lie" or produced.Type == "message_i" or produced.Type == "message_will" or produced.Type == "message_this" or produced.Type == "message_continue" or produced.Type == "message_army" or produced.Type == "message_navy" or produced.Type == "message_take" or produced.Type == "message_declare" or produced.Type == "message_do" or produced.Type == "message_on" or produced.Type == "message_saw" or produced.Type == "message_settlement" or produced.Type == "message_gift_settlement" or produced.Type == "message_them" or produced.Type == "message_be" or produced.Type == "message_lost" or produced.Type == "message_battle" or produced.Type == "message_can" or produced.Type == "message_your" or produced.Type == "message_dominion" or produced.Type == "message_through" or produced.Type == "message_sentence_ally_me" or produced.Type == "message_sentence_declare_war_on" then
		local target = nil
		local speaker = produced.Owner
		if produced.Owner == p0 then
			target = p0target
		elseif produced.Owner == p1 then
			target = p1target
		elseif produced.Owner == p2 then
			target = p2target
		elseif produced.Owner == p3 then
			target = p3target
		elseif produced.Owner == p4 then
			target = p4target
		elseif produced.Owner == p5 then
			target = p5target
		elseif produced.Owner == p6 then
			target = p6target
		elseif produced.Owner == p7 then
			target = p7target
		end
		if produced.Owner == p0 and p0msg[1] == "Start" then
			AddMessage(produced.TooltipName,p0msg)
		elseif produced.Owner == p1 and p1msg[1] == "Start"  then
			AddMessage(produced.TooltipName,p1msg)
		elseif produced.Owner == p2 and p2msg[1] == "Start"  then
			AddMessage(produced.TooltipName,p2msg)
		elseif produced.Owner == p3 and p3msg[1] == "Start"  then
			AddMessage(produced.TooltipName,p3msg)
		elseif produced.Owner == p4 and p4msg[1] == "Start"  then
			AddMessage(produced.TooltipName,p4msg)
		elseif produced.Owner == p5 and p5msg[1] == "Start"  then
			AddMessage(produced.TooltipName,p5msg)
		elseif produced.Owner == p6 and p6msg[1] == "Start"  then
			AddMessage(produced.TooltipName,p6msg)
		elseif produced.Owner == p7 and p7msg[1] == "Start"  then
			AddMessage(produced.TooltipName,p7msg)
		end
	end
end

DeclareWar = function(producer,target)
	local diplopins = {"diplomacypin-war-stark", "diplomacypin-war-tully", "diplomacypin-war-arryn", "diplomacypin-war-lannister", "diplomacypin-war-greyjoy", "diplomacypin-war-baratheon", "diplomacypin-war-martell", "diplomacypin-war-tyrell"}
	local players = {p0,p1,p2,p3,p4,p5,p6,p7}
	for i = 1, #players do
		if producer == players[i] then
			local actor = Actor.Create(diplopins[i], true, { Owner = players[target], Location = CPos.New(10,10)})
			KeepAlive(actor)
		end
	end
end

AddMessage = function(message,messagetexts)
	messagetexts[#messagetexts+1] = message
end

KeepAlive = function(target)
	target.GrantCondition("keepalive", 5)
	target.GrantCondition("recentlyborn", 100)
end

SendMessage = function(speaker,messagetexts,target)
	local speakerName = ""
	local targetName = ""
	local textColor = HSLColor.Aqua
	if speaker == p0 then
		speakerName = "Starks (" .. p0.Name .. ") to "
		textColor = HSLColor.FromHex("FFFFFF")
	elseif speaker == p1 then
		speakerName = "Tullys " .. p1.Name .. ") to "
		textColor = HSLColor.FromHex("0094FF")
	elseif speaker == p2 then
		speakerName = "Arryns " .. p2.Name .. ") to "
		textColor = HSLColor.FromHex("00FFFF")
	elseif speaker == p3 then
		speakerName = "Lannisters " .. p3.Name .. ") to "
		textColor = HSLColor.FromHex("FF0000")
	elseif speaker == p4 then
		speakerName = "Greyjoys " .. p4.Name .. ") to "
		textColor = HSLColor.FromHex("606060")
	elseif speaker == p5 then
		speakerName = "Baratheons " .. p5.Name .. ") to "
		textColor = HSLColor.FromHex("FFD800")
	elseif speaker == p6 then
		speakerName = "Martells " .. p6.Name .. ") to "
		textColor = HSLColor.FromHex("FF6A00")
	elseif speaker == p7 then
		speakerName = "Tyrells " .. p7.Name .. ") to "
		textColor = HSLColor.FromHex("5BAD64")
	end
	if target == p0 then
		targetName = "Starks (" .. p0.Name .. ")"
	elseif target == p1 then
		targetName = "Tullys (" .. p1.Name .. ")"
	elseif target == p2 then
		targetName = "Arryns (" .. p2.Name .. ")"
	elseif target == p3 then
		targetName = "Lannisters (" .. p3.Name .. ")"
	elseif target == p4 then
		targetName = "Greyjoys (" .. p4.Name .. ")"
	elseif target == p5 then
		targetName = "Baratheons (" .. p5.Name .. ")"
	elseif target == p6 then
		targetName = "Martells (" .. p6.Name .. ")"
	elseif target == p7 then
		targetName = "Tyrells (" .. p7.Name .. ")"
	end
	local message = ""
	for i = 2, #messagetexts do
		if i > 2 and messagetexts[i] ~= "Stark" and messagetexts[i] ~= "Tully" and messagetexts[i] ~= "Arryn" and messagetexts[i] ~= "Lannister" and messagetexts[i] ~= "Greyjoy" and messagetexts[i] ~= "Baratheon" and messagetexts[i] ~= "Martell" and messagetexts[i] ~= "Tyrell" and messagetexts[i] ~= "I"  then
			messagetexts[i] = string.lower(messagetexts[i])
		end
		if messagetexts[i] == "Send 100 Gold" then
			target.Cash = target.Cash + 100
		end
		if messagetexts[i] == "Send 5 food" then
			local actor = Actor.Create("message_has_food", true, { Owner = target, Location = CPos.New(1,1) })
		end
		if messagetexts[i] == "Here" then
			Radar.Ping(speaker,Map.CenterOfCell(speaker.GetActorsByType("crow")[1].Location),textColor,375)
			Radar.Ping(target,Map.CenterOfCell(speaker.GetActorsByType("crow")[1].Location),textColor,375)
		end
		if messagetexts[i] == "Gift Settlement" then
			local settlement = nil
			local settlementName = ""
			local actors = Map.ActorsInCircle(Map.CenterOfCell(speaker.GetActorsByType("crow")[1].Location),WDist.FromCells(2))
			for i = 1, #actors do
				if actors[i].Type == "bigtown" or actors[i].Type == "smalltown" then
					if actors[i].Owner == speaker and actors[i].Health == actors[i].MaxHealth then
						actors[i].Owner = target
						messagetexts[i] = "(Gifts settlement)"
						KeepScore()
					else
						messagetexts[i] = "(Could not gift settlement due to low health)"
					end
				else
					messagetexts[i] = "(Could not gift settlement due to Gauntlet not being near a settlement)"
				end
			end
		end
		message = message .. " " .. messagetexts[i]
	end
	if target.IsLocalPlayer == true then
		Media.DisplayMessage("" .. message .. ".", "" ..speakerName .. targetName .. "", textColor)
	end
	if speaker.IsLocalPlayer == true then
		Media.DisplayMessage(""	 .. message .. ".", "" ..speakerName .. targetName .. "", textColor)
	end
	if messagetexts == p0msg then
		p0msg = {}
	elseif messagetexts == p1msg then
		p1msg = {}
	elseif messagetexts == p2msg then
		p2msg = {}
	elseif messagetexts == p3msg then
		p3msg = {}
	elseif messagetexts == p4msg then
		p4msg = {}
	elseif messagetexts == p5msg then
		p5msg = {}
	elseif messagetexts == p6msg then
		p6msg = {}
	end 
end

CreateNeutralities = function(player)
	local actor = Actor.Create("diplomacypin-neutral-lannister", true, { Owner = player, Location = CPos.New(10,10) })
	local actor = Actor.Create("diplomacypin-neutral-stark", true, { Owner = player, Location = CPos.New(10,10) })
	local actor = Actor.Create("diplomacypin-neutral-tully", true, { Owner = player, Location = CPos.New(10,10) })
	local actor = Actor.Create("diplomacypin-neutral-arryn", true, { Owner = player, Location = CPos.New(10,10) })
	local actor = Actor.Create("diplomacypin-neutral-baratheon", true, { Owner = player, Location = CPos.New(10,10) })
	local actor = Actor.Create("diplomacypin-neutral-tyrell", true, { Owner = player, Location = CPos.New(10,10) })
	local actor = Actor.Create("diplomacypin-neutral-martell", true, { Owner = player, Location = CPos.New(10,10) })
	local actor = Actor.Create("diplomacypin-neutral-greyjoy", true, { Owner = player, Location = CPos.New(10,10) })
end

GotTimer = function()
	local minutes = math.floor(gottimer / 60)
	local seconds = gottimer - (minutes * 60)
	local timetext = ""
	if seconds < 10 then
		timetext = "Time left to gain influence in King's Landing: " .. minutes .. ":0" .. seconds .. ""
	else
		timetext = "Time left to gain influence in King's Landing: " .. minutes .. ":" .. seconds .. ""
	end
	gottimer = gottimer - 1
	if p0.IsLocalPlayer == true then
		UserInterface.SetMissionText(timetext .. "\n\nStark:\nInfluence in King's Landing: " .. p0got .. "\nBribes to the Gold Cloaks: " .. p0gold .. "", HSLColor.White)
	elseif p1.IsLocalPlayer == true then
		UserInterface.SetMissionText(timetext .. "\n\nTully:\nInfluence in King's Landing: " .. p1got .. "\nBribes to the Gold Cloaks: " .. p1gold .. "", HSLColor.White)
	elseif p2.IsLocalPlayer == true then
		UserInterface.SetMissionText(timetext .. "\n\nArryn:\nInfluence in King's Landing: " .. p2got .. "\nBribes to the Gold Cloaks: " .. p2gold .. "", HSLColor.White)
	elseif p3.IsLocalPlayer == true then
		UserInterface.SetMissionText(timetext .. "\n\nLannister:\nInfluence in King's Landings: " .. p3got .. "\nBribes to the Gold Cloaks: " .. p3gold .. "", HSLColor.White)
	elseif p4.IsLocalPlayer == true then
		UserInterface.SetMissionText(timetext .. "\n\nGreyjoy:\nInfluence in King's Landing: " .. p4got .. "\nBribes to the Gold Cloaks: " .. p4gold .. "", HSLColor.White)
	elseif p5.IsLocalPlayer == true then
		UserInterface.SetMissionText(timetext .. "\n\nBaratheon:\nInfluence in King's Landing: " .. p5got .. "\nBribes to the Gold Cloaks: " .. p5gold .. "", HSLColor.White)
	elseif p6.IsLocalPlayer == true then
		UserInterface.SetMissionText(timetext .. "\n\nMartell:\nInfluence in King's Landing: " .. p6got .. "\nBribes to the Gold Cloaks: " .. p6gold .. "", HSLColor.White)
	elseif p7.IsLocalPlayer == true then
		UserInterface.SetMissionText(timetext .. "\n\nTyrell:\nInfluence in King's Landing: " .. p7got .. "\nBribes to the Gold Cloaks: " .. p7gold .. "", HSLColor.White)
	end
	if gottimer > 0 then
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			GotTimer()
		end)
	else
		UserInterface.SetMissionText("King Robert Baratheon is dead! The succession of the Seven Kingdoms is in question!", HSLColor.White)
		DecideWinnerofGot()
	end
end

DecideWinnerofGot = function()
	if starkp.IsDead == false then
		starkp.GrantCondition("gotended")
	end
	if tullyp.IsDead == false then
		tullyp.GrantCondition("gotended")
	end
	if arrynp.IsDead == false then
		arrynp.GrantCondition("gotended")
	end
	if lannisterp.IsDead == false then
		lannisterp.GrantCondition("gotended")
	end
	if greyjoyp.IsDead == false then
		greyjoyp.GrantCondition("gotended")
	end
	if baratheonp.IsDead == false then
		baratheonp.GrantCondition("gotended")
	end
	if martellp.IsDead == false then
		martellp.GrantCondition("gotended")
	end
	if tyrellp.IsDead == false then
		tyrellp.GrantCondition("gotended")
	end
	Media.DisplayMessage("The King of the Seven Kingdoms, Robert of the house Baratheon is dead and the succession is disputed amongst rumors and machinations!")
	local highscore = 0
	local winner = nil
	local tie = 0
	local ties = {}
	if p0gold > highscore then
		winner = p0
		highscore = p0gold
	end
	if p1gold > highscore then
		winner = p1
		highscore = p1gold
	end
	if p2gold > highscore then
		winner = p2
		highscore = p2gold
	end
	if p3gold > highscore then
		winner = p3
		highscore = p3gold
	end
	if p4gold > highscore then
		winner = p4
		highscore = p4gold
	end
	if p5gold > highscore then
		winner = p5
		highscore = p5gold
	end
	if p6gold > highscore then
		winner = p6
		highscore = p6gold
	end
	if p7gold > highscore then
		winner = p7
		highscore = p7gold
	end
	Trigger.AfterDelay(DateTime.Seconds(5), function()
		Media.DisplayMessage("Kings Landing is full of manouvering, as the noble houses seek to secure the Iron Throne for them selves.")
	end)
	local winnertext = ""
	if winner == p0 then
		p0got = p0got + 10
		winnerp = p0
		winnertext = "The Starks have secured the backing of the Gold Cloaks."
	elseif winner == p1 then
		p1got = p1got + 10
		winnerp = p1
		winnertext = "The Tullys have secured the backing of the Gold Cloaks."
	elseif winner == p2 then
		p2got = p2got + 10
		winnerp = p2
		winnertext = "The Arryns have secured the backing of the Gold Cloaks."
	elseif winner == p3 then
		p3got = p3got + 10
		winnerp = p3
		winnertext = "The Lannisters have secured the backing of the Gold Cloaks."
	elseif winner == p4 then
		p4got = p4got + 10
		winnerp = p4
		winnertext = "The Greyjoys have secured the backing of the Gold Cloaks."
	elseif winner == p5 then
		p5got = p5got + 10
		winnerp = p5
		winnertext = "The Baratheons have secured the backing of the Gold Cloaks."
	elseif winner == p6 then
		p6got = p6got + 10
		winnerp = p6
		winnertext = "The Martells have secured the backing of the Gold Cloaks."
	elseif winner == p7 then
		p7got = p7got + 10
		winnerp = p7
		winnertext = "The Tyrells have secured the backing of the Gold Cloaks."
	end
	Trigger.AfterDelay(DateTime.Seconds(10), function()
		Media.DisplayMessage("" .. winnertext .. "")
		if winnerp ~= nil then
			local actor = Actor.Create("pikeman", true, { Owner = winnerp, Location = CPos.New(128,140)})
			actor.Scatter()
			local actor = Actor.Create("pikeman", true, { Owner = winnerp, Location = CPos.New(128,140)})
			actor.Scatter()
		end
	end)
	Trigger.AfterDelay(DateTime.Seconds(11), function()
		winner = nil
		highscore = -5000
		if p0got > highscore then
			winner = p0
			highscore = p0got
		end
		if p1got > highscore then
			winner = p1
			highscore = p1got
		end
		if p2got > highscore then
			winner = p2
			highscore = p2got
		end
		if p3got > highscore then
			winner = p3
			highscore = p3got
		end
		if p4got > highscore then
			winner = p4
			highscore = p4got
		end
		if p5got > highscore then
			winner = p5
			highscore = p5got
		end
		if p6got > highscore then
			winner = p6
			highscore = p6got
		end
		if p7got > highscore then
			winner = p7
			highscore = p7got
		end
		if p0got == highscore then
			tie = tie + 1
			ties[tie] = p0
		end
		if p1got == highscore then
			tie = tie + 1
			ties[tie] = p1
		end
		if p2got == highscore then
			tie = tie + 1
			ties[tie] = p2
		end
		if p3got == highscore then
			tie = tie + 1
			ties[tie] = p3
		end
		if p4got == highscore then
			tie = tie + 1
			ties[tie] = p4
		end
		if p5got == highscore then
			tie = tie + 1
			ties[tie] = p5
		end
		if p6got == highscore then
			tie = tie + 1
			ties[tie] = p6
		end
		if p7got == highscore then
			tie = tie + 1
			ties[tie] = p7
		end
		if tie == 1 then
			if winner == p0 then
				GiveCrownlands(p0)
				Trigger.AfterDelay(DateTime.Seconds(1), function()
					Media.DisplayMessage("The Starks have secured the throne!")
				end)
			elseif winner == p1 then
				GiveCrownlands(p1)
				Trigger.AfterDelay(DateTime.Seconds(1), function()
					Media.DisplayMessage("The Tullys have secured the throne!")
				end)
			elseif winner == p2 then
				GiveCrownlands(p2)
				Trigger.AfterDelay(DateTime.Seconds(1), function()
					Media.DisplayMessage("The Arryns have secured the throne!")
				end)
			elseif winner == p3 then
				GiveCrownlands(p3)
				Trigger.AfterDelay(DateTime.Seconds(1), function()
					Media.DisplayMessage("The Lannisters have secured the throne!")
				end)
			elseif winner == p4 then
				GiveCrownlands(p4)
				Trigger.AfterDelay(DateTime.Seconds(1), function()
					Media.DisplayMessage("The Greyjoys have secured the throne!")
				end)
			elseif winner == p5 then
				GiveCrownlands(p5)
				Trigger.AfterDelay(DateTime.Seconds(1), function()
					Media.DisplayMessage("The Baratheons have secured the throne!")
				end)
			elseif winner == p6 then
				GiveCrownlands(p6)
				Trigger.AfterDelay(DateTime.Seconds(1), function()
					Media.DisplayMessage("The Martells have secured the throne!")
				end)
			elseif winner == p7 then
				GiveCrownlands(p7)
				Trigger.AfterDelay(DateTime.Seconds(1), function()
					Media.DisplayMessage("The Tyrells have secured the throne!")
				end)
			end
		else
			GiveCrownlandsToTies(ties)
			local tieTexts = ""
			for i = 1, #ties do
				if ties[i] == p0 then
				 tieTexts = tieTexts .. "Starks, "
				end
				if ties[i] == p1 then
				 tieTexts = tieTexts .. "Tullys, "
				end
				if ties[i] == p2 then
				 tieTexts = tieTexts .. "Arryns, "
				end
				if ties[i] == p3 then
				 tieTexts = tieTexts .. "Lannisters, "
				end
				if ties[i] == p4 then
				 tieTexts = tieTexts .. "Greyjoys, "
				end
				if ties[i] == p5 then
				 tieTexts = tieTexts .. "Baratheons, "
				end
				if ties[i] == p6 then
				 tieTexts = tieTexts .. "Martells, "
				end
				if ties[i] == p0 then
				 tieTexts = tieTexts .. "Tyrells, "
				end
			end
			tieTexts = tieTexts .. "dividing the loyalties of the Crownlands."
			Trigger.AfterDelay(DateTime.Seconds(5), function()
				Media.DisplayMessage("There is a tie between following houses: " .. tieTexts .. " Various settlements have declared for these factions.")
			end)
		end
		if p0.IsLocalPlayer == true then
			UserInterface.SetMissionText("", HSLColor.White)
		elseif p1.IsLocalPlayer == true then
			UserInterface.SetMissionText("", HSLColor.White)
		elseif p2.IsLocalPlayer == true then
			UserInterface.SetMissionText("", HSLColor.White)
		elseif p3.IsLocalPlayer == true then
			UserInterface.SetMissionText("", HSLColor.White)
		elseif p4.IsLocalPlayer == true then
			UserInterface.SetMissionText("", HSLColor.White)
		elseif p5.IsLocalPlayer == true then
			UserInterface.SetMissionText("", HSLColor.White)
		elseif p6.IsLocalPlayer == true then
			UserInterface.SetMissionText("", HSLColor.White)
		elseif p7.IsLocalPlayer == true then
			UserInterface.SetMissionText("", HSLColor.White)
		end
	end)
end

GiveCrownlands = function(targetplayer)
	Trigger.AfterDelay(DateTime.Seconds(5), function()
		kingslanding.Owner = targetplayer
		rosby.Owner = targetplayer
		antlers.Owner = targetplayer
		duskendale.Owner = targetplayer
		chyttering.Owner = targetplayer
		sharppoint.Owner = targetplayer
		dragonstone.Owner = targetplayer
		rooksrest.Owner = targetplayer
		dyreden.Owner = targetplayer
		kingslandingtext.Owner = targetplayer
		rosbytext.Owner = targetplayer
		antlerstext.Owner = targetplayer
		duskendaletext.Owner = targetplayer
		chytteringtext.Owner = targetplayer
		sharppointtext.Owner = targetplayer
		dragonstonetext.Owner = targetplayer
		rooksresttext.Owner = targetplayer
		dyredentext.Owner = targetplayer
		rn1.Owner = targetplayer
		rn2.Owner = targetplayer
		rn3.Owner = targetplayer
		Media.DisplayMessage("What ever happens next is bound to drag the whole of Westeros into war and chaos.")
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	end)
end

GiveCrownlandsToTies = function(ties)
	Trigger.AfterDelay(DateTime.Seconds(5), function()
		local newowner = ties[Utils.RandomInteger(1,#ties+1)]
		kingslanding.Owner = newowner
		kingslandingtext.Owner = newowner
		newowner = ties[Utils.RandomInteger(1,#ties+1)]
		rosby.Owner = newowner
		rosbytext.Owner = newowner
		newowner = ties[Utils.RandomInteger(1,#ties+1)]
		antlers.Owner = newowner
		antlerstext.Owner = newowner
		newowner = ties[Utils.RandomInteger(1,#ties+1)]
		duskendale.Owner = newowner
		duskendaletext.Owner = newowner
		newowner = ties[Utils.RandomInteger(1,#ties+1)]
		chyttering.Owner = newowner
		chytteringtext.Owner = newowner
		newowner = ties[Utils.RandomInteger(1,#ties+1)]
		sharppoint.Owner = newowner
		sharppointtext.Owner = newowner
		newowner = ties[Utils.RandomInteger(1,#ties+1)]
		dragonstone.Owner = newowner
		dragonstonetext.Owner = newowner
		rn1.Owner = newowner
		rn2.Owner = newowner
		rn3.Owner = newowner
		newowner = ties[Utils.RandomInteger(1,#ties+1)]
		rooksrest.Owner = newowner
		rooksresttext.Owner = newowner
		newowner = ties[Utils.RandomInteger(1,#ties+1)]
		dyreden.Owner = newowner
		dyredentext.Owner = newowner
		Media.DisplayMessage("What ever happens next is bound to drag the whole of Westeros into war and chaos.")
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			KeepScore()
		end)
	end)
end

CaptureTriggers = function()
	local players = {p0,p1,p2,p3,p4,p5,p6,p7}
	for i = 1, #players do
		local bigtowns = players[i].GetActorsByType("bigtown")
		for i = 1, #bigtowns do
			Trigger.OnCapture(bigtowns[i], TownCaptured)
		end
		local smalltowns = players[i].GetActorsByType("smalltown")
		for i = 1, #bigtowns do
			Trigger.OnCapture(smalltowns[i], TownCaptured)
		end
	end
end

TownCaptured = function(self,captor,oldOwner,newOwner)
	KeepScore()
end

KeepScore = function()
	local players = {p0,p1,p2,p3,p4,p5,p6,p7}
	local score = 0
	local allyscore = 0
	local scores = {p0score, p1score, p2score, p3score, p4score, p5score, p6score, p7score}
	local alliances = {"diplomacypin-ally-stark", "diplomacypin-ally-tully", "diplomacypin-ally-arryn", "diplomacypin-ally-lannister", "diplomacypin-ally-greyjoy", "diplomacypin-ally-baratheon", "diplomacypin-ally-martell", "diplomacypin-ally-tyrell"}
	local toptext = "Win condition: Hold King's Landing and have " .. winpoints .. " points. | The winner's allies rank in order of their points.\nFaction name:  Cities|Towns|Allies|King's Landing = Total score\n"
	local towntext = ""
	local haskingslanding = false
	for i =1, #players do
		local bigtowns = players[i].GetActorsByType("bigtown")
		local smalltowns = players[i].GetActorsByType("smalltown")
		scores[i] = #bigtowns*4 + #smalltowns
	end
	for i = 1, #players do
		score = 0
		allyscore = 0
		haskingslanding = false
		if players[i] == p0 then
			towntext = "" .. towntext .. "Starks: "
		elseif players[i] == p1 then
			towntext = "" .. towntext .. "Tullys: "
		elseif players[i] == p2 then
			towntext = "" .. towntext .. "Arryns: "
		elseif players[i] == p3 then
			towntext = "" .. towntext .. "Lannisters: "
		elseif players[i] == p4 then
			towntext = "" .. towntext .. "Greyjoys: "
		elseif players[i] == p5 then
			towntext = "" .. towntext .. "Baratheons: "
		elseif players[i] == p6 then
			towntext = "" .. towntext .. "Martells: "
		elseif players[i] == p7 then
			towntext = "" .. towntext .. "Tyrells: "
		end
		towntext = towntext .. "(" .. players[i].Name .. ")"
		local bigtowns = players[i].GetActorsByType("bigtown")
		towntext = "" .. towntext .. " " .. #bigtowns .. ""
		local smalltowns = players[i].GetActorsByType("smalltown")
		towntext = "" .. towntext .. "|" .. #smalltowns .. ""
		for i = 1, #bigtowns do
			if bigtowns[i] == kingslanding then

				haskingslanding = true
			end
		end
		for y = 1, #players do
			if #players[i].GetActorsByType(alliances[y]) > 0 then
				if #players[y].GetActorsByType(alliances[i]) > 0 then
					allyscore = allyscore + math.floor((#players[y].GetActorsByType("bigtown")*4 + #players[y].GetActorsByType("smalltown"))/4)
				end
			end
		end
		towntext = "" .. towntext .. "|" .. allyscore .. ""
		score = score + allyscore + #bigtowns*4 + #smalltowns
		if haskingslanding == false then
			towntext = "" .. towntext .. "|n/a"
		else
			towntext = "" .. towntext .. "|KL"
		end
		towntext = "" .. towntext .. "=" .. score .. "      "
		if i == 4 then
			towntext = towntext .. "\n"
		end
		if score > winpoints and kingslanding.Owner == players[i] then
			winners[1] = players[i]
			for y = 1, #players do
				if #players[i].GetActorsByType(alliances[y]) > 0 then
					if #players[y].GetActorsByType(alliances[i]) > 0 then
						winners[#winners+1] = players[y]
					end
				end
			end
			EndGame()
		end
	end
	UserInterface.SetMissionText(toptext .. "\n" .. towntext .. "\n", HSLColor.White)
end

EndGame = function()
	local players = {p0,p1,p2,p3,p4,p5,p6,p7}
	local kill = {}
	local iswinner = false
	for i = 1, #players do
		iswinner = false
		for y = 1, #winners do
			if players[i] == winners[y] then
				iswinner = true
			end
		end
		if iswinner == false then
			kill[#kill+1] = players[i]
		end
	end
	for i = 1, #winners do
		winners[i].MarkCompletedObjective(0)
	end
	for i = 1, #kill do
		kill[i].MarkFailedObjective(0)
	end
	Player.GetPlayer("Boltons").MarkFailedObjective(0)
	Player.GetPlayer("Freys").MarkFailedObjective(0)
	Player.GetPlayer("Royces").MarkFailedObjective(0)
	Player.GetPlayer("Crakehalls").MarkFailedObjective(0)
	Player.GetPlayer("Harlaws").MarkFailedObjective(0)
	Player.GetPlayer("Carons").MarkFailedObjective(0)
	Player.GetPlayer("Daynes").MarkFailedObjective(0)
	Player.GetPlayer("Hightowers").MarkFailedObjective(0)
end

SetWinConditionAndInfluence = function()	
	if Map.LobbyOption("wincondition") == "50" then
		winpoints = 50
	elseif Map.LobbyOption("wincondition") == "75" then
		winpoints = 75
	elseif Map.LobbyOption("wincondition") == "100" then
		winpoints = 100
	elseif Map.LobbyOption("wincondition") == "150" then
		winpoints = 150
	end
	if Map.LobbyOption("influence") == "60" then
		gottimer = 60
	elseif Map.LobbyOption("influence") == "120" then
		gottimer = 120
	elseif Map.LobbyOption("influence") == "180" then
		gottimer = 180
	elseif Map.LobbyOption("influence") == "240" then
		gottimer = 240
	elseif Map.LobbyOption("influence") == "300" then
		gottimer = 300
	elseif Map.LobbyOption("influence") == "0" then
		gottimer = 0
	end
end

InterestPayment = function(targetplayer)
	local pdebt = 0
	local interesttext = ""
	for i = 1, #mainfactions do
		if mainfactions[i] == targetplayer then
			pdebt = debts[i]
		end
	end
	if pdebt > 0 then
		if targetplayer.IsLocalPlayer == true then
			if pdebt > targetplayer.Resources then
				local overflow = 0
				overflow = math.floor(pdebt * 0.1) - targetplayer.Resources
				interesttext = "\nInterest payment of 10%: " .. math.floor(pdebt*0.1) .. ".\nNew debt: " .. overflow .. "\nTotal debt: " .. (pdebt + overflow)
				targetplayer.Resources = targetplayer.Resources - math.floor(pdebt * 0.1)
				pdebt = pdebt + overflow
			else
				targetplayer.Resources = targetplayer.Resources - pdebt * 0.1
				interesttext = "\nInterest payment of 10%: " .. math.floor(pdebt*0.1) .. ""
				if pdebt > 50 and targetplayer.Resources > 50 then
					targetplayer.Resources = targetplayer.Resources - 50
					pdebt = pdebt - 50
					interesttext = interesttext .. "\nDebt after 50$ instalment: " .. pdebt .. "", "Iron Bank"
				end
			end
			if pdebt > 500 then
				interesttext = interesttext .. "\n--------------\nYour total debt is over 500. You can not build any more armies and navies or recruit mercenaries."
				targetplayer.GetActorsByTypes({"stark", "tully", "arryn", "lannister", "greyjoy", "baratheon", "martell", "tyrell"})[1].GrantCondition("lowpower",1500)
			end
			if targetplayer == p0 then
				incomemsg[1] = incomemsg[1] .. interesttext
			elseif targetplayer == p1 then
				incomemsg[2] = incomemsg[2] .. interesttext
			elseif targetplayer == p2 then
				incomemsg[3] = incomemsg[3] .. interesttext
			elseif targetplayer == p3 then
				incomemsg[4] = incomemsg[4] .. interesttext
			elseif targetplayer == p4 then
				incomemsg[5] = incomemsg[5] .. interesttext
			elseif targetplayer == p5 then
				incomemsg[6] = incomemsg[6] .. interesttext
			elseif targetplayer == p6 then
				incomemsg[7] = incomemsg[7] .. interesttext
			elseif targetplayer == p7 then
				incomemsg[8] = incomemsg[8] .. interesttext
			end
		end
	end
	for i = 1, #mainfactions do
		if mainfactions[i] == targetplayer then
			debts[i] = pdebt
		end
	end
	PlayerMessage(targetplayer)
end

FindAllUnits = function(targetplayer)
	if targetplayer.IsLocalPlayer == true then
		Media.PlaySound("coins.aud")
	end
	if targetplayer == p0 then
		p0cost = 0
	elseif targetplayer == p1 then
		p1cost = 0
	elseif targetplayer == p2 then
		p2cost = 0
	elseif targetplayer == p3 then
		p3cost = 0
	elseif targetplayer == p4 then
		p4cost = 0
	elseif targetplayer == p5 then
		p5cost = 0
	elseif targetplayer == p6 then
		p6cost = 0
	elseif targetplayer == p7 then
		p7cost = 0
	end
	local allunits = {}
	local totalcost = 0
	allunits = targetplayer.GetActorsByTypes({"militia", "skirmisher", "spearman", "swordman", "pikeman", "archer", "longbow", "crossbow", "catapult", "trebuchet", "scouts", "cavalry", "knight", "horsearcher", "cog", "carrack", "galley", "transportship", "peasant", "mercenary_militia", "mercenary_skirmisher", "mercenary_swordman", "mercenary_pikeman", "mercenary_crossbow", "mercenary_scouts", "mercenary_cavalry", "mercenary_knight", "mercenary_horsearcher", "pirate_carrack", "pirate_caravel", "mercenary_unsullied"})
	for i = 1, #allunits do
		totalcost = totalcost + math.floor(Actor.Cost(allunits[i].Type)*0.1)
	end
	TotalCost(targetplayer,totalcost)
end

TotalCost = function(targetplayer,totalcost)
	local pdebt = 0
	local income = 0
	local incometext = ""
	for i = 1, #mainfactions do
		if targetplayer == mainfactions[i] then
			pdebt = debts[i]
			income = incomes[i]
		end
	end
		if targetplayer.IsLocalPlayer then
			incometext = "Total income: " .. targetplayer.Resources - income .. ".\nMiltary upkeep: -" .. totalcost .. ".\nNet income: " .. targetplayer.Resources - income - totalcost .. "."
		end
		if totalcost > targetplayer.Resources then
			local overflow = 0
			overflow = totalcost - targetplayer.Resources
			targetplayer.Resources = targetplayer.Resources - totalcost
			pdebt = pdebt + overflow
			if targetplayer.IsLocalPlayer == true then
				incometext = incometext .. "\n--------------\nNew debt: " .. overflow
			end
		else
			targetplayer.Resources = targetplayer.Resources - totalcost
		end
		for i = 1, #mainfactions do
			if targetplayer == mainfactions[i] then
				debts[i] = pdebt
				if targetplayer.IsLocalPlayer == true then
					if targetplayer == p0 then
						incomemsg[1] = incometext
					elseif targetplayer == p1 then
						incomemsg[2] = incometext
					elseif targetplayer == p2 then
						incomemsg[3] = incometext
					elseif targetplayer == p3 then
						incomemsg[4] = incometext
					elseif targetplayer == p4 then
						incomemsg[5] = incometext
					elseif targetplayer == p5 then
						incomemsg[6] = incometext
					elseif targetplayer == p6 then
						incomemsg[7] = incometext
					elseif targetplayer == p7 then
						incomemsg[8] = incometext
					end
				end
			end
		end
end

PlayerMessage = function(targetplayer)
	local playernumber = 0
	for i = 1, #mainfactions do
		if mainfactions[i] == targetplayer then
			playernumber = i
		end
	end
	if targetplayer.IsLocalPlayer == true then
		Media.DisplaySystemMessage(incomemsg[playernumber], "Marshall")
	end
end

Tick = function()
	neutmoney = Player.GetPlayer("Neutral").Resources
	if neutmoney > neutincome then
		neutincome = neutmoney
		for i = 1, #mainfactions do
			FindAllUnits(mainfactions[i])
			InterestPayment(mainfactions[i])
		end
	else
		for i = 1, #mainfactions do
			incomes[i] = mainfactions[i].Resources
		end
	end
end

PlayMusic = function()
	Media.PlayMusic(nil, PlayMusic)
end

WorldLoaded = function()
	SetWinConditionAndInfluence()
	CreateNeutralities(p0)
	CreateNeutralities(p1)
	CreateNeutralities(p2)
	CreateNeutralities(p3)
	CreateNeutralities(p4)
	CreateNeutralities(p5)
	CreateNeutralities(p6)
	CreateNeutralities(p7)
	Trigger.OnAnyProduction(Produces)
	CaptureTriggers()
	GotTimer()
	Media.PlayMusic("longsilence", PlayMusic)
end