Implement player attribute backend (#4155)
[oweals/minetest.git] / src / remoteplayer.h
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 #ifndef REMOTEPLAYER_HEADER
22 #define REMOTEPLAYER_HEADER
23
24 #include "player.h"
25
26 class PlayerSAO;
27
28 enum RemotePlayerChatResult
29 {
30         RPLAYER_CHATRESULT_OK,
31         RPLAYER_CHATRESULT_FLOODING,
32         RPLAYER_CHATRESULT_KICK,
33 };
34
35 /*
36         Player on the server
37 */
38 class RemotePlayer : public Player
39 {
40 public:
41         RemotePlayer(const char *name, IItemDefManager *idef);
42         virtual ~RemotePlayer() {}
43
44         void save(std::string savedir, IGameDef *gamedef);
45         void deSerialize(std::istream &is, const std::string &playername, PlayerSAO *sao);
46
47         PlayerSAO *getPlayerSAO() { return m_sao; }
48         void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; }
49
50         const RemotePlayerChatResult canSendChatMessage();
51
52         void setHotbarItemcount(s32 hotbar_itemcount)
53         {
54                 hud_hotbar_itemcount = hotbar_itemcount;
55         }
56
57         s32 getHotbarItemcount() const { return hud_hotbar_itemcount; }
58
59         void overrideDayNightRatio(bool do_override, float ratio)
60         {
61                 m_day_night_ratio_do_override = do_override;
62                 m_day_night_ratio = ratio;
63         }
64
65         void getDayNightRatio(bool *do_override, float *ratio)
66         {
67                 *do_override = m_day_night_ratio_do_override;
68                 *ratio = m_day_night_ratio;
69         }
70
71         void setHotbarImage(const std::string &name)
72         {
73                 hud_hotbar_image = name;
74         }
75
76         std::string getHotbarImage() const
77         {
78                 return hud_hotbar_image;
79         }
80
81         void setHotbarSelectedImage(const std::string &name)
82         {
83                 hud_hotbar_selected_image = name;
84         }
85
86         const std::string &getHotbarSelectedImage() const
87         {
88                 return hud_hotbar_selected_image;
89         }
90
91         void setSky(const video::SColor &bgcolor, const std::string &type,
92                                 const std::vector<std::string> &params)
93         {
94                 m_sky_bgcolor = bgcolor;
95                 m_sky_type = type;
96                 m_sky_params = params;
97         }
98
99         void getSky(video::SColor *bgcolor, std::string *type,
100                                 std::vector<std::string> *params)
101         {
102                 *bgcolor = m_sky_bgcolor;
103                 *type = m_sky_type;
104                 *params = m_sky_params;
105         }
106
107         bool checkModified() const { return m_dirty || inventory.checkModified(); }
108
109         void setModified(const bool x)
110         {
111                 m_dirty = x;
112                 if (!x)
113                         inventory.setModified(x);
114         }
115
116         void setLocalAnimations(v2s32 frames[4], float frame_speed)
117         {
118                 for (int i = 0; i < 4; i++)
119                         local_animations[i] = frames[i];
120                 local_animation_speed = frame_speed;
121         }
122
123         void getLocalAnimations(v2s32 *frames, float *frame_speed)
124         {
125                 for (int i = 0; i < 4; i++)
126                         frames[i] = local_animations[i];
127                 *frame_speed = local_animation_speed;
128         }
129
130         void setDirty(bool dirty) { m_dirty = true; }
131
132         u16 protocol_version;
133 private:
134         /*
135                 serialize() writes a bunch of text that can contain
136                 any characters except a '\0', and such an ending that
137                 deSerialize stops reading exactly at the right point.
138         */
139         void serialize(std::ostream &os);
140         void serializeExtraAttributes(std::string &output);
141
142         PlayerSAO *m_sao;
143         bool m_dirty;
144
145         static bool m_setting_cache_loaded;
146         static float m_setting_chat_message_limit_per_10sec;
147         static u16 m_setting_chat_message_limit_trigger_kick;
148
149         u32 m_last_chat_message_sent;
150         float m_chat_message_allowance;
151         u16 m_message_rate_overhead;
152
153         bool m_day_night_ratio_do_override;
154         float m_day_night_ratio;
155         std::string hud_hotbar_image;
156         std::string hud_hotbar_selected_image;
157
158         std::string m_sky_type;
159         video::SColor m_sky_bgcolor;
160         std::vector<std::string> m_sky_params;
161 };
162
163 #endif