Dimension.cpp 24 KB

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