]> git.lizzy.rs Git - minetest.git/blob - src/script/common/c_converter.h
Add minetest.get_player_window_information() (#12367)
[minetest.git] / src / script / common / c_converter.h
1 /*
2 Minetest
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
21 /******************************************************************************/
22 /******************************************************************************/
23 /* WARNING!!!! do NOT add this header in any include file or any code file    */
24 /*             not being a script/modapi file!!!!!!!!                         */
25 /******************************************************************************/
26 /******************************************************************************/
27 #pragma once
28
29 #include <vector>
30 #include <unordered_map>
31
32 #include "irrlichttypes_bloated.h"
33 #include "common/c_types.h"
34
35 extern "C" {
36 #include <lua.h>
37 }
38
39 std::string        getstringfield_default(lua_State *L, int table,
40                              const char *fieldname, const std::string &default_);
41 bool               getboolfield_default(lua_State *L, int table,
42                              const char *fieldname, bool default_);
43 float              getfloatfield_default(lua_State *L, int table,
44                              const char *fieldname, float default_);
45 int                getintfield_default(lua_State *L, int table,
46                              const char *fieldname, int default_);
47
48 bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname);
49
50 template<typename T>
51 bool getintfield(lua_State *L, int table,
52                 const char *fieldname, T &result)
53 {
54         lua_getfield(L, table, fieldname);
55         bool got = false;
56         if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)){
57                 result = lua_tointeger(L, -1);
58                 got = true;
59         }
60         lua_pop(L, 1);
61         return got;
62 }
63
64 // Retrieve an v3s16 where all components are optional (falls back to default)
65 v3s16              getv3s16field_default(lua_State *L, int table,
66                              const char *fieldname, v3s16 default_);
67
68 bool               getstringfield(lua_State *L, int table,
69                              const char *fieldname, std::string &result);
70 size_t             getstringlistfield(lua_State *L, int table,
71                              const char *fieldname,
72                              std::vector<std::string> *result);
73 void               read_groups(lua_State *L, int index,
74                              std::unordered_map<std::string, int> &result);
75 bool               getboolfield(lua_State *L, int table,
76                              const char *fieldname, bool &result);
77 bool               getfloatfield(lua_State *L, int table,
78                              const char *fieldname, float &result);
79
80 void               setstringfield(lua_State *L, int table,
81                              const char *fieldname, const std::string &value);
82 void               setintfield(lua_State *L, int table,
83                              const char *fieldname, int value);
84 void               setfloatfield(lua_State *L, int table,
85                              const char *fieldname, float value);
86 void               setboolfield(lua_State *L, int table,
87                              const char *fieldname, bool value);
88
89 v3f                 checkFloatPos       (lua_State *L, int index);
90 v2f                 check_v2f           (lua_State *L, int index);
91 v3f                 check_v3f           (lua_State *L, int index);
92 v3s16               check_v3s16         (lua_State *L, int index);
93
94 v3f                 read_v3f            (lua_State *L, int index);
95 v2f                 read_v2f            (lua_State *L, int index);
96 v2s16               read_v2s16          (lua_State *L, int index);
97 v2s32               read_v2s32          (lua_State *L, int index);
98 video::SColor       read_ARGB8          (lua_State *L, int index);
99 bool                read_color          (lua_State *L, int index,
100                                          video::SColor *color);
101 bool                is_color_table      (lua_State *L, int index);
102
103 aabb3f              read_aabb3f         (lua_State *L, int index, f32 scale);
104 v3s16               read_v3s16          (lua_State *L, int index);
105 std::vector<aabb3f> read_aabb3f_vector  (lua_State *L, int index, f32 scale);
106 size_t              read_stringlist     (lua_State *L, int index,
107                                          std::vector<std::string> *result);
108
109 void                push_v2s16          (lua_State *L, v2s16 p);
110 void                push_v2s32          (lua_State *L, v2s32 p);
111 void                push_v2u32          (lua_State *L, v2u32 p);
112 void                push_v3s16          (lua_State *L, v3s16 p);
113 void                push_aabb3f         (lua_State *L, aabb3f box);
114 void                push_ARGB8          (lua_State *L, video::SColor color);
115 void                pushFloatPos        (lua_State *L, v3f p);
116 void                push_v3f            (lua_State *L, v3f p);
117 void                push_v2f            (lua_State *L, v2f p);
118
119 void                warn_if_field_exists(lua_State *L, int table,
120                                          const char *fieldname,
121                                          const std::string &message);
122
123 size_t write_array_slice_float(lua_State *L, int table_index, float *data,
124         v3u16 data_size, v3u16 slice_offset, v3u16 slice_size);
125
126 // This must match the implementation in builtin/game/misc_s.lua
127 // Note that this returns a floating point result as Lua integers are 32-bit
128 inline lua_Number hash_node_position(v3s16 pos)
129 {
130         return (((s64)pos.Z + 0x8000L) << 32)
131                         | (((s64)pos.Y + 0x8000L) << 16)
132                         | ((s64)pos.X + 0x8000L);
133 }