Dimension.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. currentGame->setVisibility(entity, 1);
  96. }
  97. void Dimension::setChunk(Chunk* chunk, Punkt center, World* zWorld)
  98. {
  99. char addr[8];
  100. getAddrOfWorld(center, addr);
  101. Chunk* old = chunks->z(addr, 8);
  102. cs.lock();
  103. if (old)
  104. {
  105. zWorld->setVisibility(old, 0);
  106. int index = 0;
  107. for (auto iterator = chunkList.begin(); iterator; ++iterator, ++index)
  108. {
  109. if ((Chunk*)iterator == old)
  110. {
  111. if (chunk)
  112. iterator.set(chunk);
  113. else
  114. chunkList.remove(index);
  115. break;
  116. }
  117. }
  118. }
  119. else if (chunk)
  120. chunkList.add(chunk);
  121. chunks->set(addr, 8, chunk);
  122. if (chunk)
  123. {
  124. zWorld->setVisibility(chunk, 1);
  125. }
  126. cs.unlock();
  127. }
  128. bool Dimension::hasChunck(int x, int y) const
  129. {
  130. return zChunk(Punkt(x, y));
  131. }
  132. void Dimension::removeDistantChunks(Punkt wPos, World* zWorld)
  133. {
  134. Array<int> removed;
  135. int index = 0;
  136. for (Chunk* chunk : chunkList)
  137. {
  138. if ((chunk->getCenter() - wPos).getLength() > MAX_VIEW_DISTANCE)
  139. removed.add(index, 0);
  140. index++;
  141. }
  142. for (int i : removed)
  143. {
  144. Chunk* chunk = chunkList.get(i);
  145. zWorld->setVisibility(chunk, 0);
  146. setChunk(0, chunk->getCenter(), zWorld);
  147. }
  148. }
  149. void Dimension::setBlock(Block* block)
  150. {
  151. cs.lock();
  152. Chunk* c = zChunk(currentGame->getChunkCenter((int)floor(block->getPos().x), (int)floor(block->getPos().y)));
  153. if (c)
  154. c->setBlock(block);
  155. else
  156. block->release();
  157. cs.unlock();
  158. }
  159. void Dimension::removeBlock(Block* zBlock)
  160. {
  161. cs.lock();
  162. Chunk* c = zChunk(currentGame->getChunkCenter((int)floor(zBlock->getPos().x), (int)floor(zBlock->getPos().y)));
  163. if (c)
  164. c->removeBlock(zBlock);
  165. cs.unlock();
  166. }
  167. Entity* Dimension::zEntity(int id)
  168. {
  169. cs.lock();
  170. for (Entity* e : *entities)
  171. {
  172. if (e->getId() == id)
  173. {
  174. cs.unlock();
  175. return e;
  176. }
  177. }
  178. cs.unlock();
  179. return 0;
  180. }
  181. Entity* Dimension::getEntity(int id)
  182. {
  183. cs.lock();
  184. for (Entity* e : *entities)
  185. {
  186. if (e->getId() == id)
  187. {
  188. Entity* result = dynamic_cast<Entity*>(e->getThis());
  189. cs.unlock();
  190. return result;
  191. }
  192. }
  193. cs.unlock();
  194. return 0;
  195. }
  196. void Dimension::removeEntity(int id)
  197. {
  198. currentGame->lockWorld();
  199. cs.lock();
  200. int index = 0;
  201. for (Entity* e : *entities)
  202. {
  203. if (e->getId() == id)
  204. {
  205. currentGame->setVisibility(e, 0);
  206. entities->remove(index);
  207. cs.unlock();
  208. currentGame->unlockWorld();
  209. return;
  210. }
  211. index++;
  212. }
  213. cs.unlock();
  214. currentGame->unlockWorld();
  215. }