3 Copyright (C) 2010-2016 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2014-2016 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "remoteplayer.h"
22 #include "content_sao.h"
25 #include "porting.h" // strlcpy
32 // static config cache for remoteplayer
33 bool RemotePlayer::m_setting_cache_loaded = false;
34 float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f;
35 u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0;
37 RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
42 m_last_chat_message_sent(time(NULL)),
43 m_chat_message_allowance(5.0f),
44 m_message_rate_overhead(0),
46 hud_hotbar_selected_image("")
48 if (!RemotePlayer::m_setting_cache_loaded) {
49 RemotePlayer::m_setting_chat_message_limit_per_10sec =
50 g_settings->getFloat("chat_message_limit_per_10sec");
51 RemotePlayer::m_setting_chat_message_limit_trigger_kick =
52 g_settings->getU16("chat_message_limit_trigger_kick");
53 RemotePlayer::m_setting_cache_loaded = true;
55 movement_acceleration_default = g_settings->getFloat("movement_acceleration_default") * BS;
56 movement_acceleration_air = g_settings->getFloat("movement_acceleration_air") * BS;
57 movement_acceleration_fast = g_settings->getFloat("movement_acceleration_fast") * BS;
58 movement_speed_walk = g_settings->getFloat("movement_speed_walk") * BS;
59 movement_speed_crouch = g_settings->getFloat("movement_speed_crouch") * BS;
60 movement_speed_fast = g_settings->getFloat("movement_speed_fast") * BS;
61 movement_speed_climb = g_settings->getFloat("movement_speed_climb") * BS;
62 movement_speed_jump = g_settings->getFloat("movement_speed_jump") * BS;
63 movement_liquid_fluidity = g_settings->getFloat("movement_liquid_fluidity") * BS;
64 movement_liquid_fluidity_smooth = g_settings->getFloat("movement_liquid_fluidity_smooth") * BS;
65 movement_liquid_sink = g_settings->getFloat("movement_liquid_sink") * BS;
66 movement_gravity = g_settings->getFloat("movement_gravity") * BS;
69 void RemotePlayer::save(std::string savedir, IGameDef *gamedef)
72 * We have to open all possible player files in the players directory
73 * and check their player names because some file systems are not
74 * case-sensitive and player names are case-sensitive.
77 // A player to deserialize files into to check their names
78 RemotePlayer testplayer("", gamedef->idef());
81 std::string path = savedir + m_name;
82 for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
83 if (!fs::PathExists(path)) {
84 // Open file and serialize
85 std::ostringstream ss(std::ios_base::binary);
87 if (!fs::safeWriteToFile(path, ss.str())) {
88 infostream << "Failed to write " << path << std::endl;
93 // Open file and deserialize
94 std::ifstream is(path.c_str(), std::ios_base::binary);
96 infostream << "Failed to open " << path << std::endl;
99 testplayer.deSerialize(is, path, NULL);
101 if (strcmp(testplayer.getName(), m_name) == 0) {
102 // Open file and serialize
103 std::ostringstream ss(std::ios_base::binary);
105 if (!fs::safeWriteToFile(path, ss.str())) {
106 infostream << "Failed to write " << path << std::endl;
111 path = savedir + m_name + itos(i);
114 infostream << "Didn't find free file for player " << m_name << std::endl;
118 void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,
123 if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
124 throw SerializationError("PlayerArgsEnd of player " + playername + " not found!");
128 //args.getS32("version"); // Version field value not used
129 std::string name = args.get("name");
130 strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
134 sao->setHPRaw(args.getS32("hp"));
135 } catch(SettingNotFoundException &e) {
136 sao->setHPRaw(PLAYER_MAX_HP);
140 sao->setBasePosition(args.getV3F("position"));
141 } catch (SettingNotFoundException &e) {}
144 sao->setPitch(args.getFloat("pitch"));
145 } catch (SettingNotFoundException &e) {}
147 sao->setYaw(args.getFloat("yaw"));
148 } catch (SettingNotFoundException &e) {}
151 sao->setBreath(args.getS32("breath"));
152 } catch (SettingNotFoundException &e) {}
155 inventory.deSerialize(is);
157 if (inventory.getList("craftpreview") == NULL) {
158 // Convert players without craftpreview
159 inventory.addList("craftpreview", 1);
161 bool craftresult_is_preview = true;
162 if(args.exists("craftresult_is_preview"))
163 craftresult_is_preview = args.getBool("craftresult_is_preview");
164 if(craftresult_is_preview)
167 inventory.getList("craftresult")->changeItem(0, ItemStack());
172 void RemotePlayer::serialize(std::ostream &os)
174 // Utilize a Settings object for storing values
176 args.setS32("version", 1);
177 args.set("name", m_name);
178 //args.set("password", m_password);
180 // This should not happen
182 args.setS32("hp", m_sao->getHP());
183 args.setV3F("position", m_sao->getBasePosition());
184 args.setFloat("pitch", m_sao->getPitch());
185 args.setFloat("yaw", m_sao->getYaw());
186 args.setS32("breath", m_sao->getBreath());
190 os<<"PlayerArgsEnd\n";
192 inventory.serialize(os);
195 const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
197 // Rate limit messages
198 u32 now = time(NULL);
199 float time_passed = now - m_last_chat_message_sent;
200 m_last_chat_message_sent = now;
202 // If this feature is disabled
203 if (m_setting_chat_message_limit_per_10sec <= 0.0) {
204 return RPLAYER_CHATRESULT_OK;
207 m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f);
208 if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) {
209 m_chat_message_allowance = m_setting_chat_message_limit_per_10sec;
212 if (m_chat_message_allowance < 1.0f) {
213 infostream << "Player " << m_name
214 << " chat limited due to excessive message amount." << std::endl;
216 // Kick player if flooding is too intensive
217 m_message_rate_overhead++;
218 if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) {
219 return RPLAYER_CHATRESULT_KICK;
222 return RPLAYER_CHATRESULT_FLOODING;
225 // Reinit message overhead
226 if (m_message_rate_overhead > 0) {
227 m_message_rate_overhead = 0;
230 m_chat_message_allowance -= 1.0f;
231 return RPLAYER_CHATRESULT_OK;