////// The four bunkers the player shelters behind.////// A bunker is a grid of small blocks. Anything that hits one -- a bullet on its way up, a/// bomb on its way down, or an invader that has marched far enough -- destroys the blocks/// near the impact and is itself stopped. Blocks never come back, so the bunkers erode over/// a game exactly as they do in the original.////// All of this is pure. Damage is a function from a bunker and an impact point to a new/// bunker.///mod Bunkers {////// The undamaged shape: an arch, so the cannon can shelter underneath and still shoot/// out through the top.///defshape(): Vector[String] = Vector#{"...######...","..########..",".##########.","############","############","####....####","###......###","###......###" }////// The blocks of an undamaged bunker, row by row.///deffreshCells(): Vector[Bool] = {lettotalCells = Rules.bunkerColumns() *Rules.bunkerRows();letshapes = shape();Vector.range(0, totalCells) |> Vector.map(index -> {letcolumn = Int32.remainder(index, Rules.bunkerColumns());letrow = index/Rules.bunkerColumns();matchVector.nth(row, shapes) {case Some(line) =>if (column<String.length(line))String.charAt(column, line) =='#'elsefalsecase None => false } }) }////// Four undamaged bunkers, evenly spaced.///pubdefinitial(): Vector[Bunker] =Vector.range(0, Rules.bunkerCount()) |> Vector.map(index -> { origin = {x = Rules.bunkerLeft(index), y = Rules.bunkerTop()}, cells = freshCells() })/// The centre of one block, in screen coordinates.pubdefcellCentre(bunker: Bunker, index: Int32): Vec2 = {letcolumn = Int32.remainder(index, Rules.bunkerColumns());letrow = index/Rules.bunkerColumns();letbCell = Rules.bunkerCell();letcx = bunker#origin#x + (Int32.toFloat32(column) +0.5f32) *bCell;letcy = bunker#origin#y + (Int32.toFloat32(row) +0.5f32) *bCell; {x = cx, y = cy} }////// The size of a single bunker cell block as a 2D vector.///pubdefcellSize(): Vec2 = {x = Rules.bunkerCell(), y = Rules.bunkerCell()}/// The whole bunker's footprint, used to reject distant shots cheaply.defbounds(bunker: Bunker): (Vec2, Vec2) = {letbCell = Rules.bunkerCell();letwidth = Int32.toFloat32(Rules.bunkerColumns()) *bCell;letheight = Int32.toFloat32(Rules.bunkerRows()) *bCell;letpos = {x = bunker#origin#x +width/2.0f32, y = bunker#origin#y +height/2.0f32};letsize = {x = width, y = height}; (pos, size) }defcloseRun(row: Int32, col: Int32, start: Option[Int32], acc: List[(Int32, Int32, Int32)]):List[(Int32, Int32, Int32)] =matchstart {case None => acccase Some(startCol) => (row, startCol, col-startCol) :: acc }////// The surviving blocks, collapsed into horizontal runs as `(row, startColumn, length)`.////// The same trick the sprites use, for the same reason: an intact bunker is 96 blocks/// but only about a dozen runs, and drawing it a block at a time was costing real frame/// rate once there were four of them on screen.///pubdefruns(bunker: Bunker): List[(Int32, Int32, Int32)] = {letbCols = Rules.bunkerColumns();letbRows = Rules.bunkerRows();letscanRow = (acc, row) -> {letscanned = Vector.foldLeft( (scan, col) ->letidx = row*bCols+col;if (Vector.get(idx, bunker#cells))matchscan#runStart {case None => {runStart = Some(col) | scan}case Some(_) => scan }elsematchscan#runStart {case None => scancase Some(startCol) =>letrunItem = (row, startCol, col-startCol); {runs = runItem :: scan#runs, runStart = None} }, {runs = Nil, runStart = None},Vector.range(0, bCols) );letclosedRuns = closeRun(row, bCols, scanned#runStart, scanned#runs);List.append(closedRuns, acc) };List.foldLeft(scanRow, Nil, List.range(0, bRows)) }////// Whether a shot of width `width`, fired from `x`, would meet a bunker on its way up.////// The rules do not care -- carving a slit through your own bunker is legal, and in the/// arcade original it is a tactic -- but a player who is not doing it deliberately is/// destroying their own cover and wasting the shot. This is the question to ask before/// pulling the trigger.////// This asks the question of the *same extent* `contact` uses, and so gives the same/// answer by construction. Two narrower versions were wrong in opposite directions, and/// both cost real play:////// - Sampling only the centre (`blocks`) misses the block beside the shot, because a/// bullet is four pixels wide and so is a block. The bot cleared itself a slit, declared/// the way open, and demolished its own cover from below -- one shot in seven struck a/// bunker the guard had called clear./// - Sampling the two edges as well over-corrects. `Collide.overlaps` is strict, so a/// bullet centred on a block column does *not* touch the next column -- but the right/// edge lands exactly on that column's boundary and floors into it. A perfectly drilled/// channel then reads as blocked, which makes camping impossible to aim for.////// Sampling points cannot express a strict overlap at all, so this does not sample.///pubdefobstructs(bunkers: Vector[Bunker], x: Float32, width: Float32): Bool =Vector.exists(bunker -> meets(bunker, x, width), bunkers)////// Whether any standing block of `bunker` lies in the path of a shot of `width` at `x`.////// Scans columns rather than cells: a shot rising through a bunker meets every row of the/// columns it overlaps, so which row is irrelevant to the question and only the horizontal/// extent matters. The comparison is the x half of `Collide.overlaps`, strict included.///defmeets(bunker: Bunker, x: Float32, width: Float32): Bool = {letreach = (width+Rules.bunkerCell()) /2.0f32;letinReach = column -> {letcentre = bunker#origin#x + (Int32.toFloat32(column) +0.5f32) *Rules.bunkerCell();Float32.abs(centre-x) <reach };Vector.exists(column -> inReach(column) andstandingInColumn(bunker, column),Vector.range(0, Rules.bunkerColumns()) ) }/// Whether any block is left in one column of a bunker.defstandingInColumn(bunker: Bunker, column: Int32): Bool =Vector.exists(row ->Vector.nth(row*Rules.bunkerColumns() +column, bunker#cells) |> Option.getWithDefault(false),Vector.range(0, Rules.bunkerRows()) )////// Whether any standing block sits directly above the single point `x`.////// A point is not what gets fired, so this is rarely the question a caller wants -- see/// `obstructs`, which asks it of a whole bullet.///pubdefblocks(bunkers: Vector[Bunker], x: Float32): Bool =Vector.exists(bunker -> {letoffset = (x-bunker#origin#x) /Rules.bunkerCell();matchFloat32.tryToInt32(Float32.floor(offset)) {case None => falsecase Some(column) =>if (column<0orcolumn>=Rules.bunkerColumns())falseelsestandingInColumn(bunker, column) } },bunkers )////// The number of intact, surviving blocks in a single bunker.///pubdefremaining(bunker: Bunker): Int32 = Vector.count(alive -> alive, bunker#cells)////// The total number of intact, surviving blocks across all bunkers in the vector.///pubdeftotalRemaining(bunkers: Vector[Bunker]): Int32 =Vector.foldLeft((total, bunker) -> total+remaining(bunker), 0, bunkers)////// The surviving block this box strikes, if any: the one whose centre is nearest.////// Returning the block rather than just "yes" matters. A shot is up to twelve pixels/// tall, so it can overlap a block whose centre is further away than the blast radius./// Centring the crater on the shot in that case destroys nothing while still consuming/// the shot -- the bunker silently eats bullets. Centring it on the block that was/// actually struck cannot.///defcontact(bunker: Bunker, pos: Vec2, size: Vec2): Option[Int32] = {// Reject on the whole footprint first; most shots are nowhere near a bunker, and// this turns a scan of 96 blocks into one comparison.let (centre, footprint) = bounds(bunker);letnearer = (best, index) ->if (notstruckAt(bunker, index, pos, size))bestelse {letdistance = distanceSquared(cellCentre(bunker, index), pos);matchbest {// `<=` keeps the earlier block when two are equidistant, so the answer// does not depend on which way the vector happens to be walked.case Some((_, best2)) ifbest2<=distance => bestcase_ => Some((index, distance)) } };if (notCollide.overlaps(aPos = pos, aSize = size, bPos = centre, bSize = footprint)) NoneelseVector.foldLeft(nearer, None, cellIndices(bunker)) |> Option.map(fst) }/// The indices of a bunker's blocks. Every search here is over these rather than over the/// cells themselves, because placing a block needs its index.defcellIndices(bunker: Bunker): Vector[Int32] = Vector.range(0, Vector.length(bunker#cells))/// Whether the block at `index` is still standing and overlaps the given box.defstruckAt(bunker: Bunker, index: Int32, pos: Vec2, size: Vec2): Bool =Vector.get(index, bunker#cells)andCollide.overlaps( aPos = pos, aSize = size, bPos = cellCentre(bunker, index), bSize = cellSize() )defdistanceSquared(a: Vec2, b: Vec2): Float32 = {letdx = a#x -b#x;letdy = a#y -b#y;dx*dx+dy*dy }////// Whether anything of `bunker` still overlaps the given box.////// Asks the same question as `contact` and stops at the first answer. `contact` cannot:/// it has to see every overlapping block to return the *nearest*, which is what centres a/// crater correctly, and a caller that only wants "yes" pays for all 96 cells to throw the/// index away. `erode` asks this of every living invader against every bunker on every/// tick once the formation is that low, so the difference is not academic.////// `Vector.exists` short-circuits, so the early exit survives the move away from a/// hand-written scan; the cost is the index vector `cellIndices` builds per call.///deftouches(bunker: Bunker, pos: Vec2, size: Vec2): Bool = {// Same footprint rejection `contact` opens with, and for the same reason.let (centre, footprint) = bounds(bunker);Collide.overlaps(aPos = pos, aSize = size, bPos = centre, bSize = footprint)andVector.exists(index -> struckAt(bunker, index, pos, size), cellIndices(bunker)) }////// Removes every block within `radius` of `at`.///pubdefdamage(bunker: Bunker, at: Vec2, radius: Float32): Bunker = {letcells = Vector.mapWithIndex( (index, alive) ->if (notalive)falseelse {letcentre = cellCentre(bunker, index);letdx = centre#x -at#x;letdy = centre#y -at#y; (dx*dx+dy*dy) >radius*radius },bunker#cells ); {cells = cells | bunker} }////// Offers one shot to the bunkers.////// Returns the bunkers after any damage, and whether the shot was absorbed. An absorbed/// shot is removed by the caller, which is what makes a bunker actually protect the/// player rather than merely look like it does.////// `findMap` stops at the first bunker the shot meets, which is what keeps one shot from/// damaging two shelters standing in a line.pubdefabsorb(bunkers: Vector[Bunker], pos: Vec2, size: Vec2, crater: Float32): (Vector[Bunker],Bool ) = {letstruck = which -> {letb = Vector.get(which, bunkers);contact(b, pos, size) |> Option.map(cell -> (which, cell)) };letbRange = Vector.range(0, Vector.length(bunkers));matchVector.findMap(struck, bRange) {case None => (bunkers, false)case Some((which, cell)) => {letbunker = Vector.get(which, bunkers);lethitPos = cellCentre(bunker, cell);letdamagedBunker = damage(bunker, hitPos, crater);letupdatedBunkers = Vector.update(which, damagedBunker, bunkers); (updatedBunkers, true) } } }////// Offers a whole list of bombs to the bunkers, which carry their own crater size.////// Separate from `absorbAll` rather than generalising it: the crater is a property of the/// individual bomb here, not of the batch, and threading a per-item function through the/// batch version would make the common case -- bullets, all identical -- read worse to/// buy nothing.///pubdefabsorbBombs(bunkers: Vector[Bunker], bombs: List[Bomb]): (Vector[Bunker],List[Bomb] ) = {letpass = List.foldLeft( (acc, bomb) -> {letpos = {x = bomb#x, y = bomb#y};letcrater = Rules.bombCraterFor(bomb#kind);let (damaged, wasHit) = absorb(acc#bunkers, pos, Rules.bombSize(), crater);letnextKept = if (wasHit) acc#kept elsebomb :: acc#kept; {bunkers = damaged, kept = nextKept} }, {bunkers = bunkers, kept = (Nil: List[Bomb])},bombs ); (pass#bunkers, List.reverse(pass#kept)) }////// Offers a whole list of shots to the bunkers.////// Returns the damaged bunkers and the shots that got through, in the order they arrived./// One shot damages at most one bunker: `absorb` stops at the first it makes contact with,/// so a bullet cannot bore through two shelters standing in a line.///pubdefabsorbAll(bunkers: Vector[Bunker], shots: List[Vec2], size: Vec2, crater: Float32): (Vector[Bunker],List[Vec2] ) = {letinitAcc = {bunkers = bunkers, kept = (Nil: List[Vec2])};letpass = List.foldLeft( (acc, shot) -> {let (damaged, wasHit) = absorb(acc#bunkers, shot, size, crater);letnextKept = if (wasHit) acc#kept elseshot :: acc#kept; {bunkers = damaged, kept = nextKept} },initAcc,shots ); (pass#bunkers, List.reverse(pass#kept)) }////// Invaders grind away any bunker they march into.////// Guarded by the formation's lowest point, so for most of a game this costs one/// comparison rather than a scan of every block against every invader.///pubdeferode(bunkers: Vector[Bunker], invaders: Vector[Invader]): Vector[Bunker] = {letlowest =Vector.foldLeft( (low, inv) -> if (inv#alive) Float32.max(low, inv#pos#y) elselow,0.0f32,invaders );if (lowest+Rules.invaderSize()#y /2.0f32<Rules.bunkerTop())bunkerselseVector.map(bunker ->Vector.foldLeft( (acc, inv) ->if (notinv#alive)accelseif (nottouches(acc, inv#pos, Rules.invaderSize()))accelsedamage(acc, inv#pos, Rules.invaderSize()#x /2.0f32),bunker,invaders ),bunkers ) }}