Changeset 10


Ignore:
Timestamp:
Mar 26, 2015, 10:14:59 PM (10 years ago)
Author:
chronos
Message:
  • Fixed: Passengers behaviour in case of circural track and trainn going only in onde direction.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r9 r10  
    2828    Lines: TMetroLines;
    2929    ShapeDistance: array[TStationShape] of Integer;
    30     function GetBestStationForShape(Shape: TStationShape; Check: TMetroStation): Boolean;
     30    function GetBestStationForShape(Shape: TStationShape; Check: TMetroStation;
     31      CurrentStationIndex: Integer): Boolean;
    3132    constructor Create;
    3233    destructor Destroy; override;
     
    6061    constructor Create;
    6162    destructor Destroy; override;
     63    function IsCircular: Boolean;
    6264  end;
    6365
     
    384386end;
    385387
     388function TMetroLine.IsCircular: Boolean;
     389begin
     390  Result := False;
     391  if Stations.Count >= 2 then
     392    Result := (Stations.Last = Stations.First);
     393end;
     394
    386395{ TMetroTrain }
    387396
     
    436445
    437446function TMetroStation.GetBestStationForShape(Shape: TStationShape;
    438   Check: TMetroStation): Boolean;
    439 var
    440   I: Integer;
     447  Check: TMetroStation; CurrentStationIndex: Integer): Boolean;
     448var
     449  I: Integer;
     450  T: Integer;
    441451  Distance: Integer;
    442452  StationIndex: Integer;
     453  DirectionUp: Boolean;
     454  DirectionDown: Boolean;
     455  NextStationUp: TMetroStation;
     456  NextStationDown: TMetroStation;
    443457begin
    444458  Distance := High(Integer);
    445459  for I := 0 to Lines.Count - 1 do
    446460  with TMetroLine(Lines[I]) do begin
    447     StationIndex := Stations.IndexOf(Self);
    448     if StationIndex > 0 then begin
    449       if (TMetroStation(Stations[StationIndex - 1]).ShapeDistance[Shape] <> -1) and
    450         (TMetroStation(Stations[StationIndex - 1]).ShapeDistance[Shape] < Distance) then begin
    451         Distance := TMetroStation(Stations[StationIndex - 1]).ShapeDistance[Shape];
     461    StationIndex := CurrentStationIndex;
     462    if IsCircular then begin
     463      DirectionUp := False;
     464      DirectionDown := False;
     465      for T := 0 to Trains.Count - 1 do begin
     466        if TMetroTrain(Trains[T]).Direction = 1 then DirectionUp := True;
     467        if TMetroTrain(Trains[T]).Direction = -1 then DirectionDown := True;
    452468      end;
    453     end;
    454     if (StationIndex >= 0) and (StationIndex < Stations.Count - 1) then begin
    455       if (TMetroStation(Stations[StationIndex + 1]).ShapeDistance[Shape] <> -1) and
    456         (TMetroStation(Stations[StationIndex + 1]).ShapeDistance[Shape] < Distance) then begin
    457         Distance := TMetroStation(Stations[StationIndex + 1]).ShapeDistance[Shape];
    458       end;
    459     end
     469      if StationIndex = 0 then
     470        NextStationDown := TMetroStation(Stations[Stations.Count - 2])
     471      else
     472      if StationIndex > 0 then
     473        NextStationDown := TMetroStation(Stations[StationIndex - 1]);
     474
     475      if (StationIndex >= 0) and (StationIndex = Stations.Count - 1) then
     476        NextStationUp := TMetroStation(Stations[1])
     477      else
     478      if (StationIndex >= 0) and (StationIndex < Stations.Count - 1) then
     479        NextStationUp := TMetroStation(Stations[StationIndex + 1]);
     480    end else begin
     481      if StationIndex > 0 then begin
     482        DirectionDown := True;
     483        NextStationDown := TMetroStation(Stations[StationIndex - 1])
     484      end else DirectionDown := False;
     485      if (StationIndex >= 0) and (StationIndex < Stations.Count - 1) then begin
     486        DirectionUp := True;
     487        NextStationUp := TMetroStation(Stations[StationIndex + 1]);
     488      end else DirectionUp := False;
     489    end;
     490    if DirectionDown and (NextStationDown.ShapeDistance[Shape] <> -1) and
     491      (NextStationDown.ShapeDistance[Shape] < Distance) then begin
     492      Distance := NextStationDown.ShapeDistance[Shape];
     493    end;
     494    if DirectionUp and (NextStationUp.ShapeDistance[Shape] <> -1) and
     495      (NextStationUp.ShapeDistance[Shape] < Distance) then begin
     496      Distance := NextStationUp.ShapeDistance[Shape];
     497    end;
    460498  end;
    461499  Result := Check.ShapeDistance[Shape] = Distance;
     
    586624var
    587625  I: Integer;
     626  T: Integer;
    588627  StationIndex: Integer;
     628  DirectionDown: Boolean;
     629  DirectionUp: Boolean;
    589630begin
    590631  with Station do begin
     
    593634      // Do for all lines connected to station
    594635      for I := 0 to Lines.Count - 1 do
    595       with TMetroLine(Lines[I]) do begin
    596         StationIndex := Stations.IndexOf(Station);
    597         // Update for all adjecent stations
    598         if StationIndex > 0 then
    599           ComputeShapeDistanceStation(TMetroStation(Stations[StationIndex - 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
    600         if (StationIndex >= 0) and (StationIndex < Stations.Count - 1) then
    601           ComputeShapeDistanceStation(TMetroStation(Stations[StationIndex + 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
     636      with TMetroLine(Lines[I]) do
     637        for StationIndex := 0 to Stations.Count - 1 do
     638        if TMetroStation(Stations[StationIndex]) = Station then begin
     639        if not IsCircular then begin
     640          // Update for all adjecent stations
     641          if StationIndex > 0 then
     642            ComputeShapeDistanceStation(TMetroStation(Stations[StationIndex - 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
     643          if (StationIndex >= 0) and (StationIndex < Stations.Count - 1) then
     644            ComputeShapeDistanceStation(TMetroStation(Stations[StationIndex + 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
     645        end else begin
     646          // If circular then trains might go in single direction so passengers
     647          // waiting for oposite directions are wrong
     648          DirectionUp := False;
     649          DirectionDown := False;
     650          for T := 0 to Trains.Count - 1 do begin
     651            if TMetroTrain(Trains[T]).Direction = 1 then DirectionUp := True;
     652            if TMetroTrain(Trains[T]).Direction = -1 then DirectionDown := True;
     653          end;
     654          // Update for all adjecent stations
     655          if DirectionUp then begin
     656            if StationIndex = 0 then
     657              ComputeShapeDistanceStation(TMetroStation(Stations[Stations.Count - 2]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
     658            if StationIndex > 0 then
     659              ComputeShapeDistanceStation(TMetroStation(Stations[StationIndex - 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
     660          end;
     661          if DirectionDown then begin
     662            if (StationIndex >= 0) and (StationIndex = Stations.Count - 1) then
     663              ComputeShapeDistanceStation(TMetroStation(Stations[1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
     664            if (StationIndex >= 0) and (StationIndex < Stations.Count - 1) then
     665              ComputeShapeDistanceStation(TMetroStation(Stations[StationIndex + 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1);
     666          end;
     667        end;
    602668      end;
    603669    end;
     
    650716  PosDelta: Integer;
    651717  CurrentStation: TMetroStation;
     718  CurrentStationIndex: Integer;
    652719begin
    653720  if State = gsRunning then begin
     
    688755      if InStation then begin
    689756        if (Now - StationStopTime) > OneSecond then begin
     757          CurrentStationIndex := TargetStationIndex;
    690758          CurrentStation := TargetStation;
    691759
     
    721789          // Unload passengers to change line
    722790          for P := Passengers.Count - 1 downto 0 do begin
    723             if not CurrentStation.GetBestStationForShape(TMetroPassenger(Passengers[P]).Shape, TargetStation) then begin
     791            if not CurrentStation.GetBestStationForShape(TMetroPassenger(Passengers[P]).Shape, TargetStation, CurrentStationIndex) then begin
    724792              Passenger := TMetroPassenger(Passengers[P]);
    725793              Passengers.Delete(P);
     
    730798
    731799          // Load new passengers
     800          if Assigned(CurrentStation) and (CurrentStationIndex < Line.Stations.Count) then
    732801          for P := CurrentStation.Passengers.Count - 1 downto 0 do begin
    733802            if (Passengers.Count < TrainPassengerCount) then begin
    734803              Passenger := TMetroPassenger(CurrentStation.Passengers[P]);
    735               if CurrentStation.GetBestStationForShape(Passenger.Shape, TargetStation) then begin
     804              if CurrentStation.GetBestStationForShape(Passenger.Shape, TargetStation, CurrentStationIndex) then begin
    736805                Passenger.Station := nil;
    737806                CurrentStation.Passengers.Delete(P);
     
    10241093        Shape, PassengerSize);
    10251094    end;
     1095
     1096    Canvas.Brush.Style := bsClear;
     1097    Text := '';
     1098    for P := 0 to 2 do
     1099      Text := Text + IntToStr(ShapeDistance[TStationShape(P)]) + ',';
     1100    Canvas.TextOut(Position.X + StationSize div 2, Position.Y + StationSize div 2, Text);
    10261101  end;
    10271102
Note: See TracChangeset for help on using the changeset viewer.