Dimension.cpp 27 KB

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