1cbf492b1fccefe354d0a1a0b2731cbb30361d6d
[oweals/minetest.git] / src / content_sao.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef CONTENT_SAO_HEADER
21 #define CONTENT_SAO_HEADER
22
23 #include "serverobject.h"
24 #include "content_object.h"
25 #include "itemgroup.h"
26 #include "player.h"
27 #include "object_properties.h"
28
29 ServerActiveObject* createItemSAO(ServerEnvironment *env, v3f pos,
30                 const std::string itemstring);
31
32 /*
33         LuaEntitySAO needs some internals exposed.
34 */
35
36 class LuaEntitySAO : public ServerActiveObject
37 {
38 public:
39         LuaEntitySAO(ServerEnvironment *env, v3f pos,
40                         const std::string &name, const std::string &state);
41         ~LuaEntitySAO();
42         u8 getType() const
43         { return ACTIVEOBJECT_TYPE_LUAENTITY; }
44         u8 getSendType() const
45         { return ACTIVEOBJECT_TYPE_GENERIC; }
46         virtual void addedToEnvironment(u32 dtime_s);
47         static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
48                         const std::string &data);
49         void step(float dtime, bool send_recommended);
50         std::string getClientInitializationData();
51         std::string getStaticData();
52         int punch(v3f dir,
53                         const ToolCapabilities *toolcap=NULL,
54                         ServerActiveObject *puncher=NULL,
55                         float time_from_last_punch=1000000);
56         void rightClick(ServerActiveObject *clicker);
57         void setPos(v3f pos);
58         void moveTo(v3f pos, bool continuous);
59         float getMinimumSavedMovement();
60         std::string getDescription();
61         void setHP(s16 hp);
62         s16 getHP() const;
63         void setArmorGroups(const ItemGroupList &armor_groups);
64         ObjectProperties* accessObjectProperties();
65         void notifyObjectPropertiesModified();
66         /* LuaEntitySAO-specific */
67         void setVelocity(v3f velocity);
68         v3f getVelocity();
69         void setAcceleration(v3f acceleration);
70         v3f getAcceleration();
71         void setYaw(float yaw);
72         float getYaw();
73         void setTextureMod(const std::string &mod);
74         void setSprite(v2s16 p, int num_frames, float framelength,
75                         bool select_horiz_by_yawpitch);
76         void setAnimations(int frame_start, int frame_end, float frame_speed, float frame_blend);
77         std::string getName();
78 private:
79         std::string getPropertyPacket();
80         void sendPosition(bool do_interpolate, bool is_movement_end);
81
82         std::string m_init_name;
83         std::string m_init_state;
84         bool m_registered;
85         struct ObjectProperties m_prop;
86         
87         s16 m_hp;
88         v3f m_velocity;
89         v3f m_acceleration;
90         float m_yaw;
91         ItemGroupList m_armor_groups;
92         
93         bool m_properties_sent;
94         float m_last_sent_yaw;
95         v3f m_last_sent_position;
96         v3f m_last_sent_velocity;
97         float m_last_sent_position_timer;
98         float m_last_sent_move_precision;
99         bool m_armor_groups_sent;
100 };
101
102 /*
103         PlayerSAO needs some internals exposed.
104 */
105
106 class PlayerSAO : public ServerActiveObject
107 {
108 public:
109         PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
110                         const std::set<std::string> &privs, bool is_singleplayer);
111         ~PlayerSAO();
112         u8 getType() const
113         { return ACTIVEOBJECT_TYPE_PLAYER; }
114         u8 getSendType() const
115         { return ACTIVEOBJECT_TYPE_GENERIC; }
116         std::string getDescription();
117
118         /*
119                 Active object <-> environment interface
120         */
121
122         void addedToEnvironment(u32 dtime_s);
123         void removingFromEnvironment();
124         bool isStaticAllowed() const;
125         bool unlimitedTransferDistance() const;
126         std::string getClientInitializationData();
127         std::string getStaticData();
128         void step(float dtime, bool send_recommended);
129         void setBasePosition(const v3f &position);
130         void setPos(v3f pos);
131         void moveTo(v3f pos, bool continuous);
132
133         /*
134                 Interaction interface
135         */
136
137         int punch(v3f dir,
138                 const ToolCapabilities *toolcap,
139                 ServerActiveObject *puncher,
140                 float time_from_last_punch);
141         void rightClick(ServerActiveObject *clicker);
142         s16 getHP() const;
143         void setHP(s16 hp);
144         
145         void setArmorGroups(const ItemGroupList &armor_groups);
146         ObjectProperties* accessObjectProperties();
147         void notifyObjectPropertiesModified();
148
149         /*
150                 Inventory interface
151         */
152
153         Inventory* getInventory();
154         const Inventory* getInventory() const;
155         InventoryLocation getInventoryLocation() const;
156         void setInventoryModified();
157         std::string getWieldList() const;
158         int getWieldIndex() const;
159         void setWieldIndex(int i);
160
161         /*
162                 PlayerSAO-specific
163         */
164
165         void disconnected();
166
167         Player* getPlayer()
168         {
169                 return m_player;
170         }
171         u16 getPeerID() const
172         {
173                 return m_peer_id;
174         }
175
176         // Cheat prevention
177
178         v3f getLastGoodPosition() const
179         {
180                 return m_last_good_position;
181         }
182         float resetTimeFromLastPunch()
183         {
184                 float r = m_time_from_last_punch;
185                 m_time_from_last_punch = 0.0;
186                 return r;
187         }
188         void noCheatDigStart(v3s16 p)
189         {
190                 m_nocheat_dig_pos = p;
191                 m_nocheat_dig_time = 0;
192         }
193         v3s16 getNoCheatDigPos()
194         {
195                 return m_nocheat_dig_pos;
196         }
197         float getNoCheatDigTime()
198         {
199                 return m_nocheat_dig_time;
200         }
201         void noCheatDigEnd()
202         {
203                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
204         }
205
206         // Other
207
208         void updatePrivileges(const std::set<std::string> &privs,
209                         bool is_singleplayer)
210         {
211                 m_privs = privs;
212                 m_is_singleplayer = is_singleplayer;
213         }
214
215 private:
216         std::string getPropertyPacket();
217         
218         Player *m_player;
219         u16 m_peer_id;
220         Inventory *m_inventory;
221
222         // Cheat prevention
223         v3f m_last_good_position;
224         float m_last_good_position_age;
225         float m_time_from_last_punch;
226         v3s16 m_nocheat_dig_pos;
227         float m_nocheat_dig_time;
228
229         int m_wield_index;
230         bool m_position_not_sent;
231         ItemGroupList m_armor_groups;
232         bool m_armor_groups_sent;
233         bool m_properties_sent;
234         struct ObjectProperties m_prop;
235         // Cached privileges for enforcement
236         std::set<std::string> m_privs;
237         bool m_is_singleplayer;
238
239 public:
240         // Some flags used by Server
241         bool m_teleported;
242         bool m_inventory_not_sent;
243         bool m_hp_not_sent;
244         bool m_wielded_item_not_sent;
245 };
246
247 #endif
248