]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_player.cpp
Add minetest.register_on_player_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 "util/string.h"
25
26 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
27 {
28         SCRIPTAPI_PRECHECKHEADER
29
30         // Get core.registered_on_newplayers
31         lua_getglobal(L, "core");
32         lua_getfield(L, -1, "registered_on_newplayers");
33         // Call callbacks
34         objectrefGetOrCreate(L, player);
35         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
36 }
37
38 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
39 {
40         SCRIPTAPI_PRECHECKHEADER
41
42         // Get core.registered_on_dieplayers
43         lua_getglobal(L, "core");
44         lua_getfield(L, -1, "registered_on_dieplayers");
45         // Call callbacks
46         objectrefGetOrCreate(L, player);
47         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
48 }
49
50 bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
51                 ServerActiveObject *hitter,
52                 float time_from_last_punch,
53                 const ToolCapabilities *toolcap,
54                 v3f dir,
55                 s16 damage)
56 {
57         SCRIPTAPI_PRECHECKHEADER
58         // Get core.registered_on_punchplayers
59         lua_getglobal(L, "core");
60         lua_getfield(L, -1, "registered_on_punchplayers");
61         // Call callbacks
62         objectrefGetOrCreate(L, player);
63         objectrefGetOrCreate(L, hitter);
64         lua_pushnumber(L, time_from_last_punch);
65         push_tool_capabilities(L, *toolcap);
66         push_v3f(L, dir);
67         lua_pushnumber(L, damage);
68         script_run_callbacks(L, 6, RUN_CALLBACKS_MODE_OR);
69         return lua_toboolean(L, -1);
70 }
71
72 s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
73         s16 hp_change)
74 {
75         SCRIPTAPI_PRECHECKHEADER
76
77         // Get core.registered_on_player_hpchange
78         lua_getglobal(L, "core");
79         lua_getfield(L, -1, "registered_on_player_hpchange");
80         lua_remove(L, -2);
81
82         objectrefGetOrCreate(L, player);
83         lua_pushnumber(L, hp_change);
84         if (lua_pcall(L, 2, 1, m_errorhandler))
85                 scriptError();
86         hp_change = lua_tointeger(L, -1);
87         lua_pop(L, -1);
88         return hp_change;
89 }
90
91 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
92 {
93         SCRIPTAPI_PRECHECKHEADER
94
95         // Get core.registered_on_respawnplayers
96         lua_getglobal(L, "core");
97         lua_getfield(L, -1, "registered_on_respawnplayers");
98         // Call callbacks
99         objectrefGetOrCreate(L, player);
100         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
101         bool positioning_handled_by_some = lua_toboolean(L, -1);
102         return positioning_handled_by_some;
103 }
104
105 bool ScriptApiPlayer::on_prejoinplayer(
106         const std::string &name,
107         const std::string &ip,
108         std::string *reason)
109 {
110         SCRIPTAPI_PRECHECKHEADER
111
112         // Get core.registered_on_prejoinplayers
113         lua_getglobal(L, "core");
114         lua_getfield(L, -1, "registered_on_prejoinplayers");
115         lua_pushstring(L, name.c_str());
116         lua_pushstring(L, ip.c_str());
117         script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
118         if (lua_isstring(L, -1)) {
119                 reason->assign(lua_tostring(L, -1));
120                 return true;
121         }
122         return false;
123 }
124
125 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
126 {
127         SCRIPTAPI_PRECHECKHEADER
128
129         // Get core.registered_on_joinplayers
130         lua_getglobal(L, "core");
131         lua_getfield(L, -1, "registered_on_joinplayers");
132         // Call callbacks
133         objectrefGetOrCreate(L, player);
134         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
135 }
136
137 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
138 {
139         SCRIPTAPI_PRECHECKHEADER
140
141         // Get core.registered_on_leaveplayers
142         lua_getglobal(L, "core");
143         lua_getfield(L, -1, "registered_on_leaveplayers");
144         // Call callbacks
145         objectrefGetOrCreate(L, player);
146         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
147 }
148
149 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
150                 const std::string &cheat_type)
151 {
152         SCRIPTAPI_PRECHECKHEADER
153
154         // Get core.registered_on_cheats
155         lua_getglobal(L, "core");
156         lua_getfield(L, -1, "registered_on_cheats");
157         // Call callbacks
158         objectrefGetOrCreate(L, player);
159         lua_newtable(L);
160         lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
161         lua_setfield(L, -2, "type");
162         script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
163 }
164
165 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
166                 const std::string &formname,
167                 const StringMap &fields)
168 {
169         SCRIPTAPI_PRECHECKHEADER
170
171         // Get core.registered_on_chat_messages
172         lua_getglobal(L, "core");
173         lua_getfield(L, -1, "registered_on_player_receive_fields");
174         // Call callbacks
175         // param 1
176         objectrefGetOrCreate(L, player);
177         // param 2
178         lua_pushstring(L, formname.c_str());
179         // param 3
180         lua_newtable(L);
181         StringMap::const_iterator it;
182         for (it = fields.begin(); it != fields.end(); ++it) {
183                 const std::string &name = it->first;
184                 const std::string &value = it->second;
185                 lua_pushstring(L, name.c_str());
186                 lua_pushlstring(L, value.c_str(), value.size());
187                 lua_settable(L, -3);
188         }
189         script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
190 }
191
192 ScriptApiPlayer::~ScriptApiPlayer()
193 {
194 }
195
196