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
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
use crate::app::App;
use crate::common::{tool_panel, Minimap, Warping};
use crate::cutscene::CutsceneBuilder;
use crate::edit::EditMode;
use crate::game::{PopupMsg, State, Transition};
use crate::helpers::ID;
use crate::sandbox::gameplay::{GameplayMode, GameplayState};
use crate::sandbox::{
    maybe_exit_sandbox, spawn_agents_around, AgentMeter, SandboxControls, SandboxMode,
    SpeedControls, TimePanel,
};
use abstutil::Timer;
use ezgui::{
    hotkey, hotkeys, lctrl, Btn, Color, Composite, EventCtx, GfxCtx, HorizontalAlignment, Key,
    Line, Outcome, RewriteColor, ScreenPt, Text, TextExt, VerticalAlignment, Widget,
};
use geom::{ArrowCap, Distance, Duration, PolyLine, Pt2D, Time};
use map_model::raw::OriginalRoad;
use map_model::{osm, BuildingID, Map, OriginalLane, Position};
use sim::{
    AgentID, Analytics, BorderSpawnOverTime, CarID, DrivingGoal, IndividTrip, OriginDestination,
    PersonID, PersonSpec, Scenario, ScenarioGenerator, SpawnOverTime, SpawnTrip, VehicleType,
};
use std::collections::BTreeSet;

const ESCORT: CarID = CarID(0, VehicleType::Car);
const CAR_BIKE_CONTENTION_GOAL: Duration = Duration::const_seconds(60.0);

pub struct Tutorial {
    top_center: Composite,
    last_finished_task: Task,

    msg_panel: Option<Composite>,
    warped: bool,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct TutorialPointer {
    pub stage: usize,
    // Index into messages. messages.len() means the actual task.
    pub part: usize,
}

impl TutorialPointer {
    pub fn new(stage: usize, part: usize) -> TutorialPointer {
        TutorialPointer { stage, part }
    }
}

impl Tutorial {
    pub fn start(ctx: &mut EventCtx, app: &mut App) -> Transition {
        Transition::Multi(vec![
            Transition::Push(SandboxMode::new(
                ctx,
                app,
                GameplayMode::Tutorial(
                    app.session
                        .tutorial
                        .as_ref()
                        .map(|tut| tut.current)
                        .unwrap_or(TutorialPointer::new(0, 0)),
                ),
            )),
            Transition::Push(intro_story(ctx, app)),
        ])
    }

    pub fn new(
        ctx: &mut EventCtx,
        app: &mut App,
        current: TutorialPointer,
    ) -> Box<dyn GameplayState> {
        if app.session.tutorial.is_none() {
            app.session.tutorial = Some(TutorialState::new(ctx, app));
        }
        let mut tut = app.session.tutorial.take().unwrap();
        tut.current = current;
        let state = tut.make_state(ctx, app);
        app.session.tutorial = Some(tut);
        state
    }

    fn inner_event(
        &mut self,
        ctx: &mut EventCtx,
        app: &mut App,
        controls: &mut SandboxControls,
        tut: &mut TutorialState,
    ) -> Option<Transition> {
        // First of all, might need to initiate warping
        if !self.warped {
            if let Some((ref id, zoom)) = tut.stage().warp_to {
                self.warped = true;
                return Some(Transition::Push(Warping::new(
                    ctx,
                    id.canonical_point(&app.primary).unwrap(),
                    Some(zoom),
                    None,
                    &mut app.primary,
                )));
            }
        }

        match self.top_center.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "Quit" => {
                    return Some(maybe_exit_sandbox(ctx));
                }
                "previous tutorial" => {
                    tut.current = TutorialPointer::new(tut.current.stage - 1, 0);
                    return Some(transition(ctx, app, tut));
                }
                "next tutorial" => {
                    tut.current = TutorialPointer::new(tut.current.stage + 1, 0);
                    return Some(transition(ctx, app, tut));
                }
                "instructions" => {
                    tut.current = TutorialPointer::new(tut.current.stage, 0);
                    return Some(transition(ctx, app, tut));
                }
                "edit map" => {
                    // TODO Ideally this would be an inactive button in message states
                    if self.msg_panel.is_none() {
                        let mode = GameplayMode::Tutorial(tut.current);
                        return Some(Transition::Push(EditMode::new(ctx, app, mode)));
                    }
                }
                _ => unreachable!(),
            },
            _ => {}
        }

        if let Some(ref mut msg) = self.msg_panel {
            match msg.event(ctx) {
                Outcome::Clicked(x) => match x.as_ref() {
                    "previous message" => {
                        tut.prev();
                        return Some(transition(ctx, app, tut));
                    }
                    "next message" | "Try it" => {
                        tut.next();
                        return Some(transition(ctx, app, tut));
                    }
                    _ => unreachable!(),
                },
                _ => {
                    // Don't allow other interactions
                    return Some(Transition::Keep);
                }
            }
        }

