Warrior.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include <iostream>
  3. #include <random>
  4. #include <ctime>
  5. #include "unit.h"
  6. #include "Warrior.h"
  7. Warrior::Warrior(std::string id){
  8. }
  9. Warrior::~Warrior(){}
  10. double Warrior::reduceIncomingDamage(std::string damageType, int damage) {//returns damage after reducing by defence
  11. assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
  12. damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M');
  13. assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
  14. assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
  15. if (damageType[0] == 'p' || damageType[0] == 'P') {
  16. srand(time(0));
  17. double reduced = (1 - 2.5 * physic_defence_ / 100) * damage;
  18. if (rand() % 10 == 0) {
  19. reduced = reduced / 2.0;
  20. }
  21. return reduced;
  22. }
  23. else if (damageType[0] == 'm' || damageType[0] == 'M') {
  24. return (1 - 2.5 * magic_defence_ / 100) * damage;
  25. }
  26. }
  27. bool Warrior::canAttackForDistance(int distance){
  28. return (canMoveForDistance(attack_cost_ * getMovementSpeed() + distance - attack_range_));
  29. }
  30. bool Warrior::canAttackToCell(Cell * destination){
  31. return lenOfActualPath(destination) != 0 &&
  32. canAttackForDistance(lenOfActualPath(destination));
  33. }
  34. bool Warrior::canAttackUnit(Unit * target){
  35. return canAttackToCell(target->getLocation());
  36. }