LuaAI:
(1) fixed some naming inconsistencies; (2) hidden some methods to the cache table, so that they don't lie around unused in the ai table
This commit is contained in:
parent
6bfaa5ef88
commit
b9f9e29a0b
4 changed files with 52 additions and 29 deletions
|
@ -4,18 +4,19 @@ return {
|
|||
init = function(ai)
|
||||
|
||||
ai.cache = {}
|
||||
ai.cache.data = {}
|
||||
|
||||
function ai.update_cache(item, getter)
|
||||
ai.cache[item] = ai[getter]()
|
||||
return ai.cache[item]
|
||||
function ai.cache.update_cache(item, getter)
|
||||
ai.cache.data[item] = ai.cache[getter]()
|
||||
return ai.cache.data[item]
|
||||
end
|
||||
|
||||
function ai.get_cached_item(item, getter, validator)
|
||||
if not ai.cache[item] or not ai[validator]() then
|
||||
local result = ai.update_cache(item, getter)
|
||||
function ai.cache.get_cached_item(item, getter, validator)
|
||||
if not ai.cache.data[item] or not ai.cache[validator]() then
|
||||
local result = ai.cache.update_cache(item, getter)
|
||||
return result
|
||||
end
|
||||
return ai.cache[item]
|
||||
return ai.cache.data[item]
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,36 +9,36 @@ return {
|
|||
cache.init(ai)
|
||||
|
||||
-- Validator section
|
||||
function ai.dst_src_validator()
|
||||
function ai.cache.dst_src_validator()
|
||||
if not ai.is_dst_src_valid() then
|
||||
ai.cache["dstsrc"] = nil
|
||||
ai.cache.data["dst_src"] = nil
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function ai.dst_src_enemy_validator()
|
||||
function ai.cache.dst_src_enemy_validator()
|
||||
if not ai.is_dst_src_enemy_valid() then
|
||||
ai.cache["enemy_dstsrc"] = nil
|
||||
ai.cache.data["enemy_dst_src"] = nil
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function ai.src_dst_validator()
|
||||
function ai.cache.src_dst_validator()
|
||||
if not ai.is_src_dst_valid() then
|
||||
ai.cache["srcdst"] = nil
|
||||
ai.cache.data["src_dst"] = nil
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function ai.src_dst_enemy_validator()
|
||||
function ai.cache.src_dst_enemy_validator()
|
||||
if not ai.is_src_dst_enemy_valid() then
|
||||
ai.cache["enemy_srcdst"] = nil
|
||||
ai.cache.data["enemy_src_dst"] = nil
|
||||
return false
|
||||
end
|
||||
|
||||
|
@ -47,22 +47,37 @@ return {
|
|||
|
||||
-- End of validator section
|
||||
|
||||
-- Hiding of get_new_* methods
|
||||
local to_hide = {
|
||||
[1] = "get_new_src_dst",
|
||||
[2] = "get_new_dst_src",
|
||||
[3] = "get_new_enemy_src_dst",
|
||||
[4] = "get_new_enemy_dst_src"
|
||||
}
|
||||
|
||||
for i, v in ipairs(to_hide) do
|
||||
ai.cache[v] = ai[v]
|
||||
ai[v] = nil
|
||||
end
|
||||
|
||||
-- End of hiding get_new_* methods
|
||||
|
||||
-- Proxy function section
|
||||
|
||||
function ai.get_dstsrc()
|
||||
return ai.get_cached_item("dstsrc", "get_new_dstsrc", "dst_src_validator")
|
||||
function ai.get_dst_src()
|
||||
return ai.cache.get_cached_item("dst_src", "get_new_dst_src", "dst_src_validator")
|
||||
end
|
||||
|
||||
function ai.get_srcdst()
|
||||
return ai.get_cached_item("srcdst", "get_new_srcdst", "dst_src_enemy_validator")
|
||||
function ai.get_src_dst()
|
||||
return ai.cache.get_cached_item("src_dst", "get_new_src_dst", "dst_src_enemy_validator")
|
||||
end
|
||||
|
||||
function ai.get_enemy_dstsrc()
|
||||
return ai.get_cached_item("enemy_dstsrc", "get_new_enemy_dstsrc", "src_dst_validator")
|
||||
function ai.get_enemy_dst_src()
|
||||
return ai.cache.get_cached_item("enemy_dst_src", "get_new_enemy_dst_src", "src_dst_validator")
|
||||
end
|
||||
|
||||
function ai.get_enemy_srcdst()
|
||||
return ai.get_cached_item("enemy_srcdst", "get_new_enemy_srcdst", "src_dst_enemy_validator")
|
||||
function ai.get_enemy_src_dst()
|
||||
return ai.cache.get_cached_item("enemy_src_dst", "get_new_enemy_src_dst", "src_dst_enemy_validator")
|
||||
end
|
||||
|
||||
-- End of proxy function section
|
||||
|
|
|
@ -263,12 +263,19 @@ ai_stdlib.init(ai)
|
|||
function my_ai:stage_hello()
|
||||
local debug_utils = wesnoth.require "~add-ons/Wesnoth_Lua_Pack/debug_utils.lua"
|
||||
|
||||
local mm = ai.get_dstsrc()
|
||||
local mm = ai.get_dst_src()
|
||||
local mm1 = ai.get_src_dst()
|
||||
local mm2 = ai.get_enemy_dst_src()
|
||||
local mm3 = ai.get_enemy_src_dst()
|
||||
|
||||
local mm1 = ai.get_dst_src()
|
||||
local mm11 = ai.get_src_dst()
|
||||
local mm21 = ai.get_enemy_dst_src()
|
||||
local mm31 = ai.get_enemy_src_dst()
|
||||
|
||||
--wesnoth.message("type " .. type(mo))
|
||||
--debug_utils.dbms(dstsrc,false,"variable",false)
|
||||
debug_utils.dbms(mm,false,"variable",false)
|
||||
--debug_utils.dbms(ai,false,"variable",false)
|
||||
--debug_utils.dbms(mm[2].movements,false,"variable",false)
|
||||
|
||||
|
||||
|
|
|
@ -778,10 +778,10 @@ lua_ai_context* lua_ai_context::create(lua_State *L, char const *code, ai::engin
|
|||
static luaL_Reg const callbacks[] = {
|
||||
{ "attack", &cfun_ai_execute_attack },
|
||||
// Move maps
|
||||
{ "get_new_dstsrc", &cfun_ai_get_dstsrc },
|
||||
{ "get_new_srcdst", &cfun_ai_get_srcdst },
|
||||
{ "get_new_enemy_dstsrc", &cfun_ai_get_enemy_dstsrc },
|
||||
{ "get_new_enemy_srcdst", &cfun_ai_get_enemy_srcdst },
|
||||
{ "get_new_dst_src", &cfun_ai_get_dstsrc },
|
||||
{ "get_new_src_dst", &cfun_ai_get_srcdst },
|
||||
{ "get_new_enemy_dst_src", &cfun_ai_get_enemy_dstsrc },
|
||||
{ "get_new_enemy_src_dst", &cfun_ai_get_enemy_srcdst },
|
||||
// End of move maps
|
||||
// Goals and targets
|
||||
{ "get_targets", &cfun_ai_get_targets },
|
||||
|
|
Loading…
Add table
Reference in a new issue