]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_client.cpp
0f4d7eaae4a5bfc58334f53cd6b2999f3e40b3b4
[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
34 extern MainGameCallback *g_gamecallback;
35
36 int ModApiClient::l_get_current_modname(lua_State *L)
37 {
38         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
39         return 1;
40 }
41
42 // get_last_run_mod()
43 int ModApiClient::l_get_last_run_mod(lua_State *L)
44 {
45         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
46         const char *current_mod = lua_tostring(L, -1);
47         if (current_mod == NULL || current_mod[0] == '\0') {
48                 lua_pop(L, 1);
49                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
50         }
51         return 1;
52 }
53
54 // set_last_run_mod(modname)
55 int ModApiClient::l_set_last_run_mod(lua_State *L)
56 {
57         if (!lua_isstring(L, 1))
58                 return 0;
59
60         const char *mod = lua_tostring(L, 1);
61         getScriptApiBase(L)->setOriginDirect(mod);
62         lua_pushboolean(L, true);
63         return 1;
64 }
65
66 // display_chat_message(message)
67 int ModApiClient::l_display_chat_message(lua_State *L)
68 {
69         if (!lua_isstring(L, 1))
70                 return 0;
71
72         std::string message = luaL_checkstring(L, 1);
73         getClient(L)->pushToChatQueue(utf8_to_wide(message));
74         lua_pushboolean(L, true);
75         return 1;
76 }
77
78 // get_player_names()
79 int ModApiClient::l_get_player_names(lua_State *L)
80 {
81         const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
82         lua_createtable(L, plist.size(), 0);
83         int newTable = lua_gettop(L);
84         int index = 1;
85         std::list<std::string>::const_iterator iter;
86         for (iter = plist.begin(); iter != plist.end(); iter++) {
87                 lua_pushstring(L, (*iter).c_str());
88                 lua_rawseti(L, newTable, index);
89                 index++;
90         }
91         return 1;
92 }
93
94 // show_formspec(formspec)
95 int ModApiClient::l_show_formspec(lua_State *L)
96 {
97         if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
98                 return 0;
99
100         ClientEvent event;
101         event.type = CE_SHOW_LOCAL_FORMSPEC;
102         event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
103         event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
104         getClient(L)->pushToEventQueue(event);
105         lua_pushboolean(L, true);
106         return 1;
107 }
108
109 // send_respawn()
110 int ModApiClient::l_send_respawn(lua_State *L)
111 {
112         getClient(L)->sendRespawn();
113         return 0;
114 }
115
116 // disconnect()
117 int ModApiClient::l_disconnect(lua_State *L)
118 {
119         // Stops badly written Lua code form causing boot loops
120         if (getClient(L)->isShutdown()) {
121                 lua_pushboolean(L, false);
122                 return 1;
123         }
124
125         g_gamecallback->disconnect();
126         lua_pushboolean(L, true);
127         return 1;
128 }
129
130 // gettext(text)
131 int ModApiClient::l_gettext(lua_State *L)
132 {
133         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
134         lua_pushstring(L, text.c_str());
135
136         return 1;
137 }
138
139 // get_node(pos)
140 // pos = {x=num, y=num, z=num}
141 int ModApiClient::l_get_node(lua_State *L)
142 {
143         // pos
144         v3s16 pos = read_v3s16(L, 1);
145         // Do it
146         bool pos_ok;
147         MapNode n = getClient(L)->getNode(pos, &pos_ok);
148         // Return node
149         pushnode(L, n, getClient(L)->ndef());
150         return 1;
151 }
152
153 // get_node_or_nil(pos)
154 // pos = {x=num, y=num, z=num}
155 int ModApiClient::l_get_node_or_nil(lua_State *L)
156 {
157         // pos
158         v3s16 pos = read_v3s16(L, 1);
159         // Do it
160         bool pos_ok;
161         MapNode n = getClient(L)->getNode(pos, &pos_ok);
162         if (pos_ok) {
163                 // Return node
164                 pushnode(L, n, getClient(L)->ndef());
165         } else {
166                 lua_pushnil(L);
167         }
168         return 1;
169 }
170
171 int ModApiClient::l_get_wielded_item(lua_State *L)
172 {
173         Client *client = getClient(L);
174
175         Inventory local_inventory(client->idef());
176         client->getLocalInventory(local_inventory);
177
178         InventoryList *mlist = local_inventory.getList("main");
179
180         if (mlist && client->getPlayerItem() < mlist->getSize()) {
181                 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
182         } else {
183                 LuaItemStack::create(L, ItemStack());
184         }
185         return 1;
186 }
187
188 // get_meta(pos)
189 int ModApiClient::l_get_meta(lua_State *L)
190 {
191         v3s16 p = read_v3s16(L, 1);
192         NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
193         NodeMetaRef::createClient(L, meta);
194         return 1;
195 }
196
197 int ModApiClient::l_sound_play(lua_State *L)
198 {
199         ISoundManager *sound = getClient(L)->getSoundManager();
200
201         SimpleSoundSpec spec;
202         read_soundspec(L, 1, spec);
203         float gain = 1.0;
204         bool looped = false;
205         s32 handle;
206
207         if (lua_istable(L, 2)) {
208                 getfloatfield(L, 2, "gain", gain);
209                 getboolfield(L, 2, "loop", looped);
210
211                 lua_getfield(L, 2, "pos");
212                 if (!lua_isnil(L, -1)) {
213                         v3f pos = read_v3f(L, -1) * BS;
214                         lua_pop(L, 1);
215                         handle =
216                             sound->playSoundAt(spec.name, looped, gain * spec.gain, pos);
217                         lua_pushinteger(L, handle);
218                         return 1;
219                 }
220         }
221
222         handle = sound->playSound(spec.name, looped, gain * spec.gain);
223         lua_pushinteger(L, handle);
224
225         return 1;
226 }
227
228 int ModApiClient::l_sound_stop(lua_State *L)
229 {
230         u32 handle = luaL_checkinteger(L, 1);
231
232         getClient(L)->getSoundManager()->stopSound(handle);
233
234         return 0;
235 }
236
237 // get_protocol_version()
238 int ModApiClient::l_get_protocol_version(lua_State *L)
239 {
240         lua_pushinteger(L, getClient(L)->getProtoVersion());
241         return 1;
242 }
243
244 void ModApiClient::Initialize(lua_State *L, int top)
245 {
246         API_FCT(get_current_modname);
247         API_FCT(display_chat_message);
248         API_FCT(get_player_names);
249         API_FCT(set_last_run_mod);
250         API_FCT(get_last_run_mod);
251         API_FCT(show_formspec);
252         API_FCT(send_respawn);
253         API_FCT(gettext);
254         API_FCT(get_node);
255         API_FCT(get_node_or_nil);
256         API_FCT(get_wielded_item);
257         API_FCT(disconnect);
258         API_FCT(get_meta);
259         API_FCT(sound_play);
260         API_FCT(sound_stop);
261         API_FCT(get_protocol_version);
262 }