Dimension.cpp 28 KB

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