6 use Time::HiRes qw(gettimeofday);
9 my ($class, $args) = @_;
13 driver => $args->{driver} || '--',
20 car_img => $args->{car_img},
22 track => $args->{track},
27 $self->set_order($self->{id});
32 sub gui { return shift->{gui}; }
34 sub track { return shift->{track}; }
37 my ($self, $key, $val) = @_;
39 return 1 if !defined $self->{$key} && !defined $val
40 || (defined $self->{$key} && defined $val
41 && $self->{$key} eq $val);
48 my ($self, $val, $button, $time) = @_;
50 return if $self->same('throttle', $val)
51 && $self->same('button', $button);
53 if ($self->track->{start_in_progress} && $val) {
54 $self->{early_start} = 1;
55 $self->gui->set_name($self->{order}, 'Early start!');
58 if ($self->{in_pit_lane} && defined $val) {
59 $self->{last_finish_time} = $time;
61 $self->leave_pit_lane;
65 $self->gui->set_throttle($self->{order}, $val, $button);
70 my ($self, $val) = @_;
72 return if $self->same('fuel', $val);
73 $self->gui->set_fuel($self->{order}, $val);
85 my ($self, $name) = @_;
87 return if $self->same('car_img', $name);
88 $self->gui->set_car_icon($self->{order}, $name);
92 my ($self, $lap_nr) = @_;
95 $self->{avg_lap} = undef;
96 $self->{lap} = $lap_nr;
97 $self->gui->set_avg_lap($self->{order}, undef);
98 $self->gui->set_lap($self->{order}, $lap_nr);
102 return if $self->same('lap', $lap_nr);
103 if (defined $self->{lap} && defined $self->track->{race_rounds}
104 && $self->track->{race_rounds} > 0
105 && $self->{lap} > $self->track->{race_rounds}) {
106 $self->{finished} = 1;
109 $self->gui->set_lap($self->{order}, $lap_nr);
110 if ($self->track->{race_running} && $self->{lap} > 1) {
111 my $now = $self->{last_finish_time};
112 my $avg = ($now - $self->track->{race_running_since})
113 / ($self->{lap} - 1);
114 $self->{avg_lap} = $avg;
115 $self->gui->set_avg_lap($self->{order}, $self->{avg_lap});
120 my ($self, $lap_time) = @_;
122 if (!defined $lap_time) {
123 $self->gui->set_best_lap($self->{order}, undef);
124 $self->{best_lap} = undef;
127 return if $self->same('laptime', $lap_time);
129 $self->gui->set_laptime($self->{order}, $lap_time);
131 return if !defined $lap_time || $lap_time <= 1.0
132 || $self->{in_pit_lane};
134 return if $self->{finished};
136 return if defined $self->{best_lap} && $self->{best_lap} <= $lap_time;
138 $self->{best_lap} = $lap_time;
139 my $global = $self->{track}->check_best_lap($lap_time,
142 $self->gui->set_best_lap($self->{order}, $lap_time, $global);
146 my ($self, $driver_name) = @_;
148 return if $self->same('driver', $driver_name);
149 $self->gui->set_name($self->{order}, $driver_name);
155 return if $self->{in_pit_lane};
157 $self->{in_pit_lane} = 1;
164 return if !$self->{in_pit_lane};
166 $self->{in_pit_lane} = 0;
171 my ($self, $pos) = @_;
173 $self->{order} = $pos;
175 $self->gui->set_name($self->{order}, $self->{driver});
176 $self->gui->set_car_icon($self->{order}, $self->{car_img});
177 $self->gui->set_throttle($self->{order}, $self->{throttle},
179 $self->gui->set_lap($self->{order}, $self->{lap});
180 $self->gui->set_laptime($self->{order}, $self->{laptime});
181 $self->gui->set_best_lap($self->{order}, $self->{best_lap});
182 $self->gui->set_avg_lap($self->{order}, $self->{avg_lap});
183 $self->gui->set_fuel($self->{order}, $self->{fuel});
184 $self->gui->set_car_id($self->{order}, $self->{id} + 1);
185 $self->gui->set_distance($self->{order},
186 $self->{time_diff}, $self->{lap_diff}, $self->{grey_diff});
193 if ($self->{in_pit_lane}) {
194 $self->{state} = 'pit';
195 } elsif ($self->{finished}) {
196 $self->{state} = 'finished';
197 } elsif ($self->{early_start}) {
198 $self->{state} = 'disqualified';
200 $self->{state} = 'greenflag';
203 $self->gui->set_state($self->{order}, $self->{state});
210 $self->set_laptime(undef);
211 $self->{finished} = 0;
212 $self->{in_pit_lane} = 0;
213 $self->{early_start} = undef;
214 $self->{last_finish_time} = undef;
215 $self->{first_finish_time} = undef;
216 $self->{time_diff} = undef;
217 $self->{lap_diff} = undef;
218 $self->{total_time} = undef;
219 $self->{grey_diff} = undef;
223 my ($self, $time, $regular) = @_;
225 if (defined $self->{last_finish_time}
226 && $time - $self->{last_finish_time} < 2) {
228 $self->{last_finish_time} = $time;
229 if ($regular && $self->{in_pit_lane}) {
230 $self->leave_pit_lane;
236 if (defined $self->{first_finish_time}) {
237 $self->set_laptime($time - $self->{first_finish_time});
240 $self->enter_pit_lane;
243 $self->{first_finish_time} = $self->{last_finish_time} = $time;
245 if ($self->track->{race_running}) {
246 $self->set_lap($self->{lap} + 1);
247 } elsif ($self->track->{qualification_running} && !$self->{finished}) {
248 $self->set_lap($self->{lap} + 1);
249 if ($self->{lap} > $self->track->{race_rounds}) {
250 $self->{finished} = 1;
258 sub recalc_distance {
259 my ($self, $lap_first, $time_first, $finishing) = @_;
261 return if !defined $lap_first || !defined $self->{lap}
262 || !defined $time_first || !defined $self->{first_finish_time};
264 $self->{total_time} = undef;
265 $self->{lap_diff} = undef;
266 $self->{time_diff} = undef;
267 $self->{grey_diff} = undef;
269 if ($self->{lap} == $lap_first) {
270 if ($self->{first_finish_time} == $time_first) {
271 $self->{total_time} = $self->{first_finish_time}
272 - $self->track->{race_running_since};
274 $self->{time_diff} = $self->{first_finish_time}
278 $self->{lap_diff} = $lap_first - $self->{lap};
281 $self->gui->set_distance($self->{order}, $self->{total_time},
282 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});
285 $self->{finished} = 1;
290 sub recalc_qual_distance {
291 my ($self, $time_first) = @_;
293 return if !defined $self->{best_lap} || $self->{best_lap} <= 0;
295 $self->{total_time} = undef;
296 $self->{time_diff} = undef;
297 $self->{grey_diff} = undef;
298 $self->{lap_diff} = undef;
300 if ($self->{best_lap} == $time_first) {
301 $self->{total_time} = $self->{best_lap};
303 $self->{time_diff} = $self->{best_lap} - $time_first;
306 $self->gui->set_distance($self->{order}, $self->{total_time},
307 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});
310 sub greyout_distance {
313 return if $self->{grey_diff};
314 $self->{grey_diff} = 1;
315 $self->gui->set_distance($self->{order}, $self->{total_time},
316 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});