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