A minefield game mode
Use mineLaid, mineTrigger and mineRemove to cap how many mines a player may lay, spare their own team, score the kills, and build a disarm mechanic.
Mines are the most scriptable thing a Fallout 4 player carries. Every one that settles becomes a replicated entity with an owner, a radius and an explosion, and every stage of its life is an event you can refuse. You will turn that into a game mode: two teams, a budget of mines each, no friendly detonations, a score, and a way to disarm somebody else’s work.
What you will learn
Section titled “What you will learn”- The three mine events, and which of them can say no.
- Why
mineTriggeris asked repeatedly rather than once. - Keeping per-player counts that survive a reload, in a state bag rather than a module variable.
- Reading
Mine.all()to build a live picture of the field.
minefield/ package.json tsconfig.json src/server/ index.ts teams and scoring teams.ts who is on which side mines.ts the budget and the friendly-fire rule disarm.ts taking somebody else's mine out of the ground1. The life of a mine
Section titled “1. The life of a mine”| Event | When | Refusable |
|---|---|---|
mineLaid |
The mine has settled and is armed | Yes. false removes it, unexploded |
mineTrigger |
A player is standing on somebody’s mine | Yes. false leaves it armed |
mineRemove |
It is gone, however that happened | No. The handle still reads |
mineRemove covers every ending: the blast, a disarm, a script calling remove, and the owner
disconnecting. You do not have to clean up a leaver’s mines yourself; the server already does, and
tells you through this event.
2. Teams
Section titled “2. Teams”Two teams, stored where both the server and the owning client can see them:
export const Team = { Brotherhood: "bos", Railroad: "rr" } as const;export type TeamId = (typeof Team)[keyof typeof Team];
export function teamOf(player: Player): TeamId | null { const value = player.state.get("team"); return value === Team.Brotherhood || value === Team.Railroad ? value : null;}
export function setTeam(player: Player, team: TeamId): void { // 'broadcast': everyone's game sees it, so a client HUD can colour nametags. player.state.set("team", team, { scope: "broadcast" }); player.setNametagColor(team === Team.Brotherhood ? 0x4f86c6ff : 0xc65353ff);}
Events.on("playerReady", (player: Player) => { if (teamOf(player)) return; const counts = Player.all().filter((other) => teamOf(other) === Team.Brotherhood).length; setTeam(player, counts * 2 <= Player.all().length ? Team.Brotherhood : Team.Railroad);});3. A budget, enforced where it cannot be bypassed
Section titled “3. A budget, enforced where it cannot be bypassed”A player’s own game decides to place a mine; the server decides whether it stays. mineLaid fires
once the mine has settled and armed, and returning false takes it out of the world without it
going off.
const MAX_PER_PLAYER = 4;
function laidBy(player: Player): Mine[] { return Mine.all().filter((mine) => mine.getOwner()?.id === player.id);}
Events.on("mineLaid", (mine: Mine) => { const owner = mine.getOwner(); if (!owner) return;
// This mine is already in the list: it settled before the event was raised. const laid = laidBy(owner).length; if (laid > MAX_PER_PLAYER) { Chat.sendToPlayer(owner, `Your ${MAX_PER_PLAYER} mines are already out there.`, { author: "Sapper" }); return false; }
Chat.sendToPlayer(owner, `Mine armed. ${MAX_PER_PLAYER - laid} left.`, { author: "Sapper" }); return true;});4. No friendly detonations
Section titled “4. No friendly detonations”mineTrigger is raised when a player walks onto somebody’s armed mine, before it goes off.
Returning false keeps it armed, and their game asks again every half second while they stand on
it. That repetition is the point: a rule that depends on who is standing there has to be
re-evaluated, not decided once.
import { teamOf } from "./teams.js";
const MAX_PER_PLAYER = 4;
function laidBy(player: Player): Mine[] { return Mine.all().filter((mine) => mine.getOwner()?.id === player.id);}
Events.on("mineLaid", (mine: Mine) => { const owner = mine.getOwner(); if (!owner) return;
const laid = laidBy(owner).length; if (laid > MAX_PER_PLAYER) { Chat.sendToPlayer(owner, `Your ${MAX_PER_PLAYER} mines are already out there.`, { author: "Sapper" }); return false; }
Chat.sendToPlayer(owner, `Mine armed. ${MAX_PER_PLAYER - laid} left.`, { author: "Sapper" }); return true;});
Events.on("mineTrigger", (mine: Mine, player: Player) => { const owner = mine.getOwner(); if (!owner) return true;
if (owner.id === player.id) { // Your own mine never takes you. return false; }
if (teamOf(owner) !== null && teamOf(owner) === teamOf(player)) { return false; }
Chat.sendToAll(`${player.nickname} found ${owner.nickname}'s mine.`, { author: "Sapper" }); return true;});A teammate can now walk a corridor of friendly mines and come out the other side, and the same corridor still takes an enemy.
5. Score it
Section titled “5. Score it”mineRemove is the only event that is guaranteed to fire, so it is the honest place to count. Pair
it with the trigger that preceded it to tell a detonation from a disarm:
/** Mines that went off under somebody, by mine ID. */const triggered = new Map<number, number>();
Events.on("mineTrigger", (mine: Mine, player: Player) => { triggered.set(mine.id, player.id);});
Events.on("mineRemove", (mine: Mine) => { const victimId = triggered.get(mine.id); triggered.delete(mine.id);
const owner = mine.getOwner(); if (!owner || victimId === undefined) return;
const score = Number(owner.state.get("mineKills") ?? 0) + 1; owner.state.set("mineKills", score, { scope: "broadcast" }); Chat.sendToPlayer(owner, `Mine kill. Total: ${score}.`, { author: "Sapper" });});6. Disarm somebody else’s
Section titled “6. Disarm somebody else’s”Nothing in the game lets a player pick a mine back up, so build it: crouch near an enemy mine, hold a key, and it comes out of the ground. The client asks, the server decides.
import { teamOf } from "./teams.js";
const REACH = 150;
Events.onClient("sapper:disarm", (...args: unknown[]) => { const player = args[0]; if (!(player instanceof Player)) return;
const nearest = Mine.all() .map((mine) => ({ mine, away: mine.position.distance(player.position) })) .filter((entry) => entry.away <= REACH) .sort((a, b) => a.away - b.away)[0];
if (!nearest) { Chat.sendToPlayer(player, "Nothing within reach.", { author: "Sapper" }); return; }
const owner = nearest.mine.getOwner(); if (owner && teamOf(owner) === teamOf(player)) { Chat.sendToPlayer(player, "That one is yours to keep.", { author: "Sapper" }); return; }
// remove() takes it out unexploded. detonate() would set it off where it lies. nearest.mine.remove(); const score = Number(player.state.get("disarms") ?? 0) + 1; player.state.set("disarms", score, { scope: "broadcast" }); Chat.sendToPlayer(player, `Disarmed. Total: ${score}.`, { author: "Sapper" });});The client half is three lines, and deliberately carries no information the server needs to trust:
Key.bind("g", "down", () => { Events.emitServer("sapper:disarm"); Hud.showSubtitle("Disarming...", 2);});Note what is not sent: no mine ID, no position. The server finds the nearest mine from the player’s own authoritative position, so a modified client cannot disarm something across the map.
7. A field report
Section titled “7. A field report”Mine.all() is the whole minefield, which makes a status command two lines of work:
import "./teams.js";import "./mines.js";import "./score.js";import "./disarm.js";
Events.on("playerCommand", (player: Player, command: string) => { if (command !== "field") return; const mines = Mine.all(); Chat.sendToPlayer(player, `${mines.length} armed mines out there.`, { author: "Sapper" }); for (const mine of mines.slice(0, 5)) { const away = Math.round(mine.position.distance(player.position) / 100); Chat.sendToPlayer(player, ` ${mine.getOwner()?.nickname ?? "unknown"} at ${away} m, radius ${Math.round(mine.radius)}`, { author: "Sapper" }); }});Try it
Section titled “Try it”-
Build and load:
Server console refreshensure minefield -
Lay five mines. The fifth is refused and never arms.
-
Walk over your own. Nothing happens, and nothing happens again half a second later.
-
Have a player from the other team walk the same ground.
-
Stand next to an enemy mine and press G.
-
Type
/fieldto see what is still out there.
What to change next
Section titled “What to change next”- A detonator. Keep each player’s mines in a list and add a command that calls
detonate()on all of them at once.mine.explosiontells you which blast each one carries. - Mark the field for its owner. Use
setVisibleToso a mine only replicates to the team that laid it, and the other side has to find them the hard way. - Round scoring. Reset the state bags on a timer, announce a winner with
Chat.sendToAll, and clear the field by callingremove()on everything inMine.all().