minimap.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "../hud.h"
  18. #include "irrlichttypes_extrabloated.h"
  19. #include "util/thread.h"
  20. #include "voxel.h"
  21. #include <map>
  22. #include <string>
  23. #include <vector>
  24. class Client;
  25. class ITextureSource;
  26. class IShaderSource;
  27. #define MINIMAP_MAX_SX 512
  28. #define MINIMAP_MAX_SY 512
  29. enum MinimapShape {
  30. MINIMAP_SHAPE_SQUARE,
  31. MINIMAP_SHAPE_ROUND,
  32. };
  33. struct MinimapModeDef {
  34. MinimapType type;
  35. std::string label;
  36. u16 scan_height;
  37. u16 map_size;
  38. std::string texture;
  39. u16 scale;
  40. };
  41. struct MinimapMarker {
  42. MinimapMarker(scene::ISceneNode *parent_node):
  43. parent_node(parent_node)
  44. {
  45. }
  46. scene::ISceneNode *parent_node;
  47. };
  48. struct MinimapPixel {
  49. //! The topmost node that the minimap displays.
  50. MapNode n;
  51. u16 height;
  52. u16 air_count;
  53. };
  54. struct MinimapMapblock {
  55. void getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos);
  56. MinimapPixel data[MAP_BLOCKSIZE * MAP_BLOCKSIZE];
  57. };
  58. struct MinimapData {
  59. MinimapModeDef mode;
  60. v3s16 pos;
  61. v3s16 old_pos;
  62. MinimapPixel minimap_scan[MINIMAP_MAX_SX * MINIMAP_MAX_SY];
  63. bool map_invalidated;
  64. bool minimap_shape_round;
  65. video::IImage *minimap_mask_round = nullptr;
  66. video::IImage *minimap_mask_square = nullptr;
  67. video::ITexture *texture = nullptr;
  68. video::ITexture *heightmap_texture = nullptr;
  69. bool textures_initialised = false; // True if the following textures are not nullptrs.
  70. video::ITexture *minimap_overlay_round = nullptr;
  71. video::ITexture *minimap_overlay_square = nullptr;
  72. video::ITexture *player_marker = nullptr;
  73. video::ITexture *object_marker_red = nullptr;
  74. };
  75. struct QueuedMinimapUpdate {
  76. v3s16 pos;
  77. MinimapMapblock *data = nullptr;
  78. };
  79. class MinimapUpdateThread : public UpdateThread {
  80. public:
  81. MinimapUpdateThread() : UpdateThread("Minimap") {}
  82. virtual ~MinimapUpdateThread();
  83. void getMap(v3s16 pos, s16 size, s16 height);
  84. void enqueueBlock(v3s16 pos, MinimapMapblock *data);
  85. bool pushBlockUpdate(v3s16 pos, MinimapMapblock *data);
  86. bool popBlockUpdate(QueuedMinimapUpdate *update);
  87. MinimapData *data = nullptr;
  88. protected:
  89. virtual void doUpdate();
  90. private:
  91. std::mutex m_queue_mutex;
  92. std::deque<QueuedMinimapUpdate> m_update_queue;
  93. std::map<v3s16, MinimapMapblock *> m_blocks_cache;
  94. };
  95. class Minimap {
  96. public:
  97. Minimap(Client *client);
  98. ~Minimap();
  99. void addBlock(v3s16 pos, MinimapMapblock *data);
  100. v3f getYawVec();
  101. void setPos(v3s16 pos);
  102. v3s16 getPos() const { return data->pos; }
  103. void setAngle(f32 angle);
  104. f32 getAngle() const { return m_angle; }
  105. void toggleMinimapShape();
  106. void setMinimapShape(MinimapShape shape);
  107. MinimapShape getMinimapShape();
  108. void clearModes() { m_modes.clear(); };
  109. void addMode(MinimapModeDef mode);
  110. void addMode(MinimapType type, u16 size = 0, const std::string &label = "",
  111. const std::string &texture = "", u16 scale = 1);
  112. void setModeIndex(size_t index);
  113. size_t getModeIndex() const { return m_current_mode_index; };
  114. size_t getMaxModeIndex() const { return m_modes.size() - 1; };
  115. void nextMode();
  116. MinimapModeDef getModeDef() const { return data->mode; }
  117. video::IImage *getMinimapMask();
  118. video::ITexture *getMinimapTexture();
  119. void blitMinimapPixelsToImageRadar(video::IImage *map_image);
  120. void blitMinimapPixelsToImageSurface(video::IImage *map_image,
  121. video::IImage *heightmap_image);
  122. scene::SMeshBuffer *getMinimapMeshBuffer();
  123. MinimapMarker* addMarker(scene::ISceneNode *parent_node);
  124. void removeMarker(MinimapMarker **marker);
  125. void updateActiveMarkers();
  126. void drawMinimap(core::rect<s32> rect);
  127. video::IVideoDriver *driver;
  128. Client* client;
  129. MinimapData *data;
  130. private:
  131. ITextureSource *m_tsrc;
  132. IShaderSource *m_shdrsrc;
  133. const NodeDefManager *m_ndef;
  134. MinimapUpdateThread *m_minimap_update_thread = nullptr;
  135. scene::SMeshBuffer *m_meshbuffer;
  136. bool m_enable_shaders;
  137. std::vector<MinimapModeDef> m_modes;
  138. size_t m_current_mode_index;
  139. u16 m_surface_mode_scan_height;
  140. f32 m_angle;
  141. std::mutex m_mutex;
  142. std::list<MinimapMarker*> m_markers;
  143. std::list<v2f> m_active_markers;
  144. };