Skip to content

Commit

Permalink
reclaim shards: trying to prevent error "Empty interval in math.rando…
Browse files Browse the repository at this point in the history
…m()" in another way
  • Loading branch information
Ruwetuin committed Mar 19, 2024
1 parent d62ccab commit 934c599
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions luarules/gadgets/fx_reclaim_shards.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ end
local GetFeaturePosition = Spring.GetFeaturePosition
local SpawnCEG = Spring.SpawnCEG
local random = math.random
local abs = math.abs

local cegs = { "reclaimshards1", "reclaimshards2", "reclaimshards3" }
local featureList = {}
Expand All @@ -32,7 +33,7 @@ for featureDefID, featureDef in pairs(FeatureDefs) do
y = math.floor(featureDef.model.maxy * 0.66)
}
if featureList[featureDefID].minX == featureList[featureDefID].maxX or featureList[featureDefID].minZ == featureList[featureDefID].maxZ then
featureList[featureDefID] = nil -- to prevent error: "Empty interval in math.random()"
featureList[featureDefID] = nil -- to prevent error: "Empty interval in math.random()" .... DID DIDNT HELP :-(
end
end
end
Expand All @@ -51,8 +52,8 @@ function gadget:AllowFeatureBuildStep(builderID, builderTeam, featureID, feature
local params = featureList[featureDefID] or nil
if params then
local x, y, z = GetFeaturePosition(featureID)
x = x + random(params.minX, params.maxX)
z = z + random(params.minZ, params.maxZ)
x = x - abs(params.minX) + ((abs(params.minX) + abs(params.maxX)) * random())
z = z - abs(params.minZ) + ((abs(params.minZ) + abs(params.maxZ)) * random())
y = y + params.y
cegList[featureID] = { ceg = cegs[random(1, #cegs)], x = x, y = y, z = z }
end
Expand Down

2 comments on commit 934c599

@WatchTheFort
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like our problem?
https://stackoverflow.com/questions/20171224/interval-is-empty-lua-math-random-isnt-working-for-large-numbers

Second parameter is larger than integer max value, so wraps around to negative, and is now less than the first parameter.

@Ruwetuin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my previous attempt I already capped the min+max to -500 and 500 in lines 29-33

Please sign in to comment.