]> git.lizzy.rs Git - dragonfireclient.git/blob - src/environment.h
hopefully fixed a bit more
[dragonfireclient.git] / src / environment.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 #ifndef ENVIRONMENT_HEADER
21 #define ENVIRONMENT_HEADER
22
23 /*
24         This class is the game's environment.
25         It contains:
26         - The map
27         - Players
28         - Other objects
29         - The current time in the game (actually it only contains the brightness)
30         - etc.
31 */
32
33 #include <list>
34 #include "common_irrlicht.h"
35 #include "player.h"
36 #include "map.h"
37 #include <ostream>
38 #include "utility.h"
39
40 class Environment
41 {
42 public:
43         // Environment will delete the map passed to the constructor
44         Environment();
45         virtual ~Environment();
46
47         /*
48                 Step everything in environment.
49                 - Move players
50                 - Step mobs
51                 - Run timers of map
52         */
53         virtual void step(f32 dtime) = 0;
54
55         virtual Map & getMap() = 0;
56
57         virtual void addPlayer(Player *player);
58         void removePlayer(u16 peer_id);
59         Player * getPlayer(u16 peer_id);
60         Player * getPlayer(const char *name);
61         Player * getRandomConnectedPlayer();
62         Player * getNearestConnectedPlayer(v3f pos);
63         core::list<Player*> getPlayers();
64         core::list<Player*> getPlayers(bool ignore_disconnected);
65         void printPlayers(std::ostream &o);
66         
67         void setDayNightRatio(u32 r);
68         u32 getDayNightRatio();
69
70 protected:
71         // peer_ids in here should be unique, except that there may be many 0s
72         core::list<Player*> m_players;
73         // Brightness
74         u32 m_daynight_ratio;
75 };
76
77 /*
78         The server-side environment.
79
80         This is not thread-safe. Server uses an environment mutex.
81 */
82
83 #include "serverobject.h"
84
85 class Server;
86
87 class ServerEnvironment : public Environment
88 {
89 public:
90         ServerEnvironment(ServerMap *map, Server *server);
91         ~ServerEnvironment();
92
93         Map & getMap()
94         {
95                 return *m_map;
96         }
97
98         ServerMap & getServerMap()
99         {
100                 return *m_map;
101         }
102
103         Server * getServer()
104         {
105                 return m_server;
106         }
107
108         void step(f32 dtime);
109
110         void serializePlayers(const std::string &savedir);
111         void deSerializePlayers(const std::string &savedir);
112
113         /*
114                 ActiveObjects
115         */
116
117         ServerActiveObject* getActiveObject(u16 id);
118
119         /*
120                 Adds an active object to the environment.
121                 Environment handles deletion of object.
122                 Object may be deleted by environment immediately.
123                 If id of object is 0, assigns a free id to it.
124                 Returns the id of the object.
125                 Returns 0 if not added and thus deleted.
126         */
127         u16 addActiveObject(ServerActiveObject *object);
128         
129         /*
130                 Finds out what new objects have been added to
131                 inside a radius around a position
132         */
133         void getAddedActiveObjects(v3s16 pos, s16 radius,
134                         core::map<u16, bool> &current_objects,
135                         core::map<u16, bool> &added_objects);
136
137         /*
138                 Finds out what new objects have been removed from
139                 inside a radius around a position
140         */
141         void getRemovedActiveObjects(v3s16 pos, s16 radius,
142                         core::map<u16, bool> &current_objects,
143                         core::map<u16, bool> &removed_objects);
144         
145         /*
146                 Gets the next message emitted by some active object.
147                 Returns a message with id=0 if no messages are available.
148         */
149         ActiveObjectMessage getActiveObjectMessage();
150         
151 private:
152         ServerMap *m_map;
153         Server *m_server;
154         core::map<u16, ServerActiveObject*> m_active_objects;
155         Queue<ActiveObjectMessage> m_active_object_messages;
156         float m_random_spawn_timer;
157         float m_send_recommended_timer;
158         IntervalLimiter m_object_management_interval;
159 };
160
161 #ifndef SERVER
162
163 #include "clientobject.h"
164
165 /*
166         The client-side environment.
167
168         This is not thread-safe.
169         Must be called from main (irrlicht) thread (uses the SceneManager)
170         Client uses an environment mutex.
171 */
172
173 class ClientEnvironment : public Environment
174 {
175 public:
176         ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
177         ~ClientEnvironment();
178
179         Map & getMap()
180         {
181                 return *m_map;
182         }
183
184         ClientMap & getClientMap()
185         {
186                 return *m_map;
187         }
188
189         void step(f32 dtime);
190
191         virtual void addPlayer(Player *player);
192         LocalPlayer * getLocalPlayer();
193
194         void updateMeshes(v3s16 blockpos);
195         void expireMeshes(bool only_daynight_diffed);
196
197         /*
198                 ActiveObjects
199         */
200         
201         ClientActiveObject* getActiveObject(u16 id);
202
203         /*
204                 Adds an active object to the environment.
205                 Environment handles deletion of object.
206                 Object may be deleted by environment immediately.
207                 If id of object is 0, assigns a free id to it.
208                 Returns the id of the object.
209                 Returns 0 if not added and thus deleted.
210         */
211         u16 addActiveObject(ClientActiveObject *object);
212
213         void addActiveObject(u16 id, u8 type, const std::string &init_data);
214         void removeActiveObject(u16 id);
215
216         void processActiveObjectMessage(u16 id, const std::string &data);
217         
218         // Get all nearby objects
219         void getActiveObjects(v3f origin, f32 max_d,
220                         core::array<DistanceSortedActiveObject> &dest);
221         
222 private:
223         ClientMap *m_map;
224         scene::ISceneManager *m_smgr;
225         core::map<u16, ClientActiveObject*> m_active_objects;
226 };
227
228 #endif
229
230 #endif
231