]> git.lizzy.rs Git - dragonfireclient.git/blob - src/remoteplayer.cpp
Breath cheat fix: server side
[dragonfireclient.git] / src / remoteplayer.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2016 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2014-2016 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
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.
10
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.
15
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.
19 */
20
21 #include "remoteplayer.h"
22 #include "content_sao.h"
23 #include "filesys.h"
24 #include "gamedef.h"
25 #include "porting.h"  // strlcpy
26 #include "settings.h"
27
28
29 /*
30         RemotePlayer
31 */
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;
36
37 RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
38         Player(name, idef),
39         protocol_version(0),
40         m_sao(NULL),
41         m_dirty(false),
42         m_last_chat_message_sent(time(NULL)),
43         m_chat_message_allowance(5.0f),
44         m_message_rate_overhead(0),
45         hud_hotbar_image(""),
46         hud_hotbar_selected_image("")
47 {
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;
54         }
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;
67 }
68
69 void RemotePlayer::save(std::string savedir, IGameDef *gamedef)
70 {
71         /*
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.
75          */
76
77         // A player to deserialize files into to check their names
78         RemotePlayer testplayer("", gamedef->idef());
79
80         savedir += DIR_DELIM;
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);
86                         serialize(ss);
87                         if (!fs::safeWriteToFile(path, ss.str())) {
88                                 infostream << "Failed to write " << path << std::endl;
89                         }
90                         setModified(false);
91                         return;
92                 }
93                 // Open file and deserialize
94                 std::ifstream is(path.c_str(), std::ios_base::binary);
95                 if (!is.good()) {
96                         infostream << "Failed to open " << path << std::endl;
97                         return;
98                 }
99                 testplayer.deSerialize(is, path, NULL);
100                 is.close();
101                 if (strcmp(testplayer.getName(), m_name) == 0) {
102                         // Open file and serialize
103                         std::ostringstream ss(std::ios_base::binary);
104                         serialize(ss);
105                         if (!fs::safeWriteToFile(path, ss.str())) {
106                                 infostream << "Failed to write " << path << std::endl;
107                         }
108                         setModified(false);
109                         return;
110                 }
111                 path = savedir + m_name + itos(i);
112         }
113
114         infostream << "Didn't find free file for player " << m_name << std::endl;
115         return;
116 }
117
118 void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,
119                 PlayerSAO *sao)
120 {
121         Settings args;
122
123         if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
124                 throw SerializationError("PlayerArgsEnd of player " + playername + " not found!");
125         }
126
127         m_dirty = true;
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
132         if (sao) {
133                 try {
134                         sao->setHPRaw(args.getS32("hp"));
135                 } catch(SettingNotFoundException &e) {
136                         sao->setHPRaw(PLAYER_MAX_HP);
137                 }
138
139                 try {
140                         sao->setBasePosition(args.getV3F("position"));
141                 } catch (SettingNotFoundException &e) {}
142
143                 try {
144                         sao->setPitch(args.getFloat("pitch"));
145                 } catch (SettingNotFoundException &e) {}
146                 try {
147                         sao->setYaw(args.getFloat("yaw"));
148                 } catch (SettingNotFoundException &e) {}
149
150                 try {
151                         sao->setBreath(args.getS32("breath"), false);
152                 } catch (SettingNotFoundException &e) {}
153         }
154
155         inventory.deSerialize(is);
156
157         if (inventory.getList("craftpreview") == NULL) {
158                 // Convert players without craftpreview
159                 inventory.addList("craftpreview", 1);
160
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)
165                 {
166                         // Clear craftresult
167                         inventory.getList("craftresult")->changeItem(0, ItemStack());
168                 }
169         }
170 }
171
172 void RemotePlayer::serialize(std::ostream &os)
173 {
174         // Utilize a Settings object for storing values
175         Settings args;
176         args.setS32("version", 1);
177         args.set("name", m_name);
178         //args.set("password", m_password);
179
180         // This should not happen
181         assert(m_sao);
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());
187
188         args.writeLines(os);
189
190         os<<"PlayerArgsEnd\n";
191
192         inventory.serialize(os);
193 }
194
195 const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
196 {
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;
201
202         // If this feature is disabled
203         if (m_setting_chat_message_limit_per_10sec <= 0.0) {
204                 return RPLAYER_CHATRESULT_OK;
205         }
206
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;
210         }
211
212         if (m_chat_message_allowance < 1.0f) {
213                 infostream << "Player " << m_name
214                                 << " chat limited due to excessive message amount." << std::endl;
215
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;
220                 }
221
222                 return RPLAYER_CHATRESULT_FLOODING;
223         }
224
225         // Reinit message overhead
226         if (m_message_rate_overhead > 0) {
227                 m_message_rate_overhead = 0;
228         }
229
230         m_chat_message_allowance -= 1.0f;
231         return RPLAYER_CHATRESULT_OK;
232 }