Dimension.cpp 27 KB

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