ChunkMap.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "ChunkMap.h"
  2. #include "Chunk.h"
  3. #include "Constants.h"
  4. #include "Game.h"
  5. ChunkMap::ChunkMap(Framework::Punkt chunkCenter)
  6. : ReferenceCounter(),
  7. chunkCenter(chunkCenter)
  8. {
  9. pixels = new MapPixel[CHUNK_SIZE * CHUNK_SIZE];
  10. memset(pixels, 0, sizeof(MapPixel) * CHUNK_SIZE * CHUNK_SIZE);
  11. }
  12. ChunkMap::ChunkMap(Chunk* zChunk)
  13. : ReferenceCounter(),
  14. chunkCenter(zChunk->location)
  15. {
  16. pixels = new MapPixel[CHUNK_SIZE * CHUNK_SIZE];
  17. memset(pixels, 0, sizeof(MapPixel) * CHUNK_SIZE * CHUNK_SIZE);
  18. MapBlock blocksBuffer[256];
  19. for (int x = 0; x < CHUNK_SIZE; x++)
  20. {
  21. for (int y = 0; y < CHUNK_SIZE; y++)
  22. {
  23. int count = 0;
  24. bool visible = 1;
  25. for (int height = WORLD_HEIGHT / 2 - 1; height >= 0; height--)
  26. {
  27. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT;
  28. const Block* block1
  29. = CONST_BLOCK(zChunk->blocks[index + height * 2],
  30. zChunk->blockIds[index + height * 2]);
  31. const Block* block2
  32. = CONST_BLOCK(zChunk->blocks[index + height * 2 + 1],
  33. zChunk->blockIds[index + height * 2 + 1]);
  34. int color1 = 0;
  35. int color2 = 0;
  36. if (visible) color2 = block2->getMapColor();
  37. visible = block2->isPassable() || block2->isTransparent();
  38. if (visible) color1 = block1->getMapColor();
  39. visible = block1->isPassable() || block1->isTransparent();
  40. if (color1 || color2)
  41. {
  42. MapBlock tmp = {(unsigned char)height,
  43. ((color1 >> 24) & 0xFF) > ((color2 >> 24) & 0xFF)
  44. ? color1
  45. : color2};
  46. blocksBuffer[256 - ++count] = tmp;
  47. }
  48. }
  49. int i = x * CHUNK_SIZE + y;
  50. pixels[i].blocks = new MapBlock[count];
  51. memcpy(pixels[i].blocks,
  52. blocksBuffer + 256 - count,
  53. sizeof(MapBlock) * count);
  54. pixels[i].len = (unsigned char)count;
  55. }
  56. }
  57. }
  58. ChunkMap::ChunkMap(Framework::StreamReader* zReader)
  59. : ReferenceCounter()
  60. {
  61. zReader->lese((char*)&chunkCenter.x, 4);
  62. zReader->lese((char*)&chunkCenter.y, 4);
  63. pixels = new MapPixel[CHUNK_SIZE * CHUNK_SIZE];
  64. memset(pixels, 0, sizeof(MapPixel) * CHUNK_SIZE * CHUNK_SIZE);
  65. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  66. {
  67. zReader->lese((char*)&pixels[i].len, 1);
  68. if (pixels[i].len > 0)
  69. {
  70. pixels[i].blocks = new MapBlock[pixels[i].len];
  71. zReader->lese(
  72. (char*)pixels[i].blocks, (int)sizeof(MapBlock) * pixels[i].len);
  73. }
  74. }
  75. }
  76. ChunkMap::~ChunkMap()
  77. {
  78. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  79. {
  80. delete[] pixels[i].blocks;
  81. }
  82. delete[] pixels;
  83. }
  84. bool ChunkMap::update(
  85. char x, char y, unsigned char height, int color1, int color2)
  86. {
  87. cs.lock();
  88. int index = x * CHUNK_SIZE + y;
  89. bool found = 0;
  90. int resultColor
  91. = ((color1 >> 24) & 0xFF) > ((color2 >> 24) & 0xFF) ? color1 : color2;
  92. bool removed = !((resultColor >> 24) & 0xFF);
  93. bool changed = 0;
  94. for (int i = 0; i < pixels[index].len; i++)
  95. {
  96. if (pixels[index].blocks[i].height == height)
  97. {
  98. changed = pixels[index].blocks[i].color != resultColor;
  99. pixels[index].blocks[i].color = resultColor;
  100. found = 1;
  101. }
  102. else if (!found && pixels[index].blocks[i].height > height)
  103. {
  104. break;
  105. }
  106. if (found && removed && i < pixels[index].len - 1)
  107. {
  108. pixels[index].blocks[i] = pixels[index].blocks[i + 1];
  109. }
  110. }
  111. if (found && removed)
  112. {
  113. changed = 1;
  114. pixels[index].len--;
  115. }
  116. else if (!found && !removed)
  117. {
  118. MapBlock* blocks = new MapBlock[pixels[index].len + 1];
  119. bool added = 0;
  120. for (int i = 0; i < pixels[index].len; i++)
  121. {
  122. if (pixels[index].blocks[i].height < height)
  123. {
  124. blocks[i] = pixels[index].blocks[i];
  125. }
  126. else
  127. {
  128. if (!added)
  129. {
  130. blocks[i] = {height, resultColor};
  131. added = 1;
  132. }
  133. blocks[i + 1] = pixels[index].blocks[i];
  134. }
  135. }
  136. if (!added)
  137. {
  138. blocks[pixels[index].len] = {height, resultColor};
  139. }
  140. changed = 1;
  141. pixels[index].len++;
  142. delete[] pixels[index].blocks;
  143. pixels[index].blocks = blocks;
  144. }
  145. cs.unlock();
  146. return changed;
  147. }
  148. void ChunkMap::writeTo(Framework::StreamWriter* zWriter) const
  149. {
  150. zWriter->schreibe((char*)&chunkCenter.x, 4);
  151. zWriter->schreibe((char*)&chunkCenter.y, 4);
  152. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  153. {
  154. zWriter->schreibe((char*)&pixels[i].len, 1);
  155. if (pixels[i].len > 0)
  156. {
  157. zWriter->schreibe(
  158. (char*)pixels[i].blocks, (int)sizeof(MapBlock) * pixels[i].len);
  159. }
  160. }
  161. }
  162. Framework::Punkt ChunkMap::getChunkCenter() const
  163. {
  164. return chunkCenter;
  165. }