aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_player.cpp
blob: 100434fc35048955cd849f5e16df438c424afe30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "cpp_api/s_player.h"
#include "cpp_api/s_internal.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "debug.h"
#include "inventorymanager.h"
#include "lua_api/l_inventory.h"
#include "lua_api/l_item.h"
#include "util/string.h"

void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_newplayers
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_newplayers");
	// Call callbacks
	objectrefGetOrCreate(L, player);
	runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
}

void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player, const PlayerHPChangeReason &reason)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get callback table
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_dieplayers");

	// Push arguments
	objectrefGetOrCreate(L, player);
	pushPlayerHPChangeReason(L, reason);

	// Run callbacks
	runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
}

bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
		ServerActiveObject *hitter,
		float time_from_last_punch,
		const ToolCapabilities *toolcap,
		v3f dir,
		s16 damage)
{
	SCRIPTAPI_PRECHECKHEADER
	// Get core.registered_on_punchplayers
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_punchplayers");
	// Call callbacks
	objectrefGetOrCreate(L, player);
	objectrefGetOrCreate(L, hitter);
	lua_pushnumber(L, time_from_last_punch);
	push_tool_capabilities(L, *toolcap);
	push_v3f(L, dir);
	lua_pushnumber(L, damage);
	runCallbacks(6, RUN_CALLBACKS_MODE_OR);
	return readParam<bool>(L, -1);
}

s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
	s16 hp_change, const PlayerHPChangeReason &reason)
{
	SCRIPTAPI_PRECHECKHEADER

	int error_handler = PUSH_ERROR_HANDLER(L);

	// Get core.registered_on_player_hpchange
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_player_hpchange");
	lua_remove(L, -2);

	// Push arguments
	objectrefGetOrCreate(L, player);
	lua_pushnumber(L, hp_change);
	pushPlayerHPChangeReason(L, reason);

	// Call callbacks
	PCALL_RES(lua_pcall(L, 3, 1, error_handler));
	hp_change = lua_tointeger(L, -1);
	lua_pop(L, 2); // Pop result and error handler
	return hp_change;
}

bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_respawnplayers
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_respawnplayers");
	// Call callbacks
	objectrefGetOrCreate(L, player);
	runCallbacks(1, RUN_CALLBACKS_MODE_OR);
	return readParam<bool>(L, -1);
}

bool ScriptApiPlayer::on_prejoinplayer(
	const std::string &name,
	const std::string &ip,
	std::string *reason)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_prejoinplayers
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_prejoinplayers");
	lua_pushstring(L, name.c_str());
	lua_pushstring(L, ip.c_str());
	runCallbacks(2, RUN_CALLBACKS_MODE_OR);
	if (lua_isstring(L, -1)) {
		reason->assign(readParam<std::string>(L, -1));
		return true;
	}
	return false;
}

bool ScriptApiPlayer::can_bypass_userlimit(const std::string &name, const std::string &ip)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_prejoinplayers
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_can_bypass_userlimit");
	lua_pushstring(L, name.c_str());
	lua_pushstring(L, ip.c_str());
	runCallbacks(2, RUN_CALLBACKS_MODE_OR);
	return lua_toboolean(L, -1);
}

void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_joinplayers
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_joinplayers");
	// Call callbacks
	objectrefGetOrCreate(L, player);
	runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
}

void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
		bool timeout)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_leaveplayers
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_leaveplayers");
	// Call callbacks
	objectrefGetOrCreate(L, player);
	lua_pushboolean(L, timeout);
	runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
}

void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
		const std::string &cheat_type)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_cheats
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_cheats");
	// Call callbacks
	objectrefGetOrCreate(L, player);
	lua_newtable(L);
	lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
	lua_setfield(L, -2, "type");
	runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
}

void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
		const std::string &formname,
		const StringMap &fields)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_chat_messages
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_player_receive_fields");
	// Call callbacks
	// param 1
	objectrefGetOrCreate(L, player);
	// param 2
	lua_pushstring(L, formname.c_str());
	// param 3
	lua_newtable(L);
	StringMap::const_iterator it;
	for (it = fields.begin(); it != fields.end(); ++it) {
		const std::string &name = it->first;
		const std::string &value = it->second;
		lua_pushstring(L, name.c_str());
		lua_pushlstring(L, value.c_str(), value.size());
		lua_settable(L, -3);
	}
	runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
}

