update [randon_placement]

removes use_delay: one could easily use [delay] inside [command] so
there is no reason to have that there.
changes 'radius' -> 'min_distance'
removes a debugging message
This commit is contained in:
gfgtdf 2015-12-07 22:31:56 +01:00
parent 3eadad1822
commit 9d656ad755

View file

@ -1645,31 +1645,30 @@ function wml_actions.unsynced(cfg)
end
wesnoth.wml_actions.random_placement = function(cfg)
local dist_le_radius = nil
local dist_le = nil
local parsed = helper.shallow_parsed(cfg)
local filter = helper.get_child(parsed, "filter") or {}
local command = helper.get_child(parsed, "command") or helper.wml_error("[random_placement] missing required [command] subtag")
local radius = cfg.radius or 0
local distance = cfg.min_distance or 0
local num_items = cfg.num_items or helper.wml_error("[random_placement] missing required 'num_items' attribute")
local variable = cfg.variable or helper.wml_error("[random_placement] missing required 'variable' attribute")
local use_delay = cfg.use_delay == true
local allow_less = cfg.allow_less == true
local variable_previous = utils.start_var_scope(variable)
if radius < 0 then
-- optimisation for radius = -1
dist_le_radius = function() return false end
elseif radius == 0 then
-- optimisation for radius = 0
dist_le_radius = function(x1,y1,x2,y2) return x1 == x2 and y1 == y2 end
if distance < 0 then
-- optimisation for distance = -1
dist_le = function() return false end
elseif distance == 0 then
-- optimisation for distance = 0
dist_le = function(x1,y1,x2,y2) return x1 == x2 and y1 == y2 end
else
-- optimisation: cloasure is faster than string lookups.
local math_abs = math.abs
-- same effect as helper.distance_between(x1,y1,x2,y2) <= radius but faster.
dist_le_radius = function(x1,y1,x2,y2)
-- same effect as helper.distance_between(x1,y1,x2,y2) <= distance but faster.
dist_le = function(x1,y1,x2,y2)
local d_x = math_abs(x1-x2)
if d_x > radius then
if d_x > distance then
return false
end
if d_x % 2 ~= 0 then
@ -1680,7 +1679,7 @@ wesnoth.wml_actions.random_placement = function(cfg)
end
end
local d_y = math_abs(y1-y2)
return d_x + 2*d_y <= 2*radius
return d_x + 2*d_y <= 2*distance
end
end
@ -1693,7 +1692,7 @@ wesnoth.wml_actions.random_placement = function(cfg)
for i = 1, num_items do
if size == 0 then
if allow_less then
wesnoth.message("placed only " .. i .. " items")
print("placed only " .. i .. " items")
return
else
helper.wml_error("[random_placement] failed to place items. only " .. i .. " items were placed")
@ -1705,17 +1704,13 @@ wesnoth.wml_actions.random_placement = function(cfg)
wesnoth.set_variable(variable .. ".y", point[2])
wesnoth.set_variable(variable .. ".n", i)
for j = size, 1, -1 do
if dist_le_radius(locs[j][1], locs[j][2], point[1], point[2]) then
if dist_le(locs[j][1], locs[j][2], point[1], point[2]) then
-- optimisation: swapping elements and storing size in an extra variable is faster than table.remove(locs, j)
locs[j] = locs[size]
size = size - 1
end
end
wesnoth.wml_actions.command (command)
if use_delay and (i % 10 == 0) then
--prevent freezing.
wesnoth.delay(0);
end
end
utils.end_var_scope(variable, variable_previous)