'use strict'; /* * Arrival handlers for the movements that are not fights: reinforce, transport, settle. Plus the shared pieces every * arrival uses (cargo delivery, bouncing a movement back, brief descriptions for reports). Each handler runs inside the * `arrive` event's transaction at the event's own due time. */ const R = require('../../public/js/rules.js'); const D = require('../../public/js/data.js'); const { loadTown, settle, loadWorld } = require('../state'); const { createTown } = require('../commands/joinWorld'); const U = require('./units'); const M = require('./movements'); const P = require('./power'); const { createReport } = require('./reports'); const townBrief = (t) => t ? { id: t.id, name: t.name, x: t.x, y: t.y } : null; const opBrief = (o) => o ? { id: o.id, name: o.name, asNumber: o.as_number } : null; // Add resources to a SETTLED town, capped at its storage (an amount already above the cap is kept). -> what was lost // `bonus` (optional, per resource) is part of the cargo that is credited even above the Rack hall size (one-time rewards). function addCargo(db, town, cargo, bonus) { const lost = {}; for (const r of D.RESOURCES) { const add = (cargo && cargo[r]) || 0; if (!add) continue; const cur = town.res[r]; const free = Math.max(0, add - ((bonus && bonus[r]) || 0)); const next = Math.min(Math.max(R.storageCapOf(town, r), cur), cur + free) + (add - free); // escrowed goods of open offers keep their room (storageCapOf) lost[r] = Math.max(0, Math.round(cur + add - next)); town.res[r] = next; } db.run('UPDATE towns SET fiber = ?, hardware = ?, capital = ? WHERE id = ?', town.res.fiber, town.res.hardware, town.res.capital, town.id); return lost; } // The movement cannot do what it came for: its units (and cargo) turn around and a report tells the sender why. function bounce(ctx, mv, now, reason, details) { const { db } = ctx; const dest = U.homeTownFor(db, mv.ownerId, mv.fromTownId); if (dest && !U.isEmpty(mv.units)) { M.launchReturn(ctx, { worldId: mv.worldId, ownerId: mv.ownerId, fromTownId: mv.toTownId, at: mv.to, to: dest, units: mv.units, cargo: mv.cargo, parentId: mv.id, extra: { bounced: reason } }); } createReport(ctx, { worldId: mv.worldId, type: 'movement_failed', now, summary: { movementType: mv.type, reason, to: mv.to }, data: { movementType: mv.type, reason, details: details || {}, to: mv.to, units: mv.units }, recipients: [{ userId: U.userOfOperator(db, mv.ownerId), role: 'sender' }], }); } function arriveReinforce(ctx, mv, ev) { const { db } = ctx; const now = ev.dueAt; if (!mv.toTownId) return require('./landing').reinforce(ctx, mv, ev); // a backbone landing site: the garrison (E6) const target = mv.toTownId ? db.get('SELECT * FROM towns WHERE id = ?', mv.toTownId) : null; if (!target || !U.mayReinforce(db, mv.ownerId, target.owner_id)) return bounce(ctx, mv, now, 'not_allied'); const ownerTown = U.homeTownFor(db, mv.ownerId, mv.fromTownId); if (!ownerTown) return; // the sender lost every POP meanwhile: nothing to belong to const host = loadTown(db, target.id); settle(db, host, now); // the host's draw rises at this moment // Moving units between your own POPs: they simply live in the new POP now. Only other operators' units are "stationed". if (ownerTown.id === host.id || target.owner_id === mv.ownerId) U.addHome(db, host.id, mv.units); else U.addStationed(db, host.id, ownerTown.id, mv.units); P.reschedule(ctx, host.id, now); const sender = U.getOperator(db, mv.ownerId), hostOp = U.getOperator(db, target.owner_id); createReport(ctx, { worldId: mv.worldId, type: 'reinforce', now, summary: { host: townBrief(target), from: townBrief(ownerTown), units: mv.units }, data: { host: townBrief(target), hostOperator: opBrief(hostOp), from: townBrief(ownerTown), operator: opBrief(sender), units: mv.units }, recipients: [{ userId: U.userOfOperator(db, sender.id), role: 'sender' }, { userId: U.userOfOperator(db, hostOp.id), role: 'target' }], }); } function arriveTransport(ctx, mv, ev) { const { db } = ctx; const now = ev.dueAt; // A shipment to a backbone landing site (extra.siteId) feeds the site's stockpile; refused ones ride home like any undeliverable cargo. const siteDelivered = !!(mv.extra && mv.extra.siteId) && require('../endgame').deliverShipment(ctx, mv, now); const target = mv.toTownId ? loadTown(db, mv.toTownId) : null; let lost = {}; if (target) { settle(db, target, now); lost = addCargo(db, target, mv.cargo); const sender = U.userOfOperator(db, mv.ownerId), receiver = U.userOfOperator(db, target.ownerId); if (receiver) ctx.notify(receiver, mv.worldId, 'transport_arrived', { movementId: mv.id, townId: target.id, fromTownId: mv.fromTownId, cargo: mv.cargo, cargoLost: lost, own: receiver === sender }); if (sender && sender !== receiver) ctx.notify(sender, mv.worldId, 'transport_delivered', { movementId: mv.id, toTownId: target.id, cargo: mv.cargo, cargoLost: lost }); for (const fn of M.hooks.transportArrived) fn(ctx, mv, { added: mv.cargo, lost }); } const cargoBack = target || siteDelivered ? {} : mv.cargo; // no such POP any more: the cargo rides home with the carriers // The empty carriers drive back; the link is busy until they are home. const dest = U.homeTownFor(db, mv.ownerId, mv.fromTownId); if (dest) { const world = loadWorld(db, mv.worldId); const dist = R.distance(mv.to.x, mv.to.y, dest.x, dest.y); M.launchReturn(ctx, { worldId: mv.worldId, ownerId: mv.ownerId, fromTownId: mv.toTownId, at: mv.to, to: dest, units: {}, cargo: cargoBack, arriveAt: now + R.transportMs(dist, world.speed), parentId: mv.id, extra: Object.assign({}, mv.extra, { link: 1 }) }); } } // Three Rollout crews found a new POP on the empty tile, if it is still empty land, still in range, footprint still enough. function arriveSettle(ctx, mv, ev) { const { db } = ctx; const now = ev.dueAt; const world = loadWorld(db, mv.worldId); const op = U.getOperator(db, mv.ownerId); const { x, y } = mv.to; const tile = R.tileAt(world.seed, world.map_radius, x, y, R.worldAgeDays(world.start_at, now, world.speed)); const occupied = !!db.get('SELECT 1 AS x FROM towns WHERE world_id = ? AND x = ? AND y = ?', world.id, x, y); const own = U.operatorTowns(db, op.id); let nearest = Infinity; for (const t of own) nearest = Math.min(nearest, R.distance(t.x, t.y, x, y)); const chk = R.settleCheck({ tileType: tile.type, backbone: R.isBackboneSite(world.seed, world.map_radius, x, y), occupied, nearestOwnDist: nearest, ownedCount: own.length, footprint: U.operatorFootprint(db, op.id, now) }); if (!chk.ok) return bounce(ctx, mv, now, chk.code, chk); const wild = R.cleanWild(mv.extra.wild); const name = op.name + ' POP ' + (own.length + 1); const id = createTown(db, { worldId: world.id, ownerId: op.id, name, x, y, isCapital: false, now, buildings: R.newTownBuildings(wild), res: D.EXPANSION.startResources }); db.run('UPDATE towns SET battery = 0, loyalty = ? WHERE id = ?', D.EXPANSION.foundedLoyalty, id); const founded = { id, name, x, y }; createReport(ctx, { worldId: world.id, type: 'settle', now, summary: { success: true, town: founded }, data: { success: true, town: founded, wild, crews: mv.units, from: mv.fromTownId }, recipients: [{ userId: U.userOfOperator(db, op.id), role: 'sender' }], }); } module.exports = { addCargo, bounce, arriveReinforce, arriveTransport, arriveSettle, townBrief, opBrief };