base.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * base.h, 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. #pragma once
  11. #include <any>
  12. #include <string>
  13. #include <vector>
  14. #include <boost/core/demangle.hpp>
  15. #include <boost/format.hpp>
  16. // Import + Export macro declarations
  17. // If MMAI_DLL is defined => this header is imported from VCMI's MMAI lib.
  18. // Otherwise, it is imported from vcmi-gym.
  19. #if defined(_WIN32)
  20. # if defined(__GNUC__)
  21. # define MMAI_IMPORT __attribute__((dllimport))
  22. # define MMAI_EXPORT __attribute__((dllexport))
  23. # else
  24. # define MMAI_IMPORT __declspec(dllimport)
  25. # define MMAI_EXPORT __declspec(dllexport)
  26. # endif
  27. # ifndef ELF_VISIBILITY
  28. # define ELF_VISIBILITY
  29. # endif
  30. #else
  31. # ifdef __GNUC__
  32. # define MMAI_IMPORT __attribute__((visibility("default")))
  33. # define MMAI_EXPORT __attribute__((visibility("default")))
  34. # ifndef ELF_VISIBILITY
  35. # define ELF_VISIBILITY __attribute__((visibility("default")))
  36. # endif
  37. # endif
  38. #endif
  39. #ifdef MMAI_DLL
  40. # define MMAI_DLL_LINKAGE MMAI_EXPORT
  41. #else
  42. # define MMAI_DLL_LINKAGE MMAI_IMPORT
  43. #endif
  44. namespace MMAI::Schema
  45. {
  46. #define EI(enum_value) static_cast<int>(enum_value)
  47. using Action = int;
  48. using BattlefieldState = std::vector<float>;
  49. using ActionMask = std::vector<bool>;
  50. using AttentionMask = std::vector<float>;
  51. // Same control actions for all versions
  52. constexpr Action ACTION_RETREAT = 0;
  53. constexpr Action ACTION_RESET = -1;
  54. constexpr Action ACTION_RENDER_ANSI = -2;
  55. class IState
  56. {
  57. public:
  58. virtual const ActionMask * getActionMask() const = 0;
  59. virtual const AttentionMask * getAttentionMask() const = 0;
  60. virtual const BattlefieldState * getBattlefieldState() const = 0;
  61. // Supplementary data may differ across versions => expose it as std::any
  62. // XXX: ensure the real data type has MMAI_DLL_LINKAGE to prevent std::any_cast errors
  63. virtual std::any getSupplementaryData() const = 0;
  64. virtual int version() const = 0;
  65. virtual ~IState() = default;
  66. };
  67. enum class ModelType : int
  68. {
  69. SCRIPTED, // e.g. BattleAI, StupidAI
  70. NN, // pre-trained models stored in a file
  71. _count
  72. };
  73. enum class Side : int
  74. {
  75. LEFT, // BattleSide::LEFT
  76. RIGHT, // BattleSide::RIGHT
  77. BOTH // for models able to play as either left or right
  78. };
  79. class IModel
  80. {
  81. public:
  82. virtual ModelType getType() = 0;
  83. virtual std::string getName() = 0;
  84. virtual int getVersion() = 0;
  85. virtual int getAction(const IState *) = 0;
  86. virtual double getValue(const IState *) = 0;
  87. virtual Side getSide() = 0;
  88. virtual ~IModel() = default;
  89. };
  90. // Convenience formatter for std::any cast errors
  91. inline std::string AnyCastError(const std::any & any, const std::type_info & t)
  92. {
  93. if(!any.has_value())
  94. {
  95. return "no value";
  96. }
  97. else if(any.type() != t)
  98. {
  99. return boost::str(
  100. boost::format("type mismatch: want: %s/%u, have: %s/%u") % boost::core::demangle(t.name()) % t.hash_code()
  101. % boost::core::demangle(any.type().name()) % any.type().hash_code()
  102. );
  103. }
  104. else
  105. {
  106. return "";
  107. }
  108. }
  109. }