Allow damage for attached objects, add attach/detach callbacks (#6786)
[oweals/minetest.git] / src / serverobject.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #pragma once
21
22 #include <unordered_set>
23 #include "irrlichttypes_bloated.h"
24 #include "activeobject.h"
25 #include "inventorymanager.h"
26 #include "itemgroup.h"
27 #include "util/container.h"
28
29 /*
30
31 Some planning
32 -------------
33
34 * Server environment adds an active object, which gets the id 1
35 * The active object list is scanned for each client once in a while,
36   and it finds out what objects have been added that are not known
37   by the client yet. This scan is initiated by the Server class and
38   the result ends up directly to the server.
39 * A network packet is created with the info and sent to the client.
40 * Environment converts objects to static data and static data to
41   objects, based on how close players are to them.
42
43 */
44
45 class ServerEnvironment;
46 struct ItemStack;
47 struct ToolCapabilities;
48 struct ObjectProperties;
49 struct PlayerHPChangeReason;
50
51 class ServerActiveObject : public ActiveObject
52 {
53 public:
54         /*
55                 NOTE: m_env can be NULL, but step() isn't called if it is.
56                 Prototypes are used that way.
57         */
58         ServerActiveObject(ServerEnvironment *env, v3f pos);
59         virtual ~ServerActiveObject() = default;
60
61         virtual ActiveObjectType getSendType() const
62         { return getType(); }
63
64         // Called after id has been set and has been inserted in environment
65         virtual void addedToEnvironment(u32 dtime_s){};
66         // Called before removing from environment
67         virtual void removingFromEnvironment(){};
68         // Returns true if object's deletion is the job of the
69         // environment
70         virtual bool environmentDeletes() const
71         { return true; }
72
73         // Create a certain type of ServerActiveObject
74         static ServerActiveObject* create(ActiveObjectType type,
75                         ServerEnvironment *env, u16 id, v3f pos,
76                         const std::string &data);
77
78         /*
79                 Some simple getters/setters
80         */
81         v3f getBasePosition() const { return m_base_position; }
82         void setBasePosition(v3f pos){ m_base_position = pos; }
83         ServerEnvironment* getEnv(){ return m_env; }
84
85         /*
86                 Some more dynamic interface
87         */
88
89         virtual void setPos(const v3f &pos)
90                 { setBasePosition(pos); }
91         // continuous: if true, object does not stop immediately at pos
92         virtual void moveTo(v3f pos, bool continuous)
93                 { setBasePosition(pos); }
94         // If object has moved less than this and data has not changed,
95         // saving to disk may be omitted
96         virtual float getMinimumSavedMovement();
97
98         virtual std::string getDescription(){return "SAO";}
99
100         /*
101                 Step object in time.
102                 Messages added to messages are sent to client over network.
103
104                 send_recommended:
105                         True at around 5-10 times a second, same for all objects.
106                         This is used to let objects send most of the data at the
107                         same time so that the data can be combined in a single
108                         packet.
109         */
110         virtual void step(float dtime, bool send_recommended){}
111
112         /*
113                 The return value of this is passed to the client-side object
114                 when it is created
115         */
116         virtual std::string getClientInitializationData(u16 protocol_version){return "";}
117
118         /*
119                 The return value of this is passed to the server-side object
120                 when it is created (converted from static to active - actually
121                 the data is the static form)
122         */
123         virtual void getStaticData(std::string *result) const
124         {
125                 assert(isStaticAllowed());
126                 *result = "";
127         }
128         /*
129                 Return false in here to never save and instead remove object
130                 on unload. getStaticData() will not be called in that case.
131         */
132         virtual bool isStaticAllowed() const
133         {return true;}
134
135         // Returns tool wear
136         virtual int punch(v3f dir,
137                         const ToolCapabilities *toolcap=NULL,
138                         ServerActiveObject *puncher=NULL,
139                         float time_from_last_punch=1000000)
140         { return 0; }
141         virtual void rightClick(ServerActiveObject *clicker)
142         {}
143         virtual void setHP(s16 hp, const PlayerHPChangeReason &reason)
144         {}
145         virtual s16 getHP() const
146         { return 0; }
147
148         virtual void setArmorGroups(const ItemGroupList &armor_groups)
149         {}
150         virtual const ItemGroupList &getArmorGroups()
151         { static const ItemGroupList rv; return rv; }
152         virtual void setPhysicsOverride(float physics_override_speed, float physics_override_jump, float physics_override_gravity)
153         {}
154         virtual void setAnimation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
155         {}
156         virtual void getAnimation(v2f *frames, float *frame_speed, float *frame_blend, bool *frame_loop)
157         {}
158         virtual void setAnimationSpeed(float frame_speed)
159         {}
160         virtual void setBonePosition(const std::string &bone, v3f position, v3f rotation)
161         {}
162         virtual void getBonePosition(const std::string &bone, v3f *position, v3f *lotation)
163         {}
164         virtual void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation)
165         {}
166         virtual void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation)
167         {}
168         virtual void clearChildAttachments() {}
169         virtual void clearParentAttachment() {}
170         virtual void addAttachmentChild(int child_id)
171         {}
172         virtual void removeAttachmentChild(int child_id)
173         {}
174         virtual const std::unordered_set<int> &getAttachmentChildIds()
175         { static const std::unordered_set<int> rv; return rv; }
176         virtual ObjectProperties* accessObjectProperties()
177         { return NULL; }
178         virtual void notifyObjectPropertiesModified()
179         {}
180
181         // Inventory and wielded item
182         virtual Inventory* getInventory()
183         { return NULL; }
184         virtual const Inventory* getInventory() const
185         { return NULL; }
186         virtual InventoryLocation getInventoryLocation() const
187         { return InventoryLocation(); }
188         virtual void setInventoryModified()
189         {}
190         virtual std::string getWieldList() const
191         { return ""; }
192         virtual int getWieldIndex() const
193         { return 0; }
194         virtual ItemStack getWieldedItem() const;
195         virtual bool setWieldedItem(const ItemStack &item);
196         inline void attachParticleSpawner(u32 id)
197         {
198                 m_attached_particle_spawners.insert(id);
199         }
200         inline void detachParticleSpawner(u32 id)
201         {
202                 m_attached_particle_spawners.erase(id);
203         }
204
205
206         /*
207                 Number of players which know about this object. Object won't be
208                 deleted until this is 0 to keep the id preserved for the right
209                 object.
210         */
211         u16 m_known_by_count = 0;
212
213         /*
214                 - Whether this object is to be removed when nobody knows about
215                   it anymore.
216                 - Removal is delayed to preserve the id for the time during which
217                   it could be confused to some other object by some client.
218                 - This is usually set to true by the step() method when the object wants
219                   to be deleted but can be set by anything else too.
220         */
221         bool m_pending_removal = false;
222
223         /*
224                 Same purpose as m_pending_removal but for deactivation.
225                 deactvation = save static data in block, remove active object
226
227                 If this is set alongside with m_pending_removal, removal takes
228                 priority.
229         */
230         bool m_pending_deactivation = false;
231
232         /*
233                 A getter that unifies the above to answer the question:
234                 "Can the environment still interact with this object?"
235         */
236         inline bool isGone() const
237         { return m_pending_removal || m_pending_deactivation; }
238
239         /*
240                 Whether the object's static data has been stored to a block
241         */
242         bool m_static_exists = false;
243         /*
244                 The block from which the object was loaded from, and in which
245                 a copy of the static data resides.
246         */
247         v3s16 m_static_block = v3s16(1337,1337,1337);
248
249         /*
250                 Queue of messages to be sent to the client
251         */
252         std::queue<ActiveObjectMessage> m_messages_out;
253
254 protected:
255         virtual void onAttach(int parent_id) {}
256         virtual void onDetach(int parent_id) {}
257
258         // Used for creating objects based on type
259         typedef ServerActiveObject* (*Factory)
260                         (ServerEnvironment *env, v3f pos,
261                         const std::string &data);
262         static void registerType(u16 type, Factory f);
263
264         ServerEnvironment *m_env;
265         v3f m_base_position;
266         std::unordered_set<u32> m_attached_particle_spawners;
267
268 private:
269         // Used for creating objects based on type
270         static std::map<u16, Factory> m_types;
271 };