1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// Disable some noisy lints
#![allow(clippy::too_many_arguments, clippy::type_complexity)]

#[macro_use]
extern crate anyhow;
#[macro_use]
extern crate log;

use structopt::StructOpt;

use abstio::MapName;
use abstutil::Timer;
use geom::Duration;
use map_gui::colors::ColorSchemeChoice;
use map_gui::load::FutureLoader;
use map_gui::options::Options;
use map_gui::tools::{PopupMsg, URLManager};
use map_model::{Map, MapEdits};
use sim::Sim;
use widgetry::{EventCtx, Settings, State, Transition};

use crate::app::{App, Flags, PerMap};
use crate::common::jump_to_time_upon_startup;
use crate::pregame::TitleScreen;
use crate::sandbox::{GameplayMode, SandboxMode};

mod app;
mod challenges;
mod common;
mod debug;
mod devtools;
mod edit;
mod info;
mod layer;
mod ltn;
mod pregame;
mod sandbox;
mod ungap;

pub fn main() {
    let settings = Settings::new("A/B Street");
    run(settings);
}

#[derive(StructOpt)]
#[structopt(name = "abstreet", about = "The A/B Street traffic simulator")]
struct Args {
    #[structopt(flatten)]
    flags: Flags,
    /// Start with these map edits loaded. This should be the name of edits, not a full path.
    #[structopt(long = "edits")]
    start_with_edits: Option<String>,
    /// Initially position the camera here. The format is an OSM-style `zoom/lat/lon` string
    /// (https://wiki.openstreetmap.org/wiki/Browsing#Other_URL_tricks).
    #[structopt(long)]
    cam: Option<String>,
    /// Start the simulation at this time
    #[structopt(long = "time", parse(try_from_str = Duration::parse))]
    start_time: Option<Duration>,
    /// Load the map at this path as a secondary debug map to compare to the main one
    #[structopt(long = "diff")]
    diff_map: Option<String>,
    /// Print raw widgetry events to the console for debugging
    #[structopt(long)]
    dump_raw_events: bool,
    /// Override the monitor's auto-detected scale factor
    #[structopt(long)]
    scale_factor: Option<f64>,

    /// Dev mode exposes experimental tools useful for debugging, but that'd likely confuse most
    /// players.
    #[structopt(long)]
    dev: bool,
    /// The color scheme for map elements, agents, and the UI.
    #[structopt(long, parse(try_from_str = ColorSchemeChoice::parse))]
    color_scheme: Option<ColorSchemeChoice>,
    /// When making a screen recording, enable this option to hide some UI elements
    #[structopt(long)]
    minimal_controls: bool,

    /// Run a configured set of simulations and record prebaked data.
    #[structopt(long)]
    prebake: bool,

    /// Start at the tutorial intro screen
    #[structopt(long)]
    tutorial_intro: bool,
    /// Start by listing gameplay challenges
    #[structopt(long)]
    challenges: bool,
    /// Start in the simulation sandbox mode
    #[structopt(long)]
    sandbox: bool,
    /// Start by showing community proposals
    #[structopt(long)]
    proposals: bool,
    /// Launch Ungap the Map, a bike network planning tool
    #[structopt(long)]
    ungap: bool,
    /// Start a low-traffic neighborhood planner
    #[structopt(long)]
    ltn: bool,
    /// Start by listing internal developer tools
    #[structopt(long)]
    devtools: bool,
    /// Start by showing this KMl file in a debug viewer
    #[structopt(long = "kml")]
    load_kml: Option<String>,
    /// Start playing a particular challenge
    #[structopt(long)]
    challenge: Option<String>,
    /// Start on a particular tutorial stage
    #[structopt(long)]
    tutorial: Option<usize>,
    /// Start in ActDev mode for a particular site name.
    #[structopt(long)]
    actdev: Option<String>,
    /// Start by showing an ActDev scenario. Either "base" or "go_active".
    #[structopt(long)]
    actdev_scenario: Option<String>,
}

