Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/client/ClientGameRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ export class ClientGameRunner {
this.eventBus.emit(
new SendAttackIntentEvent(
this.gameView.owner(tile).id(),
this.myPlayer.troops() * this.renderer.uiState.attackRatio,
this.renderer.uiState.attackRatio,
),
);
} else if (this.canAutoBoat(actions, tile)) {
Expand Down Expand Up @@ -607,7 +607,7 @@ export class ClientGameRunner {
this.eventBus.emit(
new SendAttackIntentEvent(
this.gameView.owner(tile).id(),
this.myPlayer.troops() * this.renderer.uiState.attackRatio,
this.renderer.uiState.attackRatio,
),
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/client/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ export class SendSpawnIntentEvent implements GameEvent {
export class SendAttackIntentEvent implements GameEvent {
constructor(
public readonly targetID: PlayerID | null,
public readonly troops: number,
public readonly attackRatio: number,
) {}
}

export class SendBoatAttackIntentEvent implements GameEvent {
constructor(
public readonly targetID: PlayerID | null,
public readonly dst: TileRef,
public readonly troops: number,
public readonly attackRatio: number,
public readonly src: TileRef | null = null,
) {}
}
Expand Down Expand Up @@ -456,7 +456,7 @@ export class Transport {
type: "attack",
clientID: this.lobbyConfig.clientID,
targetID: event.targetID,
troops: event.troops,
attackRatio: event.attackRatio,
});
}

Expand All @@ -465,7 +465,7 @@ export class Transport {
type: "boat",
clientID: this.lobbyConfig.clientID,
targetID: event.targetID,
troops: event.troops,
attackRatio: event.attackRatio,
dst: event.dst,
src: event.src,
});
Expand Down
7 changes: 2 additions & 5 deletions src/client/graphics/layers/PlayerActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ export class PlayerActionHandler {

handleAttack(player: PlayerView, targetId: string | null) {
this.eventBus.emit(
new SendAttackIntentEvent(
targetId,
this.uiState.attackRatio * player.troops(),
),
new SendAttackIntentEvent(targetId, this.uiState.attackRatio),
);
}

Expand All @@ -49,7 +46,7 @@ export class PlayerActionHandler {
new SendBoatAttackIntentEvent(
targetId,
targetTile,
this.uiState.attackRatio * player.troops(),
this.uiState.attackRatio,
spawnTile,
),
);
Expand Down
4 changes: 2 additions & 2 deletions src/core/Schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const AllianceExtensionIntentSchema = BaseIntentSchema.extend({
export const AttackIntentSchema = BaseIntentSchema.extend({
type: z.literal("attack"),
targetID: ID.nullable(),
troops: z.number().nonnegative().nullable(),
attackRatio: z.number().nonnegative().nullable(),
});

export const SpawnIntentSchema = BaseIntentSchema.extend({
Expand All @@ -243,7 +243,7 @@ export const SpawnIntentSchema = BaseIntentSchema.extend({
export const BoatAttackIntentSchema = BaseIntentSchema.extend({
type: z.literal("boat"),
targetID: ID.nullable(),
troops: z.number().nonnegative(),
attackRatio: z.number().nonnegative(),
dst: z.number(),
src: z.number().nullable(),
});
Expand Down
20 changes: 16 additions & 4 deletions src/core/execution/AttackExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ export class AttackExecution implements Execution {

private random = new PseudoRandom(123);

private startTroops: number | null = null;

private target: Player | TerraNullius;

private mg: Game;

private attack: Attack | null = null;

constructor(
private startTroops: number | null = null,
private attackRatio: number | null = null,
private _owner: Player,
private _targetID: PlayerID | null,
private sourceTile: TileRef | null = null,
private removeTroops: boolean = true,
private extraTroops?: number,
) {}

public targetID(): PlayerID | null {
Expand Down Expand Up @@ -102,13 +105,22 @@ export class AttackExecution implements Execution {
}
}

this.startTroops ??= this.mg
.config()
.attackAmount(this._owner, this.target);
const currentTroops = this._owner.troops();
if (this.attackRatio) {
this.startTroops = this.attackRatio * currentTroops;
} else if (this.extraTroops) {
this.startTroops = this.extraTroops;
} else {
this.startTroops = this.mg
.config()
.attackAmount(this._owner, this.target);
}

if (this.removeTroops) {
this.startTroops = Math.min(this._owner.troops(), this.startTroops);
this._owner.removeTroops(this.startTroops);
}

this.attack = this._owner.createAttack(
this.target,
this.startTroops,
Expand Down
4 changes: 2 additions & 2 deletions src/core/execution/ExecutionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Executor {
switch (intent.type) {
case "attack": {
return new AttackExecution(
intent.troops,
intent.attackRatio,
player,
intent.targetID,
null,
Expand All @@ -75,7 +75,7 @@ export class Executor {
player,
intent.targetID,
intent.dst,
intent.troops,
intent.attackRatio,
intent.src,
);
case "allianceRequest":
Expand Down
10 changes: 2 additions & 8 deletions src/core/execution/FakeHumanExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,7 @@ export class FakeHumanExecution implements Execution {
return;
}
this.mg.addExecution(
new TransportShipExecution(
this.player,
other.id(),
closest.y,
this.player.troops() / 5,
null,
),
new TransportShipExecution(this.player, other.id(), closest.y, 0.2, null),
);
}

Expand Down Expand Up @@ -623,7 +617,7 @@ export class FakeHumanExecution implements Execution {
this.player,
this.mg.owner(dst).id(),
dst,
this.player.troops() / 5,
0.2,
null,
),
);
Expand Down
16 changes: 11 additions & 5 deletions src/core/execution/TransportShipExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class TransportShipExecution implements Execution {
// TODO make private
public path: TileRef[];
private dst: TileRef | null;
private startTroops: number | null = null;

private boat: Unit;

Expand All @@ -41,7 +42,7 @@ export class TransportShipExecution implements Execution {
private attacker: Player,
private targetID: PlayerID | null,
private ref: TileRef,
private startTroops: number,
private attackRatio: number,
private src: TileRef | null,
) {
this.originalOwner = this.attacker;
Expand Down Expand Up @@ -94,9 +95,13 @@ export class TransportShipExecution implements Execution {
this.target = mg.player(this.targetID);
}

this.startTroops ??= this.mg
.config()
.boatAttackAmount(this.attacker, this.target);
if (this.attackRatio) {
this.startTroops = this.attacker.troops() * this.attackRatio;
} else {
this.startTroops = this.mg
.config()
.boatAttackAmount(this.attacker, this.target);
}

this.startTroops = Math.min(this.startTroops, this.attacker.troops());

Expand Down Expand Up @@ -249,11 +254,12 @@ export class TransportShipExecution implements Execution {
} else {
this.mg.addExecution(
new AttackExecution(
this.boat.troops(),
null, // Don't use ratio for creating attack on landing
this.attacker,
this.targetID,
this.dst,
false,
this.boat.troops(),
),
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/execution/utils/BotBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export class BotBehavior {
forceSendAttack(target: Player | TerraNullius) {
this.game.addExecution(
new AttackExecution(
this.player.troops() / 2,
0.5,
this.player,
target.isPlayer() ? target.id() : this.game.terraNullius().id(),
),
Expand All @@ -396,9 +396,10 @@ export class BotBehavior {
const targetTroops = maxTroops * reserveRatio;
const troops = this.player.troops() - targetTroops;
if (troops < 1) return;
const ratio = troops / this.player.troops();
this.game.addExecution(
new AttackExecution(
troops,
ratio,
this.player,
target.isPlayer() ? target.id() : this.game.terraNullius().id(),
),
Expand Down
Loading