3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #ifndef CONTENT_SAO_HEADER
21 #define CONTENT_SAO_HEADER
23 #include <util/numeric.h>
24 #include "serverobject.h"
25 #include "itemgroup.h"
26 #include "object_properties.h"
27 #include "constants.h"
29 class UnitSAO: public ServerActiveObject
32 UnitSAO(ServerEnvironment *env, v3f pos):
33 ServerActiveObject(env, pos),
37 virtual void setYaw(const float yaw) { m_yaw = yaw; }
38 float getYaw() const { return m_yaw; };
39 f32 getRadYaw() const { return m_yaw * core::DEGTORAD; }
41 f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; }
43 s16 getHP() const { return m_hp; }
44 // Use a function, if isDead can be defined by other conditions
45 bool isDead() const { return m_hp == 0; }
52 LuaEntitySAO needs some internals exposed.
55 class LuaEntitySAO : public UnitSAO
58 LuaEntitySAO(ServerEnvironment *env, v3f pos,
59 const std::string &name, const std::string &state);
61 ActiveObjectType getType() const
62 { return ACTIVEOBJECT_TYPE_LUAENTITY; }
63 ActiveObjectType getSendType() const
64 { return ACTIVEOBJECT_TYPE_GENERIC; }
65 virtual void addedToEnvironment(u32 dtime_s);
66 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
67 const std::string &data);
69 void step(float dtime, bool send_recommended);
70 std::string getClientInitializationData(u16 protocol_version);
71 std::string getStaticData();
73 const ToolCapabilities *toolcap=NULL,
74 ServerActiveObject *puncher=NULL,
75 float time_from_last_punch=1000000);
76 void rightClick(ServerActiveObject *clicker);
77 void setPos(const v3f &pos);
78 void moveTo(v3f pos, bool continuous);
79 float getMinimumSavedMovement();
80 std::string getDescription();
83 void setArmorGroups(const ItemGroupList &armor_groups);
84 ItemGroupList getArmorGroups();
85 void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
86 void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
87 void setBonePosition(const std::string &bone, v3f position, v3f rotation);
88 void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
89 void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
90 void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
91 void addAttachmentChild(int child_id);
92 void removeAttachmentChild(int child_id);
93 UNORDERED_SET<int> getAttachmentChildIds();
94 ObjectProperties* accessObjectProperties();
95 void notifyObjectPropertiesModified();
96 /* LuaEntitySAO-specific */
97 void setVelocity(v3f velocity);
99 void setAcceleration(v3f acceleration);
100 v3f getAcceleration();
102 void setTextureMod(const std::string &mod);
103 void setSprite(v2s16 p, int num_frames, float framelength,
104 bool select_horiz_by_yawpitch);
105 std::string getName();
106 bool getCollisionBox(aabb3f *toset);
107 bool collideWithObjects();
109 std::string getPropertyPacket();
110 void sendPosition(bool do_interpolate, bool is_movement_end);
112 std::string m_init_name;
113 std::string m_init_state;
115 struct ObjectProperties m_prop;
120 ItemGroupList m_armor_groups;
122 bool m_properties_sent;
123 float m_last_sent_yaw;
124 v3f m_last_sent_position;
125 v3f m_last_sent_velocity;
126 float m_last_sent_position_timer;
127 float m_last_sent_move_precision;
128 bool m_armor_groups_sent;
130 v2f m_animation_range;
131 float m_animation_speed;
132 float m_animation_blend;
133 bool m_animation_loop;
134 bool m_animation_sent;
136 UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
137 bool m_bone_position_sent;
139 int m_attachment_parent_id;
140 UNORDERED_SET<int> m_attachment_child_ids;
141 std::string m_attachment_bone;
142 v3f m_attachment_position;
143 v3f m_attachment_rotation;
144 bool m_attachment_sent;
148 PlayerSAO needs some internals exposed.
156 LagPool(): m_pool(15), m_max(15)
158 void setMax(float new_max)
164 void add(float dtime)
170 bool grab(float dtime)
174 if(m_pool + dtime > m_max)
183 class PlayerSAO : public UnitSAO
186 PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer);
188 ActiveObjectType getType() const
189 { return ACTIVEOBJECT_TYPE_PLAYER; }
190 ActiveObjectType getSendType() const
191 { return ACTIVEOBJECT_TYPE_GENERIC; }
192 std::string getDescription();
195 Active object <-> environment interface
198 void addedToEnvironment(u32 dtime_s);
199 void removingFromEnvironment();
200 bool isStaticAllowed() const;
201 std::string getClientInitializationData(u16 protocol_version);
202 std::string getStaticData();
204 void step(float dtime, bool send_recommended);
205 void setBasePosition(const v3f &position);
206 void setPos(const v3f &pos);
207 void moveTo(v3f pos, bool continuous);
208 void setYaw(const float yaw);
209 // Data should not be sent at player initialization
210 void setYawAndSend(const float yaw);
211 void setPitch(const float pitch);
212 // Data should not be sent at player initialization
213 void setPitchAndSend(const float pitch);
214 f32 getPitch() const { return m_pitch; }
215 f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
217 f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
218 void setFov(const float pitch);
219 f32 getFov() const { return m_fov; }
220 void setWantedRange(const s16 range);
221 s16 getWantedRange() const { return m_wanted_range; }
224 Interaction interface
228 const ToolCapabilities *toolcap,
229 ServerActiveObject *puncher,
230 float time_from_last_punch);
231 void rightClick(ServerActiveObject *clicker);
233 void setHPRaw(s16 hp) { m_hp = hp; }
235 u16 getBreath() const { return m_breath; }
236 void setBreath(const u16 breath, bool send = true);
237 void setArmorGroups(const ItemGroupList &armor_groups);
238 ItemGroupList getArmorGroups();
239 void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
240 void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
241 void setBonePosition(const std::string &bone, v3f position, v3f rotation);
242 void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
243 void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
244 void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
245 void addAttachmentChild(int child_id);
246 void removeAttachmentChild(int child_id);
247 UNORDERED_SET<int> getAttachmentChildIds();
248 ObjectProperties* accessObjectProperties();
249 void notifyObjectPropertiesModified();
255 Inventory* getInventory();
256 const Inventory* getInventory() const;
257 InventoryLocation getInventoryLocation() const;
258 std::string getWieldList() const;
259 ItemStack getWieldedItem() const;
260 bool setWieldedItem(const ItemStack &item);
261 int getWieldIndex() const;
262 void setWieldIndex(int i);
270 RemotePlayer *getPlayer() { return m_player; }
271 u16 getPeerID() const { return m_peer_id; }
275 v3f getLastGoodPosition() const
277 return m_last_good_position;
279 float resetTimeFromLastPunch()
281 float r = m_time_from_last_punch;
282 m_time_from_last_punch = 0.0;
285 void noCheatDigStart(v3s16 p)
287 m_nocheat_dig_pos = p;
288 m_nocheat_dig_time = 0;
290 v3s16 getNoCheatDigPos()
292 return m_nocheat_dig_pos;
294 float getNoCheatDigTime()
296 return m_nocheat_dig_time;
300 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
302 LagPool& getDigPool()
306 // Returns true if cheated
307 bool checkMovementCheat();
311 void updatePrivileges(const std::set<std::string> &privs,
312 bool is_singleplayer)
315 m_is_singleplayer = is_singleplayer;
318 bool getCollisionBox(aabb3f *toset);
319 bool collideWithObjects();
321 void initialize(RemotePlayer *player, const std::set<std::string> &privs);
323 v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
324 v3f getEyeOffset() const { return v3f(0, BS * 1.625f, 0); }
327 std::string getPropertyPacket();
328 void unlinkPlayerSessionAndSave();
330 RemotePlayer *m_player;
332 Inventory *m_inventory;
338 v3f m_last_good_position;
339 float m_time_from_last_punch;
340 v3s16 m_nocheat_dig_pos;
341 float m_nocheat_dig_time;
344 IntervalLimiter m_breathing_interval;
345 IntervalLimiter m_drowning_interval;
348 bool m_position_not_sent;
349 ItemGroupList m_armor_groups;
350 bool m_armor_groups_sent;
352 bool m_properties_sent;
353 struct ObjectProperties m_prop;
354 // Cached privileges for enforcement
355 std::set<std::string> m_privs;
356 bool m_is_singleplayer;
358 v2f m_animation_range;
359 float m_animation_speed;
360 float m_animation_blend;
361 bool m_animation_loop;
362 bool m_animation_sent;
364 // Stores position and rotation for each bone name
365 UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
366 bool m_bone_position_sent;
368 int m_attachment_parent_id;
369 UNORDERED_SET<int> m_attachment_child_ids;
370 std::string m_attachment_bone;
371 v3f m_attachment_position;
372 v3f m_attachment_rotation;
373 bool m_attachment_sent;
379 float m_physics_override_speed;
380 float m_physics_override_jump;
381 float m_physics_override_gravity;
382 bool m_physics_override_sneak;
383 bool m_physics_override_sneak_glitch;
384 bool m_physics_override_sent;