Changeset 447 for trunk/AI/StdAI/ToolAI.pas
- Timestamp:
- May 19, 2022, 10:39:34 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/AI/StdAI/ToolAI.pas
r442 r447 11 11 type 12 12 TGroupTransportPlan = record 13 LoadLoc, uixTransport, nLoad, TurnsEmpty, TurnsLoaded: integer;14 uixLoad: array[0..15] of integer;13 LoadLoc, uixTransport, nLoad, TurnsEmpty, TurnsLoaded: Integer; 14 uixLoad: array[0..15] of Integer; 15 15 end; 16 16 … … 18 18 TToolAI = class(TCustomAI) 19 19 protected 20 {$IFDEF DEBUG}DebugMap: array[0..lxmax * lymax - 1] of integer;{$ENDIF}21 22 function CenterOfEmpire: integer;20 {$IFDEF DEBUG}DebugMap: array[0..lxmax * lymax - 1] of Integer;{$ENDIF} 21 22 function CenterOfEmpire: Integer; 23 23 // tile that is in the middle of all own cities 24 24 25 function CityTaxBalance(cix: integer; const CityReport: TCityReport): integer;25 function CityTaxBalance(cix: Integer; const CityReport: TCityReport): Integer; 26 26 // calculates exact difference of income and maintenance cost for a single city 27 27 // positive result = income higher than maintenance … … 29 29 // respects production and food converted to gold 30 30 // CityReport must have been prepared before 31 procedure SumCities(TaxRate: integer; var TaxSum, ScienceSum: integer);31 procedure SumCities(TaxRate: Integer; var TaxSum, ScienceSum: Integer); 32 32 // calculates exact total tax and science income 33 33 // tax is reduced by maintenance (so might be negative) … … 46 46 procedure JobAssignment_Initialize; 47 47 // initialization, must be called first of the JobAssignment functions 48 procedure JobAssignment_AddJob(Loc, Job, Score: integer);48 procedure JobAssignment_AddJob(Loc, Job, Score: Integer); 49 49 // add job for settlers with certain score 50 50 // jobs include founding cities! 51 procedure JobAssignment_AddUnit(uix: integer);51 procedure JobAssignment_AddUnit(uix: Integer); 52 52 // add a settler unit to do jobs 53 53 procedure JobAssignment_Go; … … 57 57 // starting a job one turn earlier counts the same as 4 points of score 58 58 // function does not cancel jobs that are already started 59 function JobAssignment_GotJob(uix: integer): boolean;59 function JobAssignment_GotJob(uix: Integer): Boolean; 60 60 // can be called after JobAssignment_Go to find out whether 61 61 // a certain settler has been assigned a job to … … 64 64 // calculates formations and districts 65 65 66 function CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: integer;67 var TimeAfterStep, RecoverTurns: integer; FromTile, ToTile: integer;68 IsCapture: boolean): integer;66 function CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: Integer; 67 var TimeAfterStep, RecoverTurns: Integer; FromTile, ToTile: Integer; 68 IsCapture: Boolean): Integer; 69 69 // forecast single unit move between adjacent tiles 70 70 // format of TimeBeforeStep and TimeAfterStep: $1000*number of turns + $800-MP left … … 74 74 // CrossCorner=1 for long moves that cross the tile corner, =0 for short ones that don't 75 75 76 function GetMyMoveStyle(mix, Health: integer): integer;77 78 function Unit_MoveEx(uix, ToLoc: integer; Options: integer = 0): integer;76 function GetMyMoveStyle(mix, Health: Integer): Integer; 77 78 function Unit_MoveEx(uix, ToLoc: Integer; Options: Integer = 0): Integer; 79 79 80 80 procedure SeaTransport_BeginInitialize; … … 90 90 // - all transports have same capacity 91 91 // - no transport is damaged 92 procedure SeaTransport_AddLoad(uix: integer);93 procedure SeaTransport_AddTransport(uix: integer);94 procedure SeaTransport_AddDestination(Loc: integer);95 function SeaTransport_MakeGroupPlan(var TransportPlan: TGroupTransportPlan): boolean;92 procedure SeaTransport_AddLoad(uix: Integer); 93 procedure SeaTransport_AddTransport(uix: Integer); 94 procedure SeaTransport_AddDestination(Loc: Integer); 95 function SeaTransport_MakeGroupPlan(var TransportPlan: TGroupTransportPlan): Boolean; 96 96 // make plan for group of units to transport from a single loading location by a single transport 97 97 // the plan optimizes: … … 103 103 // function returns false if no more transports are possible 104 104 105 function CurrentMStrength(Domain: integer): integer;105 function CurrentMStrength(Domain: Integer): Integer; 106 106 end; 107 107 … … 132 132 133 133 var 134 nContinent, nOcean, nDistrict: integer;135 Formation: array[0..lxmax * lymax - 1] of integer;134 nContinent, nOcean, nDistrict: Integer; 135 Formation: array[0..lxmax * lymax - 1] of Integer; 136 136 // water: ocean index, land: continent index, sorted by size 137 137 // territory unpassable due to peace treaty divides a continent 138 District: array[0..lxmax * lymax - 1] of integer;138 District: array[0..lxmax * lymax - 1] of Integer; 139 139 // index of coherent own territory, sorted by size 140 CityResult: array[0..nCmax - 1] of integer;141 142 Advancedness: array[0..nAdv - 1] of integer;140 CityResult: array[0..nCmax - 1] of Integer; 141 142 Advancedness: array[0..nAdv - 1] of Integer; 143 143 // total number of prerequisites for each advance 144 144 … … 149 149 150 150 type 151 pinteger = ^ integer;151 pinteger = ^Integer; 152 152 153 153 var 154 154 // for JobAssignment 155 MaxScore: integer;156 TileJob, TileJobScore: array[0..lxmax * lymax - 1] of byte;157 JobLocOfSettler: array[0..nUmax - 1] of integer; // ToAssign = find job155 MaxScore: Integer; 156 TileJob, TileJobScore: array[0..lxmax * lymax - 1] of Byte; 157 JobLocOfSettler: array[0..nUmax - 1] of Integer; // ToAssign = find job 158 158 159 159 // for Transport 160 TransportMoveStyle, TransportCapacity, nTransportLoad: integer;161 InitComplete, HaveDestinations: boolean;162 uixTransportLoad, TransportAvailable: array[0..nUmax - 1] of integer;163 TurnsAfterLoad: array[0..lxmax * lymax - 1] of shortint;164 165 procedure ReplaceD(Start, Stop: pinteger; Raider, Twix: integer);160 TransportMoveStyle, TransportCapacity, nTransportLoad: Integer; 161 InitComplete, HaveDestinations: Boolean; 162 uixTransportLoad, TransportAvailable: array[0..nUmax - 1] of Integer; 163 TurnsAfterLoad: array[0..lxmax * lymax - 1] of ShortInt; 164 165 procedure ReplaceD(Start, Stop: pinteger; Raider, Twix: Integer); 166 166 begin 167 167 while Start <> Stop do … … 173 173 end; 174 174 175 function NextZero(Start, Stop: pinteger; Mask: cardinal): pinteger;175 function NextZero(Start, Stop: pinteger; Mask: Cardinal): pinteger; 176 176 begin 177 177 while (Start <> Stop) and (Start^ and Mask <> 0) do … … 180 180 end; 181 181 182 function TToolAI.CenterOfEmpire: integer;183 var 184 cix, Loc, x, y, sy, n: integer;185 a, su, sv: double;186 begin 187 n:= 0;182 function TToolAI.CenterOfEmpire: Integer; 183 var 184 cix, Loc, X, Y, sy, N: Integer; 185 A, su, sv: Double; 186 begin 187 N := 0; 188 188 sy := 0; 189 189 su := 0; … … 194 194 if Loc >= 0 then 195 195 begin 196 y:= Loc div G.lx;197 x := Loc - y* G.lx;198 Inc(sy, y);199 a := 2 * pi * x/ G.lx;200 su := su + cos( a);201 sv := sv + sin( a);202 Inc( n);203 end; 204 end; 205 a:= arctan2(sv, su);206 x := round(G.lx * a/ (2 * pi));207 while x>= G.lx do208 Dec( x, G.lx);209 while x< 0 do210 Inc( x, G.lx);211 Result := ((2 * sy + n) div (2 * n)) * G.lx + x;212 end; 213 214 function TToolAI.CityTaxBalance(cix: integer; const CityReport: TCityReport): integer;215 var 216 i: integer;196 Y := Loc div G.lx; 197 X := Loc - Y * G.lx; 198 Inc(sy, Y); 199 A := 2 * pi * X / G.lx; 200 su := su + cos(A); 201 sv := sv + sin(A); 202 Inc(N); 203 end; 204 end; 205 A := arctan2(sv, su); 206 X := round(G.lx * A / (2 * pi)); 207 while X >= G.lx do 208 Dec(X, G.lx); 209 while X < 0 do 210 Inc(X, G.lx); 211 Result := ((2 * sy + N) div (2 * N)) * G.lx + X; 212 end; 213 214 function TToolAI.CityTaxBalance(cix: Integer; const CityReport: TCityReport): Integer; 215 var 216 I: Integer; 217 217 begin 218 218 Result := 0; … … 229 229 Inc(Result, CityReport.FoodRep - CityReport.Eaten); 230 230 end; 231 for i:= nWonder to nImp - 1 do232 if MyCity[cix].Built[ i] > 0 then233 Dec(Result, Imp[ i].Maint);234 end; 235 236 procedure TToolAI.SumCities(TaxRate: integer; var TaxSum, ScienceSum: integer);237 var 238 cix, p1: integer;231 for I := nWonder to nImp - 1 do 232 if MyCity[cix].Built[I] > 0 then 233 Dec(Result, Imp[I].Maint); 234 end; 235 236 procedure TToolAI.SumCities(TaxRate: Integer; var TaxSum, ScienceSum: Integer); 237 var 238 cix, p1: Integer; 239 239 CityReport: TCityReport; 240 240 begin … … 242 242 ScienceSum := 0; 243 243 if RO.Government = gAnarchy then 244 exit;244 Exit; 245 245 for p1 := 0 to nPl - 1 do 246 246 if RO.Tribute[p1] <= RO.TributePaid[p1] then … … 268 268 procedure TToolAI.OptimizeCityTiles; 269 269 var 270 cix: integer;270 cix: Integer; 271 271 begin 272 272 for cix := 0 to RO.nCity - 1 do … … 278 278 procedure TToolAI.GetCityProdPotential; 279 279 var 280 cix: integer;280 cix: Integer; 281 281 Advice: TCityTileAdviceData; 282 282 begin … … 286 286 begin 287 287 Advice.ResourceWeights := rwMaxProd; 288 Server(sGetCityTileAdvice, me, cix, Advice);288 Server(sGetCityTileAdvice, Me, cix, Advice); 289 289 CityResult[cix] := Advice.CityReport.ProdRep; // considers factory, but shouldn't 290 290 end; … … 293 293 procedure TToolAI.GetCityTradePotential; 294 294 var 295 cix: integer;295 cix: Integer; 296 296 Advice: TCityTileAdviceData; 297 297 begin … … 301 301 begin 302 302 Advice.ResourceWeights := rwMaxScience; 303 Server(sGetCityTileAdvice, me, cix, Advice);303 Server(sGetCityTileAdvice, Me, cix, Advice); 304 304 CityResult[cix] := Advice.CityReport.Trade; 305 305 end; … … 314 314 procedure TToolAI.JobAssignment_Initialize; 315 315 begin 316 fillchar(JobLocOfSettler, RO.nUn * sizeof(integer), $FF); // -1317 fillchar(TileJob, MapSize, jNone);318 fillchar(TileJobScore, MapSize, 0);316 FillChar(JobLocOfSettler, RO.nUn * SizeOf(Integer), $FF); // -1 317 FillChar(TileJob, MapSize, jNone); 318 FillChar(TileJobScore, MapSize, 0); 319 319 MaxScore := 0; 320 320 end; 321 321 322 procedure TToolAI.JobAssignment_AddJob(Loc, Job, Score: integer);322 procedure TToolAI.JobAssignment_AddJob(Loc, Job, Score: Integer); 323 323 begin 324 324 if Score > 255 then … … 333 333 end; 334 334 335 procedure TToolAI.JobAssignment_AddUnit(uix: integer);336 begin 337 assert(MyModel[MyUnit[uix].mix].Kind in [mkSettler, mkSlaves]);335 procedure TToolAI.JobAssignment_AddUnit(uix: Integer); 336 begin 337 Assert(MyModel[MyUnit[uix].mix].Kind in [mkSettler, mkSlaves]); 338 338 JobLocOfSettler[uix] := ToAssign; 339 339 end; 340 340 341 function TToolAI.JobAssignment_GotJob(uix: integer): boolean;341 function TToolAI.JobAssignment_GotJob(uix: Integer): Boolean; 342 342 begin 343 343 Result := JobLocOfSettler[uix] >= 0; … … 347 347 const 348 348 DistanceScore = 4; 349 StepSizeByTerrain: array[0..11] of integer =349 StepSizeByTerrain: array[0..11] of Integer = 350 350 (0, 0, 1, 2, 1, 1, 0, 1, 0, 1, 1, 2); 351 351 //Oc-Sh-Gr-De-Pr-Tu-Ar-Sw-XX-Fo-Hi-Mo 352 352 var 353 353 uix, BestScore, BestCount, BestLoc, BestJob, BestDistance, TestLoc, 354 NextLoc, TestDistance, V8, TestScore, StepSize, MoveResult: integer;355 UnitsToAssign: boolean;354 NextLoc, TestDistance, V8, TestScore, StepSize, MoveResult: Integer; 355 UnitsToAssign: Boolean; 356 356 Adjacent: TVicinity8Loc; 357 357 SettlerOfJobLoc, DistToLoc: array[0..lxmax * lymax - 1] of smallint; 358 358 // DistToLoc is only defined where SettlerOfJobLoc>=0 359 TileChecked: array[0..lxmax * lymax - 1] of boolean;360 begin 361 fillchar(SettlerOfJobLoc, MapSize * 2, $FF); // -1359 TileChecked: array[0..lxmax * lymax - 1] of Boolean; 360 begin 361 FillChar(SettlerOfJobLoc, MapSize * 2, $FF); // -1 362 362 363 363 // keep up jobs that are already started … … 380 380 BestJob := jNone; 381 381 BestScore := -999999; 382 FillChar(TileChecked, MapSize * sizeof(boolean), False);382 FillChar(TileChecked, MapSize * SizeOf(Boolean), False); 383 383 Pile.Create(MapSize); 384 384 Pile.Put(MyUnit[uix].Loc, 0); // start search for new job at current location … … 399 399 and (Map[NextLoc] and (fUnit or fOwned) <> fUnit) // no foreign unit 400 400 and ((RO.Territory[NextLoc] < 0) or 401 (RO.Territory[NextLoc] = me)) // no foreign territory401 (RO.Territory[NextLoc] = Me)) // no foreign territory 402 402 and (Map[TestLoc] and Map[NextLoc] and fInEnemyZoC = 0) then 403 403 // move not prevented by ZoC … … 414 414 ((SettlerOfJobLoc[TestLoc] < 0) or (DistToLoc[TestLoc] > TestDistance)) then 415 415 begin 416 TestScore := integer(TileJobScore[TestLoc]) - DistanceScore * TestDistance;416 TestScore := Integer(TileJobScore[TestLoc]) - DistanceScore * TestDistance; 417 417 if TestScore > BestScore then 418 418 BestCount := 0; … … 469 469 procedure TToolAI.AnalyzeMap; 470 470 var 471 i, j, Loc, Loc1, V8, Count, Kind, MostIndex: integer;471 I, J, Loc, Loc1, V8, Count, Kind, MostIndex: Integer; 472 472 Adjacent: TVicinity8Loc; 473 473 IndexOfID: array[0..lxmax * lymax - 1] of smallint; 474 474 IDOfIndex: array[0..lxmax * lymax div 2 - 1] of smallint; 475 475 begin 476 fillchar(District, MapSize * 4, $FF);476 FillChar(District, MapSize * 4, $FF); 477 477 for Loc := 0 to MapSize - 1 do 478 478 if Map[Loc] and fTerrain = fUNKNOWN then … … 500 500 Formation[Loc], Formation[Loc1]); 501 501 end; 502 if (RO.Territory[Loc] = me) and (Map[Loc] and fTerrain >= fGrass) then502 if (RO.Territory[Loc] = Me) and (Map[Loc] and fTerrain >= fGrass) then 503 503 begin 504 504 District[Loc] := Loc; … … 545 545 Inc(Count); 546 546 end; 547 for i:= 0 to Count - 2 do548 begin 549 MostIndex := i;550 for j := i+ 1 to Count - 1 do551 if IndexOfID[IDOfIndex[ j]] > IndexOfID[IDOfIndex[MostIndex]] then552 MostIndex := j;553 if MostIndex <> ithen554 begin 555 j := IDOfIndex[i];556 IDOfIndex[ i] := IDOfIndex[MostIndex];557 IDOfIndex[MostIndex] := j;558 end; 559 end; 560 for i:= 0 to Count - 1 do561 IndexOfID[IDOfIndex[ i]] := i;547 for I := 0 to Count - 2 do 548 begin 549 MostIndex := I; 550 for J := I + 1 to Count - 1 do 551 if IndexOfID[IDOfIndex[J]] > IndexOfID[IDOfIndex[MostIndex]] then 552 MostIndex := J; 553 if MostIndex <> I then 554 begin 555 J := IDOfIndex[I]; 556 IDOfIndex[I] := IDOfIndex[MostIndex]; 557 IDOfIndex[MostIndex] := J; 558 end; 559 end; 560 for I := 0 to Count - 1 do 561 IndexOfID[IDOfIndex[I]] := I; 562 562 563 563 case Kind of … … 605 605 // other: | Basic | 0| Speed | X X X | MaxTerrType | 606 606 607 function TToolAI.GetMyMoveStyle(mix, Health: integer): integer;607 function TToolAI.GetMyMoveStyle(mix, Health: Integer): Integer; 608 608 begin 609 609 with MyModel[mix] do … … 614 614 begin 615 615 Inc(Result, (50 + (Speed - 150) * 13 shr 7) shl 8); //HeavyCost 616 if RO.Wonder[woShinkansen].EffectiveOwner <> me then616 if RO.Wonder[woShinkansen].EffectiveOwner <> Me then 617 617 Inc(Result, Speed * (4 * 1311) shr 17); // RailCost 618 if (RO.Wonder[woGardens].EffectiveOwner <> me) or618 if (RO.Wonder[woGardens].EffectiveOwner <> Me) or 619 619 (Kind = mkSettler) and (Speed >= 200) then 620 620 Inc(Result, msHostile); … … 631 631 begin 632 632 Result := Speed; 633 if RO.Wonder[woMagellan].EffectiveOwner = me then633 if RO.Wonder[woMagellan].EffectiveOwner = Me then 634 634 Inc(Result, 200); 635 635 if Health < 100 then … … 646 646 end; 647 647 648 function TToolAI.CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: integer;649 var TimeAfterStep, RecoverTurns: integer; FromTile, ToTile: integer;650 IsCapture: boolean): integer;651 var 652 MoveCost, RecoverCost: integer;648 function TToolAI.CheckStep(MoveStyle, TimeBeforeStep, CrossCorner: Integer; 649 var TimeAfterStep, RecoverTurns: Integer; FromTile, ToTile: Integer; 650 IsCapture: Boolean): Integer; 651 var 652 MoveCost, RecoverCost: Integer; 653 653 begin 654 654 //IsCapture:=true; 655 assert(((FromTile and fTerrain <= fMountains) or (FromTile and655 Assert(((FromTile and fTerrain <= fMountains) or (FromTile and 656 656 fTerrain = fUNKNOWN)) and ((ToTile and fTerrain <= fMountains) or 657 657 (ToTile and fTerrain = fUNKNOWN))); … … 701 701 if ToTile and fPeace <> 0 then 702 702 Result := csCheckTerritory; 703 exit;703 Exit; 704 704 end; 705 705 end; … … 820 820 begin 821 821 Result := csForbiddenTile; 822 exit;822 Exit; 823 823 end; 824 824 end … … 869 869 // must wait for next turn 870 870 Result := csOk; 871 exit;871 Exit; 872 872 end; 873 873 end; … … 894 894 -------- Pathfinding Reference Implementation -------- 895 895 var 896 MoveStyle,V8,Loc,Time,NextLoc,NextTime,RecoverTurns: integer;896 MoveStyle,V8,Loc,Time,NextLoc,NextTime,RecoverTurns: Integer; 897 897 Adjacent: TVicinity8Loc; 898 Reached: array[0..lxmax*lymax-1] of boolean;899 begin 900 fillchar(Reached, MapSize, false);898 Reached: array[0..lxmax*lymax-1] of Boolean; 899 begin 900 FillChar(Reached, MapSize, False); 901 901 MoveStyle:=GetMyMoveStyle(MyUnit[uix].mix, MyUnit[uix].Health); 902 902 Pile.Create(MapSize); … … 906 906 // todo: check exit condition, e.g. whether destination reached 907 907 908 Reached[Loc]:= true;908 Reached[Loc]:=True; 909 909 V8_to_Loc(Loc, Adjacent); 910 910 for V8:=0 to 7 do … … 916 916 Pile.Put(NextLoc, NextTime+RecoverTurns*$1000); 917 917 csForbiddenTile: 918 Reached[NextLoc]:= true; // don't check moving there again918 Reached[NextLoc]:=True; // don't check moving there again 919 919 csCheckTerritory: 920 920 if RO.Territory[NextLoc]=RO.Territory[Loc] then … … 927 927 *) 928 928 929 function TToolAI.Unit_MoveEx(uix, ToLoc: integer; Options: integer): integer;929 function TToolAI.Unit_MoveEx(uix, ToLoc: Integer; Options: Integer): Integer; 930 930 var 931 931 Loc, NextLoc, Temp, FromLoc, EndLoc, Time, V8, MoveResult, RecoverTurns, 932 NextTime, MoveStyle: integer;932 NextTime, MoveStyle: Integer; 933 933 Adjacent: TVicinity8Loc; 934 PreLoc: array[0..lxmax * lymax - 1] of integer;935 Reached: array[0..lxmax * lymax - 1] of boolean;934 PreLoc: array[0..lxmax * lymax - 1] of Integer; 935 Reached: array[0..lxmax * lymax - 1] of Boolean; 936 936 begin 937 937 Result := eOk; 938 938 FromLoc := MyUnit[uix].Loc; 939 939 if FromLoc = ToLoc then 940 exit;940 Exit; 941 941 942 942 FillChar(Reached, MapSize, False); … … 1003 1003 begin 1004 1004 Result := MoveResult; 1005 break;1005 Break; 1006 1006 end; 1007 1007 end; … … 1016 1016 procedure TToolAI.SeaTransport_BeginInitialize; 1017 1017 begin 1018 fillchar(TransportAvailable, RO.nUn * sizeof(integer), $FF); // -11018 FillChar(TransportAvailable, RO.nUn * SizeOf(Integer), $FF); // -1 1019 1019 InitComplete := False; 1020 1020 HaveDestinations := False; … … 1025 1025 end; 1026 1026 1027 procedure TToolAI.SeaTransport_AddLoad(uix: integer);1028 var 1029 i: integer;1030 begin 1031 assert(not InitComplete); // call order violation!1027 procedure TToolAI.SeaTransport_AddLoad(uix: Integer); 1028 var 1029 I: Integer; 1030 begin 1031 Assert(not InitComplete); // call order violation! 1032 1032 if Map[MyUnit[uix].Loc] and fTerrain < fGrass then 1033 exit;1034 for i:= 0 to nTransportLoad - 1 do1035 if uix = uixTransportLoad[ i] then1036 exit;1033 Exit; 1034 for I := 0 to nTransportLoad - 1 do 1035 if uix = uixTransportLoad[I] then 1036 Exit; 1037 1037 uixTransportLoad[nTransportLoad] := uix; 1038 1038 Inc(nTransportLoad); 1039 1039 end; 1040 1040 1041 procedure TToolAI.SeaTransport_AddTransport(uix: integer);1042 var 1043 MoveStyle: integer;1044 begin 1045 assert(not InitComplete); // call order violation!1046 assert(MyModel[MyUnit[uix].mix].Cap[mcSeaTrans] > 0);1041 procedure TToolAI.SeaTransport_AddTransport(uix: Integer); 1042 var 1043 MoveStyle: Integer; 1044 begin 1045 Assert(not InitComplete); // call order violation! 1046 Assert(MyModel[MyUnit[uix].mix].Cap[mcSeaTrans] > 0); 1047 1047 TransportAvailable[uix] := 1; 1048 1048 with MyModel[MyUnit[uix].mix] do … … 1058 1058 end; 1059 1059 1060 procedure TToolAI.SeaTransport_AddDestination(Loc: integer);1061 begin 1062 assert(not InitComplete); // call order violation!1060 procedure TToolAI.SeaTransport_AddDestination(Loc: Integer); 1061 begin 1062 Assert(not InitComplete); // call order violation! 1063 1063 Pile.Put(Loc, $800); 1064 1064 HaveDestinations := True; … … 1067 1067 procedure TToolAI.SeaTransport_EndInitialize; 1068 1068 var 1069 Loc0, Time0, V8, Loc1, ArriveTime, RecoverTurns: integer;1069 Loc0, Time0, V8, Loc1, ArriveTime, RecoverTurns: Integer; 1070 1070 Adjacent: TVicinity8Loc; 1071 1071 begin 1072 assert(not InitComplete); // call order violation!1072 Assert(not InitComplete); // call order violation! 1073 1073 InitComplete := True; 1074 1074 if HaveDestinations then 1075 1075 begin // calculate TurnsAfterLoad from destination locs 1076 fillchar(TurnsAfterLoad, MapSize, $FF); // -11076 FillChar(TurnsAfterLoad, MapSize, $FF); // -1 1077 1077 while Pile.Get(Loc0, Time0) do 1078 1078 begin // search backward … … 1100 1100 1101 1101 function TToolAI.SeaTransport_MakeGroupPlan( 1102 var TransportPlan: TGroupTransportPlan): boolean;1103 var 1104 V8, i, j, iPicked, uix, Loc0, Time0, Loc1, RecoverTurns, MoveStyle,1102 var TransportPlan: TGroupTransportPlan): Boolean; 1103 var 1104 V8, I, J, iPicked, uix, Loc0, Time0, Loc1, RecoverTurns, MoveStyle, 1105 1105 TurnsLoaded, TurnCount, tuix, tuix1, ArriveTime, TotalDelay, 1106 1106 BestTotalDelay, GroupCount, BestGroupCount, BestLoadLoc, FullMovementLoc, 1107 nSelectedLoad, f, OriginContinent, a, b: integer;1108 CompleteFlag, NotReachedFlag, ContinueUnit: cardinal;1109 IsComplete, ok, IsFirstLoc: boolean;1107 nSelectedLoad, F, OriginContinent, A, B: Integer; 1108 CompleteFlag, NotReachedFlag, ContinueUnit: Cardinal; 1109 IsComplete, ok, IsFirstLoc: Boolean; 1110 1110 StartLocPtr, ArrivedEnd: pinteger; 1111 1111 Adjacent: TVicinity8Loc; 1112 uixSelectedLoad: array[0..15] of integer;1113 tuixSelectedLoad: array[0..15] of integer;1114 Arrived: array[0..lxmax * lymax] of cardinal;1112 uixSelectedLoad: array[0..15] of Integer; 1113 tuixSelectedLoad: array[0..15] of Integer; 1114 Arrived: array[0..lxmax * lymax] of Cardinal; 1115 1115 ResponsibleTransport: array[0..lxmax * lymax - 1] of smallint; 1116 TurnsBeforeLoad: array[0..lxmax * lymax - 1] of shortint;1117 GroupComplete: array[0..lxmax * lymax - 1] of boolean;1118 begin 1119 assert(InitComplete); // call order violation!1116 TurnsBeforeLoad: array[0..lxmax * lymax - 1] of ShortInt; 1117 GroupComplete: array[0..lxmax * lymax - 1] of Boolean; 1118 begin 1119 Assert(InitComplete); // call order violation! 1120 1120 1121 1121 if HaveDestinations and (nTransportLoad > 0) then … … 1128 1128 for tuix := 0 to nTransportLoad - 1 do 1129 1129 begin 1130 Loc_to_ab(MyUnit[uix].Loc, MyUnit[uixTransportLoad[tuix]].Loc, a, b);1131 if (abs( a) <= 1) and (abs(b) <= 1) then1130 Loc_to_ab(MyUnit[uix].Loc, MyUnit[uixTransportLoad[tuix]].Loc, A, B); 1131 if (abs(A) <= 1) and (abs(B) <= 1) then 1132 1132 begin 1133 assert((a <> 0) or (b<> 0));1133 Assert((A <> 0) or (B <> 0)); 1134 1134 Inc(GroupCount); 1135 1135 end; … … 1145 1145 for tuix := nTransportLoad - 1 downto 0 do 1146 1146 begin 1147 Loc_to_ab(TransportPlan.LoadLoc, MyUnit[uixTransportLoad[tuix]].Loc, a, b);1148 if (abs( a) <= 1) and (abs(b) <= 1) then1147 Loc_to_ab(TransportPlan.LoadLoc, MyUnit[uixTransportLoad[tuix]].Loc, A, B); 1148 if (abs(A) <= 1) and (abs(B) <= 1) then 1149 1149 begin 1150 1150 TransportPlan.uixLoad[TransportPlan.nLoad] := uixTransportLoad[tuix]; … … 1153 1153 Inc(TransportPlan.nLoad); 1154 1154 if TransportPlan.nLoad = TransportCapacity then 1155 break;1155 Break; 1156 1156 end; 1157 1157 end; 1158 1158 Result := True; 1159 exit;1159 Exit; 1160 1160 end; 1161 1161 end; … … 1165 1165 begin 1166 1166 // select units from same continent 1167 fillchar(Arrived, 4 * nContinent, 0); // misuse Arrived as counter1167 FillChar(Arrived, 4 * nContinent, 0); // misuse Arrived as counter 1168 1168 for tuix := 0 to nTransportLoad - 1 do 1169 1169 begin 1170 assert(Map[MyUnit[uixTransportLoad[tuix]].Loc] and fTerrain >= fGrass);1171 f:= Formation[MyUnit[uixTransportLoad[tuix]].Loc];1172 if f>= 0 then1173 Inc(Arrived[ f]);1170 Assert(Map[MyUnit[uixTransportLoad[tuix]].Loc] and fTerrain >= fGrass); 1171 F := Formation[MyUnit[uixTransportLoad[tuix]].Loc]; 1172 if F >= 0 then 1173 Inc(Arrived[F]); 1174 1174 end; 1175 1175 OriginContinent := 0; 1176 for f:= 1 to nContinent - 1 do1177 if Arrived[ f] > Arrived[OriginContinent] then1178 OriginContinent := f;1176 for F := 1 to nContinent - 1 do 1177 if Arrived[F] > Arrived[OriginContinent] then 1178 OriginContinent := F; 1179 1179 nSelectedLoad := 0; 1180 1180 for tuix := 0 to nTransportLoad - 1 do … … 1185 1185 Inc(nSelectedLoad); 1186 1186 if nSelectedLoad = 16 then 1187 break;1187 Break; 1188 1188 end; 1189 1189 1190 1190 Pile.Create(MapSize); 1191 fillchar(ResponsibleTransport, MapSize * 2, $FF); // -11192 fillchar(TurnsBeforeLoad, MapSize, $FF); // -11191 FillChar(ResponsibleTransport, MapSize * 2, $FF); // -1 1192 FillChar(TurnsBeforeLoad, MapSize, $FF); // -1 1193 1193 ok := False; 1194 1194 for uix := 0 to RO.nUn - 1 do … … 1203 1203 Result := False; 1204 1204 Pile.Free; 1205 exit;1205 Exit; 1206 1206 end; 1207 1207 while Pile.Get(Loc0, Time0) do … … 1224 1224 end; 1225 1225 1226 fillchar(Arrived, MapSize * 4, $55); // set NotReachedFlag for all tiles1227 fillchar(GroupComplete, MapSize, False);1226 FillChar(Arrived, MapSize * 4, $55); // set NotReachedFlag for all tiles 1227 FillChar(GroupComplete, MapSize, False); 1228 1228 BestLoadLoc := -1; 1229 1229 … … 1232 1232 begin 1233 1233 uix := uixSelectedLoad[tuix]; 1234 if MyUnit[uix].Movement = integer(MyModel[MyUnit[uix].mix].Speed) then1234 if MyUnit[uix].Movement = Integer(MyModel[MyUnit[uix].mix].Speed) then 1235 1235 begin 1236 1236 NotReachedFlag := 1 shl (2 * tuix); … … 1246 1246 if (TurnsBeforeLoad[Loc1] >= 0) and (TurnsAfterLoad[Loc1] >= 0) then 1247 1247 begin 1248 i:= 1;1248 I := 1; 1249 1249 GroupCount := 0; 1250 1250 for tuix1 := 0 to nSelectedLoad - 1 do 1251 1251 begin 1252 if Arrived[loc1] and i= 0 then1252 if Arrived[loc1] and I = 0 then 1253 1253 Inc(GroupCount); 1254 i := ishl 2;1254 I := I shl 2; 1255 1255 end; 1256 assert(GroupCount <= TransportCapacity);1256 Assert(GroupCount <= TransportCapacity); 1257 1257 if (GroupCount = TransportCapacity) or (GroupCount = nSelectedLoad) then 1258 1258 GroupComplete[loc1] := True; … … 1292 1292 begin 1293 1293 Pile.Put(MyUnit[uix].Loc, $1800 - MyUnit[uix].Movement); 1294 if MyUnit[uix].Movement = integer(MyModel[MyUnit[uix].mix].Speed) then1294 if MyUnit[uix].Movement = Integer(MyModel[MyUnit[uix].mix].Speed) then 1295 1295 FullMovementLoc := MyUnit[uix].Loc; 1296 1296 // surrounding tiles can be loaded immediately … … 1308 1308 if StartLocPtr <> ArrivedEnd then 1309 1309 begin 1310 Loc0 := ( integer(StartLocPtr) - integer(@Arrived)) shr 2;1310 Loc0 := (Integer(StartLocPtr) - Integer(@Arrived)) shr 2; 1311 1311 Inc(StartLocPtr); 1312 1312 Time0 := $800; … … 1316 1316 if IsFirstLoc then 1317 1317 ContinueUnit := ContinueUnit and not (1 shl tuix); 1318 break;1318 Break; 1319 1319 end; 1320 1320 IsFirstLoc := False; … … 1323 1323 if not GroupComplete[Loc0] and (Map[Loc0] and fTerrain <> fMountains) then 1324 1324 begin // check whether group complete -- no mountains because complete flag might be faked there 1325 i:= 1;1325 I := 1; 1326 1326 GroupCount := 0; 1327 1327 for tuix1 := 0 to nSelectedLoad - 1 do 1328 1328 begin 1329 if Arrived[Loc0] and i= 0 then1329 if Arrived[Loc0] and I = 0 then 1330 1330 Inc(GroupCount); 1331 i := ishl 2;1331 I := I shl 2; 1332 1332 end; 1333 assert(GroupCount <= TransportCapacity);1333 Assert(GroupCount <= TransportCapacity); 1334 1334 if (GroupCount = TransportCapacity) or (GroupCount = nSelectedLoad) then 1335 1335 GroupComplete[Loc0] := True; … … 1353 1353 if (TurnsBeforeLoad[Loc1] >= 0) and (TurnsAfterLoad[Loc1] >= 0) then 1354 1354 begin 1355 i:= 1;1355 I := 1; 1356 1356 GroupCount := 0; 1357 1357 for tuix1 := 0 to nSelectedLoad - 1 do 1358 1358 begin 1359 if Arrived[loc1] and i= 0 then1359 if Arrived[loc1] and I = 0 then 1360 1360 Inc(GroupCount); 1361 i := ishl 2;1361 I := I shl 2; 1362 1362 end; 1363 assert(GroupCount <= TransportCapacity);1363 Assert(GroupCount <= TransportCapacity); 1364 1364 if (GroupCount = TransportCapacity) or 1365 1365 (GroupCount = nSelectedLoad) then … … 1436 1436 if 1 shl (2 * tuix) and Arrived[BestLoadLoc] = 0 then 1437 1437 begin 1438 assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);1438 Assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]); 1439 1439 TransportPlan.uixLoad[TransportPlan.nLoad] := uixSelectedLoad[tuix]; 1440 1440 uixTransportLoad[tuixSelectedLoad[tuix]] := … … 1444 1444 end; 1445 1445 Result := True; 1446 exit;1446 Exit; 1447 1447 end; 1448 1448 … … 1451 1451 for tuix := nSelectedLoad - 1 downto 0 do 1452 1452 begin 1453 assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]);1453 Assert(uixTransportLoad[tuixSelectedLoad[tuix]] = uixSelectedLoad[tuix]); 1454 1454 uixTransportLoad[tuixSelectedLoad[tuix]] := 1455 1455 uixTransportLoad[nTransportLoad - 1]; … … 1464 1464 // Misc 1465 1465 1466 function TToolAI.CurrentMStrength(Domain: integer): integer;1467 var 1468 i: integer;1466 function TToolAI.CurrentMStrength(Domain: Integer): Integer; 1467 var 1468 I: Integer; 1469 1469 begin 1470 1470 Result := 0; 1471 for i:= 0 to nUpgrade - 1 do1472 with upgrade[Domain, i] do1471 for I := 0 to nUpgrade - 1 do 1472 with upgrade[Domain, I] do 1473 1473 if (Preq = preNone) or (Preq >= 0) and 1474 1474 ((RO.Tech[Preq] >= tsApplicable) or (Preq in FutureTech) and … … 1486 1486 procedure SetAdvancedness; 1487 1487 var 1488 ad, j, Reduction, AgeThreshold: integer;1489 known: array[0..nAdv - 1] of integer;1490 1491 procedure MarkPreqs(ad: integer);1488 ad, J, Reduction, AgeThreshold: Integer; 1489 known: array[0..nAdv - 1] of Integer; 1490 1491 procedure MarkPreqs(ad: Integer); 1492 1492 var 1493 i: integer;1493 I: Integer; 1494 1494 begin 1495 1495 if known[ad] = 0 then 1496 1496 begin 1497 1497 known[ad] := 1; 1498 for i:= 0 to 2 do1499 if AdvPreq[ad, i] >= 0 then1500 MarkPreqs(AdvPreq[ad, i]);1498 for I := 0 to 2 do 1499 if AdvPreq[ad, I] >= 0 then 1500 MarkPreqs(AdvPreq[ad, I]); 1501 1501 end; 1502 1502 end; … … 1508 1508 FillChar(known, SizeOf(known), 0); 1509 1509 MarkPreqs(ad); 1510 for j:= 0 to nAdv - 1 do1511 if known[ j] > 0 then1510 for J := 0 to nAdv - 1 do 1511 if known[J] > 0 then 1512 1512 Inc(Advancedness[ad]); 1513 1513 end;
Note:
See TracChangeset
for help on using the changeset viewer.