abstractsettings.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * abstractsettings.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 "abstractsettings.h"
  12. #include "../mapcontroller.h"
  13. #include "../../lib/mapObjects/CGHeroInstance.h"
  14. #include "../../lib/mapObjects/CGCreature.h"
  15. #include "../../lib/CHeroHandler.h"
  16. #include "../../lib/mapObjects/CGCreature.h"
  17. //parses date for lose condition (1m 1w 1d)
  18. int expiredDate(const QString & date)
  19. {
  20. int result = 0;
  21. for(auto component : date.split(" "))
  22. {
  23. int days = component.left(component.lastIndexOf('d')).toInt();
  24. int weeks = component.left(component.lastIndexOf('w')).toInt();
  25. int months = component.left(component.lastIndexOf('m')).toInt();
  26. result += days > 0 ? days - 1 : 0;
  27. result += (weeks > 0 ? weeks - 1 : 0) * 7;
  28. result += (months > 0 ? months - 1 : 0) * 28;
  29. }
  30. return result;
  31. }
  32. QString expiredDate(int date)
  33. {
  34. QString result;
  35. int m = date / 28;
  36. int w = (date % 28) / 7;
  37. int d = date % 7;
  38. if(m)
  39. result += QString::number(m) + "m";
  40. if(w)
  41. {
  42. if(!result.isEmpty())
  43. result += " ";
  44. result += QString::number(w) + "w";
  45. }
  46. if(d)
  47. {
  48. if(!result.isEmpty())
  49. result += " ";
  50. result += QString::number(d) + "d";
  51. }
  52. return result;
  53. }
  54. int3 posFromJson(const JsonNode & json)
  55. {
  56. return int3(json.Vector()[0].Integer(), json.Vector()[1].Integer(), json.Vector()[2].Integer());
  57. }
  58. std::vector<JsonNode> linearJsonArray(const JsonNode & json)
  59. {
  60. std::vector<JsonNode> result;
  61. if(json.getType() == JsonNode::JsonType::DATA_STRUCT)
  62. result.push_back(json);
  63. if(json.getType() == JsonNode::JsonType::DATA_VECTOR)
  64. {
  65. for(auto & node : json.Vector())
  66. {
  67. auto subvector = linearJsonArray(node);
  68. result.insert(result.end(), subvector.begin(), subvector.end());
  69. }
  70. }
  71. return result;
  72. }
  73. AbstractSettings::AbstractSettings(QWidget *parent)
  74. : QWidget{parent}
  75. {
  76. }
  77. void AbstractSettings::initialize(MapController & c)
  78. {
  79. controller = &c;
  80. }
  81. std::string AbstractSettings::getTownName(const CMap & map, int objectIdx)
  82. {
  83. std::string name;
  84. if(auto town = dynamic_cast<const CGTownInstance*>(map.objects[objectIdx].get()))
  85. {
  86. name = town->getNameTranslated();
  87. if(name.empty())
  88. name = town->getTown()->faction->getNameTranslated();
  89. }
  90. return name;
  91. }
  92. std::string AbstractSettings::getHeroName(const CMap & map, int objectIdx)
  93. {
  94. std::string name;
  95. if(auto hero = dynamic_cast<const CGHeroInstance*>(map.objects[objectIdx].get()))
  96. {
  97. name = hero->getNameTranslated();
  98. }
  99. return name;
  100. }
  101. std::string AbstractSettings::getMonsterName(const CMap & map, int objectIdx)
  102. {
  103. std::string name;
  104. if(auto monster = dynamic_cast<const CGCreature*>(map.objects[objectIdx].get()))
  105. {
  106. name = boost::str(boost::format("%1% at %2%") % monster->getObjectName() % monster->anchorPos().toString());
  107. }
  108. return name;
  109. }
  110. JsonNode AbstractSettings::conditionToJson(const EventCondition & event)
  111. {
  112. JsonNode result;
  113. result["condition"].Integer() = event.condition;
  114. result["value"].Integer() = event.value;
  115. result["objectType"].String() = event.objectType.toString();
  116. result["objectInstanceName"].String() = event.objectInstanceName;
  117. {
  118. auto & position = result["position"].Vector();
  119. position.resize(3);
  120. position[0].Float() = event.position.x;
  121. position[1].Float() = event.position.y;
  122. position[2].Float() = event.position.z;
  123. }
  124. return result;
  125. };