scripted_model.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * scripted_model.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 "schema/base.h"
  12. #include "scripted_model.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. int ScriptedModel::getVersion()
  41. {
  42. return -1;
  43. };
  44. // The below methods should never be called on this object:
  45. int ScriptedModel::getAction(const MMAI::Schema::IState * s)
  46. {
  47. warn("getAction", -666);
  48. return -666;
  49. };
  50. double ScriptedModel::getValue(const MMAI::Schema::IState * s)
  51. {
  52. warn("getValue", -666);
  53. return -666;
  54. };
  55. void ScriptedModel::warn(const std::string & m, int retval) const
  56. {
  57. logAi->warn("method %s called on a ScriptedModel object; returning %d\n", m.c_str(), retval);
  58. }
  59. }