Game.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. #include "Game.h"
  2. #include "Zeit.h"
  3. #include "Player.h"
  4. #include "OverworldDimension.h"
  5. #include "AddChunkUpdate.h"
  6. #include "NoBlock.h"
  7. #include "AsynchronCall.h"
  8. #include "Entity.h"
  9. #include "AddEntityUpdate.h"
  10. #include "EntityRemovedUpdate.h"
  11. using namespace Framework;
  12. GameClient::GameClient(Player* zPlayer, FCKlient* client)
  13. : ReferenceCounter(),
  14. zPlayer(zPlayer),
  15. client(client),
  16. viewDistance(DEFAULT_VIEW_DISTANCE),
  17. first(1),
  18. online(1),
  19. finished(0)
  20. {
  21. new AsynchronCall("Game Client", [this]()
  22. {
  23. while (online)
  24. {
  25. other.lock();
  26. if (updateQueue.hat(0))
  27. {
  28. WorldUpdate* update = updateQueue.get(0);
  29. updateQueue.remove(0);
  30. other.unlock();
  31. background.lock();
  32. this->client->zBackgroundWriter()->schreibe((char*)&Message::WORLD_UPDATE, 1);
  33. update->writeAndCheck(this->client->zBackgroundWriter());
  34. background.unlock();
  35. update->release();
  36. }
  37. else
  38. {
  39. other.unlock();
  40. updateSync.wait();
  41. }
  42. }
  43. finished = 1;
  44. });
  45. }
  46. GameClient::~GameClient()
  47. {
  48. online = 0;
  49. updateSync.notify();
  50. while (!finished)
  51. Sleep(100);
  52. client->release();
  53. }
  54. void GameClient::sendWorldUpdate(WorldUpdate* update)
  55. {
  56. bool add = 0;
  57. if (zPlayer->getCurrentDimensionId() == update->getAffectedDimension())
  58. {
  59. auto pos = (Vec3<int>)zPlayer->getPosition();
  60. int dist = update->distanceTo(pos.x, pos.y);
  61. if (dist < viewDistance * CHUNK_SIZE)
  62. {
  63. other.lock();
  64. int index = 0;
  65. for (auto update2 : updateQueue)
  66. {
  67. int dist2 = update2->distanceTo(pos.x, pos.y);
  68. if (dist2 > dist)
  69. break;
  70. index++;
  71. }
  72. if (update->getType() == AddChunkUpdateType::ID)
  73. ((AddChunkUpdate*)update)->zChunk()->addView(zPlayer);
  74. updateQueue.add(update, index);
  75. other.unlock();
  76. updateSync.notify();
  77. add = 1;
  78. }
  79. }
  80. if (!add)
  81. update->release();
  82. }
  83. void GameClient::reply()
  84. {
  85. other.lock();
  86. for (auto req : requests)
  87. Game::INSTANCE->api(req, this);
  88. requests.leeren();
  89. other.unlock();
  90. int x = (int)floor(zPlayer->getPosition().x);
  91. int y = (int)floor(zPlayer->getPosition().y);
  92. int d = zPlayer->getCurrentDimensionId();
  93. // send world to client
  94. if (first)
  95. {
  96. foreground.lock();
  97. int id = zPlayer->getId();
  98. client->zForegroundWriter()->schreibe((char*)&Message::POSITION_UPDATE, 1);
  99. client->zForegroundWriter()->schreibe((char*)&id, 4);
  100. foreground.unlock();
  101. first = 0;
  102. Dimension* dim = Game::INSTANCE->zDimension(d);
  103. if (dim)
  104. {
  105. for (int xP = x - CHUNK_SIZE * viewDistance; xP <= x + CHUNK_SIZE * viewDistance; xP += CHUNK_SIZE)
  106. {
  107. for (int yP = y - CHUNK_SIZE * viewDistance; yP <= y + CHUNK_SIZE * viewDistance; yP += CHUNK_SIZE)
  108. {
  109. Chunk* chunk = dim->zChunk(Game::INSTANCE->getChunkCenter(xP, yP));
  110. if (chunk)
  111. sendWorldUpdate(new AddChunkUpdate(dynamic_cast<Chunk*>(chunk->getThis())));
  112. }
  113. }
  114. }
  115. Game::INSTANCE->requestArea({ x - CHUNK_SIZE * viewDistance, y - CHUNK_SIZE * viewDistance, x + CHUNK_SIZE * viewDistance, y + CHUNK_SIZE * viewDistance, d });
  116. }
  117. else
  118. {
  119. Punkt lastMin = Game::INSTANCE->getChunkCenter((int)floor(lastPos.x) - CHUNK_SIZE * viewDistance, (int)floor(lastPos.y) - CHUNK_SIZE * viewDistance);
  120. Punkt curMin = Game::INSTANCE->getChunkCenter(x - CHUNK_SIZE * viewDistance, y - CHUNK_SIZE * viewDistance);
  121. Punkt lastMax = Game::INSTANCE->getChunkCenter((int)floor(lastPos.x) + CHUNK_SIZE * viewDistance, (int)floor(lastPos.y) + CHUNK_SIZE * viewDistance);
  122. Punkt curMax = Game::INSTANCE->getChunkCenter(x + CHUNK_SIZE * viewDistance, y + CHUNK_SIZE * viewDistance);
  123. Dimension* dim = Game::INSTANCE->zDimension(d);
  124. if (dim)
  125. {
  126. for (int xP = curMin.x; xP <= curMax.x; xP += CHUNK_SIZE)
  127. {
  128. for (int yP = curMin.y; yP <= curMax.y; yP += CHUNK_SIZE)
  129. {
  130. if (xP < lastMin.x || xP > lastMax.x || yP < lastMin.y || yP > lastMax.y)
  131. {
  132. Chunk* chunk = dim->zChunk(Game::INSTANCE->getChunkCenter(xP, yP));
  133. if (chunk)
  134. sendWorldUpdate(new AddChunkUpdate(dynamic_cast<Chunk*>(chunk->getThis())));
  135. else
  136. Game::INSTANCE->requestArea(Game::INSTANCE->getChunckArea(Game::INSTANCE->getChunkCenter(xP, yP)));
  137. }
  138. }
  139. }
  140. for (int xP = lastMin.x; xP <= lastMax.x; xP += CHUNK_SIZE)
  141. {
  142. for (int yP = lastMin.y; yP <= lastMax.y; yP += CHUNK_SIZE)
  143. {
  144. if (xP < curMin.x || xP > curMax.x || yP < curMin.y || yP > curMax.y)
  145. {
  146. Chunk* chunk = dim->zChunk(Game::INSTANCE->getChunkCenter(xP, yP));
  147. if (chunk)
  148. chunk->removeView(zPlayer);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. lastPos = zPlayer->getPosition();
  155. }
  156. void GameClient::logout()
  157. {
  158. online = 0;
  159. }
  160. void GameClient::addMessage(StreamReader* reader)
  161. {
  162. short len = 0;
  163. reader->lese((char*)&len, 2);
  164. InMemoryBuffer* buffer = new InMemoryBuffer();
  165. char* tmp = new char[len];
  166. reader->lese(tmp, len);
  167. buffer->schreibe(tmp, len);
  168. delete[]tmp;
  169. other.lock();
  170. requests.add(buffer);
  171. other.unlock();
  172. }
  173. bool GameClient::isOnline() const
  174. {
  175. return online;
  176. }
  177. void GameClient::sendResponse(NetworkMessage* zResponse)
  178. {
  179. if (zResponse->isAreaAffected({ lastPos.x - (float)CHUNK_SIZE * (float)viewDistance, lastPos.y - (float)CHUNK_SIZE * (float)viewDistance, 0.f }, { lastPos.x + (float)CHUNK_SIZE * (float)viewDistance, lastPos.y + (float)CHUNK_SIZE * (float)viewDistance, (float)WORLD_HEIGHT }, zPlayer->getCurrentDimensionId()))
  180. {
  181. if (zResponse->isUseBackground())
  182. {
  183. background.lock();
  184. zResponse->writeTo(client->zBackgroundWriter());
  185. background.unlock();
  186. }
  187. else
  188. {
  189. foreground.lock();
  190. zResponse->writeTo(client->zForegroundWriter());
  191. foreground.unlock();
  192. }
  193. }
  194. }
  195. Player* GameClient::zEntity() const
  196. {
  197. return zPlayer;
  198. }
  199. void GameClient::sendTypes()
  200. {
  201. foreground.lock();
  202. int count = StaticRegistry<BlockType>::INSTANCE.getCount();
  203. client->zForegroundWriter()->schreibe((char*)&count, 4);
  204. for (int i = 0; i < count; i++)
  205. {
  206. BlockType* t = StaticRegistry<BlockType>::INSTANCE.zElement(i);
  207. int id = t->getId();
  208. client->zForegroundWriter()->schreibe((char*)&id, 4);
  209. bool inst = t->doesNeedClientInstance();
  210. client->zForegroundWriter()->schreibe((char*)&inst, 1);
  211. int maxHp = t->getInitialMaxHP();
  212. client->zForegroundWriter()->schreibe((char*)&maxHp, 4);
  213. t->getModel().writeTo(client->zForegroundWriter());
  214. }
  215. count = StaticRegistry<ItemType>::INSTANCE.getCount();
  216. client->zForegroundWriter()->schreibe((char*)&count, 4);
  217. for (int i = 0; i < count; i++)
  218. {
  219. ItemType* t = StaticRegistry<ItemType>::INSTANCE.zElement(i);
  220. int id = t->getId();
  221. client->zForegroundWriter()->schreibe((char*)&id, 4);
  222. t->getModel().writeTo(client->zForegroundWriter());
  223. }
  224. count = StaticRegistry<EntityType>::INSTANCE.getCount();
  225. client->zForegroundWriter()->schreibe((char*)&count, 4);
  226. for (int i = 0; i < count; i++)
  227. {
  228. EntityType* t = StaticRegistry<EntityType>::INSTANCE.zElement(i);
  229. int id = t->getId();
  230. client->zForegroundWriter()->schreibe((char*)&id, 4);
  231. t->getModel().writeTo(client->zForegroundWriter());
  232. }
  233. foreground.unlock();
  234. }
  235. Game::Game(Framework::Text name, Framework::Text worldsDir)
  236. : Thread(),
  237. name(name),
  238. dimensions(new RCArray<Dimension>()),
  239. updates(new RCArray<WorldUpdate>()),
  240. clients(new RCArray<GameClient>()),
  241. ticker(new TickOrganizer()),
  242. path((const char*)(worldsDir + "/" + name)),
  243. stop(0),
  244. tickId(0),
  245. nextEntityId(0),
  246. generator(0),
  247. loader(0),
  248. totalTickTime(0),
  249. tickCounter(0)
  250. {
  251. if (!DateiExistiert(worldsDir + "/" + name))
  252. DateiPfadErstellen(worldsDir + "/" + name + "/");
  253. Datei d;
  254. d.setDatei(path + "/eid");
  255. if (d.existiert())
  256. {
  257. d.open(Datei::Style::lesen);
  258. d.lese((char*)&nextEntityId, 4);
  259. d.close();
  260. }
  261. start();
  262. }
  263. Game::~Game()
  264. {
  265. dimensions->release();
  266. updates->release();
  267. clients->release();
  268. generator->release();
  269. loader->release();
  270. }
  271. void Game::initialize()
  272. {
  273. int seed = 0;
  274. int index = 0;
  275. for (char* n = name; *n; n++)
  276. seed += (int)pow((float)*n * 31, (float)++index);
  277. generator = new WorldGenerator(seed);
  278. loader = new WorldLoader();
  279. recipies.loadRecipies("data/recipies");
  280. }
  281. void Game::thread()
  282. {
  283. ZeitMesser m;
  284. while (!stop)
  285. {
  286. m.messungStart();
  287. ticker->nextTick();
  288. Array<int> removed;
  289. cs.lock();
  290. int index = 0;
  291. for (auto player : *clients)
  292. {
  293. if (!player->isOnline())
  294. {
  295. std::cout << "player " << player->zEntity()->getName() << " disconnected.\n";
  296. Datei pFile;
  297. pFile.setDatei(path + "/player/" + player->zEntity()->getName());
  298. pFile.erstellen();
  299. if (pFile.open(Datei::Style::schreiben))
  300. PlayerEntityType::INSTANCE->saveEntity(player->zEntity(), &pFile);
  301. pFile.close();
  302. removed.add(index, 0);
  303. this->requestWorldUpdate(new EntityRemovedUpdate(player->zEntity()->getId(), player->zEntity()->getCurrentDimensionId(), player->zEntity()->getPosition()));
  304. }
  305. index++;
  306. }
  307. for (auto i : removed)
  308. clients->remove(i);
  309. cs.unlock();
  310. for (auto dim : *dimensions)
  311. dim->tickEntities();
  312. cs.lock();
  313. while (updates->hat(0))
  314. {
  315. WorldUpdate* update = updates->z(0);
  316. for (auto client : *clients)
  317. client->sendWorldUpdate(dynamic_cast<WorldUpdate*>(update->getThis()));
  318. if (!zDimension(update->getAffectedDimension()))
  319. addDimension(new Dimension(update->getAffectedDimension()));
  320. update->onUpdate(zDimension(update->getAffectedDimension()));
  321. updates->remove(0);
  322. }
  323. cs.unlock();
  324. for (auto client : *clients)
  325. client->reply();
  326. cs.lock();
  327. for (auto dim : *dimensions)
  328. dim->removeOldChunks();
  329. cs.unlock();
  330. m.messungEnde();
  331. double sec = m.getSekunden();
  332. tickCounter++;
  333. totalTickTime += sec;
  334. if (tickCounter >= 1000)
  335. {
  336. std::cout << "Average Tick time: " << (totalTickTime / 1000) << "\n";
  337. if ((totalTickTime / 1000) * 20 > 1)
  338. {
  339. std::cout << "The game runns slower than normal.\n";
  340. }
  341. else
  342. {
  343. std::cout << "No performance issues detected.\n";
  344. }
  345. totalTickTime = 0;
  346. tickCounter = 0;
  347. }
  348. if (sec < 0.05)
  349. Sleep((int)((0.05 - sec) * 1000));
  350. else
  351. {
  352. std::cout << "WARNING: tick needed " << sec << " seconds. The game will run sower then normal.\n";
  353. }
  354. }
  355. save();
  356. }
  357. void Game::api(Framework::StreamReader* zRequest, GameClient* zOrigin)
  358. {
  359. char type;
  360. zRequest->lese(&type, 1);
  361. NetworkMessage response;
  362. switch (type)
  363. {
  364. case 1: // world
  365. {
  366. int dimensionId;
  367. zRequest->lese((char*)&dimensionId, 4);
  368. Dimension* dim = zDimension(dimensionId);
  369. if (!dim)
  370. {
  371. dim = new Dimension(dimensionId);
  372. addDimension(dim);
  373. }
  374. dim->api(zRequest, &response);
  375. break;
  376. }
  377. case 2: // player
  378. zOrigin->zEntity()->playerApi(zRequest, &response);
  379. break;
  380. case 3: // entity
  381. {
  382. int id;
  383. zRequest->lese((char*)&id, 4);
  384. for (Dimension* dim : *dimensions)
  385. {
  386. Entity* entity = dim->zEntity(id);
  387. if (entity)
  388. {
  389. entity->api(zRequest, &response);
  390. break;
  391. }
  392. }
  393. break;
  394. }
  395. case 4:
  396. { // inventory
  397. bool isEntity;
  398. zRequest->lese((char*)&isEntity, 1);
  399. Inventory* target;
  400. if (isEntity)
  401. {
  402. int id;
  403. zRequest->lese((char*)&id, 4);
  404. target = zEntity(id);
  405. }
  406. else
  407. {
  408. int dim;
  409. Vec3<int> pos;
  410. zRequest->lese((char*)&dim, 4);
  411. zRequest->lese((char*)&pos.x, 4);
  412. zRequest->lese((char*)&pos.y, 4);
  413. zRequest->lese((char*)&pos.z, 4);
  414. target = zBlockAt(pos, dim);
  415. }
  416. if (target)
  417. target->inventoryApi(zRequest, &response, zOrigin->zEntity());
  418. break;
  419. }
  420. default:
  421. std::cout << "received unknown api request in game with type " << (int)type << "\n";
  422. }
  423. if (!response.isEmpty())
  424. {
  425. if (response.isBroadcast())
  426. broadcastMessage(&response);
  427. else
  428. zOrigin->sendResponse(&response);
  429. }
  430. }
  431. void Game::broadcastMessage(NetworkMessage* zResponse)
  432. {
  433. for (auto client : *clients)
  434. client->sendResponse(zResponse);
  435. }
  436. void Game::sendMessage(NetworkMessage* zResponse, Entity* zTargetPlayer)
  437. {
  438. for (auto client : *clients)
  439. {
  440. if (client->zEntity()->getId() == zTargetPlayer->getId())
  441. {
  442. client->sendResponse(zResponse);
  443. break;
  444. }
  445. }
  446. }
  447. bool Game::requestWorldUpdate(WorldUpdate* update)
  448. {
  449. cs.lock();
  450. for (WorldUpdate* u : *updates)
  451. {
  452. if (u->getMaxAffectedPoint().x >= update->getMinAffectedPoint().x && u->getMinAffectedPoint().x <= update->getMaxAffectedPoint().x &&
  453. u->getMaxAffectedPoint().y >= update->getMinAffectedPoint().y && u->getMinAffectedPoint().y <= update->getMaxAffectedPoint().y &&
  454. u->getMaxAffectedPoint().z >= update->getMinAffectedPoint().z && u->getMinAffectedPoint().z <= update->getMaxAffectedPoint().z && u->getType() == update->getType())
  455. {
  456. cs.unlock();
  457. update->release();
  458. return 0;
  459. }
  460. }
  461. updates->add(update);
  462. cs.unlock();
  463. return 1;
  464. }
  465. GameClient* Game::addPlayer(FCKlient* client, Framework::Text name)
  466. {
  467. cs.lock();
  468. Datei pFile;
  469. pFile.setDatei(path + "/player/" + name);
  470. std::cout << "player " << name.getText() << " connected.\n";
  471. Player* player;
  472. bool isNew = 0;
  473. if (!pFile.existiert() || !pFile.open(Datei::Style::lesen))
  474. {
  475. player = (Player*)PlayerEntityType::INSTANCE->createEntityAt(Vec3<float>(0.5, 0.5, 0), OverworldDimension::ID);
  476. player->setName(name);
  477. isNew = 1;
  478. }
  479. else
  480. {
  481. player = (Player*)PlayerEntityType::INSTANCE->loadEntity(&pFile);
  482. pFile.close();
  483. }
  484. GameClient* gameClient = new GameClient(player, client);
  485. gameClient->sendTypes();
  486. clients->add(gameClient);
  487. requestArea(getChunckArea(getChunkCenter((int)player->getPosition().x, (int)player->getPosition().y)));
  488. while (!zDimension(player->getCurrentDimensionId()) || (isNew && !zDimension(player->getCurrentDimensionId())->zChunk(getChunkCenter((int)player->getPosition().x, (int)player->getPosition().y))))
  489. {
  490. cs.unlock();
  491. Sleep(1000);
  492. cs.lock();
  493. }
  494. if (isNew)
  495. {
  496. Either<Block*, int> b = AirBlockBlockType::ID;
  497. int h = WORLD_HEIGHT;
  498. while (((b.isA() && (!(Block*)b || ((Block*)b)->isPassable())) || (b.isB() && StaticRegistry<BlockType>::INSTANCE.zElement(b)->zDefault()->isPassable())) && h > 0)
  499. b = zBlockAt({ (int)player->getPosition().x, (int)player->getPosition().y, --h }, player->getCurrentDimensionId());
  500. player->setPosition({ player->getPosition().x, player->getPosition().y, (float)h + 1.f });
  501. }
  502. requestWorldUpdate(new AddEntityUpdate(player, player->getCurrentDimensionId()));
  503. cs.unlock();
  504. return dynamic_cast<GameClient*>(gameClient->getThis());
  505. }
  506. bool Game::isChunkLoaded(int x, int y, int dimension) const
  507. {
  508. Dimension* dim = zDimension(dimension);
  509. return (dim && dim->hasChunck(x, y));
  510. }
  511. bool Game::doesChunkExist(int x, int y, int dimension)
  512. {
  513. cs.lock();
  514. bool result = isChunkLoaded(x, y, dimension) || loader->existsChunk(x, y, dimension);
  515. if (!result)
  516. {
  517. for (WorldUpdate* update : *updates)
  518. {
  519. if (update->getType() == AddChunkUpdateType::ID)
  520. result |= ((AddChunkUpdate*)update)->zChunk()->getCenter() == Framework::Punkt(x, y);
  521. }
  522. }
  523. cs.unlock();
  524. return result;
  525. }
  526. Framework::Either<Block*, int> Game::zBlockAt(Framework::Vec3<int> location, int dimension) const
  527. {
  528. Dimension* dim = zDimension(dimension);
  529. if (dim)
  530. return dim->zBlock(location);
  531. return 0;
  532. }
  533. Block* Game::zRealBlockInstance(Framework::Vec3<int> location, int dimension)
  534. {
  535. Dimension* dim = zDimension(dimension);
  536. if (dim)
  537. return dim->zRealBlockInstance(location);
  538. return 0;
  539. }
  540. Dimension* Game::zDimension(int id) const
  541. {
  542. for (auto dim : *dimensions)
  543. {
  544. if (dim->getDimensionId() == id)
  545. return dim;
  546. }
  547. return 0;
  548. }
  549. Framework::Punkt Game::getChunkCenter(int x, int y)
  550. {
  551. return Punkt(((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2, ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2);
  552. }
  553. Area Game::getChunckArea(Punkt center) const
  554. {
  555. return { center.x - CHUNK_SIZE / 2, center.y - CHUNK_SIZE / 2, center.x + CHUNK_SIZE / 2 - 1, center.y + CHUNK_SIZE / 2 - 1, 0 };
  556. }
  557. Framework::Text Game::getWorldDirectory() const
  558. {
  559. return path;
  560. }
  561. void Game::requestArea(Area area)
  562. {
  563. generator->requestGeneration(area);
  564. loader->requestLoading(area);
  565. }
  566. void Game::save() const
  567. {
  568. Datei d;
  569. d.setDatei(path + "/eid");
  570. d.open(Datei::Style::schreiben);
  571. d.schreibe((char*)&nextEntityId, 4);
  572. d.close();
  573. for (auto dim : *dimensions)
  574. dim->save(path);
  575. }
  576. void Game::requestStop()
  577. {
  578. stop = 1;
  579. warteAufThread(1000000);
  580. }
  581. void Game::addDimension(Dimension* d)
  582. {
  583. dimensions->add(d);
  584. }
  585. int Game::getNextEntityId()
  586. {
  587. cs.lock();
  588. int result = nextEntityId++;
  589. cs.unlock();
  590. return result;
  591. }
  592. WorldGenerator* Game::zGenerator() const
  593. {
  594. return generator;
  595. }
  596. Game* Game::INSTANCE = 0;
  597. void Game::initialize(Framework::Text name, Framework::Text worldsDir)
  598. {
  599. if (!Game::INSTANCE)
  600. {
  601. Game::INSTANCE = new Game(name, worldsDir);
  602. Game::INSTANCE->initialize();
  603. }
  604. }
  605. Entity* Game::zEntity(int id, int dimensionId) const
  606. {
  607. Dimension* d = zDimension(dimensionId);
  608. if (d)
  609. return d->zEntity(id);
  610. return 0;
  611. }
  612. Entity* Game::zEntity(int id) const
  613. {
  614. for (Dimension* d : *dimensions)
  615. {
  616. Entity* e = d->zEntity(id);
  617. if (e)
  618. return e;
  619. }
  620. return 0;
  621. }
  622. Entity* Game::zNearestEntity(int dimensionId, Framework::Vec3<float> pos, std::function<bool(Entity*)> filter)
  623. {
  624. Dimension* d = zDimension(dimensionId);
  625. if (!d)
  626. return 0;
  627. return d->zNearestEntity(pos, filter);
  628. }
  629. const RecipieLoader& Game::getRecipies() const
  630. {
  631. return recipies;
  632. }