use std::collections::{BTreeMap, VecDeque};
use std::fs::File;
use std::io;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
use std::sync::{Arc, Condvar, Mutex, PoisonError};
use std::time::{Duration, Instant};
use crate::error::ErrorExt;
use crate::navmesh::build::{BuildOptions, TileOutcome, build_tile};
use crate::navmesh::capture::{Capture, CaptureFile, CaptureWriter};
use crate::navmesh::client::{Client, FetchError, Host, RetryPolicy};
use crate::navmesh::detour::{NavMesh, NavMeshParams, NavMeshSet};
use crate::navmesh::islands::{IslandReport, filter_islands};
use crate::navmesh::wire::{TileInput, WorldBlock, WorldParams};
use crate::warning::{Warning, WarningSink};
const FETCH_QUEUE: usize = 64;
const MAX_POLYS: i32 = 1 << 20;
const MAX_PLAN: i64 = 1 << 22;
const MIN_WINDOW: usize = 256;
#[derive(Debug)]
#[non_exhaustive]
pub enum Source {
Servers(Vec<Host>),
Capture(Capture),
CaptureFile(CaptureFile),
}
pub const MAX_WORKERS: usize = 256;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct GenerateOptions {
pub range: Option<[i32; 4]>,
pub base: Option<NavMeshSet>,
pub skip_existing: bool,
pub checkpoint: Option<(usize, PathBuf)>,
pub record: Option<PathBuf>,
pub threads: Option<usize>,
pub connections: Option<usize>,
pub seed: Option<[f32; 3]>,
pub island_filter: bool,
pub build: BuildOptions,
pub retry: RetryPolicy,
#[doc(hidden)]
pub window: Option<usize>,
}
impl Default for GenerateOptions {
fn default() -> Self {
Self {
range: None,
base: None,
skip_existing: false,
checkpoint: None,
record: None,
threads: None,
connections: None,
seed: None,
island_filter: true,
build: BuildOptions::default(),
retry: RetryPolicy::default(),
window: None,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Progress {
pub done: usize,
pub total: usize,
pub built: usize,
pub empty: usize,
pub failed: usize,
pub elapsed: Duration,
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct GenerateReport {
pub set: NavMeshSet,
pub built: usize,
pub empty: usize,
pub failed: Vec<(i32, i32, String)>,
pub fetch_errors: Vec<(i32, i32, String)>,
pub stray_tiles: usize,
pub islands: Option<IslandReport>,
pub seconds: f64,
pub stopped: Option<String>,
pub seed: Option<[f32; 3]>,
pub peak_pending: usize,
pub window: usize,
}
pub(crate) fn clip_range(range: Option<[i32; 4]>, tiles_x: i32, tiles_z: i32) -> Option<[i32; 4]> {
let [rx0, rz0, rx1, rz1] = range.unwrap_or([0, 0, -1, -1]);
let last = |n: i32| n.saturating_sub(1);
let x1 = if rx1 < 0 {
last(tiles_x)
} else {
rx1.min(last(tiles_x))
};
let z1 = if rz1 < 0 {
last(tiles_z)
} else {
rz1.min(last(tiles_z))
};
let (x0, z0) = (rx0.max(0), rz0.max(0));
(x0 <= x1 && z0 <= z1).then_some([x0, z0, x1, z1])
}
pub fn plan_tiles(world: &WorldBlock, range: Option<[i32; 4]>) -> io::Result<Vec<(i32, i32)>> {
let Some([x0, z0, x1, z1]) = clip_range(range, world.tiles_x, world.tiles_z) else {
return Err(error!("empty tile range"));
};
let count = (i64::from(x1) - i64::from(x0) + 1) * (i64::from(z1) - i64::from(z0) + 1);
if count > MAX_PLAN {
return Err(error!(
"tile range too large ({count} tiles, limit {MAX_PLAN})"
));
}
let count = usize::try_from(count).map_err(|_| error!("empty tile range"))?;
let block = world.block_size.max(1);
let step = usize::try_from(block).unwrap_or(1);
let mut plan = Vec::new();
plan.try_reserve_exact(count)
.map_err(|e| error!("cannot allocate the tile plan ({count} tiles): {e}"))?;
for bz in (z0..=z1).step_by(step) {
for bx in (x0..=x1).step_by(step) {
let ez = bz.saturating_add(block - 1).min(z1);
let ex = bx.saturating_add(block - 1).min(x1);
for z in bz..=ez {
for x in bx..=ex {
plan.push((x, z));
}
}
}
}
Ok(plan)
}
pub fn probe(host: &Host, policy: &RetryPolicy) -> io::Result<WorldParams> {
Client::connect(host, policy)?.world_params()
}
enum Fetch {
Tile(TileInput),
Skipped,
Wire(String),
Net(String),
}
struct Fetched {
idx: usize,
what: Fetch,
}
enum Outcome {
Tile(Box<TileOutcome>),
Skipped,
Failed(String),
Net(String),
}
struct Built {
idx: usize,
outcome: Outcome,
input: Option<TileInput>,
}
enum FetchFrom<'a> {
Servers(&'a [Host]),
Capture(&'a Capture),
CaptureFile(&'a CaptureFile),
}
struct FetchShared<'a> {
plan: &'a [(i32, i32)],
next: &'a AtomicUsize,
stop: &'a AtomicBool,
alive: &'a AtomicUsize,
connections: usize,
gate: &'a Gate,
window: usize,
world: &'a WorldParams,
from: FetchFrom<'a>,
policy: &'a RetryPolicy,
}
struct Handback {
why: String,
proven: u32,
unproven: u32,
}
impl Handback {
fn is_poison(&self) -> bool {
self.proven >= 2 || (self.proven >= 1 && self.unproven >= 1)
}
}
#[derive(Default)]
struct Work {
applied: usize,
retry: VecDeque<(usize, Handback)>,
inflight: usize,
}
struct Gate {
work: Mutex<Work>,
changed: Condvar,
}
impl Gate {
fn new() -> Self {
Self {
work: Mutex::new(Work::default()),
changed: Condvar::new(),
}
}
fn set_applied(&self, n: usize) {
self.work
.lock()
.unwrap_or_else(PoisonError::into_inner)
.applied = n;
self.changed.notify_all();
}
fn wake(&self) {
drop(self.work.lock().unwrap_or_else(PoisonError::into_inner));
self.changed.notify_all();
}
fn claim(
&self,
shared: &FetchShared<'_>,
windowed: bool,
prefer_fresh: bool,
before_wait: &mut dyn FnMut(),
) -> Option<(usize, Option<Handback>)> {
let mut work = self.work.lock().unwrap_or_else(PoisonError::into_inner);
let mut waited = Some(());
loop {
if shared.stop.load(Ordering::Relaxed) {
return None;
}
let next = shared.next.load(Ordering::Relaxed);
let fresh = next < shared.plan.len()
&& (!windowed || next < work.applied.saturating_add(shared.window));
if !(prefer_fresh && fresh)
&& let Some((idx, back)) = work.retry.pop_front()
{
work.inflight += 1;
return Some((idx, Some(back)));
}
if fresh {
shared.next.store(next + 1, Ordering::Relaxed);
work.inflight += 1;
return Some((next, None));
}
if next >= shared.plan.len() && work.inflight == 0 {
return None;
}
if waited.take().is_some() {
before_wait();
}
work = self
.changed
.wait(work)
.unwrap_or_else(PoisonError::into_inner);
}
}
fn done(&self, idx: usize, back: Option<Handback>) {
let mut work = self.work.lock().unwrap_or_else(PoisonError::into_inner);
work.inflight = work.inflight.saturating_sub(1);
if let Some(back) = back {
work.retry.push_back((idx, back));
}
drop(work);
self.changed.notify_all();
}
}
struct StopOnDrop<'a> {
stop: &'a AtomicBool,
gate: &'a Gate,
}
impl Drop for StopOnDrop<'_> {
fn drop(&mut self) {
self.stop.store(true, Ordering::Relaxed);
self.gate.wake();
}
}
enum Got {
Done(Fetch),
Dead(String),
}
fn fetch_one(
shared: &FetchShared<'_>,
slot: usize,
client: &mut Option<Client>,
at: (i32, i32),
) -> Got {
let (col, row) = at;
match &shared.from {
FetchFrom::Capture(capture) => Got::Done(
capture
.tile(col, row)
.map_or(Fetch::Skipped, |tile| Fetch::Tile(tile.clone())),
),
FetchFrom::CaptureFile(file) => Got::Done(match file.tile(col, row) {
Ok(Some(tile)) => Fetch::Tile(tile),
Ok(None) => Fetch::Skipped,
Err(e) if e.kind() == io::ErrorKind::InvalidData => Fetch::Wire(e.to_string()),
Err(e) => Fetch::Net(e.to_string()),
}),
FetchFrom::Servers(hosts) => {
if client.is_none() {
let Some(host) = hosts.get(slot % hosts.len().max(1)) else {
return Got::Dead("no data server".to_owned());
};
*client = Some(Client::lazy(host, shared.policy));
}
let Some(conn) = client.as_mut() else {
return Got::Dead("not connected".to_owned());
};
match conn.tile(col, row, shared.world) {
Ok(tile) => Got::Done(Fetch::Tile(tile)),
Err(FetchError::Wire(e)) => Got::Done(Fetch::Wire(e.to_string())),
Err(FetchError::Network(e)) if conn.last_failure_was_answer() => {
Got::Done(Fetch::Net(e.to_string()))
}
Err(FetchError::Network(e)) => Got::Dead(e.to_string()),
}
}
}
}
struct FetcherExit<'a> {
shared: &'a FetchShared<'a>,
armed: bool,
}
impl Drop for FetcherExit<'_> {
fn drop(&mut self) {
if self.armed {
self.shared.stop.store(true, Ordering::Relaxed);
self.shared.gate.wake();
self.shared.alive.fetch_sub(1, Ordering::AcqRel);
}
}
}
fn shares_host(shared: &FetchShared<'_>, slot: usize) -> bool {
match &shared.from {
FetchFrom::Servers(hosts) if !hosts.is_empty() => {
(0..shared.connections)
.filter(|k| k % hosts.len() == slot % hosts.len())
.count()
> 1
}
_ => false,
}
}
fn fetcher(shared: &FetchShared<'_>, slot: usize, tx: &SyncSender<Fetched>) {
let mut exit = FetcherExit {
shared,
armed: true,
};
let mut client: Option<Client> = None;
let mut delivered = false;
let others_on_host = shares_host(shared, slot);
while let Some((idx, back)) = shared.gate.claim(shared, true, !delivered, &mut || {
if others_on_host {
client = None;
}
}) {
let Some(&at) = shared.plan.get(idx) else {
shared.gate.done(idx, None);
break;
};
let what = match fetch_one(shared, slot, &mut client, at) {
Got::Done(what) => what,
Got::Dead(message) => {
let (mut proven, mut unproven) =
back.as_ref().map_or((0, 0), |b| (b.proven, b.unproven));
if delivered {
proven += 1;
} else {
unproven += 1;
}
let why = back.map_or(message, |b| b.why);
let hand = Handback {
why,
proven,
unproven,
};
if !hand.is_poison() {
shared.gate.done(idx, Some(hand));
break;
}
let why = hand.why;
client = None;
Fetch::Net(why)
}
};
delivered |= matches!(what, Fetch::Tile(_) | Fetch::Skipped);
if tx.send(Fetched { idx, what }).is_err() {
return;
}
shared.gate.done(idx, None);
}
exit.armed = false;
if shared.alive.fetch_sub(1, Ordering::AcqRel) == 1 && !shared.stop.load(Ordering::Relaxed) {
while let Some((idx, back)) = shared.gate.claim(shared, false, false, &mut || {}) {
let message = back.map_or_else(
|| "not fetched: every connection failed".to_owned(),
|b| b.why,
);
let sent = tx.send(Fetched {
idx,
what: Fetch::Net(message),
});
shared.gate.done(idx, None);
if sent.is_err() {
return;
}
}
}
}
fn build_guarded(build: impl FnOnce() -> io::Result<TileOutcome>) -> Outcome {
match catch_unwind(AssertUnwindSafe(build)) {
Ok(Ok(outcome)) => Outcome::Tile(Box::new(outcome)),
Ok(Err(e)) => Outcome::Failed(e.to_string()),
Err(_) => Outcome::Failed("internal error: tile build panicked".to_owned()),
}
}
fn builder(
world: &WorldParams,
options: &GenerateOptions,
rx: &Mutex<Receiver<Fetched>>,
tx: &SyncSender<Built>,
) {
let record = options.record.is_some();
loop {
let msg = rx.lock().unwrap_or_else(PoisonError::into_inner).recv();
let Ok(Fetched { idx, what }) = msg else {
return;
};
let (outcome, input) = match what {
Fetch::Tile(input) => {
let outcome = build_guarded(|| build_tile(world, &input, &options.build));
(outcome, record.then_some(input))
}
Fetch::Skipped => (Outcome::Skipped, None),
Fetch::Wire(m) => (Outcome::Failed(m), None),
Fetch::Net(m) => (Outcome::Net(m), None),
};
if tx
.send(Built {
idx,
outcome,
input,
})
.is_err()
{
return;
}
}
}
struct Assembler<'a> {
mesh: NavMesh,
plan: &'a [(i32, i32)],
report: GenerateReport,
recorder: Option<CaptureWriter<File>>,
checkpoint: Option<(usize, &'a Path)>,
grid: (i32, i32),
stop: &'a AtomicBool,
gate: &'a Gate,
skipped: usize,
start: Instant,
}
impl Assembler<'_> {
fn finished(&self) -> usize {
self.report.built + self.report.empty + self.report.failed.len()
}
fn halt(&mut self, reason: String) {
if self.report.stopped.is_none() {
self.report.stopped = Some(reason);
}
self.stop.store(true, Ordering::Relaxed);
self.gate.wake();
}
fn fail(&mut self, (x, z): (i32, i32), message: String, warnings: &mut dyn WarningSink) {
warnings.warn(Warning::new(
Some("navmesh-tile-failed"),
format!("tile ({x},{z}): {message}"),
));
self.report.failed.push((x, z, message));
}
fn apply(&mut self, msg: Built, warnings: &mut dyn WarningSink) {
let Some(&at) = self.plan.get(msg.idx) else {
return;
};
let finished_before = self.finished();
if let (Some(w), Some(input)) = (self.recorder.as_mut(), msg.input.as_ref())
&& let Err(e) = w.add(input)
{
self.halt(format!("cannot write the capture: {e}"));
}
match msg.outcome {
Outcome::Skipped => self.skipped += 1,
Outcome::Net(m) => self.report.fetch_errors.push((at.0, at.1, m)),
Outcome::Failed(m) => self.fail(at, m, warnings),
Outcome::Tile(outcome) => match *outcome {
TileOutcome::Empty => {
self.mesh.remove_tile_at(at.0, at.1);
self.report.empty += 1;
}
TileOutcome::Built {
data,
stray_part_ids,
..
} => match self.mesh.set_tile(data) {
Ok(_) => {
self.report.built += 1;
if stray_part_ids > 0 {
self.report.stray_tiles += 1;
warnings.warn(Warning::new(
Some("navmesh-stray-part"),
format!(
"tile ({},{}): {stray_part_ids} polygons had out-of-range door part ids, which were ignored",
at.0, at.1
),
));
}
}
Err(e) => self.fail(at, e.to_string(), warnings),
},
},
}
self.checkpoint_if_due(finished_before);
}
fn checkpoint_if_due(&mut self, finished_before: usize) {
let Some((every, path)) = self.checkpoint else {
return;
};
let done = self.finished();
if self.report.stopped.is_some() || done == finished_before || !done.is_multiple_of(every) {
return;
}
let set = self.mesh.to_set(self.grid.0, self.grid.1);
if let Err(e) = set.write_atomic(path) {
self.halt(format!("checkpoint {}: {e}", path.display()));
}
}
fn progress(&self, progress: &mut dyn FnMut(&Progress)) {
progress(&Progress {
done: self.finished() + self.skipped + self.report.fetch_errors.len(),
total: self.plan.len(),
built: self.report.built,
empty: self.report.empty,
failed: self.report.failed.len(),
elapsed: self.start.elapsed(),
});
}
}
fn capture_range(capture: &Capture) -> Option<[i32; 4]> {
let tiles = capture.tiles();
let first = tiles.first()?;
let mut r = [first.x, first.z, first.x, first.z];
for t in tiles {
r = [r[0].min(t.x), r[1].min(t.z), r[2].max(t.x), r[3].max(t.z)];
}
Some(r)
}
fn create_mesh(world: &WorldBlock, base: Option<&NavMeshSet>) -> io::Result<(NavMesh, (i32, i32))> {
if let Some(set) = base {
let p = &set.params;
let mismatch = p.tile_width.to_bits() != world.tile_width.to_bits()
|| p.tile_height.to_bits() != world.tile_height.to_bits()
|| p.orig.iter().any(|c| c.to_bits() & 0x7fff_ffff != 0)
|| set.tiles_x != world.tiles_x
|| set.tiles_z != world.tiles_z;
if mismatch {
return Err(error!(
"base navmesh does not match the world (base: {} x {} tiles of {} x {} m at {:?}; world: {} x {} tiles of {} x {} m at [0.0, 0.0, 0.0])",
set.tiles_x,
set.tiles_z,
p.tile_width,
p.tile_height,
p.orig,
world.tiles_x,
world.tiles_z,
world.tile_width,
world.tile_height
));
}
return Ok((
NavMesh::from_set(set).prepend_error("cannot use the base mesh")?,
(set.tiles_x, set.tiles_z),
));
}
let max_tiles = world
.tiles_x
.checked_mul(world.tiles_z)
.filter(|n| *n > 0)
.ok_or_else(|| error!("invalid tile grid {} x {}", world.tiles_x, world.tiles_z))?;
let params = NavMeshParams {
orig: [0.0; 3],
tile_width: world.tile_width,
tile_height: world.tile_height,
max_tiles,
max_polys: MAX_POLYS,
};
Ok((NavMesh::new(params)?, (world.tiles_x, world.tiles_z)))
}
fn run_pipeline(
asm: &mut Assembler<'_>,
world: &WorldParams,
from: FetchFrom<'_>,
default_connections: usize,
options: &GenerateOptions,
progress: &mut dyn FnMut(&Progress),
warnings: &mut dyn WarningSink,
) {
let builders = options
.threads
.unwrap_or_else(|| {
std::thread::available_parallelism().map_or(1, |n| n.get().saturating_sub(1))
})
.max(1);
let connections = options.connections.unwrap_or(default_connections).max(1);
let window = options
.window
.unwrap_or_else(|| {
MIN_WINDOW.max(
FETCH_QUEUE
.saturating_add(builders.saturating_mul(5))
.saturating_add(connections),
)
})
.max(1);
let (plan, stop, gate) = (asm.plan, asm.stop, asm.gate);
let next = AtomicUsize::new(0);
let alive = AtomicUsize::new(connections);
let shared = FetchShared {
plan,
next: &next,
stop,
alive: &alive,
connections,
gate,
window,
world,
from,
policy: &options.retry,
};
let (fetch_tx, fetch_rx) = sync_channel::<Fetched>(FETCH_QUEUE);
let (built_tx, built_rx) = sync_channel::<Built>(builders.saturating_mul(4));
let fetch_rx = Arc::new(Mutex::new(fetch_rx));
let mut peak = 0usize;
std::thread::scope(|scope| {
let _wake_on_exit = StopOnDrop { stop, gate };
for slot in 0..connections {
let tx = fetch_tx.clone();
let shared = &shared;
scope.spawn(move || fetcher(shared, slot, &tx));
}
drop(fetch_tx);
for _ in 0..builders {
let tx = built_tx.clone();
let rx = Arc::clone(&fetch_rx);
scope.spawn(move || builder(world, options, &rx, &tx));
}
drop(built_tx);
drop(fetch_rx);
let mut pending: BTreeMap<usize, Built> = BTreeMap::new();
let mut expected = 0usize;
for msg in built_rx {
pending.insert(msg.idx, msg);
peak = peak.max(pending.len());
while let Some(done) = pending.remove(&expected) {
expected += 1;
asm.apply(done, warnings);
gate.set_applied(expected);
asm.progress(progress);
}
}
});
asm.report.peak_pending = peak;
asm.report.window = window;
}
fn open_recorder(path: &Path, params: &WorldParams) -> io::Result<CaptureWriter<File>> {
let file = File::create(path).map_err(|e| error!("cannot create {}: {e}", path.display()))?;
CaptureWriter::new(file, params).prepend_error("cannot write the capture")
}
fn check_worker_counts(options: &GenerateOptions, source: &Source) -> io::Result<()> {
if let Source::Servers(hosts) = source
&& options.connections.is_none()
&& hosts.len() > MAX_WORKERS
{
return Err(error!(
"hosts: at most {MAX_WORKERS} data servers (one connection each)"
));
}
if options.threads.is_some_and(|n| n > MAX_WORKERS) {
return Err(error!("threads: at most {MAX_WORKERS} builder threads"));
}
if options.connections.is_some_and(|n| n > MAX_WORKERS) {
return Err(error!("connections: at most {MAX_WORKERS} connections"));
}
Ok(())
}
fn same_file(a: &Path, b: &Path) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
if let (Ok(x), Ok(y)) = (std::fs::metadata(a), std::fs::metadata(b))
&& x.dev() == y.dev()
&& x.ino() == y.ino()
{
return true;
}
}
matches!((std::fs::canonicalize(a), std::fs::canonicalize(b)), (Ok(x), Ok(y)) if x == y)
}
fn open_source<'a>(
source: &'a Source,
options: &GenerateOptions,
) -> io::Result<(WorldParams, Option<[i32; 4]>, FetchFrom<'a>, usize)> {
Ok(match source {
Source::Capture(c) => {
let range = options.range.or_else(|| capture_range(c));
(c.params().clone(), range, FetchFrom::Capture(c), 1)
}
Source::CaptureFile(c) => {
if let (Some(record), Some(read)) = (&options.record, c.path())
&& same_file(record, read)
{
return Err(error!(
"the recording {} is the capture being read; give another file",
record.display()
));
}
let range = options.range.or_else(|| c.bounds());
if !c.covers(range) {
let fmt = |r: Option<[i32; 4]>| {
r.map_or_else(
|| "no tiles".to_owned(),
|[x0, z0, x1, z1]| format!("{x0},{z0},{x1},{z1}"),
)
};
return Err(error!(
"the capture was indexed for the range {} but the run asks for {}; open the capture with the range of the run",
fmt(c.indexed_range()),
fmt(range)
));
}
(c.params().clone(), range, FetchFrom::CaptureFile(c), 1)
}
Source::Servers(hosts) => {
let first = hosts
.first()
.ok_or_else(|| error!("no data server given"))?;
(
probe(first, &options.retry)?,
options.range,
FetchFrom::Servers(hosts),
hosts.len(),
)
}
})
}
#[expect(
clippy::needless_pass_by_value,
reason = "the API hands the source (a capture can be large) over to the run"
)]
pub fn generate(
source: Source,
options: &GenerateOptions,
progress: &mut dyn FnMut(&Progress),
warnings: &mut dyn WarningSink,
) -> io::Result<GenerateReport> {
let start = Instant::now();
check_worker_counts(options, &source)?;
if options.skip_existing && options.base.is_none() {
return Err(error!("--skip-existing needs --base"));
}
let (world_params, range, from, default_connections) = open_source(&source, options)?;
let world = &world_params.world;
let (mesh, grid) = create_mesh(world, options.base.as_ref())?;
let mut plan = match range {
Some(r) => plan_tiles(world, Some(r))?,
None if matches!(source, Source::Capture(_) | Source::CaptureFile(_)) => {
return Err(error!("empty tile range"));
}
None => plan_tiles(world, None)?,
};
if options.skip_existing {
plan.retain(|&(x, z)| mesh.tile_at(x, z).is_none());
}
let recorder = match &options.record {
Some(path) => Some(open_recorder(path, &world_params)?),
None => None,
};
let stop = AtomicBool::new(false);
let gate = Gate::new();
let mut asm = Assembler {
mesh,
plan: &plan,
report: GenerateReport::default(),
recorder,
checkpoint: options
.checkpoint
.as_ref()
.map(|(n, path)| ((*n).max(1), path.as_path())),
grid,
stop: &stop,
gate: &gate,
skipped: 0,
start,
};
run_pipeline(
&mut asm,
&world_params,
from,
default_connections,
options,
progress,
warnings,
);
let skipped = asm.skipped;
let mut report = std::mem::take(&mut asm.report);
report.seed = Some(options.seed.unwrap_or(world.seed_position));
let mut mesh = asm.mesh;
if report.stopped.is_none() && options.island_filter && world.filter_isolated_islands {
let seed = options.seed.unwrap_or(world.seed_position);
report.islands = filter_islands(&mut mesh, seed);
if report.islands.is_none() {
warnings.warn(Warning::new(
Some("navmesh-seed"),
format!(
"island filter skipped: no polygon near the seed ({:.1} {:.1} {:.1})",
seed[0], seed[1], seed[2]
),
));
}
}
report.set = mesh.into_set(grid.0, grid.1);
report.seconds = start.elapsed().as_secs_f64();
progress(&Progress {
done: report.built
+ report.empty
+ report.failed.len()
+ skipped
+ report.fetch_errors.len(),
total: plan.len(),
built: report.built,
empty: report.empty,
failed: report.failed.len(),
elapsed: start.elapsed(),
});
Ok(report)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_panicking_fetcher_stops_the_run_and_wakes_the_waiters() {
let world = WorldParams::default();
let policy = RetryPolicy::default();
let plan = [(0, 0), (1, 0)];
let (next, stop, alive) = (
AtomicUsize::new(0),
AtomicBool::new(false),
AtomicUsize::new(2),
);
let gate = Gate::new();
let shared = FetchShared {
plan: &plan,
next: &next,
stop: &stop,
alive: &alive,
connections: 1,
gate: &gate,
window: 1,
world: &world,
from: FetchFrom::Servers(&[]),
policy: &policy,
};
let (ready_tx, ready_rx) = std::sync::mpsc::channel();
std::thread::scope(|scope| {
let waiter = scope.spawn(|| {
assert_eq!(
gate.claim(&shared, true, false, &mut || {}).map(|c| c.0),
Some(0)
);
ready_tx.send(()).ok();
gate.claim(&shared, true, false, &mut || {}).is_none()
});
ready_rx.recv().expect("waiter started");
let panicked = catch_unwind(AssertUnwindSafe(|| {
let _exit = FetcherExit {
shared: &shared,
armed: true,
};
std::panic::resume_unwind(Box::new("boom"))
}));
assert!(panicked.is_err());
assert!(waiter.join().expect("waiter"), "the waiter was released");
});
assert!(stop.load(Ordering::Relaxed));
assert_eq!(alive.load(Ordering::Relaxed), 1);
}
#[test]
fn a_fetcher_that_ends_normally_does_not_stop_the_run() {
let world = WorldParams::default();
let policy = RetryPolicy::default();
let (next, stop, alive) = (
AtomicUsize::new(0),
AtomicBool::new(false),
AtomicUsize::new(1),
);
let gate = Gate::new();
let shared = FetchShared {
plan: &[],
next: &next,
stop: &stop,
alive: &alive,
connections: 1,
gate: &gate,
window: 1,
world: &world,
from: FetchFrom::Servers(&[]),
policy: &policy,
};
drop(FetcherExit {
shared: &shared,
armed: false,
});
assert!(!stop.load(Ordering::Relaxed));
assert_eq!(alive.load(Ordering::Relaxed), 1);
}
#[test]
fn a_panicking_build_becomes_a_failed_tile() {
let outcome = build_guarded(|| std::panic::resume_unwind(Box::new("boom")));
assert!(
matches!(&outcome, Outcome::Failed(m) if m == "internal error: tile build panicked")
);
let outcome = build_guarded(|| Err(error!("bad tile")));
assert!(matches!(&outcome, Outcome::Failed(m) if m == "bad tile"));
let outcome = build_guarded(|| Ok(TileOutcome::Empty));
assert!(matches!(&outcome, Outcome::Tile(t) if **t == TileOutcome::Empty));
}
}