Dimension.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "Dimension.h"
  2. #include "Constants.h"
  3. #include "Datei.h"
  4. #include "Game.h"
  5. #include "Globals.h"
  6. #include "World.h"
  7. using namespace Framework;
  8. #define MAX_VIEW_DISTANCE CHUNK_SIZE * 8
  9. Dimension::Dimension()
  10. : chunks(new Trie<Chunk>()),
  11. entities(new RCArray<Entity>())
  12. {}
  13. Dimension::~Dimension()
  14. {
  15. entities->release();
  16. chunks->release();
  17. }
  18. void Dimension::getAddrOf(Punkt cPos, char* addr) const
  19. {
  20. *(int*)addr = cPos.x;
  21. *((int*)addr + 1) = cPos.y;
  22. }
  23. void Dimension::getAddrOfWorld(Punkt wPos, char* addr) const
  24. {
  25. if (wPos.x < 0)
  26. wPos.x -= CHUNK_SIZE;
  27. if (wPos.y < 0) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
  28. wPos.y -= CHUNK_SIZE;
  29. wPos /= CHUNK_SIZE;
  30. getAddrOf(wPos, addr);
  31. }
  32. void Dimension::api(char* message)
  33. {
  34. switch (message[0])
  35. {
  36. case 1: // chunck
  37. {
  38. int cX = *(int*)(message + 1);
  39. int cY = *(int*)(message + 5);
  40. Chunk* ch = zChunk(Punkt(cX, cY));
  41. if (ch)
  42. ch->api(message + 9);
  43. break;
  44. }
  45. case 2: // entity
  46. {
  47. int eId = *(int*)(message + 1);
  48. Entity* e = zEntity(eId);
  49. if (e)
  50. e->api(message + 5);
  51. break;
  52. }
  53. case 3: // block
  54. {
  55. int px = *(int*)(message + 1);
  56. int py = *(int*)(message + 5);
  57. int pz = *(int*)(message + 9);
  58. Block* b = zBlock(Framework::Vec3<int>(px, py, pz));
  59. if (b)
  60. b->api(message + 13);
  61. break;
  62. }
  63. }
  64. }
  65. Chunk* Dimension::zChunk(Punkt wPos) const
  66. {
  67. char addr[8];
  68. getAddrOfWorld(wPos, addr);
  69. return chunks->z(addr, 8);
  70. }
  71. Block* Dimension::zBlock(Vec3<int> location)
  72. {
  73. Chunk* c = zChunk(currentGame->getChunkCenter(location.x, location.y));
  74. if (c)
  75. return c->zBlockAt(location);
  76. return 0;
  77. }
  78. Block* Dimension::getBlock(Vec3<int> location)
  79. {
  80. cs.lock();
  81. Chunk* c = zChunk(currentGame->getChunkCenter(location.x, location.y));
  82. if (c)
  83. {
  84. Block* b = c->zBlockAt(location);
  85. b = b ? dynamic_cast<Block*>(b->getThis()) : 0;
  86. cs.unlock();
  87. return b;
  88. }
  89. cs.unlock();
  90. return 0;
  91. }
  92. void Dimension::addEntity(Entity* entity)
  93. {
  94. entities->add(entity);
  95. }
  96. void Dimension::setChunk(Chunk* chunk, Punkt center, World* zWorld)
  97. {
  98. char addr[8];
  99. getAddrOfWorld(center, addr);
  100. Chunk* old = chunks->z(addr, 8);
  101. cs.lock();
  102. if (old)
  103. {
  104. zWorld->setVisibility(old, 0);
  105. int index = 0;
  106. for (auto iterator = chunkList.begin(); iterator; ++iterator, ++index)
  107. {
  108. if ((Chunk*)iterator == old)
  109. {
  110. if (chunk)
  111. iterator.set(chunk);
  112. else
  113. chunkList.remove(index);
  114. break;
  115. }
  116. }
  117. }
  118. else if (chunk)
  119. chunkList.add(chunk);
  120. chunks->set(addr, 8, chunk);
  121. if (chunk)
  122. {
  123. zWorld->setVisibility(chunk, 1);
  124. }
  125. cs.unlock();
  126. }
  127. bool Dimension::hasChunck(int x, int y) const
  128. {
  129. return zChunk(Punkt(x, y));
  130. }
  131. void Dimension::removeDistantChunks(Punkt wPos, World* zWorld)
  132. {
  133. Array<int> removed;
  134. int index = 0;
  135. for (Chunk* chunk : chunkList)
  136. {
  137. if ((chunk->getCenter() - wPos).getLength() > MAX_VIEW_DISTANCE)
  138. removed.add(index, 0);
  139. index++;
  140. }
  141. for (int i : removed)
  142. {
  143. Chunk* chunk = chunkList.get(i);
  144. zWorld->setVisibility(chunk, 0);
  145. setChunk(0, chunk->getCenter(), zWorld);
  146. }
  147. }
  148. void Dimension::setBlock(Block* block)
  149. {
  150. cs.lock();
  151. Chunk* c = zChunk(currentGame->getChunkCenter((int)floor(block->getPos().x), (int)floor(block->getPos().y)));
  152. if (c)
  153. c->setBlock(block);
  154. else
  155. block->release();
  156. cs.unlock();
  157. }
  158. void Dimension::removeBlock(Block* zBlock)
  159. {
  160. cs.lock();
  161. Chunk* c = zChunk(currentGame->getChunkCenter((int)floor(zBlock->getPos().x), (int)floor(zBlock->getPos().y)));
  162. if (c)
  163. c->removeBlock(zBlock);
  164. cs.unlock();
  165. }
  166. Entity* Dimension::zEntity(int id)
  167. {
  168. cs.lock();
  169. for (Entity* e : *entities)
  170. {
  171. if (e->getId() == id)
  172. {
  173. cs.unlock();
  174. return e;
  175. }
  176. }
  177. cs.unlock();
  178. return 0;
  179. }
  180. Entity* Dimension::getEntity(int id)
  181. {
  182. cs.lock();
  183. for (Entity* e : *entities)
  184. {
  185. if (e->getId() == id)
  186. {
  187. Entity* result = dynamic_cast<Entity*>(e->getThis());
  188. cs.unlock();
  189. return result;
  190. }
  191. }
  192. cs.unlock();
  193. return 0;
  194. }
  195. void Dimension::removeEntity(int id)
  196. {
  197. currentGame->lockWorld();
  198. cs.lock();
  199. int index = 0;
  200. for (Entity* e : *entities)
  201. {
  202. if (e->getId() == id)
  203. {
  204. currentGame->setVisibility(e, 0);
  205. entities->remove(index);
  206. cs.unlock();
  207. currentGame->unlockWorld();
  208. return;
  209. }
  210. index++;
  211. }
  212. cs.unlock();
  213. currentGame->unlockWorld();
  214. }