X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=SCX%2FTrack.pm;h=fcf6b80d94747fbe3cf4e1af8edf0d285f78de9a;hb=47a074f4d544cc734a6c053025747ec08149370b;hp=2feed611c7b5394d482e1e6db8db0cbbf3cf398d;hpb=972687391b212153ee34a6ecf616fd1c1a53d01b;p=slotcarman.git diff --git a/SCX/Track.pm b/SCX/Track.pm index 2feed61..fcf6b80 100644 --- a/SCX/Track.pm +++ b/SCX/Track.pm @@ -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; } @@ -41,6 +45,7 @@ sub race_start { return if $self->{race_running} || $self->{start_in_progress} || $self->{qualification_running}; + $self->{round} = 0; $self->{race_running} = 0; $self->{start_in_progress} = 1; $self->{semaphore} = 0; @@ -104,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; @@ -119,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 { @@ -188,37 +198,53 @@ sub recalc_order { } (0..5); my $lap_max = $laps[$new_order[0]]; - if (defined $lap_max && $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 { my ($self, $time, $regular, @cars) = @_; - my @processed; + my %processed; + my $was_processed; + for my $car (@cars) { - push @processed, $car - if $self->car($car)->finish_line($time, $regular); + if ($self->car($car)->finish_line($time, $regular)) { + $processed{$car} = 1; + $was_processed = 1; + } } - if (@processed) { - my ($first_car, $lap_max, $time_min) - = $self->recalc_order($time); + return if !$was_processed; + + my ($lap_max_changed, $lap_max, $time_min) + = $self->recalc_order($time); - for my $car (@processed) { - $self->car($car)->recalc_distance($lap_max, $time_min); + 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; } } }