ChunkMap.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. void 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. for (int i = 0; i < pixels[index].len; i++)
  93. {
  94. if (pixels[index].blocks[i].height == height)
  95. {
  96. pixels[index].blocks[i].color = resultColor;
  97. found = 1;
  98. }
  99. else if (!found && pixels[index].blocks[i].height > height)
  100. {
  101. break;
  102. }
  103. if (found && removed && i < pixels[index].len - 1)
  104. {
  105. pixels[index].blocks[i] = pixels[index].blocks[i + 1];
  106. }
  107. }
  108. if (found && removed)
  109. {
  110. pixels[index].len--;
  111. }
  112. else if (!found && !removed)
  113. {
  114. MapBlock* blocks = new MapBlock[pixels[index].len + 1];
  115. bool added = 0;
  116. for (int i = 0; i < pixels[index].len; i++)
  117. {
  118. if (pixels[index].blocks[i].height < height)
  119. {
  120. blocks[i] = pixels[index].blocks[i];
  121. }
  122. else
  123. {
  124. if (!added)
  125. {
  126. blocks[i] = {height, resultColor};
  127. added = 1;
  128. }
  129. blocks[i + 1] = pixels[index].blocks[i];
  130. }
  131. }
  132. if (!added)
  133. {
  134. blocks[pixels[index].len] = {height, resultColor};
  135. }
  136. pixels[index].len++;
  137. delete[] pixels[index].blocks;
  138. pixels[index].blocks = blocks;
  139. }
  140. cs.unlock();
  141. }
  142. void ChunkMap::writeTo(Framework::StreamWriter* zWriter) const
  143. {
  144. zWriter->schreibe((char*)&chunkCenter.x, 4);
  145. zWriter->schreibe((char*)&chunkCenter.y, 4);
  146. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  147. {
  148. zWriter->schreibe((char*)&pixels[i].len, 1);
  149. if (pixels[i].len > 0)
  150. {
  151. zWriter->schreibe(
  152. (char*)pixels[i].blocks, (int)sizeof(MapBlock) * pixels[i].len);
  153. }
  154. }
  155. }
  156. Framework::Punkt ChunkMap::getChunkCenter() const
  157. {
  158. return chunkCenter;
  159. }