Dimension.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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)
  493. {
  494. updateLightAtChunkBorders(center);
  495. chunk->updateLightSources();
  496. }
  497. }
  498. void Dimension::save(Text worldDir) const
  499. {
  500. Datei d;
  501. d.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  502. + "/meta.dim");
  503. d.erstellen();
  504. d.open(Datei::Style::schreiben);
  505. d.schreibe((char*)&nextStructureId, 8);
  506. d.schreibe((char*)&currentDayTime, 8);
  507. d.close();
  508. for (auto chunk = chunkList.begin(); chunk; chunk++)
  509. {
  510. if (!chunk._) continue;
  511. Datei* file = new Datei();
  512. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  513. filePath.appendHex(chunk->getCenter().x);
  514. filePath += "_";
  515. filePath.appendHex(chunk->getCenter().y);
  516. filePath += ".chunk";
  517. file->setDatei(filePath);
  518. if (file->open(Datei::Style::schreiben)) chunk->save(file);
  519. file->close();
  520. file->release();
  521. char addr[8];
  522. getAddrOfWorld(chunk->getCenter(), addr);
  523. map->saveMap(addr, 8);
  524. }
  525. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  526. Datei* file = new Datei();
  527. file->setDatei(filePath);
  528. if (file->open(Datei::Style::schreiben))
  529. {
  530. for (Entity* entity : *entities)
  531. {
  532. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  533. {
  534. if (!entity->isRemoved())
  535. {
  536. int type = entity->zType()->getId();
  537. file->schreibe((char*)&type, 4);
  538. Game::INSTANCE->zEntityType(type)->saveEntity(entity, file);
  539. }
  540. }
  541. else
  542. {
  543. Datei pFile;
  544. pFile.setDatei(worldDir + "/player/"
  545. + Game::INSTANCE->getPlayerId(
  546. ((Player*)entity)->getName()));
  547. if (pFile.open(Datei::Style::schreiben))
  548. Game::INSTANCE->zEntityType(EntityTypeEnum::PLAYER)
  549. ->saveEntity(entity, &pFile);
  550. }
  551. }
  552. file->close();
  553. }
  554. for (MultiblockStructure* structure : structures)
  555. {
  556. saveStructure(structure);
  557. }
  558. }
  559. int Dimension::getDimensionId() const
  560. {
  561. return dimensionId;
  562. }
  563. bool Dimension::hasChunck(int x, int y)
  564. {
  565. if (zChunk(Punkt(x, y))) return 1;
  566. removedChunksCs.lock();
  567. for (Chunk* c : removedChunks)
  568. {
  569. if (c->getCenter().x == x && c->getCenter().y == y)
  570. {
  571. removedChunksCs.unlock();
  572. return 1;
  573. }
  574. }
  575. removedChunksCs.unlock();
  576. return 0;
  577. }
  578. bool Dimension::reviveChunk(int x, int y)
  579. {
  580. chunkCs.lock();
  581. if (zChunk(Punkt(x, y)))
  582. {
  583. chunkCs.unlock();
  584. return 1;
  585. }
  586. removedChunksCs.lock();
  587. int index = 0;
  588. for (ArrayIterator<Chunk*> i = removedChunks.begin(); i; i++)
  589. {
  590. if (i->getCenter().x == x && i->getCenter().y == y)
  591. {
  592. setChunk(dynamic_cast<Chunk*>(i->getThis()), Punkt(x, y));
  593. if (index > 0) i.remove();
  594. removedChunksCs.unlock();
  595. chunkCs.unlock();
  596. return 1;
  597. }
  598. index++;
  599. }
  600. removedChunksCs.unlock();
  601. chunkCs.unlock();
  602. return 0;
  603. }
  604. float Dimension::getGravity() const
  605. {
  606. return gravity;
  607. }
  608. void Dimension::removeOldChunks()
  609. {
  610. chunkCs.lock();
  611. int index = 0;
  612. for (Chunk* chunk : chunkList)
  613. {
  614. if (!chunk->hasObservers()) setChunk(0, chunk->getCenter());
  615. index++;
  616. }
  617. chunkCs.unlock();
  618. structurCs.lock();
  619. ArrayIterator<MultiblockStructure*> i = structures.begin();
  620. while (i)
  621. {
  622. if (i->isEmpty())
  623. {
  624. i.remove();
  625. continue;
  626. }
  627. else if (i->isFullyUnloaded())
  628. {
  629. saveStructure(i);
  630. i.remove();
  631. continue;
  632. }
  633. i++;
  634. }
  635. structurCs.unlock();
  636. }
  637. Entity* Dimension::zEntity(int id)
  638. {
  639. for (auto entity : *entities)
  640. {
  641. if (!entity->isRemoved() && entity->getId() == id) return entity;
  642. }
  643. return 0;
  644. }
  645. Entity* Dimension::zNearestEntity(
  646. Framework::Vec3<float> pos, std::function<bool(Entity*)> filter)
  647. {
  648. Entity* result = 0;
  649. float sqDist = 0;
  650. for (auto entity : *entities)
  651. {
  652. if (!entity->isRemoved() && filter(entity))
  653. {
  654. float d = pos.abstandSq(entity->getPosition());
  655. if (!result || d < sqDist)
  656. {
  657. result = entity;
  658. sqDist = d;
  659. }
  660. }
  661. }
  662. return result;
  663. }
  664. void Dimension::removeEntity(int id)
  665. {
  666. int index = 0;
  667. for (auto entity : *entities)
  668. {
  669. if (entity->getId() == id)
  670. {
  671. entities->remove(index);
  672. return;
  673. }
  674. index++;
  675. }
  676. }
  677. void Dimension::removeSubscriptions(Entity* zEntity)
  678. {
  679. for (Chunk* chunk : chunkList)
  680. chunk->removeObserver(zEntity);
  681. }
  682. void Dimension::updateLightning(Vec3<int> location)
  683. {
  684. lightCs.lock();
  685. lightUpdateQueue.add(location, 0);
  686. lightCs.unlock();
  687. }
  688. void Dimension::updateLightningWithoutWait(Framework::Vec3<int> location)
  689. {
  690. prioLightCs.lock();
  691. priorizedLightUpdateQueue.add(location, 0);
  692. prioLightCs.unlock();
  693. }
  694. void Dimension::updateLightAtChunkBorders(Punkt chunkCenter)
  695. {
  696. if (lightUpdateQueue.getEintragAnzahl() > 300000)
  697. {
  698. Logging::warning()
  699. << "light calculation queue is over 300000 blocks long";
  700. }
  701. bool xn = zChunk(chunkCenter - Punkt(CHUNK_SIZE, 0)) != 0;
  702. bool xp = zChunk(chunkCenter + Punkt(CHUNK_SIZE, 0)) != 0;
  703. bool yn = zChunk(chunkCenter - Punkt(0, CHUNK_SIZE)) != 0;
  704. bool yp = zChunk(chunkCenter + Punkt(0, CHUNK_SIZE)) != 0;
  705. for (int i = WORLD_HEIGHT - 1; i >= 0; i--)
  706. {
  707. for (int j = 0; j < CHUNK_SIZE; j++)
  708. {
  709. if (xn)
  710. {
  711. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 - 1,
  712. chunkCenter.y - CHUNK_SIZE / 2 + j,
  713. i));
  714. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2,
  715. chunkCenter.y - CHUNK_SIZE / 2 + j,
  716. i));
  717. }
  718. if (xp)
  719. {
  720. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2 - 1,
  721. chunkCenter.y - CHUNK_SIZE / 2 + j,
  722. i));
  723. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2,
  724. chunkCenter.y - CHUNK_SIZE / 2 + j,
  725. i));
  726. }
  727. if (yn)
  728. {
  729. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  730. chunkCenter.y - CHUNK_SIZE / 2 - 1,
  731. i));
  732. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  733. chunkCenter.y - CHUNK_SIZE / 2,
  734. i));
  735. }
  736. if (yp)
  737. {
  738. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  739. chunkCenter.y + CHUNK_SIZE / 2 - 1,
  740. i));
  741. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  742. chunkCenter.y + CHUNK_SIZE / 2,
  743. i));
  744. }
  745. }
  746. }
  747. }
  748. __int64 Dimension::getNextStructureId()
  749. {
  750. return nextStructureId++;
  751. }
  752. void Dimension::addStructure(MultiblockStructure* structure)
  753. {
  754. structurCs.lock();
  755. structures.add(structure);
  756. structurCs.unlock();
  757. }
  758. MultiblockStructure* Dimension::zStructureByPosition(
  759. Framework::Vec3<int> uniquePosition)
  760. {
  761. __int64 id = structureManager->getStructureId(uniquePosition);
  762. if (id > 0)
  763. {
  764. return zStructureById(id);
  765. }
  766. return 0;
  767. }
  768. MultiblockStructure* Dimension::zStructureById(__int64 id)
  769. {
  770. structurCs.lock();
  771. for (MultiblockStructure* str : structures)
  772. {
  773. if (str->getStructureId() == id)
  774. {
  775. structurCs.unlock();
  776. return str;
  777. }
  778. }
  779. MultiblockStructure* structure = structureManager->loadStructure(id);
  780. if (structure)
  781. {
  782. structures.add(structure);
  783. structurCs.unlock();
  784. return structure;
  785. }
  786. Logging::warning() << "did not find Structure information file '"
  787. << std::hex << id << "'.";
  788. structurCs.unlock();
  789. return 0;
  790. }
  791. void Dimension::requestStopAndWait()
  792. {
  793. stop = 1;
  794. warteAufThread(1000000);
  795. }
  796. void Dimension::updateMap(int x, int y, int height)
  797. {
  798. chunkCs.lock();
  799. int h1 = height % 2 == 0 ? height : height - 1;
  800. int h2 = h1 + 1;
  801. const Block* b1 = zBlockOrDefault({x, y, h1});
  802. const Block* b2 = zBlockOrDefault({x, y, h2});
  803. bool visible = 1;
  804. if (h2 != WORLD_HEIGHT - 1)
  805. {
  806. const Block* b3 = zBlockOrDefault({x, y, h2 + 1});
  807. visible = b3->isPassable() || b3->isTransparent();
  808. }
  809. int color1
  810. = (b2->isPassable() || b2->isTransparent()) ? b1->getMapColor() : 0;
  811. int color2 = visible ? b2->getMapColor() : 0;
  812. int color1m = 0;
  813. int color2m = 0;
  814. if (h1 > 0)
  815. {
  816. const Block* b1m = zBlockOrDefault({x, y, h1 - 2});
  817. const Block* b2m = zBlockOrDefault({x, y, h1 - 1});
  818. color1m = (b2m->isPassable() || b2m->isTransparent())
  819. ? b1m->getMapColor()
  820. : 0;
  821. color2m = (b1->isPassable() || b1->isTransparent()) ? b2m->getMapColor()
  822. : 0;
  823. }
  824. char addr[8];
  825. Punkt center = Game::INSTANCE->getChunkCenter(x, y);
  826. getAddrOfWorld(center, addr);
  827. ChunkMap* cMap = map->getMap(addr, 8, center);
  828. if (cMap)
  829. {
  830. Framework::Vec3<int> chunkLocation = chunkCoordinates({x, y, height});
  831. if (cMap->update((char)chunkLocation.x,
  832. (char)chunkLocation.y,
  833. (unsigned char)(chunkLocation.z / 2),
  834. color1,
  835. color2)
  836. || (h1 > 0
  837. && cMap->update((char)chunkLocation.x,
  838. (char)chunkLocation.y,
  839. (unsigned char)(chunkLocation.z / 2 - 1),
  840. color1m,
  841. color2m)))
  842. {
  843. map->onMapUpdated(addr, 8);
  844. }
  845. }
  846. chunkCs.unlock();
  847. }
  848. int Dimension::getChunkCount()
  849. {
  850. return chunkList.getEintragAnzahl();
  851. }
  852. double Dimension::getCurrentDayTime() const
  853. {
  854. return currentDayTime;
  855. }
  856. double Dimension::getNightDuration() const
  857. {
  858. return nightDuration;
  859. }
  860. double Dimension::getNightTransitionDuration() const
  861. {
  862. return nightTransitionDuration;
  863. }
  864. double Dimension::getDayDuration() const
  865. {
  866. return dayDuration;
  867. }