Dimension.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 RCTrie<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. laterHandler.execute();
  468. updateLightAtChunkBorders(center);
  469. }
  470. void Dimension::save(Text worldDir) const
  471. {
  472. Datei d;
  473. d.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  474. + "/nst.id");
  475. d.erstellen();
  476. d.open(Datei::Style::schreiben);
  477. d.schreibe((char*)&nextStructureId, 8);
  478. d.close();
  479. for (auto chunk = chunkList.begin(); chunk; chunk++)
  480. {
  481. if (!chunk._) continue;
  482. Datei* file = new Datei();
  483. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  484. filePath.appendHex(chunk->getCenter().x);
  485. filePath += "_";
  486. filePath.appendHex(chunk->getCenter().y);
  487. filePath += ".chunk";
  488. file->setDatei(filePath);
  489. if (file->open(Datei::Style::schreiben)) chunk->save(file);
  490. file->close();
  491. file->release();
  492. char addr[8];
  493. getAddrOfWorld(chunk->getCenter(), addr);
  494. map->saveMap(addr, 8);
  495. }
  496. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  497. Datei* file = new Datei();
  498. file->setDatei(filePath);
  499. if (file->open(Datei::Style::schreiben))
  500. {
  501. for (Entity* entity : *entities)
  502. {
  503. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  504. {
  505. if (!entity->isRemoved())
  506. {
  507. int type = entity->zType()->getId();
  508. file->schreibe((char*)&type, 4);
  509. StaticRegistry<EntityType>::INSTANCE.zElement(type)
  510. ->saveEntity(entity, file);
  511. }
  512. }
  513. else
  514. {
  515. Datei pFile;
  516. pFile.setDatei(worldDir + "/player/"
  517. + Game::INSTANCE->getPlayerId(
  518. ((Player*)entity)->getName()));
  519. if (pFile.open(Datei::Style::schreiben))
  520. StaticRegistry<EntityType>::INSTANCE
  521. .zElement(EntityTypeEnum::PLAYER)
  522. ->saveEntity(entity, &pFile);
  523. }
  524. }
  525. file->close();
  526. }
  527. for (MultiblockStructure* structure : structures)
  528. {
  529. saveStructure(structure);
  530. }
  531. }
  532. int Dimension::getDimensionId() const
  533. {
  534. return dimensionId;
  535. }
  536. bool Dimension::hasChunck(int x, int y)
  537. {
  538. if (zChunk(Punkt(x, y))) return 1;
  539. removedChunksCs.lock();
  540. for (Chunk* c : removedChunks)
  541. {
  542. if (c->getCenter().x == x && c->getCenter().y == y)
  543. {
  544. removedChunksCs.unlock();
  545. return 1;
  546. }
  547. }
  548. removedChunksCs.unlock();
  549. return 0;
  550. }
  551. bool Dimension::reviveChunk(int x, int y)
  552. {
  553. chunkCs.lock();
  554. if (zChunk(Punkt(x, y)))
  555. {
  556. chunkCs.unlock();
  557. return 1;
  558. }
  559. removedChunksCs.lock();
  560. int index = 0;
  561. for (Iterator<Chunk*> i = removedChunks.begin(); i; i++)
  562. {
  563. if (i->getCenter().x == x && i->getCenter().y == y)
  564. {
  565. setChunk(dynamic_cast<Chunk*>(i->getThis()), Punkt(x, y));
  566. if (index > 0) i.remove();
  567. removedChunksCs.unlock();
  568. chunkCs.unlock();
  569. return 1;
  570. }
  571. index++;
  572. }
  573. removedChunksCs.unlock();
  574. chunkCs.unlock();
  575. return 0;
  576. }
  577. float Dimension::getGravity() const
  578. {
  579. return gravity;
  580. }
  581. void Dimension::removeOldChunks()
  582. {
  583. chunkCs.lock();
  584. int index = 0;
  585. for (Chunk* chunk : chunkList)
  586. {
  587. if (!chunk->hasObservers()) setChunk(0, chunk->getCenter());
  588. index++;
  589. }
  590. chunkCs.unlock();
  591. structurCs.lock();
  592. Iterator<MultiblockStructure*> i = structures.begin();
  593. while (i)
  594. {
  595. if (i->isEmpty())
  596. {
  597. i.remove();
  598. continue;
  599. }
  600. else if (i->isFullyUnloaded())
  601. {
  602. saveStructure(i);
  603. i.remove();
  604. continue;
  605. }
  606. i++;
  607. }
  608. structurCs.unlock();
  609. }
  610. Entity* Dimension::zEntity(int id)
  611. {
  612. for (auto entity : *entities)
  613. {
  614. if (!entity->isRemoved() && entity->getId() == id) return entity;
  615. }
  616. return 0;
  617. }
  618. Entity* Dimension::zNearestEntity(
  619. Framework::Vec3<float> pos, std::function<bool(Entity*)> filter)
  620. {
  621. Entity* result = 0;
  622. float sqDist = 0;
  623. for (auto entity : *entities)
  624. {
  625. if (!entity->isRemoved() && filter(entity))
  626. {
  627. float d = pos.abstandSq(entity->getPosition());
  628. if (!result || d < sqDist)
  629. {
  630. result = entity;
  631. sqDist = d;
  632. }
  633. }
  634. }
  635. return result;
  636. }
  637. void Dimension::removeEntity(int id)
  638. {
  639. int index = 0;
  640. for (auto entity : *entities)
  641. {
  642. if (entity->getId() == id)
  643. {
  644. entities->remove(index);
  645. return;
  646. }
  647. index++;
  648. }
  649. }
  650. void Dimension::removeSubscriptions(Entity* zEntity)
  651. {
  652. for (Chunk* chunk : chunkList)
  653. chunk->removeObserver(zEntity);
  654. }
  655. void Dimension::updateLightning(Vec3<int> location)
  656. {
  657. lightCs.lock();
  658. lightUpdateQueue.add(location, 0);
  659. lightCs.unlock();
  660. }
  661. void Dimension::updateLightningWithoutWait(Framework::Vec3<int> location)
  662. {
  663. prioLightCs.lock();
  664. priorizedLightUpdateQueue.add(location, 0);
  665. prioLightCs.unlock();
  666. }
  667. void Dimension::updateLightAtChunkBorders(Punkt chunkCenter)
  668. {
  669. if (lightUpdateQueue.getEintragAnzahl() > 300000)
  670. {
  671. std::cout
  672. << "warning: light calculation queue is over 300000 blocks long\n";
  673. }
  674. for (int i = WORLD_HEIGHT - 1; i >= 0; i--)
  675. {
  676. for (int j = 0; j < CHUNK_SIZE; j++)
  677. {
  678. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 - 1,
  679. chunkCenter.y - CHUNK_SIZE / 2 + j,
  680. i));
  681. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2,
  682. chunkCenter.y - CHUNK_SIZE / 2 + j,
  683. i));
  684. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2 - 1,
  685. chunkCenter.y - CHUNK_SIZE / 2 + j,
  686. i));
  687. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2,
  688. chunkCenter.y - CHUNK_SIZE / 2 + j,
  689. i));
  690. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  691. chunkCenter.y - CHUNK_SIZE / 2 - 1,
  692. i));
  693. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  694. chunkCenter.y - CHUNK_SIZE / 2,
  695. i));
  696. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  697. chunkCenter.y + CHUNK_SIZE / 2 - 1,
  698. i));
  699. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  700. chunkCenter.y + CHUNK_SIZE / 2,
  701. i));
  702. }
  703. }
  704. }
  705. __int64 Dimension::getNextStructureId()
  706. {
  707. return nextStructureId++;
  708. }
  709. void Dimension::addStructure(MultiblockStructure* structure)
  710. {
  711. structurCs.lock();
  712. structures.add(structure);
  713. structurCs.unlock();
  714. }
  715. MultiblockStructure* Dimension::zStructureByPosition(
  716. Framework::Vec3<int> uniquePosition)
  717. {
  718. structurCs.lock();
  719. for (MultiblockStructure* str : structures)
  720. {
  721. if (str->getUniquePosition() == uniquePosition)
  722. {
  723. structurCs.unlock();
  724. return str;
  725. }
  726. }
  727. // search for structure file
  728. Datei dir(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  729. + "/structures");
  730. RCArray<Text>* names = dir.getDateiListe();
  731. if (names)
  732. {
  733. Vec3<int> uPos;
  734. for (Text* name : *names)
  735. {
  736. Datei d(Text(dir.zPfad()->getText()) + "/" + name->getText());
  737. if (d.open(Datei::Style::lesen))
  738. {
  739. d.lese((char*)&uPos.x, 4);
  740. d.lese((char*)&uPos.y, 4);
  741. d.lese((char*)&uPos.z, 4);
  742. if (uPos == uniquePosition)
  743. {
  744. int type;
  745. d.lese((char*)&type, 4);
  746. __int64 strId;
  747. d.lese((char*)&strId, 8);
  748. MultiblockStructure* str
  749. = StaticRegistry<MultiblockStructureType>::INSTANCE
  750. .zElement(type)
  751. ->loadStructure(
  752. dimensionId, strId, uniquePosition, &d);
  753. d.close();
  754. structures.add(str);
  755. names->release();
  756. structurCs.unlock();
  757. return str;
  758. }
  759. d.close();
  760. }
  761. }
  762. names->release();
  763. }
  764. structurCs.unlock();
  765. return 0;
  766. }
  767. MultiblockStructure* Dimension::zStructureById(__int64 id)
  768. {
  769. structurCs.lock();
  770. for (MultiblockStructure* str : structures)
  771. {
  772. if (str->getStructureId() == id)
  773. {
  774. structurCs.unlock();
  775. return str;
  776. }
  777. }
  778. // search for structure file
  779. Text path = Game::INSTANCE->getWorldDirectory() + "/dim/"
  780. + Text(dimensionId) + "/structures/";
  781. path.appendHex(id);
  782. path += ".str";
  783. Datei d(path);
  784. Vec3<int> uPos;
  785. if (d.open(Datei::Style::lesen))
  786. {
  787. d.lese((char*)&uPos.x, 4);
  788. d.lese((char*)&uPos.y, 4);
  789. d.lese((char*)&uPos.z, 4);
  790. int type;
  791. d.lese((char*)&type, 4);
  792. __int64 strId;
  793. d.lese((char*)&strId, 8);
  794. MultiblockStructure* str
  795. = StaticRegistry<MultiblockStructureType>::INSTANCE.zElement(type)
  796. ->loadStructure(dimensionId, strId, uPos, &d);
  797. d.close();
  798. structures.add(str);
  799. structurCs.unlock();
  800. return str;
  801. }
  802. std::cout << "WARNING: did not find Structure information file '" << path
  803. << "'.\n";
  804. structurCs.unlock();
  805. return 0;
  806. }
  807. void Dimension::requestStopAndWait()
  808. {
  809. stop = 1;
  810. warteAufThread(1000000);
  811. }
  812. void Dimension::updateMap(int x, int y, int height)
  813. {
  814. chunkCs.lock();
  815. int h1 = height % 2 == 0 ? height : height - 1;
  816. int h2 = h1 + 1;
  817. const Block* b1 = zBlockOrDefault({x, y, h1});
  818. const Block* b2 = zBlockOrDefault({x, y, h2});
  819. bool visible = 1;
  820. if (h2 != WORLD_HEIGHT - 1)
  821. {
  822. const Block* b3 = zBlockOrDefault({x, y, h2 + 1});
  823. visible = b3->isPassable() || b3->isTransparent();
  824. }
  825. int color1
  826. = (b2->isPassable() || b2->isTransparent()) ? b1->getMapColor() : 0;
  827. int color2 = visible ? b2->getMapColor() : 0;
  828. int color1m = 0;
  829. int color2m = 0;
  830. if (h1 > 0)
  831. {
  832. const Block* b1m = zBlockOrDefault({x, y, h1 - 2});
  833. const Block* b2m = zBlockOrDefault({x, y, h1 - 1});
  834. color1m = (b2m->isPassable() || b2m->isTransparent())
  835. ? b1m->getMapColor()
  836. : 0;
  837. color2m = (b1->isPassable() || b1->isTransparent()) ? b2m->getMapColor()
  838. : 0;
  839. }
  840. char addr[8];
  841. Punkt center = Game::INSTANCE->getChunkCenter(x, y);
  842. getAddrOfWorld(center, addr);
  843. ChunkMap* cMap = map->getMap(addr, 8, center);
  844. if (cMap)
  845. {
  846. Framework::Vec3<int> chunkLocation = chunkCoordinates({x, y, height});
  847. if (cMap->update((char)chunkLocation.x,
  848. (char)chunkLocation.y,
  849. (unsigned char)(chunkLocation.z / 2),
  850. color1,
  851. color2)
  852. || (h1 > 0
  853. && cMap->update((char)chunkLocation.x,
  854. (char)chunkLocation.y,
  855. (unsigned char)(chunkLocation.z / 2) - 1,
  856. color1m,
  857. color2m)))
  858. {
  859. map->onMapUpdated(addr, 8);
  860. }
  861. }
  862. chunkCs.unlock();
  863. }
  864. int Dimension::getChunkCount()
  865. {
  866. return chunkList.getEintragAnzahl();
  867. }