1604.00
[voxelands.git] / src / serverobject.h
blob0834e3a4a4b4c427d5ccfad709c06d968504111c
1 /************************************************************************
2 * Minetest-c55
3 * Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 * serverobject.h
6 * voxelands - 3d voxel world sandbox game
7 * Copyright (C) Lisa 'darkrose' Milne 2013-2014 <lisa@ltmnet.com>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 * See the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>
22 * License updated from GPLv2 or later to GPLv3 or later by Lisa Milne
23 * for Voxelands.
24 ************************************************************************/
26 #ifndef SERVEROBJECT_HEADER
27 #define SERVEROBJECT_HEADER
29 #include "common_irrlicht.h"
30 #include "activeobject.h"
31 #include "mapnode.h"
32 #include "utility.h"
34 #define MOB_NONE 0
35 #define MOB_PASSIVE 1
36 #define MOB_AGGRESSIVE 2
37 #define MOB_DESTRUCTIVE 3
41 Some planning
42 -------------
44 * Server environment adds an active object, which gets the id 1
46 * The active object list is scanned for each client once in a while,
47 * and it finds out what objects have been added that are not known
48 * by the client yet. This scan is initiated by the Server class and
49 * the result ends up directly to the server.
51 * A network packet is created with the info and sent to the client.
52 * Environment converts objects to static data and static data to
53 * objects, based on how close players are to them.
56 class ServerEnvironment;
57 class InventoryItem;
58 class Player;
60 class ServerActiveObject : public ActiveObject
62 public:
64 NOTE: m_env can be NULL, but step() isn't called if it is.
65 Prototypes are used that way.
67 ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos);
68 virtual ~ServerActiveObject();
70 // Create a certain type of ServerActiveObject
71 static ServerActiveObject* create(u8 type,
72 ServerEnvironment *env, u16 id, v3f pos,
73 const std::string &data);
76 Some simple getters/setters
78 v3f getBasePosition()
79 {return m_base_position;}
80 void setBasePosition(v3f pos)
81 {m_base_position = pos;}
82 ServerEnvironment* getEnv()
83 {return m_env;}
86 Step object in time.
87 Messages added to messages are sent to client over network.
89 send_recommended:
90 True at around 5-10 times a second, same for all objects.
91 This is used to let objects send most of the data at the
92 same time so that the data can be combined in a single
93 packet.
95 virtual void step(float dtime, bool send_recommended){}
98 The return value of this is passed to the client-side object
99 when it is created
101 virtual std::string getClientInitializationData(){return "";}
104 The return value of this is passed to the server-side object
105 when it is created (converted from static to active - actually
106 the data is the static form)
108 virtual std::string getStaticData(){return "";}
111 Item that the player gets when this object is picked up.
112 If NULL, object cannot be picked up.
114 virtual InventoryItem* createPickedUpItem(content_t punch_item) {return NULL;}
117 If the object doesn't return an item, this will be called.
118 Return value is tool wear.
120 virtual u16 punch(content_t punch_item, v3f dir, const std::string &playername) {return 0;}
123 return true if inventory is modified
125 virtual bool rightClick(Player *player) {return false;}
127 virtual u8 level(){return MOB_PASSIVE;}
130 Number of players which know about this object. Object won't be
131 deleted until this is 0 to keep the id preserved for the right
132 object.
134 u16 m_known_by_count;
137 - Whether this object is to be removed when nobody knows about
138 it anymore.
139 - Removal is delayed to preserve the id for the time during which
140 it could be confused to some other object by some client.
141 - This is set to true by the step() method when the object wants
142 to be deleted.
143 - This can be set to true by anything else too.
145 bool m_removed;
148 This is set to true when a block should be removed from the active
149 object list but couldn't be removed because the id has to be
150 reserved for some client.
152 The environment checks this periodically. If this is true and also
153 m_known_by_count is true,
155 bool m_pending_deactivation;
158 Whether the object's static data has been stored to a block
160 bool m_static_exists;
162 The block from which the object was loaded from, and in which
163 a copy of the static data resides.
165 v3s16 m_static_block;
168 Queue of messages to be sent to the client
170 Queue<ActiveObjectMessage> m_messages_out;
172 protected:
173 // Used for creating objects based on type
174 typedef ServerActiveObject* (*Factory)
175 (ServerEnvironment *env, u16 id, v3f pos,
176 const std::string &data);
177 static void registerType(u16 type, Factory f);
179 ServerEnvironment *m_env;
180 v3f m_base_position;
182 private:
183 // Used for creating objects based on type
184 static core::map<u16, Factory> m_types;
187 #endif