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
use super::*;
use grid::{GridPoint3, Dir};
use super::triangles::TRIANGLES;
use na;
const RESOLUTION: [i64; 2] = [32, 64];
#[test]
fn move_forward_in_positive_x_direction() {
let mut pos = GridPoint3::default();
let mut dir = Dir::default();
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
assert_eq!(GridPoint3::default().with_x(1), pos);
assert_eq!(Dir::default(), dir);
}
#[test]
fn move_forward_into_northern_tropic_pentagon() {
// Start facing east, just west of a northern tropic pentagon.
let mut pos = GridPoint3::default().with_x(1).with_y(RESOLUTION[0] - 1);
let mut dir = Dir::new(4);
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// We should now be sitting on the northern tropic pentagon,
// facing south-east in root 1.
//
// Note that it wouldn't be legal to step in this direction.
assert_eq!(
GridPoint3::default().with_root(1).with_x(RESOLUTION[0]),
pos
);
assert_eq!(Dir::new(3), dir);
// Turn around, and walk back! Note some hacks to get back to a
// legal movement direction for now... and then using the smart
// turning functions to handle rebasing.
dir = Dir::new(dir.index + 1);
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// We should now be back where we started, but facing west.
assert_eq!(
GridPoint3::default().with_x(1).with_y(RESOLUTION[0] - 1),
pos
);
assert_eq!(Dir::new(10), dir);
}
#[test]
fn turn_left_at_northern_tropic() {
let triangle = &TRIANGLES[2];
// Start at triangle apex.
// Both parts of the apex are expressed in terms of x-dimension.
let apex = na::Point2::new(triangle.apex[0], triangle.apex[1]) * RESOLUTION[0];
let mut pos = GridPoint3::default().with_root(0).with_x(apex.x).with_y(
apex.y,
);
let mut dir = Dir::new(triangle.x_dir);
// Should be facing north in root 0.
assert_eq!(0, pos.root.index);
assert_eq!(Dir::new(8), dir);
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing west in root 0.
assert_eq!(0, pos.root.index);
assert_eq!(Dir::new(10), dir);
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing south-west in root 0.
assert_eq!(0, pos.root.index);
assert_eq!(Dir::new(0), dir);
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing south-east in root 0.
assert_eq!(0, pos.root.index);
assert_eq!(Dir::new(2), dir);
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing east in root 1.
assert_eq!(1, pos.root.index);
assert_eq!(Dir::new(4), dir);
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing north in root 1.
// Note that this represents where we started, but we should be
// stable in the same root we just came from instead of unnecessarily
// rebasing on the neighbour.
assert_eq!(1, pos.root.index);
assert_eq!(Dir::new(6), dir);
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing west in root 0.
// This is a repeat of the first turn we made, but now we're
// coming in from a neighbouring root rather than starting in root 0.
assert_eq!(0, pos.root.index);
assert_eq!(Dir::new(10), dir);
}
#[test]
fn turn_right_at_northern_tropic() {
let triangle = &TRIANGLES[2];
// Start at triangle apex.
// Both parts of the apex are expressed in terms of x-dimension.
let apex = na::Point2::new(triangle.apex[0], triangle.apex[1]) * RESOLUTION[0];
let mut pos = GridPoint3::default().with_root(0).with_x(apex.x).with_y(
apex.y,
);
let mut dir = Dir::new(triangle.x_dir);
// Should be facing north in root 0.
assert_eq!(0, pos.root.index);
assert_eq!(Dir::new(8), dir);
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing east in root 1.
assert_eq!(1, pos.root.index);
assert_eq!(Dir::new(4), dir);
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing south-east in root 1.
assert_eq!(1, pos.root.index);
assert_eq!(Dir::new(2), dir);
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing south-west in root 0.
assert_eq!(0, pos.root.index);
assert_eq!(Dir::new(0), dir);
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing west in root 0.
assert_eq!(0, pos.root.index);
assert_eq!(Dir::new(10), dir);
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Should be facing north in root 0.
// Note that this represents where we started, but unlike `turn_left_at_northern_tropic`,
// this will be the exact same position as we started in -- not a different representation.
// This is because the other test has a way of representing the starting angle from a
// different quad, because it approaches it from a different root that shares that angle.
assert_eq!(0, pos.root.index);
assert_eq!(Dir::new(8), dir);
}
#[test]
fn move_east_under_north_pole() {
// Start just south of the north pole in root 4,
// facing north-east.
let mut pos = GridPoint3::default().with_root(4).with_x(1).with_y(1);
let mut dir = Dir::new(6);
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// We should now be on the edge of root 4 and 0,
// facing east into root 0.
assert_eq!(GridPoint3::default().with_x(1), pos);
assert_eq!(Dir::new(4), dir);
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// We should now be on the edge of root 0 and 1,
// facing south-east into root 1.
assert_eq!(GridPoint3::default().with_root(1).with_x(1), pos);
assert_eq!(Dir::new(2), dir);
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// We should now be just south of the north pole in root 1.
assert_eq!(GridPoint3::default().with_root(1).with_x(1).with_y(1), pos);
assert_eq!(Dir::new(2), dir);
}
#[test]
fn move_west_under_north_pole() {
// Start just south of the north pole in root 1,
// facing north-west.
let mut pos = GridPoint3::default().with_root(1).with_x(1).with_y(1);
let mut dir = Dir::new(8);
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// We should now be on the edge of root 1 and 0,
// facing west into root 0.
assert_eq!(GridPoint3::default().with_y(1), pos);
assert_eq!(Dir::new(10), dir);
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// We should now be on the edge of root 0 and 4,
// facing south-west into root 1.
assert_eq!(GridPoint3::default().with_root(4).with_y(1), pos);
assert_eq!(Dir::new(0), dir);
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// We should now be just south of the north pole in root 4.
assert_eq!(GridPoint3::default().with_root(4).with_x(1).with_y(1), pos);
assert_eq!(Dir::new(0), dir);
}
#[test]
fn walk_anticlockwise_around_all_pentagons() {
// For each triangle in each root, start at its apex, take one step
// out along its x-axis, and then walk around in a circle just beyond
// the pentagon until we're back at the first hexagon we visited.
let triangle_indexes: Vec<usize> = (0..12).collect();
for root_index in 0..5 {
for triangle_index in triangle_indexes.iter() {
println!(
"Starting in root {} at apex of triangle {}.",
root_index,
triangle_index
);
let triangle = &TRIANGLES[*triangle_index];
// Start at triangle apex.
// Both parts of the apex are expressed in terms of x-dimension.
let apex = na::Point2::new(triangle.apex[0], triangle.apex[1]) * RESOLUTION[0];
let mut pos = GridPoint3::default()
.with_root(root_index)
.with_x(apex.x)
.with_y(apex.y);
let mut dir = Dir::new(triangle.x_dir);
// Take one step out along the x-axis and then face towards
// the next hexagon moving in an anticlockwise circle around
// the starting pentagon.
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Remember where we're supposed to end up.
let final_pos = pos.clone();
let final_dir = dir.clone();
for _ in 0..5 {
// Step forward. This should land us in the equivalent
// hexagon in the next root anti-clockwise from here.
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// Turn left. This will point us back at the next root
// anti-clockwise from here, leaving us ready to step again.
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
}
// We should now be back at the first hexagon we visited.
assert_eq!(final_pos, pos);
assert_eq!(final_dir, dir);
}
}
}
#[test]
fn walk_clockwise_around_all_pentagons() {
// For each triangle in each root, start at its apex, take one step
// out along its x-axis, and then walk around in a circle just beyond
// the pentagon until we're back at the first hexagon we visited.
let triangle_indexes: Vec<usize> = (0..12).collect();
for root_index in 0..5 {
for triangle_index in triangle_indexes.iter() {
println!(
"Starting in root {} at apex of triangle {}.",
root_index,
triangle_index
);
let triangle = &TRIANGLES[*triangle_index];
// Start at triangle apex.
// Both parts of the apex are expressed in terms of x-dimension.
let apex = na::Point2::new(triangle.apex[0], triangle.apex[1]) * RESOLUTION[0];
let mut pos = GridPoint3::default()
.with_root(root_index)
.with_x(apex.x)
.with_y(apex.y);
let mut dir = Dir::new(triangle.x_dir);
// Take one step out along the x-axis and then face towards
// the next hexagon moving in an clockwise circle around
// the starting pentagon.
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
// Remember where we're supposed to end up.
let final_pos = pos.clone();
let final_dir = dir.clone();
for _ in 0..5 {
// Step forward. This should land us in the equivalent
// hexagon in the next root clockwise from here.
move_forward(&mut pos, &mut dir, RESOLUTION).unwrap();
// Turn right. This will point us back at the next root
// clockwise from here, leaving us ready to step again.
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
}
// We should now be back at the first hexagon we visited.
assert_eq!(final_pos, pos);
assert_eq!(final_dir, dir);
}
}
}
#[test]
fn random_walks() {
use rand;
use rand::Rng;
// Try to simulate all the kinds of stepping and turning that a
// CellDweller might actually be able to do in the real world.
//
// Use a small resolution; we want to very quickly stumble
// upon pathological cases.
const RESOLUTION: [i64; 2] = [4, 8];
// Multiple walks will reveal problems that one long walk won't;
// e.g. forgetting to switch the turn bias if on a pentagon when turning
// around at the end of the walk. (Yes, I forgot this, and it made this
// test intermittently fail. Hodor.)
const WALKS: usize = 100;
const STEPS: usize = 100;
// Keep a list of the opposite steps we'll need to take to get
// back home.
enum Action {
StepForward,
TurnLeft,
TurnRight,
}
for _ in 0..WALKS {
let mut rng = rand::thread_rng();
// Start at (0, 0). This is not a very interesting place to start, but we'll
// be randomly walking all over the place, so there shouldn't be any need for
// the starting point to be interesting.
let mut pos = GridPoint3::default();
let mut dir = Dir::default();
let mut last_turn_bias = TurnDir::Left;
let mut crumbs: Vec<Action> = Vec::new();
for _ in 0..STEPS {
// Consider turning several times before stepping,
// with low probability on each.
for _ in 0..10 {
let f: f32 = rng.gen();
if f < 0.02 {
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
crumbs.push(Action::TurnLeft);
println!("Turned left.");
} else if f < 0.01 {
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
crumbs.push(Action::TurnRight);
println!("Turned right.");
}
}
step_forward_and_face_neighbor(&mut pos, &mut dir, RESOLUTION, &mut last_turn_bias)
.unwrap();
crumbs.push(Action::StepForward);
println!("Stepped forward: {:?}", pos);
}
// Aaaand turn around and walk back home!
turn_around_and_face_neighbor(&mut pos, &mut dir, RESOLUTION, last_turn_bias);
if is_pentagon(&pos, RESOLUTION) {
// Update turn bias; if we walk forward again, we want a _repeat_
// of the movement we just un-did.
last_turn_bias = last_turn_bias.opposite();
}
// Retrace steps.
crumbs.reverse();
for crumb in crumbs {
match crumb {
Action::StepForward => {
step_forward_and_face_neighbor(
&mut pos,
&mut dir,
RESOLUTION,
&mut last_turn_bias,
).unwrap()
}
Action::TurnLeft => {
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap()
}
Action::TurnRight => {
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap()
}
}
println!(
"Retracing crumbs walking forward; now at: {:?}, {:?}",
pos,
dir
);
}
// We should now be back at the start, but re-based into another root,
// facing down one of its axes.
//
// Note that depending on the number of STEPS, whether we turned one
// or more times at the first step, and therefore via which root we
// arrive home, both the root and the direction will vary.
assert_eq!(0, pos.x);
assert_eq!(0, pos.y);
}
}
#[test]
fn random_walks_retraced_by_stepping_backwards() {
use rand;
use rand::Rng;
let mut rng = rand::thread_rng();
// Try to simulate all the kinds of stepping and turning that a
// CellDweller might actually be able to do in the real world.
//
// Use a small resolution; we want to very quickly stumble
// upon pathological cases.
const RESOLUTION: [i64; 2] = [4, 8];
// Multiple walks will reveal problems that one long walk won't;
// e.g. forgetting to switch the turn bias if on a pentagon when turning
// around at the end of the walk. (Yes, I forgot this, and it made this
// test intermittently fail. Hodor.)
const WALKS: usize = 100;
const STEPS: usize = 100;
// Keep a list of the opposite steps we'll need to take to get
// back home.
enum Action {
StepBackward,
TurnLeft,
TurnRight,
}
for _ in 0..WALKS {
// Start at (0, 0). This is not a very interesting place to start, but we'll
// be randomly walking all over the place, so there shouldn't be any need for
// the starting point to be interesting.
let mut pos = GridPoint3::default();
let mut dir = Dir::default();
let mut last_turn_bias = TurnDir::Left;
let mut crumbs: Vec<Action> = Vec::new();
for _ in 0..STEPS {
// Consider turning several times before stepping,
// with low probability on each.
for _ in 0..10 {
let f: f32 = rng.gen();
if f < 0.02 {
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
crumbs.push(Action::TurnLeft);
println!("Turned left.");
} else if f < 0.01 {
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap();
crumbs.push(Action::TurnRight);
println!("Turned right.");
}
}
step_forward_and_face_neighbor(&mut pos, &mut dir, RESOLUTION, &mut last_turn_bias)
.unwrap();
crumbs.push(Action::StepBackward);
println!("Stepped forward: {:?}", pos);
}
// Aaaand start walking backwards to find home!
crumbs.reverse();
for crumb in crumbs {
match crumb {
Action::StepBackward => {
step_backward_and_face_neighbor(
&mut pos,
&mut dir,
RESOLUTION,
&mut last_turn_bias,
).unwrap()
}
Action::TurnLeft => {
turn_left_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap()
}
Action::TurnRight => {
turn_right_by_one_hex_edge(&mut pos, &mut dir, RESOLUTION).unwrap()
}
}
println!(
"Retracing crumbs walking backward; now at: {:?}, {:?}",
pos,
dir
);
}
// We should now be back at the start, but re-based into another root,
// facing down one of its axes.
//
// Note that depending on the number of STEPS, whether we turned one
// or more times at the first step, and therefore via which root we
// arrive home, both the root and the direction will vary.
assert_eq!(0, pos.x);
assert_eq!(0, pos.y);
}
}