The Battle for Wesnoth  1.15.12+dev
lua_object.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2018 by Dmitry Kovalenko <nephro.wes@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Lua object(code) wrapper implementation
18  */
19 
20 #include "ai/lua/lua_object.hpp"
21 #include "ai/lua/engine_lua.hpp"
23 #include "scripting/lua_common.hpp"
24 #include "resources.hpp"
25 
26 #include "lua/lauxlib.h"
27 
28 namespace ai {
29 
31  {
32  // empty
33  }
34 
35  // MSVC fails to compile without this line
37 
38  template <>
39  std::shared_ptr<aspect_attacks_lua_filter> lua_object<aspect_attacks_lua_filter>::to_type(lua_State *L, int n)
40  {
41  std::shared_ptr<aspect_attacks_lua_filter> att(new aspect_attacks_lua_filter);
42  att->lua = nullptr;
43  att->ref_own_ = att->ref_enemy_ = -1;
44  if(!lua_istable(L, n)) {
45  return att;
46  }
47  lua_getfield(L, n, "own");
48  if(lua_istable(L, -1)) {
49  vconfig vcfg(config(), true);
50  if(luaW_tovconfig(L, -1, vcfg)) {
51  att->filter_own_.reset(new unit_filter(vcfg));
52  }
53  } else if(lua_isfunction(L, -1)) {
54  att->lua = L;
55  att->ref_own_ = luaL_ref(L, LUA_REGISTRYINDEX);
56  assert(att->ref_own_ != -1);
57  }
58  lua_getfield(L, n, "enemy");
59  if(lua_istable(L, -1)) {
60  vconfig vcfg(config(), true);
61  if(luaW_tovconfig(L, -1, vcfg)) {
62  att->filter_enemy_.reset(new unit_filter(vcfg));
63  }
64  } else if(lua_isfunction(L, -1)) {
65  att->lua = L;
66  att->ref_enemy_ = luaL_ref(L, LUA_REGISTRYINDEX);
67  assert(att->ref_enemy_ != -1);
68  }
69  lua_pop(L, 2);
70  return att;
71  }
72 
73 } //end of namespace ai
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
Gets an optional vconfig from either a table or a userdata.
Definition: lua_common.cpp:839
LUALIB_API int luaL_ref(lua_State *L, int t)
Definition: lauxlib.cpp:646
Lua object(value) wrapper implementation.
#define lua_pop(L, n)
Definition: lua.h:364
std::shared_ptr< T > to_type(lua_State *, int)
Definition: lua_object.hpp:83
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
#define lua_isfunction(L, n)
Definition: lua.h:372
Aspect: attacks.
#define LUA_REGISTRYINDEX
Definition: lua.h:44
#define lua_istable(L, n)
Definition: lua.h:373
A variable-expanding proxy for the config class.
Definition: variable.hpp:44
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
LUA_API int lua_getfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:655
LUA AI Support engine - creating specific ai components from config.
static map_location::DIRECTION n