ZeroK-RTS / Zero-K

Open source RTS game running on the Spring/Recoil engine
https://zero-k.info
GNU General Public License v2.0
690 stars 207 forks source link

Extract spirals from start unit spawner #5197

Open sprunk opened 8 months ago

sprunk commented 8 months ago

5089 added a spiral generator into the start unit spawner gadget. It sounds like generally useful math and doesn't really belong there. Extract it to a Spring.Utilities function, perhaps one that returns a closure that successively yields the spiral when called repeatedly.

sprunk commented 6 months ago

Closure test cases. for direction, "w", "n", "e" and "s" stand for west, north, east and south.

local spiralWithExtraParameters = Spring.Utilities.GetSpiralGenerator(12, 34, { step = 3, startingDirection = "n",  clockwise = false })
local x1, z1 = spiralWithExtraParameters()
local x2, z2 = spiralWithExtraParameters()
local x3, z3 = spiralWithExtraParameters()

if  x1 == 12 and z1 == 34
and x2 == 12 and z2 == 31
and x3 ==  9 and z3 == 31 then
  Spring.Echo("OK")
else
  error("fail")
end
local defaultSpiral = Spring.Utilities.GetSpiralGenerator()
local spiralWithDefaultsExplicitlySpecified = Spring.Utilities.GetSpiralGenerator(0, 0, { step = 1, startingDirection = 0,  clockwise = false })

for i = 1, 20 do
  local xD, zD = defaultSpiral()
  local xE, zE = spiralWithDefaultsExplicitlySpecified()
  if xD ~= xE or zD ~= zE then
    error("fail")
  end
end
Spring.Echo("OK 2")
local angledSpiral = Spring.Utilities.GetSpiralGenerator(0, 0, { step = 2, startingDirection = math.tau / 6, clockwise = true })

local x1, z1 = angledSpiral()
local x2, z2 = angledSpiral()
if x1 ~=                1 or z1 ~= math.sqrt(3)
or x2 ~= math.sqrt(3) + 1 or z2 ~= math.sqrt(3) - 1 then
  error("fail")
end
Spring.Echo("OK 3")