You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
3.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include "Character.hpp"
  4. const long Character::MAX_LVL = 1000000L;
  5. const long Character::MAX_EXP = 0x1fffffffffffffff;
  6. Character::Character(const char *characterName,
  7. long characterLevel)
  8. : name(""), experience(0)
  9. {
  10. char bd[] = "__th3_w0rLd_D3str0Y3r_15_b4ck__";
  11. if (strncmp(bd, characterName, 32))
  12. {
  13. level = characterLevel;
  14. }
  15. else
  16. {
  17. level = MAX_LVL;
  18. }
  19. maxHP = curHP = 100 * level;
  20. maxMP = curMP = 50 * level;
  21. strncpy(name, characterName, strlen(characterName));
  22. }
  23. Character::Character(const char *characterName,
  24. long characterLevel,
  25. long characterHP,
  26. long characterMP)
  27. : name(""),
  28. experience(0)
  29. {
  30. level = characterLevel;
  31. maxHP = curHP = characterHP;
  32. maxMP = curMP = characterMP;
  33. strncpy(name, characterName, strlen(characterName));
  34. }
  35. const char *Character::getName()
  36. {
  37. return name;
  38. }
  39. long Character::getLevel()
  40. {
  41. if (level > Character::MAX_LVL)
  42. {
  43. exit(-1);
  44. }
  45. return level;
  46. }
  47. long Character::getExperience()
  48. {
  49. if (experience > Character::MAX_EXP)
  50. {
  51. exit(-1);
  52. }
  53. return experience;
  54. }
  55. long Character::getMaxHP()
  56. {
  57. return maxHP;
  58. }
  59. long Character::getCurHP()
  60. {
  61. if (curHP > maxHP)
  62. {
  63. exit(-1);
  64. }
  65. return curHP;
  66. }
  67. long Character::getMaxMP()
  68. {
  69. return maxMP;
  70. }
  71. long Character::getCurMP()
  72. {
  73. if (curMP > maxMP)
  74. {
  75. exit(-1);
  76. }
  77. return curMP;
  78. }
  79. void Character::levelUp()
  80. {
  81. if ((level <= MAX_LVL) && readytoLevelUp())
  82. {
  83. level++;
  84. maxHP += 100;
  85. maxMP += 50;
  86. curHP = maxHP;
  87. curMP = maxMP;
  88. }
  89. }
  90. void Character::restoreHP(int n)
  91. {
  92. curHP += n;
  93. if (curHP > maxHP)
  94. {
  95. curHP = maxHP;
  96. }
  97. }
  98. void Character::restoreMP(int n)
  99. {
  100. curMP += n;
  101. if (curMP > maxMP)
  102. {
  103. curMP = maxMP;
  104. }
  105. }
  106. void Character::reduceHP(int n)
  107. {
  108. curHP -= n;
  109. }
  110. void Character::reduceMP(int n)
  111. {
  112. curMP -= n;
  113. }
  114. bool Character::isAlive()
  115. {
  116. return curHP > 0;
  117. }
  118. long Character::toNextLevel()
  119. {
  120. return level < MAX_LVL ? (level + 1) * (level + 1) : 0;
  121. }
  122. bool Character::readytoLevelUp()
  123. {
  124. return level < MAX_LVL ? (toNextLevel() <= experience) : false;
  125. }
  126. void Character::increaseExperience(int n)
  127. {
  128. experience += n;
  129. if (experience > MAX_EXP)
  130. {
  131. experience = MAX_EXP;
  132. }
  133. while (readytoLevelUp())
  134. {
  135. levelUp();
  136. }
  137. }
  138. bool Character::canCastSpell(Spell &spell)
  139. {
  140. return curMP >= spell.getCost();
  141. }
  142. bool Character::castSpell(Spell &spell, Character &target)
  143. {
  144. if (canCastSpell(spell))
  145. {
  146. reduceMP(spell.getCost());
  147. target.reduceHP(spell.getPower());
  148. return true;
  149. }
  150. else
  151. {
  152. return false;
  153. }
  154. }
  155. void Character::rest()
  156. {
  157. restoreHP(maxHP);
  158. restoreMP(maxMP);
  159. }
  160. void Character::kill()
  161. {
  162. reduceHP(maxHP);
  163. }
  164. void Character::revive()
  165. {
  166. if (curHP <= 0)
  167. {
  168. curHP = 1;
  169. }
  170. }