unit.cpp 5.8 KB

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