Skip to content

Commit c7d016b

Browse files
committed
let's not overcomplicate pythagorean theorem
1 parent f5f9655 commit c7d016b

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

LuaUI/Widgets/unit_smart_nanos.lua

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ function widget:UnitFinished(unitID, unitDefID, unitTeam)
123123
nanoTurrets[unitID] = {}
124124
nanoTurrets[unitID].unitDefID = unitDefID
125125
nanoTurrets[unitID].buildDistance = UnitDefs[nanoTurrets[unitID].unitDefID].buildDistance
126-
nanoTurrets[unitID].buildDistanceSqr = (UnitDefs[nanoTurrets[unitID].unitDefID].buildDistance * UnitDefs[nanoTurrets[unitID].unitDefID].buildDistance)
127126
nanoTurrets[unitID].damaged = false
128127
local posX,_,posZ = spGetUnitPosition(unitID)
129128
nanoTurrets[unitID].posX = posX
@@ -185,9 +184,12 @@ function widget:CommandNotify(id, params, options)
185184
end
186185
end
187186

188-
local function getDistance(x1,z1,x2,z2)
189-
local dx,dz = x1-x2,z1-z2
190-
return (dx*dx)+(dz*dz)
187+
local function posInRangeOfUnit(unitdata, x, z, additional_distance)
188+
additional_distance = additional_distance or 0
189+
local a = unitdata.posX - x
190+
local b = unitdata.posZ - z
191+
local c = unitdata.buildDistance + additional_distance
192+
return a^2 + b^2 <= c^2
191193
end
192194

193195
local function processOrderQueue()
@@ -296,7 +298,7 @@ function widget:Update(deltaTime)
296298
local targetDefID = spGetUnitDefID(prevUnit)
297299
if (targetDefID ~= nil) and UnitDefs[targetDefID].canMove then
298300
local uX, _, uZ = spGetUnitPosition(prevUnit)
299-
if (getDistance(unitDefs.posX, unitDefs.posZ, uX, uZ) > unitDefs.buildDistanceSqr) then
301+
if posInRangeOfUnit(unitDefs, uX, uZ) then
300302
commandMe = true
301303
end
302304
end
@@ -306,7 +308,7 @@ function widget:Update(deltaTime)
306308
local targetDefID = spGetUnitDefID(prevUnit)
307309
if (targetDefID ~= nil) and UnitDefs[targetDefID].canMove then
308310
local uX, _, uZ = spGetUnitPosition(prevUnit)
309-
if (getDistance(unitDefs.posX, unitDefs.posZ, uX, uZ) > unitDefs.buildDistanceSqr) then
311+
if posInRangeOfUnit(unitDefs, uX, uZ) then
310312
commandMe = true
311313
end
312314
end
@@ -372,8 +374,7 @@ function widget:Update(deltaTime)
372374
for i = #nearFeatures, 1, -1 do
373375
local fX, _, fZ = spGetFeaturePosition(featureID)
374376
local fd = spGetFeatureDefID(featureID)
375-
local radiusSqr = (FeatureDefs[fd].radius * FeatureDefs[fd].radius)
376-
if not FeatureDefs[fd].reclaimable or not (getDistance(unitDefs.posX, unitDefs.posZ, fX, fZ) < (unitDefs.buildDistanceSqr + radiusSqr)) then
377+
if not FeatureDefs[fd].reclaimable or not posInRangeOfUnit(unitDefs, fX, fZ, FeatureDefs[fd].radius) then
377378
table.remove(nearFeatures, i)
378379
end
379380
end

0 commit comments

Comments
 (0)