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