        // Interaction things
        if tut.interaction() == Task::Camera {
            if app.primary.current_selection == Some(ID::Building(tut.fire_station))
                && app.per_obj.left_click(ctx, "put out the... fire?")
            {
                tut.next();
                return Some(transition(ctx, app, tut));
            }
        } else if tut.interaction() == Task::InspectObjects {
            // TODO Have to wiggle the mouse or something after opening the panel, because of the
            // order in SandboxMode.
            match controls.common.as_ref().unwrap().info_panel_open(app) {
                Some(ID::Lane(l)) => {
                    if app.primary.map.get_l(l).is_biking() && !tut.inspected_bike_lane {
                        tut.inspected_bike_lane = true;
                        self.top_center = tut.make_top_center(ctx, false);
                    }
                }
                Some(ID::Building(_)) => {
                    if !tut.inspected_building {
                        tut.inspected_building = true;
                        self.top_center = tut.make_top_center(ctx, false);
                    }
                }
                Some(ID::Intersection(i)) => {
                    let i = app.primary.map.get_i(i);
                    if i.is_stop_sign() && !tut.inspected_stop_sign {
                        tut.inspected_stop_sign = true;
                        self.top_center = tut.make_top_center(ctx, false);
                    }
                    if i.is_border() && !tut.inspected_border {
                        tut.inspected_border = true;
                        self.top_center = tut.make_top_center(ctx, false);
                    }
                }
                _ => {}
            }
            if tut.inspected_bike_lane
                && tut.inspected_building
                && tut.inspected_stop_sign
                && tut.inspected_border
            {
                tut.next();
                return Some(transition(ctx, app, tut));
            }
        } else if tut.interaction() == Task::TimeControls {
            if app.primary.sim.time() >= Time::START_OF_DAY + Duration::hours(17) {
                tut.next();
                return Some(transition(ctx, app, tut));
            }
        } else if tut.interaction() == Task::PauseResume {
            let is_paused = controls.speed.as_ref().unwrap().is_paused();
            if tut.was_paused && !is_paused {
                tut.was_paused = false;
            }
            if !tut.was_paused && is_paused {
                tut.num_pauses += 1;
                tut.was_paused = true;
                self.top_center = tut.make_top_center(ctx, false);
            }
            if tut.num_pauses == 3 {
                tut.next();
                return Some(transition(ctx, app, tut));
            }
        } else if tut.interaction() == Task::Escort {
            let following_car =
                controls.common.as_ref().unwrap().info_panel_open(app) == Some(ID::Car(ESCORT));
            let is_parked = app
                .primary
                .sim
                .agent_to_trip(AgentID::Car(ESCORT))
                .is_none();
            if !tut.car_parked && is_parked && tut.following_car {
                tut.car_parked = true;
                self.top_center = tut.make_top_center(ctx, false);
            }

            if following_car && !tut.following_car {
                // TODO There's a delay of one event before the checklist updates, because the
                // info panel opening happens at the end of the event. Not a big deal.
                tut.following_car = true;
                self.top_center = tut.make_top_center(ctx, false);
            }

            if tut.prank_done {
                tut.next();
                return Some(transition(ctx, app, tut));
            }
        } else if tut.interaction() == Task::LowParking {
            if tut.parking_found {
                tut.next();
                return Some(transition(ctx, app, tut));
            }
        } else if tut.interaction() == Task::WatchBikes {
            if app.primary.sim.time() >= Time::START_OF_DAY + Duration::minutes(3) {
                tut.next();
                return Some(transition(ctx, app, tut));
            }
        } else if tut.interaction() == Task::FixBikes {
            if app.primary.sim.is_done() {
                let mut before = Duration::ZERO;
                let mut after = Duration::ZERO;
                for (b, a, _) in app
                    .primary
                    .sim
                    .get_analytics()
                    .both_finished_trips(app.primary.sim.get_end_of_day(), app.prebaked())
                {
                    before = before.max(b);
                    after = after.max(a);
                }
                if !tut.score_delivered {
                    tut.score_delivered = true;
                    if before == after {
                        return Some(Transition::Push(PopupMsg::new(
                            ctx,
                            "All trips completed",
                            vec![
                                "Your changes didn't affect anything!",
                                "Try editing the map to create some bike lanes.",
                            ],
                        )));
                    }
                    if after > before {
                        return Some(Transition::Push(PopupMsg::new(
                            ctx,
                            "All trips completed",
                            vec![
                                "Your changes made things worse!".to_string(),
                                format!(
                                    "All trips originally finished in {}, but now they took {}",
                                    before, after
                                ),
                                "".to_string(),
                                "Try again!".to_string(),
                            ],
                        )));
                    }
                    if before - after < CAR_BIKE_CONTENTION_GOAL {
                        return Some(Transition::Push(PopupMsg::new(
                            ctx,
                            "All trips completed",
                            vec![
                                "Nice, you helped things a bit!".to_string(),
                                format!(
                                    "All trips originally took {}, but now they took {}",
                                    before, after
                                ),
                                "".to_string(),
                                "See if you can do a little better though.".to_string(),
                            ],
                        )));
                    }
                    return Some(Transition::Push(PopupMsg::new(
                        ctx,
                        "All trips completed",
                        vec![format!(
                            "Awesome! All trips originally took {}, but now they only took {}",
                            before, after
                        )],
                    )));
                }
                if before - after >= CAR_BIKE_CONTENTION_GOAL {
                    tut.next();
                }
                return Some(transition(ctx, app, tut));
            }
        } else if tut.interaction() == Task::Done {
            // If the player chooses to stay here, at least go back to the message panel.
            tut.prev();
            return Some(maybe_exit_sandbox(ctx));
        }

        None
    }
}

