n18tile 0.1.0

Defines 18xx tile elements and track networks.
Documentation
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
use crate::{City, Connection, Connections, Dit, Draw, Label, Track};
use cairo::Context;
use n18hex::{Colour, Hex, HexColour, HexFace, HexPosition};
use std::collections::{BTreeMap, BTreeSet};

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum DrawLayer {
    Under,
    Normal,
    Over,
    Topmost,
}

impl DrawLayer {
    pub fn below(&self) -> Option<Self> {
        use DrawLayer::*;
        match self {
            Under => None,
            Normal => Some(Under),
            Over => Some(Normal),
            Topmost => Some(Over),
        }
    }

    pub fn above(&self) -> Option<Self> {
        use DrawLayer::*;
        match self {
            Under => Some(Normal),
            Normal => Some(Over),
            Over => Some(Topmost),
            Topmost => None,
        }
    }
}

pub type LabelAndPos = (Label, HexPosition);

pub type Tiles = Vec<Tile>;

/// A tile contains some number of track segments and cities.
#[derive(PartialEq, Debug, Clone)]
pub struct Tile {
    pub colour: HexColour,
    pub name: String,
    tracks: Vec<Track>,
    cities: Vec<City>,
    // Track indices by drawing layer.
    tracks_tbl: BTreeMap<DrawLayer, Vec<usize>>,
    // City indices by drawing layer.
    cities_tbl: BTreeMap<DrawLayer, Vec<usize>>,
    // The revenue(s) for any dits and/or cities.
    revenues: Vec<usize>,
    // Tile labels: tile name, revenue, city name, etc.
    labels: Vec<LabelAndPos>,
    // Whether to show the tile name.
    show_tile_name: bool,
    // Connections between tracks, dits, cities, and hex faces.
    conns: Connections,
    // For an offboard tile, this identifies the tile faces that are connected
    // to onboard tiles; for all other tiles, this is `None`.
    offboard_faces: Option<Vec<HexFace>>,
}

