ActionAttack.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../Rule.ts" />
  3. /// <reference path="../Rulebook.ts" />
  4. /// <reference path="../Things/Weapon/Weapon.ts" />
  5. enum ActionAttackReaction {
  6. DODGE, RESIST, COUNTERATTACK
  7. }
  8. class ActionAttack extends Action {
  9. public static check = new Rulebook<ActionAttack>("Check Attack");
  10. public static carry = new Rulebook<ActionAttack>("Carry out Attack");
  11. public actingAgressively = true;
  12. public aggressivenessRating = 1;
  13. private targetReaction : ActionAttackReaction;
  14. public allowedStances = [PersonStance.STANDING];
  15. public static randomness = 2;
  16. public static dodgeCost = 1;
  17. public static fistCost = 1;
  18. public static gettingAttacked = new OneOf(OneOf.PURELY_AT_RANDOM,
  19. "moves on to attack you!",
  20. "attacks you!",
  21. "tries to attack you!",
  22. "decides to attack you!",
  23. "initiates an attack!",
  24. "is attacking!",
  25. "approaches with malicious intent!",
  26. "aims at you with malicious intent!",
  27. "aims at you!",
  28. "goes for you!",
  29. "comes for you!",
  30. "seems hostile!",
  31. "seems to want to fight!",
  32. "wants to fight!",
  33. "is coming right for you!",
  34. "is coming right at you!",
  35. "is not friendly at all!"
  36. );
  37. // TODO: Add infinitely more of these...
  38. public constructor (actor : Thing, ...nouns : Array<any>) {
  39. super(actor, ...nouns);
  40. this.requiresNoun = true;
  41. this.requiresVisibility = false;
  42. }
  43. public getTarget () : Thing {
  44. return this.getNoun(0);
  45. }
  46. public getWeapons () : Array<Weapon> {
  47. let weaponNoun = this.getNoun(1);
  48. if (weaponNoun == undefined) {
  49. let weapons = <Array<Weapon>> this.actor.getWorns(Weapon);
  50. return weapons;
  51. }
  52. return [weaponNoun];
  53. }
  54. public getCommandText () {
  55. let weaponName;
  56. let weapons = this.getWeapons();
  57. if (weapons.length == 0) {
  58. weaponName = "fists";
  59. } else {
  60. weaponName = [];
  61. weapons.forEach((weapon => {
  62. weaponName.push(weapon.getPrintedName());
  63. }));
  64. weaponName = weaponName.join(" and ");
  65. }
  66. return "attack " + this.getNoun(0).getPrintedName() + " with " + weaponName;
  67. }
  68. public generateDescription (group : ContentGroup) {
  69. this.say.add(...(CombatDescription.getDescription(group)));
  70. }
  71. public static checkAttackable = ActionAttack.check.createAndAddRule({
  72. name : "Attack - Is it attackable?",
  73. firstPriority : Rule.PRIORITY_HIGHEST,
  74. code : async (runner : RulebookRunner<ActionAttack>) => {
  75. let action = runner.noun;
  76. let target = runner.noun.getTarget();
  77. if (!(target instanceof Person) && !target.breakable) {
  78. if (action.actor == WorldState.player) {
  79. action.say.add("You can't possibly damage that.");
  80. }
  81. action.stop();
  82. return false;
  83. }
  84. }
  85. });
  86. public static checkAccuracyRule = ActionAttack.check.createAndAddRule({
  87. name : "Attack - Is it a hit?",
  88. code : async (runner : RulebookRunner<ActionAttack>) => {
  89. let action = runner.noun;
  90. let actorDex = (<Person> runner.noun.actor).getStat(Attributes.Agility);
  91. let targetDex = 0;
  92. let target = runner.noun.getTarget();
  93. if (target instanceof Person) {
  94. targetDex = target.getStat(Attributes.Agility);
  95. }
  96. let reaction = ActionAttackReaction.DODGE;
  97. if (target == WorldState.player) {
  98. Elements.CurrentTurnHandler.printAsContent(new Say(action.actor, " ", ActionAttack.gettingAttacked.getOne()));
  99. let choices = ["Counter-Attack", "Dodge", "Resist"];
  100. let choice = await Controls.giveChoices(false, ...choices);
  101. if (choice[1] == 0) {
  102. reaction = ActionAttackReaction.COUNTERATTACK;
  103. } else if (choice[1] == 2) {
  104. reaction = ActionAttackReaction.DODGE;
  105. } else {
  106. reaction = ActionAttackReaction.RESIST;
  107. }
  108. }
  109. let weapons = action.getWeapons();
  110. for (let i = 0; i < weapons.length; i++) {
  111. (<Person> action.actor).changeStamina(- weapons[i].attackCost);
  112. }
  113. if (weapons.length == 0) {
  114. (<Person> action.actor).changeStamina(- ActionAttack.fistCost)
  115. }
  116. action.targetReaction = reaction;
  117. if (reaction == ActionAttackReaction.DODGE) {
  118. (<Person> target).changeStamina(- ActionAttack.dodgeCost);
  119. let attack = Math.floor(Math.random() * (ActionAttack.randomness + ActionAttack.randomness + 1)) - ActionAttack.randomness;
  120. let defense = Math.floor(Math.random() * (ActionAttack.randomness + ActionAttack.randomness + 1)) - ActionAttack.randomness;
  121. attack += actorDex;
  122. defense += targetDex;
  123. if (attack < defense) {
  124. action.generateDescription(
  125. (new ContentGroup())
  126. .addUnit(
  127. (new CombatUnit())
  128. .setActor(action.actor)
  129. .setTarget(target)
  130. .setWeapon(...weapons)
  131. .addMarker(CombatHit.MISS)
  132. )
  133. );
  134. return false;
  135. }
  136. }
  137. }
  138. });
  139. public static carryOutAttack = ActionAttack.carry.createAndAddRule({
  140. name: "Attack - Go for the throat!",
  141. code: async (runner: RulebookRunner<ActionAttack>) => {
  142. let action = runner.noun;
  143. let target = runner.noun.getTarget();
  144. let damage = 0;
  145. let weapons = action.getWeapons();
  146. for (let i = 0; i < weapons.length; i++) {
  147. damage += weapons[i].getDamage();
  148. }
  149. if (weapons.length == 0) {
  150. damage += (<Person> action.actor).getStat(Attributes.Strength);
  151. }
  152. if (!(target instanceof Person)) {
  153. if (target.breakable && target.breakableOn <= damage) {
  154. target.break();
  155. action.generateDescription(
  156. (new ContentGroup())
  157. .addUnit(
  158. (new CombatUnit())
  159. .setActor(action.actor)
  160. .setTarget(target)
  161. .setWeapon(...weapons)
  162. .addMarker(CombatHit.HIT, CombatResult.KILLED)
  163. )
  164. );
  165. }
  166. } else {
  167. let damageReduction = target.getStat(Attributes.Strength);
  168. if (action.targetReaction == ActionAttackReaction.RESIST) {
  169. damageReduction = Math.floor(damageReduction * 1.5)
  170. }
  171. let finalDamage = damage - damageReduction;
  172. if (finalDamage < 0) {
  173. finalDamage = 0;
  174. }
  175. action.aggressivenessRating = 1 + finalDamage;
  176. let torso = <HumanoidTorso> (<Person> target).getPart(HumanoidTorso);
  177. if (torso != undefined) {
  178. torso.changeSoreness(finalDamage);
  179. }
  180. let hitType = (finalDamage * torso.getWeightedSoreness()) > (target.getMaxHealth() / 3) ? CombatHit.CRITICAL : CombatHit.HIT;
  181. let targetHealth = target.getHealthOnScale();
  182. let knockedOff = hitType == CombatHit.CRITICAL;
  183. if (knockedOff) {
  184. target.stance = PersonStance.ALLFOURS;
  185. }
  186. let result = targetHealth < -9 ? CombatResult.KILLED : targetHealth <= 0 ? CombatResult.KNOCKED_OFF :
  187. knockedOff ? CombatResult.KNOCKED : undefined;
  188. if (targetHealth < -9) {
  189. target.die();
  190. }
  191. action.generateDescription(
  192. (new ContentGroup())
  193. .addUnit(
  194. (new CombatUnit())
  195. .setActor(action.actor)
  196. .setTarget(target)
  197. .setWeapon(...weapons)
  198. .addMarker(hitType, result)
  199. )
  200. );
  201. }
  202. }
  203. });
  204. }
  205. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  206. {
  207. name : "Hyperlink - Attack",
  208. firstPriority : Rule.PRIORITY_HIGHEST,
  209. code : (rulebook : RulebookRunner<Thing>) => {
  210. let thing = <Thing> rulebook.noun;
  211. if ((thing instanceof Person || thing.breakable) && thing.isVisibleTo(WorldState.player)) {
  212. Elements.HyperlinkHandler.addAvailableAction("Attack", new ActionAttack(WorldState.player, thing));
  213. }
  214. }
  215. }
  216. ));