BonusEnum.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * BonusEnum.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 "BonusEnum.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. #define BONUS_NAME(x) { #x, BonusType::x },
  14. const std::map<std::string, BonusType> bonusNameMap = {
  15. BONUS_LIST
  16. };
  17. #undef BONUS_NAME
  18. #define BONUS_VALUE(x) { #x, BonusValueType::x },
  19. const std::map<std::string, BonusValueType> bonusValueMap = { BONUS_VALUE_LIST };
  20. #undef BONUS_VALUE
  21. #define BONUS_SOURCE(x) { #x, BonusSource::x },
  22. const std::map<std::string, BonusSource> bonusSourceMap = { BONUS_SOURCE_LIST };
  23. #undef BONUS_SOURCE
  24. #define BONUS_ITEM(x) { #x, BonusDuration::x },
  25. const std::map<std::string, BonusDuration::Type> bonusDurationMap =
  26. {
  27. BONUS_ITEM(PERMANENT)
  28. BONUS_ITEM(ONE_BATTLE)
  29. BONUS_ITEM(ONE_DAY)
  30. BONUS_ITEM(ONE_WEEK)
  31. BONUS_ITEM(N_TURNS)
  32. BONUS_ITEM(N_DAYS)
  33. BONUS_ITEM(UNTIL_BEING_ATTACKED)
  34. BONUS_ITEM(UNTIL_ATTACK)
  35. BONUS_ITEM(STACK_GETS_TURN)
  36. BONUS_ITEM(COMMANDER_KILLED)
  37. { "UNITL_BEING_ATTACKED", BonusDuration::UNTIL_BEING_ATTACKED }//typo, but used in some mods
  38. };
  39. #undef BONUS_ITEM
  40. #define BONUS_ITEM(x) { #x, BonusLimitEffect::x },
  41. const std::map<std::string, BonusLimitEffect> bonusLimitEffect =
  42. {
  43. BONUS_ITEM(NO_LIMIT)
  44. BONUS_ITEM(ONLY_DISTANCE_FIGHT)
  45. BONUS_ITEM(ONLY_MELEE_FIGHT)
  46. };
  47. #undef BONUS_ITEM
  48. namespace BonusDuration
  49. {
  50. JsonNode toJson(const Type & duration)
  51. {
  52. std::vector<std::string> durationNames;
  53. for(auto durBit = 0; durBit < duration.size(); durBit++)
  54. {
  55. if(duration[durBit])
  56. durationNames.push_back(vstd::findKey(bonusDurationMap, duration & Type().set(durBit)));
  57. }
  58. if(durationNames.size() == 1)
  59. {
  60. return JsonUtils::stringNode(durationNames[0]);
  61. }
  62. else
  63. {
  64. JsonNode node(JsonNode::JsonType::DATA_VECTOR);
  65. for(const std::string & dur : durationNames)
  66. node.Vector().push_back(JsonUtils::stringNode(dur));
  67. return node;
  68. }
  69. }
  70. }
  71. VCMI_LIB_NAMESPACE_END