]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_client.cpp
Order es_DrawType exactly like enum NodeDrawType in nodedef.h (#5946)
[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_shutdown()
29 {
30         SCRIPTAPI_PRECHECKHEADER
31
32         // Get registered shutdown hooks
33         lua_getglobal(L, "core");
34         lua_getfield(L, -1, "registered_on_shutdown");
35         // Call callbacks
36         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
37 }
38
39 void ScriptApiClient::on_connect()
40 {
41         SCRIPTAPI_PRECHECKHEADER
42
43         // get registered connect hooks
44         lua_getglobal(L, "core");
45         lua_getfield(L, -1, "registered_on_connect");
46         // Call callback
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         bool ate = lua_toboolean(L, -1);
61         return ate;
62 }
63
64 bool ScriptApiClient::on_receiving_message(const std::string &message)
65 {
66         SCRIPTAPI_PRECHECKHEADER
67
68         // Get core.registered_on_chat_messages
69         lua_getglobal(L, "core");
70         lua_getfield(L, -1, "registered_on_receiving_chat_message");
71         // Call callbacks
72         lua_pushstring(L, message.c_str());
73         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
74         bool ate = lua_toboolean(L, -1);
75         return ate;
76 }
77
78 void ScriptApiClient::on_damage_taken(int32_t damage_amount)
79 {
80         SCRIPTAPI_PRECHECKHEADER
81
82         // Get core.registered_on_chat_messages
83         lua_getglobal(L, "core");
84         lua_getfield(L, -1, "registered_on_damage_taken");
85         // Call callbacks
86         lua_pushinteger(L, damage_amount);
87         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
88 }
89
90 void ScriptApiClient::on_hp_modification(int32_t newhp)
91 {
92         SCRIPTAPI_PRECHECKHEADER
93
94         // Get core.registered_on_chat_messages
95         lua_getglobal(L, "core");
96         lua_getfield(L, -1, "registered_on_hp_modification");
97         // Call callbacks
98         lua_pushinteger(L, newhp);
99         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
100 }
101
102 void ScriptApiClient::on_death()
103 {
104         SCRIPTAPI_PRECHECKHEADER
105
106         // Get registered shutdown hooks
107         lua_getglobal(L, "core");
108         lua_getfield(L, -1, "registered_on_death");
109         // Call callbacks
110         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
111 }
112
113 void ScriptApiClient::environment_step(float dtime)
114 {
115         SCRIPTAPI_PRECHECKHEADER
116
117         // Get core.registered_globalsteps
118         lua_getglobal(L, "core");
119         lua_getfield(L, -1, "registered_globalsteps");
120         // Call callbacks
121         lua_pushnumber(L, dtime);
122         try {
123                 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
124         } catch (LuaError &e) {
125                 getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
126                                 + script_get_backtrace(L));
127         }
128 }
129
130 void ScriptApiClient::on_formspec_input(const std::string &formname,
131         const StringMap &fields)
132 {
133         SCRIPTAPI_PRECHECKHEADER
134
135         // Get core.registered_on_chat_messages
136         lua_getglobal(L, "core");
137         lua_getfield(L, -1, "registered_on_formspec_input");
138         // Call callbacks
139         // param 1
140         lua_pushstring(L, formname.c_str());
141         // param 2
142         lua_newtable(L);
143         StringMap::const_iterator it;
144         for (it = fields.begin(); it != fields.end(); ++it) {
145                 const std::string &name = it->first;
146                 const std::string &value = it->second;
147                 lua_pushstring(L, name.c_str());
148                 lua_pushlstring(L, value.c_str(), value.size());
149                 lua_settable(L, -3);
150         }
151         runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
152 }
153
154 bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
155 {
156         SCRIPTAPI_PRECHECKHEADER
157
158         INodeDefManager *ndef = getClient()->ndef();
159
160         // Get core.registered_on_dignode
161         lua_getglobal(L, "core");
162         lua_getfield(L, -1, "registered_on_dignode");
163
164         // Push data
165         push_v3s16(L, p);
166         pushnode(L, node, ndef);
167
168         // Call functions
169         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
170         return lua_toboolean(L, -1);
171 }
172
173 bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
174 {
175         SCRIPTAPI_PRECHECKHEADER
176
177         INodeDefManager *ndef = getClient()->ndef();
178
179         // Get core.registered_on_punchgnode
180         lua_getglobal(L, "core");
181         lua_getfield(L, -1, "registered_on_punchnode");
182
183         // Push data
184         push_v3s16(L, p);
185         pushnode(L, node, ndef);
186
187         // Call functions
188         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
189         bool blocked = lua_toboolean(L, -1);
190         return blocked;
191 }
192
193 bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
194 {
195         SCRIPTAPI_PRECHECKHEADER
196
197         // Get core.registered_on_placenode
198         lua_getglobal(L, "core");
199         lua_getfield(L, -1, "registered_on_placenode");
200
201         // Push data
202         push_pointed_thing(L, pointed, true);
203         push_item_definition(L, item);
204
205         // Call functions
206         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
207         return lua_toboolean(L, -1);
208 }
209
210 bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
211 {
212         SCRIPTAPI_PRECHECKHEADER
213
214         // Get core.registered_on_item_use
215         lua_getglobal(L, "core");
216         lua_getfield(L, -1, "registered_on_item_use");
217
218         // Push data
219         LuaItemStack::create(L, item);
220         push_pointed_thing(L, pointed, true);
221
222         // Call functions
223         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
224         return lua_toboolean(L, -1);
225 }
226
227 void ScriptApiClient::setEnv(ClientEnvironment *env)
228 {
229         ScriptApiBase::setEnv(env);
230 }