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
use crate::draw::Draw;
use cairo::Context;
use n18hex::{
    Coord, Delta, Direction, Hex, HexColour, HexCorner, HexFace, HexPosition,
    PI,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Tokens {
    Single,
    Double,
    Triple,
    Quadruple,
}

impl Tokens {
    pub fn count(&self) -> usize {
        use Tokens::*;

        match self {
            Single => 1,
            Double => 2,
            Triple => 3,
            Quadruple => 4,
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Rotation {
    Zero,
    Cw90,
    Acw90,
    HalfTurn,
}

impl Rotation {
    pub fn radians(&self) -> f64 {
        match self {
            Rotation::Zero => 0.0,
            Rotation::Cw90 => PI / 2.0,
            Rotation::Acw90 => -PI / 2.0,
            Rotation::HalfTurn => PI,
        }
    }
}

/// Cities that are connected by track.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct City {
    pub tokens: Tokens,
    pub revenue: usize,
    pub position: HexPosition,
    pub angle: Rotation,
    /// Mark unavailable token space(s) with a solid colour.
    pub fill_colour: Option<HexColour>,
}

impl City {
    // TODO: allow a city to be "terminal" in that routes cannot leave it
    // (i.e., red off-map tiles) ... also need triangular track segments ...
    pub fn rotate(mut self, angle: Rotation) -> Self {
        self.angle = angle;
        self
    }

    fn delta_coords(
        &self,
        from: &Coord,
        delta: Option<Delta>,
        hex: &Hex,
    ) -> Coord {
        let radius = 0.5 * hex.max_d;
        match delta {
            Some(Delta::InDir(angle, frac)) => {
                let angle = angle.radians();
                Coord {
                    x: frac * radius * angle.cos(),
                    y: frac * radius * angle.sin(),
                }
            }
            Some(Delta::ToCentre(frac)) => from * -frac,
            None => (0.0, 0.0).into(),
        }
    }

    fn translate_coords(&self, hex: &Hex) -> Coord {
        match self.position {
            HexPosition::Centre(delta) => {
                let from = (0.0, 0.0).into();
                let d = self.delta_coords(&from, delta, hex);
                &from + &d
            }
            HexPosition::Face(face, delta) => {
                let exit = hex.midpoint(&face);
                let centre = exit.scale_by(0.7);
                let d = self.delta_coords(&centre, delta, hex);
                &centre + &d
            }
            HexPosition::Corner(corner, delta) => {
                let c1 = hex.corner_coord(&corner.next());
                let c2 = hex.corner_coord(&corner.prev());
                let centre = c1.average(c2);
                let d = self.delta_coords(&centre, delta, hex);
                &centre + &d
            }
        }
    }

    fn rotate_angle(&self, hex: &Hex) -> f64 {
        use HexCorner::*;

        // NOTE: must account for the hex orientation.
        let angle = self.angle.radians() + hex.orientation().arc_offset();
        if let HexPosition::Corner(corner, _) = self.position {
            // NOTE: currently only implemented for two-token cities.
            if self.tokens == Tokens::Double {
                let extra = match corner {
                    TopLeft => -PI / 6.0,
                    TopRight => PI / 6.0,
                    Right => 3.0 * PI / 6.0,
                    BottomRight => 5.0 * PI / 6.0,
                    BottomLeft => 7.0 * PI / 6.0,
                    Left => 9.0 * PI / 6.0,
                };
                return angle + extra;
            }
        }
        angle
    }

    pub fn translate_begin(&self, hex: &Hex, ctx: &Context) {
        let coord = self.translate_coords(hex);
        ctx.translate(coord.x, coord.y);
        ctx.rotate(self.rotate_angle(hex));
    }

    pub fn translate_end(&self, hex: &Hex, ctx: &Context) {
        let coord = self.translate_coords(hex);
        ctx.rotate(-self.rotate_angle(hex));
        ctx.translate(-coord.x, -coord.y);
    }

    pub fn in_dir(mut self, dir: Direction, frac: f64) -> Self {
        self.position = self.position.in_dir(dir, frac);
        self
    }

    /// Moves the city towards the hex centre.
    ///
    /// This can be used to ensure that a city is always placed in the same
    /// position, regardless of the map's [orientation](n18hex::Orientation).
    pub fn to_centre(mut self, frac: f64) -> Self {
        self.position = self.position.to_centre(frac);
        self
    }

    pub fn with_fill(mut self, fill: HexColour) -> Self {
        self.fill_colour = Some(fill);
        self
    }

    pub fn single(revenue: usize) -> City {
        City {
            tokens: Tokens::Single,
            revenue,
            position: HexPosition::Centre(None),
            angle: Rotation::Zero,
            fill_colour: None,
        }
    }

    pub fn single_at_face(revenue: usize, face: &HexFace) -> City {
        City {
            tokens: Tokens::Single,
            revenue,
            position: HexPosition::Face(*face, None),
            angle: Rotation::Zero,
            fill_colour: None,
        }
    }

    pub fn single_at_corner(revenue: usize, corner: &HexCorner) -> City {
        City {
            tokens: Tokens::Single,
            revenue,
            position: HexPosition::Corner(*corner, None),
            angle: Rotation::Zero,
            fill_colour: None,
        }
    }

    pub fn double(revenue: usize) -> City {
        City {
            tokens: Tokens::Double,
            revenue,
            position: HexPosition::Centre(None),
            angle: Rotation::Zero,
            fill_colour: None,
        }
    }

    pub fn double_at_corner(revenue: usize, corner: &HexCorner) -> City {
        City {
            tokens: Tokens::Double,
            revenue,
            position: HexPosition::Corner(*corner, None),
            angle: Rotation::Zero,
            fill_colour: None,
        }
    }

    // TODO: triple as a triangle or as a row of 3 tokens?
    pub fn triple(revenue: usize) -> City {
        City {
            tokens: Tokens::Triple,
            revenue,
            position: HexPosition::Centre(None),
            angle: Rotation::Zero,
            fill_colour: None,
        }
    }

    pub fn quad(revenue: usize) -> City {
        City {
            tokens: Tokens::Quadruple,
            revenue,
            position: HexPosition::Centre(None),
            angle: Rotation::Zero,
            fill_colour: None,
        }
    }

    // TODO: pub fn sextuple(revenue: usize) -> City
    // See tiles 8887 and 8888 for the game 1880:
    // http://www.fwtwr.com/18xx/tiles/tiles.asp?xGame=1880

    fn define_fg_path(&self, hex: &Hex, ctx: &Context) {
        let radius = hex.theme.token_space_radius.absolute(hex);
        self.define_bg_path(hex, ctx);

        match self.tokens {
            Tokens::Single => {}
            Tokens::Double => {
                // Define each token space.
                for x in &[radius, -radius] {
                    ctx.new_sub_path();
                    ctx.arc(*x, 0.0, radius, 0.0, 2.0 * PI);
                }
            }
            Tokens::Triple => {
                // Each circle is centred at the tip of an equilateral triangle
                // with side length 2 * radius; it has height radius * sqrt(3).
                let half_height = radius * (3.0_f64).sqrt() / 2.0;
                let centres = vec![
                    (-radius, half_height),
                    (radius, half_height),
                    (0.0, -half_height),
                ];
                // Define each token space.
                for (x, y) in &centres {
                    ctx.new_sub_path();
                    ctx.arc(*x, *y, radius, 0.0, 2.0 * PI);
                }
            }
            Tokens::Quadruple => {
                // Define each token space.
                for x in &[radius, -radius] {
                    for y in &[radius, -radius] {
                        ctx.new_sub_path();
                        ctx.arc(*x, *y, radius, 0.0, 2.0 * PI);
                    }
                }
            }
        }
    }

    fn define_bg_path(&self, hex: &Hex, ctx: &Context) {
        let radius = hex.theme.token_space_radius.absolute(hex);
        ctx.new_path();

        match self.tokens {
            Tokens::Single => {
                let (x, y) = (0.0, 0.0);
                ctx.arc(x, y, radius, 0.0, 2.0 * PI);
            }
            Tokens::Double => {
                // Define the containing box.
                ctx.move_to(-radius, radius);
                ctx.line_to(radius, radius);
                ctx.arc_negative(radius, 0.0, radius, PI / 2.0, -PI / 2.0);
                ctx.line_to(radius, -radius);
                ctx.line_to(-radius, -radius);
                ctx.arc_negative(-radius, 0.0, radius, -PI / 2.0, PI / 2.0);
                ctx.close_path();
            }
            Tokens::Triple => {
                // Each circle is centred at the tip of an equilateral triangle
                // with side length 2 * radius; it has height radius * sqrt(3).
                let half_height = radius * (3.0_f64).sqrt() / 2.0;
                let centres = vec![
                    (-radius, half_height),
                    (radius, half_height),
                    (0.0, -half_height),
                ];
                // Define the containing box.
                // Want the middle half of each edge of an equilateral triangle
                // that is larger than that described by the circle centres.
                let scale = 2.0 / ((3.0_f64).sqrt() - 1.0);
                let translate_y = -0.5 * radius;
                let pts: Vec<(f64, f64)> = centres
                    .iter()
                    .map(|(x, y)| (scale * x, scale * y + translate_y))
                    .collect();
                ctx.move_to(
                    pts[0].0 + 1.0 / 3.0 * (pts[1].0 - pts[0].0),
                    pts[0].1 + 1.0 / 3.0 * (pts[1].1 - pts[0].1),
                );
                ctx.line_to(
                    pts[0].0 + 2.0 / 3.0 * (pts[1].0 - pts[0].0),
                    pts[0].1 + 2.0 / 3.0 * (pts[1].1 - pts[0].1),
                );
                ctx.arc_negative(
                    centres[1].0,
                    centres[1].1,
                    radius,
                    PI / 2.0,
                    -PI / 6.0,
                );
                ctx.line_to(
                    pts[1].0 + 2.0 / 3.0 * (pts[2].0 - pts[1].0),
                    pts[1].1 + 2.0 / 3.0 * (pts[2].1 - pts[1].1),
                );
                ctx.arc_negative(
                    centres[2].0,
                    centres[2].1,
                    radius,
                    -PI / 6.0,
                    -5.0 * PI / 6.0,
                );
                ctx.line_to(
                    pts[2].0 + 2.0 / 3.0 * (pts[0].0 - pts[2].0),
                    pts[2].1 + 2.0 / 3.0 * (pts[0].1 - pts[2].1),
                );
                ctx.arc_negative(
                    centres[0].0,
                    centres[0].1,
                    radius,
                    -5.0 * PI / 6.0,
                    PI / 2.0,
                );
                ctx.close_path();
            }
            Tokens::Quadruple => {
                // Define the containing box.
                ctx.move_to(-radius, 2.0 * radius);
                ctx.line_to(radius, 2.0 * radius);
                ctx.arc_negative(radius, radius, radius, PI / 2.0, 0.0);
                ctx.line_to(2.0 * radius, radius);
                ctx.line_to(2.0 * radius, -radius);
                ctx.arc_negative(radius, -radius, radius, 0.0, -PI / 2.0);
                ctx.line_to(radius, -2.0 * radius);
                ctx.line_to(-radius, -2.0 * radius);
                ctx.arc_negative(-radius, -radius, radius, -PI / 2.0, -PI);
                ctx.line_to(-2.0 * radius, -radius);
                ctx.line_to(-2.0 * radius, radius);
                ctx.arc_negative(
                    -radius,
                    radius,
                    radius,
                    -PI,
                    -3.0 * PI / 2.0,
                );
                ctx.line_to(-radius, 2.0 * radius);
            }
        }
    }

    pub fn token_ixs(&self) -> Vec<usize> {
        (0..self.tokens.count()).collect()
    }

    pub fn define_token_path(
        &self,
        ix: usize,
        hex: &Hex,
        ctx: &Context,
    ) -> bool {
        if ix >= self.tokens.count() {
            return false;
        }

        self.translate_begin(hex, ctx);
        let radius = hex.theme.token_space_radius.absolute(hex);
        ctx.new_path();

        match self.tokens {
            Tokens::Single => {
                let (x, y) = (0.0, 0.0);
                ctx.arc(x, y, radius, 0.0, 2.0 * PI);
            }
            Tokens::Double => {
                let x = vec![radius, -radius][ix];
                ctx.arc(x, 0.0, radius, 0.0, 2.0 * PI);
            }
            Tokens::Triple => {
                let half_height = radius * (3.0_f64).sqrt() / 2.0;
                let (x, y) = vec![
                    (-radius, half_height),
                    (radius, half_height),
                    (0.0, -half_height),
                ][ix];
                ctx.arc(x, y, radius, 0.0, 2.0 * PI);
            }
            Tokens::Quadruple => {
                let (x, y) = vec![
                    (radius, radius),
                    (radius, -radius),
                    (-radius, radius),
                    (-radius, -radius),
                ][ix];
                ctx.arc(x, y, radius, 0.0, 2.0 * PI);
            }
        }

        self.translate_end(hex, ctx);

        true
    }
}

impl Draw for City {
    fn define_boundary(&self, hex: &Hex, ctx: &Context) {
        self.translate_begin(hex, ctx);
        self.define_bg_path(hex, ctx);
        self.translate_end(hex, ctx);
    }

    fn draw_bg(&self, hex: &Hex, ctx: &Context) {
        self.translate_begin(hex, ctx);

        self.define_bg_path(hex, ctx);
        hex.theme.token_space_outer.apply_line_and_stroke(ctx, hex);
        ctx.stroke_preserve().unwrap();
        hex.theme.token_space_outer.apply_fill(ctx);
        ctx.fill_preserve().unwrap();

        self.translate_end(hex, ctx);
    }

    fn draw_fg(&self, hex: &Hex, ctx: &Context) {
        self.translate_begin(hex, ctx);

        self.define_bg_path(hex, ctx);
        match self.fill_colour {
            None => hex.theme.token_space_inner.apply_fill(ctx),
            Some(colour) => hex.theme.apply_hex_colour(ctx, colour),
        }
        ctx.fill_preserve().unwrap();
        self.define_fg_path(hex, ctx);
        hex.theme.token_space_inner.apply_line_and_stroke(ctx, hex);
        ctx.stroke().unwrap();

        self.translate_end(hex, ctx);
    }
}