split scriptapi.cpp
[oweals/minetest.git] / src / scriptapi_common.h
1 /*
2 Minetest-c55
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef LUA_COMMON_H_
21 #define LUA_COMMON_H_
22
23 extern "C" {
24 #include <lua.h>
25 }
26
27 #include "server.h"
28 #include "environment.h"
29 #include "nodedef.h"
30 #include "util/pointedthing.h"
31 #include "tool.h"
32
33 Server* get_server(lua_State *L);
34 ServerEnvironment* get_env(lua_State *L);
35
36 void warn_if_field_exists(lua_State *L, int table,
37         const char *fieldname, const std::string &message);
38
39 ToolCapabilities   read_tool_capabilities        (lua_State *L, int table);
40 void               push_tool_capabilities        (lua_State *L,
41         const ToolCapabilities &prop);
42 void               set_tool_capabilities         (lua_State *L, int table,
43         const ToolCapabilities &toolcap);
44
45 void               realitycheck                  (lua_State *L);
46
47 void               push_pointed_thing            (lua_State *L,
48         const PointedThing& pointed);
49
50 void               stackDump                     (lua_State *L, std::ostream &o);
51
52 class StackUnroller
53 {
54 private:
55         lua_State *m_lua;
56         int m_original_top;
57 public:
58         StackUnroller(lua_State *L):
59                 m_lua(L),
60                 m_original_top(-1)
61         {
62                 m_original_top = lua_gettop(m_lua); // store stack height
63         }
64         ~StackUnroller()
65         {
66                 lua_settop(m_lua, m_original_top); // restore stack height
67         }
68 };
69
70 /* definitions */
71 // What scriptapi_run_callbacks does with the return values of callbacks.
72 // Regardless of the mode, if only one callback is defined,
73 // its return value is the total return value.
74 // Modes only affect the case where 0 or >= 2 callbacks are defined.
75 enum RunCallbacksMode
76 {
77         // Returns the return value of the first callback
78         // Returns nil if list of callbacks is empty
79         RUN_CALLBACKS_MODE_FIRST,
80         // Returns the return value of the last callback
81         // Returns nil if list of callbacks is empty
82         RUN_CALLBACKS_MODE_LAST,
83         // If any callback returns a false value, the first such is returned
84         // Otherwise, the first callback's return value (trueish) is returned
85         // Returns true if list of callbacks is empty
86         RUN_CALLBACKS_MODE_AND,
87         // Like above, but stops calling callbacks (short circuit)
88         // after seeing the first false value
89         RUN_CALLBACKS_MODE_AND_SC,
90         // If any callback returns a true value, the first such is returned
91         // Otherwise, the first callback's return value (falseish) is returned
92         // Returns false if list of callbacks is empty
93         RUN_CALLBACKS_MODE_OR,
94         // Like above, but stops calling callbacks (short circuit)
95         // after seeing the first true value
96         RUN_CALLBACKS_MODE_OR_SC,
97         // Note: "a true value" and "a false value" refer to values that
98         // are converted by lua_toboolean to true or false, respectively.
99 };
100
101 struct EnumString
102 {
103         int num;
104         const char *str;
105 };
106
107 bool string_to_enum(const EnumString *spec, int &result,
108                 const std::string &str);
109
110 int getenumfield(lua_State *L, int table,
111                 const char *fieldname, const EnumString *spec, int default_);
112 #endif /* LUA_COMMON_H_ */