PlayerMessageProcessor.cpp 29 KB

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