/
ca_forest_animals_tusklet_move.lua
54 lines (43 loc) · 1.67 KB
/
ca_forest_animals_tusklet_move.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local function get_tusklets(cfg)
local tusklets = AH.get_units_with_moves {
side = wesnoth.current.side,
type = cfg.tusklet_type
}
return tusklets
end
local function get_tuskers(cfg)
local tuskers = wesnoth.get_units {
side = wesnoth.current.side,
type = cfg.tusker_type
}
return tuskers
end
local ca_forest_animals_tusklet_move = {}
function ca_forest_animals_tusklet_move:evaluation(cfg)
-- Tusklets will simply move toward the closest tusker, without regard for anything else
-- Except if no tuskers are left, in which case ca_forest_animals_move takes over and does a random move
if (not cfg.tusker_type) or (not cfg.tusklet_type) then return 0 end
if (not get_tusklets(cfg)[1]) then return 0 end
if (not get_tuskers(cfg)[1]) then return 0 end
return cfg.ca_score
end
function ca_forest_animals_tusklet_move:execution(cfg)
local tusklet = get_tusklets(cfg)[1]
local tuskers = get_tuskers(cfg)
local goto_tusker, min_dist = {}, 9e99
for _,tusker in ipairs(tuskers) do
local dist = H.distance_between(tusker.x, tusker.y, tusklet.x, tusklet.y)
if (dist < min_dist) then
min_dist, goto_tusker = dist, tusker
end
end
local best_hex = AH.find_best_move(tusklet, function(x, y)
return - H.distance_between(x, y, goto_tusker.x, goto_tusker.y)
end)
AH.movefull_stopunit(ai, tusklet, best_hex)
-- Also make sure tusklets never attack
if tusklet and tusklet.valid then AH.checked_stopunit_all(ai, tusklet) end
end
return ca_forest_animals_tusklet_move