Dimension.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. void Dimension::saveStructure(MultiblockStructure* zStructure) const
  298. {
  299. Datei d;
  300. Text path = Game::INSTANCE->getWorldDirectory() + "/dim/"
  301. + Text(dimensionId) + "/structures/";
  302. path.appendHex(zStructure->getStructureId());
  303. path += ".str";
  304. d.setDatei(path);
  305. d.erstellen();
  306. d.open(Datei::Style::schreiben);
  307. auto uPos = zStructure->getUniquePosition();
  308. d.schreibe((char*)&uPos.x, 4);
  309. d.schreibe((char*)&uPos.y, 4);
  310. d.schreibe((char*)&uPos.z, 4);
  311. int typeId = zStructure->getStructureTypeId();
  312. d.schreibe((char*)&typeId, 4);
  313. __int64 strId = zStructure->getStructureId();
  314. d.schreibe((char*)&strId, 8);
  315. StaticRegistry<MultiblockStructureType>::INSTANCE
  316. .zElement(zStructure->getStructureTypeId())
  317. ->saveStructure(zStructure, &d);
  318. d.close();
  319. }
  320. Chunk* Dimension::zChunk(Punkt wPos) const
  321. {
  322. char addr[8];
  323. getAddrOfWorld(wPos, addr);
  324. return chunks->z(addr, 8);
  325. }
  326. Framework::Either<Block*, int> Dimension::zBlock(Vec3<int> location)
  327. {
  328. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  329. if (c)
  330. {
  331. int x = location.x % CHUNK_SIZE;
  332. int y = location.y % CHUNK_SIZE;
  333. if (x < 0) x += CHUNK_SIZE;
  334. if (y < 0) y += CHUNK_SIZE;
  335. return c->zBlockAt(Vec3<int>(x, y, location.z));
  336. }
  337. return 0;
  338. }
  339. Block* Dimension::zRealBlockInstance(Framework::Vec3<int> location)
  340. {
  341. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  342. if (c)
  343. {
  344. int x = location.x % CHUNK_SIZE;
  345. int y = location.y % CHUNK_SIZE;
  346. if (x < 0) x += CHUNK_SIZE;
  347. if (y < 0) y += CHUNK_SIZE;
  348. c->instantiateBlock(Vec3<int>(x, y, location.z));
  349. auto result = c->zBlockAt(Vec3<int>(x, y, location.z));
  350. return result.isA() ? result.getA() : 0;
  351. }
  352. return 0;
  353. }
  354. const Block* Dimension::zBlockOrDefault(Framework::Vec3<int> location)
  355. {
  356. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  357. if (c)
  358. {
  359. int x = location.x % CHUNK_SIZE;
  360. int y = location.y % CHUNK_SIZE;
  361. if (x < 0) x += CHUNK_SIZE;
  362. if (y < 0) y += CHUNK_SIZE;
  363. return c->zBlockConst(Vec3<int>(x, y, location.z));
  364. }
  365. return &NoBlock::INSTANCE;
  366. }
  367. void Dimension::placeBlock(
  368. Framework::Vec3<int> location, Framework::Either<Block*, int> block)
  369. {
  370. Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
  371. if (c)
  372. {
  373. int x = location.x % CHUNK_SIZE;
  374. int y = location.y % CHUNK_SIZE;
  375. if (x < 0) x += CHUNK_SIZE;
  376. if (y < 0) y += CHUNK_SIZE;
  377. if (block.isA())
  378. c->putBlockAt(Vec3<int>(x, y, location.z), block);
  379. else
  380. {
  381. c->putBlockAt(Vec3<int>(x, y, location.z), 0);
  382. c->putBlockTypeAt(Vec3<int>(x, y, location.z), block);
  383. }
  384. }
  385. else if (block.isA())
  386. block.getA()->release();
  387. }
  388. void Dimension::sendBlockInfo(Framework::Vec3<int> location)
  389. {
  390. Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
  391. if (c)
  392. {
  393. int x = location.x % CHUNK_SIZE;
  394. int y = location.y % CHUNK_SIZE;
  395. if (x < 0) x += CHUNK_SIZE;
  396. if (y < 0) y += CHUNK_SIZE;
  397. c->sendBlockInfo(Vec3<int>(x, y, location.z));
  398. }
  399. }
  400. void Dimension::addEntity(Entity* entity)
  401. {
  402. entities->add(entity);
  403. }
  404. void Dimension::setChunk(Chunk* chunk, Punkt center)
  405. {
  406. char addr[8];
  407. getAddrOfWorld(center, addr);
  408. if (chunk) map->loadMap(addr, 8, chunk);
  409. chunkCs.lock();
  410. Chunk* old = chunks->get(addr, 8);
  411. if (old)
  412. {
  413. Game::INSTANCE->zTickOrganizer()->removeTickSource(old);
  414. old->prepareRemove();
  415. for (int i = 0; i < chunkList.getEintragAnzahl(); i++)
  416. {
  417. if (chunkList.get(i) == old)
  418. {
  419. chunkList.remove(i);
  420. break;
  421. }
  422. }
  423. }
  424. chunks->set(addr, 8, chunk);
  425. if (chunk)
  426. {
  427. chunkList.add(chunk);
  428. chunk->setAdded();
  429. }
  430. getAddrOfWorld(center + Punkt(CHUNK_SIZE, 0), addr);
  431. Chunk* zChunk = chunks->z(addr, 8);
  432. if (zChunk)
  433. {
  434. zChunk->setNeighbor(WEST, chunk);
  435. if (chunk)
  436. {
  437. chunk->setNeighbor(EAST, zChunk);
  438. }
  439. }
  440. getAddrOfWorld(center + Punkt(-CHUNK_SIZE, 0), addr);
  441. zChunk = chunks->z(addr, 8);
  442. if (zChunk)
  443. {
  444. zChunk->setNeighbor(EAST, chunk);
  445. if (chunk) chunk->setNeighbor(WEST, zChunk);
  446. }
  447. getAddrOfWorld(center + Punkt(0, CHUNK_SIZE), addr);
  448. zChunk = chunks->z(addr, 8);
  449. if (zChunk)
  450. {
  451. zChunk->setNeighbor(NORTH, chunk);
  452. if (chunk) chunk->setNeighbor(SOUTH, zChunk);
  453. }
  454. getAddrOfWorld(center + Punkt(0, -CHUNK_SIZE), addr);
  455. zChunk = chunks->z(addr, 8);
  456. if (zChunk)
  457. {
  458. zChunk->setNeighbor(SOUTH, chunk);
  459. if (chunk) chunk->setNeighbor(NORTH, zChunk);
  460. }
  461. DoLaterHandler laterHandler;
  462. if (chunk)
  463. {
  464. cs.lock();
  465. int index = 0;
  466. for (Iterator<RequestQueue> iterator = waitingRequests.begin();
  467. iterator;)
  468. {
  469. Entity* zE = Game::INSTANCE->zEntity(iterator.val().sourceId);
  470. if (zE)
  471. {
  472. if (iterator.val().chunkCenter == chunk->getCenter())
  473. {
  474. chunk->api(iterator.val().request, zE, laterHandler);
  475. iterator.val().request->release();
  476. iterator.remove();
  477. continue;
  478. }
  479. }
  480. else
  481. {
  482. iterator.val().request->release();
  483. iterator.remove();
  484. continue;
  485. }
  486. iterator++;
  487. index++;
  488. }
  489. cs.unlock();
  490. Game::INSTANCE->zTickOrganizer()->addTickSource(chunk);
  491. }
  492. chunkCs.unlock();
  493. if (old)
  494. {
  495. old->onUnloaded();
  496. removedChunksCs.lock();
  497. removedChunks.add(old);
  498. removedChunksCs.unlock();
  499. }
  500. if (chunk) chunk->onLoaded();
  501. updateLightAtChunkBorders(center);
  502. }
  503. void Dimension::save(Text worldDir) const
  504. {
  505. Datei d;
  506. d.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  507. + "/nst.id");
  508. d.erstellen();
  509. d.open(Datei::Style::schreiben);
  510. d.schreibe((char*)&nextStructureId, 8);
  511. d.close();
  512. for (auto chunk = chunkList.begin(); chunk; chunk++)
  513. {
  514. if (!chunk._) continue;
  515. Datei* file = new Datei();
  516. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  517. filePath.appendHex(chunk->getCenter().x);
  518. filePath += "_";
  519. filePath.appendHex(chunk->getCenter().y);
  520. filePath += ".chunk";
  521. file->setDatei(filePath);
  522. if (file->open(Datei::Style::schreiben)) chunk->save(file);
  523. file->close();
  524. file->release();
  525. char addr[8];
  526. getAddrOfWorld(chunk->getCenter(), addr);
  527. map->saveMap(addr, 8);
  528. }
  529. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  530. Datei* file = new Datei();
  531. file->setDatei(filePath);
  532. if (file->open(Datei::Style::schreiben))
  533. {
  534. for (Entity* entity : *entities)
  535. {
  536. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  537. {
  538. if (!entity->isRemoved())
  539. {
  540. int type = entity->zType()->getId();
  541. file->schreibe((char*)&type, 4);
  542. StaticRegistry<EntityType>::INSTANCE.zElement(type)
  543. ->saveEntity(entity, file);
  544. }
  545. }
  546. else
  547. {
  548. Datei pFile;
  549. pFile.setDatei(worldDir + "/player/"
  550. + Game::INSTANCE->getPlayerId(
  551. ((Player*)entity)->getName()));
  552. if (pFile.open(Datei::Style::schreiben))
  553. StaticRegistry<EntityType>::INSTANCE
  554. .zElement(EntityTypeEnum::PLAYER)
  555. ->saveEntity(entity, &pFile);
  556. }
  557. }
  558. file->close();
  559. }
  560. for (MultiblockStructure* structure : structures)
  561. {
  562. saveStructure(structure);
  563. }
  564. }
  565. int Dimension::getDimensionId() const
  566. {
  567. return dimensionId;
  568. }
  569. bool Dimension::hasChunck(int x, int y)
  570. {
  571. if (zChunk(Punkt(x, y))) return 1;
  572. removedChunksCs.lock();
  573. for (Chunk* c : removedChunks)
  574. {
  575. if (c->getCenter().x == x && c->getCenter().y == y)
  576. {
  577. removedChunksCs.unlock();
  578. return 1;
  579. }
  580. }
  581. removedChunksCs.unlock();
  582. return 0;
  583. }
  584. bool Dimension::reviveChunk(int x, int y)
  585. {
  586. chunkCs.lock();
  587. if (zChunk(Punkt(x, y)))
  588. {
  589. chunkCs.unlock();
  590. return 1;
  591. }
  592. removedChunksCs.lock();
  593. int index = 0;
  594. for (Iterator<Chunk*> i = removedChunks.begin(); i; i++)
  595. {
  596. if (i->getCenter().x == x && i->getCenter().y == y)
  597. {
  598. setChunk(dynamic_cast<Chunk*>(i->getThis()), Punkt(x, y));
  599. if (index > 0) i.remove();
  600. removedChunksCs.unlock();
  601. chunkCs.unlock();
  602. return 1;
  603. }
  604. index++;
  605. }
  606. removedChunksCs.unlock();
  607. chunkCs.unlock();
  608. return 0;
  609. }
  610. float Dimension::getGravity() const
  611. {
  612. return gravity;
  613. }
  614. void Dimension::removeOldChunks()
  615. {
  616. chunkCs.lock();
  617. int index = 0;
  618. for (Chunk* chunk : chunkList)
  619. {
  620. if (!chunk->hasObservers()) setChunk(0, chunk->getCenter());
  621. index++;
  622. }
  623. chunkCs.unlock();
  624. structurCs.lock();
  625. Iterator<MultiblockStructure*> i = structures.begin();
  626. while (i)
  627. {
  628. if (i->isEmpty())
  629. {
  630. i.remove();
  631. continue;
  632. }
  633. else if (i->isFullyUnloaded())
  634. {
  635. saveStructure(i);
  636. i.remove();
  637. continue;
  638. }
  639. i++;
  640. }
  641. structurCs.unlock();
  642. }
  643. Entity* Dimension::zEntity(int id)
  644. {
  645. for (auto entity : *entities)
  646. {
  647. if (!entity->isRemoved() && entity->getId() == id) return entity;
  648. }
  649. return 0;
  650. }
  651. Entity* Dimension::zNearestEntity(
  652. Framework::Vec3<float> pos, std::function<bool(Entity*)> filter)
  653. {
  654. Entity* result = 0;
  655. float sqDist = 0;
  656. for (auto entity : *entities)
  657. {
  658. if (!entity->isRemoved() && filter(entity))
  659. {
  660. float d = pos.abstandSq(entity->getPosition());
  661. if (!result || d < sqDist)
  662. {
  663. result = entity;
  664. sqDist = d;
  665. }
  666. }
  667. }
  668. return result;
  669. }
  670. void Dimension::removeEntity(int id)
  671. {
  672. int index = 0;
  673. for (auto entity : *entities)
  674. {
  675. if (entity->getId() == id)
  676. {
  677. entities->remove(index);
  678. return;
  679. }
  680. index++;
  681. }
  682. }
  683. void Dimension::removeSubscriptions(Entity* zEntity)
  684. {
  685. for (Chunk* chunk : chunkList)
  686. chunk->removeObserver(zEntity);
  687. }
  688. void Dimension::updateLightning(Vec3<int> location)
  689. {
  690. lightCs.lock();
  691. lightUpdateQueue.add(location, 0);
  692. lightCs.unlock();
  693. }
  694. void Dimension::updateLightningWithoutWait(Framework::Vec3<int> location)
  695. {
  696. prioLightCs.lock();
  697. priorizedLightUpdateQueue.add(location, 0);
  698. prioLightCs.unlock();
  699. }
  700. void Dimension::updateLightAtChunkBorders(Punkt chunkCenter)
  701. {
  702. if (lightUpdateQueue.getEintragAnzahl() > 300000)
  703. {
  704. std::cout
  705. << "warning: light calculation queue is over 300000 blocks long\n";
  706. }
  707. for (int i = WORLD_HEIGHT - 1; i >= 0; i--)
  708. {
  709. for (int j = 0; j < CHUNK_SIZE; j++)
  710. {
  711. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 - 1,
  712. chunkCenter.y - CHUNK_SIZE / 2 + j,
  713. i));
  714. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2,
  715. chunkCenter.y - CHUNK_SIZE / 2 + j,
  716. i));
  717. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2 - 1,
  718. chunkCenter.y - CHUNK_SIZE / 2 + j,
  719. i));
  720. updateLightning(Vec3<int>(chunkCenter.x + CHUNK_SIZE / 2,
  721. chunkCenter.y - CHUNK_SIZE / 2 + j,
  722. i));
  723. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  724. chunkCenter.y - CHUNK_SIZE / 2 - 1,
  725. i));
  726. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  727. chunkCenter.y - CHUNK_SIZE / 2,
  728. i));
  729. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  730. chunkCenter.y + CHUNK_SIZE / 2 - 1,
  731. i));
  732. updateLightning(Vec3<int>(chunkCenter.x - CHUNK_SIZE / 2 + j,
  733. chunkCenter.y + CHUNK_SIZE / 2,
  734. i));
  735. }
  736. }
  737. }
  738. __int64 Dimension::getNextStructureId()
  739. {
  740. return nextStructureId++;
  741. }
  742. void Dimension::addStructure(MultiblockStructure* structure)
  743. {
  744. structurCs.lock();
  745. structures.add(structure);
  746. structurCs.unlock();
  747. }
  748. MultiblockStructure* Dimension::zStructureByPosition(
  749. Framework::Vec3<int> uniquePosition)
  750. {
  751. structurCs.lock();
  752. for (MultiblockStructure* str : structures)
  753. {
  754. if (str->getUniquePosition() == uniquePosition)
  755. {
  756. structurCs.unlock();
  757. return str;
  758. }
  759. }
  760. // search for structure file
  761. Datei dir(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(dimensionId)
  762. + "/structures");
  763. RCArray<Text>* names = dir.getDateiListe();
  764. if (names)
  765. {
  766. Vec3<int> uPos;
  767. for (Text* name : *names)
  768. {
  769. Datei d(Text(dir.zPfad()->getText()) + "/" + name->getText());
  770. if (d.open(Datei::Style::lesen))
  771. {
  772. d.lese((char*)&uPos.x, 4);
  773. d.lese((char*)&uPos.y, 4);
  774. d.lese((char*)&uPos.z, 4);
  775. if (uPos == uniquePosition)
  776. {
  777. int type;
  778. d.lese((char*)&type, 4);
  779. __int64 strId;
  780. d.lese((char*)&strId, 8);
  781. MultiblockStructure* str
  782. = StaticRegistry<MultiblockStructureType>::INSTANCE
  783. .zElement(type)
  784. ->loadStructure(
  785. dimensionId, strId, uniquePosition, &d);
  786. d.close();
  787. structures.add(str);
  788. names->release();
  789. structurCs.unlock();
  790. return str;
  791. }
  792. d.close();
  793. }
  794. }
  795. names->release();
  796. }
  797. structurCs.unlock();
  798. return 0;
  799. }
  800. MultiblockStructure* Dimension::zStructureById(__int64 id)
  801. {
  802. structurCs.lock();
  803. for (MultiblockStructure* str : structures)
  804. {
  805. if (str->getStructureId() == id)
  806. {
  807. structurCs.unlock();
  808. return str;
  809. }
  810. }
  811. // search for structure file
  812. Text path = Game::INSTANCE->getWorldDirectory() + "/dim/"
  813. + Text(dimensionId) + "/structures/";
  814. path.appendHex(id);
  815. path += ".str";
  816. Datei d(path);
  817. Vec3<int> uPos;
  818. if (d.open(Datei::Style::lesen))
  819. {
  820. d.lese((char*)&uPos.x, 4);
  821. d.lese((char*)&uPos.y, 4);
  822. d.lese((char*)&uPos.z, 4);
  823. int type;
  824. d.lese((char*)&type, 4);
  825. __int64 strId;
  826. d.lese((char*)&strId, 8);
  827. MultiblockStructure* str
  828. = StaticRegistry<MultiblockStructureType>::INSTANCE.zElement(type)
  829. ->loadStructure(dimensionId, strId, uPos, &d);
  830. d.close();
  831. structures.add(str);
  832. structurCs.unlock();
  833. return str;
  834. }
  835. std::cout << "WARNING: did not find Structure information file '" << path
  836. << "'.\n";
  837. structurCs.unlock();
  838. return 0;
  839. }
  840. void Dimension::requestStopAndWait()
  841. {
  842. stop = 1;
  843. warteAufThread(1000000);
  844. }
  845. void Dimension::updateMap(int x, int y, int height)
  846. {
  847. chunkCs.lock();
  848. int h1 = height % 2 == 0 ? height : height - 1;
  849. int h2 = h1 + 1;
  850. const Block* b1 = zBlockOrDefault({x, y, h1});
  851. const Block* b2 = zBlockOrDefault({x, y, h2});
  852. bool visible = 1;
  853. if (h2 != WORLD_HEIGHT - 1)
  854. {
  855. const Block* b3 = zBlockOrDefault({x, y, h2 + 1});
  856. visible = b3->isPassable() || b3->isTransparent();
  857. }
  858. int color1
  859. = (b2->isPassable() || b2->isTransparent()) ? b1->getMapColor() : 0;
  860. int color2 = visible ? b2->getMapColor() : 0;
  861. char addr[8];
  862. Punkt center = Game::INSTANCE->getChunkCenter(x, y);
  863. getAddrOfWorld(center, addr);
  864. ChunkMap* cMap = map->getMap(addr, 8, center);
  865. if (cMap)
  866. {
  867. x = x % CHUNK_SIZE;
  868. y = y % CHUNK_SIZE;
  869. if (x < 0) x += CHUNK_SIZE;
  870. if (y < 0) y += CHUNK_SIZE;
  871. cMap->update(
  872. (char)x, (char)y, (unsigned char)(height / 2), color1, color2);
  873. }
  874. chunkCs.unlock();
  875. }
  876. int Dimension::getChunkCount() {
  877. return chunkList.getEintragAnzahl();
  878. }