]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_player.cpp
Add reasons to on_dieplayer and on_hpchange
[dragonfireclient.git] / src / script / cpp_api / s_player.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "cpp_api/s_player.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "debug.h"
25 #include "util/string.h"
26
27 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
28 {
29         SCRIPTAPI_PRECHECKHEADER
30
31         // Get core.registered_on_newplayers
32         lua_getglobal(L, "core");
33         lua_getfield(L, -1, "registered_on_newplayers");
34         // Call callbacks
35         objectrefGetOrCreate(L, player);
36         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
37 }
38
39 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player, const PlayerHPChangeReason &reason)
40 {
41         SCRIPTAPI_PRECHECKHEADER
42
43         // Get callback table
44         lua_getglobal(L, "core");
45         lua_getfield(L, -1, "registered_on_dieplayers");
46
47         // Push arguments
48         objectrefGetOrCreate(L, player);
49         pushPlayerHPChangeReason(L, reason);
50
51         // Run callbacks
52         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
53 }
54
55 bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
56                 ServerActiveObject *hitter,
57                 float time_from_last_punch,
58                 const ToolCapabilities *toolcap,
59                 v3f dir,
60                 s16 damage)
61 {
62         SCRIPTAPI_PRECHECKHEADER
63         // Get core.registered_on_punchplayers
64         lua_getglobal(L, "core");
65         lua_getfield(L, -1, "registered_on_punchplayers");
66         // Call callbacks
67         objectrefGetOrCreate(L, player);
68         objectrefGetOrCreate(L, hitter);
69         lua_pushnumber(L, time_from_last_punch);
70         push_tool_capabilities(L, *toolcap);
71         push_v3f(L, dir);
72         lua_pushnumber(L, damage);
73         runCallbacks(6, RUN_CALLBACKS_MODE_OR);
74         return lua_toboolean(L, -1);
75 }
76
77 s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
78         s16 hp_change, const PlayerHPChangeReason &reason)
79 {
80         SCRIPTAPI_PRECHECKHEADER
81
82         int error_handler = PUSH_ERROR_HANDLER(L);
83
84         // Get core.registered_on_player_hpchange
85         lua_getglobal(L, "core");
86         lua_getfield(L, -1, "registered_on_player_hpchange");
87         lua_remove(L, -2);
88
89         // Push arguments
90         objectrefGetOrCreate(L, player);
91         lua_pushnumber(L, hp_change);
92         pushPlayerHPChangeReason(L, reason);
93
94         // Call callbacks
95         PCALL_RES(lua_pcall(L, 3, 1, error_handler));
96         hp_change = lua_tointeger(L, -1);
97         lua_pop(L, 2); // Pop result and error handler
98         return hp_change;
99 }
100
101 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
102 {
103         SCRIPTAPI_PRECHECKHEADER
104
105         // Get core.registered_on_respawnplayers
106         lua_getglobal(L, "core");
107         lua_getfield(L, -1, "registered_on_respawnplayers");
108         // Call callbacks
109         objectrefGetOrCreate(L, player);
110         runCallbacks(1, RUN_CALLBACKS_MODE_OR);
111         bool positioning_handled_by_some = lua_toboolean(L, -1);
112         return positioning_handled_by_some;
113 }
114
115 bool ScriptApiPlayer::on_prejoinplayer(
116         const std::string &name,
117         const std::string &ip,
118         std::string *reason)
119 {
120         SCRIPTAPI_PRECHECKHEADER
121
122         // Get core.registered_on_prejoinplayers
123         lua_getglobal(L, "core");
124         lua_getfield(L, -1, "registered_on_prejoinplayers");
125         lua_pushstring(L, name.c_str());
126         lua_pushstring(L, ip.c_str());
127         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
128         if (lua_isstring(L, -1)) {
129                 reason->assign(lua_tostring(L, -1));
130                 return true;
131         }
132         return false;
133 }
134
135 bool ScriptApiPlayer::can_bypass_userlimit(const std::string &name, const std::string &ip)
136 {
137         SCRIPTAPI_PRECHECKHEADER
138
139         // Get core.registered_on_prejoinplayers
140         lua_getglobal(L, "core");
141         lua_getfield(L, -1, "registered_can_bypass_userlimit");
142         lua_pushstring(L, name.c_str());
143         lua_pushstring(L, ip.c_str());
144         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
145         return lua_toboolean(L, -1);
146 }
147
148 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
149 {
150         SCRIPTAPI_PRECHECKHEADER
151
152         // Get core.registered_on_joinplayers
153         lua_getglobal(L, "core");
154         lua_getfield(L, -1, "registered_on_joinplayers");
155         // Call callbacks
156         objectrefGetOrCreate(L, player);
157         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
158 }
159
160 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
161                 bool timeout)
162 {
163         SCRIPTAPI_PRECHECKHEADER
164
165         // Get core.registered_on_leaveplayers
166         lua_getglobal(L, "core");
167         lua_getfield(L, -1, "registered_on_leaveplayers");
168         // Call callbacks
169         objectrefGetOrCreate(L, player);
170         lua_pushboolean(L, timeout);
171         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
172 }
173
174 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
175                 const std::string &cheat_type)
176 {
177         SCRIPTAPI_PRECHECKHEADER
178
179         // Get core.registered_on_cheats
180         lua_getglobal(L, "core");
181         lua_getfield(L, -1, "registered_on_cheats");
182         // Call callbacks
183         objectrefGetOrCreate(L, player);
184         lua_newtable(L);
185         lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
186         lua_setfield(L, -2, "type");
187         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
188 }
189
190 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
191                 const std::string &formname,
192                 const StringMap &fields)
193 {
194         SCRIPTAPI_PRECHECKHEADER
195
196         // Get core.registered_on_chat_messages
197         lua_getglobal(L, "core");
198         lua_getfield(L, -1, "registered_on_player_receive_fields");
199         // Call callbacks
200         // param 1
201         objectrefGetOrCreate(L, player);
202         // param 2
203         lua_pushstring(L, formname.c_str());
204         // param 3
205         lua_newtable(L);
206         StringMap::const_iterator it;
207         for (it = fields.begin(); it != fields.end(); ++it) {
208                 const std::string &name = it->first;
209                 const std::string &value = it->second;
210                 lua_pushstring(L, name.c_str());
211                 lua_pushlstring(L, value.c_str(), value.size());
212                 lua_settable(L, -3);
213         }
214         runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
215 }
216
217 void ScriptApiPlayer::on_auth_failure(const std::string &name, const std::string &ip)
218 {
219         SCRIPTAPI_PRECHECKHEADER
220
221         // Get core.registered_on_auth_failure
222         lua_getglobal(L, "core");
223         lua_getfield(L, -1, "registered_on_auth_fail");
224         lua_pushstring(L, name.c_str());
225         lua_pushstring(L, ip.c_str());
226         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
227 }