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