Remove function ai_helper.random()
This was using the synced RNG which is exactly the wrong thing to do within the AI evaluation/execution functions.
This commit is contained in:
parent
68c2bd8953
commit
e78462c395
11 changed files with 18 additions and 28 deletions
|
@ -164,16 +164,6 @@ function ai_helper.choose(input, value)
|
|||
return best_input, max_value, best_key
|
||||
end
|
||||
|
||||
function ai_helper.random(min, max)
|
||||
-- Use this function as Lua's 'math.random' is not replay or MP safe
|
||||
|
||||
if not max then min, max = 1, min end
|
||||
wesnoth.fire("set_variable", { name = "LUA_random", rand = string.format("%d..%d", min, max) })
|
||||
local res = wesnoth.get_variable "LUA_random"
|
||||
wesnoth.set_variable "LUA_random"
|
||||
return res
|
||||
end
|
||||
|
||||
function ai_helper.table_copy(t)
|
||||
-- Make a copy of a table (rather than just another pointer to the same table)
|
||||
local copy = {}
|
||||
|
@ -258,7 +248,7 @@ function ai_helper.LS_random_hex(set)
|
|||
-- This seems "inelegant", but I can't come up with another way without creating an extra array
|
||||
-- Return -1, -1 if set is empty
|
||||
|
||||
local r = ai_helper.random(set:size())
|
||||
local r = math.random(set:size())
|
||||
local i, xr, yr = 1, -1, -1
|
||||
set:iter( function(x, y, v)
|
||||
if (i == r) then xr, yr = x, y end
|
||||
|
@ -970,7 +960,7 @@ function ai_helper.find_best_move(units, rating_function, cfg)
|
|||
local rating = rating_function(x, y)
|
||||
|
||||
-- If cfg.random is set, add some randomness (on 0.0001 - 0.0099 level)
|
||||
if (not cfg.no_random) then rating = rating + ai_helper.random(99) / 10000. end
|
||||
if (not cfg.no_random) then rating = rating + math.random(99) / 10000. end
|
||||
-- If cfg.labels is set: insert values for label map
|
||||
if cfg.labels then reach_map:insert(x, y, rating) end
|
||||
|
||||
|
|
|
@ -34,10 +34,10 @@ function ca_big_animals:execution(ai, cfg)
|
|||
|
||||
for i,unit in ipairs(units) do
|
||||
-- Unit gets a new goal if none exist or on any move with 10% random chance
|
||||
local r = AH.random(10)
|
||||
local r = math.random(10)
|
||||
if (not unit.variables.goal_x) or (r == 1) then
|
||||
local locs = AH.get_passable_locations(cfg.filter_location or {})
|
||||
local rand = AH.random(#locs)
|
||||
local rand = math.random(#locs)
|
||||
--print(type, ': #locs', #locs, rand)
|
||||
unit.variables.goal_x, unit.variables.goal_y = locs[rand][1], locs[rand][2]
|
||||
end
|
||||
|
|
|
@ -95,7 +95,7 @@ function ca_forest_animals_move:execution(ai, cfg)
|
|||
|
||||
-- Choose one of the possible locations at random
|
||||
if reachable_terrain[1] then
|
||||
local rand = AH.random(#reachable_terrain)
|
||||
local rand = math.random(#reachable_terrain)
|
||||
-- This is not a full move, as running away might happen next
|
||||
if (unit.x ~= reachable_terrain[rand][1]) or (unit.y ~= reachable_terrain[rand][2]) then
|
||||
ai.move(unit, reachable_terrain[rand][1], reachable_terrain[rand][2])
|
||||
|
|
|
@ -38,10 +38,10 @@ function ca_forest_animals_new_rabbit:execution(ai, cfg)
|
|||
if (holes[i].image ~= cfg.rabbit_hole_img) and (holes[i].halo ~= cfg.rabbit_hole_img) then
|
||||
table.remove(holes, i)
|
||||
else
|
||||
holes[i].random = AH.random(100)
|
||||
holes[i].random = math.random(100)
|
||||
end
|
||||
else
|
||||
holes[i].random = AH.random(100)
|
||||
holes[i].random = math.random(100)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -37,9 +37,9 @@ function ca_herding_dog_move:execution(ai, cfg)
|
|||
-- Or, if dog cannot get there, prefer to be av_dist from the center
|
||||
local rating = 0
|
||||
if herding_perimeter:get(x, y) then
|
||||
rating = rating + 1000 + AH.random(99) / 100.
|
||||
rating = rating + 1000 + math.random(99) / 100.
|
||||
else
|
||||
rating = rating - math.abs(H.distance_between(x, y, cfg.herd_x, cfg.herd_y) - av_dist) + AH.random(99) / 100.
|
||||
rating = rating - math.abs(H.distance_between(x, y, cfg.herd_x, cfg.herd_y) - av_dist) + math.random(99) / 100.
|
||||
end
|
||||
|
||||
return rating
|
||||
|
|
|
@ -75,11 +75,11 @@ function ca_hunter:execution(ai, cfg)
|
|||
-- If hunting_status is not set for the unit -> default behavior -> hunting
|
||||
if (not unit.variables.hunting_status) then
|
||||
-- Unit gets a new goal if none exist or on any move with 10% random chance
|
||||
local r = AH.random(10)
|
||||
local r = math.random(10)
|
||||
if (not unit.variables.goal_x) or (r <= 1) then
|
||||
-- 'locs' includes border hexes, but that does not matter here
|
||||
locs = AH.get_passable_locations((cfg.filter_location or {}), unit)
|
||||
local rand = AH.random(#locs)
|
||||
local rand = math.random(#locs)
|
||||
--print('#locs', #locs, rand)
|
||||
unit.variables.goal_x, unit.variables.goal_y = locs[rand][1], locs[rand][2]
|
||||
end
|
||||
|
|
|
@ -60,7 +60,7 @@ function ca_lurkers:execution(ai, cfg)
|
|||
if rattack_nt_target:size() > 0 then
|
||||
|
||||
-- Choose one of the possible attack locations at random
|
||||
local rand = AH.random(1, rattack_nt_target:size())
|
||||
local rand = math.random(1, rattack_nt_target:size())
|
||||
local dst = rattack_nt_target:to_stable_pairs()
|
||||
AH.movefull_stopunit(ai, me, dst[rand])
|
||||
ai.attack(dst[rand][1], dst[rand][2], target.x, target.y)
|
||||
|
@ -81,7 +81,7 @@ function ca_lurkers:execution(ai, cfg)
|
|||
reachable_wander_terrain:inter(reach)
|
||||
|
||||
-- get one of the reachable wander terrain hexes randomly
|
||||
local rand = AH.random(1, reachable_wander_terrain:size())
|
||||
local rand = math.random(1, reachable_wander_terrain:size())
|
||||
--print(" reach_wander no allies: " .. reachable_wander_terrain:size() .. " rand #: " .. rand)
|
||||
local dst = reachable_wander_terrain:to_stable_pairs()
|
||||
if dst[1] then
|
||||
|
|
|
@ -86,7 +86,7 @@ function ca_recruit_random:evaluation(ai, cfg)
|
|||
-- The point is that this will blacklist the CA if an unaffordable recruit was
|
||||
-- chosen -> no cheaper recruits will be selected in subsequent calls
|
||||
if (n_recruits > 0) then
|
||||
local rand_prob = AH.random(1e6)
|
||||
local rand_prob = math.random(1e6)
|
||||
for typ,pr in pairs(prob) do
|
||||
if (pr.p_i <= rand_prob) and (rand_prob < pr.p_f) then
|
||||
recruit = typ
|
||||
|
|
|
@ -33,7 +33,7 @@ function swarm_move:execution(ai, cfg)
|
|||
--print('#units, #units_no_moves, #enemies', #units, #units_no_moves, #enemies)
|
||||
|
||||
-- pick a random unit and remove it from 'units'
|
||||
local rand = AH.random(#units)
|
||||
local rand = math.random(#units)
|
||||
local unit = units[rand]
|
||||
table.remove(units, rand)
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ function ca_wolves_multipacks_wander:execution(ai, cfg)
|
|||
end
|
||||
|
||||
-- Pack gets a new goal if none exist or on any move with 10% random chance
|
||||
local r = AH.random(10)
|
||||
local r = math.random(10)
|
||||
if (not goal[1]) or (r == 1) then
|
||||
local w,h,b = wesnoth.get_map_size()
|
||||
local locs = {}
|
||||
|
@ -49,7 +49,7 @@ function ca_wolves_multipacks_wander:execution(ai, cfg)
|
|||
-- We only check whether the first wolf can get there
|
||||
local unreachable = true
|
||||
while unreachable do
|
||||
local rand = AH.random(#locs)
|
||||
local rand = math.random(#locs)
|
||||
local next_hop = AH.next_hop(wolves[1], locs[rand][1], locs[rand][2])
|
||||
if next_hop then
|
||||
goal = { locs[rand][1], locs[rand][2] }
|
||||
|
|
|
@ -37,7 +37,7 @@ function ca_wolves_wander:execution(ai, cfg)
|
|||
|
||||
local max_rating, goal_hex = -9e99, {}
|
||||
reach_map:iter( function (x, y, v)
|
||||
local rating = v + AH.random(99)/100.
|
||||
local rating = v + math.random(99)/100.
|
||||
if avoid:get(x, y) then rating = rating - 1000 end
|
||||
|
||||
if (rating > max_rating) then
|
||||
|
|
Loading…
Add table
Reference in a new issue