]> git.lizzy.rs Git - minetest.git/blob - src/minimap.h
ebb74c4fb9c1195862fcf59b207b12b06d64d3fb
[minetest.git] / src / minimap.h
1 /*
2 Minetest
3 Copyright (C) 2010-2015 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 #ifndef MINIMAP_HEADER
21 #define MINIMAP_HEADER
22
23 #include "irrlichttypes_extrabloated.h"
24 #include "client.h"
25 #include "voxel.h"
26 #include "jthread/jmutex.h"
27 #include <map>
28 #include <string>
29 #include <vector>
30
31 enum MinimapMode {
32         MINIMAP_MODE_OFF,
33         MINIMAP_MODE_SURFACEx1,
34         MINIMAP_MODE_SURFACEx2,
35         MINIMAP_MODE_SURFACEx4,
36         MINIMAP_MODE_RADARx1,
37         MINIMAP_MODE_RADARx2,
38         MINIMAP_MODE_RADARx4
39 };
40
41 struct MinimapPixel
42 {
43         u16 id;
44         u16 height;
45         u16 air_count;
46         u16 light;
47 };
48
49 struct MinimapMapblock
50 {
51         MinimapPixel data[MAP_BLOCKSIZE * MAP_BLOCKSIZE];
52 };
53
54 struct MinimapData
55 {
56         bool radar;
57         MinimapMode mode;
58         v3s16 pos;
59         v3s16 old_pos;
60         u16 scan_height;
61         u16 map_size;
62         MinimapPixel minimap_scan[512 * 512];
63         bool map_invalidated;
64         bool minimap_shape_round;
65         video::IImage *minimap_image;
66         video::IImage *heightmap_image;
67         video::IImage *minimap_mask_round;
68         video::IImage *minimap_mask_square;
69         video::ITexture *texture;
70         video::ITexture *heightmap_texture;
71         video::ITexture *minimap_overlay_round;
72         video::ITexture *minimap_overlay_square;
73         video::ITexture *player_marker;
74 };
75
76 struct QueuedMinimapUpdate
77 {
78         v3s16 pos;
79         MinimapMapblock *data;
80
81         QueuedMinimapUpdate();
82         ~QueuedMinimapUpdate();
83 };
84
85 /*
86         A thread-safe queue of minimap mapblocks update tasks
87 */
88
89 class MinimapUpdateQueue
90 {
91 public:
92         MinimapUpdateQueue();
93
94         ~MinimapUpdateQueue();
95
96         void addBlock(v3s16 pos, MinimapMapblock *data);
97
98         QueuedMinimapUpdate *pop();
99
100         u32 size()
101         {
102                 JMutexAutoLock lock(m_mutex);
103                 return m_queue.size();
104         }
105
106 private:
107         std::vector<QueuedMinimapUpdate*> m_queue;
108         JMutex m_mutex;
109 };
110
111 class MinimapUpdateThread : public JThread
112 {
113 private:
114
115 public:
116         MinimapUpdateThread(IrrlichtDevice *device, Client *client)
117         {
118                 this->device = device;
119                 this->client = client;
120                 this->driver = device->getVideoDriver();
121                 this->tsrc = client->getTextureSource();
122         }
123         void getMap (v3s16 pos, s16 size, s16 height, bool radar);
124         MinimapPixel *getMinimapPixel (v3s16 pos, s16 height, s16 &pixel_height);
125         s16 getAirCount (v3s16 pos, s16 height);
126         video::SColor getColorFromId(u16 id);
127         IrrlichtDevice *device;
128         Client *client;
129         video::IVideoDriver *driver;
130         ITextureSource *tsrc;
131         void *Thread();
132         MinimapData *data;
133         MinimapUpdateQueue m_queue;
134         std::map<v3s16, MinimapMapblock *> m_blocks_cache;
135 };
136
137 class Mapper
138 {
139 private:
140         MinimapUpdateThread *m_minimap_update_thread;
141         video::ITexture *minimap_texture;
142         scene::SMeshBuffer *m_meshbuffer;
143         bool m_enable_shaders;
144         JMutex m_mutex;
145
146 public:
147         Mapper(IrrlichtDevice *device, Client *client);
148         ~Mapper();
149
150         void addBlock(v3s16 pos, MinimapMapblock *data);
151         void setPos(v3s16 pos);
152         video::ITexture* getMinimapTexture();
153         v3f getYawVec();
154         MinimapMode getMinimapMode();
155         void setMinimapMode(MinimapMode mode);
156         void toggleMinimapShape();
157         scene::SMeshBuffer *getMinimapMeshBuffer();
158         void drawMinimap();
159         IrrlichtDevice *device;
160         Client *client;
161         video::IVideoDriver *driver;
162         LocalPlayer *player;
163         ITextureSource *tsrc;
164         IShaderSource *shdrsrc;
165         MinimapData *data;
166 };
167
168 #endif