]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_client.cpp
Modernize lua read (part 2 & 3): C++ templating assurance (#7410)
[dragonfireclient.git] / src / script / cpp_api / s_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 "s_client.h"
22 #include "s_internal.h"
23 #include "client.h"
24 #include "common/c_converter.h"
25 #include "common/c_content.h"
26 #include "s_item.h"
27
28 void ScriptApiClient::on_mods_loaded()
29 {
30         SCRIPTAPI_PRECHECKHEADER
31
32         // Get registered shutdown hooks
33         lua_getglobal(L, "core");
34         lua_getfield(L, -1, "registered_on_mods_loaded");
35         // Call callbacks
36         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
37 }
38
39 void ScriptApiClient::on_shutdown()
40 {
41         SCRIPTAPI_PRECHECKHEADER
42
43         // Get registered shutdown hooks
44         lua_getglobal(L, "core");
45         lua_getfield(L, -1, "registered_on_shutdown");
46         // Call callbacks
47         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
48 }
49
50 bool ScriptApiClient::on_sending_message(const std::string &message)
51 {
52         SCRIPTAPI_PRECHECKHEADER
53
54         // Get core.registered_on_chat_messages
55         lua_getglobal(L, "core");
56         lua_getfield(L, -1, "registered_on_sending_chat_message");
57         // Call callbacks
58         lua_pushstring(L, message.c_str());
59         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
60         return readParam<bool>(L, -1);
61 }
62
63 bool ScriptApiClient::on_receiving_message(const std::string &message)
64 {
65         SCRIPTAPI_PRECHECKHEADER
66
67         // Get core.registered_on_chat_messages
68         lua_getglobal(L, "core");
69         lua_getfield(L, -1, "registered_on_receiving_chat_message");
70         // Call callbacks
71         lua_pushstring(L, message.c_str());
72         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
73         return readParam<bool>(L, -1);
74 }
75
76 void ScriptApiClient::on_damage_taken(int32_t damage_amount)
77 {
78         SCRIPTAPI_PRECHECKHEADER
79
80         // Get core.registered_on_chat_messages
81         lua_getglobal(L, "core");
82         lua_getfield(L, -1, "registered_on_damage_taken");
83         // Call callbacks
84         lua_pushinteger(L, damage_amount);
85         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
86 }
87
88 void ScriptApiClient::on_hp_modification(int32_t newhp)
89 {
90         SCRIPTAPI_PRECHECKHEADER
91
92         // Get core.registered_on_chat_messages
93         lua_getglobal(L, "core");
94         lua_getfield(L, -1, "registered_on_hp_modification");
95         // Call callbacks
96         lua_pushinteger(L, newhp);
97         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
98 }
99
100 void ScriptApiClient::on_death()
101 {
102         SCRIPTAPI_PRECHECKHEADER
103
104         // Get registered shutdown hooks
105         lua_getglobal(L, "core");
106         lua_getfield(L, -1, "registered_on_death");
107         // Call callbacks
108         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
109 }
110
111 void ScriptApiClient::environment_step(float dtime)
112 {
113         SCRIPTAPI_PRECHECKHEADER
114
115         // Get core.registered_globalsteps
116         lua_getglobal(L, "core");
117         lua_getfield(L, -1, "registered_globalsteps");
118         // Call callbacks
119         lua_pushnumber(L, dtime);
120         try {
121                 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
122         } catch (LuaError &e) {
123                 getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
124                                 + script_get_backtrace(L));
125         }
126 }
127
128 void ScriptApiClient::on_formspec_input(const std::string &formname,
129         const StringMap &fields)
130 {
131         SCRIPTAPI_PRECHECKHEADER
132
133         // Get core.registered_on_chat_messages
134         lua_getglobal(L, "core");
135         lua_getfield(L, -1, "registered_on_formspec_input");
136         // Call callbacks
137         // param 1
138         lua_pushstring(L, formname.c_str());
139         // param 2
140         lua_newtable(L);
141         StringMap::const_iterator it;
142         for (it = fields.begin(); it != fields.end(); ++it) {
143                 const std::string &name = it->first;
144                 const std::string &value = it->second;
145                 lua_pushstring(L, name.c_str());
146                 lua_pushlstring(L, value.c_str(), value.size());
147                 lua_settable(L, -3);
148         }
149         runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
150 }
151
152 bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
153 {
154         SCRIPTAPI_PRECHECKHEADER
155
156         const NodeDefManager *ndef = getClient()->ndef();
157
158         // Get core.registered_on_dignode
159         lua_getglobal(L, "core");
160         lua_getfield(L, -1, "registered_on_dignode");
161
162         // Push data
163         push_v3s16(L, p);
164         pushnode(L, node, ndef);
165
166         // Call functions
167         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
168         return lua_toboolean(L, -1);
169 }
170
171 bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
172 {
173         SCRIPTAPI_PRECHECKHEADER
174
175         const NodeDefManager *ndef = getClient()->ndef();
176
177         // Get core.registered_on_punchgnode
178         lua_getglobal(L, "core");
179         lua_getfield(L, -1, "registered_on_punchnode");
180
181         // Push data
182         push_v3s16(L, p);
183         pushnode(L, node, ndef);
184
185         // Call functions
186         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
187         return readParam<bool>(L, -1);
188 }
189
190 bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
191 {
192         SCRIPTAPI_PRECHECKHEADER
193
194         // Get core.registered_on_placenode
195         lua_getglobal(L, "core");
196         lua_getfield(L, -1, "registered_on_placenode");
197
198         // Push data
199         push_pointed_thing(L, pointed, true);
200         push_item_definition(L, item);
201
202         // Call functions
203         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
204         return readParam<bool>(L, -1);
205 }
206
207 bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
208 {
209         SCRIPTAPI_PRECHECKHEADER
210
211         // Get core.registered_on_item_use
212         lua_getglobal(L, "core");
213         lua_getfield(L, -1, "registered_on_item_use");
214
215         // Push data
216         LuaItemStack::create(L, item);
217         push_pointed_thing(L, pointed, true);
218
219         // Call functions
220         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
221         return readParam<bool>(L, -1);
222 }
223
224 bool ScriptApiClient::on_inventory_open(Inventory *inventory)
225 {
226         SCRIPTAPI_PRECHECKHEADER
227
228         lua_getglobal(L, "core");
229         lua_getfield(L, -1, "registered_on_inventory_open");
230
231         std::vector<const InventoryList*> lists = inventory->getLists();
232         std::vector<const InventoryList*>::iterator iter = lists.begin();
233         lua_createtable(L, 0, lists.size());
234         for (; iter != lists.end(); iter++) {
235                 const char* name = (*iter)->getName().c_str();
236                 lua_pushstring(L, name);
237                 push_inventory_list(L, inventory, name);
238                 lua_rawset(L, -3);
239         }
240
241         runCallbacks(1, RUN_CALLBACKS_MODE_OR);
242         return readParam<bool>(L, -1);
243 }
244
245 void ScriptApiClient::setEnv(ClientEnvironment *env)
246 {
247         ScriptApiBase::setEnv(env);
248 }