The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_unit_type.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[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 
16 
17 #include "scripting/lua_common.hpp"
18 #include "scripting/push_check.hpp"
19 #include "units/types.hpp"
20 
21 #include <string>
22 #include <string.h>
23 
24 #include "lua/lua.h"
25 #include "lua/lauxlib.h"
26 
27 /**
28  * Implementation for a lua reference to a unit_type.
29  */
30 
31 // Registry key
32 static const char * UnitType = "unit type";
33 
34 /**
35  * Gets some data on a unit type (__index metamethod).
36  * - Arg 1: table containing an "id" field.
37  * - Arg 2: string containing the name of the property.
38  * - Ret 1: something containing the attribute.
39  */
41 {
42  char const *m = luaL_checkstring(L, 2);
43  lua_pushstring(L, "id");
44  lua_rawget(L, 1);
45  const unit_type *utp = unit_types.find(lua_tostring(L, -1));
46  if (!utp) return luaL_argerror(L, 1, "unknown unit type");
47  unit_type const &ut = *utp;
48 
49  // Find the corresponding attribute.
50  return_tstring_attrib("name", ut.type_name());
51  return_string_attrib("id", ut.id());
52  return_string_attrib("alignment", ut.alignment().to_string());
53  return_string_attrib("race", ut.race_id());
54  return_int_attrib("max_hitpoints", ut.hitpoints());
55  return_int_attrib("max_moves", ut.movement());
56  return_int_attrib("max_experience", ut.experience_needed());
57  return_int_attrib("cost", ut.cost());
58  return_int_attrib("level", ut.level());
59  return_int_attrib("recall_cost", ut.recall_cost());
60  return_cfgref_attrib("__cfg", ut.get_cfg());
61  if (strcmp(m, "traits") == 0) {
62  lua_newtable(L);
63  for (const config& trait : ut.possible_traits()) {
64  const std::string& id = trait["id"];
65  lua_pushlstring(L, id.c_str(), id.length());
66  luaW_pushconfig(L, trait);
67  lua_rawset(L, -3);
68  }
69  return 1;
70  }
71  if (strcmp(m, "abilities") == 0) {
72  lua_push(L, ut.get_ability_list());
73  return 1;
74  }
75  if (strcmp(m, "attacks") == 0) {
76  lua_createtable(L, 1, 0);
77  lua_pushvalue(L, 1);
78  // hack: store the unit_type at -1 because we want positive indices to refer to the attacks.
79  lua_rawseti(L, -2, -1);
82  lua_setmetatable(L, -2);
83  return 1;
84  }
85  return 0;
86 }
87 
88 namespace lua_unit_type {
90  {
92 
94  lua_setfield(L, -2, "__index");
95  lua_pushstring(L, "unit type");
96  lua_setfield(L, -2, "__metatable");
97 
98  return "Adding unit type metatable...\n";
99  }
100 }
101 
103 {
104  lua_createtable(L, 0, 1);
105  lua_pushstring(L, id.c_str());
106  lua_setfield(L, -2, "id");
108 }
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
Definition: lapi.cpp:579
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:667
#define lua_pushcfunction(L, f)
Definition: lua.h:328
#define return_tstring_attrib(name, accessor)
Definition: lua_common.hpp:163
const t_string & type_name() const
The name of the unit in the current language setting.
Definition: types.hpp:112
void luaW_pushunittype(lua_State *L, const std::string &id)
Create a lua object containing a reference to a unittype, and a metatable to access the properties...
int movement() const
Definition: types.hpp:128
This namespace contains bindings for lua to hold a reference to a unit type and access its stats...
std::string register_metatable(lua_State *L)
std::vector< std::string > get_ability_list() const
Definition: types.cpp:545
#define return_string_attrib(name, accessor)
Definition: lua_common.hpp:175
unit_type_data unit_types
Definition: types.cpp:1314
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:286
void lua_push(lua_State *L, const T &val)
Definition: push_check.hpp:314
int level() const
Definition: types.hpp:126
GLuint GLsizei GLsizei * length
Definition: glew.h:1793
#define return_cfgref_attrib(name, accessor)
Definition: lua_common.hpp:204
#define return_int_attrib(name, accessor)
Definition: lua_common.hpp:178
static const char * UnitType
Implementation for a lua reference to a unit_type.
LUA_API const char * lua_pushlstring(lua_State *L, const char *s, size_t len)
Definition: lapi.cpp:495
LUA_API int lua_setmetatable(lua_State *L, int objindex)
Definition: lapi.cpp:806
#define lua_newtable(L)
Definition: lua.h:324
std::string race_id() const
Returns the ID of this type's race without the need to build the type.
Definition: types.hpp:215
int cost() const
Definition: types.hpp:134
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:274
LUA_API void lua_rawset(lua_State *L, int idx)
Definition: lapi.cpp:764
config::const_child_itors possible_traits() const
Definition: types.hpp:182
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
Definition: lauxlib.cpp:152
#define lua_tostring(L, i)
Definition: lua.h:345
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
Definition: lapi.cpp:778
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.cpp:229
const config & get_cfg() const
Definition: types.hpp:222
int hitpoints() const
Definition: types.hpp:123
int experience_needed(bool with_acceleration=true) const
Definition: types.cpp:514
#define LUA_REGISTRYINDEX
Definition: lua.h:39
const GLdouble * m
Definition: glew.h:6968
void luaW_pushconfig(lua_State *L, config const &cfg)
Converts a config object to a Lua table pushed at the top of the stack.
Definition: lua_common.cpp:663
static int impl_unit_type_get(lua_State *L)
Gets some data on a unit type (__index metamethod).
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
Definition: types.cpp:1155
luatypekey const uattacksKey
Definition: lua_types.cpp:31
int recall_cost() const
Definition: types.hpp:127
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
LUA_API void lua_rawget(lua_State *L, int idx)
Definition: lapi.cpp:633
GLsizei const GLcharARB ** string
Definition: glew.h:4503
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:507
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:752
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:115
#define luaL_checkstring(L, n)
Definition: lauxlib.h:115