Dimension.cpp 26 KB

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