////// The rules of Invaders, as a pure function.////// `step` takes a world and one input snapshot and returns the next world. It has no clock,/// no window, and no `IO`, and its randomness is threaded through as data. That is what lets/// the whole game be tested headlessly, and what makes an identical input script replay to an/// identical world.////// `step` is a pipeline of small stages. Each one is worth reading on its own, and each is/// tested on its own.///mod Game {////// The formation at the start of a game.///defformation(level: Int32): Vector[Invader] =Vector.range(0, Rules.totalInvaders()) |> Vector.map(index -> {letcolumn = Int32.remainder(index, Rules.columns());letrow = index/Rules.columns();letfx = Rules.formationLeft() +Int32.toFloat32(column) *Rules.columnSpacing();letfy = Rules.formationTopFor(level) +Int32.toFloat32(row) *Rules.rowSpacing(); {pos = {x = fx, y = fy}, alive = true, kind = Rules.kindForRow(row), row = row} })////// A fresh game. Pure, and identical for the same seed.///pubdefinitial(seed: Int64): World = { phase = Phase.Playing, tick = 0, playerX = Rules.fieldWidth() /2.0f32, bullets = Nil, bombs = Nil, invaders = formation(1), bunkers = Bunkers.initial(), marchDir = 1.0f32, score = 0, lives = Rules.startingLives(), rng = Rng.fromSeed(seed), seed = seed, hiScore = 0, level = 1, pause = 0, blasts = Nil, mystery = None, mysteryTimer = Rules.mysteryInterval(), shield = 0, nextBonusAt = Rules.bonusLifeScore(), marchNote = 0, shotsFired = 0, marchedSince = 0.0f32 }defclampTo(lo: Float32, hi: Float32, v: Float32): Float32 =if (v<lo) loelseif (v>hi) hielsev/// Plays `cmd` and returns `w` unchanged, so it drops into a pipeline stage.defemit(cmd: SoundCmd, w: World): World \ Sound = {Sound.play(cmd);w }// -- pipeline stages --------------------------------------------------------------/// Left and right move the player, who cannot leave the field.defmovePlayer(input: Snapshot, w: World): World = {letheld = key -> Set.memberOf(key, input#held);letleft = if (held(Key.Left)) -Rules.playerSpeed() else0.0f32;letright = if (held(Key.Right)) Rules.playerSpeed() else0.0f32;lethalf = Rules.playerSize()#x /2.0f32;letmoved = w#playerX +left+right; {playerX = clampTo(half, Rules.fieldWidth() -half, moved) | w} }////// Space fires, if the previous shot has cleared the screen.////// There is no timer: the rate of fire is whatever the last shot's flight allows, which/// is the original's rule and the reason accuracy matters. Holding the key down refires/// the instant the screen is clear, so nothing is lost by holding it.///deffire(input: Snapshot, w: World): World \ Sound =if (List.length(w#bullets) >=Rules.shotsAloft())welseif (notSet.memberOf(Key.Space, input#held))welse {letbarrel = Rules.playerY() -Rules.playerSize()#y;letmuzzle = {x = w#playerX, y = barrel};emit(SoundCmd.Shoot, { bullets = muzzle :: w#bullets, shotsFired = w#shotsFired +1 | w }) }/// Bullets rise and are dropped when they leave the top of the field.defmoveBullets(w: World): World = { bullets = w#bullets |> List.map(b -> {y = b#y -Rules.bulletSpeed() | b}) |> List.filter(b -> b#y >-Rules.bulletSize()#y) | w }////// Bombs fall and are dropped when they leave the bottom of the field.////// Each kind falls at its own speed, and the wiggler also sways: its horizontal step/// reverses every `bombWiggleTicks`, which is a triangle wave written as a sign flip./// The sway is clamped to the field so a wiggler dropped by an invader at the wall/// cannot walk off the edge and fall where nothing can be hit.///defmoveBombs(w: World): World = {lethalf = Rules.bombSize()#x /2.0f32;letfloor = Rules.fieldHeight() +Rules.bombSize()#y;letsway =b ->if (b#kind != BombKind.Wiggly)0.0f32elseif (Int32.remainder(b#age /Rules.bombWiggleTicks(), 2) ==0)Rules.bombWiggleSpeed()else-Rules.bombWiggleSpeed(); { bombs = w#bombs |> List.map(b -> { x = clampTo(half, Rules.fieldWidth() -half, b#x +sway(b)), y = b#y +Rules.bombSpeedFor(b#kind), age = b#age +1 | b }) |> List.filter(b -> b#y <floor) | w } }/// The x of the leftmost and rightmost living invader, if any are left.defliveBounds(invaders: Vector[Invader]): Option[(Float32, Float32)] = {letupdateBound = (acc, inv) ->if (notinv#alive)accelsematchacc {case None => Some((inv#pos#x, inv#pos#x))case Some((lo, hi)) => Some((Float32.min(lo, inv#pos#x), Float32.max(hi, inv#pos#x))) };Vector.foldLeft(updateBound, None, invaders) }////// The formation marches sideways, and drops a row when it reaches a wall.////// The bounds come from the *living* invaders only, so clearing a whole outer column/// lets the rest march further, exactly as the arcade original does.///defmarchInvaders(w: World): World \ Sound = {lethalf = Rules.invaderSize()#x /2.0f32;matchliveBounds(w#invaders) {case None => wcase Some((lo, hi)) => {letliving = Vector.count(inv -> inv#alive, w#invaders);letstepX = w#marchDir *Rules.marchSpeedFor(living, w#level);// The bassline advances by however far the formation just moved, so its// tempo tracks the speed-up without a separate timer.letsounded = beat(Float32.abs(stepX), w);letrightWall = Rules.fieldWidth() -half;lethitsWall = (lo+stepX) <halfor (hi+stepX) >rightWall;if (hitsWall)letupdatedInvaders = Vector.map(inv -> { pos = {y = inv#pos#y +Rules.marchDrop() | inv#pos} | inv },sounded#invaders ); {marchDir = -sounded#marchDir, invaders = updatedInvaders | sounded}elseletupdatedInvaders = Vector.map(inv -> { pos = {x = inv#pos#x +stepX | inv#pos} | inv },sounded#invaders ); {invaders = updatedInvaders | sounded} } } }////// Advances the marching bassline by `distance`, sounding a note when one is due.///defbeat(distance: Float32, w: World): World \ Sound = {lettotal = w#marchedSince +distance;if (total<Rules.marchNoteDistance()) {marchedSince = total | w}elseemit(SoundCmd.MarchStep(w#marchNote), { marchedSince = total-Rules.marchNoteDistance(), marchNote = Int32.remainder(w#marchNote +1, SoundCmd.marchNotes()) | w }) }/// The lowest living invader in each column is the only one that can drop a bomb.defbombers(invaders: Vector[Invader]): List[Invader] =Vector.foldLeft( (acc, inv) ->if (notinv#alive)accelse {letbelow = other -> other#pos#x ==inv#pos#x andother#pos#y >inv#pos#y;letcovered = List.exists(below, acc);if (covered) accelseinv :: List.filter(other -> other#pos#x !=inv#pos#x, acc) }, Nil,invaders )defplaceBomb(inv: Invader, rng: Rng, w: World): World = {let (kind, afterKind) = Rules.bombKindFrom(rng);letbomb = {x = inv#pos#x, y = inv#pos#y +Rules.invaderSize()#y, kind = kind, age = 0}; {bombs = bomb :: w#bombs, rng = afterKind | w} }////// Helper to spawn a bomb when chance succeeds.///defspawnBomb(candidates: List[Invader], rng: Rng, w: World): World = {letcount = List.length(candidates);if (count==0) {rng = rng | w}else {let (pick, afterPick) = Rng.nextInt32Below(count, rng);matchList.nth(pick, candidates) {case None => {rng = afterPick | w}case Some(inv) => placeBomb(inv, afterPick, w) } } }////// Occasionally, one of them drops a bomb.////// The cap is checked before the die is rolled, so a formation already at three bombs/// does not silently consume randomness -- which would make the generator's position/// depend on how crowded the screen was, and two replays of the same input diverge.///definvaderFire(w: World): World =if (List.length(w#bombs) >=Rules.maxBombs())welse {let (drops, afterChance) = Rng.chance(Rules.bombChanceFor(w#level), w#rng);if (notdrops) {rng = afterChance | w}elsespawnBomb(bombers(w#invaders), afterChance, w) }////// What one pass over the bombs carries.////// A named record rather than a tuple, so that the fold below reads as what it does to/// each field instead of as three values kept in the right order by hand. `kept` is/// newest-first, as an accumulated list is, and reversed once at the end.///typealiasShotPass = {bullets = List[Vec2], rng = Rng, kept = List[Bomb]}////// One bomb against the shots still in flight.///defmeetBomb(pass: ShotPass, bomb: Bomb): ShotPass = {letstruck =b ->Collide.overlaps( aPos = b, aSize = Rules.bulletSize(), bPos = {x = bomb#x, y = bomb#y}, bSize = Rules.bombSize() );if (notList.exists(struck, pass#bullets)) {kept = bomb :: pass#kept | pass}else {// The shot is spent either way; only the bomb's fate is a die roll.let (survives, next) = Rng.chance(Rules.bombSurvivesShot(), pass#rng); { bullets = List.filter(b -> notstruck(b), pass#bullets), rng = next, kept = if (survives) bomb :: pass#kept elsepass#kept } } }////// Shots meet bombs in mid-air.////// Asymmetric, as in the original: the cannon's shot is always spent, the bomb usually/// but not always dies with it. Runs after the bunkers have had their say, so a shot/// already absorbed by a shelter cannot also knock a bomb down on its way through.////// Folded bomb-first rather than bullet-first so that one shot cannot clear two bombs:/// each bullet is removed the moment it meets anything.///defresolveShotsMeetBombs(w: World): World = {letinitPass = { bullets = w#bullets, rng = w#rng, kept = Nil };letpass = List.foldLeft(meetBomb, initPass, w#bombs); {bombs = List.reverse(pass#kept), bullets = pass#bullets, rng = pass#rng | w} }/// The index of the first living invader this bullet hits.defhitIndex(bullet: Vec2, invaders: Vector[Invader]): Option[Int32] =Vector.findIndexOfLeft(inv ->inv#aliveandCollide.overlaps( aPos = bullet, aSize = Rules.bulletSize(), bPos = inv#pos, bSize = Rules.invaderSize() ),invaders )////// What one pass over the bullets carries: the shots that missed, and what the hits did.///typealiasBulletPass = { kept = List[Vec2], invaders = Vector[Invader], score = Int32, blasts = List[Blast] }////// One bullet against the formation.////// A bullet that hits is gone -- it is not added to `kept` -- which is what stops one shot/// clearing two invaders: the fold never offers it to a second one.///defmeetInvader(pass: BulletPass, bullet: Vec2): BulletPass =matchhitIndex(bullet, pass#invaders) {case None => {kept = bullet :: pass#kept | pass}case Some(i) => {lethit = Vector.get(i, pass#invaders);letstruck = {alive = false | hit};letblast = { pos = hit#pos, ticksLeft = Rules.invaderBlastTicks(), kind = BlastKind.InvaderBlast }; { invaders = Vector.update(i, struck, pass#invaders), score = pass#score +Rules.pointsFor(hit#kind), blasts = blast :: pass#blasts | pass } } }////// Bullets meet invaders.////// A bullet is spent on the first invader it hits, so one shot can never clear two.///defresolveBullets(w: World): World \ Sound = {letstart = {kept = Nil, invaders = w#invaders, score = w#score, blasts = w#blasts};letpass = List.foldLeft(meetInvader, start, w#bullets);letkilled = List.length(pass#blasts) -List.length(w#blasts);// One noise per kill: two bullets landing in one tick must be heard twice.List.forEach(_ -> Sound.play(SoundCmd.InvaderDies), List.range(0, killed)); { bullets = List.reverse(pass#kept), invaders = pass#invaders, score = pass#score, blasts = pass#blasts | w } }////// Bombs meet the player.////// A hit costs one life, clears the bombs on screen, leaves wreckage, and freezes the/// game for a beat -- being blown up should be something you watch, not something you/// only notice from the life counter.///defresolveBombs(w: World): World \ Sound = {letplayer = {x = w#playerX, y = Rules.playerY()};letoverlapsPlayer =b ->Collide.overlaps( aPos = {x = b#x, y = b#y}, aSize = Rules.bombSize(), bPos = player, bSize = Rules.playerSize() );lethit = List.exists(overlapsPlayer, w#bombs);if (nothit)welseif (w#shield >0)// The shield takes it. The bombs on screen are still cleared, so a shielded hit// buys breathing room as well as a life.emit(SoundCmd.ShieldBlock, {bombs = Nil | w})else {letwreck = { pos = player, ticksLeft = Rules.playerBlastTicks(), kind = BlastKind.PlayerBlast };emit(SoundCmd.PlayerDies, { lives = w#lives -1, bombs = Nil, blasts = wreck :: w#blasts, pause = Rules.deathPauseTicks() | w }) } }/// Half the mystery ship's width -- how far off-screen it spawns and despawns.defmysteryMargin(): Float32 = Rules.mysterySize()#x /2.0f32+2.0f32defstepActiveMystery(m: Mystery, w: World): World = {letx = m#x +m#dir *Rules.mysterySpeed();if (x<-mysteryMargin() orx>Rules.fieldWidth() +mysteryMargin()) {mystery = None | w}else {mystery = Some({x = x | m}) | w} }////// Whether the formation has been thinned past the point where a saucer is welcome.////// See `Rules.mysteryMinInvaders`: the last few invaders race, and a bonus dangled then/// arrives at the worst possible moment.///defformationTooThin(w: World): Bool =Vector.count(inv -> inv#alive, w#invaders) <=Rules.mysteryMinInvaders()////// Where a saucer entering from one side starts, and which way it then flies.////// One answer rather than two: the x and the direction are the same decision, and asking/// the same question twice is how they drift apart into a ship flying off its own edge./// It starts a margin clear of the screen so it slides in rather than appearing whole.///defmysteryEntry(fromLeft: Bool): Mystery =if (fromLeft) {x = -mysteryMargin(), dir = 1.0f32}else {x = Rules.fieldWidth() +mysteryMargin(), dir = -1.0f32}////// Launches a saucer from a randomly chosen side and sets the wait for the next one.////// The generator is threaded through both draws and returned in the world, so the whole/// sequence -- side, then jitter -- replays exactly from the same seed.///deflaunchMystery(w: World): World \ Sound = {let (fromLeft, afterSide) = Rng.chance(2, w#rng);let (jitter, next) = Rng.nextInt32Below(Rules.mysterySpread(), afterSide);emit(SoundCmd.MysteryAppears, { mystery = Some(mysteryEntry(fromLeft)), mysteryTimer = Rules.mysteryInterval() +jitter, rng = next | w }) }////// Counts down to the next saucer, and launches it when the count runs out.///defspawnMystery(w: World): World \ Sound =if (formationTooThin(w))welseif (w#mysteryTimer >1) {mysteryTimer = w#mysteryTimer -1 | w}elselaunchMystery(w)////// Flies the mystery ship across, or counts down to the next one.///defmoveMystery(w: World): World \ Sound = matchw#mystery {case Some(m) => stepActiveMystery(m, w)case None => spawnMystery(w) }////// Bullets meet the mystery ship.////// Runs after the invaders have been resolved: the ship flies above the formation, so a/// bullet has already had its chance at everything below it.///defresolveMystery(w: World): World \ Sound = matchw#mystery {case None => wcase Some(m) => {letpos = {x = m#x, y = Rules.mysteryY()};lethits =b ->Collide.overlaps( aPos = b, aSize = Rules.bulletSize(), bPos = pos, bSize = Rules.mysterySize() );if (notList.exists(hits, w#bullets))welse {letpoints = Rules.mysteryScoreFor(w#shotsFired);letblast = { pos = pos, ticksLeft = Rules.invaderBlastTicks(), kind = BlastKind.InvaderBlast };// The bonus is not only points: it also raises the cannon's shield.emit(SoundCmd.MysteryDies, { mystery = None, bullets = List.filter(b -> nothits(b), w#bullets), score = w#score +points, blasts = blast :: w#blasts, shield = Rules.shieldTicks() | w }) } } }////// Awards an extra life each time the score passes the next threshold.////// Loops rather than awarding once, because a single tick could cross two thresholds --/// shooting the mystery ship for 300 while already near the line, say -- and skipping/// one would be a silent loss.///defawardBonusLife(w: World): World \ Sound =if (w#score <w#nextBonusAt)welse {letraised = {nextBonusAt = w#nextBonusAt +Rules.bonusLifeScore() | w};if (w#lives >=Rules.maxLives())// The threshold still advances, so a capped player does not get a fresh// award on every subsequent point.awardBonusLife(raised)elseawardBonusLife(emit(SoundCmd.ExtraLife, {lives = raised#lives +1 | raised})) }/// Counts the shield down. It runs on wall time, not on hits.defageShield(w: World): World = {shield = Int32.max(0, w#shield -1) | w}////// Ages every blast by a tick and drops the ones that have burnt out.///defageBlasts(w: World): World = { blasts = w#blasts |> List.map(b -> {ticksLeft = b#ticksLeft -1 | b}) |> List.filter(b -> b#ticksLeft >0) | w }/// Whether any living invader has reached the player's line.definvasionComplete(invaders: Vector[Invader]): Bool =Vector.exists(inv -> inv#alive andinv#pos#y >=Rules.invasionLine(), invaders)////// Shots meet the bunkers, before anything else can be hit.////// Order matters: this runs after the shots have moved but before they are checked/// against invaders and the player, so a bunker genuinely stops a shot rather than/// merely being drawn in its way.///defresolveBunkers(w: World): World = {let (afterBullets, bullets) =Bunkers.absorbAll(w#bunkers, w#bullets, Rules.bulletSize(), Rules.bulletCrater());let (afterBombs, bombs) = Bunkers.absorbBombs(afterBullets, w#bombs);letfinalBunkers = Bunkers.erode(afterBombs, w#invaders); {bunkers = finalBunkers, bullets = bullets, bombs = bombs | w} }/// The hi-score is the best score seen so far.deftrackHiScore(w: World): World = if (w#score >w#hiScore) {hiScore = w#score | w} elsew////// Decides whether the formation is cleared, or the game is lost.////// Clearing is not winning: it starts a pause, after which a harder level begins.///defcheckEnd(w: World): World =if (notVector.exists(inv -> inv#alive, w#invaders)) { phase = Phase.Cleared, pause = Rules.levelPauseTicks(), bullets = Nil, bombs = Nil | w }elseif (w#lives <=0orinvasionComplete(w#invaders)) {phase = Phase.Lost | w}elsew////// The next level: a fresh formation and fresh bunkers, marching faster and dropping/// more bombs. Score, lives and the generator carry over.///defnextLevel(w: World): World = { phase = Phase.Playing, level = w#level +1, pause = 0,// Each wave starts lower than the last until the cycle resets, so a later wave is// not merely faster -- it is shorter. invaders = formation(w#level +1), bunkers = Bunkers.initial(), bullets = Nil, bombs = Nil, marchDir = 1.0f32, blasts = Nil, mystery = None, mysteryTimer = Rules.mysteryInterval(), marchNote = 0,// The shot counter is per board, as the saucer's value is. shotsFired = 0, marchedSince = 0.0f32, playerX = Rules.fieldWidth() /2.0f32 | w }/// One entry of a digest: `|label(field,field,...)`.defgroup(label: String, fields: List[String]): String ="|${label}("+String.intercalate(",", fields) +")"/// Every element rendered and run together: a digest has no separator between entries.defeachOf(show: a -> String, xs: List[a]): String =List.foldLeft((acc, x) -> acc+show(x), "", xs)/// The same, for the two collections the world keeps in a `Vector`.defeachIn(show: a -> String, xs: Vector[a]): String =Vector.foldLeft((acc, x) -> acc+show(x), "", xs)////// The world's own fields, in a fixed order: phase, tick, cannon, score, lives, march,/// best, level, pause, shield, bonus, shots, generator.///defheader(w: World): String = {letstage = "${w#phase}|t=${w#tick}";letcannon = "|x=${w#playerX}|s=${w#score}";letlives = "|l=${w#lives}|d=${w#marchDir}";letbest = "|h=${w#hiScore}|v=${w#level}";letheld = "|p=${w#pause}|g=${w#shield}";letbonus = "|n=${w#nextBonusAt}|f=${w#shotsFired}";letseed = "|r=${w#rng}";stage+cannon+lives+best+held+bonus+seed }/// One shot in flight: `|b(x,y)`.defshowShot(b: Vec2): String = {letat = "${b#x}" :: "${b#y}" :: Nil;group("b", at) }/// One bomb: `|m(kind,x,y,age)`.defshowBomb(b: Bomb): String = {letat = "${b#x}" :: "${b#y}" :: Nil;letfields = "${b#kind}" :: at:::"${b#age}" :: Nil;group("m", fields) }/// One invader: where it is, or `|.` for a gap where one used to be.defshowInvader(inv: Invader): String = {letat = "${inv#pos#x}" :: "${inv#pos#y}" :: Nil;if (inv#alive) group("i", at) else"|." }/// One bunker, as the number of blocks it has left.defshowShelter(bunker: Bunker): String = "|k${Bunkers.remaining(bunker)}"/// One explosion: `|e(kind,x,y,ticksLeft)`.defshowBlast(b: Blast): String = {letat = "${b#pos#x}" :: "${b#pos#y}" :: Nil;letfields = "${b#kind}" :: at:::"${b#ticksLeft}" :: Nil;group("e", fields) }/// The saucer: where it is, or how long until the next one.defshowMystery(w: World): String = matchw#mystery {case None => "|u-${w#mysteryTimer}"case Some(m) => group("u", "${m#x}" :: "${m#dir}" :: Nil) }////// An exact, comparable rendering of a world.////// Flix records do not derive `Eq`, and comparing worlds is what the replay tests do, so/// the game provides this instead. It is deliberately exact rather than rounded -- two/// runs of the same code on the same machine produce bit-identical floats, and IEEE/// arithmetic is deterministic across machines too, so there is nothing to be gained by/// blurring it. A digest also gives a readable diff when a replay does drift.///pubdefdigest(w: World): String =header(w)+eachOf(showShot, w#bullets)+eachOf(showBomb, w#bombs)+eachIn(showInvader, w#invaders)+eachIn(showShelter, w#bunkers)+eachOf(showBlast, w#blasts)+showMystery(w)// -- the step ---------------------------------------------------------------------defstepPlaying(w: World, input: Snapshot): World \ Sound =if (w#pause >0) {letcountDown = n -> {pause = n#pause -1, tick = n#tick +1 | n};w |> ageBlasts |> countDown } elsew |> movePlayer(input) |> fire(input) |> moveBullets |> moveBombs |> marchInvaders |> moveMystery |> invaderFire// Ages before anything can grant a shield, so a shield lasts exactly its// full duration rather than losing its first tick to the grant itself. |> ageShield |> resolveBunkers |> resolveShotsMeetBombs |> resolveBullets |> resolveMystery |> resolveBombs |> awardBonusLife |> ageBlasts |> trackHiScore |> checkEnd |> (next -> {tick = next#tick +1 | next})////// Advances the game by exactly one logical tick.////// Pure. Given the same world and the same input, always the same result.///pubdefstep(w: World, input: Snapshot): World \ Sound = {matchw#phase {case Phase.Playing => stepPlaying(w, input)case Phase.Cleared =>// A beat to see the field cleared, then the next level.if (w#pause >1) {pause = w#pause -1 | w}elsenextLevel(w)case Phase.Lost =>// Enter starts a new game. Nothing else happens once it is over, so a lost world// is stable no matter how many steps run. The hi-score outlives the game, the// way an arcade cabinet's does.if (Set.memberOf(Key.Enter, input#pressed)) {hiScore = w#hiScore | initial(w#seed)}elsew } }}