Skip to content

Commit

Permalink
Handle Truant ability
Browse files Browse the repository at this point in the history
Closes #13.
  • Loading branch information
taylorhansen committed Feb 19, 2019
1 parent a9738c8 commit 4058df7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
23 changes: 18 additions & 5 deletions src/bot/battle/EventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,28 @@ export class EventProcessor
})
.on("cant", event =>
{
const active = this.getActive(event.id.owner);
if (event.reason === "recharge")
{
// successfully completed its recharge turn
this.getActive(event.id.owner).volatile.mustRecharge =
false;
active.volatile.mustRecharge = false;
}
else if (event.reason.startsWith("ability: "))
{
// can't move due to an ability
const ability = event.reason.substr("ability: ".length);
active.ability = ability;

// truant turn and recharge turn overlap
if (ability === "Truant") active.volatile.mustRecharge = false;
}

if (event.moveName)
{
const moveId =
EventProcessor.parseIDName(event.moveName);
const moveId = EventProcessor.parseIDName(event.moveName);
// prevented from using a move, which might not have
// been revealed before
this.getActive(event.id.owner).revealMove(moveId);
active.revealMove(moveId);
}
})
.on("curestatus", event =>
Expand Down Expand Up @@ -300,6 +309,10 @@ export class EventProcessor
*/
public handleEvents(events: AnyBattleEvent[]): void
{
// this field should only stay true if one of these events contains a
// |turn| message
this._newTurn = false;

for (let i = 0; i < events.length; ++i)
{
const event = events[i];
Expand Down
32 changes: 23 additions & 9 deletions src/bot/battle/state/VolatileStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ export class VolatileStatus
/** Name of override ability. */
public overrideAbilityName: string;

/** Whether the Truant ability will activate next turn. */
public get truant(): boolean
{
return this._truant;
}
private _truant: boolean;

/** Creates a VolatileStatus object. */
constructor()
{
Expand All @@ -88,11 +95,12 @@ export class VolatileStatus
this._stallTurns = 0;
this.overrideAbility = 0;
this.overrideAbilityName = "";
this._truant = false;
}

/**
* Increments temporary status turns. Should be called once per turn by the
* parent Pokemon object.
* Updates temporary status counters. Must be called at the end of the turn,
* after a Choice has been sent to the server.
*/
public updateStatusTurns(): void
{
Expand All @@ -101,10 +109,7 @@ export class VolatileStatus
// update disabled move turns
for (let i = 0; i < this.disableTurns.length; ++i)
{
if (this.disableTurns[i])
{
++this.disableTurns[i];
}
if (this.disableTurns[i]) ++this.disableTurns[i];
}

// if twoTurn was set this turn, the two-turn move must be completed or
Expand All @@ -115,6 +120,12 @@ export class VolatileStatus
// counter will reset
if (!this.stalled) this._stallTurns = 0;
this.stalled = false;

if (this.overrideAbilityName === "truant")
{
this._truant = !this._truant;
}
else this._truant = false;
}

/**
Expand Down Expand Up @@ -195,7 +206,8 @@ export class VolatileStatus
return /*boostable stats*/Object.keys(boostableStatNames).length +
/*confuse*/1 + /*disable*/4 + /*locked move*/1 +
/*two-turn status*/numTwoTurnMoves + /*must recharge*/1 +
/*stall fail rate*/1 + /*override ability*/dex.numAbilities;
/*stall fail rate*/1 + /*override ability*/dex.numAbilities +
/*truant*/1;
}

// istanbul ignore next: unstable, hard to test
Expand All @@ -220,7 +232,8 @@ export class VolatileStatus
...Object.keys(this._boosts).map(
(key: BoostableStatName) => this._boosts[key]),
confused, ...disabled, this.lockedMove ? 1 : 0, ...twoTurn,
this.mustRecharge ? 1 : 0, stallFailRate, ...overrideAbility
this.mustRecharge ? 1 : 0, stallFailRate, ...overrideAbility,
this._truant ? 1 : 0
];
return a;
}
Expand Down Expand Up @@ -249,7 +262,8 @@ ${VolatileStatus.pluralTurns(d)}`),
this.mustRecharge ? ["must recharge"] : [],
this._stallTurns ?
[`stalling for ${this._stallTurns - 1} \
${VolatileStatus.pluralTurns(this._stallTurns)}`] : [])
${VolatileStatus.pluralTurns(this._stallTurns)}`] : [],
this._truant ? ["truant next turn"] : [])
.join(", ")}]`;
}

Expand Down
40 changes: 40 additions & 0 deletions test/bot/battle/BattleAndEventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,46 @@ describe("Battle and EventProcessor", function()

describe("cant", function()
{
it("Should reveal ability", async function()
{
const mon = battle.state.teams.them.active;
mon.ability = "swiftswim";
await listener.dispatch("battleprogress",
{
events: [{type: "cant", id: them1, reason: "ability: Damp"}]
});
expect(mon.ability).to.equal("damp");
expect(mon.baseAbility).to.equal("swiftswim");
});

it("Should properly handle truant ability", async function()
{
// tslint:disable:no-unused-expression
const mon = battle.state.teams.us.active;
mon.ability = "truant";
mon.volatile.mustRecharge = true;
expect(mon.volatile.truant).to.be.false;

await listener.dispatch("battleprogress",
{events: [{type: "turn", num: 3}]});
expect(mon.volatile.truant).to.be.true;

await listener.dispatch("battleprogress",
{
events:
[
{type: "cant", id: us1, reason: "ability: Truant"}
]
});
expect(mon.volatile.mustRecharge).to.be.false;
expect(mon.volatile.truant).to.be.true;

await listener.dispatch("battleprogress",
{events: [{type: "turn", num: 4}]});
expect(mon.volatile.truant).to.be.false;
// tslint:enable:no-unused-expression
});

it("Should reveal failed move", async function()
{
const mon = battle.state.teams.them.active;
Expand Down
5 changes: 5 additions & 0 deletions test/bot/battle/state/VolatileStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ describe("VolatileStatus", function()
volatile.twoTurn = "Bounce";
volatile.mustRecharge = true;
volatile.stall(true);
volatile.overrideAbility = 1;
volatile.overrideAbilityName = "something"; // not actually valid

volatile.clear();
// tslint:disable:no-unused-expression
Expand All @@ -36,6 +38,9 @@ describe("VolatileStatus", function()
expect(volatile.twoTurn).to.equal("");
expect(volatile.mustRecharge).to.be.false;
expect(volatile.stallTurns).to.equal(0);
expect(volatile.overrideAbility).to.equal(0);
expect(volatile.overrideAbilityName).to.equal("");
expect(volatile.truant).to.be.false;
// tslint:enable:no-unused-expression
});
});
Expand Down

0 comments on commit 4058df7

Please sign in to comment.