struct Setup {
    flags: Flags,
    opts: Options,
    start_with_edits: Option<String>,
    initialize_tutorial: bool,
    center_camera: Option<String>,
    start_time: Option<Duration>,
    diff_map: Option<String>,
    mode: Mode,
}

// TODO Switch to explicit enum subcommands, each of which includes precisely the set of common
// flags that're valid for that mode
#[derive(PartialEq)]
enum Mode {
    SomethingElse,
    TutorialIntro,
    Challenges,
    Sandbox,
    Proposals,
    Ungap,
    Ltn,
    Devtools,
    LoadKML(String),
    Gameplay(GameplayMode),
}

fn run(mut settings: Settings) {
    abstutil::logger::setup();

    settings = settings
        .read_svg(Box::new(abstio::slurp_bytes))
        .window_icon(abstio::path("system/assets/pregame/icon.png"))
        .loading_tips(map_gui::tools::loading_tips())
        // This is approximately how much the 3 top panels in sandbox mode require.
        .require_minimum_width(1500.0);

    let mut args = Args::from_iter(abstutil::cli_args());
    args.flags.sim_flags.initialize();

    if args.prebake {
        challenges::prebake::prebake_all();
        return;
    }

    let mut setup = Setup {
        flags: args.flags,
        opts: Options::load_or_default(),
        start_with_edits: args.start_with_edits,
        initialize_tutorial: false,
        center_camera: args.cam,
        start_time: args.start_time,
        diff_map: args.diff_map,
        mode: if args.tutorial_intro {
            Mode::TutorialIntro
        } else if args.challenges {
            Mode::Challenges
        } else if args.sandbox {
            Mode::Sandbox
        } else if args.proposals {
            Mode::Proposals
        } else if args.ungap {
            Mode::Ungap
        } else if args.ltn {
            Mode::Ltn
        } else if args.devtools {
            Mode::Devtools
        } else if let Some(kml) = args.load_kml {
            Mode::LoadKML(kml)
        } else {
            Mode::SomethingElse
        },
    };

    setup.opts.toggle_day_night_colors = true;
    // Update options from CLI flags
    setup.opts.dev = args.dev;
    setup.opts.minimal_controls = args.minimal_controls;
    if let Some(cs) = args.color_scheme {
        setup.opts.color_scheme = cs;
        setup.opts.toggle_day_night_colors = false;
    }

    settings = settings.canvas_settings(setup.opts.canvas_settings.clone());

    if args.dump_raw_events {
        settings = settings.dump_raw_events();
    }
    if let Some(s) = args.scale_factor {
        settings = settings.scale_factor(s);
    }

    if let Some(x) = args.challenge {
        // TODO This is a weak form of mutual exclusion; just use subcommands
        assert!(setup.mode == Mode::SomethingElse);
        let mut aliases = Vec::new();
        'OUTER: for (_, stages) in challenges::Challenge::all() {
            for challenge in stages {
                if challenge.alias == x {
                    setup.flags.sim_flags.load = challenge.gameplay.map_name().path();
                    setup.mode = Mode::Gameplay(challenge.gameplay);
                    break 'OUTER;
                } else {
                    aliases.push(challenge.alias);
                }
            }
        }
        if setup.mode == Mode::SomethingElse {
            panic!("Invalid --challenge={}. Choices: {}", x, aliases.join(", "));
        }
    }
    if let Some(n) = args.tutorial {
        setup.initialize_tutorial = true;
        setup.mode = Mode::Gameplay(sandbox::GameplayMode::Tutorial(
            sandbox::TutorialPointer::new(n - 1, 0),
        ));
    }

    // Don't keep the scenario modifiers in the original sim_flags; they shouldn't apply to
    // other scenarios loaed in the UI later.
    let modifiers = setup.flags.sim_flags.scenario_modifiers.drain(..).collect();

    if setup.mode == Mode::SomethingElse && setup.flags.sim_flags.load.contains("scenarios/") {
        let (map_name, scenario) = abstio::parse_scenario_path(&setup.flags.sim_flags.load);
        setup.flags.sim_flags.load = map_name.path();
        setup.mode = Mode::Gameplay(sandbox::GameplayMode::PlayScenario(
            map_name, scenario, modifiers,
        ));
    }

    if let Some(site) = args.actdev {
        // Handle if the site was accidentally passed in with underscores. Otherwise, some study
        // areas won't be found!
        let site = site.replace("_", "-");
        let city = site.replace("-", "_");
        let name = MapName::new("gb", &city, "center");
        setup.flags.sim_flags.load = name.path();
        setup.flags.study_area = Some(site);
        // Parking data in the actdev maps is nonexistent, so many people have convoluted walking
        // routes just to fetch their car. Just disable parking entirely.
        setup.flags.sim_flags.opts.infinite_parking = true;
        let scenario = if args.actdev_scenario == Some("go_active".to_string()) {
            "go_active".to_string()
        } else {
            "base".to_string()
        };
        setup.mode = Mode::Gameplay(sandbox::GameplayMode::Actdev(name, scenario, false));
    }

    widgetry::run(settings, |ctx| setup_app(ctx, setup))
}

