Quantcast
Channel: The Battle for Wesnoth Forums
Viewing all articles
Browse latest Browse all 2132

Lua Labs • Re: Micro AIs new question

$
0
0
Hi,
Although I've got the logic working in WML it's got a few rough edges (mainly to do with the side swapping) so I revisited the forest animals micro_ai lua and realised that on a second viewing it made more sense. :D
So, I've hacked changed the code and disabled the "flee enemies" bit, hopefully. :crossed_fingers:

However I'm not clear on what goes where nor whether I only need to shove code into a [preload] event and/or whether I need to also embed the code via wesnoth.require in _main.cfg.
There are a lot of possible combinations so I thought I'd ask here first.

FWIW here are the changes I made:
Copied data\ai\micro_ais\mai-defs\animals.lua to local lua directory as mai_forest_animals_ignore_enemies.lua.
Deleted all micro ais except forest animals, and edited that (only changed 2 lines; the micro_ai name and id).

Code:

-- Ugly hack of forest animals ai to remove avoidance of enemy unitslocal AH = wesnoth.require "ai/lua/ai_helper.lua"local MAIH = wesnoth.require("ai/micro_ais/micro_ai_helper.lua")local rabbit_registry_counter = 0;local save_rabbit_spawn, save_rabbit_despawnlocal function register_rabbit_commands()function wesnoth.custom_synced_commands.rabbit_despawn(cfg)--TODO: maybe we only want to allow erasing of unit of certain types/sides/locations?wesnoth.units.erase(cfg.x, cfg.y)endfunction wesnoth.custom_synced_commands.rabbit_spawn(cfg)--TODO: maybe we only want to allow creation of unit of certain types/sides/locations?wesnoth.units.to_map({ side = wesnoth.current.side, type = cfg.rabbit_type}, cfg.x, cfg.y)endendfunction wesnoth.persistent_tags.micro_ai_rabbits.read(cfg)rabbit_registry_counter = cfg.counter or 0register_rabbit_commands()endfunction wesnoth.persistent_tags.micro_ai_rabbits.write(add)if rabbit_registry_counter > 0 thenadd{counter = rabbit_registry_counter}endend-- Rename micro ai to "forest_animals_noavoid"-- Rename ai_id to "mai_forest_animals_noavoid"--function wesnoth.micro_ais.forest_animals_noavoid(cfg)local optional_keys = { rabbit_type = 'string', rabbit_number = 'integer',rabbit_enemy_distance = 'integer', rabbit_hole_img = 'string', tusker_type = 'string',tusklet_type = 'string', deer_type = 'string', filter_location = 'tag'}local score = cfg.ca_score or 300000local CA_parms = {ai_id = 'mai_forest_animals_noavoid',{ ca_id = "new_rabbit", location = 'ca_forest_animals_new_rabbit.lua', score = score },{ ca_id = "tusker_attack", location = 'ca_forest_animals_tusker_attack.lua', score = score - 1 },{ ca_id = "move", location = 'ca_forest_animals_move.lua', score = score - 2 },{ ca_id = "tusklet_move", location = 'ca_forest_animals_tusklet_move.lua', score = score - 3 }}-- Register custom synced commands for the rabbit AIif cfg.action == "delete" thenrabbit_registry_counter = rabbit_registry_counter - 1if rabbit_registry_counter == 0 thenwesnoth.custom_synced_commands.rabbit_spawn = save_rabbit_spawnwesnoth.custom_synced_commands.rabbit_despawn = save_rabbit_despawnendelseif rabbit_registry_counter == 0 thensave_rabbit_spawn = wesnoth.custom_synced_commands.rabbit_spawnsave_rabbit_despawn = wesnoth.custom_synced_commands.rabbit_despawnendrabbit_registry_counter = rabbit_registry_counter + 1register_rabbit_commands()endreturn {}, optional_keys, CA_parmsend

Then likewise copied data\ai\micro_ais\cas\ca_forest_animals_move.lua to local lua directory as ca_forest_animals_move_ignore_enemies.lua.

Here I just commented out the logic to store nearby enemies:

Code:

function ca_forest_animals_move:execution(cfg)    -- These animals run from any enemy    local unit = get_forest_animals(cfg)[1]    local enemies = AH.get_attackable_enemies()...    -- Behavior is different depending on whether a predator is close or not    local close_enemies = {}---- Leave close_enemies empty ------    for _,enemy in ipairs(enemies) do--        if (M.distance_between(unit.x, unit.y, enemy.x, enemy.y) <= unit.max_moves+1) then--            table.insert(close_enemies, enemy)--        end--    end    -- If no close enemies, do a random move    local wander_terrain = wml.get_child(cfg, "filter_location") or {}...
I've just done the minimum to hopefully create a working custom micro_ai, I appreciate it's ugly.
Will tidy and refine later once I am able to test it.

So, my question is what (if anything) do I need to put in _main.cfg and/or the scenario [preload event?
Do I need to use wesnoth.require anywhere?
Can I make the micro_ai globally visible across the entire [campaign]?

Code fragment examples would be really helpful; pointing me to the wiki won't as I'm too thick to understand I've read that but (unless I've missed something) am still unclear as to what goes where? :?

Thanks in advance for any advice/guidance!

Cheers!
-- Spannerbag

Statistics: Posted by Spannerbag — Today, 9:23 am



Viewing all articles
Browse latest Browse all 2132

Trending Articles