Dimension.cpp 27 KB

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