Game.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. #include "Game.h"
  2. #include "AddEntityUpdate.h"
  3. #include "AsynchronCall.h"
  4. #include "Chat.h"
  5. #include "Dimension.h"
  6. #include "Entity.h"
  7. #include "EntityRemovedUpdate.h"
  8. #include "ItemEntity.h"
  9. #include "JsonUtils.h"
  10. #include "MultiblockTree.h"
  11. #include "NetworkMessage.h"
  12. #include "NoBlock.h"
  13. #include "Player.h"
  14. #include "PlayerHand.h"
  15. #include "PlayerRegister.h"
  16. #include "Quest.h"
  17. #include "RecipieLoader.h"
  18. #include "Server.h"
  19. #include "TickOrganizer.h"
  20. #include "UIController.h"
  21. #include "WorldGenerator.h"
  22. #include "WorldLoader.h"
  23. #include "Zeit.h"
  24. using namespace Framework;
  25. GameClient::GameClient(Player* zPlayer, FCKlient* client)
  26. : Thread(),
  27. zPlayer(zPlayer),
  28. client(client),
  29. viewDistance(DEFAULT_VIEW_DISTANCE),
  30. first(1),
  31. online(1),
  32. finished(0),
  33. backgroundFinished(0),
  34. foregroundFinished(0)
  35. {
  36. new AsynchronCall("Game Client Updates", [this]() {
  37. while (online)
  38. {
  39. other.lock();
  40. if (updateQueue.hat(0))
  41. {
  42. WorldUpdate* update = updateQueue.get(0);
  43. updateQueue.remove(0);
  44. other.unlock();
  45. background.lock();
  46. this->client->zBackgroundWriter()->schreibe(
  47. (char*)&Message::WORLD_UPDATE, 1);
  48. update->writeAndCheck(this->client->zBackgroundWriter());
  49. background.unlock();
  50. update->release();
  51. }
  52. else
  53. {
  54. other.unlock();
  55. updateSync.wait();
  56. }
  57. }
  58. finished = 1;
  59. });
  60. start();
  61. }
  62. GameClient::~GameClient()
  63. {
  64. online = 0;
  65. updateSync.notify();
  66. emptyForegroundQueueSync.notifyAll();
  67. emptyBackgroundQueueSync.notifyAll();
  68. foregroundQueueSync.notify();
  69. backgroundQueueSync.notify();
  70. while (!finished || !foregroundFinished || !backgroundFinished)
  71. Sleep(100);
  72. client->release();
  73. }
  74. void GameClient::thread()
  75. {
  76. new AsynchronCall("Game Client Background", [this]() {
  77. while (online)
  78. {
  79. queueCs.lock();
  80. if (backgroundQueue.hat(0))
  81. {
  82. NetworkMessage* message = backgroundQueue.get(0);
  83. backgroundQueue.remove(0);
  84. queueCs.unlock();
  85. background.lock();
  86. message->writeTo(client->zBackgroundWriter());
  87. background.unlock();
  88. message->release();
  89. }
  90. else
  91. {
  92. queueCs.unlock();
  93. emptyBackgroundQueueSync.notifyAll();
  94. while (!backgroundQueueSync.wait(1000))
  95. {
  96. emptyBackgroundQueueSync.notifyAll();
  97. }
  98. }
  99. }
  100. backgroundFinished = 1;
  101. });
  102. while (online)
  103. {
  104. queueCs.lock();
  105. if (foregroundQueue.hat(0))
  106. {
  107. NetworkMessage* message = foregroundQueue.get(0);
  108. foregroundQueue.remove(0);
  109. queueCs.unlock();
  110. foreground.lock();
  111. message->writeTo(client->zForegroundWriter());
  112. foreground.unlock();
  113. message->release();
  114. }
  115. else
  116. {
  117. queueCs.unlock();
  118. emptyForegroundQueueSync.notifyAll();
  119. while (!foregroundQueueSync.wait(1000))
  120. {
  121. emptyForegroundQueueSync.notifyAll();
  122. }
  123. }
  124. }
  125. foregroundFinished = 1;
  126. }
  127. void GameClient::sendWorldUpdate(WorldUpdate* update)
  128. {
  129. bool add = 0;
  130. if (zPlayer->getDimensionId() == update->getAffectedDimension())
  131. {
  132. auto pos = (Vec3<int>)zPlayer->getPosition();
  133. int dist = update->distanceTo(pos.x, pos.y);
  134. if (dist < viewDistance * CHUNK_SIZE)
  135. {
  136. other.lock();
  137. updateQueue.add(update);
  138. other.unlock();
  139. updateSync.notify();
  140. add = 1;
  141. }
  142. }
  143. if (!add) update->release();
  144. }
  145. void GameClient::reply()
  146. {
  147. other.lock();
  148. for (auto req : requests)
  149. Game::INSTANCE->api(req, this);
  150. requests.leeren();
  151. other.unlock();
  152. if (first)
  153. {
  154. foreground.lock();
  155. int id = zPlayer->getId();
  156. client->zForegroundWriter()->schreibe(
  157. (char*)&Message::POSITION_UPDATE, 1);
  158. client->zForegroundWriter()->schreibe((char*)&id, 4);
  159. id = zPlayer->getDimensionId();
  160. client->zForegroundWriter()->schreibe((char*)&id, 4);
  161. client->zForegroundWriter()->schreibe((char*)&Message::API_MESSAGE, 1);
  162. int len = 10;
  163. client->zForegroundWriter()->schreibe((char*)&len, 4);
  164. client->zForegroundWriter()->schreibe("\1", 1);
  165. client->zForegroundWriter()->schreibe((char*)&id, 4);
  166. client->zForegroundWriter()->schreibe("\6", 1);
  167. float gravity = Game::INSTANCE->zDimension(zPlayer->getDimensionId())
  168. ->getGravity();
  169. client->zForegroundWriter()->schreibe((char*)&gravity, 4);
  170. foreground.unlock();
  171. first = 0;
  172. }
  173. }
  174. void GameClient::logout()
  175. {
  176. online = 0;
  177. updateSync.notify();
  178. emptyForegroundQueueSync.notifyAll();
  179. emptyBackgroundQueueSync.notifyAll();
  180. foregroundQueueSync.notify();
  181. backgroundQueueSync.notify();
  182. }
  183. void GameClient::addMessage(StreamReader* reader)
  184. {
  185. short len = 0;
  186. reader->lese((char*)&len, 2);
  187. InMemoryBuffer* buffer = new InMemoryBuffer();
  188. char* tmp = new char[len];
  189. reader->lese(tmp, len);
  190. buffer->schreibe(tmp, len);
  191. delete[] tmp;
  192. other.lock();
  193. requests.add(buffer);
  194. other.unlock();
  195. }
  196. bool GameClient::isOnline() const
  197. {
  198. return online;
  199. }
  200. void GameClient::sendResponse(NetworkMessage* response)
  201. {
  202. queueCs.lock();
  203. if (response->isUseBackground())
  204. {
  205. if (backgroundQueue.getEintragAnzahl() > 20)
  206. {
  207. queueCs.unlock();
  208. while (!emptyBackgroundQueueSync.wait(1000))
  209. {
  210. backgroundQueueSync.notify();
  211. }
  212. queueCs.lock();
  213. }
  214. backgroundQueue.add(response);
  215. queueCs.unlock();
  216. backgroundQueueSync.notify();
  217. }
  218. else
  219. {
  220. if (foregroundQueue.getEintragAnzahl() > 100)
  221. {
  222. queueCs.unlock();
  223. std::cout << "WARNING: Game paused because nework connection to "
  224. << zPlayer->getName() << " is to slow.\n";
  225. ZeitMesser m;
  226. m.messungStart();
  227. while (foregroundQueue.getEintragAnzahl() > 0)
  228. {
  229. foregroundQueueSync.notify();
  230. emptyForegroundQueueSync.wait(100);
  231. }
  232. m.messungEnde();
  233. std::cout << "WARNING: Game resumed after " << m.getSekunden()
  234. << " seconds.\n";
  235. queueCs.lock();
  236. }
  237. foregroundQueue.add(response);
  238. queueCs.unlock();
  239. foregroundQueueSync.notify();
  240. }
  241. }
  242. Player* GameClient::zEntity() const
  243. {
  244. return zPlayer;
  245. }
  246. void GameClient::sendTypes()
  247. {
  248. foreground.lock();
  249. int count = 0;
  250. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  251. {
  252. if (Game::INSTANCE->zBlockType(i)) count++;
  253. }
  254. client->zForegroundWriter()->schreibe((char*)&count, 4);
  255. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  256. {
  257. const BlockType* t = Game::INSTANCE->zBlockType(i);
  258. if (t)
  259. {
  260. t->writeTypeInfo(client->zForegroundWriter());
  261. }
  262. }
  263. count = 0;
  264. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  265. {
  266. if (Game::INSTANCE->zItemType(i)) count++;
  267. }
  268. client->zForegroundWriter()->schreibe((char*)&count, 4);
  269. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  270. {
  271. const ItemType* t = Game::INSTANCE->zItemType(i);
  272. if (t)
  273. {
  274. int id = t->getId();
  275. client->zForegroundWriter()->schreibe((char*)&id, 4);
  276. char len = (char)t->getName().getLength();
  277. client->zForegroundWriter()->schreibe((char*)&len, 1);
  278. client->zForegroundWriter()->schreibe(t->getName().getText(), len);
  279. short tlen = (short)t->getTooltipUIML().getLength();
  280. client->zForegroundWriter()->schreibe((char*)&tlen, 2);
  281. client->zForegroundWriter()->schreibe(
  282. t->getTooltipUIML().getText(), tlen);
  283. if (t->zModel())
  284. {
  285. t->zModel()->writeTo(client->zForegroundWriter());
  286. }
  287. else
  288. {
  289. ModelInfo("", Framework::RCArray<Framework::Text>(), false, 1.f)
  290. .writeTo(client->zForegroundWriter());
  291. }
  292. }
  293. }
  294. count = 0;
  295. for (int i = 0; i < Game::INSTANCE->getEntityTypeCount(); i++)
  296. {
  297. if (Game::INSTANCE->zEntityType(i)) count++;
  298. }
  299. client->zForegroundWriter()->schreibe((char*)&count, 4);
  300. for (int i = 0; i < count; i++)
  301. {
  302. const EntityType* t = Game::INSTANCE->zEntityType(i);
  303. int id = t->getId();
  304. client->zForegroundWriter()->schreibe((char*)&id, 4);
  305. if (t->zModel())
  306. {
  307. t->zModel()->writeTo(client->zForegroundWriter());
  308. }
  309. else
  310. {
  311. ModelInfo("", Framework::RCArray<Framework::Text>(), false, 1.f)
  312. .writeTo(client->zForegroundWriter());
  313. }
  314. }
  315. foreground.unlock();
  316. }
  317. Game::Game(Framework::Text name, Framework::Text worldsDir)
  318. : Thread(),
  319. name(name),
  320. typeRegistry(new TypeRegistry()),
  321. dimensions(new RCArray<Dimension>()),
  322. updates(new RCArray<WorldUpdate>()),
  323. clients(new RCArray<GameClient>()),
  324. questManager(new QuestManager()),
  325. ticker(new TickOrganizer()),
  326. path((const char*)(worldsDir + "/" + name)),
  327. stop(0),
  328. tickId(0),
  329. nextEntityId(0),
  330. generator(0),
  331. loader(0),
  332. recipies(new RecipieLoader()),
  333. chat(0),
  334. playerRegister(new PlayerRegister(path)),
  335. uiController(new UIController()),
  336. totalTickTime(0),
  337. tickCounter(0),
  338. averageTickTime(0),
  339. ticksPerSecond(0),
  340. totalTime(0),
  341. blockTypes(0),
  342. blockTypeCount(0),
  343. itemTypes(0),
  344. itemTypeCount(0),
  345. entityTypes(0),
  346. entityTypeCount(0),
  347. multiblockStructureTypes(0),
  348. multiblockStructureTypeCount(0)
  349. {
  350. if (!DateiExistiert(path)) DateiPfadErstellen(path + "/");
  351. Datei d;
  352. d.setDatei(path + "/eid");
  353. if (d.existiert())
  354. {
  355. d.open(Datei::Style::lesen);
  356. d.lese((char*)&nextEntityId, 4);
  357. d.close();
  358. }
  359. start();
  360. }
  361. Game::~Game()
  362. {
  363. dimensions->release();
  364. updates->release();
  365. clients->release();
  366. generator->release();
  367. loader->release();
  368. chat->release();
  369. playerRegister->release();
  370. typeRegistry->release();
  371. uiController->release();
  372. recipies->release();
  373. for (int i = 0; i < blockTypeCount; i++)
  374. {
  375. if (blockTypes[i]) blockTypes[i]->release();
  376. }
  377. delete[] blockTypes;
  378. for (int i = 0; i < itemTypeCount; i++)
  379. {
  380. if (itemTypes[i]) itemTypes[i]->release();
  381. }
  382. delete[] itemTypes;
  383. for (int i = 0; i < entityTypeCount; i++)
  384. {
  385. if (entityTypes[i]) entityTypes[i]->release();
  386. }
  387. delete[] entityTypes;
  388. for (int i = 0; i < multiblockStructureTypeCount; i++)
  389. {
  390. if (multiblockStructureTypes[i]) multiblockStructureTypes[i]->release();
  391. }
  392. delete[] multiblockStructureTypes;
  393. }
  394. void Game::initialize()
  395. {
  396. // TODO load mods libraries
  397. // load block types
  398. std::cout << "Loading block types\n";
  399. Framework::Array<BlockType*> blockTypeArray;
  400. Framework::JSON::Validator::JSONValidator* validator
  401. = Framework::JSON::Validator::JSONValidator::buildForArray()
  402. ->addAcceptedTypeInArray(typeRegistry->getValidator<BlockType>())
  403. ->removeInvalidEntries()
  404. ->finishArray();
  405. loadAllJsonsFromDirectory("data/blocks",
  406. [this, &blockTypeArray, validator](
  407. Framework::JSON::JSONValue* zValue, Framework::Text path) {
  408. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  409. validationResults;
  410. Framework::JSON::JSONValue* validParts
  411. = validator->getValidParts(zValue, &validationResults);
  412. for (Framework::JSON::Validator::JSONValidationResult* result :
  413. validationResults)
  414. {
  415. result->printInvalidInfo();
  416. }
  417. if (validParts)
  418. {
  419. for (Framework::JSON::JSONValue* value : *validParts->asArray())
  420. {
  421. BlockType* blockType
  422. = typeRegistry->fromJson<BlockType>(value);
  423. if (blockType)
  424. {
  425. blockTypeArray.add(blockType);
  426. }
  427. }
  428. validParts->release();
  429. }
  430. });
  431. validator->release();
  432. std::cout << "Loaded " << blockTypeArray.getEintragAnzahl()
  433. << " block types from data/blocks\n";
  434. blockTypes = new BlockType*[2 + blockTypeArray.getEintragAnzahl()];
  435. blockTypes[0]
  436. = new NoBlockBlockType(&NoBlock::INSTANCE, "__not_yet_generated");
  437. blockTypes[1] = new NoBlockBlockType(&AirBlock::INSTANCE, "Air");
  438. blockTypeCount = 2;
  439. for (BlockType* blockType : blockTypeArray)
  440. {
  441. blockTypes[blockTypeCount++] = blockType;
  442. }
  443. for (int i = 0; i < blockTypeCount; i++)
  444. {
  445. blockTypes[i]->setTypeId(i);
  446. }
  447. std::cout << "Loading item types\n";
  448. Framework::Array<ItemType*> itemTypeArray;
  449. validator
  450. = Framework::JSON::Validator::JSONValidator::buildForArray()
  451. ->addAcceptedTypeInArray(typeRegistry->getValidator<ItemType>())
  452. ->removeInvalidEntries()
  453. ->finishArray();
  454. loadAllJsonsFromDirectory("data/items",
  455. [this, &itemTypeArray, validator](
  456. Framework::JSON::JSONValue* zValue, Framework::Text path) {
  457. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  458. validationResults;
  459. Framework::JSON::JSONValue* validParts
  460. = validator->getValidParts(zValue, &validationResults);
  461. for (Framework::JSON::Validator::JSONValidationResult* result :
  462. validationResults)
  463. {
  464. result->printInvalidInfo();
  465. }
  466. if (validParts)
  467. {
  468. for (Framework::JSON::JSONValue* value : *validParts->asArray())
  469. {
  470. ItemType* itemType
  471. = typeRegistry->fromJson<ItemType>(value);
  472. if (itemType)
  473. {
  474. itemTypeArray.add(itemType);
  475. }
  476. }
  477. validParts->release();
  478. }
  479. });
  480. validator->release();
  481. std::cout << "Loaded " << itemTypeArray.getEintragAnzahl()
  482. << " item types from data/items\n";
  483. itemTypes
  484. = new ItemType*[blockTypeCount + itemTypeArray.getEintragAnzahl()];
  485. itemTypes[0] = new PlayerHandItemType();
  486. itemTypeCount = 1;
  487. for (int i = 0; i < blockTypeCount; i++)
  488. {
  489. ItemType* itemType = blockTypes[i]->createItemType();
  490. if (itemType)
  491. {
  492. itemTypes[itemTypeCount++] = itemType;
  493. }
  494. }
  495. for (ItemType* itemType : itemTypeArray)
  496. {
  497. itemTypes[itemTypeCount++] = itemType;
  498. }
  499. for (int i = 0; i < itemTypeCount; i++)
  500. {
  501. itemTypes[i]->setTypeId(i);
  502. }
  503. std::cout << "Loading entity types\n";
  504. Framework::Array<EntityType*> entityTypeArray;
  505. /* validator
  506. = Framework::JSON::Validator::JSONValidator::buildForArray()
  507. ->addAcceptedTypeInArray(typeRegistry->getValidator<EntityType>())
  508. ->removeInvalidEntries()
  509. ->finishArray();
  510. loadAllJsonsFromDirectory("data/entities",
  511. [this, &entityTypeArray, validator](
  512. Framework::JSON::JSONValue* zValue, Framework::Text path) {
  513. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  514. validationResults;
  515. Framework::JSON::JSONValue* validParts
  516. = validator->getValidParts(zValue, &validationResults);
  517. for (Framework::JSON::Validator::JSONValidationResult* result :
  518. validationResults)
  519. {
  520. result->printInvalidInfo();
  521. }
  522. if (validParts)
  523. {
  524. for (Framework::JSON::JSONValue* value : *validParts->asArray())
  525. {
  526. EntityType* entityType
  527. = typeRegistry->fromJson<EntityType>(value);
  528. if (entityType)
  529. {
  530. entityTypeArray.add(entityType);
  531. }
  532. }
  533. validParts->release();
  534. }
  535. });
  536. validator->release();*/
  537. std::cout << "Loaded " << entityTypeArray.getEintragAnzahl()
  538. << " entity types from data/entities\n";
  539. entityTypes = new EntityType*[2 + entityTypeArray.getEintragAnzahl()];
  540. entityTypes[0] = new PlayerEntityType();
  541. entityTypes[1] = new ItemEntityType();
  542. entityTypeCount = 2;
  543. for (EntityType* entityType : entityTypeArray)
  544. {
  545. entityTypes[entityTypeCount++] = entityType;
  546. }
  547. for (int i = 0; i < entityTypeCount; i++)
  548. {
  549. entityTypes[i]->setTypeId(i);
  550. }
  551. // initialize loaded types
  552. bool allInitialized = false;
  553. while (!allInitialized)
  554. {
  555. allInitialized = true;
  556. for (int i = 0; i < blockTypeCount; i++)
  557. {
  558. if (blockTypes[i] && !blockTypes[i]->initialize(this))
  559. {
  560. std::cout << "ERROR: Could not initialize Block Type '"
  561. << blockTypes[i]->getName() << "'.\n";
  562. blockTypes[i]->release();
  563. blockTypes[i] = 0;
  564. allInitialized = false;
  565. }
  566. }
  567. }
  568. allInitialized = false;
  569. while (!allInitialized)
  570. {
  571. allInitialized = true;
  572. for (int i = 0; i < itemTypeCount; i++)
  573. {
  574. if (itemTypes[i] && !itemTypes[i]->initialize(this))
  575. {
  576. std::cout << "ERROR: Could not initialize Item Type '"
  577. << itemTypes[i]->getName() << "'.\n";
  578. itemTypes[i]->release();
  579. itemTypes[i] = 0;
  580. allInitialized = false;
  581. }
  582. }
  583. }
  584. allInitialized = false;
  585. while (!allInitialized)
  586. {
  587. allInitialized = true;
  588. for (int i = 0; i < entityTypeCount; i++)
  589. {
  590. if (entityTypes[i] && !entityTypes[i]->initialize(this))
  591. {
  592. std::cout << "ERROR: Could not initialize Entity Type '"
  593. << entityTypes[i]->getName() << "'.\n";
  594. entityTypes[i]->release();
  595. entityTypes[i] = 0;
  596. allInitialized = false;
  597. }
  598. }
  599. }
  600. for (int i = 0; i < blockTypeCount; i++)
  601. {
  602. if (blockTypes[i])
  603. {
  604. blockTypes[i]->initializeDefault();
  605. }
  606. }
  607. multiblockStructureTypes = new MultiblockStructureType*[1];
  608. multiblockStructureTypes[0] = new MultiblockTreeStructureType();
  609. multiblockStructureTypeCount = 1;
  610. // save syntax info
  611. Framework::DateiRemove("data/syntax");
  612. typeRegistry->writeSyntaxInfo("data/syntax");
  613. // initialize world generator and world loader
  614. int seed = 0;
  615. int index = 0;
  616. for (const char* n = name; *n; n++)
  617. seed += (int)pow((float)*n * 31, (float)++index);
  618. generator = new WorldGenerator(seed);
  619. loader = new WorldLoader();
  620. // load recipies
  621. recipies->loadRecipies("data/recipies");
  622. // initialize chat
  623. chat = new Chat();
  624. // load quests
  625. questManager->loadQuests();
  626. }
  627. void Game::thread()
  628. {
  629. ZeitMesser waitForLock;
  630. ZeitMesser removeOldClients;
  631. ZeitMesser tickEntities;
  632. ZeitMesser worldUpdates;
  633. ZeitMesser clientReply;
  634. ZeitMesser removeOldChunks;
  635. ZeitMesser m;
  636. ZeitMesser total;
  637. total.messungStart();
  638. double tickTime = 0;
  639. double sleepTime = 0;
  640. int nextTimeSync = MAX_TICKS_PER_SECOND;
  641. while (!stop)
  642. {
  643. m.messungStart();
  644. ticker->nextTick();
  645. actionsCs.lock();
  646. while (actions.getEintragAnzahl() > 0)
  647. {
  648. actions.get(0)();
  649. actions.remove(0);
  650. }
  651. actionsCs.unlock();
  652. Array<int> removed;
  653. double waitTotal = 0;
  654. waitForLock.messungStart();
  655. cs.lock();
  656. waitForLock.messungEnde();
  657. waitTotal += waitForLock.getSekunden();
  658. removeOldClients.messungStart();
  659. int index = 0;
  660. nextTimeSync--;
  661. for (auto player : *clients)
  662. {
  663. if (!player->isOnline())
  664. {
  665. uiController->removePlayerDialogs(player->zEntity()->getId());
  666. chat->removeObserver(player->zEntity()->getId());
  667. chat->broadcastMessage(
  668. Framework::Text(player->zEntity()->getName())
  669. + " left the game.",
  670. Chat::CHANNEL_INFO);
  671. Datei pFile;
  672. pFile.setDatei(path + "/player/"
  673. + getPlayerId(player->zEntity()->getName()));
  674. pFile.erstellen();
  675. if (pFile.open(Datei::Style::schreiben))
  676. zEntityType(EntityTypeEnum::PLAYER)
  677. ->saveEntity(player->zEntity(), &pFile);
  678. pFile.close();
  679. removed.add(index, 0);
  680. Dimension* dim
  681. = zDimension(player->zEntity()->getDimensionId());
  682. dim->removeSubscriptions(player->zEntity());
  683. this->requestWorldUpdate(
  684. new EntityRemovedUpdate(player->zEntity()->getId(),
  685. player->zEntity()->getDimensionId(),
  686. player->zEntity()->getPosition()));
  687. }
  688. else
  689. {
  690. if (nextTimeSync <= 0 && player->zEntity())
  691. {
  692. Dimension* zDim
  693. = zDimension(player->zEntity()->getDimensionId());
  694. if (zDim)
  695. {
  696. NetworkMessage* msg = new NetworkMessage();
  697. msg->syncTime(zDim->getCurrentDayTime(),
  698. zDim->getNightDuration(),
  699. zDim->getNightTransitionDuration(),
  700. zDim->getDayDuration());
  701. player->sendResponse(msg);
  702. }
  703. }
  704. }
  705. index++;
  706. }
  707. if (nextTimeSync <= 0)
  708. {
  709. nextTimeSync = MAX_TICKS_PER_SECOND;
  710. }
  711. for (auto i : removed)
  712. clients->remove(i);
  713. removeOldClients.messungEnde();
  714. cs.unlock();
  715. tickEntities.messungStart();
  716. for (auto dim : *dimensions)
  717. dim->tickEntities();
  718. tickEntities.messungEnde();
  719. waitForLock.messungStart();
  720. cs.lock();
  721. waitForLock.messungEnde();
  722. waitTotal += waitForLock.getSekunden();
  723. worldUpdates.messungStart();
  724. while (updates->hat(0))
  725. {
  726. WorldUpdate* update = updates->z(0);
  727. for (auto client : *clients)
  728. client->sendWorldUpdate(
  729. dynamic_cast<WorldUpdate*>(update->getThis()));
  730. if (!zDimension(update->getAffectedDimension()))
  731. {
  732. Dimension* dim = generator->createDimension(
  733. update->getAffectedDimension());
  734. if (dim)
  735. addDimension(dim);
  736. else
  737. {
  738. std::cout << "ERROR: could not create dimension "
  739. << update->getAffectedDimension()
  740. << ". No Factory was provided.\n";
  741. }
  742. }
  743. if (zDimension(update->getAffectedDimension()))
  744. update->onUpdate(zDimension(update->getAffectedDimension()));
  745. updates->remove(0);
  746. }
  747. worldUpdates.messungEnde();
  748. cs.unlock();
  749. clientReply.messungStart();
  750. for (auto client : *clients)
  751. client->reply();
  752. clientReply.messungEnde();
  753. waitForLock.messungStart();
  754. cs.lock();
  755. waitForLock.messungEnde();
  756. waitTotal += waitForLock.getSekunden();
  757. removeOldChunks.messungStart();
  758. for (auto dim : *dimensions)
  759. dim->removeOldChunks();
  760. removeOldChunks.messungEnde();
  761. cs.unlock();
  762. m.messungEnde();
  763. double sec = m.getSekunden();
  764. tickCounter++;
  765. totalTickTime += sec;
  766. sleepTime += 1.0 / MAX_TICKS_PER_SECOND - tickTime;
  767. if (sleepTime > 0)
  768. {
  769. Sleep((int)(sleepTime * 1000));
  770. }
  771. total.messungEnde();
  772. total.messungStart();
  773. tickTime = total.getSekunden();
  774. totalTime += tickTime;
  775. if (totalTime >= 1)
  776. {
  777. averageTickTime = totalTickTime / tickCounter;
  778. ticksPerSecond = tickCounter;
  779. totalTickTime = 0;
  780. tickCounter = 0;
  781. totalTime = 0;
  782. std::cout << std::flush; // update info in console
  783. }
  784. else if (sec > 1)
  785. {
  786. std::cout << "WARNING: tick needed " << sec
  787. << " seconds. The game will run sower then normal.\n";
  788. std::cout << "waiting: " << waitTotal << "\nremoveOldClients: "
  789. << removeOldClients.getSekunden()
  790. << "\ntickEntities:" << tickEntities.getSekunden()
  791. << "\nworldUpdates: " << worldUpdates.getSekunden()
  792. << "\nclientReply: " << clientReply.getSekunden()
  793. << "\nremoveOldChunks:" << removeOldChunks.getSekunden()
  794. << "\n";
  795. }
  796. }
  797. save();
  798. generator->exitAndWait();
  799. loader->exitAndWait();
  800. ticker->exitAndWait();
  801. for (Dimension* dim : *dimensions)
  802. dim->requestStopAndWait();
  803. std::cout << "Game thread exited\n";
  804. }
  805. void Game::api(Framework::InMemoryBuffer* zRequest, GameClient* zOrigin)
  806. {
  807. char type;
  808. zRequest->lese(&type, 1);
  809. NetworkMessage* response = new NetworkMessage();
  810. switch (type)
  811. {
  812. case 1: // world
  813. {
  814. Dimension* dim = zDimension(zOrigin->zEntity()->getDimensionId());
  815. if (!dim)
  816. {
  817. dim = generator->createDimension(
  818. zOrigin->zEntity()->getDimensionId());
  819. if (!dim)
  820. {
  821. std::cout << "ERROR: could not create dimension "
  822. << zOrigin->zEntity()->getDimensionId()
  823. << ". No Factory was provided.\n";
  824. return;
  825. }
  826. addDimension(dim);
  827. }
  828. dim->api(zRequest, response, zOrigin->zEntity());
  829. break;
  830. }
  831. case 2: // player
  832. zOrigin->zEntity()->playerApi(zRequest, response);
  833. break;
  834. case 3: // entity
  835. {
  836. int id;
  837. zRequest->lese((char*)&id, 4);
  838. for (Dimension* dim : *dimensions)
  839. {
  840. Entity* entity = dim->zEntity(id);
  841. if (entity)
  842. {
  843. entity->api(zRequest, response, zOrigin->zEntity());
  844. break;
  845. }
  846. }
  847. break;
  848. }
  849. case 4:
  850. { // inventory
  851. bool isEntity;
  852. zRequest->lese((char*)&isEntity, 1);
  853. Inventory* target;
  854. if (isEntity)
  855. {
  856. int id;
  857. zRequest->lese((char*)&id, 4);
  858. target = zEntity(id);
  859. }
  860. else
  861. {
  862. int dim;
  863. Vec3<int> pos;
  864. zRequest->lese((char*)&dim, 4);
  865. zRequest->lese((char*)&pos.x, 4);
  866. zRequest->lese((char*)&pos.y, 4);
  867. zRequest->lese((char*)&pos.z, 4);
  868. target = zBlockAt(pos, dim);
  869. }
  870. if (target)
  871. target->inventoryApi(zRequest, response, zOrigin->zEntity());
  872. break;
  873. }
  874. case 5:
  875. { // crafting uiml request
  876. int id;
  877. zRequest->lese((char*)&id, 4);
  878. Text uiml = recipies->getCrafingUIML(id);
  879. Text dialogId = "crafting_";
  880. dialogId += id;
  881. uiController->addDialog(new UIDialog(dialogId,
  882. zOrigin->zEntity()->getId(),
  883. new Framework::XML::Element(uiml)));
  884. break;
  885. }
  886. case 6:
  887. { // chat message
  888. chat->chatApi(zRequest, zOrigin->zEntity(), response);
  889. break;
  890. }
  891. case 7: // other dimension
  892. {
  893. int dimensionId;
  894. zRequest->lese((char*)&dimensionId, 4);
  895. Dimension* dim = zDimension(dimensionId);
  896. if (dim)
  897. {
  898. dim->api(zRequest, response, zOrigin->zEntity());
  899. }
  900. break;
  901. }
  902. case 8: // ui message
  903. {
  904. uiController->api(zRequest, response, zOrigin->zEntity());
  905. break;
  906. }
  907. default:
  908. std::cout << "received unknown api request in game with type "
  909. << (int)type << "\n";
  910. }
  911. if (!response->isEmpty())
  912. {
  913. if (response->isBroadcast())
  914. broadcastMessage(response);
  915. else
  916. zOrigin->sendResponse(response);
  917. }
  918. else
  919. {
  920. response->release();
  921. }
  922. }
  923. void Game::updateLightning(int dimensionId, Vec3<int> location)
  924. {
  925. Dimension* zDim = zDimension(dimensionId);
  926. if (zDim) zDim->updateLightning(location);
  927. }
  928. void Game::updateLightningWithoutWait(int dimensionId, Vec3<int> location)
  929. {
  930. Dimension* zDim = zDimension(dimensionId);
  931. if (zDim) zDim->updateLightningWithoutWait(location);
  932. }
  933. void Game::broadcastMessage(NetworkMessage* response)
  934. {
  935. for (auto client : *clients)
  936. client->sendResponse(
  937. dynamic_cast<NetworkMessage*>(response->getThis()));
  938. response->release();
  939. }
  940. void Game::sendMessage(NetworkMessage* response, Entity* zTargetPlayer)
  941. {
  942. for (auto client : *clients)
  943. {
  944. if (client->zEntity()->getId() == zTargetPlayer->getId())
  945. {
  946. client->sendResponse(response);
  947. return;
  948. }
  949. }
  950. response->release();
  951. }
  952. bool Game::requestWorldUpdate(WorldUpdate* update)
  953. {
  954. cs.lock();
  955. updates->add(update);
  956. cs.unlock();
  957. return 1;
  958. }
  959. bool Game::checkPlayer(Framework::Text name, Framework::Text secret)
  960. {
  961. if (playerRegister->checkSecret(name, secret))
  962. return 1;
  963. else
  964. {
  965. std::cout << "player " << name.getText()
  966. << " tryed to connect with an invalid secret.\n";
  967. return 0;
  968. }
  969. }
  970. bool Game::existsPlayer(Framework::Text name)
  971. {
  972. return playerRegister->hasPlayer(name);
  973. }
  974. Framework::Text Game::createPlayer(Framework::Text name)
  975. {
  976. return playerRegister->addPlayer(name);
  977. }
  978. GameClient* Game::addPlayer(FCKlient* client, Framework::Text name)
  979. {
  980. cs.lock();
  981. int id = playerRegister->getPlayerId(name);
  982. Datei pFile;
  983. pFile.setDatei(path + "/player/" + id);
  984. Player* player;
  985. bool isNew = 0;
  986. if (!pFile.existiert() || !pFile.open(Datei::Style::lesen))
  987. {
  988. player = (Player*)zEntityType(EntityTypeEnum::PLAYER)
  989. ->createEntityAt(
  990. Vec3<float>(0.5, 0.5, 0), DimensionEnum::OVERWORLD);
  991. player->setName(name);
  992. isNew = 1;
  993. }
  994. else
  995. {
  996. player
  997. = (Player*)zEntityType(EntityTypeEnum::PLAYER)->loadEntity(&pFile);
  998. pFile.close();
  999. }
  1000. if (player->getId() >= nextEntityId)
  1001. {
  1002. nextEntityId = player->getId() + 1;
  1003. }
  1004. GameClient* gameClient = new GameClient(player, client);
  1005. gameClient->sendTypes();
  1006. clients->add(gameClient);
  1007. if (!zDimension(player->getDimensionId()))
  1008. {
  1009. Dimension* dim = generator->createDimension(player->getDimensionId());
  1010. if (!dim)
  1011. {
  1012. std::cout << "ERROR: could not create dimension "
  1013. << (int)player->getDimensionId()
  1014. << ". No Factory was provided.\n";
  1015. return 0;
  1016. }
  1017. NetworkMessage* msg = new NetworkMessage();
  1018. msg->syncTime(dim->getCurrentDayTime(),
  1019. dim->getNightDuration(),
  1020. dim->getNightTransitionDuration(),
  1021. dim->getDayDuration());
  1022. gameClient->sendResponse(msg);
  1023. this->addDimension(dim);
  1024. }
  1025. // subscribe the new player as an observer of the new chunk
  1026. Dimension* dim = zDimension(player->getDimensionId());
  1027. InMemoryBuffer* buffer = new InMemoryBuffer();
  1028. buffer->schreibe("\0", 1);
  1029. Punkt center = getChunkCenter(
  1030. (int)player->getPosition().x, (int)player->getPosition().y);
  1031. buffer->schreibe((char*)&center.x, 4);
  1032. buffer->schreibe((char*)&center.y, 4);
  1033. buffer->schreibe("\0", 1);
  1034. dim->api(buffer, 0, player);
  1035. buffer->release();
  1036. while (isNew
  1037. && !dim->zChunk(getChunkCenter(
  1038. (int)player->getPosition().x, (int)player->getPosition().y)))
  1039. {
  1040. cs.unlock();
  1041. Sleep(1000);
  1042. cs.lock();
  1043. }
  1044. if (isNew)
  1045. {
  1046. Either<Block*, int> b = BlockTypeEnum::AIR;
  1047. int h = WORLD_HEIGHT;
  1048. while (((b.isA() && (!(Block*)b || ((Block*)b)->isPassable()))
  1049. || (b.isB() && zBlockType(b)->zDefault()->isPassable()))
  1050. && h > 0)
  1051. b = zBlockAt({(int)player->getPosition().x,
  1052. (int)player->getPosition().y,
  1053. --h},
  1054. player->getDimensionId());
  1055. player->setPosition(
  1056. {player->getPosition().x, player->getPosition().y, (float)h + 1.f});
  1057. }
  1058. requestWorldUpdate(new AddEntityUpdate(player, player->getDimensionId()));
  1059. chat->addObserver(gameClient->zEntity()->getId());
  1060. chat->broadcastMessage(name + " joined the game.", Chat::CHANNEL_INFO);
  1061. cs.unlock();
  1062. return dynamic_cast<GameClient*>(gameClient->getThis());
  1063. }
  1064. bool Game::isChunkLoaded(int x, int y, int dimension) const
  1065. {
  1066. Dimension* dim = zDimension(dimension);
  1067. return (dim && dim->hasChunck(x, y));
  1068. }
  1069. bool Game::doesChunkExist(int x, int y, int dimension)
  1070. {
  1071. cs.lock();
  1072. bool result = isChunkLoaded(x, y, dimension)
  1073. || loader->existsChunk(x, y, dimension);
  1074. cs.unlock();
  1075. return result;
  1076. }
  1077. void Game::blockTargetChanged(Block* zBlock)
  1078. {
  1079. for (GameClient* client : *this->clients)
  1080. {
  1081. if (client->zEntity()->zTarget()
  1082. && client->zEntity()->zTarget()->isBlock(
  1083. zBlock->getPos(), NO_DIRECTION))
  1084. {
  1085. client->zEntity()->onTargetChange();
  1086. }
  1087. }
  1088. }
  1089. void Game::entityTargetChanged(Entity* zEntity)
  1090. {
  1091. for (GameClient* client : *this->clients)
  1092. {
  1093. if (client->zEntity()->zTarget()
  1094. && client->zEntity()->zTarget()->isEntity(zEntity->getId()))
  1095. {
  1096. client->zEntity()->onTargetChange();
  1097. }
  1098. }
  1099. }
  1100. void Game::spawnItem(
  1101. Framework::Vec3<float> location, int dimensionId, Item* stack)
  1102. {
  1103. spawnItem(location, dimensionId, new ItemStack(stack, 1));
  1104. }
  1105. void Game::spawnItem(
  1106. Framework::Vec3<float> location, int dimensionId, ItemStack* stack)
  1107. {
  1108. ItemEntity* itemEntity
  1109. = (ItemEntity*)zEntityType(EntityTypeEnum::ITEM)
  1110. ->createEntity(
  1111. location, dimensionId, Game::INSTANCE->getNextEntityId());
  1112. itemEntity->unsaveAddItem(stack, NO_DIRECTION, 0);
  1113. stack->release();
  1114. requestWorldUpdate(new AddEntityUpdate(itemEntity, dimensionId));
  1115. }
  1116. Framework::Either<Block*, int> Game::zBlockAt(
  1117. Framework::Vec3<int> location, int dimension) const
  1118. {
  1119. Dimension* dim = zDimension(dimension);
  1120. if (dim) return dim->zBlock(location);
  1121. return 0;
  1122. }
  1123. Block* Game::zRealBlockInstance(Framework::Vec3<int> location, int dimension)
  1124. {
  1125. Dimension* dim = zDimension(dimension);
  1126. if (dim) return dim->zRealBlockInstance(location);
  1127. return 0;
  1128. }
  1129. int Game::getBlockType(Framework::Vec3<int> location, int dimension)
  1130. {
  1131. Dimension* dim = zDimension(dimension);
  1132. if (dim) return dim->getBlockType(location);
  1133. return 0;
  1134. }
  1135. Dimension* Game::zDimension(int id) const
  1136. {
  1137. for (auto dim : *dimensions)
  1138. {
  1139. if (dim->getDimensionId() == id) return dim;
  1140. }
  1141. return 0;
  1142. }
  1143. Framework::Punkt Game::getChunkCenter(int x, int y)
  1144. {
  1145. return Punkt(((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE
  1146. + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2,
  1147. ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE
  1148. + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2);
  1149. }
  1150. Area Game::getChunckArea(Punkt center) const
  1151. {
  1152. return {center.x - CHUNK_SIZE / 2,
  1153. center.y - CHUNK_SIZE / 2,
  1154. center.x + CHUNK_SIZE / 2 - 1,
  1155. center.y + CHUNK_SIZE / 2 - 1,
  1156. 0};
  1157. }
  1158. Framework::Text Game::getWorldDirectory() const
  1159. {
  1160. return path;
  1161. }
  1162. void Game::requestArea(Area area)
  1163. {
  1164. generator->requestGeneration(area);
  1165. loader->requestLoading(area);
  1166. }
  1167. void Game::save() const
  1168. {
  1169. questManager->saveQuests();
  1170. Datei d;
  1171. d.setDatei(path + "/eid");
  1172. d.open(Datei::Style::schreiben);
  1173. d.schreibe((char*)&nextEntityId, 4);
  1174. d.close();
  1175. playerRegister->save();
  1176. for (auto dim : *dimensions)
  1177. dim->save(path);
  1178. chat->save();
  1179. std::cout << "Game was saved\n";
  1180. }
  1181. void Game::requestStop()
  1182. {
  1183. stop = 1;
  1184. warteAufThread(1000000);
  1185. }
  1186. void Game::addDimension(Dimension* d)
  1187. {
  1188. dimensions->add(d);
  1189. }
  1190. int Game::getNextEntityId()
  1191. {
  1192. cs.lock();
  1193. int result = nextEntityId++;
  1194. cs.unlock();
  1195. return result;
  1196. }
  1197. WorldGenerator* Game::zGenerator() const
  1198. {
  1199. return generator;
  1200. }
  1201. Game* Game::INSTANCE = 0;
  1202. void Game::initialize(Framework::Text name, Framework::Text worldsDir)
  1203. {
  1204. if (!Game::INSTANCE)
  1205. {
  1206. Game::INSTANCE = new Game(name, worldsDir);
  1207. Game::INSTANCE->initialize();
  1208. }
  1209. }
  1210. Entity* Game::zEntity(int id, int dimensionId) const
  1211. {
  1212. Dimension* d = zDimension(dimensionId);
  1213. if (d) return d->zEntity(id);
  1214. return 0;
  1215. }
  1216. Entity* Game::zEntity(int id) const
  1217. {
  1218. for (Dimension* d : *dimensions)
  1219. {
  1220. Entity* e = d->zEntity(id);
  1221. if (e) return e;
  1222. }
  1223. // for new players that are currently loading
  1224. for (GameClient* client : *clients)
  1225. {
  1226. if (client->zEntity()->getId() == id)
  1227. {
  1228. return client->zEntity();
  1229. }
  1230. }
  1231. return 0;
  1232. }
  1233. Entity* Game::zNearestEntity(int dimensionId,
  1234. Framework::Vec3<float> pos,
  1235. std::function<bool(Entity*)> filter)
  1236. {
  1237. Dimension* d = zDimension(dimensionId);
  1238. if (!d) return 0;
  1239. return d->zNearestEntity(pos, filter);
  1240. }
  1241. RecipieLoader* Game::zRecipies() const
  1242. {
  1243. return recipies;
  1244. }
  1245. void Game::doLater(std::function<void()> action)
  1246. {
  1247. actionsCs.lock();
  1248. actions.add(action);
  1249. actionsCs.unlock();
  1250. }
  1251. TickOrganizer* Game::zTickOrganizer() const
  1252. {
  1253. return ticker;
  1254. }
  1255. Chat* Game::zChat() const
  1256. {
  1257. return chat;
  1258. }
  1259. Player* Game::zPlayerByName(const char* name) const
  1260. {
  1261. for (GameClient* client : *clients)
  1262. {
  1263. if (strcmp(client->zEntity()->getName(), name) == 0)
  1264. {
  1265. return client->zEntity();
  1266. }
  1267. }
  1268. return 0;
  1269. }
  1270. TypeRegistry* Game::zTypeRegistry() const
  1271. {
  1272. return typeRegistry;
  1273. }
  1274. int Game::getPlayerId(const char* name) const
  1275. {
  1276. return playerRegister->getPlayerId(name);
  1277. }
  1278. QuestManager* Game::zQuestManager() const
  1279. {
  1280. return questManager;
  1281. }
  1282. UIController* Game::zUIController() const
  1283. {
  1284. return uiController;
  1285. }
  1286. double Game::getAverageTickTime() const
  1287. {
  1288. return averageTickTime;
  1289. }
  1290. int Game::getTicksPerSecond() const
  1291. {
  1292. return ticksPerSecond;
  1293. }
  1294. int Game::getPlayerCount() const
  1295. {
  1296. return clients->getEintragAnzahl();
  1297. }
  1298. int Game::getChunkCount() const
  1299. {
  1300. int result = 0;
  1301. for (Dimension* dim : *dimensions)
  1302. {
  1303. result += dim->getChunkCount();
  1304. }
  1305. return result;
  1306. }
  1307. const BlockType* Game::zBlockType(int id) const
  1308. {
  1309. return blockTypes[id];
  1310. }
  1311. const ItemType* Game::zItemType(int id) const
  1312. {
  1313. return itemTypes[id];
  1314. }
  1315. const EntityType* Game::zEntityType(int id) const
  1316. {
  1317. return entityTypes[id];
  1318. }
  1319. int Game::getBlockTypeId(const char* name) const
  1320. {
  1321. for (int i = 0; i < blockTypeCount; i++)
  1322. {
  1323. if (blockTypes[i]
  1324. && Framework::Text(blockTypes[i]->getName()).istGleich(name))
  1325. {
  1326. return i;
  1327. }
  1328. }
  1329. std::cout << "WARNING: no block type with name '" << name << "' found.\n";
  1330. return -1;
  1331. }
  1332. int Game::getItemTypeId(const char* name) const
  1333. {
  1334. for (int i = 0; i < itemTypeCount; i++)
  1335. {
  1336. if (itemTypes[i]
  1337. && Framework::Text(itemTypes[i]->getName()).istGleich(name))
  1338. {
  1339. return i;
  1340. }
  1341. }
  1342. std::cout << "WARNING: no item type with name '" << name << "' found.\n";
  1343. return -1;
  1344. }
  1345. int Game::getBlockTypeCount() const
  1346. {
  1347. return blockTypeCount;
  1348. }
  1349. int Game::getItemTypeCount() const
  1350. {
  1351. return itemTypeCount;
  1352. }
  1353. int Game::getEntityTypeCount() const
  1354. {
  1355. return entityTypeCount;
  1356. }
  1357. const MultiblockStructureType* Game::zMultiblockStructureType(int id) const
  1358. {
  1359. return multiblockStructureTypes[id];
  1360. }
  1361. int Game::getMultiblockStructureTypeCount() const
  1362. {
  1363. return multiblockStructureTypeCount;
  1364. }