Luanti 5.10.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>
11#include "activeobject.h"
12#include "itemgroup.h"
13#include "util/container.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 */
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 = nullptr,
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
144 virtual void setArmorGroups(const ItemGroupList &armor_groups)
145 {}
146 virtual const ItemGroupList &getArmorGroups() const
147 { static ItemGroupList rv; return rv; }
148 virtual void setAnimation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
149 {}
150 virtual void getAnimation(v2f *frames, float *frame_speed, float *frame_blend, bool *frame_loop)
151 {}
152 virtual void setAnimationSpeed(float frame_speed)
153 {}
154 virtual void setBoneOverride(const std::string &bone, const BoneOverride &props)
155 {}
156 virtual BoneOverride getBoneOverride(const std::string &bone)
157 { BoneOverride props; return props; }
158 virtual const BoneOverrideMap &getBoneOverrides() const
159 { static BoneOverrideMap rv; return rv; }
160 virtual const std::unordered_set<object_t> &getAttachmentChildIds() const
161 { static std::unordered_set<object_t> rv; return rv; }
162 virtual ServerActiveObject *getParent() const { return nullptr; }
164 { return NULL; }
166 {}
167
168 // Inventory and wielded item
169 virtual Inventory *getInventory() const
170 { return NULL; }
172 virtual void setInventoryModified()
173 {}
174 virtual std::string getWieldList() const
175 { return ""; }
176 virtual u16 getWieldIndex() const
177 { return 0; }
178 virtual ItemStack getWieldedItem(ItemStack *selected,
179 ItemStack *hand = nullptr) const;
180 virtual bool setWieldedItem(const ItemStack &item);
181 inline void attachParticleSpawner(u32 id)
182 {
184 }
185 inline void detachParticleSpawner(u32 id)
186 {
188 }
189
190 std::string generateUpdateInfantCommand(u16 infant_id, u16 protocol_version);
191
192 void dumpAOMessagesToQueue(std::queue<ActiveObjectMessage> &queue);
193
194 /*
195 Number of players which know about this object. Object won't be
196 deleted until this is 0 to keep the id preserved for the right
197 object.
198 */
200
201 /*
202 A getter that unifies the above to answer the question:
203 "Can the environment still interact with this object?"
204 */
205 inline bool isGone() const
207
208 inline bool isPendingRemoval() const
209 { return m_pending_removal; }
210
211 /*
212 Whether the object's static data has been stored to a block
213
214 Note that `!isStaticAllowed() && m_static_exists` is a valid state
215 (though it usually doesn't persist long) and you need to be careful
216 about handling it.
217 */
218 bool m_static_exists = false;
219 /*
220 The block from which the object was loaded from, and in which
221 a copy of the static data resides.
222 */
223 v3s16 m_static_block = v3s16(1337,1337,1337);
224
225 // Names of players to whom the object is to be sent, not considering parents.
226 using Observers = std::optional<std::unordered_set<std::string>>;
228
238 bool isEffectivelyObservedBy(const std::string &player_name);
239
240protected:
241 // Cached intersection of m_observers of this object and all its parents.
242 std::optional<Observers> m_effective_observers;
243
244 virtual void onMarkedForDeactivation() {}
245 virtual void onMarkedForRemoval() {}
246
249 std::unordered_set<u32> m_attached_particle_spawners;
250
251 /*
252 Same purpose as m_pending_removal but for deactivation.
253 deactvation = save static data in block, remove active object
254
255 If this is set alongside with m_pending_removal, removal takes
256 priority.
257 Note: Do not assign this directly, use markForDeactivation() instead.
258 */
260
261 /*
262 - Whether this object is to be removed when nobody knows about
263 it anymore.
264 - Removal is delayed to preserve the id for the time during which
265 it could be confused to some other object by some client.
266 - This is usually set to true by the step() method when the object wants
267 to be deleted but can be set by anything else too.
268 Note: Do not assign this directly, use markForRemoval() instead.
269 */
270 bool m_pending_removal = false;
271
272 /*
273 Queue of messages to be sent to the client
274 */
275 std::queue<ActiveObjectMessage> m_messages_out;
276};
std::unordered_map< std::string, BoneOverride > BoneOverrideMap
Definition activeobject.h:134
ActiveObjectType
Definition activeobject.h:14
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:191
Definition activeobject.h:141
virtual ActiveObjectType getType() const =0
Definition inventory.h:314
Definition serveractiveobject.h:41
v3f getBasePosition() const
Definition serveractiveobject.h:65
virtual void setAnimationSpeed(float frame_speed)
Definition serveractiveobject.h:152
virtual void addPos(const v3f &added_pos)
Definition serveractiveobject.h:75
virtual bool setWieldedItem(const ItemStack &item)
Definition serveractiveobject.cpp:31
virtual BoneOverride getBoneOverride(const std::string &bone)
Definition serveractiveobject.h:156
virtual Inventory * getInventory() const
Definition serveractiveobject.h:169
void markForDeactivation()
Definition serveractiveobject.cpp:69
virtual bool shouldUnload() const
Definition serveractiveobject.h:127
virtual void setBoneOverride(const std::string &bone, const BoneOverride &props)
Definition serveractiveobject.h:154
ServerEnvironment * m_env
Definition serveractiveobject.h:247
virtual void setPos(const v3f &pos)
Definition serveractiveobject.h:73
virtual void onMarkedForDeactivation()
Definition serveractiveobject.h:244
const Observers & recalculateEffectiveObservers()
Force a recalculation of final observers (including all parents).
Definition serveractiveobject.cpp:112
const Observers & getEffectiveObservers()
Cache m_effective_observers with the names of all observers, also indirect observers (object attachme...
Definition serveractiveobject.cpp:89
virtual u32 punch(v3f dir, const ToolCapabilities *toolcap=nullptr, ServerActiveObject *puncher=nullptr, float time_from_last_punch=1000000.0f, u16 initial_wear=0)
Definition serveractiveobject.h:131
virtual std::string getClientInitializationData(u16 protocol_version)
Definition serveractiveobject.h:102
bool m_static_exists
Definition serveractiveobject.h:218
virtual ItemStack getWieldedItem(ItemStack *selected, ItemStack *hand=nullptr) const
Definition serveractiveobject.cpp:22
std::optional< Observers > m_effective_observers
Definition serveractiveobject.h:242
bool isEffectivelyObservedBy(const std::string &player_name)
Whether the object is sent to player_name
Definition serveractiveobject.cpp:121
virtual const BoneOverrideMap & getBoneOverrides() const
Definition serveractiveobject.h:158
virtual void getAnimation(v2f *frames, float *frame_speed, float *frame_blend, bool *frame_loop)
Definition serveractiveobject.h:150
std::unordered_set< u32 > m_attached_particle_spawners
Definition serveractiveobject.h:249
virtual void setInventoryModified()
Definition serveractiveobject.h:172
bool m_pending_deactivation
Definition serveractiveobject.h:259
std::optional< std::unordered_set< std::string > > Observers
Definition serveractiveobject.h:226
void markForRemoval()
Definition serveractiveobject.cpp:61
ServerActiveObject(ServerEnvironment *env, v3f pos)
Definition serveractiveobject.cpp:10
virtual void addedToEnvironment(u32 dtime_s)
Definition serveractiveobject.h:54
u16 m_known_by_count
Definition serveractiveobject.h:199
virtual ActiveObjectType getSendType() const
Definition serveractiveobject.h:50
void invalidateEffectiveObservers()
Invalidate final observer cache.
Definition serveractiveobject.cpp:82
void detachParticleSpawner(u32 id)
Definition serveractiveobject.h:185
virtual u16 getHP() const
Definition serveractiveobject.h:141
bool isPendingRemoval() const
Definition serveractiveobject.h:208
void attachParticleSpawner(u32 id)
Definition serveractiveobject.h:181
std::string generateUpdateInfantCommand(u16 infant_id, u16 protocol_version)
Definition serveractiveobject.cpp:36
virtual void setAnimation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
Definition serveractiveobject.h:148
virtual void setArmorGroups(const ItemGroupList &armor_groups)
Definition serveractiveobject.h:144
virtual const ItemGroupList & getArmorGroups() const
Definition serveractiveobject.h:146
virtual float getMinimumSavedMovement()
Definition serveractiveobject.cpp:17
void dumpAOMessagesToQueue(std::queue< ActiveObjectMessage > &queue)
Definition serveractiveobject.cpp:53
virtual ServerActiveObject * getParent() const
Definition serveractiveobject.h:162
ServerEnvironment * getEnv()
Definition serveractiveobject.h:67
std::queue< ActiveObjectMessage > m_messages_out
Definition serveractiveobject.h:275
virtual const std::unordered_set< object_t > & getAttachmentChildIds() const
Definition serveractiveobject.h:160
virtual std::string getWieldList() const
Definition serveractiveobject.h:174
virtual void notifyObjectPropertiesModified()
Definition serveractiveobject.h:165
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:176
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:223
void setBasePosition(v3f pos)
Definition serveractiveobject.h:66
virtual void onMarkedForRemoval()
Definition serveractiveobject.h:245
virtual ObjectProperties * accessObjectProperties()
Definition serveractiveobject.h:163
virtual InventoryLocation getInventoryLocation() const
Definition serveractiveobject.cpp:77
Observers m_observers
Definition serveractiveobject.h:227
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:270
bool isGone() const
Definition serveractiveobject.h:205
v3f m_base_position
Definition serveractiveobject.h:248
Definition serverenvironment.h:219
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:19
Definition object_properties.h:15
Definition player_sao.h:227
Definition tool.h:46