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 getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
53 void addAttachmentChild(int child_id);
54 void removeAttachmentChild(int child_id);
55 const UNORDERED_SET<int> &getAttachmentChildIds();
56 ObjectProperties* accessObjectProperties();
57 void notifyObjectPropertiesModified();
62 bool m_properties_sent;
63 struct ObjectProperties m_prop;
65 ItemGroupList m_armor_groups;
66 bool m_armor_groups_sent;
68 v2f m_animation_range;
69 float m_animation_speed;
70 float m_animation_blend;
71 bool m_animation_loop;
72 bool m_animation_sent;
74 // Stores position and rotation for each bone name
75 UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
76 bool m_bone_position_sent;
78 int m_attachment_parent_id;
79 UNORDERED_SET<int> m_attachment_child_ids;
80 std::string m_attachment_bone;
81 v3f m_attachment_position;
82 v3f m_attachment_rotation;
83 bool m_attachment_sent;
87 LuaEntitySAO needs some internals exposed.
90 class LuaEntitySAO : public UnitSAO
93 LuaEntitySAO(ServerEnvironment *env, v3f pos,
94 const std::string &name, const std::string &state);
96 ActiveObjectType getType() const
97 { return ACTIVEOBJECT_TYPE_LUAENTITY; }
98 ActiveObjectType getSendType() const
99 { return ACTIVEOBJECT_TYPE_GENERIC; }
100 virtual void addedToEnvironment(u32 dtime_s);
101 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
102 const std::string &data);
103 void step(float dtime, bool send_recommended);
104 std::string getClientInitializationData(u16 protocol_version);
105 void getStaticData(std::string *result) const;
107 const ToolCapabilities *toolcap=NULL,
108 ServerActiveObject *puncher=NULL,
109 float time_from_last_punch=1000000);
110 void rightClick(ServerActiveObject *clicker);
111 void setPos(const v3f &pos);
112 void moveTo(v3f pos, bool continuous);
113 float getMinimumSavedMovement();
114 std::string getDescription();
117 /* LuaEntitySAO-specific */
118 void setVelocity(v3f velocity);
120 void setAcceleration(v3f acceleration);
121 v3f getAcceleration();
123 void setTextureMod(const std::string &mod);
124 std::string getTextureMod() const;
125 void setSprite(v2s16 p, int num_frames, float framelength,
126 bool select_horiz_by_yawpitch);
127 std::string getName();
128 bool getCollisionBox(aabb3f *toset) const;
129 bool collideWithObjects() const;
131 std::string getPropertyPacket();
132 void sendPosition(bool do_interpolate, bool is_movement_end);
134 std::string m_init_name;
135 std::string m_init_state;
141 float m_last_sent_yaw;
142 v3f m_last_sent_position;
143 v3f m_last_sent_velocity;
144 float m_last_sent_position_timer;
145 float m_last_sent_move_precision;
146 std::string m_current_texture_modifier;
150 PlayerSAO needs some internals exposed.
158 LagPool(): m_pool(15), m_max(15)
160 void setMax(float new_max)
166 void add(float dtime)
172 bool grab(float dtime)
176 if(m_pool + dtime > m_max)
185 class PlayerSAO : public UnitSAO
188 PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer);
190 ActiveObjectType getType() const
191 { return ACTIVEOBJECT_TYPE_PLAYER; }
192 ActiveObjectType getSendType() const
193 { return ACTIVEOBJECT_TYPE_GENERIC; }
194 std::string getDescription();
197 Active object <-> environment interface
200 void addedToEnvironment(u32 dtime_s);
201 void removingFromEnvironment();
202 bool isStaticAllowed() const { return false; }
203 std::string getClientInitializationData(u16 protocol_version);
204 void getStaticData(std::string *result) const;
205 void step(float dtime, bool send_recommended);
206 void setBasePosition(const v3f &position);
207 void setPos(const v3f &pos);
208 void moveTo(v3f pos, bool continuous);
209 void setYaw(const float yaw);
210 // Data should not be sent at player initialization
211 void setYawAndSend(const float yaw);
212 void setPitch(const float pitch);
213 // Data should not be sent at player initialization
214 void setPitchAndSend(const float pitch);
215 f32 getPitch() const { return m_pitch; }
216 f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
218 f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
219 void setFov(const float pitch);
220 f32 getFov() const { return m_fov; }
221 void setWantedRange(const s16 range);
222 s16 getWantedRange() const { return m_wanted_range; }
225 Interaction interface
229 const ToolCapabilities *toolcap,
230 ServerActiveObject *puncher,
231 float time_from_last_punch);
232 void rightClick(ServerActiveObject *clicker) {}
234 void setHPRaw(s16 hp) { m_hp = hp; }
236 u16 getBreath() const { return m_breath; }
237 void setBreath(const u16 breath, bool send = true);
243 Inventory* getInventory();
244 const Inventory* getInventory() const;
245 InventoryLocation getInventoryLocation() const;
246 std::string getWieldList() const;
247 ItemStack getWieldedItem() const;
248 bool setWieldedItem(const ItemStack &item);
249 int getWieldIndex() const;
250 void setWieldIndex(int i);
258 RemotePlayer *getPlayer() { return m_player; }
259 u16 getPeerID() const { return m_peer_id; }
263 v3f getLastGoodPosition() const
265 return m_last_good_position;
267 float resetTimeFromLastPunch()
269 float r = m_time_from_last_punch;
270 m_time_from_last_punch = 0.0;
273 void noCheatDigStart(v3s16 p)
275 m_nocheat_dig_pos = p;
276 m_nocheat_dig_time = 0;
278 v3s16 getNoCheatDigPos()
280 return m_nocheat_dig_pos;
282 float getNoCheatDigTime()
284 return m_nocheat_dig_time;
288 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
290 LagPool& getDigPool()
294 // Returns true if cheated
295 bool checkMovementCheat();
299 void updatePrivileges(const std::set<std::string> &privs,
300 bool is_singleplayer)
303 m_is_singleplayer = is_singleplayer;
306 bool getCollisionBox(aabb3f *toset) const;
307 bool collideWithObjects() const { return true; }
309 void initialize(RemotePlayer *player, const std::set<std::string> &privs);
311 v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
312 v3f getEyeOffset() const;
315 std::string getPropertyPacket();
316 void unlinkPlayerSessionAndSave();
318 RemotePlayer *m_player;
320 Inventory *m_inventory;
326 v3f m_last_good_position;
327 float m_time_from_last_punch;
328 v3s16 m_nocheat_dig_pos;
329 float m_nocheat_dig_time;
332 IntervalLimiter m_breathing_interval;
333 IntervalLimiter m_drowning_interval;
336 bool m_position_not_sent;
338 // Cached privileges for enforcement
339 std::set<std::string> m_privs;
340 bool m_is_singleplayer;
347 float m_physics_override_speed;
348 float m_physics_override_jump;
349 float m_physics_override_gravity;
350 bool m_physics_override_sneak;
351 bool m_physics_override_sneak_glitch;
352 bool m_physics_override_sent;