8 use Time::HiRes qw(gettimeofday);
9 use Glib qw(TRUE FALSE);
12 our $SEMAPHORE_STEP = 1000;
15 my ($class, $args) = @_;
23 qualification_setup => -100,
29 $self->{cars}->[$i] = SCX::Car->new({
37 $self->{gui}->time(undef);
38 $self->{gui}->best_lap(undef, undef);
43 sub car { return shift->{cars}->[shift]; }
46 my ($self, $time) = @_;
48 return if $self->{race_running} || $self->{start_in_progress}
49 || $self->{qualification_running};
51 if ($time - $self->{qualification_setup} < 1) {
52 $self->{qualification_running} = 1;
55 $self->{race_running} = 0;
56 $self->{start_in_progress} = 1;
57 $self->{semaphore} = 0;
58 $self->{gui}->show_semaphore(0);
59 Glib::Timeout->add($SEMAPHORE_STEP, \&semaphore_step, $self);
67 return FALSE if !$self->{start_in_progress} && !$self->{race_running};
70 if ($self->{semaphore} <= 5) {
71 $self->{gui}->show_semaphore($self->{semaphore});
72 my $timeout = $SEMAPHORE_STEP;
73 $timeout += $SEMAPHORE_STEP * rand()
74 if $self->{semaphore} == 5;
75 Glib::Timeout->add($timeout, \&semaphore_step, $self);
76 } elsif ($self->{semaphore} == 6) {
77 $self->{race_running} = 1;
78 $self->{race_running_since} = gettimeofday;
79 $self->{start_in_progress} = undef;
80 $self->{gui}->show_semaphore(0);
81 Glib::Timeout->add($SEMAPHORE_STEP, \&semaphore_step, $self);
83 $self->{gui}->show_semaphore(undef);
84 $self->{semaphore} = undef;
92 $self->{race_running} = 0;
96 my ($self, $rounds, $time) = @_;
98 if ($time - $self->{qualification_setup} < 1) {
102 $self->{race_rounds} = $rounds;
104 $self->{race_rounds} = 0;
110 $self->{best_lap} = undef;
112 $self->{gui}->show_semaphore(undef);
113 $self->{race_running} = 0;
114 $self->{qualification_running} = 0;
115 $self->{start_in_progress} = 0;
117 $self->{gui}->time(undef);
118 $self->{gui}->best_lap(undef);
124 $self->{race_running} = 0;
125 $self->{qualification_running} = 0;
126 $self->{start_in_progress} = 0;
127 $self->{race_finishing} = 0;
128 $self->{best_lap} = undef;
132 $self->{gui}->best_lap(undef);
133 $self->{gui}->time(undef);
136 $self->car($car)->reset;
144 if ($self->{qualification_running}
145 || $self->{now} - $self->{qualification_setup} < 1) {
146 $msg = 'Qualification: ' . $self->{race_rounds}
147 . ($self->{race_rounds} == 1 ? ' round' : ' rounds');
148 } elsif ($self->{race_rounds}) {
149 $msg = $self->{round} . '/' . $self->{race_rounds};
151 $msg = $self->{round};
154 $self->{gui}->rounds($msg);
158 my ($self, $time, $who) = @_;
160 return if !defined $time || $time == 0;
162 # print "Check_best_lap $time $who vs ",
163 # defined $self->{best_lap} ? $self->{best_lap} : 'undef',
165 if (!defined $self->{best_lap}
166 || $time < $self->{best_lap}) {
167 $self->{best_lap} = $time;
168 $self->{gui}->best_lap($time, $who);
174 sub qualification_setup {
175 my ($self, $rounds, $cars, $time) = @_;
177 return if $self->{qualification_running};
180 $self->car($car)->set_lap(undef);
181 $self->car($car)->set_laptime(undef);
184 $self->{qualification_setup} = $time;
185 $self->{race_rounds} = $rounds;
186 $self->{qualification_cars} = $cars;
187 $self->{gui}->time(undef);
188 $self->{gui}->best_lap(undef);
192 sub packet_received {
193 my ($self, $time) = @_;
195 $self->{now} = $time;
197 if ($self->{race_running}) {
198 $self->{gui}->time($time - $self->{race_running_since});
203 my ($self, $now) = @_;
205 return if !$self->{race_running};
210 $laps[$id] = $self->car($id)->{lap} // -1;
211 $times[$id] = $self->car($id)->{first_finish_time} // $now;
214 my @new_order = sort {
215 $laps[$b] <=> $laps[$a]
217 $times[$a] <=> $times[$b]
222 my $lap_max = $laps[$new_order[0]];
223 my $lap_max_changed = 0;
224 if (defined $lap_max && defined $self->{round}
225 && $lap_max != $self->{round}) {
226 $self->{round} = $lap_max;
227 $lap_max_changed = 1;
231 if ($self->{round} && $self->{race_rounds}
232 && $self->{round} > $self->{race_rounds}) {
233 $self->{race_finishing} = 1;
237 my $car = $new_order[$id];
238 if ($self->car($car)->{order} != $id) {
239 $self->car($car)->set_order($id);
242 return ($lap_max_changed, $lap_max, $times[$new_order[0]]);
245 sub recalc_qual_order {
248 return if !$self->{qualification_running};
252 $times[$id] = $self->car($id)->{best_lap};
253 if (!defined $times[$id] || $times[$id] <= 0) {
254 $times[$id] = 999_999;
258 my @new_order = sort {
259 $times[$a] <=> $times[$b]
264 my $best_time = $times[$new_order[0]];
267 my $car = $new_order[$id];
268 if ($self->car($car)->{order} != $id) {
269 $self->car($car)->set_order($id);
272 return ($times[$new_order[0]]);
276 my ($self, $time, $regular, @cars) = @_;
281 for my $car (@cars) {
282 if ($self->car($car)->finish_line($time, $regular)) {
283 $processed{$car} = 1;
288 return if !$was_processed;
290 if ($self->{qualification_running}) {
291 my ($best) = $self->recalc_qual_order;
293 $self->car($car)->recalc_qual_distance($best);
298 my ($lap_max_changed, $lap_max, $time_min)
299 = $self->recalc_order($time);
302 if ($processed{$car}) {
303 $self->car($car)->recalc_distance(
304 $lap_max, $time_min, $self->{race_finishing},
306 } elsif ($lap_max_changed) {
307 $self->car($car)->greyout_distance;