Dimension.cpp 24 KB

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