]> git.lizzy.rs Git - dragonfireclient.git/blob - src/scriptapi_node.cpp
Add new drawtype GLASSLIKE_FRAMED
[dragonfireclient.git] / src / scriptapi_node.cpp
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 #include "scriptapi.h"
21 #include "scriptapi_node.h"
22 #include "util/pointedthing.h"
23 #include "script.h"
24 #include "scriptapi_common.h"
25 #include "scriptapi_types.h"
26 #include "scriptapi_item.h"
27 #include "scriptapi_object.h"
28
29
30 struct EnumString es_DrawType[] =
31 {
32         {NDT_NORMAL, "normal"},
33         {NDT_AIRLIKE, "airlike"},
34         {NDT_LIQUID, "liquid"},
35         {NDT_FLOWINGLIQUID, "flowingliquid"},
36         {NDT_GLASSLIKE, "glasslike"},
37         {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
38         {NDT_ALLFACES, "allfaces"},
39         {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
40         {NDT_TORCHLIKE, "torchlike"},
41         {NDT_SIGNLIKE, "signlike"},
42         {NDT_PLANTLIKE, "plantlike"},
43         {NDT_FENCELIKE, "fencelike"},
44         {NDT_RAILLIKE, "raillike"},
45         {NDT_NODEBOX, "nodebox"},
46         {0, NULL},
47 };
48
49 struct EnumString es_ContentParamType[] =
50 {
51         {CPT_NONE, "none"},
52         {CPT_LIGHT, "light"},
53         {0, NULL},
54 };
55
56 struct EnumString es_ContentParamType2[] =
57 {
58         {CPT2_NONE, "none"},
59         {CPT2_FULL, "full"},
60         {CPT2_FLOWINGLIQUID, "flowingliquid"},
61         {CPT2_FACEDIR, "facedir"},
62         {CPT2_WALLMOUNTED, "wallmounted"},
63         {0, NULL},
64 };
65
66 struct EnumString es_LiquidType[] =
67 {
68         {LIQUID_NONE, "none"},
69         {LIQUID_FLOWING, "flowing"},
70         {LIQUID_SOURCE, "source"},
71         {0, NULL},
72 };
73
74 struct EnumString es_NodeBoxType[] =
75 {
76         {NODEBOX_REGULAR, "regular"},
77         {NODEBOX_FIXED, "fixed"},
78         {NODEBOX_WALLMOUNTED, "wallmounted"},
79         {0, NULL},
80 };
81
82
83 bool scriptapi_node_on_punch(lua_State *L, v3s16 p, MapNode node,
84                 ServerActiveObject *puncher)
85 {
86         realitycheck(L);
87         assert(lua_checkstack(L, 20));
88         StackUnroller stack_unroller(L);
89
90         INodeDefManager *ndef = get_server(L)->ndef();
91
92         // Push callback function on stack
93         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_punch"))
94                 return false;
95
96         // Call function
97         push_v3s16(L, p);
98         pushnode(L, node, ndef);
99         objectref_get_or_create(L, puncher);
100         if(lua_pcall(L, 3, 0, 0))
101                 script_error(L, "error: %s", lua_tostring(L, -1));
102         return true;
103 }
104
105 bool scriptapi_node_on_dig(lua_State *L, v3s16 p, MapNode node,
106                 ServerActiveObject *digger)
107 {
108         realitycheck(L);
109         assert(lua_checkstack(L, 20));
110         StackUnroller stack_unroller(L);
111
112         INodeDefManager *ndef = get_server(L)->ndef();
113
114         // Push callback function on stack
115         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_dig"))
116                 return false;
117
118         // Call function
119         push_v3s16(L, p);
120         pushnode(L, node, ndef);
121         objectref_get_or_create(L, digger);
122         if(lua_pcall(L, 3, 0, 0))
123                 script_error(L, "error: %s", lua_tostring(L, -1));
124         return true;
125 }
126
127 void scriptapi_node_on_construct(lua_State *L, v3s16 p, MapNode node)
128 {
129         realitycheck(L);
130         assert(lua_checkstack(L, 20));
131         StackUnroller stack_unroller(L);
132
133         INodeDefManager *ndef = get_server(L)->ndef();
134
135         // Push callback function on stack
136         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_construct"))
137                 return;
138
139         // Call function
140         push_v3s16(L, p);
141         if(lua_pcall(L, 1, 0, 0))
142                 script_error(L, "error: %s", lua_tostring(L, -1));
143 }
144
145 void scriptapi_node_on_destruct(lua_State *L, v3s16 p, MapNode node)
146 {
147         realitycheck(L);
148         assert(lua_checkstack(L, 20));
149         StackUnroller stack_unroller(L);
150
151         INodeDefManager *ndef = get_server(L)->ndef();
152
153         // Push callback function on stack
154         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_destruct"))
155                 return;
156
157         // Call function
158         push_v3s16(L, p);
159         if(lua_pcall(L, 1, 0, 0))
160                 script_error(L, "error: %s", lua_tostring(L, -1));
161 }
162
163 void scriptapi_node_after_destruct(lua_State *L, v3s16 p, MapNode node)
164 {
165         realitycheck(L);
166         assert(lua_checkstack(L, 20));
167         StackUnroller stack_unroller(L);
168
169         INodeDefManager *ndef = get_server(L)->ndef();
170
171         // Push callback function on stack
172         if(!get_item_callback(L, ndef->get(node).name.c_str(), "after_destruct"))
173                 return;
174
175         // Call function
176         push_v3s16(L, p);
177         pushnode(L, node, ndef);
178         if(lua_pcall(L, 2, 0, 0))
179                 script_error(L, "error: %s", lua_tostring(L, -1));
180 }
181
182 bool scriptapi_node_on_timer(lua_State *L, v3s16 p, MapNode node, f32 dtime)
183 {
184         realitycheck(L);
185         assert(lua_checkstack(L, 20));
186         StackUnroller stack_unroller(L);
187
188         INodeDefManager *ndef = get_server(L)->ndef();
189
190         // Push callback function on stack
191         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_timer"))
192                 return false;
193
194         // Call function
195         push_v3s16(L, p);
196         lua_pushnumber(L,dtime);
197         if(lua_pcall(L, 2, 1, 0))
198                 script_error(L, "error: %s", lua_tostring(L, -1));
199         if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
200                 return true;
201
202         return false;
203 }
204
205 void scriptapi_node_on_receive_fields(lua_State *L, v3s16 p,
206                 const std::string &formname,
207                 const std::map<std::string, std::string> &fields,
208                 ServerActiveObject *sender)
209 {
210         realitycheck(L);
211         assert(lua_checkstack(L, 20));
212         StackUnroller stack_unroller(L);
213
214         INodeDefManager *ndef = get_server(L)->ndef();
215
216         // If node doesn't exist, we don't know what callback to call
217         MapNode node = get_env(L)->getMap().getNodeNoEx(p);
218         if(node.getContent() == CONTENT_IGNORE)
219                 return;
220
221         // Push callback function on stack
222         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_receive_fields"))
223                 return;
224
225         // Call function
226         // param 1
227         push_v3s16(L, p);
228         // param 2
229         lua_pushstring(L, formname.c_str());
230         // param 3
231         lua_newtable(L);
232         for(std::map<std::string, std::string>::const_iterator
233                         i = fields.begin(); i != fields.end(); i++){
234                 const std::string &name = i->first;
235                 const std::string &value = i->second;
236                 lua_pushstring(L, name.c_str());
237                 lua_pushlstring(L, value.c_str(), value.size());
238                 lua_settable(L, -3);
239         }
240         // param 4
241         objectref_get_or_create(L, sender);
242         if(lua_pcall(L, 4, 0, 0))
243                 script_error(L, "error: %s", lua_tostring(L, -1));
244 }