At Defcon 5, I gather all city information:
Code: Select all
local allCities = GetCityIDs()
local us = GetOwnTeamID()
for i, city in ipairs(allCities) do
if city:GetTeamID() == us then
table.insert(myCities, city)
else
table.insert(enemyCities, city)
end
end
Now that I know where cities are and how many there are, I begin to place ground units The command IsValidLocation() returns true if it's Land (ground only, sea for naval), and if it's not on the exact coordinates of a city. IsValidLocation should be modified (I think) to return false if the coordinates are within a radius of 1.8 of any given city. Nevertheless, I need to accomplish this some kind of way so I don't place too close to a city and take double damage (strikes to one cause damage to both).
I've tried various forms of : (to no avail)
Code: Select all
locations = {}
function TestTest()
StartLongTask(function()
local x, y, d = -74.000000, 40.750000, 1.8 -- New York
-- or use:
--local x = math.random(-110, -91)
--local y = math.random(47, 51)
--local d = 1.8
WhiteboardDrawCircle(x, y, d)
counter = 1
repeat
local x1 = x + math.random(-3.6, 3.6)
local y1 = y + math.random(-3.6, 3.6)
local dist = GetRealDistance(x, y, x1, y1)
for i, city in ipairs(myCities) do
if GetRealDistance(x, y, city:GetLongitude(),city:GetLatitude()) > 1.8 then
else
table.insert(locations, city)
end
end
-- try to use zero as good location
--(didn't bump against any cities), > zero as too close
if # locations > 0 then
WhiteboardDrawSquare(x1, y1, 0.5)
else
WhiteboardDrawSquare(x1, y1, 0.1)
end
for i, city in ipairs(locations) do -- empty the table ?
city = nil
end
YieldLongTask()
counter = counter + 1
until counter == 100
end)
end
function GetRealDistance(x1,y1,x2,y2)
dist = math.sqrt((x2-x1)^2 + (y2-y1)^2)
return dist
end
This code doesn't throw any errors, but it doesn't do what I'm expecting it to do, which is see if my original x, y are within a radius of 1.8 of any city.
A simple test version of it works really well, but only looks at one predefined location. In Defcon, the cities might change from game to game.
Working code:
Code: Select all
function TestTest()
StartLongTask(function()
local x, y, d = -74.000000, 40.750000, 1.8 -- New York
-- or use:
--local x = math.random(-110, -91)
--local y = math.random(47, 51)
--local d = 1.8
WhiteboardDrawCircle(x, y, d)
counter = 1
repeat
local x1 = x + math.random(-3.6, 3.6)
local y1 = y + math.random(-3.6, 3.6)
local dist = GetRealDistance(x, y, x1, y1)
if dist > 1.8 then
WhiteboardDrawSquare(x1, y1, 0.5)
else
WhiteboardDrawSquare(x1, y1, 0.1)
end
YieldLongTask()
counter = counter + 1
until counter == 100
end)
end
function GetRealDistance(x1,y1,x2,y2)
dist = math.sqrt((x2-x1)^2 + (y2-y1)^2)
return dist
end