Dimension.cpp 30 KB

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