-
Notifications
You must be signed in to change notification settings - Fork 0
/
car_system.cpp
291 lines (251 loc) · 8.32 KB
/
car_system.cpp
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
#include "car_system.h"
#include <random>
constexpr const std::size_t Car_System::SLOW_DOWN_BIT;
constexpr const std::size_t Car_System::EMPTY;
constexpr const std::size_t Car_System::BARRIER;
Car_System::Car_System(std::size_t length, std::size_t lanes, std::size_t car_density, std::size_t max_speed, std::size_t slow_down_chance)
{
multiple_lanes = true;
create(length, lanes, car_density, max_speed, slow_down_chance);
}
void Car_System::calculate()
{
// random machine
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<std::size_t> randomization(0, 99);
// [[pos, lane], new_lane]
std::vector< std::pair<std::pair<std::size_t, std::size_t>, std::size_t> > lane_changes;
std::vector< std::vector<std::size_t> > reserved_pos(lanes);
//std::vector< std::vector< std::pair<std::size_t, std::size_t> > > lane_changes(lanes);
for(std::size_t lane = 0; lane < lanes; ++lane)
{
for(std::size_t pos = 0; pos < length; ++pos)
{
// skip empty cells
if(!is_car_at(pos, lane))
continue;
// accelerate
std::size_t speed = get_speed(pos, lane);
if(speed < max_speed)
++speed;
// calculate distance to next car/barrier
std::size_t gap = 0;
while(gap++ < max_speed)
if(is_car_at((pos + gap) % length, lane) || is_barrier_at((pos + gap) % length, lane))
break;
--gap;
// if enabled, calc distances of other lanes
if(multiple_lanes)
{
std::size_t gap_left = 0, gap_right = 0;
if(lane > 0)
{
while(gap_left++ < max_speed)
if(is_car_at((pos + gap_left) % length, lane - 1) || is_barrier_at((pos + gap_left) % length, lane - 1) || std::find(reserved_pos[lane - 1].begin(), reserved_pos[lane - 1].end(), pos + gap_left) != reserved_pos[lane - 1].end())
break;
--gap_left;
std::size_t gap_back = 0;
while(gap_back++ < max_speed)
{
std::size_t temp_pos = (gap_back <= pos) ? pos - gap_back : length - (gap_back - pos);
if(is_car_at(temp_pos, lane - 1))
break;
else if(is_barrier_at(temp_pos, lane - 1))
{
gap_back = max_speed + 1;
break;
}
}
if(gap_back <= max_speed || is_car_at(pos, lane - 1) || is_barrier_at(pos, lane - 1) || std::find(reserved_pos[lane - 1].begin(), reserved_pos[lane - 1].end(), pos) != reserved_pos[lane - 1].end())
gap_left = 0;
}
if(lane < lanes - 1)
{
while(gap_right++ < max_speed)
if(is_car_at((pos + gap_right) % length, lane + 1) || is_barrier_at((pos + gap_right) % length, lane + 1) || std::find(reserved_pos[lane + 1].begin(), reserved_pos[lane + 1].end(), pos + gap_right) != reserved_pos[lane + 1].end())
break;
--gap_right;
std::size_t gap_back = 0;
while(gap_back++ < max_speed)
{
std::size_t temp_pos = (gap_back <= pos) ? pos - gap_back : length - (gap_back - pos);
if(is_car_at(temp_pos, lane + 1))
break;
else if(is_barrier_at(temp_pos, lane + 1))
{
gap_back = max_speed + 1;
break;
}
}
if(gap_back <= max_speed || is_car_at(pos, lane + 1) || is_barrier_at(pos, lane + 1) || std::find(reserved_pos[lane + 1].begin(), reserved_pos[lane + 1].end(), pos) != reserved_pos[lane + 1].end())
gap_right = 0;
}
if(gap < speed && /*gap_left > speed*/gap_left > gap && randomization(mt) < 100)
{
lane_changes.push_back({{pos, lane}, lane - 1});
reserved_pos[lane - 1].push_back(pos);
//lane_changes[lane - 1].push_back({pos, lane});
if(gap_left < speed)
speed = gap_left;
gap = speed;
}
else if(gap < speed && /*gap_right > speed*/gap_right > gap && randomization(mt) < 100)
{
lane_changes.push_back({{pos, lane}, lane + 1});
reserved_pos[lane + 1].push_back(pos);
//lane_changes[lane + 1].push_back({pos, lane});
if(gap_right < speed)
speed = gap_right;
gap = speed;
}
}
if(gap < speed)
speed = gap;
// random slow down
if(randomization(mt) < slow_down_chance && speed > 0)
--speed;
for(std::size_t i = 0; i <= speed; ++i)
if(is_slow_down_at((pos + i) % length, lane) && speed > 1)
--speed;
set_car_speed(speed, pos, lane);
}
}
for(const auto& a : lane_changes)
{
std::size_t speed = get_speed(a.first.first, a.first.second);
remove_car(a.first.first, a.first.second);
add_car(speed, a.first.first, a.second);
}
}
void Car_System::generate()
{
calculate();
// update pos
for(std::size_t lane = 0; lane < lanes; ++lane)
{
for(std::size_t pos = 0; pos < length; ++pos)
{
// skip empty cells
if(!is_car_at(pos, lane))
continue;
std::size_t new_pos = (pos + get_speed(pos, lane)) % length;
system[new_pos + lane * length] = get_speed(pos, lane) | ((is_slow_down_at(new_pos, lane)) ? SLOW_DOWN_BIT : 0x00);
if(pos != new_pos)
remove_car(pos, lane);
if(new_pos < pos)
break;
pos = new_pos;
}
}
}
void Car_System::create(std::size_t length, std::size_t lanes, std::size_t car_density, std::size_t max_speed, std::size_t slow_down_chance)
{
this->length = length;
this->lanes = lanes;
this->car_density = car_density;
this->max_speed = max_speed;
this->slow_down_chance = slow_down_chance;
system.resize(length * lanes);
// random machine
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<std::size_t> dist(0, 99);
// generate new cars
for(std::size_t lane = 0; lane < this->lanes; ++lane)
{
for(std::size_t pos = 0; pos < this->length; ++pos)
{
std::size_t random = dist(mt);
if(random < this->car_density)
system[pos + lane * length] = random % (this->max_speed + 1);
else
system[pos + lane * length] = EMPTY;
}
}
}
void Car_System::reset()
{
std::fill(system.begin(), system.end(), EMPTY);
}
void Car_System::add_car(std::size_t speed, std::size_t pos, std::size_t lane)
{
system[pos + lane * length] = speed | ((is_slow_down_at(pos, lane)) ? SLOW_DOWN_BIT : 0x00);
}
void Car_System::remove_car(std::size_t pos, std::size_t lane)
{
system[pos + lane * length] = EMPTY | ((is_slow_down_at(pos, lane)) ? SLOW_DOWN_BIT : 0x00);
}
void Car_System::set_car_speed(std::size_t speed, std::size_t pos, std::size_t lane)
{
if(is_car_at(pos, lane))
{
system[pos + lane * length] = speed | ((is_slow_down_at(pos, lane)) ? SLOW_DOWN_BIT : 0x00);
}
}
void Car_System::add_barrier(std::size_t pos, std::size_t lane)
{
system[pos + lane * length] = BARRIER | ((is_slow_down_at(pos, lane)) ? SLOW_DOWN_BIT : 0x00);
}
void Car_System::remove_barrier(std::size_t pos, std::size_t lane)
{
system[pos + lane * length] = EMPTY | ((is_slow_down_at(pos, lane)) ? SLOW_DOWN_BIT : 0x00);
}
void Car_System::add_slow_down(std::size_t pos, std::size_t lane)
{
system[pos + lane * length] = set_slow_down_bit(system[pos + lane * length]);
}
void Car_System::remove_slow_down(std::size_t pos, std::size_t lane)
{
system[pos + lane * length] = clear_slow_down_bit(system[pos + lane * length]);
}
double Car_System::get_avg_speed(std::size_t lane)
{
auto sum_lane = [this](std::size_t lane, std::size_t& avg_speed, std::size_t& cars)
{
for(std::size_t pos = 0; pos < length; ++pos)
{
// skip empty cells
if(!is_car_at(pos, lane))
continue;
avg_speed += system[pos + lane * length];
++cars;
}
};
std::size_t avg_speed = 0;
std::size_t cars = 0;
if(lane == std::numeric_limits<std::size_t>::max())
for(lane = 0; lane < lanes; ++lane)
sum_lane(lane, avg_speed, cars);
else
sum_lane(lane, avg_speed, cars);
return (cars != 0) ? (static_cast<double>(avg_speed) / cars) : 0;
}
std::size_t Car_System::get_car_amount()
{
std::size_t cars = 0;
for(std::size_t lane = 0; lane < lanes; ++lane)
for(std::size_t pos = 0; pos < length; ++pos)
if(is_car_at(pos, lane))
++cars;
return cars;
}
std::size_t Car_System::get_speed(std::size_t pos, std::size_t lane)
{
std::size_t speed = clear_slow_down_bit(system[pos + lane * length]);
return (speed != EMPTY && speed != BARRIER) ? speed : 0;
}
bool Car_System::is_car_at(std::size_t pos, std::size_t lane)
{
if(clear_slow_down_bit(system[pos + lane * length]) != EMPTY && clear_slow_down_bit(system[pos + lane * length]) != BARRIER)
return true;
return false;
}
bool Car_System::is_slow_down_at(std::size_t pos, std::size_t lane)
{
return system[pos + lane * length] & SLOW_DOWN_BIT;
}
bool Car_System::is_barrier_at(std::size_t pos, std::size_t lane)
{
return (clear_slow_down_bit(system[pos + lane * length]) == BARRIER);
}