Skip to content

Commit

Permalink
Fix rabbits AI being broken by save-load
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 17, 2018
1 parent 21def91 commit ba80424
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions data/ai/micro_ais/mai-defs/animals.lua
Expand Up @@ -85,6 +85,33 @@ end
local rabbit_registry_counter = 0;
local save_rabbit_spawn, save_rabbit_despawn

local 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.erase_unit(cfg.x, cfg.y)
end

function wesnoth.custom_synced_commands.rabbit_spawn(cfg)
--TODO: maybe we only want to allow creation of unit of certain types/sides/locations?
wesnoth.put_unit({ side = wesnoth.current.side, type = cfg.rabbit_type}, cfg.x, cfg.y)
end
end

function wesnoth.hooks.on_load(cfg)
local rabbits = wml.get_child(cfg, "micro_ai_rabbits")
if rabbits then
wml.remove_child(cfg, "micro_ai_rabbits")
rabbit_registry_counter = rabbits.counter
register_rabbit_commands()
end
end

function wesnoth.hooks.on_save(cfg)
if rabbit_registry_counter > 0 then
table.insert(cfg, wml.tag.micro_ai_rabbits{counter = rabbit_registry_counter})
end
end

function wesnoth.micro_ais.forest_animals(cfg)
local optional_keys = { "rabbit_type", "rabbit_number", "rabbit_enemy_distance", "rabbit_hole_img",
"tusker_type", "tusklet_type", "deer_type", "[filter_location]"
Expand Down Expand Up @@ -113,15 +140,7 @@ function wesnoth.micro_ais.forest_animals(cfg)

rabbit_registry_counter = rabbit_registry_counter + 1

function wesnoth.custom_synced_commands.rabbit_despawn(cfg)
--TODO: maybe we only want to allow erasing of unit of certain types/sides/locations?
wesnoth.erase_unit(cfg.x, cfg.y)
end

function wesnoth.custom_synced_commands.rabbit_spawn(cfg)
--TODO: maybe we only want to allow creation of unit of certain types/sides/locations?
wesnoth.put_unit({ side = wesnoth.current.side, type = cfg.rabbit_type}, cfg.x, cfg.y)
end
register_rabbit_commands()
end

return {}, optional_keys, CA_parms
Expand Down

0 comments on commit ba80424

Please sign in to comment.