]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_client.cpp
154dd61947dcc5395423937fffb99e24371b9779
[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
27 void ScriptApiClient::on_shutdown()
28 {
29         SCRIPTAPI_PRECHECKHEADER
30
31         // Get registered shutdown hooks
32         lua_getglobal(L, "core");
33         lua_getfield(L, -1, "registered_on_shutdown");
34         // Call callbacks
35         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
36 }
37
38 bool ScriptApiClient::on_sending_message(const std::string &message)
39 {
40         SCRIPTAPI_PRECHECKHEADER
41
42         // Get core.registered_on_chat_messages
43         lua_getglobal(L, "core");
44         lua_getfield(L, -1, "registered_on_sending_chat_messages");
45         // Call callbacks
46         lua_pushstring(L, message.c_str());
47         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
48         bool ate = lua_toboolean(L, -1);
49         return ate;
50 }
51
52 bool ScriptApiClient::on_receiving_message(const std::string &message)
53 {
54         SCRIPTAPI_PRECHECKHEADER
55
56         // Get core.registered_on_chat_messages
57         lua_getglobal(L, "core");
58         lua_getfield(L, -1, "registered_on_receiving_chat_messages");
59         // Call callbacks
60         lua_pushstring(L, message.c_str());
61         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
62         bool ate = lua_toboolean(L, -1);
63         return ate;
64 }
65
66 void ScriptApiClient::on_damage_taken(int32_t damage_amount)
67 {
68         SCRIPTAPI_PRECHECKHEADER
69
70         // Get core.registered_on_chat_messages
71         lua_getglobal(L, "core");
72         lua_getfield(L, -1, "registered_on_damage_taken");
73         // Call callbacks
74         lua_pushinteger(L, damage_amount);
75         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
76 }
77
78 void ScriptApiClient::on_hp_modification(int32_t newhp)
79 {
80         SCRIPTAPI_PRECHECKHEADER
81
82         // Get core.registered_on_chat_messages
83         lua_getglobal(L, "core");
84         lua_getfield(L, -1, "registered_on_hp_modification");
85         // Call callbacks
86         lua_pushinteger(L, newhp);
87         runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
88 }
89
90 void ScriptApiClient::on_death()
91 {
92         SCRIPTAPI_PRECHECKHEADER
93
94         // Get registered shutdown hooks
95         lua_getglobal(L, "core");
96         lua_getfield(L, -1, "registered_on_death");
97         // Call callbacks
98         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
99 }
100
101 void ScriptApiClient::environment_step(float dtime)
102 {
103         SCRIPTAPI_PRECHECKHEADER
104
105         // Get core.registered_globalsteps
106         lua_getglobal(L, "core");
107         lua_getfield(L, -1, "registered_globalsteps");
108         // Call callbacks
109         lua_pushnumber(L, dtime);
110         try {
111                 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
112         } catch (LuaError &e) {
113                 getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
114                                 + script_get_backtrace(L));
115         }
116 }
117
118 void ScriptApiClient::on_formspec_input(const std::string &formname,
119         const StringMap &fields)
120 {
121         SCRIPTAPI_PRECHECKHEADER
122
123         // Get core.registered_on_chat_messages
124         lua_getglobal(L, "core");
125         lua_getfield(L, -1, "registered_on_formspec_input");
126         // Call callbacks
127         // param 1
128         lua_pushstring(L, formname.c_str());
129         // param 2
130         lua_newtable(L);
131         StringMap::const_iterator it;
132         for (it = fields.begin(); it != fields.end(); ++it) {
133                 const std::string &name = it->first;
134                 const std::string &value = it->second;
135                 lua_pushstring(L, name.c_str());
136                 lua_pushlstring(L, value.c_str(), value.size());
137                 lua_settable(L, -3);
138         }
139         runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
140 }
141
142 bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
143 {
144         SCRIPTAPI_PRECHECKHEADER
145
146         INodeDefManager *ndef = getClient()->ndef();
147
148         // Get core.registered_on_dignode
149         lua_getglobal(L, "core");
150         lua_getfield(L, -1, "registered_on_dignode");
151
152         // Push data
153         push_v3s16(L, p);
154         pushnode(L, node, ndef);
155
156         // Call functions
157         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
158         return lua_toboolean(L, -1);
159 }
160
161 bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
162 {
163         SCRIPTAPI_PRECHECKHEADER
164
165         INodeDefManager *ndef = getClient()->ndef();
166
167         // Get core.registered_on_punchgnode
168         lua_getglobal(L, "core");
169         lua_getfield(L, -1, "registered_on_punchnode");
170
171         // Push data
172         push_v3s16(L, p);
173         pushnode(L, node, ndef);
174
175         // Call functions
176         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
177         bool blocked = lua_toboolean(L, -1);
178         return blocked;
179 }