The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_api.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Guillaume Melquiond <[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 #ifndef SCRIPTING_LUA_API_HPP
16 #define SCRIPTING_LUA_API_HPP
17 
18 #include <cstddef>
19 #include <string>
20 #include "lua_types.hpp" // the luatype typedef
21 #include "units/ptr.hpp"
22 
23 struct lua_State;
24 
25 /**
26  * Converts a Lua value to a unit pointer.
27  */
28 unit* luaW_tounit(lua_State *L, int index, bool only_on_map = false);
29 
30 /**
31  * Displays a message in the chat window.
32  */
33 void chat_message(std::string const &caption, std::string const &msg);
34 
35 /**
36  * Calls a Lua function stored below its @a nArgs arguments at the top of the stack.
37  * @param nRets LUA_MULTRET for unbounded return values.
38  * @return true if the call was successful and @a nRets return values are available.
39  */
40 bool luaW_pcall(lua_State *L, int nArgs, int nRets, bool allow_wml_error = false);
41 
42 /**
43  * Converts a Lua value to a unit pointer.
44  */
45 unit& luaW_checkunit(lua_State *L, int index, bool only_on_map = false);
46 class lua_unit;
48 /**
49  * Similar to luaW_checkunit/luaW_tounit but returns a unit_ptr, use this instead of
50  * luaW_checkunit/luaW_tounit when uasing an api that needs unit_ptr.
51  */
52 unit_ptr luaW_tounit_ptr(lua_State *L, int index, bool only_on_map);
53 unit_ptr luaW_checkunit_ptr(lua_State *L, int index, bool only_on_map);
54 
55 struct map_location;
56 
57 /**
58  * Storage for a unit, either owned by the Lua code (#ptr != 0), a
59  * local variable unit (c_ptr != 0), on a recall list (#side != 0), or on the map.
60  * Shared units are represented by their underlying ID (#uid).
61  */
62 class lua_unit
63 {
64  size_t uid;
66  int side;
68  lua_unit(lua_unit const &);
69 
70 public:
71  lua_unit(size_t u): uid(u), ptr(), side(0), c_ptr() {}
72  lua_unit(unit_ptr u): uid(0), ptr(u), side(0), c_ptr() {}
73  lua_unit(int s, size_t u): uid(u), ptr(), side(s), c_ptr() {}
74  lua_unit(unit& u): uid(0), ptr(), side(0), c_ptr(&u) {}
75  ~lua_unit();
76  bool on_map() const { return !ptr && side == 0; }
77  int on_recall_list() const { return side; }
78  unit* get();
80 
81  void clear_ref() { uid = 0; ptr = unit_ptr(); side = 0; c_ptr = nullptr; }
82  // Clobbers loc
83  bool put_map(const map_location &loc);
84 };
85 
86 #endif
Definition: unit.hpp:95
~lua_unit()
Definition: lua_api.cpp:113
unit & luaW_checkunit(lua_State *L, int index, bool only_on_map=false)
Converts a Lua value to a unit pointer.
Definition: lua_api.cpp:205
unit * luaW_tounit(lua_State *L, int index, bool only_on_map=false)
Converts a Lua value to a unit pointer.
Definition: lua_api.cpp:183
unit_ptr get_shared()
Definition: lua_api.cpp:128
lua_unit(int s, size_t u)
Definition: lua_api.hpp:73
unit * c_ptr
Definition: lua_api.hpp:67
unit_ptr luaW_tounit_ptr(lua_State *L, int index, bool only_on_map)
Similar to luaW_checkunit/luaW_tounit but returns a unit_ptr, use this instead of luaW_checkunit/luaW...
Definition: lua_api.cpp:191
bool luaW_pcall(lua_State *L, int nArgs, int nRets, bool allow_wml_error=false)
Calls a Lua function stored below its nArgs arguments at the top of the stack.
Definition: lua_api.cpp:59
lua_unit(unit_ptr u)
Definition: lua_api.hpp:72
Encapsulates the map of the game.
Definition: location.hpp:38
Storage for a unit, either owned by the Lua code (ptr != 0), a local variable unit (c_ptr != 0)...
Definition: lua_api.hpp:62
int on_recall_list() const
Definition: lua_api.hpp:77
lua_unit(size_t u)
Definition: lua_api.hpp:71
int side
Definition: lua_api.hpp:66
GLuint index
Definition: glew.h:1782
size_t uid
Definition: lua_api.hpp:64
void chat_message(std::string const &caption, std::string const &msg)
Displays a message in the chat window.
Definition: lua_api.cpp:48
lua_unit(unit &u)
Definition: lua_api.hpp:74
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
bool on_map() const
Definition: lua_api.hpp:76
lua_unit * luaW_pushlocalunit(lua_State *L, unit &u)
Definition: lua_api.cpp:212
unit_ptr luaW_checkunit_ptr(lua_State *L, int index, bool only_on_map)
Definition: lua_api.cpp:199
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
bool put_map(const map_location &loc)
Definition: lua_api.cpp:143
unit_ptr ptr
Definition: lua_api.hpp:65
lua_unit(lua_unit const &)
GLdouble s
Definition: glew.h:1358
void clear_ref()
Definition: lua_api.hpp:81
GLsizei const GLcharARB ** string
Definition: glew.h:4503