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