Feast 2.0!
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

747 lines
20 KiB

  1. import { Creature } from "./creature"
  2. import { TextLike, DynText, ToBe, LiveText, PairLineArgs, PairLine } from './language'
  3. import { LogEntry, LogLines, FAElem, LogLine, FormatEntry, FormatOpt, PropElem, nilLog, Newline } from './interface'
  4. import { Resistances } from './entity'
  5. import { World } from './world'
  6. import { TestCategory } from './combat/tests'
  7. import { VoreContainer } from './vore'
  8. export enum DamageType {
  9. Pierce = "Pierce",
  10. Slash = "Slash",
  11. Crush = "Crush",
  12. Acid = "Acid",
  13. Seduction = "Seduction",
  14. Dominance = "Dominance",
  15. Heal = "Heal",
  16. Pure = "Pure"
  17. }
  18. export interface DamageInstance {
  19. type: DamageType;
  20. amount: number;
  21. target: Vigor | Stat;
  22. }
  23. export enum Vigor {
  24. Health = "Health",
  25. Stamina = "Stamina",
  26. Resolve = "Resolve"
  27. }
  28. export const VigorIcons: {[key in Vigor]: string} = {
  29. Health: "fas fa-heart",
  30. Stamina: "fas fa-bolt",
  31. Resolve: "fas fa-brain"
  32. }
  33. export const VigorDescs: {[key in Vigor]: string} = {
  34. Health: "How much damage you can take",
  35. Stamina: "How much energy you have",
  36. Resolve: "How much dominance you can resist"
  37. }
  38. export type Vigors = {[key in Vigor]: number}
  39. export enum Stat {
  40. Toughness = "Toughness",
  41. Power = "Power",
  42. Reflexes = "Reflexes",
  43. Agility = "Agility",
  44. Willpower = "Willpower",
  45. Charm = "Charm"
  46. }
  47. export type Stats = {[key in Stat]: number}
  48. export const StatToVigor: {[key in Stat]: Vigor} = {
  49. Toughness: Vigor.Health,
  50. Power: Vigor.Health,
  51. Reflexes: Vigor.Stamina,
  52. Agility: Vigor.Stamina,
  53. Willpower: Vigor.Resolve,
  54. Charm: Vigor.Resolve
  55. }
  56. export const StatIcons: {[key in Stat]: string} = {
  57. Toughness: 'fas fa-heartbeat',
  58. Power: 'fas fa-fist-raised',
  59. Reflexes: 'fas fa-stopwatch',
  60. Agility: 'fas fa-feather',
  61. Willpower: 'fas fa-book',
  62. Charm: 'fas fa-comments'
  63. }
  64. export const StatDescs: {[key in Stat]: string} = {
  65. Toughness: 'Your brute resistance',
  66. Power: 'Your brute power',
  67. Reflexes: 'Your ability to dodge',
  68. Agility: 'Your ability to move quickly',
  69. Willpower: 'Your mental resistance',
  70. Charm: 'Your mental power'
  71. }
  72. export enum VoreStat {
  73. Mass = "Mass",
  74. Bulk = "Bulk",
  75. Prey = "Prey"
  76. }
  77. export type VoreStats = {[key in VoreStat]: number}
  78. export const VoreStatIcons: {[key in VoreStat]: string} = {
  79. [VoreStat.Mass]: "fas fa-weight",
  80. [VoreStat.Bulk]: "fas fa-weight-hanging",
  81. [VoreStat.Prey]: "fas fa-utensils"
  82. }
  83. export const VoreStatDescs: {[key in VoreStat]: string} = {
  84. [VoreStat.Mass]: "How much you weigh",
  85. [VoreStat.Bulk]: "Your weight, plus the weight of your prey",
  86. [VoreStat.Prey]: "How many creatures you've got inside of you"
  87. }
  88. export interface CombatTest {
  89. test: (user: Creature, target: Creature) => boolean;
  90. odds: (user: Creature, target: Creature) => number;
  91. explain: (user: Creature, target: Creature) => LogEntry;
  92. fail: (user: Creature, target: Creature) => LogEntry;
  93. }
  94. /**
  95. * An instance of damage. Contains zero or more [[DamageInstance]] objects
  96. */
  97. export class Damage {
  98. readonly damages: DamageInstance[]
  99. constructor (...damages: DamageInstance[]) {
  100. this.damages = damages
  101. }
  102. scale (factor: number): Damage {
  103. const results: Array<DamageInstance> = []
  104. this.damages.forEach(damage => {
  105. results.push({
  106. type: damage.type,
  107. amount: damage.amount * factor,
  108. target: damage.target
  109. })
  110. })
  111. return new Damage(...results)
  112. }
  113. // TODO make this combine damage instances when appropriate
  114. combine (other: Damage): Damage {
  115. return new Damage(...this.damages.concat(other.damages))
  116. }
  117. toString (): string {
  118. return this.damages.map(damage => damage.amount + " " + damage.type).join("/")
  119. }
  120. render (): LogEntry {
  121. return new LogLine(...this.damages.flatMap(instance => {
  122. if (instance.target in Vigor) {
  123. return [instance.amount.toString(), new FAElem(VigorIcons[instance.target as Vigor]), " " + instance.type]
  124. } else if (instance.target in Stat) {
  125. return [instance.amount.toString(), new FAElem(StatIcons[instance.target as Stat]), " " + instance.type]
  126. } else {
  127. // this should never happen!
  128. return []
  129. }
  130. }))
  131. }
  132. // TODO is there a way to do this that will satisfy the typechecker?
  133. renderShort (): LogEntry {
  134. /* eslint-disable-next-line */
  135. const vigorTotals: Vigors = Object.keys(Vigor).reduce((total: any, key) => { total[key] = 0; return total }, {})
  136. /* eslint-disable-next-line */
  137. const statTotals: Stats = Object.keys(Stat).reduce((total: any, key) => { total[key] = 0; return total }, {})
  138. this.damages.forEach(instance => {
  139. const factor = instance.type === DamageType.Heal ? -1 : 1
  140. if (instance.target in Vigor) {
  141. vigorTotals[instance.target as Vigor] += factor * instance.amount
  142. } else if (instance.target in Stat) {
  143. statTotals[instance.target as Stat] += factor * instance.amount
  144. }
  145. })
  146. const vigorEntries = Object.keys(Vigor).flatMap(key => vigorTotals[key as Vigor] === 0 ? [] : [new PropElem(key as Vigor, vigorTotals[key as Vigor]), ' '])
  147. const statEntries = Object.keys(Stat).flatMap(key => statTotals[key as Stat] === 0 ? [] : [new PropElem(key as Stat, statTotals[key as Stat]), ' '])
  148. return new FormatEntry(new LogLine(...vigorEntries.concat(statEntries)), FormatOpt.DamageInst)
  149. }
  150. }
  151. /**
  152. * Computes damage given the source and target of the damage.
  153. */
  154. export interface DamageFormula {
  155. calc (user: Creature, target: Creature): Damage;
  156. describe (user: Creature, target: Creature): LogEntry;
  157. explain (user: Creature): LogEntry;
  158. }
  159. export class CompositeDamageFormula implements DamageFormula {
  160. constructor (private formulas: DamageFormula[]) {
  161. }
  162. calc (user: Creature, target: Creature): Damage {
  163. return this.formulas.reduce((total: Damage, next: DamageFormula) => total.combine(next.calc(user, target)), new Damage())
  164. }
  165. describe (user: Creature, target: Creature): LogEntry {
  166. return new LogLines(...this.formulas.map(formula => formula.describe(user, target)))
  167. }
  168. explain (user: Creature): LogEntry {
  169. return new LogLines(...this.formulas.map(formula => formula.explain(user)))
  170. }
  171. }
  172. /**
  173. * Simply returns the damage it was given.
  174. */
  175. export class ConstantDamageFormula implements DamageFormula {
  176. constructor (private damage: Damage) {
  177. }
  178. calc (user: Creature, target: Creature): Damage {
  179. return this.damage
  180. }
  181. describe (user: Creature, target: Creature): LogEntry {
  182. return this.explain(user)
  183. }
  184. explain (user: Creature): LogEntry {
  185. return new LogLine('Deal ', this.damage.renderShort())
  186. }
  187. }
  188. /**
  189. * Randomly scales the damage it was given with a factor of (1-x) to (1+x)
  190. */
  191. export class UniformRandomDamageFormula implements DamageFormula {
  192. constructor (private damage: Damage, private variance: number) {
  193. }
  194. calc (user: Creature, target: Creature): Damage {
  195. return this.damage.scale(Math.random() * this.variance * 2 - this.variance + 1)
  196. }
  197. describe (user: Creature, target: Creature): LogEntry {
  198. return this.explain(user)
  199. }
  200. explain (user: Creature): LogEntry {
  201. return new LogLine('Deal between ', this.damage.scale(1 - this.variance).renderShort(), ' and ', this.damage.scale(1 + this.variance).renderShort(), '.')
  202. }
  203. }
  204. export class StatDamageFormula implements DamageFormula {
  205. constructor (private factors: Array<{ stat: Stat|VoreStat; fraction: number; type: DamageType; target: Vigor|Stat }>) {
  206. }
  207. calc (user: Creature, target: Creature): Damage {
  208. const instances: Array<DamageInstance> = this.factors.map(factor => {
  209. if (factor.stat in Stat) {
  210. return {
  211. amount: factor.fraction * user.stats[factor.stat as Stat],
  212. target: factor.target,
  213. type: factor.type
  214. }
  215. } else if (factor.stat in VoreStat) {
  216. return {
  217. amount: factor.fraction * user.voreStats[factor.stat as VoreStat],
  218. target: factor.target,
  219. type: factor.type
  220. }
  221. } else {
  222. // should be impossible; .stat is Stat|VoreStat
  223. return {
  224. amount: 0,
  225. target: Vigor.Health,
  226. type: DamageType.Heal
  227. }
  228. }
  229. })
  230. return new Damage(...instances)
  231. }
  232. describe (user: Creature, target: Creature): LogEntry {
  233. return new LogLine(
  234. this.explain(user),
  235. `, for a total of `,
  236. this.calc(user, target).renderShort()
  237. )
  238. }
  239. explain (user: Creature): LogEntry {
  240. return new LogLine(
  241. `Deal `,
  242. ...this.factors.map(factor => new LogLine(
  243. `${factor.fraction * 100}% of your `,
  244. new PropElem(factor.stat),
  245. ` as `,
  246. new PropElem(factor.target)
  247. )).joinGeneral(new LogLine(`, `), new LogLine(` and `))
  248. )
  249. }
  250. }
  251. /**
  252. * Deals a percentage of the target's current vigors/stats
  253. */
  254. export class FractionDamageFormula implements DamageFormula {
  255. constructor (private factors: Array<{ fraction: number; target: Vigor|Stat; type: DamageType }>) {
  256. }
  257. calc (user: Creature, target: Creature): Damage {
  258. const instances: Array<DamageInstance> = this.factors.map(factor => {
  259. if (factor.target in Stat) {
  260. return {
  261. amount: Math.max(0, factor.fraction * target.stats[factor.target as Stat]),
  262. target: factor.target,
  263. type: factor.type
  264. }
  265. } else if (factor.target in Vigor) {
  266. return {
  267. amount: Math.max(factor.fraction * target.vigors[factor.target as Vigor]),
  268. target: factor.target,
  269. type: factor.type
  270. }
  271. } else {
  272. // should be impossible; .target is Stat|Vigor
  273. return {
  274. amount: 0,
  275. target: Vigor.Health,
  276. type: DamageType.Heal
  277. }
  278. }
  279. })
  280. return new Damage(...instances)
  281. }
  282. describe (user: Creature, target: Creature): LogEntry {
  283. return this.explain(user)
  284. }
  285. explain (user: Creature): LogEntry {
  286. return new LogLine(
  287. `Deal damage equal to `,
  288. ...this.factors.map(factor => new LogLine(
  289. `${factor.fraction * 100}% of your target's `,
  290. new PropElem(factor.target)
  291. )).joinGeneral(new LogLine(`, `), new LogLine(` and `))
  292. )
  293. }
  294. }
  295. export enum Side {
  296. Heroes,
  297. Monsters
  298. }
  299. /**
  300. * A Combatant has a list of possible actions to take, as well as a side.
  301. */
  302. export interface Combatant {
  303. actions: Array<Action>;
  304. groupActions: Array<GroupAction>;
  305. side: Side;
  306. }
  307. /**
  308. * An Action is anything that can be done by a [[Creature]] to a [[Creature]].
  309. */
  310. export abstract class Action {
  311. constructor (
  312. public name: TextLike,
  313. public desc: TextLike,
  314. public conditions: Array<Condition> = [],
  315. public tests: Array<CombatTest> = []
  316. ) {
  317. }
  318. allowed (user: Creature, target: Creature): boolean {
  319. return this.conditions.every(cond => cond.allowed(user, target))
  320. }
  321. toString (): string {
  322. return this.name.toString()
  323. }
  324. try (user: Creature, target: Creature): LogEntry {
  325. const failReason = this.tests.find(test => !test.test(user, target))
  326. if (failReason !== undefined) {
  327. return failReason.fail(user, target)
  328. } else {
  329. return this.execute(user, target)
  330. }
  331. }
  332. describe (user: Creature, target: Creature, verbose = true): LogEntry {
  333. return new LogLines(
  334. ...(verbose ? this.conditions.map(condition => condition.explain(user, target)).concat([new Newline()]) : []),
  335. new LogLine(
  336. `Success chance: ${(this.odds(user, target) * 100).toFixed(0)}%`
  337. ),
  338. new Newline(),
  339. ...this.tests.map(test => test.explain(user, target))
  340. )
  341. }
  342. odds (user: Creature, target: Creature): number {
  343. return this.tests.reduce((total, test) => total * test.odds(user, target), 1)
  344. }
  345. abstract execute (user: Creature, target: Creature): LogEntry
  346. }
  347. export class CompositionAction extends Action {
  348. public consequences: Array<Consequence>;
  349. constructor (
  350. name: TextLike,
  351. desc: TextLike,
  352. properties: {
  353. conditions?: Array<Condition>;
  354. consequences?: Array<Consequence>;
  355. tests?: Array<CombatTest>;
  356. }
  357. ) {
  358. super(name, desc, properties.conditions ?? [], properties.tests ?? [])
  359. this.consequences = properties.consequences ?? []
  360. }
  361. execute (user: Creature, target: Creature): LogEntry {
  362. return new LogLines(
  363. ...this.consequences.filter(consequence => consequence.applicable(user, target)).map(consequence => consequence.apply(user, target))
  364. )
  365. }
  366. describe (user: Creature, target: Creature): LogEntry {
  367. return new LogLines(
  368. ...this.consequences.map(consequence => consequence.describe(user, target)).concat(
  369. new Newline(),
  370. super.describe(user, target)
  371. )
  372. )
  373. }
  374. }
  375. /**
  376. * A Condition describes whether or not something is permissible between two [[Creature]]s
  377. */
  378. export interface Condition {
  379. allowed: (user: Creature, target: Creature) => boolean;
  380. explain: (user: Creature, target: Creature) => LogEntry;
  381. }
  382. export interface Actionable {
  383. actions: Array<Action>;
  384. }
  385. export abstract class GroupAction extends Action {
  386. constructor (name: TextLike, desc: TextLike, conditions: Array<Condition>) {
  387. super(name, desc, conditions)
  388. }
  389. allowedGroup (user: Creature, targets: Array<Creature>): Array<Creature> {
  390. return targets.filter(target => this.allowed(user, target))
  391. }
  392. executeGroup (user: Creature, targets: Array<Creature>): LogEntry {
  393. return new LogLines(...targets.map(target => this.execute(user, target)))
  394. }
  395. abstract describeGroup (user: Creature, targets: Array<Creature>): LogEntry
  396. }
  397. /**
  398. * Individual status effects, items, etc. should override some of these hooks.
  399. * Some hooks just produce a log entry.
  400. * Some hooks return results along with a log entry.
  401. */
  402. export class Effective {
  403. /**
  404. * Executes when the effect is initially applied
  405. */
  406. onApply (creature: Creature): LogEntry { return nilLog }
  407. /**
  408. * Executes when the effect is removed
  409. */
  410. onRemove (creature: Creature): LogEntry { return nilLog }
  411. /**
  412. * Executes before the creature tries to perform an action
  413. */
  414. preAction (creature: Creature): { prevented: boolean; log: LogEntry } {
  415. return {
  416. prevented: false,
  417. log: nilLog
  418. }
  419. }
  420. /**
  421. * Executes before another creature tries to perform an action that targets this creature
  422. */
  423. preReceiveAction (creature: Creature, attacker: Creature): { prevented: boolean; log: LogEntry } {
  424. return {
  425. prevented: false,
  426. log: nilLog
  427. }
  428. }
  429. /**
  430. * Executes before the creature receives damage (or healing)
  431. */
  432. preDamage (creature: Creature, damage: Damage): Damage {
  433. return damage
  434. }
  435. /**
  436. * Executes before the creature is attacked
  437. */
  438. preAttack (creature: Creature, attacker: Creature): { prevented: boolean; log: LogEntry } {
  439. return {
  440. prevented: false,
  441. log: nilLog
  442. }
  443. }
  444. /**
  445. * Executes when a creature's turn starts
  446. */
  447. preTurn (creature: Creature): { prevented: boolean; log: LogEntry } {
  448. return {
  449. prevented: false,
  450. log: nilLog
  451. }
  452. }
  453. /**
  454. * Modifies the effective resistance to a certain damage type
  455. */
  456. modResistance (type: DamageType, factor: number): number {
  457. return factor
  458. }
  459. /**
  460. * Called when a test is about to resolve. Decides if the creature should automatically fail.
  461. */
  462. failTest (creature: Creature, opponent: Creature): { failed: boolean; log: LogEntry } {
  463. return {
  464. failed: false,
  465. log: nilLog
  466. }
  467. }
  468. /**
  469. * Changes a creature's size. This represents the change in *mass*
  470. */
  471. scale (scale: number): number {
  472. return scale
  473. }
  474. /**
  475. * Additively modifies a creature's score for an offensive test
  476. */
  477. modTestOffense (attacker: Creature, defender: Creature, kind: TestCategory): number {
  478. return 0
  479. }
  480. /**
  481. * Additively modifies a creature's score for a defensive test
  482. */
  483. modTestDefense (defender: Creature, attacker: Creature, kind: TestCategory): number {
  484. return 0
  485. }
  486. /**
  487. * Affects digestion damage
  488. */
  489. modDigestionDamage (predator: Creature, prey: Creature, container: VoreContainer, damage: Damage): Damage {
  490. return damage
  491. }
  492. /**
  493. * Affects a stat
  494. */
  495. modStat (creature: Creature, stat: Stat, current: number): number {
  496. return current
  497. }
  498. /**
  499. * Provides actions
  500. */
  501. actions (user: Creature): Array<Action> {
  502. return []
  503. }
  504. }
  505. /**
  506. * A displayable status effect
  507. */
  508. export interface VisibleStatus {
  509. name: TextLike;
  510. desc: TextLike;
  511. icon: TextLike;
  512. topLeft: string;
  513. bottomRight: string;
  514. }
  515. /**
  516. * This kind of status is never explicitly applied to an entity -- e.g., a dead entity will show
  517. * a status indicating that it is dead, but entities cannot be "given" the dead effect
  518. */
  519. export class ImplicitStatus implements VisibleStatus {
  520. topLeft = ''
  521. bottomRight = ''
  522. constructor (public name: TextLike, public desc: TextLike, public icon: string) {
  523. }
  524. }
  525. /**
  526. * This kind of status is explicitly given to a creature.
  527. */
  528. export abstract class StatusEffect extends Effective implements VisibleStatus {
  529. constructor (public name: TextLike, public desc: TextLike, public icon: string) {
  530. super()
  531. }
  532. get topLeft () { return '' }
  533. get bottomRight () { return '' }
  534. }
  535. export type EncounterDesc = {
  536. name: TextLike;
  537. intro: (world: World) => LogEntry;
  538. }
  539. /**
  540. * An Encounter describes a fight: who is in it and whose turn it is
  541. */
  542. export class Encounter {
  543. initiatives: Map<Creature, number>
  544. currentMove: Creature
  545. turnTime = 100
  546. constructor (public desc: EncounterDesc, public combatants: Creature[]) {
  547. this.initiatives = new Map()
  548. combatants.forEach(combatant => this.initiatives.set(combatant, 0))
  549. this.currentMove = combatants[0]
  550. this.nextMove()
  551. }
  552. nextMove (totalTime = 0): LogEntry {
  553. this.initiatives.set(this.currentMove, 0)
  554. const times = new Map<Creature, number>()
  555. this.combatants.forEach(combatant => {
  556. // this should never be undefined
  557. const currentProgress = this.initiatives.get(combatant) ?? 0
  558. const remaining = (this.turnTime - currentProgress) / Math.sqrt(Math.max(combatant.stats.Agility, 1))
  559. times.set(combatant, remaining)
  560. })
  561. this.currentMove = this.combatants.reduce((closest, next) => {
  562. const closestTime = times.get(closest) ?? 0
  563. const nextTime = times.get(next) ?? 0
  564. return closestTime <= nextTime ? closest : next
  565. }, this.combatants[0])
  566. const closestRemaining = (this.turnTime - (this.initiatives.get(this.currentMove) ?? 0)) / Math.sqrt(Math.max(this.currentMove.stats.Agility, 1))
  567. this.combatants.forEach(combatant => {
  568. // still not undefined...
  569. const currentProgress = this.initiatives.get(combatant) ?? 0
  570. this.initiatives.set(combatant, currentProgress + closestRemaining * Math.sqrt(Math.max(combatant.stats.Agility, 1)))
  571. })
  572. // TODO: still let the creature use drained-vigor moves
  573. if (this.currentMove.disabled) {
  574. return this.nextMove(closestRemaining + totalTime)
  575. } else {
  576. // applies digestion every time combat advances
  577. const tickResults = this.combatants.flatMap(
  578. combatant => combatant.containers.map(
  579. container => container.tick(5 * (closestRemaining + totalTime))
  580. )
  581. )
  582. const effectResults = this.currentMove.effects.map(effect => effect.preTurn(this.currentMove)).filter(effect => effect.prevented)
  583. if (effectResults.some(result => result.prevented)) {
  584. const parts = effectResults.map(result => result.log).concat([this.nextMove()])
  585. return new LogLines(
  586. ...parts,
  587. ...tickResults
  588. )
  589. } else {
  590. return new LogLines(
  591. ...tickResults
  592. )
  593. }
  594. }
  595. return nilLog
  596. }
  597. /**
  598. * Combat is won once one side is completely disabled
  599. */
  600. get winner (): null|Side {
  601. const remaining: Set<Side> = new Set(this.combatants.filter(combatant => !combatant.disabled).map(combatant => combatant.side))
  602. if (remaining.size === 1) {
  603. return Array.from(remaining)[0]
  604. } else {
  605. return null
  606. }
  607. }
  608. /**
  609. * Combat is completely won once one side is completely destroyed
  610. */
  611. get totalWinner (): null|Side {
  612. const remaining: Set<Side> = new Set(this.combatants.filter(combatant => !combatant.destroyed).map(combatant => combatant.side))
  613. if (remaining.size === 1) {
  614. return Array.from(remaining)[0]
  615. } else {
  616. return null
  617. }
  618. }
  619. }
  620. export abstract class Consequence {
  621. constructor (public conditions: Condition[]) {
  622. }
  623. applicable (user: Creature, target: Creature): boolean {
  624. return this.conditions.every(cond => cond.allowed(user, target))
  625. }
  626. abstract describe (user: Creature, target: Creature): LogEntry
  627. abstract apply (user: Creature, target: Creature): LogEntry
  628. }