e4d81cd2d970d92805d1e2d7737aec59c7c88c2d
[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         void setAnimations(v2f frames, float frame_speed, float frame_blend);
65         void setBonePosRot(std::string bone, v3f position, v3f rotation);
66         void setAttachment(ServerActiveObject *parent, std::string bone, v3f position, v3f rotation);
67         ObjectProperties* accessObjectProperties();
68         void notifyObjectPropertiesModified();
69         /* LuaEntitySAO-specific */
70         void setVelocity(v3f velocity);
71         v3f getVelocity();
72         void setAcceleration(v3f acceleration);
73         v3f getAcceleration();
74         void setYaw(float yaw);
75         float getYaw();
76         void setTextureMod(const std::string &mod);
77         void setSprite(v2s16 p, int num_frames, float framelength,
78                         bool select_horiz_by_yawpitch);
79         std::string getName();
80 private:
81         std::string getPropertyPacket();
82         void sendPosition(bool do_interpolate, bool is_movement_end);
83
84         std::string m_init_name;
85         std::string m_init_state;
86         bool m_registered;
87         struct ObjectProperties m_prop;
88         
89         s16 m_hp;
90         v3f m_velocity;
91         v3f m_acceleration;
92         float m_yaw;
93         ItemGroupList m_armor_groups;
94         
95         bool m_properties_sent;
96         float m_last_sent_yaw;
97         v3f m_last_sent_position;
98         v3f m_last_sent_velocity;
99         float m_last_sent_position_timer;
100         float m_last_sent_move_precision;
101         bool m_armor_groups_sent;
102         ServerActiveObject *m_parent;
103 };
104
105 /*
106         PlayerSAO needs some internals exposed.
107 */
108
109 class PlayerSAO : public ServerActiveObject
110 {
111 public:
112         PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
113                         const std::set<std::string> &privs, bool is_singleplayer);
114         ~PlayerSAO();
115         u8 getType() const
116         { return ACTIVEOBJECT_TYPE_PLAYER; }
117         u8 getSendType() const
118         { return ACTIVEOBJECT_TYPE_GENERIC; }
119         std::string getDescription();
120
121         /*
122                 Active object <-> environment interface
123         */
124
125         void addedToEnvironment(u32 dtime_s);
126         void removingFromEnvironment();
127         bool isStaticAllowed() const;
128         bool unlimitedTransferDistance() const;
129         std::string getClientInitializationData();
130         std::string getStaticData();
131         void step(float dtime, bool send_recommended);
132         void setBasePosition(const v3f &position);
133         void setPos(v3f pos);
134         void moveTo(v3f pos, bool continuous);
135
136         /*
137                 Interaction interface
138         */
139
140         int punch(v3f dir,
141                 const ToolCapabilities *toolcap,
142                 ServerActiveObject *puncher,
143                 float time_from_last_punch);
144         void rightClick(ServerActiveObject *clicker);
145         s16 getHP() const;
146         void setHP(s16 hp);
147         
148         void setArmorGroups(const ItemGroupList &armor_groups);
149         void setAnimations(v2f frames, float frame_speed, float frame_blend);
150         void setBonePosRot(std::string bone, v3f position, v3f rotation);
151         void setAttachment(ServerActiveObject *parent, std::string bone, v3f position, v3f rotation);
152         ObjectProperties* accessObjectProperties();
153         void notifyObjectPropertiesModified();
154
155         /*
156                 Inventory interface
157         */
158
159         Inventory* getInventory();
160         const Inventory* getInventory() const;
161         InventoryLocation getInventoryLocation() const;
162         void setInventoryModified();
163         std::string getWieldList() const;
164         int getWieldIndex() const;
165         void setWieldIndex(int i);
166
167         /*
168                 PlayerSAO-specific
169         */
170
171         void disconnected();
172
173         Player* getPlayer()
174         {
175                 return m_player;
176         }
177         u16 getPeerID() const
178         {
179                 return m_peer_id;
180         }
181
182         // Cheat prevention
183
184         v3f getLastGoodPosition() const
185         {
186                 return m_last_good_position;
187         }
188         float resetTimeFromLastPunch()
189         {
190                 float r = m_time_from_last_punch;
191                 m_time_from_last_punch = 0.0;
192                 return r;
193         }
194         void noCheatDigStart(v3s16 p)
195         {
196                 m_nocheat_dig_pos = p;
197                 m_nocheat_dig_time = 0;
198         }
199         v3s16 getNoCheatDigPos()
200         {
201                 return m_nocheat_dig_pos;
202         }
203         float getNoCheatDigTime()
204         {
205                 return m_nocheat_dig_time;
206         }
207         void noCheatDigEnd()
208         {
209                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
210         }
211
212         // Other
213
214         void updatePrivileges(const std::set<std::string> &privs,
215                         bool is_singleplayer)
216         {
217                 m_privs = privs;
218                 m_is_singleplayer = is_singleplayer;
219         }
220
221 private:
222         std::string getPropertyPacket();
223         
224         Player *m_player;
225         u16 m_peer_id;
226         Inventory *m_inventory;
227
228         // Cheat prevention
229         v3f m_last_good_position;
230         float m_last_good_position_age;
231         float m_time_from_last_punch;
232         v3s16 m_nocheat_dig_pos;
233         float m_nocheat_dig_time;
234
235         int m_wield_index;
236         bool m_position_not_sent;
237         ItemGroupList m_armor_groups;
238         bool m_armor_groups_sent;
239         ServerActiveObject *m_parent;
240         bool m_properties_sent;
241         struct ObjectProperties m_prop;
242         // Cached privileges for enforcement
243         std::set<std::string> m_privs;
244         bool m_is_singleplayer;
245
246 public:
247         // Some flags used by Server
248         bool m_teleported;
249         bool m_inventory_not_sent;
250         bool m_hp_not_sent;
251         bool m_wielded_item_not_sent;
252 };
253
254 #endif
255