The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_object.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Dmitry Kovalenko <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 
21 #include "ai/lua/lua_object.hpp"
22 #include "ai/lua/engine_lua.hpp"
24 #include "scripting/lua_types.hpp"
25 #include "scripting/lua_common.hpp"
26 #include "resources.hpp"
27 
28 #include <boost/smart_ptr/make_shared_object.hpp>
29 #include "lua/lauxlib.h"
30 
31 namespace ai {
32 
34  {
35  // empty
36  }
37 
38  // MSVC fails to compile without this line
40 
41  template <>
43  {
45  att->lua = nullptr;
46  att->ref_own_ = att->ref_enemy_ = -1;
47  if(!lua_istable(L, n)) {
48  return att;
49  }
50  lua_getfield(L, n, "own");
51  if(lua_istable(L, -1)) {
52  config cfg;
53  vconfig vcfg(cfg, true);
54  if(luaW_tovconfig(L, -1, vcfg)) {
55  att->filter_own_.reset(new unit_filter(vcfg, resources::filter_con));
56  }
57  } else if(lua_isfunction(L, -1)) {
58  att->lua = L;
59  att->ref_own_ = luaL_ref(L, LUA_REGISTRYINDEX);
60  assert(att->ref_own_ != -1);
61  }
62  lua_getfield(L, n, "enemy");
63  if(lua_istable(L, -1)) {
64  config cfg;
65  vconfig vcfg(cfg, true);
66  if(luaW_tovconfig(L, -1, vcfg)) {
67  att->filter_enemy_.reset(new unit_filter(vcfg, resources::filter_con));
68  }
69  } else if(lua_isfunction(L, -1)) {
70  att->lua = L;
71  att->ref_enemy_ = luaL_ref(L, LUA_REGISTRYINDEX);
72  assert(att->ref_enemy_ != -1);
73  }
74  lua_pop(L, 2);
75  return att;
76  }
77 
78 } //end of namespace ai
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:622
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:757
LUALIB_API int luaL_ref(lua_State *L, int t)
Definition: lauxlib.cpp:524
Lua object(value) wrapper implementation.
#define lua_pop(L, n)
Definition: lua.h:322
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
filter_context * filter_con
Definition: resources.cpp:23
#define lua_isfunction(L, n)
Definition: lua.h:330
Aspect: attacks.
#define LUA_REGISTRYINDEX
Definition: lua.h:39
GLclampd n
Definition: glew.h:5903
#define lua_istable(L, n)
Definition: lua.h:331
boost::shared_ptr< T > to_type(lua_State *, int)
Definition: lua_object.hpp:78
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
LUA AI Support engine - creating specific ai components from config.