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