Game.cpp 40 KB

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