PlayerMessageProcessor.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * CGameHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "PlayerMessageProcessor.h"
  12. #include "TurnOrderProcessor.h"
  13. #include "../CGameHandler.h"
  14. #include "../CVCMIServer.h"
  15. #include "../TurnTimerHandler.h"
  16. #include "../../lib/CPlayerState.h"
  17. #include "../../lib/StartInfo.h"
  18. #include "../../lib/entities/building/CBuilding.h"
  19. #include "../../lib/entities/hero/CHeroHandler.h"
  20. #include "../../lib/gameState/CGameState.h"
  21. #include "../../lib/mapObjects/CGTownInstance.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../lib/modding/IdentifierStorage.h"
  24. #include "../../lib/modding/ModScope.h"
  25. #include "../../lib/mapping/CMap.h"
  26. #include "../../lib/networkPacks/PacksForClient.h"
  27. #include "../../lib/networkPacks/StackLocation.h"
  28. #include "../../lib/serializer/Connection.h"
  29. #include "../../lib/spells/CSpellHandler.h"
  30. #include "../lib/VCMIDirs.h"
  31. PlayerMessageProcessor::PlayerMessageProcessor(CGameHandler * gameHandler)
  32. :gameHandler(gameHandler)
  33. {
  34. }
  35. void PlayerMessageProcessor::playerMessage(PlayerColor player, const std::string & message, ObjectInstanceID currObj)
  36. {
  37. if(!message.empty() && message[0] == '!')
  38. {
  39. broadcastMessage(player, message);
  40. handleCommand(player, message);
  41. return;
  42. }
  43. if(handleCheatCode(message, player, currObj))
  44. {
  45. if(!gameHandler->getPlayerSettings(player)->isControlledByAI())
  46. {
  47. MetaString txt;
  48. txt.appendLocalString(EMetaText::GENERAL_TXT, 260);
  49. broadcastSystemMessage(txt);
  50. }
  51. if(!player.isSpectator())
  52. gameHandler->checkVictoryLossConditionsForPlayer(player); //Player enter win code or got required art\creature
  53. return;
  54. }
  55. broadcastMessage(player, message);
  56. }
  57. void PlayerMessageProcessor::commandExit(PlayerColor player, const std::vector<std::string> & words)
  58. {
  59. bool isHost = gameHandler->gameLobby()->isPlayerHost(player);
  60. if(!isHost)
  61. return;
  62. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.gameterminated"));
  63. gameHandler->gameLobby()->setState(EServerState::SHUTDOWN);
  64. }
  65. void PlayerMessageProcessor::commandKick(PlayerColor player, const std::vector<std::string> & words)
  66. {
  67. bool isHost = gameHandler->gameLobby()->isPlayerHost(player);
  68. if(!isHost)
  69. return;
  70. if(words.size() == 2)
  71. {
  72. auto playername = words[1];
  73. PlayerColor playerToKick(PlayerColor::CANNOT_DETERMINE);
  74. if(std::all_of(playername.begin(), playername.end(), ::isdigit))
  75. playerToKick = PlayerColor(std::stoi(playername));
  76. else
  77. {
  78. for(auto & c : gameHandler->connections)
  79. {
  80. if(c.first.toString() == playername)
  81. playerToKick = c.first;
  82. }
  83. }
  84. if(playerToKick != PlayerColor::CANNOT_DETERMINE)
  85. {
  86. PlayerCheated pc;
  87. pc.player = playerToKick;
  88. pc.losingCheatCode = true;
  89. gameHandler->sendAndApply(pc);
  90. gameHandler->checkVictoryLossConditionsForPlayer(playerToKick);
  91. }
  92. }
  93. }
  94. void PlayerMessageProcessor::commandSave(PlayerColor player, const std::vector<std::string> & words)
  95. {
  96. bool isHost = gameHandler->gameLobby()->isPlayerHost(player);
  97. if(!isHost)
  98. return;
  99. if(words.size() == 2)
  100. {
  101. gameHandler->save("Saves/" + words[1]);
  102. MetaString str;
  103. str.appendTextID("vcmi.broadcast.gamesavedas");
  104. str.appendRawString(" ");
  105. str.appendRawString(words[1]);
  106. broadcastSystemMessage(str);
  107. }
  108. }
  109. void PlayerMessageProcessor::commandCheaters(PlayerColor player, const std::vector<std::string> & words)
  110. {
  111. int playersCheated = 0;
  112. for(const auto & player : gameHandler->gameState()->players)
  113. {
  114. if(player.second.cheated)
  115. {
  116. auto str = MetaString::createFromTextID("vcmi.broadcast.playercheater");
  117. str.replaceName(player.first);
  118. broadcastSystemMessage(str);
  119. playersCheated++;
  120. }
  121. }
  122. if(!playersCheated)
  123. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.nocheater"));
  124. }
  125. void PlayerMessageProcessor::commandStatistic(PlayerColor player, const std::vector<std::string> & words)
  126. {
  127. bool isHost = gameHandler->gameLobby()->isPlayerHost(player);
  128. if(!isHost)
  129. return;
  130. std::string path = gameHandler->gameState()->statistic.writeCsv();
  131. auto str = MetaString::createFromTextID("vcmi.broadcast.statisticfile");
  132. str.replaceRawString(path);
  133. broadcastSystemMessage(str);
  134. }
  135. void PlayerMessageProcessor::commandHelp(PlayerColor player, const std::vector<std::string> & words)
  136. {
  137. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.help.commands"));
  138. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.help.exit"));
  139. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.help.kick"));
  140. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.help.save"));
  141. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.help.statistic"));
  142. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.help.commandsall"));
  143. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.help.help"));
  144. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.help.cheaters"));
  145. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.help.vote"));
  146. }
  147. void PlayerMessageProcessor::commandVote(PlayerColor player, const std::vector<std::string> & words)
  148. {
  149. if(words.size() < 2)
  150. {
  151. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.vote.allow"));
  152. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.vote.force"));
  153. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.vote.abort"));
  154. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.vote.timer"));
  155. return;
  156. }
  157. if(words[1] == "yes" || words[1] == "no" || words[1] == MetaString::createFromTextID("vcmi.broadcast.vote.yes").toString() || words[1] == MetaString::createFromTextID("vcmi.broadcast.vote.no").toString())
  158. {
  159. if(currentVote == ECurrentChatVote::NONE)
  160. {
  161. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.vote.noactive"));
  162. return;
  163. }
  164. if(words[1] == "yes" || words[1] == MetaString::createFromTextID("vcmi.broadcast.vote.yes").toString())
  165. {
  166. awaitingPlayers.erase(player);
  167. if(awaitingPlayers.empty())
  168. finishVoting();
  169. return;
  170. }
  171. if(words[1] == "no" || words[1] == MetaString::createFromTextID("vcmi.broadcast.vote.no").toString())
  172. {
  173. abortVoting();
  174. return;
  175. }
  176. }
  177. const auto & parseNumber = [](const std::string & input) -> std::optional<int>
  178. {
  179. try
  180. {
  181. return std::stol(input);
  182. }
  183. catch(std::logic_error &)
  184. {
  185. return std::nullopt;
  186. }
  187. };
  188. if(words[1] == "simturns" && words.size() > 2)
  189. {
  190. if(words[2] == "allow" && words.size() > 3)
  191. {
  192. auto daysCount = parseNumber(words[3]);
  193. if(daysCount && daysCount.value() > 0)
  194. startVoting(player, ECurrentChatVote::SIMTURNS_ALLOW, daysCount.value());
  195. return;
  196. }
  197. if(words[2] == "force" && words.size() > 3)
  198. {
  199. auto daysCount = parseNumber(words[3]);
  200. if(daysCount && daysCount.value() > 0)
  201. startVoting(player, ECurrentChatVote::SIMTURNS_FORCE, daysCount.value());
  202. return;
  203. }
  204. if(words[2] == "abort")
  205. {
  206. startVoting(player, ECurrentChatVote::SIMTURNS_ABORT, 0);
  207. return;
  208. }
  209. }
  210. if(words[1] == "timer" && words.size() > 2)
  211. {
  212. if(words[2] == "prolong" && words.size() > 3)
  213. {
  214. auto secondsCount = parseNumber(words[3]);
  215. if(secondsCount && secondsCount.value() > 0)
  216. startVoting(player, ECurrentChatVote::TIMER_PROLONG, secondsCount.value());
  217. return;
  218. }
  219. }
  220. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.vote.notrecognized"));
  221. }
  222. void PlayerMessageProcessor::finishVoting()
  223. {
  224. MetaString msg;
  225. switch(currentVote)
  226. {
  227. case ECurrentChatVote::SIMTURNS_ALLOW:
  228. msg.appendTextID("vcmi.broadcast.vote.success.untilcontacts");
  229. msg.replaceRawString(std::to_string(currentVoteParameter));
  230. broadcastSystemMessage(msg);
  231. gameHandler->turnOrder->setMaxSimturnsDuration(currentVoteParameter);
  232. break;
  233. case ECurrentChatVote::SIMTURNS_FORCE:
  234. msg.appendTextID("vcmi.broadcast.vote.success.contactsblocked");
  235. msg.replaceRawString(std::to_string(currentVoteParameter));
  236. broadcastSystemMessage(msg);
  237. gameHandler->turnOrder->setMinSimturnsDuration(currentVoteParameter);
  238. break;
  239. case ECurrentChatVote::SIMTURNS_ABORT:
  240. msg.appendTextID("vcmi.broadcast.vote.success.nextday");
  241. broadcastSystemMessage(msg);
  242. gameHandler->turnOrder->setMinSimturnsDuration(0);
  243. gameHandler->turnOrder->setMaxSimturnsDuration(0);
  244. break;
  245. case ECurrentChatVote::TIMER_PROLONG:
  246. msg.appendTextID("vcmi.broadcast.vote.success.timer");
  247. msg.replaceRawString(std::to_string(currentVoteParameter));
  248. broadcastSystemMessage(msg);
  249. gameHandler->turnTimerHandler->prolongTimers(currentVoteParameter * 1000);
  250. break;
  251. }
  252. currentVote = ECurrentChatVote::NONE;
  253. currentVoteParameter = -1;
  254. }
  255. void PlayerMessageProcessor::abortVoting()
  256. {
  257. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.vote.aborted"));
  258. currentVote = ECurrentChatVote::NONE;
  259. }
  260. void PlayerMessageProcessor::startVoting(PlayerColor initiator, ECurrentChatVote what, int parameter)
  261. {
  262. currentVote = what;
  263. currentVoteParameter = parameter;
  264. MetaString msg;
  265. switch(currentVote)
  266. {
  267. case ECurrentChatVote::SIMTURNS_ALLOW:
  268. msg.appendTextID("vcmi.broadcast.vote.start.untilcontacts");
  269. msg.replaceRawString(std::to_string(parameter));
  270. broadcastSystemMessage(msg);
  271. break;
  272. case ECurrentChatVote::SIMTURNS_FORCE:
  273. msg.appendTextID("vcmi.broadcast.vote.start.contactsblocked");
  274. msg.replaceRawString(std::to_string(parameter));
  275. broadcastSystemMessage(msg);
  276. break;
  277. case ECurrentChatVote::SIMTURNS_ABORT:
  278. msg.appendTextID("vcmi.broadcast.vote.start.nextday");
  279. broadcastSystemMessage(msg);
  280. break;
  281. case ECurrentChatVote::TIMER_PROLONG:
  282. msg.appendTextID("vcmi.broadcast.vote.start.timer");
  283. msg.replaceRawString(std::to_string(parameter));
  284. broadcastSystemMessage(msg);
  285. break;
  286. default:
  287. return;
  288. }
  289. broadcastSystemMessage(MetaString::createFromTextID("vcmi.broadcast.vote.hint"));
  290. awaitingPlayers.clear();
  291. for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
  292. {
  293. auto state = gameHandler->getPlayerState(player, false);
  294. if(state && state->isHuman() && initiator != player)
  295. awaitingPlayers.insert(player);
  296. }
  297. if(awaitingPlayers.empty())
  298. finishVoting();
  299. }
  300. void PlayerMessageProcessor::handleCommand(PlayerColor player, const std::string & message)
  301. {
  302. if(message.empty() || message[0] != '!')
  303. return;
  304. std::vector<std::string> words;
  305. boost::split(words, message, boost::is_any_of(" "));
  306. if(words[0] == "!exit" || words[0] == "!quit")
  307. commandExit(player, words);
  308. if(words[0] == "!help")
  309. commandHelp(player, words);
  310. if(words[0] == "!vote")
  311. commandVote(player, words);
  312. if(words[0] == "!kick")
  313. commandKick(player, words);
  314. if(words[0] == "!save")
  315. commandSave(player, words);
  316. if(words[0] == "!cheaters")
  317. commandCheaters(player, words);
  318. if(words[0] == "!statistic")
  319. commandStatistic(player, words);
  320. }
  321. void PlayerMessageProcessor::cheatGiveSpells(PlayerColor player, const CGHeroInstance * hero)
  322. {
  323. if (!hero)
  324. return;
  325. ///Give hero spellbook
  326. if (!hero->hasSpellbook())
  327. gameHandler->giveHeroNewArtifact(hero, ArtifactID::SPELLBOOK, ArtifactPosition::SPELLBOOK);
  328. ///Give all spells with bonus (to allow banned spells)
  329. GiveBonus giveBonus(GiveBonus::ETarget::OBJECT);
  330. giveBonus.id = hero->id;
  331. giveBonus.bonus = Bonus(BonusDuration::PERMANENT, BonusType::SPELLS_OF_LEVEL, BonusSource::OTHER, 0, BonusSourceID());
  332. //start with level 0 to skip abilities
  333. for (int level = 1; level <= GameConstants::SPELL_LEVELS; level++)
  334. {
  335. giveBonus.bonus.subtype = BonusCustomSubtype::spellLevel(level);
  336. gameHandler->sendAndApply(giveBonus);
  337. }
  338. ///Give mana
  339. SetMana sm;
  340. sm.hid = hero->id;
  341. sm.val = 999;
  342. sm.absolute = true;
  343. gameHandler->sendAndApply(sm);
  344. }
  345. void PlayerMessageProcessor::cheatBuildTown(PlayerColor player, const CGTownInstance * town)
  346. {
  347. if (!town)
  348. return;
  349. for (auto & build : town->getTown()->buildings)
  350. {
  351. if (!town->hasBuilt(build.first)
  352. && !build.second->getNameTranslated().empty()
  353. && build.first != BuildingID::SHIP)
  354. {
  355. gameHandler->buildStructure(town->id, build.first, true);
  356. }
  357. }
  358. }
  359. void PlayerMessageProcessor::cheatGiveArmy(PlayerColor player, const CGHeroInstance * hero, std::vector<std::string> words)
  360. {
  361. if (!hero)
  362. return;
  363. std::string creatureIdentifier = words.empty() ? "archangel" : words[0];
  364. std::optional<int> amountPerSlot;
  365. try
  366. {
  367. amountPerSlot = std::stol(words.at(1));
  368. }
  369. catch(std::logic_error&)
  370. {
  371. }
  372. std::optional<int32_t> creatureId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "creature", creatureIdentifier, false);
  373. if(creatureId.has_value())
  374. {
  375. const auto * creature = CreatureID(creatureId.value()).toCreature();
  376. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  377. {
  378. if (!hero->hasStackAtSlot(SlotID(i)))
  379. {
  380. if (amountPerSlot.has_value())
  381. gameHandler->insertNewStack(StackLocation(hero, SlotID(i)), creature, *amountPerSlot);
  382. else
  383. gameHandler->insertNewStack(StackLocation(hero, SlotID(i)), creature, 5 * std::pow(10, i));
  384. }
  385. }
  386. }
  387. }
  388. void PlayerMessageProcessor::cheatGiveMachines(PlayerColor player, const CGHeroInstance * hero)
  389. {
  390. if (!hero)
  391. return;
  392. if (!hero->getArt(ArtifactPosition::MACH1))
  393. gameHandler->giveHeroNewArtifact(hero, ArtifactID::BALLISTA, ArtifactPosition::MACH1);
  394. if (!hero->getArt(ArtifactPosition::MACH2))
  395. gameHandler->giveHeroNewArtifact(hero, ArtifactID::AMMO_CART, ArtifactPosition::MACH2);
  396. if (!hero->getArt(ArtifactPosition::MACH3))
  397. gameHandler->giveHeroNewArtifact(hero, ArtifactID::FIRST_AID_TENT, ArtifactPosition::MACH3);
  398. }
  399. void PlayerMessageProcessor::cheatGiveArtifacts(PlayerColor player, const CGHeroInstance * hero, std::vector<std::string> words)
  400. {
  401. if (!hero)
  402. return;
  403. if (!words.empty())
  404. {
  405. for (auto const & word : words)
  406. {
  407. auto artID = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "artifact", word, false);
  408. if(artID && VLC->arth->objects[*artID])
  409. gameHandler->giveHeroNewArtifact(hero, ArtifactID(*artID), ArtifactPosition::FIRST_AVAILABLE);
  410. }
  411. }
  412. else
  413. {
  414. for(int g = 7; g < VLC->arth->objects.size(); ++g) //including artifacts from mods
  415. {
  416. if(VLC->arth->objects[g]->canBePutAt(hero))
  417. gameHandler->giveHeroNewArtifact(hero, ArtifactID(g), ArtifactPosition::FIRST_AVAILABLE);
  418. }
  419. }
  420. }
  421. void PlayerMessageProcessor::cheatGiveScrolls(PlayerColor player, const CGHeroInstance * hero)
  422. {
  423. if(!hero)
  424. return;
  425. for(const auto & spell : VLC->spellh->objects)
  426. if(gameHandler->gameState()->isAllowed(spell->getId()) && !spell->isSpecial())
  427. {
  428. gameHandler->giveHeroNewScroll(hero, spell->getId(), ArtifactPosition::FIRST_AVAILABLE);
  429. }
  430. }
  431. void PlayerMessageProcessor::cheatLevelup(PlayerColor player, const CGHeroInstance * hero, std::vector<std::string> words)
  432. {
  433. if (!hero)
  434. return;
  435. int levelsToGain;
  436. try
  437. {
  438. levelsToGain = std::stol(words.at(0));
  439. }
  440. catch(std::logic_error&)
  441. {
  442. levelsToGain = 1;
  443. }
  444. gameHandler->giveExperience(hero, VLC->heroh->reqExp(hero->level + levelsToGain) - VLC->heroh->reqExp(hero->level));
  445. }
  446. void PlayerMessageProcessor::cheatExperience(PlayerColor player, const CGHeroInstance * hero, std::vector<std::string> words)
  447. {
  448. if (!hero)
  449. return;
  450. int expAmountProcessed;
  451. try
  452. {
  453. expAmountProcessed = std::stol(words.at(0));
  454. }
  455. catch(std::logic_error&)
  456. {
  457. expAmountProcessed = 10000;
  458. }
  459. gameHandler->giveExperience(hero, expAmountProcessed);
  460. }
  461. void PlayerMessageProcessor::cheatMovement(PlayerColor player, const CGHeroInstance * hero, std::vector<std::string> words)
  462. {
  463. if (!hero)
  464. return;
  465. SetMovePoints smp;
  466. smp.hid = hero->id;
  467. bool unlimited = false;
  468. try
  469. {
  470. smp.val = std::stol(words.at(0));
  471. }
  472. catch(std::logic_error&)
  473. {
  474. smp.val = 1000000;
  475. unlimited = true;
  476. }
  477. gameHandler->sendAndApply(smp);
  478. GiveBonus gb(GiveBonus::ETarget::OBJECT);
  479. gb.bonus.type = BonusType::FREE_SHIP_BOARDING;
  480. gb.bonus.duration = unlimited ? BonusDuration::PERMANENT : BonusDuration::ONE_DAY;
  481. gb.bonus.source = BonusSource::OTHER;
  482. gb.id = hero->id;
  483. gameHandler->giveHeroBonus(&gb);
  484. if(unlimited)
  485. {
  486. GiveBonus gb(GiveBonus::ETarget::OBJECT);
  487. gb.bonus.type = BonusType::UNLIMITED_MOVEMENT;
  488. gb.bonus.duration = BonusDuration::PERMANENT;
  489. gb.bonus.source = BonusSource::OTHER;
  490. gb.id = hero->id;
  491. gameHandler->giveHeroBonus(&gb);
  492. }
  493. }
  494. void PlayerMessageProcessor::cheatResources(PlayerColor player, std::vector<std::string> words)
  495. {
  496. int baseResourceAmount;
  497. try
  498. {
  499. baseResourceAmount = std::stol(words.at(0));
  500. }
  501. catch(std::logic_error&)
  502. {
  503. baseResourceAmount = 100;
  504. }
  505. TResources resources;
  506. resources[EGameResID::GOLD] = baseResourceAmount * 1000;
  507. for (GameResID i = EGameResID::WOOD; i < EGameResID::GOLD; ++i)
  508. resources[i] = baseResourceAmount;
  509. gameHandler->giveResources(player, resources);
  510. }
  511. void PlayerMessageProcessor::cheatVictory(PlayerColor player)
  512. {
  513. PlayerCheated pc;
  514. pc.player = player;
  515. pc.winningCheatCode = true;
  516. gameHandler->sendAndApply(pc);
  517. }
  518. void PlayerMessageProcessor::cheatDefeat(PlayerColor player)
  519. {
  520. PlayerCheated pc;
  521. pc.player = player;
  522. pc.losingCheatCode = true;
  523. gameHandler->sendAndApply(pc);
  524. }
  525. void PlayerMessageProcessor::cheatMapReveal(PlayerColor player, bool reveal)
  526. {
  527. FoWChange fc;
  528. fc.mode = reveal ? ETileVisibility::REVEALED : ETileVisibility::HIDDEN;
  529. fc.player = player;
  530. const auto & fowMap = gameHandler->gameState()->getPlayerTeam(player)->fogOfWarMap;
  531. const auto & mapSize = gameHandler->gameState()->getMapSize();
  532. auto hlp_tab = new int3[mapSize.x * mapSize.y * mapSize.z];
  533. int lastUnc = 0;
  534. for(int z = 0; z < mapSize.z; z++)
  535. for(int x = 0; x < mapSize.x; x++)
  536. for(int y = 0; y < mapSize.y; y++)
  537. if(!fowMap[z][x][y] || fc.mode == ETileVisibility::HIDDEN)
  538. hlp_tab[lastUnc++] = int3(x, y, z);
  539. fc.tiles.insert(hlp_tab, hlp_tab + lastUnc);
  540. delete [] hlp_tab;
  541. gameHandler->sendAndApply(fc);
  542. }
  543. void PlayerMessageProcessor::cheatPuzzleReveal(PlayerColor player)
  544. {
  545. TeamState *t = gameHandler->gameState()->getPlayerTeam(player);
  546. for(auto & obj : gameHandler->gameState()->map->objects)
  547. {
  548. if(obj && obj->ID == Obj::OBELISK && !obj->wasVisited(player))
  549. {
  550. gameHandler->setObjPropertyID(obj->id, ObjProperty::OBELISK_VISITED, t->id);
  551. for(const auto & color : t->players)
  552. {
  553. gameHandler->setObjPropertyID(obj->id, ObjProperty::VISITED, color);
  554. PlayerCheated pc;
  555. pc.player = color;
  556. gameHandler->sendAndApply(pc);
  557. }
  558. }
  559. }
  560. }
  561. void PlayerMessageProcessor::cheatMaxLuck(PlayerColor player, const CGHeroInstance * hero)
  562. {
  563. if (!hero)
  564. return;
  565. GiveBonus gb;
  566. gb.bonus = Bonus(BonusDuration::PERMANENT, BonusType::MAX_LUCK, BonusSource::OTHER, 0, BonusSourceID(Obj(Obj::NO_OBJ)));
  567. gb.id = hero->id;
  568. gameHandler->giveHeroBonus(&gb);
  569. }
  570. void PlayerMessageProcessor::cheatFly(PlayerColor player, const CGHeroInstance * hero)
  571. {
  572. if (!hero)
  573. return;
  574. GiveBonus gb;
  575. gb.bonus = Bonus(BonusDuration::PERMANENT, BonusType::FLYING_MOVEMENT, BonusSource::OTHER, 0, BonusSourceID(Obj(Obj::NO_OBJ)));
  576. gb.id = hero->id;
  577. gameHandler->giveHeroBonus(&gb);
  578. }
  579. void PlayerMessageProcessor::cheatMaxMorale(PlayerColor player, const CGHeroInstance * hero)
  580. {
  581. if (!hero)
  582. return;
  583. GiveBonus gb;
  584. gb.bonus = Bonus(BonusDuration::PERMANENT, BonusType::MAX_MORALE, BonusSource::OTHER, 0, BonusSourceID(Obj(Obj::NO_OBJ)));
  585. gb.id = hero->id;
  586. gameHandler->giveHeroBonus(&gb);
  587. }
  588. bool PlayerMessageProcessor::handleCheatCode(const std::string & cheat, PlayerColor player, ObjectInstanceID currObj)
  589. {
  590. std::vector<std::string> words;
  591. boost::split(words, boost::trim_copy(cheat), boost::is_any_of("\t\r\n "));
  592. if (words.empty() || !gameHandler->getStartInfo()->extraOptionsInfo.cheatsAllowed)
  593. return false;
  594. //Make cheat name case-insensitive, but keep words/parameters (e.g. creature name) as it
  595. std::string cheatName = boost::to_lower_copy(words[0]);
  596. words.erase(words.begin());
  597. std::vector<std::string> townTargetedCheats = { "vcmiarmenelos", "vcmibuild", "nwczion" };
  598. std::vector<std::string> playerTargetedCheats = {
  599. "vcmiformenos", "vcmiresources", "nwctheconstruct",
  600. "vcmimelkor", "vcmilose", "nwcbluepill",
  601. "vcmisilmaril", "vcmiwin", "nwcredpill",
  602. "vcmieagles", "vcmimap", "nwcwhatisthematrix",
  603. "vcmiungoliant", "vcmihidemap", "nwcignoranceisbliss",
  604. "vcmiobelisk", "nwcoracle"
  605. };
  606. std::vector<std::string> heroTargetedCheats = {
  607. "vcmiainur", "vcmiarchangel", "nwctrinity",
  608. "vcmiangband", "vcmiblackknight", "nwcagents",
  609. "vcmiglaurung", "vcmicrystal", "vcmiazure",
  610. "vcmifaerie", "vcmiarmy", "vcminissi",
  611. "vcmiistari", "vcmispells", "nwcthereisnospoon",
  612. "vcminoldor", "vcmimachines", "nwclotsofguns",
  613. "vcmiglorfindel", "vcmilevel", "nwcneo",
  614. "vcminahar", "vcmimove", "nwcnebuchadnezzar",
  615. "vcmiforgeofnoldorking", "vcmiartifacts",
  616. "vcmiolorin", "vcmiexp",
  617. "vcmiluck", "nwcfollowthewhiterabbit",
  618. "vcmimorale", "nwcmorpheus",
  619. "vcmigod", "nwctheone",
  620. "vcmiscrolls"
  621. };
  622. if (!vstd::contains(townTargetedCheats, cheatName) && !vstd::contains(playerTargetedCheats, cheatName) && !vstd::contains(heroTargetedCheats, cheatName))
  623. return false;
  624. bool playerTargetedCheat = false;
  625. for (const auto & i : gameHandler->gameState()->players)
  626. {
  627. if (words.empty())
  628. break;
  629. if (i.first == PlayerColor::NEUTRAL)
  630. continue;
  631. if (words.front() == "ai" && i.second.human)
  632. continue;
  633. if (words.front() != "all" && words.front() != i.first.toString())
  634. continue;
  635. std::vector<std::string> parameters = words;
  636. PlayerCheated pc;
  637. pc.player = i.first;
  638. gameHandler->sendAndApply(pc);
  639. playerTargetedCheat = true;
  640. parameters.erase(parameters.begin());
  641. if (vstd::contains(playerTargetedCheats, cheatName))
  642. executeCheatCode(cheatName, i.first, ObjectInstanceID::NONE, parameters);
  643. if (vstd::contains(townTargetedCheats, cheatName))
  644. for (const auto & t : i.second.getTowns())
  645. executeCheatCode(cheatName, i.first, t->id, parameters);
  646. if (vstd::contains(heroTargetedCheats, cheatName))
  647. for (const auto & h : i.second.getHeroes())
  648. executeCheatCode(cheatName, i.first, h->id, parameters);
  649. }
  650. PlayerCheated pc;
  651. pc.player = player;
  652. gameHandler->sendAndApply(pc);
  653. if (!playerTargetedCheat)
  654. executeCheatCode(cheatName, player, currObj, words);
  655. return true;
  656. }
  657. void PlayerMessageProcessor::executeCheatCode(const std::string & cheatName, PlayerColor player, ObjectInstanceID currObj, const std::vector<std::string> & words)
  658. {
  659. const CGHeroInstance * hero = gameHandler->getHero(currObj);
  660. const CGTownInstance * town = gameHandler->getTown(currObj);
  661. if (!town && hero)
  662. town = hero->visitedTown;
  663. const auto & doCheatGiveSpells = [&]() { cheatGiveSpells(player, hero); };
  664. const auto & doCheatBuildTown = [&]() { cheatBuildTown(player, town); };
  665. const auto & doCheatGiveArmyCustom = [&]() { cheatGiveArmy(player, hero, words); };
  666. const auto & doCheatGiveArmyFixed = [&](std::vector<std::string> customWords) { cheatGiveArmy(player, hero, customWords); };
  667. const auto & doCheatGiveMachines = [&]() { cheatGiveMachines(player, hero); };
  668. const auto & doCheatGiveArtifacts = [&]() { cheatGiveArtifacts(player, hero, words); };
  669. const auto & doCheatLevelup = [&]() { cheatLevelup(player, hero, words); };
  670. const auto & doCheatExperience = [&]() { cheatExperience(player, hero, words); };
  671. const auto & doCheatMovement = [&]() { cheatMovement(player, hero, words); };
  672. const auto & doCheatResources = [&]() { cheatResources(player, words); };
  673. const auto & doCheatVictory = [&]() { cheatVictory(player); };
  674. const auto & doCheatDefeat = [&]() { cheatDefeat(player); };
  675. const auto & doCheatMapReveal = [&]() { cheatMapReveal(player, true); };
  676. const auto & doCheatMapHide = [&]() { cheatMapReveal(player, false); };
  677. const auto & doCheatRevealPuzzle = [&]() { cheatPuzzleReveal(player); };
  678. const auto & doCheatMaxLuck = [&]() { cheatMaxLuck(player, hero); };
  679. const auto & doCheatMaxMorale = [&]() { cheatMaxMorale(player, hero); };
  680. const auto & doCheatGiveScrolls = [&]() { cheatGiveScrolls(player, hero); };
  681. const auto & doCheatTheOne = [&]()
  682. {
  683. if(!hero)
  684. return;
  685. cheatMapReveal(player, true);
  686. cheatGiveArmy(player, hero, { "archangel", "5" });
  687. cheatMovement(player, hero, { });
  688. cheatFly(player, hero);
  689. };
  690. // Unimplemented H3 cheats:
  691. // nwcphisherprice - Changes and brightens the game colors.
  692. std::map<std::string, std::function<void()>> callbacks = {
  693. {"vcmiainur", [&] () {doCheatGiveArmyFixed({ "archangel", "5" });} },
  694. {"nwctrinity", [&] () {doCheatGiveArmyFixed({ "archangel", "5" });} },
  695. {"vcmiangband", [&] () {doCheatGiveArmyFixed({ "blackKnight", "10" });} },
  696. {"vcmiglaurung", [&] () {doCheatGiveArmyFixed({ "crystalDragon", "5000" });} },
  697. {"vcmiarchangel", [&] () {doCheatGiveArmyFixed({ "archangel", "5" });} },
  698. {"nwcagents", [&] () {doCheatGiveArmyFixed({ "blackKnight", "10" });} },
  699. {"vcmiblackknight", [&] () {doCheatGiveArmyFixed({ "blackKnight", "10" });} },
  700. {"vcmicrystal", [&] () {doCheatGiveArmyFixed({ "crystalDragon", "5000" });} },
  701. {"vcmiazure", [&] () {doCheatGiveArmyFixed({ "azureDragon", "5000" });} },
  702. {"vcmifaerie", [&] () {doCheatGiveArmyFixed({ "fairieDragon", "5000" });} },
  703. {"vcmiarmy", doCheatGiveArmyCustom },
  704. {"vcminissi", doCheatGiveArmyCustom },
  705. {"vcmiistari", doCheatGiveSpells },
  706. {"vcmispells", doCheatGiveSpells },
  707. {"nwcthereisnospoon", doCheatGiveSpells },
  708. {"vcmiarmenelos", doCheatBuildTown },
  709. {"vcmibuild", doCheatBuildTown },
  710. {"nwczion", doCheatBuildTown },
  711. {"vcminoldor", doCheatGiveMachines },
  712. {"vcmimachines", doCheatGiveMachines },
  713. {"nwclotsofguns", doCheatGiveMachines },
  714. {"vcmiforgeofnoldorking", doCheatGiveArtifacts },
  715. {"vcmiartifacts", doCheatGiveArtifacts },
  716. {"vcmiglorfindel", doCheatLevelup },
  717. {"vcmilevel", doCheatLevelup },
  718. {"nwcneo", doCheatLevelup },
  719. {"vcmiolorin", doCheatExperience },
  720. {"vcmiexp", doCheatExperience },
  721. {"vcminahar", doCheatMovement },
  722. {"vcmimove", doCheatMovement },
  723. {"nwcnebuchadnezzar", doCheatMovement },
  724. {"vcmiformenos", doCheatResources },
  725. {"vcmiresources", doCheatResources },
  726. {"nwctheconstruct", doCheatResources },
  727. {"nwcbluepill", doCheatDefeat },
  728. {"vcmimelkor", doCheatDefeat },
  729. {"vcmilose", doCheatDefeat },
  730. {"nwcredpill", doCheatVictory },
  731. {"vcmisilmaril", doCheatVictory },
  732. {"vcmiwin", doCheatVictory },
  733. {"nwcwhatisthematrix", doCheatMapReveal },
  734. {"vcmieagles", doCheatMapReveal },
  735. {"vcmimap", doCheatMapReveal },
  736. {"vcmiungoliant", doCheatMapHide },
  737. {"vcmihidemap", doCheatMapHide },
  738. {"nwcignoranceisbliss", doCheatMapHide },
  739. {"vcmiobelisk", doCheatRevealPuzzle },
  740. {"nwcoracle", doCheatRevealPuzzle },
  741. {"vcmiluck", doCheatMaxLuck },
  742. {"nwcfollowthewhiterabbit", doCheatMaxLuck },
  743. {"vcmimorale", doCheatMaxMorale },
  744. {"nwcmorpheus", doCheatMaxMorale },
  745. {"vcmigod", doCheatTheOne },
  746. {"nwctheone", doCheatTheOne },
  747. {"vcmiscrolls", doCheatGiveScrolls },
  748. };
  749. assert(callbacks.count(cheatName));
  750. if (callbacks.count(cheatName))
  751. callbacks.at(cheatName)();
  752. }
  753. void PlayerMessageProcessor::sendSystemMessage(std::shared_ptr<CConnection> connection, const MetaString & message)
  754. {
  755. SystemMessage sm;
  756. sm.text = message;
  757. connection->sendPack(sm);
  758. }
  759. void PlayerMessageProcessor::sendSystemMessage(std::shared_ptr<CConnection> connection, const std::string & message)
  760. {
  761. MetaString str;
  762. str.appendRawString(message);
  763. sendSystemMessage(connection, str);
  764. }
  765. void PlayerMessageProcessor::broadcastSystemMessage(MetaString message)
  766. {
  767. SystemMessage sm;
  768. sm.text = message;
  769. gameHandler->sendToAllClients(sm);
  770. }
  771. void PlayerMessageProcessor::broadcastSystemMessage(const std::string & message)
  772. {
  773. MetaString str;
  774. str.appendRawString(message);
  775. broadcastSystemMessage(str);
  776. }
  777. void PlayerMessageProcessor::broadcastMessage(PlayerColor playerSender, const std::string & message)
  778. {
  779. PlayerMessageClient temp_message(playerSender, message);
  780. gameHandler->sendAndApply(temp_message);
  781. }