]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/common/c_converter.h
Merge branch 'master' into master
[dragonfireclient.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 template<class T>
65 bool getv3intfield(lua_State *L, int index,
66                 const char *fieldname, T &result)
67 {
68         lua_getfield(L, index, fieldname);
69         bool got = false;
70         if (lua_istable(L, -1)) {
71                 got |= getintfield(L, -1, "x", result.X);
72                 got |= getintfield(L, -1, "y", result.Y);
73                 got |= getintfield(L, -1, "z", result.Z);
74         }
75         lua_pop(L, 1);
76         return got;
77 }
78
79 v3s16              getv3s16field_default(lua_State *L, int table,
80                              const char *fieldname, v3s16 default_);
81 bool               getstringfield(lua_State *L, int table,
82                              const char *fieldname, std::string &result);
83 size_t             getstringlistfield(lua_State *L, int table,
84                              const char *fieldname,
85                              std::vector<std::string> *result);
86 void               read_groups(lua_State *L, int index,
87                              std::unordered_map<std::string, int> &result);
88 bool               getboolfield(lua_State *L, int table,
89                              const char *fieldname, bool &result);
90 bool               getfloatfield(lua_State *L, int table,
91                              const char *fieldname, float &result);
92
93 void               setstringfield(lua_State *L, int table,
94                              const char *fieldname, const std::string &value);
95 void               setintfield(lua_State *L, int table,
96                              const char *fieldname, int value);
97 void               setfloatfield(lua_State *L, int table,
98                              const char *fieldname, float value);
99 void               setboolfield(lua_State *L, int table,
100                              const char *fieldname, bool value);
101
102 v3f                 checkFloatPos       (lua_State *L, int index);
103 v3f                 check_v3f           (lua_State *L, int index);
104 v3s16               check_v3s16         (lua_State *L, int index);
105
106 v3f                 read_v3f            (lua_State *L, int index);
107 v2f                 read_v2f            (lua_State *L, int index);
108 v2s16               read_v2s16          (lua_State *L, int index);
109 v2s32               read_v2s32          (lua_State *L, int index);
110 video::SColor       read_ARGB8          (lua_State *L, int index);
111 bool                read_color          (lua_State *L, int index,
112                                          video::SColor *color);
113 bool                is_color_table      (lua_State *L, int index);
114
115 aabb3f              read_aabb3f         (lua_State *L, int index, f32 scale);
116 v3s16               read_v3s16          (lua_State *L, int index);
117 std::vector<aabb3f> read_aabb3f_vector  (lua_State *L, int index, f32 scale);
118 size_t              read_stringlist     (lua_State *L, int index,
119                                          std::vector<std::string> *result);
120
121 void                push_float_string   (lua_State *L, float value);
122 void                push_v3_float_string(lua_State *L, v3f p);
123 void                push_v2_float_string(lua_State *L, v2f p);
124 void                push_v2s16          (lua_State *L, v2s16 p);
125 void                push_v2s32          (lua_State *L, v2s32 p);
126 void                push_v3s16          (lua_State *L, v3s16 p);
127 void                push_aabb3f         (lua_State *L, aabb3f box);
128 void                push_ARGB8          (lua_State *L, video::SColor color);
129 void                pushFloatPos        (lua_State *L, v3f p);
130 void                push_v3f            (lua_State *L, v3f p);
131 void                push_v2f            (lua_State *L, v2f p);
132
133 void                warn_if_field_exists(lua_State *L, int table,
134                                          const char *fieldname,
135                                          const std::string &message);
136
137 size_t write_array_slice_float(lua_State *L, int table_index, float *data,
138         v3u16 data_size, v3u16 slice_offset, v3u16 slice_size);
139 size_t write_array_slice_u16(lua_State *L, int table_index, u16 *data,
140         v3u16 data_size, v3u16 slice_offset, v3u16 slice_size);