Changeset 11 for trunk


Ignore:
Timestamp:
Mar 26, 2015, 10:53:11 PM (10 years ago)
Author:
chronos
Message:
  • Modified: Better new station placement. Random angle is used and new station is placed as far from center as necessery to have minimum distance from other stations.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r10 r11  
    183183  ImageLocomotiveName = 'Images/Locomotive.png';
    184184  TrainPassengerCount = 6;
    185   StationMinDistance = 50;
     185  StationMinDistance = 100;
     186  StationMaxDistance = 300;
    186187  MaxWaitingPassengers = 10;
    187188
     
    236237  I: Integer;
    237238  Pass: Integer;
     239  Angle: Double;
     240  L: Integer;
     241const
     242  Step = 20;
    238243begin
    239244  Result := TMetroStation.Create;
    240245  Result.Engine := Engine;
     246  Angle := Random * 2 * Pi;
    241247  // Ensure minimum distance between stations
    242248  Pass := 0;
     249  L := Step;
    243250  repeat
    244     Result.Position := Point(Trunc(Engine.Map.Size.X * 0.1 + Random(Trunc(Engine.Map.Size.X * 0.8))),
    245       Trunc(Engine.Map.Size.Y * 0.1 + Random(Trunc(Engine.Map.Size.Y * 0.8))));
     251    Result.Position := Point(Trunc(Engine.Map.Size.X / 2 + Cos(Angle) * L),
     252      Trunc(Engine.Map.Size.Y / 2 + Sin(Angle) * L));
    246253    MinD := High(Integer);
    247254    for I := 0 to Engine.Stations.Count - 1 do begin
     
    250257    end;
    251258    Inc(Pass);
    252   until (MinD > StationMinDistance) or (Pass > 100) or (Engine.Stations.Count = 0);
     259    L := L + StationMinDistance div 2;
     260  until (MinD > StationMinDistance) or
     261    (Pass > 1000) or (Engine.Stations.Count = 0);
    253262  Result.Shape := TStationShape(Random(Integer(Engine.ShapeCount)));
    254263  Add(Result);
     
    756765        if (Now - StationStopTime) > OneSecond then begin
    757766          CurrentStationIndex := TargetStationIndex;
    758           CurrentStation := TargetStation;
     767          if CurrentStationIndex >= Line.Stations.Count then
     768            CurrentStationIndex := Line.Stations.Count - 1;
     769          CurrentStation := nil;
     770          if Assigned(Line) then
     771            if (CurrentStationIndex >= 0) and (CurrentStationIndex < Line.Stations.Count) then
     772          CurrentStation := TMetroStation(Line.Stations[CurrentStationIndex]);
    759773
    760774          // Choose next target station
     
    779793
    780794          // Unload passengers in target station
     795          if Assigned(CurrentStation) and (CurrentStationIndex < Line.Stations.Count) then
    781796          for P := Passengers.Count - 1 downto 0 do begin
    782797            if TMetroPassenger(Passengers[P]).Shape = CurrentStation.Shape then begin
     
    788803          end;
    789804          // Unload passengers to change line
     805          if Assigned(CurrentStation) and (CurrentStationIndex < Line.Stations.Count) then
    790806          for P := Passengers.Count - 1 downto 0 do begin
    791             if not CurrentStation.GetBestStationForShape(TMetroPassenger(Passengers[P]).Shape, TargetStation, CurrentStationIndex) then begin
     807            if not CurrentStation.GetBestStationForShape(TMetroPassenger(Passengers[P]).Shape,
     808            TargetStation, CurrentStationIndex) then begin
    792809              Passenger := TMetroPassenger(Passengers[P]);
    793810              Passengers.Delete(P);
Note: See TracChangeset for help on using the changeset viewer.