250 lines
12 KiB
TypeScript
250 lines
12 KiB
TypeScript
import type { Session } from '../../../session/Session';
|
|
import { pool, withTx } from '../repo/q';
|
|
import { getPilot, lockPilots, pilotsInSector, savePilot, type Pilot } from '../repo/players';
|
|
import { reaversInSector, getReaver, saveReaver } from '../repo/npcs';
|
|
import { getDefense, saveDefense, friendlyDefense } from '../repo/defenses';
|
|
import { planetsInSector, getPlanet, savePlanet, ownsPlanet } from '../repo/planets';
|
|
import { isConcord } from '../repo/world';
|
|
import { addNews, tell } from '../repo/news';
|
|
import { battle, alignmentShift } from '../engine/combat';
|
|
import { lanceDamage } from '../data/planets';
|
|
import { ship } from '../data/ships';
|
|
import { destroyPilot } from '../actions/fate';
|
|
import { EXPLOSION } from '../art/scenes';
|
|
import { commas, esc, readNumber, pickFromList, art, beat } from './ui';
|
|
|
|
type Target =
|
|
| { kind: 'pilot'; id: number; label: string }
|
|
| { kind: 'reaver'; id: number; label: string }
|
|
| { kind: 'defense'; label: string }
|
|
| { kind: 'planet'; id: number; label: string };
|
|
|
|
async function sayBattle(s: Session, mine: number, lost: number, killed: number, shields: number): Promise<void> {
|
|
await s.println(' |07Your fighters scream out of the launch bays...');
|
|
await beat();
|
|
await s.println(` |08» |07Committed |15${commas(mine)}|07, lost |12${commas(lost)}|07. Destroyed |10${commas(killed)}|07 fighters` +
|
|
(shields ? ` and |10${commas(shields)}|07 shield points.` : '.'));
|
|
}
|
|
|
|
/** The "A" command: pick something in the sector to shoot. */
|
|
export async function attackMenu(s: Session, uid: number): Promise<void> {
|
|
const p = (await getPilot(pool, uid))!;
|
|
if (isConcord(p.sector)) {
|
|
await s.println(' |10Concord Patrol cutters lock weapons on you. |07"Not in Concord Space, pilot."');
|
|
return;
|
|
}
|
|
if (p.fighters <= 0) {
|
|
await s.println(' |12You have no fighters to attack with.');
|
|
return;
|
|
}
|
|
const targets: Target[] = [];
|
|
for (const o of await pilotsInSector(pool, p.sector, uid)) {
|
|
if (p.syndicate_id && o.syndicate_id === p.syndicate_id) continue;
|
|
targets.push({ kind: 'pilot', id: o.user_id, label: `|11${esc(o.handle)} |08in "${esc(o.ship_name)}" (${ship(o.ship_type).name})` });
|
|
}
|
|
for (const r of await reaversInSector(pool, p.sector)) {
|
|
targets.push({ kind: 'reaver', id: r.id, label: `|12Reaver "${esc(r.name)}" |08(${ship(r.ship_type).name})` });
|
|
}
|
|
const def = await getDefense(pool, p.sector);
|
|
if (def && def.fighters > 0 && !friendlyDefense(def, uid, p.syndicate_id)) {
|
|
targets.push({ kind: 'defense', label: `|15${commas(def.fighters)} |07fighters |08owned by ${esc(def.owner_handle)}` });
|
|
}
|
|
for (const pl of await planetsInSector(pool, p.sector)) {
|
|
if (!ownsPlanet(pl, uid, p.syndicate_id)) targets.push({ kind: 'planet', id: pl.id, label: `|10Planet ${esc(pl.name)} |08(${pl.owner_handle ? esc(pl.owner_handle) : 'unclaimed'})` });
|
|
}
|
|
if (!targets.length) {
|
|
await s.println(' |07Nothing here worth shooting at.');
|
|
return;
|
|
}
|
|
await s.println();
|
|
const i = await pickFromList(s, 'Targets in sector', targets.map((t) => t.label));
|
|
if (i === null) return;
|
|
await s.print(`|03 Commit how many fighters |08(1-${commas(p.fighters)}, Enter=all)|03: `);
|
|
const n = await readNumber(s, 1, p.fighters, p.fighters);
|
|
if (!n) return;
|
|
const t = targets[i];
|
|
if (t.kind === 'pilot') await attackPilot(s, uid, t.id, n);
|
|
else if (t.kind === 'reaver') await attackReaver(s, uid, t.id, n);
|
|
else if (t.kind === 'defense') await attackDefense(s, uid, n);
|
|
else await attackPlanet(s, uid, t.id, n);
|
|
}
|
|
|
|
/** Spends the attack turn; returns false (and explains) if out of turns. */
|
|
function spendTurn(p: Pilot): boolean {
|
|
if (p.turns_left <= 0) return false;
|
|
p.turns_left--;
|
|
return true;
|
|
}
|
|
|
|
export async function attackPilot(s: Session, uid: number, targetId: number, n: number): Promise<void> {
|
|
const out = await withTx(async (c) => {
|
|
const locked = await lockPilots(c, [uid, targetId]);
|
|
const me = locked.get(uid)!;
|
|
const them = locked.get(targetId);
|
|
if (!them || them.sector !== me.sector || them.landed_planet !== null) return { gone: true as const };
|
|
if (!spendTurn(me)) return { noTurns: true as const };
|
|
n = Math.min(n, me.fighters);
|
|
const b = battle({ fighters: n, shields: 0 }, { fighters: them.fighters, shields: them.shields }, Math.random);
|
|
me.fighters -= b.attackerLost;
|
|
them.fighters = b.defender.fighters;
|
|
them.shields = b.defender.shields;
|
|
let loot = 0;
|
|
if (b.defenderBroken) {
|
|
loot = await destroyPilot(c, them, { kind: 'pilot', pilot: me }, false);
|
|
} else {
|
|
me.alignment += Math.round(alignmentShift(them.alignment) / 5);
|
|
await savePilot(c, them);
|
|
await tell(them.user_id, `|12${esc(me.handle)} attacked you in sector ${me.sector}!|07 You lost ${b.defenderFightersLost} fighters and ${b.defenderShieldsLost} shields.`, c);
|
|
}
|
|
await savePilot(c, me);
|
|
return { b, loot, name: them.handle };
|
|
});
|
|
if ('gone' in out) return void (await s.println(' |07Your target is no longer here.'));
|
|
if ('noTurns' in out) return void (await s.println(' |12You are out of turns for today.'));
|
|
await sayBattle(s, n, out.b.attackerLost, out.b.defenderFightersLost, out.b.defenderShieldsLost);
|
|
if (out.b.defenderBroken) {
|
|
await art(s, EXPLOSION);
|
|
await s.println(` |14${esc(out.name)}'s ship breaks apart! |07You salvage |15${commas(out.loot)}|07 credits.`);
|
|
} else {
|
|
await s.println(` |07${esc(out.name)} survives the exchange.`);
|
|
}
|
|
}
|
|
|
|
export async function attackReaver(s: Session, uid: number, reaverId: number, n: number): Promise<void> {
|
|
const out = await withTx(async (c) => {
|
|
const me = (await getPilot(c, uid, true))!;
|
|
const r = await getReaver(c, reaverId, true);
|
|
if (!r || r.sector !== me.sector) return { gone: true as const };
|
|
if (!spendTurn(me)) return { noTurns: true as const };
|
|
n = Math.min(n, me.fighters);
|
|
const b = battle({ fighters: n, shields: 0 }, { fighters: r.fighters, shields: r.shields }, Math.random);
|
|
me.fighters -= b.attackerLost;
|
|
r.fighters = b.defender.fighters;
|
|
r.shields = b.defender.shields;
|
|
let bounty = 0;
|
|
if (b.defenderBroken) {
|
|
bounty = r.credits + 2000 + r.ship_type * 1500;
|
|
me.credits += bounty;
|
|
me.xp += 20 + r.ship_type * 10;
|
|
me.alignment += 10;
|
|
me.kills++;
|
|
await addNews(`|11${esc(me.handle)}|07 destroyed the Reaver |12"${esc(r.name)}"|07 of clan ${esc(r.clan)} in sector ${me.sector}.`, c);
|
|
}
|
|
await saveReaver(c, r);
|
|
await savePilot(c, me);
|
|
return { b, bounty, name: r.name };
|
|
});
|
|
if ('gone' in out) return void (await s.println(' |07The Reaver has slipped away.'));
|
|
if ('noTurns' in out) return void (await s.println(' |12You are out of turns for today.'));
|
|
await sayBattle(s, n, out.b.attackerLost, out.b.defenderFightersLost, out.b.defenderShieldsLost);
|
|
if (out.b.defenderBroken) {
|
|
await art(s, EXPLOSION);
|
|
await s.println(` |14"${esc(out.name)}" is scrap! |07Concord pays a bounty of |15${commas(out.bounty)}|07 credits.`);
|
|
} else {
|
|
await s.println(` |07"${esc(out.name)}" is still out there, and now it's angry.`);
|
|
}
|
|
}
|
|
|
|
/** Returns true if the sector's hostile fighters were wiped out. */
|
|
export async function attackDefense(s: Session, uid: number, n: number): Promise<boolean> {
|
|
const out = await withTx(async (c) => {
|
|
const me = (await getPilot(c, uid, true))!;
|
|
const d = await getDefense(c, me.sector, true);
|
|
if (!d || d.fighters <= 0 || friendlyDefense(d, uid, me.syndicate_id)) return { gone: true as const };
|
|
if (!spendTurn(me)) return { noTurns: true as const };
|
|
n = Math.min(n, me.fighters);
|
|
const b = battle({ fighters: n, shields: 0 }, { fighters: d.fighters, shields: 0 }, Math.random);
|
|
me.fighters -= b.attackerLost;
|
|
d.fighters = b.defender.fighters;
|
|
let swept = 0;
|
|
if (b.defenderBroken && d.mines > 0) {
|
|
swept = Math.min(d.mines, b.attacker.fighters);
|
|
d.mines -= swept;
|
|
me.fighters -= swept;
|
|
}
|
|
if (b.defenderBroken) me.xp += 5;
|
|
await saveDefense(c, d);
|
|
await savePilot(c, me);
|
|
await tell(d.owner_id, `|12${esc(me.handle)} hit your fighters in sector ${me.sector}:|07 ${b.defenderFightersLost} lost${b.defenderBroken ? ' (all gone)' : ''}.`, c);
|
|
return { b, swept };
|
|
});
|
|
if ('gone' in out) return true;
|
|
if ('noTurns' in out) {
|
|
await s.println(' |12You are out of turns for today.');
|
|
return false;
|
|
}
|
|
await sayBattle(s, n, out.b.attackerLost, out.b.defenderFightersLost, 0);
|
|
if (out.b.defenderBroken) {
|
|
await s.println(' |10The sector is clear.' + (out.swept ? ` |07Your fighters sweep |15${out.swept}|07 mines (and are lost doing it).` : ''));
|
|
}
|
|
return out.b.defenderBroken;
|
|
}
|
|
|
|
export async function attackPlanet(s: Session, uid: number, planetId: number, n: number): Promise<void> {
|
|
const out = await withTx(async (c) => {
|
|
const me = (await getPilot(c, uid, true))!;
|
|
const pl = await getPlanet(c, planetId, true);
|
|
if (!pl || pl.sector !== me.sector || ownsPlanet(pl, uid, me.syndicate_id)) return { gone: true as const };
|
|
if (!spendTurn(me)) return { noTurns: true as const };
|
|
n = Math.min(n, me.fighters);
|
|
let lance = 0;
|
|
if (lanceDamage(pl.bastion) > 0 && pl.stock[0] >= 10) {
|
|
pl.stock[0] -= 10;
|
|
lance = Math.min(n, lanceDamage(pl.bastion));
|
|
n -= lance;
|
|
me.fighters -= lance;
|
|
}
|
|
const b = battle({ fighters: n, shields: 0 }, { fighters: Math.floor(pl.fighters), shields: Math.floor(pl.shields) }, Math.random, 1, 1.2);
|
|
me.fighters -= b.attackerLost;
|
|
pl.fighters = b.defender.fighters;
|
|
pl.shields = b.defender.shields;
|
|
const prevOwner = pl.owner_id;
|
|
if (b.defenderBroken) {
|
|
pl.owner_id = uid;
|
|
pl.syndicate_id = me.syndicate_id;
|
|
me.xp += 50 + pl.bastion * 25;
|
|
me.alignment -= prevOwner ? 20 : 0;
|
|
await addNews(`|11${esc(me.handle)}|07 seized the planet |10${esc(pl.name)}|07 in sector ${pl.sector}${pl.owner_handle ? ` from ${esc(pl.owner_handle)}` : ''}!`, c);
|
|
}
|
|
await savePlanet(c, pl);
|
|
await savePilot(c, me);
|
|
if (prevOwner && prevOwner !== uid) {
|
|
await tell(prevOwner, b.defenderBroken
|
|
? `|12${esc(me.handle)} CAPTURED your planet ${esc(pl.name)} in sector ${pl.sector}!`
|
|
: `|12${esc(me.handle)} assaulted your planet ${esc(pl.name)} (sector ${pl.sector}) and was driven off.`, c);
|
|
}
|
|
return { b, lance, name: pl.name, sent: n + lance };
|
|
});
|
|
if ('gone' in out) return void (await s.println(' |07You cannot attack that planet now.'));
|
|
if ('noTurns' in out) return void (await s.println(' |12You are out of turns for today.'));
|
|
if (out.lance) await s.println(` |14The planet's Lance Battery fires! |12${commas(out.lance)}|07 of your fighters are vaporized.`);
|
|
await sayBattle(s, out.sent, out.b.attackerLost + out.lance, out.b.defenderFightersLost, out.b.defenderShieldsLost);
|
|
if (out.b.defenderBroken) await s.println(` |14The defenders of ${esc(out.name)} surrender. |10The planet is yours!`);
|
|
else await s.println(` |07${esc(out.name)} holds out.`);
|
|
}
|
|
|
|
/** The "T" command: tag a ship with a tracer beacon. */
|
|
export async function placeTracer(s: Session, uid: number): Promise<void> {
|
|
const p = (await getPilot(pool, uid))!;
|
|
if (p.tracers <= 0) return void (await s.println(' |07You have no tracer beacons. The Bazaar sells them.'));
|
|
const others = await pilotsInSector(pool, p.sector, uid);
|
|
if (!others.length) return void (await s.println(' |07There are no other ships here.'));
|
|
const i = await pickFromList(s, 'Tag which ship?', others.map((o) => `|11${esc(o.handle)} |08"${esc(o.ship_name)}"`));
|
|
if (i === null) return;
|
|
const target = others[i];
|
|
const spotted = Math.random() < 0.2;
|
|
await withTx(async (c) => {
|
|
const me = (await getPilot(c, uid, true))!;
|
|
if (me.tracers <= 0) return;
|
|
me.tracers--;
|
|
await savePilot(c, me);
|
|
await c.query(
|
|
'INSERT INTO sl_tracers (owner_id, target_id) VALUES ($1, $2) ON CONFLICT (owner_id, target_id) DO UPDATE SET placed_at = now()',
|
|
[uid, target.user_id],
|
|
);
|
|
if (spotted) await tell(target.user_id, `|14Your hull sensors pick up a tracer beacon. Someone in sector ${me.sector} is following you.`, c);
|
|
});
|
|
await s.println(` |10Tracer attached to ${esc(target.handle)}'s hull.${spotted ? ' |12(They may have noticed.)' : ''} |07Track them from the Computer.`);
|
|
}
|
|
|