fn setup_app(ctx: &mut EventCtx, mut setup: Setup) -> (App, Vec<Box<dyn State<App>>>) {
    let title = !setup.opts.dev
        && !setup.flags.sim_flags.load.contains("player/save")
        && !setup.flags.sim_flags.load.contains("/scenarios/")
        && setup.mode == Mode::SomethingElse;

    // Load the map used previously if we're starting on the title screen without any overrides.
    if title && setup.flags.sim_flags.load == MapName::seattle("montlake").path() {
        if let Ok(default) = abstio::maybe_read_json::<map_gui::tools::DefaultMap>(
            abstio::path_player("maps.json"),
            &mut Timer::throwaway(),
        ) {
            setup.flags.sim_flags.load = default.last_map.path();
        }
    }

    // If we're starting directly in a challenge mode, the tutorial, or by playing a scenario,
    // usually time is midnight, so save some effort and start with the correct color scheme. If
    // we're loading a savestate and it's actually daytime, we'll pay a small penalty to switch
    // colors.
    if let Mode::Gameplay(
        GameplayMode::PlayScenario(_, _, _)
        | GameplayMode::FixTrafficSignals
        | GameplayMode::OptimizeCommute(_, _)
        | GameplayMode::Tutorial(_),
    ) = setup.mode
    {
        setup.opts.color_scheme = map_gui::colors::ColorSchemeChoice::NightMode;
    }
    if setup.mode != Mode::SomethingElse {
        setup.opts.color_scheme = map_gui::colors::ColorSchemeChoice::DayMode;
    }
    let cs = map_gui::colors::ColorScheme::new(ctx, setup.opts.color_scheme);

    // No web support; this uses blocking IO
    let secondary = setup.diff_map.as_ref().map(|path| {
        ctx.loading_screen("load secondary map", |ctx, timer| {
            // Use this low-level API, since the secondary map file probably isn't in the usual
            // directory structure
            let mut map: Map = abstio::read_binary(path.clone(), timer);
            map.map_loaded_directly(timer);
            let sim = Sim::new(&map, setup.flags.sim_flags.opts.clone());
            let mut per_map =
                PerMap::map_loaded(map, sim, setup.flags.clone(), &setup.opts, &cs, ctx, timer);
            per_map.is_secondary = true;
            per_map
        })
    });

    // SimFlags::load doesn't know how to do async IO, which we need on the web. But in the common
    // case, all we're creating there is a map. If so, use the proper async interface.
    //
    // Note if we started with a scenario, main() rewrote it to be the appropriate map, along with
    // mode.
    if setup.flags.sim_flags.load.contains("/maps/") {
        // Get App created with a dummy blank map
        let map = Map::blank();
        let sim = Sim::new(&map, setup.flags.sim_flags.opts.clone());
        let primary = PerMap::map_loaded(
            map,
            sim,
            setup.flags.clone(),
            &setup.opts,
            &cs,
            ctx,
            &mut Timer::throwaway(),
        );
        let app = App {
            primary,
            secondary: None,
            store_unedited_map_in_secondary: false,
            cs,
            opts: setup.opts.clone(),
            per_obj: crate::app::PerObjectActions::new(),
            session: crate::app::SessionState::empty(),
        };
        let map_name = MapName::from_path(&app.primary.current_flags.sim_flags.load).unwrap();
        let states = vec![map_gui::load::MapLoader::new_state(
            ctx,
            &app,
            map_name,
            Box::new(move |ctx, app| {
                Transition::Clear(continue_app_setup(ctx, app, title, setup, secondary))
            }),
        )];
        (app, states)
    } else {
        // We're loading a savestate or a RawMap. Do it with blocking IO. This won't
        // work on the web.
        let primary = ctx.loading_screen("load map", |ctx, timer| {
            assert!(setup.flags.sim_flags.scenario_modifiers.is_empty());
            let (map, sim, _) = setup.flags.sim_flags.load_synchronously(timer);
            PerMap::map_loaded(map, sim, setup.flags.clone(), &setup.opts, &cs, ctx, timer)
        });
        assert!(secondary.is_none());
        let mut app = App {
            primary,
            secondary: None,
            store_unedited_map_in_secondary: false,
            cs,
            opts: setup.opts.clone(),
            per_obj: crate::app::PerObjectActions::new(),
            session: crate::app::SessionState::empty(),
        };

        let states = continue_app_setup(ctx, &mut app, title, setup, None);
        (app, states)
    }
}

