The Battle for Wesnoth  1.19.0-dev
lua_object.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2024
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"
23 #include "scripting/lua_common.hpp"
24 
25 #include "lua/wrapper_lauxlib.h"
26 
27 namespace ai {
28 
30  {
31  // empty
32  }
33 
34  // MSVC fails to compile without this line
36 
37  template <>
38  std::shared_ptr<aspect_attacks_lua_filter> lua_object<aspect_attacks_lua_filter>::to_type(lua_State *L, int n)
39  {
40  std::shared_ptr<aspect_attacks_lua_filter> att(new aspect_attacks_lua_filter);
41  att->lua = nullptr;
42  att->ref_own_ = att->ref_enemy_ = -1;
43  if(!lua_istable(L, n)) {
44  return att;
45  }
46  lua_getfield(L, n, "own");
47  if(lua_istable(L, -1)) {
48  vconfig vcfg(config(), true);
49  if(luaW_tovconfig(L, -1, vcfg)) {
50  att->filter_own_.reset(new unit_filter(vcfg));
51  }
52  } else if(lua_isfunction(L, -1)) {
53  att->lua = L;
54  att->ref_own_ = luaL_ref(L, LUA_REGISTRYINDEX);
55  assert(att->ref_own_ != -1);
56  }
57  lua_getfield(L, n, "enemy");
58  if(lua_istable(L, -1)) {
59  vconfig vcfg(config(), true);
60  if(luaW_tovconfig(L, -1, vcfg)) {
61  att->filter_enemy_.reset(new unit_filter(vcfg));
62  }
63  } else if(lua_isfunction(L, -1)) {
64  att->lua = L;
65  att->ref_enemy_ = luaL_ref(L, LUA_REGISTRYINDEX);
66  assert(att->ref_enemy_ != -1);
67  }
68  lua_pop(L, 2);
69  return att;
70  }
71 
72 } //end of namespace ai
Aspect: attacks.
std::shared_ptr< T > to_type(lua_State *, int)
Definition: lua_object.hpp:87
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
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:934
Lua object(value) wrapper implementation.
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
static map_location::DIRECTION n