DimensionMap.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "DimensionMap.h"
  2. #include "Constants.h"
  3. #include "World.h"
  4. DimensionMap::DimensionMap()
  5. : ZeichnungHintergrund(),
  6. originChunkCenter(0, 0),
  7. scrollOffset(0, 0),
  8. chunkCount(0),
  9. pixelsPerBlock(16),
  10. maxHeight(255),
  11. waitingForChunk(0),
  12. drag(0)
  13. {
  14. setStyle(Style::Sichtbar | Style::Erlaubt);
  15. chunks = new Framework::Trie<ChunkMap>();
  16. setMausEreignis(_ret1ME);
  17. requestNextChunk();
  18. }
  19. DimensionMap::~DimensionMap()
  20. {
  21. chunks->release();
  22. }
  23. void DimensionMap::getAddrOf(Punkt cPos, char* addr) const
  24. {
  25. *(int*)addr = cPos.x;
  26. *((int*)addr + 1) = cPos.y;
  27. }
  28. void DimensionMap::getAddrOfWorld(Punkt wPos, char* addr) const
  29. {
  30. // needed because otherwise would (-8, -8) have the same
  31. // adress as (8, 8)
  32. if (wPos.x < 0) wPos.x -= CHUNK_SIZE;
  33. if (wPos.y < 0) wPos.y -= CHUNK_SIZE;
  34. wPos /= CHUNK_SIZE;
  35. getAddrOf(wPos, addr);
  36. }
  37. Framework::Punkt DimensionMap::getMinVisibleChunkCenter(
  38. Framework::Punkt& screenPos) const
  39. {
  40. screenPos = getSize() / 2 - scrollOffset;
  41. Punkt currentChunkCenter = originChunkCenter;
  42. while (screenPos.x + pixelsPerBlock * (CHUNK_SIZE / 2) >= 0)
  43. {
  44. screenPos.x -= pixelsPerBlock * CHUNK_SIZE;
  45. currentChunkCenter.x -= CHUNK_SIZE;
  46. }
  47. while (screenPos.y + pixelsPerBlock * (CHUNK_SIZE / 2) >= 0)
  48. {
  49. screenPos.y -= pixelsPerBlock * CHUNK_SIZE;
  50. currentChunkCenter.y -= CHUNK_SIZE;
  51. }
  52. while (screenPos.x + pixelsPerBlock * (CHUNK_SIZE / 2) < 0)
  53. {
  54. screenPos.x += pixelsPerBlock * CHUNK_SIZE;
  55. currentChunkCenter.x += CHUNK_SIZE;
  56. }
  57. while (screenPos.y + pixelsPerBlock * (CHUNK_SIZE / 2) < 0)
  58. {
  59. screenPos.y += pixelsPerBlock * CHUNK_SIZE;
  60. currentChunkCenter.y += CHUNK_SIZE;
  61. }
  62. return currentChunkCenter;
  63. }
  64. Framework::Punkt DimensionMap::getMaxVisibleChunkCenter(
  65. Framework::Punkt& screenPos) const
  66. {
  67. screenPos = getSize() / 2 - scrollOffset;
  68. Punkt currentChunkCenter = originChunkCenter;
  69. while (screenPos.x - pixelsPerBlock * (CHUNK_SIZE / 2) < getBreite())
  70. {
  71. screenPos.x += pixelsPerBlock * CHUNK_SIZE;
  72. currentChunkCenter.x += CHUNK_SIZE;
  73. }
  74. while (screenPos.y - pixelsPerBlock * (CHUNK_SIZE / 2) < getHeight())
  75. {
  76. screenPos.y += pixelsPerBlock * CHUNK_SIZE;
  77. currentChunkCenter.y += CHUNK_SIZE;
  78. }
  79. while (screenPos.x - pixelsPerBlock * (CHUNK_SIZE / 2) >= getBreite())
  80. {
  81. screenPos.x -= pixelsPerBlock * CHUNK_SIZE;
  82. currentChunkCenter.x -= CHUNK_SIZE;
  83. }
  84. while (screenPos.y - pixelsPerBlock * (CHUNK_SIZE / 2) >= getHeight())
  85. {
  86. screenPos.y -= pixelsPerBlock * CHUNK_SIZE;
  87. currentChunkCenter.y -= CHUNK_SIZE;
  88. }
  89. return currentChunkCenter;
  90. }
  91. bool DimensionMap::tick(double time)
  92. {
  93. if (lastSize != getSize())
  94. {
  95. lastSize = getSize();
  96. requestNextChunk();
  97. }
  98. return ZeichnungHintergrund::tick(time);
  99. }
  100. void DimensionMap::requestNextChunk()
  101. {
  102. cs.lock();
  103. if (waitingForChunk)
  104. {
  105. cs.unlock();
  106. return;
  107. }
  108. if (chunkCount == 0)
  109. {
  110. waitingForChunk = 1;
  111. Vec3<float> playerPos
  112. = World::INSTANCE->getCurrentPlayerEntity()->getPos();
  113. char msg[9];
  114. msg[0] = 2;
  115. *(int*)(msg + 1) = (int)playerPos.x;
  116. *(int*)(msg + 5) = (int)playerPos.y;
  117. World::INSTANCE->zClient()->dimensionAPIRequest(msg, 9);
  118. }
  119. else
  120. {
  121. Punkt minScreenPos;
  122. Punkt minVisibleChunk = getMinVisibleChunkCenter(minScreenPos);
  123. Punkt maxScreenPos;
  124. Punkt maxVisibleChunk = getMaxVisibleChunkCenter(maxScreenPos);
  125. Punkt screenPos = minScreenPos;
  126. Punkt screenCenter = getSize() / 2;
  127. double minDist = -1;
  128. Punkt resultChunk(0, 0);
  129. char addr[8];
  130. for (int x = minVisibleChunk.x; x <= maxVisibleChunk.x; x += CHUNK_SIZE)
  131. {
  132. for (int y = minVisibleChunk.y; y <= maxVisibleChunk.y;
  133. y += CHUNK_SIZE)
  134. {
  135. getAddrOfWorld({x, y}, addr);
  136. if (!chunks->z(addr, 8))
  137. {
  138. if (minDist < 0
  139. || (screenCenter - screenPos).getLengthSq() < minDist)
  140. {
  141. minDist = (screenCenter - screenPos).getLengthSq();
  142. resultChunk = {x, y};
  143. }
  144. }
  145. screenPos.y += pixelsPerBlock * CHUNK_SIZE;
  146. }
  147. screenPos.x += pixelsPerBlock * CHUNK_SIZE;
  148. screenPos.y = minScreenPos.y;
  149. }
  150. if (minDist >= 0)
  151. {
  152. waitingForChunk = 1;
  153. char msg[9];
  154. msg[0] = 2;
  155. *(int*)(msg + 1) = (int)resultChunk.x;
  156. *(int*)(msg + 5) = (int)resultChunk.y;
  157. World::INSTANCE->zClient()->dimensionAPIRequest(msg, 9);
  158. }
  159. }
  160. cs.unlock();
  161. }
  162. void DimensionMap::addChunk(ChunkMap* chunk)
  163. {
  164. cs.lock();
  165. if (chunkCount == 0) originChunkCenter = chunk->getChunkCenter();
  166. char addr[8];
  167. getAddrOfWorld(chunk->getChunkCenter(), addr);
  168. chunks->set(addr, 8, chunk);
  169. chunkCount++;
  170. waitingForChunk = 0;
  171. cs.unlock();
  172. requestNextChunk();
  173. }
  174. void DimensionMap::render(Framework::Bild& rObj)
  175. {
  176. ZeichnungHintergrund::render(rObj);
  177. if (!rObj.setDrawOptions(innenPosition, innenSize)) return;
  178. cs.lock();
  179. Punkt minScreenPos;
  180. Punkt minVisibleChunk = getMinVisibleChunkCenter(minScreenPos);
  181. Punkt maxScreenPos;
  182. Punkt maxVisibleChunk = getMaxVisibleChunkCenter(maxScreenPos);
  183. char addr[8];
  184. Punkt screenPos = minScreenPos;
  185. for (int x = minVisibleChunk.x; x <= maxVisibleChunk.x; x += CHUNK_SIZE)
  186. {
  187. for (int y = minVisibleChunk.y; y <= maxVisibleChunk.y; y += CHUNK_SIZE)
  188. {
  189. getAddrOfWorld({x, y}, addr);
  190. ChunkMap* map = chunks->z(addr, 8);
  191. if (map)
  192. {
  193. map->setMaxHeight((unsigned char)maxHeight);
  194. rObj.drawBildSkall(
  195. screenPos.x - (pixelsPerBlock * CHUNK_SIZE) / 2,
  196. screenPos.y - (pixelsPerBlock * CHUNK_SIZE) / 2,
  197. pixelsPerBlock * CHUNK_SIZE,
  198. pixelsPerBlock * CHUNK_SIZE,
  199. map->getRenderedImage());
  200. }
  201. screenPos.y += pixelsPerBlock * CHUNK_SIZE;
  202. }
  203. screenPos.x += pixelsPerBlock * CHUNK_SIZE;
  204. screenPos.y = minScreenPos.y;
  205. }
  206. cs.unlock();
  207. rObj.releaseDrawOptions();
  208. }
  209. void DimensionMap::doMausEreignis(Framework::MausEreignis& me, bool userRet)
  210. {
  211. if (me.id == ME_PLinks)
  212. {
  213. drag = 1;
  214. lastMouse = {me.mx, me.my};
  215. }
  216. if (me.id == ME_RLinks || me.id == ME_Leaves) drag = 0;
  217. if (me.id == ME_Bewegung && drag)
  218. {
  219. scrollOffset -= Punkt(me.mx, me.my) - lastMouse;
  220. lastMouse = Punkt(me.mx, me.my);
  221. rend = 1;
  222. requestNextChunk();
  223. }
  224. if (me.id == ME_DScroll && pixelsPerBlock > 1)
  225. {
  226. scrollOffset = (scrollOffset / pixelsPerBlock) * (pixelsPerBlock - 1);
  227. pixelsPerBlock--;
  228. rend = 1;
  229. requestNextChunk();
  230. }
  231. if (me.id == ME_UScroll)
  232. {
  233. scrollOffset = (scrollOffset / pixelsPerBlock) * (pixelsPerBlock + 1);
  234. pixelsPerBlock++;
  235. rend = 1;
  236. requestNextChunk();
  237. }
  238. if (me.id == ME_RRechts)
  239. {
  240. if (maxHeight != 255)
  241. {
  242. maxHeight = 255;
  243. }
  244. else
  245. {
  246. maxHeight
  247. = (int)(World::INSTANCE->getCurrentPlayerEntity()->getPos().z
  248. / 2);
  249. }
  250. rend = 1;
  251. }
  252. ZeichnungHintergrund::doMausEreignis(me, userRet);
  253. }