mirror of
https://github.com/wesnoth/wesnoth
synced 2025-04-27 18:18:35 +00:00
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:
parent
c06529aa18
commit
f0506b2d31
@ -144,7 +144,7 @@ function ai_helper.checked_attack(ai, attacker, defender, weapon)
|
|||||||
ai.attack(attacker, defender, weapon)
|
ai.attack(attacker, defender, weapon)
|
||||||
end
|
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)
|
local check = ai.check_move(unit, x, y)
|
||||||
|
|
||||||
if (not check.ok) then
|
if (not check.ok) then
|
||||||
@ -153,12 +153,24 @@ function ai_helper.checked_move_full(ai, unit, x, y)
|
|||||||
-- E_AMBUSHED = 2005
|
-- E_AMBUSHED = 2005
|
||||||
-- E_NOT_REACHED_DESTINATION = 2007
|
-- E_NOT_REACHED_DESTINATION = 2007
|
||||||
if (check.status ~= 2001) and (check.status ~= 2005) and (check.status ~= 2007) then
|
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
|
return
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
----- General functionality and maths helper functions ------
|
----- 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
|
if (max_rating > -9e99) then
|
||||||
--W.message { speaker = unit.id, message = 'Moving out of way' }
|
--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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ function ca_big_animals:execution(ai, cfg)
|
|||||||
--AH.put_labels(reach_map)
|
--AH.put_labels(reach_map)
|
||||||
|
|
||||||
if (best_hex[1] ~= unit.x) or (best_hex[2] ~= unit.y) then
|
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)
|
else -- If animal did not move, we need to stop it (also delete the goal)
|
||||||
ai.stopunit_moves(unit)
|
ai.stopunit_moves(unit)
|
||||||
unit.variables.goal_x = nil
|
unit.variables.goal_x = nil
|
||||||
|
@ -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)
|
--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
|
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
|
end
|
||||||
|
|
||||||
-- If this is a move for a level-up attack, do the attack also
|
-- If this is a move for a level-up attack, do the attack also
|
||||||
|
@ -98,7 +98,7 @@ function ca_forest_animals_move:execution(ai, cfg)
|
|||||||
local rand = math.random(#reachable_terrain)
|
local rand = math.random(#reachable_terrain)
|
||||||
-- This is not a full move, as running away might happen next
|
-- 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
|
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
|
end
|
||||||
else -- or if no close reachable terrain was found, move toward the closest
|
else -- or if no close reachable terrain was found, move toward the closest
|
||||||
local locs = wesnoth.get_locations(wander_terrain)
|
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)
|
local next_hop = AH.next_hop(unit, x, y)
|
||||||
--print(next_hop[1], next_hop[2])
|
--print(next_hop[1], next_hop[2])
|
||||||
if (unit.x ~= next_hop[1]) or (unit.y ~= next_hop[2]) then
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -132,7 +132,7 @@ function ca_hang_out:execution(ai, cfg, self)
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- Otherwise move unit and mark as having been used
|
-- 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
|
best_unit.variables.mai_hangout_moved = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -86,7 +86,7 @@ function ca_herding_herd_sheep:execution(ai, cfg)
|
|||||||
ai.stopunit_moves(best_dog)
|
ai.stopunit_moves(best_dog)
|
||||||
else
|
else
|
||||||
--print('Dog moving to herd sheep')
|
--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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -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
|
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)
|
AH.movefull_stopunit(ai, sheep, x, y)
|
||||||
else
|
else
|
||||||
ai.move(sheep, x, y)
|
AH.checked_move(ai, sheep, x, y)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ function ca_hunter:execution(ai, cfg)
|
|||||||
--AH.put_labels(reach_map)
|
--AH.put_labels(reach_map)
|
||||||
|
|
||||||
if (best_hex[1] ~= unit.x) or (best_hex[2] ~= unit.y) then
|
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)
|
else -- If hunter did not move, we need to stop it (also delete the goal)
|
||||||
ai.stopunit_moves(unit)
|
ai.stopunit_moves(unit)
|
||||||
unit.variables.goal_x, unit.variables.goal_y = nil, nil
|
unit.variables.goal_x, unit.variables.goal_y = nil, nil
|
||||||
|
@ -58,7 +58,7 @@ function ca_messenger_move:execution(ai, cfg, self)
|
|||||||
--print(next_hop[1], next_hop[2])
|
--print(next_hop[1], next_hop[2])
|
||||||
|
|
||||||
if next_hop and ((next_hop[1] ~= messenger.x) or (next_hop[2] ~= messenger.y)) then
|
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
|
else
|
||||||
ai.stopunit_moves(messenger)
|
ai.stopunit_moves(messenger)
|
||||||
end
|
end
|
||||||
|
@ -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 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)
|
local nh = AH.next_hop(patrol, x, y)
|
||||||
if nh and ((nh[1] ~= patrol.x) or (nh[2] ~= patrol.y)) then
|
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
|
else
|
||||||
ai.stopunit_moves(patrol)
|
ai.stopunit_moves(patrol)
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user