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 if (defined $lap_time) {
132 if ($lap_time > 1.0 && !$self->{in_pit_lane}
133 && (!defined $self->{best_lap}
134 || $self->{best_lap} > $lap_time
137 $self->{best_lap} = $lap_time;
138 my $global = $self->{track}->check_best_lap($lap_time,
141 $self->gui->set_best_lap($self->{order}, $lap_time,
148 my ($self, $driver_name) = @_;
150 return if $self->same('driver', $driver_name);
151 $self->gui->set_name($self->{order}, $driver_name);
157 return if $self->{in_pit_lane};
159 $self->{in_pit_lane} = 1;
166 return if !$self->{in_pit_lane};
168 $self->{in_pit_lane} = 0;
173 my ($self, $pos) = @_;
175 $self->{order} = $pos;
177 $self->gui->set_name($self->{order}, $self->{driver});
178 $self->gui->set_car_icon($self->{order}, $self->{car_img});
179 $self->gui->set_throttle($self->{order}, $self->{throttle},
181 $self->gui->set_lap($self->{order}, $self->{lap});
182 $self->gui->set_laptime($self->{order}, $self->{laptime});
183 $self->gui->set_best_lap($self->{order}, $self->{best_lap});
184 $self->gui->set_avg_lap($self->{order}, $self->{avg_lap});
185 $self->gui->set_fuel($self->{order}, $self->{fuel});
186 $self->gui->set_car_id($self->{order}, $self->{id} + 1);
187 $self->gui->set_distance($self->{order},
188 $self->{time_diff}, $self->{lap_diff}, $self->{grey_diff});
195 if ($self->{in_pit_lane}) {
196 $self->{state} = 'pit';
197 } elsif ($self->{finished}) {
198 $self->{state} = 'finished';
199 } elsif ($self->{early_start}) {
200 $self->{state} = 'disqualified';
202 $self->{state} = 'greenflag';
205 $self->gui->set_state($self->{order}, $self->{state});
212 $self->set_laptime(undef);
213 $self->{finished} = 0;
214 $self->{in_pit_lane} = 0;
215 $self->{early_start} = undef;
216 $self->{last_finish_time} = undef;
217 $self->{first_finish_time} = undef;
218 $self->{time_diff} = undef;
219 $self->{lap_diff} = undef;
220 $self->{total_time} = undef;
221 $self->{grey_diff} = undef;
225 my ($self, $time, $regular) = @_;
227 if (defined $self->{last_finish_time}
228 && $time - $self->{last_finish_time} < 2) {
230 $self->{last_finish_time} = $time;
231 if ($regular && $self->{in_pit_lane}) {
232 $self->leave_pit_lane;
238 if (defined $self->{first_finish_time}) {
239 $self->set_laptime($time - $self->{first_finish_time});
242 $self->enter_pit_lane;
245 $self->{first_finish_time} = $self->{last_finish_time} = $time;
247 if ($self->track->{race_running}) {
248 $self->set_lap($self->{lap} + 1);
254 sub recalc_distance {
255 my ($self, $lap_first, $time_first, $finishing) = @_;
257 return if !defined $lap_first || !defined $self->{lap}
258 || !defined $time_first || !defined $self->{first_finish_time};
260 $self->{total_time} = undef;
261 $self->{lap_diff} = undef;
262 $self->{time_diff} = undef;
263 $self->{grey_diff} = undef;
265 if ($self->{lap} == $lap_first) {
266 if ($self->{first_finish_time} == $time_first) {
267 $self->{total_time} = $self->{first_finish_time}
268 - $self->track->{race_running_since};
270 $self->{time_diff} = $self->{first_finish_time}
274 $self->{lap_diff} = $lap_first - $self->{lap};
277 $self->gui->set_distance($self->{order}, $self->{total_time},
278 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});
281 $self->{finished} = 1;
286 sub greyout_distance {
289 return if $self->{grey_diff};
290 $self->{grey_diff} = 1;
291 $self->gui->set_distance($self->{order}, $self->{total_time},
292 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});