impl GameplayState for Tutorial {
    fn event(
        &mut self,
        ctx: &mut EventCtx,
        app: &mut App,
        controls: &mut SandboxControls,
    ) -> Option<Transition> {
        // Dance around borrow-checker issues
        let mut tut = app.session.tutorial.take().unwrap();

        // The arrows get screwy when window size changes.
        let window_dims = (ctx.canvas.window_width, ctx.canvas.window_height);
        if window_dims != tut.window_dims {
            tut.stages = TutorialState::new(ctx, app).stages;
            tut.window_dims = window_dims;
        }

        let result = self.inner_event(ctx, app, controls, &mut tut);
        app.session.tutorial = Some(tut);
        result
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        let tut = app.session.tutorial.as_ref().unwrap();

        if self.msg_panel.is_some() {
            State::grey_out_map(g, app);
        }

        self.top_center.draw(g);

        if let Some(ref msg) = self.msg_panel {
            // Arrows underneath the message panel, but on top of other panels
            if let Some((_, _, Some(fxn))) = tut.lines() {
                let pt = (fxn)(g, app);
                g.fork_screenspace();
                if let Ok(pl) = PolyLine::new(vec![
                    self.msg_panel
                        .as_ref()
                        .unwrap()
                        .center_of("next message")
                        .to_pt(),
                    pt,
                ]) {
                    g.draw_polygon(
                        Color::RED,
                        pl.make_arrow(Distance::meters(20.0), ArrowCap::Triangle),
                    );
                }
                g.unfork();
            }

            msg.draw(g);
        }

        // Special things
        if tut.interaction() == Task::Camera {
            g.draw_polygon(
                Color::hex("#e25822"),
                app.primary.map.get_b(tut.fire_station).polygon.clone(),
            );
        }
    }

    fn can_move_canvas(&self) -> bool {
        self.msg_panel.is_none()
    }
    fn can_examine_objects(&self) -> bool {
        self.last_finished_task >= Task::WatchBikes
    }
    fn has_common(&self) -> bool {
        self.last_finished_task >= Task::Camera
    }
    fn has_tool_panel(&self) -> bool {
        true
    }
    fn has_time_panel(&self) -> bool {
        self.last_finished_task >= Task::InspectObjects
    }
    fn has_speed(&self) -> bool {
        self.last_finished_task >= Task::InspectObjects
    }
    fn has_agent_meter(&self) -> bool {
        self.last_finished_task >= Task::PauseResume
    }
    fn has_minimap(&self) -> bool {
        self.last_finished_task >= Task::Escort
    }
}

#[derive(PartialEq, PartialOrd, Clone, Copy)]
enum Task {
    Nil,
    Camera,
    InspectObjects,
    TimeControls,
    PauseResume,
    Escort,
    LowParking,
    WatchBikes,
    FixBikes,
    Done,
}

impl Task {
    fn top_txt(self, state: &TutorialState) -> Text {
        let simple = match self {
            Task::Nil => unreachable!(),
            Task::Camera => "Put out the fire at the fire station",
            Task::InspectObjects => {
                let mut txt = Text::from(Line("Find one of each:"));
                for (name, done) in vec![
                    ("bike lane", state.inspected_bike_lane),
                    ("building", state.inspected_building),
                    ("intersection with stop sign", state.inspected_stop_sign),
                    ("intersection on the map border", state.inspected_border),
                ] {
                    if done {
                        txt.add(Line(format!("[X] {}", name)).fg(Color::GREEN));
                    } else {
                        txt.add(Line(format!("[ ] {}", name)));
                    }
                }
                return txt;
            }
            Task::TimeControls => "Wait until after 5pm",
            Task::PauseResume => {
                let mut txt = Text::from(Line("[ ] Pause/resume "));
                txt.append(Line(format!("{} times", 3 - state.num_pauses)).fg(Color::GREEN));
                return txt;
            }
            Task::Escort => {
                // Inspect the target car, wait for them to park, draw WASH ME on the window
                let mut txt = Text::new();
                if state.following_car {
                    txt.add(Line("[X] follow the target car").fg(Color::GREEN));
                } else {
                    txt.add(Line("[ ] follow the target car"));
                }
                if state.car_parked {
                    txt.add(Line("[X] wait for them to park").fg(Color::GREEN));
                } else {
                    txt.add(Line("[ ] wait for them to park"));
                }
                if state.prank_done {
                    txt.add(Line("[X] click car and press c to draw WASH ME").fg(Color::GREEN));
                } else {
                    txt.add(Line("[ ] click car and press "));
                    // TODO ctx.style().hotkey_color
                    txt.append(Line(Key::C.describe()).fg(Color::GREEN));
                    txt.append(Line(" to draw WASH ME"));
                }
                return txt;
            }
            Task::LowParking => {
                let mut txt = Text::from(Line(
                    "1) Find a road with almost no parking spots available",
                ));
                txt.add(Line("2) Click it and press "));
                // TODO ctx.style().hotkey_color
                txt.append(Line(Key::C.describe()).fg(Color::GREEN));
                txt.append(Line(" to check the occupancy"));
                return txt;
            }
            Task::WatchBikes => "Watch for 3 minutes",
            Task::FixBikes => {
                return Text::from(Line(format!(
                    "[ ] Complete all trips {} faster",
                    CAR_BIKE_CONTENTION_GOAL
                )));
            }
            Task::Done => "Tutorial complete!",
        };
        Text::from(Line(simple))
    }

    fn label(self) -> &'static str {
        match self {
            Task::Nil => unreachable!(),
            Task::Camera => "Moving the drone",
            Task::InspectObjects => "Interacting with objects",
            Task::TimeControls => "Passing the time",
            Task::PauseResume => "Pausing/resuming",
            Task::Escort => "Following people",
            Task::LowParking => "Exploring map layers",
            Task::WatchBikes => "Observing a problem",
            Task::FixBikes => "Editing lanes",
            Task::Done => "Tutorial complete!",
        }
    }
}

struct Stage {
    messages: Vec<(
        Vec<String>,
        HorizontalAlignment,
        Option<Box<dyn Fn(&GfxCtx, &App) -> Pt2D>>,
    )>,
    task: Task,
    warp_to: Option<(ID, f64)>,
    spawn: Option<Box<dyn Fn(&mut App)>>,
}

fn arrow(pt: ScreenPt) -> Option<Box<dyn Fn(&GfxCtx, &App) -> Pt2D>> {
    Some(Box::new(move |_, _| pt.to_pt()))
}

