PeasentTypes = {"mwwand1","mwwand2","mwwand3","mwwand6","mwwand4","mwwand5"}
Noble = {"noble3","noble4","noble1","noble2"}


WorldLoaded = function()
	-- set up Player
	People = {}
	
	Trigger.AfterDelay(DateTime.Seconds(1), function()
		People = Player.GetPlayers(function(P)
			return P.InternalName ~= "Neutral" and P.InternalName ~= "Creeps" and P.InternalName ~= "Everyone"
		end)
	end)
	
	Trigger.AfterDelay(DateTime.Seconds(2),function()
		--print(tostring(#People) .. " player found")
		for i = 1,#People do
		
			local who = People[i]
			--print(who.Name .. " Triggeredd!")
						
			Trigger.AfterDelay(DateTime.Seconds(10),function()
				SpawnSettlers(who)
			end)
			Trigger.AfterDelay(DateTime.Seconds(10),function()
				SpawnCommander(who)
			end)
		end
		Trigger.AfterDelay(DateTime.Seconds(60),SpawnSmithWorker)
	end)
end

Tick = function()
end

Talkagain = function(Actor)
	if not Actor.IsDead and (Actor.Type == "mwwand1" or Actor.Type == "mwwand2" or Actor.Type == "mwwand3" or Actor.Type == "mwwand4" or Actor.Type == "mwwand5" or Actor.Type == "mwwand6") then
		Actor.GrantCondition("Talking",Utils.RandomInteger(150,500))
		Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(15,90)),function()
			Talkagain(Actor)
		end)
	end
end

