]> www.fi.muni.cz Git - slotcarman.git/blobdiff - SCX/Track.pm
Init fixes, lap counting fixes.
[slotcarman.git] / SCX / Track.pm
index b71257e5a32546a8132d3f68438321ffa8f7b367..fcf6b80d94747fbe3cf4e1af8edf0d285f78de9a 100644 (file)
@@ -14,11 +14,12 @@ our $SEMAPHORE_STEP = 1000;
 sub new {
        my ($class, $args) = @_;
 
-       my $self;
-
-       $self->{gui} = $args->{gui} or croak;
-       $self->{race_running} = 0;
-       $self->{lap_counting_up} = 1;
+       my $self = {
+               gui => $args->{gui},
+               race_running => 0,
+               lap_counting_up => 1,
+               round => 0,
+       };
 
        bless $self, $class;
 
@@ -28,9 +29,12 @@ sub new {
                        id => $i,
                        track => $self,
                });
-               $self->car($i)->set_order($i);
        }
 
+       $self->print_rounds;
+       $self->{gui}->time(undef);
+       $self->{gui}->best_lap(undef, undef);
+
        return $self;
 }
 
@@ -105,6 +109,7 @@ sub reset {
 
        $self->{race_running} = 0;
        $self->{start_in_progress} = 0;
+       $self->{race_finishing} = 0;
        $self->{best_lap} = undef;
        $self->{round} = 0;
 
@@ -120,12 +125,16 @@ sub reset {
 sub print_rounds {
        my ($self) = @_;
 
-       $self->{gui}->rounds($self->{qualification_running}
-               ? 'Qualification'
-               : $self->{race_rounds}
-               ? $self->{round} . '/' . $self->{race_rounds}
-               : $self->{round}
-       );
+       my $msg;
+       if ($self->{qualification_running}) {
+               $msg = 'Qualification';
+       } elsif ($self->{race_rounds}) {
+               $msg = $self->{round} . '/' . $self->{race_rounds};
+       } else {
+               $msg = $self->{round};
+       }
+
+       $self->{gui}->rounds($msg);
 }
 
 sub check_best_lap {
@@ -189,20 +198,26 @@ sub recalc_order {
        } (0..5);
 
        my $lap_max = $laps[$new_order[0]];
-       if (defined $lap_max && $self->{round} && $lap_max != $self->{round}
-               && (!$self->{race_rounds}
-                       || $lap_max <= $self->{race_rounds})) {
+       my $lap_max_changed = 0;
+       if (defined $lap_max && defined $self->{round}
+               && $lap_max != $self->{round}) {
                $self->{round} = $lap_max;
+               $lap_max_changed = 1;
                $self->print_rounds;
        }
 
+       if ($self->{round} && $self->{race_rounds}
+               && $self->{round} > $self->{race_rounds}) {
+               $self->{race_finishing} = 1;
+       }
+
        for my $id (0..5) {
                my $car = $new_order[$id];
                if ($self->car($car)->{order} != $id) {
                        $self->car($car)->set_order($id);
                }
        }
-       return ($new_order[0], $lap_max, $times[$new_order[0]]);
+       return ($lap_max_changed, $lap_max, $times[$new_order[0]]);
 }
 
 sub finish_line {
@@ -218,19 +233,18 @@ sub finish_line {
                }
         }
 
-       
-        if ($was_processed) {
-                my ($first_car, $lap_max, $time_min)
-                       = $self->recalc_order($time);
-
-               for my $car (0..5) {
-                       if ($processed{$car}) {
-                               $self->car($car)->recalc_distance(
-                                       $lap_max, $time_min
-                               );
-                       } else {
-                               #$self->car($car)->greyout_distance;
-                       }
+       return if !$was_processed;
+
+       my ($lap_max_changed, $lap_max, $time_min)
+               = $self->recalc_order($time);
+
+       for my $car (0..5) {
+               if ($processed{$car}) {
+                       $self->car($car)->recalc_distance(
+                               $lap_max, $time_min, $self->{race_finishing},
+                       );
+               } elsif ($lap_max_changed) {
+                       $self->car($car)->greyout_distance;
                }
        }
 }