Dimension.cpp 28 KB

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