Dimension.cpp 23 KB

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