ChunkMap.cpp 5.2 KB

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