SpawnCommander = function(Player)


	local who = Player
	local commander = {}
	local barracks = {}
	local stalls = {}
	local hut = {}

	commander = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return (C.Type == "mwcivcom" ) and C.Owner == who
	end)
	--print(who.Name .. " Checked for peasantss: " .. tostring(#peasantss))
	
	barracks = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return (C.Type == "barrnew" or C.Type == "handnew" ) and C.Owner == who
	end)
	
	stalls = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return (C.Type == "stall" or C.Type == "nodstall" ) and C.Owner == who
	end)
	
	hut = barracks[Utils.RandomInteger(1,#barracks+1)]
	
	if #commander < 1 and hut ~= nil and #stalls > 0 then
					
		peasent = Actor.Create("mwcivcom",true,{Owner = who,Location = hut.Location+CVec.New(1,1)})
		peasent.Move(peasent.Location+CVec.New(0,2),3)
		--print(who.Name .. " peasants spawned!")
	
	end
	
	Trigger.AfterDelay(DateTime.Seconds(10+math.floor(100/((#stalls*2 + #barracks +2 )/2))),function()
		SpawnCommander(who)
	end)	
end

SpawnSmithWorker = function()

	for i = 1,#People do
	
		local who = People[i]
		local labor = {}
		local worksgops = {}
		local hut = {}
	
		labor = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
			return (C.Type == "mwwand9" ) and C.Owner == People[i]
		end)
		--print(who.Name .. " Checked for peasantss: " .. tostring(#peasantss))
		
		worksgops = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
			return (C.Type == "workshopnod" or C.Type == "workshopgdi" ) and C.Owner == People[i]
		end)
		
		hut = worksgops[Utils.RandomInteger(1,#worksgops+1)]
		
		if #labor < #worksgops*2 and hut ~= nil then
						
			peasent = Actor.Create("mwwand9",true,{Owner = who,Location = hut.Location+CVec.New(1,0)})
			peasent.Move(peasent.Location+CVec.New(0,-1),3)
			--print(who.Name .. " peasants spawned!")
		
		end
		
	end

	for i = 1,#People do
	
		local who = People[i]
		local bursar = {}
		local procssmall = {}
		local procslarge = {}
		local hut = {}
		local facts = {}
	
		bursar = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
			return (C.Type == "mwwand8" ) and C.Owner == People[i]
		end)
		--print(who.Name .. " Checked for peasantss: " .. tostring(#peasantss))
		
		procssmall = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
			return C.Type == "zeroproc" and C.Owner == People[i]
		end)
		
		procslarge = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
			return C.Type == "procnew" and C.Owner == People[i]
		end)
		
		facts = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
			return C.Type == "factnew" and C.Owner == People[i]
		end)
		hut = facts[Utils.RandomInteger(1,#facts+1)]
		
		
		if ((2 + #bursar*3) < (#procssmall + #procslarge*2)) and hut ~= nil then
						
			peasent = Actor.Create("mwwand8",true,{Owner = who,Location = hut.Location+CVec.New(1,2)})
			peasent.Move(peasent.Location+CVec.New(0,1),3)
			--print(who.Name .. " peasants spawned!")
		
		end
		
	end
	
	

	for i = 1,#People do
	
		local who = People[i]
		local blacksmiths = {}
		local smiths = nil
		local hut = {}
	
		blacksmiths = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
			return (C.Type == "mwwand7" ) and C.Owner == People[i]
		end)
		--print(who.Name .. " Checked for peasantss: " .. tostring(#peasantss))
		
		smiths = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
			return (C.Type == "hqnewnod" or C.Type == "hqnewgdi") and C.Owner == People[i]
		end)
		
		
		if #blacksmiths < #smiths then
		
			hut = smiths[Utils.RandomInteger(1,#smiths+1)]
				
			peasent = Actor.Create("mwwand7",true,{Owner = who,Location = hut.Location+CVec.New(1,1)})
			peasent.Move(peasent.Location+CVec.New(0,1),3)
			--print(who.Name .. " peasants spawned!")
		
		end
		
	end
	Trigger.AfterDelay(DateTime.Seconds(60),SpawnSmithWorker)
end

SpawnSettlers = function(Player)


	local who = Player
	local hut = nil
	local largehuts = {}
	local huts = {}
	local peasantss = {}
	local peasent = nil
	local PeasType = nil
	local castles = nil
	local nobles = nil
	
	
	peasantss = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return (C.Type == "mwwand1" or C.Type == "mwwand2" or C.Type == "mwwand3" or C.Type == "mwwand4" or C.Type == "mwwand5" or C.Type == "mwwand6") and C.Owner == who
	end)
	--print(who.Name .. " Checked for peasantss: " .. tostring(#peasantss))
	
	huts = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return C.Type == "nukenew" and C.Owner == who
	end)
	--print(who.Name .. " Checked for huts: " .. tostring(#huts))
	
	largehuts = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return C.Type == "nuk2new" and C.Owner == who
	end)
	--print(who.Name .. " Checked for largehuts: " .. tostring(#largehuts))
	
	if #peasantss < (#huts*3 + #largehuts*5) then
	
		if #largehuts ~= 0 and #huts ~= 0 then
		
			local rnd = Utils.RandomInteger(1,3)
			if rnd == 1 then
				hut = huts[Utils.RandomInteger(1,#huts+1)]
				--print(who.Name .. " small hut choosen! " .. tostring(hut.Type) )
			elseif rnd == 2 then
				hut = largehuts[Utils.RandomInteger(1,#largehuts+1)]
				--print(who.Name .. " large hut choosen! " .. tostring(hut.Type) )
			end
			
		elseif #largehuts ~= 0 then
			hut = largehuts[Utils.RandomInteger(1,#largehuts+1)]
			--print(who.Name .. " large hut choosen! " .. tostring(hut.Type) )
		elseif #huts ~= 0 then
			hut = huts[Utils.RandomInteger(1,#huts+1)]
			--print(who.Name .. " small hut choosen! " .. tostring(hut.Type)  )
		else
			local Facts = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
				return C.Type == "factnew" and C.Owner == who
			end)
			hut = Facts[Utils.RandomInteger(1,#Facts+1)]
		end
		
		PeasType = PeasentTypes[Utils.RandomInteger(1,#PeasentTypes+1)]
		
		peasent = Actor.Create(PeasType,true,{Owner = who,Location = hut.Location+CVec.New(1,1)})
		peasent.Move(peasent.Location+CVec.New(0,1),3)
		Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(30,150)),function()
			Talkagain(peasent)
		end)
		--print(who.Name .. " peasants spawned!")
	
	end
	
	nobles = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return (C.Type == "noble1" or C.Type == "noble2" or C.Type == "noble3" or C.Type == "noble4") and C.Owner == who
	end)
	--print(who.Name .. " Checked for peasantss: " .. tostring(#peasantss))
	
	castles = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return (C.Type == "castle" or C.Type == "castle2") and C.Owner == who
	end)
	
	
	
	landlord = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return C.Type == "noble5" and C.Owner == who
	end)
	
	if #landlord < (#castles) and #castles ~= 0 then
	
		hut = castles[Utils.RandomInteger(1,#castles+1)]
		--print(who.Name .. " male choosen!" .. PeasType)
		
		peasent = Actor.Create("noble5",true,{Owner = who,Location = hut.Location+CVec.New(1,2)})
		peasent.Move(peasent.Location+CVec.New(0,1),3)
		Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(15,90)),function()
			Talkagain(peasent)
		end)
	elseif #nobles < (#castles+4) and #castles ~= 0 then
	
		hut = castles[Utils.RandomInteger(1,#castles+1)]
		PeasType = Noble[Utils.RandomInteger(1,#Noble+1)]
		--print(who.Name .. " male choosen!" .. PeasType)
		
		peasent = Actor.Create(PeasType,true,{Owner = who,Location = hut.Location+CVec.New(1,2)})
		peasent.Move(peasent.Location+CVec.New(0,1),3)
		Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(15,90)),function()
			Talkagain(peasent)
		end)
	end
	
	if (#largehuts + #huts) == 0 then
		Trigger.AfterDelay(DateTime.Seconds(10),function()
			SpawnSettlers(who)
		end)
	else
		Trigger.AfterDelay(DateTime.Seconds(10+math.floor(60/((#castles*3 +#largehuts*2 + #huts +2)/2))),function()
			SpawnSettlers(who)
		end)	
	end
end