Dimension.cpp 28 KB

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