]> git.lizzy.rs Git - minetest.git/blob - src/scriptapi.cpp
Translated using Weblate (German)
[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 // register_biome_groups({frequencies})
620 static int l_register_biome_groups(lua_State *L)
621 {
622         luaL_checktype(L, 1, LUA_TTABLE);
623         int index = 1;
624
625         BiomeDefManager *bmgr = get_server(L)->getBiomeDef();
626         if (!bmgr) {
627                 verbosestream << "register_biome_groups: BiomeDefManager not active" << std::endl;
628                 return 0;
629         }
630
631         lua_pushnil(L);
632         for (int i = 1; lua_next(L, index) != 0; i++) {
633                 bmgr->addBiomeGroup(lua_tonumber(L, -1));
634                 lua_pop(L, 1);
635         }
636         lua_pop(L, 1);
637
638         return 0;
639 }
640
641 // register_biome({lots of stuff})
642 static int l_register_biome(lua_State *L)
643 {
644         luaL_checktype(L, 1, LUA_TTABLE);
645         int index = 1, groupid;
646         std::string nodename;
647
648         IWritableNodeDefManager *ndef = get_server(L)->getWritableNodeDefManager();
649         BiomeDefManager *bmgr = get_server(L)->getBiomeDef();
650         if (!bmgr) {
651                 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
652                 return 0;
653         }
654
655         groupid = getintfield_default(L, index, "group_id", 0);
656
657         enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
658                                         "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
659         Biome *b = bmgr->createBiome(terrain);
660
661         b->name = getstringfield_default(L, index, "name", "");
662
663         if (getstringfield(L, index, "node_top", nodename))
664                 b->n_top = MapNode(ndef->getId(nodename));
665         else
666                 b->n_top = MapNode(CONTENT_IGNORE);
667
668         if (getstringfield(L, index, "node_filler", nodename))
669                 b->n_filler = MapNode(ndef->getId(nodename));
670         else
671                 b->n_filler = b->n_top;
672
673         b->ntopnodes = getintfield_default(L, index, "num_top_nodes", 0);
674
675         b->height_min   = getintfield_default(L, index, "height_min", 0);
676         b->height_max   = getintfield_default(L, index, "height_max", 0);
677         b->heat_min     = getfloatfield_default(L, index, "heat_min", 0.);
678         b->heat_max     = getfloatfield_default(L, index, "heat_max", 0.);
679         b->humidity_min = getfloatfield_default(L, index, "humidity_min", 0.);
680         b->humidity_max = getfloatfield_default(L, index, "humidity_max", 0.);
681
682         b->np = new NoiseParams; // should read an entire NoiseParams later on...
683         getfloatfield(L, index, "scale", b->np->scale);
684         getfloatfield(L, index, "offset", b->np->offset);
685
686         b->groupid = (s8)groupid;
687         b->flags   = 0; //reserved
688
689         bmgr->addBiome(b);
690
691         verbosestream << "register_biome: " << b->name << std::endl;
692         return 0;
693 }
694
695
696 static int l_register_ore(lua_State *L)
697 {
698         int index = 1;
699         luaL_checktype(L, index, LUA_TTABLE);
700         
701         IWritableNodeDefManager *ndef = get_server(L)->getWritableNodeDefManager();
702         EmergeManager *emerge = get_server(L)->getEmergeManager();
703         
704         enum OreType oretype = (OreType)getenumfield(L, index,
705                                 "ore_type", es_OreType, ORE_SCATTER);   
706         Ore *ore = createOre(oretype);
707         if (!ore) {
708                 errorstream << "register_ore: ore_type "
709                         << oretype << " not implemented";
710                 return 0;
711         }
712         
713         ore->ore_name       = getstringfield_default(L, index, "ore", "");
714         ore->wherein_name   = getstringfield_default(L, index, "wherein", "");
715         ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
716         ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
717         ore->clust_size     = getintfield_default(L, index, "clust_size", 0);
718         ore->height_min     = getintfield_default(L, index, "height_min", 0);
719         ore->height_max     = getintfield_default(L, index, "height_max", 0);
720         ore->nthresh        = getfloatfield_default(L, index, "noise_threshhold", 0.);
721
722         lua_getfield(L, index, "noise_params");
723         ore->np = read_noiseparams(L, -1);
724         lua_pop(L, 1);
725         
726         ore->noise = NULL;
727         
728         if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
729                 errorstream << "register_ore: clust_scarcity and clust_num_ores"
730                         " must be greater than 0" << std::endl;
731                 delete ore;
732                 return 0;
733         }
734         
735         emerge->ores.push_back(ore);
736         
737         verbosestream << "register_ore: ore '" << ore->ore_name
738                 << "' registered" << std::endl;
739         return 0;
740 }
741
742
743 // setting_set(name, value)
744 static int l_setting_set(lua_State *L)
745 {
746         const char *name = luaL_checkstring(L, 1);
747         const char *value = luaL_checkstring(L, 2);
748         g_settings->set(name, value);
749         return 0;
750 }
751
752 // setting_get(name)
753 static int l_setting_get(lua_State *L)
754 {
755         const char *name = luaL_checkstring(L, 1);
756         try{
757                 std::string value = g_settings->get(name);
758                 lua_pushstring(L, value.c_str());
759         } catch(SettingNotFoundException &e){
760                 lua_pushnil(L);
761         }
762         return 1;
763 }
764
765 // setting_getbool(name)
766 static int l_setting_getbool(lua_State *L)
767 {
768         const char *name = luaL_checkstring(L, 1);
769         try{
770                 bool value = g_settings->getBool(name);
771                 lua_pushboolean(L, value);
772         } catch(SettingNotFoundException &e){
773                 lua_pushnil(L);
774         }
775         return 1;
776 }
777
778 // setting_save()
779 static int l_setting_save(lua_State *L)
780 {
781         get_server(L)->saveConfig();
782         return 0;
783 }
784
785 // chat_send_all(text)
786 static int l_chat_send_all(lua_State *L)
787 {
788         const char *text = luaL_checkstring(L, 1);
789         // Get server from registry
790         Server *server = get_server(L);
791         // Send
792         server->notifyPlayers(narrow_to_wide(text));
793         return 0;
794 }
795
796 // chat_send_player(name, text)
797 static int l_chat_send_player(lua_State *L)
798 {
799         const char *name = luaL_checkstring(L, 1);
800         const char *text = luaL_checkstring(L, 2);
801         // Get server from registry
802         Server *server = get_server(L);
803         // Send
804         server->notifyPlayer(name, narrow_to_wide(text));
805         return 0;
806 }
807
808 // get_player_privs(name, text)
809 static int l_get_player_privs(lua_State *L)
810 {
811         const char *name = luaL_checkstring(L, 1);
812         // Get server from registry
813         Server *server = get_server(L);
814         // Do it
815         lua_newtable(L);
816         int table = lua_gettop(L);
817         std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
818         for(std::set<std::string>::const_iterator
819                         i = privs_s.begin(); i != privs_s.end(); i++){
820                 lua_pushboolean(L, true);
821                 lua_setfield(L, table, i->c_str());
822         }
823         lua_pushvalue(L, table);
824         return 1;
825 }
826
827 // get_ban_list()
828 static int l_get_ban_list(lua_State *L)
829 {
830         lua_pushstring(L, get_server(L)->getBanDescription("").c_str());
831         return 1;
832 }
833
834 // get_ban_description()
835 static int l_get_ban_description(lua_State *L)
836 {
837         const char * ip_or_name = luaL_checkstring(L, 1);
838         lua_pushstring(L, get_server(L)->getBanDescription(std::string(ip_or_name)).c_str());
839         return 1;
840 }
841
842 // ban_player()
843 static int l_ban_player(lua_State *L)
844 {
845         const char * name = luaL_checkstring(L, 1);
846         Player *player = get_env(L)->getPlayer(name);
847         if(player == NULL)
848         {
849                 lua_pushboolean(L, false); // no such player
850                 return 1;
851         }
852         try
853         {
854                 Address addr = get_server(L)->getPeerAddress(get_env(L)->getPlayer(name)->peer_id);
855                 std::string ip_str = addr.serializeString();
856                 get_server(L)->setIpBanned(ip_str, name);
857         }
858         catch(con::PeerNotFoundException) // unlikely
859         {
860                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
861                 lua_pushboolean(L, false); // error
862                 return 1;
863         }
864         lua_pushboolean(L, true);
865         return 1;
866 }
867
868 // unban_player_or_ip()
869 static int l_unban_player_of_ip(lua_State *L)
870 {
871         const char * ip_or_name = luaL_checkstring(L, 1);
872         get_server(L)->unsetIpBanned(ip_or_name);
873         lua_pushboolean(L, true);
874         return 1;
875 }
876
877 // show_formspec(playername,formname,formspec)
878 static int l_show_formspec(lua_State *L)
879 {
880         const char *playername = luaL_checkstring(L, 1);
881         const char *formname = luaL_checkstring(L, 2);
882         const char *formspec = luaL_checkstring(L, 3);
883
884         if(get_server(L)->showFormspec(playername,formspec,formname))
885         {
886                 lua_pushboolean(L, true);
887         }else{
888                 lua_pushboolean(L, false);
889         }
890         return 1;
891 }
892
893 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
894 static int l_get_dig_params(lua_State *L)
895 {
896         std::map<std::string, int> groups;
897         read_groups(L, 1, groups);
898         ToolCapabilities tp = read_tool_capabilities(L, 2);
899         if(lua_isnoneornil(L, 3))
900                 push_dig_params(L, getDigParams(groups, &tp));
901         else
902                 push_dig_params(L, getDigParams(groups, &tp,
903                                         luaL_checknumber(L, 3)));
904         return 1;
905 }
906
907 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
908 static int l_get_hit_params(lua_State *L)
909 {
910         std::map<std::string, int> groups;
911         read_groups(L, 1, groups);
912         ToolCapabilities tp = read_tool_capabilities(L, 2);
913         if(lua_isnoneornil(L, 3))
914                 push_hit_params(L, getHitParams(groups, &tp));
915         else
916                 push_hit_params(L, getHitParams(groups, &tp,
917                                         luaL_checknumber(L, 3)));
918         return 1;
919 }
920
921 // get_current_modname()
922 static int l_get_current_modname(lua_State *L)
923 {
924         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
925         return 1;
926 }
927
928 // get_modpath(modname)
929 static int l_get_modpath(lua_State *L)
930 {
931         std::string modname = luaL_checkstring(L, 1);
932         // Do it
933         if(modname == "__builtin"){
934                 std::string path = get_server(L)->getBuiltinLuaPath();
935                 lua_pushstring(L, path.c_str());
936                 return 1;
937         }
938         const ModSpec *mod = get_server(L)->getModSpec(modname);
939         if(!mod){
940                 lua_pushnil(L);
941                 return 1;
942         }
943         lua_pushstring(L, mod->path.c_str());
944         return 1;
945 }
946
947 // get_modnames()
948 // the returned list is sorted alphabetically for you
949 static int l_get_modnames(lua_State *L)
950 {
951         // Get a list of mods
952         std::list<std::string> mods_unsorted, mods_sorted;
953         get_server(L)->getModNames(mods_unsorted);
954
955         // Take unsorted items from mods_unsorted and sort them into
956         // mods_sorted; not great performance but the number of mods on a
957         // server will likely be small.
958         for(std::list<std::string>::iterator i = mods_unsorted.begin();
959             i != mods_unsorted.end(); ++i)
960         {
961                 bool added = false;
962                 for(std::list<std::string>::iterator x = mods_sorted.begin();
963                     x != mods_sorted.end(); ++x)
964                 {
965                         // I doubt anybody using Minetest will be using
966                         // anything not ASCII based :)
967                         if((*i).compare(*x) <= 0)
968                         {
969                                 mods_sorted.insert(x, *i);
970                                 added = true;
971                                 break;
972                         }
973                 }
974                 if(!added)
975                         mods_sorted.push_back(*i);
976         }
977
978         // Get the table insertion function from Lua.
979         lua_getglobal(L, "table");
980         lua_getfield(L, -1, "insert");
981         int insertion_func = lua_gettop(L);
982
983         // Package them up for Lua
984         lua_newtable(L);
985         int new_table = lua_gettop(L);
986         std::list<std::string>::iterator i = mods_sorted.begin();
987         while(i != mods_sorted.end())
988         {
989                 lua_pushvalue(L, insertion_func);
990                 lua_pushvalue(L, new_table);
991                 lua_pushstring(L, (*i).c_str());
992                 if(lua_pcall(L, 2, 0, 0) != 0)
993                 {
994                         script_error(L, "error: %s", lua_tostring(L, -1));
995                 }
996                 ++i;
997         }
998         return 1;
999 }
1000
1001 // get_worldpath()
1002 static int l_get_worldpath(lua_State *L)
1003 {
1004         std::string worldpath = get_server(L)->getWorldPath();
1005         lua_pushstring(L, worldpath.c_str());
1006         return 1;
1007 }
1008
1009 // sound_play(spec, parameters)
1010 static int l_sound_play(lua_State *L)
1011 {
1012         SimpleSoundSpec spec;
1013         read_soundspec(L, 1, spec);
1014         ServerSoundParams params;
1015         read_server_sound_params(L, 2, params);
1016         s32 handle = get_server(L)->playSound(spec, params);
1017         lua_pushinteger(L, handle);
1018         return 1;
1019 }
1020
1021 // sound_stop(handle)
1022 static int l_sound_stop(lua_State *L)
1023 {
1024         int handle = luaL_checkinteger(L, 1);
1025         get_server(L)->stopSound(handle);
1026         return 0;
1027 }
1028
1029 // is_singleplayer()
1030 static int l_is_singleplayer(lua_State *L)
1031 {
1032         lua_pushboolean(L, get_server(L)->isSingleplayer());
1033         return 1;
1034 }
1035
1036 // get_password_hash(name, raw_password)
1037 static int l_get_password_hash(lua_State *L)
1038 {
1039         std::string name = luaL_checkstring(L, 1);
1040         std::string raw_password = luaL_checkstring(L, 2);
1041         std::string hash = translatePassword(name,
1042                         narrow_to_wide(raw_password));
1043         lua_pushstring(L, hash.c_str());
1044         return 1;
1045 }
1046
1047 // notify_authentication_modified(name)
1048 static int l_notify_authentication_modified(lua_State *L)
1049 {
1050         std::string name = "";
1051         if(lua_isstring(L, 1))
1052                 name = lua_tostring(L, 1);
1053         get_server(L)->reportPrivsModified(name);
1054         return 0;
1055 }
1056
1057 // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
1058 static int l_rollback_get_last_node_actor(lua_State *L)
1059 {
1060         v3s16 p = read_v3s16(L, 1);
1061         int range = luaL_checknumber(L, 2);
1062         int seconds = luaL_checknumber(L, 3);
1063         Server *server = get_server(L);
1064         IRollbackManager *rollback = server->getRollbackManager();
1065         v3s16 act_p;
1066         int act_seconds = 0;
1067         std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
1068         lua_pushstring(L, actor.c_str());
1069         push_v3s16(L, act_p);
1070         lua_pushnumber(L, act_seconds);
1071         return 3;
1072 }
1073
1074 // rollback_revert_actions_by(actor, seconds) -> bool, log messages
1075 static int l_rollback_revert_actions_by(lua_State *L)
1076 {
1077         std::string actor = luaL_checkstring(L, 1);
1078         int seconds = luaL_checknumber(L, 2);
1079         Server *server = get_server(L);
1080         IRollbackManager *rollback = server->getRollbackManager();
1081         std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
1082         std::list<std::string> log;
1083         bool success = server->rollbackRevertActions(actions, &log);
1084         // Push boolean result
1085         lua_pushboolean(L, success);
1086         // Get the table insert function and push the log table
1087         lua_getglobal(L, "table");
1088         lua_getfield(L, -1, "insert");
1089         int table_insert = lua_gettop(L);
1090         lua_newtable(L);
1091         int table = lua_gettop(L);
1092         for(std::list<std::string>::const_iterator i = log.begin();
1093                         i != log.end(); i++)
1094         {
1095                 lua_pushvalue(L, table_insert);
1096                 lua_pushvalue(L, table);
1097                 lua_pushstring(L, i->c_str());
1098                 if(lua_pcall(L, 2, 0, 0))
1099                         script_error(L, "error: %s", lua_tostring(L, -1));
1100         }
1101         lua_remove(L, -2); // Remove table
1102         lua_remove(L, -2); // Remove insert
1103         return 2;
1104 }
1105
1106 static const struct luaL_Reg minetest_f [] = {
1107         {"debug", l_debug},
1108         {"log", l_log},
1109         {"request_shutdown", l_request_shutdown},
1110         {"get_server_status", l_get_server_status},
1111         {"register_item_raw", l_register_item_raw},
1112         {"register_alias_raw", l_register_alias_raw},
1113         {"register_craft", l_register_craft},
1114         {"register_biome", l_register_biome},
1115         {"register_biome_groups", l_register_biome_groups},
1116         {"register_ore", l_register_ore},
1117         {"setting_set", l_setting_set},
1118         {"setting_get", l_setting_get},
1119         {"setting_getbool", l_setting_getbool},
1120         {"setting_save",l_setting_save},
1121         {"chat_send_all", l_chat_send_all},
1122         {"chat_send_player", l_chat_send_player},
1123         {"get_player_privs", l_get_player_privs},
1124         {"get_ban_list", l_get_ban_list},
1125         {"get_ban_description", l_get_ban_description},
1126         {"ban_player", l_ban_player},
1127         {"unban_player_or_ip", l_unban_player_of_ip},
1128         {"get_inventory", l_get_inventory},
1129         {"create_detached_inventory_raw", l_create_detached_inventory_raw},
1130         {"show_formspec", l_show_formspec},
1131         {"get_dig_params", l_get_dig_params},
1132         {"get_hit_params", l_get_hit_params},
1133         {"get_current_modname", l_get_current_modname},
1134         {"get_modpath", l_get_modpath},
1135         {"get_modnames", l_get_modnames},
1136         {"get_worldpath", l_get_worldpath},
1137         {"sound_play", l_sound_play},
1138         {"sound_stop", l_sound_stop},
1139         {"is_singleplayer", l_is_singleplayer},
1140         {"get_password_hash", l_get_password_hash},
1141         {"notify_authentication_modified", l_notify_authentication_modified},
1142         {"get_craft_result", l_get_craft_result},
1143         {"get_craft_recipe", l_get_craft_recipe},
1144         {"get_all_craft_recipes", l_get_all_craft_recipes},
1145         {"rollback_get_last_node_actor", l_rollback_get_last_node_actor},
1146         {"rollback_revert_actions_by", l_rollback_revert_actions_by},
1147         {"add_particle", l_add_particle},
1148         {"add_particlespawner", l_add_particlespawner},
1149         {"delete_particlespawner", l_delete_particlespawner},
1150         {NULL, NULL}
1151 };
1152
1153
1154 /*
1155         Main export function
1156 */
1157
1158 void scriptapi_export(lua_State *L, Server *server)
1159 {
1160         realitycheck(L);
1161         assert(lua_checkstack(L, 20));
1162         verbosestream<<"scriptapi_export()"<<std::endl;
1163         StackUnroller stack_unroller(L);
1164
1165         // Store server as light userdata in registry
1166         lua_pushlightuserdata(L, server);
1167         lua_setfield(L, LUA_REGISTRYINDEX, "minetest_server");
1168
1169         // Register global functions in table minetest
1170         lua_newtable(L);
1171         luaL_register(L, NULL, minetest_f);
1172         lua_setglobal(L, "minetest");
1173
1174         // Get the main minetest table
1175         lua_getglobal(L, "minetest");
1176
1177         // Add tables to minetest
1178         lua_newtable(L);
1179         lua_setfield(L, -2, "object_refs");
1180         lua_newtable(L);
1181         lua_setfield(L, -2, "luaentities");
1182
1183         // Register wrappers
1184         LuaItemStack::Register(L);
1185         InvRef::Register(L);
1186         NodeMetaRef::Register(L);
1187         NodeTimerRef::Register(L);
1188         ObjectRef::Register(L);
1189         EnvRef::Register(L);
1190         LuaPseudoRandom::Register(L);
1191         LuaPerlinNoise::Register(L);
1192         LuaPerlinNoiseMap::Register(L);
1193 }