]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_client.cpp
5f94747029dea11d6c4d827adab118dde05e3753
[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 "common/c_content.h"
23 #include "common/c_converter.h"
24 #include "cpp_api/s_base.h"
25 #include "gettext.h"
26 #include "l_internal.h"
27 #include "lua_api/l_item.h"
28 #include "mainmenumanager.h"
29 #include "util/string.h"
30
31 extern MainGameCallback *g_gamecallback;
32
33 int ModApiClient::l_get_current_modname(lua_State *L)
34 {
35         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
36         return 1;
37 }
38
39 // get_last_run_mod()
40 int ModApiClient::l_get_last_run_mod(lua_State *L)
41 {
42         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
43         const char *current_mod = lua_tostring(L, -1);
44         if (current_mod == NULL || current_mod[0] == '\0') {
45                 lua_pop(L, 1);
46                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
47         }
48         return 1;
49 }
50
51 // set_last_run_mod(modname)
52 int ModApiClient::l_set_last_run_mod(lua_State *L)
53 {
54         if (!lua_isstring(L, 1))
55                 return 0;
56
57         const char *mod = lua_tostring(L, 1);
58         getScriptApiBase(L)->setOriginDirect(mod);
59         lua_pushboolean(L, true);
60         return 1;
61 }
62
63 // display_chat_message(message)
64 int ModApiClient::l_display_chat_message(lua_State *L)
65 {
66         if (!lua_isstring(L, 1))
67                 return 0;
68
69         std::string message = luaL_checkstring(L, 1);
70         getClient(L)->pushToChatQueue(utf8_to_wide(message));
71         lua_pushboolean(L, true);
72         return 1;
73 }
74
75 // get_player_names()
76 int ModApiClient::l_get_player_names(lua_State *L)
77 {
78         const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
79         lua_createtable(L, plist.size(), 0);
80         int newTable = lua_gettop(L);
81         int index = 1;
82         std::list<std::string>::const_iterator iter;
83         for (iter = plist.begin(); iter != plist.end(); iter++) {
84                 lua_pushstring(L, (*iter).c_str());
85                 lua_rawseti(L, newTable, index);
86                 index++;
87         }
88         return 1;
89 }
90
91 // show_formspec(formspec)
92 int ModApiClient::l_show_formspec(lua_State *L)
93 {
94         if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
95                 return 0;
96
97         ClientEvent event;
98         event.type = CE_SHOW_LOCAL_FORMSPEC;
99         event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
100         event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
101         getClient(L)->pushToEventQueue(event);
102         lua_pushboolean(L, true);
103         return 1;
104 }
105
106 // send_respawn()
107 int ModApiClient::l_send_respawn(lua_State *L)
108 {
109         getClient(L)->sendRespawn();
110         return 0;
111 }
112
113 // disconnect()
114 int ModApiClient::l_disconnect(lua_State *L)
115 {
116         // Stops badly written Lua code form causing boot loops
117         if (getClient(L)->isShutdown()) {
118                 lua_pushboolean(L, false);
119                 return 1;
120         }
121
122         g_gamecallback->disconnect();
123         lua_pushboolean(L, true);
124         return 1;
125 }
126
127 // gettext(text)
128 int ModApiClient::l_gettext(lua_State *L)
129 {
130         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
131         lua_pushstring(L, text.c_str());
132
133         return 1;
134 }
135
136 // get_node(pos)
137 // pos = {x=num, y=num, z=num}
138 int ModApiClient::l_get_node(lua_State *L)
139 {
140         // pos
141         v3s16 pos = read_v3s16(L, 1);
142         // Do it
143         bool pos_ok;
144         MapNode n = getClient(L)->getNode(pos, &pos_ok);
145         // Return node
146         pushnode(L, n, getClient(L)->ndef());
147         return 1;
148 }
149
150 // get_node_or_nil(pos)
151 // pos = {x=num, y=num, z=num}
152 int ModApiClient::l_get_node_or_nil(lua_State *L)
153 {
154         // pos
155         v3s16 pos = read_v3s16(L, 1);
156         // Do it
157         bool pos_ok;
158         MapNode n = getClient(L)->getNode(pos, &pos_ok);
159         if (pos_ok) {
160                 // Return node
161                 pushnode(L, n, getClient(L)->ndef());
162         } else {
163                 lua_pushnil(L);
164         }
165         return 1;
166 }
167
168 int ModApiClient::l_get_wielded_item(lua_State *L)
169 {
170         Client *client = getClient(L);
171
172         Inventory local_inventory(client->idef());
173         client->getLocalInventory(local_inventory);
174
175         InventoryList *mlist = local_inventory.getList("main");
176
177         if (mlist && client->getPlayerItem() < mlist->getSize()) {
178                 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
179         } else {
180                 LuaItemStack::create(L, ItemStack());
181         }
182         return 1;
183 }
184
185 void ModApiClient::Initialize(lua_State *L, int top)
186 {
187         API_FCT(get_current_modname);
188         API_FCT(display_chat_message);
189         API_FCT(get_player_names);
190         API_FCT(set_last_run_mod);
191         API_FCT(get_last_run_mod);
192         API_FCT(show_formspec);
193         API_FCT(send_respawn);
194         API_FCT(gettext);
195         API_FCT(get_node);
196         API_FCT(get_node_or_nil);
197         API_FCT(get_wielded_item);
198         API_FCT(disconnect);
199 }