]> git.lizzy.rs Git - minetest.git/blob - src/remoteplayer.cpp
Optimize headers (part 2) (#6272)
[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 <json/json.h>
23 #include "content_sao.h"
24 #include "filesys.h"
25 #include "gamedef.h"
26 #include "porting.h"  // strlcpy
27 #include "server.h"
28 #include "settings.h"
29
30 /*
31         RemotePlayer
32 */
33 // static config cache for remoteplayer
34 bool RemotePlayer::m_setting_cache_loaded = false;
35 float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f;
36 u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0;
37
38 RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
39         Player(name, idef)
40 {
41         if (!RemotePlayer::m_setting_cache_loaded) {
42                 RemotePlayer::m_setting_chat_message_limit_per_10sec =
43                         g_settings->getFloat("chat_message_limit_per_10sec");
44                 RemotePlayer::m_setting_chat_message_limit_trigger_kick =
45                         g_settings->getU16("chat_message_limit_trigger_kick");
46                 RemotePlayer::m_setting_cache_loaded = true;
47         }
48         movement_acceleration_default   = g_settings->getFloat("movement_acceleration_default")   * BS;
49         movement_acceleration_air       = g_settings->getFloat("movement_acceleration_air")       * BS;
50         movement_acceleration_fast      = g_settings->getFloat("movement_acceleration_fast")      * BS;
51         movement_speed_walk             = g_settings->getFloat("movement_speed_walk")             * BS;
52         movement_speed_crouch           = g_settings->getFloat("movement_speed_crouch")           * BS;
53         movement_speed_fast             = g_settings->getFloat("movement_speed_fast")             * BS;
54         movement_speed_climb            = g_settings->getFloat("movement_speed_climb")            * BS;
55         movement_speed_jump             = g_settings->getFloat("movement_speed_jump")             * BS;
56         movement_liquid_fluidity        = g_settings->getFloat("movement_liquid_fluidity")        * BS;
57         movement_liquid_fluidity_smooth = g_settings->getFloat("movement_liquid_fluidity_smooth") * BS;
58         movement_liquid_sink            = g_settings->getFloat("movement_liquid_sink")            * BS;
59         movement_gravity                = g_settings->getFloat("movement_gravity")                * BS;
60
61         // copy defaults
62         m_cloud_params.density = 0.4f;
63         m_cloud_params.color_bright = video::SColor(229, 240, 240, 255);
64         m_cloud_params.color_ambient = video::SColor(255, 0, 0, 0);
65         m_cloud_params.height = 120.0f;
66         m_cloud_params.thickness = 16.0f;
67         m_cloud_params.speed = v2f(0.0f, -2.0f);
68 }
69
70 void RemotePlayer::serializeExtraAttributes(std::string &output)
71 {
72         assert(m_sao);
73         Json::Value json_root;
74         const PlayerAttributes &attrs = m_sao->getExtendedAttributes();
75         for (PlayerAttributes::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
76                 json_root[(*it).first] = (*it).second;
77         }
78
79         Json::FastWriter writer;
80         output = writer.write(json_root);
81         m_sao->setExtendedAttributeModified(false);
82 }
83
84
85 void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,
86                 PlayerSAO *sao)
87 {
88         Settings args;
89
90         if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
91                 throw SerializationError("PlayerArgsEnd of player " + playername + " not found!");
92         }
93
94         m_dirty = true;
95         //args.getS32("version"); // Version field value not used
96         const std::string &name = args.get("name");
97         strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
98
99         if (sao) {
100                 try {
101                         sao->setHPRaw(args.getS32("hp"));
102                 } catch(SettingNotFoundException &e) {
103                         sao->setHPRaw(PLAYER_MAX_HP);
104                 }
105
106                 try {
107                         sao->setBasePosition(args.getV3F("position"));
108                 } catch (SettingNotFoundException &e) {}
109
110                 try {
111                         sao->setPitch(args.getFloat("pitch"));
112                 } catch (SettingNotFoundException &e) {}
113                 try {
114                         sao->setYaw(args.getFloat("yaw"));
115                 } catch (SettingNotFoundException &e) {}
116
117                 try {
118                         sao->setBreath(args.getS32("breath"), false);
119                 } catch (SettingNotFoundException &e) {}
120
121                 try {
122                         const std::string &extended_attributes = args.get("extended_attributes");
123                         Json::Reader reader;
124                         Json::Value attr_root;
125                         reader.parse(extended_attributes, attr_root);
126
127                         const Json::Value::Members attr_list = attr_root.getMemberNames();
128                         for (Json::Value::Members::const_iterator it = attr_list.begin();
129                                         it != attr_list.end(); ++it) {
130                                 Json::Value attr_value = attr_root[*it];
131                                 sao->setExtendedAttribute(*it, attr_value.asString());
132                         }
133                 } catch (SettingNotFoundException &e) {}
134         }
135
136         inventory.deSerialize(is);
137
138         if (inventory.getList("craftpreview") == NULL) {
139                 // Convert players without craftpreview
140                 inventory.addList("craftpreview", 1);
141
142                 bool craftresult_is_preview = true;
143                 if(args.exists("craftresult_is_preview"))
144                         craftresult_is_preview = args.getBool("craftresult_is_preview");
145                 if(craftresult_is_preview)
146                 {
147                         // Clear craftresult
148                         inventory.getList("craftresult")->changeItem(0, ItemStack());
149                 }
150         }
151 }
152
153 void RemotePlayer::serialize(std::ostream &os)
154 {
155         // Utilize a Settings object for storing values
156         Settings args;
157         args.setS32("version", 1);
158         args.set("name", m_name);
159
160         // This should not happen
161         assert(m_sao);
162         args.setS32("hp", m_sao->getHP());
163         args.setV3F("position", m_sao->getBasePosition());
164         args.setFloat("pitch", m_sao->getPitch());
165         args.setFloat("yaw", m_sao->getYaw());
166         args.setS32("breath", m_sao->getBreath());
167
168         std::string extended_attrs = "";
169         serializeExtraAttributes(extended_attrs);
170         args.set("extended_attributes", extended_attrs);
171
172         args.writeLines(os);
173
174         os<<"PlayerArgsEnd\n";
175
176         inventory.serialize(os);
177 }
178
179 const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
180 {
181         // Rate limit messages
182         u32 now = time(NULL);
183         float time_passed = now - m_last_chat_message_sent;
184         m_last_chat_message_sent = now;
185
186         // If this feature is disabled
187         if (m_setting_chat_message_limit_per_10sec <= 0.0) {
188                 return RPLAYER_CHATRESULT_OK;
189         }
190
191         m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f);
192         if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) {
193                 m_chat_message_allowance = m_setting_chat_message_limit_per_10sec;
194         }
195
196         if (m_chat_message_allowance < 1.0f) {
197                 infostream << "Player " << m_name
198                                 << " chat limited due to excessive message amount." << std::endl;
199
200                 // Kick player if flooding is too intensive
201                 m_message_rate_overhead++;
202                 if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) {
203                         return RPLAYER_CHATRESULT_KICK;
204                 }
205
206                 return RPLAYER_CHATRESULT_FLOODING;
207         }
208
209         // Reinit message overhead
210         if (m_message_rate_overhead > 0) {
211                 m_message_rate_overhead = 0;
212         }
213
214         m_chat_message_allowance -= 1.0f;
215         return RPLAYER_CHATRESULT_OK;
216 }