]> www.fi.muni.cz Git - slotcarman.git/blob - SCX/Car.pm
Init fixes, lap counting fixes.
[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         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
135                         )) {
136
137                         $self->{best_lap} = $lap_time;
138                         my $global = $self->{track}->check_best_lap($lap_time,
139                                 $self->{driver}
140                         );
141                         $self->gui->set_best_lap($self->{order}, $lap_time,
142                                 $global);
143                 }
144         }
145 }
146
147 sub set_driver {
148         my ($self, $driver_name) = @_;
149
150         return if $self->same('driver', $driver_name);
151         $self->gui->set_name($self->{order}, $driver_name);
152 }
153
154 sub enter_pit_lane {
155         my ($self) = @_;
156
157         return if $self->{in_pit_lane};
158
159         $self->{in_pit_lane} = 1;
160         $self->print_state;
161 }
162
163 sub leave_pit_lane {
164         my ($self) = @_;
165
166         return if !$self->{in_pit_lane};
167
168         $self->{in_pit_lane} = 0;
169         $self->print_state;
170 }
171
172 sub set_order {
173         my ($self, $pos) = @_;
174
175         $self->{order} = $pos;
176         
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},
180                 $self->{button});
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});
189         $self->print_state;
190 }
191
192 sub print_state {
193         my ($self) = @_;
194
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';
201         } else {
202                 $self->{state} = 'greenflag';
203         }
204
205         $self->gui->set_state($self->{order}, $self->{state});
206 }
207         
208 sub reset {
209         my ($self) = @_;
210
211         $self->set_lap(0);
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;
222 }
223
224 sub finish_line {
225         my ($self, $time, $regular) = @_;
226
227         if (defined $self->{last_finish_time} 
228                 && $time - $self->{last_finish_time} < 2) {
229
230                 $self->{last_finish_time} = $time;
231                 if ($regular && $self->{in_pit_lane}) {
232                         $self->leave_pit_lane;
233                 }
234                 return undef;
235         }
236
237         if ($regular) {
238                 if (defined $self->{first_finish_time}) {
239                         $self->set_laptime($time - $self->{first_finish_time});
240                 }
241         } else {
242                 $self->enter_pit_lane;
243         }
244
245         $self->{first_finish_time} = $self->{last_finish_time} = $time;
246
247         if ($self->track->{race_running}) {
248                 $self->set_lap($self->{lap} + 1);
249         }
250
251         return 1;
252 }
253
254 sub recalc_distance {
255         my ($self, $lap_first, $time_first, $finishing) = @_;
256
257         return if !defined $lap_first || !defined $self->{lap}
258                 || !defined $time_first || !defined $self->{first_finish_time};
259
260         $self->{total_time} = undef;
261         $self->{lap_diff} = undef;
262         $self->{time_diff} = undef;
263         $self->{grey_diff} = undef;
264
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};
269                 } else {
270                         $self->{time_diff} = $self->{first_finish_time}
271                                 - $time_first;
272                 }
273         } else {
274                 $self->{lap_diff} = $lap_first - $self->{lap};
275         }
276
277         $self->gui->set_distance($self->{order}, $self->{total_time},
278                 $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff});
279
280         if ($finishing) {
281                 $self->{finished} = 1;
282                 $self->print_state;
283         }
284 }
285
286 sub greyout_distance {
287         my ($self) = @_;
288
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});
293 }
294
295 1;
296