Dimension.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #include "Dimension.h"
  2. #include "Constants.h"
  3. #include "Datei.h"
  4. #include "Game.h"
  5. #include "NoBlock.h"
  6. using namespace Framework;
  7. Dimension::Dimension(int id)
  8. : Thread(),
  9. nextStructureId(0),
  10. dimensionId(id),
  11. gravity(9.8f),
  12. chunks(new Trie<Chunk>()),
  13. entities(new RCArray<Entity>()),
  14. stop(0)
  15. {
  16. Datei d;
  17. d.setDatei(
  18. Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(id) + "/nst.id");
  19. if (d.existiert())
  20. {
  21. d.open(Datei::Style::lesen);
  22. d.lese((char*)&nextStructureId, 8);
  23. d.close();
  24. }
  25. start();
  26. }
  27. Dimension::~Dimension()
  28. {
  29. entities->release();
  30. chunks->release();
  31. }
  32. void Dimension::api(Framework::InMemoryBuffer* zRequest,
  33. NetworkMessage* zResponse,
  34. Entity* zSource)
  35. {
  36. DoLaterHandler laterHandler;
  37. char type;
  38. zRequest->lese(&type, 1);
  39. switch (type)
  40. {
  41. case 0: // chunk message
  42. {
  43. Punkt center;
  44. zRequest->lese((char*)&center.x, 4);
  45. zRequest->lese((char*)&center.y, 4);
  46. cs.lock();
  47. Chunk* cC = zChunk(center);
  48. if (!cC)
  49. {
  50. // TODO: have a max amount of waiting requests per player
  51. waitingRequests.add(
  52. {dynamic_cast<InMemoryBuffer*>(zRequest->getThis()),
  53. center,
  54. zSource->getId()});
  55. Game::INSTANCE->requestArea({center.x - CHUNK_SIZE / 2,
  56. center.y - CHUNK_SIZE / 2,
  57. center.x + CHUNK_SIZE / 2 - 1,
  58. center.y + CHUNK_SIZE / 2 - 1,
  59. dimensionId});
  60. }
  61. else
  62. {
  63. cC->api(zRequest, zSource, laterHandler);
  64. }
  65. cs.unlock();
  66. break;
  67. }
  68. case 1: // block message
  69. Vec3<int> location;
  70. zRequest->lese((char*)&location.x, 4);
  71. zRequest->lese((char*)&location.y, 4);
  72. zRequest->lese((char*)&location.z, 4);
  73. Framework::Either<Block*, int> block = zBlock(location);
  74. if (block.isA())
  75. {
  76. block.getA()->api(zRequest, zResponse);
  77. }
  78. break;
  79. }
  80. }
  81. void Dimension::tickEntities()
  82. {
  83. for (auto entity : *entities)
  84. {
  85. if (!entity->isRemoved()
  86. && (entity->isMoving()
  87. || zChunk(Punkt((int)entity->getPosition().x,
  88. (int)entity->getPosition().y))))
  89. entity->prepareTick(this);
  90. }
  91. int index = 0;
  92. for (auto entity : *entities)
  93. {
  94. if (!entity->isRemoved()
  95. && (entity->isMoving()
  96. || zChunk(Punkt((int)entity->getPosition().x,
  97. (int)entity->getPosition().y))))
  98. entity->tick(this);
  99. index++;
  100. }
  101. }
  102. void Dimension::thread()
  103. {
  104. // light calculation
  105. int index = 0;
  106. ZeitMesser messer;
  107. messer.messungStart();
  108. double time = 0;
  109. bool isForeground = 0;
  110. Framework::Array<Framework::Vec3<int>> internalLightUpdateQueue;
  111. while (!stop)
  112. {
  113. Vec3<int> position;
  114. if (internalLightUpdateQueue.getEintragAnzahl())
  115. {
  116. position = internalLightUpdateQueue.get(0);
  117. internalLightUpdateQueue.remove(0);
  118. }
  119. else
  120. {
  121. removedChunksCs.lock();
  122. if (removedChunks.getEintragAnzahl() > 0)
  123. {
  124. Chunk* removedChunk = removedChunks.z(0);
  125. removedChunksCs.unlock();
  126. Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/"
  127. + getDimensionId() + "/";
  128. filePath.appendHex(removedChunk->getCenter().x);
  129. filePath += "_";
  130. filePath.appendHex(removedChunk->getCenter().y);
  131. filePath += ".chunk";
  132. Datei d;
  133. d.setDatei(filePath);
  134. d.erstellen();
  135. d.open(Datei::Style::schreiben);
  136. removedChunk->save(&d);
  137. d.close();
  138. removedChunksCs.lock();
  139. removedChunks.remove(0);
  140. }
  141. removedChunksCs.unlock();
  142. if (priorizedLightUpdateQueue.getEintragAnzahl())
  143. {
  144. prioLightCs.lock();
  145. position = priorizedLightUpdateQueue.get(0);
  146. priorizedLightUpdateQueue.remove(0);
  147. prioLightCs.unlock();
  148. isForeground = 1;
  149. }
  150. else
  151. {
  152. if (!lightUpdateQueue.getEintragAnzahl())
  153. {
  154. messer.messungEnde();
  155. time += messer.getSekunden();
  156. Sleep(500);
  157. messer.messungStart();
  158. continue;
  159. }
  160. lightCs.lock();
  161. position = lightUpdateQueue.get(0);
  162. lightUpdateQueue.remove(0);
  163. lightCs.unlock();
  164. isForeground = 0;
  165. }
  166. }
  167. Chunk* chunk
  168. = zChunk(Game::INSTANCE->getChunkCenter(position.x, position.y));
  169. if (position.z >= 0 && position.z < WORLD_HEIGHT)
  170. {
  171. if (chunk)
  172. {
  173. int x = position.x % CHUNK_SIZE;
  174. int y = position.y % CHUNK_SIZE;
  175. if (x < 0) x += CHUNK_SIZE;
  176. if (y < 0) y += CHUNK_SIZE;
  177. unsigned char* light
  178. = chunk->getLightData(Vec3<int>(x, y, position.z));
  179. unsigned char dayLight[6] = {255, 255, 255, 0, 0, 0};
  180. unsigned char noLight[6] = {0, 0, 0, 0, 0, 0};
  181. unsigned char newLight[6] = {0, 0, 0, 0, 0, 0};
  182. // add neighbor light emission
  183. for (int i = 0; i < 6; i++)
  184. {
  185. unsigned char* neighborLeight;
  186. Vec3<int> neighborPos
  187. = position + getDirection(getDirectionFromIndex(i));
  188. if (neighborPos.z < 0)
  189. {
  190. neighborLeight = noLight;
  191. }
  192. else if (neighborPos.z >= WORLD_HEIGHT)
  193. {
  194. neighborLeight = dayLight;
  195. }
  196. else
  197. {
  198. Chunk* neighborChunk
  199. = zChunk(Game::INSTANCE->getChunkCenter(
  200. neighborPos.x, neighborPos.y));
  201. int x = neighborPos.x % CHUNK_SIZE;
  202. int y = neighborPos.y % CHUNK_SIZE;
  203. if (x < 0) x += CHUNK_SIZE;
  204. if (y < 0) y += CHUNK_SIZE;
  205. if (neighborChunk)
  206. neighborLeight = neighborChunk->getLightData(
  207. Vec3<int>(x, y, neighborPos.z));
  208. else
  209. neighborLeight = noLight;
  210. }
  211. for (int j = 0; j < 3; j++)
  212. newLight[j] = (unsigned char)MAX(newLight[j],
  213. i == getDirectionIndex(TOP)
  214. ? neighborLeight[j]
  215. : (unsigned char)((float)neighborLeight[j]
  216. * 0.8f));
  217. for (int j = 3; j < 6; j++)
  218. newLight[j] = (unsigned char)MAX(newLight[j],
  219. (unsigned char)((float)neighborLeight[j] * 0.85f));
  220. }
  221. const Block* current = zBlockOrDefault(position);
  222. if (!current) current = &NoBlock::INSTANCE;
  223. // add own light emission
  224. for (int j = 3; j < 6; j++)
  225. newLight[j] = (unsigned char)MAX(
  226. newLight[j], current->getLightEmisionColor()[j - 3]);
  227. current->filterPassingLight(newLight);
  228. current->filterPassingLight(newLight + 3);
  229. for (int i = 0; i < 6; i++)
  230. {
  231. if (newLight[i] != light[i])
  232. {
  233. chunk->setLightData(Vec3<int>(x, y, position.z),
  234. newLight,
  235. isForeground);
  236. for (int j = 0; j < 6; j++)
  237. internalLightUpdateQueue.add(
  238. position
  239. + getDirection(getDirectionFromIndex(j)),
  240. 0);
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. index++;
  247. if (index > 100000)
  248. {
  249. messer.messungEnde();
  250. time += messer.getSekunden();
  251. std::cout << "100000 light updates needed " << time << " seconds\n";
  252. time = 0;
  253. index = 0;
  254. Sleep(250);
  255. messer.messungStart();
  256. }
  257. }
  258. std::cout << Text("Dimension ") + this->getDimensionId()
  259. + " update Thread exited.\n";
  260. }
  261. void Dimension::getAddrOf(Punkt cPos, char* addr) const
  262. {
  263. *(int*)addr = cPos.x;
  264. *((int*)addr + 1) = cPos.y;
  265. }
  266. void Dimension::getAddrOfWorld(Punkt wPos, char* addr) const
  267. {
  268. if (wPos.x < 0) wPos.x -= CHUNK_SIZE;
  269. if (wPos.y < 0) // needed because otherwise would (-8, -8) have the same
  270. // adress as (8, 8)
  271. wPos.y -= CHUNK_SIZE;
  272. wPos /= CHUNK_SIZE;
  273. getAddrOf(wPos, addr);
  274. }
  275. Chunk* Dimension::zChunk(Punkt wPos) const
  276. {
  277. char addr[8];
  278. getAddrOfWorld(wPos, addr);
  279. return chunks->z(addr, 8);
  280. }
  281. Framework::Either<Block*, int> Dimension::zBlock(Vec3<int> location)
  282. {
  283. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  284. if (c)
  285. {
  286. int x = location.x % CHUNK_SIZE;
  287. int y = location.y % CHUNK_SIZE;
  288. if (x < 0) x += CHUNK_SIZE;
  289. if (y < 0) y += CHUNK_SIZE;
  290. return c->zBlockAt(Vec3<int>(x, y, location.z));
  291. }
  292. return 0;
  293. }
  294. Block* Dimension::zRealBlockInstance(Framework::Vec3<int> location)
  295. {
  296. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  297. if (c)
  298. {
  299. int x = location.x % CHUNK_SIZE;
  300. int y = location.y % CHUNK_SIZE;
  301. if (x < 0) x += CHUNK_SIZE;
  302. if (y < 0) y += CHUNK_SIZE;
  303. c->instantiateBlock(Vec3<int>(x, y, location.z));
  304. auto result = c->zBlockAt(Vec3<int>(x, y, location.z));
  305. return result.isA() ? result.getA() : 0;
  306. }
  307. return 0;
  308. }
  309. const Block* Dimension::zBlockOrDefault(Framework::Vec3<int> location)
  310. {
  311. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  312. if (c)
  313. {
  314. int x = location.x % CHUNK_SIZE;
  315. int y = location.y % CHUNK_SIZE;
  316. if (x < 0) x += CHUNK_SIZE;
  317. if (y < 0) y += CHUNK_SIZE;
  318. return c->zBlockConst(Vec3<int>(x, y, location.z));
  319. }
  320. return 0;
  321. }
  322. void Dimension::placeBlock(
  323. Framework::Vec3<int> location, Framework::Either<Block*, int> block)
  324. {
  325. Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
  326. if (c)
  327. {
  328. int x = location.x % CHUNK_SIZE;
  329. int y = location.y % CHUNK_SIZE;
  330. if (x < 0) x += CHUNK_SIZE;
  331. if (y < 0) y += CHUNK_SIZE;
  332. if (block.isA())
  333. c->putBlockAt(Vec3<int>(x, y, location.z), block);
  334. else
  335. {
  336. c->putBlockAt(Vec3<int>(x, y, location.z), 0);
  337. c->putBlockTypeAt(Vec3<int>(x, y, location.z), block);
  338. }
  339. }
  340. else if (block.isA())
  341. block.getA()->release();
  342. }
  343. void Dimension::sendBlockInfo(Framework::Vec3<int> location)
  344. {
  345. Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
  346. if (c)
  347. {
  348. int x = location.x % CHUNK_SIZE;
  349. int y = location.y % CHUNK_SIZE;
  350. if (x < 0) x += CHUNK_SIZE;
  351. if (y < 0) y += CHUNK_SIZE;
  352. c->sendBlockInfo(Vec3<int>(x, y, location.z));
  353. }
  354. }
  355. void Dimension::addEntity(Entity* entity)
  356. {
  357. entities->add(entity);
  358. }
  359. void Dimension::setChunk(Chunk* chunk, Punkt center)
  360. {
  361. chunkCs.lock();
  362. char addr[8];
  363. getAddrOfWorld(center, addr);
  364. Chunk* old = chunks->get(addr, 8);
  365. if (old)
  366. {
  367. Game::INSTANCE->zTickOrganizer()->removeTickSource(old);
  368. old->prepareRemove();
  369. for (int i = 0; i < chunkList.getEintragAnzahl(); i++)
  370. {
  371. if (chunkList.get(i) == old)
  372. {
  373. chunkList.remove(i);
  374. break;
  375. }
  376. }
  377. }
  378. chunks->set(addr, 8, chunk);
  379. if (chunk)
  380. {
  381. chunkList.add(chunk);
  382. chunk->setAdded();
  383. }
  384. getAddrOfWorld(center + Punkt(CHUNK_SIZE, 0), addr);
  385. Chunk* zChunk = chunks->z(addr, 8);
  386. if (zChunk)
  387. {
  388. zChunk->setNeighbor(WEST, chunk);
  389. if (chunk)
  390. {
  391. chunk->setNeighbor(EAST, zChunk);
  392. }
  393. }
  394. getAddrOfWorld(center + Punkt(-CHUNK_SIZE, 0), addr);
  395. zChunk = chunks->z(addr, 8);
  396. if (zChunk)
  397. {
  398. zChunk->setNeighbor(EAST, chunk);
  399. if (chunk) chunk->setNeighbor(WEST, zChunk);
  400. }
  401. getAddrOfWorld(center + Punkt(0, CHUNK_SIZE), addr);
  402. zChunk = chunks->z(addr, 8);
  403. if (zChunk)
  404. {
  405. zChunk->setNeighbor(NORTH, chunk);
  406. if (chunk) chunk->setNeighbor(SOUTH, zChunk);
  407. }
  408. getAddrOfWorld(center + Punkt(0, -CHUNK_SIZE), addr);
  409. zChunk = chunks->z(addr, 8);
  410. if (zChunk)
  411. {
  412. zChunk->setNeighbor(SOUTH, chunk);
  413. if (chunk) chunk->setNeighbor(NORTH, zChunk);
  414. }
  415. DoLaterHandler laterHandler;
  416. if (chunk)
  417. {
  418. cs.lock();
  419. int index = 0;
  420. for (Iterator<RequestQueue> iterator = waitingRequests.begin();
  421. iterator;)
  422. {
  423. Entity* zE = Game::INSTANCE->zEntity(iterator.val().sourceId);
  424. if (zE)
  425. {
  426. if (iterator.val().chunkCenter == chunk->getCenter())
  427. {
  428. chunk->api(iterator.val().request, zE, laterHandler);
  429. iterator.val().request->release();
  430. iterator.remove();
  431. continue;
  432. }
  433. }
  434. else
  435. {
  436. iterator.val().request->release();
  437. iterator.remove();
  438. continue;
  439. }
  440. iterator++;
  441. index++;
  442. }
  443. cs.unlock();
  444. Game::INSTANCE->zTickOrganizer()->addTickSource(chunk);
  445. }
  446. chunkCs.unlock();
  447. if (old)
  448. {
  449. old->onUnloaded();
  450. removedChunksCs.lock();
  451. removedChunks.add(old);
  452. removedChunksCs.unlock();
  453. }
  454. if (chunk) chunk->onLoaded();
  455. updateLightAtChunkBorders(center);
  456. }
  457. void Dimension::save(Text worldDir) const
  458. {
  459. Datei d;
  460. d.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  461. + "/nst.id");
  462. d.erstellen();
  463. d.open(Datei::Style::schreiben);
  464. d.schreibe((char*)&nextStructureId, 8);
  465. d.close();
  466. for (auto chunk = chunkList.begin(); chunk; chunk++)
  467. {
  468. if (!chunk._) continue;
  469. Datei* file = new Datei();
  470. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  471. filePath.appendHex(chunk->getCenter().x);
  472. filePath += "_";
  473. filePath.appendHex(chunk->getCenter().y);
  474. filePath += ".chunk";
  475. file->setDatei(filePath);
  476. if (file->open(Datei::Style::schreiben)) chunk->save(file);
  477. file->close();
  478. file->release();
  479. }
  480. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  481. Datei* file = new Datei();
  482. file->setDatei(filePath);
  483. if (file->open(Datei::Style::schreiben))
  484. {
  485. for (Entity* entity : *entities)
  486. {
  487. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  488. {
  489. if (!entity->isRemoved())
  490. {
  491. int type = entity->zType()->getId();
  492. file->schreibe((char*)&type, 4);
  493. StaticRegistry<EntityType>::INSTANCE.zElement(type)
  494. ->saveEntity(entity, file);
  495. }
  496. }
  497. else
  498. {
  499. Datei pFile;
  500. pFile.setDatei(
  501. worldDir + "/player/" + Game::INSTANCE->getPlayerId(((Player*)entity)->getName()));
  502. if (pFile.open(Datei::Style::schreiben))
  503. StaticRegistry<EntityType>::INSTANCE
  504. .zElement(EntityTypeEnum::PLAYER)
  505. ->saveEntity(entity, &pFile);
  506. }
  507. }
  508. file->close();
  509. }
  510. }
  511. int Dimension::getDimensionId() const
  512. {
  513. return dimensionId;
  514. }
  515. bool Dimension::hasChunck(int x, int y)
  516. {
  517. if (zChunk(Punkt(x, y))) return 1;
  518. removedChunksCs.lock();
  519. for (Chunk* c : removedChunks)
  520. {
  521. if (c->getCenter().x == x && c->getCenter().y == y)
  522. {
  523. removedChunksCs.unlock();
  524. return 1;
  525. }
  526. }
  527. removedChunksCs.unlock();
  528. return 0;
  529. }
  530. bool Dimension::reviveChunk(int x, int y)
  531. {
  532. chunkCs.lock();
  533. if (zChunk(Punkt(x, y)))
  534. {
  535. chunkCs.unlock();
  536. return 1;
  537. }
  538. removedChunksCs.lock();
  539. int index = 0;
  540. for (Iterator<Chunk*> i = removedChunks.begin(); i; i++)
  541. {
  542. if (i->getCenter().x == x && i->getCenter().y == y)
  543. {
  544. setChunk(dynamic_cast<Chunk*>(i->getThis()), Punkt(x, y));
  545. if (index > 0) i.remove();
  546. removedChunksCs.unlock();
  547. chunkCs.unlock();
  548. return 1;
  549. }
  550. index++;
  551. }
  552. removedChunksCs.unlock();
  553. chunkCs.unlock();
  554. return 0;
  555. }
  556. float Dimension::getGravity() const
  557. {
  558. return gravity;
  559. }
  560. void Dimension::removeOldChunks()
  561. {
  562. chunkCs.lock();
  563. int index = 0;
  564. for (Chunk* chunk : chunkList)
  565. {
  566. if (!chunk->hasObservers()) setChunk(0, chunk->getCenter());
  567. index++;
  568. }
  569. chunkCs.unlock();
  570. structurCs.lock();
  571. Iterator<MultiblockStructure*> i = structures.begin();
  572. while (i)
  573. {
  574. if (i->isEmpty())
  575. i.remove();
  576. else if (i->isFullyUnloaded())
  577. {
  578. Datei d;
  579. Text path = Game::INSTANCE->getWorldDirectory() + "/dim/"
  580. + Text(dimensionId) + "/structures/";
  581. path.appendHex(i->getStructureId());
  582. path += ".str";
  583. d.setDatei(path);
  584. d.erstellen();
  585. d.open(Datei::Style::schreiben);
  586. auto uPos = i->getUniquePosition();
  587. d.schreibe((char*)&uPos.x, 4);
  588. d.schreibe((char*)&uPos.y, 4);
  589. d.schreibe((char*)&uPos.z, 4);
  590. int typeId = i->getStructureTypeId();
  591. d.schreibe((char*)&typeId, 4);
  592. __int64 strId = i->getStructureId();
  593. d.schreibe((char*)&strId, 8);
  594. StaticRegistry<MultiblockStructureType>::INSTANCE
  595. .zElement(i->getStructureTypeId())
  596. ->saveStructure(i.val(), &d);
  597. d.close();
  598. i.remove();
  599. }
  600. i++;
  601. }
  602. structurCs.unlock();
  603. }
  604. Entity* Dimension::zEntity(int id)
  605. {
  606. for (auto entity : *entities)
  607. {
  608. if (!entity->isRemoved() && entity->getId() == id) return entity;
  609. }
  610. return 0;
  611. }
  612. Entity* Dimension::zNearestEntity(
  613. Framework::Vec3<float> pos, std::function<bool(Entity*)> filter)
  614. {
  615. Entity* result = 0;
  616. float sqDist = 0;
  617. for (auto entity : *entities)
  618. {
  619. if (!entity->isRemoved() && filter(entity))
  620. {
  621. float d = pos.abstandSq(entity->getPosition());
  622. if (!result || d < sqDist)
  623. {
  624. result = entity;
  625. sqDist = d;
  626. }
  627. }
  628. }
  629. return result;
  630. }
  631. void Dimension::removeEntity(int id)
  632. {
  633. int index = 0;
  634. for (auto entity : *entities)
  635. {
  636. if (entity->getId() == id)
  637. {
  638. entities->remove(index);
  639. return;
  640. }
  641. index++;
  642. }
  643. }
  644. void Dimension::removeSubscriptions(Entity* zEntity)
  645. {
  646. for (Chunk* chunk : chunkList)
  647. chunk->removeObserver(zEntity);
  648. }
  649. void Dimension::updateLightning(Vec3<int> location)
  650. {
  651. lightCs.lock();
  652. lightUpdateQueue.add(location, 0);
  653. lightCs.unlock();
  654. }
  655. void Dimension::updateLightningWithoutWait(Framework::Vec3<int> location)
  656. {
  657. prioLightCs.lock();
  658. priorizedLightUpdateQueue.add(location, 0);
  659. prioLightCs.unlock();
  660. }
  661. void Dimension::updateLightAtChunkBorders(Punkt chunkCenter)
  662. {
  663. if (lightUpdateQueue.getEintragAnzahl() > 300000)
  664. {
  665. std::cout
  666. << "warning: light calculation queue is over 300000 blocks long";
  667. }
  668. for (int i = WORLD_HEIGHT - 1; i >= 0; i--)
  669. {
  670. for (int j = 0; j < CHUNK_SIZE; j++)
  671. {
  672. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 - 1,
  673. chunkCenter.y - CHUNK_SIZE / 2 + j,
  674. i));
  675. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2,
  676. chunkCenter.y - CHUNK_SIZE / 2 + j,
  677. i));
  678. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2 - 1,
  679. chunkCenter.y - CHUNK_SIZE / 2 + j,
  680. i));
  681. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2,
  682. chunkCenter.y - CHUNK_SIZE / 2 + j,
  683. i));
  684. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  685. chunkCenter.y - CHUNK_SIZE / 2 - 1,
  686. i));
  687. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  688. chunkCenter.y - CHUNK_SIZE / 2,
  689. i));
  690. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  691. chunkCenter.y + CHUNK_SIZE / 2 - 1,
  692. i));
  693. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  694. chunkCenter.y + CHUNK_SIZE / 2,
  695. i));
  696. }
  697. }
  698. }
  699. __int64 Dimension::getNextStructureId()
  700. {
  701. return nextStructureId++;
  702. }
  703. void Dimension::addStructure(MultiblockStructure* structure)
  704. {
  705. structurCs.lock();
  706. structures.add(structure);
  707. structurCs.unlock();
  708. }
  709. MultiblockStructure* Dimension::zStructureByPosition(
  710. Framework::Vec3<int> uniquePosition)
  711. {
  712. structurCs.lock();
  713. for (MultiblockStructure* str : structures)
  714. {
  715. if (str->getUniquePosition() == uniquePosition)
  716. {
  717. structurCs.unlock();
  718. return str;
  719. }
  720. }
  721. // search for structure file
  722. Datei dir(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  723. + "/structures");
  724. RCArray<Text>* names = dir.getDateiListe();
  725. if (names)
  726. {
  727. Vec3<int> uPos;
  728. for (Text* name : *names)
  729. {
  730. Datei d(Text(dir.zPfad()->getText()) + "/" + name->getText());
  731. if (d.open(Datei::Style::lesen))
  732. {
  733. d.lese((char*)&uPos.x, 4);
  734. d.lese((char*)&uPos.y, 4);
  735. d.lese((char*)&uPos.z, 4);
  736. if (uPos == uniquePosition)
  737. {
  738. int type;
  739. d.lese((char*)&type, 4);
  740. __int64 strId;
  741. d.lese((char*)&strId, 8);
  742. MultiblockStructure* str
  743. = StaticRegistry<MultiblockStructureType>::INSTANCE
  744. .zElement(type)
  745. ->loadStructure(
  746. dimensionId, strId, uniquePosition, &d);
  747. d.close();
  748. structures.add(str);
  749. names->release();
  750. structurCs.unlock();
  751. return str;
  752. }
  753. d.close();
  754. }
  755. }
  756. names->release();
  757. }
  758. structurCs.unlock();
  759. return 0;
  760. }
  761. MultiblockStructure* Dimension::zStructureById(__int64 id)
  762. {
  763. structurCs.lock();
  764. for (MultiblockStructure* str : structures)
  765. {
  766. if (str->getStructureId() == id)
  767. {
  768. structurCs.unlock();
  769. return str;
  770. }
  771. }
  772. // search for structure file
  773. Text path = Game::INSTANCE->getWorldDirectory() + "/dim/"
  774. + Text(dimensionId) + "/structures/";
  775. path.appendHex(id);
  776. path += ".str";
  777. Datei d(path);
  778. Vec3<int> uPos;
  779. if (d.open(Datei::Style::lesen))
  780. {
  781. d.lese((char*)&uPos.x, 4);
  782. d.lese((char*)&uPos.y, 4);
  783. d.lese((char*)&uPos.z, 4);
  784. int type;
  785. d.lese((char*)&type, 4);
  786. __int64 strId;
  787. d.lese((char*)&strId, 8);
  788. MultiblockStructure* str
  789. = StaticRegistry<MultiblockStructureType>::INSTANCE.zElement(type)
  790. ->loadStructure(dimensionId, strId, uPos, &d);
  791. d.close();
  792. structures.add(str);
  793. structurCs.unlock();
  794. return str;
  795. }
  796. std::cout << "WARNING: did not find Structure information file '" << path << "'.";
  797. structurCs.unlock();
  798. return 0;
  799. }
  800. void Dimension::requestStopAndWait()
  801. {
  802. stop = 1;
  803. warteAufThread(1000000);
  804. }