forked from XanClic/PSSAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconflict_mask.c
96 lines (78 loc) · 2.84 KB
/
conflict_mask.c
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
#include <assert.h>
#include <stdbool.h>
#include "conflict_mask.h"
#include "schedule.h"
tl_config_t tl_conflict_mask[TL_COUNT];
bool tl_config_valid[1 << TL_COUNT];
void build_conflict_mask(void)
{
/*
* Every straight/right TL:
* - conflicts with every straight/right, except the opposite
* - conflicts with every left turn, except its own
* - conflicts with the opposite road's pedestrian TL
* Every left turn TL:
* - conflicts with every straight/right, except its own
* - conflicts with every left turn, except the opposite
* - conflicts with the target road's pedestrian TL
*/
// Straight/right car traffic lights
for (int i = 0; i < 4; i++) {
// conflict masks
int straight = 0xf, left = 0xf, pedestrian = 0xf;
// Cannot conflict with itself
straight &= ~(1 << i);
// Does not conflict with opposite
straight &= ~(1 << ((i + 2) % 4));
// Does not conflict with own left turn
left &= ~(1 << i);
// Conflicts with parallel roads' pedestrian TLs
pedestrian &= ~(1 << ((i + 1) % 4));
pedestrian &= ~(1 << ((i + 3) % 4));
tl_conflict_mask[i] = straight
| (left << 4)
| (pedestrian << 8);
}
// Left turn car traffic lights
for (int i = 0; i < 4; i++) {
int straight = 0xf, left = 0xf, pedestrian = 0xf;
// Does not conflict with own straight/right
straight &= ~(1 << i);
// Cannot conflict with itself
left &= ~(1 << i);
// Does not conflict with opposite
left &= ~(1 << ((i + 2) % 4));
// Does not conflict with the opposite road or the one to the right
pedestrian &= ~(1 << ((i + 1) % 4));
pedestrian &= ~(1 << ((i + 2) % 4));
tl_conflict_mask[i + 4] = straight
| (left << 4)
| (pedestrian << 8);
}
// Pedestrian traffic lights
for (int i = 0; i < 4; i++) {
// They don't conflict with each other; so just collect the car TL
// conflicts from what we've built already
for (int j = 0; j < 8; j++) {
if (tl_conflict_mask[j] & (1 << (i + 8))) {
tl_conflict_mask[i + 8] |= 1 << j;
}
}
}
// Sanity check: Check symmetry
for (int i = 0; i < TL_COUNT; i++) {
for (int j = 0; j < TL_COUNT; j++) {
assert(((tl_conflict_mask[i] >> j) & 1) == ((tl_conflict_mask[j] >> i) & 1));
}
}
// Build tl_config_valid
for (int i = 0; i < (1 << TL_COUNT); i++) {
tl_config_valid[i] = true;
for (int j = 0; j < 12; j++) {
if ((i & (1 << j)) && (i & tl_conflict_mask[j])) {
tl_config_valid[i] = false;
break;
}
}
}
}