Translation to Portuguese of Brazil for Minetest
[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)
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         // Get server from registry
763         Server *server = get_server(L);
764         // Send
765         server->notifyPlayer(name, narrow_to_wide(text));
766         return 0;
767 }
768
769 // get_player_privs(name, text)
770 static int l_get_player_privs(lua_State *L)
771 {
772         const char *name = luaL_checkstring(L, 1);
773         // Get server from registry
774         Server *server = get_server(L);
775         // Do it
776         lua_newtable(L);
777         int table = lua_gettop(L);
778         std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
779         for(std::set<std::string>::const_iterator
780                         i = privs_s.begin(); i != privs_s.end(); i++){
781                 lua_pushboolean(L, true);
782                 lua_setfield(L, table, i->c_str());
783         }
784         lua_pushvalue(L, table);
785         return 1;
786 }
787
788 // get_ban_list()
789 static int l_get_ban_list(lua_State *L)
790 {
791         lua_pushstring(L, get_server(L)->getBanDescription("").c_str());
792         return 1;
793 }
794
795 // get_ban_description()
796 static int l_get_ban_description(lua_State *L)
797 {
798         const char * ip_or_name = luaL_checkstring(L, 1);
799         lua_pushstring(L, get_server(L)->getBanDescription(std::string(ip_or_name)).c_str());
800         return 1;
801 }
802
803 // ban_player()
804 static int l_ban_player(lua_State *L)
805 {
806         const char * name = luaL_checkstring(L, 1);
807         Player *player = get_env(L)->getPlayer(name);
808         if(player == NULL)
809         {
810                 lua_pushboolean(L, false); // no such player
811                 return 1;
812         }
813         try
814         {
815                 Address addr = get_server(L)->getPeerAddress(get_env(L)->getPlayer(name)->peer_id);
816                 std::string ip_str = addr.serializeString();
817                 get_server(L)->setIpBanned(ip_str, name);
818         }
819         catch(con::PeerNotFoundException) // unlikely
820         {
821                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
822                 lua_pushboolean(L, false); // error
823                 return 1;
824         }
825         lua_pushboolean(L, true);
826         return 1;
827 }
828
829 // unban_player_or_ip()
830 static int l_unban_player_of_ip(lua_State *L)
831 {
832         const char * ip_or_name = luaL_checkstring(L, 1);
833         get_server(L)->unsetIpBanned(ip_or_name);
834         lua_pushboolean(L, true);
835         return 1;
836 }
837
838 // show_formspec(playername,formname,formspec)
839 static int l_show_formspec(lua_State *L)
840 {
841         const char *playername = luaL_checkstring(L, 1);
842         const char *formname = luaL_checkstring(L, 2);
843         const char *formspec = luaL_checkstring(L, 3);
844
845         if(get_server(L)->showFormspec(playername,formspec,formname))
846         {
847                 lua_pushboolean(L, true);
848         }else{
849                 lua_pushboolean(L, false);
850         }
851         return 1;
852 }
853
854 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
855 static int l_get_dig_params(lua_State *L)
856 {
857         std::map<std::string, int> groups;
858         read_groups(L, 1, groups);
859         ToolCapabilities tp = read_tool_capabilities(L, 2);
860         if(lua_isnoneornil(L, 3))
861                 push_dig_params(L, getDigParams(groups, &tp));
862         else
863                 push_dig_params(L, getDigParams(groups, &tp,
864                                         luaL_checknumber(L, 3)));
865         return 1;
866 }
867
868 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
869 static int l_get_hit_params(lua_State *L)
870 {
871         std::map<std::string, int> groups;
872         read_groups(L, 1, groups);
873         ToolCapabilities tp = read_tool_capabilities(L, 2);
874         if(lua_isnoneornil(L, 3))
875                 push_hit_params(L, getHitParams(groups, &tp));
876         else
877                 push_hit_params(L, getHitParams(groups, &tp,
878                                         luaL_checknumber(L, 3)));
879         return 1;
880 }
881
882 // get_current_modname()
883 static int l_get_current_modname(lua_State *L)
884 {
885         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
886         return 1;
887 }
888
889 // get_modpath(modname)
890 static int l_get_modpath(lua_State *L)
891 {
892         std::string modname = luaL_checkstring(L, 1);
893         // Do it
894         if(modname == "__builtin"){
895                 std::string path = get_server(L)->getBuiltinLuaPath();
896                 lua_pushstring(L, path.c_str());
897                 return 1;
898         }
899         const ModSpec *mod = get_server(L)->getModSpec(modname);
900         if(!mod){
901                 lua_pushnil(L);
902                 return 1;
903         }
904         lua_pushstring(L, mod->path.c_str());
905         return 1;
906 }
907
908 // get_modnames()
909 // the returned list is sorted alphabetically for you
910 static int l_get_modnames(lua_State *L)
911 {
912         // Get a list of mods
913         std::list<std::string> mods_unsorted, mods_sorted;
914         get_server(L)->getModNames(mods_unsorted);
915
916         // Take unsorted items from mods_unsorted and sort them into
917         // mods_sorted; not great performance but the number of mods on a
918         // server will likely be small.
919         for(std::list<std::string>::iterator i = mods_unsorted.begin();
920             i != mods_unsorted.end(); ++i)
921         {
922                 bool added = false;
923                 for(std::list<std::string>::iterator x = mods_sorted.begin();
924                     x != mods_sorted.end(); ++x)
925                 {
926                         // I doubt anybody using Minetest will be using
927                         // anything not ASCII based :)
928                         if((*i).compare(*x) <= 0)
929                         {
930                                 mods_sorted.insert(x, *i);
931                                 added = true;
932                                 break;
933                         }
934                 }
935                 if(!added)
936                         mods_sorted.push_back(*i);
937         }
938
939         // Get the table insertion function from Lua.
940         lua_getglobal(L, "table");
941         lua_getfield(L, -1, "insert");
942         int insertion_func = lua_gettop(L);
943
944         // Package them up for Lua
945         lua_newtable(L);
946         int new_table = lua_gettop(L);
947         std::list<std::string>::iterator i = mods_sorted.begin();
948         while(i != mods_sorted.end())
949         {
950                 lua_pushvalue(L, insertion_func);
951                 lua_pushvalue(L, new_table);
952                 lua_pushstring(L, (*i).c_str());
953                 if(lua_pcall(L, 2, 0, 0) != 0)
954                 {
955                         script_error(L, "error: %s", lua_tostring(L, -1));
956                 }
957                 ++i;
958         }
959         return 1;
960 }
961
962 // get_worldpath()
963 static int l_get_worldpath(lua_State *L)
964 {
965         std::string worldpath = get_server(L)->getWorldPath();
966         lua_pushstring(L, worldpath.c_str());
967         return 1;
968 }
969
970 // sound_play(spec, parameters)
971 static int l_sound_play(lua_State *L)
972 {
973         SimpleSoundSpec spec;
974         read_soundspec(L, 1, spec);
975         ServerSoundParams params;
976         read_server_sound_params(L, 2, params);
977         s32 handle = get_server(L)->playSound(spec, params);
978         lua_pushinteger(L, handle);
979         return 1;
980 }
981
982 // sound_stop(handle)
983 static int l_sound_stop(lua_State *L)
984 {
985         int handle = luaL_checkinteger(L, 1);
986         get_server(L)->stopSound(handle);
987         return 0;
988 }
989
990 // is_singleplayer()
991 static int l_is_singleplayer(lua_State *L)
992 {
993         lua_pushboolean(L, get_server(L)->isSingleplayer());
994         return 1;
995 }
996
997 // get_password_hash(name, raw_password)
998 static int l_get_password_hash(lua_State *L)
999 {
1000         std::string name = luaL_checkstring(L, 1);
1001         std::string raw_password = luaL_checkstring(L, 2);
1002         std::string hash = translatePassword(name,
1003                         narrow_to_wide(raw_password));
1004         lua_pushstring(L, hash.c_str());
1005         return 1;
1006 }
1007
1008 // notify_authentication_modified(name)
1009 static int l_notify_authentication_modified(lua_State *L)
1010 {
1011         std::string name = "";
1012         if(lua_isstring(L, 1))
1013                 name = lua_tostring(L, 1);
1014         get_server(L)->reportPrivsModified(name);
1015         return 0;
1016 }
1017
1018 // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
1019 static int l_rollback_get_last_node_actor(lua_State *L)
1020 {
1021         v3s16 p = read_v3s16(L, 1);
1022         int range = luaL_checknumber(L, 2);
1023         int seconds = luaL_checknumber(L, 3);
1024         Server *server = get_server(L);
1025         IRollbackManager *rollback = server->getRollbackManager();
1026         v3s16 act_p;
1027         int act_seconds = 0;
1028         std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
1029         lua_pushstring(L, actor.c_str());
1030         push_v3s16(L, act_p);
1031         lua_pushnumber(L, act_seconds);
1032         return 3;
1033 }
1034
1035 // rollback_revert_actions_by(actor, seconds) -> bool, log messages
1036 static int l_rollback_revert_actions_by(lua_State *L)
1037 {
1038         std::string actor = luaL_checkstring(L, 1);
1039         int seconds = luaL_checknumber(L, 2);
1040         Server *server = get_server(L);
1041         IRollbackManager *rollback = server->getRollbackManager();
1042         std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
1043         std::list<std::string> log;
1044         bool success = server->rollbackRevertActions(actions, &log);
1045         // Push boolean result
1046         lua_pushboolean(L, success);
1047         // Get the table insert function and push the log table
1048         lua_getglobal(L, "table");
1049         lua_getfield(L, -1, "insert");
1050         int table_insert = lua_gettop(L);
1051         lua_newtable(L);
1052         int table = lua_gettop(L);
1053         for(std::list<std::string>::const_iterator i = log.begin();
1054                         i != log.end(); i++)
1055         {
1056                 lua_pushvalue(L, table_insert);
1057                 lua_pushvalue(L, table);
1058                 lua_pushstring(L, i->c_str());
1059                 if(lua_pcall(L, 2, 0, 0))
1060                         script_error(L, "error: %s", lua_tostring(L, -1));
1061         }
1062         lua_remove(L, -2); // Remove table
1063         lua_remove(L, -2); // Remove insert
1064         return 2;
1065 }
1066
1067 static const struct luaL_Reg minetest_f [] = {
1068         {"debug", l_debug},
1069         {"log", l_log},
1070         {"request_shutdown", l_request_shutdown},
1071         {"get_server_status", l_get_server_status},
1072         {"register_item_raw", l_register_item_raw},
1073         {"register_alias_raw", l_register_alias_raw},
1074         {"register_craft", l_register_craft},
1075         {"register_biome", l_register_biome},
1076         {"register_ore", l_register_ore},
1077         {"setting_set", l_setting_set},
1078         {"setting_get", l_setting_get},
1079         {"setting_getbool", l_setting_getbool},
1080         {"setting_save",l_setting_save},
1081         {"chat_send_all", l_chat_send_all},
1082         {"chat_send_player", l_chat_send_player},
1083         {"get_player_privs", l_get_player_privs},
1084         {"get_ban_list", l_get_ban_list},
1085         {"get_ban_description", l_get_ban_description},
1086         {"ban_player", l_ban_player},
1087         {"unban_player_or_ip", l_unban_player_of_ip},
1088         {"get_inventory", l_get_inventory},
1089         {"create_detached_inventory_raw", l_create_detached_inventory_raw},
1090         {"show_formspec", l_show_formspec},
1091         {"get_dig_params", l_get_dig_params},
1092         {"get_hit_params", l_get_hit_params},
1093         {"get_current_modname", l_get_current_modname},
1094         {"get_modpath", l_get_modpath},
1095         {"get_modnames", l_get_modnames},
1096         {"get_worldpath", l_get_worldpath},
1097         {"sound_play", l_sound_play},
1098         {"sound_stop", l_sound_stop},
1099         {"is_singleplayer", l_is_singleplayer},
1100         {"get_password_hash", l_get_password_hash},
1101         {"notify_authentication_modified", l_notify_authentication_modified},
1102         {"get_craft_result", l_get_craft_result},
1103         {"get_craft_recipe", l_get_craft_recipe},
1104         {"get_all_craft_recipes", l_get_all_craft_recipes},
1105         {"rollback_get_last_node_actor", l_rollback_get_last_node_actor},
1106         {"rollback_revert_actions_by", l_rollback_revert_actions_by},
1107         {"add_particle", l_add_particle},
1108         {"add_particlespawner", l_add_particlespawner},
1109         {"delete_particlespawner", l_delete_particlespawner},
1110         {NULL, NULL}
1111 };
1112
1113
1114 /*
1115         Main export function
1116 */
1117
1118 void scriptapi_export(lua_State *L, Server *server)
1119 {
1120         realitycheck(L);
1121         assert(lua_checkstack(L, 20));
1122         verbosestream<<"scriptapi_export()"<<std::endl;
1123         StackUnroller stack_unroller(L);
1124
1125         // Store server as light userdata in registry
1126         lua_pushlightuserdata(L, server);
1127         lua_setfield(L, LUA_REGISTRYINDEX, "minetest_server");
1128
1129         // Register global functions in table minetest
1130         lua_newtable(L);
1131         luaL_register(L, NULL, minetest_f);
1132         lua_setglobal(L, "minetest");
1133
1134         // Get the main minetest table
1135         lua_getglobal(L, "minetest");
1136
1137         // Add tables to minetest
1138         lua_newtable(L);
1139         lua_setfield(L, -2, "object_refs");
1140         lua_newtable(L);
1141         lua_setfield(L, -2, "luaentities");
1142
1143         // Register wrappers
1144         LuaItemStack::Register(L);
1145         InvRef::Register(L);
1146         NodeMetaRef::Register(L);
1147         NodeTimerRef::Register(L);
1148         ObjectRef::Register(L);
1149         EnvRef::Register(L);
1150         LuaPseudoRandom::Register(L);
1151         LuaPerlinNoise::Register(L);
1152         LuaPerlinNoiseMap::Register(L);
1153 }