1
1

ActionAttack.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // TODO: Reduce these god functions into smaller functions that are easier to modify by future changes.
  87. public static checkAccuracyRule = ActionAttack.check.createAndAddRule({
  88. name : "Attack - Is it a hit?",
  89. code : async (runner : RulebookRunner<ActionAttack>) => {
  90. let action = runner.noun;
  91. let actorDex = (<Person> runner.noun.actor).getStat(Attributes.Agility);
  92. let actor = (<Person> runner.noun.actor);
  93. if (actor.stance == PersonStance.ALLFOURS) {
  94. actorDex = Math.floor(actorDex/2);
  95. }
  96. let targetDex = 0;
  97. let target = runner.noun.getTarget();
  98. if (target instanceof Person) {
  99. targetDex = target.getStat(Attributes.Agility);
  100. if (target.stance == PersonStance.ALLFOURS) {
  101. targetDex = Math.floor(targetDex/2);
  102. } else if (target.stance == PersonStance.KNOCKEDOUT) {
  103. targetDex = 0;
  104. }
  105. let reaction = ActionAttackReaction.DODGE;
  106. if (target.stance == PersonStance.KNOCKEDOUT) {
  107. reaction = ActionAttackReaction.RESIST;
  108. } else {
  109. if (target == WorldState.player) {
  110. Elements.CurrentTurnHandler.printAsContent(new Say(action.actor, " ", ActionAttack.gettingAttacked.getOne()));
  111. let choices = ["Counter-Attack", "Dodge", "Resist"];
  112. let choice = await Controls.giveChoices(false, ...choices);
  113. if (choice[1] == 0) {
  114. reaction = ActionAttackReaction.COUNTERATTACK;
  115. } else if (choice[1] == 2) {
  116. reaction = ActionAttackReaction.DODGE;
  117. } else {
  118. reaction = ActionAttackReaction.RESIST;
  119. }
  120. }
  121. }
  122. let weapons = action.getWeapons();
  123. for (let i = 0; i < weapons.length; i++) {
  124. (<Person> action.actor).changeStamina(- weapons[i].attackCost);
  125. }
  126. if (weapons.length == 0) {
  127. (<Person> action.actor).changeStamina(- ActionAttack.fistCost)
  128. }
  129. action.targetReaction = reaction;
  130. if (reaction == ActionAttackReaction.DODGE) {
  131. (<Person> target).changeStamina(- ActionAttack.dodgeCost);
  132. let attack = Math.floor(Math.random() * (ActionAttack.randomness + ActionAttack.randomness + 1)) - ActionAttack.randomness;
  133. let defense = Math.floor(Math.random() * (ActionAttack.randomness + ActionAttack.randomness + 1)) - ActionAttack.randomness;
  134. attack += actorDex;
  135. defense += targetDex;
  136. if (attack < defense) {
  137. action.generateDescription(
  138. (new ContentGroup())
  139. .addUnit(
  140. (new CombatUnit())
  141. .setActor(action.actor)
  142. .setTarget(target)
  143. .setWeapon(...weapons)
  144. .addMarker(CombatHit.MISS)
  145. )
  146. );
  147. return false;
  148. }
  149. }
  150. }
  151. }
  152. });
  153. public static carryOutAttack = ActionAttack.carry.createAndAddRule({
  154. name: "Attack - Go for the throat!",
  155. code: async (runner: RulebookRunner<ActionAttack>) => {
  156. let action = runner.noun;
  157. let target = runner.noun.getTarget();
  158. let damage = 0;
  159. let weapons = action.getWeapons();
  160. for (let i = 0; i < weapons.length; i++) {
  161. damage += weapons[i].getDamage();
  162. }
  163. if (weapons.length == 0) {
  164. damage += (<Person> action.actor).getStat(Attributes.Strength);
  165. }
  166. if (!(target instanceof Person)) {
  167. if (target.breakable && target.breakableOn <= damage) {
  168. target.break();
  169. action.generateDescription(
  170. (new ContentGroup())
  171. .addUnit(
  172. (new CombatUnit())
  173. .setActor(action.actor)
  174. .setTarget(target)
  175. .setWeapon(...weapons)
  176. .addMarker(CombatHit.HIT, CombatResult.KILLED)
  177. )
  178. );
  179. }
  180. } else {
  181. let damageReduction = target.getStat(Attributes.Strength);
  182. if (action.targetReaction == ActionAttackReaction.RESIST) {
  183. damageReduction = Math.floor(damageReduction * 1.5)
  184. }
  185. let finalDamage = damage - damageReduction;
  186. if (finalDamage < 0) {
  187. finalDamage = 0;
  188. }
  189. action.aggressivenessRating = 1 + finalDamage;
  190. let torso = <HumanoidTorso> (<Person> target).getPart(HumanoidTorso);
  191. if (torso != undefined) {
  192. torso.changeSoreness(finalDamage);
  193. }
  194. let hitType = (finalDamage * torso.getWeightedSoreness()) > (target.getMaxHealth() / 3) ? CombatHit.CRITICAL : CombatHit.HIT;
  195. let targetHealth = target.getHealthOnScale();
  196. let knockedOff = hitType == CombatHit.CRITICAL;
  197. if (knockedOff) {
  198. target.stance = PersonStance.ALLFOURS;
  199. }
  200. let result = targetHealth < -9 ? CombatResult.KILLED : targetHealth <= 0 ? CombatResult.KNOCKED_OFF :
  201. knockedOff ? CombatResult.KNOCKED : undefined;
  202. if (targetHealth < -9) {
  203. target.die();
  204. }
  205. action.generateDescription(
  206. (new ContentGroup())
  207. .addUnit(
  208. (new CombatUnit())
  209. .setActor(action.actor)
  210. .setTarget(target)
  211. .setWeapon(...weapons)
  212. .addMarker(hitType, result)
  213. )
  214. );
  215. }
  216. }
  217. });
  218. }
  219. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  220. {
  221. name : "Hyperlink - Attack",
  222. firstPriority : Rule.PRIORITY_HIGHEST,
  223. priority : Rule.PRIORITY_MEDIUM,
  224. code : (rulebook : RulebookRunner<Thing>) => {
  225. let thing = <Thing> rulebook.noun;
  226. if ((thing instanceof Person || thing.breakable) && thing.isVisibleTo(WorldState.player)) {
  227. Elements.HyperlinkHandler.addAvailableAction("Attack", new ActionAttack(WorldState.player, thing));
  228. }
  229. }
  230. }
  231. ));