impl Stage {
    fn new(task: Task) -> Stage {
        Stage {
            messages: Vec::new(),
            task,
            warp_to: None,
            spawn: None,
        }
    }

    fn msg<I: Into<String>>(
        mut self,
        lines: Vec<I>,
        point_to: Option<Box<dyn Fn(&GfxCtx, &App) -> Pt2D>>,
    ) -> Stage {
        self.messages.push((
            lines.into_iter().map(|l| l.into()).collect(),
            HorizontalAlignment::Center,
            point_to,
        ));
        self
    }
    fn left_aligned_msg<I: Into<String>>(
        mut self,
        lines: Vec<I>,
        point_to: Option<Box<dyn Fn(&GfxCtx, &App) -> Pt2D>>,
    ) -> Stage {
        self.messages.push((
            lines.into_iter().map(|l| l.into()).collect(),
            HorizontalAlignment::Left,
            point_to,
        ));
        self
    }

    fn warp_to(mut self, id: ID, zoom: Option<f64>) -> Stage {
        assert!(self.warp_to.is_none());
        self.warp_to = Some((id, zoom.unwrap_or(4.0)));
        self
    }

    fn spawn(mut self, cb: Box<dyn Fn(&mut App)>) -> Stage {
        assert!(self.spawn.is_none());
        self.spawn = Some(cb);
        self
    }

    fn spawn_scenario(self, generator: ScenarioGenerator) -> Stage {
        self.spawn(Box::new(move |app| {
            let mut timer = Timer::new("spawn scenario with prebaked results");
            let scenario = generator.generate(
                &app.primary.map,
                &mut app.primary.current_flags.sim_flags.make_rng(),
                &mut timer,
            );
            scenario.instantiate(
                &mut app.primary.sim,
                &app.primary.map,
                &mut app.primary.current_flags.sim_flags.make_rng(),
                &mut timer,
            );

            let prebaked: Analytics = abstutil::read_binary(
                abstutil::path_prebaked_results(&scenario.map_name, &scenario.scenario_name),
                &mut timer,
            );
            app.set_prebaked(Some((
                scenario.map_name.clone(),
                scenario.scenario_name.clone(),
                prebaked,
            )));
        }))
    }
}

pub struct TutorialState {
    stages: Vec<Stage>,
    pub current: TutorialPointer,

    window_dims: (f64, f64),

    // Goofy state for just some stages.
    inspected_bike_lane: bool,
    inspected_building: bool,
    inspected_stop_sign: bool,
    inspected_border: bool,

    was_paused: bool,
    num_pauses: usize,

    following_car: bool,
    car_parked: bool,
    prank_done: bool,

    parking_found: bool,

    score_delivered: bool,

    fire_station: BuildingID,
}

fn make_bike_lane_scenario(map: &Map) -> ScenarioGenerator {
    let mut s = ScenarioGenerator::empty("car vs bike contention");
    s.border_spawn_over_time.push(BorderSpawnOverTime {
        num_peds: 0,
        num_cars: 10,
        num_bikes: 10,
        percent_use_transit: 0.0,
        start_time: Time::START_OF_DAY,
        stop_time: Time::START_OF_DAY + Duration::seconds(10.0),
        start_from_border: map
            .find_r_by_osm_id(OriginalRoad::new(263665925, (2499826475, 53096959)))
            .unwrap()
            .backwards(),
        goal: OriginDestination::GotoBldg(map.find_b_by_osm_id(bldg(217699501)).unwrap()),
    });
    s
}

fn transition(ctx: &mut EventCtx, app: &mut App, tut: &mut TutorialState) -> Transition {
    tut.reset_state();
    let mode = GameplayMode::Tutorial(tut.current);
    Transition::Replace(SandboxMode::new(ctx, app, mode))
}

impl TutorialState {
    // These're mutex to each state, but still important to reset. Otherwise if you go back to a
    // previous interaction stage, it'll just be automatically marked done.
    fn reset_state(&mut self) {
        self.inspected_bike_lane = false;
        self.inspected_building = false;
        self.inspected_stop_sign = false;
        self.inspected_border = false;
        self.was_paused = true;
        self.num_pauses = 0;
        self.score_delivered = false;
        self.following_car = false;
        self.car_parked = false;
        self.prank_done = false;
        self.parking_found = false;
    }

    fn stage(&self) -> &Stage {
        &self.stages[self.current.stage]
    }

    fn interaction(&self) -> Task {
        let stage = self.stage();
        if self.current.part == stage.messages.len() {
            stage.task
        } else {
            Task::Nil
        }
    }
    fn lines(
        &self,
    ) -> Option<&(
        Vec<String>,
        HorizontalAlignment,
        Option<Box<dyn Fn(&GfxCtx, &App) -> Pt2D>>,
    )> {
        let stage = self.stage();
        if self.current.part == stage.messages.len() {
            None
        } else {
            Some(&stage.messages[self.current.part])
        }
    }

    fn next(&mut self) {
        self.current.part += 1;
        if self.current.part == self.stage().messages.len() + 1 {
            self.current = TutorialPointer::new(self.current.stage + 1, 0);
        }
    }
    fn prev(&mut self) {
        if self.current.part == 0 {
            self.current = TutorialPointer::new(
                self.current.stage - 1,
                self.stages[self.current.stage - 1].messages.len(),
            );
        } else {
            self.current.part -= 1;
        }
    }

