- Timestamp:
- Apr 3, 2015, 10:40:01 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 2 2 BigMetro 3 3 BigMetro.lps 4 heaptrclog.trc
-
- Property svn:ignore
-
trunk/BigMetro.lpi
r3 r20 17 17 <StringTable ProductVersion=""/> 18 18 </VersionInfo> 19 <BuildModes Count="1"> 20 <Item1 Name="Default" Default="True"/> 19 <BuildModes Count="2"> 20 <Item1 Name="Debug" Default="True"/> 21 <Item2 Name="Release"> 22 <CompilerOptions> 23 <Version Value="11"/> 24 <Target> 25 <Filename Value="BigMetro"/> 26 </Target> 27 <SearchPaths> 28 <IncludeFiles Value="$(ProjOutDir)"/> 29 <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> 30 </SearchPaths> 31 <Parsing> 32 <SyntaxOptions> 33 <SyntaxMode Value="Delphi"/> 34 <CStyleOperator Value="False"/> 35 <AllowLabel Value="False"/> 36 <CPPInline Value="False"/> 37 </SyntaxOptions> 38 </Parsing> 39 <CodeGeneration> 40 <SmartLinkUnit Value="True"/> 41 <Optimizations> 42 <OptimizationLevel Value="3"/> 43 </Optimizations> 44 </CodeGeneration> 45 <Linking> 46 <Debugging> 47 <GenerateDebugInfo Value="False"/> 48 </Debugging> 49 <Options> 50 <Win32> 51 <GraphicApplication Value="True"/> 52 </Win32> 53 </Options> 54 </Linking> 55 </CompilerOptions> 56 </Item2> 21 57 </BuildModes> 22 58 <PublishOptions> … … 83 119 <StackChecks Value="True"/> 84 120 </Checks> 85 <VerifyObjMethodCallValidity Value="True"/>86 121 </CodeGeneration> 87 122 <Linking> 123 <Debugging> 124 <UseHeaptrc Value="True"/> 125 </Debugging> 88 126 <Options> 89 127 <Win32> … … 92 130 </Options> 93 131 </Linking> 132 <Other> 133 <CustomOptions Value="-dDEBUG"/> 134 </Other> 94 135 </CompilerOptions> 95 136 <Debugging> -
trunk/BigMetro.lpr
r3 r20 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UFormMain, UEngine, UGeometric10 Forms, SysUtils, UFormMain, UEngine, UGeometric 11 11 { you can add units after this }; 12 12 13 13 {$R *.res} 14 14 15 {$IFDEF DEBUG} 16 const 17 HeapTraceLog = 'heaptrclog.trc'; 18 {$ENDIF} 19 15 20 begin 21 {$IFDEF DEBUG} 22 // Heap trace 23 DeleteFile(ExtractFilePath(ParamStr(0)) + HeapTraceLog); 24 SetHeapTraceOutput(ExtractFilePath(ParamStr(0)) + HeapTraceLog); 25 {$ENDIF} 26 16 27 RequireDerivedFormResource := True; 17 28 Application.Initialize; -
trunk/UEngine.pas
r19 r20 31 31 Lines: TMetroLines; 32 32 ShapeDistance: array[TStationShape] of Integer; 33 function GetBestStationForShape(Shape: TStationShape; Check, Current: TLineStation): Boolean;33 function IsBestStationForShape(Shape: TStationShape; Check, Current: TLineStation): Boolean; 34 34 constructor Create; 35 35 destructor Destroy; override; … … 119 119 InStation: Boolean; 120 120 StationStopTime: TDateTime; 121 LastTrainMoveTime: TDateTime; 121 122 TargetStation: TLineStation; 122 123 function GetPosition: TPoint; … … 218 219 TrainSize = 40; 219 220 LineColorsDist = 50; 220 TrainSpeed = 3;221 TrainSpeed = 2000; 221 222 ImagePassengerName = 'Images/Passenger.png'; 222 223 ImageLocomotiveName = 'Images/Locomotive.png'; … … 228 229 TrackClickDistance = 20; 229 230 EndStationLength = 50; 231 ShowDistances = False; 232 //TimePerSecond = (60 * OneMinute); 233 TimePerSecond = (60 * OneMinute); 230 234 231 235 implementation … … 404 408 Index: Integer; 405 409 begin 410 if not Assigned(Station) then 411 raise Exception.Create('Station have to be defined'); 406 412 if not Assigned(LineStationDown) and not Assigned(LineStationUp) and (LineStations.Count > 0) then 407 413 raise Exception.Create('No old line station to connect new station'); … … 462 468 LineStation.MapStation.Lines.Remove(Self); 463 469 Index := LineStations.IndexOf(LineStation); 470 471 // Remove station from trains 472 for I := 0 to Trains.Count - 1 do 473 with TMetroTrain(Trains[I]) do 474 if TargetStation = LineStation then 475 TargetStation := TLineStation(LineStations[(Index + 1) mod LineStations.Count]); 476 464 477 LineStations.Delete(Index); 465 478 … … 543 556 begin 544 557 LineStations := TLineStations.Create; 545 LineStations.OwnsObjects := False;558 LineStations.OwnsObjects := True; 546 559 Trains := TMetroTrains.Create; 547 560 Trains.OwnsObjects := False; … … 632 645 { TMapStation } 633 646 634 function TMapStation. GetBestStationForShape(Shape: TStationShape;647 function TMapStation.IsBestStationForShape(Shape: TStationShape; 635 648 Check, Current: TLineStation): Boolean; 636 649 var … … 643 656 NextStationUp: TLineStation; 644 657 NextStationDown: TLineStation; 658 CurrentLineStation: TLineStation; 645 659 begin 646 660 Distance := High(Integer); 647 661 for I := 0 to Lines.Count - 1 do 648 662 with TMetroLine(Lines[I]) do begin 649 StationIndex := LineStations.IndexOf(Current); 663 CurrentLineStation := LineStations.SearchMapStation(Current.MapStation); 664 StationIndex := LineStations.IndexOf(CurrentLineStation); 650 665 if IsCircular then begin 651 666 DirectionUp := False; … … 685 700 end; 686 701 end; 687 Result := Check.MapStation.ShapeDistance[Shape] = Distance;702 Result := (Check.MapStation.ShapeDistance[Shape] <> -1) and (Check.MapStation.ShapeDistance[Shape] <= Distance); 688 703 end; 689 704 … … 883 898 for I := 0 to Lines.Count - 1 do 884 899 with TMetroLine(Lines[I]) do 885 for StationIndex := 0 to Stations.Count - 1 do886 if T MapStation(Stations[StationIndex])= Station then begin900 for StationIndex := 0 to LineStations.Count - 1 do 901 if TLineStation(LineStations[StationIndex]).MapStation = Station then begin 887 902 if not IsCircular then begin 888 903 // Update for all adjecent stations 889 904 if StationIndex > 0 then 890 ComputeShapeDistanceStation(TMapStation(Stations[StationIndex - 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 891 if (StationIndex >= 0) and (StationIndex < Stations.Count - 1) then 892 ComputeShapeDistanceStation(TMapStation(Stations[StationIndex + 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 905 ComputeShapeDistanceStation(TLineStation(LineStations[StationIndex - 1]).MapStation, 906 UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 907 if (StationIndex >= 0) and (StationIndex < LineStations.Count - 1) then 908 ComputeShapeDistanceStation(TLineStation(LineStations[StationIndex + 1]).MapStation, 909 UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 893 910 end else begin 894 911 // If circular then trains might go in single direction so passengers … … 903 920 if DirectionUp then begin 904 921 if StationIndex = 0 then 905 ComputeShapeDistanceStation(TMapStation(Stations[Stations.Count - 2]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 922 ComputeShapeDistanceStation(TLineStation(LineStations[Stations.Count - 2]).MapStation, 923 UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 906 924 if StationIndex > 0 then 907 ComputeShapeDistanceStation(TMapStation(Stations[StationIndex - 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 925 ComputeShapeDistanceStation(TLineStation(LineStations[StationIndex - 1]).MapStation, 926 UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 908 927 end; 909 928 if DirectionDown then begin 910 if (StationIndex >= 0) and (StationIndex = Stations.Count - 1) then 911 ComputeShapeDistanceStation(TMapStation(Stations[1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 912 if (StationIndex >= 0) and (StationIndex < Stations.Count - 1) then 913 ComputeShapeDistanceStation(TMapStation(Stations[StationIndex + 1]), UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 929 if (StationIndex >= 0) and (StationIndex = LineStations.Count - 1) then 930 ComputeShapeDistanceStation(TLineStation(LineStations[1]).MapStation, 931 UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 932 if (StationIndex >= 0) and (StationIndex < LineStations.Count - 1) then 933 ComputeShapeDistanceStation(TLineStation(LineStations[StationIndex + 1]).MapStation, 934 UpdatedShape, Station.ShapeDistance[UpdatedShape] + 1); 914 935 end; 915 936 end; … … 927 948 PosDelta: Integer; 928 949 TargetStationIndex: Integer; 950 PosChange: Integer; 929 951 begin 930 952 // Move trains … … 933 955 if Assigned(Line) then begin 934 956 if InStation then begin 935 if ( Now - StationStopTime) > OneSecondthen begin957 if (Time - StationStopTime) > OneHour then begin 936 958 CurrentStation := TargetStation; 937 959 … … 970 992 if Assigned(CurrentStation) then 971 993 for P := Passengers.Count - 1 downto 0 do begin 972 if not CurrentStation.MapStation. GetBestStationForShape(TMetroPassenger(Passengers[P]).Shape,994 if not CurrentStation.MapStation.IsBestStationForShape(TMetroPassenger(Passengers[P]).Shape, 973 995 TargetStation, CurrentStation) then begin 974 996 Passenger := TMetroPassenger(Passengers[P]); … … 980 1002 981 1003 // Load new passengers 1004 if Assigned(CurrentStation) and not Assigned(CurrentStation.MapStation) then 1005 raise Exception.Create('Station have to have MapStation'); 982 1006 if Assigned(CurrentStation) then 983 1007 for P := CurrentStation.MapStation.Passengers.Count - 1 downto 0 do begin 984 1008 if (Passengers.Count < TrainPassengerCount) then begin 985 1009 Passenger := TMetroPassenger(CurrentStation.MapStation.Passengers[P]); 986 if CurrentStation.MapStation. GetBestStationForShape(Passenger.Shape,1010 if CurrentStation.MapStation.IsBestStationForShape(Passenger.Shape, 987 1011 TargetStation, CurrentStation) then begin 988 1012 Passenger.Station := nil; … … 996 1020 LastPosDelta := Abs(TrackPos - Line.GetStationTrackPos(TargetStation)); 997 1021 InStation := False; 1022 LastTrainMoveTime := Time; 998 1023 end; 999 1024 end else begin 1000 TrackPos := TrackPos + Direction * TrainSpeed; 1025 PosChange := Trunc(Direction * TrainSpeed * (Time - LastTrainMoveTime)); 1026 TrackPos := TrackPos + PosChange; 1027 LastTrainMoveTime := Time; 1001 1028 if TrackPos < 0 then begin 1002 1029 if Line.IsCircular then TrackPos := Line.GetTrackLength … … 1012 1039 TrackPos := Line.GetStationTrackPos(TargetStation); 1013 1040 InStation := True; 1014 StationStopTime := Now;1041 StationStopTime := Time; 1015 1042 end; 1016 1043 LastPosDelta := PosDelta; … … 1094 1121 end; 1095 1122 end; 1096 1097 1123 end; 1098 1124 1099 1125 procedure TEngine.Tick; 1100 1126 const 1101 NewStationPeriod = 20 * OneSecond; 1127 NewStationPeriod = 1; 1128 NewShapePeriod = 10; 1129 NewTrainPeriod = 7; // Each week 1102 1130 NewPassengerPeriod = 0.3 * OneSecond; 1103 NewPassengerProbability = 0.0 1;1131 NewPassengerProbability = 0.005; 1104 1132 var 1105 1133 Passenger: TMetroPassenger; … … 1107 1135 begin 1108 1136 if State = gsRunning then begin 1109 FTime := FTime + (Now - LastTickTime) / OneSecond * (60 * OneMinute);1137 FTime := FTime + (Now - LastTickTime) / OneSecond * TimePerSecond; 1110 1138 1111 1139 // Add new trains 1112 if ( Now - LastNewWeekTime) > 7then begin1113 LastNewWeekTime := Now;1140 if (Time - LastNewWeekTime) > NewTrainPeriod then begin 1141 LastNewWeekTime := Time; 1114 1142 Trains.AddNew; 1115 1143 // TODO: Show notification screen with confirmation … … 1117 1145 1118 1146 // Add new shape 1119 if ( Now - LastNewShapeTime) > 10then begin1120 LastNewShapeTime := Now;1147 if (Time - LastNewShapeTime) > NewShapePeriod then begin 1148 LastNewShapeTime := Time; 1121 1149 if ShapeCount <= Integer(High(TStationShape)) then Inc(ShapeCount); 1122 1150 end; 1123 1151 1124 1152 // Add new stations 1125 if ( Now- LastNewStationTime) > NewStationPeriod then begin1126 LastNewStationTime := Now;1153 if (Time - LastNewStationTime) > NewStationPeriod then begin 1154 LastNewStationTime := Time; 1127 1155 Stations.AddNew; 1128 1156 end; 1129 1157 1130 1158 // Add new passengers 1131 if ( Now- LastNewPassengerTime) > NewPassengerPeriod then begin1132 LastNewPassengerTime := Now;1159 if (Time - LastNewPassengerTime) > NewPassengerPeriod then begin 1160 LastNewPassengerTime := Time; 1133 1161 for I := 0 to Stations.Count - 1 do 1134 1162 with TMapStation(Stations[I]) do … … 1167 1195 if MouseHold then begin 1168 1196 FocusedStation := GetStationOnPos(Position); 1197 Line := nil; 1169 1198 if Assigned(TrackStationDown) then Line := TrackStationDown.Line; 1170 1199 if Assigned(TrackStationUp) then Line := TrackStationUp.Line; 1171 if not Assigned(LastFocusedStation) and Assigned(FocusedStation) then begin1200 if Assigned(Line) and not Assigned(LastFocusedStation) and Assigned(FocusedStation) then begin 1172 1201 if Assigned(TrackStationDown) and (TrackStationDown.LineStation.MapStation = FocusedStation) then begin 1173 1202 // Disconnect down … … 1183 1212 Line.DisconnectStation(CurrentTrackPoint.LineStation); 1184 1213 end else 1185 if Assigned(Line) and (Line.LineStations.SearchMapStation(FocusedStation) = nil) then begin 1214 if Assigned(Line) and ((Line.LineStations.SearchMapStation(FocusedStation) = nil) or 1215 ((Line.LineStations.Count > 0) and 1216 ((TLineStation(Line.LineStations.First).MapStation = FocusedStation) or 1217 (TLineStation(Line.LineStations.Last).MapStation = FocusedStation)))) then begin 1186 1218 if Assigned(TrackStationDown) then LineStationDown := TrackStationDown.LineStation 1187 1219 else LineStationDown := nil; … … 1192 1224 else if Assigned(TrackStationUp) then TrackStationUp := TrackStationUp.GetDown; 1193 1225 end; 1194 {else1195 if (TrackStationDown.Line.LineStations.Count > 0) and (TLineStation(TrackStationDown.Line.LineStations.Last).MapStation = FocusedStation) then begin1196 Line.ConnectStation(FocusedStation, TrackStationDown, TrackStationUp);1197 TrackStationDown := TTrackPoint(TrackStationDown.Line.TrackPoints.Last);1198 end;1199 }1200 1226 end; 1201 1227 LastFocusedStation := FocusedStation; … … 1209 1235 I: Integer; 1210 1236 begin 1211 MouseHold := False;1212 TrackStationDown := nil;1213 TrackStationUp := nil;1214 1215 1237 if Button = mbLeft then begin 1216 1238 … … 1222 1244 Exit; 1223 1245 end; 1246 1247 // Remove single line station on line 1248 if Assigned(TrackStationDown) and (TrackStationDown.Line.LineStations.Count = 1) then begin 1249 TrackStationDown.Line.DisconnectStation(TLineStation(TrackStationDown.Line.LineStations.First)); 1250 end; 1251 if Assigned(TrackStationUp) and (TrackStationUp.Line.LineStations.Count = 1) then begin 1252 TrackStationUp.Line.DisconnectStation(TLineStation(TrackStationUp.Line.LineStations.First)); 1253 end; 1224 1254 end else 1225 1255 if Button = mbRight then begin 1226 1256 SelectedLine := nil; 1227 1257 end; 1258 MouseHold := False; 1259 TrackStationDown := nil; 1260 TrackStationUp := nil; 1228 1261 end; 1229 1262 … … 1268 1301 Station := GetStationOnPos(Position); 1269 1302 if Assigned(Station) then begin 1270 NewLine := GetUnusedLine; 1303 if Assigned(SelectedLine) and (SelectedLine.LineStations.Count = 0) then NewLine := SelectedLine 1304 else NewLine := GetUnusedLine; 1271 1305 if Assigned(NewLine) then begin 1272 1306 NewLine.ConnectStation(Station, nil, nil); … … 1302 1336 end; 1303 1337 1304 for I := 0 to 2do begin1338 for I := 0 to 5 do begin 1305 1339 Lines.AddNew; 1306 1340 NewTrain := TMetroTrain.Create; … … 1309 1343 1310 1344 SelectedLine := nil; 1311 LastNewStationTime := Now; 1312 LastNewPassengerTime := Now; 1313 LastNewWeekTime := Now; 1314 LastNewShapeTime := Now; 1345 FTime := 0; 1346 LastNewStationTime := Time; 1347 LastNewPassengerTime := Time; 1348 LastNewWeekTime := Time; 1349 LastNewShapeTime := Time; 1315 1350 LastTickTime := Now; 1316 FTime := 0;1317 1351 State := gsRunning; 1318 1352 end; … … 1447 1481 end; 1448 1482 1449 { 1450 Canvas.Brush.Style := bsClear;1451 Text := '';1452 for P := 0 to 2do1453 Text := Text + IntToStr(ShapeDistance[TStationShape(P)]) + ',';1454 Canvas.TextOut(Position.X + StationSize div 2, Position.Y + StationSize div 2, Text);1455 }1483 if ShowDistances then begin 1484 Canvas.Brush.Style := bsClear; 1485 Text := ''; 1486 for P := 0 to 5 do 1487 Text := Text + IntToStr(ShapeDistance[TStationShape(P)]) + ','; 1488 Canvas.TextOut(Position.X + StationSize div 2, Position.Y + StationSize div 2, Text); 1489 end; 1456 1490 end; 1457 1491
Note:
See TracChangeset
for help on using the changeset viewer.