/
lua_object.cpp
76 lines (65 loc) · 2.01 KB
/
lua_object.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Copyright (C) 2011 - 2016 by Dmitry Kovalenko <nephro.wes@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Lua object(code) wrapper implementation
*/
#include "ai/lua/lua_object.hpp"
#include "ai/lua/engine_lua.hpp"
#include "ai/default/aspect_attacks.hpp"
#include "scripting/lua_common.hpp"
#include "resources.hpp"
#include "lua/lauxlib.h"
namespace ai {
lua_object_base::lua_object_base()
{
// empty
}
// MSVC fails to compile without this line
template class lua_object<aspect_attacks_lua_filter>;
template <>
std::shared_ptr<aspect_attacks_lua_filter> lua_object<aspect_attacks_lua_filter>::to_type(lua_State *L, int n)
{
std::shared_ptr<aspect_attacks_lua_filter> att(new aspect_attacks_lua_filter);
att->lua = nullptr;
att->ref_own_ = att->ref_enemy_ = -1;
if(!lua_istable(L, n)) {
return att;
}
lua_getfield(L, n, "own");
if(lua_istable(L, -1)) {
config cfg;
vconfig vcfg(cfg, true);
if(luaW_tovconfig(L, -1, vcfg)) {
att->filter_own_.reset(new unit_filter(vcfg, resources::filter_con));
}
} else if(lua_isfunction(L, -1)) {
att->lua = L;
att->ref_own_ = luaL_ref(L, LUA_REGISTRYINDEX);
assert(att->ref_own_ != -1);
}
lua_getfield(L, n, "enemy");
if(lua_istable(L, -1)) {
config cfg;
vconfig vcfg(cfg, true);
if(luaW_tovconfig(L, -1, vcfg)) {
att->filter_enemy_.reset(new unit_filter(vcfg, resources::filter_con));
}
} else if(lua_isfunction(L, -1)) {
att->lua = L;
att->ref_enemy_ = luaL_ref(L, LUA_REGISTRYINDEX);
assert(att->ref_enemy_ != -1);
}
lua_pop(L, 2);
return att;
}
} //end of namespace ai