Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/wesnoth/wesnoth
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jun 19, 2014
2 parents a43fbf7 + 1491934 commit 267ebd3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 67 deletions.
2 changes: 2 additions & 0 deletions changelog
Expand Up @@ -19,6 +19,8 @@ Version 1.13.0-dev:
them for UMC and adding functionality from other versions of Wesnoth. Some
code cleanup for consistency, readability and speed, as well as fixing of
some minor and subtle bugs.
* Fix bug in Random Recruit Micro AI: the AI can now handle custom castle
terrain independent of its terrain code
* Campaigns:
* Changed all occurrences of {FLAG_VARIANT ragged} to {FLAG_VARIANT6 ragged}
* Eastern Invasion:
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 267ebd3

Please sign in to comment.