ScriptedModel.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * ScriptedModel.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 "ScriptedModel.h"
  12. #include "schema/base.h"
  13. namespace MMAI::BAI
  14. {
  15. ScriptedModel::ScriptedModel(const std::string & keyword) : keyword(keyword)
  16. {
  17. static const std::vector<std::string> FALLBACKS = {"StupidAI", "BattleAI"};
  18. auto it = std::ranges::find(FALLBACKS, keyword);
  19. if(it == FALLBACKS.end())
  20. throw std::runtime_error("Unsupported fallback keyword: " + keyword);
  21. }
  22. std::string ScriptedModel::getName()
  23. {
  24. return keyword;
  25. }
  26. Schema::ModelType ScriptedModel::getType()
  27. {
  28. return Schema::ModelType::SCRIPTED;
  29. }
  30. Schema::Side ScriptedModel::getSide()
  31. {
  32. return Schema::Side::BOTH;
  33. }
  34. // SCRIPTED models are dummy models which should not be used for anything
  35. // other than their getType() and getName() methods. Based on the return
  36. // value, the corresponding scripted bot (e.g. StupidAI) should be
  37. // used for the upcoming battle instead.
  38. // When MMAI fails to load an ML model, it loads a SCRIPTED model instead
  39. // as per MMAI mod's "fallback" setting in order to prevent a game crash.
  40. // The below methods should never be called on this object:
  41. int ScriptedModel::getVersion()
  42. {
  43. warn("getVersion", -666);
  44. return -666;
  45. };
  46. int ScriptedModel::getAction(const MMAI::Schema::IState * s)
  47. {
  48. warn("getAction", -666);
  49. return -666;
  50. };
  51. double ScriptedModel::getValue(const MMAI::Schema::IState * s)
  52. {
  53. warn("getValue", -666);
  54. return -666;
  55. };
  56. void ScriptedModel::warn(const std::string & m, int retval) const
  57. {
  58. logAi->error("WARNING: method %s called on a ScriptedModel object; returning %d\n", m.c_str(), retval);
  59. }
  60. }