-- MOBA.levelgen
turrets = {};
cores = {};
timeToSpawnMax = 30000;
timeToSpawn = 30000;
botFile = "MoBot.bot";
botsReady = false;

function pointAverage(pTable)
	local xTotal = 0;
	local yTotal = 0;
	count = #pTable;
	
	for i,p in ipairs(pTable) do
		xTotal = xTotal + p.x;
		yTotal = yTotal + p.y;
	end
	
	local x = xTotal / count;
	local y = yTotal / count;
	
	local returnPoint = point.new(x, y);
	return returnPoint;
end

function moveTowardX(obj, x)
	local step = point.new(2,0);
	
	if (obj:getPos().x < x) then
		obj:setPos(obj:getPos() + step);
	elseif (obj:getPos().x > x) then
		obj:setPos(obj:getPos() - step);
	end
end

function spawnMinions()
	for i=1,16 do
		local team = 1;
		local xMod = 1;
		if (i%4 < 2) then
			xMod = -1;
		end
		local yMod = 1;
		if (i%4 > 0 and i%4 < 3) then
			yMod = -1;
			team = 2;
		end
		local newBot = Robot.new(point.new(2473.5*xMod,433.5*yMod), team, botFile);
		bf:addItem(newBot);
	end
end

function testMinionCodeReady()
	cores[1][1]:setGeom(cores[1][2] - point.new(300,0));
	cores[2][1]:setGeom(cores[2][2] + point.new(300,0));
	local testBot = Robot.new(point.new(0,0), 1, botFile);
	bf:addItem(testBot);
	logprint(":"..testBot:getPlayerInfo():getName()..":");
	botsReady = string.match(testBot:getPlayerInfo():getName(), "MOBOT");
	
	--If the levelgen didn't die, then the bot file exists properly.
	--Set everything back to where it should be.
	if (botsReady) then
		testBot:removeFromGame();
		cores[1][1]:setGeom(cores[1][2]);
		cores[2][1]:setGeom(cores[2][2]);
	end
end

function onShipKilled(ship)
	-- Whenever a bot dies, remove it from the game. Don't want the minions respawning.
	if(ship:getObjType() == ObjType.Robot) then
		ship:removeFromGame();
	end
end

function onShipSpawned(ship)
	local player = ship:getPlayerInfo();
	local name = player:getName();
	
	if(ship:getObjType() == ObjType.Robot) then
		if (not string.match(name, "MOBOT")) then
			logprint("Not the right kind of bot!");
			ship:removeFromGame();
		end
	end
	
end

function onTick(delta)
	if (botsReady) then
		--Adjust turret health. (this makes them a lot harder to kill)
		for id=11,30 do
			if (turrets[id] ~= nil and turrets[id][1] ~= nil) then
				before = turrets[id][2];
				hp = turrets[id][1]:getHealth();
				if (hp > before) then
					turrets[id][1]:setHealth(before);
				elseif (hp ~= before) then
					diff = (before - hp) * 0.1;
					turrets[id][1]:setHealth(before - diff);
					turrets[id][2] = turrets[id][1]:getHealth();
				end
			end
		end
		
		--set core positions (if I am sure I can spawn minions)
		for i=1,2 do
			if (cores[i] ~= nil and cores[i][1] ~= nil and bf:findObjectById(i) ~= nil) then
				direction = 0;
				endTurrets = {};
				if (i == 1) then
					direction = -1;
					endTurrets = {{11,12,13,14},{21,22,23,24}};
				else
					direction = 1;
					endTurrets = {{19,18,17,16},{29,28,27,26}};
				end
				
				cleared = 0;
				for k,lane in ipairs(endTurrets) do
					clearSoFar = true;
					for l,id in ipairs(lane) do
						if (turrets[id] ~= nil and turrets[id][1] ~= nil) then
							if (turrets[id][2] > .25) then
								clearSoFar = false;
								break;
							end
						end
					end
					if (clearSoFar) then
						cleared = cleared + 1;
					end
				end
				
				moveTowardX(cores[i][1], (cores[i][2] + point.new(150 * direction * cleared,0)).x);
			end
		end
		
		--do the timer, and check if it is time for new minions
		timeToSpawn = timeToSpawn - delta;
		if (timeToSpawn < 1) then
			timeToSpawn = timeToSpawnMax;
			spawnMinions();
		end
	else
		if (cores[1][1] ~= nil) then
			cores[1][1]:setGeom(cores[1][2] - point.new(300,0));
		end
		if (cores[2][1] ~= nil) then
			cores[2][1]:setGeom(cores[2][2] + point.new(300,0));
		end
	end
end

function main()
	for i=11,29 do
		if (i%5 ~= 0) then
			turrets[i] = {bf:findObjectById(i), bf:findObjectById(i):getHealth()};
		end
	end
	
	for i=1,2 do
		cores[i] = {bf:findObjectById(i), bf:findObjectById(i):getGeom()};
	end
	
	timeToSpawn = timeToSpawnMax;
	
	subscribe(Event.Tick);
	subscribe(Event.ShipSpawned);
	subscribe(Event.ShipKilled);
	
	for i=1,50 do
		logprint();
	end
	logprint("Levelgen loaded...");
	
	-- check if bots can be added. If they can't, we want cores in a location that the level is still playable.
	--testMinionCodeReady();
	botsReady = true;
	
	if (botsReady) then
		logprint("...and MoBot is available!");
		logprint("Ready for action!");
	else
		logprint("...but MoBot is not available.");
		logprint("reverting to a regular core level.");
	end
end