unit.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "abstractfactory.h"
  2. #include "units/unit.h"
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <string>
  7. #include <QFile>
  8. #include <QString>
  9. #include <QTextStream>
  10. Unit::Unit(QString parameters) {
  11. QStringList params = parameters.split("|");
  12. assert(params.size() >= 2);
  13. race_id_ = params[0];
  14. unit_id_ = params[1];
  15. QString unit_folder = ":/assets/units/" + race_id_ + "/" + unit_id_ + "/";
  16. loadUnitName(unit_folder);
  17. loadUnitDescr(unit_folder);
  18. loadUnitBaseClass(unit_folder);
  19. loadUnitTraits(unit_folder);
  20. loadUnitIcon(unit_folder);
  21. loadUnitPrevSpecs(unit_folder);
  22. loadUnitUpgradeSpecs(unit_folder);
  23. }
  24. void Unit::loadUnitName(QString unit_folder) {
  25. QFile file(unit_folder + "unitname.txt");
  26. file.open(QIODevice::ReadOnly);
  27. QTextStream in(&file);
  28. in.setCodec("UTF-8");
  29. unit_name_ = in.readLine();
  30. }
  31. void Unit::loadUnitDescr(QString unit_folder) {
  32. QFile file(unit_folder + "descr.txt");
  33. file.open(QIODevice::ReadOnly);
  34. QTextStream in(&file);
  35. in.setCodec("UTF-8");
  36. unit_descr_ = in.readAll();
  37. }
  38. void Unit::loadUnitBaseClass(QString unit_folder) {
  39. QFile file(unit_folder + "baseclass.txt");
  40. file.open(QIODevice::ReadOnly);
  41. QTextStream in(&file);
  42. in.setCodec("UTF-8");
  43. base_class_id_ = in.readLine();
  44. }
  45. void Unit::loadUnitTraits(QString unit_folder) {
  46. // TO BE DONE!!!
  47. health_points_ = rand();
  48. activity_points_ = rand();
  49. attack_cost_ = rand();
  50. attack_range_ = rand();
  51. }
  52. void Unit::loadUnitIcon(QString unit_folder) {
  53. unit_icon_.load(unit_folder + "icon.png");
  54. }
  55. void Unit::loadUnitPrevSpecs(QString unit_folder) {
  56. QFile file(unit_folder + "prevgrades.txt");
  57. file.open(QIODevice::ReadOnly);
  58. QTextStream in(&file);
  59. in.setCodec("UTF-8");
  60. QString line = in.readLine();
  61. while (!in.atEnd()) {
  62. parent_specs_.push_back(line);
  63. line = in.readLine();
  64. }
  65. parent_specs_.push_back(line);
  66. line = in.readLine();
  67. }
  68. void Unit::loadUnitUpgradeSpecs(QString unit_folder) {
  69. QFile file(unit_folder + "nextgrades.txt");
  70. file.open(QIODevice::ReadOnly);
  71. QTextStream in(&file);
  72. in.setCodec("UTF-8");
  73. QString line = in.readLine();
  74. while (!in.atEnd()) {
  75. upgrade_specs_.push_back(line);
  76. line = in.readLine();
  77. }
  78. upgrade_specs_.push_back(line);
  79. line = in.readLine();
  80. }
  81. int Unit::getCost(){
  82. return cost_;
  83. }
  84. std::vector<QString> Unit::getParentSpecs(){
  85. return parent_specs_;
  86. }
  87. std::vector<QString> Unit::getUpgradeSpecs(){
  88. return upgrade_specs_;
  89. }
  90. double Unit::getExperience() {
  91. return experience_;
  92. }
  93. double Unit::getLevel() {
  94. return level_;
  95. }
  96. double Unit::getHealthPoints() {
  97. return health_points_;
  98. }
  99. double Unit::getAttackRange() {
  100. return attack_range_;
  101. }
  102. int Unit::getActivityPoints(){
  103. return activity_points_;
  104. }
  105. int Unit::getStartingActivityPoints() {
  106. return starting_activity_points_;
  107. }
  108. Cell* Unit::getLocation() {
  109. return location_;
  110. }
  111. void Unit::setLocation(Cell* to) {
  112. location_ = to;
  113. }
  114. int Unit::getMovementSpeed() {
  115. return movement_speed_;
  116. }
  117. int Unit::getAttackCost(){
  118. return attack_cost_;
  119. }
  120. double Unit::getInitiative() {
  121. return initiative_;
  122. }
  123. double Unit::getDamagePerHit() {
  124. return damage_per_hit_;
  125. }
  126. double Unit::getIntelligence() {
  127. return intelligence_;
  128. }
  129. double Unit::getStrength() {
  130. return strength_;
  131. }
  132. double Unit::getAgility() {
  133. return agility_;
  134. }
  135. int Unit::getAttackPoints(){
  136. return attack_cost_;
  137. }
  138. double Unit::getMagicDefence() {
  139. return magic_defence_;
  140. }
  141. double Unit::getPhysicDefence() {
  142. return physic_defence_;
  143. }
  144. double Unit::getRealX() {
  145. return real_x_;
  146. }
  147. void Unit::setRealX(double x) {
  148. real_x_ = x;
  149. }
  150. double Unit::getRealY() {
  151. return real_y_;
  152. }
  153. void Unit::setRealY(double y) {
  154. real_y_ = y;
  155. }
  156. void Unit::calculateDamagePerHit() {
  157. damage_per_hit_ = 0.5 * std::max(getAgility(), std::max(getStrength(), getIntelligence()));
  158. }
  159. double Unit::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
  160. assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
  161. (damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M'));
  162. assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
  163. assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
  164. if (damageType[0] == 'p' || damageType[0] == 'P') {
  165. return (1 - 2.5 * physic_defence_ / 100) * damage;
  166. }
  167. else if (damageType[0] == 'm' || damageType[0] == 'M') {
  168. return (1 - 2.5 * magic_defence_ / 100) * damage;
  169. }
  170. }
  171. int Unit::lenOfActualPath(Cell* destination) {
  172. return getLocation()->actualPath(destination).size();
  173. }
  174. bool Unit::canMoveForDistance(int distance) {
  175. return (activity_points_ * movement_speed_ >= distance);
  176. }
  177. bool Unit::canMoveToCell(Cell* destination) {
  178. return (destination->isEmpty() && lenOfActualPath(destination) > 0 && canMoveForDistance(lenOfActualPath(destination)));
  179. }
  180. void Unit::moveToCell(Cell* destination) {
  181. if (!canMoveToCell(destination))
  182. return; //here could be a gui-message about failed move (x-mark, for example)
  183. else {
  184. movement_speed_ -= lenOfActualPath(destination);
  185. setLocation(destination);
  186. }
  187. }
  188. QString Unit::getUnitId() const {
  189. return unit_id_;
  190. }
  191. QString Unit::getUnitName() const {
  192. return unit_name_;
  193. }
  194. QString Unit::getUnitDescr() const {
  195. return unit_descr_;
  196. }
  197. QString Unit::getUnitBaseClassId() const {
  198. return base_class_id_;
  199. }
  200. std::vector<QString> Unit::getUnitTraits() const {
  201. return {
  202. QString::number(health_points_),
  203. QString::number(attack_range_),
  204. QString::number(activity_points_),
  205. QString::number(initiative_)
  206. };
  207. }
  208. QImage Unit::getUnitIcon() const {
  209. return unit_icon_;
  210. }