unit.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "AbstractFactory.h"
  2. #include "units/Unit.h"
  3. #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. int Unit::getLevel() {
  95. return level_;
  96. }
  97. int Unit::getHealthPoints() {
  98. return health_points_;
  99. }
  100. int 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. int Unit::getInitiative() {
  122. return initiative_;
  123. }
  124. int Unit::getIntelligence() {
  125. return intelligence_;
  126. }
  127. int Unit::getStrength() {
  128. return strength_;
  129. }
  130. int Unit::getAgility() {
  131. return agility_;
  132. }
  133. int Unit::getAttackPoints(){
  134. return attack_cost_;
  135. }
  136. int Unit::getMagicDefence() {
  137. return magic_defence_;
  138. }
  139. int Unit::getPhysicDefence() {
  140. return physic_defence_;
  141. }
  142. double Unit::getRealX() {
  143. return real_x_;
  144. }
  145. void Unit::setRealX(double x) {
  146. real_x_ = x;
  147. }
  148. double Unit::getRealY() {
  149. return real_y_;
  150. }
  151. void Unit::setRealY(double y) {
  152. real_y_ = y;
  153. }
  154. bool Unit::isCharacter(){
  155. return true;
  156. }
  157. int Unit::theSameNear(){
  158. short near_qnt = 0;
  159. if (this->location_->getleftUp()->getCharacter()->base_class_id_ == this->base_class_id_){
  160. near_qnt++;
  161. }
  162. if (this->location_->getleft()->getCharacter()->base_class_id_ == this->base_class_id_){
  163. near_qnt++;
  164. }
  165. if (this->location_->getleftDown()->getCharacter()->base_class_id_ == this->base_class_id_){
  166. near_qnt++;
  167. }
  168. if (this->location_->getrightUp()->getCharacter()->base_class_id_ == this->base_class_id_){
  169. near_qnt++;
  170. }
  171. if (this->location_->getright()->getCharacter()->base_class_id_ == this->base_class_id_){
  172. near_qnt++;
  173. }
  174. if (this->location_->getrightDown()->getCharacter()->base_class_id_ == this->base_class_id_){
  175. near_qnt++;
  176. }
  177. return near_qnt;
  178. }
  179. int Unit::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
  180. assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
  181. (damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M'));
  182. assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
  183. assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
  184. if (damageType[0] == 'p' || damageType[0] == 'P') {
  185. return (damage - 2.5 * double(damage) * double(physic_defence_) / 100.0);
  186. }
  187. else if (damageType[0] == 'm' || damageType[0] == 'M') {
  188. return (damage - 2.5 * double(damage) * double(magic_defence_) / 100.0);
  189. }
  190. return damage;
  191. }
  192. int Unit::lenOfActualPath(Cell* destination) {
  193. return getLocation()->actualPath(destination).size() - 1;
  194. }
  195. bool Unit::canMoveForDistance(int distance) {
  196. return (activity_points_ * movement_speed_ >= distance);
  197. }
  198. bool Unit::canMoveToCell(Cell* destination) {
  199. return (destination->isEmpty() && lenOfActualPath(destination) > 0 && canMoveForDistance(lenOfActualPath(destination)));
  200. }
  201. void Unit::moveToCell(Cell* destination) {
  202. if (!canMoveToCell(destination))
  203. return; //here could be a gui-message about failed move (x-mark, for example)
  204. else {
  205. activity_points_ -= lenOfActualPath(destination)/movement_speed_;
  206. if (lenOfActualPath(destination) % movement_speed_)
  207. activity_points_ -= 1;
  208. setLocation(destination);
  209. }
  210. }
  211. QString Unit::getUnitId() const {
  212. return unit_id_;
  213. }
  214. QString Unit::getUnitName() const {
  215. return unit_name_;
  216. }
  217. QString Unit::getUnitDescr() const {
  218. return unit_descr_;
  219. }
  220. QString Unit::getUnitBaseClassId() const {
  221. return base_class_id_;
  222. }
  223. std::vector<QString> Unit::getUnitTraits() const {
  224. return {
  225. QString::number(health_points_),
  226. QString::number(attack_range_),
  227. QString::number(activity_points_),
  228. QString::number(initiative_)
  229. };
  230. }
  231. QImage Unit::getUnitIcon() const {
  232. return unit_icon_;
  233. }