impl Tile {
    /// Creates a new tile and determines the connectivity between track
    /// segments, revenue centres, and hex faces.
    ///
    /// Note that `hex` should be the [**default hexagon**](Hex::default),
    /// to ensure that track connectivity is valid and consistent.
    /// By making this an argument, a single [Hex] can be used to construct
    /// all required tiles, rather than creating a new [Hex] for each tile.
    pub fn new<S: Into<String>>(
        colour: HexColour,
        name: S,
        tracks: Vec<Track>,
        cities: Vec<City>,
        hex: &Hex,
    ) -> Self {
        // TODO: check track connectivity and crossing, determine layers
        // Also save this information in a form that's amenable for
        // building track networks ... ???
        // TODO: detect track segments that cross a city (and that this isn't
        // part of the clipped path) and break them into separate segments
        // (e.g., straight -> mid + mid; gentle_l -> ...)
        // Hmmm ... maybe not
        let ctx = hex.context();
        let mut tracks_tbl = BTreeMap::new();
        let mut cities_tbl = BTreeMap::new();
        let default_layer = DrawLayer::Normal;
        let dt = 0.10;
        let mut track_layers = BTreeMap::new();
        let name = name.into();
        // TODO: can we divide this up into smaller functions and check
        // some invariant conditions and write test cases?
        let verbose = false;
        if verbose {
            println!("Inspecting tile {} ...", name);
        }
        for (i, track) in tracks.iter().enumerate() {
            for (j, other) in tracks.iter().enumerate().skip(i + 1) {
                if track.crosses(other, hex, dt, ctx) {
                    if verbose {
                        println!("    Tracks {} and {} cross", i, j);
                    }
                    // NOTE: if the underlying track's layer is Over, the
                    // overlying track needs to be in the Top layer.
                    if let Some(l) = track_layers.get(&i) {
                        if l == &DrawLayer::Over {
                            track_layers.insert(j, DrawLayer::Topmost);
                            continue;
                        }
                    }
                    let this_layer = default_layer.below().unwrap();
                    let other_layer = default_layer.above().unwrap();
                    track_layers.insert(i, this_layer);
                    track_layers.insert(j, other_layer);
                } else {
                    track_layers.entry(i).or_insert(default_layer);
                }
            }
            track_layers.entry(i).or_insert(default_layer);
        }
        if verbose {
            println!("    Have {} tracks", tracks.len());
            for (key, val) in track_layers.iter() {
                println!("        key: {} val: {:?}", key, val);
            }
        }
        // NOTE: there can be zero, one, or multiple revenues for a tile.
        let mut revenues: Vec<usize> = tracks
            .iter()
            .filter_map(|t| t.dit.map(|(_, revenue, _)| revenue))
            .chain(cities.iter().map(|c| c.revenue))
            .collect();
        revenues.sort_unstable();
        revenues.dedup();
        for (cx, city) in cities.iter().enumerate() {
            let mut layer = DrawLayer::Under;
            for (i, track) in tracks.iter().enumerate() {
                // Tracks must start or end at a city, rather than passing
                // through a city. This allows routes to identify a track
                // by tile and the track index, rather than needing to worry
                // about subsets of a track.
                if track.connected_to_fill(city, hex, ctx) {
                    let track_layer = track_layers.get(&i).unwrap_or(&layer);
                    layer = std::cmp::max(layer, *track_layer);
                } else if track.intersects_fill(city, hex, dt, ctx) {
                    println!("WARNING: track crosses city, tile {}", name);
                    let track_layer = track_layers.get(&i).unwrap_or(&layer);
                    layer = std::cmp::max(layer, *track_layer);
                }
            }
            if verbose {
                println!("    City #{} in layer {:?}", cx, layer);
            }
            cities_tbl.entry(layer).or_insert_with(Vec::new).push(cx)
        }
        for (i, _track) in tracks.iter().enumerate() {
            let layer = track_layers.get(&i).unwrap();
            tracks_tbl.entry(*layer).or_insert_with(Vec::new).push(i)
        }
        let conns = Connections::new(&tracks, &cities, hex);
        Self {
            colour,
            name,
            tracks,
            cities,
            tracks_tbl,
            cities_tbl,
            revenues,
            labels: vec![],
            show_tile_name: true,
            conns,
            offboard_faces: None,
        }
    }

    /// Identifies the tile as an off-board tile that has `faces` adjacent to
    /// on-board tiles.
    pub fn with_offboard_faces<T>(mut self, faces: T) -> Self
    where
        T: IntoIterator<Item = HexFace>,
    {
        self.offboard_faces = Some(faces.into_iter().collect());
        self
    }

    /// Returns the edges adjacent to on-board tiles, if this tile is an
    /// off-board tile, otherwise returns `None`.
    pub fn offboard_faces(&self) -> Option<Vec<HexFace>> {
        self.offboard_faces.as_ref().cloned()
    }

    /// Do not display the tile name when drawing the tile.
    pub fn hide_tile_name(mut self) -> Self {
        self.show_tile_name = false;
        self
    }

    /// Returns whether the tile name is displayed when drawing the tile.
    pub fn is_tile_name_visible(&self) -> bool {
        self.show_tile_name
    }

    pub fn connections(&self, from: &Connection) -> Option<&[Connection]> {
        self.conns.from(from)
    }

    /// Returns all connections that can be reached from `start`.
    pub fn all_connections_from<T>(&self, start: T) -> BTreeSet<Connection>
    where
        T: Into<Connection>,
    {
        let start = start.into();
        self.conns.connections_from(&start)
    }

