aboutsummaryrefslogtreecommitdiff
path: root/src/keycode.cpp
blob: c5f102b4441a129f616e2e40460a42e259701eea (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
363
364
365
/*
Minetest
Copyright (C) 2010-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 "keycode.h"
#include "main.h" // For g_settings
#include "exceptions.h"
#include "settings.h"
#include "log.h"
#include "hex.h"
#include "debug.h"

class UnknownKeycode : public BaseException
{
public:
	UnknownKeycode(const char *s) :
		BaseException(s) {};
};

#define CHECKKEY(x){if(strcmp(name, #x)==0) return irr::x;}

irr::EKEY_CODE keyname_to_keycode(const char *name)
{
	CHECKKEY(KEY_LBUTTON)
	CHECKKEY(KEY_RBUTTON)
	CHECKKEY(KEY_CANCEL)
	CHECKKEY(KEY_MBUTTON)
	CHECKKEY(KEY_XBUTTON1)
	CHECKKEY(KEY_XBUTTON2)
	CHECKKEY(KEY_BACK)
	CHECKKEY(KEY_TAB)
	CHECKKEY(KEY_CLEAR)
	CHECKKEY(KEY_RETURN)
	CHECKKEY(KEY_SHIFT)
	CHECKKEY(KEY_CONTROL)
	CHECKKEY(KEY_MENU)
	CHECKKEY(KEY_PAUSE)
	CHECKKEY(KEY_CAPITAL)
	CHECKKEY(KEY_KANA)
	CHECKKEY(KEY_HANGUEL)
	CHECKKEY(KEY_HANGUL)
	CHECKKEY(KEY_JUNJA)
	CHECKKEY(KEY_FINAL)
	CHECKKEY(KEY_HANJA)
	CHECKKEY(KEY_KANJI)
	CHECKKEY(KEY_ESCAPE)
	CHECKKEY(KEY_CONVERT)
	CHECKKEY(KEY_NONCONVERT)
	CHECKKEY(KEY_ACCEPT)
	CHECKKEY(KEY_MODECHANGE)
	CHECKKEY(KEY_SPACE)
	CHECKKEY(KEY_PRIOR)
	CHECKKEY(KEY_NEXT)
	CHECKKEY(KEY_END)
	CHECKKEY(KEY_HOME)
	CHECKKEY(KEY_LEFT)
	CHECKKEY(KEY_UP)
	CHECKKEY(KEY_RIGHT)
	CHECKKEY(KEY_DOWN)
	CHECKKEY(KEY_SELECT)
	CHECKKEY(KEY_PRINT)
	CHECKKEY(KEY_EX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(name);
}

static const char *KeyNames[] =
{ "-", "KEY_LBUTTON", "KEY_RBUTTON", "KEY_CANCEL", "KEY_MBUTTON", "KEY_XBUTTON1",
		"KEY_XBUTTON2", "-", "KEY_BACK", "KEY_TAB", "-", "-", "KEY_CLEAR", "KEY_RETURN", "-",
		"-", "KEY_SHIFT", "KEY_CONTROL", "KEY_MENU", "KEY_PAUSE", "KEY_CAPITAL", "KEY_KANA", "-",
		"KEY_JUNJA", "KEY_FINAL", "KEY_KANJI", "-", "KEY_ESCAPE", "KEY_CONVERT", "KEY_NONCONVERT",
		"KEY_ACCEPT", "KEY_MODECHANGE", "KEY_SPACE", "KEY_PRIOR", "KEY_NEXT", "KEY_END",
		"KEY_HOME", "KEY_LEFT", "KEY_UP", "KEY_RIGHT", "KEY_DOWN", "KEY_SELECT", "KEY_PRINT",
		"KEY_EXECUTE", "KEY_SNAPSHOT", "KEY_INSERT", "KEY_DELETE", "KEY_HELP", "KEY_KEY_0",
		"KEY_KEY_1", "KEY_KEY_2", "KEY_KEY_3", "KEY_KEY_4", "KEY_KEY_5",
		"KEY_KEY_6", "KEY_KEY_7", "KEY_KEY_8", "KEY_KEY_9", "-", "-", "-", "-",
		"-", "-", "-", "KEY_KEY_A", "KEY_KEY_B", "KEY_KEY_C", "KEY_KEY_D",
		"KEY_KEY_E", "KEY_KEY_F", "KEY_KEY_G", "KEY_KEY_H", "KEY_KEY_I",
		"KEY_KEY_J", "KEY_KEY_K", "KEY_KEY_L", "KEY_KEY_M", "KEY_KEY_N",
		"KEY_KEY_O", "KEY_KEY_P", "KEY_KEY_Q", "KEY_KEY_R", "KEY_KEY_S",
		"KEY_KEY_T", "KEY_KEY_U", "KEY_KEY_V", "KEY_KEY_W", "KEY_KEY_X",
		"KEY_KEY_Y", "KEY_KEY_Z", "KEY_LWIN", "KEY_RWIN", "KEY_APPS", "-",
		"KEY_SLEEP", "KEY_NUMPAD0", "KEY_NUMPAD1", "KEY_NUMPAD2", "KEY_NUMPAD3",
		"KEY_NUMPAD4", "KEY_NUMPAD5", "KEY_NUMPAD6", "KEY_NUMPAD7",
		"KEY_NUMPAD8", "KEY_NUMPAD9", "KEY_MULTIPLY", "KEY_ADD", "KEY_SEPERATOR",
		"KEY_SUBTRACT", "KEY_DECIMAL", "KEY_DIVIDE", "KEY_F1", "KEY_F2", "KEY_F3",
		"KEY_F4", "KEY_F5", "KEY_F6", "KEY_F7", "KEY_F8", "KEY_F9", "KEY_F10",
		"KEY_F11", "KEY_F12", "KEY_F13", "KEY_F14", "KEY_F15", "KEY_F16",
		"KEY_F17", "KEY_F18", "KEY_F19", "KEY_F20", "KEY_F21", "KEY_F22",
		"KEY_F23", "KEY_F24", "-", "-", "-", "-", "-", "-", "-", "-",
		"KEY_NUMLOCK", "KEY_SCROLL", "-", "-", "-", "-", "-", "-", "-", "-", "-",
		"-", "-", "-", "-", "-", "KEY_LSHIFT", "KEY_RSHIFT", "KEY_LCONTROL",
		"KEY_RCONTROL", "KEY_LMENU", "KEY_RMENU", "-", "-", "-", "-", "-",
		"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
		"-", "-", "KEY_PLUS", "KEY_COMMA", "KEY_MINUS", "KEY_PERIOD", "-", "-", "-", "-", "-",
		"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
		"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
		"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
		"-", "-", "-", "-", "-", "-", "-", "-", "KEY_ATTN", "KEY_CRSEL", "KEY_EXSEL",
		"KEY_EREOF", "KEY_PLAY", "KEY_ZOOM", "KEY_PA1", "KEY_OEM_CLEAR", "-" };

#define N_(text) text

static const char *KeyNamesLang[] =
	{ "-", N_("Left Button"), N_("Right Button"), N_("Cancel"), N_("Middle Button"), N_("X Button 1"),
			N_("X Button 2"), "-", N_("Back"), N_("Tab"), "-", "-", N_("Clear"), N_("Return"), "-",
			"-", N_("Shift"), N_("Control"), N_("Menu"), N_("Pause"), N_("Capital"), N_("Kana"), "-",
			N_("Junja"), N_("Final"), N_("Kanji"), "-", N_("Escape"), N_("Convert"), N_("Nonconvert"),
			N_("Accept"), N_("Mode Change"), N_("Space"), N_("Prior"), N_("Next"), N_("End"), N_("Home"),
			N_("Left"), N_("Up"), N_("Right"), N_("Down"), N_("Select"), N_("Print"), N_("Execute"),
			N_("Snapshot"), N_("Insert"), N_("Delete"), N_("Help"), "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "-", "-", "-", "-", "-", "-", "-", "A", "B", "C",
			"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
			"R", "S", "T", "U", "V", "W", "X", "Y", "Z", N_("Left Windows"),
			N_("Right Windows"), N_("Apps"), "-", N_("Sleep"), N_("Numpad 0"), N_("Numpad 1"),
			N_("Numpad 2"), N_("Numpad 3"), N_("Numpad 4"), N_("Numpad 5"), N_("Numpad 6"), N_("Numpad 7"),
			N_("Numpad 8"), N_("Numpad 9"), N_("Numpad *"), N_("Numpad +"), N_("Numpad /"), N_("Numpad -"),
			"Numpad .", "Numpad /", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8",
			"F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16", "F17", "F18",
			"F19", "F20", "F21", "F22", "F23", "F24", "-", "-", "-", "-", "-", "-",
			"-", "-", N_("Num Lock"), N_("Scroll Lock"), "-", "-", "-", "-", "-", "-", "-",
			"-", "-", "-", "-", "-", "-", "-", N_("Left Shift"), N_("Right Shift"),
			N_("Left Control"), N_("Right Control"), N_("Left Menu"), N_("Right Menu"), "-", "-",
			"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
			"-", "-", "-", "-", "-", N_("Plus"), N_("Comma"), N_("Minus"), N_("Period"), "-", "-",
			"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
			"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
			"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
			"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", N_("Attn"), N_("CrSel"),
			N_("ExSel"), N_("Erase OEF"), N_("Play"), N_("Zoom"), N_("PA1"), N_("OEM Clear"), "-" };

#undef N_

KeyPress::KeyPress() :
	Key(irr::KEY_KEY_CODES_COUNT),
	Char(L'\0')
{}

KeyPress::KeyPress(const char *name)
{
	if (strlen(name) > 4) {
		try {
			Key = keyname_to_keycode(name);
			m_name = name;
			if (strlen(name) > 8 && strncmp(name, "KEY_KEY_", 8) == 0) {
				int chars_read = mbtowc(&Char, name + 8, 1);
				assert (chars_read == 1 && "unexpected multibyte character");
			} else
				Char = L'\0';
			return;
		} catch (UnknownKeycode &e) {};
	} else {
		// see if we can set it up as a KEY_KEY_something
		m_name = "KEY_KEY_";
		m_name += name;
		try {
			Key = keyname_to_keycode(m_name.c_str());
			int chars_read = mbtowc(&Char, name, 1);
			assert (chars_read == 1 && "unexpected multibyte character");
			return;
		} catch (UnknownKeycode &e) {};
	}

	// it's not a (known) key, just take the first char and use that

	Key = irr::KEY_KEY_CODES_COUNT;

	int mbtowc_ret = mbtowc(&Char, name, 1);
	assert (mbtowc_ret == 1 && "unexpected multibyte character");
	m_name = name[0];
}

KeyPress::KeyPress(const irr::SEvent::SKeyInput &in, bool prefer_character)
{
	Key = in.Key;
	Char = in.Char;

	if(prefer_character){
		m_name.resize(MB_CUR_MAX+1, '\0');
		int written = wctomb(&m_name[0], Char);
		if(written > 0){
			infostream<<"KeyPress: Preferring character for "<<m_name<<std::endl;
			Key = irr::KEY_KEY_CODES_COUNT;
			return;
		}
	}

	if (valid_kcode(Key)) {
		m_name = KeyNames[Key];
	} else {
		m_name.resize(MB_CUR_MAX+1, '\0');
		int written = wctomb(&m_name[0], Char);
		if(written < 0){
			std::string hexstr = hex_encode((const char*)&Char, sizeof(Char));
			errorstream<<"KeyPress: Unexpected multibyte character "<<hexstr<<std::endl;
		}
	}
}

const char *KeyPress::sym() const
{
	if (Key && Key < irr::KEY_KEY_CODES_COUNT)
		return KeyNames[Key];
	else {
		return m_name.c_str();
	}
}

const char *KeyPress::name() const
{
	if (Key && Key < irr::KEY_KEY_CODES_COUNT)
		return KeyNamesLang[Key];
	else {
		return m_name.c_str();
	}
}

const KeyPress EscapeKey("KEY_ESCAPE");
const KeyPress CancelKey("KEY_CANCEL");
const KeyPress NumberKey[] = {
	KeyPress("KEY_KEY_0"), KeyPress("KEY_KEY_1"), KeyPress("KEY_KEY_2"),
	KeyPress("KEY_KEY_3"), KeyPress("KEY_KEY_4"), KeyPress("KEY_KEY_5"),
	KeyPress("KEY_KEY_6"), KeyPress("KEY_KEY_7"), KeyPress("KEY_KEY_8"),
	KeyPress("KEY_KEY_9")};

/*
	Key config
*/

// A simple cache for quicker lookup
std::map<std::string, KeyPress> g_key_setting_cache;

KeyPress getKeySetting(const char *settingname)
{
	std::map<std::string, KeyPress>::iterator n;
	n = g_key_setting_cache.find(settingname);
	if(n != g_key_setting_cache.end())
		return n->second;
	g_key_setting_cache[settingname] = g_settings->get(settingname).c_str();
	return g_key_setting_cache.find(settingname)->second;
}

void clearKeyCache()
{
	g_key_setting_cache.clear();
}
t.begin(); i != meta_updates_list.end(); ++i) { v3s16 pos = i->first; if (map.isValidPosition(pos) && map.setNodeMetadata(pos, i->second)) continue; // Prevent from deleting metadata // Meta couldn't be set, unused metadata delete i->second; } } void Client::handleCommand_BlockData(NetworkPacket* pkt) { // Ignore too small packet if (pkt->getSize() < 6) return; v3s16 p; *pkt >> p; std::string datastring(pkt->getString(6), pkt->getSize() - 6); std::istringstream istr(datastring, std::ios_base::binary); MapSector *sector; MapBlock *block; v2s16 p2d(p.X, p.Z); sector = m_env.getMap().emergeSector(p2d); assert(sector->getPos() == p2d); block = sector->getBlockNoCreateNoEx(p.Y); if (block) { /* Update an existing block */ block->deSerialize(istr, m_server_ser_ver, false); block->deSerializeNetworkSpecific(istr); } else { /* Create a new block */ block = new MapBlock(&m_env.getMap(), p, this); block->deSerialize(istr, m_server_ser_ver, false); block->deSerializeNetworkSpecific(istr); sector->insertBlock(block); } if (m_localdb) { ServerMap::saveBlock(block, m_localdb); } /* Add it to mesh update queue and set it to be acknowledged after update. */ addUpdateMeshTaskWithEdge(p, true); } void Client::handleCommand_Inventory(NetworkPacket* pkt) { if (pkt->getSize() < 1) return; std::string datastring(pkt->getString(0), pkt->getSize()); std::istringstream is(datastring, std::ios_base::binary); LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->inventory.deSerialize(is); m_update_wielded_item = true; delete m_inventory_from_server; m_inventory_from_server = new Inventory(player->inventory); m_inventory_from_server_age = 0.0; } void Client::handleCommand_TimeOfDay(NetworkPacket* pkt) { if (pkt->getSize() < 2) return; u16 time_of_day; *pkt >> time_of_day; time_of_day = time_of_day % 24000; float time_speed = 0; if (pkt->getSize() >= 2 + 4) { *pkt >> time_speed; } else { // Old message; try to approximate speed of time by ourselves float time_of_day_f = (float)time_of_day / 24000.0f; float tod_diff_f = 0; if (time_of_day_f < 0.2 && m_last_time_of_day_f > 0.8) tod_diff_f = time_of_day_f - m_last_time_of_day_f + 1.0f; else tod_diff_f = time_of_day_f - m_last_time_of_day_f; m_last_time_of_day_f = time_of_day_f; float time_diff = m_time_of_day_update_timer; m_time_of_day_update_timer = 0; if (m_time_of_day_set) { time_speed = (3600.0f * 24.0f) * tod_diff_f / time_diff; infostream << "Client: Measured time_of_day speed (old format): " << time_speed << " tod_diff_f=" << tod_diff_f << " time_diff=" << time_diff << std::endl; } } // Update environment m_env.setTimeOfDay(time_of_day); m_env.setTimeOfDaySpeed(time_speed); m_time_of_day_set = true; //u32 dr = m_env.getDayNightRatio(); //infostream << "Client: time_of_day=" << time_of_day // << " time_speed=" << time_speed // << " dr=" << dr << std::endl; } void Client::handleCommand_ChatMessage(NetworkPacket *pkt) { /* u8 version u8 message_type u16 sendername length wstring sendername u16 length wstring message */ ChatMessage *chatMessage = new ChatMessage(); u8 version, message_type; *pkt >> version >> message_type; if (version != 1 || message_type >= CHATMESSAGE_TYPE_MAX) { delete chatMessage; return; } u64 timestamp; *pkt >> chatMessage->sender >> chatMessage->message >> timestamp; chatMessage->timestamp = static_cast<std::time_t>(timestamp); chatMessage->type = (ChatMessageType) message_type; // @TODO send this to CSM using ChatMessage object if (modsLoaded() && m_script->on_receiving_message( wide_to_utf8(chatMessage->message))) { // Message was consumed by CSM and should not be handled by client delete chatMessage; } else { pushToChatQueue(chatMessage); } } void Client::handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt) { /* u16 count of removed objects for all removed objects { u16 id } u16 count of added objects for all added objects { u16 id u8 type u32 initialization data length string initialization data } */ try { u8 type; u16 removed_count, added_count, id; // Read removed objects *pkt >> removed_count; for (u16 i = 0; i < removed_count; i++) { *pkt >> id; m_env.removeActiveObject(id); } // Read added objects *pkt >> added_count; for (u16 i = 0; i < added_count; i++) { *pkt >> id >> type; m_env.addActiveObject(id, type, pkt->readLongString()); } } catch (PacketError &e) { infostream << "handleCommand_ActiveObjectRemoveAdd: " << e.what() << ". The packet is unreliable, ignoring" << std::endl; } // m_activeobjects_received is false before the first // TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD packet is received m_activeobjects_received = true; } void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt) { /* for all objects { u16 id u16 message length string message } */ std::string datastring(pkt->getString(0), pkt->getSize()); std::istringstream is(datastring, std::ios_base::binary); try { while (is.good()) { u16 id = readU16(is); if (!is.good()) break; std::string message = deSerializeString16(is); // Pass on to the environment m_env.processActiveObjectMessage(id, message); } } catch (SerializationError &e) { errorstream << "Client::handleCommand_ActiveObjectMessages: " << "caught SerializationError: " << e.what() << std::endl; } } void Client::handleCommand_Movement(NetworkPacket* pkt) { LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); float mad, maa, maf, msw, mscr, msf, mscl, msj, lf, lfs, ls, g; *pkt >> mad >> maa >> maf >> msw >> mscr >> msf >> mscl >> msj >> lf >> lfs >> ls >> g; player->movement_acceleration_default = mad * BS; player->movement_acceleration_air = maa * BS; player->movement_acceleration_fast = maf * BS; player->movement_speed_walk = msw * BS; player->movement_speed_crouch = mscr * BS; player->movement_speed_fast = msf * BS; player->movement_speed_climb = mscl * BS; player->movement_speed_jump = msj * BS; player->movement_liquid_fluidity = lf * BS; player->movement_liquid_fluidity_smooth = lfs * BS; player->movement_liquid_sink = ls * BS; player->movement_gravity = g * BS; } void Client::handleCommand_Fov(NetworkPacket *pkt) { f32 fov; bool is_multiplier = false; f32 transition_time = 0.0f; *pkt >> fov >> is_multiplier; // Wrap transition_time extraction within a // try-catch to preserve backwards compat try { *pkt >> transition_time; } catch (PacketError &e) {}; LocalPlayer *player = m_env.getLocalPlayer(); assert(player); player->setFov({ fov, is_multiplier, transition_time }); m_camera->notifyFovChange(); } void Client::handleCommand_HP(NetworkPacket *pkt) { LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); u16 oldhp = player->hp; u16 hp; *pkt >> hp; player->hp = hp; if (modsLoaded()) m_script->on_hp_modification(hp); if (hp < oldhp) { // Add to ClientEvent queue ClientEvent *event = new ClientEvent(); event->type = CE_PLAYER_DAMAGE; event->player_damage.amount = oldhp - hp; m_client_event_queue.push(event); } } void Client::handleCommand_Breath(NetworkPacket* pkt) { LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); u16 breath; *pkt >> breath; player->setBreath(breath); } void Client::handleCommand_MovePlayer(NetworkPacket* pkt) { LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); v3f pos; f32 pitch, yaw; *pkt >> pos >> pitch >> yaw; player->setPosition(pos); infostream << "Client got TOCLIENT_MOVE_PLAYER" << " pos=(" << pos.X << "," << pos.Y << "," << pos.Z << ")" << " pitch=" << pitch << " yaw=" << yaw << std::endl; /* Add to ClientEvent queue. This has to be sent to the main program because otherwise it would just force the pitch and yaw values to whatever the camera points to. */ ClientEvent *event = new ClientEvent(); event->type = CE_PLAYER_FORCE_MOVE; event->player_force_move.pitch = pitch; event->player_force_move.yaw = yaw; m_client_event_queue.push(event); } void Client::handleCommand_DeathScreen(NetworkPacket* pkt) { bool set_camera_point_target; v3f camera_point_target; *pkt >> set_camera_point_target; *pkt >> camera_point_target; ClientEvent *event = new ClientEvent(); event->type = CE_DEATHSCREEN; event->deathscreen.set_camera_point_target = set_camera_point_target; event->deathscreen.camera_point_target_x = camera_point_target.X; event->deathscreen.camera_point_target_y = camera_point_target.Y; event->deathscreen.camera_point_target_z = camera_point_target.Z; m_client_event_queue.push(event); } void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt) { u16 num_files; *pkt >> num_files; infostream << "Client: Received media announcement: packet size: " << pkt->getSize() << std::endl; if (m_media_downloader == NULL || m_media_downloader->isStarted()) { const char *problem = m_media_downloader ? "we already saw another announcement" : "all media has been received already"; errorstream << "Client: Received media announcement but " << problem << "! " << " files=" << num_files << " size=" << pkt->getSize() << std::endl; return; } // Mesh update thread must be stopped while // updating content definitions sanity_check(!m_mesh_update_thread.isRunning()); for (u16 i = 0; i < num_files; i++) { std::string name, sha1_base64; *pkt >> name >> sha1_base64; std::string sha1_raw = base64_decode(sha1_base64); m_media_downloader->addFile(name, sha1_raw); } { std::string str; *pkt >> str; Strfnd sf(str); while (!sf.at_end()) { std::string baseurl = trim(sf.next(",")); if (!baseurl.empty()) { m_remote_media_servers.emplace_back(baseurl); m_media_downloader->addRemoteServer(baseurl); } } } m_media_downloader->step(this); } void Client::handleCommand_Media(NetworkPacket* pkt) { /* u16 command u16 total number of file bunches u16 index of this bunch u32 number of files in this bunch for each file { u16 length of name string name u32 length of data data } */ u16 num_bunches; u16 bunch_i; u32 num_files; *pkt >> num_bunches >> bunch_i >> num_files; infostream << "Client: Received files: bunch " << bunch_i << "/" << num_bunches << " files=" << num_files << " size=" << pkt->getSize() << std::endl; if (num_files == 0) return; bool init_phase = m_media_downloader && m_media_downloader->isStarted(); if (init_phase) { // Mesh update thread must be stopped while // updating content definitions sanity_check(!m_mesh_update_thread.isRunning()); } for (u32 i = 0; i < num_files; i++) { std::string name, data; *pkt >> name; data = pkt->readLongString(); bool ok = false; if (init_phase) { ok = m_media_downloader->conventionalTransferDone(name, data, this); } else { // Check pending dynamic transfers, one of them must be it for (const auto &it : m_pending_media_downloads) { if (it.second->conventionalTransferDone(name, data, this)) { ok = true; break; } } } if (!ok) { errorstream << "Client: Received media \"" << name << "\" but no downloads pending. " << num_bunches << " bunches, " << num_files << " in this one. (init_phase=" << init_phase << ")" << std::endl; } } } void Client::handleCommand_NodeDef(NetworkPacket* pkt) {