2ee727b3f010c16aa9d6dd9f0d788a6d79f5845f
[oweals/minetest.git] / src / scriptapi.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 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 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
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 General Public License for more details.
14
15 You should have received a copy of the GNU 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
22 #include <iostream>
23 #include <list>
24 extern "C" {
25 #include <lua.h>
26 #include <lualib.h>
27 #include <lauxlib.h>
28 }
29
30 #include "log.h"
31 #include "server.h"
32 #include "porting.h"
33 #include "filesys.h"
34 #include "serverobject.h"
35 #include "script.h"
36 //#include "luna.h"
37 #include "luaentity_common.h"
38 #include "content_sao.h" // For LuaEntitySAO
39 #include "itemdef.h"
40 #include "nodedef.h"
41 #include "craftdef.h"
42 #include "main.h" // For g_settings
43 #include "settings.h" // For accessing g_settings
44 #include "nodemetadata.h"
45 #include "mapblock.h" // For getNodeBlockPos
46 #include "content_nodemeta.h"
47 #include "utility.h"
48 #include "tool.h"
49
50 static void stackDump(lua_State *L, std::ostream &o)
51 {
52   int i;
53   int top = lua_gettop(L);
54   for (i = 1; i <= top; i++) {  /* repeat for each level */
55         int t = lua_type(L, i);
56         switch (t) {
57
58           case LUA_TSTRING:  /* strings */
59                 o<<"\""<<lua_tostring(L, i)<<"\"";
60                 break;
61
62           case LUA_TBOOLEAN:  /* booleans */
63                 o<<(lua_toboolean(L, i) ? "true" : "false");
64                 break;
65
66           case LUA_TNUMBER:  /* numbers */ {
67                 char buf[10];
68                 snprintf(buf, 10, "%g", lua_tonumber(L, i));
69                 o<<buf;
70                 break; }
71
72           default:  /* other values */
73                 o<<lua_typename(L, t);
74                 break;
75
76         }
77         o<<" ";
78   }
79   o<<std::endl;
80 }
81
82 static void realitycheck(lua_State *L)
83 {
84         int top = lua_gettop(L);
85         if(top >= 30){
86                 dstream<<"Stack is over 30:"<<std::endl;
87                 stackDump(L, dstream);
88                 script_error(L, "Stack is over 30 (reality check)");
89         }
90 }
91
92 class StackUnroller
93 {
94 private:
95         lua_State *m_lua;
96         int m_original_top;
97 public:
98         StackUnroller(lua_State *L):
99                 m_lua(L),
100                 m_original_top(-1)
101         {
102                 m_original_top = lua_gettop(m_lua); // store stack height
103         }
104         ~StackUnroller()
105         {
106                 lua_settop(m_lua, m_original_top); // restore stack height
107         }
108 };
109
110 class ModNameStorer
111 {
112 private:
113         lua_State *L;
114 public:
115         ModNameStorer(lua_State *L_, const std::string modname):
116                 L(L_)
117         {
118                 // Store current modname in registry
119                 lua_pushstring(L, modname.c_str());
120                 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
121         }
122         ~ModNameStorer()
123         {
124                 // Clear current modname in registry
125                 lua_pushnil(L);
126                 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
127         }
128 };
129
130 /*
131         Getters for stuff in main tables
132 */
133
134 static Server* get_server(lua_State *L)
135 {
136         // Get server from registry
137         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
138         Server *server = (Server*)lua_touserdata(L, -1);
139         lua_pop(L, 1);
140         return server;
141 }
142
143 static ServerEnvironment* get_env(lua_State *L)
144 {
145         // Get environment from registry
146         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_env");
147         ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1);
148         lua_pop(L, 1);
149         return env;
150 }
151
152 static void objectref_get(lua_State *L, u16 id)
153 {
154         // Get minetest.object_refs[i]
155         lua_getglobal(L, "minetest");
156         lua_getfield(L, -1, "object_refs");
157         luaL_checktype(L, -1, LUA_TTABLE);
158         lua_pushnumber(L, id);
159         lua_gettable(L, -2);
160         lua_remove(L, -2); // object_refs
161         lua_remove(L, -2); // minetest
162 }
163
164 static void luaentity_get(lua_State *L, u16 id)
165 {
166         // Get minetest.luaentities[i]
167         lua_getglobal(L, "minetest");
168         lua_getfield(L, -1, "luaentities");
169         luaL_checktype(L, -1, LUA_TTABLE);
170         lua_pushnumber(L, id);
171         lua_gettable(L, -2);
172         lua_remove(L, -2); // luaentities
173         lua_remove(L, -2); // minetest
174 }
175
176 /*
177         Table field getters
178 */
179
180 static bool getstringfield(lua_State *L, int table,
181                 const char *fieldname, std::string &result)
182 {
183         lua_getfield(L, table, fieldname);
184         bool got = false;
185         if(lua_isstring(L, -1)){
186                 size_t len = 0;
187                 const char *ptr = lua_tolstring(L, -1, &len);
188                 result.assign(ptr, len);
189                 got = true;
190         }
191         lua_pop(L, 1);
192         return got;
193 }
194
195 static bool getintfield(lua_State *L, int table,
196                 const char *fieldname, int &result)
197 {
198         lua_getfield(L, table, fieldname);
199         bool got = false;
200         if(lua_isnumber(L, -1)){
201                 result = lua_tonumber(L, -1);
202                 got = true;
203         }
204         lua_pop(L, 1);
205         return got;
206 }
207
208 static bool getfloatfield(lua_State *L, int table,
209                 const char *fieldname, float &result)
210 {
211         lua_getfield(L, table, fieldname);
212         bool got = false;
213         if(lua_isnumber(L, -1)){
214                 result = lua_tonumber(L, -1);
215                 got = true;
216         }
217         lua_pop(L, 1);
218         return got;
219 }
220
221 static bool getboolfield(lua_State *L, int table,
222                 const char *fieldname, bool &result)
223 {
224         lua_getfield(L, table, fieldname);
225         bool got = false;
226         if(lua_isboolean(L, -1)){
227                 result = lua_toboolean(L, -1);
228                 got = true;
229         }
230         lua_pop(L, 1);
231         return got;
232 }
233
234 static std::string checkstringfield(lua_State *L, int table,
235                 const char *fieldname)
236 {
237         lua_getfield(L, table, fieldname);
238         std::string s = luaL_checkstring(L, -1);
239         lua_pop(L, 1);
240         return s;
241 }
242
243 static std::string getstringfield_default(lua_State *L, int table,
244                 const char *fieldname, const std::string &default_)
245 {
246         std::string result = default_;
247         getstringfield(L, table, fieldname, result);
248         return result;
249 }
250
251 static int getintfield_default(lua_State *L, int table,
252                 const char *fieldname, int default_)
253 {
254         int result = default_;
255         getintfield(L, table, fieldname, result);
256         return result;
257 }
258
259 static float getfloatfield_default(lua_State *L, int table,
260                 const char *fieldname, float default_)
261 {
262         float result = default_;
263         getfloatfield(L, table, fieldname, result);
264         return result;
265 }
266
267 static bool getboolfield_default(lua_State *L, int table,
268                 const char *fieldname, bool default_)
269 {
270         bool result = default_;
271         getboolfield(L, table, fieldname, result);
272         return result;
273 }
274
275 struct EnumString
276 {
277         int num;
278         const char *str;
279 };
280
281 static bool string_to_enum(const EnumString *spec, int &result,
282                 const std::string &str)
283 {
284         const EnumString *esp = spec;
285         while(esp->str){
286                 if(str == std::string(esp->str)){
287                         result = esp->num;
288                         return true;
289                 }
290                 esp++;
291         }
292         return false;
293 }
294
295 /*static bool enum_to_string(const EnumString *spec, std::string &result,
296                 int num)
297 {
298         const EnumString *esp = spec;
299         while(esp){
300                 if(num == esp->num){
301                         result = esp->str;
302                         return true;
303                 }
304                 esp++;
305         }
306         return false;
307 }*/
308
309 static int getenumfield(lua_State *L, int table,
310                 const char *fieldname, const EnumString *spec, int default_)
311 {
312         int result = default_;
313         string_to_enum(spec, result,
314                         getstringfield_default(L, table, fieldname, ""));
315         return result;
316 }
317
318 static void setintfield(lua_State *L, int table,
319                 const char *fieldname, int value)
320 {
321         lua_pushinteger(L, value);
322         if(table < 0)
323                 table -= 1;
324         lua_setfield(L, table, fieldname);
325 }
326
327 static void setfloatfield(lua_State *L, int table,
328                 const char *fieldname, float value)
329 {
330         lua_pushnumber(L, value);
331         if(table < 0)
332                 table -= 1;
333         lua_setfield(L, table, fieldname);
334 }
335
336 static void setboolfield(lua_State *L, int table,
337                 const char *fieldname, bool value)
338 {
339         lua_pushboolean(L, value);
340         if(table < 0)
341                 table -= 1;
342         lua_setfield(L, table, fieldname);
343 }
344
345 static void warn_if_field_exists(lua_State *L, int table,
346                 const char *fieldname, const std::string &message)
347 {
348         lua_getfield(L, table, fieldname);
349         if(!lua_isnil(L, -1)){
350                 infostream<<script_get_backtrace(L)<<std::endl;
351                 infostream<<"WARNING: field \""<<fieldname<<"\": "
352                                 <<message<<std::endl;
353         }
354         lua_pop(L, 1);
355 }
356
357 /*
358         EnumString definitions
359 */
360
361 struct EnumString es_ItemType[] =
362 {
363         {ITEM_NONE, "none"},
364         {ITEM_NODE, "node"},
365         {ITEM_CRAFT, "craft"},
366         {ITEM_TOOL, "tool"},
367         {0, NULL},
368 };
369
370 struct EnumString es_DrawType[] =
371 {
372         {NDT_NORMAL, "normal"},
373         {NDT_AIRLIKE, "airlike"},
374         {NDT_LIQUID, "liquid"},
375         {NDT_FLOWINGLIQUID, "flowingliquid"},
376         {NDT_GLASSLIKE, "glasslike"},
377         {NDT_ALLFACES, "allfaces"},
378         {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
379         {NDT_TORCHLIKE, "torchlike"},
380         {NDT_SIGNLIKE, "signlike"},
381         {NDT_PLANTLIKE, "plantlike"},
382         {NDT_FENCELIKE, "fencelike"},
383         {NDT_RAILLIKE, "raillike"},
384         {0, NULL},
385 };
386
387 struct EnumString es_ContentParamType[] =
388 {
389         {CPT_NONE, "none"},
390         {CPT_LIGHT, "light"},
391         {0, NULL},
392 };
393
394 struct EnumString es_ContentParamType2[] =
395 {
396         {CPT2_NONE, "none"},
397         {CPT2_FULL, "full"},
398         {CPT2_FLOWINGLIQUID, "flowingliquid"},
399         {CPT2_FACEDIR, "facedir"},
400         {CPT2_WALLMOUNTED, "wallmounted"},
401         {0, NULL},
402 };
403
404 struct EnumString es_LiquidType[] =
405 {
406         {LIQUID_NONE, "none"},
407         {LIQUID_FLOWING, "flowing"},
408         {LIQUID_SOURCE, "source"},
409         {0, NULL},
410 };
411
412 struct EnumString es_NodeBoxType[] =
413 {
414         {NODEBOX_REGULAR, "regular"},
415         {NODEBOX_FIXED, "fixed"},
416         {NODEBOX_WALLMOUNTED, "wallmounted"},
417         {0, NULL},
418 };
419
420 /*
421         C struct <-> Lua table converter functions
422 */
423
424 static void push_v3f(lua_State *L, v3f p)
425 {
426         lua_newtable(L);
427         lua_pushnumber(L, p.X);
428         lua_setfield(L, -2, "x");
429         lua_pushnumber(L, p.Y);
430         lua_setfield(L, -2, "y");
431         lua_pushnumber(L, p.Z);
432         lua_setfield(L, -2, "z");
433 }
434
435 static v2s16 read_v2s16(lua_State *L, int index)
436 {
437         v2s16 p;
438         luaL_checktype(L, index, LUA_TTABLE);
439         lua_getfield(L, index, "x");
440         p.X = lua_tonumber(L, -1);
441         lua_pop(L, 1);
442         lua_getfield(L, index, "y");
443         p.Y = lua_tonumber(L, -1);
444         lua_pop(L, 1);
445         return p;
446 }
447
448 static v2f read_v2f(lua_State *L, int index)
449 {
450         v2f p;
451         luaL_checktype(L, index, LUA_TTABLE);
452         lua_getfield(L, index, "x");
453         p.X = lua_tonumber(L, -1);
454         lua_pop(L, 1);
455         lua_getfield(L, index, "y");
456         p.Y = lua_tonumber(L, -1);
457         lua_pop(L, 1);
458         return p;
459 }
460
461 static v3f read_v3f(lua_State *L, int index)
462 {
463         v3f pos;
464         luaL_checktype(L, index, LUA_TTABLE);
465         lua_getfield(L, index, "x");
466         pos.X = lua_tonumber(L, -1);
467         lua_pop(L, 1);
468         lua_getfield(L, index, "y");
469         pos.Y = lua_tonumber(L, -1);
470         lua_pop(L, 1);
471         lua_getfield(L, index, "z");
472         pos.Z = lua_tonumber(L, -1);
473         lua_pop(L, 1);
474         return pos;
475 }
476
477 static v3f check_v3f(lua_State *L, int index)
478 {
479         v3f pos;
480         luaL_checktype(L, index, LUA_TTABLE);
481         lua_getfield(L, index, "x");
482         pos.X = luaL_checknumber(L, -1);
483         lua_pop(L, 1);
484         lua_getfield(L, index, "y");
485         pos.Y = luaL_checknumber(L, -1);
486         lua_pop(L, 1);
487         lua_getfield(L, index, "z");
488         pos.Z = luaL_checknumber(L, -1);
489         lua_pop(L, 1);
490         return pos;
491 }
492
493 static void pushFloatPos(lua_State *L, v3f p)
494 {
495         p /= BS;
496         push_v3f(L, p);
497 }
498
499 static v3f checkFloatPos(lua_State *L, int index)
500 {
501         return check_v3f(L, index) * BS;
502 }
503
504 static void push_v3s16(lua_State *L, v3s16 p)
505 {
506         lua_newtable(L);
507         lua_pushnumber(L, p.X);
508         lua_setfield(L, -2, "x");
509         lua_pushnumber(L, p.Y);
510         lua_setfield(L, -2, "y");
511         lua_pushnumber(L, p.Z);
512         lua_setfield(L, -2, "z");
513 }
514
515 static v3s16 read_v3s16(lua_State *L, int index)
516 {
517         // Correct rounding at <0
518         v3f pf = read_v3f(L, index);
519         return floatToInt(pf, 1.0);
520 }
521
522 static v3s16 check_v3s16(lua_State *L, int index)
523 {
524         // Correct rounding at <0
525         v3f pf = check_v3f(L, index);
526         return floatToInt(pf, 1.0);
527 }
528
529 static void pushnode(lua_State *L, const MapNode &n, INodeDefManager *ndef)
530 {
531         lua_newtable(L);
532         lua_pushstring(L, ndef->get(n).name.c_str());
533         lua_setfield(L, -2, "name");
534         lua_pushnumber(L, n.getParam1());
535         lua_setfield(L, -2, "param1");
536         lua_pushnumber(L, n.getParam2());
537         lua_setfield(L, -2, "param2");
538 }
539
540 static MapNode readnode(lua_State *L, int index, INodeDefManager *ndef)
541 {
542         lua_getfield(L, index, "name");
543         const char *name = luaL_checkstring(L, -1);
544         lua_pop(L, 1);
545         u8 param1;
546         lua_getfield(L, index, "param1");
547         if(lua_isnil(L, -1))
548                 param1 = 0;
549         else
550                 param1 = lua_tonumber(L, -1);
551         lua_pop(L, 1);
552         u8 param2;
553         lua_getfield(L, index, "param2");
554         if(lua_isnil(L, -1))
555                 param2 = 0;
556         else
557                 param2 = lua_tonumber(L, -1);
558         lua_pop(L, 1);
559         return MapNode(ndef, name, param1, param2);
560 }
561
562 static video::SColor readARGB8(lua_State *L, int index)
563 {
564         video::SColor color;
565         luaL_checktype(L, index, LUA_TTABLE);
566         lua_getfield(L, index, "a");
567         if(lua_isnumber(L, -1))
568                 color.setAlpha(lua_tonumber(L, -1));
569         lua_pop(L, 1);
570         lua_getfield(L, index, "r");
571         color.setRed(lua_tonumber(L, -1));
572         lua_pop(L, 1);
573         lua_getfield(L, index, "g");
574         color.setGreen(lua_tonumber(L, -1));
575         lua_pop(L, 1);
576         lua_getfield(L, index, "b");
577         color.setBlue(lua_tonumber(L, -1));
578         lua_pop(L, 1);
579         return color;
580 }
581
582 static core::aabbox3d<f32> read_aabbox3df32(lua_State *L, int index, f32 scale)
583 {
584         core::aabbox3d<f32> box;
585         if(lua_istable(L, -1)){
586                 lua_rawgeti(L, -1, 1);
587                 box.MinEdge.X = lua_tonumber(L, -1) * scale;
588                 lua_pop(L, 1);
589                 lua_rawgeti(L, -1, 2);
590                 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
591                 lua_pop(L, 1);
592                 lua_rawgeti(L, -1, 3);
593                 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
594                 lua_pop(L, 1);
595                 lua_rawgeti(L, -1, 4);
596                 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
597                 lua_pop(L, 1);
598                 lua_rawgeti(L, -1, 5);
599                 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
600                 lua_pop(L, 1);
601                 lua_rawgeti(L, -1, 6);
602                 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
603                 lua_pop(L, 1);
604         }
605         return box;
606 }
607
608 #if 0
609 /*
610         MaterialProperties
611 */
612
613 static MaterialProperties read_material_properties(
614                 lua_State *L, int table)
615 {
616         MaterialProperties prop;
617         prop.diggability = (Diggability)getenumfield(L, -1, "diggability",
618                         es_Diggability, DIGGABLE_NORMAL);
619         getfloatfield(L, -1, "constant_time", prop.constant_time);
620         getfloatfield(L, -1, "weight", prop.weight);
621         getfloatfield(L, -1, "crackiness", prop.crackiness);
622         getfloatfield(L, -1, "crumbliness", prop.crumbliness);
623         getfloatfield(L, -1, "cuttability", prop.cuttability);
624         getfloatfield(L, -1, "flammability", prop.flammability);
625         return prop;
626 }
627 #endif
628
629 /*
630         Groups
631 */
632 static void read_groups(lua_State *L, int index,
633                 std::map<std::string, int> &result)
634 {
635         result.clear();
636         lua_pushnil(L);
637         if(index < 0)
638                 index -= 1;
639         while(lua_next(L, index) != 0){
640                 // key at index -2 and value at index -1
641                 std::string name = luaL_checkstring(L, -2);
642                 int rating = luaL_checkinteger(L, -1);
643                 result[name] = rating;
644                 // removes value, keeps key for next iteration
645                 lua_pop(L, 1);
646         }
647 }
648
649 /*
650         ToolCapabilities
651 */
652
653 static ToolCapabilities read_tool_capabilities(
654                 lua_State *L, int table)
655 {
656         ToolCapabilities toolcap;
657         getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
658         getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
659         lua_getfield(L, table, "groupcaps");
660         if(lua_istable(L, -1)){
661                 int table_groupcaps = lua_gettop(L);
662                 lua_pushnil(L);
663                 while(lua_next(L, table_groupcaps) != 0){
664                         // key at index -2 and value at index -1
665                         std::string groupname = luaL_checkstring(L, -2);
666                         if(lua_istable(L, -1)){
667                                 int table_groupcap = lua_gettop(L);
668                                 // This will be created
669                                 ToolGroupCap groupcap;
670                                 // Read simple parameters
671                                 getfloatfield(L, table_groupcap, "maxwear", groupcap.maxwear);
672                                 getintfield(L, table_groupcap, "maxlevel", groupcap.maxlevel);
673                                 // Read "times" table
674                                 lua_getfield(L, table_groupcap, "times");
675                                 if(lua_istable(L, -1)){
676                                         int table_times = lua_gettop(L);
677                                         lua_pushnil(L);
678                                         while(lua_next(L, table_times) != 0){
679                                                 // key at index -2 and value at index -1
680                                                 int rating = luaL_checkinteger(L, -2);
681                                                 float time = luaL_checknumber(L, -1);
682                                                 groupcap.times[rating] = time;
683                                                 // removes value, keeps key for next iteration
684                                                 lua_pop(L, 1);
685                                         }
686                                 }
687                                 lua_pop(L, 1);
688                                 // Insert groupcap into toolcap
689                                 toolcap.groupcaps[groupname] = groupcap;
690                         }
691                         // removes value, keeps key for next iteration
692                         lua_pop(L, 1);
693                 }
694         }
695         lua_pop(L, 1);
696         return toolcap;
697 }
698
699 static void set_tool_capabilities(lua_State *L, int table,
700                 const ToolCapabilities &toolcap)
701 {
702         setfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
703         setintfield(L, table, "max_drop_level", toolcap.max_drop_level);
704         // Create groupcaps table
705         lua_newtable(L);
706         // For each groupcap
707         for(std::map<std::string, ToolGroupCap>::const_iterator
708                         i = toolcap.groupcaps.begin(); i != toolcap.groupcaps.end(); i++){
709                 // Create groupcap table
710                 lua_newtable(L);
711                 const std::string &name = i->first;
712                 const ToolGroupCap &groupcap = i->second;
713                 // Create subtable "times"
714                 lua_newtable(L);
715                 for(std::map<int, float>::const_iterator
716                                 i = groupcap.times.begin(); i != groupcap.times.end(); i++){
717                         int rating = i->first;
718                         float time = i->second;
719                         lua_pushinteger(L, rating);
720                         lua_pushnumber(L, time);
721                         lua_settable(L, -3);
722                 }
723                 // Set subtable "times"
724                 lua_setfield(L, -2, "times");
725                 // Set simple parameters
726                 setfloatfield(L, -1, "maxwear", groupcap.maxwear);
727                 setintfield(L, -1, "maxlevel", groupcap.maxlevel);
728                 // Insert groupcap table into groupcaps table
729                 lua_setfield(L, -2, name.c_str());
730         }
731         // Set groupcaps table
732         lua_setfield(L, -2, "groupcaps");
733 }
734
735 static void push_tool_capabilities(lua_State *L,
736                 const ToolCapabilities &prop)
737 {
738         lua_newtable(L);
739         set_tool_capabilities(L, -1, prop);
740 }
741
742 /*
743         DigParams
744 */
745
746 static void set_dig_params(lua_State *L, int table,
747                 const DigParams &params)
748 {
749         setboolfield(L, table, "diggable", params.diggable);
750         setfloatfield(L, table, "time", params.time);
751         setintfield(L, table, "wear", params.wear);
752 }
753
754 static void push_dig_params(lua_State *L,
755                 const DigParams &params)
756 {
757         lua_newtable(L);
758         set_dig_params(L, -1, params);
759 }
760
761 /*
762         HitParams
763 */
764
765 static void set_hit_params(lua_State *L, int table,
766                 const HitParams &params)
767 {
768         setintfield(L, table, "hp", params.hp);
769         setintfield(L, table, "wear", params.wear);
770 }
771
772 static void push_hit_params(lua_State *L,
773                 const HitParams &params)
774 {
775         lua_newtable(L);
776         set_hit_params(L, -1, params);
777 }
778
779 /*
780         PointedThing
781 */
782
783 static void push_pointed_thing(lua_State *L, const PointedThing& pointed)
784 {
785         lua_newtable(L);
786         if(pointed.type == POINTEDTHING_NODE)
787         {
788                 lua_pushstring(L, "node");
789                 lua_setfield(L, -2, "type");
790                 push_v3s16(L, pointed.node_undersurface);
791                 lua_setfield(L, -2, "under");
792                 push_v3s16(L, pointed.node_abovesurface);
793                 lua_setfield(L, -2, "above");
794         }
795         else if(pointed.type == POINTEDTHING_OBJECT)
796         {
797                 lua_pushstring(L, "object");
798                 lua_setfield(L, -2, "type");
799                 objectref_get(L, pointed.object_id);
800                 lua_setfield(L, -2, "ref");
801         }
802         else
803         {
804                 lua_pushstring(L, "nothing");
805                 lua_setfield(L, -2, "type");
806         }
807 }
808
809 /*
810         ItemDefinition
811 */
812
813 static ItemDefinition read_item_definition(lua_State *L, int index)
814 {
815         if(index < 0)
816                 index = lua_gettop(L) + 1 + index;
817
818         // Read the item definition
819         ItemDefinition def;
820
821         def.type = (ItemType)getenumfield(L, index, "type",
822                         es_ItemType, ITEM_NONE);
823         getstringfield(L, index, "name", def.name);
824         getstringfield(L, index, "description", def.description);
825         getstringfield(L, index, "inventory_image", def.inventory_image);
826         getstringfield(L, index, "wield_image", def.wield_image);
827
828         lua_getfield(L, index, "wield_scale");
829         if(lua_istable(L, -1)){
830                 def.wield_scale = check_v3f(L, -1);
831         }
832         lua_pop(L, 1);
833
834         def.stack_max = getintfield_default(L, index, "stack_max", def.stack_max);
835         if(def.stack_max == 0)
836                 def.stack_max = 1;
837
838         lua_getfield(L, index, "on_use");
839         def.usable = lua_isfunction(L, -1);
840         lua_pop(L, 1);
841
842         getboolfield(L, index, "liquids_pointable", def.liquids_pointable);
843
844         warn_if_field_exists(L, index, "tool_digging_properties",
845                         "deprecated: use tool_capabilities");
846         
847         lua_getfield(L, index, "tool_capabilities");
848         if(lua_istable(L, -1)){
849                 def.tool_capabilities = new ToolCapabilities(
850                                 read_tool_capabilities(L, -1));
851         }
852
853         // If name is "" (hand), ensure there are ToolCapabilities
854         // because it will be looked up there whenever any other item has
855         // no ToolCapabilities
856         if(def.name == "" && def.tool_capabilities == NULL){
857                 def.tool_capabilities = new ToolCapabilities();
858         }
859
860         lua_getfield(L, index, "groups");
861         read_groups(L, -1, def.groups);
862         lua_pop(L, 1);
863
864         return def;
865 }
866
867 /*
868         ContentFeatures
869 */
870
871 static ContentFeatures read_content_features(lua_State *L, int index)
872 {
873         if(index < 0)
874                 index = lua_gettop(L) + 1 + index;
875
876         ContentFeatures f;
877         /* Name */
878         getstringfield(L, index, "name", f.name);
879
880         /* Groups */
881         lua_getfield(L, index, "groups");
882         read_groups(L, -1, f.groups);
883         lua_pop(L, 1);
884
885         /* Visual definition */
886
887         f.drawtype = (NodeDrawType)getenumfield(L, index, "drawtype", es_DrawType,
888                         NDT_NORMAL);
889         getfloatfield(L, index, "visual_scale", f.visual_scale);
890
891         lua_getfield(L, index, "tile_images");
892         if(lua_istable(L, -1)){
893                 int table = lua_gettop(L);
894                 lua_pushnil(L);
895                 int i = 0;
896                 while(lua_next(L, table) != 0){
897                         // key at index -2 and value at index -1
898                         if(lua_isstring(L, -1))
899                                 f.tname_tiles[i] = lua_tostring(L, -1);
900                         else
901                                 f.tname_tiles[i] = "";
902                         // removes value, keeps key for next iteration
903                         lua_pop(L, 1);
904                         i++;
905                         if(i==6){
906                                 lua_pop(L, 1);
907                                 break;
908                         }
909                 }
910                 // Copy last value to all remaining textures
911                 if(i >= 1){
912                         std::string lastname = f.tname_tiles[i-1];
913                         while(i < 6){
914                                 f.tname_tiles[i] = lastname;
915                                 i++;
916                         }
917                 }
918         }
919         lua_pop(L, 1);
920
921         lua_getfield(L, index, "special_materials");
922         if(lua_istable(L, -1)){
923                 int table = lua_gettop(L);
924                 lua_pushnil(L);
925                 int i = 0;
926                 while(lua_next(L, table) != 0){
927                         // key at index -2 and value at index -1
928                         int smtable = lua_gettop(L);
929                         std::string tname = getstringfield_default(
930                                         L, smtable, "image", "");
931                         bool backface_culling = getboolfield_default(
932                                         L, smtable, "backface_culling", true);
933                         MaterialSpec mspec(tname, backface_culling);
934                         f.mspec_special[i] = mspec;
935                         // removes value, keeps key for next iteration
936                         lua_pop(L, 1);
937                         i++;
938                         if(i==6){
939                                 lua_pop(L, 1);
940                                 break;
941                         }
942                 }
943         }
944         lua_pop(L, 1);
945
946         f.alpha = getintfield_default(L, index, "alpha", 255);
947
948         /* Other stuff */
949         
950         lua_getfield(L, index, "post_effect_color");
951         if(!lua_isnil(L, -1))
952                 f.post_effect_color = readARGB8(L, -1);
953         lua_pop(L, 1);
954
955         f.param_type = (ContentParamType)getenumfield(L, index, "paramtype",
956                         es_ContentParamType, CPT_NONE);
957         f.param_type_2 = (ContentParamType2)getenumfield(L, index, "paramtype2",
958                         es_ContentParamType2, CPT2_NONE);
959
960         // Warn about some deprecated fields
961         warn_if_field_exists(L, index, "wall_mounted",
962                         "deprecated: use paramtype2 = 'wallmounted'");
963         warn_if_field_exists(L, index, "light_propagates",
964                         "deprecated: determined from paramtype");
965         warn_if_field_exists(L, index, "dug_item",
966                         "deprecated: use 'drop' field");
967         warn_if_field_exists(L, index, "extra_dug_item",
968                         "deprecated: use 'drop' field");
969         warn_if_field_exists(L, index, "extra_dug_item_rarity",
970                         "deprecated: use 'drop' field");
971         
972         // True for all ground-like things like stone and mud, false for eg. trees
973         getboolfield(L, index, "is_ground_content", f.is_ground_content);
974         f.light_propagates = (f.param_type == CPT_LIGHT);
975         getboolfield(L, index, "sunlight_propagates", f.sunlight_propagates);
976         // This is used for collision detection.
977         // Also for general solidness queries.
978         getboolfield(L, index, "walkable", f.walkable);
979         // Player can point to these
980         getboolfield(L, index, "pointable", f.pointable);
981         // Player can dig these
982         getboolfield(L, index, "diggable", f.diggable);
983         // Player can climb these
984         getboolfield(L, index, "climbable", f.climbable);
985         // Player can build on these
986         getboolfield(L, index, "buildable_to", f.buildable_to);
987         // Metadata name of node (eg. "furnace")
988         getstringfield(L, index, "metadata_name", f.metadata_name);
989         // Whether the node is non-liquid, source liquid or flowing liquid
990         f.liquid_type = (LiquidType)getenumfield(L, index, "liquidtype",
991                         es_LiquidType, LIQUID_NONE);
992         // If the content is liquid, this is the flowing version of the liquid.
993         getstringfield(L, index, "liquid_alternative_flowing",
994                         f.liquid_alternative_flowing);
995         // If the content is liquid, this is the source version of the liquid.
996         getstringfield(L, index, "liquid_alternative_source",
997                         f.liquid_alternative_source);
998         // Viscosity for fluid flow, ranging from 1 to 7, with
999         // 1 giving almost instantaneous propagation and 7 being
1000         // the slowest possible
1001         f.liquid_viscosity = getintfield_default(L, index,
1002                         "liquid_viscosity", f.liquid_viscosity);
1003         // Amount of light the node emits
1004         f.light_source = getintfield_default(L, index,
1005                         "light_source", f.light_source);
1006         f.damage_per_second = getintfield_default(L, index,
1007                         "damage_per_second", f.damage_per_second);
1008         
1009         lua_getfield(L, index, "selection_box");
1010         if(lua_istable(L, -1)){
1011                 f.selection_box.type = (NodeBoxType)getenumfield(L, -1, "type",
1012                                 es_NodeBoxType, NODEBOX_REGULAR);
1013
1014                 lua_getfield(L, -1, "fixed");
1015                 if(lua_istable(L, -1))
1016                         f.selection_box.fixed = read_aabbox3df32(L, -1, BS);
1017                 lua_pop(L, 1);
1018
1019                 lua_getfield(L, -1, "wall_top");
1020                 if(lua_istable(L, -1))
1021                         f.selection_box.wall_top = read_aabbox3df32(L, -1, BS);
1022                 lua_pop(L, 1);
1023
1024                 lua_getfield(L, -1, "wall_bottom");
1025                 if(lua_istable(L, -1))
1026                         f.selection_box.wall_bottom = read_aabbox3df32(L, -1, BS);
1027                 lua_pop(L, 1);
1028
1029                 lua_getfield(L, -1, "wall_side");
1030                 if(lua_istable(L, -1))
1031                         f.selection_box.wall_side = read_aabbox3df32(L, -1, BS);
1032                 lua_pop(L, 1);
1033         }
1034         lua_pop(L, 1);
1035
1036         // Set to true if paramtype used to be 'facedir_simple'
1037         getboolfield(L, index, "legacy_facedir_simple", f.legacy_facedir_simple);
1038         // Set to true if wall_mounted used to be set to true
1039         getboolfield(L, index, "legacy_wallmounted", f.legacy_wallmounted);
1040
1041         return f;
1042 }
1043
1044 /*
1045         Inventory stuff
1046 */
1047
1048 static ItemStack read_item(lua_State *L, int index);
1049
1050 static void inventory_set_list_from_lua(Inventory *inv, const char *name,
1051                 lua_State *L, int tableindex, int forcesize=-1)
1052 {
1053         dstream<<"inventory_set_list_from_lua\n";
1054         if(tableindex < 0)
1055                 tableindex = lua_gettop(L) + 1 + tableindex;
1056         // If nil, delete list
1057         if(lua_isnil(L, tableindex)){
1058                 inv->deleteList(name);
1059                 return;
1060         }
1061         // Otherwise set list
1062         std::vector<ItemStack> items;
1063         luaL_checktype(L, tableindex, LUA_TTABLE);
1064         lua_pushnil(L);
1065         while(lua_next(L, tableindex) != 0){
1066                 // key at index -2 and value at index -1
1067                 items.push_back(read_item(L, -1));
1068                 // removes value, keeps key for next iteration
1069                 lua_pop(L, 1);
1070         }
1071         int listsize = (forcesize != -1) ? forcesize : items.size();
1072         InventoryList *invlist = inv->addList(name, listsize);
1073         int index = 0;
1074         for(std::vector<ItemStack>::const_iterator
1075                         i = items.begin(); i != items.end(); i++){
1076                 if(forcesize != -1 && index == forcesize)
1077                         break;
1078                 invlist->changeItem(index, *i);
1079                 index++;
1080         }
1081         while(forcesize != -1 && index < forcesize){
1082                 invlist->deleteItem(index);
1083                 index++;
1084         }
1085         dstream<<"inventory_set_list_from_lua done\n";
1086 }
1087
1088 static void inventory_get_list_to_lua(Inventory *inv, const char *name,
1089                 lua_State *L)
1090 {
1091         InventoryList *invlist = inv->getList(name);
1092         if(invlist == NULL){
1093                 lua_pushnil(L);
1094                 return;
1095         }
1096         // Get the table insert function
1097         lua_getglobal(L, "table");
1098         lua_getfield(L, -1, "insert");
1099         int table_insert = lua_gettop(L);
1100         // Create and fill table
1101         lua_newtable(L);
1102         int table = lua_gettop(L);
1103         for(u32 i=0; i<invlist->getSize(); i++){
1104                 ItemStack item = invlist->getItem(i);
1105                 lua_pushvalue(L, table_insert);
1106                 lua_pushvalue(L, table);
1107                 lua_pushstring(L, item.getItemString().c_str());
1108                 if(lua_pcall(L, 2, 0, 0))
1109                         script_error(L, "error: %s", lua_tostring(L, -1));
1110         }
1111 }
1112
1113 /*
1114         Helpful macros for userdata classes
1115 */
1116
1117 #define method(class, name) {#name, class::l_##name}
1118
1119 /*
1120         LuaItemStack
1121 */
1122
1123 class LuaItemStack
1124 {
1125 private:
1126         ItemStack m_stack;
1127
1128         static const char className[];
1129         static const luaL_reg methods[];
1130
1131         // Exported functions
1132         
1133         // garbage collector
1134         static int gc_object(lua_State *L)
1135         {
1136                 LuaItemStack *o = *(LuaItemStack **)(lua_touserdata(L, 1));
1137                 delete o;
1138                 return 0;
1139         }
1140
1141         // is_empty(self) -> true/false
1142         static int l_is_empty(lua_State *L)
1143         {
1144                 LuaItemStack *o = checkobject(L, 1);
1145                 ItemStack &item = o->m_stack;
1146                 lua_pushboolean(L, item.empty());
1147                 return 1;
1148         }
1149
1150         // get_name(self) -> string
1151         static int l_get_name(lua_State *L)
1152         {
1153                 LuaItemStack *o = checkobject(L, 1);
1154                 ItemStack &item = o->m_stack;
1155                 lua_pushstring(L, item.name.c_str());
1156                 return 1;
1157         }
1158
1159         // get_count(self) -> number
1160         static int l_get_count(lua_State *L)
1161         {
1162                 LuaItemStack *o = checkobject(L, 1);
1163                 ItemStack &item = o->m_stack;
1164                 lua_pushinteger(L, item.count);
1165                 return 1;
1166         }
1167
1168         // get_wear(self) -> number
1169         static int l_get_wear(lua_State *L)
1170         {
1171                 LuaItemStack *o = checkobject(L, 1);
1172                 ItemStack &item = o->m_stack;
1173                 lua_pushinteger(L, item.wear);
1174                 return 1;
1175         }
1176
1177         // get_metadata(self) -> string
1178         static int l_get_metadata(lua_State *L)
1179         {
1180                 LuaItemStack *o = checkobject(L, 1);
1181                 ItemStack &item = o->m_stack;
1182                 lua_pushlstring(L, item.metadata.c_str(), item.metadata.size());
1183                 return 1;
1184         }
1185
1186         // clear(self) -> true
1187         static int l_clear(lua_State *L)
1188         {
1189                 LuaItemStack *o = checkobject(L, 1);
1190                 o->m_stack.clear();
1191                 lua_pushboolean(L, true);
1192                 return 1;
1193         }
1194
1195         // replace(self, itemstack or itemstring or table or nil) -> true
1196         static int l_replace(lua_State *L)
1197         {
1198                 LuaItemStack *o = checkobject(L, 1);
1199                 o->m_stack = read_item(L, 2);
1200                 lua_pushboolean(L, true);
1201                 return 1;
1202         }
1203
1204         // to_string(self) -> string
1205         static int l_to_string(lua_State *L)
1206         {
1207                 LuaItemStack *o = checkobject(L, 1);
1208                 std::string itemstring = o->m_stack.getItemString();
1209                 lua_pushstring(L, itemstring.c_str());
1210                 return 1;
1211         }
1212
1213         // to_table(self) -> table or nil
1214         static int l_to_table(lua_State *L)
1215         {
1216                 LuaItemStack *o = checkobject(L, 1);
1217                 const ItemStack &item = o->m_stack;
1218                 if(item.empty())
1219                 {
1220                         lua_pushnil(L);
1221                 }
1222                 else
1223                 {
1224                         lua_newtable(L);
1225                         lua_pushstring(L, item.name.c_str());
1226                         lua_setfield(L, -2, "name");
1227                         lua_pushinteger(L, item.count);
1228                         lua_setfield(L, -2, "count");
1229                         lua_pushinteger(L, item.wear);
1230                         lua_setfield(L, -2, "wear");
1231                         lua_pushlstring(L, item.metadata.c_str(), item.metadata.size());
1232                         lua_setfield(L, -2, "metadata");
1233                 }
1234                 return 1;
1235         }
1236
1237         // get_stack_max(self) -> number
1238         static int l_get_stack_max(lua_State *L)
1239         {
1240                 LuaItemStack *o = checkobject(L, 1);
1241                 ItemStack &item = o->m_stack;
1242                 lua_pushinteger(L, item.getStackMax(get_server(L)->idef()));
1243                 return 1;
1244         }
1245
1246         // get_free_space(self) -> number
1247         static int l_get_free_space(lua_State *L)
1248         {
1249                 LuaItemStack *o = checkobject(L, 1);
1250                 ItemStack &item = o->m_stack;
1251                 lua_pushinteger(L, item.freeSpace(get_server(L)->idef()));
1252                 return 1;
1253         }
1254
1255         // is_known(self) -> true/false
1256         // Checks if the item is defined.
1257         static int l_is_known(lua_State *L)
1258         {
1259                 LuaItemStack *o = checkobject(L, 1);
1260                 ItemStack &item = o->m_stack;
1261                 bool is_known = item.isKnown(get_server(L)->idef());
1262                 lua_pushboolean(L, is_known);
1263                 return 1;
1264         }
1265
1266         // get_definition(self) -> table
1267         // Returns the item definition table from minetest.registered_items,
1268         // or a fallback one (name="unknown")
1269         static int l_get_definition(lua_State *L)
1270         {
1271                 LuaItemStack *o = checkobject(L, 1);
1272                 ItemStack &item = o->m_stack;
1273
1274                 // Get minetest.registered_items[name]
1275                 lua_getglobal(L, "minetest");
1276                 lua_getfield(L, -1, "registered_items");
1277                 luaL_checktype(L, -1, LUA_TTABLE);
1278                 lua_getfield(L, -1, item.name.c_str());
1279                 if(lua_isnil(L, -1))
1280                 {
1281                         lua_pop(L, 1);
1282                         lua_getfield(L, -1, "unknown");
1283                 }
1284                 return 1;
1285         }
1286
1287         // get_tool_capabilities(self) -> table
1288         // Returns the effective tool digging properties.
1289         // Returns those of the hand ("") if this item has none associated.
1290         static int l_get_tool_capabilities(lua_State *L)
1291         {
1292                 LuaItemStack *o = checkobject(L, 1);
1293                 ItemStack &item = o->m_stack;
1294                 const ToolCapabilities &prop =
1295                         item.getToolCapabilities(get_server(L)->idef());
1296                 push_tool_capabilities(L, prop);
1297                 return 1;
1298         }
1299
1300         // add_wear(self, amount) -> true/false
1301         // The range for "amount" is [0,65535]. Wear is only added if the item
1302         // is a tool. Adding wear might destroy the item.
1303         // Returns true if the item is (or was) a tool.
1304         static int l_add_wear(lua_State *L)
1305         {
1306                 LuaItemStack *o = checkobject(L, 1);
1307                 ItemStack &item = o->m_stack;
1308                 int amount = lua_tointeger(L, 2);
1309                 bool result = item.addWear(amount, get_server(L)->idef());
1310                 lua_pushboolean(L, result);
1311                 return 1;
1312         }
1313
1314         // add_item(self, itemstack or itemstring or table or nil) -> itemstack
1315         // Returns leftover item stack
1316         static int l_add_item(lua_State *L)
1317         {
1318                 LuaItemStack *o = checkobject(L, 1);
1319                 ItemStack &item = o->m_stack;
1320                 ItemStack newitem = read_item(L, 2);
1321                 ItemStack leftover = item.addItem(newitem, get_server(L)->idef());
1322                 create(L, leftover);
1323                 return 1;
1324         }
1325
1326         // item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
1327         // First return value is true iff the new item fits fully into the stack
1328         // Second return value is the would-be-left-over item stack
1329         static int l_item_fits(lua_State *L)
1330         {
1331                 LuaItemStack *o = checkobject(L, 1);
1332                 ItemStack &item = o->m_stack;
1333                 ItemStack newitem = read_item(L, 2);
1334                 ItemStack restitem;
1335                 bool fits = item.itemFits(newitem, &restitem, get_server(L)->idef());
1336                 lua_pushboolean(L, fits);  // first return value
1337                 create(L, restitem);       // second return value
1338                 return 2;
1339         }
1340
1341         // take_item(self, takecount=1) -> itemstack
1342         static int l_take_item(lua_State *L)
1343         {
1344                 LuaItemStack *o = checkobject(L, 1);
1345                 ItemStack &item = o->m_stack;
1346                 u32 takecount = 1;
1347                 if(!lua_isnone(L, 2))
1348                         takecount = lua_tointeger(L, 2);
1349                 ItemStack taken = item.takeItem(takecount);
1350                 create(L, taken);
1351                 return 1;
1352         }
1353
1354         // peek_item(self, peekcount=1) -> itemstack
1355         static int l_peek_item(lua_State *L)
1356         {
1357                 LuaItemStack *o = checkobject(L, 1);
1358                 ItemStack &item = o->m_stack;
1359                 u32 peekcount = 1;
1360                 if(!lua_isnone(L, 2))
1361                         peekcount = lua_tointeger(L, 2);
1362                 ItemStack peekaboo = item.peekItem(peekcount);
1363                 create(L, peekaboo);
1364                 return 1;
1365         }
1366
1367 public:
1368         LuaItemStack(const ItemStack &item):
1369                 m_stack(item)
1370         {
1371         }
1372
1373         ~LuaItemStack()
1374         {
1375         }
1376
1377         const ItemStack& getItem() const
1378         {
1379                 return m_stack;
1380         }
1381         ItemStack& getItem()
1382         {
1383                 return m_stack;
1384         }
1385         
1386         // LuaItemStack(itemstack or itemstring or table or nil)
1387         // Creates an LuaItemStack and leaves it on top of stack
1388         static int create_object(lua_State *L)
1389         {
1390                 ItemStack item = read_item(L, 1);
1391                 LuaItemStack *o = new LuaItemStack(item);
1392                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1393                 luaL_getmetatable(L, className);
1394                 lua_setmetatable(L, -2);
1395                 return 1;
1396         }
1397         // Not callable from Lua
1398         static int create(lua_State *L, const ItemStack &item)
1399         {
1400                 LuaItemStack *o = new LuaItemStack(item);
1401                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1402                 luaL_getmetatable(L, className);
1403                 lua_setmetatable(L, -2);
1404                 return 1;
1405         }
1406
1407         static LuaItemStack* checkobject(lua_State *L, int narg)
1408         {
1409                 luaL_checktype(L, narg, LUA_TUSERDATA);
1410                 void *ud = luaL_checkudata(L, narg, className);
1411                 if(!ud) luaL_typerror(L, narg, className);
1412                 return *(LuaItemStack**)ud;  // unbox pointer
1413         }
1414
1415         static void Register(lua_State *L)
1416         {
1417                 lua_newtable(L);
1418                 int methodtable = lua_gettop(L);
1419                 luaL_newmetatable(L, className);
1420                 int metatable = lua_gettop(L);
1421
1422                 lua_pushliteral(L, "__metatable");
1423                 lua_pushvalue(L, methodtable);
1424                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
1425
1426                 lua_pushliteral(L, "__index");
1427                 lua_pushvalue(L, methodtable);
1428                 lua_settable(L, metatable);
1429
1430                 lua_pushliteral(L, "__gc");
1431                 lua_pushcfunction(L, gc_object);
1432                 lua_settable(L, metatable);
1433
1434                 lua_pop(L, 1);  // drop metatable
1435
1436                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
1437                 lua_pop(L, 1);  // drop methodtable
1438
1439                 // Can be created from Lua (LuaItemStack(itemstack or itemstring or table or nil))
1440                 lua_register(L, className, create_object);
1441         }
1442 };
1443 const char LuaItemStack::className[] = "ItemStack";
1444 const luaL_reg LuaItemStack::methods[] = {
1445         method(LuaItemStack, is_empty),
1446         method(LuaItemStack, get_name),
1447         method(LuaItemStack, get_count),
1448         method(LuaItemStack, get_wear),
1449         method(LuaItemStack, get_metadata),
1450         method(LuaItemStack, clear),
1451         method(LuaItemStack, replace),
1452         method(LuaItemStack, to_string),
1453         method(LuaItemStack, to_table),
1454         method(LuaItemStack, get_stack_max),
1455         method(LuaItemStack, get_free_space),
1456         method(LuaItemStack, is_known),
1457         method(LuaItemStack, get_definition),
1458         method(LuaItemStack, get_tool_capabilities),
1459         method(LuaItemStack, add_wear),
1460         method(LuaItemStack, add_item),
1461         method(LuaItemStack, item_fits),
1462         method(LuaItemStack, take_item),
1463         method(LuaItemStack, peek_item),
1464         {0,0}
1465 };
1466
1467 static ItemStack read_item(lua_State *L, int index)
1468 {
1469         if(index < 0)
1470                 index = lua_gettop(L) + 1 + index;
1471
1472         if(lua_isnil(L, index))
1473         {
1474                 return ItemStack();
1475         }
1476         else if(lua_isuserdata(L, index))
1477         {
1478                 // Convert from LuaItemStack
1479                 LuaItemStack *o = LuaItemStack::checkobject(L, index);
1480                 return o->getItem();
1481         }
1482         else if(lua_isstring(L, index))
1483         {
1484                 // Convert from itemstring
1485                 std::string itemstring = lua_tostring(L, index);
1486                 IItemDefManager *idef = get_server(L)->idef();
1487                 try
1488                 {
1489                         ItemStack item;
1490                         item.deSerialize(itemstring, idef);
1491                         return item;
1492                 }
1493                 catch(SerializationError &e)
1494                 {
1495                         infostream<<"WARNING: unable to create item from itemstring"
1496                                         <<": "<<itemstring<<std::endl;
1497                         return ItemStack();
1498                 }
1499         }
1500         else if(lua_istable(L, index))
1501         {
1502                 // Convert from table
1503                 IItemDefManager *idef = get_server(L)->idef();
1504                 std::string name = getstringfield_default(L, index, "name", "");
1505                 int count = getintfield_default(L, index, "count", 1);
1506                 int wear = getintfield_default(L, index, "wear", 0);
1507                 std::string metadata = getstringfield_default(L, index, "metadata", "");
1508                 return ItemStack(name, count, wear, metadata, idef);
1509         }
1510         else
1511         {
1512                 throw LuaError(L, "Expecting itemstack, itemstring, table or nil");
1513         }
1514 }
1515
1516 /*
1517         InvRef
1518 */
1519
1520 class InvRef
1521 {
1522 private:
1523         InventoryLocation m_loc;
1524
1525         static const char className[];
1526         static const luaL_reg methods[];
1527
1528         static InvRef *checkobject(lua_State *L, int narg)
1529         {
1530                 luaL_checktype(L, narg, LUA_TUSERDATA);
1531                 void *ud = luaL_checkudata(L, narg, className);
1532                 if(!ud) luaL_typerror(L, narg, className);
1533                 return *(InvRef**)ud;  // unbox pointer
1534         }
1535         
1536         static Inventory* getinv(lua_State *L, InvRef *ref)
1537         {
1538                 return get_server(L)->getInventory(ref->m_loc);
1539         }
1540
1541         static InventoryList* getlist(lua_State *L, InvRef *ref,
1542                         const char *listname)
1543         {
1544                 Inventory *inv = getinv(L, ref);
1545                 if(!inv)
1546                         return NULL;
1547                 return inv->getList(listname);
1548         }
1549
1550         static void reportInventoryChange(lua_State *L, InvRef *ref)
1551         {
1552                 // Inform other things that the inventory has changed
1553                 get_server(L)->setInventoryModified(ref->m_loc);
1554         }
1555         
1556         // Exported functions
1557         
1558         // garbage collector
1559         static int gc_object(lua_State *L) {
1560                 InvRef *o = *(InvRef **)(lua_touserdata(L, 1));
1561                 delete o;
1562                 return 0;
1563         }
1564
1565         // get_size(self, listname)
1566         static int l_get_size(lua_State *L)
1567         {
1568                 InvRef *ref = checkobject(L, 1);
1569                 const char *listname = luaL_checkstring(L, 2);
1570                 InventoryList *list = getlist(L, ref, listname);
1571                 if(list){
1572                         lua_pushinteger(L, list->getSize());
1573                 } else {
1574                         lua_pushinteger(L, 0);
1575                 }
1576                 return 1;
1577         }
1578
1579         // set_size(self, listname, size)
1580         static int l_set_size(lua_State *L)
1581         {
1582                 InvRef *ref = checkobject(L, 1);
1583                 const char *listname = luaL_checkstring(L, 2);
1584                 int newsize = luaL_checknumber(L, 3);
1585                 Inventory *inv = getinv(L, ref);
1586                 if(newsize == 0){
1587                         inv->deleteList(listname);
1588                         reportInventoryChange(L, ref);
1589                         return 0;
1590                 }
1591                 InventoryList *list = inv->getList(listname);
1592                 if(list){
1593                         list->setSize(newsize);
1594                 } else {
1595                         list = inv->addList(listname, newsize);
1596                 }
1597                 reportInventoryChange(L, ref);
1598                 return 0;
1599         }
1600
1601         // get_stack(self, listname, i) -> itemstack
1602         static int l_get_stack(lua_State *L)
1603         {
1604                 InvRef *ref = checkobject(L, 1);
1605                 const char *listname = luaL_checkstring(L, 2);
1606                 int i = luaL_checknumber(L, 3) - 1;
1607                 InventoryList *list = getlist(L, ref, listname);
1608                 ItemStack item;
1609                 if(list != NULL && i >= 0 && i < (int) list->getSize())
1610                         item = list->getItem(i);
1611                 LuaItemStack::create(L, item);
1612                 return 1;
1613         }
1614
1615         // set_stack(self, listname, i, stack) -> true/false
1616         static int l_set_stack(lua_State *L)
1617         {
1618                 InvRef *ref = checkobject(L, 1);
1619                 const char *listname = luaL_checkstring(L, 2);
1620                 int i = luaL_checknumber(L, 3) - 1;
1621                 ItemStack newitem = read_item(L, 4);
1622                 InventoryList *list = getlist(L, ref, listname);
1623                 if(list != NULL && i >= 0 && i < (int) list->getSize()){
1624                         list->changeItem(i, newitem);
1625                         reportInventoryChange(L, ref);
1626                         lua_pushboolean(L, true);
1627                 } else {
1628                         lua_pushboolean(L, false);
1629                 }
1630                 return 1;
1631         }
1632
1633         // get_list(self, listname) -> list or nil
1634         static int l_get_list(lua_State *L)
1635         {
1636                 InvRef *ref = checkobject(L, 1);
1637                 const char *listname = luaL_checkstring(L, 2);
1638                 Inventory *inv = getinv(L, ref);
1639                 inventory_get_list_to_lua(inv, listname, L);
1640                 return 1;
1641         }
1642
1643         // set_list(self, listname, list)
1644         static int l_set_list(lua_State *L)
1645         {
1646                 InvRef *ref = checkobject(L, 1);
1647                 const char *listname = luaL_checkstring(L, 2);
1648                 Inventory *inv = getinv(L, ref);
1649                 InventoryList *list = inv->getList(listname);
1650                 if(list)
1651                         inventory_set_list_from_lua(inv, listname, L, 3,
1652                                         list->getSize());
1653                 else
1654                         inventory_set_list_from_lua(inv, listname, L, 3);
1655                 reportInventoryChange(L, ref);
1656                 return 0;
1657         }
1658
1659         // add_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
1660         // Returns the leftover stack
1661         static int l_add_item(lua_State *L)
1662         {
1663                 InvRef *ref = checkobject(L, 1);
1664                 const char *listname = luaL_checkstring(L, 2);
1665                 ItemStack item = read_item(L, 3);
1666                 InventoryList *list = getlist(L, ref, listname);
1667                 if(list){
1668                         ItemStack leftover = list->addItem(item);
1669                         if(leftover.count != item.count)
1670                                 reportInventoryChange(L, ref);
1671                         LuaItemStack::create(L, leftover);
1672                 } else {
1673                         LuaItemStack::create(L, item);
1674                 }
1675                 return 1;
1676         }
1677
1678         // room_for_item(self, listname, itemstack or itemstring or table or nil) -> true/false
1679         // Returns true if the item completely fits into the list
1680         static int l_room_for_item(lua_State *L)
1681         {
1682                 InvRef *ref = checkobject(L, 1);
1683                 const char *listname = luaL_checkstring(L, 2);
1684                 ItemStack item = read_item(L, 3);
1685                 InventoryList *list = getlist(L, ref, listname);
1686                 if(list){
1687                         lua_pushboolean(L, list->roomForItem(item));
1688                 } else {
1689                         lua_pushboolean(L, false);
1690                 }
1691                 return 1;
1692         }
1693
1694         // contains_item(self, listname, itemstack or itemstring or table or nil) -> true/false
1695         // Returns true if the list contains the given count of the given item name
1696         static int l_contains_item(lua_State *L)
1697         {
1698                 InvRef *ref = checkobject(L, 1);
1699                 const char *listname = luaL_checkstring(L, 2);
1700                 ItemStack item = read_item(L, 3);
1701                 InventoryList *list = getlist(L, ref, listname);
1702                 if(list){
1703                         lua_pushboolean(L, list->containsItem(item));
1704                 } else {
1705                         lua_pushboolean(L, false);
1706                 }
1707                 return 1;
1708         }
1709
1710         // remove_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
1711         // Returns the items that were actually removed
1712         static int l_remove_item(lua_State *L)
1713         {
1714                 InvRef *ref = checkobject(L, 1);
1715                 const char *listname = luaL_checkstring(L, 2);
1716                 ItemStack item = read_item(L, 3);
1717                 InventoryList *list = getlist(L, ref, listname);
1718                 if(list){
1719                         ItemStack removed = list->removeItem(item);
1720                         if(!removed.empty())
1721                                 reportInventoryChange(L, ref);
1722                         LuaItemStack::create(L, removed);
1723                 } else {
1724                         LuaItemStack::create(L, ItemStack());
1725                 }
1726                 return 1;
1727         }
1728
1729 public:
1730         InvRef(const InventoryLocation &loc):
1731                 m_loc(loc)
1732         {
1733         }
1734
1735         ~InvRef()
1736         {
1737         }
1738
1739         // Creates an InvRef and leaves it on top of stack
1740         // Not callable from Lua; all references are created on the C side.
1741         static void create(lua_State *L, const InventoryLocation &loc)
1742         {
1743                 InvRef *o = new InvRef(loc);
1744                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1745                 luaL_getmetatable(L, className);
1746                 lua_setmetatable(L, -2);
1747         }
1748         static void createPlayer(lua_State *L, Player *player)
1749         {
1750                 InventoryLocation loc;
1751                 loc.setPlayer(player->getName());
1752                 create(L, loc);
1753         }
1754         static void createNodeMeta(lua_State *L, v3s16 p)
1755         {
1756                 InventoryLocation loc;
1757                 loc.setNodeMeta(p);
1758                 create(L, loc);
1759         }
1760
1761         static void Register(lua_State *L)
1762         {
1763                 lua_newtable(L);
1764                 int methodtable = lua_gettop(L);
1765                 luaL_newmetatable(L, className);
1766                 int metatable = lua_gettop(L);
1767
1768                 lua_pushliteral(L, "__metatable");
1769                 lua_pushvalue(L, methodtable);
1770                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
1771
1772                 lua_pushliteral(L, "__index");
1773                 lua_pushvalue(L, methodtable);
1774                 lua_settable(L, metatable);
1775
1776                 lua_pushliteral(L, "__gc");
1777                 lua_pushcfunction(L, gc_object);
1778                 lua_settable(L, metatable);
1779
1780                 lua_pop(L, 1);  // drop metatable
1781
1782                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
1783                 lua_pop(L, 1);  // drop methodtable
1784
1785                 // Cannot be created from Lua
1786                 //lua_register(L, className, create_object);
1787         }
1788 };
1789 const char InvRef::className[] = "InvRef";
1790 const luaL_reg InvRef::methods[] = {
1791         method(InvRef, get_size),
1792         method(InvRef, set_size),
1793         method(InvRef, get_stack),
1794         method(InvRef, set_stack),
1795         method(InvRef, get_list),
1796         method(InvRef, set_list),
1797         method(InvRef, add_item),
1798         method(InvRef, room_for_item),
1799         method(InvRef, contains_item),
1800         method(InvRef, remove_item),
1801         {0,0}
1802 };
1803
1804 /*
1805         NodeMetaRef
1806 */
1807
1808 class NodeMetaRef
1809 {
1810 private:
1811         v3s16 m_p;
1812         ServerEnvironment *m_env;
1813
1814         static const char className[];
1815         static const luaL_reg methods[];
1816
1817         static NodeMetaRef *checkobject(lua_State *L, int narg)
1818         {
1819                 luaL_checktype(L, narg, LUA_TUSERDATA);
1820                 void *ud = luaL_checkudata(L, narg, className);
1821                 if(!ud) luaL_typerror(L, narg, className);
1822                 return *(NodeMetaRef**)ud;  // unbox pointer
1823         }
1824         
1825         static NodeMetadata* getmeta(NodeMetaRef *ref)
1826         {
1827                 NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
1828                 return meta;
1829         }
1830
1831         /*static IGenericNodeMetadata* getgenericmeta(NodeMetaRef *ref)
1832         {
1833                 NodeMetadata *meta = getmeta(ref);
1834                 if(meta == NULL)
1835                         return NULL;
1836                 if(meta->typeId() != NODEMETA_GENERIC)
1837                         return NULL;
1838                 return (IGenericNodeMetadata*)meta;
1839         }*/
1840
1841         static void reportMetadataChange(NodeMetaRef *ref)
1842         {
1843                 // Inform other things that the metadata has changed
1844                 v3s16 blockpos = getNodeBlockPos(ref->m_p);
1845                 MapEditEvent event;
1846                 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
1847                 event.p = blockpos;
1848                 ref->m_env->getMap().dispatchEvent(&event);
1849                 // Set the block to be saved
1850                 MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
1851                 if(block)
1852                         block->raiseModified(MOD_STATE_WRITE_NEEDED,
1853                                         "NodeMetaRef::reportMetadataChange");
1854         }
1855         
1856         // Exported functions
1857         
1858         // garbage collector
1859         static int gc_object(lua_State *L) {
1860                 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
1861                 delete o;
1862                 return 0;
1863         }
1864
1865         // get_type(self)
1866         static int l_get_type(lua_State *L)
1867         {
1868                 NodeMetaRef *ref = checkobject(L, 1);
1869                 NodeMetadata *meta = getmeta(ref);
1870                 if(meta == NULL){
1871                         lua_pushnil(L);
1872                         return 1;
1873                 }
1874                 // Do it
1875                 lua_pushstring(L, meta->typeName());
1876                 return 1;
1877         }
1878
1879         // allows_text_input(self)
1880         static int l_allows_text_input(lua_State *L)
1881         {
1882                 NodeMetaRef *ref = checkobject(L, 1);
1883                 NodeMetadata *meta = getmeta(ref);
1884                 if(meta == NULL) return 0;
1885                 // Do it
1886                 lua_pushboolean(L, meta->allowsTextInput());
1887                 return 1;
1888         }
1889
1890         // set_text(self, text)
1891         static int l_set_text(lua_State *L)
1892         {
1893                 NodeMetaRef *ref = checkobject(L, 1);
1894                 NodeMetadata *meta = getmeta(ref);
1895                 if(meta == NULL) return 0;
1896                 // Do it
1897                 std::string text = luaL_checkstring(L, 2);
1898                 meta->setText(text);
1899                 reportMetadataChange(ref);
1900                 return 0;
1901         }
1902
1903         // get_text(self)
1904         static int l_get_text(lua_State *L)
1905         {
1906                 NodeMetaRef *ref = checkobject(L, 1);
1907                 NodeMetadata *meta = getmeta(ref);
1908                 if(meta == NULL) return 0;
1909                 // Do it
1910                 std::string text = meta->getText();
1911                 lua_pushstring(L, text.c_str());
1912                 return 1;
1913         }
1914
1915         // get_owner(self)
1916         static int l_get_owner(lua_State *L)
1917         {
1918                 NodeMetaRef *ref = checkobject(L, 1);
1919                 NodeMetadata *meta = getmeta(ref);
1920                 if(meta == NULL) return 0;
1921                 // Do it
1922                 std::string owner = meta->getOwner();
1923                 lua_pushstring(L, owner.c_str());
1924                 return 1;
1925         }
1926
1927         // set_owner(self, string)
1928         static int l_set_owner(lua_State *L)
1929         {
1930                 NodeMetaRef *ref = checkobject(L, 1);
1931                 NodeMetadata *meta = getmeta(ref);
1932                 if(meta == NULL) return 0;
1933                 // Do it
1934                 std::string owner = luaL_checkstring(L, 2);
1935                 meta->setOwner(owner);
1936                 reportMetadataChange(ref);
1937                 return 1;
1938         }
1939
1940         // get_allow_removal(self)
1941         static int l_get_allow_removal(lua_State *L)
1942         {
1943                 NodeMetaRef *ref = checkobject(L, 1);
1944                 NodeMetadata *meta = getmeta(ref);
1945                 if(meta == NULL){
1946                         lua_pushboolean(L, true);
1947                         return 1;
1948                 }
1949                 // Do it
1950                 lua_pushboolean(L, !meta->nodeRemovalDisabled());
1951                 return 1;
1952         }
1953
1954         /* IGenericNodeMetadata interface */
1955         
1956         // set_infotext(self, text)
1957         static int l_set_infotext(lua_State *L)
1958         {
1959                 NodeMetaRef *ref = checkobject(L, 1);
1960                 NodeMetadata *meta = getmeta(ref);
1961                 if(meta == NULL) return 0;
1962                 // Do it
1963                 std::string text = luaL_checkstring(L, 2);
1964                 meta->setInfoText(text);
1965                 reportMetadataChange(ref);
1966                 return 0;
1967         }
1968
1969         // get_inventory(self)
1970         static int l_get_inventory(lua_State *L)
1971         {
1972                 NodeMetaRef *ref = checkobject(L, 1);
1973                 NodeMetadata *meta = getmeta(ref);
1974                 if(meta == NULL) return 0;
1975                 // Do it
1976                 InvRef::createNodeMeta(L, ref->m_p);
1977                 return 1;
1978         }
1979
1980         // set_inventory_draw_spec(self, text)
1981         static int l_set_inventory_draw_spec(lua_State *L)
1982         {
1983                 NodeMetaRef *ref = checkobject(L, 1);
1984                 NodeMetadata *meta = getmeta(ref);
1985                 if(meta == NULL) return 0;
1986                 // Do it
1987                 std::string text = luaL_checkstring(L, 2);
1988                 meta->setInventoryDrawSpec(text);
1989                 reportMetadataChange(ref);
1990                 return 0;
1991         }
1992
1993         // set_allow_text_input(self, text)
1994         static int l_set_allow_text_input(lua_State *L)
1995         {
1996                 NodeMetaRef *ref = checkobject(L, 1);
1997                 NodeMetadata *meta = getmeta(ref);
1998                 if(meta == NULL) return 0;
1999                 // Do it
2000                 bool b = lua_toboolean(L, 2);
2001                 meta->setAllowTextInput(b);
2002                 reportMetadataChange(ref);
2003                 return 0;
2004         }
2005
2006         // set_allow_removal(self, text)
2007         static int l_set_allow_removal(lua_State *L)
2008         {
2009                 NodeMetaRef *ref = checkobject(L, 1);
2010                 NodeMetadata *meta = getmeta(ref);
2011                 if(meta == NULL) return 0;
2012                 // Do it
2013                 bool b = lua_toboolean(L, 2);
2014                 meta->setRemovalDisabled(!b);
2015                 reportMetadataChange(ref);
2016                 return 0;
2017         }
2018
2019         // set_enforce_owner(self, text)
2020         static int l_set_enforce_owner(lua_State *L)
2021         {
2022                 NodeMetaRef *ref = checkobject(L, 1);
2023                 NodeMetadata *meta = getmeta(ref);
2024                 if(meta == NULL) return 0;
2025                 // Do it
2026                 bool b = lua_toboolean(L, 2);
2027                 meta->setEnforceOwner(b);
2028                 reportMetadataChange(ref);
2029                 return 0;
2030         }
2031
2032         // is_inventory_modified(self)
2033         static int l_is_inventory_modified(lua_State *L)
2034         {
2035                 NodeMetaRef *ref = checkobject(L, 1);
2036                 NodeMetadata *meta = getmeta(ref);
2037                 if(meta == NULL) return 0;
2038                 // Do it
2039                 lua_pushboolean(L, meta->isInventoryModified());
2040                 return 1;
2041         }
2042
2043         // reset_inventory_modified(self)
2044         static int l_reset_inventory_modified(lua_State *L)
2045         {
2046                 NodeMetaRef *ref = checkobject(L, 1);
2047                 NodeMetadata *meta = getmeta(ref);
2048                 if(meta == NULL) return 0;
2049                 // Do it
2050                 meta->resetInventoryModified();
2051                 reportMetadataChange(ref);
2052                 return 0;
2053         }
2054
2055         // is_text_modified(self)
2056         static int l_is_text_modified(lua_State *L)
2057         {
2058                 NodeMetaRef *ref = checkobject(L, 1);
2059                 NodeMetadata *meta = getmeta(ref);
2060                 if(meta == NULL) return 0;
2061                 // Do it
2062                 lua_pushboolean(L, meta->isTextModified());
2063                 return 1;
2064         }
2065
2066         // reset_text_modified(self)
2067         static int l_reset_text_modified(lua_State *L)
2068         {
2069                 NodeMetaRef *ref = checkobject(L, 1);
2070                 NodeMetadata *meta = getmeta(ref);
2071                 if(meta == NULL) return 0;
2072                 // Do it
2073                 meta->resetTextModified();
2074                 reportMetadataChange(ref);
2075                 return 0;
2076         }
2077
2078         // set_string(self, name, var)
2079         static int l_set_string(lua_State *L)
2080         {
2081                 NodeMetaRef *ref = checkobject(L, 1);
2082                 NodeMetadata *meta = getmeta(ref);
2083                 if(meta == NULL) return 0;
2084                 // Do it
2085                 std::string name = luaL_checkstring(L, 2);
2086                 size_t len = 0;
2087                 const char *s = lua_tolstring(L, 3, &len);
2088                 std::string str(s, len);
2089                 meta->setString(name, str);
2090                 reportMetadataChange(ref);
2091                 return 0;
2092         }
2093
2094         // get_string(self, name)
2095         static int l_get_string(lua_State *L)
2096         {
2097                 NodeMetaRef *ref = checkobject(L, 1);
2098                 NodeMetadata *meta = getmeta(ref);
2099                 if(meta == NULL) return 0;
2100                 // Do it
2101                 std::string name = luaL_checkstring(L, 2);
2102                 std::string str = meta->getString(name);
2103                 lua_pushlstring(L, str.c_str(), str.size());
2104                 return 1;
2105         }
2106
2107 public:
2108         NodeMetaRef(v3s16 p, ServerEnvironment *env):
2109                 m_p(p),
2110                 m_env(env)
2111         {
2112         }
2113
2114         ~NodeMetaRef()
2115         {
2116         }
2117
2118         // Creates an NodeMetaRef and leaves it on top of stack
2119         // Not callable from Lua; all references are created on the C side.
2120         static void create(lua_State *L, v3s16 p, ServerEnvironment *env)
2121         {
2122                 NodeMetaRef *o = new NodeMetaRef(p, env);
2123                 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
2124                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
2125                 luaL_getmetatable(L, className);
2126                 lua_setmetatable(L, -2);
2127         }
2128
2129         static void Register(lua_State *L)
2130         {
2131                 lua_newtable(L);
2132                 int methodtable = lua_gettop(L);
2133                 luaL_newmetatable(L, className);
2134                 int metatable = lua_gettop(L);
2135
2136                 lua_pushliteral(L, "__metatable");
2137                 lua_pushvalue(L, methodtable);
2138                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
2139
2140                 lua_pushliteral(L, "__index");
2141                 lua_pushvalue(L, methodtable);
2142                 lua_settable(L, metatable);
2143
2144                 lua_pushliteral(L, "__gc");
2145                 lua_pushcfunction(L, gc_object);
2146                 lua_settable(L, metatable);
2147
2148                 lua_pop(L, 1);  // drop metatable
2149
2150                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
2151                 lua_pop(L, 1);  // drop methodtable
2152
2153                 // Cannot be created from Lua
2154                 //lua_register(L, className, create_object);
2155         }
2156 };
2157 const char NodeMetaRef::className[] = "NodeMetaRef";
2158 const luaL_reg NodeMetaRef::methods[] = {
2159         method(NodeMetaRef, get_type),
2160         method(NodeMetaRef, allows_text_input),
2161         method(NodeMetaRef, set_text),
2162         method(NodeMetaRef, get_text),
2163         method(NodeMetaRef, get_owner),
2164         method(NodeMetaRef, set_owner),
2165         method(NodeMetaRef, get_allow_removal),
2166         method(NodeMetaRef, set_infotext),
2167         method(NodeMetaRef, get_inventory),
2168         method(NodeMetaRef, set_inventory_draw_spec),
2169         method(NodeMetaRef, set_allow_text_input),
2170         method(NodeMetaRef, set_allow_removal),
2171         method(NodeMetaRef, set_enforce_owner),
2172         method(NodeMetaRef, is_inventory_modified),
2173         method(NodeMetaRef, reset_inventory_modified),
2174         method(NodeMetaRef, is_text_modified),
2175         method(NodeMetaRef, reset_text_modified),
2176         method(NodeMetaRef, set_string),
2177         method(NodeMetaRef, get_string),
2178         {0,0}
2179 };
2180
2181 /*
2182         ObjectRef
2183 */
2184
2185 class ObjectRef
2186 {
2187 private:
2188         ServerActiveObject *m_object;
2189
2190         static const char className[];
2191         static const luaL_reg methods[];
2192
2193         static ObjectRef *checkobject(lua_State *L, int narg)
2194         {
2195                 luaL_checktype(L, narg, LUA_TUSERDATA);
2196                 void *ud = luaL_checkudata(L, narg, className);
2197                 if(!ud) luaL_typerror(L, narg, className);
2198                 return *(ObjectRef**)ud;  // unbox pointer
2199         }
2200         
2201         static ServerActiveObject* getobject(ObjectRef *ref)
2202         {
2203                 ServerActiveObject *co = ref->m_object;
2204                 return co;
2205         }
2206         
2207         static LuaEntitySAO* getluaobject(ObjectRef *ref)
2208         {
2209                 ServerActiveObject *obj = getobject(ref);
2210                 if(obj == NULL)
2211                         return NULL;
2212                 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
2213                         return NULL;
2214                 return (LuaEntitySAO*)obj;
2215         }
2216         
2217         static ServerRemotePlayer* getplayer(ObjectRef *ref)
2218         {
2219                 ServerActiveObject *obj = getobject(ref);
2220                 if(obj == NULL)
2221                         return NULL;
2222                 if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
2223                         return NULL;
2224                 return static_cast<ServerRemotePlayer*>(obj);
2225         }
2226         
2227         // Exported functions
2228         
2229         // garbage collector
2230         static int gc_object(lua_State *L) {
2231                 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
2232                 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
2233                 delete o;
2234                 return 0;
2235         }
2236
2237         // remove(self)
2238         static int l_remove(lua_State *L)
2239         {
2240                 ObjectRef *ref = checkobject(L, 1);
2241                 ServerActiveObject *co = getobject(ref);
2242                 if(co == NULL) return 0;
2243                 infostream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
2244                 co->m_removed = true;
2245                 return 0;
2246         }
2247         
2248         // getpos(self)
2249         // returns: {x=num, y=num, z=num}
2250         static int l_getpos(lua_State *L)
2251         {
2252                 ObjectRef *ref = checkobject(L, 1);
2253                 ServerActiveObject *co = getobject(ref);
2254                 if(co == NULL) return 0;
2255                 v3f pos = co->getBasePosition() / BS;
2256                 lua_newtable(L);
2257                 lua_pushnumber(L, pos.X);
2258                 lua_setfield(L, -2, "x");
2259                 lua_pushnumber(L, pos.Y);
2260                 lua_setfield(L, -2, "y");
2261                 lua_pushnumber(L, pos.Z);
2262                 lua_setfield(L, -2, "z");
2263                 return 1;
2264         }
2265         
2266         // setpos(self, pos)
2267         static int l_setpos(lua_State *L)
2268         {
2269                 ObjectRef *ref = checkobject(L, 1);
2270                 //LuaEntitySAO *co = getluaobject(ref);
2271                 ServerActiveObject *co = getobject(ref);
2272                 if(co == NULL) return 0;
2273                 // pos
2274                 v3f pos = checkFloatPos(L, 2);
2275                 // Do it
2276                 co->setPos(pos);
2277                 // Move player if applicable
2278                 ServerRemotePlayer *player = getplayer(ref);
2279                 if(player != NULL)
2280                         get_server(L)->SendMovePlayer(player);
2281                 return 0;
2282         }
2283         
2284         // moveto(self, pos, continuous=false)
2285         static int l_moveto(lua_State *L)
2286         {
2287                 ObjectRef *ref = checkobject(L, 1);
2288                 //LuaEntitySAO *co = getluaobject(ref);
2289                 ServerActiveObject *co = getobject(ref);
2290                 if(co == NULL) return 0;
2291                 // pos
2292                 v3f pos = checkFloatPos(L, 2);
2293                 // continuous
2294                 bool continuous = lua_toboolean(L, 3);
2295                 // Do it
2296                 co->moveTo(pos, continuous);
2297                 return 0;
2298         }
2299
2300         // punch(self, puncher); puncher = an another ObjectRef
2301         static int l_punch(lua_State *L)
2302         {
2303                 ObjectRef *ref = checkobject(L, 1);
2304                 ObjectRef *ref2 = checkobject(L, 2);
2305                 ServerActiveObject *co = getobject(ref);
2306                 ServerActiveObject *co2 = getobject(ref2);
2307                 if(co == NULL) return 0;
2308                 if(co2 == NULL) return 0;
2309                 // Do it
2310                 co->punch(co2);
2311                 return 0;
2312         }
2313
2314         // right_click(self, clicker); clicker = an another ObjectRef
2315         static int l_right_click(lua_State *L)
2316         {
2317                 ObjectRef *ref = checkobject(L, 1);
2318                 ObjectRef *ref2 = checkobject(L, 2);
2319                 ServerActiveObject *co = getobject(ref);
2320                 ServerActiveObject *co2 = getobject(ref2);
2321                 if(co == NULL) return 0;
2322                 if(co2 == NULL) return 0;
2323                 // Do it
2324                 co->rightClick(co2);
2325                 return 0;
2326         }
2327
2328         // set_hp(self, hp)
2329         // hp = number of hitpoints (2 * number of hearts)
2330         // returns: nil
2331         static int l_set_hp(lua_State *L)
2332         {
2333                 ObjectRef *ref = checkobject(L, 1);
2334                 luaL_checknumber(L, 2);
2335                 ServerActiveObject *co = getobject(ref);
2336                 if(co == NULL) return 0;
2337                 int hp = lua_tonumber(L, 2);
2338                 infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
2339                                 <<" hp="<<hp<<std::endl;
2340                 // Do it
2341                 co->setHP(hp);
2342                 // Return
2343                 return 0;
2344         }
2345
2346         // get_hp(self)
2347         // returns: number of hitpoints (2 * number of hearts)
2348         // 0 if not applicable to this type of object
2349         static int l_get_hp(lua_State *L)
2350         {
2351                 ObjectRef *ref = checkobject(L, 1);
2352                 ServerActiveObject *co = getobject(ref);
2353                 if(co == NULL) return 0;
2354                 int hp = co->getHP();
2355                 infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
2356                                 <<" hp="<<hp<<std::endl;
2357                 // Return
2358                 lua_pushnumber(L, hp);
2359                 return 1;
2360         }
2361
2362         // get_inventory(self)
2363         static int l_get_inventory(lua_State *L)
2364         {
2365                 ObjectRef *ref = checkobject(L, 1);
2366                 ServerActiveObject *co = getobject(ref);
2367                 if(co == NULL) return 0;
2368                 // Do it
2369                 InventoryLocation loc = co->getInventoryLocation();
2370                 if(get_server(L)->getInventory(loc) != NULL)
2371                         InvRef::create(L, loc);
2372                 else
2373                         lua_pushnil(L);
2374                 return 1;
2375         }
2376
2377         // get_wield_list(self)
2378         static int l_get_wield_list(lua_State *L)
2379         {
2380                 ObjectRef *ref = checkobject(L, 1);
2381                 ServerActiveObject *co = getobject(ref);
2382                 if(co == NULL) return 0;
2383                 // Do it
2384                 lua_pushstring(L, co->getWieldList().c_str());
2385                 return 1;
2386         }
2387
2388         // get_wield_index(self)
2389         static int l_get_wield_index(lua_State *L)
2390         {
2391                 ObjectRef *ref = checkobject(L, 1);
2392                 ServerActiveObject *co = getobject(ref);
2393                 if(co == NULL) return 0;
2394                 // Do it
2395                 lua_pushinteger(L, co->getWieldIndex() + 1);
2396                 return 1;
2397         }
2398
2399         // get_wielded_item(self)
2400         static int l_get_wielded_item(lua_State *L)
2401         {
2402                 ObjectRef *ref = checkobject(L, 1);
2403                 ServerActiveObject *co = getobject(ref);
2404                 if(co == NULL) return 0;
2405                 // Do it
2406                 LuaItemStack::create(L, co->getWieldedItem());
2407                 return 1;
2408         }
2409
2410         // set_wielded_item(self, itemstack or itemstring or table or nil)
2411         static int l_set_wielded_item(lua_State *L)
2412         {
2413                 ObjectRef *ref = checkobject(L, 1);
2414                 ServerActiveObject *co = getobject(ref);
2415                 if(co == NULL) return 0;
2416                 // Do it
2417                 ItemStack item = read_item(L, 2);
2418                 bool success = co->setWieldedItem(item);
2419                 lua_pushboolean(L, success);
2420                 return 1;
2421         }
2422
2423         /* LuaEntitySAO-only */
2424
2425         // setvelocity(self, {x=num, y=num, z=num})
2426         static int l_setvelocity(lua_State *L)
2427         {
2428                 ObjectRef *ref = checkobject(L, 1);
2429                 LuaEntitySAO *co = getluaobject(ref);
2430                 if(co == NULL) return 0;
2431                 // pos
2432                 v3f pos = checkFloatPos(L, 2);
2433                 // Do it
2434                 co->setVelocity(pos);
2435                 return 0;
2436         }
2437         
2438         // getvelocity(self)
2439         static int l_getvelocity(lua_State *L)
2440         {
2441                 ObjectRef *ref = checkobject(L, 1);
2442                 LuaEntitySAO *co = getluaobject(ref);
2443                 if(co == NULL) return 0;
2444                 // Do it
2445                 v3f v = co->getVelocity();
2446                 pushFloatPos(L, v);
2447                 return 1;
2448         }
2449         
2450         // setacceleration(self, {x=num, y=num, z=num})
2451         static int l_setacceleration(lua_State *L)
2452         {
2453                 ObjectRef *ref = checkobject(L, 1);
2454                 LuaEntitySAO *co = getluaobject(ref);
2455                 if(co == NULL) return 0;
2456                 // pos
2457                 v3f pos = checkFloatPos(L, 2);
2458                 // Do it
2459                 co->setAcceleration(pos);
2460                 return 0;
2461         }
2462         
2463         // getacceleration(self)
2464         static int l_getacceleration(lua_State *L)
2465         {
2466                 ObjectRef *ref = checkobject(L, 1);
2467                 LuaEntitySAO *co = getluaobject(ref);
2468                 if(co == NULL) return 0;
2469                 // Do it
2470                 v3f v = co->getAcceleration();
2471                 pushFloatPos(L, v);
2472                 return 1;
2473         }
2474         
2475         // setyaw(self, radians)
2476         static int l_setyaw(lua_State *L)
2477         {
2478                 ObjectRef *ref = checkobject(L, 1);
2479                 LuaEntitySAO *co = getluaobject(ref);
2480                 if(co == NULL) return 0;
2481                 // pos
2482                 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
2483                 // Do it
2484                 co->setYaw(yaw);
2485                 return 0;
2486         }
2487         
2488         // getyaw(self)
2489         static int l_getyaw(lua_State *L)
2490         {
2491                 ObjectRef *ref = checkobject(L, 1);
2492                 LuaEntitySAO *co = getluaobject(ref);
2493                 if(co == NULL) return 0;
2494                 // Do it
2495                 float yaw = co->getYaw() * core::DEGTORAD;
2496                 lua_pushnumber(L, yaw);
2497                 return 1;
2498         }
2499         
2500         // settexturemod(self, mod)
2501         static int l_settexturemod(lua_State *L)
2502         {
2503                 ObjectRef *ref = checkobject(L, 1);
2504                 LuaEntitySAO *co = getluaobject(ref);
2505                 if(co == NULL) return 0;
2506                 // Do it
2507                 std::string mod = luaL_checkstring(L, 2);
2508                 co->setTextureMod(mod);
2509                 return 0;
2510         }
2511         
2512         // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
2513         //           select_horiz_by_yawpitch=false)
2514         static int l_setsprite(lua_State *L)
2515         {
2516                 ObjectRef *ref = checkobject(L, 1);
2517                 LuaEntitySAO *co = getluaobject(ref);
2518                 if(co == NULL) return 0;
2519                 // Do it
2520                 v2s16 p(0,0);
2521                 if(!lua_isnil(L, 2))
2522                         p = read_v2s16(L, 2);
2523                 int num_frames = 1;
2524                 if(!lua_isnil(L, 3))
2525                         num_frames = lua_tonumber(L, 3);
2526                 float framelength = 0.2;
2527                 if(!lua_isnil(L, 4))
2528                         framelength = lua_tonumber(L, 4);
2529                 bool select_horiz_by_yawpitch = false;
2530                 if(!lua_isnil(L, 5))
2531                         select_horiz_by_yawpitch = lua_toboolean(L, 5);
2532                 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
2533                 return 0;
2534         }
2535
2536         // DEPRECATED
2537         // get_entity_name(self)
2538         static int l_get_entity_name(lua_State *L)
2539         {
2540                 ObjectRef *ref = checkobject(L, 1);
2541                 LuaEntitySAO *co = getluaobject(ref);
2542                 if(co == NULL) return 0;
2543                 // Do it
2544                 std::string name = co->getName();
2545                 lua_pushstring(L, name.c_str());
2546                 return 1;
2547         }
2548         
2549         // get_luaentity(self)
2550         static int l_get_luaentity(lua_State *L)
2551         {
2552                 ObjectRef *ref = checkobject(L, 1);
2553                 LuaEntitySAO *co = getluaobject(ref);
2554                 if(co == NULL) return 0;
2555                 // Do it
2556                 luaentity_get(L, co->getId());
2557                 return 1;
2558         }
2559         
2560         /* Player-only */
2561         
2562         // get_player_name(self)
2563         static int l_get_player_name(lua_State *L)
2564         {
2565                 ObjectRef *ref = checkobject(L, 1);
2566                 ServerRemotePlayer *player = getplayer(ref);
2567                 if(player == NULL){
2568                         lua_pushnil(L);
2569                         return 1;
2570                 }
2571                 // Do it
2572                 lua_pushstring(L, player->getName());
2573                 return 1;
2574         }
2575         
2576         // get_look_dir(self)
2577         static int l_get_look_dir(lua_State *L)
2578         {
2579                 ObjectRef *ref = checkobject(L, 1);
2580                 ServerRemotePlayer *player = getplayer(ref);
2581                 if(player == NULL) return 0;
2582                 // Do it
2583                 float pitch = player->getRadPitch();
2584                 float yaw = player->getRadYaw();
2585                 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
2586                 push_v3f(L, v);
2587                 return 1;
2588         }
2589
2590         // get_look_pitch(self)
2591         static int l_get_look_pitch(lua_State *L)
2592         {
2593                 ObjectRef *ref = checkobject(L, 1);
2594                 ServerRemotePlayer *player = getplayer(ref);
2595                 if(player == NULL) return 0;
2596                 // Do it
2597                 lua_pushnumber(L, player->getRadPitch());
2598                 return 1;
2599         }
2600
2601         // get_look_yaw(self)
2602         static int l_get_look_yaw(lua_State *L)
2603         {
2604                 ObjectRef *ref = checkobject(L, 1);
2605                 ServerRemotePlayer *player = getplayer(ref);
2606                 if(player == NULL) return 0;
2607                 // Do it
2608                 lua_pushnumber(L, player->getRadYaw());
2609                 return 1;
2610         }
2611
2612 public:
2613         ObjectRef(ServerActiveObject *object):
2614                 m_object(object)
2615         {
2616                 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
2617         }
2618
2619         ~ObjectRef()
2620         {
2621                 /*if(m_object)
2622                         infostream<<"ObjectRef destructing for id="
2623                                         <<m_object->getId()<<std::endl;
2624                 else
2625                         infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
2626         }
2627
2628         // Creates an ObjectRef and leaves it on top of stack
2629         // Not callable from Lua; all references are created on the C side.
2630         static void create(lua_State *L, ServerActiveObject *object)
2631         {
2632                 ObjectRef *o = new ObjectRef(object);
2633                 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
2634                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
2635                 luaL_getmetatable(L, className);
2636                 lua_setmetatable(L, -2);
2637         }
2638
2639         static void set_null(lua_State *L)
2640         {
2641                 ObjectRef *o = checkobject(L, -1);
2642                 o->m_object = NULL;
2643         }
2644         
2645         static void Register(lua_State *L)
2646         {
2647                 lua_newtable(L);
2648                 int methodtable = lua_gettop(L);
2649                 luaL_newmetatable(L, className);
2650                 int metatable = lua_gettop(L);
2651
2652                 lua_pushliteral(L, "__metatable");
2653                 lua_pushvalue(L, methodtable);
2654                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
2655
2656                 lua_pushliteral(L, "__index");
2657                 lua_pushvalue(L, methodtable);
2658                 lua_settable(L, metatable);
2659
2660                 lua_pushliteral(L, "__gc");
2661                 lua_pushcfunction(L, gc_object);
2662                 lua_settable(L, metatable);
2663
2664                 lua_pop(L, 1);  // drop metatable
2665
2666                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
2667                 lua_pop(L, 1);  // drop methodtable
2668
2669                 // Cannot be created from Lua
2670                 //lua_register(L, className, create_object);
2671         }
2672 };
2673 const char ObjectRef::className[] = "ObjectRef";
2674 const luaL_reg ObjectRef::methods[] = {
2675         // ServerActiveObject
2676         method(ObjectRef, remove),
2677         method(ObjectRef, getpos),
2678         method(ObjectRef, setpos),
2679         method(ObjectRef, moveto),
2680         method(ObjectRef, punch),
2681         method(ObjectRef, right_click),
2682         method(ObjectRef, set_hp),
2683         method(ObjectRef, get_hp),
2684         method(ObjectRef, get_inventory),
2685         method(ObjectRef, get_wield_list),
2686         method(ObjectRef, get_wield_index),
2687         method(ObjectRef, get_wielded_item),
2688         method(ObjectRef, set_wielded_item),
2689         // LuaEntitySAO-only
2690         method(ObjectRef, setvelocity),
2691         method(ObjectRef, getvelocity),
2692         method(ObjectRef, setacceleration),
2693         method(ObjectRef, getacceleration),
2694         method(ObjectRef, setyaw),
2695         method(ObjectRef, getyaw),
2696         method(ObjectRef, settexturemod),
2697         method(ObjectRef, setsprite),
2698         method(ObjectRef, get_entity_name),
2699         method(ObjectRef, get_luaentity),
2700         // Player-only
2701         method(ObjectRef, get_player_name),
2702         method(ObjectRef, get_look_dir),
2703         method(ObjectRef, get_look_pitch),
2704         method(ObjectRef, get_look_yaw),
2705         {0,0}
2706 };
2707
2708 // Creates a new anonymous reference if id=0
2709 static void objectref_get_or_create(lua_State *L,
2710                 ServerActiveObject *cobj)
2711 {
2712         if(cobj->getId() == 0){
2713                 ObjectRef::create(L, cobj);
2714         } else {
2715                 objectref_get(L, cobj->getId());
2716         }
2717 }
2718
2719 /*
2720         EnvRef
2721 */
2722
2723 class EnvRef
2724 {
2725 private:
2726         ServerEnvironment *m_env;
2727
2728         static const char className[];
2729         static const luaL_reg methods[];
2730
2731         static EnvRef *checkobject(lua_State *L, int narg)
2732         {
2733                 luaL_checktype(L, narg, LUA_TUSERDATA);
2734                 void *ud = luaL_checkudata(L, narg, className);
2735                 if(!ud) luaL_typerror(L, narg, className);
2736                 return *(EnvRef**)ud;  // unbox pointer
2737         }
2738         
2739         // Exported functions
2740
2741         // EnvRef:add_node(pos, node)
2742         // pos = {x=num, y=num, z=num}
2743         static int l_add_node(lua_State *L)
2744         {
2745                 //infostream<<"EnvRef::l_add_node()"<<std::endl;
2746                 EnvRef *o = checkobject(L, 1);
2747                 ServerEnvironment *env = o->m_env;
2748                 if(env == NULL) return 0;
2749                 // pos
2750                 v3s16 pos = read_v3s16(L, 2);
2751                 // content
2752                 MapNode n = readnode(L, 3, env->getGameDef()->ndef());
2753                 // Do it
2754                 bool succeeded = env->getMap().addNodeWithEvent(pos, n);
2755                 lua_pushboolean(L, succeeded);
2756                 return 1;
2757         }
2758
2759         // EnvRef:remove_node(pos)
2760         // pos = {x=num, y=num, z=num}
2761         static int l_remove_node(lua_State *L)
2762         {
2763                 //infostream<<"EnvRef::l_remove_node()"<<std::endl;
2764                 EnvRef *o = checkobject(L, 1);
2765                 ServerEnvironment *env = o->m_env;
2766                 if(env == NULL) return 0;
2767                 // pos
2768                 v3s16 pos = read_v3s16(L, 2);
2769                 // Do it
2770                 bool succeeded = env->getMap().removeNodeWithEvent(pos);
2771                 lua_pushboolean(L, succeeded);
2772                 return 1;
2773         }
2774
2775         // EnvRef:get_node(pos)
2776         // pos = {x=num, y=num, z=num}
2777         static int l_get_node(lua_State *L)
2778         {
2779                 //infostream<<"EnvRef::l_get_node()"<<std::endl;
2780                 EnvRef *o = checkobject(L, 1);
2781                 ServerEnvironment *env = o->m_env;
2782                 if(env == NULL) return 0;
2783                 // pos
2784                 v3s16 pos = read_v3s16(L, 2);
2785                 // Do it
2786                 MapNode n = env->getMap().getNodeNoEx(pos);
2787                 // Return node
2788                 pushnode(L, n, env->getGameDef()->ndef());
2789                 return 1;
2790         }
2791
2792         // EnvRef:get_node_or_nil(pos)
2793         // pos = {x=num, y=num, z=num}
2794         static int l_get_node_or_nil(lua_State *L)
2795         {
2796                 //infostream<<"EnvRef::l_get_node()"<<std::endl;
2797                 EnvRef *o = checkobject(L, 1);
2798                 ServerEnvironment *env = o->m_env;
2799                 if(env == NULL) return 0;
2800                 // pos
2801                 v3s16 pos = read_v3s16(L, 2);
2802                 // Do it
2803                 try{
2804                         MapNode n = env->getMap().getNode(pos);
2805                         // Return node
2806                         pushnode(L, n, env->getGameDef()->ndef());
2807                         return 1;
2808                 } catch(InvalidPositionException &e)
2809                 {
2810                         lua_pushnil(L);
2811                         return 1;
2812                 }
2813         }
2814
2815         // EnvRef:get_node_light(pos, timeofday)
2816         // pos = {x=num, y=num, z=num}
2817         // timeofday: nil = current time, 0 = night, 0.5 = day
2818         static int l_get_node_light(lua_State *L)
2819         {
2820                 EnvRef *o = checkobject(L, 1);
2821                 ServerEnvironment *env = o->m_env;
2822                 if(env == NULL) return 0;
2823                 // Do it
2824                 v3s16 pos = read_v3s16(L, 2);
2825                 u32 time_of_day = env->getTimeOfDay();
2826                 if(lua_isnumber(L, 3))
2827                         time_of_day = 24000.0 * lua_tonumber(L, 3);
2828                 time_of_day %= 24000;
2829                 u32 dnr = time_to_daynight_ratio(time_of_day);
2830                 MapNode n = env->getMap().getNodeNoEx(pos);
2831                 try{
2832                         MapNode n = env->getMap().getNode(pos);
2833                         INodeDefManager *ndef = env->getGameDef()->ndef();
2834                         lua_pushinteger(L, n.getLightBlend(dnr, ndef));
2835                         return 1;
2836                 } catch(InvalidPositionException &e)
2837                 {
2838                         lua_pushnil(L);
2839                         return 1;
2840                 }
2841         }
2842
2843         // EnvRef:add_entity(pos, entityname) -> ObjectRef or nil
2844         // pos = {x=num, y=num, z=num}
2845         static int l_add_entity(lua_State *L)
2846         {
2847                 //infostream<<"EnvRef::l_add_entity()"<<std::endl;
2848                 EnvRef *o = checkobject(L, 1);
2849                 ServerEnvironment *env = o->m_env;
2850                 if(env == NULL) return 0;
2851                 // pos
2852                 v3f pos = checkFloatPos(L, 2);
2853                 // content
2854                 const char *name = luaL_checkstring(L, 3);
2855                 // Do it
2856                 ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, "");
2857                 int objectid = env->addActiveObject(obj);
2858                 // If failed to add, return nothing (reads as nil)
2859                 if(objectid == 0)
2860                         return 0;
2861                 // Return ObjectRef
2862                 objectref_get_or_create(L, obj);
2863                 return 1;
2864         }
2865
2866         // EnvRef:add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
2867         // pos = {x=num, y=num, z=num}
2868         static int l_add_item(lua_State *L)
2869         {
2870                 //infostream<<"EnvRef::l_add_item()"<<std::endl;
2871                 EnvRef *o = checkobject(L, 1);
2872                 ServerEnvironment *env = o->m_env;
2873                 if(env == NULL) return 0;
2874                 // pos
2875                 v3f pos = checkFloatPos(L, 2);
2876                 // item
2877                 ItemStack item = read_item(L, 3);
2878                 if(item.empty() || !item.isKnown(get_server(L)->idef()))
2879                         return 0;
2880                 // Do it
2881                 ServerActiveObject *obj = new ItemSAO(env, pos, item.getItemString());
2882                 int objectid = env->addActiveObject(obj);
2883                 // If failed to add, return nothing (reads as nil)
2884                 if(objectid == 0)
2885                         return 0;
2886                 // Return ObjectRef
2887                 objectref_get_or_create(L, obj);
2888                 return 1;
2889         }
2890
2891         // EnvRef:add_rat(pos)
2892         // pos = {x=num, y=num, z=num}
2893         static int l_add_rat(lua_State *L)
2894         {
2895                 infostream<<"EnvRef::l_add_rat()"<<std::endl;
2896                 EnvRef *o = checkobject(L, 1);
2897                 ServerEnvironment *env = o->m_env;
2898                 if(env == NULL) return 0;
2899                 // pos
2900                 v3f pos = checkFloatPos(L, 2);
2901                 // Do it
2902                 ServerActiveObject *obj = new RatSAO(env, pos);
2903                 env->addActiveObject(obj);
2904                 return 0;
2905         }
2906
2907         // EnvRef:add_firefly(pos)
2908         // pos = {x=num, y=num, z=num}
2909         static int l_add_firefly(lua_State *L)
2910         {
2911                 infostream<<"EnvRef::l_add_firefly()"<<std::endl;
2912                 EnvRef *o = checkobject(L, 1);
2913                 ServerEnvironment *env = o->m_env;
2914                 if(env == NULL) return 0;
2915                 // pos
2916                 v3f pos = checkFloatPos(L, 2);
2917                 // Do it
2918                 ServerActiveObject *obj = new FireflySAO(env, pos);
2919                 env->addActiveObject(obj);
2920                 return 0;
2921         }
2922
2923         // EnvRef:get_meta(pos)
2924         static int l_get_meta(lua_State *L)
2925         {
2926                 //infostream<<"EnvRef::l_get_meta()"<<std::endl;
2927                 EnvRef *o = checkobject(L, 1);
2928                 ServerEnvironment *env = o->m_env;
2929                 if(env == NULL) return 0;
2930                 // Do it
2931                 v3s16 p = read_v3s16(L, 2);
2932                 NodeMetaRef::create(L, p, env);
2933                 return 1;
2934         }
2935
2936         // EnvRef:get_player_by_name(name)
2937         static int l_get_player_by_name(lua_State *L)
2938         {
2939                 EnvRef *o = checkobject(L, 1);
2940                 ServerEnvironment *env = o->m_env;
2941                 if(env == NULL) return 0;
2942                 // Do it
2943                 const char *name = luaL_checkstring(L, 2);
2944                 ServerRemotePlayer *player =
2945                                 static_cast<ServerRemotePlayer*>(env->getPlayer(name));
2946                 if(player == NULL){
2947                         lua_pushnil(L);
2948                         return 1;
2949                 }
2950                 // Put player on stack
2951                 objectref_get_or_create(L, player);
2952                 return 1;
2953         }
2954
2955         // EnvRef:get_objects_inside_radius(pos, radius)
2956         static int l_get_objects_inside_radius(lua_State *L)
2957         {
2958                 // Get the table insert function
2959                 lua_getglobal(L, "table");
2960                 lua_getfield(L, -1, "insert");
2961                 int table_insert = lua_gettop(L);
2962                 // Get environemnt
2963                 EnvRef *o = checkobject(L, 1);
2964                 ServerEnvironment *env = o->m_env;
2965                 if(env == NULL) return 0;
2966                 // Do it
2967                 v3f pos = checkFloatPos(L, 2);
2968                 float radius = luaL_checknumber(L, 3) * BS;
2969                 std::set<u16> ids = env->getObjectsInsideRadius(pos, radius);
2970                 lua_newtable(L);
2971                 int table = lua_gettop(L);
2972                 for(std::set<u16>::const_iterator
2973                                 i = ids.begin(); i != ids.end(); i++){
2974                         ServerActiveObject *obj = env->getActiveObject(*i);
2975                         // Insert object reference into table
2976                         lua_pushvalue(L, table_insert);
2977                         lua_pushvalue(L, table);
2978                         objectref_get_or_create(L, obj);
2979                         if(lua_pcall(L, 2, 0, 0))
2980                                 script_error(L, "error: %s", lua_tostring(L, -1));
2981                 }
2982                 return 1;
2983         }
2984
2985         // EnvRef:set_timeofday(val)
2986         // val = 0...1
2987         static int l_set_timeofday(lua_State *L)
2988         {
2989                 EnvRef *o = checkobject(L, 1);
2990                 ServerEnvironment *env = o->m_env;
2991                 if(env == NULL) return 0;
2992                 // Do it
2993                 float timeofday_f = luaL_checknumber(L, 2);
2994                 assert(timeofday_f >= 0.0 && timeofday_f <= 1.0);
2995                 int timeofday_mh = (int)(timeofday_f * 24000.0);
2996                 // This should be set directly in the environment but currently
2997                 // such changes aren't immediately sent to the clients, so call
2998                 // the server instead.
2999                 //env->setTimeOfDay(timeofday_mh);
3000                 get_server(L)->setTimeOfDay(timeofday_mh);
3001                 return 0;
3002         }
3003
3004         // EnvRef:get_timeofday() -> 0...1
3005         static int l_get_timeofday(lua_State *L)
3006         {
3007                 EnvRef *o = checkobject(L, 1);
3008                 ServerEnvironment *env = o->m_env;
3009                 if(env == NULL) return 0;
3010                 // Do it
3011                 int timeofday_mh = env->getTimeOfDay();
3012                 float timeofday_f = (float)timeofday_mh / 24000.0;
3013                 lua_pushnumber(L, timeofday_f);
3014                 return 1;
3015         }
3016
3017         static int gc_object(lua_State *L) {
3018                 EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
3019                 delete o;
3020                 return 0;
3021         }
3022
3023 public:
3024         EnvRef(ServerEnvironment *env):
3025                 m_env(env)
3026         {
3027                 infostream<<"EnvRef created"<<std::endl;
3028         }
3029
3030         ~EnvRef()
3031         {
3032                 infostream<<"EnvRef destructing"<<std::endl;
3033         }
3034
3035         // Creates an EnvRef and leaves it on top of stack
3036         // Not callable from Lua; all references are created on the C side.
3037         static void create(lua_State *L, ServerEnvironment *env)
3038         {
3039                 EnvRef *o = new EnvRef(env);
3040                 //infostream<<"EnvRef::create: o="<<o<<std::endl;
3041                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
3042                 luaL_getmetatable(L, className);
3043                 lua_setmetatable(L, -2);
3044         }
3045
3046         static void set_null(lua_State *L)
3047         {
3048                 EnvRef *o = checkobject(L, -1);
3049                 o->m_env = NULL;
3050         }
3051         
3052         static void Register(lua_State *L)
3053         {
3054                 lua_newtable(L);
3055                 int methodtable = lua_gettop(L);
3056                 luaL_newmetatable(L, className);
3057                 int metatable = lua_gettop(L);
3058
3059                 lua_pushliteral(L, "__metatable");
3060                 lua_pushvalue(L, methodtable);
3061                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
3062
3063                 lua_pushliteral(L, "__index");
3064                 lua_pushvalue(L, methodtable);
3065                 lua_settable(L, metatable);
3066
3067                 lua_pushliteral(L, "__gc");
3068                 lua_pushcfunction(L, gc_object);
3069                 lua_settable(L, metatable);
3070
3071                 lua_pop(L, 1);  // drop metatable
3072
3073                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
3074                 lua_pop(L, 1);  // drop methodtable
3075
3076                 // Cannot be created from Lua
3077                 //lua_register(L, className, create_object);
3078         }
3079 };
3080 const char EnvRef::className[] = "EnvRef";
3081 const luaL_reg EnvRef::methods[] = {
3082         method(EnvRef, add_node),
3083         method(EnvRef, remove_node),
3084         method(EnvRef, get_node),
3085         method(EnvRef, get_node_or_nil),
3086         method(EnvRef, get_node_light),
3087         method(EnvRef, add_entity),
3088         method(EnvRef, add_item),
3089         method(EnvRef, add_rat),
3090         method(EnvRef, add_firefly),
3091         method(EnvRef, get_meta),
3092         method(EnvRef, get_player_by_name),
3093         method(EnvRef, get_objects_inside_radius),
3094         method(EnvRef, set_timeofday),
3095         method(EnvRef, get_timeofday),
3096         {0,0}
3097 };
3098
3099 /*
3100         Global functions
3101 */
3102
3103 class LuaABM : public ActiveBlockModifier
3104 {
3105 private:
3106         lua_State *m_lua;
3107         int m_id;
3108
3109         std::set<std::string> m_trigger_contents;
3110         std::set<std::string> m_required_neighbors;
3111         float m_trigger_interval;
3112         u32 m_trigger_chance;
3113 public:
3114         LuaABM(lua_State *L, int id,
3115                         const std::set<std::string> &trigger_contents,
3116                         const std::set<std::string> &required_neighbors,
3117                         float trigger_interval, u32 trigger_chance):
3118                 m_lua(L),
3119                 m_id(id),
3120                 m_trigger_contents(trigger_contents),
3121                 m_required_neighbors(required_neighbors),
3122                 m_trigger_interval(trigger_interval),
3123                 m_trigger_chance(trigger_chance)
3124         {
3125         }
3126         virtual std::set<std::string> getTriggerContents()
3127         {
3128                 return m_trigger_contents;
3129         }
3130         virtual std::set<std::string> getRequiredNeighbors()
3131         {
3132                 return m_required_neighbors;
3133         }
3134         virtual float getTriggerInterval()
3135         {
3136                 return m_trigger_interval;
3137         }
3138         virtual u32 getTriggerChance()
3139         {
3140                 return m_trigger_chance;
3141         }
3142         virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
3143                         u32 active_object_count, u32 active_object_count_wider)
3144         {
3145                 lua_State *L = m_lua;
3146         
3147                 realitycheck(L);
3148                 assert(lua_checkstack(L, 20));
3149                 StackUnroller stack_unroller(L);
3150
3151                 // Get minetest.registered_abms
3152                 lua_getglobal(L, "minetest");
3153                 lua_getfield(L, -1, "registered_abms");
3154                 luaL_checktype(L, -1, LUA_TTABLE);
3155                 int registered_abms = lua_gettop(L);
3156
3157                 // Get minetest.registered_abms[m_id]
3158                 lua_pushnumber(L, m_id);
3159                 lua_gettable(L, registered_abms);
3160                 if(lua_isnil(L, -1))
3161                         assert(0);
3162                 
3163                 // Call action
3164                 luaL_checktype(L, -1, LUA_TTABLE);
3165                 lua_getfield(L, -1, "action");
3166                 luaL_checktype(L, -1, LUA_TFUNCTION);
3167                 push_v3s16(L, p);
3168                 pushnode(L, n, env->getGameDef()->ndef());
3169                 lua_pushnumber(L, active_object_count);
3170                 lua_pushnumber(L, active_object_count_wider);
3171                 if(lua_pcall(L, 4, 0, 0))
3172                         script_error(L, "error: %s", lua_tostring(L, -1));
3173         }
3174 };
3175
3176 // debug(text)
3177 // Writes a line to dstream
3178 static int l_debug(lua_State *L)
3179 {
3180         std::string text = lua_tostring(L, 1);
3181         dstream << text << std::endl;
3182         return 0;
3183 }
3184
3185 // log([level,] text)
3186 // Writes a line to the logger.
3187 // The one-argument version logs to infostream.
3188 // The two-argument version accept a log level: error, action, info, or verbose.
3189 static int l_log(lua_State *L)
3190 {
3191         std::string text;
3192         LogMessageLevel level = LMT_INFO;
3193         if(lua_isnone(L, 2))
3194         {
3195                 text = lua_tostring(L, 1);
3196         }
3197         else
3198         {
3199                 std::string levelname = lua_tostring(L, 1);
3200                 text = lua_tostring(L, 2);
3201                 if(levelname == "error")
3202                         level = LMT_ERROR;
3203                 else if(levelname == "action")
3204                         level = LMT_ACTION;
3205                 else if(levelname == "verbose")
3206                         level = LMT_VERBOSE;
3207         }
3208         log_printline(level, text);
3209         return 0;
3210 }
3211
3212 // register_item_raw({lots of stuff})
3213 static int l_register_item_raw(lua_State *L)
3214 {
3215         luaL_checktype(L, 1, LUA_TTABLE);
3216         int table = 1;
3217
3218         // Get the writable item and node definition managers from the server
3219         IWritableItemDefManager *idef =
3220                         get_server(L)->getWritableItemDefManager();
3221         IWritableNodeDefManager *ndef =
3222                         get_server(L)->getWritableNodeDefManager();
3223
3224         // Check if name is defined
3225         lua_getfield(L, table, "name");
3226         if(lua_isstring(L, -1)){
3227                 std::string name = lua_tostring(L, -1);
3228                 infostream<<"register_item_raw: "<<name<<std::endl;
3229         } else {
3230                 throw LuaError(L, "register_item_raw: name is not defined or not a string");
3231         }
3232
3233         // Check if on_use is defined
3234
3235         // Read the item definition and register it
3236         ItemDefinition def = read_item_definition(L, table);
3237         idef->registerItem(def);
3238
3239         // Read the node definition (content features) and register it
3240         if(def.type == ITEM_NODE)
3241         {
3242                 ContentFeatures f = read_content_features(L, table);
3243                 ndef->set(f.name, f);
3244         }
3245
3246         return 0; /* number of results */
3247 }
3248
3249 // register_alias_raw(name, convert_to_name)
3250 static int l_register_alias_raw(lua_State *L)
3251 {
3252         std::string name = luaL_checkstring(L, 1);
3253         std::string convert_to = luaL_checkstring(L, 2);
3254
3255         // Get the writable item definition manager from the server
3256         IWritableItemDefManager *idef =
3257                         get_server(L)->getWritableItemDefManager();
3258         
3259         idef->registerAlias(name, convert_to);
3260         
3261         return 0; /* number of results */
3262 }
3263
3264 // helper for register_craft
3265 static bool read_craft_recipe_shaped(lua_State *L, int index,
3266                 int &width, std::vector<std::string> &recipe)
3267 {
3268         if(index < 0)
3269                 index = lua_gettop(L) + 1 + index;
3270
3271         if(!lua_istable(L, index))
3272                 return false;
3273
3274         lua_pushnil(L);
3275         int rowcount = 0;
3276         while(lua_next(L, index) != 0){
3277                 int colcount = 0;
3278                 // key at index -2 and value at index -1
3279                 if(!lua_istable(L, -1))
3280                         return false;
3281                 int table2 = lua_gettop(L);
3282                 lua_pushnil(L);
3283                 while(lua_next(L, table2) != 0){
3284                         // key at index -2 and value at index -1
3285                         if(!lua_isstring(L, -1))
3286                                 return false;
3287                         recipe.push_back(lua_tostring(L, -1));
3288                         // removes value, keeps key for next iteration
3289                         lua_pop(L, 1);
3290                         colcount++;
3291                 }
3292                 if(rowcount == 0){
3293                         width = colcount;
3294                 } else {
3295                         if(colcount != width)
3296                                 return false;
3297                 }
3298                 // removes value, keeps key for next iteration
3299                 lua_pop(L, 1);
3300                 rowcount++;
3301         }
3302         return width != 0;
3303 }
3304
3305 // helper for register_craft
3306 static bool read_craft_recipe_shapeless(lua_State *L, int index,
3307                 std::vector<std::string> &recipe)
3308 {
3309         if(index < 0)
3310                 index = lua_gettop(L) + 1 + index;
3311
3312         if(!lua_istable(L, index))
3313                 return false;
3314
3315         lua_pushnil(L);
3316         while(lua_next(L, index) != 0){
3317                 // key at index -2 and value at index -1
3318                 if(!lua_isstring(L, -1))
3319                         return false;
3320                 recipe.push_back(lua_tostring(L, -1));
3321                 // removes value, keeps key for next iteration
3322                 lua_pop(L, 1);
3323         }
3324         return true;
3325 }
3326
3327 // helper for register_craft
3328 static bool read_craft_replacements(lua_State *L, int index,
3329                 CraftReplacements &replacements)
3330 {
3331         if(index < 0)
3332                 index = lua_gettop(L) + 1 + index;
3333
3334         if(!lua_istable(L, index))
3335                 return false;
3336
3337         lua_pushnil(L);
3338         while(lua_next(L, index) != 0){
3339                 // key at index -2 and value at index -1
3340                 if(!lua_istable(L, -1))
3341                         return false;
3342                 lua_rawgeti(L, -1, 1);
3343                 if(!lua_isstring(L, -1))
3344                         return false;
3345                 std::string replace_from = lua_tostring(L, -1);
3346                 lua_pop(L, 1);
3347                 lua_rawgeti(L, -1, 2);
3348                 if(!lua_isstring(L, -1))
3349                         return false;
3350                 std::string replace_to = lua_tostring(L, -1);
3351                 lua_pop(L, 1);
3352                 replacements.pairs.push_back(
3353                                 std::make_pair(replace_from, replace_to));
3354                 // removes value, keeps key for next iteration
3355                 lua_pop(L, 1);
3356         }
3357         return true;
3358 }
3359 // register_craft({output=item, recipe={{item00,item10},{item01,item11}})
3360 static int l_register_craft(lua_State *L)
3361 {
3362         //infostream<<"register_craft"<<std::endl;
3363         luaL_checktype(L, 1, LUA_TTABLE);
3364         int table = 1;
3365
3366         // Get the writable craft definition manager from the server
3367         IWritableCraftDefManager *craftdef =
3368                         get_server(L)->getWritableCraftDefManager();
3369         
3370         std::string type = getstringfield_default(L, table, "type", "shaped");
3371
3372         /*
3373                 CraftDefinitionShaped
3374         */
3375         if(type == "shaped"){
3376                 std::string output = getstringfield_default(L, table, "output", "");
3377                 if(output == "")
3378                         throw LuaError(L, "Crafting definition is missing an output");
3379
3380                 int width = 0;
3381                 std::vector<std::string> recipe;
3382                 lua_getfield(L, table, "recipe");
3383                 if(lua_isnil(L, -1))
3384                         throw LuaError(L, "Crafting definition is missing a recipe"
3385                                         " (output=\"" + output + "\")");
3386                 if(!read_craft_recipe_shaped(L, -1, width, recipe))
3387                         throw LuaError(L, "Invalid crafting recipe"
3388                                         " (output=\"" + output + "\")");
3389
3390                 CraftReplacements replacements;
3391                 lua_getfield(L, table, "replacements");
3392                 if(!lua_isnil(L, -1))
3393                 {
3394                         if(!read_craft_replacements(L, -1, replacements))
3395                                 throw LuaError(L, "Invalid replacements"
3396                                                 " (output=\"" + output + "\")");
3397                 }
3398
3399                 CraftDefinition *def = new CraftDefinitionShaped(
3400                                 output, width, recipe, replacements);
3401                 craftdef->registerCraft(def);
3402         }
3403         /*
3404                 CraftDefinitionShapeless
3405         */
3406         else if(type == "shapeless"){
3407                 std::string output = getstringfield_default(L, table, "output", "");
3408                 if(output == "")
3409                         throw LuaError(L, "Crafting definition (shapeless)"
3410                                         " is missing an output");
3411
3412                 std::vector<std::string> recipe;
3413                 lua_getfield(L, table, "recipe");
3414                 if(lua_isnil(L, -1))
3415                         throw LuaError(L, "Crafting definition (shapeless)"
3416                                         " is missing a recipe"
3417                                         " (output=\"" + output + "\")");
3418                 if(!read_craft_recipe_shapeless(L, -1, recipe))
3419                         throw LuaError(L, "Invalid crafting recipe"
3420                                         " (output=\"" + output + "\")");
3421
3422                 CraftReplacements replacements;
3423                 lua_getfield(L, table, "replacements");
3424                 if(!lua_isnil(L, -1))
3425                 {
3426                         if(!read_craft_replacements(L, -1, replacements))
3427                                 throw LuaError(L, "Invalid replacements"
3428                                                 " (output=\"" + output + "\")");
3429                 }
3430
3431                 CraftDefinition *def = new CraftDefinitionShapeless(
3432                                 output, recipe, replacements);
3433                 craftdef->registerCraft(def);
3434         }
3435         /*
3436                 CraftDefinitionToolRepair
3437         */
3438         else if(type == "toolrepair"){
3439                 float additional_wear = getfloatfield_default(L, table,
3440                                 "additional_wear", 0.0);
3441
3442                 CraftDefinition *def = new CraftDefinitionToolRepair(
3443                                 additional_wear);
3444                 craftdef->registerCraft(def);
3445         }
3446         /*
3447                 CraftDefinitionCooking
3448         */
3449         else if(type == "cooking"){
3450                 std::string output = getstringfield_default(L, table, "output", "");
3451                 if(output == "")
3452                         throw LuaError(L, "Crafting definition (cooking)"
3453                                         " is missing an output");
3454
3455                 std::string recipe = getstringfield_default(L, table, "recipe", "");
3456                 if(recipe == "")
3457                         throw LuaError(L, "Crafting definition (cooking)"
3458                                         " is missing a recipe"
3459                                         " (output=\"" + output + "\")");
3460
3461                 float cooktime = getfloatfield_default(L, table, "cooktime", 3.0);
3462
3463                 CraftDefinition *def = new CraftDefinitionCooking(
3464                                 output, recipe, cooktime);
3465                 craftdef->registerCraft(def);
3466         }
3467         /*
3468                 CraftDefinitionFuel
3469         */
3470         else if(type == "fuel"){
3471                 std::string recipe = getstringfield_default(L, table, "recipe", "");
3472                 if(recipe == "")
3473                         throw LuaError(L, "Crafting definition (fuel)"
3474                                         " is missing a recipe");
3475
3476                 float burntime = getfloatfield_default(L, table, "burntime", 1.0);
3477
3478                 CraftDefinition *def = new CraftDefinitionFuel(
3479                                 recipe, burntime);
3480                 craftdef->registerCraft(def);
3481         }
3482         else
3483         {
3484                 throw LuaError(L, "Unknown crafting definition type: \"" + type + "\"");
3485         }
3486
3487         lua_pop(L, 1);
3488         return 0; /* number of results */
3489 }
3490
3491 // setting_get(name)
3492 static int l_setting_get(lua_State *L)
3493 {
3494         const char *name = luaL_checkstring(L, 1);
3495         try{
3496                 std::string value = g_settings->get(name);
3497                 lua_pushstring(L, value.c_str());
3498         } catch(SettingNotFoundException &e){
3499                 lua_pushnil(L);
3500         }
3501         return 1;
3502 }
3503
3504 // setting_getbool(name)
3505 static int l_setting_getbool(lua_State *L)
3506 {
3507         const char *name = luaL_checkstring(L, 1);
3508         try{
3509                 bool value = g_settings->getBool(name);
3510                 lua_pushboolean(L, value);
3511         } catch(SettingNotFoundException &e){
3512                 lua_pushnil(L);
3513         }
3514         return 1;
3515 }
3516
3517 // chat_send_all(text)
3518 static int l_chat_send_all(lua_State *L)
3519 {
3520         const char *text = luaL_checkstring(L, 1);
3521         // Get server from registry
3522         Server *server = get_server(L);
3523         // Send
3524         server->notifyPlayers(narrow_to_wide(text));
3525         return 0;
3526 }
3527
3528 // chat_send_player(name, text)
3529 static int l_chat_send_player(lua_State *L)
3530 {
3531         const char *name = luaL_checkstring(L, 1);
3532         const char *text = luaL_checkstring(L, 2);
3533         // Get server from registry
3534         Server *server = get_server(L);
3535         // Send
3536         server->notifyPlayer(name, narrow_to_wide(text));
3537         return 0;
3538 }
3539
3540 // get_player_privs(name, text)
3541 static int l_get_player_privs(lua_State *L)
3542 {
3543         const char *name = luaL_checkstring(L, 1);
3544         // Get server from registry
3545         Server *server = get_server(L);
3546         // Do it
3547         lua_newtable(L);
3548         int table = lua_gettop(L);
3549         u64 privs_i = server->getPlayerAuthPrivs(name);
3550         // Special case for the "name" setting (local player / server owner)
3551         if(name == g_settings->get("name"))
3552                 privs_i = PRIV_ALL;
3553         std::set<std::string> privs_s = privsToSet(privs_i);
3554         for(std::set<std::string>::const_iterator
3555                         i = privs_s.begin(); i != privs_s.end(); i++){
3556                 lua_pushboolean(L, true);
3557                 lua_setfield(L, table, i->c_str());
3558         }
3559         lua_pushvalue(L, table);
3560         return 1;
3561 }
3562
3563 // get_inventory(location)
3564 static int l_get_inventory(lua_State *L)
3565 {
3566         InventoryLocation loc;
3567
3568         std::string type = checkstringfield(L, 1, "type");
3569         if(type == "player"){
3570                 std::string name = checkstringfield(L, 1, "name");
3571                 loc.setPlayer(name);
3572         } else if(type == "node"){
3573                 lua_getfield(L, 1, "pos");
3574                 v3s16 pos = check_v3s16(L, -1);
3575                 loc.setNodeMeta(pos);
3576         }
3577         
3578         if(get_server(L)->getInventory(loc) != NULL)
3579                 InvRef::create(L, loc);
3580         else
3581                 lua_pushnil(L);
3582         return 1;
3583 }
3584
3585 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
3586 static int l_get_dig_params(lua_State *L)
3587 {
3588         std::map<std::string, int> groups;
3589         read_groups(L, 1, groups);
3590         ToolCapabilities tp = read_tool_capabilities(L, 2);
3591         if(lua_isnoneornil(L, 3))
3592                 push_dig_params(L, getDigParams(groups, &tp));
3593         else
3594                 push_dig_params(L, getDigParams(groups, &tp,
3595                                         luaL_checknumber(L, 3)));
3596         return 1;
3597 }
3598
3599 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
3600 static int l_get_hit_params(lua_State *L)
3601 {
3602         std::map<std::string, int> groups;
3603         read_groups(L, 1, groups);
3604         ToolCapabilities tp = read_tool_capabilities(L, 2);
3605         if(lua_isnoneornil(L, 3))
3606                 push_hit_params(L, getHitParams(groups, &tp));
3607         else
3608                 push_hit_params(L, getHitParams(groups, &tp,
3609                                         luaL_checknumber(L, 3)));
3610         return 1;
3611 }
3612
3613 // get_current_modname()
3614 static int l_get_current_modname(lua_State *L)
3615 {
3616         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
3617         return 1;
3618 }
3619
3620 // get_modpath(modname)
3621 static int l_get_modpath(lua_State *L)
3622 {
3623         const char *modname = luaL_checkstring(L, 1);
3624         // Do it
3625         const ModSpec *mod = get_server(L)->getModSpec(modname);
3626         if(!mod){
3627                 lua_pushnil(L);
3628                 return 1;
3629         }
3630         lua_pushstring(L, mod->path.c_str());
3631         return 1;
3632 }
3633
3634 // get_worldpath()
3635 static int l_get_worldpath(lua_State *L)
3636 {
3637         std::string worldpath = get_server(L)->getWorldPath();
3638         lua_pushstring(L, worldpath.c_str());
3639         return 1;
3640 }
3641
3642 static const struct luaL_Reg minetest_f [] = {
3643         {"debug", l_debug},
3644         {"log", l_log},
3645         {"register_item_raw", l_register_item_raw},
3646         {"register_alias_raw", l_register_alias_raw},
3647         {"register_craft", l_register_craft},
3648         {"setting_get", l_setting_get},
3649         {"setting_getbool", l_setting_getbool},
3650         {"chat_send_all", l_chat_send_all},
3651         {"chat_send_player", l_chat_send_player},
3652         {"get_player_privs", l_get_player_privs},
3653         {"get_inventory", l_get_inventory},
3654         {"get_dig_params", l_get_dig_params},
3655         {"get_hit_params", l_get_hit_params},
3656         {"get_current_modname", l_get_current_modname},
3657         {"get_modpath", l_get_modpath},
3658         {"get_worldpath", l_get_worldpath},
3659         {NULL, NULL}
3660 };
3661
3662 /*
3663         Main export function
3664 */
3665
3666 void scriptapi_export(lua_State *L, Server *server)
3667 {
3668         realitycheck(L);
3669         assert(lua_checkstack(L, 20));
3670         infostream<<"scriptapi_export"<<std::endl;
3671         StackUnroller stack_unroller(L);
3672
3673         // Store server as light userdata in registry
3674         lua_pushlightuserdata(L, server);
3675         lua_setfield(L, LUA_REGISTRYINDEX, "minetest_server");
3676
3677         // Register global functions in table minetest
3678         lua_newtable(L);
3679         luaL_register(L, NULL, minetest_f);
3680         lua_setglobal(L, "minetest");
3681         
3682         // Get the main minetest table
3683         lua_getglobal(L, "minetest");
3684
3685         // Add tables to minetest
3686         
3687         lua_newtable(L);
3688         lua_setfield(L, -2, "object_refs");
3689         lua_newtable(L);
3690         lua_setfield(L, -2, "luaentities");
3691
3692         // Register wrappers
3693         LuaItemStack::Register(L);
3694         InvRef::Register(L);
3695         NodeMetaRef::Register(L);
3696         ObjectRef::Register(L);
3697         EnvRef::Register(L);
3698 }
3699
3700 bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
3701                 const std::string &modname)
3702 {
3703         ModNameStorer modnamestorer(L, modname);
3704
3705         if(!string_allowed(modname, "abcdefghijklmnopqrstuvwxyz"
3706                         "0123456789_")){
3707                 errorstream<<"Error loading mod \""<<modname
3708                                 <<"\": modname does not follow naming conventions: "
3709                                 <<"Only chararacters [a-z0-9_] are allowed."<<std::endl;
3710                 return false;
3711         }
3712         
3713         bool success = false;
3714
3715         try{
3716                 success = script_load(L, scriptpath.c_str());
3717         }
3718         catch(LuaError &e){
3719                 errorstream<<"Error loading mod \""<<modname
3720                                 <<"\": "<<e.what()<<std::endl;
3721         }
3722
3723         return success;
3724 }
3725
3726 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
3727 {
3728         realitycheck(L);
3729         assert(lua_checkstack(L, 20));
3730         infostream<<"scriptapi_add_environment"<<std::endl;
3731         StackUnroller stack_unroller(L);
3732
3733         // Create EnvRef on stack
3734         EnvRef::create(L, env);
3735         int envref = lua_gettop(L);
3736
3737         // minetest.env = envref
3738         lua_getglobal(L, "minetest");
3739         luaL_checktype(L, -1, LUA_TTABLE);
3740         lua_pushvalue(L, envref);
3741         lua_setfield(L, -2, "env");
3742
3743         // Store environment as light userdata in registry
3744         lua_pushlightuserdata(L, env);
3745         lua_setfield(L, LUA_REGISTRYINDEX, "minetest_env");
3746
3747         /*
3748                 Add ActiveBlockModifiers to environment
3749         */
3750
3751         // Get minetest.registered_abms
3752         lua_getglobal(L, "minetest");
3753         lua_getfield(L, -1, "registered_abms");
3754         luaL_checktype(L, -1, LUA_TTABLE);
3755         int registered_abms = lua_gettop(L);
3756         
3757         if(lua_istable(L, registered_abms)){
3758                 int table = lua_gettop(L);
3759                 lua_pushnil(L);
3760                 while(lua_next(L, table) != 0){
3761                         // key at index -2 and value at index -1
3762                         int id = lua_tonumber(L, -2);
3763                         int current_abm = lua_gettop(L);
3764
3765                         std::set<std::string> trigger_contents;
3766                         lua_getfield(L, current_abm, "nodenames");
3767                         if(lua_istable(L, -1)){
3768                                 int table = lua_gettop(L);
3769                                 lua_pushnil(L);
3770                                 while(lua_next(L, table) != 0){
3771                                         // key at index -2 and value at index -1
3772                                         luaL_checktype(L, -1, LUA_TSTRING);
3773                                         trigger_contents.insert(lua_tostring(L, -1));
3774                                         // removes value, keeps key for next iteration
3775                                         lua_pop(L, 1);
3776                                 }
3777                         } else if(lua_isstring(L, -1)){
3778                                 trigger_contents.insert(lua_tostring(L, -1));
3779                         }
3780                         lua_pop(L, 1);
3781
3782                         std::set<std::string> required_neighbors;
3783                         lua_getfield(L, current_abm, "neighbors");
3784                         if(lua_istable(L, -1)){
3785                                 int table = lua_gettop(L);
3786                                 lua_pushnil(L);
3787                                 while(lua_next(L, table) != 0){
3788                                         // key at index -2 and value at index -1
3789                                         luaL_checktype(L, -1, LUA_TSTRING);
3790                                         required_neighbors.insert(lua_tostring(L, -1));
3791                                         // removes value, keeps key for next iteration
3792                                         lua_pop(L, 1);
3793                                 }
3794                         } else if(lua_isstring(L, -1)){
3795                                 required_neighbors.insert(lua_tostring(L, -1));
3796                         }
3797                         lua_pop(L, 1);
3798
3799                         float trigger_interval = 10.0;
3800                         getfloatfield(L, current_abm, "interval", trigger_interval);
3801
3802                         int trigger_chance = 50;
3803                         getintfield(L, current_abm, "chance", trigger_chance);
3804
3805                         LuaABM *abm = new LuaABM(L, id, trigger_contents,
3806                                         required_neighbors, trigger_interval, trigger_chance);
3807                         
3808                         env->addActiveBlockModifier(abm);
3809
3810                         // removes value, keeps key for next iteration
3811                         lua_pop(L, 1);
3812                 }
3813         }
3814         lua_pop(L, 1);
3815 }
3816
3817 #if 0
3818 // Dump stack top with the dump2 function
3819 static void dump2(lua_State *L, const char *name)
3820 {
3821         // Dump object (debug)
3822         lua_getglobal(L, "dump2");
3823         luaL_checktype(L, -1, LUA_TFUNCTION);
3824         lua_pushvalue(L, -2); // Get previous stack top as first parameter
3825         lua_pushstring(L, name);
3826         if(lua_pcall(L, 2, 0, 0))
3827                 script_error(L, "error: %s", lua_tostring(L, -1));
3828 }
3829 #endif
3830
3831 /*
3832         object_reference
3833 */
3834
3835 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
3836 {
3837         realitycheck(L);
3838         assert(lua_checkstack(L, 20));
3839         //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
3840         StackUnroller stack_unroller(L);
3841
3842         // Create object on stack
3843         ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
3844         int object = lua_gettop(L);
3845
3846         // Get minetest.object_refs table
3847         lua_getglobal(L, "minetest");
3848         lua_getfield(L, -1, "object_refs");
3849         luaL_checktype(L, -1, LUA_TTABLE);
3850         int objectstable = lua_gettop(L);
3851         
3852         // object_refs[id] = object
3853         lua_pushnumber(L, cobj->getId()); // Push id
3854         lua_pushvalue(L, object); // Copy object to top of stack
3855         lua_settable(L, objectstable);
3856 }
3857
3858 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
3859 {
3860         realitycheck(L);
3861         assert(lua_checkstack(L, 20));
3862         //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
3863         StackUnroller stack_unroller(L);
3864
3865         // Get minetest.object_refs table
3866         lua_getglobal(L, "minetest");
3867         lua_getfield(L, -1, "object_refs");
3868         luaL_checktype(L, -1, LUA_TTABLE);
3869         int objectstable = lua_gettop(L);
3870         
3871         // Get object_refs[id]
3872         lua_pushnumber(L, cobj->getId()); // Push id
3873         lua_gettable(L, objectstable);
3874         // Set object reference to NULL
3875         ObjectRef::set_null(L);
3876         lua_pop(L, 1); // pop object
3877
3878         // Set object_refs[id] = nil
3879         lua_pushnumber(L, cobj->getId()); // Push id
3880         lua_pushnil(L);
3881         lua_settable(L, objectstable);
3882 }
3883
3884 bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
3885                 const std::string &message)
3886 {
3887         realitycheck(L);
3888         assert(lua_checkstack(L, 20));
3889         StackUnroller stack_unroller(L);
3890
3891         // Get minetest.registered_on_chat_messages
3892         lua_getglobal(L, "minetest");
3893         lua_getfield(L, -1, "registered_on_chat_messages");
3894         luaL_checktype(L, -1, LUA_TTABLE);
3895         int table = lua_gettop(L);
3896         // Foreach
3897         lua_pushnil(L);
3898         while(lua_next(L, table) != 0){
3899                 // key at index -2 and value at index -1
3900                 luaL_checktype(L, -1, LUA_TFUNCTION);
3901                 // Call function
3902                 lua_pushstring(L, name.c_str());
3903                 lua_pushstring(L, message.c_str());
3904                 if(lua_pcall(L, 2, 1, 0))
3905                         script_error(L, "error: %s", lua_tostring(L, -1));
3906                 bool ate = lua_toboolean(L, -1);
3907                 lua_pop(L, 1);
3908                 if(ate)
3909                         return true;
3910                 // value removed, keep key for next iteration
3911         }
3912         return false;
3913 }
3914
3915 /*
3916         misc
3917 */
3918
3919 void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player)
3920 {
3921         realitycheck(L);
3922         assert(lua_checkstack(L, 20));
3923         StackUnroller stack_unroller(L);
3924
3925         // Get minetest.registered_on_newplayers
3926         lua_getglobal(L, "minetest");
3927         lua_getfield(L, -1, "registered_on_newplayers");
3928         luaL_checktype(L, -1, LUA_TTABLE);
3929         int table = lua_gettop(L);
3930         // Foreach
3931         lua_pushnil(L);
3932         while(lua_next(L, table) != 0){
3933                 // key at index -2 and value at index -1
3934                 luaL_checktype(L, -1, LUA_TFUNCTION);
3935                 // Call function
3936                 objectref_get_or_create(L, player);
3937                 if(lua_pcall(L, 1, 0, 0))
3938                         script_error(L, "error: %s", lua_tostring(L, -1));
3939                 // value removed, keep key for next iteration
3940         }
3941 }
3942
3943 void scriptapi_on_dieplayer(lua_State *L, ServerActiveObject *player)
3944 {
3945     realitycheck(L);
3946     assert(lua_checkstack(L, 20));
3947     StackUnroller stack_unroller(L);
3948     
3949     // Get minetest.registered_on_dieplayers
3950     lua_getglobal(L, "minetest");
3951     lua_getfield(L, -1, "registered_on_dieplayers");
3952     luaL_checktype(L, -1, LUA_TTABLE);
3953     int table = lua_gettop(L);
3954     // Foreach
3955     lua_pushnil(L);
3956     while(lua_next(L, table) != 0){
3957         // key at index -2 and value at index -1
3958        luaL_checktype(L, -1, LUA_TFUNCTION);
3959         // Call function
3960        objectref_get_or_create(L, player);
3961         if(lua_pcall(L, 1, 0, 0))
3962             script_error(L, "error: %s", lua_tostring(L, -1));
3963         // value removed, keep key for next iteration
3964     }
3965 }
3966
3967
3968 bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player)
3969 {
3970         realitycheck(L);
3971         assert(lua_checkstack(L, 20));
3972         StackUnroller stack_unroller(L);
3973
3974         dstream<<"player: "<<player<<"   id: "<<player->getId()<<std::endl;
3975
3976         bool positioning_handled_by_some = false;
3977
3978         // Get minetest.registered_on_respawnplayers
3979         lua_getglobal(L, "minetest");
3980         lua_getfield(L, -1, "registered_on_respawnplayers");
3981         luaL_checktype(L, -1, LUA_TTABLE);
3982         int table = lua_gettop(L);
3983         // Foreach
3984         lua_pushnil(L);
3985         while(lua_next(L, table) != 0){
3986                 // key at index -2 and value at index -1
3987                 luaL_checktype(L, -1, LUA_TFUNCTION);
3988                 // Call function
3989                 objectref_get_or_create(L, player);
3990                 if(lua_pcall(L, 1, 1, 0))
3991                         script_error(L, "error: %s", lua_tostring(L, -1));
3992                 bool positioning_handled = lua_toboolean(L, -1);
3993                 lua_pop(L, 1);
3994                 if(positioning_handled)
3995                         positioning_handled_by_some = true;
3996                 // value removed, keep key for next iteration
3997         }
3998         return positioning_handled_by_some;
3999 }
4000
4001 void scriptapi_get_creative_inventory(lua_State *L, ServerRemotePlayer *player)
4002 {
4003         lua_getglobal(L, "minetest");
4004         lua_getfield(L, -1, "creative_inventory");
4005         luaL_checktype(L, -1, LUA_TTABLE);
4006         inventory_set_list_from_lua(&player->inventory, "main", L, -1,
4007                         PLAYER_INVENTORY_SIZE);
4008 }
4009
4010 /*
4011         item callbacks and node callbacks
4012 */
4013
4014 // Retrieves minetest.registered_items[name][callbackname]
4015 // If that is nil or on error, return false and stack is unchanged
4016 // If that is a function, returns true and pushes the
4017 // function onto the stack
4018 static bool get_item_callback(lua_State *L,
4019                 const char *name, const char *callbackname)
4020 {
4021         lua_getglobal(L, "minetest");
4022         lua_getfield(L, -1, "registered_items");
4023         lua_remove(L, -2);
4024         luaL_checktype(L, -1, LUA_TTABLE);
4025         lua_getfield(L, -1, name);
4026         lua_remove(L, -2);
4027         // Should be a table
4028         if(lua_type(L, -1) != LUA_TTABLE)
4029         {
4030                 errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
4031                 lua_pop(L, 1);
4032                 return false;
4033         }
4034         lua_getfield(L, -1, callbackname);
4035         lua_remove(L, -2);
4036         // Should be a function or nil
4037         if(lua_type(L, -1) == LUA_TFUNCTION)
4038         {
4039                 return true;
4040         }
4041         else if(lua_isnil(L, -1))
4042         {
4043                 lua_pop(L, 1);
4044                 return false;
4045         }
4046         else
4047         {
4048                 errorstream<<"Item \""<<name<<"\" callback \""
4049                         <<callbackname<<" is not a function"<<std::endl;
4050                 lua_pop(L, 1);
4051                 return false;
4052         }
4053 }
4054
4055 bool scriptapi_item_on_drop(lua_State *L, ItemStack &item,
4056                 ServerActiveObject *dropper, v3f pos)
4057 {
4058         realitycheck(L);
4059         assert(lua_checkstack(L, 20));
4060         StackUnroller stack_unroller(L);
4061
4062         // Push callback function on stack
4063         if(!get_item_callback(L, item.name.c_str(), "on_drop"))
4064                 return false;
4065
4066         // Call function
4067         LuaItemStack::create(L, item);
4068         objectref_get_or_create(L, dropper);
4069         pushFloatPos(L, pos);
4070         if(lua_pcall(L, 3, 1, 0))
4071                 script_error(L, "error: %s", lua_tostring(L, -1));
4072         if(!lua_isnil(L, -1))
4073                 item = read_item(L, -1);
4074         return true;
4075 }
4076
4077 bool scriptapi_item_on_place(lua_State *L, ItemStack &item,
4078                 ServerActiveObject *placer, const PointedThing &pointed)
4079 {
4080         realitycheck(L);
4081         assert(lua_checkstack(L, 20));
4082         StackUnroller stack_unroller(L);
4083
4084         // Push callback function on stack
4085         if(!get_item_callback(L, item.name.c_str(), "on_place"))
4086                 return false;
4087
4088         // Call function
4089         LuaItemStack::create(L, item);
4090         objectref_get_or_create(L, placer);
4091         push_pointed_thing(L, pointed);
4092         if(lua_pcall(L, 3, 1, 0))
4093                 script_error(L, "error: %s", lua_tostring(L, -1));
4094         if(!lua_isnil(L, -1))
4095                 item = read_item(L, -1);
4096         return true;
4097 }
4098
4099 bool scriptapi_item_on_use(lua_State *L, ItemStack &item,
4100                 ServerActiveObject *user, const PointedThing &pointed)
4101 {
4102         realitycheck(L);
4103         assert(lua_checkstack(L, 20));
4104         StackUnroller stack_unroller(L);
4105
4106         // Push callback function on stack
4107         if(!get_item_callback(L, item.name.c_str(), "on_use"))
4108                 return false;
4109
4110         // Call function
4111         LuaItemStack::create(L, item);
4112         objectref_get_or_create(L, user);
4113         push_pointed_thing(L, pointed);
4114         if(lua_pcall(L, 3, 1, 0))
4115                 script_error(L, "error: %s", lua_tostring(L, -1));
4116         if(!lua_isnil(L, -1))
4117                 item = read_item(L, -1);
4118         return true;
4119 }
4120
4121 bool scriptapi_node_on_punch(lua_State *L, v3s16 pos, MapNode node,
4122                 ServerActiveObject *puncher)
4123 {
4124         realitycheck(L);
4125         assert(lua_checkstack(L, 20));
4126         StackUnroller stack_unroller(L);
4127
4128         INodeDefManager *ndef = get_server(L)->ndef();
4129
4130         // Push callback function on stack
4131         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_punch"))
4132                 return false;
4133
4134         // Call function
4135         push_v3s16(L, pos);
4136         pushnode(L, node, ndef);
4137         objectref_get_or_create(L, puncher);
4138         if(lua_pcall(L, 3, 0, 0))
4139                 script_error(L, "error: %s", lua_tostring(L, -1));
4140         return true;
4141 }
4142
4143 bool scriptapi_node_on_dig(lua_State *L, v3s16 pos, MapNode node,
4144                 ServerActiveObject *digger)
4145 {
4146         realitycheck(L);
4147         assert(lua_checkstack(L, 20));
4148         StackUnroller stack_unroller(L);
4149
4150         INodeDefManager *ndef = get_server(L)->ndef();
4151
4152         // Push callback function on stack
4153         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_dig"))
4154                 return false;
4155
4156         // Call function
4157         push_v3s16(L, pos);
4158         pushnode(L, node, ndef);
4159         objectref_get_or_create(L, digger);
4160         if(lua_pcall(L, 3, 0, 0))
4161                 script_error(L, "error: %s", lua_tostring(L, -1));
4162         return true;
4163 }
4164
4165 /*
4166         environment
4167 */
4168
4169 void scriptapi_environment_step(lua_State *L, float dtime)
4170 {
4171         realitycheck(L);
4172         assert(lua_checkstack(L, 20));
4173         //infostream<<"scriptapi_environment_step"<<std::endl;
4174         StackUnroller stack_unroller(L);
4175
4176         // Get minetest.registered_globalsteps
4177         lua_getglobal(L, "minetest");
4178         lua_getfield(L, -1, "registered_globalsteps");
4179         luaL_checktype(L, -1, LUA_TTABLE);
4180         int table = lua_gettop(L);
4181         // Foreach
4182         lua_pushnil(L);
4183         while(lua_next(L, table) != 0){
4184                 // key at index -2 and value at index -1
4185                 luaL_checktype(L, -1, LUA_TFUNCTION);
4186                 // Call function
4187                 lua_pushnumber(L, dtime);
4188                 if(lua_pcall(L, 1, 0, 0))
4189                         script_error(L, "error: %s", lua_tostring(L, -1));
4190                 // value removed, keep key for next iteration
4191         }
4192 }
4193
4194 void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp)
4195 {
4196         realitycheck(L);
4197         assert(lua_checkstack(L, 20));
4198         //infostream<<"scriptapi_environment_on_generated"<<std::endl;
4199         StackUnroller stack_unroller(L);
4200
4201         // Get minetest.registered_on_generateds
4202         lua_getglobal(L, "minetest");
4203         lua_getfield(L, -1, "registered_on_generateds");
4204         luaL_checktype(L, -1, LUA_TTABLE);
4205         int table = lua_gettop(L);
4206         // Foreach
4207         lua_pushnil(L);
4208         while(lua_next(L, table) != 0){
4209                 // key at index -2 and value at index -1
4210                 luaL_checktype(L, -1, LUA_TFUNCTION);
4211                 // Call function
4212                 push_v3s16(L, minp);
4213                 push_v3s16(L, maxp);
4214                 if(lua_pcall(L, 2, 0, 0))
4215                         script_error(L, "error: %s", lua_tostring(L, -1));
4216                 // value removed, keep key for next iteration
4217         }
4218 }
4219
4220 /*
4221         luaentity
4222 */
4223
4224 bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
4225                 const std::string &staticdata)
4226 {
4227         realitycheck(L);
4228         assert(lua_checkstack(L, 20));
4229         infostream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
4230                         <<name<<"\""<<std::endl;
4231         StackUnroller stack_unroller(L);
4232         
4233         // Get minetest.registered_entities[name]
4234         lua_getglobal(L, "minetest");
4235         lua_getfield(L, -1, "registered_entities");
4236         luaL_checktype(L, -1, LUA_TTABLE);
4237         lua_pushstring(L, name);
4238         lua_gettable(L, -2);
4239         // Should be a table, which we will use as a prototype
4240         //luaL_checktype(L, -1, LUA_TTABLE);
4241         if(lua_type(L, -1) != LUA_TTABLE){
4242                 errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
4243                 return false;
4244         }
4245         int prototype_table = lua_gettop(L);
4246         //dump2(L, "prototype_table");
4247         
4248         // Create entity object
4249         lua_newtable(L);
4250         int object = lua_gettop(L);
4251
4252         // Set object metatable
4253         lua_pushvalue(L, prototype_table);
4254         lua_setmetatable(L, -2);
4255         
4256         // Add object reference
4257         // This should be userdata with metatable ObjectRef
4258         objectref_get(L, id);
4259         luaL_checktype(L, -1, LUA_TUSERDATA);
4260         if(!luaL_checkudata(L, -1, "ObjectRef"))
4261                 luaL_typerror(L, -1, "ObjectRef");
4262         lua_setfield(L, -2, "object");
4263
4264         // minetest.luaentities[id] = object
4265         lua_getglobal(L, "minetest");
4266         lua_getfield(L, -1, "luaentities");
4267         luaL_checktype(L, -1, LUA_TTABLE);
4268         lua_pushnumber(L, id); // Push id
4269         lua_pushvalue(L, object); // Copy object to top of stack
4270         lua_settable(L, -3);
4271         
4272         // Get on_activate function
4273         lua_pushvalue(L, object);
4274         lua_getfield(L, -1, "on_activate");
4275         if(!lua_isnil(L, -1)){
4276                 luaL_checktype(L, -1, LUA_TFUNCTION);
4277                 lua_pushvalue(L, object); // self
4278                 lua_pushlstring(L, staticdata.c_str(), staticdata.size());
4279                 // Call with 2 arguments, 0 results
4280                 if(lua_pcall(L, 2, 0, 0))
4281                         script_error(L, "error running function %s:on_activate: %s\n",
4282                                         name, lua_tostring(L, -1));
4283         }
4284         
4285         return true;
4286 }
4287
4288 void scriptapi_luaentity_rm(lua_State *L, u16 id)
4289 {
4290         realitycheck(L);
4291         assert(lua_checkstack(L, 20));
4292         infostream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
4293
4294         // Get minetest.luaentities table
4295         lua_getglobal(L, "minetest");
4296         lua_getfield(L, -1, "luaentities");
4297         luaL_checktype(L, -1, LUA_TTABLE);
4298         int objectstable = lua_gettop(L);
4299         
4300         // Set luaentities[id] = nil
4301         lua_pushnumber(L, id); // Push id
4302         lua_pushnil(L);
4303         lua_settable(L, objectstable);
4304         
4305         lua_pop(L, 2); // pop luaentities, minetest
4306 }
4307
4308 std::string scriptapi_luaentity_get_staticdata(lua_State *L, u16 id)
4309 {
4310         realitycheck(L);
4311         assert(lua_checkstack(L, 20));
4312         infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
4313         StackUnroller stack_unroller(L);
4314
4315         // Get minetest.luaentities[id]
4316         luaentity_get(L, id);
4317         int object = lua_gettop(L);
4318         
4319         // Get get_staticdata function
4320         lua_pushvalue(L, object);
4321         lua_getfield(L, -1, "get_staticdata");
4322         if(lua_isnil(L, -1))
4323                 return "";
4324         
4325         luaL_checktype(L, -1, LUA_TFUNCTION);
4326         lua_pushvalue(L, object); // self
4327         // Call with 1 arguments, 1 results
4328         if(lua_pcall(L, 1, 1, 0))
4329                 script_error(L, "error running function get_staticdata: %s\n",
4330                                 lua_tostring(L, -1));
4331         
4332         size_t len=0;
4333         const char *s = lua_tolstring(L, -1, &len);
4334         return std::string(s, len);
4335 }
4336
4337 void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
4338                 LuaEntityProperties *prop)
4339 {
4340         realitycheck(L);
4341         assert(lua_checkstack(L, 20));
4342         infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
4343         StackUnroller stack_unroller(L);
4344
4345         // Get minetest.luaentities[id]
4346         luaentity_get(L, id);
4347         //int object = lua_gettop(L);
4348
4349         /* Read stuff */
4350         
4351         getboolfield(L, -1, "physical", prop->physical);
4352
4353         getfloatfield(L, -1, "weight", prop->weight);
4354
4355         lua_getfield(L, -1, "collisionbox");
4356         if(lua_istable(L, -1))
4357                 prop->collisionbox = read_aabbox3df32(L, -1, 1.0);
4358         lua_pop(L, 1);
4359
4360         getstringfield(L, -1, "visual", prop->visual);
4361         
4362         lua_getfield(L, -1, "visual_size");
4363         if(lua_istable(L, -1))
4364                 prop->visual_size = read_v2f(L, -1);
4365         lua_pop(L, 1);
4366
4367         lua_getfield(L, -1, "textures");
4368         if(lua_istable(L, -1)){
4369                 prop->textures.clear();
4370                 int table = lua_gettop(L);
4371                 lua_pushnil(L);
4372                 while(lua_next(L, table) != 0){
4373                         // key at index -2 and value at index -1
4374                         if(lua_isstring(L, -1))
4375                                 prop->textures.push_back(lua_tostring(L, -1));
4376                         else
4377                                 prop->textures.push_back("");
4378                         // removes value, keeps key for next iteration
4379                         lua_pop(L, 1);
4380                 }
4381         }
4382         lua_pop(L, 1);
4383         
4384         lua_getfield(L, -1, "spritediv");
4385         if(lua_istable(L, -1))
4386                 prop->spritediv = read_v2s16(L, -1);
4387         lua_pop(L, 1);
4388
4389         lua_getfield(L, -1, "initial_sprite_basepos");
4390         if(lua_istable(L, -1))
4391                 prop->initial_sprite_basepos = read_v2s16(L, -1);
4392         lua_pop(L, 1);
4393 }
4394
4395 void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime)
4396 {
4397         realitycheck(L);
4398         assert(lua_checkstack(L, 20));
4399         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
4400         StackUnroller stack_unroller(L);
4401
4402         // Get minetest.luaentities[id]
4403         luaentity_get(L, id);
4404         int object = lua_gettop(L);
4405         // State: object is at top of stack
4406         // Get step function
4407         lua_getfield(L, -1, "on_step");
4408         if(lua_isnil(L, -1))
4409                 return;
4410         luaL_checktype(L, -1, LUA_TFUNCTION);
4411         lua_pushvalue(L, object); // self
4412         lua_pushnumber(L, dtime); // dtime
4413         // Call with 2 arguments, 0 results
4414         if(lua_pcall(L, 2, 0, 0))
4415                 script_error(L, "error running function 'on_step': %s\n", lua_tostring(L, -1));
4416 }
4417
4418 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch)
4419 void scriptapi_luaentity_punch(lua_State *L, u16 id,
4420                 ServerActiveObject *puncher, float time_from_last_punch)
4421 {
4422         realitycheck(L);
4423         assert(lua_checkstack(L, 20));
4424         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
4425         StackUnroller stack_unroller(L);
4426
4427         // Get minetest.luaentities[id]
4428         luaentity_get(L, id);
4429         int object = lua_gettop(L);
4430         // State: object is at top of stack
4431         // Get function
4432         lua_getfield(L, -1, "on_punch");
4433         if(lua_isnil(L, -1))
4434                 return;
4435         luaL_checktype(L, -1, LUA_TFUNCTION);
4436         lua_pushvalue(L, object); // self
4437         objectref_get_or_create(L, puncher); // Clicker reference
4438         lua_pushnumber(L, time_from_last_punch);
4439         // Call with 2 arguments, 0 results
4440         if(lua_pcall(L, 3, 0, 0))
4441                 script_error(L, "error running function 'on_punch': %s\n", lua_tostring(L, -1));
4442 }
4443
4444 // Calls entity:on_rightclick(ObjectRef clicker)
4445 void scriptapi_luaentity_rightclick(lua_State *L, u16 id,
4446                 ServerActiveObject *clicker)
4447 {
4448         realitycheck(L);
4449         assert(lua_checkstack(L, 20));
4450         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
4451         StackUnroller stack_unroller(L);
4452
4453         // Get minetest.luaentities[id]
4454         luaentity_get(L, id);
4455         int object = lua_gettop(L);
4456         // State: object is at top of stack
4457         // Get function
4458         lua_getfield(L, -1, "on_rightclick");
4459         if(lua_isnil(L, -1))
4460                 return;
4461         luaL_checktype(L, -1, LUA_TFUNCTION);
4462         lua_pushvalue(L, object); // self
4463         objectref_get_or_create(L, clicker); // Clicker reference
4464         // Call with 2 arguments, 0 results
4465         if(lua_pcall(L, 2, 0, 0))
4466                 script_error(L, "error running function 'on_rightclick': %s\n", lua_tostring(L, -1));
4467 }
4468