BonusEnum.cpp 1.9 KB

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