- Timestamp:
- Mar 26, 2015, 10:14:59 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UEngine.pas
r9 r10 28 28 Lines: TMetroLines; 29 29 ShapeDistance: array[TStationShape] of Integer; 30 function GetBestStationForShape(Shape: TStationShape; Check: TMetroStation): Boolean; 30 function GetBestStationForShape(Shape: TStationShape; Check: TMetroStation; 31 CurrentStationIndex: Integer): Boolean; 31 32 constructor Create; 32 33 destructor Destroy; override; … … 60 61 constructor Create; 61 62 destructor Destroy; override; 63 function IsCircular: Boolean; 62 64 end; 63 65 … … 384 386 end; 385 387 388 function TMetroLine.IsCircular: Boolean; 389 begin 390 Result := False; 391 if Stations.Count >= 2 then 392 Result := (Stations.Last = Stations.First); 393 end; 394 386 395 { TMetroTrain } 387 396 … … 436 445 437 446 function TMetroStation.GetBestStationForShape(Shape: TStationShape; 438 Check: TMetroStation): Boolean; 439 var 440 I: Integer; 447 Check: TMetroStation; CurrentStationIndex: Integer): Boolean; 448 var 449 I: Integer; 450 T: Integer; 441 451 Distance: Integer; 442 452 StationIndex: Integer; 453 DirectionUp: Boolean; 454 DirectionDown: Boolean; 455 NextStationUp: TMetroStation; 456 NextStationDown: TMetroStation; 443 457 begin 444 458 Distance := High(Integer); 445 459 for I := 0 to Lines.Count - 1 do 446 460 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; 452 468 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; 460 498 end; 461 499 Result := Check.ShapeDistance[Shape] = Distance; … … 586 624 var 587 625 I: Integer; 626 T: Integer; 588 627 StationIndex: Integer; 628 DirectionDown: Boolean; 629 DirectionUp: Boolean; 589 630 begin 590 631 with Station do begin … … 593 634 // Do for all lines connected to station 594 635 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; 602 668 end; 603 669 end; … … 650 716 PosDelta: Integer; 651 717 CurrentStation: TMetroStation; 718 CurrentStationIndex: Integer; 652 719 begin 653 720 if State = gsRunning then begin … … 688 755 if InStation then begin 689 756 if (Now - StationStopTime) > OneSecond then begin 757 CurrentStationIndex := TargetStationIndex; 690 758 CurrentStation := TargetStation; 691 759 … … 721 789 // Unload passengers to change line 722 790 for P := Passengers.Count - 1 downto 0 do begin 723 if not CurrentStation.GetBestStationForShape(TMetroPassenger(Passengers[P]).Shape, TargetStation ) then begin791 if not CurrentStation.GetBestStationForShape(TMetroPassenger(Passengers[P]).Shape, TargetStation, CurrentStationIndex) then begin 724 792 Passenger := TMetroPassenger(Passengers[P]); 725 793 Passengers.Delete(P); … … 730 798 731 799 // Load new passengers 800 if Assigned(CurrentStation) and (CurrentStationIndex < Line.Stations.Count) then 732 801 for P := CurrentStation.Passengers.Count - 1 downto 0 do begin 733 802 if (Passengers.Count < TrainPassengerCount) then begin 734 803 Passenger := TMetroPassenger(CurrentStation.Passengers[P]); 735 if CurrentStation.GetBestStationForShape(Passenger.Shape, TargetStation ) then begin804 if CurrentStation.GetBestStationForShape(Passenger.Shape, TargetStation, CurrentStationIndex) then begin 736 805 Passenger.Station := nil; 737 806 CurrentStation.Passengers.Delete(P); … … 1024 1093 Shape, PassengerSize); 1025 1094 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); 1026 1101 end; 1027 1102
Note:
See TracChangeset
for help on using the changeset viewer.