Translated using Weblate (Chinese (Simplified))
[oweals/minetest.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 "irrlichttypes_extrabloated.h"
23 #include "util/thread.h"
24 #include "voxel.h"
25 #include <map>
26 #include <string>
27 #include <vector>
28
29 class Client;
30 class ITextureSource;
31 class IShaderSource;
32
33 #define MINIMAP_MAX_SX 512
34 #define MINIMAP_MAX_SY 512
35
36 enum MinimapMode {
37         MINIMAP_MODE_OFF,
38         MINIMAP_MODE_SURFACEx1,
39         MINIMAP_MODE_SURFACEx2,
40         MINIMAP_MODE_SURFACEx4,
41         MINIMAP_MODE_RADARx1,
42         MINIMAP_MODE_RADARx2,
43         MINIMAP_MODE_RADARx4,
44         MINIMAP_MODE_COUNT,
45 };
46
47 enum MinimapShape {
48         MINIMAP_SHAPE_SQUARE,
49         MINIMAP_SHAPE_ROUND,
50 };
51
52 struct MinimapModeDef {
53         bool is_radar;
54         u16 scan_height;
55         u16 map_size;
56 };
57
58 struct MinimapPixel {
59         //! The topmost node that the minimap displays.
60         MapNode n;
61         u16 height;
62         u16 air_count;
63 };
64
65 struct MinimapMapblock {
66         void getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos);
67
68         MinimapPixel data[MAP_BLOCKSIZE * MAP_BLOCKSIZE];
69 };
70
71 struct MinimapData {
72         bool is_radar;
73         MinimapMode mode;
74         v3s16 pos;
75         v3s16 old_pos;
76         u16 scan_height;
77         u16 map_size;
78         MinimapPixel minimap_scan[MINIMAP_MAX_SX * MINIMAP_MAX_SY];
79         bool map_invalidated;
80         bool minimap_shape_round;
81         video::IImage *minimap_mask_round = nullptr;
82         video::IImage *minimap_mask_square = nullptr;
83         video::ITexture *texture = nullptr;
84         video::ITexture *heightmap_texture = nullptr;
85         video::ITexture *minimap_overlay_round = nullptr;
86         video::ITexture *minimap_overlay_square = nullptr;
87         video::ITexture *player_marker = nullptr;
88         video::ITexture *object_marker_red = nullptr;
89 };
90
91 struct QueuedMinimapUpdate {
92         v3s16 pos;
93         MinimapMapblock *data = nullptr;
94 };
95
96 class MinimapUpdateThread : public UpdateThread {
97 public:
98         MinimapUpdateThread() : UpdateThread("Minimap") {}
99         virtual ~MinimapUpdateThread();
100
101         void getMap(v3s16 pos, s16 size, s16 height);
102         void enqueueBlock(v3s16 pos, MinimapMapblock *data);
103         bool pushBlockUpdate(v3s16 pos, MinimapMapblock *data);
104         bool popBlockUpdate(QueuedMinimapUpdate *update);
105
106         MinimapData *data = nullptr;
107
108 protected:
109         virtual void doUpdate();
110
111 private:
112         std::mutex m_queue_mutex;
113         std::deque<QueuedMinimapUpdate> m_update_queue;
114         std::map<v3s16, MinimapMapblock *> m_blocks_cache;
115 };
116
117 class Minimap {
118 public:
119         Minimap(Client *client);
120         ~Minimap();
121
122         void addBlock(v3s16 pos, MinimapMapblock *data);
123
124         v3f getYawVec();
125
126         void setPos(v3s16 pos);
127         v3s16 getPos() const { return data->pos; }
128         void setAngle(f32 angle);
129         f32 getAngle() const { return m_angle; }
130         void setMinimapMode(MinimapMode mode);
131         MinimapMode getMinimapMode() const { return data->mode; }
132         void toggleMinimapShape();
133         void setMinimapShape(MinimapShape shape);
134         MinimapShape getMinimapShape();
135
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
148         video::IVideoDriver *driver;
149         Client* client;
150         MinimapData *data;
151
152 private:
153         ITextureSource *m_tsrc;
154         IShaderSource *m_shdrsrc;
155         const NodeDefManager *m_ndef;
156         MinimapUpdateThread *m_minimap_update_thread;
157         scene::SMeshBuffer *m_meshbuffer;
158         bool m_enable_shaders;
159         u16 m_surface_mode_scan_height;
160         f32 m_angle;
161         std::mutex m_mutex;
162         std::list<v2f> m_active_markers;
163 };