]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_player.cpp
Order es_DrawType exactly like enum NodeDrawType in nodedef.h (#5946)
[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         runCallbacks(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         runCallbacks(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         runCallbacks(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         int error_handler = PUSH_ERROR_HANDLER(L);
78
79         // Get core.registered_on_player_hpchange
80         lua_getglobal(L, "core");
81         lua_getfield(L, -1, "registered_on_player_hpchange");
82         lua_remove(L, -2);
83
84         objectrefGetOrCreate(L, player);
85         lua_pushnumber(L, hp_change);
86         PCALL_RES(lua_pcall(L, 2, 1, error_handler));
87         hp_change = lua_tointeger(L, -1);
88         lua_pop(L, 2); // Pop result and error handler
89         return hp_change;
90 }
91
92 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
93 {
94         SCRIPTAPI_PRECHECKHEADER
95
96         // Get core.registered_on_respawnplayers
97         lua_getglobal(L, "core");
98         lua_getfield(L, -1, "registered_on_respawnplayers");
99         // Call callbacks
100         objectrefGetOrCreate(L, player);
101         runCallbacks(1, RUN_CALLBACKS_MODE_OR);
102         bool positioning_handled_by_some = lua_toboolean(L, -1);
103         return positioning_handled_by_some;
104 }
105
106 bool ScriptApiPlayer::on_prejoinplayer(
107         const std::string &name,
108         const std::string &ip,
109         std::string *reason)
110 {
111         SCRIPTAPI_PRECHECKHEADER
112
113         // Get core.registered_on_prejoinplayers
114         lua_getglobal(L, "core");
115         lua_getfield(L, -1, "registered_on_prejoinplayers");
116         lua_pushstring(L, name.c_str());
117         lua_pushstring(L, ip.c_str());
118         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
119         if (lua_isstring(L, -1)) {
120                 reason->assign(lua_tostring(L, -1));
121                 return true;
122         }
123         return false;
124 }
125
126 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
127 {
128         SCRIPTAPI_PRECHECKHEADER
129
130         // Get core.registered_on_joinplayers
131         lua_getglobal(L, "core");
132         lua_getfield(L, -1, "registered_on_joinplayers");
133         // Call callbacks
134         objectrefGetOrCreate(L, player);
135         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
136 }
137
138 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
139                 bool timeout)
140 {
141         SCRIPTAPI_PRECHECKHEADER
142
143         // Get core.registered_on_leaveplayers
144         lua_getglobal(L, "core");
145         lua_getfield(L, -1, "registered_on_leaveplayers");
146         // Call callbacks
147         objectrefGetOrCreate(L, player);
148         lua_pushboolean(L, timeout);
149         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
150 }
151
152 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
153                 const std::string &cheat_type)
154 {
155         SCRIPTAPI_PRECHECKHEADER
156
157         // Get core.registered_on_cheats
158         lua_getglobal(L, "core");
159         lua_getfield(L, -1, "registered_on_cheats");
160         // Call callbacks
161         objectrefGetOrCreate(L, player);
162         lua_newtable(L);
163         lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
164         lua_setfield(L, -2, "type");
165         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
166 }
167
168 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
169                 const std::string &formname,
170                 const StringMap &fields)
171 {
172         SCRIPTAPI_PRECHECKHEADER
173
174         // Get core.registered_on_chat_messages
175         lua_getglobal(L, "core");
176         lua_getfield(L, -1, "registered_on_player_receive_fields");
177         // Call callbacks
178         // param 1
179         objectrefGetOrCreate(L, player);
180         // param 2
181         lua_pushstring(L, formname.c_str());
182         // param 3
183         lua_newtable(L);
184         StringMap::const_iterator it;
185         for (it = fields.begin(); it != fields.end(); ++it) {
186                 const std::string &name = it->first;
187                 const std::string &value = it->second;
188                 lua_pushstring(L, name.c_str());
189                 lua_pushlstring(L, value.c_str(), value.size());
190                 lua_settable(L, -3);
191         }
192         runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
193 }
194
195 ScriptApiPlayer::~ScriptApiPlayer()
196 {
197 }
198
199