Dimension.cpp 29 KB

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