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"
28 class UnitSAO: public ServerActiveObject
31 UnitSAO(ServerEnvironment *env, v3f pos);
34 virtual void setYaw(const float yaw) { m_yaw = yaw; }
35 float getYaw() const { return m_yaw; };
36 f32 getRadYaw() const { return m_yaw * core::DEGTORAD; }
38 f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; }
40 s16 getHP() const { return m_hp; }
41 // Use a function, if isDead can be defined by other conditions
42 bool isDead() const { return m_hp == 0; }
44 bool isAttached() const;
45 void setArmorGroups(const ItemGroupList &armor_groups);
46 const ItemGroupList &getArmorGroups();
47 void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
48 void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
49 void setBonePosition(const std::string &bone, v3f position, v3f rotation);
50 void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
51 void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
52 void detachFromParent();
53 void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
54 void addAttachmentChild(int child_id);
55 void removeAttachmentChild(int child_id);
56 const UNORDERED_SET<int> &getAttachmentChildIds();
57 ObjectProperties* accessObjectProperties();
58 void notifyObjectPropertiesModified();
63 bool m_properties_sent;
64 struct ObjectProperties m_prop;
66 ItemGroupList m_armor_groups;
67 bool m_armor_groups_sent;
69 v2f m_animation_range;
70 float m_animation_speed;
71 float m_animation_blend;
72 bool m_animation_loop;
73 bool m_animation_sent;
75 // Stores position and rotation for each bone name
76 UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
77 bool m_bone_position_sent;
79 int m_attachment_parent_id;
80 UNORDERED_SET<int> m_attachment_child_ids;
81 std::string m_attachment_bone;
82 v3f m_attachment_position;
83 v3f m_attachment_rotation;
84 bool m_attachment_sent;
88 LuaEntitySAO needs some internals exposed.
91 class LuaEntitySAO : public UnitSAO
94 LuaEntitySAO(ServerEnvironment *env, v3f pos,
95 const std::string &name, const std::string &state);
97 ActiveObjectType getType() const
98 { return ACTIVEOBJECT_TYPE_LUAENTITY; }
99 ActiveObjectType getSendType() const
100 { return ACTIVEOBJECT_TYPE_GENERIC; }
101 virtual void addedToEnvironment(u32 dtime_s);
102 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
103 const std::string &data);
104 void step(float dtime, bool send_recommended);
105 std::string getClientInitializationData(u16 protocol_version);
106 void getStaticData(std::string *result) const;
108 const ToolCapabilities *toolcap=NULL,
109 ServerActiveObject *puncher=NULL,
110 float time_from_last_punch=1000000);
111 void rightClick(ServerActiveObject *clicker);
112 void setPos(const v3f &pos);
113 void moveTo(v3f pos, bool continuous);
114 float getMinimumSavedMovement();
115 std::string getDescription();
118 /* LuaEntitySAO-specific */
119 void setVelocity(v3f velocity);
121 void setAcceleration(v3f acceleration);
122 v3f getAcceleration();
124 void setTextureMod(const std::string &mod);
125 std::string getTextureMod() const;
126 void setSprite(v2s16 p, int num_frames, float framelength,
127 bool select_horiz_by_yawpitch);
128 std::string getName();
129 bool getCollisionBox(aabb3f *toset) const;
130 bool collideWithObjects() const;
132 std::string getPropertyPacket();
133 void sendPosition(bool do_interpolate, bool is_movement_end);
135 std::string m_init_name;
136 std::string m_init_state;
142 float m_last_sent_yaw;
143 v3f m_last_sent_position;
144 v3f m_last_sent_velocity;
145 float m_last_sent_position_timer;
146 float m_last_sent_move_precision;
147 std::string m_current_texture_modifier;
151 PlayerSAO needs some internals exposed.
159 LagPool(): m_pool(15), m_max(15)
161 void setMax(float new_max)
167 void add(float dtime)
173 bool grab(float dtime)
177 if(m_pool + dtime > m_max)
186 class PlayerSAO : public UnitSAO
189 PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer);
191 ActiveObjectType getType() const
192 { return ACTIVEOBJECT_TYPE_PLAYER; }
193 ActiveObjectType getSendType() const
194 { return ACTIVEOBJECT_TYPE_GENERIC; }
195 std::string getDescription();
198 Active object <-> environment interface
201 void addedToEnvironment(u32 dtime_s);
202 void removingFromEnvironment();
203 bool isStaticAllowed() const { return false; }
204 std::string getClientInitializationData(u16 protocol_version);
205 void getStaticData(std::string *result) const;
206 void step(float dtime, bool send_recommended);
207 void setBasePosition(const v3f &position);
208 void setPos(const v3f &pos);
209 void moveTo(v3f pos, bool continuous);
210 void setYaw(const float yaw);
211 // Data should not be sent at player initialization
212 void setYawAndSend(const float yaw);
213 void setPitch(const float pitch);
214 // Data should not be sent at player initialization
215 void setPitchAndSend(const float pitch);
216 f32 getPitch() const { return m_pitch; }
217 f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
219 f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
220 void setFov(const float pitch);
221 f32 getFov() const { return m_fov; }
222 void setWantedRange(const s16 range);
223 s16 getWantedRange() const { return m_wanted_range; }
226 Interaction interface
230 const ToolCapabilities *toolcap,
231 ServerActiveObject *puncher,
232 float time_from_last_punch);
233 void rightClick(ServerActiveObject *clicker) {}
235 void setHPRaw(s16 hp) { m_hp = hp; }
237 u16 getBreath() const { return m_breath; }
238 void setBreath(const u16 breath, bool send = true);
244 Inventory* getInventory();
245 const Inventory* getInventory() const;
246 InventoryLocation getInventoryLocation() const;
247 std::string getWieldList() const;
248 ItemStack getWieldedItem() const;
249 bool setWieldedItem(const ItemStack &item);
250 int getWieldIndex() const;
251 void setWieldIndex(int i);
259 RemotePlayer *getPlayer() { return m_player; }
260 u16 getPeerID() const { return m_peer_id; }
264 v3f getLastGoodPosition() const
266 return m_last_good_position;
268 float resetTimeFromLastPunch()
270 float r = m_time_from_last_punch;
271 m_time_from_last_punch = 0.0;
274 void noCheatDigStart(v3s16 p)
276 m_nocheat_dig_pos = p;
277 m_nocheat_dig_time = 0;
279 v3s16 getNoCheatDigPos()
281 return m_nocheat_dig_pos;
283 float getNoCheatDigTime()
285 return m_nocheat_dig_time;
289 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
291 LagPool& getDigPool()
295 // Returns true if cheated
296 bool checkMovementCheat();
300 void updatePrivileges(const std::set<std::string> &privs,
301 bool is_singleplayer)
304 m_is_singleplayer = is_singleplayer;
307 bool getCollisionBox(aabb3f *toset) const;
308 bool collideWithObjects() const { return true; }
310 void initialize(RemotePlayer *player, const std::set<std::string> &privs);
312 v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
313 v3f getEyeOffset() const;
316 std::string getPropertyPacket();
317 void unlinkPlayerSessionAndSave();
319 RemotePlayer *m_player;
321 Inventory *m_inventory;
327 v3f m_last_good_position;
328 float m_time_from_last_punch;
329 v3s16 m_nocheat_dig_pos;
330 float m_nocheat_dig_time;
333 IntervalLimiter m_breathing_interval;
334 IntervalLimiter m_drowning_interval;
337 bool m_position_not_sent;
339 // Cached privileges for enforcement
340 std::set<std::string> m_privs;
341 bool m_is_singleplayer;
348 float m_physics_override_speed;
349 float m_physics_override_jump;
350 float m_physics_override_gravity;
351 bool m_physics_override_sneak;
352 bool m_physics_override_sneak_glitch;
353 bool m_physics_override_sent;