pike/val Public Public: anyone can read this; nobody can change it.
forked from benjivers/val
workbenchthis machine
Files 7524e23c398cf40fee5ee6886d8b9cb75adfcbbf
| 1 | /** |
| 2 | * One line of advice, in the player's own words. |
| 3 | * |
| 4 | * A pure function of the snapshot the server sent plus two things the client |
| 5 | * works out from the map it was given: whether you are standing on a site, |
| 6 | * and whether you are within reach of the spike. Never more than one prompt — |
| 7 | * the most urgent thing wins, because two instructions at once is none. |
| 8 | */ |
| 9 | |
| 10 | /** Within this many units the server lets you pick up, plant or defuse. */ |
| 11 | export const REACH = 46; |
| 12 | |
| 13 | const secs = (n) => Math.max(0, Math.ceil(n)); |
| 14 | |
| 15 | export function choosePrompt(snap, world = {}) { |
| 16 | if (!snap) return ""; |
| 17 | const me = snap.me; |
| 18 | const spike = snap.spike; |
| 19 | const onSite = world.onSite === true; |
| 20 | const nearSpike = world.nearSpike === true; |
| 21 | // The page is played with a mouse and the terminal with keys; one line |
| 22 | // names the input, so it says the thing the reader can actually do. |
| 23 | const buyWith = world.input === "keys" ? "Press b and buy a gun you can afford." : "Click a gun you can afford."; |
| 24 | |
| 25 | if (snap.phase === "over") return snap.result ? `Match over — ${snap.result}` : "Match over"; |
| 26 | if (me.blind > 0.05) return "You are blind — back off until it clears"; |
| 27 | if (!me.alive) |
| 28 | return snap.phase === "buy" |
| 29 | ? "Buy phase — you are back next round" |
| 30 | : "Waiting for the next round — you're in it"; |
| 31 | if (snap.phase === "end") return snap.result ? `${snap.result} — next round in ${secs(snap.clock)}s` : "Round over"; |
| 32 | if (snap.phase === "buy") return `Buy phase — ${secs(snap.clock)}s left. ${buyWith}`; |
| 33 | |
| 34 | if (me.planting > 0) return `Planting — hold F for ${secs(4 - me.planting)}s more`; |
| 35 | if (spike.planted) { |
| 36 | if (me.side === "def") { |
| 37 | if (spike.defuse > 0) return `Defusing — keep holding F, ${secs(7 - spike.defuse)}s`; |
| 38 | return nearSpike |
| 39 | ? `Spike down, ${secs(spike.fuse)}s — hold F to defuse` |
| 40 | : `Spike down, ${secs(spike.fuse)}s — get to it and hold F`; |
| 41 | } |
| 42 | return `Spike down, ${secs(spike.fuse)}s — keep them off it`; |
| 43 | } |
| 44 | |
| 45 | if (me.side === "atk") { |
| 46 | if (me.spike) return onSite ? "Hold F to plant" : "You have the spike — get to site A or B"; |
| 47 | if (spike.carried) return "Your team has the spike — go with them and take a site"; |
| 48 | if (nearSpike) return "Walk onto the spike to pick it up"; |
| 49 | return "Somebody has to carry the spike — it is on the ground"; |
| 50 | } |
| 51 | return `Defending — hold A and B, ${secs(snap.clock)}s`; |
| 52 | } |