BonusEnum.cpp 2.0 KB

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