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);
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)
122 if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
123 throw SerializationError("PlayerArgsEnd of player " +
124 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);
131 setPitch(args.getFloat("pitch"));
132 setYaw(args.getFloat("yaw"));
133 setPosition(args.getV3F("position"));
135 hp = args.getS32("hp");
136 } catch(SettingNotFoundException &e) {
141 m_breath = args.getS32("breath");
142 } catch(SettingNotFoundException &e) {
143 m_breath = PLAYER_MAX_BREATH;
146 inventory.deSerialize(is);
148 if(inventory.getList("craftpreview") == NULL) {
149 // Convert players without craftpreview
150 inventory.addList("craftpreview", 1);
152 bool craftresult_is_preview = true;
153 if(args.exists("craftresult_is_preview"))
154 craftresult_is_preview = args.getBool("craftresult_is_preview");
155 if(craftresult_is_preview)
158 inventory.getList("craftresult")->changeItem(0, ItemStack());
163 void RemotePlayer::serialize(std::ostream &os)
165 // Utilize a Settings object for storing values
167 args.setS32("version", 1);
168 args.set("name", m_name);
169 //args.set("password", m_password);
170 args.setFloat("pitch", m_pitch);
171 args.setFloat("yaw", m_yaw);
172 args.setV3F("position", m_position);
173 args.setS32("hp", hp);
174 args.setS32("breath", m_breath);
178 os<<"PlayerArgsEnd\n";
180 inventory.serialize(os);
183 void RemotePlayer::setPosition(const v3f &position)
185 if (position != m_position)
188 Player::setPosition(position);
190 m_sao->setBasePosition(position);
193 const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
195 // Rate limit messages
196 u32 now = time(NULL);
197 float time_passed = now - m_last_chat_message_sent;
198 m_last_chat_message_sent = now;
200 // If this feature is disabled
201 if (m_setting_chat_message_limit_per_10sec <= 0.0) {
202 return RPLAYER_CHATRESULT_OK;
205 m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f);
206 if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) {
207 m_chat_message_allowance = m_setting_chat_message_limit_per_10sec;
210 if (m_chat_message_allowance < 1.0f) {
211 infostream << "Player " << m_name
212 << " chat limited due to excessive message amount." << std::endl;
214 // Kick player if flooding is too intensive
215 m_message_rate_overhead++;
216 if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) {
217 return RPLAYER_CHATRESULT_KICK;
220 return RPLAYER_CHATRESULT_FLOODING;
223 // Reinit message overhead
224 if (m_message_rate_overhead > 0) {
225 m_message_rate_overhead = 0;
228 m_chat_message_allowance -= 1.0f;
229 return RPLAYER_CHATRESULT_OK;