]> git.lizzy.rs Git - minetest.git/blob - src/remoteplayer.cpp
Move RemotePlayer code to its own cpp/header
[minetest.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);
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 {
120         Settings args;
121
122         if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
123                 throw SerializationError("PlayerArgsEnd of player " +
124                                                                  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         setPitch(args.getFloat("pitch"));
132         setYaw(args.getFloat("yaw"));
133         setPosition(args.getV3F("position"));
134         try {
135                 hp = args.getS32("hp");
136         } catch(SettingNotFoundException &e) {
137                 hp = PLAYER_MAX_HP;
138         }
139
140         try {
141                 m_breath = args.getS32("breath");
142         } catch(SettingNotFoundException &e) {
143                 m_breath = PLAYER_MAX_BREATH;
144         }
145
146         inventory.deSerialize(is);
147
148         if(inventory.getList("craftpreview") == NULL) {
149                 // Convert players without craftpreview
150                 inventory.addList("craftpreview", 1);
151
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)
156                 {
157                         // Clear craftresult
158                         inventory.getList("craftresult")->changeItem(0, ItemStack());
159                 }
160         }
161 }
162
163 void RemotePlayer::serialize(std::ostream &os)
164 {
165         // Utilize a Settings object for storing values
166         Settings args;
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);
175
176         args.writeLines(os);
177
178         os<<"PlayerArgsEnd\n";
179
180         inventory.serialize(os);
181 }
182
183 void RemotePlayer::setPosition(const v3f &position)
184 {
185         if (position != m_position)
186                 m_dirty = true;
187
188         Player::setPosition(position);
189         if(m_sao)
190                 m_sao->setBasePosition(position);
191 }
192
193 const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
194 {
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;
199
200         // If this feature is disabled
201         if (m_setting_chat_message_limit_per_10sec <= 0.0) {
202                 return RPLAYER_CHATRESULT_OK;
203         }
204
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;
208         }
209
210         if (m_chat_message_allowance < 1.0f) {
211                 infostream << "Player " << m_name
212                                 << " chat limited due to excessive message amount." << std::endl;
213
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;
218                 }
219
220                 return RPLAYER_CHATRESULT_FLOODING;
221         }
222
223         // Reinit message overhead
224         if (m_message_rate_overhead > 0) {
225                 m_message_rate_overhead = 0;
226         }
227
228         m_chat_message_allowance -= 1.0f;
229         return RPLAYER_CHATRESULT_OK;
230 }