fn continue_app_setup(
    ctx: &mut EventCtx,
    app: &mut App,
    title: bool,
    setup: Setup,
    secondary: Option<PerMap>,
) -> Vec<Box<dyn State<App>>> {
    // Run this after loading the primary map. That process wipes out app.secondary.
    app.secondary = secondary;

    if !URLManager::change_camera(
        ctx,
        setup.center_camera.as_ref(),
        app.primary.map.get_gps_bounds(),
    ) {
        app.primary.init_camera_for_loaded_map(ctx, title);
    }

    // Handle savestates
    let savestate = if app
        .primary
        .current_flags
        .sim_flags
        .load
        .contains("player/saves/")
    {
        assert!(setup.mode == Mode::SomethingElse);
        Some(app.primary.clear_sim())
    } else {
        None
    };

    // Just apply this here, don't plumb to SimFlags or anything else. We recreate things using
    // these flags later, but we don't want to keep applying the same edits.
    if let Some(ref edits_name) = setup.start_with_edits {
        // Remote edits require another intermediate state to load
        if let Some(id) = edits_name.strip_prefix("remote/") {
            let (_, outer_progress_rx) = futures_channel::mpsc::channel(1);
            let (_, inner_progress_rx) = futures_channel::mpsc::channel(1);
            let url = format!("{}/get?id={}", crate::common::share::PROPOSAL_HOST_URL, id);
            return vec![FutureLoader::<App, Vec<u8>>::new_state(
                ctx,
                Box::pin(async move {
                    let bytes = abstio::http_get(url).await?;
                    let wrapper: Box<dyn Send + FnOnce(&App) -> Vec<u8>> = Box::new(move |_| bytes);
                    Ok(wrapper)
                }),
                outer_progress_rx,
                inner_progress_rx,
                "Downloading proposal",
                Box::new(move |ctx, app, result| {
                    match result
                        .and_then(|bytes| MapEdits::load_from_bytes(&app.primary.map, bytes))
                    {
                        Ok(edits) => Transition::Clear(finish_app_setup(
                            ctx,
                            app,
                            title,
                            savestate,
                            Some(edits),
                            setup,
                        )),
                        Err(err) => {
                            // TODO Fail more gracefully -- add a popup with the error, but continue
                            // app setup?
                            error!("Couldn't load remote proposal: {}", err);
                            Transition::Replace(PopupMsg::new_state(
                                ctx,
                                "Couldn't load remote proposal",
                                vec![err.to_string()],
                            ))
                        }
                    }
                }),
            )];
        }

        for path in [
            abstio::path_edits(app.primary.map.get_name(), edits_name),
            abstio::path(format!("system/proposals/{}.json", edits_name)),
        ] {
            if abstio::file_exists(&path) {
                let edits = map_model::MapEdits::load_from_file(
                    &app.primary.map,
                    path,
                    &mut Timer::throwaway(),
                )
                .unwrap();
                return finish_app_setup(ctx, app, title, savestate, Some(edits), setup);
            }
        }

        // TODO Fail more gracefully -- add a popup with the error, but continue app setup?
        panic!("Can't start with nonexistent edits {}", edits_name);
    }

    finish_app_setup(ctx, app, title, savestate, None, setup)
}

