]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_client.cpp
Code modernization: subfolders (#6283)
[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
38 int ModApiClient::l_get_current_modname(lua_State *L)
39 {
40         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
41         return 1;
42 }
43
44 // get_last_run_mod()
45 int ModApiClient::l_get_last_run_mod(lua_State *L)
46 {
47         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
48         const char *current_mod = lua_tostring(L, -1);
49         if (current_mod == NULL || current_mod[0] == '\0') {
50                 lua_pop(L, 1);
51                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
52         }
53         return 1;
54 }
55
56 // set_last_run_mod(modname)
57 int ModApiClient::l_set_last_run_mod(lua_State *L)
58 {
59         if (!lua_isstring(L, 1))
60                 return 0;
61
62         const char *mod = lua_tostring(L, 1);
63         getScriptApiBase(L)->setOriginDirect(mod);
64         lua_pushboolean(L, true);
65         return 1;
66 }
67
68 // print(text)
69 int ModApiClient::l_print(lua_State *L)
70 {
71         NO_MAP_LOCK_REQUIRED;
72         std::string text = luaL_checkstring(L, 1);
73         rawstream << text << std::endl;
74         return 0;
75 }
76
77 // display_chat_message(message)
78 int ModApiClient::l_display_chat_message(lua_State *L)
79 {
80         if (!lua_isstring(L, 1))
81                 return 0;
82
83         std::string message = luaL_checkstring(L, 1);
84         getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
85         lua_pushboolean(L, true);
86         return 1;
87 }
88
89 // send_chat_message(message)
90 int ModApiClient::l_send_chat_message(lua_State *L)
91 {
92         if (!lua_isstring(L, 1))
93                 return 0;
94
95         // If server disabled this API, discard
96         if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_CHAT_MESSAGES))
97                 return 0;
98
99         std::string message = luaL_checkstring(L, 1);
100         getClient(L)->sendChatMessage(utf8_to_wide(message));
101         return 0;
102 }
103
104 // clear_out_chat_queue()
105 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
106 {
107         getClient(L)->clearOutChatQueue();
108         return 0;
109 }
110
111 // get_player_names()
112 int ModApiClient::l_get_player_names(lua_State *L)
113 {
114         const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
115         lua_createtable(L, plist.size(), 0);
116         int newTable = lua_gettop(L);
117         int index = 1;
118         std::list<std::string>::const_iterator iter;
119         for (iter = plist.begin(); iter != plist.end(); ++iter) {
120                 lua_pushstring(L, (*iter).c_str());
121                 lua_rawseti(L, newTable, index);
122                 index++;
123         }
124         return 1;
125 }
126
127 // show_formspec(formspec)
128 int ModApiClient::l_show_formspec(lua_State *L)
129 {
130         if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
131                 return 0;
132
133         ClientEvent event;
134         event.type = CE_SHOW_LOCAL_FORMSPEC;
135         event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
136         event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
137         getClient(L)->pushToEventQueue(event);
138         lua_pushboolean(L, true);
139         return 1;
140 }
141
142 // send_respawn()
143 int ModApiClient::l_send_respawn(lua_State *L)
144 {
145         getClient(L)->sendRespawn();
146         return 0;
147 }
148
149 // disconnect()
150 int ModApiClient::l_disconnect(lua_State *L)
151 {
152         // Stops badly written Lua code form causing boot loops
153         if (getClient(L)->isShutdown()) {
154                 lua_pushboolean(L, false);
155                 return 1;
156         }
157
158         g_gamecallback->disconnect();
159         lua_pushboolean(L, true);
160         return 1;
161 }
162
163 // gettext(text)
164 int ModApiClient::l_gettext(lua_State *L)
165 {
166         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
167         lua_pushstring(L, text.c_str());
168
169         return 1;
170 }
171
172 // get_node(pos)
173 // pos = {x=num, y=num, z=num}
174 int ModApiClient::l_get_node_or_nil(lua_State *L)
175 {
176         // pos
177         v3s16 pos = read_v3s16(L, 1);
178
179         // Do it
180         bool pos_ok;
181         MapNode n = getClient(L)->getNode(pos, &pos_ok);
182         if (pos_ok) {
183                 // Return node
184                 pushnode(L, n, getClient(L)->ndef());
185         } else {
186                 lua_pushnil(L);
187         }
188         return 1;
189 }
190
191 int ModApiClient::l_get_wielded_item(lua_State *L)
192 {
193         Client *client = getClient(L);
194
195         Inventory local_inventory(client->idef());
196         client->getLocalInventory(local_inventory);
197
198         InventoryList *mlist = local_inventory.getList("main");
199
200         if (mlist && client->getPlayerItem() < mlist->getSize()) {
201                 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
202         } else {
203                 LuaItemStack::create(L, ItemStack());
204         }
205         return 1;
206 }
207
208 // get_meta(pos)
209 int ModApiClient::l_get_meta(lua_State *L)
210 {
211         v3s16 p = read_v3s16(L, 1);
212         NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
213         NodeMetaRef::createClient(L, meta);
214         return 1;
215 }
216
217 int ModApiClient::l_sound_play(lua_State *L)
218 {
219         ISoundManager *sound = getClient(L)->getSoundManager();
220
221         SimpleSoundSpec spec;
222         read_soundspec(L, 1, spec);
223         float gain = 1.0f;
224         float pitch = 1.0f;
225         bool looped = false;
226         s32 handle;
227
228         if (lua_istable(L, 2)) {
229                 getfloatfield(L, 2, "gain", gain);
230                 getfloatfield(L, 2, "pitch", pitch);
231                 getboolfield(L, 2, "loop", looped);
232
233                 lua_getfield(L, 2, "pos");
234                 if (!lua_isnil(L, -1)) {
235                         v3f pos = read_v3f(L, -1) * BS;
236                         lua_pop(L, 1);
237                         handle = sound->playSoundAt(
238                                         spec.name, looped, gain * spec.gain, pos, pitch);
239                         lua_pushinteger(L, handle);
240                         return 1;
241                 }
242         }
243
244         handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
245         lua_pushinteger(L, handle);
246
247         return 1;
248 }
249
250 int ModApiClient::l_sound_stop(lua_State *L)
251 {
252         u32 handle = luaL_checkinteger(L, 1);
253
254         getClient(L)->getSoundManager()->stopSound(handle);
255
256         return 0;
257 }
258
259 // get_server_info()
260 int ModApiClient::l_get_server_info(lua_State *L)
261 {
262         Client *client = getClient(L);
263         Address serverAddress = client->getServerAddress();
264         lua_newtable(L);
265         lua_pushstring(L, client->getAddressName().c_str());
266         lua_setfield(L, -2, "address");
267         lua_pushstring(L, serverAddress.serializeString().c_str());
268         lua_setfield(L, -2, "ip");
269         lua_pushinteger(L, serverAddress.getPort());
270         lua_setfield(L, -2, "port");
271         lua_pushinteger(L, client->getProtoVersion());
272         lua_setfield(L, -2, "protocol_version");
273         return 1;
274 }
275
276 // get_item_def(itemstring)
277 int ModApiClient::l_get_item_def(lua_State *L)
278 {
279         IGameDef *gdef = getGameDef(L);
280         assert(gdef);
281
282         IItemDefManager *idef = gdef->idef();
283         assert(idef);
284
285         if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_ITEMDEFS))
286                 return 0;
287
288         if (!lua_isstring(L, 1))
289                 return 0;
290
291         const std::string &name(lua_tostring(L, 1));
292         if (!idef->isKnown(name))
293                 return 0;
294         const ItemDefinition &def = idef->get(name);
295
296         push_item_definition_full(L, def);
297
298         return 1;
299 }
300
301 // get_node_def(nodename)
302 int ModApiClient::l_get_node_def(lua_State *L)
303 {
304         IGameDef *gdef = getGameDef(L);
305         assert(gdef);
306
307         INodeDefManager *ndef = gdef->ndef();
308         assert(ndef);
309
310         if (!lua_isstring(L, 1))
311                 return 0;
312
313         if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_NODEDEFS))
314                 return 0;
315
316         const std::string &name = lua_tostring(L, 1);
317         const ContentFeatures &cf = ndef->get(ndef->getId(name));
318         if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
319                 return 0;
320
321         push_content_features(L, cf);
322
323         return 1;
324 }
325
326 int ModApiClient::l_take_screenshot(lua_State *L)
327 {
328         Client *client = getClient(L);
329         client->makeScreenshot();
330         return 0;
331 }
332
333 int ModApiClient::l_get_privilege_list(lua_State *L)
334 {
335         const Client *client = getClient(L);
336         lua_newtable(L);
337         for (const std::string &priv : client->getPrivilegeList()) {
338                 lua_pushboolean(L, true);
339                 lua_setfield(L, -2, priv.c_str());
340         }
341         return 1;
342 }
343
344 // get_builtin_path()
345 int ModApiClient::l_get_builtin_path(lua_State *L)
346 {
347         lua_pushstring(L, BUILTIN_MOD_NAME ":");
348         return 1;
349 }
350
351 void ModApiClient::Initialize(lua_State *L, int top)
352 {
353         API_FCT(get_current_modname);
354         API_FCT(print);
355         API_FCT(display_chat_message);
356         API_FCT(send_chat_message);
357         API_FCT(clear_out_chat_queue);
358         API_FCT(get_player_names);
359         API_FCT(set_last_run_mod);
360         API_FCT(get_last_run_mod);
361         API_FCT(show_formspec);
362         API_FCT(send_respawn);
363         API_FCT(gettext);
364         API_FCT(get_node_or_nil);
365         API_FCT(get_wielded_item);
366         API_FCT(disconnect);
367         API_FCT(get_meta);
368         API_FCT(sound_play);
369         API_FCT(sound_stop);
370         API_FCT(get_server_info);
371         API_FCT(get_item_def);
372         API_FCT(get_node_def);
373         API_FCT(take_screenshot);
374         API_FCT(get_privilege_list);
375         API_FCT(get_builtin_path);
376 }