8 use Glib qw(TRUE FALSE);
11 our $SEMAPHORE_STEP = 1000;
14 my ($class, $args) = @_;
22 qualification_setup => -100,
23 no_semaphore => $args->{no_semaphore},
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;
53 } elsif ($self->{no_semaphore}) {
55 $self->{race_running} = 1;
56 $self->{race_running_since} = $self->{now};
57 $self->{start_in_progress} = undef;
60 $self->{race_running} = 0;
61 $self->{start_in_progress} = 1;
62 $self->{semaphore} = 0;
63 $self->{gui}->show_semaphore(0);
64 Glib::Timeout->add($SEMAPHORE_STEP, \&semaphore_step, $self);
72 return FALSE if !$self->{start_in_progress} && !$self->{race_running};
75 if ($self->{semaphore} <= 5) {
76 $self->{gui}->show_semaphore($self->{semaphore});
77 my $timeout = $SEMAPHORE_STEP;
78 $timeout += $SEMAPHORE_STEP * rand()
79 if $self->{semaphore} == 5;
80 Glib::Timeout->add($timeout, \&semaphore_step, $self);
81 } elsif ($self->{semaphore} == 6) {
82 $self->{race_running} = 1;
83 $self->{race_running_since} = $self->{now};
84 $self->{start_in_progress} = undef;
85 $self->{gui}->show_semaphore(0);
86 Glib::Timeout->add($SEMAPHORE_STEP, \&semaphore_step, $self);
88 $self->{gui}->show_semaphore(undef);
89 $self->{semaphore} = undef;
97 $self->{race_running} = 0;
101 my ($self, $rounds, $time) = @_;
103 if ($time - $self->{qualification_setup} < 1) {
107 $self->{race_rounds} = $rounds;
109 $self->{race_rounds} = 0;
115 $self->{best_lap} = undef;
117 $self->{gui}->show_semaphore(undef);
118 $self->{race_running} = 0;
119 $self->{qualification_running} = 0;
120 $self->{start_in_progress} = 0;
121 $self->{race_finishing} = 0;
123 $self->{gui}->time(undef);
124 $self->{gui}->best_lap(undef);
127 $self->car($car)->reset;
135 if ($self->{qualification_running}
136 || $self->{now} - $self->{qualification_setup} < 1) {
137 $msg = 'Qualification: ' . $self->{race_rounds}
138 . ($self->{race_rounds} == 1 ? ' round' : ' rounds');
139 } elsif ($self->{race_rounds}) {
140 $msg = $self->{round} . '/' . $self->{race_rounds};
142 $msg = $self->{round};
145 $self->{gui}->rounds($msg);
148 sub notify_best_lap {
149 my ($self, $id, $time, $who) = @_;
151 return if !defined $time || $time == 0;
153 # print "Check_best_lap $time $who vs ",
154 # defined $self->{best_lap} ? $self->{best_lap} : 'undef',
156 if (!defined $self->{best_lap}
157 || $time < $self->{best_lap}) {
158 $self->{best_lap} = $time;
159 $self->{gui}->best_lap($time, $who);
162 $self->car($car)->set_global_best($car == $id);
163 $self->car($car)->print_best_lap;
170 sub qualification_setup {
171 my ($self, $rounds, $cars, $time) = @_;
173 return if $self->{qualification_running};
176 $self->car($car)->set_lap(undef);
177 $self->car($car)->set_laptime(undef);
180 $self->{qualification_setup} = $time;
181 $self->{race_rounds} = $rounds;
182 $self->{qualification_cars} = $cars;
183 $self->{gui}->time(undef);
184 $self->{gui}->best_lap(undef);
188 sub packet_received {
189 my ($self, $time) = @_;
191 $self->{now} = $time;
193 if ($self->{race_running}) {
194 $self->{gui}->time($time - $self->{race_running_since});
199 my ($self, $now) = @_;
201 return if !$self->{race_running};
206 $laps[$id] = $self->car($id)->{lap} // -1;
207 $times[$id] = $self->car($id)->{first_finish_time} // $now;
210 my @new_order = sort {
211 $laps[$b] <=> $laps[$a]
213 $times[$a] <=> $times[$b]
218 my $lap_max = $laps[$new_order[0]];
219 my $lap_max_changed = 0;
220 if (defined $lap_max && defined $self->{round}
221 && $lap_max != $self->{round}) {
222 $self->{round} = $lap_max;
223 $lap_max_changed = 1;
227 if ($self->{round} && $self->{race_rounds}
228 && $self->{round} > $self->{race_rounds}) {
229 $self->{race_finishing} = 1;
233 my $car = $new_order[$id];
234 if ($self->car($car)->{order} != $id) {
235 $self->car($car)->set_order($id);
238 return ($lap_max_changed, $lap_max, $times[$new_order[0]]);
241 sub recalc_qual_order {
244 return if !$self->{qualification_running};
248 $times[$id] = $self->car($id)->{best_lap};
249 if (!defined $times[$id] || $times[$id] <= 0) {
250 $times[$id] = 999_999;
254 my @new_order = sort {
255 $times[$a] <=> $times[$b]
260 my $best_time = $times[$new_order[0]];
263 my $car = $new_order[$id];
264 if ($self->car($car)->{order} != $id) {
265 $self->car($car)->set_order($id);
268 return ($times[$new_order[0]]);
272 my ($self, $time, $regular, @cars) = @_;
277 for my $car (@cars) {
278 if ($self->car($car)->finish_line($time, $regular)) {
279 $processed{$car} = 1;
284 return if !$was_processed;
286 if ($self->{qualification_running}) {
287 my ($best) = $self->recalc_qual_order;
289 $self->car($car)->recalc_qual_distance($best);
294 my ($lap_max_changed, $lap_max, $time_min)
295 = $self->recalc_order($time);
298 if ($processed{$car}) {
299 $self->car($car)->recalc_distance(
300 $lap_max, $time_min, $self->{race_finishing},
302 } elsif ($lap_max_changed) {
303 $self->car($car)->greyout_distance;