Feast 2.0!
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

75 строки
1.8 KiB

  1. import { Creature } from './creature'
  2. import { Encounter, Action } from './combat'
  3. import { LogEntry } from './interface'
  4. import { PassAction } from './combat/actions'
  5. import { NoPassDecider, NoReleaseDecider, ChanceDecider, NoSurrenderDecider } from './ai/deciders'
  6. /**
  7. * A Decider determines how favorable an action is to perform.
  8. */
  9. export interface Decider {
  10. decide: (encounter: Encounter, user: Creature, target: Creature, action: Action) => number;
  11. }
  12. export class AI {
  13. constructor (private deciders: Decider[]) {
  14. }
  15. decide (actor: Creature, encounter: Encounter): LogEntry {
  16. const options = encounter.combatants.flatMap(enemy => actor.validActions(enemy).map(action => ({
  17. target: enemy,
  18. action: action,
  19. weight: 1
  20. })))
  21. this.deciders.forEach(
  22. decider => options.forEach(
  23. option => { option.weight *= decider.decide(encounter, actor, option.target, option.action) }
  24. )
  25. )
  26. let total = options.reduce(
  27. (sum: number, option) => sum + option.weight,
  28. 0
  29. )
  30. total *= Math.random()
  31. const chosen = options.find(
  32. option => {
  33. if (total < option.weight) {
  34. return true
  35. } else {
  36. total -= option.weight
  37. return false
  38. }
  39. }
  40. )
  41. if (chosen !== undefined) {
  42. return chosen.action.try(actor, chosen.target)
  43. }
  44. // if we filtered out EVERY action, we should just give up and pass
  45. return new PassAction().try(actor, actor)
  46. }
  47. }
  48. /**
  49. * The VoreAI tries to eat opponents, but only if the odds are good enough
  50. */
  51. export class VoreAI extends AI {
  52. constructor () {
  53. super(
  54. [
  55. new NoReleaseDecider(),
  56. new NoSurrenderDecider(),
  57. new NoPassDecider(),
  58. new ChanceDecider()
  59. ]
  60. )
  61. }
  62. }