8 my ($class, $args) = @_;
12 driver => $args->{driver} || '--',
18 car_img => $args->{car_img},
20 track => $args->{track},
25 $self->set_order($self->{id});
30 sub gui { return shift->{gui}; }
32 sub track { return shift->{track}; }
35 my ($self, $key, $val) = @_;
37 return 1 if !defined $self->{$key} && !defined $val
38 || (defined $self->{$key} && defined $val
39 && $self->{$key} eq $val);
46 my ($self, $val, $button, $time) = @_;
48 return if $self->same('throttle', $val)
49 && $self->same('button', $button);
51 if ($self->track->{start_in_progress} && $val) {
52 $self->{early_start} = 1;
53 $self->gui->set_name($self->{order}, 'Early start!');
56 if ($self->{in_pit_lane} && defined $val) {
57 $self->{last_finish_time} = $time;
59 $self->leave_pit_lane;
63 $self->gui->set_throttle($self->{order}, $val, $button);
68 my ($self, $val) = @_;
70 my $prev = $self->{fuel};
71 return if $self->same('fuel', $val);
72 if (defined $val && defined $prev && $val < 8) {
73 if ($val == $prev + 1 && !$self->{in_pit_lane}) {
74 print STDERR $self->track->{now}, ' car ',
75 $self->{id}, ' missed pit lane entry', "\n";
76 $self->enter_pit_lane;
78 if ($val == $prev - 1 && $val <= 2 && !$self->{in_pit_lane}) {
79 $self->track->{sound}->box($self->{id});
82 if (defined $val && defined $prev && $val == 8 && $prev == 7
83 && $self->{in_pit_lane}) {
84 $self->track->{sound}->filled($self->{id});
87 $self->gui->set_fuel($self->{order}, $val);
99 my ($self, $name) = @_;
101 return if $self->same('car_img', $name);
102 $self->gui->set_car_icon($self->{order}, $name);
106 my ($self, $lap_nr) = @_;
109 $self->{lap} = $lap_nr;
110 $self->gui->set_lap($self->{order}, $lap_nr);
114 return if $self->same('lap', $lap_nr);
115 if (defined $self->{lap} && defined $self->track->{race_rounds}
116 && $self->track->{race_rounds} > 0
117 && $self->{lap} > $self->track->{race_rounds}) {
118 $self->{finished} = 1;
121 $self->gui->set_lap($self->{order}, $lap_nr);
122 if ($self->track->{race_running} && $self->{lap} > 1) {
123 my $now = $self->{last_finish_time};
128 my ($self, $lap_time) = @_;
130 if (!defined $lap_time) {
131 $self->{best_lap} = undef;
132 $self->{global_best_lap} = undef;
133 $self->print_best_lap;
136 return if $self->same('laptime', $lap_time);
138 $self->gui->set_laptime($self->{order}, $lap_time);
140 return if !defined $lap_time || $lap_time <= 1.0
141 || $self->{in_pit_lane};
143 return if $self->{finished};
145 return if defined $self->{best_lap} && $self->{best_lap} <= $lap_time;
147 $self->{best_lap} = $lap_time;
148 $self->print_best_lap;
149 $self->{track}->notify_best_lap($self->{id}, $lap_time,
155 $self->gui->set_best_lap($self->{order}, $self->{best_lap},
156 $self->{global_best_lap});
159 sub set_global_best {
160 my ($self, $val) = @_;
162 $self->{global_best_lap} = $val ? 1 : undef;
166 my ($self, $driver_name) = @_;
168 return if $self->same('driver', $driver_name);
169 $self->gui->set_name($self->{order}, $driver_name);
175 return if $self->{in_pit_lane};
177 $self->{in_pit_lane} = 1;
184 return if !$self->{in_pit_lane};
186 $self->{in_pit_lane} = 0;
191 my ($self, $pos) = @_;
193 $self->{order} = $pos;
195 $self->gui->set_name($self->{order}, $self->{driver});
196 $self->gui->set_car_icon($self->{order}, $self->{car_img});
197 $self->gui->set_throttle($self->{order}, $self->{throttle},
199 $self->gui->set_lap($self->{order}, $self->{lap});
200 $self->gui->set_laptime($self->{order}, $self->{laptime});
201 $self->gui->set_fuel($self->{order}, $self->{fuel});
202 $self->gui->set_car_id($self->{order}, $self->{id} + 1);
203 $self->gui->set_distance($self->{order},
204 $self->{time_diff}, $self->{lap_diff}, $self->{grey_diff});
206 $self->print_best_lap;
212 if ($self->{in_pit_lane}) {
213 $self->{state} = 'pit';
214 } elsif ($self->{finished}) {
215 $self->{state} = 'finished';
216 } elsif ($self->{early_start}) {
217 $self->{state} = 'disqualified';
219 $self->{state} = 'greenflag';
222 $self->gui->set_state($self->{order}, $self->{state});
229 $self->set_laptime(undef);
230 $self->{finished} = 0;
231 $self->{in_pit_lane} = 0;
232 $self->{early_start} = undef;
233 $self->{last_finish_time} = undef;
234 $self->{first_finish_time} = undef;
235 $self->{time_diff} = undef;
236 $self->{lap_diff} = undef;
237 $self->{total_time} = undef;
238 $self->{grey_diff} = undef;
243 my ($self, $time, $regular) = @_;
245 if (defined $self->{last_finish_time}
246 && $time - $self->{last_finish_time} < 2) {
248 $self->{last_finish_time} = $time;
249 if ($regular && $self->{in_pit_lane}) {
250 $self->leave_pit_lane;
256 if (defined $self->{first_finish_time}) {
257 $self->set_laptime($time - $self->{first_finish_time});
260 $self->enter_pit_lane;
263 $self->{first_finish_time} = $self->{last_finish_time} = $time;
265 if ($self->track->{race_running}) {
266 $self->set_lap($self->{lap} + 1);
267 } elsif ($self->track->{qualification_running} && !$self->{finished}) {
268 $self->set_lap($self->{lap} + 1);
269 if ($self->{lap} > $self->track->{race_rounds}) {
270 $self->{finished} = 1;
278 sub recalc_distance {
279 my ($self, $lap_first, $time_first, $finishing) = @_;
281 return if !defined $lap_first || !defined $self->{lap}
282 || !defined $time_first || !defined $self->{first_finish_time};
284 $self->{total_time} = undef;
285 $self->{lap_diff} = undef;
286 $self->{time_diff} = undef;
287 $self->{grey_diff} = undef;
289 if ($self->{lap} == $lap_first) {
290 if ($self->{first_finish_time} == $time_first) {
291 $self->{total_time} = $self->{first_finish_time}
292 - $self->track->{race_running_since};
294 $self->{time_diff} = $self->{first_finish_time}
298 $self->{lap_diff} = $lap_first - $self->{lap};
301 $self->gui->set_distance($self->{order}, $self->{total_time},
302 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});
305 $self->{finished} = 1;
310 sub recalc_qual_distance {
311 my ($self, $time_first) = @_;
313 return if !defined $self->{best_lap} || $self->{best_lap} <= 0;
315 $self->{total_time} = undef;
316 $self->{time_diff} = undef;
317 $self->{grey_diff} = undef;
318 $self->{lap_diff} = undef;
320 if ($self->{best_lap} == $time_first) {
321 $self->{total_time} = $self->{best_lap};
323 $self->{time_diff} = $self->{best_lap} - $time_first;
326 $self->gui->set_distance($self->{order}, $self->{total_time},
327 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});
330 sub greyout_distance {
333 return if $self->{grey_diff};
334 $self->{grey_diff} = 1;
335 $self->gui->set_distance($self->{order}, $self->{total_time},
336 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});