    fn make_top_center(&self, ctx: &mut EventCtx, edit_map: bool) -> Composite {
        let mut col = vec![Widget::row(vec![
            Line("Tutorial").small_heading().draw(ctx),
            Widget::vert_separator(ctx, 50.0),
            if self.current.stage == 0 {
                Btn::text_fg("<").inactive(ctx)
            } else {
                Btn::text_fg("<").build(ctx, "previous tutorial", None)
            },
            {
                let mut txt = Text::from(Line(format!("Task {}", self.current.stage + 1)));
                // TODO Smaller font and use alpha for the "/9" part
                txt.append(Line(format!("/{}", self.stages.len())).fg(Color::grey(0.7)));
                txt.draw(ctx)
            },
            if self.current.stage == self.stages.len() - 1 {
                Btn::text_fg(">").inactive(ctx)
            } else {
                Btn::text_fg(">").build(ctx, "next tutorial", None)
            },
            Btn::text_fg("Quit").build_def(ctx, None),
        ])
        .centered()];
        {
            let task = self.interaction();
            if task != Task::Nil {
                col.push(Widget::row(vec![
                    Text::from(
                        Line(format!(
                            "Task {}: {}",
                            self.current.stage + 1,
                            self.stage().task.label()
                        ))
                        .small_heading(),
                    )
                    .draw(ctx),
                    // TODO also text saying "instructions"... can we layout two things easily to
                    // make a button?
                    Btn::svg_def("system/assets/tools/info.svg")
                        .build(ctx, "instructions", None)
                        .centered_vert()
                        .align_right(),
                ]));
                col.push(task.top_txt(self).draw(ctx));
            }
        }
        if edit_map {
            col.push(Btn::svg_def("system/assets/tools/edit_map.svg").build(
                ctx,
                "edit map",
                lctrl(Key::E),
            ));
        }

        Composite::new(Widget::col(col))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx)
    }

    fn make_state(&self, ctx: &mut EventCtx, app: &mut App) -> Box<dyn GameplayState> {
        if self.interaction() == Task::Nil {
            app.primary.current_selection = None;
        }

        if let Some(ref cb) = self.stage().spawn {
            (cb)(app);
            app.primary
                .sim
                .tiny_step(&app.primary.map, &mut app.primary.sim_cb);
        }

        let last_finished_task = if self.current.stage == 0 {
            Task::Nil
        } else {
            self.stages[self.current.stage - 1].task
        };

        Box::new(Tutorial {
            top_center: self.make_top_center(ctx, last_finished_task >= Task::WatchBikes),
            last_finished_task,

            msg_panel: if let Some((ref lines, horiz_align, _)) = self.lines() {
                let mut col = vec![{
                    let mut txt = Text::new();
                    txt.add(Line(self.stage().task.label()).small_heading());
                    txt.add(Line(""));

                    for l in lines {
                        txt.add(Line(l));
                    }
                    txt.wrap_to_pct(ctx, 30).draw(ctx)
                }];
                let mut controls = vec![Widget::row(vec![
                    if self.current.part > 0 {
                        Btn::svg(
                            "system/assets/tools/prev.svg",
                            RewriteColor::Change(Color::WHITE, app.cs.hovering),
                        )
                        .build(
                            ctx,
                            "previous message",
                            hotkey(Key::LeftArrow),
                        )
                    } else {
                        Widget::draw_svg_transform(
                            ctx,
                            "system/assets/tools/prev.svg",
                            RewriteColor::ChangeAll(Color::WHITE.alpha(0.5)),
                        )
                    },
                    format!("{}/{}", self.current.part + 1, self.stage().messages.len())
                        .draw_text(ctx)
                        .centered_vert(),
                    if self.current.part == self.stage().messages.len() - 1 {
                        Widget::draw_svg_transform(
                            ctx,
                            "system/assets/tools/next.svg",
                            RewriteColor::ChangeAll(Color::WHITE.alpha(0.5)),
                        )
                        .named("next message")
                    } else {
                        Btn::svg(
                            "system/assets/tools/next.svg",
                            RewriteColor::Change(Color::WHITE, app.cs.hovering),
                        )
                        .build(
                            ctx,
                            "next message",
                            hotkeys(vec![Key::RightArrow, Key::Space, Key::Enter]),
                        )
                    },
                ])];
                if self.current.part == self.stage().messages.len() - 1 {
                    controls.push(
                        Btn::text_bg2("Try it")
                            .build_def(ctx, hotkeys(vec![Key::RightArrow, Key::Space, Key::Enter])),
                    );
                }
                col.push(Widget::col(controls).align_bottom());

                Some(
                    Composite::new(Widget::col(col).outline(5.0, Color::WHITE))
                        .exact_size_percent(40, 40)
                        .aligned(*horiz_align, VerticalAlignment::Center)
                        .build(ctx),
                )
            } else {
                None
            },
            warped: false,
        })
    }

    fn new(ctx: &mut EventCtx, app: &App) -> TutorialState {
        let mut state = TutorialState {
            stages: Vec::new(),
            current: TutorialPointer::new(0, 0),
            window_dims: (ctx.canvas.window_width, ctx.canvas.window_height),

            inspected_bike_lane: false,
            inspected_building: false,
            inspected_stop_sign: false,
            inspected_border: false,
            was_paused: true,
            num_pauses: 0,
            following_car: false,
            car_parked: false,
            prank_done: false,
            parking_found: false,
            score_delivered: false,

            fire_station: app.primary.map.find_b_by_osm_id(bldg(731238736)).unwrap(),
        };

        let tool_panel = tool_panel(ctx);
        let time = TimePanel::new(ctx, app);
        let speed = SpeedControls::new(ctx, app);
        let agent_meter = AgentMeter::new(ctx, app);
        // The minimap is hidden at low zoom levels
        let orig_zoom = ctx.canvas.cam_zoom;
        ctx.canvas.cam_zoom = 100.0;
        let minimap = Minimap::new(ctx, app);
        ctx.canvas.cam_zoom = orig_zoom;

        let map = &app.primary.map;

        state.stages.push(
            Stage::new(Task::Camera)
                .warp_to(
                    ID::Intersection(map.find_i_by_osm_id(osm::NodeID(53096945)).unwrap()),
                    None,
                )
                .msg(
                    vec![
                        "Let's start by piloting your fancy new drone.",
                        "",
                        "- Click and drag to pan around the map",
                        "- Use your scroll wheel or touchpad to zoom in and out.",
                    ],
                    None,
                )
                .msg(
                    vec!["If the controls feel wrong, try adjusting the settings."],
                    arrow(tool_panel.center_of("settings")),
                )
                .msg(
                    vec![
                        "Let's try the drone ou--",
                        "",
                        "WHOA, THERE'S A FIRE STATION ON FIRE!",
                        "GO CLICK ON IT, QUICK!",
                    ],
                    None,
                )
                .msg(
                    vec![
                        "Hint:",
                        "- Look around for an unusually red building",
                        "- You have to zoom in to interact with anything on the map.",
                    ],
                    None,
                ),
        );

        state.stages.push(
            Stage::new(Task::InspectObjects)
                .msg(
                    vec![
                        "What, no fire? Er, sorry about that. Just a little joke we like to play \
                         on the new recruits.",
                    ],
                    None,
                )
                .msg(
                    vec![
                        "Now, let's learn how to inspect and interact with objects in the map.",
                        "",
                        "- Click on something.",
                        "- Hint: You have to zoom in before you can select anything.",
                    ],
                    None,
                ),
        );

        state.stages.push(
            Stage::new(Task::TimeControls)
                .warp_to(
                    ID::Intersection(map.find_i_by_osm_id(osm::NodeID(53096945)).unwrap()),
                    Some(6.5),
                )
                .msg(
                    vec![
                        "Inspection complete!",
                        "",
                        "You'll work day and night, watching traffic patterns unfold.",
                    ],
                    arrow(time.composite.center_of_panel()),
                )
                .msg(
                    vec!["You can pause or resume time"],
                    arrow(speed.composite.center_of("pause")),
                )
                .msg(
                    vec![
                        "Speed things up",
                        "",
                        "(The keyboard shortcuts are very helpful here!)",
                    ],
                    arrow(speed.composite.center_of("30x speed")),
                )
                .msg(
                    vec!["Advance time by certain amounts"],
                    arrow(speed.composite.center_of("step forwards")),
                )
                .msg(
                    vec!["And jump to the beginning of the day"],
                    arrow(speed.composite.center_of("reset to midnight")),
                )
                .msg(
                    vec!["Let's try these controls out. Wait until 5pm or later."],
                    None,
                ),
        );

        state.stages.push(
            Stage::new(Task::PauseResume)
                .msg(
                    vec!["Whew, that took a while! (Hopefully not though...)"],
                    None,
                )
                .msg(
                    vec![
                        "You might've figured it out already,",
                        "But you'll be pausing/resuming time VERY frequently",
                    ],
                    arrow(speed.composite.center_of("pause")),
                )
                .msg(
                    vec!["Just reassure me and pause/resume time a few times, alright?"],
                    None,
                ),
        );

        state.stages.push(
            Stage::new(Task::Escort)
                // Don't center on where the agents are, be a little offset
                .warp_to(
                    ID::Building(map.find_b_by_osm_id(bldg(217699780)).unwrap()),
                    Some(10.0),
                )
                .spawn(Box::new(move |app| {
                    // Seed a specific target car, and fill up the target building's private
                    // parking to force the target to park on-street.
                    let map = &app.primary.map;
                    let goal_bldg = map.find_b_by_osm_id(bldg(217701875)).unwrap();
                    let start_lane = OriginalLane {
                        parent: OriginalRoad::new(158782224, (1709145066, 53128052)),
                        num_fwd: 3,
                        num_back: 3,
                        fwd: false,
                        idx: 0,
                    }
                    .from_permanent(map)
                    .unwrap();
                    let lane_near_bldg = OriginalLane {
                        parent: OriginalRoad::new(6484869, (53163501, 53069236)),
                        num_fwd: 3,
                        num_back: 3,
                        fwd: true,
                        idx: 0,
                    }
                    .from_permanent(map)
                    .unwrap();

                    let mut scenario = Scenario::empty(map, "prank");
                    scenario.people.push(PersonSpec {
                        id: PersonID(0),
                        orig_id: None,
                        trips: vec![IndividTrip::new(
                            Time::START_OF_DAY,
                            SpawnTrip::VehicleAppearing {
                                start: Position::new(
                                    start_lane,
                                    map.get_l(start_lane).length() * 0.8,
                                ),
                                goal: DrivingGoal::ParkNear(goal_bldg),
                                is_bike: false,
                            },
                        )],
                    });
                    // Will definitely get there first
                    for i in 0..map.get_b(goal_bldg).num_parking_spots() {
                        scenario.people.push(PersonSpec {
                            id: PersonID(i + 1),
                            orig_id: None,
                            trips: vec![IndividTrip::new(
                                Time::START_OF_DAY,
                                SpawnTrip::VehicleAppearing {
                                    start: Position::new(
                                        lane_near_bldg,
                                        map.get_l(lane_near_bldg).length() / 2.0,
                                    ),
                                    goal: DrivingGoal::ParkNear(goal_bldg),
                                    is_bike: false,
                                },
                            )],
                        });
                    }
                    let mut rng = app.primary.current_flags.sim_flags.make_rng();
                    scenario.instantiate(
                        &mut app.primary.sim,
                        map,
                        &mut rng,
                        &mut Timer::new("spawn trip"),
                    );
                    app.primary.sim.tiny_step(map, &mut app.primary.sim_cb);

                    // And add some noise
                    spawn_agents_around(
                        app.primary
                            .map
                            .find_i_by_osm_id(osm::NodeID(1709145066))
                            .unwrap(),
                        app,
                    );
                }))
                .msg(
                    vec!["Alright alright, no need to wear out your spacebar."],
                    None,
                )
                .msg(
                    vec![
                        "Oh look, some people appeared!",
                        "We've got pedestrians, bikes, and cars moving around now.",
                    ],
                    None,
                )
                .msg(
                    vec!["You can see the number of them here."],
                    arrow(agent_meter.composite.center_of_panel()),
                )
                .left_aligned_msg(
                    vec![
                        "Why don't you follow this car to their destination,",
                        "see where they park, and then play a little... prank?",
                    ],
                    Some(Box::new(|g, app| {
                        g.canvas
                            .map_to_screen(
                                app.primary
                                    .sim
                                    .canonical_pt_for_agent(AgentID::Car(ESCORT), &app.primary.map)
                                    .unwrap(),
                            )
                            .to_pt()
                    })),
                )
                .msg(
                    vec![
                        "You don't have to manually chase them; just click to follow.",
                        "",
                        "(If you do lose track of them, just reset)",
                    ],
                    arrow(speed.composite.center_of("reset to midnight")),
                ),
        );

        state.stages.push(
            Stage::new(Task::LowParking)
                // TODO Actually, we ideally just want a bunch of parked cars, not all these trips
                .spawn(Box::new(|app| {
                    ScenarioGenerator {
                        scenario_name: "low parking".to_string(),
                        only_seed_buses: Some(BTreeSet::new()),
                        spawn_over_time: vec![SpawnOverTime {
                            num_agents: 1000,
                            start_time: Time::START_OF_DAY,
                            stop_time: Time::START_OF_DAY + Duration::hours(3),
                            goal: OriginDestination::Anywhere,
                            percent_driving: 1.0,
                            percent_biking: 0.0,
                            percent_use_transit: 0.0,
                        }],
                        border_spawn_over_time: Vec::new(),
                    }
                    .generate(
                        &app.primary.map,
                        &mut app.primary.current_flags.sim_flags.make_rng(),
                        &mut Timer::throwaway(),
                    )
                    .instantiate(
                        &mut app.primary.sim,
                        &app.primary.map,
                        &mut app.primary.current_flags.sim_flags.make_rng(),
                        &mut Timer::throwaway(),
                    )
                }))
                .msg(
                    vec![
                        "What an immature prank. You should re-evaluate your life decisions.",
                        "",
                        "The map is quite large, so to help you orient, the minimap shows you an \
                         overview of all activity. You can click and drag it just like the normal \
                         map.",
                    ],
                    arrow(minimap.composite.center_of("minimap")),
                )
                .msg(
                    vec![
                        "You can apply different layers to the map, to find things like:",
                        "",
                        "- roads with high traffic",
                        "- bus stops",
                        "- how much parking is filled up",
                    ],
                    arrow(minimap.composite.center_of("change layers")),
                )
                .msg(
                    vec![
                        "Let's try these out.",
                        "There are lots of cars parked everywhere. Can you find a road that's \
                         almost out of parking spots?",
                    ],
                    None,
                ),
        );

        let bike_lane_scenario = make_bike_lane_scenario(map);
        let bike_lane_focus_pt = map.find_b_by_osm_id(bldg(217699496)).unwrap();

        state.stages.push(
            Stage::new(Task::WatchBikes)
                .warp_to(ID::Building(bike_lane_focus_pt), None)
                .spawn_scenario(bike_lane_scenario.clone())
                .msg(
                    vec![
                        "Well done!",
                        "",
                        "Something's about to happen over here. Follow along and figure out what \
                         the problem is, at whatever speed you'd like.",
                    ],
                    None,
                ),
        );

        let top_center = state.make_top_center(ctx, true);
        state.stages.push(
            Stage::new(Task::FixBikes)
                .spawn_scenario(bike_lane_scenario)
                .warp_to(ID::Building(bike_lane_focus_pt), None)
                .msg(
                    vec![
                        "Looks like lots of cars and bikes trying to go to a house by the \
                         playfield.",
                        "",
                        "When lots of cars and bikes share the same lane, cars are delayed \
                         (assuming there's no room to pass) and the cyclist probably feels unsafe \
                         too.",
                    ],
                    None,
                )
                .msg(
                    vec![
                        "Luckily, you have the power to modify lanes! What if you could transform \
                         the parking lanes that aren't being used much into bike lanes?",
                    ],
                    None,
                )
                .msg(
                    vec!["To edit lanes, click 'edit map' and then select a lane."],
                    arrow(top_center.center_of("edit map")),
                )
                .msg(
                    vec![
                        "When you finish making edits, time will jump to the beginning of the \
                         next day. You can't make most changes in the middle of the day.",
                        "",
                        "Seattleites are really boring; they follow the exact same schedule \
                         everyday. They're also stubborn, so even if you try to influence their \
                         decision whether to drive, walk, bike, or take a bus, they'll do the \
                         same thing. For now, you're just trying to make things better, assuming \
                         people stick to their routine.",
                    ],
                    None,
                )
                .msg(
                    // TODO Deliberately vague with the measurement.
                    vec![
                        format!(
                            "So adjust lanes and speed up the slowest trip by at least {}.",
                            CAR_BIKE_CONTENTION_GOAL
                        ),
                        "".to_string(),
                        "You can explore results as trips finish. When everyone's finished, \
                         you'll get your final score."
                            .to_string(),
                    ],
                    arrow(agent_meter.composite.center_of("more data")),
                ),
        );

        state.stages.push(Stage::new(Task::Done).msg(
            vec![
                "You're ready for the hard stuff now.",
                "",
                "- Try out some challenges",
                "- Explore larger parts of Seattle in the sandbox, and try out any ideas you've \
                 got.",
                "- Check out community proposals, and submit your own",
                "",
                "Go have the appropriate amount of fun!",
            ],
            None,
        ));

        state

        // TODO Multi-modal trips -- including parking. (Cars per bldg, ownership)
        // TODO Explain the finished trip data
        // The city is in total crisis. You've only got 10 days to do something before all hell
        // breaks loose and people start kayaking / ziplining / crab-walking / cartwheeling / to
        // work.
    }

    pub fn scenarios_to_prebake(map: &Map) -> Vec<ScenarioGenerator> {
        vec![make_bike_lane_scenario(map)]
    }
}