void ScriptApiPlayer::on_auth_failure(const std::string &name, const std::string &ip)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_auth_failure
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_auth_fail");
	lua_pushstring(L, name.c_str());
	lua_pushstring(L, ip.c_str());
	runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
}

void ScriptApiPlayer::pushMoveArguments(
		const MoveAction &ma, int count,
		ServerActiveObject *player)
{
	lua_State *L = getStack();
	objectrefGetOrCreate(L, player); // player
	lua_pushstring(L, "move");       // action
	InvRef::create(L, ma.from_inv);  // inventory
	lua_newtable(L);
	{
		// Table containing the action information
		lua_pushstring(L, ma.from_list.c_str());
		lua_setfield(L, -2, "from_list");
		lua_pushstring(L, ma.to_list.c_str());
		lua_setfield(L, -2, "to_list");

		lua_pushinteger(L, ma.from_i + 1);
		lua_setfield(L, -2, "from_index");
		lua_pushinteger(L, ma.to_i + 1);
		lua_setfield(L, -2, "to_index");

		lua_pushinteger(L, count);
		lua_setfield(L, -2, "count");
	}
}

void ScriptApiPlayer::pushPutTakeArguments(
		const char *method, const InventoryLocation &loc,
		const std::string &listname, int index, const ItemStack &stack,
		ServerActiveObject *player)
{
	lua_State *L = getStack();
	objectrefGetOrCreate(L, player); // player
	lua_pushstring(L, method);       // action
	InvRef::create(L, loc);          // inventory
	lua_newtable(L);
	{
		// Table containing the action information
		lua_pushstring(L, listname.c_str());
		lua_setfield(L, -2, "listname");

		lua_pushinteger(L, index + 1);
		lua_setfield(L, -2, "index");

		LuaItemStack::create(L, stack);
		lua_setfield(L, -2, "stack");
	}
}

// Return number of accepted items to be moved
int ScriptApiPlayer::player_inventory_AllowMove(
		const MoveAction &ma, int count,
		ServerActiveObject *player)
{
	SCRIPTAPI_PRECHECKHEADER

	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_allow_player_inventory_actions");
	pushMoveArguments(ma, count, player);
	runCallbacks(4, RUN_CALLBACKS_MODE_OR_SC);

	return lua_type(L, -1) == LUA_TNUMBER ? lua_tonumber(L, -1) : count;
}

// Return number of accepted items to be put
int ScriptApiPlayer::player_inventory_AllowPut(
		const MoveAction &ma, const ItemStack &stack,
		ServerActiveObject *player)
{
	SCRIPTAPI_PRECHECKHEADER

	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_allow_player_inventory_actions");
	pushPutTakeArguments("put", ma.to_inv, ma.to_list, ma.to_i, stack, player);
	runCallbacks(4, RUN_CALLBACKS_MODE_OR_SC);

	return lua_type(L, -1) == LUA_TNUMBER ? lua_tonumber(L, -1) : stack.count;
}

// Return number of accepted items to be taken
int ScriptApiPlayer::player_inventory_AllowTake(
		const MoveAction &ma, const ItemStack &stack,
		ServerActiveObject *player)
{
	SCRIPTAPI_PRECHECKHEADER

	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_allow_player_inventory_actions");
	pushPutTakeArguments("take", ma.from_inv, ma.from_list, ma.from_i, stack, player);
	runCallbacks(4, RUN_CALLBACKS_MODE_OR_SC);

	return lua_type(L, -1) == LUA_TNUMBER ? lua_tonumber(L, -1) : stack.count;
}

// Report moved items
void ScriptApiPlayer::player_inventory_OnMove(
		const MoveAction &ma, int count,
		ServerActiveObject *player)
{
	SCRIPTAPI_PRECHECKHEADER

	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_player_inventory_actions");
	pushMoveArguments(ma, count, player);
	runCallbacks(4, RUN_CALLBACKS_MODE_FIRST);
}

