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