| 1 | // Runner for the Swift pet core — same contract as run-tape.ts / rs/main.rs. |
| 2 | // swiftc -O PetSim.swift main.swift -o petsim |
| 3 | // ./petsim — run the tape, print digests |
| 4 | // ./petsim --render N out.png — CoreGraphics raster of tape frame N |
| 5 | import Foundation |
| 6 | import CoreGraphics |
| 7 | import ImageIO |
| 8 | import UniformTypeIdentifiers |
| 9 | |
| 10 | func findFile(_ name: String) -> String { |
| 11 | for p in [name, "pet/\(name)", "swift/../\(name)", "../\(name)"] { |
| 12 | if FileManager.default.fileExists(atPath: p) { return p } |
| 13 | } |
| 14 | fatalError("\(name) not found") |
| 15 | } |
| 16 | func loadPoints() -> [(Double, Double)] { |
| 17 | (try! String(contentsOfFile: findFile("whale-points.tsv"), encoding: .utf8)) |
| 18 | .trimmingCharacters(in: .whitespacesAndNewlines) |
| 19 | .split(separator: "\n").map { l in |
| 20 | let c = l.split(separator: "\t").map { Double($0)! } |
| 21 | return (c[0], c[1]) |
| 22 | } |
| 23 | } |
| 24 | func loadTape() -> [(Double, PetState)] { |
| 25 | (try! String(contentsOfFile: tapePath, encoding: .utf8)) |
| 26 | .split(separator: "\n").compactMap { line -> (Double, PetState)? in |
| 27 | let c = line.split(separator: "\t") |
| 28 | if c.count < 10 || c[0] == "dt" { return nil } |
| 29 | var st = PetState() |
| 30 | st.activity = Double(c[1])!; st.coherence = Double(c[2])!; st.attention = Double(c[3])! |
| 31 | st.channel = String(c[4]); st.observed = Double(c[5])! |
| 32 | st.roamX = Double(c[6])!; st.roamY = Double(c[7])!; st.flip = Double(c[8])!; st.lit = Double(c[9])! |
| 33 | return (Double(c[0])!, st) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /// The same draw the SwiftUI view performs: petLayout() + one ellipse per |
| 38 | /// particle, fill or stroke depending on frame.hollow. |
| 39 | func renderPng(sim: PetSim, st: PetState, path: String, W: Int = 900, H: Int = 420) { |
| 40 | let space = CGColorSpaceCreateDeviceRGB() |
| 41 | let ctx = CGContext(data: nil, width: W, height: H, bitsPerComponent: 8, bytesPerRow: 0, |
| 42 | space: space, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)! |
| 43 | ctx.setFillColor(CGColor(red: 0.024, green: 0.031, blue: 0.051, alpha: 1)) |
| 44 | ctx.fill(CGRect(x: 0, y: 0, width: W, height: H)) |
| 45 | let lay = petLayout(w: Double(W), h: Double(H), state: st) |
| 46 | let f = sim.frame |
| 47 | let color = CGColor(red: f.r / 255, green: f.g / 255, blue: f.b / 255, alpha: f.alpha) |
| 48 | ctx.setFillColor(color); ctx.setStrokeColor(color); ctx.setLineWidth(1) |
| 49 | for q in sim.p { |
| 50 | let px = lay.ox + q.x * lay.scale * lay.flipX |
| 51 | let py = Double(H) - (lay.oy + q.y * lay.scale) // CG y is bottom-up |
| 52 | let rect = CGRect(x: px - lay.dot / 2, y: py - lay.dot / 2, width: lay.dot, height: lay.dot) |
| 53 | if f.hollow { ctx.strokeEllipse(in: rect) } else { ctx.fillEllipse(in: rect) } |
| 54 | } |
| 55 | let img = ctx.makeImage()! |
| 56 | let dest = CGImageDestinationCreateWithURL(URL(fileURLWithPath: path) as CFURL, UTType.png.identifier as CFString, 1, nil)! |
| 57 | CGImageDestinationAddImage(dest, img, nil) |
| 58 | CGImageDestinationFinalize(dest) |
| 59 | print("wrote \(path) · \(f.channel) · \(f.arch)\(f.hollow ? " · unobserved" : "")") |
| 60 | } |
| 61 | |
| 62 | let args = CommandLine.arguments |
| 63 | let motion = !args.contains("--reduced-motion") |
| 64 | let tapePath = args.firstIndex(of: "--tape").map { args[$0 + 1] } ?? findFile("tape.tsv") |
| 65 | let sim = PetSim(points: loadPoints(), expressionVersion: args.contains("--legacy") ? 1 : 2) |
| 66 | |
| 67 | if args.count > 1, args[1] == "--render" { |
| 68 | let target = Int(args[2])! |
| 69 | let out = args.count > 3 ? args[3] : "frame.png" |
| 70 | var st = PetState() |
| 71 | for (i, (dt, s)) in loadTape().enumerated() { |
| 72 | sim.step(dt: dt, state: s, motion: motion) |
| 73 | st = s |
| 74 | if i == target { break } |
| 75 | } |
| 76 | renderPng(sim: sim, st: st, path: out) |
| 77 | } else { |
| 78 | var f = 0 |
| 79 | for (dt, st) in loadTape() { |
| 80 | sim.step(dt: dt, state: st, motion: motion) |
| 81 | if f % 30 == 0 { |
| 82 | print(String(format: "f%04d %@ %@", f, petDigest(sim), st.channel)) |
| 83 | } |
| 84 | f += 1 |
| 85 | } |
| 86 | print("final \(petDigest(sim))") |
| 87 | } |
| 88 |