Dimension.cpp 30 KB

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