BonusEnum.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_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. BONUS_ITEM(UNTIL_OWN_ATTACK)
  39. { "UNITL_BEING_ATTACKED", BonusDuration::UNTIL_BEING_ATTACKED }//typo, but used in some mods
  40. };
  41. #undef BONUS_ITEM
  42. #define BONUS_ITEM(x) { #x, BonusLimitEffect::x },
  43. const std::map<std::string, BonusLimitEffect> bonusLimitEffect =
  44. {
  45. BONUS_ITEM(NO_LIMIT)
  46. BONUS_ITEM(ONLY_DISTANCE_FIGHT)
  47. BONUS_ITEM(ONLY_MELEE_FIGHT)
  48. };
  49. #undef BONUS_ITEM
  50. namespace BonusDuration
  51. {
  52. JsonNode toJson(const Type & duration)
  53. {
  54. std::vector<std::string> durationNames;
  55. for(size_t durBit = 0; durBit < Size; durBit++)
  56. {
  57. Type value = duration & (1 << durBit);
  58. if(value)
  59. durationNames.push_back(vstd::findKey(bonusDurationMap, value));
  60. }
  61. if(durationNames.size() == 1)
  62. {
  63. return JsonNode(durationNames[0]);
  64. }
  65. else
  66. {
  67. JsonNode node;
  68. for(const std::string & dur : durationNames)
  69. node.Vector().emplace_back(dur);
  70. return node;
  71. }
  72. }
  73. }
  74. VCMI_LIB_NAMESPACE_END