Game.cpp 40 KB

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