Skip to content

Commit

Permalink
bugfix: fix creature delay reset and delays across rounds
Browse files Browse the repository at this point in the history
* Split creature.delay() into creature.hinder() and creature.delay()
* creature.hinder() is called when an attacker delays the creature
* creature.wait() is called when the player delays their own creature
* simplified creature queue

See FreezingMoon#2158, FreezingMoon#2275
  • Loading branch information
andretchen0 committed Jul 16, 2023
1 parent 09ebc19 commit 83e0635
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 200 deletions.
9 changes: 1 addition & 8 deletions src/abilities/Dark-Priest.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,6 @@ export default (G) => {
const creatureHasMaterializationSickness =
dpriest.player.summonCreaturesWithMaterializationSickness;

// Removes temporary Creature from queue when Player chooses a
// different Creature to materialize
G.queue.removeTempCreature();

// Create full temporary Creature with placeholder position to show in queue
crea = $j.extend(
crea,
Expand All @@ -284,11 +280,8 @@ export default (G) => {
// Make temporary Creature invisible
fullCrea.sprite.alpha = 0;

// Provide full Creature to Queue
G.queue.tempCreature = fullCrea;

// Show temporary Creature in queue
G.queue.addByInitiative(fullCrea, !creatureHasMaterializationSickness);
G.queue.update();
G.updateQueueDisplay();

G.grid.forEachHex(function (hex) {
Expand Down
2 changes: 1 addition & 1 deletion src/abilities/Knightmare.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export default (G) => {
return;
}

target.delay();
target.hinder();
},
},

Expand Down
5 changes: 2 additions & 3 deletions src/abilities/Stomper.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ export default (G) => {
target.status.dizzy = true;
target.removeEffect('Earth Shaker');
} else {
target.delay(false);
target.hinder();
target.addEffect(
new Effect(
'Earth Shaker', // Name
Expand All @@ -524,8 +524,7 @@ export default (G) => {
{
// disable the ability to delay this unit as it has already been delayed
effectFn: () => {
target.delayed = true;
target.delayable = false;
target.hinder();
},
deleteTrigger: 'onEndPhase',
turnLifetime: 1,
Expand Down
1 change: 0 additions & 1 deletion src/ability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ export class Ability {
}
game.signals.creature.dispatch('abilityend', { creature: this.creature });
game.UI.btnDelay.changeState('disabled');
game.activeCreature.delayable = false;
game.UI.selectAbility(-1);

if (this.getTrigger() === 'onQuery' && !deferredEnding) {
Expand Down
127 changes: 80 additions & 47 deletions src/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ export class Creature {
dropCollection: Drop[];
protectedFromFatigue: boolean;
turnsActive: number;
delayable: boolean;
delayed: boolean;
private _nextGameTurnActive: number;
private _waitedTurn: number;
private _hinderedTurn: number;
materializationSickness: boolean;
noActionPossible: boolean;
destroy: any;
Expand Down Expand Up @@ -399,15 +400,17 @@ export class Creature {
}
// Adding Himself to creature arrays and queue
game.creatures[this.id] = this;

this.delayable = true;
this.delayed = false;
if (typeof obj.materializationSickness !== 'undefined') {
this.materializationSickness = obj.materializationSickness;
} else {
this.materializationSickness = this.isDarkPriest() ? false : true;
}
this.noActionPossible = false;

this._nextGameTurnActive =
!this.materializationSickness || this.isDarkPriest() ? this.game.turn : this.game.turn + 1;
this._waitedTurn = -1;
this._hinderedTurn = -1;
}

/**
Expand All @@ -420,15 +423,11 @@ export class Creature {
priest who must always be in the next queue to properly start the game. */
const alsoAddToCurrentQueue = disableMaterializationSickness && !this.isDarkPriest();

game.queue.addByInitiative(this, alsoAddToCurrentQueue);

if (disableMaterializationSickness) {
this.materializationSickness = false;
}

// Remove temporary Creature to prevent duplicates when the actual
// materialized Creature with correct position is added to the queue
game.queue.removeTempCreature();
game.queue.update();
game.updateQueueDisplay();

game.grid.orderCreatureZ();
Expand Down Expand Up @@ -564,61 +563,99 @@ export class Creature {
}, 1000);
}

/* deactivate(wait)
*
* wait : Boolean : Deactivate while waiting or not
*
* Preview the creature position at the given coordinates
/**
* Deactivate the creature. Called when the creature is active, then is no longer active.
*
* @param {'wait' | 'turn-end'} reason: Why is the creature deactivated?
*/
deactivate(wait: boolean) {
deactivate(reason: 'wait' | 'turn-end') {
const game = this.game;
this.delayed = wait;
this.hasWait = this.delayed;
this.hasWait = this.isDelayed;
this.status.frozen = false;
this.status.cryostasis = false;
this.status.dizzy = false;

// Effects triggers
if (!wait) {
if (reason === 'turn-end') {
this.turnsActive += 1;
this._nextGameTurnActive = game.turn + 1;
// @ts-expect-error 2554
game.onEndPhase(this);
}
}

this.delayable = false;
get isInCurrentQueue() {
return !this.dead && !this.temp && this._nextGameTurnActive <= this.game.turn;
}

/* wait()
*
* Move the creature to the end of the queue
*
get isInNextQueue() {
return !this.dead;
}

get isDelayedInNextQueue(): null | boolean {
if (!this.isInNextQueue) return null;
return !this.isInCurrentQueue && this.isDelayed;
}

/**
* @deprecated Use isDelayed
*/
wait() {
let abilityAvailable = false;
get delayed() {
return this.isDelayed;
}

if (this.delayed) {
return;
}
get isDelayed() {
return this.isWaiting || this.isHindered;
}

// If at least one ability has not been used
this.abilities.forEach((ability) => {
abilityAvailable = abilityAvailable || !ability.used;
});
get isWaiting() {
return this._waitedTurn >= this.turnsActive;
}

get isHindered() {
return this._hinderedTurn >= this.turnsActive;
}

/**
* @deprecated Use canWait
*/
get delayable() {
return this.canWait;
}

/**
* Is waiting possible?
*/
get canWait() {
const hasUnusedAbilities = this.abilities.some((a) => !a.used);
return !this.isDelayed && this.remainingMove > 0 && hasUnusedAbilities;
}

if (this.remainingMove > 0 && abilityAvailable) {
this.delay();
this.deactivate(true);
/**
* The creature waits. It will have its turn at the end of the round.
* The player has decided to delay the creature until the end of the turn.
*/
wait(): void {
if (this.canWait) {
const game = this.game;

this._waitedTurn = this.turnsActive;
this.hint('Delayed', 'msg_effects');
game.queue.update();
game.updateQueueDisplay();
this.deactivate('wait');
}
}

delay() {
/**
* A creature's turn is delayed as part of an attack from another creature.
*/
hinder(): void {
const game = this.game;

game.queue.delay(this);
this.delayable = false;
this.delayed = true;
this._hinderedTurn = this.turnsActive;
this.hint('Delayed', 'msg_effects');
game.queue.update();
game.updateQueueDisplay();
}

Expand Down Expand Up @@ -686,7 +723,6 @@ export class Creature {
},
});
}
args.creature.delayable = false;
game.UI.btnDelay.changeState('disabled');
args.creature.moveTo(hex, {
animation: args.creature.movementType() === 'flying' ? 'fly' : 'walk',
Expand All @@ -702,11 +738,7 @@ export class Creature {
if (!o.isAbility) {
if (game.UI.selectedAbility != -1) {
this.hint('Canceled', 'gamehintblack');

// If this Creature is Dark Priest, remove temporary Creature in queue
if (this.isDarkPriest()) {
game.queue.removeTempCreature();
}
game.queue.update();
}

$j('#abilities .ability').removeClass('active');
Expand Down Expand Up @@ -1632,6 +1664,7 @@ export class Creature {
}

hint(text: string, cssClass: string) {
if (typeof Phaser === 'undefined') return;
const game = this.game,
tooltipSpeed = 250,
tooltipDisplaySpeed = 500,
Expand Down Expand Up @@ -1941,7 +1974,7 @@ export class Creature {

this.cleanHex();

game.queue.remove(this);
game.queue.update();
game.updateQueueDisplay();
game.grid.updateDisplay();

Expand Down
Loading

0 comments on commit 83e0635

Please sign in to comment.