PeasentTypes = {"mwwand1","mwwand2","mwwand3","mwwand6","mwwand4","mwwand5","mwwand10","mwwand11","mwwand12"}
Noble = {"noble3","noble4","noble1","noble2"}
PropsList = {"mwconst1","mwconst2","mwconst3","mwconst4","mwconst5","mwconst6","mwconst7"}


WorldLoaded = function()
	-- set up Player
	People = {}
	
	Trigger.AfterDelay(1, function()
		People = Player.GetPlayers(function(P)
			return P.InternalName ~= "Neutral" and P.InternalName ~= "Creeps" and P.InternalName ~= "Everyone"
		end)
	end)
	
	Trigger.AfterDelay(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)
	Trigger.OnEnteredProximityTrigger(Map.CenterOfCell(CPos.New(64,64)),WDist.FromCells(128), function(a, id)
		if a.Type == "nukenew" or a.Type == "nuk2new" then
			ChangeApperance(a)
		end
		if a.Type == "mwconst1back" then
			local who = a.Owner
			local Factry = nil
			local entries = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
				return C.Type == "factnew" and C.Owner == who
			end)
			
			Factry = Utils.Random(entries)
			
			if Factry then
				a.Move(Factry.Location+CVec.New(1,2),5)
				a.Destroy()
			else
				a.Destroy()
			end
		end
	end)

end

Tick = function()
end

ChangeApperance = function(a)

	if a.Type == "nukenew" then
	
		local rndsmall = Utils.RandomInteger(1,4)
		
		if rndsmall == 1 then
			local b = Actor.Create("nuk4new",true,{Owner = a.Owner,Location = a.Location})
			a.Destroy()
		elseif rndsmall == 2 then
			local b = Actor.Create("nuk5new",true,{Owner = a.Owner,Location = a.Location})
			a.Destroy()
		end
		
	elseif a.Type == "nuk2new" then
	
		local rndlarge = Utils.RandomInteger(1,3)
		
		if rndlarge == 1 then
			local b = Actor.Create("nuk3new",true,{Owner = a.Owner,Location = a.Location})
			a.Destroy()
		end
	end
	--[[Trigger.OnEnteredProximityTrigger(Map.CenterOfCell(CPos.New(64,64)),WDist.FromCells(128), function(a, id)
		if a.Type == (a.Type == "nukenew" or a.Type == "nuk2new") then
			ChangeApperance(a)
		end
	end)--]]
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 = {}
	local nobles = {}
	local Facts = {}
	local Chicken = {}
	
	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" or C.Type == "mwwand10" or C.Type == "mwwand11" or C.Type == "mwwand12") and C.Owner == who
	end)
	
	Chicken = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return (C.Type == "mwchick1") 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" or C.Type == "nuk4new" or C.Type == "nuk5new") 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" or C.Type == "nuk3new") and C.Owner == who
	end)
	
	Facts = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return C.Type == "factnew" 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
			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)
		table.insert(peasantss,peasent)
		
		Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(30,150)),function()
			Talkagain(peasent)
		end)
		
		if (#peasantss + #nobles) > #Chicken*3 then
			Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(25,100)),function()
				local chicko = Actor.Create("mwchick1",true,{Owner = who,Location = hut.Location+CVec.New(1,2)})
				chicko.Move(peasent.Location+CVec.New(0,1),3)
			end)
		end
		
		
	end
		--print(who.Name .. " peasants spawned!")
	
	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)
		table.insert(peasantss,peasent)
		
		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
	
	local Stuff = {}
	local Spawn = nil
	local dep = nil
	local here = {}
	local Factor = nil
	
	here = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return (C.Type == "nukenew" or C.Type == "nuk4new" or C.Type == "nuk5new" or C.Type == "nuk2new" or C.Type == "nuk3new") and C.Owner == who
	end)
	
	Stuff = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(C)
		return (C.Type == "mwconst1" or C.Type == "mwconstback" or C.Type == "prop1" or C.Type == "prop2" or C.Type == "prop3" or C.Type == "prop4" or C.Type == "prop5" or C.Type == "prop6" or C.Type == "prop7" ) and C.Owner == who
	end)
	
	if #peasantss > ((#Stuff+1)*4) then
		
		Spawn = Utils.Random(here)
		Factor = Utils.Random(Facts)
		
		
		
			print(who.Name .. " TimeTrigger PropWorker!")
		Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(3,8)),function()
			if Spawn and Factor then
				print(who.Name .. " Spawn PropWorker!")
				dep = Actor.Create(Utils.Random(PropsList),true,{Owner = who,Location = Factor.Location+CVec.New(1,1)})
				
				dep.Move(dep.Location+CVec.New(0,1),3)
				dep.Move(Spawn.Location+CVec.New(Utils.RandomInteger(-4,4),Utils.RandomInteger(-4,4)),5)
				dep.Deploy()
			end
		end)
		
	end
end