ActionAttack.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../Rule.ts" />
  3. /// <reference path="../Rulebook.ts" />
  4. /// <reference path="../Tests/DiceDangerous.ts" />
  5. enum ActionAttackReaction {
  6. DODGE, RESIST, COUNTERATTACK
  7. }
  8. interface Weaponized {
  9. getAttribute : () => Attribute; // What is the attribute used for the damage
  10. getAttributeDamageFactor : () => number; // How much of the attribute is added for the attack.
  11. getAttributeForceFactor : () => number; // How much of the attribute is added for the attack's pushing down capabilities
  12. getAccuracy : () => number; // How much to add/remove from the Dex check.
  13. getCost : () => number; // The cost of performing this attack.
  14. }
  15. class ActionAttack extends Action {
  16. public static check = new Rulebook<ActionAttack>("Check Attack");
  17. public static carry = new Rulebook<ActionAttack>("Carry out Attack");
  18. public actingAgressively = true;
  19. public aggressivenessRating = 1;
  20. private targetReaction : ActionAttackReaction;
  21. private actorDexRoll : number;
  22. private targetDexRoll : number;
  23. private actorDamageRoll : number;
  24. private targetDamageRoll : number;
  25. private actorForceRoll : number;
  26. private targetForceRoll : number;
  27. public allowedStances = [PersonStance.STANDING];
  28. public static randomness = 2;
  29. public static dodgeCost = 1;
  30. public static fistCost = 1;
  31. public static gettingAttacked = new OneOf(OneOf.PURELY_AT_RANDOM,
  32. "moves on to attack you!",
  33. "attacks you!",
  34. "starts to attack you!",
  35. "decides to attack you!",
  36. "initiates an attack!",
  37. "is attacking!",
  38. "approaches with malicious intent!",
  39. "aims at you with malicious intent!",
  40. "aims at you!",
  41. "goes for you!",
  42. "comes for you!",
  43. "seems hostile!",
  44. "seems to want to fight!",
  45. "wants to fight!",
  46. "is coming right for you!",
  47. "is coming right at you!",
  48. "is not friendly at all!"
  49. );
  50. // TODO: Add infinitely more of these...
  51. public constructor (actor : Thing, ...nouns : Array<any>) {
  52. super(actor, ...nouns);
  53. this.requiresNoun = true;
  54. this.requiresVisibility = false;
  55. }
  56. public getTarget () : Thing {
  57. return this.getNoun(0);
  58. }
  59. public getWeapons () : Array<Weapon> {
  60. let weaponNoun = this.getNoun(1);
  61. if (weaponNoun == undefined) {
  62. let weapons = <Array<Weapon>>this.actor.getWorns(Weapon);
  63. if (weapons.length == 0) {
  64. // If we ever have tentaclemonsters or stuff...
  65. // TODO: Add parts for other creature types here.
  66. if (this.actor instanceof Humanoid) {
  67. return [this.actor.getPart(HumanoidHands)];
  68. }
  69. }
  70. return weapons;
  71. }
  72. return [weaponNoun];
  73. }
  74. public getCommandText () {
  75. let weaponName;
  76. let weapons = this.getWeapons();
  77. if (weapons.length == 0) {
  78. weaponName = "fists";
  79. } else {
  80. weaponName = [];
  81. weapons.forEach((weapon => {
  82. weaponName.push(weapon.getPrintedName());
  83. }));
  84. weaponName = weaponName.join(" and ");
  85. }
  86. return "attack " + this.getNoun(0).getPrintedName() + " with " + weaponName;
  87. }
  88. public generateDescription (group : ContentMolecule) {
  89. this.say.add(ContentMoleculeCombat.getSay(group));
  90. }
  91. public static checkAttackable = ActionAttack.check.createAndAddRule({
  92. name : "Attack - Is it attackable?",
  93. firstPriority : Rule.PRIORITY_HIGHEST,
  94. priority : Rule.PRIORITY_MEDIUM,
  95. code : async (runner : RulebookRunner<ActionAttack>) => {
  96. let action = runner.noun;
  97. let target = runner.noun.getTarget();
  98. if (!(target instanceof Person) && !target.breakable) {
  99. if (action.actor == WorldState.player) {
  100. action.say.add("You can't possibly damage that.");
  101. }
  102. action.stop();
  103. return false;
  104. }
  105. }
  106. });
  107. public static checkAccuracyRule = ActionAttack.check.createAndAddRule({
  108. name : "Do Dex Rolls - Choose defenses",
  109. firstPriority : ActionAttack.checkAttackable.firstPriority,
  110. priority : ActionAttack.checkAttackable.priority - 1,
  111. code : async (runner : RulebookRunner<ActionAttack>) => {
  112. let actor = <Person> runner.noun.actor;
  113. let target = runner.noun.getTarget();
  114. let action = runner.noun;
  115. action.targetReaction = ActionAttackReaction.DODGE;
  116. action.actorDexRoll = ActionAttack.getEffectiveCombatDex(actor);
  117. action.targetDexRoll = 0;
  118. if (target instanceof Person) {
  119. action.targetDexRoll = ActionAttack.getEffectiveCombatDex(actor);
  120. if (WorldState.isPlayer(target)) {
  121. // Let player choose reaction
  122. let sayWarning = new Say(Say.Mention(actor), " ", ActionAttack.gettingAttacked.getOne());
  123. let elements = await Elements.CurrentTurnHandler.getSayElementsAsContent(sayWarning);
  124. Elements.CurrentTurnHandler.print(...elements);
  125. let choices = ["Counter Attack", "Dodge", "Resist"];
  126. let choice = await Controls.giveChoices(false, ...choices);
  127. // if choice != dodge
  128. if (choice[1] != 1) {
  129. action.targetReaction = choice[1] == 0 ? ActionAttackReaction.COUNTERATTACK : ActionAttackReaction.RESIST;
  130. action.targetDexRoll = action.targetDexRoll / 2;
  131. }
  132. Elements.CurrentTurnHandler.unprint(...elements);
  133. }
  134. }
  135. }
  136. });
  137. public static checkDamageRule = ActionAttack.check.createAndAddRule({
  138. name : "Choose damages",
  139. firstPriority : ActionAttack.checkAttackable.firstPriority,
  140. priority : ActionAttack.checkAttackable.priority - 1,
  141. code : async (runner : RulebookRunner<ActionAttack>) => {
  142. let actor = <Person> runner.noun.actor;
  143. let target = runner.noun.getTarget();
  144. let action = runner.noun;
  145. let getDamage = (person : Person, ...weapons : Array<Weaponized>) => {
  146. let damage = 0;
  147. weapons.forEach(weapon => {
  148. damage += weapon.getAttributeDamageFactor() * person.getStat(weapon.getAttribute());
  149. });
  150. return damage;
  151. };
  152. let getForce = (person : Person, ...weapons : Array<Weaponized>) => {
  153. let force = 0;
  154. weapons.forEach(weapon => {
  155. force += weapon.getAttributeForceFactor() * person.getStat(weapon.getAttribute());
  156. });
  157. return force;
  158. };
  159. action.actorDamageRoll = getDamage(actor, ...action.getWeapons());
  160. action.actorForceRoll = getForce(actor, ...action.getWeapons());
  161. if (action.targetReaction == ActionAttackReaction.COUNTERATTACK) {
  162. let attack = new ActionAttack(target);
  163. action.targetDamageRoll = getDamage(<Person> target, ...attack.getWeapons()); // most likely only players do this
  164. action.targetForceRoll = getForce(<Person> target, ...attack.getWeapons()); // most likely only players do this
  165. }
  166. }
  167. });
  168. public static carryOutAttack = ActionAttack.carry.createAndAddRule({
  169. name: "Attack - Actor Attacks Person",
  170. firstPriority : Rule.PRIORITY_HIGH,
  171. priority : Rule.PRIORITY_MEDIUM,
  172. code: async (runner: RulebookRunner<ActionAttack>) => {
  173. let action = runner.noun;
  174. ActionAttack.attackOnPerson(action);
  175. if (action.targetReaction == ActionAttackReaction.COUNTERATTACK && (<Person> action.getTarget()).stance != PersonStance.KNOCKEDOUT) { // Safeguard against targets that were killed
  176. let counter = new ActionAttack(action.getTarget(), action.actor);
  177. counter.actorDamageRoll = action.targetDamageRoll;
  178. counter.actorForceRoll = action.targetForceRoll;
  179. counter.actorDexRoll = 100; // Counter Attacks always hit
  180. counter.targetDexRoll = 0;
  181. counter.targetReaction = ActionAttackReaction.DODGE;
  182. ActionAttack.attackOnPerson(counter);
  183. action.say.add(Say.PARAGRAPH_BREAK, counter.say);
  184. }
  185. }
  186. });
  187. public static attackOnPerson (action : ActionAttack) {
  188. let actor = action.actor;
  189. let target = action.getTarget();
  190. let isHit = Dice.testAgainstRoll(
  191. {name : "Attacker's Hit", value : action.actorDexRoll},
  192. {name : "Target's Dodge", value : action.targetDexRoll}
  193. ) > 0;
  194. if (!(target instanceof Person)) {
  195. ActionAttack.breakThing(action);
  196. return;
  197. }
  198. if (!isHit) {
  199. action.generateDescription(
  200. new ContentMolecule(
  201. new ContentAtomCombat(
  202. actor,
  203. target,
  204. action.getWeapons(),
  205. [ContentAtomCombat.MISS]
  206. )
  207. )
  208. );
  209. return; // End of action
  210. }
  211. let dice = new Dice("Combat Roll");
  212. let actorDamage = dice.rollAndSum(action.actorDamageRoll);
  213. // TODO: Add torso armor.
  214. let targetRes = target.getStat(Attributes.Strength) * 0.25;
  215. let previousHealth = target.getHealthOnScale();
  216. let criticalHitThreshold = 4; // Chosen arbitrarily.
  217. let isKnockedDown = false;
  218. if (target.stance == PersonStance.STANDING) {
  219. isKnockedDown = Dice.testAgainstRoll(
  220. {name : "Attacker's Force", value : action.actorForceRoll},
  221. {name : "Target's Resistance", value : target.getStat(Attributes.Strength)}
  222. ) > 0;
  223. }
  224. let finalHealth = target.getHealthOnScale();
  225. let finalDamage = Math.floor(actorDamage - targetRes) * 10; // Multiplies by 10 to make it work with Torsos Soreness
  226. action.aggressivenessRating = Math.max(finalDamage / 10, 1); // no need to multiply by 10 here.
  227. if (action.targetReaction == ActionAttackReaction.RESIST) {
  228. finalDamage = finalDamage * 2 / 3; // TODO: Figure out if two thirds is enough.
  229. }
  230. if (target instanceof Humanoid) {
  231. (<HumanoidTorso> target.getPart(HumanoidTorso)).changeSoreness(finalDamage);
  232. } else{
  233. console.warn("CANT DAMAGE");
  234. }
  235. let markers = [];
  236. if (finalDamage >= criticalHitThreshold) {
  237. markers.push(ContentAtomCombat.CRITICAL);
  238. } else {
  239. markers.push(ContentAtomCombat.HIT);
  240. }
  241. if (previousHealth > 0 && finalHealth <= 0) {
  242. markers.push(ContentAtomCombat.KNOCKED_OFF);
  243. target.stance = PersonStance.KNOCKEDOUT;
  244. } else if (finalHealth <= -10) {
  245. markers.push(ContentAtomCombat.KILLED);
  246. target.stance = PersonStance.KNOCKEDOUT;
  247. target.die();
  248. } else if (isKnockedDown) {
  249. markers.push(ContentAtomCombat.KNOCKED);
  250. target.stance = PersonStance.ALLFOURS;
  251. }
  252. action.generateDescription(
  253. new ContentMolecule(
  254. new ContentAtomCombat(
  255. actor,
  256. target,
  257. action.getWeapons(),
  258. markers
  259. )
  260. )
  261. );
  262. }
  263. public static breakThing (action : ActionAttack) {
  264. let actor = action.actor;
  265. let target = action.getTarget();
  266. let dice = new Dice("Combat Roll");
  267. let actorDamage = dice.rollAndSum(action.actorDamageRoll);
  268. if (actorDamage >= target.breakableOn) {
  269. action.generateDescription(
  270. new ContentMolecule(
  271. new ContentAtomCombat(
  272. actor,
  273. target,
  274. action.getWeapons(),
  275. [ContentAtomCombat.CRITICAL]
  276. )
  277. )
  278. );
  279. target.break();
  280. } else {
  281. action.generateDescription(
  282. new ContentMolecule(
  283. new ContentAtomCombat(
  284. actor,
  285. target,
  286. action.getWeapons(),
  287. [ContentAtomCombat.HIT]
  288. )
  289. )
  290. );
  291. }
  292. }
  293. public static getEffectiveCombatDex (person : Person) {
  294. let dex = person.getStat(Attributes.Agility);
  295. if (person.stance != PersonStance.STANDING) {
  296. return dex / 2;
  297. }
  298. return dex;
  299. }
  300. }
  301. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  302. {
  303. name : "Hyperlink - Attack",
  304. firstPriority : Rule.PRIORITY_HIGHEST,
  305. priority : Rule.PRIORITY_MEDIUM,
  306. code : (rulebook : RulebookRunner<Thing>) => {
  307. let thing = <Thing> rulebook.noun;
  308. if ((thing instanceof Person || thing.breakable) && thing.isVisibleTo(WorldState.player)) {
  309. Elements.HyperlinkHandler.addAvailableAction("Attack", new ActionAttack(WorldState.player, thing));
  310. }
  311. }
  312. }
  313. ));