Dimension.cpp 28 KB

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