pub fn actions(app: &App, id: ID) -> Vec<(Key, String)> {
    match (app.session.tutorial.as_ref().unwrap().interaction(), id) {
        (Task::LowParking, ID::Lane(_)) => {
            vec![(Key::C, "check the parking occupancy".to_string())]
        }
        (Task::Escort, ID::Car(_)) => vec![(Key::C, "draw WASH ME".to_string())],
        _ => Vec::new(),
    }
}

pub fn execute(ctx: &mut EventCtx, app: &mut App, id: ID, action: String) -> Transition {
    let mut tut = app.session.tutorial.as_mut().unwrap();
    let response = match (id, action.as_ref()) {
        (ID::Car(c), "draw WASH ME") => {
            let is_parked = app
                .primary
                .sim
                .agent_to_trip(AgentID::Car(ESCORT))
                .is_none();
            if c == ESCORT {
                if is_parked {
                    tut.prank_done = true;
                    PopupMsg::new(
                        ctx,
                        "Prank in progress",
                        vec!["You quickly scribble on the window..."],
                    )
                } else {
                    PopupMsg::new(
                        ctx,
                        "Not yet!",
                        vec![
                            "You're going to run up to an occupied car and draw on their windows?",
                            "Sounds like we should be friends.",
                            "But, er, wait for the car to park. (You can speed up time!)",
                        ],
                    )
                }
            } else if c.1 == VehicleType::Bike {
                PopupMsg::new(
                    ctx,
                    "That's a bike",
                    vec![
                        "Achievement unlocked: You attempted to draw WASH ME on a cyclist.",
                        "This game is PG-13 or something, so I can't really describe what happens \
                         next.",
                        "But uh, don't try this at home.",
                    ],
                )
            } else {
                PopupMsg::new(
                    ctx,
                    "Wrong car",
                    vec![
                        "You're looking at the wrong car.",
                        "Use the 'reset to midnight' (key binding 'X') to start over, if you lost \
                         the car to follow.",
                    ],
                )
            }
        }
        (ID::Lane(l), "check the parking occupancy") => {
            let lane = app.primary.map.get_l(l);
            if lane.is_parking() {
                let percent = (app.primary.sim.get_free_onstreet_spots(l).len() as f64)
                    / (lane.number_parking_spots() as f64);
                if percent > 0.1 {
                    PopupMsg::new(
                        ctx,
                        "Not quite",
                        vec![
                            format!("This lane has {:.0}% spots free", percent * 100.0),
                            "Try using the 'parking occupancy' layer from the minimap controls"
                                .to_string(),
                        ],
                    )
                } else {
                    tut.parking_found = true;
                    PopupMsg::new(
                        ctx,
                        "Noice",
                        vec!["Yup, parallel parking would be tough here!"],
                    )
                }
            } else {
                PopupMsg::new(ctx, "Uhh..", vec!["That's not even a parking lane"])
            }
        }
        _ => unreachable!(),
    };
    Transition::Push(response)
}

