]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_client.cpp
Cleanup various headers to reduce compilation times (#6255)
[dragonfireclient.git] / src / script / lua_api / l_client.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "l_client.h"
22 #include "chatmessage.h"
23 #include "client.h"
24 #include "clientenvironment.h"
25 #include "common/c_content.h"
26 #include "common/c_converter.h"
27 #include "cpp_api/s_base.h"
28 #include "gettext.h"
29 #include "l_internal.h"
30 #include "lua_api/l_item.h"
31 #include "lua_api/l_nodemeta.h"
32 #include "mainmenumanager.h"
33 #include "map.h"
34 #include "util/string.h"
35 #include "nodedef.h"
36
37 extern MainGameCallback *g_gamecallback;
38
39 int ModApiClient::l_get_current_modname(lua_State *L)
40 {
41         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
42         return 1;
43 }
44
45 // get_last_run_mod()
46 int ModApiClient::l_get_last_run_mod(lua_State *L)
47 {
48         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
49         const char *current_mod = lua_tostring(L, -1);
50         if (current_mod == NULL || current_mod[0] == '\0') {
51                 lua_pop(L, 1);
52                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
53         }
54         return 1;
55 }
56
57 // set_last_run_mod(modname)
58 int ModApiClient::l_set_last_run_mod(lua_State *L)
59 {
60         if (!lua_isstring(L, 1))
61                 return 0;
62
63         const char *mod = lua_tostring(L, 1);
64         getScriptApiBase(L)->setOriginDirect(mod);
65         lua_pushboolean(L, true);
66         return 1;
67 }
68
69 // print(text)
70 int ModApiClient::l_print(lua_State *L)
71 {
72         NO_MAP_LOCK_REQUIRED;
73         std::string text = luaL_checkstring(L, 1);
74         rawstream << text << std::endl;
75         return 0;
76 }
77
78 // display_chat_message(message)
79 int ModApiClient::l_display_chat_message(lua_State *L)
80 {
81         if (!lua_isstring(L, 1))
82                 return 0;
83
84         std::string message = luaL_checkstring(L, 1);
85         getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
86         lua_pushboolean(L, true);
87         return 1;
88 }
89
90 // send_chat_message(message)
91 int ModApiClient::l_send_chat_message(lua_State *L)
92 {
93         if (!lua_isstring(L, 1))
94                 return 0;
95
96         // If server disabled this API, discard
97         if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_CHAT_MESSAGES))
98                 return 0;
99
100         std::string message = luaL_checkstring(L, 1);
101         getClient(L)->sendChatMessage(utf8_to_wide(message));
102         return 0;
103 }
104
105 // clear_out_chat_queue()
106 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
107 {
108         getClient(L)->clearOutChatQueue();
109         return 0;
110 }
111
112 // get_player_names()
113 int ModApiClient::l_get_player_names(lua_State *L)
114 {
115         const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
116         lua_createtable(L, plist.size(), 0);
117         int newTable = lua_gettop(L);
118         int index = 1;
119         std::list<std::string>::const_iterator iter;
120         for (iter = plist.begin(); iter != plist.end(); ++iter) {
121                 lua_pushstring(L, (*iter).c_str());
122                 lua_rawseti(L, newTable, index);
123                 index++;
124         }
125         return 1;
126 }
127
128 // show_formspec(formspec)
129 int ModApiClient::l_show_formspec(lua_State *L)
130 {
131         if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
132                 return 0;
133
134         ClientEvent event;
135         event.type = CE_SHOW_LOCAL_FORMSPEC;
136         event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
137         event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
138         getClient(L)->pushToEventQueue(event);
139         lua_pushboolean(L, true);
140         return 1;
141 }
142
143 // send_respawn()
144 int ModApiClient::l_send_respawn(lua_State *L)
145 {
146         getClient(L)->sendRespawn();
147         return 0;
148 }
149
150 // disconnect()
151 int ModApiClient::l_disconnect(lua_State *L)
152 {
153         // Stops badly written Lua code form causing boot loops
154         if (getClient(L)->isShutdown()) {
155                 lua_pushboolean(L, false);
156                 return 1;
157         }
158
159         g_gamecallback->disconnect();
160         lua_pushboolean(L, true);
161         return 1;
162 }
163
164 // gettext(text)
165 int ModApiClient::l_gettext(lua_State *L)
166 {
167         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
168         lua_pushstring(L, text.c_str());
169
170         return 1;
171 }
172
173 // get_node(pos)
174 // pos = {x=num, y=num, z=num}
175 int ModApiClient::l_get_node_or_nil(lua_State *L)
176 {
177         // pos
178         v3s16 pos = read_v3s16(L, 1);
179
180         // Do it
181         bool pos_ok;
182         MapNode n = getClient(L)->getNode(pos, &pos_ok);
183         if (pos_ok) {
184                 // Return node
185                 pushnode(L, n, getClient(L)->ndef());
186         } else {
187                 lua_pushnil(L);
188         }
189         return 1;
190 }
191
192 int ModApiClient::l_get_wielded_item(lua_State *L)
193 {
194         Client *client = getClient(L);
195
196         Inventory local_inventory(client->idef());
197         client->getLocalInventory(local_inventory);
198
199         InventoryList *mlist = local_inventory.getList("main");
200
201         if (mlist && client->getPlayerItem() < mlist->getSize()) {
202                 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
203         } else {
204                 LuaItemStack::create(L, ItemStack());
205         }
206         return 1;
207 }
208
209 // get_meta(pos)
210 int ModApiClient::l_get_meta(lua_State *L)
211 {
212         v3s16 p = read_v3s16(L, 1);
213         NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
214         NodeMetaRef::createClient(L, meta);
215         return 1;
216 }
217
218 int ModApiClient::l_sound_play(lua_State *L)
219 {
220         ISoundManager *sound = getClient(L)->getSoundManager();
221
222         SimpleSoundSpec spec;
223         read_soundspec(L, 1, spec);
224         float gain = 1.0f;
225         float pitch = 1.0f;
226         bool looped = false;
227         s32 handle;
228
229         if (lua_istable(L, 2)) {
230                 getfloatfield(L, 2, "gain", gain);
231                 getfloatfield(L, 2, "pitch", pitch);
232                 getboolfield(L, 2, "loop", looped);
233
234                 lua_getfield(L, 2, "pos");
235                 if (!lua_isnil(L, -1)) {
236                         v3f pos = read_v3f(L, -1) * BS;
237                         lua_pop(L, 1);
238                         handle = sound->playSoundAt(
239                                         spec.name, looped, gain * spec.gain, pos, pitch);
240                         lua_pushinteger(L, handle);
241                         return 1;
242                 }
243         }
244
245         handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
246         lua_pushinteger(L, handle);
247
248         return 1;
249 }
250
251 int ModApiClient::l_sound_stop(lua_State *L)
252 {
253         u32 handle = luaL_checkinteger(L, 1);
254
255         getClient(L)->getSoundManager()->stopSound(handle);
256
257         return 0;
258 }
259
260 // get_server_info()
261 int ModApiClient::l_get_server_info(lua_State *L)
262 {
263         Client *client = getClient(L);
264         Address serverAddress = client->getServerAddress();
265         lua_newtable(L);
266         lua_pushstring(L, client->getAddressName().c_str());
267         lua_setfield(L, -2, "address");
268         lua_pushstring(L, serverAddress.serializeString().c_str());
269         lua_setfield(L, -2, "ip");
270         lua_pushinteger(L, serverAddress.getPort());
271         lua_setfield(L, -2, "port");
272         lua_pushinteger(L, client->getProtoVersion());
273         lua_setfield(L, -2, "protocol_version");
274         return 1;
275 }
276
277 // get_item_def(itemstring)
278 int ModApiClient::l_get_item_def(lua_State *L)
279 {
280         IGameDef *gdef = getGameDef(L);
281         assert(gdef);
282
283         IItemDefManager *idef = gdef->idef();
284         assert(idef);
285
286         if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_ITEMDEFS))
287                 return 0;
288
289         if (!lua_isstring(L, 1))
290                 return 0;
291
292         const std::string &name(lua_tostring(L, 1));
293         if (!idef->isKnown(name))
294                 return 0;
295         const ItemDefinition &def = idef->get(name);
296
297         push_item_definition_full(L, def);
298
299         return 1;
300 }
301
302 // get_node_def(nodename)
303 int ModApiClient::l_get_node_def(lua_State *L)
304 {
305         IGameDef *gdef = getGameDef(L);
306         assert(gdef);
307
308         INodeDefManager *ndef = gdef->ndef();
309         assert(ndef);
310
311         if (!lua_isstring(L, 1))
312                 return 0;
313
314         if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_NODEDEFS))
315                 return 0;
316
317         const std::string &name = lua_tostring(L, 1);
318         const ContentFeatures &cf = ndef->get(ndef->getId(name));
319         if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
320                 return 0;
321
322         push_content_features(L, cf);
323
324         return 1;
325 }
326
327 int ModApiClient::l_take_screenshot(lua_State *L)
328 {
329         Client *client = getClient(L);
330         client->makeScreenshot();
331         return 0;
332 }
333
334 int ModApiClient::l_get_privilege_list(lua_State *L)
335 {
336         const Client *client = getClient(L);
337         lua_newtable(L);
338         for (const std::string &priv : client->getPrivilegeList()) {
339                 lua_pushboolean(L, true);
340                 lua_setfield(L, -2, priv.c_str());
341         }
342         return 1;
343 }
344
345 // get_builtin_path()
346 int ModApiClient::l_get_builtin_path(lua_State *L)
347 {
348         lua_pushstring(L, BUILTIN_MOD_NAME ":");
349         return 1;
350 }
351
352 void ModApiClient::Initialize(lua_State *L, int top)
353 {
354         API_FCT(get_current_modname);
355         API_FCT(print);
356         API_FCT(display_chat_message);
357         API_FCT(send_chat_message);
358         API_FCT(clear_out_chat_queue);
359         API_FCT(get_player_names);
360         API_FCT(set_last_run_mod);
361         API_FCT(get_last_run_mod);
362         API_FCT(show_formspec);
363         API_FCT(send_respawn);
364         API_FCT(gettext);
365         API_FCT(get_node_or_nil);
366         API_FCT(get_wielded_item);
367         API_FCT(disconnect);
368         API_FCT(get_meta);
369         API_FCT(sound_play);
370         API_FCT(sound_stop);
371         API_FCT(get_server_info);
372         API_FCT(get_item_def);
373         API_FCT(get_node_def);
374         API_FCT(take_screenshot);
375         API_FCT(get_privilege_list);
376         API_FCT(get_builtin_path);
377 }