Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Arcade9// File : Games/TrafficMayhem/TrafficLayouts.swift10// Description : Traffic Mayhem — the three junction layouts (Four Corners,11// Main Street, Twin Junction): lanes, turn paths and road12// geometry.13// Module : Games14// Repository : https://git.spboucher.ai/orix-arcade15// Created : 2026-11-2116// Last Modified : 2026-11-2117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : Proprietary, all rights reserved (see license file)83// License File : LICENSE84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import Foundation9293nonisolated struct TrafficLane: Sendable {94 let id: Int95 let name: String96 /// Free-flowing through traffic that never stops (the player cannot control it).97 let autonomous: Bool98 let paths: [Int]99 let maxQueue: Int100 /// First difficulty stage at which this lane receives traffic.101 let openStage: Int102}103104nonisolated struct TrafficRect: Sendable, Equatable {105 var minX: Double, minY: Double, maxX: Double, maxY: Double106107 func expanded(_ d: Double) -> TrafficRect { TrafficRect(minX: minX - d, minY: minY - d, maxX: maxX + d, maxY: maxY + d) }108 func intersects(_ o: TrafficRect) -> Bool { minX < o.maxX && maxX > o.minX && minY < o.maxY && maxY > o.minY }109 func contains(_ p: Vec2) -> Bool { p.x >= minX && p.x <= maxX && p.y >= minY && p.y <= maxY }110}111112nonisolated struct TrafficMarking: Sendable {113 enum Kind: Sendable { case centerLine, laneDash, stopLine, crosswalk }114 let kind: Kind115 let from: Vec2116 let to: Vec2117 /// Crosswalk depth (along the direction of travel); unused for lines.118 var depth = 0.0119}120121nonisolated struct TrafficLayout: Sendable {122 let id: Int123 let name: String124 let blurb: String125 let lanes: [TrafficLane]126 let paths: [TrafficPath]127 let roads: [TrafficRect]128 let boxes: [TrafficRect]129 let markings: [TrafficMarking]130 /// Logical point shown at the centre of the screen.131 let focus: Vec2132 /// Vehicles to clear in one run on the previous junction to unlock this one (0 = always open).133 let unlockCleared: Int134 /// Through-traffic rhythm: traffic flows for `period - gap` seconds, then leaves a guaranteed gap.135 let throughPeriod: Double136 let throughGap: Double137 let spawnScale: Double138 let patienceBonus: Double139140 static let laneWidth = 26.0141 static let half = 13.0142 /// Distance from a junction centre to the stop line (junction box 28 + crosswalk 14).143 static let stopDistance = 42.0144145 var controlledLanes: [TrafficLane] { lanes.filter { !$0.autonomous } }146147 static let all: [TrafficLayout] = [fourCorners(), mainStreet(), twinJunction()]148 static func layout(_ id: Int) -> TrafficLayout { all[min(max(id, 0), all.count - 1)] }149150 // MARK: Layout 0 — Four Corners (all-way stop)151152 private static func fourCorners() -> TrafficLayout {153 var paths: [TrafficPath] = []154 var lanes: [TrafficLane] = []155 let names = ["South", "East", "North", "West"]156 for k in 0..<4 {157 let rotation = Double(k) * .pi / 2158 // Approach 0 comes from the south heading north on the right-hand lane; the others are rotations of it.159 let reach = k.isMultiple(of: 2) ? 470.0 : 260.0160 let start = Vec2(x: half, y: -reach).rotated(by: rotation)161 let headingDeg = 90.0 + Double(k) * 90162 var ids: [Int] = []163 for turn in [TrafficTurn.straight, .left, .right] {164 let b = PathBuilder(start: start, headingDegrees: headingDeg)165 b.forward(reach - stopDistance).markStop()166 switch turn {167 case .straight: b.forward(2 * stopDistance).markExit()168 case .left: b.forward(14).turn(.left, radius: 41).forward(14).markExit()169 case .right: b.forward(14).turn(.right, radius: 15).forward(14).markExit()170 }171 b.forward(480)172 let exit = turn == .straight ? (k + 2) % 4 : turn == .left ? (k + 3) % 4 : (k + 1) % 4173 paths.append(b.build(id: paths.count, lane: k, turn: turn, exitLane: exit))174 ids.append(paths.count - 1)175 }176 lanes.append(TrafficLane(id: k, name: names[k], autonomous: false, paths: ids, maxQueue: k.isMultiple(of: 2) ? 4 : 3, openStage: 1))177 }178 let roads = [TrafficRect(minX: -28, minY: -900, maxX: 28, maxY: 900), TrafficRect(minX: -900, minY: -28, maxX: 900, maxY: 28)]179 var markings = crossMarkings(center: .zero)180 markings += [181 .init(kind: .centerLine, from: Vec2(x: 0, y: -900), to: Vec2(x: 0, y: -stopDistance)),182 .init(kind: .centerLine, from: Vec2(x: 0, y: stopDistance), to: Vec2(x: 0, y: 900)),183 .init(kind: .centerLine, from: Vec2(x: -900, y: 0), to: Vec2(x: -stopDistance, y: 0)),184 .init(kind: .centerLine, from: Vec2(x: stopDistance, y: 0), to: Vec2(x: 900, y: 0)),185 ]186 return TrafficLayout(id: 0, name: "Four Corners", blurb: "An all-way stop. Nobody moves until you say so.",187 lanes: lanes, paths: paths, roads: roads, boxes: [TrafficRect(minX: -28, minY: -28, maxX: 28, maxY: 28)],188 markings: markings, focus: .zero, unlockCleared: 0, throughPeriod: 0, throughGap: 0, spawnScale: 1, patienceBonus: 0)189 }190191 /// Stop lines and crosswalks for a four-way crossing centred at `center`.192 private static func crossMarkings(center c: Vec2, arms: [Int] = [0, 1, 2, 3]) -> [TrafficMarking] {193 var result: [TrafficMarking] = []194 for k in arms {195 let rotation = Double(k) * .pi / 2196 func p(_ x: Double, _ y: Double) -> Vec2 { c + Vec2(x: x, y: y).rotated(by: rotation) }197 // Arm 0 is the south arm.198 result.append(.init(kind: .stopLine, from: p(1.5, -stopDistance), to: p(26, -stopDistance)))199 result.append(.init(kind: .crosswalk, from: p(-26, -35), to: p(26, -35), depth: 9))200 }201 return result202 }203204 // MARK: Layout 1 — Main Street (merge into free-flowing traffic)205206 private static func mainStreet() -> TrafficLayout {207 var paths: [TrafficPath] = []208 // Lane 0: side street, left-turn lane (x = 0) → westbound Main Street.209 let a = PathBuilder(start: Vec2(x: 0, y: -470), headingDegrees: 90)210 a.forward(479).markStop().forward(12).turn(.left, radius: 65).markExit().forward(470)211 paths.append(a.build(id: 0, lane: 0, turn: .left, exitLane: 0))212 // Lane 1: side street, right-turn lane (x = 26) → eastbound Main Street.213 let b = PathBuilder(start: Vec2(x: 26, y: -470), headingDegrees: 90)214 b.forward(479).markStop().forward(12).turn(.right, radius: 13).forward(21).markExit().forward(470)215 paths.append(b.build(id: 1, lane: 1, turn: .right, exitLane: 1))216 // Lane 2: Main Street centre turn pocket, arriving from the east → south into the side street.217 let c = PathBuilder(start: Vec2(x: 270, y: 60), headingDegrees: 180)218 c.forward(219).markStop().forward(38).turn(.left, radius: 39).forward(12).markExit().forward(480)219 paths.append(c.build(id: 2, lane: 2, turn: .left, exitLane: 2))220 // Lane 3: eastbound through traffic (y = 34). Lane 4: westbound through traffic (y = 86).221 let e = PathBuilder(start: Vec2(x: -520, y: 34), headingDegrees: 0)222 e.markStop().forward(580).markExit().forward(460)223 paths.append(e.build(id: 3, lane: 3, turn: .straight, exitLane: 1))224 let w = PathBuilder(start: Vec2(x: 520, y: 86), headingDegrees: 180)225 w.markStop().forward(585).markExit().forward(455)226 paths.append(w.build(id: 4, lane: 4, turn: .straight, exitLane: 0))227228 let lanes = [229 TrafficLane(id: 0, name: "Side street, left turn", autonomous: false, paths: [0], maxQueue: 4, openStage: 1),230 TrafficLane(id: 1, name: "Side street, right turn", autonomous: false, paths: [1], maxQueue: 4, openStage: 1),231 TrafficLane(id: 2, name: "Turn pocket", autonomous: false, paths: [2], maxQueue: 3, openStage: 2),232 TrafficLane(id: 3, name: "Eastbound", autonomous: true, paths: [3], maxQueue: 0, openStage: 1),233 TrafficLane(id: 4, name: "Westbound", autonomous: true, paths: [4], maxQueue: 0, openStage: 1),234 ]235 let roads = [TrafficRect(minX: -900, minY: 21, maxX: 900, maxY: 99), TrafficRect(minX: -39, minY: -900, maxX: 39, maxY: 21)]236 let markings: [TrafficMarking] = [237 .init(kind: .laneDash, from: Vec2(x: -900, y: 47), to: Vec2(x: -45, y: 47)),238 .init(kind: .laneDash, from: Vec2(x: 60, y: 47), to: Vec2(x: 900, y: 47)),239 .init(kind: .laneDash, from: Vec2(x: -900, y: 73), to: Vec2(x: -45, y: 73)),240 .init(kind: .laneDash, from: Vec2(x: 60, y: 73), to: Vec2(x: 900, y: 73)),241 .init(kind: .centerLine, from: Vec2(x: -13, y: -900), to: Vec2(x: -13, y: 9)),242 .init(kind: .laneDash, from: Vec2(x: 13, y: -900), to: Vec2(x: 13, y: 9)),243 .init(kind: .stopLine, from: Vec2(x: -11.5, y: 9), to: Vec2(x: 39, y: 9)),244 .init(kind: .crosswalk, from: Vec2(x: -37, y: 15), to: Vec2(x: 37, y: 15), depth: 8),245 .init(kind: .stopLine, from: Vec2(x: 51, y: 48.5), to: Vec2(x: 51, y: 71.5)),246 ]247 return TrafficLayout(id: 1, name: "Main Street", blurb: "Through traffic never stops. Slip into the gaps.",248 lanes: lanes, paths: paths, roads: roads, boxes: [TrafficRect(minX: -45, minY: 21, maxX: 60, maxY: 99)],249 markings: markings, focus: Vec2(x: 0, y: 30), unlockCleared: 25, throughPeriod: 9, throughGap: 4, spawnScale: 1.45, patienceBonus: 3)250 }251252 // MARK: Layout 2 — Twin Junction (one avenue, two cross streets)253254 private static func twinJunction() -> TrafficLayout {255 let yc = 100.0256 var paths: [TrafficPath] = []257 var lanes: [TrafficLane] = []258 func add(_ lane: Int, _ turn: TrafficTurn, _ exitLane: Int, _ builder: PathBuilder) -> Int {259 paths.append(builder.build(id: paths.count, lane: lane, turn: turn, exitLane: exitLane))260 return paths.count - 1261 }262 // Exit lanes: 0 north (top), 1 south (bottom), 2 east-bottom, 3 west-bottom, 4 east-top, 5 west-top.263 let through = 2 * yc + 2 * stopDistance // from one stop line to the far side of the other junction264265 // Lane 0: avenue northbound from the bottom (x = 13).266 func south() -> PathBuilder {267 let b = PathBuilder(start: Vec2(x: half, y: -500), headingDegrees: 90)268 b.forward(500 - yc - stopDistance).markStop()269 return b270 }271 var ids: [Int] = []272 var p = south(); p.forward(through).markExit().forward(480); ids.append(add(0, .straight, 0, p))273 p = south(); p.forward(14).turn(.right, radius: 15).forward(14).markExit().forward(480); ids.append(add(0, .right, 2, p))274 p = south(); p.forward(14).turn(.left, radius: 41).forward(14).markExit().forward(480); ids.append(add(0, .left, 3, p))275 p = south(); p.forward(2 * yc + 14).turn(.left, radius: 41).forward(14).markExit().forward(480); ids.append(add(0, .left, 5, p))276 lanes.append(TrafficLane(id: 0, name: "Avenue, northbound", autonomous: false, paths: ids, maxQueue: 3, openStage: 1))277278 // Lane 1: avenue southbound from the top (x = -13).279 func north() -> PathBuilder {280 let b = PathBuilder(start: Vec2(x: -half, y: 500), headingDegrees: 270)281 b.forward(500 - yc - stopDistance).markStop()282 return b283 }284 ids = []285 p = north(); p.forward(through).markExit().forward(480); ids.append(add(1, .straight, 1, p))286 p = north(); p.forward(14).turn(.right, radius: 15).forward(14).markExit().forward(480); ids.append(add(1, .right, 5, p))287 p = north(); p.forward(14).turn(.left, radius: 41).forward(14).markExit().forward(480); ids.append(add(1, .left, 4, p))288 p = north(); p.forward(2 * yc + 14).turn(.left, radius: 41).forward(14).markExit().forward(480); ids.append(add(1, .left, 2, p))289 lanes.append(TrafficLane(id: 1, name: "Avenue, southbound", autonomous: false, paths: ids, maxQueue: 3, openStage: 1))290291 // Cross streets. `sign` +1 = top street, -1 = bottom street.292 for (index, sign) in [(-1.0), (1.0)].enumerated() {293 let cy = sign * yc294 let eastExit = sign < 0 ? 2 : 4, westExit = sign < 0 ? 3 : 5295 // Eastbound lane (from the west, y = cy - 13).296 func west() -> PathBuilder {297 let b = PathBuilder(start: Vec2(x: -260, y: cy - half), headingDegrees: 0)298 b.forward(260 - stopDistance).markStop()299 return b300 }301 ids = []302 let laneW = 2 + index * 2303 p = west(); p.forward(2 * stopDistance).markExit().forward(480); ids.append(add(laneW, .straight, eastExit, p))304 // Right turn heads south; from the top street it then crosses the bottom junction too.305 p = west(); p.forward(14).turn(.right, radius: 15)306 if sign > 0 { p.forward(2 * yc + 14) } else { p.forward(14) }307 p.markExit().forward(480); ids.append(add(laneW, .right, 1, p))308 // Left turn heads north; from the bottom street it then crosses the top junction too.309 p = west(); p.forward(14).turn(.left, radius: 41)310 if sign < 0 { p.forward(2 * yc + 14) } else { p.forward(14) }311 p.markExit().forward(480); ids.append(add(laneW, .left, 0, p))312 lanes.append(TrafficLane(id: laneW, name: sign < 0 ? "Lower street, eastbound" : "Upper street, eastbound",313 autonomous: false, paths: ids, maxQueue: 3, openStage: 1))314315 // Westbound lane (from the east, y = cy + 13).316 func east() -> PathBuilder {317 let b = PathBuilder(start: Vec2(x: 260, y: cy + half), headingDegrees: 180)318 b.forward(260 - stopDistance).markStop()319 return b320 }321 ids = []322 let laneE = 3 + index * 2323 p = east(); p.forward(2 * stopDistance).markExit().forward(480); ids.append(add(laneE, .straight, westExit, p))324 // Right turn heads north.325 p = east(); p.forward(14).turn(.right, radius: 15)326 if sign < 0 { p.forward(2 * yc + 14) } else { p.forward(14) }327 p.markExit().forward(480); ids.append(add(laneE, .right, 0, p))328 // Left turn heads south.329 p = east(); p.forward(14).turn(.left, radius: 41)330 if sign > 0 { p.forward(2 * yc + 14) } else { p.forward(14) }331 p.markExit().forward(480); ids.append(add(laneE, .left, 1, p))332 lanes.append(TrafficLane(id: laneE, name: sign < 0 ? "Lower street, westbound" : "Upper street, westbound",333 autonomous: false, paths: ids, maxQueue: 3, openStage: 1))334 }335336 let roads = [337 TrafficRect(minX: -28, minY: -900, maxX: 28, maxY: 900),338 TrafficRect(minX: -900, minY: -yc - 28, maxX: 900, maxY: -yc + 28),339 TrafficRect(minX: -900, minY: yc - 28, maxX: 900, maxY: yc + 28),340 ]341 var markings = crossMarkings(center: Vec2(x: 0, y: -yc), arms: [0, 1, 3]) + crossMarkings(center: Vec2(x: 0, y: yc), arms: [1, 2, 3])342 markings += [343 .init(kind: .centerLine, from: Vec2(x: 0, y: -900), to: Vec2(x: 0, y: -yc - stopDistance)),344 .init(kind: .centerLine, from: Vec2(x: 0, y: -yc + 30), to: Vec2(x: 0, y: yc - 30)),345 .init(kind: .centerLine, from: Vec2(x: 0, y: yc + stopDistance), to: Vec2(x: 0, y: 900)),346 ]347 for cy in [-yc, yc] {348 markings.append(.init(kind: .centerLine, from: Vec2(x: -900, y: cy), to: Vec2(x: -stopDistance, y: cy)))349 markings.append(.init(kind: .centerLine, from: Vec2(x: stopDistance, y: cy), to: Vec2(x: 900, y: cy)))350 }351 return TrafficLayout(id: 2, name: "Twin Junction", blurb: "Two crossings, six approaches. Watch both at once.",352 lanes: lanes, paths: paths, roads: roads,353 boxes: [TrafficRect(minX: -28, minY: -yc - 28, maxX: 28, maxY: -yc + 28), TrafficRect(minX: -28, minY: yc - 28, maxX: 28, maxY: yc + 28)],354 markings: markings, focus: Vec2(x: 0, y: 12), unlockCleared: 25, throughPeriod: 0, throughGap: 0, spawnScale: 0.95, patienceBonus: 2)355 }356}357