import { CombatTest, Stat, Vigor, Stats, StatToVigor } from '../combat' import { Creature } from "../creature" import { LogEntry, LogLines, PropElem, LogLine, nilLog } from '../interface' import { Verb } from '../language' function logistic (x0: number, L: number, k: number): (x: number) => number { return (x: number) => { return L / (1 + Math.exp(-k * (x - x0))) } } // TODO this will need to be able to return a LogEntry at some point abstract class RandomTest implements CombatTest { constructor (public fail: (user: Creature, target: Creature) => LogEntry) { } test (user: Creature, target: Creature): boolean { const userFail = user.effects.map(effect => effect.failTest(user, target)) if (userFail.some(result => result.failed)) { return false } const targetFail = target.effects.map(effect => effect.failTest(target, user)) if (targetFail.some(result => result.failed)) { return true } return Math.random() < this.odds(user, target) } abstract odds(user: Creature, target: Creature): number abstract explain(user: Creature, target: Creature): LogEntry } export enum TestCategory { Attack = "Attack", Vore = "Vore" } export class OpposedStatTest extends RandomTest { private f: (x: number) => number private k = 0.1 // how much a stat can be reduced by its corresponding vigor being low private maxStatVigorPenalty = 0.5 // how much the total score can be reduced by each vigor being low private maxTotalVigorPenalty = 0.1 constructor ( public readonly userStats: Partial, public readonly targetStats: Partial, fail: (user: Creature, target: Creature) => LogEntry, public category: TestCategory, private bias = 0 ) { super(fail) this.f = logistic(0, 1, this.k) } odds (user: Creature, target: Creature): number { const userScore = this.getScore(user, this.userStats) const targetScore = this.getScore(target, this.targetStats) return this.f(userScore - targetScore + this.bias) } explain (user: Creature, target: Creature): LogEntry { return new LogLines( new LogLine( `Pits `, ...Object.entries(this.userStats).map(([stat, frac]) => { if (frac !== undefined) { return new LogLine(`${(frac * 100).toFixed(0)}% `, new PropElem(stat as Stat)) } else { return nilLog } }), ` from ${user.name.possessive} stats against `, ...Object.entries(this.targetStats).map(([stat, frac]) => { if (frac !== undefined) { return new LogLine(`${(frac * 100).toFixed(0)}% `, new PropElem(stat as Stat)) } else { return nilLog } }), ` from ${target.name.possessive} stats.` ), new LogLine( `${user.name.capital}: ${this.getScore(user, this.userStats)} // ${this.getScore(target, this.targetStats)} :${target.name.capital}` ), new LogLine( `${user.name.capital} ${user.name.conjugate(new Verb("have", "has"))} a ${(this.odds(user, target) * 100).toFixed(0)}% chance of winning this test.` ) ) } private getScore (actor: Creature, parts: Partial): number { const total = Object.entries(parts).reduce((total: number, [stat, frac]) => { let value = actor.stats[stat as Stat] * (frac === undefined ? 0 : frac) const vigor = StatToVigor[stat as Stat] value = value * (1 - this.maxStatVigorPenalty) + value * this.maxStatVigorPenalty * actor.vigors[vigor] / actor.maxVigors[vigor] return total + value }, 0) const modifiedTotal = Object.keys(Vigor).reduce( (total, vigor) => { return total * (1 - this.maxStatVigorPenalty) + total * actor.vigors[vigor as Vigor] / actor.maxVigors[vigor as Vigor] }, total ) return modifiedTotal } } export class StatVigorSizeTest extends RandomTest { private f: (x: number) => number private k = 0.1 constructor (public readonly stat: Stat, private bias = 0, fail: (user: Creature, target: Creature) => LogEntry) { super(fail) this.f = logistic(0, 1, this.k) } odds (user: Creature, target: Creature): number { let userPercent = 1 let targetPercent = 1 Object.keys(Vigor).forEach(key => { userPercent *= user.vigors[key as Vigor] / Math.max(1, user.maxVigors[key as Vigor]) targetPercent *= target.vigors[key as Vigor] / Math.max(1, target.maxVigors[key as Vigor]) userPercent = Math.max(0, userPercent) targetPercent = Math.max(0, targetPercent) }) if (userPercent === 0) { targetPercent *= 4 } if (targetPercent === 0) { userPercent *= 4 } const sizeOffset = Math.log2(user.voreStats.Mass / target.voreStats.Mass) return this.f(this.bias + sizeOffset * 5 + user.stats[this.stat] * userPercent - target.stats[this.stat] * targetPercent) } explain (user: Creature, target: Creature): LogEntry { let result: LogEntry let userPercent = 1 let targetPercent = 1 Object.keys(Vigor).forEach(key => { userPercent *= user.vigors[key as Vigor] / user.maxVigors[key as Vigor] targetPercent *= target.vigors[key as Vigor] / target.maxVigors[key as Vigor] userPercent = Math.max(0, userPercent) targetPercent = Math.max(0, targetPercent) }) if (userPercent === 0) { targetPercent *= 4 } if (targetPercent === 0) { userPercent *= 4 } const sizeOffset = Math.log2(user.voreStats.Mass / target.voreStats.Mass) const userMod = user.stats[this.stat] * userPercent const targetMod = target.stats[this.stat] * targetPercent const delta = userMod - targetMod + sizeOffset * 5 if (delta === 0) { result = new LogLine('You and the target have the same effective', new PropElem(this.stat), '.') } else if (delta < 0) { result = new LogLine('You effectively have ', new PropElem(this.stat, -delta), ' less than your foe.') } else { result = new LogLine('You effectively have ', new PropElem(this.stat, delta), ' more than you foe.') } result = new LogLine(result, 'Your odds of success are ' + (100 * this.odds(user, target)).toFixed(1) + '%') return result } } export class StatVigorTest extends RandomTest { private f: (x: number) => number private k = 0.1 constructor (public readonly stat: Stat, private bias = 0, fail: (user: Creature, target: Creature) => LogEntry) { super(fail) this.f = logistic(0, 1, this.k) } odds (user: Creature, target: Creature): number { let userPercent = 1 let targetPercent = 1 Object.keys(Vigor).forEach(key => { userPercent *= user.vigors[key as Vigor] / Math.max(1, user.maxVigors[key as Vigor]) targetPercent *= target.vigors[key as Vigor] / Math.max(1, target.maxVigors[key as Vigor]) userPercent = Math.max(0, userPercent) targetPercent = Math.max(0, targetPercent) }) if (userPercent === 0) { targetPercent *= 4 } if (targetPercent === 0) { userPercent *= 4 } return this.f(this.bias + user.stats[this.stat] * userPercent - target.stats[this.stat] * targetPercent) } explain (user: Creature, target: Creature): LogEntry { let result: LogEntry let userPercent = 1 let targetPercent = 1 Object.keys(Vigor).forEach(key => { userPercent *= user.vigors[key as Vigor] / user.maxVigors[key as Vigor] targetPercent *= target.vigors[key as Vigor] / target.maxVigors[key as Vigor] userPercent = Math.max(0, userPercent) targetPercent = Math.max(0, targetPercent) }) if (userPercent === 0) { targetPercent *= 4 } if (targetPercent === 0) { userPercent *= 4 } const userMod = user.stats[this.stat] * userPercent const targetMod = target.stats[this.stat] * targetPercent const delta = userMod - targetMod if (delta === 0) { result = new LogLine('You and the target have the same effective', new PropElem(this.stat), '.') } else if (delta < 0) { result = new LogLine('You effectively have ', new PropElem(this.stat, -delta), ' less than your foe.') } else { result = new LogLine('You effectively have ', new PropElem(this.stat, delta), ' more than you foe.') } result = new LogLine(result, 'Your odds of success are ' + (100 * this.odds(user, target)).toFixed(1) + '%') return result } } export class StatTest extends RandomTest { private f: (x: number) => number private k = 0.1 constructor (public readonly stat: Stat, private bias = 0, fail: (user: Creature, target: Creature) => LogEntry) { super(fail) this.f = logistic(0, 1, this.k) } odds (user: Creature, target: Creature): number { return this.f(this.bias + user.stats[this.stat] - target.stats[this.stat]) } explain (user: Creature, target: Creature): LogEntry { const delta: number = user.stats[this.stat] - target.stats[this.stat] let result: LogEntry if (delta === 0) { result = new LogLine('You and the target have the same ', new PropElem(this.stat), '.') } else if (delta < 0) { result = new LogLine('You have ', new PropElem(this.stat, -delta), ' less than your foe.') } else { result = new LogLine('You have ', new PropElem(this.stat, delta), ' more than you foe.') } result = new LogLine(result, 'Your odds of success are ' + (100 * this.odds(user, target)).toFixed(1) + '%') return result } } export class ChanceTest extends RandomTest { constructor (public readonly chance: number, fail: (user: Creature, target: Creature) => LogEntry) { super(fail) } odds (user: Creature, target: Creature): number { return this.chance } explain (user: Creature, target: Creature): LogEntry { return new LogLine('You have a flat ' + (100 * this.chance) + '% chance.') } }