]> www.fi.muni.cz Git - slotcarman.git/blob - SCX/Car.pm
First implementation of qualification.
[slotcarman.git] / SCX / Car.pm
1 #!/usr/bin/perl -w
2
3 package SCX::Car;
4
5 use strict;
6 use Time::HiRes qw(gettimeofday);
7
8 sub new {
9         my ($class, $args) = @_;
10
11         my $self = {
12                 gui      => $args->{gui},
13                 driver   => $args->{driver} || '--',    
14                 throttle => undef,
15                 button   => undef,
16                 fuel     => 0,
17                 lap      => 0,
18                 laptime  => 0,
19                 avg_lap  => 0,
20                 car_img  => $args->{car_img},
21                 id       => $args->{id},
22                 track    => $args->{track},
23         };
24
25         bless $self, $class;
26
27         $self->set_order($self->{id});
28
29         return $self;
30 }
31
32 sub gui { return shift->{gui}; }
33
34 sub track { return shift->{track}; }
35
36 sub same {
37         my ($self, $key, $val) = @_;
38
39         return 1 if !defined $self->{$key} && !defined $val
40                 || (defined $self->{$key} && defined $val
41                         && $self->{$key} eq $val);
42
43         $self->{$key} = $val;
44         return undef;
45 }
46
47 sub set_throttle {
48         my ($self, $val, $button, $time) = @_;
49
50         return if $self->same('throttle', $val)
51                 && $self->same('button', $button);
52
53         if ($self->track->{start_in_progress} && $val) {
54                 $self->{early_start} = 1;
55                 $self->gui->set_name($self->{order}, 'Early start!');
56         }
57
58         if ($self->{in_pit_lane} && defined $val) {
59                 $self->{last_finish_time} = $time;
60                 if ($val > 3) {
61                         $self->leave_pit_lane;
62                 }
63         }
64
65         $self->gui->set_throttle($self->{order}, $val, $button);
66 }
67
68
69 sub set_fuel {
70         my ($self, $val) = @_;
71
72         return if $self->same('fuel', $val);
73         $self->gui->set_fuel($self->{order}, $val);
74 }
75
76 sub set_light {
77         # TODO
78 }
79
80 sub set_backbutton {
81         # TODO
82 }
83
84 sub set_model {
85         my ($self, $name) = @_;
86
87         return if $self->same('car_img', $name);
88         $self->gui->set_car_icon($self->{order}, $name);
89 }
90
91 sub set_lap {
92         my ($self, $lap_nr) = @_;
93
94         if (!$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);
99                 return;
100         }
101                 
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;
107                 $self->print_state;
108         }
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});
116         }
117 }
118
119 sub set_laptime {
120         my ($self, $lap_time) = @_;
121
122         if (!defined $lap_time) {
123                 $self->gui->set_best_lap($self->{order}, undef);
124                 $self->{best_lap} = undef;
125         }
126
127         return if $self->same('laptime', $lap_time);
128
129         $self->gui->set_laptime($self->{order}, $lap_time);
130
131         return if !defined $lap_time || $lap_time <= 1.0
132                 || $self->{in_pit_lane};
133
134         return if $self->{finished};
135
136         return if defined $self->{best_lap} && $self->{best_lap} <= $lap_time;
137
138         $self->{best_lap} = $lap_time;
139         my $global = $self->{track}->check_best_lap($lap_time,
140                 $self->{driver});
141
142         $self->gui->set_best_lap($self->{order}, $lap_time, $global);
143 }
144
145 sub set_driver {
146         my ($self, $driver_name) = @_;
147
148         return if $self->same('driver', $driver_name);
149         $self->gui->set_name($self->{order}, $driver_name);
150 }
151
152 sub enter_pit_lane {
153         my ($self) = @_;
154
155         return if $self->{in_pit_lane};
156
157         $self->{in_pit_lane} = 1;
158         $self->print_state;
159 }
160
161 sub leave_pit_lane {
162         my ($self) = @_;
163
164         return if !$self->{in_pit_lane};
165
166         $self->{in_pit_lane} = 0;
167         $self->print_state;
168 }
169
170 sub set_order {
171         my ($self, $pos) = @_;
172
173         $self->{order} = $pos;
174         
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},
178                 $self->{button});
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});
187         $self->print_state;
188 }
189
190 sub print_state {
191         my ($self) = @_;
192
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';
199         } else {
200                 $self->{state} = 'greenflag';
201         }
202
203         $self->gui->set_state($self->{order}, $self->{state});
204 }
205         
206 sub reset {
207         my ($self) = @_;
208
209         $self->set_lap(0);
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;
220 }
221
222 sub finish_line {
223         my ($self, $time, $regular) = @_;
224
225         if (defined $self->{last_finish_time} 
226                 && $time - $self->{last_finish_time} < 2) {
227
228                 $self->{last_finish_time} = $time;
229                 if ($regular && $self->{in_pit_lane}) {
230                         $self->leave_pit_lane;
231                 }
232                 return undef;
233         }
234
235         if ($regular) {
236                 if (defined $self->{first_finish_time}) {
237                         $self->set_laptime($time - $self->{first_finish_time});
238                 }
239         } else {
240                 $self->enter_pit_lane;
241         }
242
243         $self->{first_finish_time} = $self->{last_finish_time} = $time;
244
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;
251                         $self->print_state;
252                 }
253         }
254
255         return 1;
256 }
257
258 sub recalc_distance {
259         my ($self, $lap_first, $time_first, $finishing) = @_;
260
261         return if !defined $lap_first || !defined $self->{lap}
262                 || !defined $time_first || !defined $self->{first_finish_time};
263
264         $self->{total_time} = undef;
265         $self->{lap_diff} = undef;
266         $self->{time_diff} = undef;
267         $self->{grey_diff} = undef;
268
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};
273                 } else {
274                         $self->{time_diff} = $self->{first_finish_time}
275                                 - $time_first;
276                 }
277         } else {
278                 $self->{lap_diff} = $lap_first - $self->{lap};
279         }
280
281         $self->gui->set_distance($self->{order}, $self->{total_time},
282                 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});
283
284         if ($finishing) {
285                 $self->{finished} = 1;
286                 $self->print_state;
287         }
288 }
289
290 sub recalc_qual_distance {
291         my ($self, $time_first) = @_;
292
293         return if !defined $self->{best_lap} || $self->{best_lap} <= 0;
294
295         $self->{total_time} = undef;
296         $self->{time_diff} = undef;
297         $self->{grey_diff} = undef;
298         $self->{lap_diff} = undef;
299
300         if ($self->{best_lap} == $time_first) {
301                 $self->{total_time} = $self->{best_lap};
302         } else {
303                 $self->{time_diff} = $self->{best_lap} - $time_first;
304         }
305
306         $self->gui->set_distance($self->{order}, $self->{total_time},
307                 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});
308 }
309
310 sub greyout_distance {
311         my ($self) = @_;
312
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});
317 }
318
319 1;
320