Enable client-side attachments, add detachment code
[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         
103         v2f m_animation_frames;
104         float m_animation_speed;
105         float m_animation_blend;
106         bool m_animations_sent;
107
108         std::map<std::string, core::vector2d<v3f> > m_animation_bone;
109         bool m_animations_bone_sent;
110         
111         ServerActiveObject *m_parent;
112         int m_attachment_parent_id;
113         std::string m_attachment_bone;
114         v3f m_attachment_position;
115         v3f m_attachment_rotation;
116         bool m_attachment_sent;
117 };
118
119 /*
120         PlayerSAO needs some internals exposed.
121 */
122
123 class PlayerSAO : public ServerActiveObject
124 {
125 public:
126         PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
127                         const std::set<std::string> &privs, bool is_singleplayer);
128         ~PlayerSAO();
129         u8 getType() const
130         { return ACTIVEOBJECT_TYPE_PLAYER; }
131         u8 getSendType() const
132         { return ACTIVEOBJECT_TYPE_GENERIC; }
133         std::string getDescription();
134
135         /*
136                 Active object <-> environment interface
137         */
138
139         void addedToEnvironment(u32 dtime_s);
140         void removingFromEnvironment();
141         bool isStaticAllowed() const;
142         bool unlimitedTransferDistance() const;
143         std::string getClientInitializationData();
144         std::string getStaticData();
145         void step(float dtime, bool send_recommended);
146         void setBasePosition(const v3f &position);
147         void setPos(v3f pos);
148         void moveTo(v3f pos, bool continuous);
149
150         /*
151                 Interaction interface
152         */
153
154         int punch(v3f dir,
155                 const ToolCapabilities *toolcap,
156                 ServerActiveObject *puncher,
157                 float time_from_last_punch);
158         void rightClick(ServerActiveObject *clicker);
159         s16 getHP() const;
160         void setHP(s16 hp);
161         
162         void setArmorGroups(const ItemGroupList &armor_groups);
163         void setAnimations(v2f frames, float frame_speed, float frame_blend);
164         void setBonePosRot(std::string bone, v3f position, v3f rotation);
165         void setAttachment(ServerActiveObject *parent, std::string bone, v3f position, v3f rotation);
166         ObjectProperties* accessObjectProperties();
167         void notifyObjectPropertiesModified();
168
169         /*
170                 Inventory interface
171         */
172
173         Inventory* getInventory();
174         const Inventory* getInventory() const;
175         InventoryLocation getInventoryLocation() const;
176         void setInventoryModified();
177         std::string getWieldList() const;
178         int getWieldIndex() const;
179         void setWieldIndex(int i);
180
181         /*
182                 PlayerSAO-specific
183         */
184
185         void disconnected();
186
187         Player* getPlayer()
188         {
189                 return m_player;
190         }
191         u16 getPeerID() const
192         {
193                 return m_peer_id;
194         }
195
196         // Cheat prevention
197
198         v3f getLastGoodPosition() const
199         {
200                 return m_last_good_position;
201         }
202         float resetTimeFromLastPunch()
203         {
204                 float r = m_time_from_last_punch;
205                 m_time_from_last_punch = 0.0;
206                 return r;
207         }
208         void noCheatDigStart(v3s16 p)
209         {
210                 m_nocheat_dig_pos = p;
211                 m_nocheat_dig_time = 0;
212         }
213         v3s16 getNoCheatDigPos()
214         {
215                 return m_nocheat_dig_pos;
216         }
217         float getNoCheatDigTime()
218         {
219                 return m_nocheat_dig_time;
220         }
221         void noCheatDigEnd()
222         {
223                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
224         }
225
226         // Other
227
228         void updatePrivileges(const std::set<std::string> &privs,
229                         bool is_singleplayer)
230         {
231                 m_privs = privs;
232                 m_is_singleplayer = is_singleplayer;
233         }
234
235 private:
236         std::string getPropertyPacket();
237         
238         Player *m_player;
239         u16 m_peer_id;
240         Inventory *m_inventory;
241
242         // Cheat prevention
243         v3f m_last_good_position;
244         float m_last_good_position_age;
245         float m_time_from_last_punch;
246         v3s16 m_nocheat_dig_pos;
247         float m_nocheat_dig_time;
248
249         int m_wield_index;
250         bool m_position_not_sent;
251         ItemGroupList m_armor_groups;
252         bool m_armor_groups_sent;
253
254
255
256         bool m_properties_sent;
257         struct ObjectProperties m_prop;
258         // Cached privileges for enforcement
259         std::set<std::string> m_privs;
260         bool m_is_singleplayer;
261
262         v2f m_animation_frames;
263         float m_animation_speed;
264         float m_animation_blend;
265         bool m_animations_sent;
266
267         std::map<std::string, core::vector2d<v3f> > m_animation_bone;
268         bool m_animations_bone_sent;
269         
270         ServerActiveObject *m_parent;
271         int m_attachment_parent_id;
272         std::string m_attachment_bone;
273         v3f m_attachment_position;
274         v3f m_attachment_rotation;
275         bool m_attachment_sent;
276
277 public:
278         // Some flags used by Server
279         bool m_teleported;
280         bool m_inventory_not_sent;
281         bool m_hp_not_sent;
282         bool m_wielded_item_not_sent;
283 };
284
285 #endif
286