    /// Returns the hexagon faces that are connected to `start`.
    pub fn connected_faces<T>(&self, start: T) -> BTreeSet<HexFace>
    where
        T: Into<Connection>,
    {
        self.all_connections_from(start)
            .into_iter()
            .filter_map(|conn| {
                if let Connection::Face { face } = conn {
                    Some(face)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Returns the index of each dit that is connected to `start`.
    pub fn connected_dits<T>(&self, start: T) -> BTreeSet<usize>
    where
        T: Into<Connection>,
    {
        self.all_connections_from(start)
            .into_iter()
            .filter_map(|conn| {
                if let Connection::Dit { ix } = conn {
                    Some(ix)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Returns the index of each city that is connected to `start`.
    pub fn connected_cities<T>(&self, start: T) -> BTreeSet<usize>
    where
        T: Into<Connection>,
    {
        self.all_connections_from(start)
            .into_iter()
            .filter_map(|conn| {
                if let Connection::City { ix } = conn {
                    Some(ix)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Returns true if `start` is connected to each hexagon face in `dests`,
    /// and is not connected to any hexagon face not included in `dests`.
    pub fn connected_faces_are<T>(&self, start: T, dests: &[HexFace]) -> bool
    where
        T: Into<Connection>,
    {
        let want_faces: BTreeSet<HexFace> = dests.iter().copied().collect();
        let faces = self.connected_faces(start);
        want_faces == faces
    }

    /// Returns true if `start` is not connected to any other hexagon face.
    pub fn no_connected_faces<T>(&self, start: T) -> bool
    where
        T: Into<Connection>,
    {
        self.connected_faces_are(start, &[])
    }

    /// Returns the index of each dit connected to `start`, if `start` is only
    /// connected to each dit in `dits` (identified by their revenue).
    /// Otherwise, returns `None`.
    pub fn connected_dits_are<T>(
        &self,
        start: T,
        dits: &[usize],
    ) -> Option<Vec<usize>>
    where
        T: Into<Connection>,
    {
        let mut want_revenues: Vec<usize> = dits.into();
        want_revenues.sort_unstable();
        let (found_revenues, found_ixs): (Vec<_>, Vec<_>) = self
            .connected_dits(start)
            .into_iter()
            .map(|ix| (self.conns.dits()[ix].revenue, ix))
            .unzip();

        // Check whether we obtained the desired results.
        if want_revenues == found_revenues {
            Some(found_ixs)
        } else {
            None
        }
    }

    /// Returns the index of each city connected to `start`, if `start` is
    /// only connected to each city in `cities` (identified by their revenue
    /// and number of token spaces, respectively).
    /// Otherwise, returns `None`.
    pub fn connected_cities_are<T>(
        &self,
        start: T,
        cities: &[(usize, usize)],
    ) -> Option<Vec<usize>>
    where
        T: Into<Connection>,
    {
        let mut want_rev_space: Vec<(usize, usize)> = cities.into();
        want_rev_space.sort_unstable();
        let (found_rev_space, found_ixs): (Vec<_>, Vec<_>) = self
            .connected_cities(start)
            .into_iter()
            .map(|ix| {
                let city = self.cities[ix];
                ((city.revenue, city.tokens.count()), ix)
            })
            .unzip();

        // Check whether we obtained the desired results.
        if want_rev_space == found_rev_space {
            Some(found_ixs)
        } else {
            None
        }
    }

    // TODO: verify labels (e.g., one revenue label for each revenue ix)

    pub fn label<P>(mut self, label: Label, pos: P) -> Self
    where
        P: Into<HexPosition>,
    {
        self.labels.push((label, pos.into()));
        self
    }

    pub fn tracks(&self) -> &[Track] {
        self.tracks.as_slice()
    }

    pub fn dits(&self) -> &[Dit] {
        self.conns.dits()
    }

    pub fn cities(&self) -> &[City] {
        self.cities.as_slice()
    }

    pub fn labels(&self) -> &[LabelAndPos] {
        self.labels.as_slice()
    }

    /// Returns the city that corresponds to the provided token location.
    pub fn city(&self, space: &TokenSpace) -> Option<&City> {
        self.cities.get(space.city_ix)
    }

    fn layer_bg(&self, layer: &DrawLayer, ctx: &Context, hex: &Hex) {
        let empty = vec![];
        for ix in self.tracks_tbl.get(layer).unwrap_or(&empty) {
            let track = self.tracks[*ix];
            track.draw_bg(hex, ctx)
        }
        let empty = vec![];
        for ix in self.cities_tbl.get(layer).unwrap_or(&empty) {
            let city = self.cities[*ix];
            city.draw_bg(hex, ctx)
        }
    }

    #[allow(dead_code)]
    fn coords_in_red(&self, layer: &DrawLayer, ctx: &Context, hex: &Hex) {
        let empty = vec![];

        for ix in self.tracks_tbl.get(layer).unwrap_or(&empty).iter() {
            let track = self.tracks[*ix];
            ctx.set_source_rgb(1.0, 0.0, 0.0);
            let line_cap = ctx.line_cap();
            ctx.set_line_cap(cairo::LineCap::Round);
            for coord in track.coords(hex, 0.1) {
                ctx.new_path();
                ctx.move_to(coord.x, coord.y);
                ctx.line_to(coord.x, coord.y);
                ctx.stroke().unwrap();
            }
            ctx.set_line_cap(line_cap);
        }
    }

    #[allow(dead_code)]
    fn dit_coords_in_red(&self, ctx: &Context, hex: &Hex) {
        use DrawLayer::*;

        // Draw the centre of each dit on a track segment as a red dot.
        for layer in &[Under, Normal, Over, Topmost] {
            let empty = vec![];
            ctx.set_source_rgb(1.0, 0.0, 0.0);
            let line_cap = ctx.line_cap();
            ctx.set_line_cap(cairo::LineCap::Round);
            for ix in self.tracks_tbl.get(layer).unwrap_or(&empty) {
                let track = self.tracks[*ix];
                if let Some(coord) = track.dit_coord(hex) {
                    ctx.new_path();
                    ctx.move_to(coord.x, coord.y);
                    ctx.line_to(coord.x, coord.y);
                    ctx.stroke().unwrap();
                }
            }
            ctx.set_line_cap(line_cap);
        }
    }

    fn layer_fg(&self, layer: &DrawLayer, ctx: &Context, hex: &Hex) {
        let empty = vec![];
        for ix in self.tracks_tbl.get(layer).unwrap_or(&empty) {
            let track = self.tracks[*ix];
            track.draw_fg(hex, ctx);
        }

        for ix in self.tracks_tbl.get(layer).unwrap_or(&empty) {
            let track = self.tracks[*ix];
            track.draw_circle_dit(hex, ctx);
        }

        // NOTE: draw coordinates along track in red.
        // self.coords_in_red(layer, ctx, hex);

        let empty = vec![];
        for ix in self.cities_tbl.get(layer).unwrap_or(&empty) {
            let city = self.cities[*ix];
            city.draw_fg(hex, ctx)
        }
    }

    pub fn revenues(&self) -> &[usize] {
        &self.revenues
    }

    /// Returns `true` if this tile is an off-board tile for which special
    /// off-board track segments should be drawn, instead of drawing the
    /// regular track segments, dits, and cities.
    pub fn only_draw_offboard_track(&self) -> bool {
        self.offboard_faces.is_some()
    }

    /// Defines the inner (foreground) layer of the off-board track segment
    /// for the specified tile `face`, if such a segment should be drawn on
    /// this face, and returns `true`.
    ///
    /// If no such segment should be drawn, this has no effect and returns
    /// `false`.
    pub fn define_offboard_track_inner_path(
        &self,
        ctx: &Context,
        hex: &Hex,
        face: &HexFace,
    ) -> bool {
        if let Some(ref faces) = self.offboard_faces {
            if !faces.iter().any(|f| f == face) {
                return false;
            }
        } else {
            return false;
        }

        // Extract relevant theme settings.
        let inner_w = hex.theme.track_inner.width.absolute(hex);
        let outer_w = hex.theme.track_outer.width.absolute(hex);
        let len = hex.theme.track_offboard_length.absolute(hex);

        // Calculate reference coordinates.
        let start = hex.midpoint(face);
        let opposite = hex.midpoint(&face.opposite());
        let end = &start + &(&opposite.normalise() * len);
        let along_face = n18hex::Coord::unit_normal(&start, &end);

        // Calculate points for the inner (foreground) style.
        let inner_1 = &start + &(&along_face * (0.5 * inner_w));
        let inner_2 = &start - &(&along_face * (0.5 * inner_w));
        let inner_3 = start.interpolate(&end, inner_w / outer_w);

        ctx.new_path();
        ctx.move_to(inner_1.x, inner_1.y);
        ctx.line_to(inner_2.x, inner_2.y);
        ctx.line_to(inner_3.x, inner_3.y);

        true
    }

    /// Draws the off-board track segment for the specified tile `face`, if
    /// such a segment should be drawn on this face, and returns `true`.
    ///
    /// If no such segment should be drawn, this has no effect and returns
    /// `false`.
    pub fn draw_offboard_segment(
        &self,
        ctx: &Context,
        hex: &Hex,
        face: &HexFace,
    ) -> bool {
        if let Some(ref faces) = self.offboard_faces {
            if !faces.iter().any(|f| f == face) {
                return false;
            }
        } else {
            return false;
        }

        // Extract relevant theme settings.
        let inner_w = hex.theme.track_inner.width.absolute(hex);
        let outer_w = hex.theme.track_outer.width.absolute(hex);
        let len = hex.theme.track_offboard_length.absolute(hex);

        // Calculate reference coordinates.
        let start = hex.midpoint(face);
        let opposite = hex.midpoint(&face.opposite());
        let end = &start + &(&opposite.normalise() * len);
        let along_face = n18hex::Coord::unit_normal(&start, &end);

        ctx.new_path();

        // Calculate points for the outer (background) style.
        let outer_1 = &start + &(&along_face * (0.5 * outer_w));
        let outer_2 = &start - &(&along_face * (0.5 * outer_w));
        let outer_3 = end;
        // NOTE: using the stroke colour to fill this path.
        hex.theme.track_outer.stroke.apply_colour(ctx);
        ctx.move_to(outer_1.x, outer_1.y);
        ctx.line_to(outer_2.x, outer_2.y);
        ctx.line_to(outer_3.x, outer_3.y);
        ctx.fill().unwrap();

        // Calculate points for the inner (foreground) style.
        let inner_1 = &start + &(&along_face * (0.5 * inner_w));
        let inner_2 = &start - &(&along_face * (0.5 * inner_w));
        let inner_3 = start.interpolate(&end, inner_w / outer_w);
        // NOTE: using the stroke colour to fill this path.
        hex.theme.track_inner.stroke.apply_colour(ctx);
        ctx.move_to(inner_1.x, inner_1.y);
        ctx.line_to(inner_2.x, inner_2.y);
        ctx.line_to(inner_3.x, inner_3.y);
        ctx.fill().unwrap();

        true
    }

    pub fn draw(&self, ctx: &Context, hex: &Hex) {
        use DrawLayer::*;

        // Draw the tile background.
        hex.draw_background(self.colour, ctx);
        if let Some(ref faces) = self.offboard_faces {
            for face in faces {
                self.draw_offboard_segment(ctx, hex, face);
            }
        } else {
            // Draw the background for the bottom two layers.
            self.layer_bg(&Under, ctx, hex);
            self.layer_bg(&Normal, ctx, hex);
            // Draw the foreground for the bottom-most layer.
            self.layer_fg(&Under, ctx, hex);
            // Draw the background of the covering layer.
            self.layer_bg(&Over, ctx, hex);
            // Draw the foreground of the normal and covering layers.
            self.layer_fg(&Normal, ctx, hex);
            self.layer_fg(&Over, ctx, hex);
            // Draw the top-most layer.
            self.layer_bg(&Topmost, ctx, hex);
            self.layer_fg(&Topmost, ctx, hex);
        }
        // Draw the tile name, except for special tiles such as those that are
        // part of the initial map and are not truly "tiles" as such.
        if self.show_tile_name {
            Colour::BLACK.apply_colour(ctx);
            let hex_pos = Label::tile_name_position(hex);
            Label::TileName.draw(ctx, hex, &hex_pos, self);
        }
        // Draw other tile labels.
        for (label, pos) in &self.labels {
            label.draw(ctx, hex, pos, self);
        }
    }

    pub fn token_spaces(&self) -> Vec<TokenSpace> {
        self.cities
            .iter()
            .enumerate()
            .flat_map(|(city_ix, city)| {
                city.token_ixs()
                    .into_iter()
                    .map(|token_ix| TokenSpace { city_ix, token_ix })
                    .collect::<Vec<_>>()
            })
            .collect()
    }

    pub fn city_token_spaces(&self, city_ix: usize) -> Vec<TokenSpace> {
        self.cities[city_ix]
            .token_ixs()
            .into_iter()
            .map(|token_ix| TokenSpace { city_ix, token_ix })
            .collect()
    }

    pub fn token_space_count(&self) -> usize {
        self.token_spaces().len()
    }

    pub fn dit_count(&self) -> usize {
        self.tracks
            .iter()
            .filter(|track| track.dit.is_some())
            .count()
    }

    pub fn define_token_space(
        &self,
        space: &TokenSpace,
        hex: &Hex,
        ctx: &Context,
    ) -> bool {
        if space.city_ix >= self.cities.len() {
            return false;
        }
        let city = self.cities[space.city_ix];
        city.define_token_path(space.token_ix, hex, ctx)
    }

    /// Check whether a tile can be upgraded to another tile.
    pub fn can_upgrade_to(&self, other: &Tile) -> bool {
        // Check whether the new tile's colour is correct.
        if let Some(colour) = self.colour.next_phase() {
            if other.colour != colour {
                return false;
            }
        } else {
            // If there is no next phase, the tile cannot be upgraded.
            return false;
        }
        // Tiles must have the same number of dits.
        if self.dit_count() != other.dit_count() {
            return false;
        }
        let self_tok_spaces = self.token_space_count();
        let other_tok_spaces = other.token_space_count();
        // City tiles can only be upgraded to from existing city tiles.
        if self_tok_spaces == 0 && other_tok_spaces > 0 {
            return false;
        }
        // Check whether the new tile has at least as many token spaces.
        if self_tok_spaces > other_tok_spaces {
            return false;
        }
        // TODO: other checks, such as preserving track connectivity?
        // That would require having access to the map, so this would have to
        // be an additional layer of filtering provided by the map itself.
        true
    }

    /// Determines the surface size for this tile, which includes a small
    /// margin on all four sides.
    fn surface_width(&self, hex: &Hex) -> f64 {
        let margin = hex.theme.tile_margin.absolute(hex);
        hex.max_d + 2.0 * margin
    }

    /// Saves the tile to a PNG file.
    pub fn save_png<P: AsRef<std::path::Path>>(
        &self,
        hex: &Hex,
        path: P,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let mut file = std::fs::File::create(path)?;
        self.write_png(hex, &mut file)
    }

    /// Saves the tile to an SVG file.
    pub fn save_svg<P: AsRef<std::path::Path>>(
        &self,
        hex: &Hex,
        path: P,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let file = std::fs::File::create(path)?;
        self.write_svg(hex, file)
    }

    /// Saves the tile to a PDF file.
    pub fn save_pdf<P: AsRef<std::path::Path>>(
        &self,
        hex: &Hex,
        path: P,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let file = std::fs::File::create(path)?;
        self.write_pdf(hex, file)
    }

    /// Writes the tile as a PNG image to the provided stream.
    pub fn write_png<W: std::io::Write>(
        &self,
        hex: &Hex,
        stream: &mut W,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let width = self.surface_width(hex);
        let dim = width as i32;
        let surface =
            cairo::ImageSurface::create(cairo::Format::ARgb32, dim, dim)
                .map_err(|_status| "Can't create surface")?;
        let ctx = cairo::Context::new(&surface)
            .expect("Can't create cairo::Context");
        ctx.translate(width / 2.0, width / 2.0);
        self.draw(&ctx, hex);
        surface.write_to_png(stream)?;
        Ok(())
    }

    /// Writes the tile as an SVG image to the provided stream.
    pub fn write_svg<W: std::io::Write + 'static>(
        &self,
        hex: &Hex,
        stream: W,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let width = self.surface_width(hex);
        let surface = cairo::SvgSurface::for_stream(width, width, stream)
            .map_err(|_status| "Can't create surface")?;
        let ctx = cairo::Context::new(&surface)
            .expect("Can't create cairo::Context");
        ctx.translate(width / 2.0, width / 2.0);
        self.draw(&ctx, hex);
        surface
            .finish_output_stream()
            .map(|_stream| ())
            .map_err(|err| err.error.into())
    }

    /// Writes the tile as a PDF image to the provided stream.
    pub fn write_pdf<W: std::io::Write + 'static>(
        &self,
        hex: &Hex,
        stream: W,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let width = self.surface_width(hex);
        let surface = cairo::PdfSurface::for_stream(width, width, stream)
            .map_err(|_status| "Can't create surface")?;
        let ctx = cairo::Context::new(&surface)
            .expect("Can't create cairo::Context");
        ctx.translate(width / 2.0, width / 2.0);
        self.draw(&ctx, hex);
        surface
            .finish_output_stream()
            .map(|_stream| ())
            .map_err(|err| err.error.into())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct TokenSpace {
    city_ix: usize,
    token_ix: usize,
}

impl TokenSpace {
    pub fn city_ix(&self) -> usize {
        self.city_ix
    }
}

#[cfg(test)]
/// Tests that check whether `Tile` appropriately layers the tile elements and
/// correctly detects their connectivity.
mod tests {
    use crate::*;

    use super::DrawLayer::*;
    use n18hex::{Hex, HexColour::*, HexFace::*};
    use TrackEnd::*;

    static HEX_DIAMETER: f64 = 150.0;

    #[test]
    /// Constructs a tile with two track segments that do not cross each
    /// other, and checks that both track segments are drawn in the `Normal`
    /// layer.
    fn no_overlaps_one_layer() {
        let hex = Hex::new(HEX_DIAMETER);
        let tile = Tile::new(
            Yellow,
            "Test".to_string(),
            vec![
                Track::hard_l(Bottom).with_span(0.0, 0.5).with_dit(
                    End,
                    10,
                    DitShape::Bar,
                ),
                Track::hard_l(Bottom).with_span(0.5, 1.0),
            ],
            vec![],
            &hex,
        );
        let items_opt = tile.tracks_tbl.get(&Normal);
        assert!(items_opt.is_some(), "No items in Normal draw layer");
        let items = items_opt.unwrap();
        assert_eq!(
            items.len(),
            2,
            "Expected two tracks in Normal draw layer"
        );
        assert_eq!(
            tile.tracks_tbl.len(),
            1,
            "Expected only one drawing layer"
        );
    }

    #[test]
    /// Constructs a tile with two track segments that cross each other, and
    /// checks that the track segments are drawn in different layers.
    fn one_overlap_two_layers() {
        let hex = Hex::new(HEX_DIAMETER);
        let tile = Tile::new(
            Yellow,
            "Test".to_string(),
            vec![Track::straight(Bottom), Track::straight(UpperLeft)],
            vec![],
            &hex,
        );
        for layer in &[Under, Over] {
            let items_opt = tile.tracks_tbl.get(layer);
            assert!(
                items_opt.is_some(),
                "No items in {:?} draw layer",
                layer
            );
            let items = items_opt.unwrap();
            assert_eq!(
                items.len(),
                1,
                "Expected one track in {:?} draw layer",
                layer
            );
        }
        assert_eq!(tile.tracks_tbl.len(), 2, "Expected two drawing layers");
    }

    #[test]
    /// Constructs a tile with three track segments that cross each other, and
    /// checks that the track segments are drawn in different layers.
    fn two_overlaps_three_layers() {
        let hex = Hex::new(HEX_DIAMETER);
        let tile = Tile::new(
            Yellow,
            "Test".to_string(),
            vec![
                Track::straight(Bottom),
                Track::straight(UpperLeft),
                Track::straight(UpperRight),
            ],
            vec![],
            &hex,
        );
        for layer in &[Under, Over, Topmost] {
            let items_opt = tile.tracks_tbl.get(layer);
            assert!(
                items_opt.is_some(),
                "No items in {:?} draw layer",
                layer
            );
            let items = items_opt.unwrap();
            assert_eq!(
                items.len(),
                1,
                "Expected one track in {:?} draw layer",
                layer
            );
        }
        assert_eq!(tile.tracks_tbl.len(), 3, "Expected three drawing layers");
    }
}