Dimension.cpp 4.5 KB

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