fn intro_story(ctx: &mut EventCtx, app: &App) -> Box<dyn State> {
    CutsceneBuilder::new("Introduction")
        .boss(
            "Argh, the mayor's on my case again about the West Seattle bridge. This day couldn't \
             get any worse.",
        )
        .player("Er, hello? Boss? I'm --")
        .boss("Yet somehow it did.. You're the new recruit. Yeah, yeah. Come in.")
        .boss(
            "Due to budget cuts, we couldn't hire a real traffic engineer, so we just called some \
             know-it-all from Reddit who seems to think they can fix Seattle traffic.",
        )
        .player("Yes, hi, my name is --")
        .boss("We can't afford name-tags, didn't you hear, budget cuts? Your name doesn't matter.")
        .player("What about my Insta handle?")
        .boss("-glare-")
        .boss(
            "Look, you think fixing traffic is easy? Hah! You can't fix one intersection without \
             breaking ten more.",
        )
        .boss(
            "And everybody wants something different! Bike lanes here! More parking! Faster \
             buses! Cheaper housing! Less rain! Free this, subsidized that!",
        )
        .boss("Light rail and robot cars aren't here to save the day! Know what you'll be using?")
        .extra("drone", 1.0, "The traffic drone")
        .player("Is that... duct tape?")
        .boss(
            "Can't spit anymore cause of COVID and don't get me started on prayers. Well, off to \
             training for you!",
        )
        .build(
            ctx,
            app,
            Box::new(|ctx| {
                Text::from(Line("Use the tutorial to learn the basic controls.").fg(Color::BLACK))
                    .draw(ctx)
            }),
        )
}

// Assumes ways
fn bldg(id: i64) -> osm::OsmID {
    osm::OsmID::Way(osm::WayID(id))
}