Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
serveractiveobject.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <cassert>
8#include <unordered_set>
9#include <optional>
10#include <queue>
12#include "activeobject.h"
13#include "itemgroup.h"
14
15
16/*
17
18Some planning
19-------------
20
21* Server environment adds an active object, which gets the id 1
22* The active object list is scanned for each client once in a while,
23 and it finds out what objects have been added that are not known
24 by the client yet. This scan is initiated by the Server class and
25 the result ends up directly to the server.
26* A network packet is created with the info and sent to the client.
27* Environment converts objects to static data and static data to
28 objects, based on how close players are to them.
29
30*/
31
33struct ItemStack;
34struct ToolCapabilities;
35struct ObjectProperties;
37class Inventory;
39
41{
42public:
43 /*
44 NOTE: m_env can be NULL, but step() isn't called if it is.
45 Prototypes are used that way.
46 */
48 virtual ~ServerActiveObject() = default;
49
51 { return getType(); }
52
53 // Called after id has been set and has been inserted in environment
54 virtual void addedToEnvironment(u32 dtime_s){};
55 // Called before removing from environment
56 virtual void removingFromEnvironment(){};
57
58 // Safely mark the object for removal or deactivation
59 void markForRemoval();
61
62 /*
63 Some simple getters/setters
64 */
66 void setBasePosition(v3f pos);
68
69 /*
70 Some more dynamic interface
71 */
72
73 virtual void setPos(const v3f &pos)
74 { setBasePosition(pos); }
75 virtual void addPos(const v3f &added_pos)
76 { setBasePosition(m_base_position + added_pos); }
77 // continuous: if true, object does not stop immediately at pos
78 virtual void moveTo(v3f pos, bool continuous)
79 { setBasePosition(pos); }
80 // If object has moved less than this and data has not changed,
81 // saving to disk may be omitted
82 virtual float getMinimumSavedMovement();
83
84 virtual std::string getDescription(){return "SAO";}
85
86 /*
87 Step object in time.
88 Messages added to messages are sent to client over network.
89
90 send_recommended:
91 True at around 5-10 times a second, same for all objects.
92 This is used to let objects send most of the data at the
93 same time so that the data can be combined in a single
94 packet.
95 */
96 virtual void step(float dtime, bool send_recommended){}
97
98 /*
99 The return value of this is passed to the client-side object
100 when it is created
101 */
102 virtual std::string getClientInitializationData(u16 protocol_version) {return "";}
103
104 /*
105 The return value of this is passed to the server-side object
106 when it is created (converted from static to active - actually
107 the data is the static form)
108 */
109 virtual void getStaticData(std::string *result) const
110 {
111 assert(isStaticAllowed());
112 *result = "";
113 }
114
115 /*
116 Return false in here to never save and instead remove object
117 on unload. getStaticData() will not be called in that case.
118 */
119 virtual bool isStaticAllowed() const
120 {return true;}
121
122 /*
123 Return false here to never unload the object.
124 isStaticAllowed && shouldUnload -> unload when out of active block range
125 !isStaticAllowed && shouldUnload -> unload when block is unloaded
126 */
127 virtual bool shouldUnload() const
128 { return true; }
129
130 // Returns added tool wear
131 virtual u32 punch(v3f dir,
132 const ToolCapabilities &toolcap,
133 ServerActiveObject *puncher = nullptr,
134 float time_from_last_punch = 1000000.0f,
135 u16 initial_wear = 0)
136 { return 0; }
137 virtual void rightClick(ServerActiveObject *clicker)
138 {}
139 virtual void setHP(s32 hp, const PlayerHPChangeReason &reason)
140 {}
141 virtual u16 getHP() const
142 { return 0; }
143
146 virtual std::string getGUID() const = 0;
147
148 virtual void setArmorGroups(const ItemGroupList &armor_groups)
149 {}
150 virtual const ItemGroupList &getArmorGroups() const
151 { static ItemGroupList rv; return rv; }
152 virtual void setAnimation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
153 {}
154 virtual void getAnimation(v2f *frames, float *frame_speed, float *frame_blend, bool *frame_loop)
155 {}
156 virtual void setAnimationSpeed(float frame_speed)
157 {}
158 virtual void setBoneOverride(const std::string &bone, const BoneOverride &props)
159 {}
160 virtual BoneOverride getBoneOverride(const std::string &bone)
161 { BoneOverride props; return props; }
162 virtual const BoneOverrideMap &getBoneOverrides() const
163 { static BoneOverrideMap rv; return rv; }
164 virtual const std::unordered_set<object_t> &getAttachmentChildIds() const
165 { static std::unordered_set<object_t> rv; return rv; }
166 virtual ServerActiveObject *getParent() const { return nullptr; }
168 { return NULL; }
170 {}
171
172 // Inventory and wielded item
173 virtual Inventory *getInventory() const
174 { return NULL; }
176 virtual void setInventoryModified()
177 {}
178 virtual std::string getWieldList() const
179 { return ""; }
180 virtual u16 getWieldIndex() const
181 { return 0; }
182 virtual ItemStack getWieldedItem(ItemStack *selected,
183 ItemStack *hand = nullptr) const;
184 virtual bool setWieldedItem(const ItemStack &item);
185 inline void attachParticleSpawner(u32 id)
186 {
188 }
189 inline void detachParticleSpawner(u32 id)
190 {
192 }
193
194 std::string generateUpdateInfantCommand(u16 infant_id, u16 protocol_version);
195
196 void dumpAOMessagesToQueue(std::queue<ActiveObjectMessage> &queue);
197
198 /*
199 Number of players which know about this object. Object won't be
200 deleted until this is 0 to keep the id preserved for the right
201 object.
202 */
204
205 /*
206 A getter that unifies the above to answer the question:
207 "Can the environment still interact with this object?"
208 */
209 inline bool isGone() const
211
212 inline bool isPendingRemoval() const
213 { return m_pending_removal; }
214
215 /*
216 Whether the object's static data has been stored to a block
217
218 Note that `!isStaticAllowed() && m_static_exists` is a valid state
219 (though it usually doesn't persist long) and you need to be careful
220 about handling it.
221 */
222 bool m_static_exists = false;
223 /*
224 The block from which the object was loaded from, and in which
225 a copy of the static data resides.
226 */
227 v3s16 m_static_block = v3s16(1337,1337,1337);
228
229 // Names of players to whom the object is to be sent, not considering parents.
230 using Observers = std::optional<std::unordered_set<std::string>>;
232
242 bool isEffectivelyObservedBy(const std::string &player_name);
243
244protected:
245 // Cached intersection of m_observers of this object and all its parents.
246 std::optional<Observers> m_effective_observers;
247
248 virtual void onMarkedForDeactivation() {}
249 virtual void onMarkedForRemoval() {}
250
252 std::unordered_set<u32> m_attached_particle_spawners;
253
254 /*
255 Same purpose as m_pending_removal but for deactivation.
256 deactvation = save static data in block, remove active object
257
258 If this is set alongside with m_pending_removal, removal takes
259 priority.
260 Note: Do not assign this directly, use markForDeactivation() instead.
261 */
263
264 /*
265 - Whether this object is to be removed when nobody knows about
266 it anymore.
267 - Removal is delayed to preserve the id for the time during which
268 it could be confused to some other object by some client.
269 - This is usually set to true by the step() method when the object wants
270 to be deleted but can be set by anything else too.
271 Note: Do not assign this directly, use markForRemoval() instead.
272 */
273 bool m_pending_removal = false;
274
275 /*
276 Queue of messages to be sent to the client
277 */
278 std::queue<ActiveObjectMessage> m_messages_out;
279
280private:
281 v3f m_base_position; // setBasePosition updates index and MUST be called
282};
std::unordered_map< std::string, BoneOverride > BoneOverrideMap
Definition activeobject.h:142
ActiveObjectType
Definition activeobject.h:14
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:207
Definition activeobject.h:149
virtual ActiveObjectType getType() const =0
Definition inventory.h:277
Definition serveractiveobject.h:41
v3f getBasePosition() const
Definition serveractiveobject.h:65
virtual void setAnimationSpeed(float frame_speed)
Definition serveractiveobject.h:156
virtual void addPos(const v3f &added_pos)
Definition serveractiveobject.h:75
virtual bool setWieldedItem(const ItemStack &item)
Definition serveractiveobject.cpp:44
virtual BoneOverride getBoneOverride(const std::string &bone)
Definition serveractiveobject.h:160
virtual Inventory * getInventory() const
Definition serveractiveobject.h:173
void markForDeactivation()
Definition serveractiveobject.cpp:82
virtual bool shouldUnload() const
Definition serveractiveobject.h:127
virtual void setBoneOverride(const std::string &bone, const BoneOverride &props)
Definition serveractiveobject.h:158
ServerEnvironment * m_env
Definition serveractiveobject.h:251
virtual void setPos(const v3f &pos)
Definition serveractiveobject.h:73
virtual void onMarkedForDeactivation()
Definition serveractiveobject.h:248
const Observers & recalculateEffectiveObservers()
Force a recalculation of final observers (including all parents).
Definition serveractiveobject.cpp:125
const Observers & getEffectiveObservers()
Cache m_effective_observers with the names of all observers, also indirect observers (object attachme...
Definition serveractiveobject.cpp:102
virtual std::string getClientInitializationData(u16 protocol_version)
Definition serveractiveobject.h:102
virtual std::string getGUID() const =0
Returns an unique ID for this object (persistent across unload, server restarts).
bool m_static_exists
Definition serveractiveobject.h:222
virtual ItemStack getWieldedItem(ItemStack *selected, ItemStack *hand=nullptr) const
Definition serveractiveobject.cpp:35
std::optional< Observers > m_effective_observers
Definition serveractiveobject.h:246
bool isEffectivelyObservedBy(const std::string &player_name)
Whether the object is sent to player_name
Definition serveractiveobject.cpp:134
virtual const BoneOverrideMap & getBoneOverrides() const
Definition serveractiveobject.h:162
virtual void getAnimation(v2f *frames, float *frame_speed, float *frame_blend, bool *frame_loop)
Definition serveractiveobject.h:154
std::unordered_set< u32 > m_attached_particle_spawners
Definition serveractiveobject.h:252
virtual void setInventoryModified()
Definition serveractiveobject.h:176
bool m_pending_deactivation
Definition serveractiveobject.h:262
std::optional< std::unordered_set< std::string > > Observers
Definition serveractiveobject.h:230
void markForRemoval()
Definition serveractiveobject.cpp:74
ServerActiveObject(ServerEnvironment *env, v3f pos)
Definition serveractiveobject.cpp:12
virtual void addedToEnvironment(u32 dtime_s)
Definition serveractiveobject.h:54
u16 m_known_by_count
Definition serveractiveobject.h:203
virtual ActiveObjectType getSendType() const
Definition serveractiveobject.h:50
void invalidateEffectiveObservers()
Invalidate final observer cache.
Definition serveractiveobject.cpp:95
void detachParticleSpawner(u32 id)
Definition serveractiveobject.h:189
virtual u16 getHP() const
Definition serveractiveobject.h:141
bool isPendingRemoval() const
Definition serveractiveobject.h:212
void attachParticleSpawner(u32 id)
Definition serveractiveobject.h:185
std::string generateUpdateInfantCommand(u16 infant_id, u16 protocol_version)
Definition serveractiveobject.cpp:49
virtual void setAnimation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
Definition serveractiveobject.h:152
virtual void setArmorGroups(const ItemGroupList &armor_groups)
Definition serveractiveobject.h:148
virtual const ItemGroupList & getArmorGroups() const
Definition serveractiveobject.h:150
virtual float getMinimumSavedMovement()
Definition serveractiveobject.cpp:30
void dumpAOMessagesToQueue(std::queue< ActiveObjectMessage > &queue)
Definition serveractiveobject.cpp:66
virtual ServerActiveObject * getParent() const
Definition serveractiveobject.h:166
ServerEnvironment * getEnv()
Definition serveractiveobject.h:67
std::queue< ActiveObjectMessage > m_messages_out
Definition serveractiveobject.h:278
virtual const std::unordered_set< object_t > & getAttachmentChildIds() const
Definition serveractiveobject.h:164
virtual std::string getWieldList() const
Definition serveractiveobject.h:178
virtual void notifyObjectPropertiesModified()
Definition serveractiveobject.h:169
virtual void getStaticData(std::string *result) const
Definition serveractiveobject.h:109
virtual void rightClick(ServerActiveObject *clicker)
Definition serveractiveobject.h:137
virtual u16 getWieldIndex() const
Definition serveractiveobject.h:180
virtual void removingFromEnvironment()
Definition serveractiveobject.h:56
virtual void step(float dtime, bool send_recommended)
Definition serveractiveobject.h:96
virtual std::string getDescription()
Definition serveractiveobject.h:84
virtual bool isStaticAllowed() const
Definition serveractiveobject.h:119
v3s16 m_static_block
Definition serveractiveobject.h:227
void setBasePosition(v3f pos)
Definition serveractiveobject.cpp:19
virtual void onMarkedForRemoval()
Definition serveractiveobject.h:249
virtual ObjectProperties * accessObjectProperties()
Definition serveractiveobject.h:167
virtual u32 punch(v3f dir, const ToolCapabilities &toolcap, ServerActiveObject *puncher=nullptr, float time_from_last_punch=1000000.0f, u16 initial_wear=0)
Definition serveractiveobject.h:131
virtual InventoryLocation getInventoryLocation() const
Definition serveractiveobject.cpp:90
Observers m_observers
Definition serveractiveobject.h:231
virtual ~ServerActiveObject()=default
virtual void moveTo(v3f pos, bool continuous)
Definition serveractiveobject.h:78
virtual void setHP(s32 hp, const PlayerHPChangeReason &reason)
Definition serveractiveobject.h:139
bool m_pending_removal
Definition serveractiveobject.h:273
bool isGone() const
Definition serveractiveobject.h:209
v3f m_base_position
Definition serveractiveobject.h:281
Definition serverenvironment.h:113
core::vector2d< f32 > v2f
Definition irr_v2d.h:11
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
core::vector3df v3f
Definition irr_v3d.h:11
std::unordered_map< std::string, int > ItemGroupList
Definition itemgroup.h:10
Definition activeobject.h:63
Definition inventorymanager.h:18
Definition inventory.h:21
Definition object_properties.h:32
Definition player_sao.h:235
Definition tool.h:53