Dimension.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. if (block.isB() && block.getB() == BlockTypeEnum::NO_BLOCK) return;
  371. Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
  372. if (c)
  373. {
  374. location = chunkCoordinates(location);
  375. if (block.isA())
  376. c->putBlockAt(location, block);
  377. else
  378. {
  379. c->putBlockAt(location, 0);
  380. c->putBlockTypeAt(location, block);
  381. }
  382. }
  383. else if (block.isA())
  384. block.getA()->release();
  385. }
  386. void Dimension::sendBlockInfo(Framework::Vec3<int> location)
  387. {
  388. Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
  389. if (c)
  390. {
  391. location = chunkCoordinates(location);
  392. c->sendBlockInfo(location);
  393. }
  394. }
  395. void Dimension::addEntity(Entity* entity)
  396. {
  397. entities->add(entity);
  398. }
  399. void Dimension::setChunk(Chunk* chunk, Punkt center)
  400. {
  401. char addr[8];
  402. getAddrOfWorld(center, addr);
  403. if (chunk) map->loadMap(addr, 8, chunk);
  404. chunkCs.lock();
  405. Chunk* old = chunks->get(addr, 8);
  406. if (old)
  407. {
  408. Game::INSTANCE->zTickOrganizer()->removeTickSource(old);
  409. old->prepareRemove();
  410. for (int i = 0; i < chunkList.getEintragAnzahl(); i++)
  411. {
  412. if (chunkList.get(i) == old)
  413. {
  414. chunkList.remove(i);
  415. break;
  416. }
  417. }
  418. }
  419. chunks->set(addr, 8, chunk);
  420. if (chunk)
  421. {
  422. chunkList.add(chunk);
  423. chunk->setAdded();
  424. }
  425. getAddrOfWorld(center + Punkt(CHUNK_SIZE, 0), addr);
  426. Chunk* zChunk = chunks->z(addr, 8);
  427. if (zChunk)
  428. {
  429. zChunk->setNeighbor(WEST, chunk);
  430. if (chunk)
  431. {
  432. chunk->setNeighbor(EAST, zChunk);
  433. }
  434. }
  435. getAddrOfWorld(center + Punkt(-CHUNK_SIZE, 0), addr);
  436. zChunk = chunks->z(addr, 8);
  437. if (zChunk)
  438. {
  439. zChunk->setNeighbor(EAST, chunk);
  440. if (chunk) chunk->setNeighbor(WEST, zChunk);
  441. }
  442. getAddrOfWorld(center + Punkt(0, CHUNK_SIZE), addr);
  443. zChunk = chunks->z(addr, 8);
  444. if (zChunk)
  445. {
  446. zChunk->setNeighbor(NORTH, chunk);
  447. if (chunk) chunk->setNeighbor(SOUTH, zChunk);
  448. }
  449. getAddrOfWorld(center + Punkt(0, -CHUNK_SIZE), addr);
  450. zChunk = chunks->z(addr, 8);
  451. if (zChunk)
  452. {
  453. zChunk->setNeighbor(SOUTH, chunk);
  454. if (chunk) chunk->setNeighbor(NORTH, zChunk);
  455. }
  456. DoLaterHandler laterHandler;
  457. if (chunk)
  458. {
  459. cs.lock();
  460. int index = 0;
  461. for (Iterator<RequestQueue> iterator = waitingRequests.begin();
  462. iterator;)
  463. {
  464. Entity* zE = Game::INSTANCE->zEntity(iterator.val().sourceId);
  465. if (zE)
  466. {
  467. if (iterator.val().chunkCenter == chunk->getCenter())
  468. {
  469. chunk->api(iterator.val().request, zE, laterHandler);
  470. iterator.val().request->release();
  471. iterator.remove();
  472. continue;
  473. }
  474. }
  475. else
  476. {
  477. iterator.val().request->release();
  478. iterator.remove();
  479. continue;
  480. }
  481. iterator++;
  482. index++;
  483. }
  484. cs.unlock();
  485. Game::INSTANCE->zTickOrganizer()->addTickSource(chunk);
  486. }
  487. chunkCs.unlock();
  488. if (old)
  489. {
  490. old->onUnloaded();
  491. removedChunksCs.lock();
  492. removedChunks.add(old);
  493. removedChunksCs.unlock();
  494. }
  495. if (chunk) chunk->onLoaded();
  496. laterHandler.execute();
  497. if (chunk) updateLightAtChunkBorders(center);
  498. }
  499. void Dimension::save(Text worldDir) const
  500. {
  501. Datei d;
  502. d.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  503. + "/meta.dim");
  504. d.erstellen();
  505. d.open(Datei::Style::schreiben);
  506. d.schreibe((char*)&nextStructureId, 8);
  507. d.schreibe((char*)&currentDayTime, 8);
  508. d.close();
  509. for (auto chunk = chunkList.begin(); chunk; chunk++)
  510. {
  511. if (!chunk._) continue;
  512. Datei* file = new Datei();
  513. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  514. filePath.appendHex(chunk->getCenter().x);
  515. filePath += "_";
  516. filePath.appendHex(chunk->getCenter().y);
  517. filePath += ".chunk";
  518. file->setDatei(filePath);
  519. if (file->open(Datei::Style::schreiben)) chunk->save(file);
  520. file->close();
  521. file->release();
  522. char addr[8];
  523. getAddrOfWorld(chunk->getCenter(), addr);
  524. map->saveMap(addr, 8);
  525. }
  526. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  527. Datei* file = new Datei();
  528. file->setDatei(filePath);
  529. if (file->open(Datei::Style::schreiben))
  530. {
  531. for (Entity* entity : *entities)
  532. {
  533. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  534. {
  535. if (!entity->isRemoved())
  536. {
  537. int type = entity->zType()->getId();
  538. file->schreibe((char*)&type, 4);
  539. StaticRegistry<EntityType>::INSTANCE.zElement(type)
  540. ->saveEntity(entity, file);
  541. }
  542. }
  543. else
  544. {
  545. Datei pFile;
  546. pFile.setDatei(worldDir + "/player/"
  547. + Game::INSTANCE->getPlayerId(
  548. ((Player*)entity)->getName()));
  549. if (pFile.open(Datei::Style::schreiben))
  550. StaticRegistry<EntityType>::INSTANCE
  551. .zElement(EntityTypeEnum::PLAYER)
  552. ->saveEntity(entity, &pFile);
  553. }
  554. }
  555. file->close();
  556. }
  557. for (MultiblockStructure* structure : structures)
  558. {
  559. saveStructure(structure);
  560. }
  561. }
  562. int Dimension::getDimensionId() const
  563. {
  564. return dimensionId;
  565. }
  566. bool Dimension::hasChunck(int x, int y)
  567. {
  568. if (zChunk(Punkt(x, y))) return 1;
  569. removedChunksCs.lock();
  570. for (Chunk* c : removedChunks)
  571. {
  572. if (c->getCenter().x == x && c->getCenter().y == y)
  573. {
  574. removedChunksCs.unlock();
  575. return 1;
  576. }
  577. }
  578. removedChunksCs.unlock();
  579. return 0;
  580. }
  581. bool Dimension::reviveChunk(int x, int y)
  582. {
  583. chunkCs.lock();
  584. if (zChunk(Punkt(x, y)))
  585. {
  586. chunkCs.unlock();
  587. return 1;
  588. }
  589. removedChunksCs.lock();
  590. int index = 0;
  591. for (Iterator<Chunk*> i = removedChunks.begin(); i; i++)
  592. {
  593. if (i->getCenter().x == x && i->getCenter().y == y)
  594. {
  595. setChunk(dynamic_cast<Chunk*>(i->getThis()), Punkt(x, y));
  596. if (index > 0) i.remove();
  597. removedChunksCs.unlock();
  598. chunkCs.unlock();
  599. return 1;
  600. }
  601. index++;
  602. }
  603. removedChunksCs.unlock();
  604. chunkCs.unlock();
  605. return 0;
  606. }
  607. float Dimension::getGravity() const
  608. {
  609. return gravity;
  610. }
  611. void Dimension::removeOldChunks()
  612. {
  613. chunkCs.lock();
  614. int index = 0;
  615. for (Chunk* chunk : chunkList)
  616. {
  617. if (!chunk->hasObservers()) setChunk(0, chunk->getCenter());
  618. index++;
  619. }
  620. chunkCs.unlock();
  621. structurCs.lock();
  622. Iterator<MultiblockStructure*> i = structures.begin();
  623. while (i)
  624. {
  625. if (i->isEmpty())
  626. {
  627. i.remove();
  628. continue;
  629. }
  630. else if (i->isFullyUnloaded())
  631. {
  632. saveStructure(i);
  633. i.remove();
  634. continue;
  635. }
  636. i++;
  637. }
  638. structurCs.unlock();
  639. }
  640. Entity* Dimension::zEntity(int id)
  641. {
  642. for (auto entity : *entities)
  643. {
  644. if (!entity->isRemoved() && entity->getId() == id) return entity;
  645. }
  646. return 0;
  647. }
  648. Entity* Dimension::zNearestEntity(
  649. Framework::Vec3<float> pos, std::function<bool(Entity*)> filter)
  650. {
  651. Entity* result = 0;
  652. float sqDist = 0;
  653. for (auto entity : *entities)
  654. {
  655. if (!entity->isRemoved() && filter(entity))
  656. {
  657. float d = pos.abstandSq(entity->getPosition());
  658. if (!result || d < sqDist)
  659. {
  660. result = entity;
  661. sqDist = d;
  662. }
  663. }
  664. }
  665. return result;
  666. }
  667. void Dimension::removeEntity(int id)
  668. {
  669. int index = 0;
  670. for (auto entity : *entities)
  671. {
  672. if (entity->getId() == id)
  673. {
  674. entities->remove(index);
  675. return;
  676. }
  677. index++;
  678. }
  679. }
  680. void Dimension::removeSubscriptions(Entity* zEntity)
  681. {
  682. for (Chunk* chunk : chunkList)
  683. chunk->removeObserver(zEntity);
  684. }
  685. void Dimension::updateLightning(Vec3<int> location)
  686. {
  687. lightCs.lock();
  688. lightUpdateQueue.add(location, 0);
  689. lightCs.unlock();
  690. }
  691. void Dimension::updateLightningWithoutWait(Framework::Vec3<int> location)
  692. {
  693. prioLightCs.lock();
  694. priorizedLightUpdateQueue.add(location, 0);
  695. prioLightCs.unlock();
  696. }
  697. void Dimension::updateLightAtChunkBorders(Punkt chunkCenter)
  698. {
  699. if (lightUpdateQueue.getEintragAnzahl() > 300000)
  700. {
  701. std::cout
  702. << "warning: light calculation queue is over 300000 blocks long\n";
  703. }
  704. for (int i = WORLD_HEIGHT - 1; i >= 0; i--)
  705. {
  706. for (int j = 0; j < CHUNK_SIZE; j++)
  707. {
  708. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 - 1,
  709. chunkCenter.y - CHUNK_SIZE / 2 + j,
  710. i));
  711. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2,
  712. chunkCenter.y - CHUNK_SIZE / 2 + j,
  713. i));
  714. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2 - 1,
  715. chunkCenter.y - CHUNK_SIZE / 2 + j,
  716. i));
  717. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2,
  718. chunkCenter.y - CHUNK_SIZE / 2 + j,
  719. i));
  720. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  721. chunkCenter.y - CHUNK_SIZE / 2 - 1,
  722. i));
  723. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  724. chunkCenter.y - CHUNK_SIZE / 2,
  725. i));
  726. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  727. chunkCenter.y + CHUNK_SIZE / 2 - 1,
  728. i));
  729. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  730. chunkCenter.y + CHUNK_SIZE / 2,
  731. i));
  732. }
  733. }
  734. }
  735. __int64 Dimension::getNextStructureId()
  736. {
  737. return nextStructureId++;
  738. }
  739. void Dimension::addStructure(MultiblockStructure* structure)
  740. {
  741. structurCs.lock();
  742. structures.add(structure);
  743. structurCs.unlock();
  744. }
  745. MultiblockStructure* Dimension::zStructureByPosition(
  746. Framework::Vec3<int> uniquePosition)
  747. {
  748. structurCs.lock();
  749. for (MultiblockStructure* str : structures)
  750. {
  751. if (str->getUniquePosition() == uniquePosition)
  752. {
  753. structurCs.unlock();
  754. return str;
  755. }
  756. }
  757. // search for structure file
  758. Datei dir(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  759. + "/structures");
  760. RCArray<Text>* names = dir.getDateiListe();
  761. if (names)
  762. {
  763. Vec3<int> uPos;
  764. for (Text* name : *names)
  765. {
  766. Datei d(Text(dir.zPfad()->getText()) + "/" + name->getText());
  767. if (d.open(Datei::Style::lesen))
  768. {
  769. d.lese((char*)&uPos.x, 4);
  770. d.lese((char*)&uPos.y, 4);
  771. d.lese((char*)&uPos.z, 4);
  772. if (uPos == uniquePosition)
  773. {
  774. int type;
  775. d.lese((char*)&type, 4);
  776. __int64 strId;
  777. d.lese((char*)&strId, 8);
  778. MultiblockStructure* str
  779. = StaticRegistry<MultiblockStructureType>::INSTANCE
  780. .zElement(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. = StaticRegistry<MultiblockStructureType>::INSTANCE.zElement(type)
  826. ->loadStructure(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. }
  914. DimensionFactory::DimensionFactory(int dimensionId)
  915. : ReferenceCounter(),
  916. dimensionId(dimensionId)
  917. {}
  918. int DimensionFactory::getDimensionId() const
  919. {
  920. return dimensionId;
  921. }