-- powerline.levelgen
powerlines = {};
gates = {};
lineN = 0;

function moveTo(item, p)
	local speed = item:getVel();
	local p2 = item:getPos();
	
	item:setVel((speed*.8) + (p-p2)*30);
end

function onTick(delta)
	for rItem,rData in pairs(gates) do
		moveTo(rItem, rData[2]);
	end

	players = bf:getGameInfo():getPlayers();
	--For each line, see if a player is powering it. Set that respective bit.
	for i=1,lineN do
		if (powerlines[i] ~= nil and (powerlines[i])[2] ~= nil) then
			pLine = powerlines[i];
			sourcePoint = pLine[1];
			
			pLine[4] = false;
			for k,player in ipairs(players) do
				if (point.distSquared(sourcePoint, player:getShip():getGeom()) < 900) then
					pLine[4] = true;
					break;
				end
			end
			--if it sill isn't powered, check if some NOT gates are powering it.
			if (pLine[4] == false) then
				for rItem,rData in pairs(gates) do
					rPoint = rData[2];
					line = rData[1];
					if (line:getTeamIndex() == -2 and line ~= pLine and point.distSquared(sourcePoint, rPoint) < 900) then
						pLine[4] = true;
						break;
					end
				end
			end
			
			--power/unpower the end item.
			if (pLine[4]) then
				if (pLine[5] == false) then
					pLine[2]:setHealth(1);
				end
				pLine[3]:setTeam(-1);
			else
				if (pLine[5] == false) then
					pLine[2]:setHealth(0);
				end
				pLine[3]:setTeam(-2);
			end
		end
	end
end

function main()
	--This is only to clear the console of previous messages. Doesn't do anything.
	for i=1,50 do
		logprint("");
	end

	results = bf:findAllObjects(ObjType.Line);
	for i,item in ipairs(results) do
		if (item:getTeamIndex() == -2) then
			geom = item:getGeom();
			sourcePoint = geom[1];
			reversed = false;
			
			endPoint = sourcePoint;
			for j=1,32 do
				if (geom[j] == nil) then
					break;
				else
					endPoint = geom[j];
				end
			end
			
			--Is there anything on the end point?
			powerable = bf:findAllObjects(ObjType.Turret, ObjType.ForceFieldProjector);
			--logprint(table.getn(powerable));
			poweredItem = nil;
			for j,pItem in ipairs(powerable) do
				if (point.distSquared(endPoint, pItem:getGeom()) <= 900) then
					poweredItem = pItem;
					break;
				end
			end
			
			--If poweredItem is still nil, then see if there is a not gate here...
			if (poweredItem == nil) then
				resources = bf:findAllObjects(ObjType.ResourceItem);
				for j,rItem in ipairs(resources)  do
					if (gates[rItem] == nil and point.distSquared(endPoint, rItem:getGeom()) <= 900) then
						--Now we have to look though other line items to see if that matches...
						--If so, then we have a NOT gate.
						for k,lItem in ipairs(results) do
							if (lItem:getTeamIndex() == -2 and k ~= i) then
								if (point.distSquared(endPoint, lItem:getGeom()[1]) <= 900) then
									reversed = true;
									poweredItem = rItem;
									gates[rItem] = {item,endPoint};
									break;
								end
							end
						end
						break;
					end
				end
			end
			
			--save {source point, what is powered, the line itself, if there is power, if it is reversed}
			powerlines[i] = {sourcePoint, poweredItem, item, false, reversed};
			lineN = lineN + 1;
		end
	end
	
	subscribe(Event.Tick);	
end