Spawn level: Add 'get_spawn_level(x, z)' API
[oweals/minetest.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)
40 {
41         SCRIPTAPI_PRECHECKHEADER
42
43         // Get core.registered_on_dieplayers
44         lua_getglobal(L, "core");
45         lua_getfield(L, -1, "registered_on_dieplayers");
46         // Call callbacks
47         objectrefGetOrCreate(L, player);
48         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
49 }
50
51 bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
52                 ServerActiveObject *hitter,
53                 float time_from_last_punch,
54                 const ToolCapabilities *toolcap,
55                 v3f dir,
56                 s16 damage)
57 {
58         SCRIPTAPI_PRECHECKHEADER
59         // Get core.registered_on_punchplayers
60         lua_getglobal(L, "core");
61         lua_getfield(L, -1, "registered_on_punchplayers");
62         // Call callbacks
63         objectrefGetOrCreate(L, player);
64         objectrefGetOrCreate(L, hitter);
65         lua_pushnumber(L, time_from_last_punch);
66         push_tool_capabilities(L, *toolcap);
67         push_v3f(L, dir);
68         lua_pushnumber(L, damage);
69         runCallbacks(6, RUN_CALLBACKS_MODE_OR);
70         return lua_toboolean(L, -1);
71 }
72
73 s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
74         s16 hp_change)
75 {
76         SCRIPTAPI_PRECHECKHEADER
77
78         int error_handler = PUSH_ERROR_HANDLER(L);
79
80         // Get core.registered_on_player_hpchange
81         lua_getglobal(L, "core");
82         lua_getfield(L, -1, "registered_on_player_hpchange");
83         lua_remove(L, -2);
84
85         objectrefGetOrCreate(L, player);
86         lua_pushnumber(L, hp_change);
87         PCALL_RES(lua_pcall(L, 2, 1, error_handler));
88         hp_change = lua_tointeger(L, -1);
89         lua_pop(L, 2); // Pop result and error handler
90         return hp_change;
91 }
92
93 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
94 {
95         SCRIPTAPI_PRECHECKHEADER
96
97         // Get core.registered_on_respawnplayers
98         lua_getglobal(L, "core");
99         lua_getfield(L, -1, "registered_on_respawnplayers");
100         // Call callbacks
101         objectrefGetOrCreate(L, player);
102         runCallbacks(1, RUN_CALLBACKS_MODE_OR);
103         bool positioning_handled_by_some = lua_toboolean(L, -1);
104         return positioning_handled_by_some;
105 }
106
107 bool ScriptApiPlayer::on_prejoinplayer(
108         const std::string &name,
109         const std::string &ip,
110         std::string *reason)
111 {
112         SCRIPTAPI_PRECHECKHEADER
113
114         // Get core.registered_on_prejoinplayers
115         lua_getglobal(L, "core");
116         lua_getfield(L, -1, "registered_on_prejoinplayers");
117         lua_pushstring(L, name.c_str());
118         lua_pushstring(L, ip.c_str());
119         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
120         if (lua_isstring(L, -1)) {
121                 reason->assign(lua_tostring(L, -1));
122                 return true;
123         }
124         return false;
125 }
126
127 bool ScriptApiPlayer::can_bypass_userlimit(const std::string &name, const std::string &ip)
128 {
129         SCRIPTAPI_PRECHECKHEADER
130
131         // Get core.registered_on_prejoinplayers
132         lua_getglobal(L, "core");
133         lua_getfield(L, -1, "registered_can_bypass_userlimit");
134         lua_pushstring(L, name.c_str());
135         lua_pushstring(L, ip.c_str());
136         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
137         return lua_toboolean(L, -1);
138 }
139
140 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
141 {
142         SCRIPTAPI_PRECHECKHEADER
143
144         // Get core.registered_on_joinplayers
145         lua_getglobal(L, "core");
146         lua_getfield(L, -1, "registered_on_joinplayers");
147         // Call callbacks
148         objectrefGetOrCreate(L, player);
149         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
150 }
151
152 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
153                 bool timeout)
154 {
155         SCRIPTAPI_PRECHECKHEADER
156
157         // Get core.registered_on_leaveplayers
158         lua_getglobal(L, "core");
159         lua_getfield(L, -1, "registered_on_leaveplayers");
160         // Call callbacks
161         objectrefGetOrCreate(L, player);
162         lua_pushboolean(L, timeout);
163         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
164 }
165
166 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
167                 const std::string &cheat_type)
168 {
169         SCRIPTAPI_PRECHECKHEADER
170
171         // Get core.registered_on_cheats
172         lua_getglobal(L, "core");
173         lua_getfield(L, -1, "registered_on_cheats");
174         // Call callbacks
175         objectrefGetOrCreate(L, player);
176         lua_newtable(L);
177         lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
178         lua_setfield(L, -2, "type");
179         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
180 }
181
182 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
183                 const std::string &formname,
184                 const StringMap &fields)
185 {
186         SCRIPTAPI_PRECHECKHEADER
187
188         // Get core.registered_on_chat_messages
189         lua_getglobal(L, "core");
190         lua_getfield(L, -1, "registered_on_player_receive_fields");
191         // Call callbacks
192         // param 1
193         objectrefGetOrCreate(L, player);
194         // param 2
195         lua_pushstring(L, formname.c_str());
196         // param 3
197         lua_newtable(L);
198         StringMap::const_iterator it;
199         for (it = fields.begin(); it != fields.end(); ++it) {
200                 const std::string &name = it->first;
201                 const std::string &value = it->second;
202                 lua_pushstring(L, name.c_str());
203                 lua_pushlstring(L, value.c_str(), value.size());
204                 lua_settable(L, -3);
205         }
206         runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
207 }
208
209 void ScriptApiPlayer::on_auth_failure(const std::string &name, const std::string &ip)
210 {
211         SCRIPTAPI_PRECHECKHEADER
212
213         // Get core.registered_on_auth_failure
214         lua_getglobal(L, "core");
215         lua_getfield(L, -1, "registered_on_auth_fail");
216         lua_pushstring(L, name.c_str());
217         lua_pushstring(L, ip.c_str());
218         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
219 }