Add button for remove building (#1609)

## Description:

Added a red delete button with trash can icon to the right-click radial
menu that allows players to voluntarily delete their own units.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I have read and accepted the CLA agreement (only required once).

## Please put your Discord username so you can be contacted if a bug or
regression is found:

Kipstz

<img width="286" height="209" alt="image"
src="https://github.com/user-attachments/assets/85142be3-2aa5-4c84-ab30-0c68289c8f85"
/>

---------

Co-authored-by: Drills Kibo <59177241+drillskibo@users.noreply.github.com>
This commit is contained in:
Kipstz
2025-08-05 07:22:07 +02:00
committed by evanpelle
parent 209de56ae6
commit 4b129a2f7f
15 changed files with 359 additions and 7 deletions
+8
View File
@@ -40,6 +40,7 @@ export type Intent =
| MoveWarshipIntent
| MarkDisconnectedIntent
| UpgradeStructureIntent
| DeleteUnitIntent
| KickPlayerIntent;
export type AttackIntent = z.infer<typeof AttackIntentSchema>;
@@ -69,6 +70,7 @@ export type MarkDisconnectedIntent = z.infer<
export type AllianceExtensionIntent = z.infer<
typeof AllianceExtensionIntentSchema
>;
export type DeleteUnitIntent = z.infer<typeof DeleteUnitIntentSchema>;
export type KickPlayerIntent = z.infer<typeof KickPlayerIntentSchema>;
export type Turn = z.infer<typeof TurnSchema>;
@@ -314,6 +316,11 @@ export const MoveWarshipIntentSchema = BaseIntentSchema.extend({
tile: z.number(),
});
export const DeleteUnitIntentSchema = BaseIntentSchema.extend({
type: z.literal("delete_unit"),
unitId: z.number(),
});
export const QuickChatIntentSchema = BaseIntentSchema.extend({
type: z.literal("quick_chat"),
recipient: ID,
@@ -351,6 +358,7 @@ const IntentSchema = z.discriminatedUnion("type", [
MoveWarshipIntentSchema,
QuickChatIntentSchema,
AllianceExtensionIntentSchema,
DeleteUnitIntentSchema,
KickPlayerIntentSchema,
]);
+1
View File
@@ -127,6 +127,7 @@ export interface Config {
emojiMessageCooldown(): Tick;
emojiMessageDuration(): Tick;
donateCooldown(): Tick;
deleteUnitCooldown(): Tick;
defaultDonationAmount(sender: Player): number;
unitInfo(type: UnitType): UnitInfo;
tradeShipGold(dist: number, numPorts: number): Gold;
+3
View File
@@ -556,6 +556,9 @@ export class DefaultConfig implements Config {
donateCooldown(): Tick {
return 10 * 10;
}
deleteUnitCooldown(): Tick {
return 5 * 10;
}
emojiMessageDuration(): Tick {
return 5 * 10;
}
+81
View File
@@ -0,0 +1,81 @@
import { Execution, Game, MessageType, Player } from "../game/Game";
export class DeleteUnitExecution implements Execution {
private active: boolean = true;
private mg: Game;
constructor(
private player: Player,
private unitId: number,
) {}
activeDuringSpawnPhase(): boolean {
return false;
}
init(mg: Game, ticks: number) {
if (!this.active) {
return;
}
this.mg = mg;
const unit = this.player.units().find((u) => u.id() === this.unitId);
if (!unit) {
console.warn(
`SECURITY: unit ${this.unitId} not found or not owned by player ${this.player.displayName()}`,
);
this.active = false;
return;
}
if (!unit.isActive()) {
console.warn(`SECURITY: unit ${this.unitId} is not active`);
this.active = false;
return;
}
const tileOwner = mg.owner(unit.tile());
if (!tileOwner.isPlayer() || tileOwner.id() !== this.player.id()) {
console.warn(
`SECURITY: unit ${this.unitId} is not on player's territory`,
);
this.active = false;
return;
}
if (!mg.isLand(unit.tile())) {
console.warn(`SECURITY: unit ${this.unitId} is not on land`);
this.active = false;
return;
}
if (mg.inSpawnPhase()) {
console.warn(`SECURITY: cannot delete units during spawn phase`);
this.active = false;
return;
}
if (!this.player.canDeleteUnit()) {
console.warn(`SECURITY: delete unit cooldown not expired`);
this.active = false;
return;
}
unit.delete(false);
this.player.recordDeleteUnit();
this.mg.displayMessage(
`events_display.unit_voluntarily_deleted`,
MessageType.UNIT_DESTROYED,
this.player.id(),
);
this.active = false;
}
tick(ticks: number) {}
isActive(): boolean {
return this.active;
}
}
+3
View File
@@ -10,6 +10,7 @@ import { AttackExecution } from "./AttackExecution";
import { BoatRetreatExecution } from "./BoatRetreatExecution";
import { BotSpawner } from "./BotSpawner";
import { ConstructionExecution } from "./ConstructionExecution";
import { DeleteUnitExecution } from "./DeleteUnitExecution";
import { DonateGoldExecution } from "./DonateGoldExecution";
import { DonateTroopsExecution } from "./DonateTroopExecution";
import { EmbargoExecution } from "./EmbargoExecution";
@@ -107,6 +108,8 @@ export class Executor {
case "upgrade_structure":
return new UpgradeStructureExecution(player, intent.unitId);
case "delete_unit":
return new DeleteUnitExecution(player, intent.unitId);
case "quick_chat":
return new QuickChatExecution(
player,
+2
View File
@@ -592,6 +592,8 @@ export interface Player {
canDonate(recipient: Player): boolean;
donateTroops(recipient: Player, troops: number): boolean;
donateGold(recipient: Player, gold: Gold): boolean;
canDeleteUnit(): boolean;
recordDeleteUnit(): void;
// Embargo
hasEmbargoAgainst(other: Player): boolean;
+4
View File
@@ -355,6 +355,10 @@ export class PlayerView {
isDisconnected(): boolean {
return this.data.isDisconnected;
}
canDeleteUnit(): boolean {
return true;
}
}
export class GameView implements GameMap {
+13
View File
@@ -95,6 +95,8 @@ export class PlayerImpl implements Player {
private relations = new Map<Player, number>();
private lastDeleteUnitTick: Tick = -1;
public _incomingAttacks: Attack[] = [];
public _outgoingAttacks: Attack[] = [];
public _outgoingLandAttacks: Attack[] = [];
@@ -635,6 +637,17 @@ export class PlayerImpl implements Player {
return true;
}
canDeleteUnit(): boolean {
return (
this.mg.ticks() - this.lastDeleteUnitTick >=
this.mg.config().deleteUnitCooldown()
);
}
recordDeleteUnit(): void {
this.lastDeleteUnitTick = this.mg.ticks();
}
hasEmbargoAgainst(other: Player): boolean {
return this.embargoes.has(other.id());
}