Dimension.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. Game::INSTANCE->zMultiblockStructureType(zStructure->getStructureTypeId())
  315. ->saveStructure(zStructure, &d);
  316. d.close();
  317. }
  318. Chunk* Dimension::zChunk(Punkt wPos) const
  319. {
  320. char addr[8];
  321. getAddrOfWorld(wPos, addr);
  322. return chunks->z(addr, 8);
  323. }
  324. Framework::Either<Block*, int> Dimension::zBlock(Vec3<int> location)
  325. {
  326. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  327. if (c)
  328. {
  329. location = chunkCoordinates(location);
  330. return c->zBlockAt(location);
  331. }
  332. return 0;
  333. }
  334. Block* Dimension::zRealBlockInstance(Framework::Vec3<int> location)
  335. {
  336. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  337. if (c)
  338. {
  339. location = chunkCoordinates(location);
  340. c->instantiateBlock(location);
  341. auto result = c->zBlockAt(location);
  342. return result.isA() ? result.getA() : 0;
  343. }
  344. return 0;
  345. }
  346. const Block* Dimension::zBlockOrDefault(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. return c->zBlockConst(location);
  353. }
  354. return &NoBlock::INSTANCE;
  355. }
  356. int Dimension::getBlockType(Framework::Vec3<int> location)
  357. {
  358. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  359. if (c)
  360. {
  361. location = chunkCoordinates(location);
  362. return c->getBlockTypeAt(location);
  363. }
  364. return BlockTypeEnum::NO_BLOCK;
  365. }
  366. void Dimension::placeBlock(
  367. Framework::Vec3<int> location, Framework::Either<Block*, int> block)
  368. {
  369. if (block.isB() && block.getB() == BlockTypeEnum::NO_BLOCK) return;
  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. Game::INSTANCE->zEntityType(type)->saveEntity(entity, file);
  539. }
  540. }
  541. else
  542. {
  543. Datei pFile;
  544. pFile.setDatei(worldDir + "/player/"
  545. + Game::INSTANCE->getPlayerId(
  546. ((Player*)entity)->getName()));
  547. if (pFile.open(Datei::Style::schreiben))
  548. Game::INSTANCE->zEntityType(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. = Game::INSTANCE->zMultiblockStructureType(type)
  777. ->loadStructure(
  778. dimensionId, strId, uniquePosition, &d);
  779. d.close();
  780. structures.add(str);
  781. names->release();
  782. structurCs.unlock();
  783. return str;
  784. }
  785. d.close();
  786. }
  787. }
  788. names->release();
  789. }
  790. structurCs.unlock();
  791. return 0;
  792. }
  793. MultiblockStructure* Dimension::zStructureById(__int64 id)
  794. {
  795. structurCs.lock();
  796. for (MultiblockStructure* str : structures)
  797. {
  798. if (str->getStructureId() == id)
  799. {
  800. structurCs.unlock();
  801. return str;
  802. }
  803. }
  804. // search for structure file
  805. Text path = Game::INSTANCE->getWorldDirectory() + "/dim/"
  806. + Text(dimensionId) + "/structures/";
  807. path.appendHex(id);
  808. path += ".str";
  809. Datei d(path);
  810. Vec3<int> uPos;
  811. if (d.open(Datei::Style::lesen))
  812. {
  813. d.lese((char*)&uPos.x, 4);
  814. d.lese((char*)&uPos.y, 4);
  815. d.lese((char*)&uPos.z, 4);
  816. int type;
  817. d.lese((char*)&type, 4);
  818. __int64 strId;
  819. d.lese((char*)&strId, 8);
  820. MultiblockStructure* str
  821. = Game::INSTANCE->zMultiblockStructureType(type)->loadStructure(
  822. dimensionId, strId, uPos, &d);
  823. d.close();
  824. structures.add(str);
  825. structurCs.unlock();
  826. return str;
  827. }
  828. std::cout << "WARNING: did not find Structure information file '" << path
  829. << "'.\n";
  830. structurCs.unlock();
  831. return 0;
  832. }
  833. void Dimension::requestStopAndWait()
  834. {
  835. stop = 1;
  836. warteAufThread(1000000);
  837. }
  838. void Dimension::updateMap(int x, int y, int height)
  839. {
  840. chunkCs.lock();
  841. int h1 = height % 2 == 0 ? height : height - 1;
  842. int h2 = h1 + 1;
  843. const Block* b1 = zBlockOrDefault({x, y, h1});
  844. const Block* b2 = zBlockOrDefault({x, y, h2});
  845. bool visible = 1;
  846. if (h2 != WORLD_HEIGHT - 1)
  847. {
  848. const Block* b3 = zBlockOrDefault({x, y, h2 + 1});
  849. visible = b3->isPassable() || b3->isTransparent();
  850. }
  851. int color1
  852. = (b2->isPassable() || b2->isTransparent()) ? b1->getMapColor() : 0;
  853. int color2 = visible ? b2->getMapColor() : 0;
  854. int color1m = 0;
  855. int color2m = 0;
  856. if (h1 > 0)
  857. {
  858. const Block* b1m = zBlockOrDefault({x, y, h1 - 2});
  859. const Block* b2m = zBlockOrDefault({x, y, h1 - 1});
  860. color1m = (b2m->isPassable() || b2m->isTransparent())
  861. ? b1m->getMapColor()
  862. : 0;
  863. color2m = (b1->isPassable() || b1->isTransparent()) ? b2m->getMapColor()
  864. : 0;
  865. }
  866. char addr[8];
  867. Punkt center = Game::INSTANCE->getChunkCenter(x, y);
  868. getAddrOfWorld(center, addr);
  869. ChunkMap* cMap = map->getMap(addr, 8, center);
  870. if (cMap)
  871. {
  872. Framework::Vec3<int> chunkLocation = chunkCoordinates({x, y, height});
  873. if (cMap->update((char)chunkLocation.x,
  874. (char)chunkLocation.y,
  875. (unsigned char)(chunkLocation.z / 2),
  876. color1,
  877. color2)
  878. || (h1 > 0
  879. && cMap->update((char)chunkLocation.x,
  880. (char)chunkLocation.y,
  881. (unsigned char)(chunkLocation.z / 2 - 1),
  882. color1m,
  883. color2m)))
  884. {
  885. map->onMapUpdated(addr, 8);
  886. }
  887. }
  888. chunkCs.unlock();
  889. }
  890. int Dimension::getChunkCount()
  891. {
  892. return chunkList.getEintragAnzahl();
  893. }
  894. double Dimension::getCurrentDayTime() const
  895. {
  896. return currentDayTime;
  897. }
  898. double Dimension::getNightDuration() const
  899. {
  900. return nightDuration;
  901. }
  902. double Dimension::getNightTransitionDuration() const
  903. {
  904. return nightTransitionDuration;
  905. }
  906. double Dimension::getDayDuration() const
  907. {
  908. return dayDuration;
  909. }
  910. DimensionFactory::DimensionFactory(int dimensionId)
  911. : ReferenceCounter(),
  912. dimensionId(dimensionId)
  913. {}
  914. int DimensionFactory::getDimensionId() const
  915. {
  916. return dimensionId;
  917. }