SPB Git

Repository Public License

DominoLevels.swift 18.1 KB 373 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Arcade9// File             : Games/DominoCity/DominoLevels.swift10// Description      : Domino City — the level builder and the fifteen11//                    handcrafted levels with their reference solutions.12// Module           : Games13// Repository       : https://git.spboucher.ai/orix-arcade14// Created          : 2026-11-2115// Last Modified    : 2026-11-2116//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author           : Simon-Pierre Boucher22// Position         : Professor of Finance23// Institution      : Université du Québec en Outaouais (UQO)24// Department       : Département des sciences administratives25// Organization     : SP Boucher26//27// Website          : https://spboucher.ai28// Git              : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General          : info@spboucher.ai35// Professional     : contact@spboucher.ai36// Direct           : simon@spboucher.ai37// Research         : research@spboucher.ai38// Development      : dev@spboucher.ai39// Support          : support@spboucher.ai40// Security/Admin   : admin@spboucher.ai41// Notifications    : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification   : Public68// Distribution     : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License          : Proprietary, all rights reserved (see license file)82// License File     : LICENSE83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation9192/// Small layout DSL: everything is placed relative to the surface it stands on, in stage units.93nonisolated struct DominoLevelBuilder {94    private(set) var pieces: [DominoPiece] = []95    private var nextID = 196    typealias Point = (x: Double, y: Double)9798    @discardableResult99    private mutating func add(_ kind: DominoPieceKind, x: Double, y: Double, w: Double, h: Double, angle: Double = 0, held: Bool = false,100                              links: [Int] = [], anchor: Point = (0, 0)) -> Int {101        let id = nextID102        nextID += 1103        pieces.append(DominoPiece(id: id, kind: kind, x: x, y: y, w: w, h: h, angle: angle, links: links, held: held, anchorX: anchor.x, anchorY: anchor.y))104        return id105    }106107    /// Flat static slab whose top surface is at `top`.108    mutating func ledge(_ x1: Double, _ x2: Double, top: Double, thickness: Double = 14) {109        add(.ledge, x: (x1 + x2) / 2, y: top - thickness / 2, w: abs(x2 - x1), h: thickness)110    }111112    /// Vertical static wall.113    mutating func wall(x: Double, bottom: Double, top: Double, thickness: Double = 12) {114        add(.ledge, x: x, y: (bottom + top) / 2, w: thickness, h: top - bottom)115    }116117    private func rampGeometry(_ a: Point, _ b: Point) -> (length: Double, angle: Double, normal: Point) {118        let dx = b.x - a.x, dy = b.y - a.y119        let length = (dx * dx + dy * dy).squareRoot()120        var normal: Point = (-dy / length, dx / length)121        if normal.y < 0 { normal = (-normal.x, -normal.y) }122        return (length, atan2(dy, dx) * 180 / .pi, normal)123    }124125    /// Static slope whose top surface runs from `a` to `b`.126    mutating func ramp(from a: Point, to b: Point, thickness: Double = 10) {127        let g = rampGeometry(a, b)128        add(.ledge, x: (a.x + b.x) / 2 - g.normal.x * thickness / 2, y: (a.y + b.y) / 2 - g.normal.y * thickness / 2, w: g.length, h: thickness, angle: g.angle)129    }130131    /// A row of dominoes standing on `top`. `step` may be negative to build leftwards; `gold` lists the indices that are targets.132    mutating func dominoes(from x: Double, top: Double, count: Int, step: Double = DominoTuning.dominoSpacing, gold: Set<Int> = []) {133        for index in 0..<count {134            add(gold.contains(index) ? .goldDomino : .domino, x: x + Double(index) * step, y: top + DominoTuning.dominoHeight / 2,135                w: DominoTuning.dominoWidth, h: DominoTuning.dominoHeight)136        }137    }138139    /// A single-column tower of crates; the top crate can be a target.140    mutating func tower(x: Double, top: Double, floors: Int, goldTop: Bool = true) {141        for floor in 0..<floors {142            add(goldTop && floor == floors - 1 ? .goldBlock : .block, x: x, y: top + DominoTuning.blockHeight * (Double(floor) + 0.5),143                w: DominoTuning.blockWidth, h: DominoTuning.blockHeight)144        }145    }146147    @discardableResult148    mutating func ball(x: Double, top: Double, radius: Double = 13, held: Bool = false) -> Int {149        add(.ball, x: x, y: top + radius, w: radius * 2, h: radius * 2, held: held)150    }151152    /// A ball parked on the slope `a`→`b` at horizontal position `x`. It stays put until a switch frees it or,153    /// when no switch is linked to it, until anything touches it.154    @discardableResult155    mutating func ball(onRampFrom a: Point, to b: Point, atX x: Double, radius: Double = 13, held: Bool = true) -> Int {156        let g = rampGeometry(a, b)157        let t = (x - a.x) / (b.x - a.x)158        let surface: Point = (x, a.y + (b.y - a.y) * t)159        return add(.ball, x: surface.x + g.normal.x * radius, y: surface.y + g.normal.y * radius, w: radius * 2, h: radius * 2, held: held)160    }161162    mutating func plank(_ x1: Double, _ x2: Double, top: Double, thickness: Double = 8) {163        add(.plank, x: (x1 + x2) / 2, y: top + thickness / 2, w: abs(x2 - x1), h: thickness)164    }165166    mutating func bell(x: Double, y: Double) { add(.bell, x: x, y: y, w: 34, h: 34) }167    mutating func gem(x: Double, y: Double) { add(.gem, x: x, y: y, w: 22, h: 22) }168169    mutating func switchPad(x: Double, y: Double, links: [Int]) { add(.switchPad, x: x, y: y, w: 32, h: 32, links: links) }170171    @discardableResult172    mutating func gate(x: Double, bottom: Double, top: Double, thickness: Double = 12) -> Int {173        add(.gate, x: x, y: (bottom + top) / 2, w: thickness, h: top - bottom)174    }175176    mutating func tar(_ x1: Double, _ x2: Double, top: Double) {177        add(.tar, x: (x1 + x2) / 2, y: top + 4, w: abs(x2 - x1), h: 12)178    }179180    mutating func bouncer(x: Double, top: Double, width: Double = 60) {181        add(.bouncer, x: x, y: top + 5, w: width, h: 10)182    }183184    /// Crane ball on a rigid cable. `angle` is measured from straight down; negative swings it up to the left.185    @discardableResult186    mutating func wreckingBall(anchor: Point, length: Double, angle: Double, radius: Double = 20, held: Bool = true) -> Int {187        let radians = angle * .pi / 180188        return add(.wreckingBall, x: anchor.x + sin(radians) * length, y: anchor.y - cos(radians) * length, w: radius * 2, h: radius * 2,189                   held: held, anchor: anchor)190    }191}192193nonisolated enum DominoLevels {194    static let ground = DominoTuning.groundTop195196    static let all: [DominoLevel] = [197        firstPush, rooftopRun, towerBlock, rollOut, switchyard, bridgeOut, tarPit, bounceHouse,198        craneDay, twoBirds, cascade, gatekeeper, skylineRelay, demolitionRow, grandFinale,199    ]200201    static func level(_ id: Int) -> DominoLevel? { all.first { $0.id == id } }202203    private static func make(_ id: Int, _ name: String, hint: String, launcher: (Double, Double), impulses: Int, solution: [DominoShot],204                             referenceChain: Int, _ build: (inout DominoLevelBuilder) -> Void) -> DominoLevel {205        var builder = DominoLevelBuilder()206        build(&builder)207        return DominoLevel(id: id, name: name, hint: hint, launcherX: launcher.0, launcherY: launcher.1, impulses: impulses, par: solution.count,208                           pieces: builder.pieces, solution: solution, referenceChain: referenceChain)209    }210211    // 1 — tutorial: one straight row.212    static let firstPush = make(1, "First Push", hint: "Drag back anywhere, aim at the first tower, let go.", launcher: (50, 100), impulses: 3,213                                solution: [.init(angle: 0, power: 0.4)], referenceChain: 8) { b in214        b.dominoes(from: 150, top: ground, count: 8, gold: [7])215    }216217    // 2 — rolling ball and ramp carry the chain down a floor.218    static let rooftopRun = make(2, "Rooftop Run", hint: "The ball carries the chain down to the street.", launcher: (30, 335), impulses: 3,219                                 solution: [.init(angle: 0, power: 0.4)], referenceChain: 13) { b in220        b.ledge(60, 280, top: 300)221        b.dominoes(from: 92, top: 300, count: 7)222        b.ramp(from: (280, 300), to: (302, 290))223        b.ball(onRampFrom: (280, 300), to: (302, 290), atX: 291)224        b.ramp(from: (400, 235), to: (250, 165))225        b.dominoes(from: 150, top: ground, count: 5, step: -26, gold: [4])226        b.gem(x: 318, y: 262)227    }228229    // 3 — towers: hit high to topple.230    static let towerBlock = make(3, "Tower Block", hint: "Lob it high: knock the golden crates off both towers.", launcher: (50, 110), impulses: 3,231                                 solution: [.init(angle: 30, power: 0.6), .init(angle: 20, power: 0.7)], referenceChain: 9) { b in232        b.tower(x: 240, top: ground, floors: 6)233        b.tower(x: 330, top: ground, floors: 4)234        b.gem(x: 285, y: 230)235    }236237    // 4 — nudge a heavy ball onto a ramp.238    static let rollOut = make(4, "Roll Out", hint: "Nudge the big ball. Gravity does the rest.", launcher: (26, 250), impulses: 2,239                              solution: [.init(angle: 0, power: 0.3)], referenceChain: 6) { b in240        b.ledge(0, 110, top: 230)241        b.ball(x: 80, top: 230, radius: 16)242        b.ramp(from: (110, 230), to: (235, 45))243        b.ledge(250, 400, top: 150)244        b.dominoes(from: 262, top: ground, count: 5, gold: [4])245    }246247    // 5 — first switch: frees a held ball.248    static let switchyard = make(5, "Switchyard", hint: "Hit the bullseye to free the ball upstairs.", launcher: (40, 90), impulses: 2,249                                 solution: [.init(angle: 22, power: 0.9)], referenceChain: 6) { b in250        let held = b.ball(onRampFrom: (400, 300), to: (215, 225), atX: 372)251        b.ramp(from: (400, 300), to: (215, 225))252        b.ledge(30, 215, top: 170)253        b.dominoes(from: 160, top: 170, count: 5, step: -26, gold: [4])254        b.switchPad(x: 300, y: 110, links: [held])255    }256257    // 6 — plank bridge over a tar pit.258    static let bridgeOut = make(6, "Bridge Out", hint: "The chain crosses the plank. Mind the tar below.", launcher: (26, 255), impulses: 2,259                                solution: [.init(angle: 0, power: 0.4)], referenceChain: 12) { b in260        b.ledge(0, 150, top: 220)261        b.ledge(250, 400, top: 220)262        b.plank(125, 275, top: 220)263        b.dominoes(from: 60, top: 220, count: 3)264        b.dominoes(from: 134, top: 228, count: 6)265        b.dominoes(from: 290, top: 220, count: 3, gold: [2])266        b.tar(150, 250, top: ground)267        b.gem(x: 200, y: 140)268    }269270    // 7 — hazard: tar swallows a rolling shot, a low roof blocks a lazy lob.271    static let tarPit = make(7, "Tar Pit", hint: "Tar swallows the ball. Fly over it.", launcher: (40, 72), impulses: 2,272                             solution: [.init(angle: 10, power: 1)], referenceChain: 4) { b in273        b.tar(110, 235, top: ground)274        b.ledge(120, 250, top: 175)275        b.dominoes(from: 256, top: ground, count: 4, gold: [3])276        b.gem(x: 180, y: 110)277    }278279    // 8 — trampoline.280    static let bounceHouse = make(8, "Bounce House", hint: "Drop the ball on the trampoline to reach the roof.", launcher: (120, 260), impulses: 3,281                                  solution: [.init(angle: 63, power: 0)], referenceChain: 4) { b in282        b.bouncer(x: 185, top: ground)283        b.ledge(245, 400, top: 290)284        b.dominoes(from: 280, top: 290, count: 4, gold: [3])285        b.gem(x: 215, y: 380)286    }287288    // 9 — crane ball released by a switch.289    static let craneDay = make(9, "Crane Day", hint: "Free the crane ball and let it swing.", launcher: (40, 80), impulses: 2,290                               solution: [.init(angle: 25, power: 1)], referenceChain: 5) { b in291        b.ledge(170, 330, top: 180)292        b.tower(x: 250, top: 180, floors: 4)293        let crane = b.wreckingBall(anchor: (230, 480), length: 230, angle: -50)294        b.bell(x: 338, y: 277)295        b.switchPad(x: 350, y: 110, links: [crane])296    }297298    // 10 — two separate objectives, two impulses.299    static let twoBirds = make(10, "Two Birds", hint: "Two rooftops, two golden towers. Make each impulse count.", launcher: (200, 70), impulses: 3,300                               solution: [.init(angle: 105, power: 0.9), .init(angle: 75, power: 0.9)], referenceChain: 6) { b in301        b.ledge(0, 110, top: 200)302        b.dominoes(from: 90, top: 200, count: 3, step: -26, gold: [2])303        b.ledge(290, 400, top: 225)304        b.dominoes(from: 310, top: 225, count: 3, gold: [2])305        b.gem(x: 200, y: 330)306    }307308    // 11 — three-floor zig-zag.309    static let cascade = make(11, "Cascade", hint: "One push, three floors.", launcher: (26, 478), impulses: 2,310                              solution: [.init(angle: 0, power: 0.4)], referenceChain: 22) { b in311        b.ledge(40, 304, top: 440)312        b.dominoes(from: 70, top: 440, count: 9)313        b.ramp(from: (304, 440), to: (328, 429))314        b.ball(onRampFrom: (304, 440), to: (328, 429), atX: 316)315        b.ramp(from: (400, 372), to: (290, 318))316        b.ledge(84, 296, top: 300)317        b.dominoes(from: 262, top: 300, count: 7, step: -26)318        b.ramp(from: (84, 300), to: (60, 289))319        b.ball(onRampFrom: (84, 300), to: (60, 289), atX: 72)320        b.ramp(from: (0, 225), to: (105, 170))321        b.dominoes(from: 170, top: ground, count: 7, gold: [6])322    }323324    // 12 — a gate splits the row; the switch opens it.325    static let gatekeeper = make(12, "Gatekeeper", hint: "Open the gate, or the chain stops dead.", launcher: (40, 90), impulses: 2,326                                 solution: [.init(angle: 47, power: 1)], referenceChain: 9) { b in327        b.dominoes(from: 120, top: ground, count: 5)328        let gate = b.gate(x: 250, bottom: ground, top: 116)329        b.ledge(238, 400, top: 130)330        b.dominoes(from: 276, top: ground, count: 4, gold: [3])331        b.switchPad(x: 175, y: 200, links: [gate])332    }333334    // 13 — trampoline to a switch, switch to a ball, ball to a sheltered row.335    static let skylineRelay = make(13, "Skyline Relay", hint: "Only a bounce reaches that switch.", launcher: (60, 230), impulses: 3,336                                   solution: [.init(angle: 60, power: 0)], referenceChain: 4) { b in337        b.ledge(20, 120, top: 300)338        b.bouncer(x: 150, top: ground)339        let held = b.ball(onRampFrom: (400, 340), to: (316, 300), atX: 380)340        b.ramp(from: (400, 340), to: (316, 300))341        b.ledge(190, 322, top: 290)342        b.wall(x: 196, bottom: 290, top: 358)343        b.ledge(190, 350, top: 372)344        b.dominoes(from: 294, top: 290, count: 3, step: -26, gold: [2])345        b.switchPad(x: 200, y: 420, links: [held])346    }347348    // 14 — three towers, two impulses at par.349    static let demolitionRow = make(14, "Demolition Row", hint: "Three towers, tallest last. Pick your shots.", launcher: (40, 150), impulses: 3,350                                    solution: [.init(angle: -5, power: 0.8)], referenceChain: 14) { b in351        b.tower(x: 150, top: ground, floors: 3)352        b.tower(x: 235, top: ground, floors: 5)353        b.tower(x: 330, top: ground, floors: 6)354        b.gem(x: 285, y: 250)355    }356357    // 15 — everything at once.358    static let grandFinale = make(15, "Grand Finale", hint: "One perfect push brings the whole block down.", launcher: (26, 498), impulses: 2,359                                  solution: [.init(angle: 0, power: 0.4)], referenceChain: 18) { b in360        b.ledge(40, 280, top: 460)361        b.dominoes(from: 70, top: 460, count: 8)362        b.ramp(from: (280, 460), to: (304, 449))363        b.ball(onRampFrom: (280, 460), to: (304, 449), atX: 292)364        let crane = b.wreckingBall(anchor: (170, 400), length: 180, angle: -50)365        b.switchPad(x: 322, y: 400, links: [crane])366        b.ledge(120, 230, top: 130)367        b.tower(x: 185, top: 130, floors: 4)368        b.bell(x: 232, y: 231)369        b.ramp(from: (400, 110), to: (300, 60))370        b.dominoes(from: 270, top: ground, count: 5, step: -26, gold: [4])371    }372}373
Files main
Menu

Theme