Commit graph

75 commits

Author SHA1 Message Date
mattsc
23fc509256 Lua AIs: fix some incorrectly used variables
Fixes #5586
2021-03-14 08:59:42 -07:00
mattsc
0421a524f9 ai_helper.get_closest_location: bug fix for border hexes
If a unit is passed as the last argument to this function, the returned hex is supposed to be passable for that unit. Hexes on the map border need to be excluded in this case.

This specifically also fixes a bug in the Messenger Escort Micro AI. Previously, waypoints right at the map border could sometimes lead to the AI showing an on-screen error message and becoming inactive.
2020-09-26 14:06:17 -07:00
mattsc
2436195012 Lua AIs: replace print() by std_print() 2019-10-05 20:05:12 -07:00
mattsc
da080dd94c Experimental AI: fix guardians being used for village actions 2019-09-18 19:41:12 -07:00
mattsc
ac2e90faca ai_helper: get_unit functions should only return live units
Live beiing defined as in get_live_units(), that is, non-petrified units
2019-09-18 16:27:18 -07:00
mattsc
4450da2b80 ai_helper.move_unit_out_of_way: prevent potential for divide-by-zero
dx = dy = 0 is a perfectly acceptable choice of values when there is no preferred direction.
2018-10-21 19:25:46 -07:00
mattsc
fb364cf2b7 AI helper: improve efficiency of get_unit functions
The old method is very slightly faster in some circumstances (e.g. for
simple filters and when all units have moves/attacks/etc. left), but we
are talking fractions of micro seconds. By contrast, depending on the
filter used, the new method can save large amounts of evaluation time
once some units have move or attacked.
2018-06-06 20:23:56 -07:00
mattsc
a5bd46e172 Lua code: replace deprecated helper.get_child() calls 2018-05-12 17:41:39 -07:00
mattsc
3b2077fdc3 Fast Micro AI ai_helper functions: fix rarely occurring bug
When the AI moves a unit out of the way for another unit’s move, it is
occasionally possible for the former unit to use up all its movement
points (e.g. if it has few MP or on narrow crowded maps). If that unit
is on the list to be moved later during the same sequence, this caused
ai_helper.robust_move_and_attack() to return nil, which then made the
Fast Micro AI produce an error message.
2017-11-08 22:33:24 -08:00
Celtic Minstrel
6efc5ae090 Replace helper.distance_between -> wesnoth.map.distance_between (fixes #1686) 2017-05-11 04:14:03 -04:00
Celtic Minstrel
0539a68eb4 Migrate ai_helper.LS_random_hex to location_set:random 2017-05-10 17:40:21 -04:00
Celtic Minstrel
f6c04e7dd1 Add functional.lua which implements higher-order functions
This covers all the main higher-order functions included in WFL, except zip.
The two already implemented in ai_helper have been replaced with redirection stubs.
2017-05-10 17:40:20 -04:00
Celtic Minstrel
9b635994e1 Add location_set.(of|to)_triples (migrated from ai_helper) 2017-05-10 17:40:14 -04:00
Celtic Minstrel
37f1c490a5 Lua: Use the second argument to error() where appropriate 2017-05-10 17:40:13 -04:00
Celtic Minstrel
8d4cf3cf62 Shorten requires where possible 2017-05-03 02:42:25 -04:00
mattsc
36b2b4e9a7 AI helper functions: consistently ignore shroud when path finding
This is the default Wesnoth AI behavior. It is done in a way that still
ignores hidden units.
2016-12-09 18:57:18 -08:00
mattsc
8d76ad32b0 ai_helper: new function find_path_with_shroud() 2016-12-09 18:53:40 -08:00
mattsc
fa48c9c2ac ai_helper: compare unit proxies directly, not by coordinate
This was not possible when this code was first written, but it is valid
syntax now.
2016-10-22 15:14:26 -07:00
mattsc
acecd77bb4 Lua AIs: use new syntax for iterating over attacks
This is both simpler and faster than the old syntax.
2016-10-22 13:54:32 -07:00
mattsc
1cef83b08a ai_helper: fix indentation 2016-10-21 21:09:54 -07:00
mattsc
cc6679159b ai_helper.robust_move_and_attack: return action result to calling function
This function is supposed to be able to deal with all kinds of rare and
weird events without throwing errors, but it should still return a
status table describing what happened. The vast majority of situations
will simply return the move_result tables. For all the strange stuff
that could also happen due to WML events etc., we set up dummy tables
with the same structure and (somewhat) descriptive error messages
instead.
2016-10-21 20:57:16 -07:00
mattsc
bd1b45e814 ai_helper checked actions: move error string into call to error()
… rather than making it a separate message.
2016-10-21 17:25:11 -07:00
mattsc
74792845d7 ai_helper.movefull_stopunit: return action result to calling function 2016-10-21 07:26:26 -07:00
mattsc
13d2cdfc29 ai_helper checked actions: return action result to calling function 2016-10-21 07:22:45 -07:00
mattsc
204ccb0584 ai_helper: move checks for incomplete/empty moves to functions 2016-10-21 07:04:02 -07:00
mattsc
022d2cd725 ai_helper checked actions: some modifications to error messages 2016-10-21 06:46:41 -07:00
Gregory A Lundberg
fc6774289e Upgrade deprecated Lua
loadstring was removed, load does the same thing.
atan2 was remvoed, atan works instead
2016-10-17 10:34:43 -05:00
mattsc
5770fa74f0 ai_helper: check variable type when viewing_side is required parameter
This is done because the code will not complain but silently produce
non-sensical results if, for example, the filter is passed as the first
argument to get_visible_units().
2016-10-12 07:42:33 -07:00
mattsc
c91a65dde4 ai_helper check unit functions: also check if unit exists 2016-10-10 07:43:38 -07:00
mattsc
f28d4336bf ai_helper.move_unit_out_of_way: normalize dx/dy here
Instead of making it a requirement that they should be normalized by
the calling function.
2016-10-09 14:25:15 -07:00
mattsc
1a5e9051a8 ai_helper: correctly and consistently deal with invisible units
Previously, some functions saw invisible units, while others did not.
Moreover, this behavior could be triggered on and off for some
functions, but not for others, and the parameter to do so was not the
same in all cases.

Now, the default setting for all ai_helper functions is to ignore
invisible units and this can be turned on and off with the same
parameter, viewing_side, with the same syntax and meaning as for the
built-in functions wesnoth.find_path() and wesnoth.find_reach(). This
means hat it can be passed directly between those functions and all the
ai_helper functions.
2016-10-09 08:51:13 -07:00
mattsc
8ad055e77f ai_helper: remove double spaces after punctuation 2016-10-05 19:30:47 -07:00
mattsc
e65a460342 ai_helper: add E_FAILED_TELEPORT to list of non-fatal move errors 2016-10-05 19:28:52 -07:00
mattsc
75817d3ba4 ai_helper: new function robust_move_and_attack() 2016-10-05 19:19:25 -07:00
mattsc
e38dd8ff16 ai_helper: new utility functions to find attackable enemies
Attackable enemies are defined as visible (can’t attack it if I can’t
see it) and not petrified.

This is in preparation for making the Micro AIs deal correctly with
hidden and petrified enemies.
2016-10-04 17:17:16 -07:00
mattsc
b00e6d5d96 ai_helper.LS_to_triples: fix function name
For consistency with LS_of_triples.
2016-10-04 17:17:16 -07:00
mattsc
371b9b67ec Lua AI helper functions: remove attacks/moves on failed action
If this is not done, there are some rare cases in which a Lua AI might
get caught in an infinite loop by trying the same failed action over
and over again.
2016-10-02 09:13:54 -07:00
mattsc
c03bd5411f Various Lua AI code: use simpler and faster new syntax
No change in behavior.
2016-09-04 18:54:26 -07:00
mattsc
ada80cfd34 Micro AIs: only display on-screen error messages when in debug mode 2014-11-01 07:45:15 -07:00
mattsc
156726a6a4 ai_helper.move_unit_out_of_way: fix direction rating
This is supposed to be a minor rating, secondary to the main rating.
2014-06-16 08:08:01 -07:00
mattsc
faeaf5e47c ai_helper.get_attacks: improve check for units in the way
Check whether the unit in the way has an unoccupied hex to move to.
Previously, it was only checked whether its reach was >1, which
sometimes can include only hexes occupied by units on its own side,
making the actual move impossible.
2014-06-15 14:22:18 -07:00
mattsc
03c1b97802 next_hop(): bug fix for ignoring units when ignore_units=true is set
A bug in the previous version let to them not being ignored when  the
were on the last hex of the next hop.
2014-05-02 19:29:37 -07:00
mattsc
f0c7ddcab1 ai_helper: make AI move error messages more descriptive 2014-04-30 18:59:25 -07:00
mattsc
bbffa5e88a ai_helper: remove a debug message
It’s served its purpose and is not needed any more.
2014-04-26 18:26:45 -07:00
mattsc
e99423bad9 Lua AIs: do not use empty tables as filters
It’s faster not to provide any argument to wesnoth.get_units() if all
units are to be found.
2014-04-26 07:18:23 -07:00
mattsc
8e9780d606 ai_helper.get_live_units: don't use filter_wml
It’s slow, the new method is between 10 and 20 times faster.
2014-04-26 06:46:32 -07:00
mattsc
9701a724a8 ai_helper.lua: avoid using table.remove
It’s slow. The inverse logic using table.insert is much faster,
especially for large tables.
2014-04-17 21:03:38 -07:00
mattsc
19eff08fd7 Fix a typo 2014-04-17 21:03:38 -07:00
mattsc
f6cdd85182 AI helper functions: code cleanup, first pass
These function libraries are still work in progress and change
frequently, so the code cleanup is mostly just to make sure the syntax
is consistent with that used in the Micro AIs.  Changes or improvements
to the algorithms might be made at a later time.
2014-04-17 21:03:38 -07:00
mattsc
98509aa2bf Fix a typo
I had actually introduced this typo intentionally for testing purposes
and then forgotten to remove it.  :-P
2014-04-10 10:39:12 -07:00