Dimension.cpp 25 KB

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