minimap.h 4.0 KB

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