abstractsettings.cpp 3.4 KB

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