Game.cpp 35 KB

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