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