DimensionMap.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. Punkt topLeft(screenPos.x - (pixelsPerBlock * CHUNK_SIZE) / 2,
  195. screenPos.y - (pixelsPerBlock * CHUNK_SIZE) / 2);
  196. rObj.drawBildSkall(topLeft.x,
  197. topLeft.y,
  198. pixelsPerBlock * CHUNK_SIZE,
  199. pixelsPerBlock * CHUNK_SIZE,
  200. map->getRenderedImage());
  201. unsigned char* heightMap = map->getHeightMap();
  202. for (int xx = 0; xx < CHUNK_SIZE - 1; xx++)
  203. {
  204. for (int yy = 0; yy < CHUNK_SIZE - 1; yy++)
  205. {
  206. if (heightMap[yy * CHUNK_SIZE + xx]
  207. > heightMap[yy * CHUNK_SIZE + xx + 1]
  208. )
  209. {
  210. rObj.drawLinieVAlpha((xx * pixelsPerBlock)
  211. + topLeft.x
  212. + pixelsPerBlock,
  213. (yy * pixelsPerBlock) + topLeft.y,
  214. pixelsPerBlock,
  215. 0xFF000000);
  216. }
  217. if (heightMap[yy * CHUNK_SIZE + xx]
  218. > heightMap[(yy + 1) * CHUNK_SIZE + xx])
  219. {
  220. rObj.drawLinieHAlpha(
  221. (xx * pixelsPerBlock) + topLeft.x,
  222. (yy * pixelsPerBlock) + topLeft.y
  223. + pixelsPerBlock,
  224. pixelsPerBlock,
  225. 0x40000000);
  226. }
  227. }
  228. }
  229. rObj.drawLinieHAlpha(topLeft.x,
  230. topLeft.y,
  231. pixelsPerBlock * CHUNK_SIZE,
  232. 0x50FFFFFF);
  233. rObj.drawLinieVAlpha(topLeft.x,
  234. topLeft.y,
  235. pixelsPerBlock * CHUNK_SIZE,
  236. 0x50FFFFFF);
  237. }
  238. screenPos.y += pixelsPerBlock * CHUNK_SIZE;
  239. }
  240. screenPos.x += pixelsPerBlock * CHUNK_SIZE;
  241. screenPos.y = minScreenPos.y;
  242. }
  243. cs.unlock();
  244. rObj.releaseDrawOptions();
  245. }
  246. void DimensionMap::doMausEreignis(Framework::MausEreignis& me, bool userRet)
  247. {
  248. if (me.id == ME_PLinks)
  249. {
  250. drag = 1;
  251. lastMouse = {me.mx, me.my};
  252. }
  253. if (me.id == ME_RLinks || me.id == ME_Leaves) drag = 0;
  254. if (me.id == ME_Bewegung && drag)
  255. {
  256. scrollOffset -= Punkt(me.mx, me.my) - lastMouse;
  257. lastMouse = Punkt(me.mx, me.my);
  258. rend = 1;
  259. requestNextChunk();
  260. }
  261. if (me.id == ME_DScroll && pixelsPerBlock > 1)
  262. {
  263. scrollOffset = (scrollOffset / pixelsPerBlock) * (pixelsPerBlock - 1);
  264. pixelsPerBlock--;
  265. rend = 1;
  266. requestNextChunk();
  267. }
  268. if (me.id == ME_UScroll)
  269. {
  270. scrollOffset = (scrollOffset / pixelsPerBlock) * (pixelsPerBlock + 1);
  271. pixelsPerBlock++;
  272. rend = 1;
  273. requestNextChunk();
  274. }
  275. if (me.id == ME_RRechts)
  276. {
  277. if (maxHeight != 255)
  278. {
  279. maxHeight = 255;
  280. }
  281. else
  282. {
  283. maxHeight
  284. = (int)(World::INSTANCE->getCurrentPlayerEntity()->getPos().z
  285. / 2);
  286. }
  287. rend = 1;
  288. }
  289. ZeichnungHintergrund::doMausEreignis(me, userRet);
  290. }