Skip to content

Commit

Permalink
Random Recruit Micro AI: fix bug with custom castle terrain
Browse files Browse the repository at this point in the history
Previously, only terrain with C or K in the terrain code passed the
SLF.  Now we check whether the hex is a castle instead, independent of
the terrain code.
  • Loading branch information
mattsc committed Jun 19, 2014
1 parent 980594e commit 8969c7a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
2 changes: 2 additions & 0 deletions changelog
Expand Up @@ -65,6 +65,8 @@ Version 1.11.15+dev:
* Fix bug #22134: Campaign prefix not used in mp campaign saves
* Made the error messages sent to stderr when the core data dir is
incorrectly set more helpful.
* Fix bug in Random Recruit Micro AI: the AI can now handle custom castle
terrain independent of its terrain code

Version 1.11.15:
* Graphics:
Expand Down
48 changes: 34 additions & 14 deletions data/ai/micro_ais/cas/ca_recruit_random.lua
@@ -1,4 +1,6 @@
local H = wesnoth.require "lua/helper.lua"
local AH = wesnoth.require("ai/lua/ai_helper.lua")
local LS = wesnoth.dofile "lua/location_set.lua"

local recruit_type

Expand All @@ -13,26 +15,44 @@ function ca_recruit_random:evaluation(ai, cfg)
return 0
end

-- Check if there is space left for recruiting
-- Find all connected castle hexes
local castle_map = LS.of_pairs({ { leader.x, leader.y } })
local width, height, border = wesnoth.get_map_size()
local castle = {
locs = wesnoth.get_locations {
x = "1-"..width, y = "1-"..height,
{ "and", {
x = leader.x, y = leader.y, radius = 200,
{ "filter_radius", { terrain = 'C*,K*,C*^*,K*^*,*^K*,*^C*' } }
} }
}
}
local new_castle_hex_found = true

while new_castle_hex_found do
new_castle_hex_found = false
local new_hexes = {}

castle_map:iter(function(x, y)
for xa,ya in H.adjacent_tiles(x, y) do
if (not castle_map:get(xa, ya))
and (xa >= 1) and (xa <= width)
and (ya >= 1) and (ya <= height)
then
local is_castle = wesnoth.get_terrain_info(wesnoth.get_terrain(xa, ya)).castle

if is_castle then
table.insert(new_hexes, { xa, ya })
new_castle_hex_found = true
end
end
end
end)

for _,hex in ipairs(new_hexes) do
castle_map:insert(hex[1], hex[2])
end
end

-- Check if there is space left for recruiting
local no_space = true
for _,loc in ipairs(castle.locs) do
local unit = wesnoth.get_unit(loc[1], loc[2])
castle_map:iter(function(x, y)
local unit = wesnoth.get_unit(x, y)
if (not unit) then
no_space = false
break
end
end
end)
if no_space then return 0 end

-- Set up the probability array
Expand Down

0 comments on commit 8969c7a

Please sign in to comment.