]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/minimap.h
Minimap as HUD element with API control
[dragonfireclient.git] / src / client / 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 #pragma once
21
22 #include "../hud.h"
23 #include "irrlichttypes_extrabloated.h"
24 #include "util/thread.h"
25 #include "voxel.h"
26 #include <map>
27 #include <string>
28 #include <vector>
29
30 class Client;
31 class ITextureSource;
32 class IShaderSource;
33
34 #define MINIMAP_MAX_SX 512
35 #define MINIMAP_MAX_SY 512
36
37 enum MinimapShape {
38         MINIMAP_SHAPE_SQUARE,
39         MINIMAP_SHAPE_ROUND,
40 };
41
42 struct MinimapModeDef {
43         MinimapType type;
44         std::string label;
45         u16 scan_height;
46         u16 map_size;
47         std::string texture;
48         u16 scale;
49 };
50
51 struct MinimapPixel {
52         //! The topmost node that the minimap displays.
53         MapNode n;
54         u16 height;
55         u16 air_count;
56 };
57
58 struct MinimapMapblock {
59         void getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos);
60
61         MinimapPixel data[MAP_BLOCKSIZE * MAP_BLOCKSIZE];
62 };
63
64 struct MinimapData {
65         MinimapModeDef mode;
66         v3s16 pos;
67         v3s16 old_pos;
68         MinimapPixel minimap_scan[MINIMAP_MAX_SX * MINIMAP_MAX_SY];
69         bool map_invalidated;
70         bool minimap_shape_round;
71         video::IImage *minimap_mask_round = nullptr;
72         video::IImage *minimap_mask_square = nullptr;
73         video::ITexture *texture = nullptr;
74         video::ITexture *heightmap_texture = nullptr;
75         video::ITexture *minimap_overlay_round = nullptr;
76         video::ITexture *minimap_overlay_square = nullptr;
77         video::ITexture *player_marker = nullptr;
78         video::ITexture *object_marker_red = nullptr;
79 };
80
81 struct QueuedMinimapUpdate {
82         v3s16 pos;
83         MinimapMapblock *data = nullptr;
84 };
85
86 class MinimapUpdateThread : public UpdateThread {
87 public:
88         MinimapUpdateThread() : UpdateThread("Minimap") {}
89         virtual ~MinimapUpdateThread();
90
91         void getMap(v3s16 pos, s16 size, s16 height);
92         void enqueueBlock(v3s16 pos, MinimapMapblock *data);
93         bool pushBlockUpdate(v3s16 pos, MinimapMapblock *data);
94         bool popBlockUpdate(QueuedMinimapUpdate *update);
95
96         MinimapData *data = nullptr;
97
98 protected:
99         virtual void doUpdate();
100
101 private:
102         std::mutex m_queue_mutex;
103         std::deque<QueuedMinimapUpdate> m_update_queue;
104         std::map<v3s16, MinimapMapblock *> m_blocks_cache;
105 };
106
107 class Minimap {
108 public:
109         Minimap(Client *client);
110         ~Minimap();
111
112         void addBlock(v3s16 pos, MinimapMapblock *data);
113
114         v3f getYawVec();
115
116         void setPos(v3s16 pos);
117         v3s16 getPos() const { return data->pos; }
118         void setAngle(f32 angle);
119         f32 getAngle() const { return m_angle; }
120         void toggleMinimapShape();
121         void setMinimapShape(MinimapShape shape);
122         MinimapShape getMinimapShape();
123
124         void clearModes() { m_modes.clear(); };
125         void addMode(MinimapModeDef mode);
126         void addMode(MinimapType type, u16 size = 0, std::string label = "",
127                         std::string texture = "", u16 scale = 1);
128
129         void setModeIndex(size_t index);
130         size_t getModeIndex() const { return m_current_mode_index; };
131         size_t getMaxModeIndex() const { return m_modes.size() - 1; };
132         void nextMode();
133
134         void setModesFromString(std::string modes_string);
135         MinimapModeDef getModeDef() const { return data->mode; }
136
137         video::ITexture *getMinimapTexture();
138
139         void blitMinimapPixelsToImageRadar(video::IImage *map_image);
140         void blitMinimapPixelsToImageSurface(video::IImage *map_image,
141                 video::IImage *heightmap_image);
142
143         scene::SMeshBuffer *getMinimapMeshBuffer();
144
145         void updateActiveMarkers();
146         void drawMinimap();
147         void drawMinimap(core::rect<s32> rect);
148
149         video::IVideoDriver *driver;
150         Client* client;
151         MinimapData *data;
152
153 private:
154         ITextureSource *m_tsrc;
155         IShaderSource *m_shdrsrc;
156         const NodeDefManager *m_ndef;
157         MinimapUpdateThread *m_minimap_update_thread = nullptr;
158         scene::SMeshBuffer *m_meshbuffer;
159         bool m_enable_shaders;
160         std::vector<MinimapModeDef> m_modes;
161         size_t m_current_mode_index;
162         u16 m_surface_mode_scan_height;
163         f32 m_angle;
164         std::mutex m_mutex;
165         std::list<v2f> m_active_markers;
166 };