// Report put items
void ScriptApiPlayer::player_inventory_OnPut(
		const MoveAction &ma, const ItemStack &stack,
		ServerActiveObject *player)
{
	SCRIPTAPI_PRECHECKHEADER

	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_player_inventory_actions");
	pushPutTakeArguments("put", ma.to_inv, ma.to_list, ma.to_i, stack, player);
	runCallbacks(4, RUN_CALLBACKS_MODE_FIRST);
}

// Report taken items
void ScriptApiPlayer::player_inventory_OnTake(
		const MoveAction &ma, const ItemStack &stack,
		ServerActiveObject *player)
{
	SCRIPTAPI_PRECHECKHEADER

	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_player_inventory_actions");
	pushPutTakeArguments("take", ma.from_inv, ma.from_list, ma.from_i, stack, player);
	runCallbacks(4, RUN_CALLBACKS_MODE_FIRST);
}
l opt">; temp_frag.column = 0; //temp_frag.bold = 0; next_frags.push_back(temp_frag); } std::wstring name_sanitized = line.name.c_str(); // Choose an indentation level if (line.name.empty()) { // Server messages hanging_indentation = 0; } else if (name_sanitized.size() + 3 <= cols/2) { // Names shorter than about half the console width hanging_indentation = line.name.size() + 3; } else { // Very long names hanging_indentation = 2; } //EnrichedString line_text(line.text); next_line.first = true; bool text_processing = false; // Produce fragments and layout them into lines while (!next_frags.empty() || in_pos < line.text.size()) { // Layout fragments into lines while (!next_frags.empty()) { ChatFormattedFragment& frag = next_frags[0]; // Force text_processing on this frag. hacky. if(frag.column == u32(INT_MAX)) text_processing = true; if (frag.text.size() <= cols - out_column) { // Fragment fits into current line frag.column = out_column; next_line.fragments.push_back(frag); out_column += frag.text.size(); next_frags.erase(next_frags.begin()); } else { // Fragment does not fit into current line // So split it up temp_frag.text = frag.text.substr(0, cols - out_column); temp_frag.column = out_column; temp_frag.meta = frag.meta; next_line.fragments.push_back(temp_frag); frag.text = frag.text.substr(cols - out_column); frag.column = 0; out_column = cols; } if (out_column == cols || text_processing) { // End the current line destination.push_back(next_line); num_added++; next_line.fragments.clear(); next_line.first = false; out_column = text_processing ? hanging_indentation : 0; text_processing = false; } } // Produce fragment(s) for next formatted line if (in_pos < line.text.size()) { u32 remaining_in_input = line.text.size() - in_pos; u32 remaining_in_output = cols - out_column; // Determine a fragment length <= the minimum of // remaining_in_{in,out}put. Try to end the fragment // on a word boundary. u32 frag_length = 1, space_pos = 0; if(!m_cache_clickable_chat_weblinks) { while (frag_length < remaining_in_input && frag_length < remaining_in_output) { if (iswspace(line.text.getString()[in_pos + frag_length])) space_pos = frag_length; ++frag_length; } if (space_pos != 0 && frag_length < remaining_in_input) frag_length = space_pos + 1; temp_frag.text = line.text.substr(in_pos, frag_length); temp_frag.column = 0; //temp_frag.bold = 0; next_frags.push_back(temp_frag); in_pos += frag_length; text_processing = true; } // Unless weblinks are enabled. Then it's slightly more complex else { u32 http_pos = u32(std::wstring::npos); text_processing = false; // set FALSE at end of this block bool halt_fragloop = false; while(!halt_fragloop) { remaining_in_input = line.text.size() - in_pos; frag_length = 0; space_pos = 0; // Note: unsigned(-1) on fail http_pos = u32(line.text.getString().find(L"https://", std::size_t(in_pos))); if(http_pos == u32(std::wstring::npos)) http_pos = u32(line.text.getString().find(L"http://", std::size_t(in_pos))); if(http_pos != u32(std::wstring::npos)) http_pos -= in_pos; while (frag_length < remaining_in_input && frag_length < remaining_in_output) { if (iswspace(line.text.getString()[in_pos + frag_length])) space_pos = frag_length; ++frag_length; } // Http not in range, grab until space or EOL, halt as normal. if(http_pos > remaining_in_output) // sufficient unless screen is infinitely wide { halt_fragloop = true; // force text processing on THIS frag text_processing = true; } // At http, grab ALL until FIRST whitespace or quote mark. loop. // If at end of string, next loop will be empty string to mark end of weblink. This is intentional. else if(http_pos == 0) { frag_length = 6; while (frag_length < remaining_in_input && !iswspace(line.text.getString()[in_pos + frag_length]) && line.text.getString()[in_pos + frag_length] != L'\'' && line.text.getString()[in_pos + frag_length] != L'\"' && line.text.getString()[in_pos + frag_length] != L';' ) { ++frag_length; } space_pos = frag_length - 1; if(frag_length >= remaining_in_output) { // Force text processing on THIS frag text_processing = true; halt_fragloop = true; } } // Http in range, grab until http, loop else { space_pos = http_pos - 1; frag_length = http_pos; // - in_pos; } if (space_pos != 0 && frag_length < remaining_in_input) frag_length = space_pos + 1; temp_frag.text = line.text.substr(in_pos, frag_length); temp_frag.column = text_processing ? u32(INT_MAX) : 0; if(http_pos == 0) { temp_frag.meta = wide_to_utf8(temp_frag.text.getString()); // FIXME: this is probably overkill to recolor the link text. temp_frag.text = EnrichedString(temp_frag.text.getString()); temp_frag.text.setDefaultColor(m_cache_chat_weblink_color); } else { temp_frag.meta = ""; } next_frags.push_back(temp_frag); in_pos += frag_length; remaining_in_output -= std::min(frag_length, remaining_in_output); /* std::cout << "----------" << std::endl; std::cout << "http_pos = " << http_pos << std::endl; std::cout << "space_pos = " << space_pos << std::endl; std::cout << "frag: '" << temp_frag.text.size() << "'" << std::endl; std::cout << "in_pos: '" << in_pos << std::endl; std::cout << "remain: '" << line.text.substr(in_pos).size() << "'" << std::endl; std::cout << "remn in in: '" << remaining_in_input << "'" << std::endl; std::cout << "remn in out: '" << remaining_in_output << "'" << std::endl; */ } // handled for fragments individually text_processing = false; } } } // End the last line if (num_added == 0 || !next_line.fragments.empty()) { destination.push_back(next_line); num_added++; } return num_added; } s32 ChatBuffer::getTopScrollPos() const { s32 formatted_count = (s32) m_formatted.size(); s32 rows = (s32) m_rows; if (rows == 0) return 0; if (formatted_count <= rows) return formatted_count - rows; return 0; } s32 ChatBuffer::getBottomScrollPos() const { s32 formatted_count = (s32) m_formatted.size(); s32 rows = (s32) m_rows; if (rows == 0) return 0; return formatted_count - rows; } void ChatBuffer::resize(u32 scrollback) { m_scrollback = scrollback; if (m_unformatted.size() > m_scrollback) deleteOldest(m_unformatted.size() - m_scrollback); } ChatPrompt::ChatPrompt(const std::wstring &prompt, u32 history_limit): m_prompt(prompt), m_history_limit(history_limit) { } void ChatPrompt::input(wchar_t ch) { m_line.insert(m_cursor, 1, ch); m_cursor++; clampView(); m_nick_completion_start = 0; m_nick_completion_end = 0; } void ChatPrompt::input(const std::wstring &str) { m_line.insert(m_cursor, str); m_cursor += str.size(); clampView(); m_nick_completion_start = 0; m_nick_completion_end = 0; } void ChatPrompt::addToHistory(const std::wstring &line) { if (!line.empty() && (m_history.size() == 0 || m_history.back() != line)) { // Remove all duplicates m_history.erase(std::remove(m_history.begin(), m_history.end(), line), m_history.end());