The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_race.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 
15 #include "scripting/lua_race.hpp"
16 
17 #include "units/race.hpp"
18 #include "scripting/lua_common.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 race,
29  * used by the wesnoth in-game races table.
30  */
31 
32 // Registry key
33 static const char * Race = "race";
34 static const char * Gen = "name generator";
35 
36 /**
37  * Gets some data on a race (__index metamethod).
38  * - Arg 1: table containing an "id" field.
39  * - Arg 2: string containing the name of the property.
40  * - Ret 1: something containing the attribute.
41  */
42 static int impl_race_get(lua_State* L)
43 {
44  char const* m = luaL_checkstring(L, 2);
45  lua_pushstring(L, "id");
46  lua_rawget(L, 1);
47  const unit_race* raceptr = unit_types.find_race(lua_tostring(L, -1));
48  if(!raceptr) return luaL_argerror(L, 1, "unknown race");
49  unit_race const &race = *raceptr;
50 
51  return_tstring_attrib("description", race.description());
52  return_tstring_attrib("name", race.name());
53  return_int_attrib("num_traits", race.num_traits());
54  return_tstring_attrib("plural_name", race.plural_name());
55  return_bool_attrib("ignore_global_traits", !race.uses_global_traits());
56  return_string_attrib("undead_variation", race.undead_variation());
57  return_cfgref_attrib("__cfg", race.get_cfg());
58  if (strcmp(m, "traits") == 0) {
59  lua_newtable(L);
60  if (race.uses_global_traits()) {
61  for (const config& trait : unit_types.traits()) {
62  const std::string& id = trait["id"];
63  lua_pushlstring(L, id.c_str(), id.length());
64  luaW_pushconfig(L, trait);
65  lua_rawset(L, -3);
66  }
67  }
68  for (const config& trait : race.additional_traits()) {
69  const std::string& id = trait["id"];
70  lua_pushlstring(L, id.c_str(), id.length());
71  luaW_pushconfig(L, trait);
72  lua_rawset(L, -3);
73  }
74  return 1;
75  }
76  if (strcmp(m, "male_name_gen") == 0) {
77  new(lua_newuserdata(L, sizeof(proxy_name_generator)))
80  lua_setmetatable(L, -2);
81  return 1;
82  }
83  if (strcmp(m, "female_name_gen") == 0) {
84  new(lua_newuserdata(L, sizeof(proxy_name_generator)))
87  lua_setmetatable(L, -2);
88  return 1;
89  }
90 
91  return 0;
92 }
93 
94 namespace lua_race {
95 
97  {
99 
100  static luaL_Reg const callbacks[] = {
101  { "__index", &impl_race_get},
102  { nullptr, nullptr }
103  };
104  luaL_setfuncs(L, callbacks, 0);
105 
106  lua_pushstring(L, "race");
107  lua_setfield(L, -2, "__metatable");
108 
109  return "Adding getrace metatable...\n";
110  }
111 }
112 
114 {
115  lua_createtable(L, 0, 1);
116  lua_pushstring(L, race.id().c_str());
117  lua_setfield(L, -2, "id");
119 }
120 
122 {
123  const race_map& races = unit_types.races();
124  lua_createtable(L, 0, races.size());
125 
126  for (const race_map::value_type &race : races)
127  {
128  assert(race.first == race.second.id());
129  luaW_pushrace(L, race.second);
130  lua_setfield(L, -2, race.first.c_str());
131  }
132 }
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:667
bool uses_global_traits() const
Definition: race.cpp:133
#define return_tstring_attrib(name, accessor)
Definition: lua_common.hpp:163
const std::string race
const t_string & name(GENDER gender=MALE) const
Definition: race.hpp:34
const std::string & undead_variation() const
Definition: race.hpp:46
const std::string & id() const
Definition: race.hpp:33
#define return_string_attrib(name, accessor)
Definition: lua_common.hpp:175
unit_type_data unit_types
Definition: types.cpp:1314
#define luaL_getmetatable(L, n)
Definition: lauxlib.h:130
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:286
LUA_API void * lua_newuserdata(lua_State *L, size_t size)
Definition: lapi.cpp:1169
const config::const_child_itors & additional_traits() const
Definition: race.cpp:138
GLuint GLsizei GLsizei * length
Definition: glew.h:1793
#define return_cfgref_attrib(name, accessor)
Definition: lua_common.hpp:204
const unit_race * find_race(const std::string &) const
Definition: types.cpp:1286
#define return_int_attrib(name, accessor)
Definition: lua_common.hpp:178
const t_string & description() const
Definition: race.hpp:36
This namespace contains bindings for lua to hold a pointer to a race, and to access and modify it...
Definition: lua_race.cpp:94
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
const t_string & plural_name() const
Definition: race.hpp:35
static const char * Race
Implementation for a lua reference to a race, used by the wesnoth in-game races table.
Definition: lua_race.cpp:33
#define lua_newtable(L)
Definition: lua.h:324
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:274
void luaW_pushracetable(lua_State *L)
Definition: lua_race.cpp:121
static int impl_race_get(lua_State *L)
Gets some data on a race (__index metamethod).
Definition: lua_race.cpp:42
LUA_API void lua_rawset(lua_State *L, int idx)
Definition: lapi.cpp:764
const race_map & races() const
Definition: types.hpp:314
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
Definition: lauxlib.cpp:152
const name_generator & generator(GENDER gender) const
Definition: race.cpp:128
#define lua_tostring(L, i)
Definition: lua.h:345
std::string register_metatable(lua_State *L)
Definition: lua_race.cpp:96
const config::const_child_itors traits() const
Definition: types.hpp:315
void luaW_pushrace(lua_State *L, const unit_race &race)
Definition: lua_race.cpp:113
#define return_bool_attrib(name, accessor)
Definition: lua_common.hpp:190
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 const char * Gen
Definition: lua_race.cpp:34
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
Definition: lauxlib.cpp:850
unsigned int num_traits() const
Definition: race.cpp:148
std::map< std::string, unit_race > race_map
Definition: race.hpp:73
const config & get_cfg() const
Definition: race.hpp:32
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
#define luaL_checkstring(L, n)
Definition: lauxlib.h:115