URL:
  <http://gna.org/bugs/?19086>

                 Summary: [add_ai_behavior] problems
                 Project: Battle for Wesnoth
            Submitted by: mattsc
            Submitted on: Wed 30 Nov 2011 10:46:17 PM GMT
                Category: Bug
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Artificial Intelligence
                  Status: None
                 Privacy: Public
             Assigned to: None
        Originator Email: 
             Open/Closed: Open
         Discussion Lock: Any
                 Release: 1.9.9
        Operating System: Mac OS 10.6.8

    _______________________________________________________

Details:

(This isn't really an AI issue, it's more a Lua AI tag thing, but this seems
to be closest 'item group' from the list.)

The [add_ai_behavior] tag defined in lua/wml-tags.lua requires global
variable 'ca_counter' to be defined (e.g. in a preload event).  There are
several issues with this:

* First of all, it is unnecessary, since this variable isn't used for
anything else but setting the behavior candidate action (BCA) id in this tag. 
ca_counter gets incremented every time a BCA is added to the AI, and the id is
set to 'bca-' plus the ca_counter value
* As far as I can tell, the id is completely irrelevant for anything but
removing the BCA later with [modify_ai].  However, since it is set
automatically, it is not clear which BCA has which id, unless the order in
which they were defined is always known.
* Moreover, unless 'ca_counter' is also stored in a WML variable, its value
gets lost over save/load cycles, which means that several BCAs can have
identical id's, making deleting a specific BCA impossible (this is the real
bug, everything else are "inconveniences")
* Less importantly, the definition of [add_ai_behavior] contains several
unnecessary parameters that are passed to [modify_ai]

I suggest replacing the current tag with the version below, called
[alt_add_ai_behavior] for now, so that they can be tested next to each other. 
The new tag fixes all of the above issues.  It preserves the original behavior
of [add_ai_behavior], but in addition:

* An optional key 'bca_id' can be given, which will be used as the id for the
BCA, thus making deletion possible even if several BCAs are assigned to the
same unit.
* If key 'bca_id' is not set, it assigns id 'bca-'+unit.id.  Thus, if only
one BCA is given to a unit, its id is always unique and known.
* There is no need any more to define global variable 'ca_counter' in the
preload event.


function wesnoth.wml_actions.alt_add_ai_behavior(cfg)

        local unit = wesnoth.get_units(helper.get_child(cfg, "filter"))[1]

        if not unit then
                helper.wml_error("[add_ai_behavior]: no unit specified")
        end

        local side = cfg.side
        local sticky = cfg.sticky or false
        local loop_id = cfg.loop_id or "main_loop"
        local eval = cfg.evaluation
        local exec = cfg.execution
        local bca_id = cfg.bca_id or ("bca-" .. unit.id)

        local ux = unit.x -- @note: did I get it right that coordinates in
C++ differ by 1 from thos in-game(and in Lua)?
        local uy = unit.y

        if not side then
                helper.wml_error("[add_ai_behavior]: no side attribute
given")
        end

        if not (eval and exec) then
                helper.wml_error("[add_ai_behavior]: invalid
execution/evaluation handler(s)")
        end

        local path = "stage[" .. loop_id .. "].candidate_action[" .. bca_id
.. "]" -- bca: behavior candidate action

        local conf = {
                ["side"] = side,
                ["action"] = "add",
                ["engine"] = "lua",
                ["path"] = path,

                {"candidate_action", {
                        ["id"] = bca_id,
                        ["name"] = bca_id,
                        ["engine"] = "lua",
                        ["sticky"] = sticky,
                        ["unit_x"] = ux,
                        ["unit_y"] = uy,
                        ["evaluation"] = eval,
                        ["execution"] = exec
                }}

        }
        wesnoth.wml_actions.modify_ai(conf)
        --wesnoth.message("Adding a behavior")
end







    _______________________________________________________

Reply to this item at:

  <http://gna.org/bugs/?19086>

_______________________________________________
  Message sent via/by Gna!
  http://gna.org/


_______________________________________________
Wesnoth-bugs mailing list
Wesnoth-bugs@gna.org
https://mail.gna.org/listinfo/wesnoth-bugs

Reply via email to