Fix itemdef drop on NULL texture
[oweals/minetest.git] / src / scriptapi.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "scriptapi.h"
21
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 "settings.h" // For accessing g_settings
31 #include "main.h" // For g_settings
32 #include "biome.h"
33 #include "emerge.h"
34 #include "script.h"
35 #include "rollback.h"
36
37 #include "scriptapi_types.h"
38 #include "scriptapi_env.h"
39 #include "scriptapi_nodetimer.h"
40 #include "scriptapi_inventory.h"
41 #include "scriptapi_nodemeta.h"
42 #include "scriptapi_object.h"
43 #include "scriptapi_noise.h"
44 #include "scriptapi_common.h"
45 #include "scriptapi_item.h"
46 #include "scriptapi_content.h"
47 #include "scriptapi_craft.h"
48 #include "scriptapi_particles.h"
49
50 /*****************************************************************************/
51 /* Mod related                                                               */
52 /*****************************************************************************/
53
54 class ModNameStorer
55 {
56 private:
57         lua_State *L;
58 public:
59         ModNameStorer(lua_State *L_, const std::string modname):
60                 L(L_)
61         {
62                 // Store current modname in registry
63                 lua_pushstring(L, modname.c_str());
64                 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
65         }
66         ~ModNameStorer()
67         {
68                 // Clear current modname in registry
69                 lua_pushnil(L);
70                 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
71         }
72 };
73
74 bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
75                 const std::string &modname)
76 {
77         ModNameStorer modnamestorer(L, modname);
78
79         if(!string_allowed(modname, "abcdefghijklmnopqrstuvwxyz"
80                         "0123456789_")){
81                 errorstream<<"Error loading mod \""<<modname
82                                 <<"\": modname does not follow naming conventions: "
83                                 <<"Only chararacters [a-z0-9_] are allowed."<<std::endl;
84                 return false;
85         }
86
87         bool success = false;
88
89         try{
90                 success = script_load(L, scriptpath.c_str());
91         }
92         catch(LuaError &e){
93                 errorstream<<"Error loading mod \""<<modname
94                                 <<"\": "<<e.what()<<std::endl;
95         }
96
97         return success;
98 }
99
100
101 /*****************************************************************************/
102 /* Auth                                                                      */
103 /*****************************************************************************/
104
105 /*
106         Privileges
107 */
108 static void read_privileges(lua_State *L, int index,
109                 std::set<std::string> &result)
110 {
111         result.clear();
112         lua_pushnil(L);
113         if(index < 0)
114                 index -= 1;
115         while(lua_next(L, index) != 0){
116                 // key at index -2 and value at index -1
117                 std::string key = luaL_checkstring(L, -2);
118                 bool value = lua_toboolean(L, -1);
119                 if(value)
120                         result.insert(key);
121                 // removes value, keeps key for next iteration
122                 lua_pop(L, 1);
123         }
124 }
125
126 static void get_auth_handler(lua_State *L)
127 {
128         lua_getglobal(L, "minetest");
129         lua_getfield(L, -1, "registered_auth_handler");
130         if(lua_isnil(L, -1)){
131                 lua_pop(L, 1);
132                 lua_getfield(L, -1, "builtin_auth_handler");
133         }
134         if(lua_type(L, -1) != LUA_TTABLE)
135                 throw LuaError(L, "Authentication handler table not valid");
136 }
137
138 bool scriptapi_get_auth(lua_State *L, const std::string &playername,
139                 std::string *dst_password, std::set<std::string> *dst_privs)
140 {
141         realitycheck(L);
142         assert(lua_checkstack(L, 20));
143         StackUnroller stack_unroller(L);
144
145         get_auth_handler(L);
146         lua_getfield(L, -1, "get_auth");
147         if(lua_type(L, -1) != LUA_TFUNCTION)
148                 throw LuaError(L, "Authentication handler missing get_auth");
149         lua_pushstring(L, playername.c_str());
150         if(lua_pcall(L, 1, 1, 0))
151                 script_error(L, "error: %s", lua_tostring(L, -1));
152
153         // nil = login not allowed
154         if(lua_isnil(L, -1))
155                 return false;
156         luaL_checktype(L, -1, LUA_TTABLE);
157
158         std::string password;
159         bool found = getstringfield(L, -1, "password", password);
160         if(!found)
161                 throw LuaError(L, "Authentication handler didn't return password");
162         if(dst_password)
163                 *dst_password = password;
164
165         lua_getfield(L, -1, "privileges");
166         if(!lua_istable(L, -1))
167                 throw LuaError(L,
168                                 "Authentication handler didn't return privilege table");
169         if(dst_privs)
170                 read_privileges(L, -1, *dst_privs);
171         lua_pop(L, 1);
172
173         return true;
174 }
175
176 void scriptapi_create_auth(lua_State *L, const std::string &playername,
177                 const std::string &password)
178 {
179         realitycheck(L);
180         assert(lua_checkstack(L, 20));
181         StackUnroller stack_unroller(L);
182
183         get_auth_handler(L);
184         lua_getfield(L, -1, "create_auth");
185         if(lua_type(L, -1) != LUA_TFUNCTION)
186                 throw LuaError(L, "Authentication handler missing create_auth");
187         lua_pushstring(L, playername.c_str());
188         lua_pushstring(L, password.c_str());
189         if(lua_pcall(L, 2, 0, 0))
190                 script_error(L, "error: %s", lua_tostring(L, -1));
191 }
192
193 bool scriptapi_set_password(lua_State *L, const std::string &playername,
194                 const std::string &password)
195 {
196         realitycheck(L);
197         assert(lua_checkstack(L, 20));
198         StackUnroller stack_unroller(L);
199
200         get_auth_handler(L);
201         lua_getfield(L, -1, "set_password");
202         if(lua_type(L, -1) != LUA_TFUNCTION)
203                 throw LuaError(L, "Authentication handler missing set_password");
204         lua_pushstring(L, playername.c_str());
205         lua_pushstring(L, password.c_str());
206         if(lua_pcall(L, 2, 1, 0))
207                 script_error(L, "error: %s", lua_tostring(L, -1));
208         return lua_toboolean(L, -1);
209 }
210
211 /*****************************************************************************/
212 /* Misc                                                                      */
213 /*****************************************************************************/
214 /*
215         Groups
216 */
217 void read_groups(lua_State *L, int index,
218                 std::map<std::string, int> &result)
219 {
220         if (!lua_istable(L,index))
221                 return;
222         result.clear();
223         lua_pushnil(L);
224         if(index < 0)
225                 index -= 1;
226         while(lua_next(L, index) != 0){
227                 // key at index -2 and value at index -1
228                 std::string name = luaL_checkstring(L, -2);
229                 int rating = luaL_checkinteger(L, -1);
230                 result[name] = rating;
231                 // removes value, keeps key for next iteration
232                 lua_pop(L, 1);
233         }
234 }
235
236 struct EnumString es_BiomeTerrainType[] =
237 {
238         {BIOME_TERRAIN_NORMAL, "normal"},
239         {BIOME_TERRAIN_LIQUID, "liquid"},
240         {BIOME_TERRAIN_NETHER, "nether"},
241         {BIOME_TERRAIN_AETHER, "aether"},
242         {BIOME_TERRAIN_FLAT,   "flat"},
243         {0, NULL},
244 };
245
246 struct EnumString es_OreType[] =
247 {
248         {ORE_SCATTER,  "scatter"},
249         {ORE_SHEET,    "sheet"},
250         {ORE_CLAYLIKE, "claylike"},
251         {0, NULL},
252 };
253
254 /*****************************************************************************/
255 /* Parameters                                                                */
256 /*****************************************************************************/
257 /*
258         DigParams
259 */
260
261 static void set_dig_params(lua_State *L, int table,
262                 const DigParams &params)
263 {
264         setboolfield(L, table, "diggable", params.diggable);
265         setfloatfield(L, table, "time", params.time);
266         setintfield(L, table, "wear", params.wear);
267 }
268
269 static void push_dig_params(lua_State *L,
270                 const DigParams &params)
271 {
272         lua_newtable(L);
273         set_dig_params(L, -1, params);
274 }
275
276 /*
277         HitParams
278 */
279
280 static void set_hit_params(lua_State *L, int table,
281                 const HitParams &params)
282 {
283         setintfield(L, table, "hp", params.hp);
284         setintfield(L, table, "wear", params.wear);
285 }
286
287 static void push_hit_params(lua_State *L,
288                 const HitParams &params)
289 {
290         lua_newtable(L);
291         set_hit_params(L, -1, params);
292 }
293
294 /*
295         ServerSoundParams
296 */
297
298 static void read_server_sound_params(lua_State *L, int index,
299                 ServerSoundParams &params)
300 {
301         if(index < 0)
302                 index = lua_gettop(L) + 1 + index;
303         // Clear
304         params = ServerSoundParams();
305         if(lua_istable(L, index)){
306                 getfloatfield(L, index, "gain", params.gain);
307                 getstringfield(L, index, "to_player", params.to_player);
308                 lua_getfield(L, index, "pos");
309                 if(!lua_isnil(L, -1)){
310                         v3f p = read_v3f(L, -1)*BS;
311                         params.pos = p;
312                         params.type = ServerSoundParams::SSP_POSITIONAL;
313                 }
314                 lua_pop(L, 1);
315                 lua_getfield(L, index, "object");
316                 if(!lua_isnil(L, -1)){
317                         ObjectRef *ref = ObjectRef::checkobject(L, -1);
318                         ServerActiveObject *sao = ObjectRef::getobject(ref);
319                         if(sao){
320                                 params.object = sao->getId();
321                                 params.type = ServerSoundParams::SSP_OBJECT;
322                         }
323                 }
324                 lua_pop(L, 1);
325                 params.max_hear_distance = BS*getfloatfield_default(L, index,
326                                 "max_hear_distance", params.max_hear_distance/BS);
327                 getboolfield(L, index, "loop", params.loop);
328         }
329 }
330
331 /*****************************************************************************/
332 /* callbacks                                                                 */
333 /*****************************************************************************/
334
335 // Push the list of callbacks (a lua table).
336 // Then push nargs arguments.
337 // Then call this function, which
338 // - runs the callbacks
339 // - removes the table and arguments from the lua stack
340 // - pushes the return value, computed depending on mode
341 void scriptapi_run_callbacks(lua_State *L, int nargs,
342                 RunCallbacksMode mode)
343 {
344         // Insert the return value into the lua stack, below the table
345         assert(lua_gettop(L) >= nargs + 1);
346         lua_pushnil(L);
347         lua_insert(L, -(nargs + 1) - 1);
348         // Stack now looks like this:
349         // ... <return value = nil> <table> <arg#1> <arg#2> ... <arg#n>
350
351         int rv = lua_gettop(L) - nargs - 1;
352         int table = rv + 1;
353         int arg = table + 1;
354
355         luaL_checktype(L, table, LUA_TTABLE);
356
357         // Foreach
358         lua_pushnil(L);
359         bool first_loop = true;
360         while(lua_next(L, table) != 0){
361                 // key at index -2 and value at index -1
362                 luaL_checktype(L, -1, LUA_TFUNCTION);
363                 // Call function
364                 for(int i = 0; i < nargs; i++)
365                         lua_pushvalue(L, arg+i);
366                 if(lua_pcall(L, nargs, 1, 0))
367                         script_error(L, "error: %s", lua_tostring(L, -1));
368
369                 // Move return value to designated space in stack
370                 // Or pop it
371                 if(first_loop){
372                         // Result of first callback is always moved
373                         lua_replace(L, rv);
374                         first_loop = false;
375                 } else {
376                         // Otherwise, what happens depends on the mode
377                         if(mode == RUN_CALLBACKS_MODE_FIRST)
378                                 lua_pop(L, 1);
379                         else if(mode == RUN_CALLBACKS_MODE_LAST)
380                                 lua_replace(L, rv);
381                         else if(mode == RUN_CALLBACKS_MODE_AND ||
382                                         mode == RUN_CALLBACKS_MODE_AND_SC){
383                                 if((bool)lua_toboolean(L, rv) == true &&
384                                                 (bool)lua_toboolean(L, -1) == false)
385                                         lua_replace(L, rv);
386                                 else
387                                         lua_pop(L, 1);
388                         }
389                         else if(mode == RUN_CALLBACKS_MODE_OR ||
390                                         mode == RUN_CALLBACKS_MODE_OR_SC){
391                                 if((bool)lua_toboolean(L, rv) == false &&
392                                                 (bool)lua_toboolean(L, -1) == true)
393                                         lua_replace(L, rv);
394                                 else
395                                         lua_pop(L, 1);
396                         }
397                         else
398                                 assert(0);
399                 }
400
401                 // Handle short circuit modes
402                 if(mode == RUN_CALLBACKS_MODE_AND_SC &&
403                                 (bool)lua_toboolean(L, rv) == false)
404                         break;
405                 else if(mode == RUN_CALLBACKS_MODE_OR_SC &&
406                                 (bool)lua_toboolean(L, rv) == true)
407                         break;
408
409                 // value removed, keep key for next iteration
410         }
411
412         // Remove stuff from stack, leaving only the return value
413         lua_settop(L, rv);
414
415         // Fix return value in case no callbacks were called
416         if(first_loop){
417                 if(mode == RUN_CALLBACKS_MODE_AND ||
418                                 mode == RUN_CALLBACKS_MODE_AND_SC){
419                         lua_pop(L, 1);
420                         lua_pushboolean(L, true);
421                 }
422                 else if(mode == RUN_CALLBACKS_MODE_OR ||
423                                 mode == RUN_CALLBACKS_MODE_OR_SC){
424                         lua_pop(L, 1);
425                         lua_pushboolean(L, false);
426                 }
427         }
428 }
429
430 bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
431                 const std::string &message)
432 {
433         realitycheck(L);
434         assert(lua_checkstack(L, 20));
435         StackUnroller stack_unroller(L);
436
437         // Get minetest.registered_on_chat_messages
438         lua_getglobal(L, "minetest");
439         lua_getfield(L, -1, "registered_on_chat_messages");
440         // Call callbacks
441         lua_pushstring(L, name.c_str());
442         lua_pushstring(L, message.c_str());
443         scriptapi_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
444         bool ate = lua_toboolean(L, -1);
445         return ate;
446 }
447
448 void scriptapi_on_shutdown(lua_State *L)
449 {
450         realitycheck(L);
451         assert(lua_checkstack(L, 20));
452         StackUnroller stack_unroller(L);
453
454         // Get registered shutdown hooks
455         lua_getglobal(L, "minetest");
456         lua_getfield(L, -1, "registered_on_shutdown");
457         // Call callbacks
458         scriptapi_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
459 }
460
461 void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player)
462 {
463         realitycheck(L);
464         assert(lua_checkstack(L, 20));
465         StackUnroller stack_unroller(L);
466
467         // Get minetest.registered_on_newplayers
468         lua_getglobal(L, "minetest");
469         lua_getfield(L, -1, "registered_on_newplayers");
470         // Call callbacks
471         objectref_get_or_create(L, player);
472         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
473 }
474
475 void scriptapi_on_dieplayer(lua_State *L, ServerActiveObject *player)
476 {
477         realitycheck(L);
478         assert(lua_checkstack(L, 20));
479         StackUnroller stack_unroller(L);
480
481         // Get minetest.registered_on_dieplayers
482         lua_getglobal(L, "minetest");
483         lua_getfield(L, -1, "registered_on_dieplayers");
484         // Call callbacks
485         objectref_get_or_create(L, player);
486         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
487 }
488
489 bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player)
490 {
491         realitycheck(L);
492         assert(lua_checkstack(L, 20));
493         StackUnroller stack_unroller(L);
494
495         // Get minetest.registered_on_respawnplayers
496         lua_getglobal(L, "minetest");
497         lua_getfield(L, -1, "registered_on_respawnplayers");
498         // Call callbacks
499         objectref_get_or_create(L, player);
500         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
501         bool positioning_handled_by_some = lua_toboolean(L, -1);
502         return positioning_handled_by_some;
503 }
504
505 void scriptapi_on_joinplayer(lua_State *L, ServerActiveObject *player)
506 {
507         realitycheck(L);
508         assert(lua_checkstack(L, 20));
509         StackUnroller stack_unroller(L);
510
511         // Get minetest.registered_on_joinplayers
512         lua_getglobal(L, "minetest");
513         lua_getfield(L, -1, "registered_on_joinplayers");
514         // Call callbacks
515         objectref_get_or_create(L, player);
516         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
517 }
518
519 void scriptapi_on_leaveplayer(lua_State *L, ServerActiveObject *player)
520 {
521         realitycheck(L);
522         assert(lua_checkstack(L, 20));
523         StackUnroller stack_unroller(L);
524
525         // Get minetest.registered_on_leaveplayers
526         lua_getglobal(L, "minetest");
527         lua_getfield(L, -1, "registered_on_leaveplayers");
528         // Call callbacks
529         objectref_get_or_create(L, player);
530         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
531 }
532
533 /*
534         player
535 */
536 void scriptapi_on_player_receive_fields(lua_State *L,
537                 ServerActiveObject *player,
538                 const std::string &formname,
539                 const std::map<std::string, std::string> &fields)
540 {
541         realitycheck(L);
542         assert(lua_checkstack(L, 20));
543         StackUnroller stack_unroller(L);
544
545         // Get minetest.registered_on_chat_messages
546         lua_getglobal(L, "minetest");
547         lua_getfield(L, -1, "registered_on_player_receive_fields");
548         // Call callbacks
549         // param 1
550         objectref_get_or_create(L, player);
551         // param 2
552         lua_pushstring(L, formname.c_str());
553         // param 3
554         lua_newtable(L);
555         for(std::map<std::string, std::string>::const_iterator
556                         i = fields.begin(); i != fields.end(); i++){
557                 const std::string &name = i->first;
558                 const std::string &value = i->second;
559                 lua_pushstring(L, name.c_str());
560                 lua_pushlstring(L, value.c_str(), value.size());
561                 lua_settable(L, -3);
562         }
563         scriptapi_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
564 }
565 /*****************************************************************************/
566 /* Api functions                                                             */
567 /*****************************************************************************/
568
569 // debug(text)
570 // Writes a line to dstream
571 static int l_debug(lua_State *L)
572 {
573         std::string text = lua_tostring(L, 1);
574         dstream << text << std::endl;
575         return 0;
576 }
577
578 // log([level,] text)
579 // Writes a line to the logger.
580 // The one-argument version logs to infostream.
581 // The two-argument version accept a log level: error, action, info, or verbose.
582 static int l_log(lua_State *L)
583 {
584         std::string text;
585         LogMessageLevel level = LMT_INFO;
586         if(lua_isnone(L, 2))
587         {
588                 text = lua_tostring(L, 1);
589         }
590         else
591         {
592                 std::string levelname = luaL_checkstring(L, 1);
593                 text = luaL_checkstring(L, 2);
594                 if(levelname == "error")
595                         level = LMT_ERROR;
596                 else if(levelname == "action")
597                         level = LMT_ACTION;
598                 else if(levelname == "verbose")
599                         level = LMT_VERBOSE;
600         }
601         log_printline(level, text);
602         return 0;
603 }
604
605 // request_shutdown()
606 static int l_request_shutdown(lua_State *L)
607 {
608         get_server(L)->requestShutdown();
609         return 0;
610 }
611
612 // get_server_status()
613 static int l_get_server_status(lua_State *L)
614 {
615         lua_pushstring(L, wide_to_narrow(get_server(L)->getStatusString()).c_str());
616         return 1;
617 }
618
619
620 // register_biome({lots of stuff})
621 static int l_register_biome(lua_State *L)
622 {
623         int index = 1;
624         luaL_checktype(L, index, LUA_TTABLE);
625
626         BiomeDefManager *bmgr = get_server(L)->getEmergeManager()->biomedef;
627         if (!bmgr) {
628                 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
629                 return 0;
630         }
631         
632         enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
633                                         "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
634         Biome *b = bmgr->createBiome(terrain);
635
636         b->name            = getstringfield_default(L, index, "name", "");
637         b->top_nodename    = getstringfield_default(L, index, "top_node", "");
638         b->top_depth       = getintfield_default(L, index, "top_depth", 0);
639         b->filler_nodename = getstringfield_default(L, index, "filler_node", "");
640         b->filler_height   = getintfield_default(L, index, "filler_height", 0);
641         b->height_min      = getintfield_default(L, index, "height_min", 0);
642         b->height_max      = getintfield_default(L, index, "height_max", 0);
643         b->heat_point      = getfloatfield_default(L, index, "heat_point", 0.);
644         b->humidity_point  = getfloatfield_default(L, index, "humidity_point", 0.);
645
646         b->flags    = 0; //reserved
647         b->c_top    = CONTENT_IGNORE;
648         b->c_filler = CONTENT_IGNORE;
649         bmgr->addBiome(b);
650
651         verbosestream << "register_biome: " << b->name << std::endl;
652         return 0;
653 }
654
655
656 static int l_register_ore(lua_State *L)
657 {
658         int index = 1;
659         luaL_checktype(L, index, LUA_TTABLE);
660         
661         EmergeManager *emerge = get_server(L)->getEmergeManager();
662         
663         enum OreType oretype = (OreType)getenumfield(L, index,
664                                 "ore_type", es_OreType, ORE_SCATTER);   
665         Ore *ore = createOre(oretype);
666         if (!ore) {
667                 errorstream << "register_ore: ore_type "
668                         << oretype << " not implemented";
669                 return 0;
670         }
671         
672         ore->ore_name       = getstringfield_default(L, index, "ore", "");
673         ore->ore_param2     = (u8)getintfield_default(L, index, "ore_param2", 0);
674         ore->wherein_name   = getstringfield_default(L, index, "wherein", "");
675         ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
676         ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
677         ore->clust_size     = getintfield_default(L, index, "clust_size", 0);
678         ore->height_min     = getintfield_default(L, index, "height_min", 0);
679         ore->height_max     = getintfield_default(L, index, "height_max", 0);
680         ore->flags          = getflagsfield(L, index, "flags", flagdesc_ore);
681         ore->nthresh        = getfloatfield_default(L, index, "noise_threshhold", 0.);
682
683         lua_getfield(L, index, "noise_params");
684         ore->np = read_noiseparams(L, -1);
685         lua_pop(L, 1);
686         
687         ore->noise = NULL;
688         
689         if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
690                 errorstream << "register_ore: clust_scarcity and clust_num_ores"
691                         " must be greater than 0" << std::endl;
692                 delete ore;
693                 return 0;
694         }
695         
696         emerge->ores.push_back(ore);
697         
698         verbosestream << "register_ore: ore '" << ore->ore_name
699                 << "' registered" << std::endl;
700         return 0;
701 }
702
703
704 // setting_set(name, value)
705 static int l_setting_set(lua_State *L)
706 {
707         const char *name = luaL_checkstring(L, 1);
708         const char *value = luaL_checkstring(L, 2);
709         g_settings->set(name, value);
710         return 0;
711 }
712
713 // setting_get(name)
714 static int l_setting_get(lua_State *L)
715 {
716         const char *name = luaL_checkstring(L, 1);
717         try{
718                 std::string value = g_settings->get(name);
719                 lua_pushstring(L, value.c_str());
720         } catch(SettingNotFoundException &e){
721                 lua_pushnil(L);
722         }
723         return 1;
724 }
725
726 // setting_getbool(name)
727 static int l_setting_getbool(lua_State *L)
728 {
729         const char *name = luaL_checkstring(L, 1);
730         try{
731                 bool value = g_settings->getBool(name);
732                 lua_pushboolean(L, value);
733         } catch(SettingNotFoundException &e){
734                 lua_pushnil(L);
735         }
736         return 1;
737 }
738
739 // setting_save()
740 static int l_setting_save(lua_State *L)
741 {
742         get_server(L)->saveConfig();
743         return 0;
744 }
745
746 // chat_send_all(text)
747 static int l_chat_send_all(lua_State *L)
748 {
749         const char *text = luaL_checkstring(L, 1);
750         // Get server from registry
751         Server *server = get_server(L);
752         // Send
753         server->notifyPlayers(narrow_to_wide(text));
754         return 0;
755 }
756
757 // chat_send_player(name, text, prepend)
758 static int l_chat_send_player(lua_State *L)
759 {
760         const char *name = luaL_checkstring(L, 1);
761         const char *text = luaL_checkstring(L, 2);
762         bool prepend = true;
763         if (lua_isboolean(L, 3))
764                 prepend = lua_toboolean(L, 3);
765         // Get server from registry
766         Server *server = get_server(L);
767         // Send
768         server->notifyPlayer(name, narrow_to_wide(text), prepend);
769         return 0;
770 }
771
772 // get_player_privs(name, text)
773 static int l_get_player_privs(lua_State *L)
774 {
775         const char *name = luaL_checkstring(L, 1);
776         // Get server from registry
777         Server *server = get_server(L);
778         // Do it
779         lua_newtable(L);
780         int table = lua_gettop(L);
781         std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
782         for(std::set<std::string>::const_iterator
783                         i = privs_s.begin(); i != privs_s.end(); i++){
784                 lua_pushboolean(L, true);
785                 lua_setfield(L, table, i->c_str());
786         }
787         lua_pushvalue(L, table);
788         return 1;
789 }
790
791 // get_player_ip()
792 static int l_get_player_ip(lua_State *L)
793 {
794         const char * name = luaL_checkstring(L, 1);
795         Player *player = get_env(L)->getPlayer(name);
796         if(player == NULL)
797         {
798                 lua_pushnil(L); // no such player
799                 return 1;
800         }
801         try
802         {
803                 Address addr = get_server(L)->getPeerAddress(get_env(L)->getPlayer(name)->peer_id);
804                 std::string ip_str = addr.serializeString();
805                 lua_pushstring(L, ip_str.c_str());
806                 return 1;
807         }
808         catch(con::PeerNotFoundException) // unlikely
809         {
810                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
811                 lua_pushnil(L); // error
812                 return 1;
813         }
814 }
815
816 // get_ban_list()
817 static int l_get_ban_list(lua_State *L)
818 {
819         lua_pushstring(L, get_server(L)->getBanDescription("").c_str());
820         return 1;
821 }
822
823 // get_ban_description()
824 static int l_get_ban_description(lua_State *L)
825 {
826         const char * ip_or_name = luaL_checkstring(L, 1);
827         lua_pushstring(L, get_server(L)->getBanDescription(std::string(ip_or_name)).c_str());
828         return 1;
829 }
830
831 // ban_player()
832 static int l_ban_player(lua_State *L)
833 {
834         const char * name = luaL_checkstring(L, 1);
835         Player *player = get_env(L)->getPlayer(name);
836         if(player == NULL)
837         {
838                 lua_pushboolean(L, false); // no such player
839                 return 1;
840         }
841         try
842         {
843                 Address addr = get_server(L)->getPeerAddress(get_env(L)->getPlayer(name)->peer_id);
844                 std::string ip_str = addr.serializeString();
845                 get_server(L)->setIpBanned(ip_str, name);
846         }
847         catch(con::PeerNotFoundException) // unlikely
848         {
849                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
850                 lua_pushboolean(L, false); // error
851                 return 1;
852         }
853         lua_pushboolean(L, true);
854         return 1;
855 }
856
857 // unban_player_or_ip()
858 static int l_unban_player_of_ip(lua_State *L)
859 {
860         const char * ip_or_name = luaL_checkstring(L, 1);
861         get_server(L)->unsetIpBanned(ip_or_name);
862         lua_pushboolean(L, true);
863         return 1;
864 }
865
866 // show_formspec(playername,formname,formspec)
867 static int l_show_formspec(lua_State *L)
868 {
869         const char *playername = luaL_checkstring(L, 1);
870         const char *formname = luaL_checkstring(L, 2);
871         const char *formspec = luaL_checkstring(L, 3);
872
873         if(get_server(L)->showFormspec(playername,formspec,formname))
874         {
875                 lua_pushboolean(L, true);
876         }else{
877                 lua_pushboolean(L, false);
878         }
879         return 1;
880 }
881
882 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
883 static int l_get_dig_params(lua_State *L)
884 {
885         std::map<std::string, int> groups;
886         read_groups(L, 1, groups);
887         ToolCapabilities tp = read_tool_capabilities(L, 2);
888         if(lua_isnoneornil(L, 3))
889                 push_dig_params(L, getDigParams(groups, &tp));
890         else
891                 push_dig_params(L, getDigParams(groups, &tp,
892                                         luaL_checknumber(L, 3)));
893         return 1;
894 }
895
896 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
897 static int l_get_hit_params(lua_State *L)
898 {
899         std::map<std::string, int> groups;
900         read_groups(L, 1, groups);
901         ToolCapabilities tp = read_tool_capabilities(L, 2);
902         if(lua_isnoneornil(L, 3))
903                 push_hit_params(L, getHitParams(groups, &tp));
904         else
905                 push_hit_params(L, getHitParams(groups, &tp,
906                                         luaL_checknumber(L, 3)));
907         return 1;
908 }
909
910 // get_current_modname()
911 static int l_get_current_modname(lua_State *L)
912 {
913         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
914         return 1;
915 }
916
917 // get_modpath(modname)
918 static int l_get_modpath(lua_State *L)
919 {
920         std::string modname = luaL_checkstring(L, 1);
921         // Do it
922         if(modname == "__builtin"){
923                 std::string path = get_server(L)->getBuiltinLuaPath();
924                 lua_pushstring(L, path.c_str());
925                 return 1;
926         }
927         const ModSpec *mod = get_server(L)->getModSpec(modname);
928         if(!mod){
929                 lua_pushnil(L);
930                 return 1;
931         }
932         lua_pushstring(L, mod->path.c_str());
933         return 1;
934 }
935
936 // get_modnames()
937 // the returned list is sorted alphabetically for you
938 static int l_get_modnames(lua_State *L)
939 {
940         // Get a list of mods
941         std::list<std::string> mods_unsorted, mods_sorted;
942         get_server(L)->getModNames(mods_unsorted);
943
944         // Take unsorted items from mods_unsorted and sort them into
945         // mods_sorted; not great performance but the number of mods on a
946         // server will likely be small.
947         for(std::list<std::string>::iterator i = mods_unsorted.begin();
948             i != mods_unsorted.end(); ++i)
949         {
950                 bool added = false;
951                 for(std::list<std::string>::iterator x = mods_sorted.begin();
952                     x != mods_sorted.end(); ++x)
953                 {
954                         // I doubt anybody using Minetest will be using
955                         // anything not ASCII based :)
956                         if((*i).compare(*x) <= 0)
957                         {
958                                 mods_sorted.insert(x, *i);
959                                 added = true;
960                                 break;
961                         }
962                 }
963                 if(!added)
964                         mods_sorted.push_back(*i);
965         }
966
967         // Get the table insertion function from Lua.
968         lua_getglobal(L, "table");
969         lua_getfield(L, -1, "insert");
970         int insertion_func = lua_gettop(L);
971
972         // Package them up for Lua
973         lua_newtable(L);
974         int new_table = lua_gettop(L);
975         std::list<std::string>::iterator i = mods_sorted.begin();
976         while(i != mods_sorted.end())
977         {
978                 lua_pushvalue(L, insertion_func);
979                 lua_pushvalue(L, new_table);
980                 lua_pushstring(L, (*i).c_str());
981                 if(lua_pcall(L, 2, 0, 0) != 0)
982                 {
983                         script_error(L, "error: %s", lua_tostring(L, -1));
984                 }
985                 ++i;
986         }
987         return 1;
988 }
989
990 // get_worldpath()
991 static int l_get_worldpath(lua_State *L)
992 {
993         std::string worldpath = get_server(L)->getWorldPath();
994         lua_pushstring(L, worldpath.c_str());
995         return 1;
996 }
997
998 // sound_play(spec, parameters)
999 static int l_sound_play(lua_State *L)
1000 {
1001         SimpleSoundSpec spec;
1002         read_soundspec(L, 1, spec);
1003         ServerSoundParams params;
1004         read_server_sound_params(L, 2, params);
1005         s32 handle = get_server(L)->playSound(spec, params);
1006         lua_pushinteger(L, handle);
1007         return 1;
1008 }
1009
1010 // sound_stop(handle)
1011 static int l_sound_stop(lua_State *L)
1012 {
1013         int handle = luaL_checkinteger(L, 1);
1014         get_server(L)->stopSound(handle);
1015         return 0;
1016 }
1017
1018 // is_singleplayer()
1019 static int l_is_singleplayer(lua_State *L)
1020 {
1021         lua_pushboolean(L, get_server(L)->isSingleplayer());
1022         return 1;
1023 }
1024
1025 // get_password_hash(name, raw_password)
1026 static int l_get_password_hash(lua_State *L)
1027 {
1028         std::string name = luaL_checkstring(L, 1);
1029         std::string raw_password = luaL_checkstring(L, 2);
1030         std::string hash = translatePassword(name,
1031                         narrow_to_wide(raw_password));
1032         lua_pushstring(L, hash.c_str());
1033         return 1;
1034 }
1035
1036 // notify_authentication_modified(name)
1037 static int l_notify_authentication_modified(lua_State *L)
1038 {
1039         std::string name = "";
1040         if(lua_isstring(L, 1))
1041                 name = lua_tostring(L, 1);
1042         get_server(L)->reportPrivsModified(name);
1043         return 0;
1044 }
1045
1046 // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
1047 static int l_rollback_get_last_node_actor(lua_State *L)
1048 {
1049         v3s16 p = read_v3s16(L, 1);
1050         int range = luaL_checknumber(L, 2);
1051         int seconds = luaL_checknumber(L, 3);
1052         Server *server = get_server(L);
1053         IRollbackManager *rollback = server->getRollbackManager();
1054         v3s16 act_p;
1055         int act_seconds = 0;
1056         std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
1057         lua_pushstring(L, actor.c_str());
1058         push_v3s16(L, act_p);
1059         lua_pushnumber(L, act_seconds);
1060         return 3;
1061 }
1062
1063 // rollback_revert_actions_by(actor, seconds) -> bool, log messages
1064 static int l_rollback_revert_actions_by(lua_State *L)
1065 {
1066         std::string actor = luaL_checkstring(L, 1);
1067         int seconds = luaL_checknumber(L, 2);
1068         Server *server = get_server(L);
1069         IRollbackManager *rollback = server->getRollbackManager();
1070         std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
1071         std::list<std::string> log;
1072         bool success = server->rollbackRevertActions(actions, &log);
1073         // Push boolean result
1074         lua_pushboolean(L, success);
1075         // Get the table insert function and push the log table
1076         lua_getglobal(L, "table");
1077         lua_getfield(L, -1, "insert");
1078         int table_insert = lua_gettop(L);
1079         lua_newtable(L);
1080         int table = lua_gettop(L);
1081         for(std::list<std::string>::const_iterator i = log.begin();
1082                         i != log.end(); i++)
1083         {
1084                 lua_pushvalue(L, table_insert);
1085                 lua_pushvalue(L, table);
1086                 lua_pushstring(L, i->c_str());
1087                 if(lua_pcall(L, 2, 0, 0))
1088                         script_error(L, "error: %s", lua_tostring(L, -1));
1089         }
1090         lua_remove(L, -2); // Remove table
1091         lua_remove(L, -2); // Remove insert
1092         return 2;
1093 }
1094
1095 static const struct luaL_Reg minetest_f [] = {
1096         {"debug", l_debug},
1097         {"log", l_log},
1098         {"request_shutdown", l_request_shutdown},
1099         {"get_server_status", l_get_server_status},
1100         {"register_item_raw", l_register_item_raw},
1101         {"register_alias_raw", l_register_alias_raw},
1102         {"register_craft", l_register_craft},
1103         {"register_biome", l_register_biome},
1104         {"register_ore", l_register_ore},
1105         {"setting_set", l_setting_set},
1106         {"setting_get", l_setting_get},
1107         {"setting_getbool", l_setting_getbool},
1108         {"setting_save",l_setting_save},
1109         {"chat_send_all", l_chat_send_all},
1110         {"chat_send_player", l_chat_send_player},
1111         {"get_player_privs", l_get_player_privs},
1112         {"get_player_ip", l_get_player_ip},
1113         {"get_ban_list", l_get_ban_list},
1114         {"get_ban_description", l_get_ban_description},
1115         {"ban_player", l_ban_player},
1116         {"unban_player_or_ip", l_unban_player_of_ip},
1117         {"get_inventory", l_get_inventory},
1118         {"create_detached_inventory_raw", l_create_detached_inventory_raw},
1119         {"show_formspec", l_show_formspec},
1120         {"get_dig_params", l_get_dig_params},
1121         {"get_hit_params", l_get_hit_params},
1122         {"get_current_modname", l_get_current_modname},
1123         {"get_modpath", l_get_modpath},
1124         {"get_modnames", l_get_modnames},
1125         {"get_worldpath", l_get_worldpath},
1126         {"sound_play", l_sound_play},
1127         {"sound_stop", l_sound_stop},
1128         {"is_singleplayer", l_is_singleplayer},
1129         {"get_password_hash", l_get_password_hash},
1130         {"notify_authentication_modified", l_notify_authentication_modified},
1131         {"get_craft_result", l_get_craft_result},
1132         {"get_craft_recipe", l_get_craft_recipe},
1133         {"get_all_craft_recipes", l_get_all_craft_recipes},
1134         {"rollback_get_last_node_actor", l_rollback_get_last_node_actor},
1135         {"rollback_revert_actions_by", l_rollback_revert_actions_by},
1136         {"add_particle", l_add_particle},
1137         {"add_particlespawner", l_add_particlespawner},
1138         {"delete_particlespawner", l_delete_particlespawner},
1139         {NULL, NULL}
1140 };
1141
1142
1143 /*
1144         Main export function
1145 */
1146
1147 void scriptapi_export(lua_State *L, Server *server)
1148 {
1149         realitycheck(L);
1150         assert(lua_checkstack(L, 20));
1151         verbosestream<<"scriptapi_export()"<<std::endl;
1152         StackUnroller stack_unroller(L);
1153
1154         // Store server as light userdata in registry
1155         lua_pushlightuserdata(L, server);
1156         lua_setfield(L, LUA_REGISTRYINDEX, "minetest_server");
1157
1158         // Register global functions in table minetest
1159         lua_newtable(L);
1160         luaL_register(L, NULL, minetest_f);
1161         lua_setglobal(L, "minetest");
1162
1163         // Get the main minetest table
1164         lua_getglobal(L, "minetest");
1165
1166         // Add tables to minetest
1167         lua_newtable(L);
1168         lua_setfield(L, -2, "object_refs");
1169         lua_newtable(L);
1170         lua_setfield(L, -2, "luaentities");
1171
1172         // Register wrappers
1173         LuaItemStack::Register(L);
1174         InvRef::Register(L);
1175         NodeMetaRef::Register(L);
1176         NodeTimerRef::Register(L);
1177         ObjectRef::Register(L);
1178         EnvRef::Register(L);
1179         LuaPseudoRandom::Register(L);
1180         LuaPerlinNoise::Register(L);
1181         LuaPerlinNoiseMap::Register(L);
1182 }