A bounty world event
Spawn a named, oversized raider warlord on a timer, mark it on every map, pay whoever lands the killing blow, and clear everything away when nobody does.
Every twenty minutes a raider warlord turns up at one of a few spots in the Commonwealth: bigger than any raider, armed with a minigun, named over its head and marked on every compass. Whoever kills it collects 500 caps. If nobody does within fifteen minutes it leaves, and the server says so.
This is the bounty feature of the default gamemode, rebuilt step by step. It touches most of what
the server can do with NPCs and world markers.
What you will learn
Section titled “What you will learn”- Spawning an NPC from a leveled list with
Npc.spawn, and why it can returnnull. - Shaping it:
setName,setScale,setMaxHealth,giveWeapon,wander. - Marking it for everyone with a
Blipand aTextLabelthat follow its body. - Paying the killer from
npcDeath, and expiring an event nobody finished. - Telling players in their HUD when an NPC turns on them, with
npcTarget. - Cleaning up what a resource placed in the world when it stops.
bounty/ package.json tsconfig.json src/server/ index.ts imports, and the /bounty command spots.ts where a warlord may appear bounty.ts spawning, rewarding, expiring alerts.ts "a raider is attacking you" src/client/ index.ts shows the server's HUD lines1. Where it can appear
Section titled “1. Where it can appear”An NPC placed in the air falls, and a fall can kill it before anybody sees it. A map marker’s own
height is often a roof or a highway deck, so measure each spot where a body actually comes to rest:
stand there and read player.position back with /tp or a debug command.
export interface Spot { name: string; position: Vector3;}
/** Open ground near map markers, measured at foot level. */export const kSpots: Spot[] = [ { name: "Red Rocket", position: new Vector3(-68779, 80148, 7380) }, { name: "Concord", position: new Vector3(-57011, 70992, 6200) }, { name: "Lexington", position: new Vector3(-32440, 41369, 3200) },];
export function randomSpot(): Spot { return kSpots[Math.floor(Math.random() * kSpots.length)] ?? kSpots[0]!;}2. Spawn and shape the warlord
Section titled “2. Spawn and shape the warlord”Npc.spawn takes an NPC record or a leveled list, by form ID or editor ID. A leveled list is
rolled on the server, which is how LCharRaider gives you a different raider each time. A list can
also roll nothing, so the result is Npc | null, and an unknown record throws.
import { type Spot, randomSpot } from "./spots.js";
const kBossBase = "LCharRaider";const kBossName = "Raider Warlord";
export function spawnWarlord(spot: Spot): Npc | string { let boss: Npc | null; try { boss = Npc.spawn(kBossBase, spot.position); } catch (error) { return `no warlord: ${String(error)}`; } if (boss === null) return "no warlord: the leveled list rolled nothing";
boss.setName(kBossName); // crosshair, health bar and dialogue boss.setScale(1.4); // body, collision and reach grow together boss.setMaxHealth(1500); // an unhurt NPC stays at full boss.giveWeapon("Minigun"); // locked in its hands, with ammunition boss.wander(); // sandbox around where it stands, instead of standing still return boss;}
export const pickSpot = randomSpot;Each of these is sent to every player’s copy of the NPC and lasts as long as the NPC does, through a
respawn(). None of them needs a player nearby: an NPC nobody is close to is walked by the server,
and the first player who comes near takes it over with all of its settings.
3. Mark it on every map
Section titled “3. Mark it on every map”A Blip puts an icon on the compass and the Pip-Boy map. A TextLabel floats text over a point or
a body. Both accept entity, which makes them follow the NPC as it moves:
export interface Markers { blip: Blip | null; label: TextLabel | null;}
export function markWarlord(boss: Npc, name: string): Markers { return { blip: Blip.create({ entity: boss, label: `Bounty: ${name}`, kind: "quest" }), // 0xRRGGBBAA. Visible from 2500 units, 60 above the head. label: TextLabel.create({ entity: boss, text: name, color: 0xff3020ff, range: 2500, height: 60 }), };}
export function clearMarkers(markers: Markers): void { markers.blip?.destroy(); markers.label?.destroy(); markers.blip = null; markers.label = null;}Pass player to either and only that player sees it: a personal waypoint, or a bounty on just one
hunter’s map.
4. Run the event
Section titled “4. Run the event”Now tie it together. One bounty at a time, a timer that expires it, and npcDeath to pay the
killer. killer is the last player who hit the NPC, or null if it died some other way: another
NPC, a fall, or npc.kill().
import { type Spot, randomSpot } from "./spots.js";import { type Markers, clearMarkers, markWarlord } from "./markers.js";
const kBossBase = "LCharRaider";const kBossName = "Raider Warlord";const kReward = 500;const kStaysMs = 15 * 60 * 1000;const kCorpseMs = 2 * 60 * 1000;const kColor = 0xffa000ff;
interface Bounty { boss: Npc; spot: Spot; markers: Markers; expiry: Timeout;}
let current: Bounty | null = null;
const announce = (text: string): void => { Chat.sendToAll(text, { author: "Bounty", color: kColor }); console.log(`[bounty] ${text}`);};
export function startBounty(): string { if (current !== null) return `a bounty is already out at ${current.spot.name}`;
const spot = randomSpot(); let boss: Npc | null; try { boss = Npc.spawn(kBossBase, spot.position); } catch (error) { return `no warlord: ${String(error)}`; } if (boss === null) return "no warlord: the leveled list rolled nothing";
boss.setName(kBossName); boss.setScale(1.4); boss.setMaxHealth(1500); boss.giveWeapon("Minigun"); boss.wander();
const spawned = boss; const expiry = setTimeout(() => { if (current?.boss.id !== spawned.id) return; clearMarkers(current.markers); current.boss.remove(); current = null; announce(`The ${kBossName} left ${spot.name} unclaimed.`); }, kStaysMs);
current = { boss: spawned, spot, markers: markWarlord(spawned, kBossName), expiry }; announce(`A ${kBossName} was sighted near ${spot.name}: ${kReward} caps to whoever brings it down.`); return `warlord ${spawned.id} out at ${spot.name}`;}
export function currentBounty(): Bounty | null { return current;}
Events.on("npcDeath", (npc: Npc, killer: Player | null) => { if (current === null || npc.id !== current.boss.id) return;
const bounty = current; current = null; clearTimeout(bounty.expiry); clearMarkers(bounty.markers);
if (killer !== null) { killer.setCaps(killer.getCaps() + kReward); announce(`${killer.nickname} brought down the ${kBossName} and claims ${kReward} caps.`); } else { announce(`The ${kBossName} near ${bounty.spot.name} is dead; nobody claims the bounty.`); }
// Leave the corpse for looting, then take it out of the session. setTimeout(() => npc.remove(), kCorpseMs);});A few details carry most of the weight here:
- Clear
currentbefore anything else. Every later line can assume the bounty is over, and a secondnpcDeathfor the same body can never pay twice. clearTimeout(bounty.expiry). Without it, the expiry fires fifteen minutes later and removes whatever NPC is incurrentby then, which may be the next warlord.npc.remove()only works on spawned NPCs. It returnsfalsefor one that stands for a placed reference from the game’s data: those live for the whole session and come back on their own.
5. Start it on a timer
Section titled “5. Start it on a timer”import { currentBounty, startBounty } from "./bounty.js";import "./alerts.js";import "./cleanup.js";
const kEveryMs = 20 * 60 * 1000;
setInterval(() => { // Nobody to hunt it: do not spend an NPC on an empty server. if (Player.all().length > 0) startBounty();}, kEveryMs);
Events.on("playerCommand", (player: Player, command: string, args: string[]) => { if (command !== "bounty") return; if (args[0] === "start") { Chat.sendToPlayer(player, startBounty(), { author: "Bounty" }); return; } const bounty = currentBounty(); const text = bounty === null ? "No bounty is out." : `Warlord near ${bounty.spot.name}: ${bounty.boss.health.toFixed(0)}/${bounty.boss.maxHealth.toFixed(0)} health.`; Chat.sendToPlayer(player, text, { author: "Bounty" });});boss.health and boss.maxHealth read 0 until some player’s game has run the NPC: the server
cannot work health out from the records alone. Treat 0 as “not yet known”, not “dead”; isDead is
the field for that.
6. Clean up on reload
Section titled “6. Clean up on reload”The resource’s timers and handlers are removed for you when it stops. The NPC, the blip and the label are not:
import { currentBounty } from "./bounty.js";import { clearMarkers } from "./markers.js";
Events.on("resourceStop", (resourceName: string) => { if (resourceName !== "bounty") return; const bounty = currentBounty(); if (bounty === null) return; clearMarkers(bounty.markers); bounty.boss.remove();});7. Tell players they are under attack
Section titled “7. Tell players they are under attack”npcTarget fires whenever the target an NPC fights changes: it noticed someone, switched, or stopped
(target is null, including on death). It fires again for the same pair while a fight goes on, so
remember the last target per NPC and only speak when it changes:
const lastTargets = new Map<number, number>();
Events.on("npcTarget", (npc: Npc, target: Player | Npc | null) => { const previous = lastTargets.get(npc.id); if (target === null) { lastTargets.delete(npc.id); return; } lastTargets.set(npc.id, target.id); if (!(target instanceof Player) || previous === target.id) return;
// player.emit forwards JSON text verbatim, so a plain string is stringified first. target.emit("alert:spotted", JSON.stringify(npc.name));});
Events.on("npcDeath", (npc: Npc) => { lastTargets.delete(npc.id);});On the client, show it in the HUD’s corner, with the game’s own “new quest” sound:
// clientEvents.on("alert:spotted", (payload: unknown) => { const name = typeof payload === "string" ? payload : ""; Hud.notify(name !== "" ? `${name} is attacking you` : "You have been spotted", "UIQuestNew");});Only named NPCs get named here, which is exactly what you want: the warlord announces itself, an ordinary raider camp does not.
Try it
Section titled “Try it”-
Build and load:
Server console refreshensure bounty -
/bounty start. Everyone gets the chat line, and a quest marker appears on the compass. -
Follow the marker. The name floats over the warlord from 2500 units away.
-
Get its attention. Your HUD says
Raider Warlord is attacking you. -
Kill it and check your caps:
/bountynow says no bounty is out. -
Start another and run
ensure bountywhile it is out. The marker and the NPC disappear.
What to change next
Section titled “What to change next”- A kill feed. Broadcast every
npcDeathwith a killer and a non-emptynpc.nameto all HUDs withEvents.emitAllClients("hud:notify", JSON.stringify(text)). Unnamed NPCs stay out, or a raider camp floods it. - A health bar on screen. Send
{ name, fraction: boss.health / boss.maxHealth }to players near the warlord every second and draw it in a browser view, the way Settlement terminal draws its panel. - Escorts. Spawn two or three ordinary raiders around the warlord and
attackwhoever it targets.