Joint positioning and rotation code, and fix a problem related to their lua API
[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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "scriptapi.h"
21
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 "object_properties.h"
37 #include "content_sao.h" // For LuaEntitySAO and PlayerSAO
38 #include "itemdef.h"
39 #include "nodedef.h"
40 #include "craftdef.h"
41 #include "main.h" // For g_settings
42 #include "settings.h" // For accessing g_settings
43 #include "nodemetadata.h"
44 #include "mapblock.h" // For getNodeBlockPos
45 #include "content_nodemeta.h"
46 #include "tool.h"
47 #include "daynightratio.h"
48 #include "noise.h" // PseudoRandom for LuaPseudoRandom
49 #include "util/pointedthing.h"
50 #include "rollback.h"
51
52 static void stackDump(lua_State *L, std::ostream &o)
53 {
54   int i;
55   int top = lua_gettop(L);
56   for (i = 1; i <= top; i++) {  /* repeat for each level */
57         int t = lua_type(L, i);
58         switch (t) {
59
60           case LUA_TSTRING:  /* strings */
61                 o<<"\""<<lua_tostring(L, i)<<"\"";
62                 break;
63
64           case LUA_TBOOLEAN:  /* booleans */
65                 o<<(lua_toboolean(L, i) ? "true" : "false");
66                 break;
67
68           case LUA_TNUMBER:  /* numbers */ {
69                 char buf[10];
70                 snprintf(buf, 10, "%g", lua_tonumber(L, i));
71                 o<<buf;
72                 break; }
73
74           default:  /* other values */
75                 o<<lua_typename(L, t);
76                 break;
77
78         }
79         o<<" ";
80   }
81   o<<std::endl;
82 }
83
84 static void realitycheck(lua_State *L)
85 {
86         int top = lua_gettop(L);
87         if(top >= 30){
88                 dstream<<"Stack is over 30:"<<std::endl;
89                 stackDump(L, dstream);
90                 script_error(L, "Stack is over 30 (reality check)");
91         }
92 }
93
94 class StackUnroller
95 {
96 private:
97         lua_State *m_lua;
98         int m_original_top;
99 public:
100         StackUnroller(lua_State *L):
101                 m_lua(L),
102                 m_original_top(-1)
103         {
104                 m_original_top = lua_gettop(m_lua); // store stack height
105         }
106         ~StackUnroller()
107         {
108                 lua_settop(m_lua, m_original_top); // restore stack height
109         }
110 };
111
112 class ModNameStorer
113 {
114 private:
115         lua_State *L;
116 public:
117         ModNameStorer(lua_State *L_, const std::string modname):
118                 L(L_)
119         {
120                 // Store current modname in registry
121                 lua_pushstring(L, modname.c_str());
122                 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
123         }
124         ~ModNameStorer()
125         {
126                 // Clear current modname in registry
127                 lua_pushnil(L);
128                 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
129         }
130 };
131
132 /*
133         Getters for stuff in main tables
134 */
135
136 static Server* get_server(lua_State *L)
137 {
138         // Get server from registry
139         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
140         Server *server = (Server*)lua_touserdata(L, -1);
141         lua_pop(L, 1);
142         return server;
143 }
144
145 static ServerEnvironment* get_env(lua_State *L)
146 {
147         // Get environment from registry
148         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_env");
149         ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1);
150         lua_pop(L, 1);
151         return env;
152 }
153
154 static void objectref_get(lua_State *L, u16 id)
155 {
156         // Get minetest.object_refs[i]
157         lua_getglobal(L, "minetest");
158         lua_getfield(L, -1, "object_refs");
159         luaL_checktype(L, -1, LUA_TTABLE);
160         lua_pushnumber(L, id);
161         lua_gettable(L, -2);
162         lua_remove(L, -2); // object_refs
163         lua_remove(L, -2); // minetest
164 }
165
166 static void luaentity_get(lua_State *L, u16 id)
167 {
168         // Get minetest.luaentities[i]
169         lua_getglobal(L, "minetest");
170         lua_getfield(L, -1, "luaentities");
171         luaL_checktype(L, -1, LUA_TTABLE);
172         lua_pushnumber(L, id);
173         lua_gettable(L, -2);
174         lua_remove(L, -2); // luaentities
175         lua_remove(L, -2); // minetest
176 }
177
178 /*
179         Table field getters
180 */
181
182 static bool getstringfield(lua_State *L, int table,
183                 const char *fieldname, std::string &result)
184 {
185         lua_getfield(L, table, fieldname);
186         bool got = false;
187         if(lua_isstring(L, -1)){
188                 size_t len = 0;
189                 const char *ptr = lua_tolstring(L, -1, &len);
190                 result.assign(ptr, len);
191                 got = true;
192         }
193         lua_pop(L, 1);
194         return got;
195 }
196
197 static bool getintfield(lua_State *L, int table,
198                 const char *fieldname, int &result)
199 {
200         lua_getfield(L, table, fieldname);
201         bool got = false;
202         if(lua_isnumber(L, -1)){
203                 result = lua_tonumber(L, -1);
204                 got = true;
205         }
206         lua_pop(L, 1);
207         return got;
208 }
209
210 static bool getfloatfield(lua_State *L, int table,
211                 const char *fieldname, float &result)
212 {
213         lua_getfield(L, table, fieldname);
214         bool got = false;
215         if(lua_isnumber(L, -1)){
216                 result = lua_tonumber(L, -1);
217                 got = true;
218         }
219         lua_pop(L, 1);
220         return got;
221 }
222
223 static bool getboolfield(lua_State *L, int table,
224                 const char *fieldname, bool &result)
225 {
226         lua_getfield(L, table, fieldname);
227         bool got = false;
228         if(lua_isboolean(L, -1)){
229                 result = lua_toboolean(L, -1);
230                 got = true;
231         }
232         lua_pop(L, 1);
233         return got;
234 }
235
236 static std::string checkstringfield(lua_State *L, int table,
237                 const char *fieldname)
238 {
239         lua_getfield(L, table, fieldname);
240         std::string s = luaL_checkstring(L, -1);
241         lua_pop(L, 1);
242         return s;
243 }
244
245 static std::string getstringfield_default(lua_State *L, int table,
246                 const char *fieldname, const std::string &default_)
247 {
248         std::string result = default_;
249         getstringfield(L, table, fieldname, result);
250         return result;
251 }
252
253 static int getintfield_default(lua_State *L, int table,
254                 const char *fieldname, int default_)
255 {
256         int result = default_;
257         getintfield(L, table, fieldname, result);
258         return result;
259 }
260
261 static float getfloatfield_default(lua_State *L, int table,
262                 const char *fieldname, float default_)
263 {
264         float result = default_;
265         getfloatfield(L, table, fieldname, result);
266         return result;
267 }
268
269 static bool getboolfield_default(lua_State *L, int table,
270                 const char *fieldname, bool default_)
271 {
272         bool result = default_;
273         getboolfield(L, table, fieldname, result);
274         return result;
275 }
276
277 struct EnumString
278 {
279         int num;
280         const char *str;
281 };
282
283 static bool string_to_enum(const EnumString *spec, int &result,
284                 const std::string &str)
285 {
286         const EnumString *esp = spec;
287         while(esp->str){
288                 if(str == std::string(esp->str)){
289                         result = esp->num;
290                         return true;
291                 }
292                 esp++;
293         }
294         return false;
295 }
296
297 /*static bool enum_to_string(const EnumString *spec, std::string &result,
298                 int num)
299 {
300         const EnumString *esp = spec;
301         while(esp){
302                 if(num == esp->num){
303                         result = esp->str;
304                         return true;
305                 }
306                 esp++;
307         }
308         return false;
309 }*/
310
311 static int getenumfield(lua_State *L, int table,
312                 const char *fieldname, const EnumString *spec, int default_)
313 {
314         int result = default_;
315         string_to_enum(spec, result,
316                         getstringfield_default(L, table, fieldname, ""));
317         return result;
318 }
319
320 static void setintfield(lua_State *L, int table,
321                 const char *fieldname, int value)
322 {
323         lua_pushinteger(L, value);
324         if(table < 0)
325                 table -= 1;
326         lua_setfield(L, table, fieldname);
327 }
328
329 static void setfloatfield(lua_State *L, int table,
330                 const char *fieldname, float value)
331 {
332         lua_pushnumber(L, value);
333         if(table < 0)
334                 table -= 1;
335         lua_setfield(L, table, fieldname);
336 }
337
338 static void setboolfield(lua_State *L, int table,
339                 const char *fieldname, bool value)
340 {
341         lua_pushboolean(L, value);
342         if(table < 0)
343                 table -= 1;
344         lua_setfield(L, table, fieldname);
345 }
346
347 static void warn_if_field_exists(lua_State *L, int table,
348                 const char *fieldname, const std::string &message)
349 {
350         lua_getfield(L, table, fieldname);
351         if(!lua_isnil(L, -1)){
352                 infostream<<script_get_backtrace(L)<<std::endl;
353                 infostream<<"WARNING: field \""<<fieldname<<"\": "
354                                 <<message<<std::endl;
355         }
356         lua_pop(L, 1);
357 }
358
359 /*
360         EnumString definitions
361 */
362
363 struct EnumString es_ItemType[] =
364 {
365         {ITEM_NONE, "none"},
366         {ITEM_NODE, "node"},
367         {ITEM_CRAFT, "craft"},
368         {ITEM_TOOL, "tool"},
369         {0, NULL},
370 };
371
372 struct EnumString es_DrawType[] =
373 {
374         {NDT_NORMAL, "normal"},
375         {NDT_AIRLIKE, "airlike"},
376         {NDT_LIQUID, "liquid"},
377         {NDT_FLOWINGLIQUID, "flowingliquid"},
378         {NDT_GLASSLIKE, "glasslike"},
379         {NDT_ALLFACES, "allfaces"},
380         {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
381         {NDT_TORCHLIKE, "torchlike"},
382         {NDT_SIGNLIKE, "signlike"},
383         {NDT_PLANTLIKE, "plantlike"},
384         {NDT_FENCELIKE, "fencelike"},
385         {NDT_RAILLIKE, "raillike"},
386         {NDT_NODEBOX, "nodebox"},
387         {0, NULL},
388 };
389
390 struct EnumString es_ContentParamType[] =
391 {
392         {CPT_NONE, "none"},
393         {CPT_LIGHT, "light"},
394         {0, NULL},
395 };
396
397 struct EnumString es_ContentParamType2[] =
398 {
399         {CPT2_NONE, "none"},
400         {CPT2_FULL, "full"},
401         {CPT2_FLOWINGLIQUID, "flowingliquid"},
402         {CPT2_FACEDIR, "facedir"},
403         {CPT2_WALLMOUNTED, "wallmounted"},
404         {0, NULL},
405 };
406
407 struct EnumString es_LiquidType[] =
408 {
409         {LIQUID_NONE, "none"},
410         {LIQUID_FLOWING, "flowing"},
411         {LIQUID_SOURCE, "source"},
412         {0, NULL},
413 };
414
415 struct EnumString es_NodeBoxType[] =
416 {
417         {NODEBOX_REGULAR, "regular"},
418         {NODEBOX_FIXED, "fixed"},
419         {NODEBOX_WALLMOUNTED, "wallmounted"},
420         {0, NULL},
421 };
422
423 struct EnumString es_CraftMethod[] =
424 {
425         {CRAFT_METHOD_NORMAL, "normal"},
426         {CRAFT_METHOD_COOKING, "cooking"},
427         {CRAFT_METHOD_FUEL, "fuel"},
428         {0, NULL},
429 };
430
431 struct EnumString es_TileAnimationType[] =
432 {
433         {TAT_NONE, "none"},
434         {TAT_VERTICAL_FRAMES, "vertical_frames"},
435         {0, NULL},
436 };
437
438 /*
439         C struct <-> Lua table converter functions
440 */
441
442 static void push_v3f(lua_State *L, v3f p)
443 {
444         lua_newtable(L);
445         lua_pushnumber(L, p.X);
446         lua_setfield(L, -2, "x");
447         lua_pushnumber(L, p.Y);
448         lua_setfield(L, -2, "y");
449         lua_pushnumber(L, p.Z);
450         lua_setfield(L, -2, "z");
451 }
452
453 static v2s16 read_v2s16(lua_State *L, int index)
454 {
455         v2s16 p;
456         luaL_checktype(L, index, LUA_TTABLE);
457         lua_getfield(L, index, "x");
458         p.X = lua_tonumber(L, -1);
459         lua_pop(L, 1);
460         lua_getfield(L, index, "y");
461         p.Y = lua_tonumber(L, -1);
462         lua_pop(L, 1);
463         return p;
464 }
465
466 static v2f read_v2f(lua_State *L, int index)
467 {
468         v2f p;
469         luaL_checktype(L, index, LUA_TTABLE);
470         lua_getfield(L, index, "x");
471         p.X = lua_tonumber(L, -1);
472         lua_pop(L, 1);
473         lua_getfield(L, index, "y");
474         p.Y = lua_tonumber(L, -1);
475         lua_pop(L, 1);
476         return p;
477 }
478
479 static v3f read_v3f(lua_State *L, int index)
480 {
481         v3f pos;
482         luaL_checktype(L, index, LUA_TTABLE);
483         lua_getfield(L, index, "x");
484         pos.X = lua_tonumber(L, -1);
485         lua_pop(L, 1);
486         lua_getfield(L, index, "y");
487         pos.Y = lua_tonumber(L, -1);
488         lua_pop(L, 1);
489         lua_getfield(L, index, "z");
490         pos.Z = lua_tonumber(L, -1);
491         lua_pop(L, 1);
492         return pos;
493 }
494
495 static v3f check_v3f(lua_State *L, int index)
496 {
497         v3f pos;
498         luaL_checktype(L, index, LUA_TTABLE);
499         lua_getfield(L, index, "x");
500         pos.X = luaL_checknumber(L, -1);
501         lua_pop(L, 1);
502         lua_getfield(L, index, "y");
503         pos.Y = luaL_checknumber(L, -1);
504         lua_pop(L, 1);
505         lua_getfield(L, index, "z");
506         pos.Z = luaL_checknumber(L, -1);
507         lua_pop(L, 1);
508         return pos;
509 }
510
511 static void pushFloatPos(lua_State *L, v3f p)
512 {
513         p /= BS;
514         push_v3f(L, p);
515 }
516
517 static v3f checkFloatPos(lua_State *L, int index)
518 {
519         return check_v3f(L, index) * BS;
520 }
521
522 static void push_v3s16(lua_State *L, v3s16 p)
523 {
524         lua_newtable(L);
525         lua_pushnumber(L, p.X);
526         lua_setfield(L, -2, "x");
527         lua_pushnumber(L, p.Y);
528         lua_setfield(L, -2, "y");
529         lua_pushnumber(L, p.Z);
530         lua_setfield(L, -2, "z");
531 }
532
533 static v3s16 read_v3s16(lua_State *L, int index)
534 {
535         // Correct rounding at <0
536         v3f pf = read_v3f(L, index);
537         return floatToInt(pf, 1.0);
538 }
539
540 static v3s16 check_v3s16(lua_State *L, int index)
541 {
542         // Correct rounding at <0
543         v3f pf = check_v3f(L, index);
544         return floatToInt(pf, 1.0);
545 }
546
547 static void pushnode(lua_State *L, const MapNode &n, INodeDefManager *ndef)
548 {
549         lua_newtable(L);
550         lua_pushstring(L, ndef->get(n).name.c_str());
551         lua_setfield(L, -2, "name");
552         lua_pushnumber(L, n.getParam1());
553         lua_setfield(L, -2, "param1");
554         lua_pushnumber(L, n.getParam2());
555         lua_setfield(L, -2, "param2");
556 }
557
558 static MapNode readnode(lua_State *L, int index, INodeDefManager *ndef)
559 {
560         lua_getfield(L, index, "name");
561         const char *name = luaL_checkstring(L, -1);
562         lua_pop(L, 1);
563         u8 param1;
564         lua_getfield(L, index, "param1");
565         if(lua_isnil(L, -1))
566                 param1 = 0;
567         else
568                 param1 = lua_tonumber(L, -1);
569         lua_pop(L, 1);
570         u8 param2;
571         lua_getfield(L, index, "param2");
572         if(lua_isnil(L, -1))
573                 param2 = 0;
574         else
575                 param2 = lua_tonumber(L, -1);
576         lua_pop(L, 1);
577         return MapNode(ndef, name, param1, param2);
578 }
579
580 static video::SColor readARGB8(lua_State *L, int index)
581 {
582         video::SColor color;
583         luaL_checktype(L, index, LUA_TTABLE);
584         lua_getfield(L, index, "a");
585         if(lua_isnumber(L, -1))
586                 color.setAlpha(lua_tonumber(L, -1));
587         lua_pop(L, 1);
588         lua_getfield(L, index, "r");
589         color.setRed(lua_tonumber(L, -1));
590         lua_pop(L, 1);
591         lua_getfield(L, index, "g");
592         color.setGreen(lua_tonumber(L, -1));
593         lua_pop(L, 1);
594         lua_getfield(L, index, "b");
595         color.setBlue(lua_tonumber(L, -1));
596         lua_pop(L, 1);
597         return color;
598 }
599
600 static aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
601 {
602         aabb3f box;
603         if(lua_istable(L, index)){
604                 lua_rawgeti(L, index, 1);
605                 box.MinEdge.X = lua_tonumber(L, -1) * scale;
606                 lua_pop(L, 1);
607                 lua_rawgeti(L, index, 2);
608                 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
609                 lua_pop(L, 1);
610                 lua_rawgeti(L, index, 3);
611                 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
612                 lua_pop(L, 1);
613                 lua_rawgeti(L, index, 4);
614                 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
615                 lua_pop(L, 1);
616                 lua_rawgeti(L, index, 5);
617                 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
618                 lua_pop(L, 1);
619                 lua_rawgeti(L, index, 6);
620                 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
621                 lua_pop(L, 1);
622         }
623         return box;
624 }
625
626 static std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
627 {
628         std::vector<aabb3f> boxes;
629         if(lua_istable(L, index)){
630                 int n = lua_objlen(L, index);
631                 // Check if it's a single box or a list of boxes
632                 bool possibly_single_box = (n == 6);
633                 for(int i = 1; i <= n && possibly_single_box; i++){
634                         lua_rawgeti(L, index, i);
635                         if(!lua_isnumber(L, -1))
636                                 possibly_single_box = false;
637                         lua_pop(L, 1);
638                 }
639                 if(possibly_single_box){
640                         // Read a single box
641                         boxes.push_back(read_aabb3f(L, index, scale));
642                 } else {
643                         // Read a list of boxes
644                         for(int i = 1; i <= n; i++){
645                                 lua_rawgeti(L, index, i);
646                                 boxes.push_back(read_aabb3f(L, -1, scale));
647                                 lua_pop(L, 1);
648                         }
649                 }
650         }
651         return boxes;
652 }
653
654 static NodeBox read_nodebox(lua_State *L, int index)
655 {
656         NodeBox nodebox;
657         if(lua_istable(L, -1)){
658                 nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
659                                 es_NodeBoxType, NODEBOX_REGULAR);
660
661                 lua_getfield(L, index, "fixed");
662                 if(lua_istable(L, -1))
663                         nodebox.fixed = read_aabb3f_vector(L, -1, BS);
664                 lua_pop(L, 1);
665
666                 lua_getfield(L, index, "wall_top");
667                 if(lua_istable(L, -1))
668                         nodebox.wall_top = read_aabb3f(L, -1, BS);
669                 lua_pop(L, 1);
670
671                 lua_getfield(L, index, "wall_bottom");
672                 if(lua_istable(L, -1))
673                         nodebox.wall_bottom = read_aabb3f(L, -1, BS);
674                 lua_pop(L, 1);
675
676                 lua_getfield(L, index, "wall_side");
677                 if(lua_istable(L, -1))
678                         nodebox.wall_side = read_aabb3f(L, -1, BS);
679                 lua_pop(L, 1);
680         }
681         return nodebox;
682 }
683
684 /*
685         Groups
686 */
687 static void read_groups(lua_State *L, int index,
688                 std::map<std::string, int> &result)
689 {
690         if (!lua_istable(L,index))
691                 return;
692         result.clear();
693         lua_pushnil(L);
694         if(index < 0)
695                 index -= 1;
696         while(lua_next(L, index) != 0){
697                 // key at index -2 and value at index -1
698                 std::string name = luaL_checkstring(L, -2);
699                 int rating = luaL_checkinteger(L, -1);
700                 result[name] = rating;
701                 // removes value, keeps key for next iteration
702                 lua_pop(L, 1);
703         }
704 }
705
706 /*
707         Privileges
708 */
709 static void read_privileges(lua_State *L, int index,
710                 std::set<std::string> &result)
711 {
712         result.clear();
713         lua_pushnil(L);
714         if(index < 0)
715                 index -= 1;
716         while(lua_next(L, index) != 0){
717                 // key at index -2 and value at index -1
718                 std::string key = luaL_checkstring(L, -2);
719                 bool value = lua_toboolean(L, -1);
720                 if(value)
721                         result.insert(key);
722                 // removes value, keeps key for next iteration
723                 lua_pop(L, 1);
724         }
725 }
726
727 /*
728         ToolCapabilities
729 */
730
731 static ToolCapabilities read_tool_capabilities(
732                 lua_State *L, int table)
733 {
734         ToolCapabilities toolcap;
735         getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
736         getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
737         lua_getfield(L, table, "groupcaps");
738         if(lua_istable(L, -1)){
739                 int table_groupcaps = lua_gettop(L);
740                 lua_pushnil(L);
741                 while(lua_next(L, table_groupcaps) != 0){
742                         // key at index -2 and value at index -1
743                         std::string groupname = luaL_checkstring(L, -2);
744                         if(lua_istable(L, -1)){
745                                 int table_groupcap = lua_gettop(L);
746                                 // This will be created
747                                 ToolGroupCap groupcap;
748                                 // Read simple parameters
749                                 getintfield(L, table_groupcap, "maxlevel", groupcap.maxlevel);
750                                 getintfield(L, table_groupcap, "uses", groupcap.uses);
751                                 // DEPRECATED: maxwear
752                                 float maxwear = 0;
753                                 if(getfloatfield(L, table_groupcap, "maxwear", maxwear)){
754                                         if(maxwear != 0)
755                                                 groupcap.uses = 1.0/maxwear;
756                                         else
757                                                 groupcap.uses = 0;
758                                         infostream<<script_get_backtrace(L)<<std::endl;
759                                         infostream<<"WARNING: field \"maxwear\" is deprecated; "
760                                                         <<"should replace with uses=1/maxwear"<<std::endl;
761                                 }
762                                 // Read "times" table
763                                 lua_getfield(L, table_groupcap, "times");
764                                 if(lua_istable(L, -1)){
765                                         int table_times = lua_gettop(L);
766                                         lua_pushnil(L);
767                                         while(lua_next(L, table_times) != 0){
768                                                 // key at index -2 and value at index -1
769                                                 int rating = luaL_checkinteger(L, -2);
770                                                 float time = luaL_checknumber(L, -1);
771                                                 groupcap.times[rating] = time;
772                                                 // removes value, keeps key for next iteration
773                                                 lua_pop(L, 1);
774                                         }
775                                 }
776                                 lua_pop(L, 1);
777                                 // Insert groupcap into toolcap
778                                 toolcap.groupcaps[groupname] = groupcap;
779                         }
780                         // removes value, keeps key for next iteration
781                         lua_pop(L, 1);
782                 }
783         }
784         lua_pop(L, 1);
785         return toolcap;
786 }
787
788 static void set_tool_capabilities(lua_State *L, int table,
789                 const ToolCapabilities &toolcap)
790 {
791         setfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
792         setintfield(L, table, "max_drop_level", toolcap.max_drop_level);
793         // Create groupcaps table
794         lua_newtable(L);
795         // For each groupcap
796         for(std::map<std::string, ToolGroupCap>::const_iterator
797                         i = toolcap.groupcaps.begin(); i != toolcap.groupcaps.end(); i++){
798                 // Create groupcap table
799                 lua_newtable(L);
800                 const std::string &name = i->first;
801                 const ToolGroupCap &groupcap = i->second;
802                 // Create subtable "times"
803                 lua_newtable(L);
804                 for(std::map<int, float>::const_iterator
805                                 i = groupcap.times.begin(); i != groupcap.times.end(); i++){
806                         int rating = i->first;
807                         float time = i->second;
808                         lua_pushinteger(L, rating);
809                         lua_pushnumber(L, time);
810                         lua_settable(L, -3);
811                 }
812                 // Set subtable "times"
813                 lua_setfield(L, -2, "times");
814                 // Set simple parameters
815                 setintfield(L, -1, "maxlevel", groupcap.maxlevel);
816                 setintfield(L, -1, "uses", groupcap.uses);
817                 // Insert groupcap table into groupcaps table
818                 lua_setfield(L, -2, name.c_str());
819         }
820         // Set groupcaps table
821         lua_setfield(L, -2, "groupcaps");
822 }
823
824 static void push_tool_capabilities(lua_State *L,
825                 const ToolCapabilities &prop)
826 {
827         lua_newtable(L);
828         set_tool_capabilities(L, -1, prop);
829 }
830
831 /*
832         DigParams
833 */
834
835 static void set_dig_params(lua_State *L, int table,
836                 const DigParams &params)
837 {
838         setboolfield(L, table, "diggable", params.diggable);
839         setfloatfield(L, table, "time", params.time);
840         setintfield(L, table, "wear", params.wear);
841 }
842
843 static void push_dig_params(lua_State *L,
844                 const DigParams &params)
845 {
846         lua_newtable(L);
847         set_dig_params(L, -1, params);
848 }
849
850 /*
851         HitParams
852 */
853
854 static void set_hit_params(lua_State *L, int table,
855                 const HitParams &params)
856 {
857         setintfield(L, table, "hp", params.hp);
858         setintfield(L, table, "wear", params.wear);
859 }
860
861 static void push_hit_params(lua_State *L,
862                 const HitParams &params)
863 {
864         lua_newtable(L);
865         set_hit_params(L, -1, params);
866 }
867
868 /*
869         PointedThing
870 */
871
872 static void push_pointed_thing(lua_State *L, const PointedThing& pointed)
873 {
874         lua_newtable(L);
875         if(pointed.type == POINTEDTHING_NODE)
876         {
877                 lua_pushstring(L, "node");
878                 lua_setfield(L, -2, "type");
879                 push_v3s16(L, pointed.node_undersurface);
880                 lua_setfield(L, -2, "under");
881                 push_v3s16(L, pointed.node_abovesurface);
882                 lua_setfield(L, -2, "above");
883         }
884         else if(pointed.type == POINTEDTHING_OBJECT)
885         {
886                 lua_pushstring(L, "object");
887                 lua_setfield(L, -2, "type");
888                 objectref_get(L, pointed.object_id);
889                 lua_setfield(L, -2, "ref");
890         }
891         else
892         {
893                 lua_pushstring(L, "nothing");
894                 lua_setfield(L, -2, "type");
895         }
896 }
897
898 /*
899         SimpleSoundSpec
900 */
901
902 static void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
903 {
904         if(index < 0)
905                 index = lua_gettop(L) + 1 + index;
906         if(lua_isnil(L, index)){
907         } else if(lua_istable(L, index)){
908                 getstringfield(L, index, "name", spec.name);
909                 getfloatfield(L, index, "gain", spec.gain);
910         } else if(lua_isstring(L, index)){
911                 spec.name = lua_tostring(L, index);
912         }
913 }
914
915 /*
916         ObjectProperties
917 */
918
919 static void read_object_properties(lua_State *L, int index,
920                 ObjectProperties *prop)
921 {
922         if(index < 0)
923                 index = lua_gettop(L) + 1 + index;
924         if(!lua_istable(L, index))
925                 return;
926
927         prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
928
929         getboolfield(L, -1, "physical", prop->physical);
930
931         getfloatfield(L, -1, "weight", prop->weight);
932
933         lua_getfield(L, -1, "collisionbox");
934         if(lua_istable(L, -1))
935                 prop->collisionbox = read_aabb3f(L, -1, 1.0);
936         lua_pop(L, 1);
937
938         getstringfield(L, -1, "visual", prop->visual);
939
940         getstringfield(L, -1, "mesh", prop->mesh);
941         
942         lua_getfield(L, -1, "visual_size");
943         if(lua_istable(L, -1))
944                 prop->visual_size = read_v2f(L, -1);
945         lua_pop(L, 1);
946
947         lua_getfield(L, -1, "animation_bone_position");
948         if(lua_istable(L, -1))
949         {
950                 lua_rawgeti (L, -1, 1);
951                 lua_rawgeti (L, -2, 2);
952                 std::string bone_name = lua_tostring(L, -2);
953                 v3f bone_pos = read_v3f(L, -1);
954                 prop->animation_bone_position[bone_name] = bone_pos;
955                 lua_pop(L, 2);
956         }
957         lua_pop(L, 1);
958
959         lua_getfield(L, -1, "animation_bone_rotation");
960         if(lua_istable(L, -1))
961         {
962                 lua_rawgeti (L, -1, 1);
963                 lua_rawgeti (L, -2, 2);
964                 std::string bone_name = lua_tostring(L, -2);
965                 v3f bone_rot = read_v3f(L, -1);
966                 prop->animation_bone_rotation[bone_name] = bone_rot;
967                 lua_pop(L, 2);
968         }
969         lua_pop(L, 1);
970
971         lua_getfield(L, -1, "textures");
972         if(lua_istable(L, -1)){
973                 prop->textures.clear();
974                 int table = lua_gettop(L);
975                 lua_pushnil(L);
976                 while(lua_next(L, table) != 0){
977                         // key at index -2 and value at index -1
978                         if(lua_isstring(L, -1))
979                                 prop->textures.push_back(lua_tostring(L, -1));
980                         else
981                                 prop->textures.push_back("");
982                         // removes value, keeps key for next iteration
983                         lua_pop(L, 1);
984                 }
985         }
986         lua_pop(L, 1);
987         
988         lua_getfield(L, -1, "spritediv");
989         if(lua_istable(L, -1))
990                 prop->spritediv = read_v2s16(L, -1);
991         lua_pop(L, 1);
992
993         lua_getfield(L, -1, "initial_sprite_basepos");
994         if(lua_istable(L, -1))
995                 prop->initial_sprite_basepos = read_v2s16(L, -1);
996         lua_pop(L, 1);
997         
998         getboolfield(L, -1, "is_visible", prop->is_visible);
999         getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
1000         getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
1001 }
1002
1003 /*
1004         ItemDefinition
1005 */
1006
1007 static ItemDefinition read_item_definition(lua_State *L, int index,
1008                 ItemDefinition default_def = ItemDefinition())
1009 {
1010         if(index < 0)
1011                 index = lua_gettop(L) + 1 + index;
1012
1013         // Read the item definition
1014         ItemDefinition def = default_def;
1015
1016         def.type = (ItemType)getenumfield(L, index, "type",
1017                         es_ItemType, ITEM_NONE);
1018         getstringfield(L, index, "name", def.name);
1019         getstringfield(L, index, "description", def.description);
1020         getstringfield(L, index, "inventory_image", def.inventory_image);
1021         getstringfield(L, index, "wield_image", def.wield_image);
1022
1023         lua_getfield(L, index, "wield_scale");
1024         if(lua_istable(L, -1)){
1025                 def.wield_scale = check_v3f(L, -1);
1026         }
1027         lua_pop(L, 1);
1028
1029         def.stack_max = getintfield_default(L, index, "stack_max", def.stack_max);
1030         if(def.stack_max == 0)
1031                 def.stack_max = 1;
1032
1033         lua_getfield(L, index, "on_use");
1034         def.usable = lua_isfunction(L, -1);
1035         lua_pop(L, 1);
1036
1037         getboolfield(L, index, "liquids_pointable", def.liquids_pointable);
1038
1039         warn_if_field_exists(L, index, "tool_digging_properties",
1040                         "deprecated: use tool_capabilities");
1041         
1042         lua_getfield(L, index, "tool_capabilities");
1043         if(lua_istable(L, -1)){
1044                 def.tool_capabilities = new ToolCapabilities(
1045                                 read_tool_capabilities(L, -1));
1046         }
1047
1048         // If name is "" (hand), ensure there are ToolCapabilities
1049         // because it will be looked up there whenever any other item has
1050         // no ToolCapabilities
1051         if(def.name == "" && def.tool_capabilities == NULL){
1052                 def.tool_capabilities = new ToolCapabilities();
1053         }
1054
1055         lua_getfield(L, index, "groups");
1056         read_groups(L, -1, def.groups);
1057         lua_pop(L, 1);
1058
1059         // Client shall immediately place this node when player places the item.
1060         // Server will update the precise end result a moment later.
1061         // "" = no prediction
1062         getstringfield(L, index, "node_placement_prediction",
1063                         def.node_placement_prediction);
1064
1065         return def;
1066 }
1067
1068 /*
1069         TileDef
1070 */
1071
1072 static TileDef read_tiledef(lua_State *L, int index)
1073 {
1074         if(index < 0)
1075                 index = lua_gettop(L) + 1 + index;
1076         
1077         TileDef tiledef;
1078
1079         // key at index -2 and value at index
1080         if(lua_isstring(L, index)){
1081                 // "default_lava.png"
1082                 tiledef.name = lua_tostring(L, index);
1083         }
1084         else if(lua_istable(L, index))
1085         {
1086                 // {name="default_lava.png", animation={}}
1087                 tiledef.name = "";
1088                 getstringfield(L, index, "name", tiledef.name);
1089                 getstringfield(L, index, "image", tiledef.name); // MaterialSpec compat.
1090                 tiledef.backface_culling = getboolfield_default(
1091                                         L, index, "backface_culling", true);
1092                 // animation = {}
1093                 lua_getfield(L, index, "animation");
1094                 if(lua_istable(L, -1)){
1095                         // {type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0}
1096                         tiledef.animation.type = (TileAnimationType)
1097                                         getenumfield(L, -1, "type", es_TileAnimationType,
1098                                         TAT_NONE);
1099                         tiledef.animation.aspect_w =
1100                                         getintfield_default(L, -1, "aspect_w", 16);
1101                         tiledef.animation.aspect_h =
1102                                         getintfield_default(L, -1, "aspect_h", 16);
1103                         tiledef.animation.length =
1104                                         getfloatfield_default(L, -1, "length", 1.0);
1105                 }
1106                 lua_pop(L, 1);
1107         }
1108
1109         return tiledef;
1110 }
1111
1112 /*
1113         ContentFeatures
1114 */
1115
1116 static ContentFeatures read_content_features(lua_State *L, int index)
1117 {
1118         if(index < 0)
1119                 index = lua_gettop(L) + 1 + index;
1120
1121         ContentFeatures f;
1122         
1123         /* Cache existence of some callbacks */
1124         lua_getfield(L, index, "on_construct");
1125         if(!lua_isnil(L, -1)) f.has_on_construct = true;
1126         lua_pop(L, 1);
1127         lua_getfield(L, index, "on_destruct");
1128         if(!lua_isnil(L, -1)) f.has_on_destruct = true;
1129         lua_pop(L, 1);
1130         lua_getfield(L, index, "after_destruct");
1131         if(!lua_isnil(L, -1)) f.has_after_destruct = true;
1132         lua_pop(L, 1);
1133
1134         /* Name */
1135         getstringfield(L, index, "name", f.name);
1136
1137         /* Groups */
1138         lua_getfield(L, index, "groups");
1139         read_groups(L, -1, f.groups);
1140         lua_pop(L, 1);
1141
1142         /* Visual definition */
1143
1144         f.drawtype = (NodeDrawType)getenumfield(L, index, "drawtype", es_DrawType,
1145                         NDT_NORMAL);
1146         getfloatfield(L, index, "visual_scale", f.visual_scale);
1147         
1148         // tiles = {}
1149         lua_getfield(L, index, "tiles");
1150         // If nil, try the deprecated name "tile_images" instead
1151         if(lua_isnil(L, -1)){
1152                 lua_pop(L, 1);
1153                 warn_if_field_exists(L, index, "tile_images",
1154                                 "Deprecated; new name is \"tiles\".");
1155                 lua_getfield(L, index, "tile_images");
1156         }
1157         if(lua_istable(L, -1)){
1158                 int table = lua_gettop(L);
1159                 lua_pushnil(L);
1160                 int i = 0;
1161                 while(lua_next(L, table) != 0){
1162                         // Read tiledef from value
1163                         f.tiledef[i] = read_tiledef(L, -1);
1164                         // removes value, keeps key for next iteration
1165                         lua_pop(L, 1);
1166                         i++;
1167                         if(i==6){
1168                                 lua_pop(L, 1);
1169                                 break;
1170                         }
1171                 }
1172                 // Copy last value to all remaining textures
1173                 if(i >= 1){
1174                         TileDef lasttile = f.tiledef[i-1];
1175                         while(i < 6){
1176                                 f.tiledef[i] = lasttile;
1177                                 i++;
1178                         }
1179                 }
1180         }
1181         lua_pop(L, 1);
1182         
1183         // special_tiles = {}
1184         lua_getfield(L, index, "special_tiles");
1185         // If nil, try the deprecated name "special_materials" instead
1186         if(lua_isnil(L, -1)){
1187                 lua_pop(L, 1);
1188                 warn_if_field_exists(L, index, "special_materials",
1189                                 "Deprecated; new name is \"special_tiles\".");
1190                 lua_getfield(L, index, "special_materials");
1191         }
1192         if(lua_istable(L, -1)){
1193                 int table = lua_gettop(L);
1194                 lua_pushnil(L);
1195                 int i = 0;
1196                 while(lua_next(L, table) != 0){
1197                         // Read tiledef from value
1198                         f.tiledef_special[i] = read_tiledef(L, -1);
1199                         // removes value, keeps key for next iteration
1200                         lua_pop(L, 1);
1201                         i++;
1202                         if(i==6){
1203                                 lua_pop(L, 1);
1204                                 break;
1205                         }
1206                 }
1207         }
1208         lua_pop(L, 1);
1209
1210         f.alpha = getintfield_default(L, index, "alpha", 255);
1211
1212         /* Other stuff */
1213         
1214         lua_getfield(L, index, "post_effect_color");
1215         if(!lua_isnil(L, -1))
1216                 f.post_effect_color = readARGB8(L, -1);
1217         lua_pop(L, 1);
1218
1219         f.param_type = (ContentParamType)getenumfield(L, index, "paramtype",
1220                         es_ContentParamType, CPT_NONE);
1221         f.param_type_2 = (ContentParamType2)getenumfield(L, index, "paramtype2",
1222                         es_ContentParamType2, CPT2_NONE);
1223
1224         // Warn about some deprecated fields
1225         warn_if_field_exists(L, index, "wall_mounted",
1226                         "deprecated: use paramtype2 = 'wallmounted'");
1227         warn_if_field_exists(L, index, "light_propagates",
1228                         "deprecated: determined from paramtype");
1229         warn_if_field_exists(L, index, "dug_item",
1230                         "deprecated: use 'drop' field");
1231         warn_if_field_exists(L, index, "extra_dug_item",
1232                         "deprecated: use 'drop' field");
1233         warn_if_field_exists(L, index, "extra_dug_item_rarity",
1234                         "deprecated: use 'drop' field");
1235         warn_if_field_exists(L, index, "metadata_name",
1236                         "deprecated: use on_add and metadata callbacks");
1237         
1238         // True for all ground-like things like stone and mud, false for eg. trees
1239         getboolfield(L, index, "is_ground_content", f.is_ground_content);
1240         f.light_propagates = (f.param_type == CPT_LIGHT);
1241         getboolfield(L, index, "sunlight_propagates", f.sunlight_propagates);
1242         // This is used for collision detection.
1243         // Also for general solidness queries.
1244         getboolfield(L, index, "walkable", f.walkable);
1245         // Player can point to these
1246         getboolfield(L, index, "pointable", f.pointable);
1247         // Player can dig these
1248         getboolfield(L, index, "diggable", f.diggable);
1249         // Player can climb these
1250         getboolfield(L, index, "climbable", f.climbable);
1251         // Player can build on these
1252         getboolfield(L, index, "buildable_to", f.buildable_to);
1253         // Whether the node is non-liquid, source liquid or flowing liquid
1254         f.liquid_type = (LiquidType)getenumfield(L, index, "liquidtype",
1255                         es_LiquidType, LIQUID_NONE);
1256         // If the content is liquid, this is the flowing version of the liquid.
1257         getstringfield(L, index, "liquid_alternative_flowing",
1258                         f.liquid_alternative_flowing);
1259         // If the content is liquid, this is the source version of the liquid.
1260         getstringfield(L, index, "liquid_alternative_source",
1261                         f.liquid_alternative_source);
1262         // Viscosity for fluid flow, ranging from 1 to 7, with
1263         // 1 giving almost instantaneous propagation and 7 being
1264         // the slowest possible
1265         f.liquid_viscosity = getintfield_default(L, index,
1266                         "liquid_viscosity", f.liquid_viscosity);
1267         getboolfield(L, index, "liquid_renewable", f.liquid_renewable);
1268         // Amount of light the node emits
1269         f.light_source = getintfield_default(L, index,
1270                         "light_source", f.light_source);
1271         f.damage_per_second = getintfield_default(L, index,
1272                         "damage_per_second", f.damage_per_second);
1273         
1274         lua_getfield(L, index, "node_box");
1275         if(lua_istable(L, -1))
1276                 f.node_box = read_nodebox(L, -1);
1277         lua_pop(L, 1);
1278
1279         lua_getfield(L, index, "selection_box");
1280         if(lua_istable(L, -1))
1281                 f.selection_box = read_nodebox(L, -1);
1282         lua_pop(L, 1);
1283
1284         // Set to true if paramtype used to be 'facedir_simple'
1285         getboolfield(L, index, "legacy_facedir_simple", f.legacy_facedir_simple);
1286         // Set to true if wall_mounted used to be set to true
1287         getboolfield(L, index, "legacy_wallmounted", f.legacy_wallmounted);
1288         
1289         // Sound table
1290         lua_getfield(L, index, "sounds");
1291         if(lua_istable(L, -1)){
1292                 lua_getfield(L, -1, "footstep");
1293                 read_soundspec(L, -1, f.sound_footstep);
1294                 lua_pop(L, 1);
1295                 lua_getfield(L, -1, "dig");
1296                 read_soundspec(L, -1, f.sound_dig);
1297                 lua_pop(L, 1);
1298                 lua_getfield(L, -1, "dug");
1299                 read_soundspec(L, -1, f.sound_dug);
1300                 lua_pop(L, 1);
1301         }
1302         lua_pop(L, 1);
1303
1304         return f;
1305 }
1306
1307 /*
1308         Inventory stuff
1309 */
1310
1311 static ItemStack read_item(lua_State *L, int index);
1312 static std::vector<ItemStack> read_items(lua_State *L, int index);
1313 // creates a table of ItemStacks
1314 static void push_items(lua_State *L, const std::vector<ItemStack> &items);
1315
1316 static void inventory_set_list_from_lua(Inventory *inv, const char *name,
1317                 lua_State *L, int tableindex, int forcesize=-1)
1318 {
1319         if(tableindex < 0)
1320                 tableindex = lua_gettop(L) + 1 + tableindex;
1321         // If nil, delete list
1322         if(lua_isnil(L, tableindex)){
1323                 inv->deleteList(name);
1324                 return;
1325         }
1326         // Otherwise set list
1327         std::vector<ItemStack> items = read_items(L, tableindex);
1328         int listsize = (forcesize != -1) ? forcesize : items.size();
1329         InventoryList *invlist = inv->addList(name, listsize);
1330         int index = 0;
1331         for(std::vector<ItemStack>::const_iterator
1332                         i = items.begin(); i != items.end(); i++){
1333                 if(forcesize != -1 && index == forcesize)
1334                         break;
1335                 invlist->changeItem(index, *i);
1336                 index++;
1337         }
1338         while(forcesize != -1 && index < forcesize){
1339                 invlist->deleteItem(index);
1340                 index++;
1341         }
1342 }
1343
1344 static void inventory_get_list_to_lua(Inventory *inv, const char *name,
1345                 lua_State *L)
1346 {
1347         InventoryList *invlist = inv->getList(name);
1348         if(invlist == NULL){
1349                 lua_pushnil(L);
1350                 return;
1351         }
1352         std::vector<ItemStack> items;
1353         for(u32 i=0; i<invlist->getSize(); i++)
1354                 items.push_back(invlist->getItem(i));
1355         push_items(L, items);
1356 }
1357
1358 /*
1359         Helpful macros for userdata classes
1360 */
1361
1362 #define method(class, name) {#name, class::l_##name}
1363
1364 /*
1365         LuaItemStack
1366 */
1367
1368 class LuaItemStack
1369 {
1370 private:
1371         ItemStack m_stack;
1372
1373         static const char className[];
1374         static const luaL_reg methods[];
1375
1376         // Exported functions
1377         
1378         // garbage collector
1379         static int gc_object(lua_State *L)
1380         {
1381                 LuaItemStack *o = *(LuaItemStack **)(lua_touserdata(L, 1));
1382                 delete o;
1383                 return 0;
1384         }
1385
1386         // is_empty(self) -> true/false
1387         static int l_is_empty(lua_State *L)
1388         {
1389                 LuaItemStack *o = checkobject(L, 1);
1390                 ItemStack &item = o->m_stack;
1391                 lua_pushboolean(L, item.empty());
1392                 return 1;
1393         }
1394
1395         // get_name(self) -> string
1396         static int l_get_name(lua_State *L)
1397         {
1398                 LuaItemStack *o = checkobject(L, 1);
1399                 ItemStack &item = o->m_stack;
1400                 lua_pushstring(L, item.name.c_str());
1401                 return 1;
1402         }
1403
1404         // get_count(self) -> number
1405         static int l_get_count(lua_State *L)
1406         {
1407                 LuaItemStack *o = checkobject(L, 1);
1408                 ItemStack &item = o->m_stack;
1409                 lua_pushinteger(L, item.count);
1410                 return 1;
1411         }
1412
1413         // get_wear(self) -> number
1414         static int l_get_wear(lua_State *L)
1415         {
1416                 LuaItemStack *o = checkobject(L, 1);
1417                 ItemStack &item = o->m_stack;
1418                 lua_pushinteger(L, item.wear);
1419                 return 1;
1420         }
1421
1422         // get_metadata(self) -> string
1423         static int l_get_metadata(lua_State *L)
1424         {
1425                 LuaItemStack *o = checkobject(L, 1);
1426                 ItemStack &item = o->m_stack;
1427                 lua_pushlstring(L, item.metadata.c_str(), item.metadata.size());
1428                 return 1;
1429         }
1430
1431         // clear(self) -> true
1432         static int l_clear(lua_State *L)
1433         {
1434                 LuaItemStack *o = checkobject(L, 1);
1435                 o->m_stack.clear();
1436                 lua_pushboolean(L, true);
1437                 return 1;
1438         }
1439
1440         // replace(self, itemstack or itemstring or table or nil) -> true
1441         static int l_replace(lua_State *L)
1442         {
1443                 LuaItemStack *o = checkobject(L, 1);
1444                 o->m_stack = read_item(L, 2);
1445                 lua_pushboolean(L, true);
1446                 return 1;
1447         }
1448
1449         // to_string(self) -> string
1450         static int l_to_string(lua_State *L)
1451         {
1452                 LuaItemStack *o = checkobject(L, 1);
1453                 std::string itemstring = o->m_stack.getItemString();
1454                 lua_pushstring(L, itemstring.c_str());
1455                 return 1;
1456         }
1457
1458         // to_table(self) -> table or nil
1459         static int l_to_table(lua_State *L)
1460         {
1461                 LuaItemStack *o = checkobject(L, 1);
1462                 const ItemStack &item = o->m_stack;
1463                 if(item.empty())
1464                 {
1465                         lua_pushnil(L);
1466                 }
1467                 else
1468                 {
1469                         lua_newtable(L);
1470                         lua_pushstring(L, item.name.c_str());
1471                         lua_setfield(L, -2, "name");
1472                         lua_pushinteger(L, item.count);
1473                         lua_setfield(L, -2, "count");
1474                         lua_pushinteger(L, item.wear);
1475                         lua_setfield(L, -2, "wear");
1476                         lua_pushlstring(L, item.metadata.c_str(), item.metadata.size());
1477                         lua_setfield(L, -2, "metadata");
1478                 }
1479                 return 1;
1480         }
1481
1482         // get_stack_max(self) -> number
1483         static int l_get_stack_max(lua_State *L)
1484         {
1485                 LuaItemStack *o = checkobject(L, 1);
1486                 ItemStack &item = o->m_stack;
1487                 lua_pushinteger(L, item.getStackMax(get_server(L)->idef()));
1488                 return 1;
1489         }
1490
1491         // get_free_space(self) -> number
1492         static int l_get_free_space(lua_State *L)
1493         {
1494                 LuaItemStack *o = checkobject(L, 1);
1495                 ItemStack &item = o->m_stack;
1496                 lua_pushinteger(L, item.freeSpace(get_server(L)->idef()));
1497                 return 1;
1498         }
1499
1500         // is_known(self) -> true/false
1501         // Checks if the item is defined.
1502         static int l_is_known(lua_State *L)
1503         {
1504                 LuaItemStack *o = checkobject(L, 1);
1505                 ItemStack &item = o->m_stack;
1506                 bool is_known = item.isKnown(get_server(L)->idef());
1507                 lua_pushboolean(L, is_known);
1508                 return 1;
1509         }
1510
1511         // get_definition(self) -> table
1512         // Returns the item definition table from minetest.registered_items,
1513         // or a fallback one (name="unknown")
1514         static int l_get_definition(lua_State *L)
1515         {
1516                 LuaItemStack *o = checkobject(L, 1);
1517                 ItemStack &item = o->m_stack;
1518
1519                 // Get minetest.registered_items[name]
1520                 lua_getglobal(L, "minetest");
1521                 lua_getfield(L, -1, "registered_items");
1522                 luaL_checktype(L, -1, LUA_TTABLE);
1523                 lua_getfield(L, -1, item.name.c_str());
1524                 if(lua_isnil(L, -1))
1525                 {
1526                         lua_pop(L, 1);
1527                         lua_getfield(L, -1, "unknown");
1528                 }
1529                 return 1;
1530         }
1531
1532         // get_tool_capabilities(self) -> table
1533         // Returns the effective tool digging properties.
1534         // Returns those of the hand ("") if this item has none associated.
1535         static int l_get_tool_capabilities(lua_State *L)
1536         {
1537                 LuaItemStack *o = checkobject(L, 1);
1538                 ItemStack &item = o->m_stack;
1539                 const ToolCapabilities &prop =
1540                         item.getToolCapabilities(get_server(L)->idef());
1541                 push_tool_capabilities(L, prop);
1542                 return 1;
1543         }
1544
1545         // add_wear(self, amount) -> true/false
1546         // The range for "amount" is [0,65535]. Wear is only added if the item
1547         // is a tool. Adding wear might destroy the item.
1548         // Returns true if the item is (or was) a tool.
1549         static int l_add_wear(lua_State *L)
1550         {
1551                 LuaItemStack *o = checkobject(L, 1);
1552                 ItemStack &item = o->m_stack;
1553                 int amount = lua_tointeger(L, 2);
1554                 bool result = item.addWear(amount, get_server(L)->idef());
1555                 lua_pushboolean(L, result);
1556                 return 1;
1557         }
1558
1559         // add_item(self, itemstack or itemstring or table or nil) -> itemstack
1560         // Returns leftover item stack
1561         static int l_add_item(lua_State *L)
1562         {
1563                 LuaItemStack *o = checkobject(L, 1);
1564                 ItemStack &item = o->m_stack;
1565                 ItemStack newitem = read_item(L, 2);
1566                 ItemStack leftover = item.addItem(newitem, get_server(L)->idef());
1567                 create(L, leftover);
1568                 return 1;
1569         }
1570
1571         // item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
1572         // First return value is true iff the new item fits fully into the stack
1573         // Second return value is the would-be-left-over item stack
1574         static int l_item_fits(lua_State *L)
1575         {
1576                 LuaItemStack *o = checkobject(L, 1);
1577                 ItemStack &item = o->m_stack;
1578                 ItemStack newitem = read_item(L, 2);
1579                 ItemStack restitem;
1580                 bool fits = item.itemFits(newitem, &restitem, get_server(L)->idef());
1581                 lua_pushboolean(L, fits);  // first return value
1582                 create(L, restitem);       // second return value
1583                 return 2;
1584         }
1585
1586         // take_item(self, takecount=1) -> itemstack
1587         static int l_take_item(lua_State *L)
1588         {
1589                 LuaItemStack *o = checkobject(L, 1);
1590                 ItemStack &item = o->m_stack;
1591                 u32 takecount = 1;
1592                 if(!lua_isnone(L, 2))
1593                         takecount = luaL_checkinteger(L, 2);
1594                 ItemStack taken = item.takeItem(takecount);
1595                 create(L, taken);
1596                 return 1;
1597         }
1598
1599         // peek_item(self, peekcount=1) -> itemstack
1600         static int l_peek_item(lua_State *L)
1601         {
1602                 LuaItemStack *o = checkobject(L, 1);
1603                 ItemStack &item = o->m_stack;
1604                 u32 peekcount = 1;
1605                 if(!lua_isnone(L, 2))
1606                         peekcount = lua_tointeger(L, 2);
1607                 ItemStack peekaboo = item.peekItem(peekcount);
1608                 create(L, peekaboo);
1609                 return 1;
1610         }
1611
1612 public:
1613         LuaItemStack(const ItemStack &item):
1614                 m_stack(item)
1615         {
1616         }
1617
1618         ~LuaItemStack()
1619         {
1620         }
1621
1622         const ItemStack& getItem() const
1623         {
1624                 return m_stack;
1625         }
1626         ItemStack& getItem()
1627         {
1628                 return m_stack;
1629         }
1630         
1631         // LuaItemStack(itemstack or itemstring or table or nil)
1632         // Creates an LuaItemStack and leaves it on top of stack
1633         static int create_object(lua_State *L)
1634         {
1635                 ItemStack item = read_item(L, 1);
1636                 LuaItemStack *o = new LuaItemStack(item);
1637                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1638                 luaL_getmetatable(L, className);
1639                 lua_setmetatable(L, -2);
1640                 return 1;
1641         }
1642         // Not callable from Lua
1643         static int create(lua_State *L, const ItemStack &item)
1644         {
1645                 LuaItemStack *o = new LuaItemStack(item);
1646                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1647                 luaL_getmetatable(L, className);
1648                 lua_setmetatable(L, -2);
1649                 return 1;
1650         }
1651
1652         static LuaItemStack* checkobject(lua_State *L, int narg)
1653         {
1654                 luaL_checktype(L, narg, LUA_TUSERDATA);
1655                 void *ud = luaL_checkudata(L, narg, className);
1656                 if(!ud) luaL_typerror(L, narg, className);
1657                 return *(LuaItemStack**)ud;  // unbox pointer
1658         }
1659
1660         static void Register(lua_State *L)
1661         {
1662                 lua_newtable(L);
1663                 int methodtable = lua_gettop(L);
1664                 luaL_newmetatable(L, className);
1665                 int metatable = lua_gettop(L);
1666
1667                 lua_pushliteral(L, "__metatable");
1668                 lua_pushvalue(L, methodtable);
1669                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
1670
1671                 lua_pushliteral(L, "__index");
1672                 lua_pushvalue(L, methodtable);
1673                 lua_settable(L, metatable);
1674
1675                 lua_pushliteral(L, "__gc");
1676                 lua_pushcfunction(L, gc_object);
1677                 lua_settable(L, metatable);
1678
1679                 lua_pop(L, 1);  // drop metatable
1680
1681                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
1682                 lua_pop(L, 1);  // drop methodtable
1683
1684                 // Can be created from Lua (LuaItemStack(itemstack or itemstring or table or nil))
1685                 lua_register(L, className, create_object);
1686         }
1687 };
1688 const char LuaItemStack::className[] = "ItemStack";
1689 const luaL_reg LuaItemStack::methods[] = {
1690         method(LuaItemStack, is_empty),
1691         method(LuaItemStack, get_name),
1692         method(LuaItemStack, get_count),
1693         method(LuaItemStack, get_wear),
1694         method(LuaItemStack, get_metadata),
1695         method(LuaItemStack, clear),
1696         method(LuaItemStack, replace),
1697         method(LuaItemStack, to_string),
1698         method(LuaItemStack, to_table),
1699         method(LuaItemStack, get_stack_max),
1700         method(LuaItemStack, get_free_space),
1701         method(LuaItemStack, is_known),
1702         method(LuaItemStack, get_definition),
1703         method(LuaItemStack, get_tool_capabilities),
1704         method(LuaItemStack, add_wear),
1705         method(LuaItemStack, add_item),
1706         method(LuaItemStack, item_fits),
1707         method(LuaItemStack, take_item),
1708         method(LuaItemStack, peek_item),
1709         {0,0}
1710 };
1711
1712 static ItemStack read_item(lua_State *L, int index)
1713 {
1714         if(index < 0)
1715                 index = lua_gettop(L) + 1 + index;
1716
1717         if(lua_isnil(L, index))
1718         {
1719                 return ItemStack();
1720         }
1721         else if(lua_isuserdata(L, index))
1722         {
1723                 // Convert from LuaItemStack
1724                 LuaItemStack *o = LuaItemStack::checkobject(L, index);
1725                 return o->getItem();
1726         }
1727         else if(lua_isstring(L, index))
1728         {
1729                 // Convert from itemstring
1730                 std::string itemstring = lua_tostring(L, index);
1731                 IItemDefManager *idef = get_server(L)->idef();
1732                 try
1733                 {
1734                         ItemStack item;
1735                         item.deSerialize(itemstring, idef);
1736                         return item;
1737                 }
1738                 catch(SerializationError &e)
1739                 {
1740                         infostream<<"WARNING: unable to create item from itemstring"
1741                                         <<": "<<itemstring<<std::endl;
1742                         return ItemStack();
1743                 }
1744         }
1745         else if(lua_istable(L, index))
1746         {
1747                 // Convert from table
1748                 IItemDefManager *idef = get_server(L)->idef();
1749                 std::string name = getstringfield_default(L, index, "name", "");
1750                 int count = getintfield_default(L, index, "count", 1);
1751                 int wear = getintfield_default(L, index, "wear", 0);
1752                 std::string metadata = getstringfield_default(L, index, "metadata", "");
1753                 return ItemStack(name, count, wear, metadata, idef);
1754         }
1755         else
1756         {
1757                 throw LuaError(L, "Expecting itemstack, itemstring, table or nil");
1758         }
1759 }
1760
1761 static std::vector<ItemStack> read_items(lua_State *L, int index)
1762 {
1763         if(index < 0)
1764                 index = lua_gettop(L) + 1 + index;
1765
1766         std::vector<ItemStack> items;
1767         luaL_checktype(L, index, LUA_TTABLE);
1768         lua_pushnil(L);
1769         while(lua_next(L, index) != 0){
1770                 // key at index -2 and value at index -1
1771                 items.push_back(read_item(L, -1));
1772                 // removes value, keeps key for next iteration
1773                 lua_pop(L, 1);
1774         }
1775         return items;
1776 }
1777
1778 // creates a table of ItemStacks
1779 static void push_items(lua_State *L, const std::vector<ItemStack> &items)
1780 {
1781         // Get the table insert function
1782         lua_getglobal(L, "table");
1783         lua_getfield(L, -1, "insert");
1784         int table_insert = lua_gettop(L);
1785         // Create and fill table
1786         lua_newtable(L);
1787         int table = lua_gettop(L);
1788         for(u32 i=0; i<items.size(); i++){
1789                 ItemStack item = items[i];
1790                 lua_pushvalue(L, table_insert);
1791                 lua_pushvalue(L, table);
1792                 LuaItemStack::create(L, item);
1793                 if(lua_pcall(L, 2, 0, 0))
1794                         script_error(L, "error: %s", lua_tostring(L, -1));
1795         }
1796         lua_remove(L, -2); // Remove table
1797         lua_remove(L, -2); // Remove insert
1798 }
1799
1800 /*
1801         InvRef
1802 */
1803
1804 class InvRef
1805 {
1806 private:
1807         InventoryLocation m_loc;
1808
1809         static const char className[];
1810         static const luaL_reg methods[];
1811
1812         static InvRef *checkobject(lua_State *L, int narg)
1813         {
1814                 luaL_checktype(L, narg, LUA_TUSERDATA);
1815                 void *ud = luaL_checkudata(L, narg, className);
1816                 if(!ud) luaL_typerror(L, narg, className);
1817                 return *(InvRef**)ud;  // unbox pointer
1818         }
1819         
1820         static Inventory* getinv(lua_State *L, InvRef *ref)
1821         {
1822                 return get_server(L)->getInventory(ref->m_loc);
1823         }
1824
1825         static InventoryList* getlist(lua_State *L, InvRef *ref,
1826                         const char *listname)
1827         {
1828                 Inventory *inv = getinv(L, ref);
1829                 if(!inv)
1830                         return NULL;
1831                 return inv->getList(listname);
1832         }
1833
1834         static void reportInventoryChange(lua_State *L, InvRef *ref)
1835         {
1836                 // Inform other things that the inventory has changed
1837                 get_server(L)->setInventoryModified(ref->m_loc);
1838         }
1839         
1840         // Exported functions
1841         
1842         // garbage collector
1843         static int gc_object(lua_State *L) {
1844                 InvRef *o = *(InvRef **)(lua_touserdata(L, 1));
1845                 delete o;
1846                 return 0;
1847         }
1848
1849         // is_empty(self, listname) -> true/false
1850         static int l_is_empty(lua_State *L)
1851         {
1852                 InvRef *ref = checkobject(L, 1);
1853                 const char *listname = luaL_checkstring(L, 2);
1854                 InventoryList *list = getlist(L, ref, listname);
1855                 if(list && list->getUsedSlots() > 0){
1856                         lua_pushboolean(L, false);
1857                 } else {
1858                         lua_pushboolean(L, true);
1859                 }
1860                 return 1;
1861         }
1862
1863         // get_size(self, listname)
1864         static int l_get_size(lua_State *L)
1865         {
1866                 InvRef *ref = checkobject(L, 1);
1867                 const char *listname = luaL_checkstring(L, 2);
1868                 InventoryList *list = getlist(L, ref, listname);
1869                 if(list){
1870                         lua_pushinteger(L, list->getSize());
1871                 } else {
1872                         lua_pushinteger(L, 0);
1873                 }
1874                 return 1;
1875         }
1876
1877         // get_width(self, listname)
1878         static int l_get_width(lua_State *L)
1879         {
1880                 InvRef *ref = checkobject(L, 1);
1881                 const char *listname = luaL_checkstring(L, 2);
1882                 InventoryList *list = getlist(L, ref, listname);
1883                 if(list){
1884                         lua_pushinteger(L, list->getWidth());
1885                 } else {
1886                         lua_pushinteger(L, 0);
1887                 }
1888                 return 1;
1889         }
1890
1891         // set_size(self, listname, size)
1892         static int l_set_size(lua_State *L)
1893         {
1894                 InvRef *ref = checkobject(L, 1);
1895                 const char *listname = luaL_checkstring(L, 2);
1896                 int newsize = luaL_checknumber(L, 3);
1897                 Inventory *inv = getinv(L, ref);
1898                 if(newsize == 0){
1899                         inv->deleteList(listname);
1900                         reportInventoryChange(L, ref);
1901                         return 0;
1902                 }
1903                 InventoryList *list = inv->getList(listname);
1904                 if(list){
1905                         list->setSize(newsize);
1906                 } else {
1907                         list = inv->addList(listname, newsize);
1908                 }
1909                 reportInventoryChange(L, ref);
1910                 return 0;
1911         }
1912
1913         // set_width(self, listname, size)
1914         static int l_set_width(lua_State *L)
1915         {
1916                 InvRef *ref = checkobject(L, 1);
1917                 const char *listname = luaL_checkstring(L, 2);
1918                 int newwidth = luaL_checknumber(L, 3);
1919                 Inventory *inv = getinv(L, ref);
1920                 InventoryList *list = inv->getList(listname);
1921                 if(list){
1922                         list->setWidth(newwidth);
1923                 } else {
1924                         return 0;
1925                 }
1926                 reportInventoryChange(L, ref);
1927                 return 0;
1928         }
1929
1930         // get_stack(self, listname, i) -> itemstack
1931         static int l_get_stack(lua_State *L)
1932         {
1933                 InvRef *ref = checkobject(L, 1);
1934                 const char *listname = luaL_checkstring(L, 2);
1935                 int i = luaL_checknumber(L, 3) - 1;
1936                 InventoryList *list = getlist(L, ref, listname);
1937                 ItemStack item;
1938                 if(list != NULL && i >= 0 && i < (int) list->getSize())
1939                         item = list->getItem(i);
1940                 LuaItemStack::create(L, item);
1941                 return 1;
1942         }
1943
1944         // set_stack(self, listname, i, stack) -> true/false
1945         static int l_set_stack(lua_State *L)
1946         {
1947                 InvRef *ref = checkobject(L, 1);
1948                 const char *listname = luaL_checkstring(L, 2);
1949                 int i = luaL_checknumber(L, 3) - 1;
1950                 ItemStack newitem = read_item(L, 4);
1951                 InventoryList *list = getlist(L, ref, listname);
1952                 if(list != NULL && i >= 0 && i < (int) list->getSize()){
1953                         list->changeItem(i, newitem);
1954                         reportInventoryChange(L, ref);
1955                         lua_pushboolean(L, true);
1956                 } else {
1957                         lua_pushboolean(L, false);
1958                 }
1959                 return 1;
1960         }
1961
1962         // get_list(self, listname) -> list or nil
1963         static int l_get_list(lua_State *L)
1964         {
1965                 InvRef *ref = checkobject(L, 1);
1966                 const char *listname = luaL_checkstring(L, 2);
1967                 Inventory *inv = getinv(L, ref);
1968                 inventory_get_list_to_lua(inv, listname, L);
1969                 return 1;
1970         }
1971
1972         // set_list(self, listname, list)
1973         static int l_set_list(lua_State *L)
1974         {
1975                 InvRef *ref = checkobject(L, 1);
1976                 const char *listname = luaL_checkstring(L, 2);
1977                 Inventory *inv = getinv(L, ref);
1978                 InventoryList *list = inv->getList(listname);
1979                 if(list)
1980                         inventory_set_list_from_lua(inv, listname, L, 3,
1981                                         list->getSize());
1982                 else
1983                         inventory_set_list_from_lua(inv, listname, L, 3);
1984                 reportInventoryChange(L, ref);
1985                 return 0;
1986         }
1987
1988         // add_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
1989         // Returns the leftover stack
1990         static int l_add_item(lua_State *L)
1991         {
1992                 InvRef *ref = checkobject(L, 1);
1993                 const char *listname = luaL_checkstring(L, 2);
1994                 ItemStack item = read_item(L, 3);
1995                 InventoryList *list = getlist(L, ref, listname);
1996                 if(list){
1997                         ItemStack leftover = list->addItem(item);
1998                         if(leftover.count != item.count)
1999                                 reportInventoryChange(L, ref);
2000                         LuaItemStack::create(L, leftover);
2001                 } else {
2002                         LuaItemStack::create(L, item);
2003                 }
2004                 return 1;
2005         }
2006
2007         // room_for_item(self, listname, itemstack or itemstring or table or nil) -> true/false
2008         // Returns true if the item completely fits into the list
2009         static int l_room_for_item(lua_State *L)
2010         {
2011                 InvRef *ref = checkobject(L, 1);
2012                 const char *listname = luaL_checkstring(L, 2);
2013                 ItemStack item = read_item(L, 3);
2014                 InventoryList *list = getlist(L, ref, listname);
2015                 if(list){
2016                         lua_pushboolean(L, list->roomForItem(item));
2017                 } else {
2018                         lua_pushboolean(L, false);
2019                 }
2020                 return 1;
2021         }
2022
2023         // contains_item(self, listname, itemstack or itemstring or table or nil) -> true/false
2024         // Returns true if the list contains the given count of the given item name
2025         static int l_contains_item(lua_State *L)
2026         {
2027                 InvRef *ref = checkobject(L, 1);
2028                 const char *listname = luaL_checkstring(L, 2);
2029                 ItemStack item = read_item(L, 3);
2030                 InventoryList *list = getlist(L, ref, listname);
2031                 if(list){
2032                         lua_pushboolean(L, list->containsItem(item));
2033                 } else {
2034                         lua_pushboolean(L, false);
2035                 }
2036                 return 1;
2037         }
2038
2039         // remove_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
2040         // Returns the items that were actually removed
2041         static int l_remove_item(lua_State *L)
2042         {
2043                 InvRef *ref = checkobject(L, 1);
2044                 const char *listname = luaL_checkstring(L, 2);
2045                 ItemStack item = read_item(L, 3);
2046                 InventoryList *list = getlist(L, ref, listname);
2047                 if(list){
2048                         ItemStack removed = list->removeItem(item);
2049                         if(!removed.empty())
2050                                 reportInventoryChange(L, ref);
2051                         LuaItemStack::create(L, removed);
2052                 } else {
2053                         LuaItemStack::create(L, ItemStack());
2054                 }
2055                 return 1;
2056         }
2057
2058 public:
2059         InvRef(const InventoryLocation &loc):
2060                 m_loc(loc)
2061         {
2062         }
2063
2064         ~InvRef()
2065         {
2066         }
2067
2068         // Creates an InvRef and leaves it on top of stack
2069         // Not callable from Lua; all references are created on the C side.
2070         static void create(lua_State *L, const InventoryLocation &loc)
2071         {
2072                 InvRef *o = new InvRef(loc);
2073                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
2074                 luaL_getmetatable(L, className);
2075                 lua_setmetatable(L, -2);
2076         }
2077         static void createPlayer(lua_State *L, Player *player)
2078         {
2079                 InventoryLocation loc;
2080                 loc.setPlayer(player->getName());
2081                 create(L, loc);
2082         }
2083         static void createNodeMeta(lua_State *L, v3s16 p)
2084         {
2085                 InventoryLocation loc;
2086                 loc.setNodeMeta(p);
2087                 create(L, loc);
2088         }
2089
2090         static void Register(lua_State *L)
2091         {
2092                 lua_newtable(L);
2093                 int methodtable = lua_gettop(L);
2094                 luaL_newmetatable(L, className);
2095                 int metatable = lua_gettop(L);
2096
2097                 lua_pushliteral(L, "__metatable");
2098                 lua_pushvalue(L, methodtable);
2099                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
2100
2101                 lua_pushliteral(L, "__index");
2102                 lua_pushvalue(L, methodtable);
2103                 lua_settable(L, metatable);
2104
2105                 lua_pushliteral(L, "__gc");
2106                 lua_pushcfunction(L, gc_object);
2107                 lua_settable(L, metatable);
2108
2109                 lua_pop(L, 1);  // drop metatable
2110
2111                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
2112                 lua_pop(L, 1);  // drop methodtable
2113
2114                 // Cannot be created from Lua
2115                 //lua_register(L, className, create_object);
2116         }
2117 };
2118 const char InvRef::className[] = "InvRef";
2119 const luaL_reg InvRef::methods[] = {
2120         method(InvRef, is_empty),
2121         method(InvRef, get_size),
2122         method(InvRef, set_size),
2123         method(InvRef, get_width),
2124         method(InvRef, set_width),
2125         method(InvRef, get_stack),
2126         method(InvRef, set_stack),
2127         method(InvRef, get_list),
2128         method(InvRef, set_list),
2129         method(InvRef, add_item),
2130         method(InvRef, room_for_item),
2131         method(InvRef, contains_item),
2132         method(InvRef, remove_item),
2133         {0,0}
2134 };
2135
2136 /*
2137         NodeMetaRef
2138 */
2139
2140 class NodeMetaRef
2141 {
2142 private:
2143         v3s16 m_p;
2144         ServerEnvironment *m_env;
2145
2146         static const char className[];
2147         static const luaL_reg methods[];
2148
2149         static NodeMetaRef *checkobject(lua_State *L, int narg)
2150         {
2151                 luaL_checktype(L, narg, LUA_TUSERDATA);
2152                 void *ud = luaL_checkudata(L, narg, className);
2153                 if(!ud) luaL_typerror(L, narg, className);
2154                 return *(NodeMetaRef**)ud;  // unbox pointer
2155         }
2156         
2157         static NodeMetadata* getmeta(NodeMetaRef *ref, bool auto_create)
2158         {
2159                 NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
2160                 if(meta == NULL && auto_create)
2161                 {
2162                         meta = new NodeMetadata(ref->m_env->getGameDef());
2163                         ref->m_env->getMap().setNodeMetadata(ref->m_p, meta);
2164                 }
2165                 return meta;
2166         }
2167
2168         static void reportMetadataChange(NodeMetaRef *ref)
2169         {
2170                 // NOTE: This same code is in rollback_interface.cpp
2171                 // Inform other things that the metadata has changed
2172                 v3s16 blockpos = getNodeBlockPos(ref->m_p);
2173                 MapEditEvent event;
2174                 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
2175                 event.p = blockpos;
2176                 ref->m_env->getMap().dispatchEvent(&event);
2177                 // Set the block to be saved
2178                 MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
2179                 if(block)
2180                         block->raiseModified(MOD_STATE_WRITE_NEEDED,
2181                                         "NodeMetaRef::reportMetadataChange");
2182         }
2183         
2184         // Exported functions
2185         
2186         // garbage collector
2187         static int gc_object(lua_State *L) {
2188                 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
2189                 delete o;
2190                 return 0;
2191         }
2192
2193         // get_string(self, name)
2194         static int l_get_string(lua_State *L)
2195         {
2196                 NodeMetaRef *ref = checkobject(L, 1);
2197                 std::string name = luaL_checkstring(L, 2);
2198
2199                 NodeMetadata *meta = getmeta(ref, false);
2200                 if(meta == NULL){
2201                         lua_pushlstring(L, "", 0);
2202                         return 1;
2203                 }
2204                 std::string str = meta->getString(name);
2205                 lua_pushlstring(L, str.c_str(), str.size());
2206                 return 1;
2207         }
2208
2209         // set_string(self, name, var)
2210         static int l_set_string(lua_State *L)
2211         {
2212                 NodeMetaRef *ref = checkobject(L, 1);
2213                 std::string name = luaL_checkstring(L, 2);
2214                 size_t len = 0;
2215                 const char *s = lua_tolstring(L, 3, &len);
2216                 std::string str(s, len);
2217
2218                 NodeMetadata *meta = getmeta(ref, !str.empty());
2219                 if(meta == NULL || str == meta->getString(name))
2220                         return 0;
2221                 meta->setString(name, str);
2222                 reportMetadataChange(ref);
2223                 return 0;
2224         }
2225
2226         // get_int(self, name)
2227         static int l_get_int(lua_State *L)
2228         {
2229                 NodeMetaRef *ref = checkobject(L, 1);
2230                 std::string name = lua_tostring(L, 2);
2231
2232                 NodeMetadata *meta = getmeta(ref, false);
2233                 if(meta == NULL){
2234                         lua_pushnumber(L, 0);
2235                         return 1;
2236                 }
2237                 std::string str = meta->getString(name);
2238                 lua_pushnumber(L, stoi(str));
2239                 return 1;
2240         }
2241
2242         // set_int(self, name, var)
2243         static int l_set_int(lua_State *L)
2244         {
2245                 NodeMetaRef *ref = checkobject(L, 1);
2246                 std::string name = lua_tostring(L, 2);
2247                 int a = lua_tointeger(L, 3);
2248                 std::string str = itos(a);
2249
2250                 NodeMetadata *meta = getmeta(ref, true);
2251                 if(meta == NULL || str == meta->getString(name))
2252                         return 0;
2253                 meta->setString(name, str);
2254                 reportMetadataChange(ref);
2255                 return 0;
2256         }
2257
2258         // get_float(self, name)
2259         static int l_get_float(lua_State *L)
2260         {
2261                 NodeMetaRef *ref = checkobject(L, 1);
2262                 std::string name = lua_tostring(L, 2);
2263
2264                 NodeMetadata *meta = getmeta(ref, false);
2265                 if(meta == NULL){
2266                         lua_pushnumber(L, 0);
2267                         return 1;
2268                 }
2269                 std::string str = meta->getString(name);
2270                 lua_pushnumber(L, stof(str));
2271                 return 1;
2272         }
2273
2274         // set_float(self, name, var)
2275         static int l_set_float(lua_State *L)
2276         {
2277                 NodeMetaRef *ref = checkobject(L, 1);
2278                 std::string name = lua_tostring(L, 2);
2279                 float a = lua_tonumber(L, 3);
2280                 std::string str = ftos(a);
2281
2282                 NodeMetadata *meta = getmeta(ref, true);
2283                 if(meta == NULL || str == meta->getString(name))
2284                         return 0;
2285                 meta->setString(name, str);
2286                 reportMetadataChange(ref);
2287                 return 0;
2288         }
2289
2290         // get_inventory(self)
2291         static int l_get_inventory(lua_State *L)
2292         {
2293                 NodeMetaRef *ref = checkobject(L, 1);
2294                 getmeta(ref, true);  // try to ensure the metadata exists
2295                 InvRef::createNodeMeta(L, ref->m_p);
2296                 return 1;
2297         }
2298         
2299         // to_table(self)
2300         static int l_to_table(lua_State *L)
2301         {
2302                 NodeMetaRef *ref = checkobject(L, 1);
2303
2304                 NodeMetadata *meta = getmeta(ref, true);
2305                 if(meta == NULL){
2306                         lua_pushnil(L);
2307                         return 1;
2308                 }
2309                 lua_newtable(L);
2310                 // fields
2311                 lua_newtable(L);
2312                 {
2313                         std::map<std::string, std::string> fields = meta->getStrings();
2314                         for(std::map<std::string, std::string>::const_iterator
2315                                         i = fields.begin(); i != fields.end(); i++){
2316                                 const std::string &name = i->first;
2317                                 const std::string &value = i->second;
2318                                 lua_pushlstring(L, name.c_str(), name.size());
2319                                 lua_pushlstring(L, value.c_str(), value.size());
2320                                 lua_settable(L, -3);
2321                         }
2322                 }
2323                 lua_setfield(L, -2, "fields");
2324                 // inventory
2325                 lua_newtable(L);
2326                 Inventory *inv = meta->getInventory();
2327                 if(inv){
2328                         std::vector<const InventoryList*> lists = inv->getLists();
2329                         for(std::vector<const InventoryList*>::const_iterator
2330                                         i = lists.begin(); i != lists.end(); i++){
2331                                 inventory_get_list_to_lua(inv, (*i)->getName().c_str(), L);
2332                                 lua_setfield(L, -2, (*i)->getName().c_str());
2333                         }
2334                 }
2335                 lua_setfield(L, -2, "inventory");
2336                 return 1;
2337         }
2338
2339         // from_table(self, table)
2340         static int l_from_table(lua_State *L)
2341         {
2342                 NodeMetaRef *ref = checkobject(L, 1);
2343                 int base = 2;
2344                 
2345                 if(lua_isnil(L, base)){
2346                         // No metadata
2347                         ref->m_env->getMap().removeNodeMetadata(ref->m_p);
2348                         lua_pushboolean(L, true);
2349                         return 1;
2350                 }
2351
2352                 // Has metadata; clear old one first
2353                 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
2354                 // Create new metadata
2355                 NodeMetadata *meta = getmeta(ref, true);
2356                 // Set fields
2357                 lua_getfield(L, base, "fields");
2358                 int fieldstable = lua_gettop(L);
2359                 lua_pushnil(L);
2360                 while(lua_next(L, fieldstable) != 0){
2361                         // key at index -2 and value at index -1
2362                         std::string name = lua_tostring(L, -2);
2363                         size_t cl;
2364                         const char *cs = lua_tolstring(L, -1, &cl);
2365                         std::string value(cs, cl);
2366                         meta->setString(name, value);
2367                         lua_pop(L, 1); // removes value, keeps key for next iteration
2368                 }
2369                 // Set inventory
2370                 Inventory *inv = meta->getInventory();
2371                 lua_getfield(L, base, "inventory");
2372                 int inventorytable = lua_gettop(L);
2373                 lua_pushnil(L);
2374                 while(lua_next(L, inventorytable) != 0){
2375                         // key at index -2 and value at index -1
2376                         std::string name = lua_tostring(L, -2);
2377                         inventory_set_list_from_lua(inv, name.c_str(), L, -1);
2378                         lua_pop(L, 1); // removes value, keeps key for next iteration
2379                 }
2380                 reportMetadataChange(ref);
2381                 lua_pushboolean(L, true);
2382                 return 1;
2383         }
2384
2385 public:
2386         NodeMetaRef(v3s16 p, ServerEnvironment *env):
2387                 m_p(p),
2388                 m_env(env)
2389         {
2390         }
2391
2392         ~NodeMetaRef()
2393         {
2394         }
2395
2396         // Creates an NodeMetaRef and leaves it on top of stack
2397         // Not callable from Lua; all references are created on the C side.
2398         static void create(lua_State *L, v3s16 p, ServerEnvironment *env)
2399         {
2400                 NodeMetaRef *o = new NodeMetaRef(p, env);
2401                 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
2402                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
2403                 luaL_getmetatable(L, className);
2404                 lua_setmetatable(L, -2);
2405         }
2406
2407         static void Register(lua_State *L)
2408         {
2409                 lua_newtable(L);
2410                 int methodtable = lua_gettop(L);
2411                 luaL_newmetatable(L, className);
2412                 int metatable = lua_gettop(L);
2413
2414                 lua_pushliteral(L, "__metatable");
2415                 lua_pushvalue(L, methodtable);
2416                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
2417
2418                 lua_pushliteral(L, "__index");
2419                 lua_pushvalue(L, methodtable);
2420                 lua_settable(L, metatable);
2421
2422                 lua_pushliteral(L, "__gc");
2423                 lua_pushcfunction(L, gc_object);
2424                 lua_settable(L, metatable);
2425
2426                 lua_pop(L, 1);  // drop metatable
2427
2428                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
2429                 lua_pop(L, 1);  // drop methodtable
2430
2431                 // Cannot be created from Lua
2432                 //lua_register(L, className, create_object);
2433         }
2434 };
2435 const char NodeMetaRef::className[] = "NodeMetaRef";
2436 const luaL_reg NodeMetaRef::methods[] = {
2437         method(NodeMetaRef, get_string),
2438         method(NodeMetaRef, set_string),
2439         method(NodeMetaRef, get_int),
2440         method(NodeMetaRef, set_int),
2441         method(NodeMetaRef, get_float),
2442         method(NodeMetaRef, set_float),
2443         method(NodeMetaRef, get_inventory),
2444         method(NodeMetaRef, to_table),
2445         method(NodeMetaRef, from_table),
2446         {0,0}
2447 };
2448
2449 /*
2450         ObjectRef
2451 */
2452
2453 class ObjectRef
2454 {
2455 private:
2456         ServerActiveObject *m_object;
2457
2458         static const char className[];
2459         static const luaL_reg methods[];
2460 public:
2461         static ObjectRef *checkobject(lua_State *L, int narg)
2462         {
2463                 luaL_checktype(L, narg, LUA_TUSERDATA);
2464                 void *ud = luaL_checkudata(L, narg, className);
2465                 if(!ud) luaL_typerror(L, narg, className);
2466                 return *(ObjectRef**)ud;  // unbox pointer
2467         }
2468         
2469         static ServerActiveObject* getobject(ObjectRef *ref)
2470         {
2471                 ServerActiveObject *co = ref->m_object;
2472                 return co;
2473         }
2474 private:
2475         static LuaEntitySAO* getluaobject(ObjectRef *ref)
2476         {
2477                 ServerActiveObject *obj = getobject(ref);
2478                 if(obj == NULL)
2479                         return NULL;
2480                 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
2481                         return NULL;
2482                 return (LuaEntitySAO*)obj;
2483         }
2484         
2485         static PlayerSAO* getplayersao(ObjectRef *ref)
2486         {
2487                 ServerActiveObject *obj = getobject(ref);
2488                 if(obj == NULL)
2489                         return NULL;
2490                 if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
2491                         return NULL;
2492                 return (PlayerSAO*)obj;
2493         }
2494         
2495         static Player* getplayer(ObjectRef *ref)
2496         {
2497                 PlayerSAO *playersao = getplayersao(ref);
2498                 if(playersao == NULL)
2499                         return NULL;
2500                 return playersao->getPlayer();
2501         }
2502         
2503         // Exported functions
2504         
2505         // garbage collector
2506         static int gc_object(lua_State *L) {
2507                 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
2508                 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
2509                 delete o;
2510                 return 0;
2511         }
2512
2513         // remove(self)
2514         static int l_remove(lua_State *L)
2515         {
2516                 ObjectRef *ref = checkobject(L, 1);
2517                 ServerActiveObject *co = getobject(ref);
2518                 if(co == NULL) return 0;
2519                 verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
2520                 co->m_removed = true;
2521                 return 0;
2522         }
2523         
2524         // getpos(self)
2525         // returns: {x=num, y=num, z=num}
2526         static int l_getpos(lua_State *L)
2527         {
2528                 ObjectRef *ref = checkobject(L, 1);
2529                 ServerActiveObject *co = getobject(ref);
2530                 if(co == NULL) return 0;
2531                 v3f pos = co->getBasePosition() / BS;
2532                 lua_newtable(L);
2533                 lua_pushnumber(L, pos.X);
2534                 lua_setfield(L, -2, "x");
2535                 lua_pushnumber(L, pos.Y);
2536                 lua_setfield(L, -2, "y");
2537                 lua_pushnumber(L, pos.Z);
2538                 lua_setfield(L, -2, "z");
2539                 return 1;
2540         }
2541         
2542         // setpos(self, pos)
2543         static int l_setpos(lua_State *L)
2544         {
2545                 ObjectRef *ref = checkobject(L, 1);
2546                 //LuaEntitySAO *co = getluaobject(ref);
2547                 ServerActiveObject *co = getobject(ref);
2548                 if(co == NULL) return 0;
2549                 // pos
2550                 v3f pos = checkFloatPos(L, 2);
2551                 // Do it
2552                 co->setPos(pos);
2553                 return 0;
2554         }
2555         
2556         // moveto(self, pos, continuous=false)
2557         static int l_moveto(lua_State *L)
2558         {
2559                 ObjectRef *ref = checkobject(L, 1);
2560                 //LuaEntitySAO *co = getluaobject(ref);
2561                 ServerActiveObject *co = getobject(ref);
2562                 if(co == NULL) return 0;
2563                 // pos
2564                 v3f pos = checkFloatPos(L, 2);
2565                 // continuous
2566                 bool continuous = lua_toboolean(L, 3);
2567                 // Do it
2568                 co->moveTo(pos, continuous);
2569                 return 0;
2570         }
2571
2572         // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
2573         static int l_punch(lua_State *L)
2574         {
2575                 ObjectRef *ref = checkobject(L, 1);
2576                 ObjectRef *puncher_ref = checkobject(L, 2);
2577                 ServerActiveObject *co = getobject(ref);
2578                 ServerActiveObject *puncher = getobject(puncher_ref);
2579                 if(co == NULL) return 0;
2580                 if(puncher == NULL) return 0;
2581                 v3f dir;
2582                 if(lua_type(L, 5) != LUA_TTABLE)
2583                         dir = co->getBasePosition() - puncher->getBasePosition();
2584                 else
2585                         dir = read_v3f(L, 5);
2586                 float time_from_last_punch = 1000000;
2587                 if(lua_isnumber(L, 3))
2588                         time_from_last_punch = lua_tonumber(L, 3);
2589                 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
2590                 dir.normalize();
2591                 // Do it
2592                 co->punch(dir, &toolcap, puncher, time_from_last_punch);
2593                 return 0;
2594         }
2595
2596         // right_click(self, clicker); clicker = an another ObjectRef
2597         static int l_right_click(lua_State *L)
2598         {
2599                 ObjectRef *ref = checkobject(L, 1);
2600                 ObjectRef *ref2 = checkobject(L, 2);
2601                 ServerActiveObject *co = getobject(ref);
2602                 ServerActiveObject *co2 = getobject(ref2);
2603                 if(co == NULL) return 0;
2604                 if(co2 == NULL) return 0;
2605                 // Do it
2606                 co->rightClick(co2);
2607                 return 0;
2608         }
2609
2610         // set_hp(self, hp)
2611         // hp = number of hitpoints (2 * number of hearts)
2612         // returns: nil
2613         static int l_set_hp(lua_State *L)
2614         {
2615                 ObjectRef *ref = checkobject(L, 1);
2616                 luaL_checknumber(L, 2);
2617                 ServerActiveObject *co = getobject(ref);
2618                 if(co == NULL) return 0;
2619                 int hp = lua_tonumber(L, 2);
2620                 /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
2621                                 <<" hp="<<hp<<std::endl;*/
2622                 // Do it
2623                 co->setHP(hp);
2624                 // Return
2625                 return 0;
2626         }
2627
2628         // get_hp(self)
2629         // returns: number of hitpoints (2 * number of hearts)
2630         // 0 if not applicable to this type of object
2631         static int l_get_hp(lua_State *L)
2632         {
2633                 ObjectRef *ref = checkobject(L, 1);
2634                 ServerActiveObject *co = getobject(ref);
2635                 if(co == NULL){
2636                         // Default hp is 1
2637                         lua_pushnumber(L, 1);
2638                         return 1;
2639                 }
2640                 int hp = co->getHP();
2641                 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
2642                                 <<" hp="<<hp<<std::endl;*/
2643                 // Return
2644                 lua_pushnumber(L, hp);
2645                 return 1;
2646         }
2647
2648         // get_inventory(self)
2649         static int l_get_inventory(lua_State *L)
2650         {
2651                 ObjectRef *ref = checkobject(L, 1);
2652                 ServerActiveObject *co = getobject(ref);
2653                 if(co == NULL) return 0;
2654                 // Do it
2655                 InventoryLocation loc = co->getInventoryLocation();
2656                 if(get_server(L)->getInventory(loc) != NULL)
2657                         InvRef::create(L, loc);
2658                 else
2659                         lua_pushnil(L); // An object may have no inventory (nil)
2660                 return 1;
2661         }
2662
2663         // get_wield_list(self)
2664         static int l_get_wield_list(lua_State *L)
2665         {
2666                 ObjectRef *ref = checkobject(L, 1);
2667                 ServerActiveObject *co = getobject(ref);
2668                 if(co == NULL) return 0;
2669                 // Do it
2670                 lua_pushstring(L, co->getWieldList().c_str());
2671                 return 1;
2672         }
2673
2674         // get_wield_index(self)
2675         static int l_get_wield_index(lua_State *L)
2676         {
2677                 ObjectRef *ref = checkobject(L, 1);
2678                 ServerActiveObject *co = getobject(ref);
2679                 if(co == NULL) return 0;
2680                 // Do it
2681                 lua_pushinteger(L, co->getWieldIndex() + 1);
2682                 return 1;
2683         }
2684
2685         // get_wielded_item(self)
2686         static int l_get_wielded_item(lua_State *L)
2687         {
2688                 ObjectRef *ref = checkobject(L, 1);
2689                 ServerActiveObject *co = getobject(ref);
2690                 if(co == NULL){
2691                         // Empty ItemStack
2692                         LuaItemStack::create(L, ItemStack());
2693                         return 1;
2694                 }
2695                 // Do it
2696                 LuaItemStack::create(L, co->getWieldedItem());
2697                 return 1;
2698         }
2699
2700         // set_wielded_item(self, itemstack or itemstring or table or nil)
2701         static int l_set_wielded_item(lua_State *L)
2702         {
2703                 ObjectRef *ref = checkobject(L, 1);
2704                 ServerActiveObject *co = getobject(ref);
2705                 if(co == NULL) return 0;
2706                 // Do it
2707                 ItemStack item = read_item(L, 2);
2708                 bool success = co->setWieldedItem(item);
2709                 lua_pushboolean(L, success);
2710                 return 1;
2711         }
2712
2713         // set_armor_groups(self, groups)
2714         static int l_set_armor_groups(lua_State *L)
2715         {
2716                 ObjectRef *ref = checkobject(L, 1);
2717                 ServerActiveObject *co = getobject(ref);
2718                 if(co == NULL) return 0;
2719                 // Do it
2720                 ItemGroupList groups;
2721                 read_groups(L, 2, groups);
2722                 co->setArmorGroups(groups);
2723                 return 0;
2724         }
2725
2726         // set_properties(self, properties)
2727         static int l_set_properties(lua_State *L)
2728         {
2729                 ObjectRef *ref = checkobject(L, 1);
2730                 ServerActiveObject *co = getobject(ref);
2731                 if(co == NULL) return 0;
2732                 ObjectProperties *prop = co->accessObjectProperties();
2733                 if(!prop)
2734                         return 0;
2735                 read_object_properties(L, 2, prop);
2736                 co->notifyObjectPropertiesModified();
2737                 return 0;
2738         }
2739
2740         /* LuaEntitySAO-only */
2741
2742         // setvelocity(self, {x=num, y=num, z=num})
2743         static int l_setvelocity(lua_State *L)
2744         {
2745                 ObjectRef *ref = checkobject(L, 1);
2746                 LuaEntitySAO *co = getluaobject(ref);
2747                 if(co == NULL) return 0;
2748                 v3f pos = checkFloatPos(L, 2);
2749                 // Do it
2750                 co->setVelocity(pos);
2751                 return 0;
2752         }
2753         
2754         // getvelocity(self)
2755         static int l_getvelocity(lua_State *L)
2756         {
2757                 ObjectRef *ref = checkobject(L, 1);
2758                 LuaEntitySAO *co = getluaobject(ref);
2759                 if(co == NULL) return 0;
2760                 // Do it
2761                 v3f v = co->getVelocity();
2762                 pushFloatPos(L, v);
2763                 return 1;
2764         }
2765         
2766         // setacceleration(self, {x=num, y=num, z=num})
2767         static int l_setacceleration(lua_State *L)
2768         {
2769                 ObjectRef *ref = checkobject(L, 1);
2770                 LuaEntitySAO *co = getluaobject(ref);
2771                 if(co == NULL) return 0;
2772                 // pos
2773                 v3f pos = checkFloatPos(L, 2);
2774                 // Do it
2775                 co->setAcceleration(pos);
2776                 return 0;
2777         }
2778         
2779         // getacceleration(self)
2780         static int l_getacceleration(lua_State *L)
2781         {
2782                 ObjectRef *ref = checkobject(L, 1);
2783                 LuaEntitySAO *co = getluaobject(ref);
2784                 if(co == NULL) return 0;
2785                 // Do it
2786                 v3f v = co->getAcceleration();
2787                 pushFloatPos(L, v);
2788                 return 1;
2789         }
2790         
2791         // setyaw(self, radians)
2792         static int l_setyaw(lua_State *L)
2793         {
2794                 ObjectRef *ref = checkobject(L, 1);
2795                 LuaEntitySAO *co = getluaobject(ref);
2796                 if(co == NULL) return 0;
2797                 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
2798                 // Do it
2799                 co->setYaw(yaw);
2800                 return 0;
2801         }
2802         
2803         // getyaw(self)
2804         static int l_getyaw(lua_State *L)
2805         {
2806                 ObjectRef *ref = checkobject(L, 1);
2807                 LuaEntitySAO *co = getluaobject(ref);
2808                 if(co == NULL) return 0;
2809                 // Do it
2810                 float yaw = co->getYaw() * core::DEGTORAD;
2811                 lua_pushnumber(L, yaw);
2812                 return 1;
2813         }
2814         
2815         // settexturemod(self, mod)
2816         static int l_settexturemod(lua_State *L)
2817         {
2818                 ObjectRef *ref = checkobject(L, 1);
2819                 LuaEntitySAO *co = getluaobject(ref);
2820                 if(co == NULL) return 0;
2821                 // Do it
2822                 std::string mod = luaL_checkstring(L, 2);
2823                 co->setTextureMod(mod);
2824                 return 0;
2825         }
2826         
2827         // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
2828         //           select_horiz_by_yawpitch=false)
2829         static int l_setsprite(lua_State *L)
2830         {
2831                 ObjectRef *ref = checkobject(L, 1);
2832                 LuaEntitySAO *co = getluaobject(ref);
2833                 if(co == NULL) return 0;
2834                 // Do it
2835                 v2s16 p(0,0);
2836                 if(!lua_isnil(L, 2))
2837                         p = read_v2s16(L, 2);
2838                 int num_frames = 1;
2839                 if(!lua_isnil(L, 3))
2840                         num_frames = lua_tonumber(L, 3);
2841                 float framelength = 0.2;
2842                 if(!lua_isnil(L, 4))
2843                         framelength = lua_tonumber(L, 4);
2844                 bool select_horiz_by_yawpitch = false;
2845                 if(!lua_isnil(L, 5))
2846                         select_horiz_by_yawpitch = lua_toboolean(L, 5);
2847                 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
2848                 return 0;
2849         }
2850
2851         // setanimations(self, mod)
2852         static int l_setanimations(lua_State *L)
2853         {
2854                 ObjectRef *ref = checkobject(L, 1);
2855                 LuaEntitySAO *co = getluaobject(ref);
2856                 if(co == NULL) return 0;
2857                 // Do it
2858                 v2s16 p(0,0);
2859                 int frame_start = 0;
2860                 if(!lua_isnil(L, 2))
2861                         frame_start = lua_tonumber(L, 2);
2862                 int frame_end = 0;
2863                 if(!lua_isnil(L, 3))
2864                         frame_end = lua_tonumber(L, 3);
2865                 float frame_speed = 15;
2866                 if(!lua_isnil(L, 4))
2867                         frame_speed = lua_tonumber(L, 4);
2868                 float frame_blend = 0;
2869                 if(!lua_isnil(L, 5))
2870                         frame_blend = lua_tonumber(L, 5);
2871                 co->setAnimations(frame_start, frame_end, frame_speed, frame_blend);
2872                 return 0;
2873         }
2874
2875         // DEPRECATED
2876         // get_entity_name(self)
2877         static int l_get_entity_name(lua_State *L)
2878         {
2879                 ObjectRef *ref = checkobject(L, 1);
2880                 LuaEntitySAO *co = getluaobject(ref);
2881                 if(co == NULL) return 0;
2882                 // Do it
2883                 std::string name = co->getName();
2884                 lua_pushstring(L, name.c_str());
2885                 return 1;
2886         }
2887         
2888         // get_luaentity(self)
2889         static int l_get_luaentity(lua_State *L)
2890         {
2891                 ObjectRef *ref = checkobject(L, 1);
2892                 LuaEntitySAO *co = getluaobject(ref);
2893                 if(co == NULL) return 0;
2894                 // Do it
2895                 luaentity_get(L, co->getId());
2896                 return 1;
2897         }
2898         
2899         /* Player-only */
2900
2901         // is_player(self)
2902         static int l_is_player(lua_State *L)
2903         {
2904                 ObjectRef *ref = checkobject(L, 1);
2905                 Player *player = getplayer(ref);
2906                 lua_pushboolean(L, (player != NULL));
2907                 return 1;
2908         }
2909         
2910         // get_player_name(self)
2911         static int l_get_player_name(lua_State *L)
2912         {
2913                 ObjectRef *ref = checkobject(L, 1);
2914                 Player *player = getplayer(ref);
2915                 if(player == NULL){
2916                         lua_pushlstring(L, "", 0);
2917                         return 1;
2918                 }
2919                 // Do it
2920                 lua_pushstring(L, player->getName());
2921                 return 1;
2922         }
2923         
2924         // get_look_dir(self)
2925         static int l_get_look_dir(lua_State *L)
2926         {
2927                 ObjectRef *ref = checkobject(L, 1);
2928                 Player *player = getplayer(ref);
2929                 if(player == NULL) return 0;
2930                 // Do it
2931                 float pitch = player->getRadPitch();
2932                 float yaw = player->getRadYaw();
2933                 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
2934                 push_v3f(L, v);
2935                 return 1;
2936         }
2937
2938         // get_look_pitch(self)
2939         static int l_get_look_pitch(lua_State *L)
2940         {
2941                 ObjectRef *ref = checkobject(L, 1);
2942                 Player *player = getplayer(ref);
2943                 if(player == NULL) return 0;
2944                 // Do it
2945                 lua_pushnumber(L, player->getRadPitch());
2946                 return 1;
2947         }
2948
2949         // get_look_yaw(self)
2950         static int l_get_look_yaw(lua_State *L)
2951         {
2952                 ObjectRef *ref = checkobject(L, 1);
2953                 Player *player = getplayer(ref);
2954                 if(player == NULL) return 0;
2955                 // Do it
2956                 lua_pushnumber(L, player->getRadYaw());
2957                 return 1;
2958         }
2959
2960         // set_inventory_formspec(self, formspec)
2961         static int l_set_inventory_formspec(lua_State *L)
2962         {
2963                 ObjectRef *ref = checkobject(L, 1);
2964                 Player *player = getplayer(ref);
2965                 if(player == NULL) return 0;
2966                 std::string formspec = luaL_checkstring(L, 2);
2967
2968                 player->inventory_formspec = formspec;
2969                 get_server(L)->reportInventoryFormspecModified(player->getName());
2970                 lua_pushboolean(L, true);
2971                 return 1;
2972         }
2973
2974         // get_inventory_formspec(self) -> formspec
2975         static int l_get_inventory_formspec(lua_State *L)
2976         {
2977                 ObjectRef *ref = checkobject(L, 1);
2978                 Player *player = getplayer(ref);
2979                 if(player == NULL) return 0;
2980
2981                 std::string formspec = player->inventory_formspec;
2982                 lua_pushlstring(L, formspec.c_str(), formspec.size());
2983                 return 1;
2984         }
2985
2986 public:
2987         ObjectRef(ServerActiveObject *object):
2988                 m_object(object)
2989         {
2990                 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
2991         }
2992
2993         ~ObjectRef()
2994         {
2995                 /*if(m_object)
2996                         infostream<<"ObjectRef destructing for id="
2997                                         <<m_object->getId()<<std::endl;
2998                 else
2999                         infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
3000         }
3001
3002         // Creates an ObjectRef and leaves it on top of stack
3003         // Not callable from Lua; all references are created on the C side.
3004         static void create(lua_State *L, ServerActiveObject *object)
3005         {
3006                 ObjectRef *o = new ObjectRef(object);
3007                 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
3008                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
3009                 luaL_getmetatable(L, className);
3010                 lua_setmetatable(L, -2);
3011         }
3012
3013         static void set_null(lua_State *L)
3014         {
3015                 ObjectRef *o = checkobject(L, -1);
3016                 o->m_object = NULL;
3017         }
3018         
3019         static void Register(lua_State *L)
3020         {
3021                 lua_newtable(L);
3022                 int methodtable = lua_gettop(L);
3023                 luaL_newmetatable(L, className);
3024                 int metatable = lua_gettop(L);
3025
3026                 lua_pushliteral(L, "__metatable");
3027                 lua_pushvalue(L, methodtable);
3028                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
3029
3030                 lua_pushliteral(L, "__index");
3031                 lua_pushvalue(L, methodtable);
3032                 lua_settable(L, metatable);
3033
3034                 lua_pushliteral(L, "__gc");
3035                 lua_pushcfunction(L, gc_object);
3036                 lua_settable(L, metatable);
3037
3038                 lua_pop(L, 1);  // drop metatable
3039
3040                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
3041                 lua_pop(L, 1);  // drop methodtable
3042
3043                 // Cannot be created from Lua
3044                 //lua_register(L, className, create_object);
3045         }
3046 };
3047 const char ObjectRef::className[] = "ObjectRef";
3048 const luaL_reg ObjectRef::methods[] = {
3049         // ServerActiveObject
3050         method(ObjectRef, remove),
3051         method(ObjectRef, getpos),
3052         method(ObjectRef, setpos),
3053         method(ObjectRef, moveto),
3054         method(ObjectRef, punch),
3055         method(ObjectRef, right_click),
3056         method(ObjectRef, set_hp),
3057         method(ObjectRef, get_hp),
3058         method(ObjectRef, get_inventory),
3059         method(ObjectRef, get_wield_list),
3060         method(ObjectRef, get_wield_index),
3061         method(ObjectRef, get_wielded_item),
3062         method(ObjectRef, set_wielded_item),
3063         method(ObjectRef, set_armor_groups),
3064         method(ObjectRef, set_properties),
3065         // LuaEntitySAO-only
3066         method(ObjectRef, setvelocity),
3067         method(ObjectRef, getvelocity),
3068         method(ObjectRef, setacceleration),
3069         method(ObjectRef, getacceleration),
3070         method(ObjectRef, setyaw),
3071         method(ObjectRef, getyaw),
3072         method(ObjectRef, settexturemod),
3073         method(ObjectRef, setsprite),
3074         method(ObjectRef, setanimations),
3075         method(ObjectRef, get_entity_name),
3076         method(ObjectRef, get_luaentity),
3077         // Player-only
3078         method(ObjectRef, is_player),
3079         method(ObjectRef, get_player_name),
3080         method(ObjectRef, get_look_dir),
3081         method(ObjectRef, get_look_pitch),
3082         method(ObjectRef, get_look_yaw),
3083         method(ObjectRef, set_inventory_formspec),
3084         method(ObjectRef, get_inventory_formspec),
3085         {0,0}
3086 };
3087
3088 // Creates a new anonymous reference if cobj=NULL or id=0
3089 static void objectref_get_or_create(lua_State *L,
3090                 ServerActiveObject *cobj)
3091 {
3092         if(cobj == NULL || cobj->getId() == 0){
3093                 ObjectRef::create(L, cobj);
3094         } else {
3095                 objectref_get(L, cobj->getId());
3096         }
3097 }
3098
3099
3100 /*
3101   PerlinNoise
3102  */
3103
3104 class LuaPerlinNoise
3105 {
3106 private:
3107         int seed;
3108         int octaves;
3109         double persistence;
3110         double scale;
3111         static const char className[];
3112         static const luaL_reg methods[];
3113
3114         // Exported functions
3115
3116         // garbage collector
3117         static int gc_object(lua_State *L)
3118         {
3119                 LuaPerlinNoise *o = *(LuaPerlinNoise **)(lua_touserdata(L, 1));
3120                 delete o;
3121                 return 0;
3122         }
3123
3124         static int l_get2d(lua_State *L)
3125         {
3126                 LuaPerlinNoise *o = checkobject(L, 1);
3127                 v2f pos2d = read_v2f(L,2);
3128                 lua_Number val = noise2d_perlin(pos2d.X/o->scale, pos2d.Y/o->scale, o->seed, o->octaves, o->persistence);
3129                 lua_pushnumber(L, val);
3130                 return 1;
3131         }
3132         static int l_get3d(lua_State *L)
3133         {
3134                 LuaPerlinNoise *o = checkobject(L, 1);
3135                 v3f pos3d = read_v3f(L,2);
3136                 lua_Number val = noise3d_perlin(pos3d.X/o->scale, pos3d.Y/o->scale, pos3d.Z/o->scale, o->seed, o->octaves, o->persistence);
3137                 lua_pushnumber(L, val);
3138                 return 1;
3139         }
3140
3141 public:
3142         LuaPerlinNoise(int a_seed, int a_octaves, double a_persistence,
3143                         double a_scale):
3144                 seed(a_seed),
3145                 octaves(a_octaves),
3146                 persistence(a_persistence),
3147                 scale(a_scale)
3148         {
3149         }
3150
3151         ~LuaPerlinNoise()
3152         {
3153         }
3154
3155         // LuaPerlinNoise(seed, octaves, persistence, scale)
3156         // Creates an LuaPerlinNoise and leaves it on top of stack
3157         static int create_object(lua_State *L)
3158         {
3159                 int seed = luaL_checkint(L, 1);
3160                 int octaves = luaL_checkint(L, 2);
3161                 double persistence = luaL_checknumber(L, 3);
3162                 double scale = luaL_checknumber(L, 4);
3163                 LuaPerlinNoise *o = new LuaPerlinNoise(seed, octaves, persistence, scale);
3164                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
3165                 luaL_getmetatable(L, className);
3166                 lua_setmetatable(L, -2);
3167                 return 1;
3168         }
3169
3170         static LuaPerlinNoise* checkobject(lua_State *L, int narg)
3171         {
3172                 luaL_checktype(L, narg, LUA_TUSERDATA);
3173                 void *ud = luaL_checkudata(L, narg, className);
3174                 if(!ud) luaL_typerror(L, narg, className);
3175                 return *(LuaPerlinNoise**)ud;  // unbox pointer
3176         }
3177
3178         static void Register(lua_State *L)
3179         {
3180                 lua_newtable(L);
3181                 int methodtable = lua_gettop(L);
3182                 luaL_newmetatable(L, className);
3183                 int metatable = lua_gettop(L);
3184
3185                 lua_pushliteral(L, "__metatable");
3186                 lua_pushvalue(L, methodtable);
3187                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
3188
3189                 lua_pushliteral(L, "__index");
3190                 lua_pushvalue(L, methodtable);
3191                 lua_settable(L, metatable);
3192
3193                 lua_pushliteral(L, "__gc");
3194                 lua_pushcfunction(L, gc_object);
3195                 lua_settable(L, metatable);
3196
3197                 lua_pop(L, 1);  // drop metatable
3198
3199                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
3200                 lua_pop(L, 1);  // drop methodtable
3201
3202                 // Can be created from Lua (PerlinNoise(seed, octaves, persistence)
3203                 lua_register(L, className, create_object);
3204         }
3205 };
3206 const char LuaPerlinNoise::className[] = "PerlinNoise";
3207 const luaL_reg LuaPerlinNoise::methods[] = {
3208         method(LuaPerlinNoise, get2d),
3209         method(LuaPerlinNoise, get3d),
3210         {0,0}
3211 };
3212
3213 /*
3214         NodeTimerRef
3215 */
3216
3217 class NodeTimerRef
3218 {
3219 private:
3220         v3s16 m_p;
3221         ServerEnvironment *m_env;
3222
3223         static const char className[];
3224         static const luaL_reg methods[];
3225
3226         static int gc_object(lua_State *L) {
3227                 NodeTimerRef *o = *(NodeTimerRef **)(lua_touserdata(L, 1));
3228                 delete o;
3229                 return 0;
3230         }
3231
3232         static NodeTimerRef *checkobject(lua_State *L, int narg)
3233         {
3234                 luaL_checktype(L, narg, LUA_TUSERDATA);
3235                 void *ud = luaL_checkudata(L, narg, className);
3236                 if(!ud) luaL_typerror(L, narg, className);
3237                 return *(NodeTimerRef**)ud;  // unbox pointer
3238         }
3239         
3240         static int l_set(lua_State *L)
3241         {
3242                 NodeTimerRef *o = checkobject(L, 1);
3243                 ServerEnvironment *env = o->m_env;
3244                 if(env == NULL) return 0;
3245                 f32 t = luaL_checknumber(L,2);
3246                 f32 e = luaL_checknumber(L,3);
3247                 env->getMap().setNodeTimer(o->m_p,NodeTimer(t,e));
3248                 return 0;
3249         }
3250         
3251         static int l_start(lua_State *L)
3252         {
3253                 NodeTimerRef *o = checkobject(L, 1);
3254                 ServerEnvironment *env = o->m_env;
3255                 if(env == NULL) return 0;
3256                 f32 t = luaL_checknumber(L,2);
3257                 env->getMap().setNodeTimer(o->m_p,NodeTimer(t,0));
3258                 return 0;
3259         }
3260         
3261         static int l_stop(lua_State *L)
3262         {
3263                 NodeTimerRef *o = checkobject(L, 1);
3264                 ServerEnvironment *env = o->m_env;
3265                 if(env == NULL) return 0;
3266                 env->getMap().removeNodeTimer(o->m_p);
3267                 return 0;
3268         }
3269         
3270         static int l_is_started(lua_State *L)
3271         {
3272                 NodeTimerRef *o = checkobject(L, 1);
3273                 ServerEnvironment *env = o->m_env;
3274                 if(env == NULL) return 0;
3275
3276                 NodeTimer t = env->getMap().getNodeTimer(o->m_p);
3277                 lua_pushboolean(L,(t.timeout != 0));
3278                 return 1;
3279         }
3280         
3281         static int l_get_timeout(lua_State *L)
3282         {
3283                 NodeTimerRef *o = checkobject(L, 1);
3284                 ServerEnvironment *env = o->m_env;
3285                 if(env == NULL) return 0;
3286
3287                 NodeTimer t = env->getMap().getNodeTimer(o->m_p);
3288                 lua_pushnumber(L,t.timeout);
3289                 return 1;
3290         }
3291         
3292         static int l_get_elapsed(lua_State *L)
3293         {
3294                 NodeTimerRef *o = checkobject(L, 1);
3295                 ServerEnvironment *env = o->m_env;
3296                 if(env == NULL) return 0;
3297
3298                 NodeTimer t = env->getMap().getNodeTimer(o->m_p);
3299                 lua_pushnumber(L,t.elapsed);
3300                 return 1;
3301         }
3302
3303 public:
3304         NodeTimerRef(v3s16 p, ServerEnvironment *env):
3305                 m_p(p),
3306                 m_env(env)
3307         {
3308         }
3309
3310         ~NodeTimerRef()
3311         {
3312         }
3313
3314         // Creates an NodeTimerRef and leaves it on top of stack
3315         // Not callable from Lua; all references are created on the C side.
3316         static void create(lua_State *L, v3s16 p, ServerEnvironment *env)
3317         {
3318                 NodeTimerRef *o = new NodeTimerRef(p, env);
3319                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
3320                 luaL_getmetatable(L, className);
3321                 lua_setmetatable(L, -2);
3322         }
3323
3324         static void set_null(lua_State *L)
3325         {
3326                 NodeTimerRef *o = checkobject(L, -1);
3327                 o->m_env = NULL;
3328         }
3329         
3330         static void Register(lua_State *L)
3331         {
3332                 lua_newtable(L);
3333                 int methodtable = lua_gettop(L);
3334                 luaL_newmetatable(L, className);
3335                 int metatable = lua_gettop(L);
3336
3337                 lua_pushliteral(L, "__metatable");
3338                 lua_pushvalue(L, methodtable);
3339                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
3340
3341                 lua_pushliteral(L, "__index");
3342                 lua_pushvalue(L, methodtable);
3343                 lua_settable(L, metatable);
3344
3345                 lua_pushliteral(L, "__gc");
3346                 lua_pushcfunction(L, gc_object);
3347                 lua_settable(L, metatable);
3348
3349                 lua_pop(L, 1);  // drop metatable
3350
3351                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
3352                 lua_pop(L, 1);  // drop methodtable
3353
3354                 // Cannot be created from Lua
3355                 //lua_register(L, className, create_object);
3356         }
3357 };
3358 const char NodeTimerRef::className[] = "NodeTimerRef";
3359 const luaL_reg NodeTimerRef::methods[] = {
3360         method(NodeTimerRef, start),
3361         method(NodeTimerRef, set),
3362         method(NodeTimerRef, stop),
3363         method(NodeTimerRef, is_started),
3364         method(NodeTimerRef, get_timeout),
3365         method(NodeTimerRef, get_elapsed),
3366         {0,0}
3367 };
3368
3369 /*
3370         EnvRef
3371 */
3372
3373 class EnvRef
3374 {
3375 private:
3376         ServerEnvironment *m_env;
3377
3378         static const char className[];
3379         static const luaL_reg methods[];
3380
3381         static int gc_object(lua_State *L) {
3382                 EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
3383                 delete o;
3384                 return 0;
3385         }
3386
3387         static EnvRef *checkobject(lua_State *L, int narg)
3388         {
3389                 luaL_checktype(L, narg, LUA_TUSERDATA);
3390                 void *ud = luaL_checkudata(L, narg, className);
3391                 if(!ud) luaL_typerror(L, narg, className);
3392                 return *(EnvRef**)ud;  // unbox pointer
3393         }
3394         
3395         // Exported functions
3396
3397         // EnvRef:set_node(pos, node)
3398         // pos = {x=num, y=num, z=num}
3399         static int l_set_node(lua_State *L)
3400         {
3401                 EnvRef *o = checkobject(L, 1);
3402                 ServerEnvironment *env = o->m_env;
3403                 if(env == NULL) return 0;
3404                 INodeDefManager *ndef = env->getGameDef()->ndef();
3405                 // parameters
3406                 v3s16 pos = read_v3s16(L, 2);
3407                 MapNode n = readnode(L, 3, ndef);
3408                 // Do it
3409                 MapNode n_old = env->getMap().getNodeNoEx(pos);
3410                 // Call destructor
3411                 if(ndef->get(n_old).has_on_destruct)
3412                         scriptapi_node_on_destruct(L, pos, n_old);
3413                 // Replace node
3414                 bool succeeded = env->getMap().addNodeWithEvent(pos, n);
3415                 if(succeeded){
3416                         // Call post-destructor
3417                         if(ndef->get(n_old).has_after_destruct)
3418                                 scriptapi_node_after_destruct(L, pos, n_old);
3419                         // Call constructor
3420                         if(ndef->get(n).has_on_construct)
3421                                 scriptapi_node_on_construct(L, pos, n);
3422                 }
3423                 lua_pushboolean(L, succeeded);
3424                 return 1;
3425         }
3426
3427         static int l_add_node(lua_State *L)
3428         {
3429                 return l_set_node(L);
3430         }
3431
3432         // EnvRef:remove_node(pos)
3433         // pos = {x=num, y=num, z=num}
3434         static int l_remove_node(lua_State *L)
3435         {
3436                 EnvRef *o = checkobject(L, 1);
3437                 ServerEnvironment *env = o->m_env;
3438                 if(env == NULL) return 0;
3439                 INodeDefManager *ndef = env->getGameDef()->ndef();
3440                 // parameters
3441                 v3s16 pos = read_v3s16(L, 2);
3442                 // Do it
3443                 MapNode n_old = env->getMap().getNodeNoEx(pos);
3444                 // Call destructor
3445                 if(ndef->get(n_old).has_on_destruct)
3446                         scriptapi_node_on_destruct(L, pos, n_old);
3447                 // Replace with air
3448                 // This is slightly optimized compared to addNodeWithEvent(air)
3449                 bool succeeded = env->getMap().removeNodeWithEvent(pos);
3450                 if(succeeded){
3451                         // Call post-destructor
3452                         if(ndef->get(n_old).has_after_destruct)
3453                                 scriptapi_node_after_destruct(L, pos, n_old);
3454                 }
3455                 lua_pushboolean(L, succeeded);
3456                 // Air doesn't require constructor
3457                 return 1;
3458         }
3459
3460         // EnvRef:get_node(pos)
3461         // pos = {x=num, y=num, z=num}
3462         static int l_get_node(lua_State *L)
3463         {
3464                 EnvRef *o = checkobject(L, 1);
3465                 ServerEnvironment *env = o->m_env;
3466                 if(env == NULL) return 0;
3467                 // pos
3468                 v3s16 pos = read_v3s16(L, 2);
3469                 // Do it
3470                 MapNode n = env->getMap().getNodeNoEx(pos);
3471                 // Return node
3472                 pushnode(L, n, env->getGameDef()->ndef());
3473                 return 1;
3474         }
3475
3476         // EnvRef:get_node_or_nil(pos)
3477         // pos = {x=num, y=num, z=num}
3478         static int l_get_node_or_nil(lua_State *L)
3479         {
3480                 EnvRef *o = checkobject(L, 1);
3481                 ServerEnvironment *env = o->m_env;
3482                 if(env == NULL) return 0;
3483                 // pos
3484                 v3s16 pos = read_v3s16(L, 2);
3485                 // Do it
3486                 try{
3487                         MapNode n = env->getMap().getNode(pos);
3488                         // Return node
3489                         pushnode(L, n, env->getGameDef()->ndef());
3490                         return 1;
3491                 } catch(InvalidPositionException &e)
3492                 {
3493                         lua_pushnil(L);
3494                         return 1;
3495                 }
3496         }
3497
3498         // EnvRef:get_node_light(pos, timeofday)
3499         // pos = {x=num, y=num, z=num}
3500         // timeofday: nil = current time, 0 = night, 0.5 = day
3501         static int l_get_node_light(lua_State *L)
3502         {
3503                 EnvRef *o = checkobject(L, 1);
3504                 ServerEnvironment *env = o->m_env;
3505                 if(env == NULL) return 0;
3506                 // Do it
3507                 v3s16 pos = read_v3s16(L, 2);
3508                 u32 time_of_day = env->getTimeOfDay();
3509                 if(lua_isnumber(L, 3))
3510                         time_of_day = 24000.0 * lua_tonumber(L, 3);
3511                 time_of_day %= 24000;
3512                 u32 dnr = time_to_daynight_ratio(time_of_day);
3513                 MapNode n = env->getMap().getNodeNoEx(pos);
3514                 try{
3515                         MapNode n = env->getMap().getNode(pos);
3516                         INodeDefManager *ndef = env->getGameDef()->ndef();
3517                         lua_pushinteger(L, n.getLightBlend(dnr, ndef));
3518                         return 1;
3519                 } catch(InvalidPositionException &e)
3520                 {
3521                         lua_pushnil(L);
3522                         return 1;
3523                 }
3524         }
3525
3526         // EnvRef:place_node(pos, node)
3527         // pos = {x=num, y=num, z=num}
3528         static int l_place_node(lua_State *L)
3529         {
3530                 EnvRef *o = checkobject(L, 1);
3531                 ServerEnvironment *env = o->m_env;
3532                 if(env == NULL) return 0;
3533                 v3s16 pos = read_v3s16(L, 2);
3534                 MapNode n = readnode(L, 3, env->getGameDef()->ndef());
3535
3536                 // Don't attempt to load non-loaded area as of now
3537                 MapNode n_old = env->getMap().getNodeNoEx(pos);
3538                 if(n_old.getContent() == CONTENT_IGNORE){
3539                         lua_pushboolean(L, false);
3540                         return 1;
3541                 }
3542                 // Create item to place
3543                 INodeDefManager *ndef = get_server(L)->ndef();
3544                 IItemDefManager *idef = get_server(L)->idef();
3545                 ItemStack item(ndef->get(n).name, 1, 0, "", idef);
3546                 // Make pointed position
3547                 PointedThing pointed;
3548                 pointed.type = POINTEDTHING_NODE;
3549                 pointed.node_abovesurface = pos;
3550                 pointed.node_undersurface = pos + v3s16(0,-1,0);
3551                 // Place it with a NULL placer (appears in Lua as a non-functional
3552                 // ObjectRef)
3553                 bool success = scriptapi_item_on_place(L, item, NULL, pointed);
3554                 lua_pushboolean(L, success);
3555                 return 1;
3556         }
3557
3558         // EnvRef:dig_node(pos)
3559         // pos = {x=num, y=num, z=num}
3560         static int l_dig_node(lua_State *L)
3561         {
3562                 EnvRef *o = checkobject(L, 1);
3563                 ServerEnvironment *env = o->m_env;
3564                 if(env == NULL) return 0;
3565                 v3s16 pos = read_v3s16(L, 2);
3566
3567                 // Don't attempt to load non-loaded area as of now
3568                 MapNode n = env->getMap().getNodeNoEx(pos);
3569                 if(n.getContent() == CONTENT_IGNORE){
3570                         lua_pushboolean(L, false);
3571                         return 1;
3572                 }
3573                 // Dig it out with a NULL digger (appears in Lua as a
3574                 // non-functional ObjectRef)
3575                 bool success = scriptapi_node_on_dig(L, pos, n, NULL);
3576                 lua_pushboolean(L, success);
3577                 return 1;
3578         }
3579
3580         // EnvRef:punch_node(pos)
3581         // pos = {x=num, y=num, z=num}
3582         static int l_punch_node(lua_State *L)
3583         {
3584                 EnvRef *o = checkobject(L, 1);
3585                 ServerEnvironment *env = o->m_env;
3586                 if(env == NULL) return 0;
3587                 v3s16 pos = read_v3s16(L, 2);
3588
3589                 // Don't attempt to load non-loaded area as of now
3590                 MapNode n = env->getMap().getNodeNoEx(pos);
3591                 if(n.getContent() == CONTENT_IGNORE){
3592                         lua_pushboolean(L, false);
3593                         return 1;
3594                 }
3595                 // Punch it with a NULL puncher (appears in Lua as a non-functional
3596                 // ObjectRef)
3597                 bool success = scriptapi_node_on_punch(L, pos, n, NULL);
3598                 lua_pushboolean(L, success);
3599                 return 1;
3600         }
3601
3602         // EnvRef:get_meta(pos)
3603         static int l_get_meta(lua_State *L)
3604         {
3605                 //infostream<<"EnvRef::l_get_meta()"<<std::endl;
3606                 EnvRef *o = checkobject(L, 1);
3607                 ServerEnvironment *env = o->m_env;
3608                 if(env == NULL) return 0;
3609                 // Do it
3610                 v3s16 p = read_v3s16(L, 2);
3611                 NodeMetaRef::create(L, p, env);
3612                 return 1;
3613         }
3614
3615         // EnvRef:get_node_timer(pos)
3616         static int l_get_node_timer(lua_State *L)
3617         {
3618                 EnvRef *o = checkobject(L, 1);
3619                 ServerEnvironment *env = o->m_env;
3620                 if(env == NULL) return 0;
3621                 // Do it
3622                 v3s16 p = read_v3s16(L, 2);
3623                 NodeTimerRef::create(L, p, env);
3624                 return 1;
3625         }
3626
3627         // EnvRef:add_entity(pos, entityname) -> ObjectRef or nil
3628         // pos = {x=num, y=num, z=num}
3629         static int l_add_entity(lua_State *L)
3630         {
3631                 //infostream<<"EnvRef::l_add_entity()"<<std::endl;
3632                 EnvRef *o = checkobject(L, 1);
3633                 ServerEnvironment *env = o->m_env;
3634                 if(env == NULL) return 0;
3635                 // pos
3636                 v3f pos = checkFloatPos(L, 2);
3637                 // content
3638                 const char *name = luaL_checkstring(L, 3);
3639                 // Do it
3640                 ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, "");
3641                 int objectid = env->addActiveObject(obj);
3642                 // If failed to add, return nothing (reads as nil)
3643                 if(objectid == 0)
3644                         return 0;
3645                 // Return ObjectRef
3646                 objectref_get_or_create(L, obj);
3647                 return 1;
3648         }
3649
3650         // EnvRef:add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
3651         // pos = {x=num, y=num, z=num}
3652         static int l_add_item(lua_State *L)
3653         {
3654                 //infostream<<"EnvRef::l_add_item()"<<std::endl;
3655                 EnvRef *o = checkobject(L, 1);
3656                 ServerEnvironment *env = o->m_env;
3657                 if(env == NULL) return 0;
3658                 // pos
3659                 v3f pos = checkFloatPos(L, 2);
3660                 // item
3661                 ItemStack item = read_item(L, 3);
3662                 if(item.empty() || !item.isKnown(get_server(L)->idef()))
3663                         return 0;
3664                 // Use minetest.spawn_item to spawn a __builtin:item
3665                 lua_getglobal(L, "minetest");
3666                 lua_getfield(L, -1, "spawn_item");
3667                 if(lua_isnil(L, -1))
3668                         return 0;
3669                 lua_pushvalue(L, 2);
3670                 lua_pushstring(L, item.getItemString().c_str());
3671                 if(lua_pcall(L, 2, 1, 0))
3672                         script_error(L, "error: %s", lua_tostring(L, -1));
3673                 return 1;
3674                 /*lua_pushvalue(L, 1);
3675                 lua_pushstring(L, "__builtin:item");
3676                 lua_pushstring(L, item.getItemString().c_str());
3677                 return l_add_entity(L);*/
3678                 /*// Do it
3679                 ServerActiveObject *obj = createItemSAO(env, pos, item.getItemString());
3680                 int objectid = env->addActiveObject(obj);
3681                 // If failed to add, return nothing (reads as nil)
3682                 if(objectid == 0)
3683                         return 0;
3684                 // Return ObjectRef
3685                 objectref_get_or_create(L, obj);
3686                 return 1;*/
3687         }
3688
3689         // EnvRef:add_rat(pos)
3690         // pos = {x=num, y=num, z=num}
3691         static int l_add_rat(lua_State *L)
3692         {
3693                 infostream<<"EnvRef::l_add_rat(): C++ mobs have been removed."
3694                                 <<" Doing nothing."<<std::endl;
3695                 return 0;
3696         }
3697
3698         // EnvRef:add_firefly(pos)
3699         // pos = {x=num, y=num, z=num}
3700         static int l_add_firefly(lua_State *L)
3701         {
3702                 infostream<<"EnvRef::l_add_firefly(): C++ mobs have been removed."
3703                                 <<" Doing nothing."<<std::endl;
3704                 return 0;
3705         }
3706
3707         // EnvRef:get_player_by_name(name)
3708         static int l_get_player_by_name(lua_State *L)
3709         {
3710                 EnvRef *o = checkobject(L, 1);
3711                 ServerEnvironment *env = o->m_env;
3712                 if(env == NULL) return 0;
3713                 // Do it
3714                 const char *name = luaL_checkstring(L, 2);
3715                 Player *player = env->getPlayer(name);
3716                 if(player == NULL){
3717                         lua_pushnil(L);
3718                         return 1;
3719                 }
3720                 PlayerSAO *sao = player->getPlayerSAO();
3721                 if(sao == NULL){
3722                         lua_pushnil(L);
3723                         return 1;
3724                 }
3725                 // Put player on stack
3726                 objectref_get_or_create(L, sao);
3727                 return 1;
3728         }
3729
3730         // EnvRef:get_objects_inside_radius(pos, radius)
3731         static int l_get_objects_inside_radius(lua_State *L)
3732         {
3733                 // Get the table insert function
3734                 lua_getglobal(L, "table");
3735                 lua_getfield(L, -1, "insert");
3736                 int table_insert = lua_gettop(L);
3737                 // Get environemnt
3738                 EnvRef *o = checkobject(L, 1);
3739                 ServerEnvironment *env = o->m_env;
3740                 if(env == NULL) return 0;
3741                 // Do it
3742                 v3f pos = checkFloatPos(L, 2);
3743                 float radius = luaL_checknumber(L, 3) * BS;
3744                 std::set<u16> ids = env->getObjectsInsideRadius(pos, radius);
3745                 lua_newtable(L);
3746                 int table = lua_gettop(L);
3747                 for(std::set<u16>::const_iterator
3748                                 i = ids.begin(); i != ids.end(); i++){
3749                         ServerActiveObject *obj = env->getActiveObject(*i);
3750                         // Insert object reference into table
3751                         lua_pushvalue(L, table_insert);
3752                         lua_pushvalue(L, table);
3753                         objectref_get_or_create(L, obj);
3754                         if(lua_pcall(L, 2, 0, 0))
3755                                 script_error(L, "error: %s", lua_tostring(L, -1));
3756                 }
3757                 return 1;
3758         }
3759
3760         // EnvRef:set_timeofday(val)
3761         // val = 0...1
3762         static int l_set_timeofday(lua_State *L)
3763         {
3764                 EnvRef *o = checkobject(L, 1);
3765                 ServerEnvironment *env = o->m_env;
3766                 if(env == NULL) return 0;
3767                 // Do it
3768                 float timeofday_f = luaL_checknumber(L, 2);
3769                 assert(timeofday_f >= 0.0 && timeofday_f <= 1.0);
3770                 int timeofday_mh = (int)(timeofday_f * 24000.0);
3771                 // This should be set directly in the environment but currently
3772                 // such changes aren't immediately sent to the clients, so call
3773                 // the server instead.
3774                 //env->setTimeOfDay(timeofday_mh);
3775                 get_server(L)->setTimeOfDay(timeofday_mh);
3776                 return 0;
3777         }
3778
3779         // EnvRef:get_timeofday() -> 0...1
3780         static int l_get_timeofday(lua_State *L)
3781         {
3782                 EnvRef *o = checkobject(L, 1);
3783                 ServerEnvironment *env = o->m_env;
3784                 if(env == NULL) return 0;
3785                 // Do it
3786                 int timeofday_mh = env->getTimeOfDay();
3787                 float timeofday_f = (float)timeofday_mh / 24000.0;
3788                 lua_pushnumber(L, timeofday_f);
3789                 return 1;
3790         }
3791
3792
3793         // EnvRef:find_node_near(pos, radius, nodenames) -> pos or nil
3794         // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
3795         static int l_find_node_near(lua_State *L)
3796         {
3797                 EnvRef *o = checkobject(L, 1);
3798                 ServerEnvironment *env = o->m_env;
3799                 if(env == NULL) return 0;
3800                 INodeDefManager *ndef = get_server(L)->ndef();
3801                 v3s16 pos = read_v3s16(L, 2);
3802                 int radius = luaL_checkinteger(L, 3);
3803                 std::set<content_t> filter;
3804                 if(lua_istable(L, 4)){
3805                         int table = 4;
3806                         lua_pushnil(L);
3807                         while(lua_next(L, table) != 0){
3808                                 // key at index -2 and value at index -1
3809                                 luaL_checktype(L, -1, LUA_TSTRING);
3810                                 ndef->getIds(lua_tostring(L, -1), filter);
3811                                 // removes value, keeps key for next iteration
3812                                 lua_pop(L, 1);
3813                         }
3814                 } else if(lua_isstring(L, 4)){
3815                         ndef->getIds(lua_tostring(L, 4), filter);
3816                 }
3817
3818                 for(int d=1; d<=radius; d++){
3819                         core::list<v3s16> list;
3820                         getFacePositions(list, d);
3821                         for(core::list<v3s16>::Iterator i = list.begin();
3822                                         i != list.end(); i++){
3823                                 v3s16 p = pos + (*i);
3824                                 content_t c = env->getMap().getNodeNoEx(p).getContent();
3825                                 if(filter.count(c) != 0){
3826                                         push_v3s16(L, p);
3827                                         return 1;
3828                                 }
3829                         }
3830                 }
3831                 return 0;
3832         }
3833
3834         // EnvRef:find_nodes_in_area(minp, maxp, nodenames) -> list of positions
3835         // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
3836         static int l_find_nodes_in_area(lua_State *L)
3837         {
3838                 EnvRef *o = checkobject(L, 1);
3839                 ServerEnvironment *env = o->m_env;
3840                 if(env == NULL) return 0;
3841                 INodeDefManager *ndef = get_server(L)->ndef();
3842                 v3s16 minp = read_v3s16(L, 2);
3843                 v3s16 maxp = read_v3s16(L, 3);
3844                 std::set<content_t> filter;
3845                 if(lua_istable(L, 4)){
3846                         int table = 4;
3847                         lua_pushnil(L);
3848                         while(lua_next(L, table) != 0){
3849                                 // key at index -2 and value at index -1
3850                                 luaL_checktype(L, -1, LUA_TSTRING);
3851                                 ndef->getIds(lua_tostring(L, -1), filter);
3852                                 // removes value, keeps key for next iteration
3853                                 lua_pop(L, 1);
3854                         }
3855                 } else if(lua_isstring(L, 4)){
3856                         ndef->getIds(lua_tostring(L, 4), filter);
3857                 }
3858
3859                 // Get the table insert function
3860                 lua_getglobal(L, "table");
3861                 lua_getfield(L, -1, "insert");
3862                 int table_insert = lua_gettop(L);
3863                 
3864                 lua_newtable(L);
3865                 int table = lua_gettop(L);
3866                 for(s16 x=minp.X; x<=maxp.X; x++)
3867                 for(s16 y=minp.Y; y<=maxp.Y; y++)
3868                 for(s16 z=minp.Z; z<=maxp.Z; z++)
3869                 {
3870                         v3s16 p(x,y,z);
3871                         content_t c = env->getMap().getNodeNoEx(p).getContent();
3872                         if(filter.count(c) != 0){
3873                                 lua_pushvalue(L, table_insert);
3874                                 lua_pushvalue(L, table);
3875                                 push_v3s16(L, p);
3876                                 if(lua_pcall(L, 2, 0, 0))
3877                                         script_error(L, "error: %s", lua_tostring(L, -1));
3878                         }
3879                 }
3880                 return 1;
3881         }
3882
3883         //      EnvRef:get_perlin(seeddiff, octaves, persistence, scale)
3884         //  returns world-specific PerlinNoise
3885         static int l_get_perlin(lua_State *L)
3886         {
3887                 EnvRef *o = checkobject(L, 1);
3888                 ServerEnvironment *env = o->m_env;
3889                 if(env == NULL) return 0;
3890
3891                 int seeddiff = luaL_checkint(L, 2);
3892                 int octaves = luaL_checkint(L, 3);
3893                 double persistence = luaL_checknumber(L, 4);
3894                 double scale = luaL_checknumber(L, 5);
3895
3896                 LuaPerlinNoise *n = new LuaPerlinNoise(seeddiff + int(env->getServerMap().getSeed()), octaves, persistence, scale);
3897                 *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
3898                 luaL_getmetatable(L, "PerlinNoise");
3899                 lua_setmetatable(L, -2);
3900                 return 1;
3901         }
3902
3903         // EnvRef:clear_objects()
3904         // clear all objects in the environment
3905         static int l_clear_objects(lua_State *L)
3906         {
3907                 EnvRef *o = checkobject(L, 1);
3908                 o->m_env->clearAllObjects();
3909                 return 0;
3910         }
3911
3912 public:
3913         EnvRef(ServerEnvironment *env):
3914                 m_env(env)
3915         {
3916                 //infostream<<"EnvRef created"<<std::endl;
3917         }
3918
3919         ~EnvRef()
3920         {
3921                 //infostream<<"EnvRef destructing"<<std::endl;
3922         }
3923
3924         // Creates an EnvRef and leaves it on top of stack
3925         // Not callable from Lua; all references are created on the C side.
3926         static void create(lua_State *L, ServerEnvironment *env)
3927         {
3928                 EnvRef *o = new EnvRef(env);
3929                 //infostream<<"EnvRef::create: o="<<o<<std::endl;
3930                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
3931                 luaL_getmetatable(L, className);
3932                 lua_setmetatable(L, -2);
3933         }
3934
3935         static void set_null(lua_State *L)
3936         {
3937                 EnvRef *o = checkobject(L, -1);
3938                 o->m_env = NULL;
3939         }
3940         
3941         static void Register(lua_State *L)
3942         {
3943                 lua_newtable(L);
3944                 int methodtable = lua_gettop(L);
3945                 luaL_newmetatable(L, className);
3946                 int metatable = lua_gettop(L);
3947
3948                 lua_pushliteral(L, "__metatable");
3949                 lua_pushvalue(L, methodtable);
3950                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
3951
3952                 lua_pushliteral(L, "__index");
3953                 lua_pushvalue(L, methodtable);
3954                 lua_settable(L, metatable);
3955
3956                 lua_pushliteral(L, "__gc");
3957                 lua_pushcfunction(L, gc_object);
3958                 lua_settable(L, metatable);
3959
3960                 lua_pop(L, 1);  // drop metatable
3961
3962                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
3963                 lua_pop(L, 1);  // drop methodtable
3964
3965                 // Cannot be created from Lua
3966                 //lua_register(L, className, create_object);
3967         }
3968 };
3969 const char EnvRef::className[] = "EnvRef";
3970 const luaL_reg EnvRef::methods[] = {
3971         method(EnvRef, set_node),
3972         method(EnvRef, add_node),
3973         method(EnvRef, remove_node),
3974         method(EnvRef, get_node),
3975         method(EnvRef, get_node_or_nil),
3976         method(EnvRef, get_node_light),
3977         method(EnvRef, place_node),
3978         method(EnvRef, dig_node),
3979         method(EnvRef, punch_node),
3980         method(EnvRef, add_entity),
3981         method(EnvRef, add_item),
3982         method(EnvRef, add_rat),
3983         method(EnvRef, add_firefly),
3984         method(EnvRef, get_meta),
3985         method(EnvRef, get_node_timer),
3986         method(EnvRef, get_player_by_name),
3987         method(EnvRef, get_objects_inside_radius),
3988         method(EnvRef, set_timeofday),
3989         method(EnvRef, get_timeofday),
3990         method(EnvRef, find_node_near),
3991         method(EnvRef, find_nodes_in_area),
3992         method(EnvRef, get_perlin),
3993         method(EnvRef, clear_objects),
3994         {0,0}
3995 };
3996
3997 /*
3998         LuaPseudoRandom
3999 */
4000
4001
4002 class LuaPseudoRandom
4003 {
4004 private:
4005         PseudoRandom m_pseudo;
4006
4007         static const char className[];
4008         static const luaL_reg methods[];
4009
4010         // Exported functions
4011         
4012         // garbage collector
4013         static int gc_object(lua_State *L)
4014         {
4015                 LuaPseudoRandom *o = *(LuaPseudoRandom **)(lua_touserdata(L, 1));
4016                 delete o;
4017                 return 0;
4018         }
4019
4020         // next(self, min=0, max=32767) -> get next value
4021         static int l_next(lua_State *L)
4022         {
4023                 LuaPseudoRandom *o = checkobject(L, 1);
4024                 int min = 0;
4025                 int max = 32767;
4026                 lua_settop(L, 3); // Fill 2 and 3 with nil if they don't exist
4027                 if(!lua_isnil(L, 2))
4028                         min = luaL_checkinteger(L, 2);
4029                 if(!lua_isnil(L, 3))
4030                         max = luaL_checkinteger(L, 3);
4031                 if(max < min){
4032                         errorstream<<"PseudoRandom.next(): max="<<max<<" min="<<min<<std::endl;
4033                         throw LuaError(L, "PseudoRandom.next(): max < min");
4034                 }
4035                 if(max - min != 32767 && max - min > 32767/5)
4036                         throw LuaError(L, "PseudoRandom.next() max-min is not 32767 and is > 32768/5. This is disallowed due to the bad random distribution the implementation would otherwise make.");
4037                 PseudoRandom &pseudo = o->m_pseudo;
4038                 int val = pseudo.next();
4039                 val = (val % (max-min+1)) + min;
4040                 lua_pushinteger(L, val);
4041                 return 1;
4042         }
4043
4044 public:
4045         LuaPseudoRandom(int seed):
4046                 m_pseudo(seed)
4047         {
4048         }
4049
4050         ~LuaPseudoRandom()
4051         {
4052         }
4053
4054         const PseudoRandom& getItem() const
4055         {
4056                 return m_pseudo;
4057         }
4058         PseudoRandom& getItem()
4059         {
4060                 return m_pseudo;
4061         }
4062         
4063         // LuaPseudoRandom(seed)
4064         // Creates an LuaPseudoRandom and leaves it on top of stack
4065         static int create_object(lua_State *L)
4066         {
4067                 int seed = luaL_checknumber(L, 1);
4068                 LuaPseudoRandom *o = new LuaPseudoRandom(seed);
4069                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
4070                 luaL_getmetatable(L, className);
4071                 lua_setmetatable(L, -2);
4072                 return 1;
4073         }
4074
4075         static LuaPseudoRandom* checkobject(lua_State *L, int narg)
4076         {
4077                 luaL_checktype(L, narg, LUA_TUSERDATA);
4078                 void *ud = luaL_checkudata(L, narg, className);
4079                 if(!ud) luaL_typerror(L, narg, className);
4080                 return *(LuaPseudoRandom**)ud;  // unbox pointer
4081         }
4082
4083         static void Register(lua_State *L)
4084         {
4085                 lua_newtable(L);
4086                 int methodtable = lua_gettop(L);
4087                 luaL_newmetatable(L, className);
4088                 int metatable = lua_gettop(L);
4089
4090                 lua_pushliteral(L, "__metatable");
4091                 lua_pushvalue(L, methodtable);
4092                 lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
4093
4094                 lua_pushliteral(L, "__index");
4095                 lua_pushvalue(L, methodtable);
4096                 lua_settable(L, metatable);
4097
4098                 lua_pushliteral(L, "__gc");
4099                 lua_pushcfunction(L, gc_object);
4100                 lua_settable(L, metatable);
4101
4102                 lua_pop(L, 1);  // drop metatable
4103
4104                 luaL_openlib(L, 0, methods, 0);  // fill methodtable
4105                 lua_pop(L, 1);  // drop methodtable
4106
4107                 // Can be created from Lua (LuaPseudoRandom(seed))
4108                 lua_register(L, className, create_object);
4109         }
4110 };
4111 const char LuaPseudoRandom::className[] = "PseudoRandom";
4112 const luaL_reg LuaPseudoRandom::methods[] = {
4113         method(LuaPseudoRandom, next),
4114         {0,0}
4115 };
4116
4117
4118
4119 /*
4120         LuaABM
4121 */
4122
4123 class LuaABM : public ActiveBlockModifier
4124 {
4125 private:
4126         lua_State *m_lua;
4127         int m_id;
4128
4129         std::set<std::string> m_trigger_contents;
4130         std::set<std::string> m_required_neighbors;
4131         float m_trigger_interval;
4132         u32 m_trigger_chance;
4133 public:
4134         LuaABM(lua_State *L, int id,
4135                         const std::set<std::string> &trigger_contents,
4136                         const std::set<std::string> &required_neighbors,
4137                         float trigger_interval, u32 trigger_chance):
4138                 m_lua(L),
4139                 m_id(id),
4140                 m_trigger_contents(trigger_contents),
4141                 m_required_neighbors(required_neighbors),
4142                 m_trigger_interval(trigger_interval),
4143                 m_trigger_chance(trigger_chance)
4144         {
4145         }
4146         virtual std::set<std::string> getTriggerContents()
4147         {
4148                 return m_trigger_contents;
4149         }
4150         virtual std::set<std::string> getRequiredNeighbors()
4151         {
4152                 return m_required_neighbors;
4153         }
4154         virtual float getTriggerInterval()
4155         {
4156                 return m_trigger_interval;
4157         }
4158         virtual u32 getTriggerChance()
4159         {
4160                 return m_trigger_chance;
4161         }
4162         virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
4163                         u32 active_object_count, u32 active_object_count_wider)
4164         {
4165                 lua_State *L = m_lua;
4166         
4167                 realitycheck(L);
4168                 assert(lua_checkstack(L, 20));
4169                 StackUnroller stack_unroller(L);
4170
4171                 // Get minetest.registered_abms
4172                 lua_getglobal(L, "minetest");
4173                 lua_getfield(L, -1, "registered_abms");
4174                 luaL_checktype(L, -1, LUA_TTABLE);
4175                 int registered_abms = lua_gettop(L);
4176
4177                 // Get minetest.registered_abms[m_id]
4178                 lua_pushnumber(L, m_id);
4179                 lua_gettable(L, registered_abms);
4180                 if(lua_isnil(L, -1))
4181                         assert(0);
4182                 
4183                 // Call action
4184                 luaL_checktype(L, -1, LUA_TTABLE);
4185                 lua_getfield(L, -1, "action");
4186                 luaL_checktype(L, -1, LUA_TFUNCTION);
4187                 push_v3s16(L, p);
4188                 pushnode(L, n, env->getGameDef()->ndef());
4189                 lua_pushnumber(L, active_object_count);
4190                 lua_pushnumber(L, active_object_count_wider);
4191                 if(lua_pcall(L, 4, 0, 0))
4192                         script_error(L, "error: %s", lua_tostring(L, -1));
4193         }
4194 };
4195
4196 /*
4197         ServerSoundParams
4198 */
4199
4200 static void read_server_sound_params(lua_State *L, int index,
4201                 ServerSoundParams &params)
4202 {
4203         if(index < 0)
4204                 index = lua_gettop(L) + 1 + index;
4205         // Clear
4206         params = ServerSoundParams();
4207         if(lua_istable(L, index)){
4208                 getfloatfield(L, index, "gain", params.gain);
4209                 getstringfield(L, index, "to_player", params.to_player);
4210                 lua_getfield(L, index, "pos");
4211                 if(!lua_isnil(L, -1)){
4212                         v3f p = read_v3f(L, -1)*BS;
4213                         params.pos = p;
4214                         params.type = ServerSoundParams::SSP_POSITIONAL;
4215                 }
4216                 lua_pop(L, 1);
4217                 lua_getfield(L, index, "object");
4218                 if(!lua_isnil(L, -1)){
4219                         ObjectRef *ref = ObjectRef::checkobject(L, -1);
4220                         ServerActiveObject *sao = ObjectRef::getobject(ref);
4221                         if(sao){
4222                                 params.object = sao->getId();
4223                                 params.type = ServerSoundParams::SSP_OBJECT;
4224                         }
4225                 }
4226                 lua_pop(L, 1);
4227                 params.max_hear_distance = BS*getfloatfield_default(L, index,
4228                                 "max_hear_distance", params.max_hear_distance/BS);
4229                 getboolfield(L, index, "loop", params.loop);
4230         }
4231 }
4232
4233 /*
4234         Global functions
4235 */
4236
4237 // debug(text)
4238 // Writes a line to dstream
4239 static int l_debug(lua_State *L)
4240 {
4241         std::string text = lua_tostring(L, 1);
4242         dstream << text << std::endl;
4243         return 0;
4244 }
4245
4246 // log([level,] text)
4247 // Writes a line to the logger.
4248 // The one-argument version logs to infostream.
4249 // The two-argument version accept a log level: error, action, info, or verbose.
4250 static int l_log(lua_State *L)
4251 {
4252         std::string text;
4253         LogMessageLevel level = LMT_INFO;
4254         if(lua_isnone(L, 2))
4255         {
4256                 text = lua_tostring(L, 1);
4257         }
4258         else
4259         {
4260                 std::string levelname = lua_tostring(L, 1);
4261                 text = lua_tostring(L, 2);
4262                 if(levelname == "error")
4263                         level = LMT_ERROR;
4264                 else if(levelname == "action")
4265                         level = LMT_ACTION;
4266                 else if(levelname == "verbose")
4267                         level = LMT_VERBOSE;
4268         }
4269         log_printline(level, text);
4270         return 0;
4271 }
4272
4273 // request_shutdown()
4274 static int l_request_shutdown(lua_State *L)
4275 {
4276         get_server(L)->requestShutdown();
4277         return 0;
4278 }
4279
4280 // get_server_status()
4281 static int l_get_server_status(lua_State *L)
4282 {
4283         lua_pushstring(L, wide_to_narrow(get_server(L)->getStatusString()).c_str());
4284         return 1;
4285 }
4286
4287 // register_item_raw({lots of stuff})
4288 static int l_register_item_raw(lua_State *L)
4289 {
4290         luaL_checktype(L, 1, LUA_TTABLE);
4291         int table = 1;
4292
4293         // Get the writable item and node definition managers from the server
4294         IWritableItemDefManager *idef =
4295                         get_server(L)->getWritableItemDefManager();
4296         IWritableNodeDefManager *ndef =
4297                         get_server(L)->getWritableNodeDefManager();
4298
4299         // Check if name is defined
4300         std::string name;
4301         lua_getfield(L, table, "name");
4302         if(lua_isstring(L, -1)){
4303                 name = lua_tostring(L, -1);
4304                 verbosestream<<"register_item_raw: "<<name<<std::endl;
4305         } else {
4306                 throw LuaError(L, "register_item_raw: name is not defined or not a string");
4307         }
4308
4309         // Check if on_use is defined
4310
4311         ItemDefinition def;
4312         // Set a distinctive default value to check if this is set
4313         def.node_placement_prediction = "__default";
4314
4315         // Read the item definition
4316         def = read_item_definition(L, table, def);
4317
4318         // Default to having client-side placement prediction for nodes
4319         // ("" in item definition sets it off)
4320         if(def.node_placement_prediction == "__default"){
4321                 if(def.type == ITEM_NODE)
4322                         def.node_placement_prediction = name;
4323                 else
4324                         def.node_placement_prediction = "";
4325         }
4326         
4327         // Register item definition
4328         idef->registerItem(def);
4329
4330         // Read the node definition (content features) and register it
4331         if(def.type == ITEM_NODE)
4332         {
4333                 ContentFeatures f = read_content_features(L, table);
4334                 ndef->set(f.name, f);
4335         }
4336
4337         return 0; /* number of results */
4338 }
4339
4340 // register_alias_raw(name, convert_to_name)
4341 static int l_register_alias_raw(lua_State *L)
4342 {
4343         std::string name = luaL_checkstring(L, 1);
4344         std::string convert_to = luaL_checkstring(L, 2);
4345
4346         // Get the writable item definition manager from the server
4347         IWritableItemDefManager *idef =
4348                         get_server(L)->getWritableItemDefManager();
4349         
4350         idef->registerAlias(name, convert_to);
4351         
4352         return 0; /* number of results */
4353 }
4354
4355 // helper for register_craft
4356 static bool read_craft_recipe_shaped(lua_State *L, int index,
4357                 int &width, std::vector<std::string> &recipe)
4358 {
4359         if(index < 0)
4360                 index = lua_gettop(L) + 1 + index;
4361
4362         if(!lua_istable(L, index))
4363                 return false;
4364
4365         lua_pushnil(L);
4366         int rowcount = 0;
4367         while(lua_next(L, index) != 0){
4368                 int colcount = 0;
4369                 // key at index -2 and value at index -1
4370                 if(!lua_istable(L, -1))
4371                         return false;
4372                 int table2 = lua_gettop(L);
4373                 lua_pushnil(L);
4374                 while(lua_next(L, table2) != 0){
4375                         // key at index -2 and value at index -1
4376                         if(!lua_isstring(L, -1))
4377                                 return false;
4378                         recipe.push_back(lua_tostring(L, -1));
4379                         // removes value, keeps key for next iteration
4380                         lua_pop(L, 1);
4381                         colcount++;
4382                 }
4383                 if(rowcount == 0){
4384                         width = colcount;
4385                 } else {
4386                         if(colcount != width)
4387                                 return false;
4388                 }
4389                 // removes value, keeps key for next iteration
4390                 lua_pop(L, 1);
4391                 rowcount++;
4392         }
4393         return width != 0;
4394 }
4395
4396 // helper for register_craft
4397 static bool read_craft_recipe_shapeless(lua_State *L, int index,
4398                 std::vector<std::string> &recipe)
4399 {
4400         if(index < 0)
4401                 index = lua_gettop(L) + 1 + index;
4402
4403         if(!lua_istable(L, index))
4404                 return false;
4405
4406         lua_pushnil(L);
4407         while(lua_next(L, index) != 0){
4408                 // key at index -2 and value at index -1
4409                 if(!lua_isstring(L, -1))
4410                         return false;
4411                 recipe.push_back(lua_tostring(L, -1));
4412                 // removes value, keeps key for next iteration
4413                 lua_pop(L, 1);
4414         }
4415         return true;
4416 }
4417
4418 // helper for register_craft
4419 static bool read_craft_replacements(lua_State *L, int index,
4420                 CraftReplacements &replacements)
4421 {
4422         if(index < 0)
4423                 index = lua_gettop(L) + 1 + index;
4424
4425         if(!lua_istable(L, index))
4426                 return false;
4427
4428         lua_pushnil(L);
4429         while(lua_next(L, index) != 0){
4430                 // key at index -2 and value at index -1
4431                 if(!lua_istable(L, -1))
4432                         return false;
4433                 lua_rawgeti(L, -1, 1);
4434                 if(!lua_isstring(L, -1))
4435                         return false;
4436                 std::string replace_from = lua_tostring(L, -1);
4437                 lua_pop(L, 1);
4438                 lua_rawgeti(L, -1, 2);
4439                 if(!lua_isstring(L, -1))
4440                         return false;
4441                 std::string replace_to = lua_tostring(L, -1);
4442                 lua_pop(L, 1);
4443                 replacements.pairs.push_back(
4444                                 std::make_pair(replace_from, replace_to));
4445                 // removes value, keeps key for next iteration
4446                 lua_pop(L, 1);
4447         }
4448         return true;
4449 }
4450 // register_craft({output=item, recipe={{item00,item10},{item01,item11}})
4451 static int l_register_craft(lua_State *L)
4452 {
4453         //infostream<<"register_craft"<<std::endl;
4454         luaL_checktype(L, 1, LUA_TTABLE);
4455         int table = 1;
4456
4457         // Get the writable craft definition manager from the server
4458         IWritableCraftDefManager *craftdef =
4459                         get_server(L)->getWritableCraftDefManager();
4460         
4461         std::string type = getstringfield_default(L, table, "type", "shaped");
4462
4463         /*
4464                 CraftDefinitionShaped
4465         */
4466         if(type == "shaped"){
4467                 std::string output = getstringfield_default(L, table, "output", "");
4468                 if(output == "")
4469                         throw LuaError(L, "Crafting definition is missing an output");
4470
4471                 int width = 0;
4472                 std::vector<std::string> recipe;
4473                 lua_getfield(L, table, "recipe");
4474                 if(lua_isnil(L, -1))
4475                         throw LuaError(L, "Crafting definition is missing a recipe"
4476                                         " (output=\"" + output + "\")");
4477                 if(!read_craft_recipe_shaped(L, -1, width, recipe))
4478                         throw LuaError(L, "Invalid crafting recipe"
4479                                         " (output=\"" + output + "\")");
4480
4481                 CraftReplacements replacements;
4482                 lua_getfield(L, table, "replacements");
4483                 if(!lua_isnil(L, -1))
4484                 {
4485                         if(!read_craft_replacements(L, -1, replacements))
4486                                 throw LuaError(L, "Invalid replacements"
4487                                                 " (output=\"" + output + "\")");
4488                 }
4489
4490                 CraftDefinition *def = new CraftDefinitionShaped(
4491                                 output, width, recipe, replacements);
4492                 craftdef->registerCraft(def);
4493         }
4494         /*
4495                 CraftDefinitionShapeless
4496         */
4497         else if(type == "shapeless"){
4498                 std::string output = getstringfield_default(L, table, "output", "");
4499                 if(output == "")
4500                         throw LuaError(L, "Crafting definition (shapeless)"
4501                                         " is missing an output");
4502
4503                 std::vector<std::string> recipe;
4504                 lua_getfield(L, table, "recipe");
4505                 if(lua_isnil(L, -1))
4506                         throw LuaError(L, "Crafting definition (shapeless)"
4507                                         " is missing a recipe"
4508                                         " (output=\"" + output + "\")");
4509                 if(!read_craft_recipe_shapeless(L, -1, recipe))
4510                         throw LuaError(L, "Invalid crafting recipe"
4511                                         " (output=\"" + output + "\")");
4512
4513                 CraftReplacements replacements;
4514                 lua_getfield(L, table, "replacements");
4515                 if(!lua_isnil(L, -1))
4516                 {
4517                         if(!read_craft_replacements(L, -1, replacements))
4518                                 throw LuaError(L, "Invalid replacements"
4519                                                 " (output=\"" + output + "\")");
4520                 }
4521
4522                 CraftDefinition *def = new CraftDefinitionShapeless(
4523                                 output, recipe, replacements);
4524                 craftdef->registerCraft(def);
4525         }
4526         /*
4527                 CraftDefinitionToolRepair
4528         */
4529         else if(type == "toolrepair"){
4530                 float additional_wear = getfloatfield_default(L, table,
4531                                 "additional_wear", 0.0);
4532
4533                 CraftDefinition *def = new CraftDefinitionToolRepair(
4534                                 additional_wear);
4535                 craftdef->registerCraft(def);
4536         }
4537         /*
4538                 CraftDefinitionCooking
4539         */
4540         else if(type == "cooking"){
4541                 std::string output = getstringfield_default(L, table, "output", "");
4542                 if(output == "")
4543                         throw LuaError(L, "Crafting definition (cooking)"
4544                                         " is missing an output");
4545
4546                 std::string recipe = getstringfield_default(L, table, "recipe", "");
4547                 if(recipe == "")
4548                         throw LuaError(L, "Crafting definition (cooking)"
4549                                         " is missing a recipe"
4550                                         " (output=\"" + output + "\")");
4551
4552                 float cooktime = getfloatfield_default(L, table, "cooktime", 3.0);
4553
4554                 CraftReplacements replacements;
4555                 lua_getfield(L, table, "replacements");
4556                 if(!lua_isnil(L, -1))
4557                 {
4558                         if(!read_craft_replacements(L, -1, replacements))
4559                                 throw LuaError(L, "Invalid replacements"
4560                                                 " (cooking output=\"" + output + "\")");
4561                 }
4562
4563                 CraftDefinition *def = new CraftDefinitionCooking(
4564                                 output, recipe, cooktime, replacements);
4565                 craftdef->registerCraft(def);
4566         }
4567         /*
4568                 CraftDefinitionFuel
4569         */
4570         else if(type == "fuel"){
4571                 std::string recipe = getstringfield_default(L, table, "recipe", "");
4572                 if(recipe == "")
4573                         throw LuaError(L, "Crafting definition (fuel)"
4574                                         " is missing a recipe");
4575
4576                 float burntime = getfloatfield_default(L, table, "burntime", 1.0);
4577
4578                 CraftReplacements replacements;
4579                 lua_getfield(L, table, "replacements");
4580                 if(!lua_isnil(L, -1))
4581                 {
4582                         if(!read_craft_replacements(L, -1, replacements))
4583                                 throw LuaError(L, "Invalid replacements"
4584                                                 " (fuel recipe=\"" + recipe + "\")");
4585                 }
4586
4587                 CraftDefinition *def = new CraftDefinitionFuel(
4588                                 recipe, burntime, replacements);
4589                 craftdef->registerCraft(def);
4590         }
4591         else
4592         {
4593                 throw LuaError(L, "Unknown crafting definition type: \"" + type + "\"");
4594         }
4595
4596         lua_pop(L, 1);
4597         return 0; /* number of results */
4598 }
4599
4600 // setting_set(name, value)
4601 static int l_setting_set(lua_State *L)
4602 {
4603         const char *name = luaL_checkstring(L, 1);
4604         const char *value = luaL_checkstring(L, 2);
4605         g_settings->set(name, value);
4606         return 0;
4607 }
4608
4609 // setting_get(name)
4610 static int l_setting_get(lua_State *L)
4611 {
4612         const char *name = luaL_checkstring(L, 1);
4613         try{
4614                 std::string value = g_settings->get(name);
4615                 lua_pushstring(L, value.c_str());
4616         } catch(SettingNotFoundException &e){
4617                 lua_pushnil(L);
4618         }
4619         return 1;
4620 }
4621
4622 // setting_getbool(name)
4623 static int l_setting_getbool(lua_State *L)
4624 {
4625         const char *name = luaL_checkstring(L, 1);
4626         try{
4627                 bool value = g_settings->getBool(name);
4628                 lua_pushboolean(L, value);
4629         } catch(SettingNotFoundException &e){
4630                 lua_pushnil(L);
4631         }
4632         return 1;
4633 }
4634
4635 // chat_send_all(text)
4636 static int l_chat_send_all(lua_State *L)
4637 {
4638         const char *text = luaL_checkstring(L, 1);
4639         // Get server from registry
4640         Server *server = get_server(L);
4641         // Send
4642         server->notifyPlayers(narrow_to_wide(text));
4643         return 0;
4644 }
4645
4646 // chat_send_player(name, text)
4647 static int l_chat_send_player(lua_State *L)
4648 {
4649         const char *name = luaL_checkstring(L, 1);
4650         const char *text = luaL_checkstring(L, 2);
4651         // Get server from registry
4652         Server *server = get_server(L);
4653         // Send
4654         server->notifyPlayer(name, narrow_to_wide(text));
4655         return 0;
4656 }
4657
4658 // get_player_privs(name, text)
4659 static int l_get_player_privs(lua_State *L)
4660 {
4661         const char *name = luaL_checkstring(L, 1);
4662         // Get server from registry
4663         Server *server = get_server(L);
4664         // Do it
4665         lua_newtable(L);
4666         int table = lua_gettop(L);
4667         std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
4668         for(std::set<std::string>::const_iterator
4669                         i = privs_s.begin(); i != privs_s.end(); i++){
4670                 lua_pushboolean(L, true);
4671                 lua_setfield(L, table, i->c_str());
4672         }
4673         lua_pushvalue(L, table);
4674         return 1;
4675 }
4676
4677 // get_ban_list()
4678 static int l_get_ban_list(lua_State *L)
4679 {
4680         lua_pushstring(L, get_server(L)->getBanDescription("").c_str());
4681         return 1;
4682 }
4683
4684 // get_ban_description()
4685 static int l_get_ban_description(lua_State *L)
4686 {
4687         const char * ip_or_name = luaL_checkstring(L, 1);
4688         lua_pushstring(L, get_server(L)->getBanDescription(std::string(ip_or_name)).c_str());
4689         return 1;
4690 }
4691
4692 // ban_player()
4693 static int l_ban_player(lua_State *L)
4694 {
4695         const char * name = luaL_checkstring(L, 1);
4696         Player *player = get_env(L)->getPlayer(name);
4697         if(player == NULL)
4698         {
4699                 lua_pushboolean(L, false); // no such player
4700                 return 1;
4701         }
4702         try
4703         {
4704                 Address addr = get_server(L)->getPeerAddress(get_env(L)->getPlayer(name)->peer_id);
4705                 std::string ip_str = addr.serializeString();
4706                 get_server(L)->setIpBanned(ip_str, name);
4707         }
4708         catch(con::PeerNotFoundException) // unlikely
4709         {
4710                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
4711                 lua_pushboolean(L, false); // error
4712                 return 1;
4713         }
4714         lua_pushboolean(L, true);
4715         return 1;
4716 }
4717
4718 // unban_player_or_ip()
4719 static int l_unban_player_of_ip(lua_State *L)
4720 {
4721         const char * ip_or_name = luaL_checkstring(L, 1);
4722         get_server(L)->unsetIpBanned(ip_or_name);
4723         lua_pushboolean(L, true);
4724         return 1;
4725 }
4726
4727 // get_inventory(location)
4728 static int l_get_inventory(lua_State *L)
4729 {
4730         InventoryLocation loc;
4731
4732         std::string type = checkstringfield(L, 1, "type");
4733         if(type == "player"){
4734                 std::string name = checkstringfield(L, 1, "name");
4735                 loc.setPlayer(name);
4736         } else if(type == "node"){
4737                 lua_getfield(L, 1, "pos");
4738                 v3s16 pos = check_v3s16(L, -1);
4739                 loc.setNodeMeta(pos);
4740         } else if(type == "detached"){
4741                 std::string name = checkstringfield(L, 1, "name");
4742                 loc.setDetached(name);
4743         }
4744         
4745         if(get_server(L)->getInventory(loc) != NULL)
4746                 InvRef::create(L, loc);
4747         else
4748                 lua_pushnil(L);
4749         return 1;
4750 }
4751
4752 // create_detached_inventory_raw(name)
4753 static int l_create_detached_inventory_raw(lua_State *L)
4754 {
4755         const char *name = luaL_checkstring(L, 1);
4756         if(get_server(L)->createDetachedInventory(name) != NULL){
4757                 InventoryLocation loc;
4758                 loc.setDetached(name);
4759                 InvRef::create(L, loc);
4760         }else{
4761                 lua_pushnil(L);
4762         }
4763         return 1;
4764 }
4765
4766 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
4767 static int l_get_dig_params(lua_State *L)
4768 {
4769         std::map<std::string, int> groups;
4770         read_groups(L, 1, groups);
4771         ToolCapabilities tp = read_tool_capabilities(L, 2);
4772         if(lua_isnoneornil(L, 3))
4773                 push_dig_params(L, getDigParams(groups, &tp));
4774         else
4775                 push_dig_params(L, getDigParams(groups, &tp,
4776                                         luaL_checknumber(L, 3)));
4777         return 1;
4778 }
4779
4780 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
4781 static int l_get_hit_params(lua_State *L)
4782 {
4783         std::map<std::string, int> groups;
4784         read_groups(L, 1, groups);
4785         ToolCapabilities tp = read_tool_capabilities(L, 2);
4786         if(lua_isnoneornil(L, 3))
4787                 push_hit_params(L, getHitParams(groups, &tp));
4788         else
4789                 push_hit_params(L, getHitParams(groups, &tp,
4790                                         luaL_checknumber(L, 3)));
4791         return 1;
4792 }
4793
4794 // get_current_modname()
4795 static int l_get_current_modname(lua_State *L)
4796 {
4797         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
4798         return 1;
4799 }
4800
4801 // get_modpath(modname)
4802 static int l_get_modpath(lua_State *L)
4803 {
4804         std::string modname = luaL_checkstring(L, 1);
4805         // Do it
4806         if(modname == "__builtin"){
4807                 std::string path = get_server(L)->getBuiltinLuaPath();
4808                 lua_pushstring(L, path.c_str());
4809                 return 1;
4810         }
4811         const ModSpec *mod = get_server(L)->getModSpec(modname);
4812         if(!mod){
4813                 lua_pushnil(L);
4814                 return 1;
4815         }
4816         lua_pushstring(L, mod->path.c_str());
4817         return 1;
4818 }
4819
4820 // get_modnames()
4821 // the returned list is sorted alphabetically for you
4822 static int l_get_modnames(lua_State *L)
4823 {
4824         // Get a list of mods
4825         core::list<std::string> mods_unsorted, mods_sorted;
4826         get_server(L)->getModNames(mods_unsorted);
4827
4828         // Take unsorted items from mods_unsorted and sort them into
4829         // mods_sorted; not great performance but the number of mods on a
4830         // server will likely be small.
4831         for(core::list<std::string>::Iterator i = mods_unsorted.begin();
4832             i != mods_unsorted.end(); i++)
4833         {
4834                 bool added = false;
4835                 for(core::list<std::string>::Iterator x = mods_sorted.begin();
4836                     x != mods_unsorted.end(); x++)
4837                 {
4838                         // I doubt anybody using Minetest will be using
4839                         // anything not ASCII based :)
4840                         if((*i).compare(*x) <= 0)
4841                         {
4842                                 mods_sorted.insert_before(x, *i);
4843                                 added = true;
4844                                 break;
4845                         }
4846                 }
4847                 if(!added)
4848                         mods_sorted.push_back(*i);
4849         }
4850
4851         // Get the table insertion function from Lua.
4852         lua_getglobal(L, "table");
4853         lua_getfield(L, -1, "insert");
4854         int insertion_func = lua_gettop(L);
4855
4856         // Package them up for Lua
4857         lua_newtable(L);
4858         int new_table = lua_gettop(L);
4859         core::list<std::string>::Iterator i = mods_sorted.begin();
4860         while(i != mods_sorted.end())
4861         {
4862                 lua_pushvalue(L, insertion_func);
4863                 lua_pushvalue(L, new_table);
4864                 lua_pushstring(L, (*i).c_str());
4865                 if(lua_pcall(L, 2, 0, 0) != 0)
4866                 {
4867                         script_error(L, "error: %s", lua_tostring(L, -1));
4868                 }
4869                 i++;
4870         }
4871         return 1;
4872 }
4873
4874 // get_worldpath()
4875 static int l_get_worldpath(lua_State *L)
4876 {
4877         std::string worldpath = get_server(L)->getWorldPath();
4878         lua_pushstring(L, worldpath.c_str());
4879         return 1;
4880 }
4881
4882 // sound_play(spec, parameters)
4883 static int l_sound_play(lua_State *L)
4884 {
4885         SimpleSoundSpec spec;
4886         read_soundspec(L, 1, spec);
4887         ServerSoundParams params;
4888         read_server_sound_params(L, 2, params);
4889         s32 handle = get_server(L)->playSound(spec, params);
4890         lua_pushinteger(L, handle);
4891         return 1;
4892 }
4893
4894 // sound_stop(handle)
4895 static int l_sound_stop(lua_State *L)
4896 {
4897         int handle = luaL_checkinteger(L, 1);
4898         get_server(L)->stopSound(handle);
4899         return 0;
4900 }
4901
4902 // is_singleplayer()
4903 static int l_is_singleplayer(lua_State *L)
4904 {
4905         lua_pushboolean(L, get_server(L)->isSingleplayer());
4906         return 1;
4907 }
4908
4909 // get_password_hash(name, raw_password)
4910 static int l_get_password_hash(lua_State *L)
4911 {
4912         std::string name = luaL_checkstring(L, 1);
4913         std::string raw_password = luaL_checkstring(L, 2);
4914         std::string hash = translatePassword(name,
4915                         narrow_to_wide(raw_password));
4916         lua_pushstring(L, hash.c_str());
4917         return 1;
4918 }
4919
4920 // notify_authentication_modified(name)
4921 static int l_notify_authentication_modified(lua_State *L)
4922 {
4923         std::string name = "";
4924         if(lua_isstring(L, 1))
4925                 name = lua_tostring(L, 1);
4926         get_server(L)->reportPrivsModified(name);
4927         return 0;
4928 }
4929
4930 // get_craft_result(input)
4931 static int l_get_craft_result(lua_State *L)
4932 {
4933         int input_i = 1;
4934         std::string method_s = getstringfield_default(L, input_i, "method", "normal");
4935         enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method",
4936                                 es_CraftMethod, CRAFT_METHOD_NORMAL);
4937         int width = 1;
4938         lua_getfield(L, input_i, "width");
4939         if(lua_isnumber(L, -1))
4940                 width = luaL_checkinteger(L, -1);
4941         lua_pop(L, 1);
4942         lua_getfield(L, input_i, "items");
4943         std::vector<ItemStack> items = read_items(L, -1);
4944         lua_pop(L, 1); // items
4945         
4946         IGameDef *gdef = get_server(L);
4947         ICraftDefManager *cdef = gdef->cdef();
4948         CraftInput input(method, width, items);
4949         CraftOutput output;
4950         bool got = cdef->getCraftResult(input, output, true, gdef);
4951         lua_newtable(L); // output table
4952         if(got){
4953                 ItemStack item;
4954                 item.deSerialize(output.item, gdef->idef());
4955                 LuaItemStack::create(L, item);
4956                 lua_setfield(L, -2, "item");
4957                 setintfield(L, -1, "time", output.time);
4958         } else {
4959                 LuaItemStack::create(L, ItemStack());
4960                 lua_setfield(L, -2, "item");
4961                 setintfield(L, -1, "time", 0);
4962         }
4963         lua_newtable(L); // decremented input table
4964         lua_pushstring(L, method_s.c_str());
4965         lua_setfield(L, -2, "method");
4966         lua_pushinteger(L, width);
4967         lua_setfield(L, -2, "width");
4968         push_items(L, input.items);
4969         lua_setfield(L, -2, "items");
4970         return 2;
4971 }
4972
4973 // get_craft_recipe(result item)
4974 static int l_get_craft_recipe(lua_State *L)
4975 {
4976         int k = 0;
4977         char tmp[20];
4978         int input_i = 1;
4979         std::string o_item = luaL_checkstring(L,input_i);
4980         
4981         IGameDef *gdef = get_server(L);
4982         ICraftDefManager *cdef = gdef->cdef();
4983         CraftInput input;
4984         CraftOutput output(o_item,0);
4985         bool got = cdef->getCraftRecipe(input, output, gdef);
4986         lua_newtable(L); // output table
4987         if(got){
4988                 lua_newtable(L);
4989                 for(std::vector<ItemStack>::const_iterator
4990                         i = input.items.begin();
4991                         i != input.items.end(); i++, k++)
4992                 {
4993                         if (i->empty())
4994                         {
4995                                 continue;
4996                         }
4997                         sprintf(tmp,"%d",k);
4998                         lua_pushstring(L,tmp);
4999                         lua_pushstring(L,i->name.c_str());
5000                         lua_settable(L, -3);
5001                 }
5002                 lua_setfield(L, -2, "items");
5003                 setintfield(L, -1, "width", input.width);
5004                 switch (input.method) {
5005                 case CRAFT_METHOD_NORMAL:
5006                         lua_pushstring(L,"normal");
5007                         break;
5008                 case CRAFT_METHOD_COOKING:
5009                         lua_pushstring(L,"cooking");
5010                         break;
5011                 case CRAFT_METHOD_FUEL:
5012                         lua_pushstring(L,"fuel");
5013                         break;
5014                 default:
5015                         lua_pushstring(L,"unknown");
5016                 }
5017                 lua_setfield(L, -2, "type");
5018         } else {
5019                 lua_pushnil(L);
5020                 lua_setfield(L, -2, "items");
5021                 setintfield(L, -1, "width", 0);
5022         }
5023         return 1;
5024 }
5025
5026 // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
5027 static int l_rollback_get_last_node_actor(lua_State *L)
5028 {
5029         v3s16 p = read_v3s16(L, 1);
5030         int range = luaL_checknumber(L, 2);
5031         int seconds = luaL_checknumber(L, 3);
5032         Server *server = get_server(L);
5033         IRollbackManager *rollback = server->getRollbackManager();
5034         v3s16 act_p;
5035         int act_seconds = 0;
5036         std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
5037         lua_pushstring(L, actor.c_str());
5038         push_v3s16(L, act_p);
5039         lua_pushnumber(L, act_seconds);
5040         return 3;
5041 }
5042
5043 // rollback_revert_actions_by(actor, seconds) -> bool, log messages
5044 static int l_rollback_revert_actions_by(lua_State *L)
5045 {
5046         std::string actor = luaL_checkstring(L, 1);
5047         int seconds = luaL_checknumber(L, 2);
5048         Server *server = get_server(L);
5049         IRollbackManager *rollback = server->getRollbackManager();
5050         std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
5051         std::list<std::string> log;
5052         bool success = server->rollbackRevertActions(actions, &log);
5053         // Push boolean result
5054         lua_pushboolean(L, success);
5055         // Get the table insert function and push the log table
5056         lua_getglobal(L, "table");
5057         lua_getfield(L, -1, "insert");
5058         int table_insert = lua_gettop(L);
5059         lua_newtable(L);
5060         int table = lua_gettop(L);
5061         for(std::list<std::string>::const_iterator i = log.begin();
5062                         i != log.end(); i++)
5063         {
5064                 lua_pushvalue(L, table_insert);
5065                 lua_pushvalue(L, table);
5066                 lua_pushstring(L, i->c_str());
5067                 if(lua_pcall(L, 2, 0, 0))
5068                         script_error(L, "error: %s", lua_tostring(L, -1));
5069         }
5070         lua_remove(L, -2); // Remove table
5071         lua_remove(L, -2); // Remove insert
5072         return 2;
5073 }
5074
5075 static const struct luaL_Reg minetest_f [] = {
5076         {"debug", l_debug},
5077         {"log", l_log},
5078         {"request_shutdown", l_request_shutdown},
5079         {"get_server_status", l_get_server_status},
5080         {"register_item_raw", l_register_item_raw},
5081         {"register_alias_raw", l_register_alias_raw},
5082         {"register_craft", l_register_craft},
5083         {"setting_set", l_setting_set},
5084         {"setting_get", l_setting_get},
5085         {"setting_getbool", l_setting_getbool},
5086         {"chat_send_all", l_chat_send_all},
5087         {"chat_send_player", l_chat_send_player},
5088         {"get_player_privs", l_get_player_privs},
5089         {"get_ban_list", l_get_ban_list},
5090         {"get_ban_description", l_get_ban_description},
5091         {"ban_player", l_ban_player},
5092         {"unban_player_or_ip", l_unban_player_of_ip},
5093         {"get_inventory", l_get_inventory},
5094         {"create_detached_inventory_raw", l_create_detached_inventory_raw},
5095         {"get_dig_params", l_get_dig_params},
5096         {"get_hit_params", l_get_hit_params},
5097         {"get_current_modname", l_get_current_modname},
5098         {"get_modpath", l_get_modpath},
5099         {"get_modnames", l_get_modnames},
5100         {"get_worldpath", l_get_worldpath},
5101         {"sound_play", l_sound_play},
5102         {"sound_stop", l_sound_stop},
5103         {"is_singleplayer", l_is_singleplayer},
5104         {"get_password_hash", l_get_password_hash},
5105         {"notify_authentication_modified", l_notify_authentication_modified},
5106         {"get_craft_result", l_get_craft_result},
5107         {"get_craft_recipe", l_get_craft_recipe},
5108         {"rollback_get_last_node_actor", l_rollback_get_last_node_actor},
5109         {"rollback_revert_actions_by", l_rollback_revert_actions_by},
5110         {NULL, NULL}
5111 };
5112
5113 /*
5114         Main export function
5115 */
5116
5117 void scriptapi_export(lua_State *L, Server *server)
5118 {
5119         realitycheck(L);
5120         assert(lua_checkstack(L, 20));
5121         verbosestream<<"scriptapi_export()"<<std::endl;
5122         StackUnroller stack_unroller(L);
5123
5124         // Store server as light userdata in registry
5125         lua_pushlightuserdata(L, server);
5126         lua_setfield(L, LUA_REGISTRYINDEX, "minetest_server");
5127
5128         // Register global functions in table minetest
5129         lua_newtable(L);
5130         luaL_register(L, NULL, minetest_f);
5131         lua_setglobal(L, "minetest");
5132         
5133         // Get the main minetest table
5134         lua_getglobal(L, "minetest");
5135
5136         // Add tables to minetest
5137         lua_newtable(L);
5138         lua_setfield(L, -2, "object_refs");
5139         lua_newtable(L);
5140         lua_setfield(L, -2, "luaentities");
5141
5142         // Register wrappers
5143         LuaItemStack::Register(L);
5144         InvRef::Register(L);
5145         NodeMetaRef::Register(L);
5146         NodeTimerRef::Register(L);
5147         ObjectRef::Register(L);
5148         EnvRef::Register(L);
5149         LuaPseudoRandom::Register(L);
5150         LuaPerlinNoise::Register(L);
5151 }
5152
5153 bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
5154                 const std::string &modname)
5155 {
5156         ModNameStorer modnamestorer(L, modname);
5157
5158         if(!string_allowed(modname, "abcdefghijklmnopqrstuvwxyz"
5159                         "0123456789_")){
5160                 errorstream<<"Error loading mod \""<<modname
5161                                 <<"\": modname does not follow naming conventions: "
5162                                 <<"Only chararacters [a-z0-9_] are allowed."<<std::endl;
5163                 return false;
5164         }
5165         
5166         bool success = false;
5167
5168         try{
5169                 success = script_load(L, scriptpath.c_str());
5170         }
5171         catch(LuaError &e){
5172                 errorstream<<"Error loading mod \""<<modname
5173                                 <<"\": "<<e.what()<<std::endl;
5174         }
5175
5176         return success;
5177 }
5178
5179 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
5180 {
5181         realitycheck(L);
5182         assert(lua_checkstack(L, 20));
5183         verbosestream<<"scriptapi_add_environment"<<std::endl;
5184         StackUnroller stack_unroller(L);
5185
5186         // Create EnvRef on stack
5187         EnvRef::create(L, env);
5188         int envref = lua_gettop(L);
5189
5190         // minetest.env = envref
5191         lua_getglobal(L, "minetest");
5192         luaL_checktype(L, -1, LUA_TTABLE);
5193         lua_pushvalue(L, envref);
5194         lua_setfield(L, -2, "env");
5195
5196         // Store environment as light userdata in registry
5197         lua_pushlightuserdata(L, env);
5198         lua_setfield(L, LUA_REGISTRYINDEX, "minetest_env");
5199
5200         /*
5201                 Add ActiveBlockModifiers to environment
5202         */
5203
5204         // Get minetest.registered_abms
5205         lua_getglobal(L, "minetest");
5206         lua_getfield(L, -1, "registered_abms");
5207         luaL_checktype(L, -1, LUA_TTABLE);
5208         int registered_abms = lua_gettop(L);
5209         
5210         if(lua_istable(L, registered_abms)){
5211                 int table = lua_gettop(L);
5212                 lua_pushnil(L);
5213                 while(lua_next(L, table) != 0){
5214                         // key at index -2 and value at index -1
5215                         int id = lua_tonumber(L, -2);
5216                         int current_abm = lua_gettop(L);
5217
5218                         std::set<std::string> trigger_contents;
5219                         lua_getfield(L, current_abm, "nodenames");
5220                         if(lua_istable(L, -1)){
5221                                 int table = lua_gettop(L);
5222                                 lua_pushnil(L);
5223                                 while(lua_next(L, table) != 0){
5224                                         // key at index -2 and value at index -1
5225                                         luaL_checktype(L, -1, LUA_TSTRING);
5226                                         trigger_contents.insert(lua_tostring(L, -1));
5227                                         // removes value, keeps key for next iteration
5228                                         lua_pop(L, 1);
5229                                 }
5230                         } else if(lua_isstring(L, -1)){
5231                                 trigger_contents.insert(lua_tostring(L, -1));
5232                         }
5233                         lua_pop(L, 1);
5234
5235                         std::set<std::string> required_neighbors;
5236                         lua_getfield(L, current_abm, "neighbors");
5237                         if(lua_istable(L, -1)){
5238                                 int table = lua_gettop(L);
5239                                 lua_pushnil(L);
5240                                 while(lua_next(L, table) != 0){
5241                                         // key at index -2 and value at index -1
5242                                         luaL_checktype(L, -1, LUA_TSTRING);
5243                                         required_neighbors.insert(lua_tostring(L, -1));
5244                                         // removes value, keeps key for next iteration
5245                                         lua_pop(L, 1);
5246                                 }
5247                         } else if(lua_isstring(L, -1)){
5248                                 required_neighbors.insert(lua_tostring(L, -1));
5249                         }
5250                         lua_pop(L, 1);
5251
5252                         float trigger_interval = 10.0;
5253                         getfloatfield(L, current_abm, "interval", trigger_interval);
5254
5255                         int trigger_chance = 50;
5256                         getintfield(L, current_abm, "chance", trigger_chance);
5257
5258                         LuaABM *abm = new LuaABM(L, id, trigger_contents,
5259                                         required_neighbors, trigger_interval, trigger_chance);
5260                         
5261                         env->addActiveBlockModifier(abm);
5262
5263                         // removes value, keeps key for next iteration
5264                         lua_pop(L, 1);
5265                 }
5266         }
5267         lua_pop(L, 1);
5268 }
5269
5270 #if 0
5271 // Dump stack top with the dump2 function
5272 static void dump2(lua_State *L, const char *name)
5273 {
5274         // Dump object (debug)
5275         lua_getglobal(L, "dump2");
5276         luaL_checktype(L, -1, LUA_TFUNCTION);
5277         lua_pushvalue(L, -2); // Get previous stack top as first parameter
5278         lua_pushstring(L, name);
5279         if(lua_pcall(L, 2, 0, 0))
5280                 script_error(L, "error: %s", lua_tostring(L, -1));
5281 }
5282 #endif
5283
5284 /*
5285         object_reference
5286 */
5287
5288 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
5289 {
5290         realitycheck(L);
5291         assert(lua_checkstack(L, 20));
5292         //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
5293         StackUnroller stack_unroller(L);
5294
5295         // Create object on stack
5296         ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
5297         int object = lua_gettop(L);
5298
5299         // Get minetest.object_refs table
5300         lua_getglobal(L, "minetest");
5301         lua_getfield(L, -1, "object_refs");
5302         luaL_checktype(L, -1, LUA_TTABLE);
5303         int objectstable = lua_gettop(L);
5304         
5305         // object_refs[id] = object
5306         lua_pushnumber(L, cobj->getId()); // Push id
5307         lua_pushvalue(L, object); // Copy object to top of stack
5308         lua_settable(L, objectstable);
5309 }
5310
5311 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
5312 {
5313         realitycheck(L);
5314         assert(lua_checkstack(L, 20));
5315         //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
5316         StackUnroller stack_unroller(L);
5317
5318         // Get minetest.object_refs table
5319         lua_getglobal(L, "minetest");
5320         lua_getfield(L, -1, "object_refs");
5321         luaL_checktype(L, -1, LUA_TTABLE);
5322         int objectstable = lua_gettop(L);
5323         
5324         // Get object_refs[id]
5325         lua_pushnumber(L, cobj->getId()); // Push id
5326         lua_gettable(L, objectstable);
5327         // Set object reference to NULL
5328         ObjectRef::set_null(L);
5329         lua_pop(L, 1); // pop object
5330
5331         // Set object_refs[id] = nil
5332         lua_pushnumber(L, cobj->getId()); // Push id
5333         lua_pushnil(L);
5334         lua_settable(L, objectstable);
5335 }
5336
5337 /*
5338         misc
5339 */
5340
5341 // What scriptapi_run_callbacks does with the return values of callbacks.
5342 // Regardless of the mode, if only one callback is defined,
5343 // its return value is the total return value.
5344 // Modes only affect the case where 0 or >= 2 callbacks are defined.
5345 enum RunCallbacksMode
5346 {
5347         // Returns the return value of the first callback
5348         // Returns nil if list of callbacks is empty
5349         RUN_CALLBACKS_MODE_FIRST,
5350         // Returns the return value of the last callback
5351         // Returns nil if list of callbacks is empty
5352         RUN_CALLBACKS_MODE_LAST,
5353         // If any callback returns a false value, the first such is returned
5354         // Otherwise, the first callback's return value (trueish) is returned
5355         // Returns true if list of callbacks is empty
5356         RUN_CALLBACKS_MODE_AND,
5357         // Like above, but stops calling callbacks (short circuit)
5358         // after seeing the first false value
5359         RUN_CALLBACKS_MODE_AND_SC,
5360         // If any callback returns a true value, the first such is returned
5361         // Otherwise, the first callback's return value (falseish) is returned
5362         // Returns false if list of callbacks is empty
5363         RUN_CALLBACKS_MODE_OR,
5364         // Like above, but stops calling callbacks (short circuit)
5365         // after seeing the first true value
5366         RUN_CALLBACKS_MODE_OR_SC,
5367         // Note: "a true value" and "a false value" refer to values that
5368         // are converted by lua_toboolean to true or false, respectively.
5369 };
5370
5371 // Push the list of callbacks (a lua table).
5372 // Then push nargs arguments.
5373 // Then call this function, which
5374 // - runs the callbacks
5375 // - removes the table and arguments from the lua stack
5376 // - pushes the return value, computed depending on mode
5377 static void scriptapi_run_callbacks(lua_State *L, int nargs,
5378                 RunCallbacksMode mode)
5379 {
5380         // Insert the return value into the lua stack, below the table
5381         assert(lua_gettop(L) >= nargs + 1);
5382         lua_pushnil(L);
5383         lua_insert(L, -(nargs + 1) - 1);
5384         // Stack now looks like this:
5385         // ... <return value = nil> <table> <arg#1> <arg#2> ... <arg#n>
5386
5387         int rv = lua_gettop(L) - nargs - 1;
5388         int table = rv + 1;
5389         int arg = table + 1;
5390
5391         luaL_checktype(L, table, LUA_TTABLE);
5392
5393         // Foreach
5394         lua_pushnil(L);
5395         bool first_loop = true;
5396         while(lua_next(L, table) != 0){
5397                 // key at index -2 and value at index -1
5398                 luaL_checktype(L, -1, LUA_TFUNCTION);
5399                 // Call function
5400                 for(int i = 0; i < nargs; i++)
5401                         lua_pushvalue(L, arg+i);
5402                 if(lua_pcall(L, nargs, 1, 0))
5403                         script_error(L, "error: %s", lua_tostring(L, -1));
5404
5405                 // Move return value to designated space in stack
5406                 // Or pop it
5407                 if(first_loop){
5408                         // Result of first callback is always moved
5409                         lua_replace(L, rv);
5410                         first_loop = false;
5411                 } else {
5412                         // Otherwise, what happens depends on the mode
5413                         if(mode == RUN_CALLBACKS_MODE_FIRST)
5414                                 lua_pop(L, 1);
5415                         else if(mode == RUN_CALLBACKS_MODE_LAST)
5416                                 lua_replace(L, rv);
5417                         else if(mode == RUN_CALLBACKS_MODE_AND ||
5418                                         mode == RUN_CALLBACKS_MODE_AND_SC){
5419                                 if(lua_toboolean(L, rv) == true &&
5420                                                 lua_toboolean(L, -1) == false)
5421                                         lua_replace(L, rv);
5422                                 else
5423                                         lua_pop(L, 1);
5424                         }
5425                         else if(mode == RUN_CALLBACKS_MODE_OR ||
5426                                         mode == RUN_CALLBACKS_MODE_OR_SC){
5427                                 if(lua_toboolean(L, rv) == false &&
5428                                                 lua_toboolean(L, -1) == true)
5429                                         lua_replace(L, rv);
5430                                 else
5431                                         lua_pop(L, 1);
5432                         }
5433                         else
5434                                 assert(0);
5435                 }
5436
5437                 // Handle short circuit modes
5438                 if(mode == RUN_CALLBACKS_MODE_AND_SC &&
5439                                 lua_toboolean(L, rv) == false)
5440                         break;
5441                 else if(mode == RUN_CALLBACKS_MODE_OR_SC &&
5442                                 lua_toboolean(L, rv) == true)
5443                         break;
5444
5445                 // value removed, keep key for next iteration
5446         }
5447
5448         // Remove stuff from stack, leaving only the return value
5449         lua_settop(L, rv);
5450
5451         // Fix return value in case no callbacks were called
5452         if(first_loop){
5453                 if(mode == RUN_CALLBACKS_MODE_AND ||
5454                                 mode == RUN_CALLBACKS_MODE_AND_SC){
5455                         lua_pop(L, 1);
5456                         lua_pushboolean(L, true);
5457                 }
5458                 else if(mode == RUN_CALLBACKS_MODE_OR ||
5459                                 mode == RUN_CALLBACKS_MODE_OR_SC){
5460                         lua_pop(L, 1);
5461                         lua_pushboolean(L, false);
5462                 }
5463         }
5464 }
5465
5466 bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
5467                 const std::string &message)
5468 {
5469         realitycheck(L);
5470         assert(lua_checkstack(L, 20));
5471         StackUnroller stack_unroller(L);
5472
5473         // Get minetest.registered_on_chat_messages
5474         lua_getglobal(L, "minetest");
5475         lua_getfield(L, -1, "registered_on_chat_messages");
5476         // Call callbacks
5477         lua_pushstring(L, name.c_str());
5478         lua_pushstring(L, message.c_str());
5479         scriptapi_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
5480         bool ate = lua_toboolean(L, -1);
5481         return ate;
5482 }
5483
5484 void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player)
5485 {
5486         realitycheck(L);
5487         assert(lua_checkstack(L, 20));
5488         StackUnroller stack_unroller(L);
5489
5490         // Get minetest.registered_on_newplayers
5491         lua_getglobal(L, "minetest");
5492         lua_getfield(L, -1, "registered_on_newplayers");
5493         // Call callbacks
5494         objectref_get_or_create(L, player);
5495         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
5496 }
5497
5498 void scriptapi_on_dieplayer(lua_State *L, ServerActiveObject *player)
5499 {
5500         realitycheck(L);
5501         assert(lua_checkstack(L, 20));
5502         StackUnroller stack_unroller(L);
5503
5504         // Get minetest.registered_on_dieplayers
5505         lua_getglobal(L, "minetest");
5506         lua_getfield(L, -1, "registered_on_dieplayers");
5507         // Call callbacks
5508         objectref_get_or_create(L, player);
5509         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
5510 }
5511
5512 bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player)
5513 {
5514         realitycheck(L);
5515         assert(lua_checkstack(L, 20));
5516         StackUnroller stack_unroller(L);
5517
5518         // Get minetest.registered_on_respawnplayers
5519         lua_getglobal(L, "minetest");
5520         lua_getfield(L, -1, "registered_on_respawnplayers");
5521         // Call callbacks
5522         objectref_get_or_create(L, player);
5523         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
5524         bool positioning_handled_by_some = lua_toboolean(L, -1);
5525         return positioning_handled_by_some;
5526 }
5527
5528 void scriptapi_on_joinplayer(lua_State *L, ServerActiveObject *player)
5529 {
5530         realitycheck(L);
5531         assert(lua_checkstack(L, 20));
5532         StackUnroller stack_unroller(L);
5533
5534         // Get minetest.registered_on_joinplayers
5535         lua_getglobal(L, "minetest");
5536         lua_getfield(L, -1, "registered_on_joinplayers");
5537         // Call callbacks
5538         objectref_get_or_create(L, player);
5539         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
5540 }
5541
5542 void scriptapi_on_leaveplayer(lua_State *L, ServerActiveObject *player)
5543 {
5544         realitycheck(L);
5545         assert(lua_checkstack(L, 20));
5546         StackUnroller stack_unroller(L);
5547
5548         // Get minetest.registered_on_leaveplayers
5549         lua_getglobal(L, "minetest");
5550         lua_getfield(L, -1, "registered_on_leaveplayers");
5551         // Call callbacks
5552         objectref_get_or_create(L, player);
5553         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
5554 }
5555
5556 static void get_auth_handler(lua_State *L)
5557 {
5558         lua_getglobal(L, "minetest");
5559         lua_getfield(L, -1, "registered_auth_handler");
5560         if(lua_isnil(L, -1)){
5561                 lua_pop(L, 1);
5562                 lua_getfield(L, -1, "builtin_auth_handler");
5563         }
5564         if(lua_type(L, -1) != LUA_TTABLE)
5565                 throw LuaError(L, "Authentication handler table not valid");
5566 }
5567
5568 bool scriptapi_get_auth(lua_State *L, const std::string &playername,
5569                 std::string *dst_password, std::set<std::string> *dst_privs)
5570 {
5571         realitycheck(L);
5572         assert(lua_checkstack(L, 20));
5573         StackUnroller stack_unroller(L);
5574         
5575         get_auth_handler(L);
5576         lua_getfield(L, -1, "get_auth");
5577         if(lua_type(L, -1) != LUA_TFUNCTION)
5578                 throw LuaError(L, "Authentication handler missing get_auth");
5579         lua_pushstring(L, playername.c_str());
5580         if(lua_pcall(L, 1, 1, 0))
5581                 script_error(L, "error: %s", lua_tostring(L, -1));
5582         
5583         // nil = login not allowed
5584         if(lua_isnil(L, -1))
5585                 return false;
5586         luaL_checktype(L, -1, LUA_TTABLE);
5587         
5588         std::string password;
5589         bool found = getstringfield(L, -1, "password", password);
5590         if(!found)
5591                 throw LuaError(L, "Authentication handler didn't return password");
5592         if(dst_password)
5593                 *dst_password = password;
5594
5595         lua_getfield(L, -1, "privileges");
5596         if(!lua_istable(L, -1))
5597                 throw LuaError(L,
5598                                 "Authentication handler didn't return privilege table");
5599         if(dst_privs)
5600                 read_privileges(L, -1, *dst_privs);
5601         lua_pop(L, 1);
5602         
5603         return true;
5604 }
5605
5606 void scriptapi_create_auth(lua_State *L, const std::string &playername,
5607                 const std::string &password)
5608 {
5609         realitycheck(L);
5610         assert(lua_checkstack(L, 20));
5611         StackUnroller stack_unroller(L);
5612         
5613         get_auth_handler(L);
5614         lua_getfield(L, -1, "create_auth");
5615         if(lua_type(L, -1) != LUA_TFUNCTION)
5616                 throw LuaError(L, "Authentication handler missing create_auth");
5617         lua_pushstring(L, playername.c_str());
5618         lua_pushstring(L, password.c_str());
5619         if(lua_pcall(L, 2, 0, 0))
5620                 script_error(L, "error: %s", lua_tostring(L, -1));
5621 }
5622
5623 bool scriptapi_set_password(lua_State *L, const std::string &playername,
5624                 const std::string &password)
5625 {
5626         realitycheck(L);
5627         assert(lua_checkstack(L, 20));
5628         StackUnroller stack_unroller(L);
5629         
5630         get_auth_handler(L);
5631         lua_getfield(L, -1, "set_password");
5632         if(lua_type(L, -1) != LUA_TFUNCTION)
5633                 throw LuaError(L, "Authentication handler missing set_password");
5634         lua_pushstring(L, playername.c_str());
5635         lua_pushstring(L, password.c_str());
5636         if(lua_pcall(L, 2, 1, 0))
5637                 script_error(L, "error: %s", lua_tostring(L, -1));
5638         return lua_toboolean(L, -1);
5639 }
5640
5641 /*
5642         player
5643 */
5644
5645 void scriptapi_on_player_receive_fields(lua_State *L, 
5646                 ServerActiveObject *player,
5647                 const std::string &formname,
5648                 const std::map<std::string, std::string> &fields)
5649 {
5650         realitycheck(L);
5651         assert(lua_checkstack(L, 20));
5652         StackUnroller stack_unroller(L);
5653
5654         // Get minetest.registered_on_chat_messages
5655         lua_getglobal(L, "minetest");
5656         lua_getfield(L, -1, "registered_on_player_receive_fields");
5657         // Call callbacks
5658         // param 1
5659         objectref_get_or_create(L, player);
5660         // param 2
5661         lua_pushstring(L, formname.c_str());
5662         // param 3
5663         lua_newtable(L);
5664         for(std::map<std::string, std::string>::const_iterator
5665                         i = fields.begin(); i != fields.end(); i++){
5666                 const std::string &name = i->first;
5667                 const std::string &value = i->second;
5668                 lua_pushstring(L, name.c_str());
5669                 lua_pushlstring(L, value.c_str(), value.size());
5670                 lua_settable(L, -3);
5671         }
5672         scriptapi_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
5673 }
5674
5675 /*
5676         item callbacks and node callbacks
5677 */
5678
5679 // Retrieves minetest.registered_items[name][callbackname]
5680 // If that is nil or on error, return false and stack is unchanged
5681 // If that is a function, returns true and pushes the
5682 // function onto the stack
5683 // If minetest.registered_items[name] doesn't exist, minetest.nodedef_default
5684 // is tried instead so unknown items can still be manipulated to some degree
5685 static bool get_item_callback(lua_State *L,
5686                 const char *name, const char *callbackname)
5687 {
5688         lua_getglobal(L, "minetest");
5689         lua_getfield(L, -1, "registered_items");
5690         lua_remove(L, -2);
5691         luaL_checktype(L, -1, LUA_TTABLE);
5692         lua_getfield(L, -1, name);
5693         lua_remove(L, -2);
5694         // Should be a table
5695         if(lua_type(L, -1) != LUA_TTABLE)
5696         {
5697                 // Report error and clean up
5698                 errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
5699                 lua_pop(L, 1);
5700
5701                 // Try minetest.nodedef_default instead
5702                 lua_getglobal(L, "minetest");
5703                 lua_getfield(L, -1, "nodedef_default");
5704                 lua_remove(L, -2);
5705                 luaL_checktype(L, -1, LUA_TTABLE);
5706         }
5707         lua_getfield(L, -1, callbackname);
5708         lua_remove(L, -2);
5709         // Should be a function or nil
5710         if(lua_type(L, -1) == LUA_TFUNCTION)
5711         {
5712                 return true;
5713         }
5714         else if(lua_isnil(L, -1))
5715         {
5716                 lua_pop(L, 1);
5717                 return false;
5718         }
5719         else
5720         {
5721                 errorstream<<"Item \""<<name<<"\" callback \""
5722                         <<callbackname<<" is not a function"<<std::endl;
5723                 lua_pop(L, 1);
5724                 return false;
5725         }
5726 }
5727
5728 bool scriptapi_item_on_drop(lua_State *L, ItemStack &item,
5729                 ServerActiveObject *dropper, v3f pos)
5730 {
5731         realitycheck(L);
5732         assert(lua_checkstack(L, 20));
5733         StackUnroller stack_unroller(L);
5734
5735         // Push callback function on stack
5736         if(!get_item_callback(L, item.name.c_str(), "on_drop"))
5737                 return false;
5738
5739         // Call function
5740         LuaItemStack::create(L, item);
5741         objectref_get_or_create(L, dropper);
5742         pushFloatPos(L, pos);
5743         if(lua_pcall(L, 3, 1, 0))
5744                 script_error(L, "error: %s", lua_tostring(L, -1));
5745         if(!lua_isnil(L, -1))
5746                 item = read_item(L, -1);
5747         return true;
5748 }
5749
5750 bool scriptapi_item_on_place(lua_State *L, ItemStack &item,
5751                 ServerActiveObject *placer, const PointedThing &pointed)
5752 {
5753         realitycheck(L);
5754         assert(lua_checkstack(L, 20));
5755         StackUnroller stack_unroller(L);
5756
5757         // Push callback function on stack
5758         if(!get_item_callback(L, item.name.c_str(), "on_place"))
5759                 return false;
5760
5761         // Call function
5762         LuaItemStack::create(L, item);
5763         objectref_get_or_create(L, placer);
5764         push_pointed_thing(L, pointed);
5765         if(lua_pcall(L, 3, 1, 0))
5766                 script_error(L, "error: %s", lua_tostring(L, -1));
5767         if(!lua_isnil(L, -1))
5768                 item = read_item(L, -1);
5769         return true;
5770 }
5771
5772 bool scriptapi_item_on_use(lua_State *L, ItemStack &item,
5773                 ServerActiveObject *user, const PointedThing &pointed)
5774 {
5775         realitycheck(L);
5776         assert(lua_checkstack(L, 20));
5777         StackUnroller stack_unroller(L);
5778
5779         // Push callback function on stack
5780         if(!get_item_callback(L, item.name.c_str(), "on_use"))
5781                 return false;
5782
5783         // Call function
5784         LuaItemStack::create(L, item);
5785         objectref_get_or_create(L, user);
5786         push_pointed_thing(L, pointed);
5787         if(lua_pcall(L, 3, 1, 0))
5788                 script_error(L, "error: %s", lua_tostring(L, -1));
5789         if(!lua_isnil(L, -1))
5790                 item = read_item(L, -1);
5791         return true;
5792 }
5793
5794 bool scriptapi_node_on_punch(lua_State *L, v3s16 p, MapNode node,
5795                 ServerActiveObject *puncher)
5796 {
5797         realitycheck(L);
5798         assert(lua_checkstack(L, 20));
5799         StackUnroller stack_unroller(L);
5800
5801         INodeDefManager *ndef = get_server(L)->ndef();
5802
5803         // Push callback function on stack
5804         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_punch"))
5805                 return false;
5806
5807         // Call function
5808         push_v3s16(L, p);
5809         pushnode(L, node, ndef);
5810         objectref_get_or_create(L, puncher);
5811         if(lua_pcall(L, 3, 0, 0))
5812                 script_error(L, "error: %s", lua_tostring(L, -1));
5813         return true;
5814 }
5815
5816 bool scriptapi_node_on_dig(lua_State *L, v3s16 p, MapNode node,
5817                 ServerActiveObject *digger)
5818 {
5819         realitycheck(L);
5820         assert(lua_checkstack(L, 20));
5821         StackUnroller stack_unroller(L);
5822
5823         INodeDefManager *ndef = get_server(L)->ndef();
5824
5825         // Push callback function on stack
5826         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_dig"))
5827                 return false;
5828
5829         // Call function
5830         push_v3s16(L, p);
5831         pushnode(L, node, ndef);
5832         objectref_get_or_create(L, digger);
5833         if(lua_pcall(L, 3, 0, 0))
5834                 script_error(L, "error: %s", lua_tostring(L, -1));
5835         return true;
5836 }
5837
5838 void scriptapi_node_on_construct(lua_State *L, v3s16 p, MapNode node)
5839 {
5840         realitycheck(L);
5841         assert(lua_checkstack(L, 20));
5842         StackUnroller stack_unroller(L);
5843
5844         INodeDefManager *ndef = get_server(L)->ndef();
5845
5846         // Push callback function on stack
5847         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_construct"))
5848                 return;
5849
5850         // Call function
5851         push_v3s16(L, p);
5852         if(lua_pcall(L, 1, 0, 0))
5853                 script_error(L, "error: %s", lua_tostring(L, -1));
5854 }
5855
5856 void scriptapi_node_on_destruct(lua_State *L, v3s16 p, MapNode node)
5857 {
5858         realitycheck(L);
5859         assert(lua_checkstack(L, 20));
5860         StackUnroller stack_unroller(L);
5861
5862         INodeDefManager *ndef = get_server(L)->ndef();
5863
5864         // Push callback function on stack
5865         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_destruct"))
5866                 return;
5867
5868         // Call function
5869         push_v3s16(L, p);
5870         if(lua_pcall(L, 1, 0, 0))
5871                 script_error(L, "error: %s", lua_tostring(L, -1));
5872 }
5873
5874 void scriptapi_node_after_destruct(lua_State *L, v3s16 p, MapNode node)
5875 {
5876         realitycheck(L);
5877         assert(lua_checkstack(L, 20));
5878         StackUnroller stack_unroller(L);
5879
5880         INodeDefManager *ndef = get_server(L)->ndef();
5881
5882         // Push callback function on stack
5883         if(!get_item_callback(L, ndef->get(node).name.c_str(), "after_destruct"))
5884                 return;
5885
5886         // Call function
5887         push_v3s16(L, p);
5888         pushnode(L, node, ndef);
5889         if(lua_pcall(L, 2, 0, 0))
5890                 script_error(L, "error: %s", lua_tostring(L, -1));
5891 }
5892
5893 bool scriptapi_node_on_timer(lua_State *L, v3s16 p, MapNode node, f32 dtime)
5894 {
5895         realitycheck(L);
5896         assert(lua_checkstack(L, 20));
5897         StackUnroller stack_unroller(L);
5898
5899         INodeDefManager *ndef = get_server(L)->ndef();
5900
5901         // Push callback function on stack
5902         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_timer"))
5903                 return false;
5904
5905         // Call function
5906         push_v3s16(L, p);
5907         lua_pushnumber(L,dtime);
5908         if(lua_pcall(L, 2, 1, 0))
5909                 script_error(L, "error: %s", lua_tostring(L, -1));
5910         if(lua_isboolean(L,-1) && lua_toboolean(L,-1) == true)
5911                 return true;
5912         
5913         return false;
5914 }
5915
5916 void scriptapi_node_on_receive_fields(lua_State *L, v3s16 p,
5917                 const std::string &formname,
5918                 const std::map<std::string, std::string> &fields,
5919                 ServerActiveObject *sender)
5920 {
5921         realitycheck(L);
5922         assert(lua_checkstack(L, 20));
5923         StackUnroller stack_unroller(L);
5924
5925         INodeDefManager *ndef = get_server(L)->ndef();
5926         
5927         // If node doesn't exist, we don't know what callback to call
5928         MapNode node = get_env(L)->getMap().getNodeNoEx(p);
5929         if(node.getContent() == CONTENT_IGNORE)
5930                 return;
5931
5932         // Push callback function on stack
5933         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_receive_fields"))
5934                 return;
5935
5936         // Call function
5937         // param 1
5938         push_v3s16(L, p);
5939         // param 2
5940         lua_pushstring(L, formname.c_str());
5941         // param 3
5942         lua_newtable(L);
5943         for(std::map<std::string, std::string>::const_iterator
5944                         i = fields.begin(); i != fields.end(); i++){
5945                 const std::string &name = i->first;
5946                 const std::string &value = i->second;
5947                 lua_pushstring(L, name.c_str());
5948                 lua_pushlstring(L, value.c_str(), value.size());
5949                 lua_settable(L, -3);
5950         }
5951         // param 4
5952         objectref_get_or_create(L, sender);
5953         if(lua_pcall(L, 4, 0, 0))
5954                 script_error(L, "error: %s", lua_tostring(L, -1));
5955 }
5956
5957 /*
5958         Node metadata inventory callbacks
5959 */
5960
5961 // Return number of accepted items to be moved
5962 int scriptapi_nodemeta_inventory_allow_move(lua_State *L, v3s16 p,
5963                 const std::string &from_list, int from_index,
5964                 const std::string &to_list, int to_index,
5965                 int count, ServerActiveObject *player)
5966 {
5967         realitycheck(L);
5968         assert(lua_checkstack(L, 20));
5969         StackUnroller stack_unroller(L);
5970
5971         INodeDefManager *ndef = get_server(L)->ndef();
5972
5973         // If node doesn't exist, we don't know what callback to call
5974         MapNode node = get_env(L)->getMap().getNodeNoEx(p);
5975         if(node.getContent() == CONTENT_IGNORE)
5976                 return 0;
5977
5978         // Push callback function on stack
5979         if(!get_item_callback(L, ndef->get(node).name.c_str(),
5980                         "allow_metadata_inventory_move"))
5981                 return count;
5982
5983         // function(pos, from_list, from_index, to_list, to_index, count, player)
5984         // pos
5985         push_v3s16(L, p);
5986         // from_list
5987         lua_pushstring(L, from_list.c_str());
5988         // from_index
5989         lua_pushinteger(L, from_index + 1);
5990         // to_list
5991         lua_pushstring(L, to_list.c_str());
5992         // to_index
5993         lua_pushinteger(L, to_index + 1);
5994         // count
5995         lua_pushinteger(L, count);
5996         // player
5997         objectref_get_or_create(L, player);
5998         if(lua_pcall(L, 7, 1, 0))
5999                 script_error(L, "error: %s", lua_tostring(L, -1));
6000         if(!lua_isnumber(L, -1))
6001                 throw LuaError(L, "allow_metadata_inventory_move should return a number");
6002         return luaL_checkinteger(L, -1);
6003 }
6004
6005 // Return number of accepted items to be put
6006 int scriptapi_nodemeta_inventory_allow_put(lua_State *L, v3s16 p,
6007                 const std::string &listname, int index, ItemStack &stack,
6008                 ServerActiveObject *player)
6009 {
6010         realitycheck(L);
6011         assert(lua_checkstack(L, 20));
6012         StackUnroller stack_unroller(L);
6013
6014         INodeDefManager *ndef = get_server(L)->ndef();
6015
6016         // If node doesn't exist, we don't know what callback to call
6017         MapNode node = get_env(L)->getMap().getNodeNoEx(p);
6018         if(node.getContent() == CONTENT_IGNORE)
6019                 return 0;
6020
6021         // Push callback function on stack
6022         if(!get_item_callback(L, ndef->get(node).name.c_str(),
6023                         "allow_metadata_inventory_put"))
6024                 return stack.count;
6025
6026         // Call function(pos, listname, index, stack, player)
6027         // pos
6028         push_v3s16(L, p);
6029         // listname
6030         lua_pushstring(L, listname.c_str());
6031         // index
6032         lua_pushinteger(L, index + 1);
6033         // stack
6034         LuaItemStack::create(L, stack);
6035         // player
6036         objectref_get_or_create(L, player);
6037         if(lua_pcall(L, 5, 1, 0))
6038                 script_error(L, "error: %s", lua_tostring(L, -1));
6039         if(!lua_isnumber(L, -1))
6040                 throw LuaError(L, "allow_metadata_inventory_put should return a number");
6041         return luaL_checkinteger(L, -1);
6042 }
6043
6044 // Return number of accepted items to be taken
6045 int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p,
6046                 const std::string &listname, int index, ItemStack &stack,
6047                 ServerActiveObject *player)
6048 {
6049         realitycheck(L);
6050         assert(lua_checkstack(L, 20));
6051         StackUnroller stack_unroller(L);
6052
6053         INodeDefManager *ndef = get_server(L)->ndef();
6054
6055         // If node doesn't exist, we don't know what callback to call
6056         MapNode node = get_env(L)->getMap().getNodeNoEx(p);
6057         if(node.getContent() == CONTENT_IGNORE)
6058                 return 0;
6059
6060         // Push callback function on stack
6061         if(!get_item_callback(L, ndef->get(node).name.c_str(),
6062                         "allow_metadata_inventory_take"))
6063                 return stack.count;
6064
6065         // Call function(pos, listname, index, count, player)
6066         // pos
6067         push_v3s16(L, p);
6068         // listname
6069         lua_pushstring(L, listname.c_str());
6070         // index
6071         lua_pushinteger(L, index + 1);
6072         // stack
6073         LuaItemStack::create(L, stack);
6074         // player
6075         objectref_get_or_create(L, player);
6076         if(lua_pcall(L, 5, 1, 0))
6077                 script_error(L, "error: %s", lua_tostring(L, -1));
6078         if(!lua_isnumber(L, -1))
6079                 throw LuaError(L, "allow_metadata_inventory_take should return a number");
6080         return luaL_checkinteger(L, -1);
6081 }
6082
6083 // Report moved items
6084 void scriptapi_nodemeta_inventory_on_move(lua_State *L, v3s16 p,
6085                 const std::string &from_list, int from_index,
6086                 const std::string &to_list, int to_index,
6087                 int count, ServerActiveObject *player)
6088 {
6089         realitycheck(L);
6090         assert(lua_checkstack(L, 20));
6091         StackUnroller stack_unroller(L);
6092
6093         INodeDefManager *ndef = get_server(L)->ndef();
6094
6095         // If node doesn't exist, we don't know what callback to call
6096         MapNode node = get_env(L)->getMap().getNodeNoEx(p);
6097         if(node.getContent() == CONTENT_IGNORE)
6098                 return;
6099
6100         // Push callback function on stack
6101         if(!get_item_callback(L, ndef->get(node).name.c_str(),
6102                         "on_metadata_inventory_move"))
6103                 return;
6104
6105         // function(pos, from_list, from_index, to_list, to_index, count, player)
6106         // pos
6107         push_v3s16(L, p);
6108         // from_list
6109         lua_pushstring(L, from_list.c_str());
6110         // from_index
6111         lua_pushinteger(L, from_index + 1);
6112         // to_list
6113         lua_pushstring(L, to_list.c_str());
6114         // to_index
6115         lua_pushinteger(L, to_index + 1);
6116         // count
6117         lua_pushinteger(L, count);
6118         // player
6119         objectref_get_or_create(L, player);
6120         if(lua_pcall(L, 7, 0, 0))
6121                 script_error(L, "error: %s", lua_tostring(L, -1));
6122 }
6123
6124 // Report put items
6125 void scriptapi_nodemeta_inventory_on_put(lua_State *L, v3s16 p,
6126                 const std::string &listname, int index, ItemStack &stack,
6127                 ServerActiveObject *player)
6128 {
6129         realitycheck(L);
6130         assert(lua_checkstack(L, 20));
6131         StackUnroller stack_unroller(L);
6132
6133         INodeDefManager *ndef = get_server(L)->ndef();
6134
6135         // If node doesn't exist, we don't know what callback to call
6136         MapNode node = get_env(L)->getMap().getNodeNoEx(p);
6137         if(node.getContent() == CONTENT_IGNORE)
6138                 return;
6139
6140         // Push callback function on stack
6141         if(!get_item_callback(L, ndef->get(node).name.c_str(),
6142                         "on_metadata_inventory_put"))
6143                 return;
6144
6145         // Call function(pos, listname, index, stack, player)
6146         // pos
6147         push_v3s16(L, p);
6148         // listname
6149         lua_pushstring(L, listname.c_str());
6150         // index
6151         lua_pushinteger(L, index + 1);
6152         // stack
6153         LuaItemStack::create(L, stack);
6154         // player
6155         objectref_get_or_create(L, player);
6156         if(lua_pcall(L, 5, 0, 0))
6157                 script_error(L, "error: %s", lua_tostring(L, -1));
6158 }
6159
6160 // Report taken items
6161 void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p,
6162                 const std::string &listname, int index, ItemStack &stack,
6163                 ServerActiveObject *player)
6164 {
6165         realitycheck(L);
6166         assert(lua_checkstack(L, 20));
6167         StackUnroller stack_unroller(L);
6168
6169         INodeDefManager *ndef = get_server(L)->ndef();
6170
6171         // If node doesn't exist, we don't know what callback to call
6172         MapNode node = get_env(L)->getMap().getNodeNoEx(p);
6173         if(node.getContent() == CONTENT_IGNORE)
6174                 return;
6175
6176         // Push callback function on stack
6177         if(!get_item_callback(L, ndef->get(node).name.c_str(),
6178                         "on_metadata_inventory_take"))
6179                 return;
6180
6181         // Call function(pos, listname, index, stack, player)
6182         // pos
6183         push_v3s16(L, p);
6184         // listname
6185         lua_pushstring(L, listname.c_str());
6186         // index
6187         lua_pushinteger(L, index + 1);
6188         // stack
6189         LuaItemStack::create(L, stack);
6190         // player
6191         objectref_get_or_create(L, player);
6192         if(lua_pcall(L, 5, 0, 0))
6193                 script_error(L, "error: %s", lua_tostring(L, -1));
6194 }
6195
6196 /*
6197         Detached inventory callbacks
6198 */
6199
6200 // Retrieves minetest.detached_inventories[name][callbackname]
6201 // If that is nil or on error, return false and stack is unchanged
6202 // If that is a function, returns true and pushes the
6203 // function onto the stack
6204 static bool get_detached_inventory_callback(lua_State *L,
6205                 const std::string &name, const char *callbackname)
6206 {
6207         lua_getglobal(L, "minetest");
6208         lua_getfield(L, -1, "detached_inventories");
6209         lua_remove(L, -2);
6210         luaL_checktype(L, -1, LUA_TTABLE);
6211         lua_getfield(L, -1, name.c_str());
6212         lua_remove(L, -2);
6213         // Should be a table
6214         if(lua_type(L, -1) != LUA_TTABLE)
6215         {
6216                 errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
6217                 lua_pop(L, 1);
6218                 return false;
6219         }
6220         lua_getfield(L, -1, callbackname);
6221         lua_remove(L, -2);
6222         // Should be a function or nil
6223         if(lua_type(L, -1) == LUA_TFUNCTION)
6224         {
6225                 return true;
6226         }
6227         else if(lua_isnil(L, -1))
6228         {
6229                 lua_pop(L, 1);
6230                 return false;
6231         }
6232         else
6233         {
6234                 errorstream<<"Detached inventory \""<<name<<"\" callback \""
6235                         <<callbackname<<"\" is not a function"<<std::endl;
6236                 lua_pop(L, 1);
6237                 return false;
6238         }
6239 }
6240
6241 // Return number of accepted items to be moved
6242 int scriptapi_detached_inventory_allow_move(lua_State *L,
6243                 const std::string &name,
6244                 const std::string &from_list, int from_index,
6245                 const std::string &to_list, int to_index,
6246                 int count, ServerActiveObject *player)
6247 {
6248         realitycheck(L);
6249         assert(lua_checkstack(L, 20));
6250         StackUnroller stack_unroller(L);
6251
6252         // Push callback function on stack
6253         if(!get_detached_inventory_callback(L, name, "allow_move"))
6254                 return count;
6255
6256         // function(inv, from_list, from_index, to_list, to_index, count, player)
6257         // inv
6258         InventoryLocation loc;
6259         loc.setDetached(name);
6260         InvRef::create(L, loc);
6261         // from_list
6262         lua_pushstring(L, from_list.c_str());
6263         // from_index
6264         lua_pushinteger(L, from_index + 1);
6265         // to_list
6266         lua_pushstring(L, to_list.c_str());
6267         // to_index
6268         lua_pushinteger(L, to_index + 1);
6269         // count
6270         lua_pushinteger(L, count);
6271         // player
6272         objectref_get_or_create(L, player);
6273         if(lua_pcall(L, 7, 1, 0))
6274                 script_error(L, "error: %s", lua_tostring(L, -1));
6275         if(!lua_isnumber(L, -1))
6276                 throw LuaError(L, "allow_move should return a number");
6277         return luaL_checkinteger(L, -1);
6278 }
6279
6280 // Return number of accepted items to be put
6281 int scriptapi_detached_inventory_allow_put(lua_State *L,
6282                 const std::string &name,
6283                 const std::string &listname, int index, ItemStack &stack,
6284                 ServerActiveObject *player)
6285 {
6286         realitycheck(L);
6287         assert(lua_checkstack(L, 20));
6288         StackUnroller stack_unroller(L);
6289
6290         // Push callback function on stack
6291         if(!get_detached_inventory_callback(L, name, "allow_put"))
6292                 return stack.count; // All will be accepted
6293
6294         // Call function(inv, listname, index, stack, player)
6295         // inv
6296         InventoryLocation loc;
6297         loc.setDetached(name);
6298         InvRef::create(L, loc);
6299         // listname
6300         lua_pushstring(L, listname.c_str());
6301         // index
6302         lua_pushinteger(L, index + 1);
6303         // stack
6304         LuaItemStack::create(L, stack);
6305         // player
6306         objectref_get_or_create(L, player);
6307         if(lua_pcall(L, 5, 1, 0))
6308                 script_error(L, "error: %s", lua_tostring(L, -1));
6309         if(!lua_isnumber(L, -1))
6310                 throw LuaError(L, "allow_put should return a number");
6311         return luaL_checkinteger(L, -1);
6312 }
6313
6314 // Return number of accepted items to be taken
6315 int scriptapi_detached_inventory_allow_take(lua_State *L,
6316                 const std::string &name,
6317                 const std::string &listname, int index, ItemStack &stack,
6318                 ServerActiveObject *player)
6319 {
6320         realitycheck(L);
6321         assert(lua_checkstack(L, 20));
6322         StackUnroller stack_unroller(L);
6323
6324         // Push callback function on stack
6325         if(!get_detached_inventory_callback(L, name, "allow_take"))
6326                 return stack.count; // All will be accepted
6327
6328         // Call function(inv, listname, index, stack, player)
6329         // inv
6330         InventoryLocation loc;
6331         loc.setDetached(name);
6332         InvRef::create(L, loc);
6333         // listname
6334         lua_pushstring(L, listname.c_str());
6335         // index
6336         lua_pushinteger(L, index + 1);
6337         // stack
6338         LuaItemStack::create(L, stack);
6339         // player
6340         objectref_get_or_create(L, player);
6341         if(lua_pcall(L, 5, 1, 0))
6342                 script_error(L, "error: %s", lua_tostring(L, -1));
6343         if(!lua_isnumber(L, -1))
6344                 throw LuaError(L, "allow_take should return a number");
6345         return luaL_checkinteger(L, -1);
6346 }
6347
6348 // Report moved items
6349 void scriptapi_detached_inventory_on_move(lua_State *L,
6350                 const std::string &name,
6351                 const std::string &from_list, int from_index,
6352                 const std::string &to_list, int to_index,
6353                 int count, ServerActiveObject *player)
6354 {
6355         realitycheck(L);
6356         assert(lua_checkstack(L, 20));
6357         StackUnroller stack_unroller(L);
6358
6359         // Push callback function on stack
6360         if(!get_detached_inventory_callback(L, name, "on_move"))
6361                 return;
6362
6363         // function(inv, from_list, from_index, to_list, to_index, count, player)
6364         // inv
6365         InventoryLocation loc;
6366         loc.setDetached(name);
6367         InvRef::create(L, loc);
6368         // from_list
6369         lua_pushstring(L, from_list.c_str());
6370         // from_index
6371         lua_pushinteger(L, from_index + 1);
6372         // to_list
6373         lua_pushstring(L, to_list.c_str());
6374         // to_index
6375         lua_pushinteger(L, to_index + 1);
6376         // count
6377         lua_pushinteger(L, count);
6378         // player
6379         objectref_get_or_create(L, player);
6380         if(lua_pcall(L, 7, 0, 0))
6381                 script_error(L, "error: %s", lua_tostring(L, -1));
6382 }
6383
6384 // Report put items
6385 void scriptapi_detached_inventory_on_put(lua_State *L,
6386                 const std::string &name,
6387                 const std::string &listname, int index, ItemStack &stack,
6388                 ServerActiveObject *player)
6389 {
6390         realitycheck(L);
6391         assert(lua_checkstack(L, 20));
6392         StackUnroller stack_unroller(L);
6393
6394         // Push callback function on stack
6395         if(!get_detached_inventory_callback(L, name, "on_put"))
6396                 return;
6397
6398         // Call function(inv, listname, index, stack, player)
6399         // inv
6400         InventoryLocation loc;
6401         loc.setDetached(name);
6402         InvRef::create(L, loc);
6403         // listname
6404         lua_pushstring(L, listname.c_str());
6405         // index
6406         lua_pushinteger(L, index + 1);
6407         // stack
6408         LuaItemStack::create(L, stack);
6409         // player
6410         objectref_get_or_create(L, player);
6411         if(lua_pcall(L, 5, 0, 0))
6412                 script_error(L, "error: %s", lua_tostring(L, -1));
6413 }
6414
6415 // Report taken items
6416 void scriptapi_detached_inventory_on_take(lua_State *L,
6417                 const std::string &name,
6418                 const std::string &listname, int index, ItemStack &stack,
6419                 ServerActiveObject *player)
6420 {
6421         realitycheck(L);
6422         assert(lua_checkstack(L, 20));
6423         StackUnroller stack_unroller(L);
6424
6425         // Push callback function on stack
6426         if(!get_detached_inventory_callback(L, name, "on_take"))
6427                 return;
6428
6429         // Call function(inv, listname, index, stack, player)
6430         // inv
6431         InventoryLocation loc;
6432         loc.setDetached(name);
6433         InvRef::create(L, loc);
6434         // listname
6435         lua_pushstring(L, listname.c_str());
6436         // index
6437         lua_pushinteger(L, index + 1);
6438         // stack
6439         LuaItemStack::create(L, stack);
6440         // player
6441         objectref_get_or_create(L, player);
6442         if(lua_pcall(L, 5, 0, 0))
6443                 script_error(L, "error: %s", lua_tostring(L, -1));
6444 }
6445
6446 /*
6447         environment
6448 */
6449
6450 void scriptapi_environment_step(lua_State *L, float dtime)
6451 {
6452         realitycheck(L);
6453         assert(lua_checkstack(L, 20));
6454         //infostream<<"scriptapi_environment_step"<<std::endl;
6455         StackUnroller stack_unroller(L);
6456
6457         // Get minetest.registered_globalsteps
6458         lua_getglobal(L, "minetest");
6459         lua_getfield(L, -1, "registered_globalsteps");
6460         // Call callbacks
6461         lua_pushnumber(L, dtime);
6462         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
6463 }
6464
6465 void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp,
6466                 u32 blockseed)
6467 {
6468         realitycheck(L);
6469         assert(lua_checkstack(L, 20));
6470         //infostream<<"scriptapi_environment_on_generated"<<std::endl;
6471         StackUnroller stack_unroller(L);
6472
6473         // Get minetest.registered_on_generateds
6474         lua_getglobal(L, "minetest");
6475         lua_getfield(L, -1, "registered_on_generateds");
6476         // Call callbacks
6477         push_v3s16(L, minp);
6478         push_v3s16(L, maxp);
6479         lua_pushnumber(L, blockseed);
6480         scriptapi_run_callbacks(L, 3, RUN_CALLBACKS_MODE_FIRST);
6481 }
6482
6483 /*
6484         luaentity
6485 */
6486
6487 bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name)
6488 {
6489         realitycheck(L);
6490         assert(lua_checkstack(L, 20));
6491         verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
6492                         <<name<<"\""<<std::endl;
6493         StackUnroller stack_unroller(L);
6494         
6495         // Get minetest.registered_entities[name]
6496         lua_getglobal(L, "minetest");
6497         lua_getfield(L, -1, "registered_entities");
6498         luaL_checktype(L, -1, LUA_TTABLE);
6499         lua_pushstring(L, name);
6500         lua_gettable(L, -2);
6501         // Should be a table, which we will use as a prototype
6502         //luaL_checktype(L, -1, LUA_TTABLE);
6503         if(lua_type(L, -1) != LUA_TTABLE){
6504                 errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
6505                 return false;
6506         }
6507         int prototype_table = lua_gettop(L);
6508         //dump2(L, "prototype_table");
6509         
6510         // Create entity object
6511         lua_newtable(L);
6512         int object = lua_gettop(L);
6513
6514         // Set object metatable
6515         lua_pushvalue(L, prototype_table);
6516         lua_setmetatable(L, -2);
6517         
6518         // Add object reference
6519         // This should be userdata with metatable ObjectRef
6520         objectref_get(L, id);
6521         luaL_checktype(L, -1, LUA_TUSERDATA);
6522         if(!luaL_checkudata(L, -1, "ObjectRef"))
6523                 luaL_typerror(L, -1, "ObjectRef");
6524         lua_setfield(L, -2, "object");
6525
6526         // minetest.luaentities[id] = object
6527         lua_getglobal(L, "minetest");
6528         lua_getfield(L, -1, "luaentities");
6529         luaL_checktype(L, -1, LUA_TTABLE);
6530         lua_pushnumber(L, id); // Push id
6531         lua_pushvalue(L, object); // Copy object to top of stack
6532         lua_settable(L, -3);
6533         
6534         return true;
6535 }
6536
6537 void scriptapi_luaentity_activate(lua_State *L, u16 id,
6538                 const std::string &staticdata, u32 dtime_s)
6539 {
6540         realitycheck(L);
6541         assert(lua_checkstack(L, 20));
6542         verbosestream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
6543         StackUnroller stack_unroller(L);
6544         
6545         // Get minetest.luaentities[id]
6546         luaentity_get(L, id);
6547         int object = lua_gettop(L);
6548         
6549         // Get on_activate function
6550         lua_pushvalue(L, object);
6551         lua_getfield(L, -1, "on_activate");
6552         if(!lua_isnil(L, -1)){
6553                 luaL_checktype(L, -1, LUA_TFUNCTION);
6554                 lua_pushvalue(L, object); // self
6555                 lua_pushlstring(L, staticdata.c_str(), staticdata.size());
6556                 lua_pushinteger(L, dtime_s);
6557                 // Call with 3 arguments, 0 results
6558                 if(lua_pcall(L, 3, 0, 0))
6559                         script_error(L, "error running function on_activate: %s\n",
6560                                         lua_tostring(L, -1));
6561         }
6562 }
6563
6564 void scriptapi_luaentity_rm(lua_State *L, u16 id)
6565 {
6566         realitycheck(L);
6567         assert(lua_checkstack(L, 20));
6568         verbosestream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
6569
6570         // Get minetest.luaentities table
6571         lua_getglobal(L, "minetest");
6572         lua_getfield(L, -1, "luaentities");
6573         luaL_checktype(L, -1, LUA_TTABLE);
6574         int objectstable = lua_gettop(L);
6575         
6576         // Set luaentities[id] = nil
6577         lua_pushnumber(L, id); // Push id
6578         lua_pushnil(L);
6579         lua_settable(L, objectstable);
6580         
6581         lua_pop(L, 2); // pop luaentities, minetest
6582 }
6583
6584 std::string scriptapi_luaentity_get_staticdata(lua_State *L, u16 id)
6585 {
6586         realitycheck(L);
6587         assert(lua_checkstack(L, 20));
6588         //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
6589         StackUnroller stack_unroller(L);
6590
6591         // Get minetest.luaentities[id]
6592         luaentity_get(L, id);
6593         int object = lua_gettop(L);
6594         
6595         // Get get_staticdata function
6596         lua_pushvalue(L, object);
6597         lua_getfield(L, -1, "get_staticdata");
6598         if(lua_isnil(L, -1))
6599                 return "";
6600         
6601         luaL_checktype(L, -1, LUA_TFUNCTION);
6602         lua_pushvalue(L, object); // self
6603         // Call with 1 arguments, 1 results
6604         if(lua_pcall(L, 1, 1, 0))
6605                 script_error(L, "error running function get_staticdata: %s\n",
6606                                 lua_tostring(L, -1));
6607         
6608         size_t len=0;
6609         const char *s = lua_tolstring(L, -1, &len);
6610         return std::string(s, len);
6611 }
6612
6613 void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
6614                 ObjectProperties *prop)
6615 {
6616         realitycheck(L);
6617         assert(lua_checkstack(L, 20));
6618         //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
6619         StackUnroller stack_unroller(L);
6620
6621         // Get minetest.luaentities[id]
6622         luaentity_get(L, id);
6623         //int object = lua_gettop(L);
6624
6625         // Set default values that differ from ObjectProperties defaults
6626         prop->hp_max = 10;
6627         
6628         /* Read stuff */
6629         
6630         prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
6631
6632         getboolfield(L, -1, "physical", prop->physical);
6633
6634         getfloatfield(L, -1, "weight", prop->weight);
6635
6636         lua_getfield(L, -1, "collisionbox");
6637         if(lua_istable(L, -1))
6638                 prop->collisionbox = read_aabb3f(L, -1, 1.0);
6639         lua_pop(L, 1);
6640
6641         getstringfield(L, -1, "visual", prop->visual);
6642
6643         getstringfield(L, -1, "mesh", prop->mesh);
6644         
6645         // Deprecated: read object properties directly
6646         read_object_properties(L, -1, prop);
6647         
6648         // Read initial_properties
6649         lua_getfield(L, -1, "initial_properties");
6650         read_object_properties(L, -1, prop);
6651         lua_pop(L, 1);
6652 }
6653
6654 void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime)
6655 {
6656         realitycheck(L);
6657         assert(lua_checkstack(L, 20));
6658         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
6659         StackUnroller stack_unroller(L);
6660
6661         // Get minetest.luaentities[id]
6662         luaentity_get(L, id);
6663         int object = lua_gettop(L);
6664         // State: object is at top of stack
6665         // Get step function
6666         lua_getfield(L, -1, "on_step");
6667         if(lua_isnil(L, -1))
6668                 return;
6669         luaL_checktype(L, -1, LUA_TFUNCTION);
6670         lua_pushvalue(L, object); // self
6671         lua_pushnumber(L, dtime); // dtime
6672         // Call with 2 arguments, 0 results
6673         if(lua_pcall(L, 2, 0, 0))
6674                 script_error(L, "error running function 'on_step': %s\n", lua_tostring(L, -1));
6675 }
6676
6677 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
6678 //                       tool_capabilities, direction)
6679 void scriptapi_luaentity_punch(lua_State *L, u16 id,
6680                 ServerActiveObject *puncher, float time_from_last_punch,
6681                 const ToolCapabilities *toolcap, v3f dir)
6682 {
6683         realitycheck(L);
6684         assert(lua_checkstack(L, 20));
6685         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
6686         StackUnroller stack_unroller(L);
6687
6688         // Get minetest.luaentities[id]
6689         luaentity_get(L, id);
6690         int object = lua_gettop(L);
6691         // State: object is at top of stack
6692         // Get function
6693         lua_getfield(L, -1, "on_punch");
6694         if(lua_isnil(L, -1))
6695                 return;
6696         luaL_checktype(L, -1, LUA_TFUNCTION);
6697         lua_pushvalue(L, object); // self
6698         objectref_get_or_create(L, puncher); // Clicker reference
6699         lua_pushnumber(L, time_from_last_punch);
6700         push_tool_capabilities(L, *toolcap);
6701         push_v3f(L, dir);
6702         // Call with 5 arguments, 0 results
6703         if(lua_pcall(L, 5, 0, 0))
6704                 script_error(L, "error running function 'on_punch': %s\n", lua_tostring(L, -1));
6705 }
6706
6707 // Calls entity:on_rightclick(ObjectRef clicker)
6708 void scriptapi_luaentity_rightclick(lua_State *L, u16 id,
6709                 ServerActiveObject *clicker)
6710 {
6711         realitycheck(L);
6712         assert(lua_checkstack(L, 20));
6713         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
6714         StackUnroller stack_unroller(L);
6715
6716         // Get minetest.luaentities[id]
6717         luaentity_get(L, id);
6718         int object = lua_gettop(L);
6719         // State: object is at top of stack
6720         // Get function
6721         lua_getfield(L, -1, "on_rightclick");
6722         if(lua_isnil(L, -1))
6723                 return;
6724         luaL_checktype(L, -1, LUA_TFUNCTION);
6725         lua_pushvalue(L, object); // self
6726         objectref_get_or_create(L, clicker); // Clicker reference
6727         // Call with 2 arguments, 0 results
6728         if(lua_pcall(L, 2, 0, 0))
6729                 script_error(L, "error running function 'on_rightclick': %s\n", lua_tostring(L, -1));
6730 }
6731