2013-05-05

Examples: Lua AI Code Library: Delete this section

← Older revision

Revision as of 00:14, 6 May 2013

(5 intermediate revisions not shown)

Line 7:

Line 7:

Setting up a Lua AI requires the definition of a Lua AI engine in the [side] tag and one or more stages executing the individual AI actions.  We restrict ourselves here to the example of using the default ''main_loop'' stage with its candidate actions.

Setting up a Lua AI requires the definition of a Lua AI engine in the [side] tag and one or more stages executing the individual AI actions.  We restrict ourselves here to the example of using the default ''main_loop'' stage with its candidate actions.

-

The example listed below is that of the ''Lua AI Demo'' scenario of the AI-demos add-on.  You can go there if you want to see this used in an actual campaign.

+

The example listed below is that of the ''
Simple
Lua AI Demo'' scenario of the
''
AI-demos
''
add-on.  You can go there if you want to see this used in an actual campaign.

Unlike Formula AI and the default ''cpp'' AI, Lua AI requires an engine to be defined via the [[Customizing_AI_in_Wesnoth_1.8#AI_engines_.28_via_.5Bengine.5D_tags_.29|[engine]]] tag in an [ai] tag inside the [side] tag.  Specifically, it does not work in [modify_side].  Here is an example of such an [ai] tag:

Unlike Formula AI and the default ''cpp'' AI, Lua AI requires an engine to be defined via the [[Customizing_AI_in_Wesnoth_1.8#AI_engines_.28_via_.5Bengine.5D_tags_.29|[engine]]] tag in an [ai] tag inside the [side] tag.  Specifically, it does not work in [modify_side].  Here is an example of such an [ai] tag:

Line 36:

Line 36:

[candidate_action]

[candidate_action]

engine=lua

engine=lua

-

name=
cart_move

+

name=
move_spearmen

-

id=
cart_move

+

id=
move_spearmen

evaluation="return (...):move_unittype_eval('Spearman', 100010)"

evaluation="return (...):move_unittype_eval('Spearman', 100010)"

execution="(...):move_unittype_exec('Spearman', 4, 20)"

execution="(...):move_unittype_exec('Spearman', 4, 20)"

Line 43:

Line 43:

[candidate_action]

[candidate_action]

engine=lua

engine=lua

-

name=
cart_move

+

name=
move_bowmen

-

id=
cart_move

+

id=
move_bowmen

evaluation="return (...):move_unittype_eval('Bowman', 99990)"

evaluation="return (...):move_unittype_eval('Bowman', 99990)"

execution="(...):move_unittype_exec('Bowman', 34, 20)"

execution="(...):move_unittype_exec('Bowman', 34, 20)"

Line 60:

Line 60:

The engine returns the return value of function ''init'' in file ''~add-ons/AI-demos/lua/luaai-demo_engine.lua''.  This sets up the custom AI functions.  Note the we are passing the ''ai'' table defined in the previous line to ''init()'', in order to make it available to the custom AI functions.

The engine returns the return value of function ''init'' in file ''~add-ons/AI-demos/lua/luaai-demo_engine.lua''.  This sets up the custom AI functions.  Note the we are passing the ''ai'' table defined in the previous line to ''init()'', in order to make it available to the custom AI functions.

-

The remaining custom lines are setting up the custom candidate actions at the end of the code, which call the ''move_unittype_eval()'' and ''move_unittype_exec()'' functions.
They
are defined in the ''luaai-demo_engine.lua'' file that is included in the [engine] tag
.

+

The remaining custom lines are setting up the custom candidate actions at the end of the code, which call the ''move_unittype_eval()'' and ''move_unittype_exec()'' functions.
These
are defined in the ''luaai-demo_engine.lua'' file that is included in the [engine] tag
:

return {

return {

Line 120:

Line 120:

-

This
functions
sets
up and
returns
a Lua table called ''luaai_demo'' which contains two functions ''move_unittype_eval()'' and ''move_unittype_exec()''.  These are the evaluation and execution functions that are
later
called by the custom AI candidate actions.

+

These
functions
set
up and
return
a Lua table called ''luaai_demo'' which contains two functions ''move_unittype_eval()'' and ''move_unittype_exec()''.  These are the evaluation and execution functions that are called by the custom AI candidate actions
.

+

+

Note that the AI behavior of this example is very simple and meant as a demonstration only.  It sends all units of a certain type to a certain location on the map.  Note that the unit type, the location as well as the candidate action evaluation score are parameters that are passed to the functions in the [candidate_action] tags.  Thus, it is possible to send Spearmen and Bowmen to different locations.  Furthermore, by using evaluation scores just below and above the default combat CA, we can have the bowmen attack enemy units within reach while the spearmen avoid all combat.  See what effect this has in practice by checking out the ''Simple Lua AI Demo'' scenario in the ''AI-demos'' add-on.

+

+

Notes:

+

*'''Important''': The evaluation functions '''may not''' change the game state.  Doing that will lead to OOS errors and may even crash the game.

+

*With [[Formula_AI_Howto|Formula AI]] it is possible to add candidate actions [[Formula_AI_Howto#A_Simpler_Method_for_Adding_Candidate_Actions|without specifically defining the RCA AI stage]].  That is not possible in Lua AI, the engine and ''main_loop'' stage always need to be set up
.

==== The ''data'' Variable ====

==== The ''data'' Variable ====

Line 133:

Line 139:

Note: since ''move_unittype_eval()'' and ''move_unittype_exec()'' are written as methods of object ''luaai_demo'' (through use of ':'), ''self'' refers to the object holding them: ''luaai_demo''.  One could just as well use ''luaai_demo.data.units'' here.

Note: since ''move_unittype_eval()'' and ''move_unittype_exec()'' are written as methods of object ''luaai_demo'' (through use of ':'), ''self'' refers to the object holding them: ''luaai_demo''.  One could just as well use ''luaai_demo.data.units'' here.

-

-

Notes:

-

*'''Important''': The evaluation functions '''may not''' change the game state.  Doing that will lead to OOS errors and may even crash the game.

-

*With [[Formula_AI_Howto|Formula AI]] it is possible to add candidate actions [[Formula_AI_Howto#A_Simpler_Method_for_Adding_Candidate_Actions|without specifically defining the RCA AI stage]].  That is not possible in Lua AI, the engine and ''main_loop'' stage always need to be set up.

== Testing setup ==

== Testing setup ==

+

+

'''*** This section has not yet been updated***'''

If we were using only the code described above, Wesnoth would have to be restarted (or the cache cleared with F5) every time we made a change to the code.  That is very tedious and slow for development and testing purposes.  There are much quicker options to accomplish this.  One of them is to include the code in a file and then type:

If we were using only the code described above, Wesnoth would have to be restarted (or the cache cleared with F5) every time we made a change to the code.  That is very tedious and slow for development and testing purposes.  There are much quicker options to accomplish this.  One of them is to include the code in a file and then type:

Line 378:

Line 382:

{MODIFY_AI_DELETE_CANDIDATE_ACTION 2 main_loop move_leader_to_keep}

{MODIFY_AI_DELETE_CANDIDATE_ACTION 2 main_loop move_leader_to_keep}

[/side]

[/side]

-

-

== Examples: Lua AI Code Library ==

-

-

Many examples of Lua AI code can be found found in the [[Lua AI Code Library]] page and in [http://forums.wesnoth.org/viewtopic.php?f=10&t=34976 this forum thread].

== A Few More Useful Tidbits ==

== A Few More Useful Tidbits ==

Show more