Dimension.cpp 26 KB

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