Dimension.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. double ausgleich = 0.0;
  320. zm.messungStart();
  321. Sleep(16);
  322. while (!stop)
  323. {
  324. zm.messungEnde();
  325. double seconds = zm.getSekunden();
  326. zm.messungStart();
  327. entityCs.lock();
  328. chunkCs.lock();
  329. auto iterator = entities->begin();
  330. auto end = entities->end();
  331. while (iterator != end)
  332. {
  333. if (!iterator->isRemoved())
  334. {
  335. iterator->prepareTick(this, seconds);
  336. iterator++;
  337. }
  338. else
  339. {
  340. auto chunk = zChunk(
  341. Game::getChunkCenter((int)iterator->getPosition().x,
  342. (int)iterator->getPosition().y));
  343. if (chunk)
  344. {
  345. chunk->onEntityLeaves(iterator, 0);
  346. }
  347. iterator.remove();
  348. }
  349. }
  350. int index = 0;
  351. for (auto entity : *entities)
  352. {
  353. if (!entity->isRemoved())
  354. {
  355. entity->tick(this, seconds);
  356. }
  357. index++;
  358. }
  359. chunkCs.unlock();
  360. entityCs.unlock();
  361. ausgleich += 1.0 / 30 - seconds;
  362. if (ausgleich > 0) Sleep((int)(ausgleich * 1000));
  363. }
  364. }
  365. Chunk* Dimension::zChunk(Punkt wPos) const
  366. {
  367. char addr[8];
  368. getAddrOfWorld(wPos, addr);
  369. return chunks->z(addr, 8);
  370. }
  371. Framework::Either<Block*, int> Dimension::zBlock(
  372. Vec3<int> location, OUT Chunk** zChunk) const
  373. {
  374. Chunk* c
  375. = this->zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  376. if (zChunk)
  377. {
  378. *zChunk = c;
  379. }
  380. if (c)
  381. {
  382. location = chunkCoordinates(location);
  383. return c->zBlockAt(location);
  384. }
  385. return 0;
  386. }
  387. Block* Dimension::zRealBlockInstance(Framework::Vec3<int> location) const
  388. {
  389. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  390. if (c)
  391. {
  392. location = chunkCoordinates(location);
  393. c->instantiateBlock(location);
  394. auto result = c->zBlockAt(location);
  395. return result.isA() ? result.getA() : 0;
  396. }
  397. return 0;
  398. }
  399. const Block* Dimension::zBlockOrDefault(Framework::Vec3<int> location) const
  400. {
  401. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  402. if (c)
  403. {
  404. location = chunkCoordinates(location);
  405. return c->zBlockConst(location);
  406. }
  407. return &NoBlock::INSTANCE;
  408. }
  409. int Dimension::getBlockType(Framework::Vec3<int> location) const
  410. {
  411. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  412. if (c)
  413. {
  414. location = chunkCoordinates(location);
  415. return c->getBlockTypeAt(location);
  416. }
  417. return BlockTypeEnum::NO_BLOCK;
  418. }
  419. void Dimension::placeBlock(
  420. Framework::Vec3<int> location, Framework::Either<Block*, int> block)
  421. {
  422. if (block.isB() && block.getB() == BlockTypeEnum::NO_BLOCK) return;
  423. Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
  424. if (c)
  425. {
  426. location = chunkCoordinates(location);
  427. if (block.isA())
  428. c->putBlockAt(location, block);
  429. else
  430. {
  431. c->putBlockAt(location, 0);
  432. c->putBlockTypeAt(location, block);
  433. }
  434. }
  435. else if (block.isA())
  436. block.getA()->release();
  437. }
  438. void Dimension::sendBlockInfo(Framework::Vec3<int> location)
  439. {
  440. Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
  441. if (c)
  442. {
  443. location = chunkCoordinates(location);
  444. c->sendBlockInfo(location);
  445. }
  446. }
  447. void Dimension::addEntity(Entity* entity)
  448. {
  449. entityCs.lock();
  450. entities->add(entity);
  451. entityCs.unlock();
  452. }
  453. void Dimension::setChunk(Chunk* chunk, Punkt center)
  454. {
  455. char addr[8];
  456. getAddrOfWorld(center, addr);
  457. if (chunk) map->loadMap(addr, 8, chunk);
  458. chunkCs.lock();
  459. Chunk* old = chunks->get(addr, 8);
  460. if (old)
  461. {
  462. Game::INSTANCE->zTickOrganizer()->removeTickSource(old);
  463. old->prepareRemove();
  464. for (int i = 0; i < chunkList.getEintragAnzahl(); i++)
  465. {
  466. if (chunkList.get(i) == old)
  467. {
  468. chunkList.remove(i);
  469. break;
  470. }
  471. }
  472. for (Entity* entity : old->getEntitiesInChunk())
  473. {
  474. removeEntity(entity->getId());
  475. }
  476. }
  477. chunks->set(addr, 8, chunk);
  478. if (chunk)
  479. {
  480. chunkList.add(chunk);
  481. }
  482. getAddrOfWorld(center + Punkt(CHUNK_SIZE, 0), addr);
  483. Chunk* zChunk = chunks->z(addr, 8);
  484. if (zChunk)
  485. {
  486. zChunk->setNeighbor(WEST, chunk);
  487. if (chunk)
  488. {
  489. chunk->setNeighbor(EAST, zChunk);
  490. Game::INSTANCE->zGenerator()->postprocessChunk(zChunk);
  491. }
  492. }
  493. getAddrOfWorld(center + Punkt(-CHUNK_SIZE, 0), addr);
  494. zChunk = chunks->z(addr, 8);
  495. if (zChunk)
  496. {
  497. zChunk->setNeighbor(EAST, chunk);
  498. if (chunk)
  499. {
  500. chunk->setNeighbor(WEST, zChunk);
  501. Game::INSTANCE->zGenerator()->postprocessChunk(zChunk);
  502. }
  503. }
  504. getAddrOfWorld(center + Punkt(0, CHUNK_SIZE), addr);
  505. zChunk = chunks->z(addr, 8);
  506. if (zChunk)
  507. {
  508. zChunk->setNeighbor(NORTH, chunk);
  509. if (chunk)
  510. {
  511. chunk->setNeighbor(SOUTH, zChunk);
  512. Game::INSTANCE->zGenerator()->postprocessChunk(zChunk);
  513. }
  514. }
  515. getAddrOfWorld(center + Punkt(0, -CHUNK_SIZE), addr);
  516. zChunk = chunks->z(addr, 8);
  517. if (zChunk)
  518. {
  519. zChunk->setNeighbor(SOUTH, chunk);
  520. if (chunk)
  521. {
  522. chunk->setNeighbor(NORTH, zChunk);
  523. Game::INSTANCE->zGenerator()->postprocessChunk(zChunk);
  524. }
  525. }
  526. DoLaterHandler laterHandler;
  527. if (chunk)
  528. {
  529. chunkCs.unlock();
  530. Game::INSTANCE->zGenerator()->postprocessChunk(chunk);
  531. chunk->setAdded();
  532. for (Entity* entity : chunk->getEntitiesInChunk())
  533. {
  534. addEntity(dynamic_cast<Entity*>(entity->getThis()));
  535. }
  536. cs.lock();
  537. int index = 0;
  538. for (ArrayIterator<RequestQueue> iterator = waitingRequests.begin();
  539. iterator;)
  540. {
  541. Entity* zE = Game::INSTANCE->zEntity(iterator.val().sourceId);
  542. if (zE)
  543. {
  544. if (iterator.val().chunkCenter == chunk->getCenter())
  545. {
  546. chunk->api(iterator.val().request, zE, laterHandler);
  547. iterator.val().request->release();
  548. iterator.remove();
  549. continue;
  550. }
  551. }
  552. else
  553. {
  554. iterator.val().request->release();
  555. iterator.remove();
  556. continue;
  557. }
  558. iterator++;
  559. index++;
  560. }
  561. cs.unlock();
  562. Game::INSTANCE->zTickOrganizer()->addTickSource(chunk);
  563. }
  564. else
  565. {
  566. chunkCs.unlock();
  567. }
  568. if (old)
  569. {
  570. old->onUnloaded();
  571. removedChunksCs.lock();
  572. removedChunks.add(old);
  573. removedChunksCs.unlock();
  574. }
  575. if (chunk) chunk->onLoaded();
  576. laterHandler.execute();
  577. if (chunk)
  578. {
  579. updateLightAtChunkBorders(chunk);
  580. chunk->updateLightSources();
  581. }
  582. }
  583. void Dimension::save(Text worldDir) const
  584. {
  585. Datei d;
  586. d.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  587. + "/meta.dim");
  588. d.erstellen();
  589. d.open(Datei::Style::schreiben);
  590. d.schreibe((char*)&nextStructureId, 8);
  591. d.schreibe((char*)&currentDayTime, 8);
  592. d.close();
  593. Array<Framework::Punkt> otherChunks;
  594. for (auto chunk = chunkList.begin(); chunk; chunk++)
  595. {
  596. if (!chunk._) continue;
  597. Datei* file = new Datei();
  598. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  599. filePath.appendHex(chunk->getCenter().x);
  600. filePath += "_";
  601. filePath.appendHex(chunk->getCenter().y);
  602. filePath += ".chunk";
  603. file->setDatei(filePath);
  604. if (file->open(Datei::Style::schreiben)) chunk->save(file, otherChunks);
  605. file->close();
  606. file->release();
  607. char addr[8];
  608. getAddrOfWorld(chunk->getCenter(), addr);
  609. map->saveMap(addr, 8);
  610. }
  611. // since all chunks were saved otherChunks can be ignored
  612. entityCs.lock();
  613. for (Entity* entity : *entities)
  614. {
  615. if (entity->zType()->getId() == EntityTypeEnum::PLAYER)
  616. {
  617. Datei pFile;
  618. pFile.setDatei(
  619. worldDir + "/player/"
  620. + Game::INSTANCE->getPlayerId(((Player*)entity)->getName()));
  621. pFile.erstellen();
  622. if (pFile.open(Datei::Style::schreiben))
  623. {
  624. Game::INSTANCE->zEntityType(EntityTypeEnum::PLAYER)
  625. ->saveEntity(entity, &pFile);
  626. pFile.close();
  627. }
  628. }
  629. }
  630. entityCs.unlock();
  631. for (MultiblockStructure* structure : structures)
  632. {
  633. saveStructure(structure);
  634. }
  635. }
  636. int Dimension::getDimensionId() const
  637. {
  638. return dimensionId;
  639. }
  640. bool Dimension::hasChunck(int x, int y) const
  641. {
  642. if (zChunk(Punkt(x, y))) return 1;
  643. removedChunksCs.lock();
  644. for (Chunk* c : removedChunks)
  645. {
  646. if (c->getCenter().x == x && c->getCenter().y == y)
  647. {
  648. removedChunksCs.unlock();
  649. return 1;
  650. }
  651. }
  652. removedChunksCs.unlock();
  653. return 0;
  654. }
  655. bool Dimension::reviveChunk(int x, int y)
  656. {
  657. chunkCs.lock();
  658. if (zChunk(Punkt(x, y)))
  659. {
  660. chunkCs.unlock();
  661. return 1;
  662. }
  663. removedChunksCs.lock();
  664. int index = 0;
  665. for (ArrayIterator<Chunk*> i = removedChunks.begin(); i; i++)
  666. {
  667. if (i->getCenter().x == x && i->getCenter().y == y)
  668. {
  669. setChunk(dynamic_cast<Chunk*>(i->getThis()), Punkt(x, y));
  670. if (index > 0) i.remove();
  671. removedChunksCs.unlock();
  672. chunkCs.unlock();
  673. return 1;
  674. }
  675. index++;
  676. }
  677. removedChunksCs.unlock();
  678. chunkCs.unlock();
  679. return 0;
  680. }
  681. float Dimension::getGravity() const
  682. {
  683. return gravity;
  684. }
  685. void Dimension::removeOldChunks()
  686. {
  687. chunkCs.lock();
  688. int index = 0;
  689. for (Chunk* chunk : chunkList)
  690. {
  691. if (!chunk->hasObservers()) setChunk(0, chunk->getCenter());
  692. index++;
  693. }
  694. chunkCs.unlock();
  695. structurCs.lock();
  696. ArrayIterator<MultiblockStructure*> i = structures.begin();
  697. while (i)
  698. {
  699. if (i->isEmpty())
  700. {
  701. i.remove();
  702. continue;
  703. }
  704. else if (i->isFullyUnloaded())
  705. {
  706. saveStructure(i);
  707. i.remove();
  708. continue;
  709. }
  710. i++;
  711. }
  712. structurCs.unlock();
  713. }
  714. Entity* Dimension::zTarget(Framework::Vec3<float> pos,
  715. Vec3<float> direction,
  716. float maxDistanceSq) const
  717. {
  718. double minDist = 0;
  719. Entity* closestEntity = 0;
  720. entityCs.lock();
  721. for (auto entity : *entities)
  722. {
  723. if (!entity->isRemoved()
  724. && entity->getPosition().abstandSq(pos) <= maxDistanceSq)
  725. {
  726. double dist = entity->getHitDistance(pos, direction);
  727. if (!isnan(dist))
  728. {
  729. if (!closestEntity || dist < minDist)
  730. {
  731. closestEntity = entity;
  732. minDist = dist;
  733. }
  734. }
  735. }
  736. }
  737. entityCs.unlock();
  738. return closestEntity;
  739. }
  740. Entity* Dimension::zEntity(int id) const
  741. {
  742. entityCs.lock();
  743. for (auto entity : *entities)
  744. {
  745. if (!entity->isRemoved() && entity->getId() == id)
  746. {
  747. entityCs.unlock();
  748. return entity;
  749. }
  750. }
  751. entityCs.unlock();
  752. return 0;
  753. }
  754. Entity* Dimension::zNearestEntity(
  755. Framework::Vec3<float> pos, std::function<bool(Entity*)> filter) const
  756. {
  757. Entity* result = 0;
  758. float sqDist = 0;
  759. entityCs.lock();
  760. for (auto entity : *entities)
  761. {
  762. if (!entity->isRemoved() && filter(entity))
  763. {
  764. float d = pos.abstandSq(entity->getPosition());
  765. if (!result || d < sqDist)
  766. {
  767. result = entity;
  768. sqDist = d;
  769. }
  770. }
  771. }
  772. entityCs.unlock();
  773. return result;
  774. }
  775. void Dimension::removeEntity(int id)
  776. {
  777. int index = 0;
  778. entityCs.lock();
  779. for (auto entity : *entities)
  780. {
  781. if (entity->getId() == id)
  782. {
  783. entities->remove(index);
  784. entityCs.unlock();
  785. return;
  786. }
  787. index++;
  788. }
  789. entityCs.unlock();
  790. }
  791. void Dimension::removeSubscriptions(Entity* zEntity)
  792. {
  793. for (Chunk* chunk : chunkList)
  794. chunk->removeObserver(zEntity);
  795. }
  796. void Dimension::updateLightning(Vec3<int> location)
  797. {
  798. lightCs.lock();
  799. lightUpdateQueue.add(location, 0);
  800. lightCs.unlock();
  801. }
  802. void Dimension::updateLightningWithoutWait(Framework::Vec3<int> location)
  803. {
  804. prioLightCs.lock();
  805. priorizedLightUpdateQueue.add(location, 0);
  806. prioLightCs.unlock();
  807. }
  808. void Dimension::updateLightAtChunkBorders(Chunk* zChunk)
  809. {
  810. if (lightUpdateQueue.getEintragAnzahl() > 300000)
  811. {
  812. Logging::warning()
  813. << "light calculation queue is over 300000 blocks long";
  814. }
  815. Punkt center = zChunk->getCenter();
  816. Chunk* xn = this->zChunk(center - Punkt(CHUNK_SIZE, 0));
  817. Chunk* xp = this->zChunk(center + Punkt(CHUNK_SIZE, 0));
  818. Chunk* yn = this->zChunk(center - Punkt(0, CHUNK_SIZE));
  819. Chunk* yp = this->zChunk(center + Punkt(0, CHUNK_SIZE));
  820. for (int i = WORLD_HEIGHT - 1; i >= 0; i--)
  821. {
  822. for (int j = 0; j < CHUNK_SIZE; j++)
  823. {
  824. if (xn)
  825. {
  826. unsigned char* light
  827. = xn->getLightData(Vec3<int>(CHUNK_SIZE - 1, j, i));
  828. unsigned char* light2
  829. = zChunk->getLightData(Vec3<int>(0, j, i));
  830. if (*(int*)light != *(int*)light2
  831. || *(int*)(light + 2) != *(int*)(light2 + 2))
  832. {
  833. updateLightning(Vec3<int>(center.x - CHUNK_SIZE / 2 - 1,
  834. center.y - CHUNK_SIZE / 2 + j,
  835. i));
  836. updateLightning(Vec3<int>(center.x - CHUNK_SIZE / 2,
  837. center.y - CHUNK_SIZE / 2 + j,
  838. i));
  839. }
  840. }
  841. if (xp)
  842. {
  843. unsigned char* light = xp->getLightData(Vec3<int>(0, j, i));
  844. unsigned char* light2
  845. = zChunk->getLightData(Vec3<int>(CHUNK_SIZE - 1, j, i));
  846. if (*(int*)light != *(int*)light2
  847. || *(int*)(light + 2) != *(int*)(light2 + 2))
  848. {
  849. updateLightning(Vec3<int>(center.x + CHUNK_SIZE / 2 - 1,
  850. center.y - CHUNK_SIZE / 2 + j,
  851. i));
  852. updateLightning(Vec3<int>(center.x + CHUNK_SIZE / 2,
  853. center.y - CHUNK_SIZE / 2 + j,
  854. i));
  855. }
  856. }
  857. if (yn)
  858. {
  859. unsigned char* light
  860. = yn->getLightData(Vec3<int>(j, CHUNK_SIZE - 1, i));
  861. unsigned char* light2
  862. = zChunk->getLightData(Vec3<int>(j, 0, i));
  863. if (*(int*)light != *(int*)light2
  864. || *(int*)(light + 2) != *(int*)(light2 + 2))
  865. {
  866. updateLightning(Vec3<int>(center.x - CHUNK_SIZE / 2 + j,
  867. center.y - CHUNK_SIZE / 2 - 1,
  868. i));
  869. updateLightning(Vec3<int>(center.x - CHUNK_SIZE / 2 + j,
  870. center.y - CHUNK_SIZE / 2,
  871. i));
  872. }
  873. }
  874. if (yp)
  875. {
  876. unsigned char* light = yp->getLightData(Vec3<int>(j, 0, i));
  877. unsigned char* light2
  878. = zChunk->getLightData(Vec3<int>(j, CHUNK_SIZE - 1, i));
  879. if (*(int*)light != *(int*)light2
  880. || *(int*)(light + 2) != *(int*)(light2 + 2))
  881. {
  882. updateLightning(Vec3<int>(center.x - CHUNK_SIZE / 2 + j,
  883. center.y + CHUNK_SIZE / 2 - 1,
  884. i));
  885. updateLightning(Vec3<int>(center.x - CHUNK_SIZE / 2 + j,
  886. center.y + CHUNK_SIZE / 2,
  887. i));
  888. }
  889. }
  890. }
  891. }
  892. }
  893. __int64 Dimension::getNextStructureId()
  894. {
  895. return nextStructureId++;
  896. }
  897. void Dimension::addStructure(MultiblockStructure* structure)
  898. {
  899. structurCs.lock();
  900. structures.add(structure);
  901. structurCs.unlock();
  902. }
  903. MultiblockStructure* Dimension::zStructureByPosition(
  904. Framework::Vec3<int> uniquePosition)
  905. {
  906. __int64 id = structureManager->getStructureId(uniquePosition);
  907. if (id > 0)
  908. {
  909. return zStructureById(id);
  910. }
  911. return 0;
  912. }
  913. MultiblockStructure* Dimension::zStructureById(__int64 id)
  914. {
  915. structurCs.lock();
  916. for (MultiblockStructure* str : structures)
  917. {
  918. if (str->getStructureId() == id)
  919. {
  920. structurCs.unlock();
  921. return str;
  922. }
  923. }
  924. MultiblockStructure* structure = structureManager->loadStructure(id);
  925. if (structure)
  926. {
  927. structures.add(structure);
  928. structurCs.unlock();
  929. return structure;
  930. }
  931. Logging::warning() << "did not find Structure information file '"
  932. << std::hex << id << "'.";
  933. structurCs.unlock();
  934. return 0;
  935. }
  936. void Dimension::requestStopAndWait()
  937. {
  938. stop = 1;
  939. warteAufThread(1000000);
  940. }
  941. void Dimension::updateMap(int x, int y, int height)
  942. {
  943. chunkCs.lock();
  944. int h1 = height % 2 == 0 ? height : height - 1;
  945. int h2 = h1 + 1;
  946. const Block* b1 = zBlockOrDefault({x, y, h1});
  947. const Block* b2 = zBlockOrDefault({x, y, h2});
  948. bool visible = 1;
  949. if (h2 != WORLD_HEIGHT - 1)
  950. {
  951. const Block* b3 = zBlockOrDefault({x, y, h2 + 1});
  952. visible = b3->isPassable() || b3->isTransparent();
  953. }
  954. int color1
  955. = (b2->isPassable() || b2->isTransparent()) ? b1->getMapColor() : 0;
  956. int color2 = visible ? b2->getMapColor() : 0;
  957. int color1m = 0;
  958. int color2m = 0;
  959. if (h1 > 0)
  960. {
  961. const Block* b1m = zBlockOrDefault({x, y, h1 - 2});
  962. const Block* b2m = zBlockOrDefault({x, y, h1 - 1});
  963. color1m = (b2m->isPassable() || b2m->isTransparent())
  964. ? b1m->getMapColor()
  965. : 0;
  966. color2m = (b1->isPassable() || b1->isTransparent()) ? b2m->getMapColor()
  967. : 0;
  968. }
  969. char addr[8];
  970. Punkt center = Game::INSTANCE->getChunkCenter(x, y);
  971. getAddrOfWorld(center, addr);
  972. ChunkMap* cMap = map->getMap(addr, 8, center);
  973. if (cMap)
  974. {
  975. Framework::Vec3<int> chunkLocation = chunkCoordinates({x, y, height});
  976. if (cMap->update((char)chunkLocation.x,
  977. (char)chunkLocation.y,
  978. (unsigned char)(chunkLocation.z / 2),
  979. color1,
  980. color2)
  981. || (h1 > 0
  982. && cMap->update((char)chunkLocation.x,
  983. (char)chunkLocation.y,
  984. (unsigned char)(chunkLocation.z / 2 - 1),
  985. color1m,
  986. color2m)))
  987. {
  988. map->onMapUpdated(addr, 8);
  989. }
  990. }
  991. chunkCs.unlock();
  992. }
  993. int Dimension::getChunkCount() const
  994. {
  995. return chunkList.getEintragAnzahl();
  996. }
  997. double Dimension::getCurrentDayTime() const
  998. {
  999. return currentDayTime;
  1000. }
  1001. double Dimension::getNightDuration() const
  1002. {
  1003. return nightDuration;
  1004. }
  1005. double Dimension::getNightTransitionDuration() const
  1006. {
  1007. return nightTransitionDuration;
  1008. }
  1009. double Dimension::getDayDuration() const
  1010. {
  1011. return dayDuration;
  1012. }