fn finish_app_setup(
    ctx: &mut EventCtx,
    app: &mut App,
    title: bool,
    savestate: Option<Sim>,
    edits: Option<MapEdits>,
    setup: Setup,
) -> Vec<Box<dyn State<App>>> {
    if setup.mode == Mode::Ungap {
        app.store_unedited_map_in_secondary = true;
    }
    if let Some(edits) = edits {
        ctx.loading_screen("apply initial edits", |ctx, timer| {
            crate::edit::apply_map_edits(ctx, app, edits);
            app.primary.map.recalculate_pathfinding_after_edits(timer);
            app.primary.clear_sim();
        });
    }

    if setup.initialize_tutorial {
        crate::sandbox::gameplay::Tutorial::initialize(ctx, app);
    }

    if title {
        return vec![TitleScreen::new_state(ctx, app)];
    }

    let state = if let Some(ss) = savestate {
        app.primary.sim = ss;
        SandboxMode::start_from_savestate(app)
    } else {
        match setup.mode {
            Mode::Gameplay(gameplay) => {
                if let GameplayMode::Actdev(_, _, _) = gameplay {
                    SandboxMode::async_new(
                        app,
                        gameplay,
                        jump_to_time_upon_startup(Duration::hours(8)),
                    )
                } else if let Some(t) = setup.start_time {
                    SandboxMode::async_new(app, gameplay, jump_to_time_upon_startup(t))
                } else {
                    SandboxMode::simple_new(app, gameplay)
                }
            }
            Mode::SomethingElse => {
                // Not attempting to keep the primary and secondary simulations synchronized at the
                // same time yet. Just handle this one startup case, so we can switch maps without
                // constantly flopping day/night mode.
                if let Some(ref mut secondary) = app.secondary {
                    secondary.sim.timed_step(
                        &secondary.map,
                        Duration::hours(6),
                        &mut None,
                        &mut Timer::throwaway(),
                    );
                }

                // We got here by just passing --dev and a map as flags; we're just looking at an
                // empty map. Start in the daytime.
                SandboxMode::async_new(
                    app,
                    GameplayMode::Freeform(app.primary.map.get_name().clone()),
                    jump_to_time_upon_startup(Duration::hours(6)),
                )
            }
            Mode::TutorialIntro => sandbox::gameplay::Tutorial::start(ctx, app),
            Mode::Challenges => challenges::ChallengesPicker::new_state(ctx, app),
            Mode::Sandbox => SandboxMode::simple_new(
                app,
                GameplayMode::PlayScenario(
                    app.primary.map.get_name().clone(),
                    pregame::default_scenario_for_map(app.primary.map.get_name()),
                    Vec::new(),
                ),
            ),
            Mode::Proposals => pregame::proposals::Proposals::new_state(ctx, None),
            Mode::Ungap => {
                let layers = ungap::Layers::new(ctx, app);
                ungap::ExploreMap::new_state(ctx, app, layers)
            }
            Mode::Ltn => ltn::BrowseNeighborhoods::new_state(ctx, app),
            Mode::Devtools => devtools::DevToolsMode::new_state(ctx, app),
            Mode::LoadKML(path) => crate::devtools::kml::ViewKML::new_state(ctx, app, Some(path)),
        }
    };
    vec![TitleScreen::new_state(ctx, app), state]
}

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = "run")]
pub fn run_wasm(root_dom_id: String, assets_base_url: String, assets_are_gzipped: bool) {
    let settings = Settings::new("A/B Street")
        .root_dom_element_id(root_dom_id)
        .assets_base_url(assets_base_url)
        .assets_are_gzipped(assets_are_gzipped);

    run(settings);
}