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