abstractsettings.cpp 3.4 KB

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