Dimension.cpp 24 KB

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