Dimension.cpp 28 KB

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