Micro AIs: add check for valid move and error message

This now checks whether the move intended to be done with ai.move is
possible and display an error message if it is not.
This commit is contained in:
mattsc 2014-02-27 10:11:41 -08:00
parent c06529aa18
commit f0506b2d31
10 changed files with 26 additions and 14 deletions

View file

@ -144,7 +144,7 @@ function ai_helper.checked_attack(ai, attacker, defender, weapon)
ai.attack(attacker, defender, weapon)
end
function ai_helper.checked_move_full(ai, unit, x, y)
function ai_helper.checked_move_core(ai, unit, x, y, move_type)
local check = ai.check_move(unit, x, y)
if (not check.ok) then
@ -153,12 +153,24 @@ function ai_helper.checked_move_full(ai, unit, x, y)
-- E_AMBUSHED = 2005
-- E_NOT_REACHED_DESTINATION = 2007
if (check.status ~= 2001) and (check.status ~= 2005) and (check.status ~= 2007) then
ai_helper.checked_action_error('ai.move_full', check.status)
ai_helper.checked_action_error(move_type, check.status)
return
end
end
ai.move_full(unit, x, y)
if (move_type == 'ai.move_full') then
ai.move_full(unit, x, y)
else
ai.move(unit, x, y)
end
end
function ai_helper.checked_move_full(ai, unit, x, y)
ai_helper.checked_move_core(ai, unit, x, y, 'ai.move_full')
end
function ai_helper.checked_move(ai, unit, x, y)
ai_helper.checked_move_core(ai, unit, x, y, 'ai.move')
end
----- General functionality and maths helper functions ------
@ -1041,7 +1053,7 @@ function ai_helper.move_unit_out_of_way(ai, unit, cfg)
if (max_rating > -9e99) then
--W.message { speaker = unit.id, message = 'Moving out of way' }
ai.move(unit, best_hex[1], best_hex[2])
ai_helper.checked_move(ai, unit, best_hex[1], best_hex[2])
end
end

View file

@ -82,7 +82,7 @@ function ca_big_animals:execution(ai, cfg)
--AH.put_labels(reach_map)
if (best_hex[1] ~= unit.x) or (best_hex[2] ~= unit.y) then
ai.move(unit, best_hex[1], best_hex[2]) -- partial move only
AH.checked_move(ai, unit, best_hex[1], best_hex[2]) -- partial move only
else -- If animal did not move, we need to stop it (also delete the goal)
ai.stopunit_moves(unit)
unit.variables.goal_x = nil

View file

@ -497,7 +497,7 @@ function ca_bottleneck_move:execution(ai, cfg, self)
--print("Moving unit:",self.data.unit.id, self.data.unit.x, self.data.unit.y, " ->", best_hex[1], best_hex[2], " -- turn:", wesnoth.current.turn)
if (self.data.unit.x ~= self.data.hex[1]) or (self.data.unit.y ~= self.data.hex[2]) then -- test needed for level-up move
ai.move(self.data.unit, self.data.hex[1], self.data.hex[2]) -- don't want full move, as this might be stepping out of the way
AH.checked_move(ai, self.data.unit, self.data.hex[1], self.data.hex[2]) -- don't want full move, as this might be stepping out of the way
end
-- If this is a move for a level-up attack, do the attack also

View file

@ -98,7 +98,7 @@ function ca_forest_animals_move:execution(ai, cfg)
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])
AH.checked_move(ai, unit, reachable_terrain[rand][1], reachable_terrain[rand][2])
end
else -- or if no close reachable terrain was found, move toward the closest
local locs = wesnoth.get_locations(wander_terrain)
@ -114,7 +114,7 @@ function ca_forest_animals_move:execution(ai, cfg)
local next_hop = AH.next_hop(unit, x, y)
--print(next_hop[1], next_hop[2])
if (unit.x ~= next_hop[1]) or (unit.y ~= next_hop[2]) then
ai.move(unit, next_hop[1], next_hop[2])
AH.checked_move(ai, unit, next_hop[1], next_hop[2])
end
end
end

View file

@ -132,7 +132,7 @@ function ca_hang_out:execution(ai, cfg, self)
end
else
-- Otherwise move unit and mark as having been used
ai.move(best_unit, best_hex[1], best_hex[2])
AH.checked_move(ai, best_unit, best_hex[1], best_hex[2])
best_unit.variables.mai_hangout_moved = true
end
end

View file

@ -86,7 +86,7 @@ function ca_herding_herd_sheep:execution(ai, cfg)
ai.stopunit_moves(best_dog)
else
--print('Dog moving to herd sheep')
ai.move(best_dog, best_hex[1], best_hex[2]) -- partial move only
AH.checked_move(ai, best_dog, best_hex[1], best_hex[2]) -- partial move only
end
end

View file

@ -43,7 +43,7 @@ function ca_herding_sheep_move:execution(ai, cfg)
if herding_area:get(x, y) or (not dogs[1]) or ((x == sheep.x) and (y == sheep.y)) then
AH.movefull_stopunit(ai, sheep, x, y)
else
ai.move(sheep, x, y)
AH.checked_move(ai, sheep, x, y)
end
end

View file

@ -113,7 +113,7 @@ function ca_hunter:execution(ai, cfg)
--AH.put_labels(reach_map)
if (best_hex[1] ~= unit.x) or (best_hex[2] ~= unit.y) then
ai.move(unit, best_hex[1], best_hex[2]) -- partial move only
AH.checked_move(ai, unit, best_hex[1], best_hex[2]) -- partial move only
else -- If hunter did not move, we need to stop it (also delete the goal)
ai.stopunit_moves(unit)
unit.variables.goal_x, unit.variables.goal_y = nil, nil

View file

@ -58,7 +58,7 @@ function ca_messenger_move:execution(ai, cfg, self)
--print(next_hop[1], next_hop[2])
if next_hop and ((next_hop[1] ~= messenger.x) or (next_hop[2] ~= messenger.y)) then
ai.move(messenger, next_hop[1], next_hop[2])
AH.checked_move(ai, messenger, next_hop[1], next_hop[2])
else
ai.stopunit_moves(messenger)
end

View file

@ -119,7 +119,7 @@ function ca_patrol:execution(ai, cfg, self)
local x, y = wesnoth.find_vacant_tile(self.data[patrol.id..'_x'], self.data[patrol.id..'_y'], patrol)
local nh = AH.next_hop(patrol, x, y)
if nh and ((nh[1] ~= patrol.x) or (nh[2] ~= patrol.y)) then
ai.move(patrol, nh[1], nh[2])
AH.checked_move(ai, patrol, nh[1], nh[2])
else
ai.stopunit_moves(patrol)
end