Initial commit: offline deterministic Rust job-board simulator
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
[build]
|
||||||
|
rustflags = ["-C", "target-cpu=native"]
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
/target
|
||||||
|
**/*.rs.bk
|
||||||
|
*.csv
|
||||||
|
*.ndjson
|
||||||
Generated
+407
@@ -0,0 +1,407 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstream"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"anstyle-parse",
|
||||||
|
"anstyle-query",
|
||||||
|
"anstyle-wincon",
|
||||||
|
"colorchoice",
|
||||||
|
"is_terminal_polyfill",
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle"
|
||||||
|
version = "1.0.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-parse"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
|
||||||
|
dependencies = [
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-query"
|
||||||
|
version = "1.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-wincon"
|
||||||
|
version = "3.0.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"once_cell_polyfill",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "block-buffer"
|
||||||
|
version = "0.10.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
|
||||||
|
dependencies = [
|
||||||
|
"generic-array",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap"
|
||||||
|
version = "4.6.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "50066ca966813ca8957ee15361752f19424a8ff620dbe9c8993d9f99d99b04b3"
|
||||||
|
dependencies = [
|
||||||
|
"clap_builder",
|
||||||
|
"clap_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_builder"
|
||||||
|
version = "4.6.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a0ed97c96ef4efb3cc84e09fa388df4463e7cc3224ec38d7b1d9e45749333590"
|
||||||
|
dependencies = [
|
||||||
|
"anstream",
|
||||||
|
"anstyle",
|
||||||
|
"clap_lex",
|
||||||
|
"strsim",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_derive"
|
||||||
|
version = "4.6.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b4c174f11f949b9ca8c60580925e282b9cbd5454750e506f08648b2038080cd7"
|
||||||
|
dependencies = [
|
||||||
|
"heck",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_lex"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e0921b3876b3a1df8a1c9f912e7f94342458022c2e84288015893d83c4d62a79"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colorchoice"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cpufeatures"
|
||||||
|
version = "0.2.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-deque"
|
||||||
|
version = "0.8.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-epoch",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-epoch"
|
||||||
|
version = "0.9.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-utils"
|
||||||
|
version = "0.8.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crypto-common"
|
||||||
|
version = "0.1.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
|
||||||
|
dependencies = [
|
||||||
|
"generic-array",
|
||||||
|
"typenum",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "digest"
|
||||||
|
version = "0.10.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
||||||
|
dependencies = [
|
||||||
|
"block-buffer",
|
||||||
|
"crypto-common",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.19.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "generic-array"
|
||||||
|
version = "0.14.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
||||||
|
dependencies = [
|
||||||
|
"typenum",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "is_terminal_polyfill"
|
||||||
|
version = "1.70.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.191"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell_polyfill"
|
||||||
|
version = "1.70.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pp-jobboard-sim"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"clap",
|
||||||
|
"itoa",
|
||||||
|
"rayon",
|
||||||
|
"rustc-hash",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"sha2",
|
||||||
|
"smallvec",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.107"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.47"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon"
|
||||||
|
version = "1.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
"rayon-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon-core"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-deque",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc-hash"
|
||||||
|
version = "2.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
||||||
|
dependencies = [
|
||||||
|
"serde_core",
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_core"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.154"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"memchr",
|
||||||
|
"serde",
|
||||||
|
"serde_core",
|
||||||
|
"zmij",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha2"
|
||||||
|
version = "0.10.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "smallvec"
|
||||||
|
version = "1.16.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3244542535d5de959d90ec55d1b5ff0fd2bb7b1310223907d5f11932847f7b59"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "3.0.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "typenum"
|
||||||
|
version = "1.20.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utf8parse"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "version_check"
|
||||||
|
version = "0.9.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-link"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.61.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||||
|
dependencies = [
|
||||||
|
"windows-link",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zmij"
|
||||||
|
version = "1.0.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
[package]
|
||||||
|
name = "pp-jobboard-sim"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
description = "High-throughput offline deterministic aviation job-board generator and simulator"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clap = { version = "4.5", features = ["derive"] }
|
||||||
|
rayon = "1.10"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
sha2 = "0.10"
|
||||||
|
smallvec = "1.13"
|
||||||
|
rustc-hash = "2.1"
|
||||||
|
itoa = "1.0"
|
||||||
|
ryu = "1.0"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
opt-level = 3
|
||||||
|
lto = "fat"
|
||||||
|
codegen-units = 1
|
||||||
|
panic = "abort"
|
||||||
|
strip = false
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
# pp-jobboard-sim
|
||||||
|
|
||||||
|
High-throughput, deterministic offline Rust job-board generator and economy simulator implementing the clean-room job-board specification.
|
||||||
|
|
||||||
|
No proprietary game assets or hardcoded plane catalogs are required—supply your own `--world` / `--ourairports` and `--planes-file` JSON, or use the built-in synthetic world generator for rapid benchmarking.
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo build --release
|
||||||
|
cargo test --release
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### 1. Single airport board (`board`)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Synthetic world (default 4,000 global airports)
|
||||||
|
./target/release/pp-jobboard-sim board \
|
||||||
|
--airport S0000 \
|
||||||
|
--planes light_piston,skyliner \
|
||||||
|
--salt 123456789 \
|
||||||
|
--at 1796067000000 \
|
||||||
|
--level 2 \
|
||||||
|
--partition
|
||||||
|
|
||||||
|
# Custom world JSON + planes JSON
|
||||||
|
./target/release/pp-jobboard-sim \
|
||||||
|
--world data/world.json \
|
||||||
|
--planes-file data/planes.json \
|
||||||
|
board --airport KSAN --planes skylark,skyliner --master my-seed --json
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Large-scale parallel global simulation (`sim`)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# In-memory analytics across 4,000 airports × 48 buckets × all planes (1.536M boards / ~13.3M jobs)
|
||||||
|
./target/release/pp-jobboard-sim sim --synthetic-airports 4000 --buckets 48
|
||||||
|
|
||||||
|
# Export all generated jobs to CSV for DuckDB / Polars / Pandas analysis
|
||||||
|
./target/release/pp-jobboard-sim sim \
|
||||||
|
--world data/world.json \
|
||||||
|
--planes-file data/planes.json \
|
||||||
|
--master balance-run-1 \
|
||||||
|
--buckets 48 \
|
||||||
|
--mode csv \
|
||||||
|
--out jobs.csv
|
||||||
|
|
||||||
|
# Or load public-domain OurAirports airports.csv directly
|
||||||
|
./target/release/pp-jobboard-sim \
|
||||||
|
--ourairports airports.csv \
|
||||||
|
--planes-file data/planes.json \
|
||||||
|
sim --buckets 48 --mode stats --json
|
||||||
|
```
|
||||||
|
|
||||||
|
## Performance architecture
|
||||||
|
|
||||||
|
- **Per-origin sorted major & pad caches** inside thread-local `BoardScratch` with `partition_point` binary search (`O(log M + K)`, zero per-slot trig or sorting).
|
||||||
|
- **4°×4° spatial grid + precomputed neighbor cache** built in parallel via Rayon with unit-sphere chord-squared filtering (3 FMAs, zero trig).
|
||||||
|
- **Precomputed spread LUTs** (`(i+1)^(1/p) - i^(1/p)`) eliminating billions of `powf()` calls in weighted destination sampling.
|
||||||
|
- **3D unit-sphere cross-track prefilter** (`dot(normalize(A × B), S)`) for passenger photo detours.
|
||||||
|
- **Zero-contention Rayon fold/reduce** with mergeable fixed-bucket histograms, reusable `Vec<Job>` buffers, and `itoa`/`ryu` CSV writers.
|
||||||
+2649
File diff suppressed because it is too large
Load Diff
+233
@@ -0,0 +1,233 @@
|
|||||||
|
use std::f64::consts::{FRAC_PI_2, PI, TAU};
|
||||||
|
|
||||||
|
pub const NM: f64 = 1852.0;
|
||||||
|
pub const EARTH_R: f64 = 6_371_008.8;
|
||||||
|
pub const DEG_TO_RAD: f64 = PI / 180.0;
|
||||||
|
pub const RAD_TO_DEG: f64 = 180.0 / PI;
|
||||||
|
|
||||||
|
/// Precomputed spherical & 3D unit-sphere coordinates for zero-trig fast distance filtering.
|
||||||
|
#[derive(Clone, Copy, Debug, Default)]
|
||||||
|
pub struct GeoPoint {
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
pub lat_rad: f64,
|
||||||
|
pub lon_rad: f64,
|
||||||
|
pub sin_lat: f64,
|
||||||
|
pub cos_lat: f64,
|
||||||
|
pub x: f64,
|
||||||
|
pub y: f64,
|
||||||
|
pub z: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GeoPoint {
|
||||||
|
#[inline]
|
||||||
|
pub fn new(lat: f64, lon: f64) -> Self {
|
||||||
|
let lat_rad = lat * DEG_TO_RAD;
|
||||||
|
let lon_rad = lon * DEG_TO_RAD;
|
||||||
|
let (sin_lat, cos_lat) = lat_rad.sin_cos();
|
||||||
|
let (sin_lon, cos_lon) = lon_rad.sin_cos();
|
||||||
|
Self {
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
lat_rad,
|
||||||
|
lon_rad,
|
||||||
|
sin_lat,
|
||||||
|
cos_lat,
|
||||||
|
x: cos_lat * cos_lon,
|
||||||
|
y: cos_lat * sin_lon,
|
||||||
|
z: sin_lat,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Squared 3D chord length on the unit sphere (monotonic with great-circle distance).
|
||||||
|
/// 3 subtractions + 3 muls + 2 adds — zero trig!
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn chord_sq(a: &GeoPoint, b: &GeoPoint) -> f64 {
|
||||||
|
let dx = a.x - b.x;
|
||||||
|
let dy = a.y - b.y;
|
||||||
|
let dz = a.z - b.z;
|
||||||
|
dx * dx + dy * dy + dz * dz
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert a great-circle distance in metres to squared chord length on the unit sphere.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn dist_m_to_chord_sq(dist_m: f64) -> f64 {
|
||||||
|
if dist_m <= 0.0 {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
let half_angle = (dist_m / (2.0 * EARTH_R)).min(FRAC_PI_2);
|
||||||
|
let s = half_angle.sin();
|
||||||
|
4.0 * s * s
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert squared unit-sphere chord length to exact great-circle distance in metres
|
||||||
|
/// (algebraically identical to haversine, accurate down to millimetres).
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn chord_sq_to_dist_m(csq: f64) -> f64 {
|
||||||
|
if csq <= 0.0 {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
let half_chord = (csq * 0.25).sqrt().min(1.0);
|
||||||
|
2.0 * EARTH_R * half_chord.asin()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exact great-circle distance in metres using precomputed unit-sphere coordinates.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn dist_m(a: &GeoPoint, b: &GeoPoint) -> f64 {
|
||||||
|
chord_sq_to_dist_m(chord_sq(a, b))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Forward azimuth bearing from `a` to `b` in radians `[0, 2*PI)`.
|
||||||
|
#[inline]
|
||||||
|
pub fn bearing_rad(a: &GeoPoint, b: &GeoPoint) -> f64 {
|
||||||
|
let dlon = b.lon_rad - a.lon_rad;
|
||||||
|
let y = dlon.sin() * b.cos_lat;
|
||||||
|
let x = a.cos_lat * b.sin_lat - a.sin_lat * b.cos_lat * dlon.cos();
|
||||||
|
let brg = y.atan2(x);
|
||||||
|
if brg < 0.0 { brg + TAU } else { brg }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shortest angular difference between two bearings in radians `[0, PI]`.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn angular_distance(b1: f64, b2: f64) -> f64 {
|
||||||
|
let d = (b1 - b2).abs() % TAU;
|
||||||
|
if d > PI { TAU - d } else { d }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move from `origin` along `bearing_r` (radians) by `distance_m` metres.
|
||||||
|
#[inline]
|
||||||
|
pub fn destination_point(origin: &GeoPoint, bearing_r: f64, distance_m: f64) -> (f64, f64) {
|
||||||
|
let ang = distance_m / EARTH_R;
|
||||||
|
let (sin_ang, cos_ang) = ang.sin_cos();
|
||||||
|
let (sin_brg, cos_brg) = bearing_r.sin_cos();
|
||||||
|
|
||||||
|
let sin_lat2 = (origin.sin_lat * cos_ang + origin.cos_lat * sin_ang * cos_brg).clamp(-1.0, 1.0);
|
||||||
|
let lat2 = sin_lat2.asin();
|
||||||
|
let lon2 = origin.lon_rad
|
||||||
|
+ (sin_brg * sin_ang * origin.cos_lat).atan2(cos_ang - origin.sin_lat * sin_lat2);
|
||||||
|
|
||||||
|
let mut lon_deg = lon2 * RAD_TO_DEG;
|
||||||
|
if lon_deg > 180.0 {
|
||||||
|
lon_deg -= 360.0;
|
||||||
|
} else if lon_deg < -180.0 {
|
||||||
|
lon_deg += 360.0;
|
||||||
|
}
|
||||||
|
(lat2 * RAD_TO_DEG, lon_deg)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compute signed cross-track distance (metres) and along-track distance (metres) of point `p`
|
||||||
|
/// relative to the great-circle path `a -> b`.
|
||||||
|
#[inline]
|
||||||
|
pub fn cross_and_along_track(a: &GeoPoint, b: &GeoPoint, p: &GeoPoint) -> (f64, f64) {
|
||||||
|
let d_ap = dist_m(a, p) / EARTH_R;
|
||||||
|
let brg_ap = bearing_rad(a, p);
|
||||||
|
let brg_ab = bearing_rad(a, b);
|
||||||
|
let delta = brg_ap - brg_ab;
|
||||||
|
let sin_xt = (d_ap.sin() * delta.sin()).clamp(-1.0, 1.0);
|
||||||
|
let xt = sin_xt.asin();
|
||||||
|
let at = (d_ap.cos() / xt.cos().max(1e-12)).clamp(-1.0, 1.0).acos();
|
||||||
|
// Preserve sign of along-track when p is behind a.
|
||||||
|
let at_signed = if delta.cos() < 0.0 { -at } else { at };
|
||||||
|
(xt * EARTH_R, at_signed * EARTH_R)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calendar month (1..=12) and UTC hour (0.0..24.0) from epoch milliseconds.
|
||||||
|
/// Uses Howard Hinnant's civil-from-days algorithm (O(1), exact, no chrono dependency).
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct TimeInfo {
|
||||||
|
pub year: i32,
|
||||||
|
pub month: u32,
|
||||||
|
pub day: u32,
|
||||||
|
pub weekday: u32, // 0 = Sun .. 6 = Sat
|
||||||
|
pub utc_hour: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn time_info(epoch_ms: i64) -> TimeInfo {
|
||||||
|
let days = epoch_ms.div_euclid(86_400_000);
|
||||||
|
let ms_in_day = epoch_ms.rem_euclid(86_400_000);
|
||||||
|
let utc_hour = (ms_in_day as f64) / 3_600_000.0;
|
||||||
|
let weekday = (days + 4).rem_euclid(7) as u32;
|
||||||
|
|
||||||
|
let z = days + 719468;
|
||||||
|
let era = if z >= 0 { z } else { z - 146096 } / 146097;
|
||||||
|
let doe = (z - era * 146097) as u64; // [0, 146096]
|
||||||
|
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
|
||||||
|
let y = (yoe as i64) + era * 400;
|
||||||
|
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||||
|
let mp = (5 * doy + 2) / 153;
|
||||||
|
let d = doy - (153 * mp + 2) / 5 + 1;
|
||||||
|
let m = if mp < 10 { mp + 3 } else { mp - 9 };
|
||||||
|
let year = (y + if m <= 2 { 1 } else { 0 }) as i32;
|
||||||
|
|
||||||
|
TimeInfo {
|
||||||
|
year,
|
||||||
|
month: m as u32,
|
||||||
|
day: d as u32,
|
||||||
|
weekday,
|
||||||
|
utc_hour,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub enum HemisphereSeason {
|
||||||
|
Winter,
|
||||||
|
Spring,
|
||||||
|
Summer,
|
||||||
|
Autumn,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn season_at(month: u32, lat: f64) -> HemisphereSeason {
|
||||||
|
let north = match month {
|
||||||
|
12 | 1 | 2 => HemisphereSeason::Winter,
|
||||||
|
3 | 4 | 5 => HemisphereSeason::Spring,
|
||||||
|
6 | 7 | 8 => HemisphereSeason::Summer,
|
||||||
|
_ => HemisphereSeason::Autumn,
|
||||||
|
};
|
||||||
|
if lat >= 0.0 {
|
||||||
|
north
|
||||||
|
} else {
|
||||||
|
match north {
|
||||||
|
HemisphereSeason::Winter => HemisphereSeason::Summer,
|
||||||
|
HemisphereSeason::Spring => HemisphereSeason::Autumn,
|
||||||
|
HemisphereSeason::Summer => HemisphereSeason::Winter,
|
||||||
|
HemisphereSeason::Autumn => HemisphereSeason::Spring,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Approximate local solar hour in `[0, 24)`.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn local_solar_hour(utc_hour: f64, lon: f64) -> f64 {
|
||||||
|
(utc_hour + lon / 15.0).rem_euclid(24.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Approximate solar elevation angle in degrees (sufficient for daylight/night checks).
|
||||||
|
#[inline]
|
||||||
|
pub fn is_dark(info: &TimeInfo, lat: f64, lon: f64) -> bool {
|
||||||
|
let h = local_solar_hour(info.utc_hour, lon);
|
||||||
|
// Approximate day of year (0..365)
|
||||||
|
let doy = ((info.month.saturating_sub(1)) * 30 + info.day) as f64;
|
||||||
|
let decl = -23.44 * DEG_TO_RAD * ((TAU / 365.0) * (doy + 10.0)).cos();
|
||||||
|
let lat_r = lat * DEG_TO_RAD;
|
||||||
|
let hour_angle = (h - 12.0) * 15.0 * DEG_TO_RAD;
|
||||||
|
let sin_elev = lat_r.sin() * decl.sin() + lat_r.cos() * decl.cos() * hour_angle.cos();
|
||||||
|
sin_elev < -0.05 // below ~-3 deg twilight
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Weekly event index per Section 8:
|
||||||
|
/// floor((time - 2024-01-01) / 7d), with a 2592e5 ms (3-day) summer break after week 152.
|
||||||
|
pub fn event_week_index(epoch_ms: i64) -> i64 {
|
||||||
|
const EPOCH_2024_01_01_MS: i64 = 1_704_067_200_000;
|
||||||
|
const WEEK_MS: i64 = 7 * 86_400_000;
|
||||||
|
const BREAK_MS: i64 = 259_200_000; // 2592e5 ms
|
||||||
|
let elapsed = epoch_ms - EPOCH_2024_01_01_MS;
|
||||||
|
let w = elapsed.div_euclid(WEEK_MS);
|
||||||
|
if w > 152 {
|
||||||
|
(elapsed - BREAK_MS).div_euclid(WEEK_MS)
|
||||||
|
} else {
|
||||||
|
w
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
pub mod generator;
|
||||||
|
pub mod geo;
|
||||||
|
pub mod model;
|
||||||
|
pub mod rng;
|
||||||
|
pub mod rotor;
|
||||||
|
pub mod sim;
|
||||||
|
pub mod synthetic;
|
||||||
|
pub mod world;
|
||||||
+385
@@ -0,0 +1,385 @@
|
|||||||
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::time::{Instant, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
use clap::{Parser, Subcommand, ValueEnum};
|
||||||
|
|
||||||
|
use pp_jobboard_sim::generator::{BoardScratch, generate_board, partition_and_sort};
|
||||||
|
use pp_jobboard_sim::model::{BoardOptions, FilterMode, Plane, SortMode, WorldData};
|
||||||
|
use pp_jobboard_sim::rng::{board_salt, bucket_of, day_of, shop_salt};
|
||||||
|
use pp_jobboard_sim::sim::{ExportMode, SimConfig, run_simulation};
|
||||||
|
use pp_jobboard_sim::synthetic::{generate_synthetic_world, load_ourairports_csv};
|
||||||
|
use pp_jobboard_sim::world::WorldIndex;
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(
|
||||||
|
name = "pp-jobboard-sim",
|
||||||
|
about = "High-throughput offline deterministic aviation job-board generator and balance simulator"
|
||||||
|
)]
|
||||||
|
struct Cli {
|
||||||
|
/// Path to world JSON (airports, majors, pads, sea_bases, sights, planes)
|
||||||
|
#[arg(long, global = true)]
|
||||||
|
world: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// Path to OurAirports airports.csv (alternative to --world)
|
||||||
|
#[arg(long, global = true)]
|
||||||
|
ourairports: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// Path to external planes JSON (overrides planes in world/synthetic)
|
||||||
|
#[arg(long, global = true)]
|
||||||
|
planes_file: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// Number of synthetic global airports to generate if neither --world nor --ourairports is set
|
||||||
|
#[arg(long, global = true, default_value_t = 4000)]
|
||||||
|
synthetic_airports: usize,
|
||||||
|
|
||||||
|
/// Seed for synthetic world generation
|
||||||
|
#[arg(long, global = true, default_value_t = 42)]
|
||||||
|
world_seed: u32,
|
||||||
|
|
||||||
|
#[command(subcommand)]
|
||||||
|
cmd: Command,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, ValueEnum)]
|
||||||
|
enum CliExportMode {
|
||||||
|
Stats,
|
||||||
|
Csv,
|
||||||
|
Ndjson,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, ValueEnum)]
|
||||||
|
enum CliSort {
|
||||||
|
NetPerHour,
|
||||||
|
GrossPay,
|
||||||
|
FlightTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum Command {
|
||||||
|
/// Generate a job board for a single airport and plane set
|
||||||
|
Board {
|
||||||
|
/// Airport ICAO/code
|
||||||
|
#[arg(long)]
|
||||||
|
airport: String,
|
||||||
|
|
||||||
|
/// Comma-separated plane type IDs (e.g. light_piston,skyliner)
|
||||||
|
#[arg(long, alias = "fleet")]
|
||||||
|
planes: String,
|
||||||
|
|
||||||
|
/// Offline master seed string
|
||||||
|
#[arg(long, default_value = "offline")]
|
||||||
|
master: String,
|
||||||
|
|
||||||
|
/// Explicit uint32 board salt (overrides --master derivation)
|
||||||
|
#[arg(long)]
|
||||||
|
salt: Option<u32>,
|
||||||
|
|
||||||
|
/// Explicit uint32 daily shop salt
|
||||||
|
#[arg(long)]
|
||||||
|
day_salt: Option<u32>,
|
||||||
|
|
||||||
|
/// Epoch timestamp in milliseconds (defaults to current time)
|
||||||
|
#[arg(long)]
|
||||||
|
at: Option<i64>,
|
||||||
|
|
||||||
|
/// Airport level (0..=3)
|
||||||
|
#[arg(long, default_value_t = 0)]
|
||||||
|
level: u8,
|
||||||
|
|
||||||
|
/// Output raw JSON
|
||||||
|
#[arg(long)]
|
||||||
|
json: bool,
|
||||||
|
|
||||||
|
/// Apply presentation partitioning (ok/off/no) for the first plane
|
||||||
|
#[arg(long)]
|
||||||
|
partition: bool,
|
||||||
|
|
||||||
|
/// Sort order when --partition is enabled
|
||||||
|
#[arg(long, value_enum, default_value_t = CliSort::NetPerHour)]
|
||||||
|
sort: CliSort,
|
||||||
|
},
|
||||||
|
|
||||||
|
/// Run a multi-core global simulation across airports × buckets × planes
|
||||||
|
Sim {
|
||||||
|
/// Comma-separated plane IDs (defaults to all loaded planes)
|
||||||
|
#[arg(long)]
|
||||||
|
planes: Option<String>,
|
||||||
|
|
||||||
|
/// Offline master seed string
|
||||||
|
#[arg(long, default_value = "offline")]
|
||||||
|
master: String,
|
||||||
|
|
||||||
|
/// Explicit uint32 board salt (if set, reused for all buckets)
|
||||||
|
#[arg(long)]
|
||||||
|
salt: Option<u32>,
|
||||||
|
|
||||||
|
/// Starting epoch timestamp in milliseconds
|
||||||
|
#[arg(long, default_value_t = 1_796_067_000_000)]
|
||||||
|
at: i64,
|
||||||
|
|
||||||
|
/// Number of 30-minute buckets to simulate per airport
|
||||||
|
#[arg(long, default_value_t = 48)]
|
||||||
|
buckets: usize,
|
||||||
|
|
||||||
|
/// Airport level (0..=3)
|
||||||
|
#[arg(long, default_value_t = 0)]
|
||||||
|
level: u8,
|
||||||
|
|
||||||
|
/// Limit number of airports simulated
|
||||||
|
#[arg(long)]
|
||||||
|
airports: Option<usize>,
|
||||||
|
|
||||||
|
/// Output mode: stats (in-memory analytics), csv, or ndjson
|
||||||
|
#[arg(long, value_enum, default_value_t = CliExportMode::Stats)]
|
||||||
|
mode: CliExportMode,
|
||||||
|
|
||||||
|
/// Output file path (for csv or ndjson mode)
|
||||||
|
#[arg(long)]
|
||||||
|
out: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// Print full JSON report
|
||||||
|
#[arg(long)]
|
||||||
|
json: bool,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_world(cli: &Cli, precompute_neighbors: bool) -> WorldIndex {
|
||||||
|
let mut data: WorldData = if let Some(ref p) = cli.world {
|
||||||
|
let txt = fs::read_to_string(p).expect("failed to read --world file");
|
||||||
|
serde_json::from_str(&txt).expect("failed to parse --world JSON")
|
||||||
|
} else if let Some(ref p) = cli.ourairports {
|
||||||
|
load_ourairports_csv(p).expect("failed to load --ourairports CSV")
|
||||||
|
} else {
|
||||||
|
generate_synthetic_world(cli.world_seed, cli.synthetic_airports)
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(ref pf) = cli.planes_file {
|
||||||
|
let txt = fs::read_to_string(pf).expect("failed to read --planes-file");
|
||||||
|
let planes: Vec<Plane> =
|
||||||
|
serde_json::from_str(&txt).expect("failed to parse --planes-file JSON");
|
||||||
|
data.planes = planes;
|
||||||
|
}
|
||||||
|
|
||||||
|
WorldIndex::build(data, precompute_neighbors)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn now_epoch_ms() -> i64 {
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap()
|
||||||
|
.as_millis() as i64
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let cli = Cli::parse();
|
||||||
|
|
||||||
|
match &cli.cmd {
|
||||||
|
Command::Board {
|
||||||
|
airport,
|
||||||
|
planes,
|
||||||
|
master,
|
||||||
|
salt,
|
||||||
|
day_salt,
|
||||||
|
at,
|
||||||
|
level,
|
||||||
|
json,
|
||||||
|
partition,
|
||||||
|
sort,
|
||||||
|
} => {
|
||||||
|
let world = load_world(&cli, false);
|
||||||
|
let (ap_idx, origin) = world
|
||||||
|
.get_airport(airport)
|
||||||
|
.unwrap_or_else(|| panic!("airport not found: {airport}"));
|
||||||
|
|
||||||
|
let plane_refs: Vec<&Plane> = planes
|
||||||
|
.split(',')
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|id| {
|
||||||
|
world
|
||||||
|
.planes
|
||||||
|
.get(id)
|
||||||
|
.unwrap_or_else(|| panic!("plane not found: {id}"))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let epoch_ms = at.unwrap_or_else(now_epoch_ms);
|
||||||
|
let bucket = bucket_of(epoch_ms);
|
||||||
|
let s = salt.unwrap_or_else(|| board_salt(master, bucket));
|
||||||
|
let ds = day_salt.unwrap_or_else(|| shop_salt(master, day_of(epoch_ms)));
|
||||||
|
|
||||||
|
let opts = BoardOptions {
|
||||||
|
salt: s,
|
||||||
|
day_salt: ds,
|
||||||
|
level: *level,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut scratch = BoardScratch::with_capacity();
|
||||||
|
let jobs = generate_board(&world, ap_idx, epoch_ms, &plane_refs, &opts, &mut scratch);
|
||||||
|
|
||||||
|
if *partition {
|
||||||
|
let sm = match sort {
|
||||||
|
CliSort::NetPerHour => SortMode::NetPerHour,
|
||||||
|
CliSort::GrossPay => SortMode::GrossPay,
|
||||||
|
CliSort::FlightTime => SortMode::FlightTime,
|
||||||
|
};
|
||||||
|
let part = partition_and_sort(
|
||||||
|
&world,
|
||||||
|
origin,
|
||||||
|
plane_refs[0],
|
||||||
|
jobs,
|
||||||
|
epoch_ms,
|
||||||
|
true,
|
||||||
|
sm,
|
||||||
|
&FilterMode::All,
|
||||||
|
);
|
||||||
|
if *json {
|
||||||
|
println!("{}", serde_json::to_string_pretty(&part).unwrap());
|
||||||
|
} else {
|
||||||
|
println!(
|
||||||
|
"Airport {} | bucket={} salt={} | ok={} off={} no={} | refresh in {}m",
|
||||||
|
origin.raw.code,
|
||||||
|
bucket,
|
||||||
|
s,
|
||||||
|
part.ok.len(),
|
||||||
|
part.off.len(),
|
||||||
|
part.no.len(),
|
||||||
|
part.refresh_mins
|
||||||
|
);
|
||||||
|
for r in &part.ok {
|
||||||
|
println!(
|
||||||
|
" {:7} {:6} -> {:6} {:6.1}nm pax={:<3} kg={:<5} pay=${:<7} net=${:<7} ${:<7.0}/hr {}",
|
||||||
|
r.job.kind.as_str(),
|
||||||
|
r.job.from,
|
||||||
|
r.job.to,
|
||||||
|
r.job.nm,
|
||||||
|
r.job.pax,
|
||||||
|
r.job.kg,
|
||||||
|
r.job.pay,
|
||||||
|
r.net_pay,
|
||||||
|
r.net_per_hour,
|
||||||
|
r.job.what.as_deref().unwrap_or("")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if *json {
|
||||||
|
println!("{}", serde_json::to_string_pretty(&jobs).unwrap());
|
||||||
|
} else {
|
||||||
|
println!(
|
||||||
|
"Airport {} | bucket={} salt={} | {} jobs",
|
||||||
|
origin.raw.code,
|
||||||
|
bucket,
|
||||||
|
s,
|
||||||
|
jobs.len()
|
||||||
|
);
|
||||||
|
for j in &jobs {
|
||||||
|
println!(
|
||||||
|
" {:7} {:6} -> {:6} {:6.1}nm pax={:<3} kg={:<5} pay=${:<7} {}",
|
||||||
|
j.kind.as_str(),
|
||||||
|
j.from,
|
||||||
|
j.to,
|
||||||
|
j.nm,
|
||||||
|
j.pax,
|
||||||
|
j.kg,
|
||||||
|
j.pay,
|
||||||
|
j.what.as_deref().unwrap_or("")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::Sim {
|
||||||
|
planes,
|
||||||
|
master,
|
||||||
|
salt,
|
||||||
|
at,
|
||||||
|
buckets,
|
||||||
|
level,
|
||||||
|
airports,
|
||||||
|
mode,
|
||||||
|
out,
|
||||||
|
json,
|
||||||
|
} => {
|
||||||
|
let t_idx = Instant::now();
|
||||||
|
let world = load_world(&cli, true);
|
||||||
|
let idx_secs = t_idx.elapsed().as_secs_f64();
|
||||||
|
|
||||||
|
let plane_refs: Vec<&Plane> = if let Some(list) = planes {
|
||||||
|
list.split(',')
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|id| {
|
||||||
|
world
|
||||||
|
.planes
|
||||||
|
.get(id)
|
||||||
|
.unwrap_or_else(|| panic!("plane not found: {id}"))
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
world.plane_list.iter().collect()
|
||||||
|
};
|
||||||
|
|
||||||
|
let export_mode = match mode {
|
||||||
|
CliExportMode::Stats => ExportMode::StatsOnly,
|
||||||
|
CliExportMode::Csv => ExportMode::Csv,
|
||||||
|
CliExportMode::Ndjson => ExportMode::Ndjson,
|
||||||
|
};
|
||||||
|
|
||||||
|
let cfg = SimConfig {
|
||||||
|
master,
|
||||||
|
start_epoch_ms: *at,
|
||||||
|
num_buckets: *buckets,
|
||||||
|
explicit_salt: *salt,
|
||||||
|
level: *level,
|
||||||
|
aurora: false,
|
||||||
|
fair_weather: true,
|
||||||
|
airport_limit: *airports,
|
||||||
|
export_mode,
|
||||||
|
out_path: out.as_deref(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let report = run_simulation(&world, &plane_refs, &cfg).expect("simulation failed");
|
||||||
|
|
||||||
|
if *json {
|
||||||
|
println!("{}", serde_json::to_string_pretty(&report).unwrap());
|
||||||
|
} else {
|
||||||
|
eprintln!(
|
||||||
|
"World indexed ({} airports, {} majors, {} pads, {} sights) in {:.3}s",
|
||||||
|
world.airports.len(),
|
||||||
|
world.majors.len(),
|
||||||
|
world.pads.len(),
|
||||||
|
world.sights.len(),
|
||||||
|
idx_secs
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"Simulated {} boards ({} jobs) in {:.3}s [{:.0} boards/sec | {:.0} jobs/sec]",
|
||||||
|
report.total_boards,
|
||||||
|
report.total_jobs,
|
||||||
|
report.elapsed_secs,
|
||||||
|
report.boards_per_sec,
|
||||||
|
report.jobs_per_sec
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"{:<18} {:>8} {:>9} {:>6} {:>9} {:>9} {:>9} {:>9} {:>7}",
|
||||||
|
"PLANE", "BOARDS", "JOBS", "J/B", "AVG $/HR", "BEST$/HR", "P50 $/HR", "P90 $/HR", "AVG NM"
|
||||||
|
);
|
||||||
|
for p in &report.planes {
|
||||||
|
println!(
|
||||||
|
"{:<18} {:>8} {:>9} {:>6.2} {:>9.0} {:>9.0} {:>9.0} {:>9.0} {:>7.1}",
|
||||||
|
p.plane_id,
|
||||||
|
p.boards,
|
||||||
|
p.jobs,
|
||||||
|
p.avg_jobs_per_board,
|
||||||
|
p.avg_net_per_hr,
|
||||||
|
p.avg_best_net_per_hr,
|
||||||
|
p.p50_net_per_hr,
|
||||||
|
p.p90_net_per_hr,
|
||||||
|
p.avg_nm
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+866
@@ -0,0 +1,866 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Runway {
|
||||||
|
pub len: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub hard: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub water: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub hdg: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Farm {
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub hdg: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Beach {
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub hdg: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub len: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub sea: i32,
|
||||||
|
#[serde(default)]
|
||||||
|
pub name: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub elev: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum PadKind {
|
||||||
|
Hospital,
|
||||||
|
City,
|
||||||
|
Remote,
|
||||||
|
Rig,
|
||||||
|
Ship,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PadKind {
|
||||||
|
pub const ALL: [PadKind; 5] = [
|
||||||
|
PadKind::Hospital,
|
||||||
|
PadKind::City,
|
||||||
|
PadKind::Remote,
|
||||||
|
PadKind::Rig,
|
||||||
|
PadKind::Ship,
|
||||||
|
];
|
||||||
|
|
||||||
|
pub const SLING_ORDER: [PadKind; 4] = [
|
||||||
|
PadKind::Remote,
|
||||||
|
PadKind::Rig,
|
||||||
|
PadKind::Ship,
|
||||||
|
PadKind::City,
|
||||||
|
];
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn attempts(self) -> usize {
|
||||||
|
match self {
|
||||||
|
PadKind::Hospital => 2,
|
||||||
|
PadKind::City => 1,
|
||||||
|
PadKind::Remote => 2,
|
||||||
|
PadKind::Rig => 2,
|
||||||
|
PadKind::Ship => 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn gate(self) -> f64 {
|
||||||
|
match self {
|
||||||
|
PadKind::Hospital => 0.85,
|
||||||
|
PadKind::Rig | PadKind::Ship => 0.75,
|
||||||
|
PadKind::City | PadKind::Remote => 0.60,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Pad {
|
||||||
|
pub kind: PadKind,
|
||||||
|
#[serde(default)]
|
||||||
|
pub deck: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub hdg: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub small: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct SeaDock {
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub hdg: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct SeaBase {
|
||||||
|
pub dock: SeaDock,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Airport {
|
||||||
|
pub code: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub name: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub city: String,
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub elev: f64, // metres
|
||||||
|
/// 0 hub, 1 regional, 2 airstrip
|
||||||
|
#[serde(default = "default_size")]
|
||||||
|
pub size: u8,
|
||||||
|
#[serde(default)]
|
||||||
|
pub sched: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub country: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub longest: f64, // metres
|
||||||
|
#[serde(default)]
|
||||||
|
pub rw: Vec<Runway>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub farm: Option<Farm>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub beach: Option<Beach>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub pad: Option<Pad>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub sea: Option<SeaBase>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub marine: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_size() -> u8 {
|
||||||
|
2
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Airport {
|
||||||
|
#[inline]
|
||||||
|
pub fn longest_hard_m(&self) -> f64 {
|
||||||
|
let mut best = 0.0f64;
|
||||||
|
for r in &self.rw {
|
||||||
|
if r.hard && !r.water && r.len > best {
|
||||||
|
best = r.len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
best
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn longest_land_m(&self) -> f64 {
|
||||||
|
let mut best = 0.0f64;
|
||||||
|
for r in &self.rw {
|
||||||
|
if !r.water && r.len > best {
|
||||||
|
best = r.len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if best > 0.0 { best } else { self.longest }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn has_hard_longest(&self) -> bool {
|
||||||
|
if self.rw.is_empty() {
|
||||||
|
return self.size <= 1;
|
||||||
|
}
|
||||||
|
let mut best_len = -1.0f64;
|
||||||
|
let mut best_hard = false;
|
||||||
|
for r in &self.rw {
|
||||||
|
if !r.water && r.len > best_len {
|
||||||
|
best_len = r.len;
|
||||||
|
best_hard = r.hard;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
best_hard
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Short field per Section 3.4: any runway under 800 m, or the longest runway is soft.
|
||||||
|
#[inline]
|
||||||
|
pub fn is_short_field(&self) -> bool {
|
||||||
|
if self.pad.is_some() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if self.rw.is_empty() {
|
||||||
|
return self.longest < 800.0;
|
||||||
|
}
|
||||||
|
let any_under_800 = self.rw.iter().any(|r| !r.water && r.len < 800.0);
|
||||||
|
any_under_800 || !self.has_hard_longest()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_pad_only(&self) -> bool {
|
||||||
|
self.pad.is_some() && self.rw.is_empty() && self.longest <= 0.0
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_seaplane_only(&self) -> bool {
|
||||||
|
self.sea.is_some() && self.rw.iter().all(|r| r.water)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn has_usable_runway(&self, need_m: f64, hard: bool) -> bool {
|
||||||
|
if self.is_pad_only() || self.is_seaplane_only() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if hard {
|
||||||
|
self.longest_hard_m() >= need_m
|
||||||
|
} else {
|
||||||
|
self.longest_land_m() >= need_m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct RotorSpec {
|
||||||
|
#[serde(default)]
|
||||||
|
pub d: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub big: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub tilt: bool,
|
||||||
|
#[serde(default, rename = "hoverKw")]
|
||||||
|
pub hover_kw: f64,
|
||||||
|
#[serde(default, rename = "maxKw")]
|
||||||
|
pub max_kw: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub sto: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct HookSpec {
|
||||||
|
#[serde(default)]
|
||||||
|
pub kg: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub line: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub kt: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Plane {
|
||||||
|
pub id: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub name: String,
|
||||||
|
#[serde(default = "default_plane_kind")]
|
||||||
|
pub kind: String, // "piston", "turboprop", "jet", "heli", etc.
|
||||||
|
#[serde(default)]
|
||||||
|
pub seats: u32,
|
||||||
|
#[serde(default, rename = "cargoKg")]
|
||||||
|
pub cargo_kg: f64,
|
||||||
|
#[serde(default, rename = "runwayM")]
|
||||||
|
pub runway_m: f64,
|
||||||
|
#[serde(default, rename = "rangeNm")]
|
||||||
|
pub range_nm: f64,
|
||||||
|
#[serde(default, rename = "cruiseKt")]
|
||||||
|
pub cruise_kt: f64,
|
||||||
|
#[serde(default, rename = "cruiseFt")]
|
||||||
|
pub cruise_ft: f64,
|
||||||
|
#[serde(default, rename = "ceilingFt")]
|
||||||
|
pub ceiling_ft: f64,
|
||||||
|
#[serde(default, rename = "massKg")]
|
||||||
|
pub mass_kg: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub rotor: Option<RotorSpec>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub hook: Option<HookSpec>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub tanker: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub scoop: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub snorkel: bool,
|
||||||
|
#[serde(default, rename = "tankL")]
|
||||||
|
pub tank_l: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub drops: u32,
|
||||||
|
#[serde(default)]
|
||||||
|
pub ag: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub historic: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub searches: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub nvg: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub ramp: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub floats: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub amphib: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub water_only: bool,
|
||||||
|
#[serde(default, rename = "burnGph")]
|
||||||
|
pub burn_gph: f64,
|
||||||
|
#[serde(default, rename = "upkeepNm")]
|
||||||
|
pub upkeep_nm: f64,
|
||||||
|
#[serde(default = "default_fuel_price", rename = "fuelPrice")]
|
||||||
|
pub fuel_price: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_plane_kind() -> String {
|
||||||
|
"piston".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_fuel_price() -> f64 {
|
||||||
|
6.5
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Plane {
|
||||||
|
#[inline]
|
||||||
|
pub fn is_cargo_only(&self) -> bool {
|
||||||
|
self.seats == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_airliner(&self) -> bool {
|
||||||
|
self.seats > 60
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_super_large(&self) -> bool {
|
||||||
|
self.seats > 150
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_jet(&self) -> bool {
|
||||||
|
self.kind == "jet"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_rotor(&self) -> bool {
|
||||||
|
self.rotor.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_tiltrotor(&self) -> bool {
|
||||||
|
self.rotor.as_ref().is_some_and(|r| r.tilt)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_seaplane(&self) -> bool {
|
||||||
|
self.floats || self.amphib || self.water_only
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_light_plane(&self) -> bool {
|
||||||
|
!self.is_jet() && !self.is_rotor() && self.seats <= 6 && !self.is_airliner()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn effective_range_nm(&self) -> f64 {
|
||||||
|
if self.range_nm > 0.0 {
|
||||||
|
self.range_nm
|
||||||
|
} else {
|
||||||
|
500.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn effective_ceiling_ft(&self) -> f64 {
|
||||||
|
if self.ceiling_ft > 0.0 {
|
||||||
|
self.ceiling_ft
|
||||||
|
} else if self.is_jet() {
|
||||||
|
41_000.0
|
||||||
|
} else if self.is_rotor() {
|
||||||
|
14_000.0
|
||||||
|
} else {
|
||||||
|
16_000.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn effective_cruise_kt(&self) -> f64 {
|
||||||
|
if self.cruise_kt > 0.0 {
|
||||||
|
self.cruise_kt
|
||||||
|
} else {
|
||||||
|
120.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Estimated flight cost per Section 7:
|
||||||
|
/// round(nm * upkeepNm + burnGph * (0.1 + nm / cruiseKt) * 0.85 * fuelPrice)
|
||||||
|
#[inline]
|
||||||
|
pub fn flight_cost(&self, nm: f64) -> i64 {
|
||||||
|
let fuel_h = 0.1 + nm / self.effective_cruise_kt();
|
||||||
|
let cost = nm * self.upkeep_nm + self.burn_gph * fuel_h * 0.85 * self.fuel_price;
|
||||||
|
cost.round() as i64
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Estimated flight time in seconds per Section 7:
|
||||||
|
/// cruise profile + 20 s + 4 min per visit.
|
||||||
|
#[inline]
|
||||||
|
pub fn flight_time_secs(&self, nm: f64, visits: usize) -> f64 {
|
||||||
|
let cruise_secs = (nm / self.effective_cruise_kt()) * 3600.0;
|
||||||
|
cruise_secs + 20.0 + (visits as f64) * 240.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Sight {
|
||||||
|
pub name: String,
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
#[serde(default = "default_sight_r")]
|
||||||
|
pub r: f64, // metres
|
||||||
|
#[serde(default)]
|
||||||
|
pub alt: f64, // feet
|
||||||
|
#[serde(default, rename = "mapOnly")]
|
||||||
|
pub map_only: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub survey: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub lit: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_sight_r() -> f64 {
|
||||||
|
2000.0
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Fire {
|
||||||
|
pub id: String,
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub hdg: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub n: u32,
|
||||||
|
#[serde(default)]
|
||||||
|
pub t: i64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub out: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum EventMode {
|
||||||
|
Inside,
|
||||||
|
Into,
|
||||||
|
Out,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct EventBox {
|
||||||
|
pub min_lat: f64,
|
||||||
|
pub max_lat: f64,
|
||||||
|
pub min_lon: f64,
|
||||||
|
pub max_lon: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventBox {
|
||||||
|
#[inline]
|
||||||
|
pub fn contains(&self, lat: f64, lon: f64) -> bool {
|
||||||
|
lat >= self.min_lat && lat <= self.max_lat && lon >= self.min_lon && lon <= self.max_lon
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn center(&self) -> (f64, f64) {
|
||||||
|
(
|
||||||
|
0.5 * (self.min_lat + self.max_lat),
|
||||||
|
0.5 * (self.min_lon + self.max_lon),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||||
|
pub struct EventRule {
|
||||||
|
#[serde(default, rename = "minKg")]
|
||||||
|
pub min_kg: Option<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct EventState {
|
||||||
|
pub id: String,
|
||||||
|
#[serde(default = "default_event_mode")]
|
||||||
|
pub mode: EventMode,
|
||||||
|
#[serde(rename = "box")]
|
||||||
|
pub bbox: EventBox,
|
||||||
|
#[serde(default)]
|
||||||
|
pub what: String,
|
||||||
|
#[serde(default = "default_event_kind")]
|
||||||
|
pub kind: JobKind,
|
||||||
|
#[serde(default)]
|
||||||
|
pub met: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub rule: EventRule,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_event_mode() -> EventMode {
|
||||||
|
EventMode::Inside
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_event_kind() -> JobKind {
|
||||||
|
JobKind::Cargo
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct CompanyBase {
|
||||||
|
pub code: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub name: String,
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
#[serde(default = "default_gates")]
|
||||||
|
pub gates: u32,
|
||||||
|
#[serde(default)]
|
||||||
|
pub low: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_gates() -> u32 {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct CompanyState {
|
||||||
|
pub id: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub name: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub bases: Vec<CompanyBase>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct ProjectState {
|
||||||
|
pub code: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub name: String,
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub what: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub sling: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct StoredJob {
|
||||||
|
pub id: String,
|
||||||
|
pub from: String,
|
||||||
|
pub to: String,
|
||||||
|
pub kind: JobKind,
|
||||||
|
#[serde(default)]
|
||||||
|
pub pax: u32,
|
||||||
|
#[serde(default)]
|
||||||
|
pub kg: u32,
|
||||||
|
#[serde(default)]
|
||||||
|
pub nm: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub pay: i64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub what: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub company: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub expires: i64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub is_contract: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum JobKind {
|
||||||
|
Pax,
|
||||||
|
Cargo,
|
||||||
|
Vip,
|
||||||
|
Exec,
|
||||||
|
Mail,
|
||||||
|
Medical,
|
||||||
|
Fresh,
|
||||||
|
Group,
|
||||||
|
Art,
|
||||||
|
Tour,
|
||||||
|
Sar,
|
||||||
|
Survey,
|
||||||
|
Fire,
|
||||||
|
Supply,
|
||||||
|
Materials,
|
||||||
|
Show,
|
||||||
|
Crop,
|
||||||
|
Solo,
|
||||||
|
Lift,
|
||||||
|
Banner,
|
||||||
|
Drop,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl JobKind {
|
||||||
|
pub const COUNT: usize = 21;
|
||||||
|
|
||||||
|
pub const ALL: [JobKind; 21] = [
|
||||||
|
JobKind::Pax,
|
||||||
|
JobKind::Cargo,
|
||||||
|
JobKind::Vip,
|
||||||
|
JobKind::Exec,
|
||||||
|
JobKind::Mail,
|
||||||
|
JobKind::Medical,
|
||||||
|
JobKind::Fresh,
|
||||||
|
JobKind::Group,
|
||||||
|
JobKind::Art,
|
||||||
|
JobKind::Tour,
|
||||||
|
JobKind::Sar,
|
||||||
|
JobKind::Survey,
|
||||||
|
JobKind::Fire,
|
||||||
|
JobKind::Supply,
|
||||||
|
JobKind::Materials,
|
||||||
|
JobKind::Show,
|
||||||
|
JobKind::Crop,
|
||||||
|
JobKind::Solo,
|
||||||
|
JobKind::Lift,
|
||||||
|
JobKind::Banner,
|
||||||
|
JobKind::Drop,
|
||||||
|
];
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn idx(self) -> usize {
|
||||||
|
self as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn as_str(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
JobKind::Pax => "pax",
|
||||||
|
JobKind::Cargo => "cargo",
|
||||||
|
JobKind::Vip => "vip",
|
||||||
|
JobKind::Exec => "exec",
|
||||||
|
JobKind::Mail => "mail",
|
||||||
|
JobKind::Medical => "medical",
|
||||||
|
JobKind::Fresh => "fresh",
|
||||||
|
JobKind::Group => "group",
|
||||||
|
JobKind::Art => "art",
|
||||||
|
JobKind::Tour => "tour",
|
||||||
|
JobKind::Sar => "sar",
|
||||||
|
JobKind::Survey => "survey",
|
||||||
|
JobKind::Fire => "fire",
|
||||||
|
JobKind::Supply => "supply",
|
||||||
|
JobKind::Materials => "materials",
|
||||||
|
JobKind::Show => "show",
|
||||||
|
JobKind::Crop => "crop",
|
||||||
|
JobKind::Solo => "solo",
|
||||||
|
JobKind::Lift => "lift",
|
||||||
|
JobKind::Banner => "banner",
|
||||||
|
JobKind::Drop => "drop",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exact Section 3.4 kind multipliers.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn multiplier(self) -> f64 {
|
||||||
|
match self {
|
||||||
|
JobKind::Pax => 1.0,
|
||||||
|
JobKind::Cargo => 0.9,
|
||||||
|
JobKind::Vip => 1.8,
|
||||||
|
JobKind::Exec => 2.2,
|
||||||
|
JobKind::Mail => 1.0,
|
||||||
|
JobKind::Medical => 1.5,
|
||||||
|
JobKind::Fresh => 1.3,
|
||||||
|
JobKind::Group => 1.3,
|
||||||
|
JobKind::Art => 2.0,
|
||||||
|
JobKind::Tour => 1.0,
|
||||||
|
JobKind::Sar => 2.0,
|
||||||
|
JobKind::Survey => 1.4,
|
||||||
|
JobKind::Fire => 1.8,
|
||||||
|
JobKind::Supply => 1.0,
|
||||||
|
JobKind::Materials => 1.0,
|
||||||
|
JobKind::Show => 2.5,
|
||||||
|
JobKind::Crop => 1.2,
|
||||||
|
JobKind::Solo => 1.0,
|
||||||
|
JobKind::Lift => 1.1,
|
||||||
|
JobKind::Banner => 1.25,
|
||||||
|
JobKind::Drop => 1.2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Aerial work kinds get 1.5x base distance pay per Section 3.4.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn is_aerial(self) -> bool {
|
||||||
|
matches!(
|
||||||
|
self,
|
||||||
|
JobKind::Tour
|
||||||
|
| JobKind::Sar
|
||||||
|
| JobKind::Survey
|
||||||
|
| JobKind::Fire
|
||||||
|
| JobKind::Crop
|
||||||
|
| JobKind::Banner
|
||||||
|
| JobKind::Drop
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn is_pax_detour_eligible(self) -> bool {
|
||||||
|
matches!(self, JobKind::Pax | JobKind::Vip | JobKind::Exec | JobKind::Group)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub struct Coord {
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub struct PhotoTarget {
|
||||||
|
pub name: String,
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct VisitStop {
|
||||||
|
pub code: String,
|
||||||
|
pub lat: f64,
|
||||||
|
pub lon: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Job {
|
||||||
|
pub id: String,
|
||||||
|
pub from: String,
|
||||||
|
pub to: String,
|
||||||
|
pub kind: JobKind,
|
||||||
|
pub pax: u32,
|
||||||
|
pub kg: u32,
|
||||||
|
pub nm: f64,
|
||||||
|
pub pay: i64,
|
||||||
|
pub expires: i64,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub what: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub season: Option<&'static str>,
|
||||||
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||||
|
pub short: bool,
|
||||||
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||||
|
pub big_charter: bool,
|
||||||
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||||
|
pub water: bool,
|
||||||
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||||
|
pub pad: bool,
|
||||||
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||||
|
pub sling: bool,
|
||||||
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||||
|
pub is_contract: bool,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub photo: Option<PhotoTarget>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub via: Option<Coord>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub work_point: Option<Coord>,
|
||||||
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||||
|
pub visits: Vec<VisitStop>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub company: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub event: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub deadline_min: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub sea_state: Option<u8>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub passes: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub min_runway_m: Option<f64>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub dest_elev_m: Option<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||||
|
pub struct WorldData {
|
||||||
|
#[serde(default)]
|
||||||
|
pub airports: Vec<Airport>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub majors: Vec<Airport>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub pads: Vec<Airport>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub sea_bases: Vec<Airport>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub sights: Vec<Sight>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub planes: Vec<Plane>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub fires: Vec<Fire>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub event: Option<EventState>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub company: Option<CompanyState>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub projects: Vec<ProjectState>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct BoardOptions<'a> {
|
||||||
|
pub salt: u32,
|
||||||
|
pub day_salt: u32,
|
||||||
|
pub level: u8,
|
||||||
|
pub first_solo: bool,
|
||||||
|
pub aurora_active: bool,
|
||||||
|
pub fair_weather: bool,
|
||||||
|
pub dev: bool,
|
||||||
|
pub fires: &'a [Fire],
|
||||||
|
pub event: Option<&'a EventState>,
|
||||||
|
pub company: Option<&'a CompanyState>,
|
||||||
|
pub projects: &'a [ProjectState],
|
||||||
|
pub stored: &'a [StoredJob],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Default for BoardOptions<'a> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
salt: 0,
|
||||||
|
day_salt: 0,
|
||||||
|
level: 0,
|
||||||
|
first_solo: false,
|
||||||
|
aurora_active: false,
|
||||||
|
fair_weather: true,
|
||||||
|
dev: false,
|
||||||
|
fires: &[],
|
||||||
|
event: None,
|
||||||
|
company: None,
|
||||||
|
projects: &[],
|
||||||
|
stored: &[],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub enum SortMode {
|
||||||
|
NetPerHour,
|
||||||
|
GrossPay,
|
||||||
|
FlightTime,
|
||||||
|
CloserToEvent,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum FilterMode<'a> {
|
||||||
|
All,
|
||||||
|
NewAirports(&'a rustc_hash::FxHashSet<String>),
|
||||||
|
Company(&'a str),
|
||||||
|
TowardsEvent(&'a EventState),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize)]
|
||||||
|
pub struct BoardRow {
|
||||||
|
pub job: Job,
|
||||||
|
pub net_pay: i64,
|
||||||
|
pub flight_secs: f64,
|
||||||
|
pub net_per_hour: f64,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub reason: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub closer_nm: Option<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize)]
|
||||||
|
pub struct PartitionedBoard {
|
||||||
|
pub ok: Vec<BoardRow>,
|
||||||
|
pub off: Vec<BoardRow>,
|
||||||
|
pub no: Vec<BoardRow>,
|
||||||
|
pub refresh_mins: i64,
|
||||||
|
}
|
||||||
+119
@@ -0,0 +1,119 @@
|
|||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
pub const BUCKET_MS: i64 = 1_800_000;
|
||||||
|
pub const DAY_MS: i64 = 86_400_000;
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn bucket_of(epoch_ms: i64) -> i64 {
|
||||||
|
epoch_ms.div_euclid(BUCKET_MS)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn day_of(epoch_ms: i64) -> i64 {
|
||||||
|
epoch_ms.div_euclid(DAY_MS)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deterministic offline 32-bit little-endian board salt from master seed and bucket.
|
||||||
|
pub fn board_salt(master: &str, bucket: i64) -> u32 {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.update(master.as_bytes());
|
||||||
|
hasher.update(b":");
|
||||||
|
let mut buf = itoa::Buffer::new();
|
||||||
|
hasher.update(buf.format(bucket).as_bytes());
|
||||||
|
let digest = hasher.finalize();
|
||||||
|
u32::from_le_bytes([digest[0], digest[1], digest[2], digest[3]])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deterministic offline daily shop salt (used for air-show days).
|
||||||
|
pub fn shop_salt(master: &str, day: i64) -> u32 {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.update(master.as_bytes());
|
||||||
|
hasher.update(b":shop:");
|
||||||
|
let mut buf = itoa::Buffer::new();
|
||||||
|
hasher.update(buf.format(day).as_bytes());
|
||||||
|
let digest = hasher.finalize();
|
||||||
|
u32::from_le_bytes([digest[0], digest[1], digest[2], digest[3]])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Java-style 32-bit signed wrapping string hash (matching JS charCodeAt).
|
||||||
|
#[inline]
|
||||||
|
pub fn hash31(s: &str) -> i32 {
|
||||||
|
let mut h: i32 = 0;
|
||||||
|
if s.is_ascii() {
|
||||||
|
for &b in s.as_bytes() {
|
||||||
|
h = h.wrapping_mul(31).wrapping_add(b as i32);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for ch in s.encode_utf16() {
|
||||||
|
h = h.wrapping_mul(31).wrapping_add(ch as i32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stateless 32-bit pair hash normalised to [0, 1].
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn rnd2(x: i32, y: i32) -> f64 {
|
||||||
|
let a = (x as u32).wrapping_mul(1597334673);
|
||||||
|
let b = (y as u32).wrapping_mul(3812015801);
|
||||||
|
let v = (a ^ b).wrapping_mul(1597334673);
|
||||||
|
let v = v ^ (v >> 16);
|
||||||
|
(v as f64) / 4294967295.0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stateless channel-addressed board sampler:
|
||||||
|
/// base = hash31(code) ^ salt
|
||||||
|
/// p(tag, i) = rnd2(base + tag * 7919, bucket * 31 + i)
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct Sampler {
|
||||||
|
base: i32,
|
||||||
|
b31: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sampler {
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn new(code_hash: i32, bucket: i64, salt: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
base: code_hash ^ (salt as i32),
|
||||||
|
b31: (bucket as i32).wrapping_mul(31),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn from_code(code: &str, bucket: i64, salt: u32) -> Self {
|
||||||
|
Self::new(hash31(code), bucket, salt)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn p(self, tag: i32, i: i32) -> f64 {
|
||||||
|
rnd2(
|
||||||
|
self.base.wrapping_add(tag.wrapping_mul(7919)),
|
||||||
|
self.b31.wrapping_add(i),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Format an unsigned 32-bit integer in lowercase base-36 (for `LZ-<base36>`).
|
||||||
|
pub fn base36_u32(mut n: u32) -> String {
|
||||||
|
const DIGITS: &[u8; 36] = b"0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
|
if n == 0 {
|
||||||
|
return "0".to_string();
|
||||||
|
}
|
||||||
|
let mut buf = [0u8; 8];
|
||||||
|
let mut i = buf.len();
|
||||||
|
while n > 0 {
|
||||||
|
i -= 1;
|
||||||
|
buf[i] = DIGITS[(n % 36) as usize];
|
||||||
|
n /= 36;
|
||||||
|
}
|
||||||
|
std::str::from_utf8(&buf[i..]).unwrap().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Round to nearest 10 (`floor(x/10 + 0.5) * 10`).
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn round10(x: f64) -> i64 {
|
||||||
|
if x <= 0.0 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
((x / 10.0 + 0.5).floor() as i64) * 10
|
||||||
|
}
|
||||||
+589
@@ -0,0 +1,589 @@
|
|||||||
|
use crate::generator::{BoardScratch, compute_pay, make_job_id_num};
|
||||||
|
use crate::geo::{
|
||||||
|
HemisphereSeason, NM, TimeInfo, destination_point, dist_m, season_at,
|
||||||
|
};
|
||||||
|
use crate::model::{BoardOptions, Coord, Job, JobKind, PadKind, Plane, VisitStop};
|
||||||
|
use crate::rng::{BUCKET_MS, Sampler, base36_u32, hash31};
|
||||||
|
use crate::world::{IndexedAirport, Neighbor, WorldIndex};
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn filter_cached_pads(
|
||||||
|
world: &WorldIndex,
|
||||||
|
cached: &[Neighbor],
|
||||||
|
max_m: f64,
|
||||||
|
ceiling_m: f64,
|
||||||
|
big_rotor: bool,
|
||||||
|
out: &mut Vec<Neighbor>,
|
||||||
|
) {
|
||||||
|
out.clear();
|
||||||
|
for &nb in cached {
|
||||||
|
if nb.dist_m > max_m {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let p = &world.pads[nb.idx as usize];
|
||||||
|
if p.raw.elev > ceiling_m {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if big_rotor && p.raw.pad.as_ref().is_some_and(|pad| pad.small) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
out.push(nb);
|
||||||
|
if out.len() == 12 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
struct PadTemplate {
|
||||||
|
kind: JobKind,
|
||||||
|
what: &'static str,
|
||||||
|
pax_min: u32,
|
||||||
|
pax_max: u32,
|
||||||
|
kg_min: u32,
|
||||||
|
kg_max: u32,
|
||||||
|
prem: f64,
|
||||||
|
first_hop: bool,
|
||||||
|
tilt_only: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
const HOSPITAL_TEMPLATES: &[PadTemplate] = &[
|
||||||
|
PadTemplate { kind: JobKind::Medical, what: "Hospital transfer", pax_min: 1, pax_max: 2, kg_min: 0, kg_max: 0, prem: 1.15, first_hop: false, tilt_only: false },
|
||||||
|
PadTemplate { kind: JobKind::Medical, what: "Organ transport", pax_min: 0, pax_max: 0, kg_min: 15, kg_max: 40, prem: 1.30, first_hop: false, tilt_only: false },
|
||||||
|
PadTemplate { kind: JobKind::Medical, what: "Emergency medevac", pax_min: 1, pax_max: 2, kg_min: 0, kg_max: 0, prem: 1.35, first_hop: true, tilt_only: false },
|
||||||
|
];
|
||||||
|
|
||||||
|
const CITY_TEMPLATES: &[PadTemplate] = &[
|
||||||
|
PadTemplate { kind: JobKind::Vip, what: "City VIP transfer", pax_min: 1, pax_max: 4, kg_min: 0, kg_max: 0, prem: 1.10, first_hop: false, tilt_only: false },
|
||||||
|
PadTemplate { kind: JobKind::Pax, what: "Heliport shuttle", pax_min: 2, pax_max: 5, kg_min: 0, kg_max: 0, prem: 1.05, first_hop: false, tilt_only: false },
|
||||||
|
PadTemplate { kind: JobKind::Exec, what: "Executive tiltrotor shuttle", pax_min: 2, pax_max: 8, kg_min: 0, kg_max: 0, prem: 1.25, first_hop: false, tilt_only: true },
|
||||||
|
];
|
||||||
|
|
||||||
|
const REMOTE_TEMPLATES: &[PadTemplate] = &[
|
||||||
|
PadTemplate { kind: JobKind::Pax, what: "Lodge guest transfer", pax_min: 2, pax_max: 5, kg_min: 0, kg_max: 0, prem: 1.15, first_hop: false, tilt_only: false },
|
||||||
|
PadTemplate { kind: JobKind::Cargo, what: "Backcountry supplies", pax_min: 0, pax_max: 0, kg_min: 120, kg_max: 400, prem: 1.15, first_hop: false, tilt_only: false },
|
||||||
|
PadTemplate { kind: JobKind::Medical, what: "Remote medevac", pax_min: 1, pax_max: 2, kg_min: 0, kg_max: 0, prem: 1.30, first_hop: true, tilt_only: false },
|
||||||
|
];
|
||||||
|
|
||||||
|
const RIG_TEMPLATES: &[PadTemplate] = &[
|
||||||
|
PadTemplate { kind: JobKind::Pax, what: "Offshore crew change", pax_min: 3, pax_max: 10, kg_min: 0, kg_max: 0, prem: 1.25, first_hop: false, tilt_only: false },
|
||||||
|
PadTemplate { kind: JobKind::Cargo, what: "Rig parts delivery", pax_min: 0, pax_max: 0, kg_min: 150, kg_max: 500, prem: 1.20, first_hop: false, tilt_only: false },
|
||||||
|
];
|
||||||
|
|
||||||
|
const SHIP_TEMPLATES: &[PadTemplate] = &[
|
||||||
|
PadTemplate { kind: JobKind::Vip, what: "Superyacht VIP transfer", pax_min: 1, pax_max: 4, kg_min: 0, kg_max: 0, prem: 1.30, first_hop: false, tilt_only: false },
|
||||||
|
PadTemplate { kind: JobKind::Cargo, what: "Vessel resupply", pax_min: 0, pax_max: 0, kg_min: 100, kg_max: 350, prem: 1.20, first_hop: false, tilt_only: false },
|
||||||
|
PadTemplate { kind: JobKind::Pax, what: "Marine pilot transfer", pax_min: 1, pax_max: 3, kg_min: 0, kg_max: 0, prem: 1.15, first_hop: false, tilt_only: false },
|
||||||
|
];
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn is_yacht_name(name: &str) -> bool {
|
||||||
|
if name == "Superyacht" {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
let bytes = name.as_bytes();
|
||||||
|
if bytes.len() >= 3 && bytes[0] == b'M' && bytes[1] == b'Y' && !bytes[2].is_ascii_alphanumeric() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if bytes.len() >= 4
|
||||||
|
&& bytes[0] == b'M'
|
||||||
|
&& bytes[1] == b'/'
|
||||||
|
&& bytes[2] == b'Y'
|
||||||
|
&& !bytes[3].is_ascii_alphanumeric()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
name == "MY" || name == "M/Y"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn is_remote_lodge(name: &str) -> bool {
|
||||||
|
let lower = name.to_ascii_lowercase();
|
||||||
|
lower.contains("lodge") || lower.contains("resort") || lower.contains("cabin")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Section 5: Rotor pad jobs, landing tours / heli-ski, and cargo-hook slings.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn generate_rotor_board(
|
||||||
|
world: &WorldIndex,
|
||||||
|
origin_idx: usize,
|
||||||
|
origin: &IndexedAirport,
|
||||||
|
plane: &Plane,
|
||||||
|
bucket: i64,
|
||||||
|
sampler: Sampler,
|
||||||
|
tinfo: &TimeInfo,
|
||||||
|
opts: &BoardOptions<'_>,
|
||||||
|
id_suffix: &str,
|
||||||
|
scratch: &mut BoardScratch,
|
||||||
|
out: &mut Vec<Job>,
|
||||||
|
) {
|
||||||
|
let rotor = match &plane.rotor {
|
||||||
|
Some(r) => r,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
let expires = (bucket + 1) * BUCKET_MS;
|
||||||
|
let range_nm = plane.effective_range_nm();
|
||||||
|
let tilt_scale = if rotor.tilt { 1.8 } else { 1.0 };
|
||||||
|
let max_pad_m = (range_nm * 0.45).min(160.0 * tilt_scale) * NM;
|
||||||
|
let ceiling_m = plane.effective_ceiling_ft() * 0.3048;
|
||||||
|
|
||||||
|
// 5.1 Pad jobs
|
||||||
|
for (l, &pkind) in PadKind::ALL.iter().enumerate() {
|
||||||
|
filter_cached_pads(
|
||||||
|
world,
|
||||||
|
&scratch.origin_pads[pkind as usize],
|
||||||
|
max_pad_m,
|
||||||
|
ceiling_m,
|
||||||
|
rotor.big,
|
||||||
|
&mut scratch.pad_buf,
|
||||||
|
);
|
||||||
|
if scratch.pad_buf.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let templates = match pkind {
|
||||||
|
PadKind::Hospital => HOSPITAL_TEMPLATES,
|
||||||
|
PadKind::City => CITY_TEMPLATES,
|
||||||
|
PadKind::Remote => REMOTE_TEMPLATES,
|
||||||
|
PadKind::Rig => RIG_TEMPLATES,
|
||||||
|
PadKind::Ship => SHIP_TEMPLATES,
|
||||||
|
};
|
||||||
|
|
||||||
|
for c in 0..pkind.attempts() {
|
||||||
|
if scratch.pad_buf.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let tag = 80 + (l as i32) * 4 + (c as i32);
|
||||||
|
if sampler.p(tag, 0) > pkind.gate() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let pick_idx = ((sampler.p(tag, 1).powf(1.4)) * (scratch.pad_buf.len() as f64)).floor()
|
||||||
|
as usize;
|
||||||
|
let nb = scratch.pad_buf.remove(pick_idx.min(scratch.pad_buf.len() - 1));
|
||||||
|
let dest = &world.pads[nb.idx as usize];
|
||||||
|
|
||||||
|
// Filter job list per Section 5.1 step 3
|
||||||
|
let mut eligible: smallvec::SmallVec<[&PadTemplate; 4]> = smallvec::SmallVec::new();
|
||||||
|
for tpl in templates {
|
||||||
|
if tpl.tilt_only && !rotor.tilt {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if pkind == PadKind::Ship {
|
||||||
|
if is_yacht_name(&dest.raw.name) {
|
||||||
|
if !matches!(tpl.kind, JobKind::Vip | JobKind::Cargo) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else if tpl.kind == JobKind::Vip {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pkind == PadKind::Remote && !is_remote_lodge(&dest.raw.name) && tpl.kind == JobKind::Pax {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
eligible.push(tpl);
|
||||||
|
}
|
||||||
|
if eligible.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let tpl_idx = (sampler.p(tag, 2) * (eligible.len() as f64)).floor() as usize;
|
||||||
|
let tpl = eligible[tpl_idx.min(eligible.len() - 1)];
|
||||||
|
|
||||||
|
// Optional first hop for medevac (5-40 NM away)
|
||||||
|
let mut visits = Vec::with_capacity(2);
|
||||||
|
let mut total_m = 0.0f64;
|
||||||
|
if tpl.first_hop {
|
||||||
|
let first_kind = if pkind == PadKind::Hospital {
|
||||||
|
PadKind::Remote
|
||||||
|
} else {
|
||||||
|
PadKind::Hospital
|
||||||
|
};
|
||||||
|
filter_cached_pads(
|
||||||
|
world,
|
||||||
|
&scratch.origin_pads[first_kind as usize],
|
||||||
|
40.0 * NM,
|
||||||
|
ceiling_m,
|
||||||
|
rotor.big,
|
||||||
|
&mut scratch.pad_buf2,
|
||||||
|
);
|
||||||
|
if let Some(fh) = scratch
|
||||||
|
.pad_buf2
|
||||||
|
.iter()
|
||||||
|
.find(|n| n.dist_m >= 5.0 * NM && n.dist_m <= 40.0 * NM)
|
||||||
|
{
|
||||||
|
let fp = &world.pads[fh.idx as usize];
|
||||||
|
total_m += fh.dist_m;
|
||||||
|
total_m += dist_m(&fp.geo, &dest.geo);
|
||||||
|
total_m += nb.dist_m;
|
||||||
|
visits.push(VisitStop {
|
||||||
|
code: fp.raw.code.clone(),
|
||||||
|
lat: fp.raw.lat,
|
||||||
|
lon: fp.raw.lon,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
total_m = 2.0 * nb.dist_m;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
total_m = 2.0 * nb.dist_m;
|
||||||
|
}
|
||||||
|
visits.push(VisitStop {
|
||||||
|
code: dest.raw.code.clone(),
|
||||||
|
lat: dest.raw.lat,
|
||||||
|
lon: dest.raw.lon,
|
||||||
|
});
|
||||||
|
|
||||||
|
let nm = total_m / NM;
|
||||||
|
if nm > range_nm * 0.92 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let seat_scale = ((plane.seats as f64) / 5.0).max(1.0);
|
||||||
|
let cargo_scale = (plane.cargo_kg / 450.0).max(1.0);
|
||||||
|
let p3 = sampler.p(tag, 3);
|
||||||
|
let pax = if tpl.pax_max > 0 && plane.seats > 0 {
|
||||||
|
let base_p = tpl.pax_min as f64
|
||||||
|
+ (p3 * ((tpl.pax_max - tpl.pax_min + 1) as f64)).floor();
|
||||||
|
(base_p * seat_scale).round().min(plane.seats as f64).max(1.0) as u32
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
let kg = if tpl.kg_max > 0 && plane.cargo_kg > 0.0 {
|
||||||
|
let base_k = tpl.kg_min as f64
|
||||||
|
+ (p3 * ((tpl.kg_max - tpl.kg_min) as f64)).round();
|
||||||
|
(((base_k * cargo_scale) / 10.0).round() * 10.0)
|
||||||
|
.min(plane.cargo_kg)
|
||||||
|
.max(10.0) as u32
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
let overshoot = (nm / (range_nm * 0.45).max(1.0) - 1.0).max(0.0);
|
||||||
|
let rate_ratio = (180.0 / plane.effective_cruise_kt()).clamp(0.8, 1.6);
|
||||||
|
let prem = tpl.prem
|
||||||
|
* rate_ratio
|
||||||
|
* (1.0 + 0.25 * overshoot)
|
||||||
|
* if rotor.tilt { 1.1 } else { 1.0 };
|
||||||
|
|
||||||
|
let pay = compute_pay(nm, pax, kg, tpl.kind, prem, opts.level, false);
|
||||||
|
out.push(Job {
|
||||||
|
id: make_job_id_num(&mut scratch.ibuf, &origin.raw.code, bucket, tag, id_suffix),
|
||||||
|
from: origin.raw.code.clone(),
|
||||||
|
to: origin.raw.code.clone(),
|
||||||
|
kind: tpl.kind,
|
||||||
|
pax,
|
||||||
|
kg,
|
||||||
|
nm,
|
||||||
|
pay,
|
||||||
|
expires,
|
||||||
|
what: Some(format!("{} ({})", tpl.what, dest.raw.name)),
|
||||||
|
season: None,
|
||||||
|
short: false,
|
||||||
|
big_charter: false,
|
||||||
|
water: false,
|
||||||
|
pad: true,
|
||||||
|
sling: false,
|
||||||
|
is_contract: false,
|
||||||
|
photo: None,
|
||||||
|
via: None,
|
||||||
|
work_point: Some(Coord {
|
||||||
|
lat: dest.raw.lat,
|
||||||
|
lon: dest.raw.lon,
|
||||||
|
}),
|
||||||
|
visits,
|
||||||
|
company: None,
|
||||||
|
event: None,
|
||||||
|
deadline_min: None,
|
||||||
|
sea_state: None,
|
||||||
|
passes: None,
|
||||||
|
min_runway_m: None,
|
||||||
|
dest_elev_m: Some(dest.raw.elev),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5.2 Landing tours (tag 140, 70%) and heli-ski (tag 141, winter, >= 9000 ft)
|
||||||
|
if let Some(cache) = world.neighbor_cache.get(origin_idx) {
|
||||||
|
let max_alt_m = ceiling_m - 900.0;
|
||||||
|
let mtn_sights: smallvec::SmallVec<[usize; 8]> = cache
|
||||||
|
.nearby_sights
|
||||||
|
.iter()
|
||||||
|
.filter_map(|nb| {
|
||||||
|
if nb.dist_m < 6.0 * NM || nb.dist_m > 45.0 * NM {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let s = &world.sights[nb.idx as usize];
|
||||||
|
let s_alt_m = s.raw.alt * 0.3048;
|
||||||
|
if s.is_mountain && s_alt_m <= max_alt_m {
|
||||||
|
Some(nb.idx as usize)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if !mtn_sights.is_empty() && sampler.p(140, 0) < 0.70 {
|
||||||
|
let pick = (sampler.p(140, 1) * (mtn_sights.len() as f64)).floor() as usize;
|
||||||
|
let s = &world.sights[mtn_sights[pick.min(mtn_sights.len() - 1)]];
|
||||||
|
let (lz_lat, lz_lon) = destination_point(&s.geo, sampler.p(140, 2) * 6.283185, s.raw.r.max(800.0));
|
||||||
|
let lz_code = format!("LZ-{}", base36_u32(hash31(&s.raw.name) as u32));
|
||||||
|
let dm = dist_m(&origin.geo, &s.geo);
|
||||||
|
let nm = (2.0 * dm) / NM;
|
||||||
|
if nm <= range_nm * 0.92 {
|
||||||
|
let pax = (1 + (sampler.p(140, 3) * (plane.seats.min(6) as f64)).floor() as u32)
|
||||||
|
.min(plane.seats.max(1));
|
||||||
|
let pay = compute_pay(nm, pax, 0, JobKind::Tour, 1.2, opts.level, false);
|
||||||
|
out.push(Job {
|
||||||
|
id: format!("{}-{}-140{}", origin.raw.code, bucket, id_suffix),
|
||||||
|
from: origin.raw.code.clone(),
|
||||||
|
to: origin.raw.code.clone(),
|
||||||
|
kind: JobKind::Tour,
|
||||||
|
pax,
|
||||||
|
kg: 0,
|
||||||
|
nm,
|
||||||
|
pay,
|
||||||
|
expires,
|
||||||
|
what: Some(format!("Mountain landing at {}", s.raw.name)),
|
||||||
|
season: None,
|
||||||
|
short: false,
|
||||||
|
big_charter: false,
|
||||||
|
water: false,
|
||||||
|
pad: true,
|
||||||
|
sling: false,
|
||||||
|
is_contract: false,
|
||||||
|
photo: None,
|
||||||
|
via: None,
|
||||||
|
work_point: Some(Coord {
|
||||||
|
lat: lz_lat,
|
||||||
|
lon: lz_lon,
|
||||||
|
}),
|
||||||
|
visits: vec![VisitStop {
|
||||||
|
code: lz_code.clone(),
|
||||||
|
lat: lz_lat,
|
||||||
|
lon: lz_lon,
|
||||||
|
}],
|
||||||
|
company: None,
|
||||||
|
event: None,
|
||||||
|
deadline_min: None,
|
||||||
|
sea_state: None,
|
||||||
|
passes: None,
|
||||||
|
min_runway_m: None,
|
||||||
|
dest_elev_m: Some(s.raw.alt * 0.3048),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Winter heli-ski (tag 141) if >= 9000 ft
|
||||||
|
if s.raw.alt >= 9000.0
|
||||||
|
&& season_at(tinfo.month, s.raw.lat) == HemisphereSeason::Winter
|
||||||
|
{
|
||||||
|
let ski_pax = plane.seats.min(8).max(1);
|
||||||
|
let ski_pay = compute_pay(nm, ski_pax, 0, JobKind::Tour, 1.35, opts.level, false);
|
||||||
|
out.push(Job {
|
||||||
|
id: format!("{}-{}-141{}", origin.raw.code, bucket, id_suffix),
|
||||||
|
from: origin.raw.code.clone(),
|
||||||
|
to: origin.raw.code.clone(),
|
||||||
|
kind: JobKind::Tour,
|
||||||
|
pax: ski_pax,
|
||||||
|
kg: 0,
|
||||||
|
nm,
|
||||||
|
pay: ski_pay,
|
||||||
|
expires,
|
||||||
|
what: Some(format!("Heli-ski {}", s.raw.name)),
|
||||||
|
season: Some("ski"),
|
||||||
|
short: false,
|
||||||
|
big_charter: false,
|
||||||
|
water: false,
|
||||||
|
pad: true,
|
||||||
|
sling: false,
|
||||||
|
is_contract: false,
|
||||||
|
photo: None,
|
||||||
|
via: None,
|
||||||
|
work_point: Some(Coord {
|
||||||
|
lat: lz_lat,
|
||||||
|
lon: lz_lon,
|
||||||
|
}),
|
||||||
|
visits: vec![VisitStop {
|
||||||
|
code: lz_code,
|
||||||
|
lat: lz_lat,
|
||||||
|
lon: lz_lon,
|
||||||
|
}],
|
||||||
|
company: None,
|
||||||
|
event: None,
|
||||||
|
deadline_min: None,
|
||||||
|
sea_state: None,
|
||||||
|
passes: None,
|
||||||
|
min_runway_m: None,
|
||||||
|
dest_elev_m: Some(s.raw.alt * 0.3048),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5.3 Slings (cargo hook only, tags 120 + k)
|
||||||
|
if let Some(hook) = &plane.hook {
|
||||||
|
for (k, &pkind) in PadKind::SLING_ORDER.iter().enumerate() {
|
||||||
|
let tag = 120 + k as i32;
|
||||||
|
let gate = if pkind == PadKind::Remote { 0.80 } else { 0.50 };
|
||||||
|
if sampler.p(tag, 0) > gate {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
filter_cached_pads(
|
||||||
|
world,
|
||||||
|
&scratch.origin_pads[pkind as usize],
|
||||||
|
max_pad_m.min(80.0 * NM),
|
||||||
|
ceiling_m,
|
||||||
|
rotor.big,
|
||||||
|
&mut scratch.pad_buf,
|
||||||
|
);
|
||||||
|
if scratch.pad_buf.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let pick = (sampler.p(tag, 1) * (scratch.pad_buf.len() as f64)).floor() as usize;
|
||||||
|
let nb = scratch.pad_buf[pick.min(scratch.pad_buf.len() - 1)];
|
||||||
|
let dest = &world.pads[nb.idx as usize];
|
||||||
|
|
||||||
|
// Density altitude derating on hook capacity
|
||||||
|
let elev_factor = (1.0 - (dest.raw.elev / 6000.0) * 0.35).clamp(0.4, 1.0);
|
||||||
|
let max_hook_kg = hook.kg * elev_factor;
|
||||||
|
if max_hook_kg < 100.0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let load_kg = (((0.35 + sampler.p(tag, 3) * 0.60) * max_hook_kg) / 10.0).round() * 10.0;
|
||||||
|
let kg = load_kg.clamp(100.0, max_hook_kg) as u32;
|
||||||
|
let nm = (2.0 * nb.dist_m) / NM;
|
||||||
|
if nm > range_nm * 0.92 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let pay = compute_pay(nm, 0, kg, JobKind::Lift, 1.25, opts.level, false);
|
||||||
|
out.push(Job {
|
||||||
|
id: make_job_id_num(&mut scratch.ibuf, &origin.raw.code, bucket, tag, id_suffix),
|
||||||
|
from: origin.raw.code.clone(),
|
||||||
|
to: origin.raw.code.clone(),
|
||||||
|
kind: JobKind::Lift,
|
||||||
|
pax: 0,
|
||||||
|
kg,
|
||||||
|
nm,
|
||||||
|
pay,
|
||||||
|
expires,
|
||||||
|
what: Some(format!("Sling load to {}", dest.raw.name)),
|
||||||
|
season: None,
|
||||||
|
short: false,
|
||||||
|
big_charter: false,
|
||||||
|
water: false,
|
||||||
|
pad: true,
|
||||||
|
sling: true,
|
||||||
|
is_contract: false,
|
||||||
|
photo: None,
|
||||||
|
via: None,
|
||||||
|
work_point: Some(Coord {
|
||||||
|
lat: dest.raw.lat,
|
||||||
|
lon: dest.raw.lon,
|
||||||
|
}),
|
||||||
|
visits: vec![VisitStop {
|
||||||
|
code: dest.raw.code.clone(),
|
||||||
|
lat: dest.raw.lat,
|
||||||
|
lon: dest.raw.lon,
|
||||||
|
}],
|
||||||
|
company: None,
|
||||||
|
event: None,
|
||||||
|
deadline_min: None,
|
||||||
|
sea_state: None,
|
||||||
|
passes: None,
|
||||||
|
min_runway_m: None,
|
||||||
|
dest_elev_m: Some(dest.raw.elev),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Section 6: Seaplane module (`~w` salt, max 3 jobs, x1.3 seaplane premium).
|
||||||
|
pub fn generate_seaplane_jobs(
|
||||||
|
world: &WorldIndex,
|
||||||
|
origin_idx: usize,
|
||||||
|
origin: &IndexedAirport,
|
||||||
|
plane: &Plane,
|
||||||
|
bucket: i64,
|
||||||
|
opts: &BoardOptions<'_>,
|
||||||
|
out: &mut Vec<Job>,
|
||||||
|
) {
|
||||||
|
let sea_code = format!("{}~w", origin.raw.code);
|
||||||
|
let sampler = Sampler::from_code(&sea_code, bucket, opts.salt);
|
||||||
|
let expires = (bucket + 1) * BUCKET_MS;
|
||||||
|
let range_cap = plane.effective_range_nm() * 0.92;
|
||||||
|
|
||||||
|
let Some(cache) = world.neighbor_cache.get(origin_idx) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if cache.nearby_sea.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let max_jobs = cache.nearby_sea.len().min(3);
|
||||||
|
let mut used: smallvec::SmallVec<[u32; 3]> = smallvec::SmallVec::new();
|
||||||
|
for i in 0..max_jobs {
|
||||||
|
let tag = 150 + i as i32;
|
||||||
|
let pick = (sampler.p(tag, 1) * (cache.nearby_sea.len() as f64)).floor() as usize;
|
||||||
|
let nb = cache.nearby_sea[pick.min(cache.nearby_sea.len() - 1)];
|
||||||
|
if used.contains(&nb.idx) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let nm = nb.dist_m / NM;
|
||||||
|
if nm > range_cap {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
used.push(nb.idx);
|
||||||
|
let base = &world.sea_bases[nb.idx as usize];
|
||||||
|
let lower = base.raw.name.to_ascii_lowercase();
|
||||||
|
let (kind, what) = if lower.contains("lodge") {
|
||||||
|
(JobKind::Pax, format!("Floatplane lodge guests ({})", base.raw.name))
|
||||||
|
} else if lower.contains("harbour") || lower.contains("harbor") {
|
||||||
|
(JobKind::Cargo, format!("Harbour freight ({})", base.raw.name))
|
||||||
|
} else if sampler.p(tag, 2) < 0.65 {
|
||||||
|
(JobKind::Pax, format!("Seaplane charter ({})", base.raw.name))
|
||||||
|
} else {
|
||||||
|
(JobKind::Mail, format!("Waterway mail ({})", base.raw.name))
|
||||||
|
};
|
||||||
|
|
||||||
|
let scale = ((plane.seats as f64) / 5.0).max(1.0);
|
||||||
|
let (pax, kg) = if kind == JobKind::Pax && plane.seats > 0 {
|
||||||
|
let p = ((1.0 + (sampler.p(tag, 3) * 4.0).floor()) * scale)
|
||||||
|
.round()
|
||||||
|
.min(plane.seats as f64)
|
||||||
|
.max(1.0) as u32;
|
||||||
|
(p, 0)
|
||||||
|
} else {
|
||||||
|
let k = (((60.0 + sampler.p(tag, 3) * 240.0) * scale) / 10.0).round() * 10.0;
|
||||||
|
(0, k.min(plane.cargo_kg.max(50.0)) as u32)
|
||||||
|
};
|
||||||
|
|
||||||
|
let pay = compute_pay(nm, pax, kg, kind, 1.30, opts.level, false);
|
||||||
|
out.push(Job {
|
||||||
|
id: format!("{}-{}-sea{}", origin.raw.code, bucket, i),
|
||||||
|
from: origin.raw.code.clone(),
|
||||||
|
to: base.raw.code.clone(),
|
||||||
|
kind,
|
||||||
|
pax,
|
||||||
|
kg,
|
||||||
|
nm,
|
||||||
|
pay,
|
||||||
|
expires,
|
||||||
|
what: Some(what),
|
||||||
|
season: None,
|
||||||
|
short: false,
|
||||||
|
big_charter: false,
|
||||||
|
water: true,
|
||||||
|
pad: false,
|
||||||
|
sling: false,
|
||||||
|
is_contract: false,
|
||||||
|
photo: None,
|
||||||
|
via: None,
|
||||||
|
work_point: None,
|
||||||
|
visits: Vec::new(),
|
||||||
|
company: None,
|
||||||
|
event: None,
|
||||||
|
deadline_min: None,
|
||||||
|
sea_state: None,
|
||||||
|
passes: None,
|
||||||
|
min_runway_m: None,
|
||||||
|
dest_elev_m: Some(0.0),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
+548
@@ -0,0 +1,548 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufWriter, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use rayon::prelude::*;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
use crate::generator::{BoardScratch, generate_board_into};
|
||||||
|
use crate::model::{BoardOptions, Job, JobKind, Plane};
|
||||||
|
use crate::rng::{BUCKET_MS, board_salt, bucket_of, day_of, shop_salt};
|
||||||
|
use crate::world::WorldIndex;
|
||||||
|
|
||||||
|
/// Fixed-bucket histogram for O(1) thread-local mergeable net $/hr percentiles.
|
||||||
|
/// Covers $0 to $500,000/hr in 500 bins of $1,000/hr (+ overflow bin).
|
||||||
|
const HIST_BINS: usize = 502;
|
||||||
|
const HIST_STEP: f64 = 1000.0;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct PayHistogram {
|
||||||
|
pub bins: [u64; HIST_BINS],
|
||||||
|
pub total: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for PayHistogram {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
bins: [0; HIST_BINS],
|
||||||
|
total: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PayHistogram {
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn record(&mut self, net_per_hr: f64) {
|
||||||
|
let idx = if net_per_hr <= 0.0 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
((net_per_hr / HIST_STEP) as usize + 1).min(HIST_BINS - 1)
|
||||||
|
};
|
||||||
|
self.bins[idx] += 1;
|
||||||
|
self.total += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn merge(&mut self, other: &Self) {
|
||||||
|
for i in 0..HIST_BINS {
|
||||||
|
self.bins[i] += other.bins[i];
|
||||||
|
}
|
||||||
|
self.total += other.total;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn quantile(&self, q: f64) -> f64 {
|
||||||
|
if self.total == 0 {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
let target = ((self.total as f64) * q).ceil() as u64;
|
||||||
|
let mut acc = 0u64;
|
||||||
|
for (i, &c) in self.bins.iter().enumerate() {
|
||||||
|
acc += c;
|
||||||
|
if acc >= target {
|
||||||
|
return if i == 0 {
|
||||||
|
0.0
|
||||||
|
} else {
|
||||||
|
((i - 1) as f64 + 0.5) * HIST_STEP
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
((HIST_BINS - 1) as f64) * HIST_STEP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
pub struct KindAccum {
|
||||||
|
pub count: u64,
|
||||||
|
pub sum_pay: f64,
|
||||||
|
pub sum_net: f64,
|
||||||
|
pub sum_net_per_hr: f64,
|
||||||
|
pub sum_nm: f64,
|
||||||
|
pub sum_pax: u64,
|
||||||
|
pub sum_kg: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KindAccum {
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn add(&mut self, j: &Job, net: i64, net_hr: f64) {
|
||||||
|
self.count += 1;
|
||||||
|
self.sum_pay += j.pay as f64;
|
||||||
|
self.sum_net += net as f64;
|
||||||
|
self.sum_net_per_hr += net_hr;
|
||||||
|
self.sum_nm += j.nm;
|
||||||
|
self.sum_pax += j.pax as u64;
|
||||||
|
self.sum_kg += j.kg as u64;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn merge(&mut self, o: &Self) {
|
||||||
|
self.count += o.count;
|
||||||
|
self.sum_pay += o.sum_pay;
|
||||||
|
self.sum_net += o.sum_net;
|
||||||
|
self.sum_net_per_hr += o.sum_net_per_hr;
|
||||||
|
self.sum_nm += o.sum_nm;
|
||||||
|
self.sum_pax += o.sum_pax;
|
||||||
|
self.sum_kg += o.sum_kg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct PlaneAccum {
|
||||||
|
pub boards: u64,
|
||||||
|
pub jobs: u64,
|
||||||
|
pub eligible_jobs: u64,
|
||||||
|
pub short_jobs: u64,
|
||||||
|
pub photo_jobs: u64,
|
||||||
|
pub sum_pay: f64,
|
||||||
|
pub sum_net: f64,
|
||||||
|
pub sum_net_per_hr: f64,
|
||||||
|
pub sum_best_net_per_hr: f64,
|
||||||
|
pub sum_nm: f64,
|
||||||
|
pub by_size_jobs: [u64; 3],
|
||||||
|
pub by_size_net_hr: [f64; 3],
|
||||||
|
pub by_kind: [KindAccum; JobKind::COUNT],
|
||||||
|
pub hist: PayHistogram,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for PlaneAccum {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
boards: 0,
|
||||||
|
jobs: 0,
|
||||||
|
eligible_jobs: 0,
|
||||||
|
short_jobs: 0,
|
||||||
|
photo_jobs: 0,
|
||||||
|
sum_pay: 0.0,
|
||||||
|
sum_net: 0.0,
|
||||||
|
sum_net_per_hr: 0.0,
|
||||||
|
sum_best_net_per_hr: 0.0,
|
||||||
|
sum_nm: 0.0,
|
||||||
|
by_size_jobs: [0; 3],
|
||||||
|
by_size_net_hr: [0.0; 3],
|
||||||
|
by_kind: std::array::from_fn(|_| KindAccum::default()),
|
||||||
|
hist: PayHistogram::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PlaneAccum {
|
||||||
|
pub fn merge(&mut self, o: &Self) {
|
||||||
|
self.boards += o.boards;
|
||||||
|
self.jobs += o.jobs;
|
||||||
|
self.eligible_jobs += o.eligible_jobs;
|
||||||
|
self.short_jobs += o.short_jobs;
|
||||||
|
self.photo_jobs += o.photo_jobs;
|
||||||
|
self.sum_pay += o.sum_pay;
|
||||||
|
self.sum_net += o.sum_net;
|
||||||
|
self.sum_net_per_hr += o.sum_net_per_hr;
|
||||||
|
self.sum_best_net_per_hr += o.sum_best_net_per_hr;
|
||||||
|
self.sum_nm += o.sum_nm;
|
||||||
|
for i in 0..3 {
|
||||||
|
self.by_size_jobs[i] += o.by_size_jobs[i];
|
||||||
|
self.by_size_net_hr[i] += o.by_size_net_hr[i];
|
||||||
|
}
|
||||||
|
for i in 0..JobKind::COUNT {
|
||||||
|
self.by_kind[i].merge(&o.by_kind[i]);
|
||||||
|
}
|
||||||
|
self.hist.merge(&o.hist);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize)]
|
||||||
|
pub struct KindSummary {
|
||||||
|
pub kind: &'static str,
|
||||||
|
pub count: u64,
|
||||||
|
pub share_pct: f64,
|
||||||
|
pub avg_pay: f64,
|
||||||
|
pub avg_net: f64,
|
||||||
|
pub avg_net_per_hr: f64,
|
||||||
|
pub avg_nm: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize)]
|
||||||
|
pub struct PlaneSummary {
|
||||||
|
pub plane_id: String,
|
||||||
|
pub boards: u64,
|
||||||
|
pub jobs: u64,
|
||||||
|
pub avg_jobs_per_board: f64,
|
||||||
|
pub avg_pay: f64,
|
||||||
|
pub avg_net: f64,
|
||||||
|
pub avg_net_per_hr: f64,
|
||||||
|
pub avg_best_net_per_hr: f64,
|
||||||
|
pub p50_net_per_hr: f64,
|
||||||
|
pub p90_net_per_hr: f64,
|
||||||
|
pub p99_net_per_hr: f64,
|
||||||
|
pub avg_nm: f64,
|
||||||
|
pub short_field_pct: f64,
|
||||||
|
pub photo_detour_pct: f64,
|
||||||
|
pub hub_avg_net_per_hr: f64,
|
||||||
|
pub regional_avg_net_per_hr: f64,
|
||||||
|
pub airstrip_avg_net_per_hr: f64,
|
||||||
|
pub by_kind: Vec<KindSummary>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize)]
|
||||||
|
pub struct SimReport {
|
||||||
|
pub airports_simulated: usize,
|
||||||
|
pub buckets_simulated: usize,
|
||||||
|
pub planes_simulated: usize,
|
||||||
|
pub total_boards: u64,
|
||||||
|
pub total_jobs: u64,
|
||||||
|
pub elapsed_secs: f64,
|
||||||
|
pub boards_per_sec: f64,
|
||||||
|
pub jobs_per_sec: f64,
|
||||||
|
pub planes: Vec<PlaneSummary>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub enum ExportMode {
|
||||||
|
StatsOnly,
|
||||||
|
Csv,
|
||||||
|
Ndjson,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SimConfig<'a> {
|
||||||
|
pub master: &'a str,
|
||||||
|
pub start_epoch_ms: i64,
|
||||||
|
pub num_buckets: usize,
|
||||||
|
pub explicit_salt: Option<u32>,
|
||||||
|
pub level: u8,
|
||||||
|
pub aurora: bool,
|
||||||
|
pub fair_weather: bool,
|
||||||
|
pub airport_limit: Option<usize>,
|
||||||
|
pub export_mode: ExportMode,
|
||||||
|
pub out_path: Option<&'a Path>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fast zero-alloc CSV row writer into a byte buffer.
|
||||||
|
fn push_csv_row(
|
||||||
|
buf: &mut Vec<u8>,
|
||||||
|
bucket: i64,
|
||||||
|
plane_id: &str,
|
||||||
|
origin_size: u8,
|
||||||
|
j: &Job,
|
||||||
|
net: i64,
|
||||||
|
net_hr: f64,
|
||||||
|
ibuf: &mut itoa::Buffer,
|
||||||
|
rbuf: &mut ryu::Buffer,
|
||||||
|
) {
|
||||||
|
buf.extend_from_slice(ibuf.format(bucket).as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(plane_id.as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(j.id.as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(j.from.as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(ibuf.format(origin_size).as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(j.to.as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(j.kind.as_str().as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(ibuf.format(j.pax).as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(ibuf.format(j.kg).as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(rbuf.format((j.nm * 100.0).round() / 100.0).as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(ibuf.format(j.pay).as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(ibuf.format(net).as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.extend_from_slice(ibuf.format(net_hr.round() as i64).as_bytes());
|
||||||
|
buf.push(b',');
|
||||||
|
buf.push(if j.short { b'1' } else { b'0' });
|
||||||
|
buf.push(b',');
|
||||||
|
buf.push(if j.photo.is_some() { b'1' } else { b'0' });
|
||||||
|
buf.push(b'\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run a massively parallel global simulation across all selected airports × buckets × planes.
|
||||||
|
pub fn run_simulation(
|
||||||
|
world: &WorldIndex,
|
||||||
|
planes: &[&Plane],
|
||||||
|
cfg: &SimConfig<'_>,
|
||||||
|
) -> std::io::Result<SimReport> {
|
||||||
|
let t0 = Instant::now();
|
||||||
|
let start_bucket = bucket_of(cfg.start_epoch_ms);
|
||||||
|
|
||||||
|
// Precompute salts for every bucket (avoids repeated SHA-256 in the hot loop!)
|
||||||
|
let bucket_salts: Vec<(i64, i64, u32, u32)> = (0..cfg.num_buckets)
|
||||||
|
.map(|bi| {
|
||||||
|
let b = start_bucket + (bi as i64);
|
||||||
|
let epoch = b * BUCKET_MS;
|
||||||
|
let salt = cfg.explicit_salt.unwrap_or_else(|| board_salt(cfg.master, b));
|
||||||
|
let ds = shop_salt(cfg.master, day_of(epoch));
|
||||||
|
(b, epoch, salt, ds)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let n_airports = cfg
|
||||||
|
.airport_limit
|
||||||
|
.map(|l| l.min(world.airports.len()))
|
||||||
|
.unwrap_or(world.airports.len());
|
||||||
|
let n_planes = planes.len();
|
||||||
|
|
||||||
|
let writer: Option<Mutex<BufWriter<File>>> = match (cfg.export_mode, cfg.out_path) {
|
||||||
|
(ExportMode::Csv, Some(p)) => {
|
||||||
|
let mut w = BufWriter::with_capacity(4 << 20, File::create(p)?);
|
||||||
|
w.write_all(b"bucket,plane,id,from,origin_size,to,kind,pax,kg,nm,pay,net_pay,net_per_hr,short,photo\n")?;
|
||||||
|
Some(Mutex::new(w))
|
||||||
|
}
|
||||||
|
(ExportMode::Ndjson, Some(p)) => {
|
||||||
|
let w = BufWriter::with_capacity(4 << 20, File::create(p)?);
|
||||||
|
Some(Mutex::new(w))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Parallelize over airports; each worker reuses BoardScratch + per-plane accumulators + I/O buffer!
|
||||||
|
let merged: Vec<PlaneAccum> = (0..n_airports)
|
||||||
|
.into_par_iter()
|
||||||
|
.fold(
|
||||||
|
|| {
|
||||||
|
(
|
||||||
|
BoardScratch::with_capacity(),
|
||||||
|
Vec::<Job>::with_capacity(32),
|
||||||
|
vec![PlaneAccum::default(); n_planes],
|
||||||
|
Vec::<u8>::with_capacity(1 << 18), // 256 KB per-thread write chunk
|
||||||
|
itoa::Buffer::new(),
|
||||||
|
ryu::Buffer::new(),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|(mut scratch, mut jobs, mut accums, mut io_buf, mut ibuf, mut rbuf), ap_idx| {
|
||||||
|
let origin = &world.airports[ap_idx];
|
||||||
|
if origin.is_pad_only {
|
||||||
|
return (scratch, jobs, accums, io_buf, ibuf, rbuf);
|
||||||
|
}
|
||||||
|
let origin_size = (origin.raw.size as usize).min(2);
|
||||||
|
|
||||||
|
for &(b, epoch, salt, ds) in &bucket_salts {
|
||||||
|
let opts = BoardOptions {
|
||||||
|
salt,
|
||||||
|
day_salt: ds,
|
||||||
|
level: cfg.level,
|
||||||
|
first_solo: false,
|
||||||
|
aurora_active: cfg.aurora,
|
||||||
|
fair_weather: cfg.fair_weather,
|
||||||
|
dev: false,
|
||||||
|
fires: &[],
|
||||||
|
event: None,
|
||||||
|
company: None,
|
||||||
|
projects: &[],
|
||||||
|
stored: &[],
|
||||||
|
};
|
||||||
|
|
||||||
|
for (pi, &plane) in planes.iter().enumerate() {
|
||||||
|
generate_board_into(
|
||||||
|
world,
|
||||||
|
ap_idx,
|
||||||
|
epoch,
|
||||||
|
&[plane],
|
||||||
|
&opts,
|
||||||
|
&mut scratch,
|
||||||
|
&mut jobs,
|
||||||
|
);
|
||||||
|
let acc = &mut accums[pi];
|
||||||
|
acc.boards += 1;
|
||||||
|
let mut best_hr = f64::NEG_INFINITY;
|
||||||
|
|
||||||
|
for j in &jobs {
|
||||||
|
let cost = plane.flight_cost(j.nm);
|
||||||
|
let net = j.pay - cost;
|
||||||
|
let secs = plane.flight_time_secs(j.nm, j.visits.len());
|
||||||
|
let net_hr = if secs > 0.0 {
|
||||||
|
(net as f64) / (secs / 3600.0)
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
|
|
||||||
|
acc.jobs += 1;
|
||||||
|
acc.sum_pay += j.pay as f64;
|
||||||
|
acc.sum_net += net as f64;
|
||||||
|
acc.sum_net_per_hr += net_hr;
|
||||||
|
acc.sum_nm += j.nm;
|
||||||
|
if j.short {
|
||||||
|
acc.short_jobs += 1;
|
||||||
|
}
|
||||||
|
if j.photo.is_some() {
|
||||||
|
acc.photo_jobs += 1;
|
||||||
|
}
|
||||||
|
if net_hr > best_hr {
|
||||||
|
best_hr = net_hr;
|
||||||
|
}
|
||||||
|
acc.by_size_jobs[origin_size] += 1;
|
||||||
|
acc.by_size_net_hr[origin_size] += net_hr;
|
||||||
|
acc.by_kind[j.kind.idx()].add(j, net, net_hr);
|
||||||
|
acc.hist.record(net_hr);
|
||||||
|
|
||||||
|
match cfg.export_mode {
|
||||||
|
ExportMode::Csv => {
|
||||||
|
push_csv_row(
|
||||||
|
&mut io_buf,
|
||||||
|
b,
|
||||||
|
&plane.id,
|
||||||
|
origin.raw.size,
|
||||||
|
j,
|
||||||
|
net,
|
||||||
|
net_hr,
|
||||||
|
&mut ibuf,
|
||||||
|
&mut rbuf,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
ExportMode::Ndjson => {
|
||||||
|
if let Ok(line) = serde_json::to_string(j) {
|
||||||
|
io_buf.extend_from_slice(line.as_bytes());
|
||||||
|
io_buf.push(b'\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ExportMode::StatsOnly => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if best_hr.is_finite() {
|
||||||
|
acc.sum_best_net_per_hr += best_hr;
|
||||||
|
acc.eligible_jobs += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if io_buf.len() >= (256 << 10) {
|
||||||
|
if let Some(ref mtx) = writer {
|
||||||
|
if let Ok(mut w) = mtx.lock() {
|
||||||
|
let _ = w.write_all(&io_buf);
|
||||||
|
}
|
||||||
|
io_buf.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush remaining per-airport chunk if large
|
||||||
|
if !io_buf.is_empty() && io_buf.len() >= (64 << 10) {
|
||||||
|
if let Some(ref mtx) = writer {
|
||||||
|
if let Ok(mut w) = mtx.lock() {
|
||||||
|
let _ = w.write_all(&io_buf);
|
||||||
|
}
|
||||||
|
io_buf.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(scratch, jobs, accums, io_buf, ibuf, rbuf)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.map(|(_, _, accums, io_buf, _, _)| {
|
||||||
|
if !io_buf.is_empty() {
|
||||||
|
if let Some(ref mtx) = writer {
|
||||||
|
if let Ok(mut w) = mtx.lock() {
|
||||||
|
let _ = w.write_all(&io_buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
accums
|
||||||
|
})
|
||||||
|
.reduce(
|
||||||
|
|| vec![PlaneAccum::default(); n_planes],
|
||||||
|
|mut a, b| {
|
||||||
|
for i in 0..n_planes {
|
||||||
|
a[i].merge(&b[i]);
|
||||||
|
}
|
||||||
|
a
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some(mtx) = writer {
|
||||||
|
mtx.into_inner().unwrap().flush()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let elapsed = t0.elapsed().as_secs_f64();
|
||||||
|
let mut total_boards = 0u64;
|
||||||
|
let mut total_jobs = 0u64;
|
||||||
|
let mut plane_summaries = Vec::with_capacity(n_planes);
|
||||||
|
|
||||||
|
for (pi, acc) in merged.iter().enumerate() {
|
||||||
|
total_boards += acc.boards;
|
||||||
|
total_jobs += acc.jobs;
|
||||||
|
let jf = (acc.jobs.max(1)) as f64;
|
||||||
|
let bf = (acc.boards.max(1)) as f64;
|
||||||
|
let ef = (acc.eligible_jobs.max(1)) as f64;
|
||||||
|
|
||||||
|
let mut by_kind = Vec::new();
|
||||||
|
for k in JobKind::ALL {
|
||||||
|
let ka = &acc.by_kind[k.idx()];
|
||||||
|
if ka.count == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let kf = ka.count as f64;
|
||||||
|
by_kind.push(KindSummary {
|
||||||
|
kind: k.as_str(),
|
||||||
|
count: ka.count,
|
||||||
|
share_pct: (kf / jf) * 100.0,
|
||||||
|
avg_pay: ka.sum_pay / kf,
|
||||||
|
avg_net: ka.sum_net / kf,
|
||||||
|
avg_net_per_hr: ka.sum_net_per_hr / kf,
|
||||||
|
avg_nm: ka.sum_nm / kf,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
by_kind.sort_by(|a, b| b.count.cmp(&a.count));
|
||||||
|
|
||||||
|
let size_avg = |i: usize| -> f64 {
|
||||||
|
if acc.by_size_jobs[i] > 0 {
|
||||||
|
acc.by_size_net_hr[i] / (acc.by_size_jobs[i] as f64)
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
plane_summaries.push(PlaneSummary {
|
||||||
|
plane_id: planes[pi].id.clone(),
|
||||||
|
boards: acc.boards,
|
||||||
|
jobs: acc.jobs,
|
||||||
|
avg_jobs_per_board: (acc.jobs as f64) / bf,
|
||||||
|
avg_pay: acc.sum_pay / jf,
|
||||||
|
avg_net: acc.sum_net / jf,
|
||||||
|
avg_net_per_hr: acc.sum_net_per_hr / jf,
|
||||||
|
avg_best_net_per_hr: acc.sum_best_net_per_hr / ef,
|
||||||
|
p50_net_per_hr: acc.hist.quantile(0.50),
|
||||||
|
p90_net_per_hr: acc.hist.quantile(0.90),
|
||||||
|
p99_net_per_hr: acc.hist.quantile(0.99),
|
||||||
|
avg_nm: acc.sum_nm / jf,
|
||||||
|
short_field_pct: ((acc.short_jobs as f64) / jf) * 100.0,
|
||||||
|
photo_detour_pct: ((acc.photo_jobs as f64) / jf) * 100.0,
|
||||||
|
hub_avg_net_per_hr: size_avg(0),
|
||||||
|
regional_avg_net_per_hr: size_avg(1),
|
||||||
|
airstrip_avg_net_per_hr: size_avg(2),
|
||||||
|
by_kind,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(SimReport {
|
||||||
|
airports_simulated: n_airports,
|
||||||
|
buckets_simulated: cfg.num_buckets,
|
||||||
|
planes_simulated: n_planes,
|
||||||
|
total_boards,
|
||||||
|
total_jobs,
|
||||||
|
elapsed_secs: elapsed,
|
||||||
|
boards_per_sec: (total_boards as f64) / elapsed.max(1e-9),
|
||||||
|
jobs_per_sec: (total_jobs as f64) / elapsed.max(1e-9),
|
||||||
|
planes: plane_summaries,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,752 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use crate::model::{
|
||||||
|
Airport, Beach, Farm, HookSpec, Pad, PadKind, Plane, RotorSpec, Runway, SeaBase, SeaDock,
|
||||||
|
Sight, WorldData,
|
||||||
|
};
|
||||||
|
use crate::rng::rnd2;
|
||||||
|
|
||||||
|
/// Generate a realistic, deterministic global synthetic world (airports, hubs, regionals,
|
||||||
|
/// airstrips, helipads, seaplane bases, and sights) for large-scale offline simulation
|
||||||
|
/// when no external world dataset is supplied.
|
||||||
|
pub fn generate_synthetic_world(seed: u32, num_airports: usize) -> WorldData {
|
||||||
|
// Population-weighted global regions (center lat, center lon, spread lat, spread lon, country)
|
||||||
|
const REGIONS: &[(f64, f64, f64, f64, &str)] = &[
|
||||||
|
(37.5, -118.0, 9.0, 12.0, "US"), // US West
|
||||||
|
(39.0, -82.0, 8.0, 14.0, "US"), // US East
|
||||||
|
(48.5, 8.0, 7.0, 14.0, "EU"), // Western Europe
|
||||||
|
(52.0, 22.0, 7.0, 12.0, "PL"), // Central/Eastern Europe
|
||||||
|
(35.5, 137.0, 5.0, 6.0, "JP"), // Japan
|
||||||
|
(31.0, 114.0, 9.0, 10.0, "CN"), // East China
|
||||||
|
(-24.0, -48.0, 9.0, 9.0, "BR"), // South America East
|
||||||
|
(-33.0, 147.0, 7.0, 10.0, "AU"), // Australia SE
|
||||||
|
(21.0, -157.0, 2.0, 3.0, "US"), // Hawaii
|
||||||
|
(61.5, -149.0, 4.0, 8.0, "US"), // Alaska
|
||||||
|
(46.5, 8.2, 2.0, 4.0, "CH"), // Alps (high elev)
|
||||||
|
(25.0, 55.0, 5.0, 8.0, "AE"), // Gulf
|
||||||
|
(-41.0, 173.0, 4.0, 4.0, "NZ"), // New Zealand
|
||||||
|
(19.0, -99.0, 5.0, 8.0, "MX"), // Mexico
|
||||||
|
];
|
||||||
|
|
||||||
|
let s = seed as i32;
|
||||||
|
let mut airports = Vec::with_capacity(num_airports);
|
||||||
|
let mut majors = Vec::new();
|
||||||
|
let mut pads = Vec::new();
|
||||||
|
let mut sea_bases = Vec::new();
|
||||||
|
let mut sights = Vec::new();
|
||||||
|
|
||||||
|
for i in 0..num_airports {
|
||||||
|
let ii = i as i32;
|
||||||
|
let reg_idx = (rnd2(s, ii * 13 + 1) * REGIONS.len() as f64).floor() as usize;
|
||||||
|
let (clat, clon, slat, slon, country) = REGIONS[reg_idx.min(REGIONS.len() - 1)];
|
||||||
|
|
||||||
|
// Box-Muller-ish clamped spread using two uniform draws
|
||||||
|
let u1 = rnd2(s, ii * 13 + 2) + rnd2(s, ii * 13 + 3) - 1.0;
|
||||||
|
let u2 = rnd2(s, ii * 13 + 4) + rnd2(s, ii * 13 + 5) - 1.0;
|
||||||
|
let lat = (clat + u1 * slat).clamp(-78.0, 78.0);
|
||||||
|
let mut lon = clon + u2 * slon;
|
||||||
|
if lon > 180.0 {
|
||||||
|
lon -= 360.0;
|
||||||
|
} else if lon < -180.0 {
|
||||||
|
lon += 360.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let r_size = rnd2(s, ii * 13 + 6);
|
||||||
|
let size: u8 = if r_size < 0.08 {
|
||||||
|
0 // hub
|
||||||
|
} else if r_size < 0.30 {
|
||||||
|
1 // regional
|
||||||
|
} else {
|
||||||
|
2 // airstrip
|
||||||
|
};
|
||||||
|
|
||||||
|
let (longest, hard) = match size {
|
||||||
|
0 => (2600.0 + rnd2(s, ii * 13 + 7) * 1600.0, true),
|
||||||
|
1 => (1200.0 + rnd2(s, ii * 13 + 7) * 1400.0, true),
|
||||||
|
_ => {
|
||||||
|
let is_hard = rnd2(s, ii * 13 + 8) < 0.55;
|
||||||
|
(400.0 + rnd2(s, ii * 13 + 7) * 850.0, is_hard)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// High mountain elevation in Alps / mountain pockets
|
||||||
|
let elev = if country == "CH" || rnd2(s, ii * 13 + 9) < 0.12 {
|
||||||
|
1250.0 + rnd2(s, ii * 13 + 10) * 1200.0
|
||||||
|
} else {
|
||||||
|
5.0 + rnd2(s, ii * 13 + 10) * 500.0
|
||||||
|
};
|
||||||
|
|
||||||
|
let sched = size == 1 && longest >= 1900.0 && rnd2(s, ii * 13 + 11) < 0.35;
|
||||||
|
let has_farm = size >= 1 && rnd2(s, ii * 13 + 12) < 0.25;
|
||||||
|
let has_beach = rnd2(s, ii * 13 + 14) < 0.15;
|
||||||
|
let marine = has_beach || rnd2(s, ii * 13 + 15) < 0.12;
|
||||||
|
|
||||||
|
// Deterministic synthetic ICAO-like code: S + 4 base36 chars or 4-letter code
|
||||||
|
let code = format!("S{:04X}", i);
|
||||||
|
let rw = vec![Runway {
|
||||||
|
len: longest.round(),
|
||||||
|
hard,
|
||||||
|
water: false,
|
||||||
|
hdg: (rnd2(s, ii * 13 + 16) * 360.0).round(),
|
||||||
|
}];
|
||||||
|
|
||||||
|
let ap = Airport {
|
||||||
|
code: code.clone(),
|
||||||
|
name: format!("Synth Field {i}"),
|
||||||
|
city: format!("City {i}"),
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
elev: elev.round(),
|
||||||
|
size,
|
||||||
|
sched,
|
||||||
|
country: country.to_string(),
|
||||||
|
longest: longest.round(),
|
||||||
|
rw,
|
||||||
|
farm: if has_farm {
|
||||||
|
Some(Farm {
|
||||||
|
lat: lat + 0.03,
|
||||||
|
lon: lon + 0.03,
|
||||||
|
hdg: 90.0,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
beach: if has_beach {
|
||||||
|
Some(Beach {
|
||||||
|
lat: lat + 0.02,
|
||||||
|
lon: lon - 0.02,
|
||||||
|
hdg: 180.0,
|
||||||
|
len: 2500.0,
|
||||||
|
sea: 1,
|
||||||
|
name: format!("Beach {i}"),
|
||||||
|
elev: 0.0,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
pad: None,
|
||||||
|
sea: None,
|
||||||
|
marine,
|
||||||
|
};
|
||||||
|
|
||||||
|
if size <= 1 {
|
||||||
|
majors.push(ap.clone());
|
||||||
|
}
|
||||||
|
airports.push(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate synthetic helipads (~20% of airport count)
|
||||||
|
let num_pads = (num_airports / 5).max(20);
|
||||||
|
for i in 0..num_pads {
|
||||||
|
let ii = (i + 100_000) as i32;
|
||||||
|
let anchor = &airports[i % airports.len()];
|
||||||
|
let dlat = (rnd2(s, ii * 7 + 1) - 0.5) * 0.6;
|
||||||
|
let dlon = (rnd2(s, ii * 7 + 2) - 0.5) * 0.6;
|
||||||
|
let kind = PadKind::ALL[i % PadKind::ALL.len()];
|
||||||
|
let name = match kind {
|
||||||
|
PadKind::Hospital => format!("General Hospital {i}"),
|
||||||
|
PadKind::City => format!("Downtown Heliport {i}"),
|
||||||
|
PadKind::Remote => {
|
||||||
|
if i % 2 == 0 {
|
||||||
|
format!("Alpine Lodge {i}")
|
||||||
|
} else {
|
||||||
|
format!("Survey Outpost {i}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PadKind::Rig => format!("Platform Alpha {i}"),
|
||||||
|
PadKind::Ship => {
|
||||||
|
if i % 3 == 0 {
|
||||||
|
format!("M/Y Horizon {i}")
|
||||||
|
} else {
|
||||||
|
format!("Research Vessel {i}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
pads.push(Airport {
|
||||||
|
code: format!("HP{:04X}", i),
|
||||||
|
name,
|
||||||
|
city: anchor.city.clone(),
|
||||||
|
lat: (anchor.lat + dlat).clamp(-80.0, 80.0),
|
||||||
|
lon: anchor.lon + dlon,
|
||||||
|
elev: if kind == PadKind::Rig || kind == PadKind::Ship {
|
||||||
|
15.0
|
||||||
|
} else {
|
||||||
|
anchor.elev
|
||||||
|
},
|
||||||
|
size: 2,
|
||||||
|
sched: false,
|
||||||
|
country: anchor.country.clone(),
|
||||||
|
longest: 0.0,
|
||||||
|
rw: Vec::new(),
|
||||||
|
farm: None,
|
||||||
|
beach: None,
|
||||||
|
pad: Some(Pad {
|
||||||
|
kind,
|
||||||
|
deck: 15.0,
|
||||||
|
hdg: 0.0,
|
||||||
|
small: rnd2(s, ii * 7 + 3) < 0.25,
|
||||||
|
}),
|
||||||
|
sea: None,
|
||||||
|
marine: matches!(kind, PadKind::Rig | PadKind::Ship),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate synthetic seaplane bases (~10% of airport count)
|
||||||
|
let num_sea = (num_airports / 10).max(10);
|
||||||
|
for i in 0..num_sea {
|
||||||
|
let ii = (i + 200_000) as i32;
|
||||||
|
let anchor = &airports[(i * 3) % airports.len()];
|
||||||
|
let dlat = (rnd2(s, ii * 7 + 1) - 0.5) * 0.5;
|
||||||
|
let dlon = (rnd2(s, ii * 7 + 2) - 0.5) * 0.5;
|
||||||
|
let lat = (anchor.lat + dlat).clamp(-80.0, 80.0);
|
||||||
|
let lon = anchor.lon + dlon;
|
||||||
|
let name = match i % 3 {
|
||||||
|
0 => format!("Pine Lake Lodge {i}"),
|
||||||
|
1 => format!("Bay Harbour Seaplane Base {i}"),
|
||||||
|
_ => format!("Cove Water Aerodrome {i}"),
|
||||||
|
};
|
||||||
|
sea_bases.push(Airport {
|
||||||
|
code: format!("SP{:04X}", i),
|
||||||
|
name,
|
||||||
|
city: anchor.city.clone(),
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
elev: 0.0,
|
||||||
|
size: 2,
|
||||||
|
sched: false,
|
||||||
|
country: anchor.country.clone(),
|
||||||
|
longest: 1500.0,
|
||||||
|
rw: vec![Runway {
|
||||||
|
len: 1500.0,
|
||||||
|
hard: false,
|
||||||
|
water: true,
|
||||||
|
hdg: 0.0,
|
||||||
|
}],
|
||||||
|
farm: None,
|
||||||
|
beach: None,
|
||||||
|
pad: None,
|
||||||
|
sea: Some(SeaBase {
|
||||||
|
dock: SeaDock { lat, lon, hdg: 0.0 },
|
||||||
|
}),
|
||||||
|
marine: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate synthetic sights (~15% of airport count)
|
||||||
|
let num_sights = (num_airports / 7).max(20);
|
||||||
|
for i in 0..num_sights {
|
||||||
|
let ii = (i + 300_000) as i32;
|
||||||
|
let anchor = &airports[(i * 5) % airports.len()];
|
||||||
|
let dlat = (rnd2(s, ii * 7 + 1) - 0.5) * 0.7;
|
||||||
|
let dlon = (rnd2(s, ii * 7 + 2) - 0.5) * 0.7;
|
||||||
|
let is_mtn = i % 3 == 0;
|
||||||
|
let name = if is_mtn {
|
||||||
|
format!("Mount Peak {i}")
|
||||||
|
} else {
|
||||||
|
format!("Landmark Vista {i}")
|
||||||
|
};
|
||||||
|
sights.push(Sight {
|
||||||
|
name,
|
||||||
|
lat: (anchor.lat + dlat).clamp(-80.0, 80.0),
|
||||||
|
lon: anchor.lon + dlon,
|
||||||
|
r: 1500.0 + rnd2(s, ii * 7 + 3) * 2500.0,
|
||||||
|
alt: if is_mtn { 9500.0 } else { 1500.0 },
|
||||||
|
map_only: false,
|
||||||
|
survey: rnd2(s, ii * 7 + 4) < 0.65,
|
||||||
|
lit: rnd2(s, ii * 7 + 5) < 0.4,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
WorldData {
|
||||||
|
airports,
|
||||||
|
majors,
|
||||||
|
pads,
|
||||||
|
sea_bases,
|
||||||
|
sights,
|
||||||
|
planes: default_archetype_planes(),
|
||||||
|
fires: Vec::new(),
|
||||||
|
event: None,
|
||||||
|
company: None,
|
||||||
|
projects: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generic archetype plane definitions for synthetic runs / tests when no external
|
||||||
|
/// `--planes` JSON file is supplied. Users should supply their own `--planes <file.json>`
|
||||||
|
/// for custom fleet definitions.
|
||||||
|
pub fn default_archetype_planes() -> Vec<Plane> {
|
||||||
|
vec![
|
||||||
|
Plane {
|
||||||
|
id: "light_piston".into(),
|
||||||
|
name: "Light Piston".into(),
|
||||||
|
kind: "piston".into(),
|
||||||
|
seats: 3,
|
||||||
|
cargo_kg: 100.0,
|
||||||
|
runway_m: 427.0,
|
||||||
|
range_nm: 550.0,
|
||||||
|
cruise_kt: 122.0,
|
||||||
|
cruise_ft: 8500.0,
|
||||||
|
ceiling_ft: 14000.0,
|
||||||
|
mass_kg: 1100.0,
|
||||||
|
rotor: None,
|
||||||
|
hook: None,
|
||||||
|
tanker: false,
|
||||||
|
scoop: false,
|
||||||
|
snorkel: false,
|
||||||
|
tank_l: 0.0,
|
||||||
|
drops: 0,
|
||||||
|
ag: false,
|
||||||
|
historic: false,
|
||||||
|
searches: true,
|
||||||
|
nvg: false,
|
||||||
|
ramp: false,
|
||||||
|
floats: false,
|
||||||
|
amphib: false,
|
||||||
|
water_only: false,
|
||||||
|
burn_gph: 9.0,
|
||||||
|
upkeep_nm: 2.5,
|
||||||
|
fuel_price: 6.5,
|
||||||
|
},
|
||||||
|
Plane {
|
||||||
|
id: "cargo_ag".into(),
|
||||||
|
name: "Cargo Ag".into(),
|
||||||
|
kind: "piston".into(),
|
||||||
|
seats: 0,
|
||||||
|
cargo_kg: 545.0,
|
||||||
|
runway_m: 274.0,
|
||||||
|
range_nm: 270.0,
|
||||||
|
cruise_kt: 90.0,
|
||||||
|
cruise_ft: 6000.0,
|
||||||
|
ceiling_ft: 12000.0,
|
||||||
|
mass_kg: 1400.0,
|
||||||
|
rotor: None,
|
||||||
|
hook: None,
|
||||||
|
tanker: false,
|
||||||
|
scoop: false,
|
||||||
|
snorkel: false,
|
||||||
|
tank_l: 0.0,
|
||||||
|
drops: 0,
|
||||||
|
ag: true,
|
||||||
|
historic: false,
|
||||||
|
searches: false,
|
||||||
|
nvg: false,
|
||||||
|
ramp: false,
|
||||||
|
floats: false,
|
||||||
|
amphib: false,
|
||||||
|
water_only: false,
|
||||||
|
burn_gph: 13.0,
|
||||||
|
upkeep_nm: 2.5,
|
||||||
|
fuel_price: 6.5,
|
||||||
|
},
|
||||||
|
Plane {
|
||||||
|
id: "utility_turboprop".into(),
|
||||||
|
name: "Utility Turboprop".into(),
|
||||||
|
kind: "turboprop".into(),
|
||||||
|
seats: 9,
|
||||||
|
cargo_kg: 1300.0,
|
||||||
|
runway_m: 580.0,
|
||||||
|
range_nm: 990.0,
|
||||||
|
cruise_kt: 180.0,
|
||||||
|
cruise_ft: 12000.0,
|
||||||
|
ceiling_ft: 25000.0,
|
||||||
|
mass_kg: 3900.0,
|
||||||
|
rotor: None,
|
||||||
|
hook: None,
|
||||||
|
tanker: false,
|
||||||
|
scoop: false,
|
||||||
|
snorkel: false,
|
||||||
|
tank_l: 0.0,
|
||||||
|
drops: 0,
|
||||||
|
ag: false,
|
||||||
|
historic: false,
|
||||||
|
searches: true,
|
||||||
|
nvg: false,
|
||||||
|
ramp: true,
|
||||||
|
floats: false,
|
||||||
|
amphib: false,
|
||||||
|
water_only: false,
|
||||||
|
burn_gph: 55.0,
|
||||||
|
upkeep_nm: 4.0,
|
||||||
|
fuel_price: 5.5,
|
||||||
|
},
|
||||||
|
Plane {
|
||||||
|
id: "bizjet".into(),
|
||||||
|
name: "Business Jet".into(),
|
||||||
|
kind: "jet".into(),
|
||||||
|
seats: 12,
|
||||||
|
cargo_kg: 1500.0,
|
||||||
|
runway_m: 1585.0,
|
||||||
|
range_nm: 4560.0,
|
||||||
|
cruise_kt: 475.0,
|
||||||
|
cruise_ft: 41000.0,
|
||||||
|
ceiling_ft: 47000.0,
|
||||||
|
mass_kg: 18000.0,
|
||||||
|
rotor: None,
|
||||||
|
hook: None,
|
||||||
|
tanker: false,
|
||||||
|
scoop: false,
|
||||||
|
snorkel: false,
|
||||||
|
tank_l: 0.0,
|
||||||
|
drops: 0,
|
||||||
|
ag: false,
|
||||||
|
historic: false,
|
||||||
|
searches: false,
|
||||||
|
nvg: false,
|
||||||
|
ramp: false,
|
||||||
|
floats: false,
|
||||||
|
amphib: false,
|
||||||
|
water_only: false,
|
||||||
|
burn_gph: 290.0,
|
||||||
|
upkeep_nm: 6.0,
|
||||||
|
fuel_price: 5.5,
|
||||||
|
},
|
||||||
|
Plane {
|
||||||
|
id: "skyliner".into(),
|
||||||
|
name: "Narrowbody Airliner".into(),
|
||||||
|
kind: "jet".into(),
|
||||||
|
seats: 150,
|
||||||
|
cargo_kg: 15000.0,
|
||||||
|
runway_m: 1890.0,
|
||||||
|
range_nm: 4050.0,
|
||||||
|
cruise_kt: 450.0,
|
||||||
|
cruise_ft: 37000.0,
|
||||||
|
ceiling_ft: 41000.0,
|
||||||
|
mass_kg: 70000.0,
|
||||||
|
rotor: None,
|
||||||
|
hook: None,
|
||||||
|
tanker: false,
|
||||||
|
scoop: false,
|
||||||
|
snorkel: false,
|
||||||
|
tank_l: 0.0,
|
||||||
|
drops: 0,
|
||||||
|
ag: false,
|
||||||
|
historic: false,
|
||||||
|
searches: false,
|
||||||
|
nvg: false,
|
||||||
|
ramp: false,
|
||||||
|
floats: false,
|
||||||
|
amphib: false,
|
||||||
|
water_only: false,
|
||||||
|
burn_gph: 800.0,
|
||||||
|
upkeep_nm: 14.0,
|
||||||
|
fuel_price: 5.5,
|
||||||
|
},
|
||||||
|
Plane {
|
||||||
|
id: "heavy_airliner".into(),
|
||||||
|
name: "Heavy Airliner".into(),
|
||||||
|
kind: "jet".into(),
|
||||||
|
seats: 300,
|
||||||
|
cargo_kg: 30000.0,
|
||||||
|
runway_m: 2682.0,
|
||||||
|
range_nm: 7350.0,
|
||||||
|
cruise_kt: 490.0,
|
||||||
|
cruise_ft: 39000.0,
|
||||||
|
ceiling_ft: 43000.0,
|
||||||
|
mass_kg: 240000.0,
|
||||||
|
rotor: None,
|
||||||
|
hook: None,
|
||||||
|
tanker: false,
|
||||||
|
scoop: false,
|
||||||
|
snorkel: false,
|
||||||
|
tank_l: 0.0,
|
||||||
|
drops: 0,
|
||||||
|
ag: false,
|
||||||
|
historic: false,
|
||||||
|
searches: false,
|
||||||
|
nvg: false,
|
||||||
|
ramp: false,
|
||||||
|
floats: false,
|
||||||
|
amphib: false,
|
||||||
|
water_only: false,
|
||||||
|
burn_gph: 2100.0,
|
||||||
|
upkeep_nm: 20.0,
|
||||||
|
fuel_price: 5.5,
|
||||||
|
},
|
||||||
|
Plane {
|
||||||
|
id: "utility_heli".into(),
|
||||||
|
name: "Utility Helicopter".into(),
|
||||||
|
kind: "heli".into(),
|
||||||
|
seats: 6,
|
||||||
|
cargo_kg: 650.0,
|
||||||
|
runway_m: 0.0,
|
||||||
|
range_nm: 350.0,
|
||||||
|
cruise_kt: 135.0,
|
||||||
|
cruise_ft: 5000.0,
|
||||||
|
ceiling_ft: 16000.0,
|
||||||
|
mass_kg: 3200.0,
|
||||||
|
rotor: Some(RotorSpec {
|
||||||
|
d: 11.0,
|
||||||
|
big: false,
|
||||||
|
tilt: false,
|
||||||
|
hover_kw: 700.0,
|
||||||
|
max_kw: 1100.0,
|
||||||
|
sto: false,
|
||||||
|
}),
|
||||||
|
hook: Some(HookSpec {
|
||||||
|
kg: 1200.0,
|
||||||
|
line: 25.0,
|
||||||
|
kt: 85.0,
|
||||||
|
}),
|
||||||
|
tanker: true,
|
||||||
|
scoop: false,
|
||||||
|
snorkel: true,
|
||||||
|
tank_l: 1200.0,
|
||||||
|
drops: 3,
|
||||||
|
ag: false,
|
||||||
|
historic: false,
|
||||||
|
searches: true,
|
||||||
|
nvg: false,
|
||||||
|
ramp: false,
|
||||||
|
floats: false,
|
||||||
|
amphib: false,
|
||||||
|
water_only: false,
|
||||||
|
burn_gph: 70.0,
|
||||||
|
upkeep_nm: 7.0,
|
||||||
|
fuel_price: 5.5,
|
||||||
|
},
|
||||||
|
Plane {
|
||||||
|
id: "amphib_float".into(),
|
||||||
|
name: "Amphibious Seaplane".into(),
|
||||||
|
kind: "piston".into(),
|
||||||
|
seats: 8,
|
||||||
|
cargo_kg: 700.0,
|
||||||
|
runway_m: 488.0,
|
||||||
|
range_nm: 560.0,
|
||||||
|
cruise_kt: 160.0,
|
||||||
|
cruise_ft: 10000.0,
|
||||||
|
ceiling_ft: 15000.0,
|
||||||
|
mass_kg: 3400.0,
|
||||||
|
rotor: None,
|
||||||
|
hook: None,
|
||||||
|
tanker: true,
|
||||||
|
scoop: true,
|
||||||
|
snorkel: false,
|
||||||
|
tank_l: 1800.0,
|
||||||
|
drops: 3,
|
||||||
|
ag: false,
|
||||||
|
historic: false,
|
||||||
|
searches: true,
|
||||||
|
nvg: false,
|
||||||
|
ramp: false,
|
||||||
|
floats: true,
|
||||||
|
amphib: true,
|
||||||
|
water_only: false,
|
||||||
|
burn_gph: 46.0,
|
||||||
|
upkeep_nm: 4.0,
|
||||||
|
fuel_price: 6.5,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Load a public-domain OurAirports `airports.csv` into `WorldData`.
|
||||||
|
/// Maps large_airport -> hub (0), medium_airport -> regional (1), small_airport -> airstrip (2),
|
||||||
|
/// heliport -> pad, seaplane_base -> sea_base.
|
||||||
|
pub fn load_ourairports_csv(path: &Path) -> std::io::Result<WorldData> {
|
||||||
|
let file = File::open(path)?;
|
||||||
|
let reader = BufReader::with_capacity(1 << 20, file);
|
||||||
|
let mut lines = reader.lines();
|
||||||
|
let header_line = lines.next().transpose()?.unwrap_or_default();
|
||||||
|
let headers = parse_csv_row(&header_line);
|
||||||
|
let col = |name: &str| headers.iter().position(|h| h == name);
|
||||||
|
|
||||||
|
let i_ident = col("ident").unwrap_or(1);
|
||||||
|
let i_type = col("type").unwrap_or(2);
|
||||||
|
let i_name = col("name").unwrap_or(3);
|
||||||
|
let i_lat = col("latitude_deg").unwrap_or(4);
|
||||||
|
let i_lon = col("longitude_deg").unwrap_or(5);
|
||||||
|
let i_elev = col("elevation_ft").unwrap_or(6);
|
||||||
|
let i_country = col("iso_country").unwrap_or(8);
|
||||||
|
let i_muni = col("municipality").unwrap_or(10);
|
||||||
|
let i_sched = col("scheduled_service").unwrap_or(11);
|
||||||
|
|
||||||
|
let mut airports = Vec::new();
|
||||||
|
let mut majors = Vec::new();
|
||||||
|
let mut pads = Vec::new();
|
||||||
|
let mut sea_bases = Vec::new();
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
let line = line?;
|
||||||
|
if line.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let cols = parse_csv_row(&line);
|
||||||
|
if cols.len() <= i_lon {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let kind = cols[i_type].as_str();
|
||||||
|
if kind == "closed" || kind == "balloonport" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let Ok(lat) = cols[i_lat].parse::<f64>() else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Ok(lon) = cols[i_lon].parse::<f64>() else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let elev_m = cols
|
||||||
|
.get(i_elev)
|
||||||
|
.and_then(|s| s.parse::<f64>().ok())
|
||||||
|
.unwrap_or(0.0)
|
||||||
|
* 0.3048;
|
||||||
|
let code = cols[i_ident].clone();
|
||||||
|
let name = cols.get(i_name).cloned().unwrap_or_default();
|
||||||
|
let city = cols.get(i_muni).cloned().unwrap_or_default();
|
||||||
|
let country = cols.get(i_country).cloned().unwrap_or_default();
|
||||||
|
let sched = cols.get(i_sched).is_some_and(|s| s == "yes");
|
||||||
|
|
||||||
|
match kind {
|
||||||
|
"large_airport" | "medium_airport" | "small_airport" => {
|
||||||
|
let size = match kind {
|
||||||
|
"large_airport" => 0,
|
||||||
|
"medium_airport" => 1,
|
||||||
|
_ => 2,
|
||||||
|
};
|
||||||
|
let (longest, hard) = match size {
|
||||||
|
0 => (3000.0, true),
|
||||||
|
1 => (1800.0, true),
|
||||||
|
_ => (850.0, true),
|
||||||
|
};
|
||||||
|
let ap = Airport {
|
||||||
|
code,
|
||||||
|
name,
|
||||||
|
city,
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
elev: elev_m,
|
||||||
|
size,
|
||||||
|
sched: size == 1 && sched,
|
||||||
|
country,
|
||||||
|
longest,
|
||||||
|
rw: vec![Runway {
|
||||||
|
len: longest,
|
||||||
|
hard,
|
||||||
|
water: false,
|
||||||
|
hdg: 0.0,
|
||||||
|
}],
|
||||||
|
farm: None,
|
||||||
|
beach: None,
|
||||||
|
pad: None,
|
||||||
|
sea: None,
|
||||||
|
marine: false,
|
||||||
|
};
|
||||||
|
if size <= 1 {
|
||||||
|
majors.push(ap.clone());
|
||||||
|
}
|
||||||
|
airports.push(ap);
|
||||||
|
}
|
||||||
|
"heliport" => {
|
||||||
|
let lower = name.to_ascii_lowercase();
|
||||||
|
let pk = if lower.contains("hosp") || lower.contains("med") {
|
||||||
|
PadKind::Hospital
|
||||||
|
} else if lower.contains("rig") || lower.contains("platform") {
|
||||||
|
PadKind::Rig
|
||||||
|
} else if lower.contains("ship") || lower.contains("yacht") {
|
||||||
|
PadKind::Ship
|
||||||
|
} else if lower.contains("lodge") || lower.contains("remote") {
|
||||||
|
PadKind::Remote
|
||||||
|
} else {
|
||||||
|
PadKind::City
|
||||||
|
};
|
||||||
|
pads.push(Airport {
|
||||||
|
code,
|
||||||
|
name,
|
||||||
|
city,
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
elev: elev_m,
|
||||||
|
size: 2,
|
||||||
|
sched: false,
|
||||||
|
country,
|
||||||
|
longest: 0.0,
|
||||||
|
rw: Vec::new(),
|
||||||
|
farm: None,
|
||||||
|
beach: None,
|
||||||
|
pad: Some(Pad {
|
||||||
|
kind: pk,
|
||||||
|
deck: 0.0,
|
||||||
|
hdg: 0.0,
|
||||||
|
small: false,
|
||||||
|
}),
|
||||||
|
sea: None,
|
||||||
|
marine: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
"seaplane_base" => {
|
||||||
|
sea_bases.push(Airport {
|
||||||
|
code,
|
||||||
|
name,
|
||||||
|
city,
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
elev: elev_m,
|
||||||
|
size: 2,
|
||||||
|
sched: false,
|
||||||
|
country,
|
||||||
|
longest: 1500.0,
|
||||||
|
rw: vec![Runway {
|
||||||
|
len: 1500.0,
|
||||||
|
hard: false,
|
||||||
|
water: true,
|
||||||
|
hdg: 0.0,
|
||||||
|
}],
|
||||||
|
farm: None,
|
||||||
|
beach: None,
|
||||||
|
pad: None,
|
||||||
|
sea: Some(SeaBase {
|
||||||
|
dock: SeaDock { lat, lon, hdg: 0.0 },
|
||||||
|
}),
|
||||||
|
marine: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(WorldData {
|
||||||
|
airports,
|
||||||
|
majors,
|
||||||
|
pads,
|
||||||
|
sea_bases,
|
||||||
|
sights: Vec::new(),
|
||||||
|
planes: default_archetype_planes(),
|
||||||
|
fires: Vec::new(),
|
||||||
|
event: None,
|
||||||
|
company: None,
|
||||||
|
projects: Vec::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_csv_row(line: &str) -> Vec<String> {
|
||||||
|
let mut out = Vec::with_capacity(18);
|
||||||
|
let mut cur = String::new();
|
||||||
|
let mut in_quotes = false;
|
||||||
|
let bytes = line.as_bytes();
|
||||||
|
let mut i = 0;
|
||||||
|
while i < bytes.len() {
|
||||||
|
let b = bytes[i];
|
||||||
|
if in_quotes {
|
||||||
|
if b == b'"' {
|
||||||
|
if i + 1 < bytes.len() && bytes[i + 1] == b'"' {
|
||||||
|
cur.push('"');
|
||||||
|
i += 2;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
in_quotes = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cur.push(b as char);
|
||||||
|
}
|
||||||
|
} else if b == b'"' {
|
||||||
|
in_quotes = true;
|
||||||
|
} else if b == b',' {
|
||||||
|
out.push(std::mem::take(&mut cur));
|
||||||
|
} else {
|
||||||
|
cur.push(b as char);
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
out.push(cur);
|
||||||
|
out
|
||||||
|
}
|
||||||
+510
@@ -0,0 +1,510 @@
|
|||||||
|
use rayon::prelude::*;
|
||||||
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
|
use crate::geo::{
|
||||||
|
EARTH_R, GeoPoint, NM, RAD_TO_DEG, bearing_rad, chord_sq, chord_sq_to_dist_m,
|
||||||
|
dist_m_to_chord_sq,
|
||||||
|
};
|
||||||
|
use crate::model::{Airport, PadKind, Plane, Sight, WorldData};
|
||||||
|
use crate::rng::hash31;
|
||||||
|
|
||||||
|
/// 4° x 4° spatial grid (45 lat bands x 90 lon bands = 4050 cells).
|
||||||
|
const CELL_DEG: f64 = 4.0;
|
||||||
|
const LAT_BINS: usize = 45;
|
||||||
|
const LON_BINS: usize = 90;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn lat_bin(lat: f64) -> usize {
|
||||||
|
let idx = ((lat + 90.0) / CELL_DEG).floor() as isize;
|
||||||
|
idx.clamp(0, (LAT_BINS - 1) as isize) as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn lon_bin(lon: f64) -> usize {
|
||||||
|
let wrapped = (lon + 180.0).rem_euclid(360.0);
|
||||||
|
((wrapped / CELL_DEG).floor() as usize).min(LON_BINS - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SpatialGrid {
|
||||||
|
cells: Vec<Vec<u32>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SpatialGrid {
|
||||||
|
pub fn build<'a>(iter: impl Iterator<Item = (usize, &'a GeoPoint)>) -> Self {
|
||||||
|
let mut cells = vec![Vec::new(); LAT_BINS * LON_BINS];
|
||||||
|
for (idx, g) in iter {
|
||||||
|
let b = lat_bin(g.lat) * LON_BINS + lon_bin(g.lon);
|
||||||
|
cells[b].push(idx as u32);
|
||||||
|
}
|
||||||
|
Self { cells }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invoke `f(idx, chord_sq)` for all items in cells overlapping `radius_m` around `origin`.
|
||||||
|
#[inline]
|
||||||
|
pub fn query_radius<F>(&self, origin: &GeoPoint, radius_m: f64, mut f: F)
|
||||||
|
where
|
||||||
|
F: FnMut(u32, f64),
|
||||||
|
{
|
||||||
|
let max_csq = dist_m_to_chord_sq(radius_m);
|
||||||
|
let dlat_deg = (radius_m / EARTH_R) * RAD_TO_DEG;
|
||||||
|
let min_lat = (origin.lat - dlat_deg).max(-90.0);
|
||||||
|
let max_lat = (origin.lat + dlat_deg).min(90.0);
|
||||||
|
let r0 = lat_bin(min_lat);
|
||||||
|
let r1 = lat_bin(max_lat);
|
||||||
|
|
||||||
|
let max_abs_lat = min_lat.abs().max(max_lat.abs());
|
||||||
|
let cos_limit = (max_abs_lat * std::f64::consts::PI / 180.0).cos();
|
||||||
|
let full_lon = cos_limit < 0.05 || (dlat_deg / cos_limit) >= 179.0;
|
||||||
|
|
||||||
|
if full_lon {
|
||||||
|
for r in r0..=r1 {
|
||||||
|
let row = r * LON_BINS;
|
||||||
|
for c in 0..LON_BINS {
|
||||||
|
for &idx in &self.cells[row + c] {
|
||||||
|
f(idx, max_csq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let dlon_deg = dlat_deg / cos_limit;
|
||||||
|
let c0 = lon_bin(origin.lon - dlon_deg);
|
||||||
|
let c1 = lon_bin(origin.lon + dlon_deg);
|
||||||
|
for r in r0..=r1 {
|
||||||
|
let row = r * LON_BINS;
|
||||||
|
if c0 <= c1 {
|
||||||
|
for c in c0..=c1 {
|
||||||
|
for &idx in &self.cells[row + c] {
|
||||||
|
f(idx, max_csq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for c in c0..LON_BINS {
|
||||||
|
for &idx in &self.cells[row + c] {
|
||||||
|
f(idx, max_csq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for c in 0..=c1 {
|
||||||
|
for &idx in &self.cells[row + c] {
|
||||||
|
f(idx, max_csq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct IndexedAirport {
|
||||||
|
pub raw: Airport,
|
||||||
|
pub geo: GeoPoint,
|
||||||
|
pub code_hash: i32,
|
||||||
|
pub longest_hard_m: f64,
|
||||||
|
pub longest_land_m: f64,
|
||||||
|
pub is_short: bool,
|
||||||
|
pub is_pad_only: bool,
|
||||||
|
pub is_sea_only: bool,
|
||||||
|
pub has_hard_longest: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IndexedAirport {
|
||||||
|
pub fn from_airport(raw: Airport) -> Self {
|
||||||
|
let geo = GeoPoint::new(raw.lat, raw.lon);
|
||||||
|
let code_hash = hash31(&raw.code);
|
||||||
|
let longest_hard_m = raw.longest_hard_m();
|
||||||
|
let longest_land_m = raw.longest_land_m();
|
||||||
|
let is_short = raw.is_short_field();
|
||||||
|
let is_pad_only = raw.is_pad_only();
|
||||||
|
let is_sea_only = raw.is_seaplane_only();
|
||||||
|
let has_hard_longest = raw.has_hard_longest();
|
||||||
|
Self {
|
||||||
|
raw,
|
||||||
|
geo,
|
||||||
|
code_hash,
|
||||||
|
longest_hard_m,
|
||||||
|
longest_land_m,
|
||||||
|
is_short,
|
||||||
|
is_pad_only,
|
||||||
|
is_sea_only,
|
||||||
|
has_hard_longest,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn usable_runway(&self, need_m: f64, hard: bool) -> bool {
|
||||||
|
if self.is_pad_only || self.is_sea_only {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if hard {
|
||||||
|
self.longest_hard_m >= need_m
|
||||||
|
} else {
|
||||||
|
self.longest_land_m >= need_m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tight cache-friendly major-airport record for SIMD-friendly global range scans.
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct MajorFast {
|
||||||
|
pub x: f64,
|
||||||
|
pub y: f64,
|
||||||
|
pub z: f64,
|
||||||
|
pub longest_hard_m: f64,
|
||||||
|
pub longest_land_m: f64,
|
||||||
|
pub size: u8,
|
||||||
|
pub major_idx: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct IndexedSight {
|
||||||
|
pub raw: Sight,
|
||||||
|
pub geo: GeoPoint,
|
||||||
|
pub is_mountain: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IndexedSight {
|
||||||
|
pub fn from_sight(raw: Sight) -> Self {
|
||||||
|
let geo = GeoPoint::new(raw.lat, raw.lon);
|
||||||
|
let lower = raw.name.to_ascii_lowercase();
|
||||||
|
let is_mountain = lower.contains("mount")
|
||||||
|
|| lower.contains("mt ")
|
||||||
|
|| lower.contains("mt.")
|
||||||
|
|| lower.contains("peak")
|
||||||
|
|| lower.contains("glacier")
|
||||||
|
|| lower.contains("canyon")
|
||||||
|
|| lower.contains("ridge")
|
||||||
|
|| lower.contains("summit")
|
||||||
|
|| lower.contains("volcano")
|
||||||
|
|| lower.contains("alps")
|
||||||
|
|| lower.contains("sierra");
|
||||||
|
Self {
|
||||||
|
raw,
|
||||||
|
geo,
|
||||||
|
is_mountain,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Precomputed neighbor entry (sorted by `(dist_m, code)`).
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct Neighbor {
|
||||||
|
pub idx: u32,
|
||||||
|
pub dist_m: f64,
|
||||||
|
pub bearing: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
pub struct PrecomputedNeighbors {
|
||||||
|
/// Fixed-wing airports in (3704 m, 160 NM], sorted by (dist_m, code).
|
||||||
|
pub nearby_fw: Vec<Neighbor>,
|
||||||
|
/// Index into `airports` of the nearest regional airport (size == 1) within 160 NM.
|
||||||
|
pub nearest_regional: Option<u32>,
|
||||||
|
/// Active (non-mapOnly) sights in (3704 m, 60 NM], sorted by dist_m.
|
||||||
|
pub nearby_sights: Vec<Neighbor>,
|
||||||
|
/// Seaplane bases in (3704 m, 160 NM], sorted by dist_m.
|
||||||
|
pub nearby_sea: Vec<Neighbor>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct WorldIndex {
|
||||||
|
pub airports: Vec<IndexedAirport>,
|
||||||
|
pub code_to_airport: FxHashMap<String, usize>,
|
||||||
|
pub majors: Vec<IndexedAirport>,
|
||||||
|
pub majors_fast: Vec<MajorFast>,
|
||||||
|
pub pads: Vec<IndexedAirport>,
|
||||||
|
pub sea_bases: Vec<IndexedAirport>,
|
||||||
|
pub sights: Vec<IndexedSight>,
|
||||||
|
pub planes: FxHashMap<String, Plane>,
|
||||||
|
pub plane_list: Vec<Plane>,
|
||||||
|
|
||||||
|
pub airports_grid: SpatialGrid,
|
||||||
|
pub pads_grid: SpatialGrid,
|
||||||
|
pub sea_grid: SpatialGrid,
|
||||||
|
pub sights_grid: SpatialGrid,
|
||||||
|
|
||||||
|
/// Optional precomputed neighbor cache indexed by airport index.
|
||||||
|
pub neighbor_cache: Vec<PrecomputedNeighbors>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WorldIndex {
|
||||||
|
pub fn build(data: WorldData, precompute_neighbors: bool) -> Self {
|
||||||
|
let mut airports: Vec<IndexedAirport> = data
|
||||||
|
.airports
|
||||||
|
.into_iter()
|
||||||
|
.map(IndexedAirport::from_airport)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Build code map
|
||||||
|
let mut code_to_airport = FxHashMap::default();
|
||||||
|
code_to_airport.reserve(airports.len());
|
||||||
|
for (i, a) in airports.iter().enumerate() {
|
||||||
|
code_to_airport.insert(a.raw.code.clone(), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If majors is empty, derive majors from airports with size <= 1 (hubs + regionals)
|
||||||
|
let majors: Vec<IndexedAirport> = if data.majors.is_empty() {
|
||||||
|
airports
|
||||||
|
.iter()
|
||||||
|
.filter(|a| a.raw.size <= 1 && !a.is_pad_only && !a.is_sea_only)
|
||||||
|
.cloned()
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
data.majors
|
||||||
|
.into_iter()
|
||||||
|
.map(IndexedAirport::from_airport)
|
||||||
|
.collect()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Ensure majors are also present in airports list if not already
|
||||||
|
for m in &majors {
|
||||||
|
if !code_to_airport.contains_key(&m.raw.code) {
|
||||||
|
let idx = airports.len();
|
||||||
|
code_to_airport.insert(m.raw.code.clone(), idx);
|
||||||
|
airports.push(m.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let majors_fast: Vec<MajorFast> = majors
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, m)| MajorFast {
|
||||||
|
x: m.geo.x,
|
||||||
|
y: m.geo.y,
|
||||||
|
z: m.geo.z,
|
||||||
|
longest_hard_m: m.longest_hard_m,
|
||||||
|
longest_land_m: m.longest_land_m,
|
||||||
|
size: m.raw.size,
|
||||||
|
major_idx: i as u32,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let pads: Vec<IndexedAirport> = if data.pads.is_empty() {
|
||||||
|
airports
|
||||||
|
.iter()
|
||||||
|
.filter(|a| a.raw.pad.is_some())
|
||||||
|
.cloned()
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
data.pads
|
||||||
|
.into_iter()
|
||||||
|
.map(IndexedAirport::from_airport)
|
||||||
|
.collect()
|
||||||
|
};
|
||||||
|
|
||||||
|
let sea_bases: Vec<IndexedAirport> = if data.sea_bases.is_empty() {
|
||||||
|
airports
|
||||||
|
.iter()
|
||||||
|
.filter(|a| a.raw.sea.is_some())
|
||||||
|
.cloned()
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
data.sea_bases
|
||||||
|
.into_iter()
|
||||||
|
.map(IndexedAirport::from_airport)
|
||||||
|
.collect()
|
||||||
|
};
|
||||||
|
|
||||||
|
let sights: Vec<IndexedSight> = data
|
||||||
|
.sights
|
||||||
|
.into_iter()
|
||||||
|
.filter(|s| !s.map_only)
|
||||||
|
.map(IndexedSight::from_sight)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut planes = FxHashMap::default();
|
||||||
|
for p in &data.planes {
|
||||||
|
planes.insert(p.id.clone(), p.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
let airports_grid = SpatialGrid::build(
|
||||||
|
airports
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(_, a)| !a.is_pad_only && !a.is_sea_only)
|
||||||
|
.map(|(i, a)| (i, &a.geo)),
|
||||||
|
);
|
||||||
|
let pads_grid = SpatialGrid::build(pads.iter().enumerate().map(|(i, a)| (i, &a.geo)));
|
||||||
|
let sea_grid = SpatialGrid::build(sea_bases.iter().enumerate().map(|(i, a)| (i, &a.geo)));
|
||||||
|
let sights_grid = SpatialGrid::build(sights.iter().enumerate().map(|(i, s)| (i, &s.geo)));
|
||||||
|
|
||||||
|
let mut world = Self {
|
||||||
|
airports,
|
||||||
|
code_to_airport,
|
||||||
|
majors,
|
||||||
|
majors_fast,
|
||||||
|
pads,
|
||||||
|
sea_bases,
|
||||||
|
sights,
|
||||||
|
planes,
|
||||||
|
plane_list: data.planes,
|
||||||
|
airports_grid,
|
||||||
|
pads_grid,
|
||||||
|
sea_grid,
|
||||||
|
sights_grid,
|
||||||
|
neighbor_cache: Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if precompute_neighbors {
|
||||||
|
world.build_neighbor_cache();
|
||||||
|
}
|
||||||
|
world
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parallel precomputation of time-invariant nearby airports, sights, and seaplane bases.
|
||||||
|
pub fn build_neighbor_cache(&mut self) {
|
||||||
|
let min_csq = dist_m_to_chord_sq(3704.0);
|
||||||
|
let max_fw_m = 160.0 * NM; // 296_320 m
|
||||||
|
let max_sight_m = 60.0 * NM;
|
||||||
|
let max_sea_m = 160.0 * NM;
|
||||||
|
|
||||||
|
let cache: Vec<PrecomputedNeighbors> = (0..self.airports.len())
|
||||||
|
.into_par_iter()
|
||||||
|
.map(|origin_idx| {
|
||||||
|
let origin = &self.airports[origin_idx];
|
||||||
|
if origin.is_pad_only {
|
||||||
|
return PrecomputedNeighbors::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut nearby_fw = Vec::new();
|
||||||
|
self.airports_grid
|
||||||
|
.query_radius(&origin.geo, max_fw_m, |idx, max_csq| {
|
||||||
|
if idx as usize == origin_idx {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let cand = &self.airports[idx as usize];
|
||||||
|
let csq = chord_sq(&origin.geo, &cand.geo);
|
||||||
|
if csq > min_csq && csq <= max_csq {
|
||||||
|
let d = chord_sq_to_dist_m(csq);
|
||||||
|
if d > 3704.0 && d <= max_fw_m {
|
||||||
|
nearby_fw.push(Neighbor {
|
||||||
|
idx,
|
||||||
|
dist_m: d,
|
||||||
|
bearing: bearing_rad(&origin.geo, &cand.geo),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
nearby_fw.sort_unstable_by(|a, b| {
|
||||||
|
a.dist_m.total_cmp(&b.dist_m).then_with(|| {
|
||||||
|
self.airports[a.idx as usize]
|
||||||
|
.raw
|
||||||
|
.code
|
||||||
|
.cmp(&self.airports[b.idx as usize].raw.code)
|
||||||
|
})
|
||||||
|
});
|
||||||
|
let nearest_regional = nearby_fw
|
||||||
|
.iter()
|
||||||
|
.find(|n| self.airports[n.idx as usize].raw.size == 1)
|
||||||
|
.map(|n| n.idx);
|
||||||
|
|
||||||
|
let mut nearby_sights = Vec::new();
|
||||||
|
self.sights_grid
|
||||||
|
.query_radius(&origin.geo, max_sight_m, |idx, max_csq| {
|
||||||
|
let s = &self.sights[idx as usize];
|
||||||
|
let csq = chord_sq(&origin.geo, &s.geo);
|
||||||
|
if csq > min_csq && csq <= max_csq {
|
||||||
|
let d = chord_sq_to_dist_m(csq);
|
||||||
|
if d > 3704.0 && d <= max_sight_m {
|
||||||
|
nearby_sights.push(Neighbor {
|
||||||
|
idx,
|
||||||
|
dist_m: d,
|
||||||
|
bearing: bearing_rad(&origin.geo, &s.geo),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
nearby_sights.sort_unstable_by(|a, b| a.dist_m.total_cmp(&b.dist_m));
|
||||||
|
|
||||||
|
let mut nearby_sea = Vec::new();
|
||||||
|
self.sea_grid
|
||||||
|
.query_radius(&origin.geo, max_sea_m, |idx, max_csq| {
|
||||||
|
let s = &self.sea_bases[idx as usize];
|
||||||
|
if s.raw.code == origin.raw.code {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let csq = chord_sq(&origin.geo, &s.geo);
|
||||||
|
if csq > min_csq && csq <= max_csq {
|
||||||
|
let d = chord_sq_to_dist_m(csq);
|
||||||
|
if d > 3704.0 && d <= max_sea_m {
|
||||||
|
nearby_sea.push(Neighbor {
|
||||||
|
idx,
|
||||||
|
dist_m: d,
|
||||||
|
bearing: bearing_rad(&origin.geo, &s.geo),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
nearby_sea.sort_unstable_by(|a, b| a.dist_m.total_cmp(&b.dist_m));
|
||||||
|
|
||||||
|
PrecomputedNeighbors {
|
||||||
|
nearby_fw,
|
||||||
|
nearest_regional,
|
||||||
|
nearby_sights,
|
||||||
|
nearby_sea,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
self.neighbor_cache = cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_airport(&self, code: &str) -> Option<(usize, &IndexedAirport)> {
|
||||||
|
self.code_to_airport
|
||||||
|
.get(code)
|
||||||
|
.copied()
|
||||||
|
.map(|i| (i, &self.airports[i]))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Collect nearby pads of a given kind within `max_m` around `origin`, sorted by distance.
|
||||||
|
pub fn query_pads_of_kind(
|
||||||
|
&self,
|
||||||
|
origin: &IndexedAirport,
|
||||||
|
kind: PadKind,
|
||||||
|
max_m: f64,
|
||||||
|
ceiling_m: f64,
|
||||||
|
big_rotor: bool,
|
||||||
|
out: &mut Vec<Neighbor>,
|
||||||
|
) {
|
||||||
|
out.clear();
|
||||||
|
let min_csq = dist_m_to_chord_sq(500.0);
|
||||||
|
self.pads_grid
|
||||||
|
.query_radius(&origin.geo, max_m, |idx, max_csq| {
|
||||||
|
let p = &self.pads[idx as usize];
|
||||||
|
let Some(pad) = &p.raw.pad else { return };
|
||||||
|
if pad.kind != kind {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if big_rotor && pad.small {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if p.raw.elev > ceiling_m {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if p.raw.code == origin.raw.code {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let csq = chord_sq(&origin.geo, &p.geo);
|
||||||
|
if csq > min_csq && csq <= max_csq {
|
||||||
|
let d = chord_sq_to_dist_m(csq);
|
||||||
|
if d <= max_m {
|
||||||
|
out.push(Neighbor {
|
||||||
|
idx,
|
||||||
|
dist_m: d,
|
||||||
|
bearing: bearing_rad(&origin.geo, &p.geo),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
out.sort_unstable_by(|a, b| {
|
||||||
|
a.dist_m.total_cmp(&b.dist_m).then_with(|| {
|
||||||
|
self.pads[a.idx as usize]
|
||||||
|
.raw
|
||||||
|
.code
|
||||||
|
.cmp(&self.pads[b.idx as usize].raw.code)
|
||||||
|
})
|
||||||
|
});
|
||||||
|
if out.len() > 12 {
|
||||||
|
out.truncate(12);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,214 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use pp_jobboard_sim::generator::{BoardScratch, compute_pay, generate_board, partition_and_sort};
|
||||||
|
use pp_jobboard_sim::geo::NM;
|
||||||
|
use pp_jobboard_sim::model::{
|
||||||
|
BoardOptions, CompanyBase, CompanyState, EventBox, EventMode, EventRule, EventState,
|
||||||
|
FilterMode, JobKind, ProjectState, SortMode,
|
||||||
|
};
|
||||||
|
use pp_jobboard_sim::rng::{BUCKET_MS, board_salt, bucket_of, hash31, rnd2};
|
||||||
|
use pp_jobboard_sim::synthetic::generate_synthetic_world;
|
||||||
|
use pp_jobboard_sim::world::WorldIndex;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_hash31_and_rnd2_exact() {
|
||||||
|
// Java-style hash31("KSAN") = ((('K'*31+'S')*31+'A')*31+'N')
|
||||||
|
let expected = (((('K' as i32) * 31 + ('S' as i32)) * 31 + ('A' as i32)) * 31) + ('N' as i32);
|
||||||
|
assert_eq!(hash31("KSAN"), expected);
|
||||||
|
|
||||||
|
// rnd2 must be deterministic and in [0, 1]
|
||||||
|
let v1 = rnd2(expected, 12345);
|
||||||
|
let v2 = rnd2(expected, 12345);
|
||||||
|
assert_eq!(v1, v2);
|
||||||
|
assert!((0.0..=1.0).contains(&v1));
|
||||||
|
|
||||||
|
// Offline SHA-256 board_salt deterministic
|
||||||
|
let s1 = board_salt("offline", 997_815);
|
||||||
|
let s2 = board_salt("offline", 997_815);
|
||||||
|
let s3 = board_salt("offline", 997_816);
|
||||||
|
assert_eq!(s1, s2);
|
||||||
|
assert_ne!(s1, s3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_pay_formula_matches_spec() {
|
||||||
|
// Section 3.4: 25 nm, 1 pax, pax(1.0), level 0, not short:
|
||||||
|
// base = 60*(15 + 25/3) = 1400; capacity=1 -> 0.6+0.4*1=1.0 -> 1400
|
||||||
|
assert_eq!(compute_pay(25.0, 1, 0, JobKind::Pax, 1.0, 0, false), 1400);
|
||||||
|
// 50 nm, 1 pax -> 60*(15+50/3) = 1900
|
||||||
|
assert_eq!(compute_pay(50.0, 1, 0, JobKind::Pax, 1.0, 0, false), 1900);
|
||||||
|
// 100 nm, 1 pax -> 60*(15+100/3) = 2900
|
||||||
|
assert_eq!(compute_pay(100.0, 1, 0, JobKind::Pax, 1.0, 0, false), 2900);
|
||||||
|
// 400 nm, 36 pax -> 60*(15+400/3)*(0.6+0.4*36^0.6) ≈ 35910
|
||||||
|
let p400_36 = compute_pay(400.0, 36, 0, JobKind::Pax, 1.0, 0, false);
|
||||||
|
assert!((p400_36 - 35910).abs() <= 20, "got {p400_36}");
|
||||||
|
// Short field +10%, level +5% per level, always multiple of 10
|
||||||
|
let p_short = compute_pay(100.0, 1, 0, JobKind::Pax, 1.0, 2, true);
|
||||||
|
assert_eq!(p_short % 10, 0);
|
||||||
|
assert!(p_short > 2900);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_determinism_and_invariants() {
|
||||||
|
let data = generate_synthetic_world(42, 600);
|
||||||
|
let world = WorldIndex::build(data, true);
|
||||||
|
let epoch_ms: i64 = 1_796_067_000_000;
|
||||||
|
let bucket = bucket_of(epoch_ms);
|
||||||
|
let expected_exp = (bucket + 1) * BUCKET_MS;
|
||||||
|
|
||||||
|
let opts = BoardOptions {
|
||||||
|
salt: 123_456_789,
|
||||||
|
day_salt: 987_654,
|
||||||
|
level: 2,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut scratch = BoardScratch::with_capacity();
|
||||||
|
let planes: Vec<_> = world.plane_list.iter().collect();
|
||||||
|
|
||||||
|
for ap_idx in 0..30 {
|
||||||
|
let origin = &world.airports[ap_idx];
|
||||||
|
for &plane in &planes {
|
||||||
|
let b1 = generate_board(&world, ap_idx, epoch_ms, &[plane], &opts, &mut scratch);
|
||||||
|
let b2 = generate_board(&world, ap_idx, epoch_ms, &[plane], &opts, &mut scratch);
|
||||||
|
|
||||||
|
// Property 1: byte-identical JSON for identical inputs
|
||||||
|
let j1 = serde_json::to_string(&b1).unwrap();
|
||||||
|
let j2 = serde_json::to_string(&b2).unwrap();
|
||||||
|
assert_eq!(j1, j2, "non-deterministic board for {} / {}", origin.raw.code, plane.id);
|
||||||
|
|
||||||
|
// Property 3, 4, 5: unique IDs, no duplicate fixed-wing dest, valid expiry, pay % 10 == 0
|
||||||
|
let mut ids = HashSet::new();
|
||||||
|
let mut fw_dests = HashSet::new();
|
||||||
|
for job in &b1 {
|
||||||
|
assert!(ids.insert(job.id.clone()), "duplicate id {}", job.id);
|
||||||
|
assert_eq!(job.expires, expected_exp);
|
||||||
|
assert!(job.pay >= 0 && job.pay % 10 == 0, "invalid pay {}", job.pay);
|
||||||
|
if job.to != job.from && !job.big_charter && !job.water {
|
||||||
|
assert!(
|
||||||
|
fw_dests.insert(job.to.clone()),
|
||||||
|
"duplicate dest {} on {}/{}",
|
||||||
|
job.to,
|
||||||
|
origin.raw.code,
|
||||||
|
plane.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Property 2: adjacent buckets share no IDs; salt change reshuffles
|
||||||
|
let next_epoch = epoch_ms + BUCKET_MS;
|
||||||
|
let b_next = generate_board(&world, ap_idx, next_epoch, &[plane], &opts, &mut scratch);
|
||||||
|
for job in &b_next {
|
||||||
|
assert!(!ids.contains(&job.id), "adjacent bucket shared id {}", job.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_salt_change_reshuffles_and_events_company_projects() {
|
||||||
|
let data = generate_synthetic_world(99, 500);
|
||||||
|
let world = WorldIndex::build(data, true);
|
||||||
|
let epoch_ms: i64 = 1_796_067_000_000;
|
||||||
|
let plane = world.planes.get("utility_turboprop").unwrap();
|
||||||
|
let ap_idx = world
|
||||||
|
.airports
|
||||||
|
.iter()
|
||||||
|
.position(|a| a.raw.size == 0)
|
||||||
|
.unwrap();
|
||||||
|
let origin = &world.airports[ap_idx];
|
||||||
|
let other_hub = world
|
||||||
|
.airports
|
||||||
|
.iter()
|
||||||
|
.find(|a| a.raw.size == 0 && a.raw.code != origin.raw.code)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let ev = EventState {
|
||||||
|
id: "wk-test".into(),
|
||||||
|
mode: EventMode::Inside,
|
||||||
|
bbox: EventBox {
|
||||||
|
min_lat: origin.raw.lat - 10.0,
|
||||||
|
max_lat: origin.raw.lat + 10.0,
|
||||||
|
min_lon: origin.raw.lon - 10.0,
|
||||||
|
max_lon: origin.raw.lon + 10.0,
|
||||||
|
},
|
||||||
|
what: "Relief Cargo".into(),
|
||||||
|
kind: JobKind::Cargo,
|
||||||
|
met: true,
|
||||||
|
rule: EventRule { min_kg: Some(200.0) },
|
||||||
|
};
|
||||||
|
|
||||||
|
let co = CompanyState {
|
||||||
|
id: "CO1".into(),
|
||||||
|
name: "Test Air".into(),
|
||||||
|
bases: vec![
|
||||||
|
CompanyBase {
|
||||||
|
code: origin.raw.code.clone(),
|
||||||
|
name: origin.raw.name.clone(),
|
||||||
|
lat: origin.raw.lat,
|
||||||
|
lon: origin.raw.lon,
|
||||||
|
gates: 2,
|
||||||
|
low: false,
|
||||||
|
},
|
||||||
|
CompanyBase {
|
||||||
|
code: other_hub.raw.code.clone(),
|
||||||
|
name: other_hub.raw.name.clone(),
|
||||||
|
lat: other_hub.raw.lat,
|
||||||
|
lon: other_hub.raw.lon,
|
||||||
|
gates: 1,
|
||||||
|
low: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
let proj = ProjectState {
|
||||||
|
code: other_hub.raw.code.clone(),
|
||||||
|
name: other_hub.raw.name.clone(),
|
||||||
|
lat: other_hub.raw.lat,
|
||||||
|
lon: other_hub.raw.lon,
|
||||||
|
what: "Runway lights".into(),
|
||||||
|
sling: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
let opts1 = BoardOptions {
|
||||||
|
salt: 11111,
|
||||||
|
day_salt: 22222,
|
||||||
|
level: 1,
|
||||||
|
event: Some(&ev),
|
||||||
|
company: Some(&co),
|
||||||
|
projects: &[proj],
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let opts2 = BoardOptions {
|
||||||
|
salt: 99999,
|
||||||
|
..opts1.clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut scratch = BoardScratch::with_capacity();
|
||||||
|
let b1 = generate_board(&world, ap_idx, epoch_ms, &[plane], &opts1, &mut scratch);
|
||||||
|
let b2 = generate_board(&world, ap_idx, epoch_ms, &[plane], &opts2, &mut scratch);
|
||||||
|
|
||||||
|
let dests1: Vec<_> = b1.iter().map(|j| (&j.to, j.pay)).collect();
|
||||||
|
let dests2: Vec<_> = b2.iter().map(|j| (&j.to, j.pay)).collect();
|
||||||
|
assert_ne!(dests1, dests2, "salt change should reshuffle board");
|
||||||
|
|
||||||
|
// Verify presentation partition & sort
|
||||||
|
let part = partition_and_sort(
|
||||||
|
&world,
|
||||||
|
origin,
|
||||||
|
plane,
|
||||||
|
b1,
|
||||||
|
epoch_ms,
|
||||||
|
true,
|
||||||
|
SortMode::NetPerHour,
|
||||||
|
&FilterMode::All,
|
||||||
|
);
|
||||||
|
assert!(!part.ok.is_empty());
|
||||||
|
for w in part.ok.windows(2) {
|
||||||
|
assert!(w[0].net_per_hour >= w[1].net_per_hour);
|
||||||
|
}
|
||||||
|
assert!(part.refresh_mins > 0 && part.refresh_mins <= 30);
|
||||||
|
|
||||||
|
// Sanity check NM constant
|
||||||
|
assert_eq!(NM, 1852.0);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user