1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
|
{$INCLUDE Switches.inc}
unit ClientTools;
interface
uses
Protocol;
const
nOfferedResourceWeights = 6;
OfferedResourceWeights: array [0 .. nOfferedResourceWeights - 1] of cardinal =
(rwOff, rwMaxScience, rwForceScience, rwMaxGrowth, rwForceProd, rwMaxProd);
type
TImpOrder = array [0 .. (nImp + 4) div 4 * 4 - 1] of shortint;
TEnhancementJobs = array [0 .. 11, 0 .. 7] of byte;
JobResultSet = set of 0 .. 39;
TMapOption = (
// options switched by buttons
moPolitical = 0, moCityNames = 1, moGreatWall = 4, moGrid = 5, moBareTerrain = 6,
// other options
moEditMode = 16, moLocCodes = 17
);
TMapOptions = set of TMapOption;
TSaveOption = (soAlEffectiveMovesOnly = 0, soEnMoves = 1, soEnAttacks = 2,
soEnNoMoves = 3, soWaitTurn = 4, soEffectiveMovesOnly = 5, soEnFastMoves = 6,
soSlowMoves = 7, soFastMoves = 8, soVeryFastMoves = 9, soNames = 10,
soRepList = 11, soRepScreens = 12, soSoundOff = 13, soSoundOn = 14,
soSoundOnAlt = 15, soScrollSlow = 16, soScrollFast = 17, soScrollOff = 18,
soAlSlowMoves = 19, soAlFastMoves = 20, somAlNoMoves = 21, soTellAI = 30);
TSaveOptions = set of TSaveOption;
var
Server: TServerCall;
G: TNewGameData;
me: integer;
MyRO: ^TPlayerContext;
MyMap: ^TTileList;
MyUn: ^TUnList;
MyCity: ^TCityList;
MyModel: ^TModelList;
AdvValue: array [0 .. nAdv - 1] of integer;
function dLoc(Loc, dx, dy: integer): integer;
function Distance(Loc0, Loc1: integer): integer;
function UnrestAtLoc(uix, Loc: integer): boolean;
function GetMoveAdvice(uix, ToLoc: integer;
var MoveAdviceData: TMoveAdviceData): integer;
function ColorOfHealth(Health: integer): integer;
function IsMultiPlayerGame: boolean;
procedure ItsMeAgain(p: integer);
function GetAge(p: integer): integer;
function IsCivilReportNew(Enemy: integer): boolean;
function IsMilReportNew(Enemy: integer): boolean;
function CutCityFoodSurplus(FoodSurplus: integer; IsCityAlive: boolean;
gov, size: integer): integer;
function CityTaxBalance(cix: integer; const CityReport: TCityReportNew): integer;
procedure SumCities(var TaxSum, ScienceSum: integer);
function JobTest(uix, Job: integer; IgnoreResults: JobResultSet = []): boolean;
procedure GetUnitInfo(Loc: integer; var uix: integer; var UnitInfo: TUnitInfo);
procedure GetCityInfo(Loc: integer; var cix: integer; var CityInfo: TCityInfo);
function UnitExhausted(uix: integer): boolean;
function ModelHash(const ModelInfo: TModelInfo): integer;
function ProcessEnhancement(uix: integer; const Jobs: TEnhancementJobs): integer;
function AutoBuild(cix: integer; const ImpOrder: TImpOrder): boolean;
procedure DebugMessage(Level: integer; Text: string);
procedure CityOptimizer_BeginOfTurn;
procedure CityOptimizer_CityChange(cix: integer);
procedure CityOptimizer_TileBecomesAvailable(Loc: integer);
procedure CityOptimizer_ReleaseCityTiles(cix, ReleasedTiles: integer);
procedure CityOptimizer_BeforeRemoveUnit(uix: integer);
procedure CityOptimizer_AfterRemoveUnit;
procedure CityOptimizer_EndOfTurn;
function GetMyCityByLoc(Loc: Integer): PCity;
function GetEnemyCityByLoc(Loc: Integer): PCityInfo;
function GetMyUnitByLoc(Loc: Integer): PUn;
function GetEnemyUnitByLoc(Loc: Integer): PUnitInfo;
implementation
var
CityNeedsOptimize: array [0 .. ncmax - 1] of boolean;
function dLoc(Loc, dx, dy: integer): integer;
var
y0: integer;
begin
y0 := (Loc + G.lx * 1024) div G.lx - 1024;
Result := (Loc + (dx + y0 and 1 + G.lx * 1024) shr 1) mod G.lx + G.lx * (y0 + dy);
end;
function Distance(Loc0, Loc1: integer): integer;
var
dx, dy: integer;
begin
Inc(Loc0, G.lx * 1024);
Inc(Loc1, G.lx * 1024);
dx := abs(((Loc1 mod G.lx * 2 + Loc1 div G.lx and 1) -
(Loc0 mod G.lx * 2 + Loc0 div G.lx and 1) + 3 * G.lx) mod (2 * G.lx) - G.lx);
dy := abs(Loc1 div G.lx - Loc0 div G.lx);
Result := dx + dy + abs(dx - dy) shr 1;
end;
function UnrestAtLoc(uix, Loc: integer): boolean;
var
uix1: integer;
begin
Result := False;
if MyModel[MyUn[uix].mix].Flags and mdCivil = 0 then
case MyRO.Government of
gRepublic, gFuture:
Result := (MyRO.Territory[Loc] >= 0) and (MyRO.Territory[Loc] <> me) and
(MyRO.Treaty[MyRO.Territory[Loc]] < trAlliance);
gDemocracy:
Result := (MyRO.Territory[Loc] < 0) or (MyRO.Territory[Loc] <> me) and
(MyRO.Treaty[MyRO.Territory[Loc]] < trAlliance);
end;
with MyModel[MyUn[uix].mix] do
if Cap[mcSeaTrans] + Cap[mcAirTrans] + Cap[mcCarrier] > 0 then
for uix1 := 0 to MyRO.nUn - 1 do // check transported units too
if (MyUn[uix1].Loc >= 0) and (MyUn[uix1].Master = uix) then
Result := Result or UnrestAtLoc(uix1, Loc);
end;
function GetMoveAdvice(uix, ToLoc: integer;
var MoveAdviceData: TMoveAdviceData): integer;
var
MinEndHealth: integer;
begin
if MyModel[MyUn[uix].mix].Domain = dGround then
MinEndHealth := 100
else
MinEndHealth := 1; // resistent to hostile terrain -- don't consider
repeat
if MyUn[uix].Health >= MinEndHealth then
begin
MoveAdviceData.ToLoc := ToLoc;
MoveAdviceData.MoreTurns := 999;
MoveAdviceData.MaxHostile_MovementLeft := MyUn[uix].Health - MinEndHealth;
Result := Server(sGetMoveAdvice, me, uix, MoveAdviceData);
if (MinEndHealth <= 1) or (Result <> eNoWay) then
exit;
end;
case MinEndHealth of
100:
MinEndHealth := 50;
50:
MinEndHealth := 25;
25:
MinEndHealth := 12;
else
MinEndHealth := 1
end;
until False;
end;
function ColorOfHealth(Health: integer): integer;
var
red, green: integer;
begin
green := 400 * Health div 100;
if green > 200 then
green := 200;
red := 510 * (100 - Health) div 100;
if red > 255 then
red := 255;
Result := green shl 8 + red;
end;
function IsMultiPlayerGame: boolean;
var
p1: integer;
begin
Result := False;
for p1 := 1 to nPl - 1 do
if G.RO[p1] <> nil then
Result := True;
end;
procedure ItsMeAgain(p: integer);
begin
if G.RO[p] <> nil then
MyRO := pointer(G.RO[p])
else if G.SuperVisorRO[p] <> nil then
MyRO := pointer(G.SuperVisorRO[p])
else
exit;
me := p;
MyMap := pointer(MyRO.Map);
MyUn := pointer(MyRO.Un);
MyCity := pointer(MyRO.City);
MyModel := pointer(MyRO.Model);
end;
function GetAge(p: integer): integer;
var
i: integer;
begin
if p = me then begin
Result := 0;
for i := 1 to 3 do
if MyRO.Tech[AgePreq[i]] >= tsApplicable then
Result := i;
end else begin
Result := 0;
for i := 1 to 3 do
if MyRO.EnemyReport[p].Tech[AgePreq[i]] >= tsApplicable then
Result := i;
end;
end;
function IsCivilReportNew(Enemy: integer): boolean;
var
i: integer;
begin
assert(Enemy <> me);
i := MyRO.EnemyReport[Enemy].TurnOfCivilReport;
Result := (i = MyRO.Turn) or (i = MyRO.Turn - 1) and (Enemy > me);
end;
function IsMilReportNew(Enemy: integer): boolean;
var
i: integer;
begin
assert(Enemy <> me);
i := MyRO.EnemyReport[Enemy].TurnOfMilReport;
Result := (i = MyRO.Turn) or (i = MyRO.Turn - 1) and (Enemy > me);
end;
function CutCityFoodSurplus(FoodSurplus: integer; IsCityAlive: boolean;
gov, size: integer): integer;
begin
Result := FoodSurplus;
if not IsCityAlive or (Result > 0) and ((gov = gFuture) or
(size >= NeedAqueductSize) and (Result < 2)) then
Result := 0; { no growth }
end;
function CityTaxBalance(cix: integer; const CityReport: TCityReportNew): integer;
var
i: integer;
begin
Result := 0;
if (CityReport.HappinessBalance >= 0) { no disorder } and
(MyCity[cix].Flags and chCaptured = 0) then // not captured
begin
Inc(Result, CityReport.Tax);
if (MyCity[cix].Project and (cpImp + cpIndex) = cpImp + imTrGoods) and
(CityReport.Production > 0) then
Inc(Result, CityReport.Production);
if ((MyRO.Government = gFuture) or (MyCity[cix].size >=
NeedAqueductSize) and (CityReport.FoodSurplus < 2)) and
(CityReport.FoodSurplus > 0) then
Inc(Result, CityReport.FoodSurplus);
end;
for i := nWonder to nImp - 1 do
if MyCity[cix].Built[i] > 0 then
Dec(Result, Imp[i].Maint);
end;
procedure SumCities(var TaxSum, ScienceSum: integer);
var
cix: integer;
CityReport: TCityReportNew;
begin
TaxSum := MyRO.OracleIncome;
ScienceSum := 0;
if MyRO.Government = gAnarchy then
exit;
for cix := 0 to MyRO.nCity - 1 do
if MyCity[cix].Loc >= 0 then
begin
CityReport.HypoTiles := -1;
CityReport.HypoTaxRate := -1;
CityReport.HypoLuxuryRate := -1;
Server(sGetCityReportNew, me, cix, CityReport);
if (CityReport.HappinessBalance >= 0) { no disorder } and
(MyCity[cix].Flags and chCaptured = 0) then // not captured
ScienceSum := ScienceSum + CityReport.Science;
TaxSum := TaxSum + CityTaxBalance(cix, CityReport);
end;
end;
function JobTest(uix, Job: integer; IgnoreResults: JobResultSet): boolean;
var
Test: integer;
begin
Test := Server(sStartJob + Job shl 4 - sExecute, me, uix, nil^);
Result := (Test >= rExecuted) or (Test in IgnoreResults);
end;
procedure GetUnitInfo(Loc: integer; var uix: integer; var UnitInfo: TUnitInfo);
var
i, Cnt: integer;
begin
if MyMap[Loc] and fOwned <> 0 then
begin
Server(sGetDefender, me, Loc, uix);
Cnt := 0;
for i := 0 to MyRO.nUn - 1 do
if MyUn[i].Loc = Loc then
Inc(Cnt);
MakeUnitInfo(me, MyUn[uix], UnitInfo);
if Cnt > 1 then
UnitInfo.Flags := UnitInfo.Flags or unMulti;
end
else
begin
uix := MyRO.nEnemyUn - 1;
while (uix > 0) and (MyRO.EnemyUn[uix].Loc <> Loc) do
Dec(uix);
UnitInfo := MyRO.EnemyUn[uix];
end;
end;
procedure GetCityInfo(Loc: integer; var cix: integer; var CityInfo: TCityInfo);
begin
if MyMap[Loc] and fOwned <> 0 then
begin
CityInfo.Loc := Loc;
cix := MyRO.nCity - 1;
while (cix >= 0) and (MyCity[cix].Loc <> Loc) do
Dec(cix);
with CityInfo do
begin
Owner := me;
ID := MyCity[cix].ID;
size := MyCity[cix].size;
Flags := 0;
if MyCity[cix].Built[imPalace] > 0 then
Inc(Flags, ciCapital);
if (MyCity[cix].Built[imWalls] > 0) or
(MyMap[MyCity[cix].Loc] and fGrWall <> 0) then
Inc(Flags, ciWalled);
if MyCity[cix].Built[imCoastalFort] > 0 then
Inc(Flags, ciCoastalFort);
if MyCity[cix].Built[imMissileBat] > 0 then
Inc(Flags, ciMissileBat);
if MyCity[cix].Built[imBunker] > 0 then
Inc(Flags, ciBunker);
if MyCity[cix].Built[imSpacePort] > 0 then
Inc(Flags, ciSpacePort);
end;
end
else
begin
cix := MyRO.nEnemyCity - 1;
while (cix >= 0) and (MyRO.EnemyCity[cix].Loc <> Loc) do
Dec(cix);
CityInfo := MyRO.EnemyCity[cix];
end;
end;
function UnitExhausted(uix: integer): boolean;
// check if another move of this unit is still possible
var
dx, dy: integer;
begin
Result := True;
if (MyUn[uix].Movement > 0) or
(MyRO.Wonder[woShinkansen].EffectiveOwner = me) then
if (MyUn[uix].Movement >= 100) or
((MyModel[MyUn[uix].mix].Kind = mkCaravan) and
(MyMap[MyUn[uix].Loc] and fCity <> 0)) then
Result := False
else
for dx := -2 to 2 do
for dy := -2 to 2 do
if abs(dx) + abs(dy) = 2 then
if Server(sMoveUnit - sExecute + dx and 7 shl 4 + dy and
7 shl 7, me, uix, nil^) >= rExecuted then
Result := False;
end;
function ModelHash(const ModelInfo: TModelInfo): integer;
var
i, FeatureCode, Hash1, Hash2, Hash2r, d: cardinal;
begin
Hash1 := 0;
Hash2 := 0;
with ModelInfo do
if Kind > mkEnemyDeveloped then
Result := integer($C0000000 + Speed div 50 + Kind shl 8)
else
begin
FeatureCode := 0;
for i := mcFirstNonCap to nFeature - 1 do
if 1 shl Domain and Feature[i].Domains <> 0 then
begin
FeatureCode := FeatureCode * 2;
if 1 shl (i - mcFirstNonCap) <> 0 then
Inc(FeatureCode);
end;
case Domain of
dGround:
begin
assert(FeatureCode < 1 shl 8);
assert(Attack < 5113);
assert(Defense < 2273);
assert(Cost < 1611);
Hash1 := (Attack * 2273 + Defense) * 9 + (Speed - 150) div 50;
Hash2 := FeatureCode * 1611 + Cost;
end;
dSea:
begin
assert(FeatureCode < 1 shl 9);
assert(Attack < 12193);
assert(Defense < 6097);
assert(Cost < 4381);
Hash1 := ((Attack * 6097 + Defense) * 5 +
(Speed - 350) div 100) * 2;
if Weight >= 6 then
Inc(Hash1);
Hash2 := ((TTrans * 17 + ATrans_Fuel) shl 9 + FeatureCode) *
4381 + Cost;
end;
dAir:
begin
assert(FeatureCode < 1 shl 5);
assert(Attack < 2407);
assert(Defense < 1605);
assert(Bombs < 4813);
assert(Cost < 2089);
Hash1 := (Attack * 1605 + Defense) shl 5 + FeatureCode;
Hash2 := ((Bombs * 7 + ATrans_Fuel) * 4 + TTrans) * 2089 + Cost;
end;
end;
Hash2r := 0;
for i := 0 to 7 do
begin
Hash2r := Hash2r * 13;
d := Hash2 div 13;
Inc(Hash2r, Hash2 - d * 13);
Hash2 := d;
end;
Result := integer(Domain shl 30 + Hash1 xor Hash2r);
end;
end;
function ProcessEnhancement(uix: integer; const Jobs: TEnhancementJobs): integer;
{ return values:
eJobDone - all applicable jobs done
eOK - enhancement not complete
eDied - job done and died (thurst) }
var
stage, NextJob, Tile: integer;
Done: set of jNone .. jPoll;
begin
Done := [];
Tile := MyMap[MyUn[uix].Loc];
if Tile and fRoad <> 0 then
include(Done, jRoad);
if Tile and fRR <> 0 then
include(Done, jRR);
if (Tile and fTerImp = tiIrrigation) or (Tile and fTerImp = tiFarm) then
include(Done, jIrr);
if Tile and fTerImp = tiFarm then
include(Done, jFarm);
if Tile and fTerImp = tiMine then
include(Done, jMine);
if Tile and fPoll = 0 then
include(Done, jPoll);
if MyUn[uix].Job = jNone then
Result := eJobDone
else
Result := eOK;
while (Result <> eOK) and (Result <> eDied) do
begin
stage := -1;
repeat
if stage = -1 then
NextJob := jPoll
else
NextJob := Jobs[Tile and fTerrain, stage];
if (NextJob = jNone) or not (NextJob in Done) then
Break;
Inc(stage);
until stage = 5;
if (stage = 5) or (NextJob = jNone) then
begin
Result := eJobDone;
Break;
end; // tile enhancement complete
Result := Server(sStartJob + NextJob shl 4, me, uix, nil^);
include(Done, NextJob);
end;
end;
function AutoBuild(cix: integer; const ImpOrder: TImpOrder): boolean;
var
i, NewProject: integer;
begin
Result := False;
if (MyCity[cix].Project and (cpImp + cpIndex) = cpImp + imTrGoods) or
(MyCity[cix].Flags and chProduction <> 0) then
begin
i := 0;
repeat
while (ImpOrder[i] >= 0) and (MyCity[cix].Built[ImpOrder[i]] > 0) do
Inc(i);
if ImpOrder[i] < 0 then
Break;
assert(i < nImp);
NewProject := cpImp + ImpOrder[i];
if Server(sSetCityProject, me, cix, NewProject) >= rExecuted then
begin
Result := True;
CityOptimizer_CityChange(cix);
Break;
end;
Inc(i);
until False;
end;
end;
procedure CalculateAdvValues;
var
i, j: integer;
known: array [0 .. nAdv - 1] of integer;
procedure MarkPreqs(i: integer);
begin
if known[i] = 0 then
begin
known[i] := 1;
if (i <> adScience) and (i <> adMassProduction) then
begin
if (AdvPreq[i, 0] >= 0) then
MarkPreqs(AdvPreq[i, 0]);
if (AdvPreq[i, 1] >= 0) then
MarkPreqs(AdvPreq[i, 1]);
end;
end;
end;
begin
FillChar(AdvValue, SizeOf(AdvValue), 0);
for i := 0 to nAdv - 1 do
begin
FillChar(known, SizeOf(known), 0);
MarkPreqs(i);
for j := 0 to nAdv - 1 do
if known[j] > 0 then
Inc(AdvValue[i]);
if i in FutureTech then
Inc(AdvValue[i], 3000)
else if known[adMassProduction] > 0 then
Inc(AdvValue[i], 2000)
else if known[adScience] > 0 then
Inc(AdvValue[i], 1000);
end;
end;
procedure DebugMessage(Level: integer; Text: string);
begin
Server(sMessage, me, Level, PChar(Text)^);
end;
function MarkCitiesAround(Loc, cixExcept: integer): boolean;
// return whether a city was marked
var
cix: integer;
begin
Result := False;
for cix := 0 to MyRO.nCity - 1 do
if (cix <> cixExcept) and (MyCity[cix].Loc >= 0) and
(MyCity[cix].Flags and chCaptured = 0) and
(Distance(MyCity[cix].Loc, Loc) <= 5) then
begin
CityNeedsOptimize[cix] := True;
Result := True;
end;
end;
procedure OptimizeCities(CheckOnly: boolean);
var
cix, fix, dx, dy, Loc1, OptiType: integer;
Done: boolean;
Advice: TCityTileAdviceData;
begin
repeat
Done := True;
for cix := 0 to MyRO.nCity - 1 do
if CityNeedsOptimize[cix] then
begin
OptiType := (MyCity[cix].Status shr 4) and $0F;
if OptiType <> 0 then
begin
Advice.ResourceWeights := OfferedResourceWeights[OptiType];
Server(sGetCityTileAdvice, me, cix, Advice);
if Advice.Tiles <> MyCity[cix].Tiles then
if CheckOnly then
begin
// TODO: What is this assert for?
// Need to optimize city tiles but CheckOnly true?
//assert(false)
end
else
begin
for fix := 1 to 26 do
if MyCity[cix].Tiles and not Advice.Tiles and
(1 shl fix) <> 0 then
begin // tile no longer used by this city -- check using it by another
dy := fix shr 2 - 3;
dx := fix and 3 shl 1 - 3 + (dy + 3) and 1;
Loc1 := dLoc(MyCity[cix].Loc, dx, dy);
if MarkCitiesAround(Loc1, cix) then
Done := False;
end;
Server(sSetCityTiles, me, cix, Advice.Tiles);
end;
end;
CityNeedsOptimize[cix] := False;
end;
until Done;
end;
procedure CityOptimizer_BeginOfTurn;
var
cix: integer;
begin
FillChar(CityNeedsOptimize, MyRO.nCity - 1, 0); // false
if MyRO.Government <> gAnarchy then
begin
for cix := 0 to MyRO.nCity - 1 do
if (MyCity[cix].Loc >= 0) and (MyCity[cix].Flags and chCaptured = 0)
then
CityNeedsOptimize[cix] := True;
OptimizeCities(False); // optimize all cities
end;
end;
procedure CityOptimizer_CityChange(cix: integer);
begin
if cix < 0 then
{ unsupported units need no0 change }
else
if (MyRO.Government <> gAnarchy) and (cix <> -1) and (MyCity[cix].Flags and
chCaptured = 0) then
begin
CityNeedsOptimize[cix] := True;
OptimizeCities(False);
end;
end;
procedure CityOptimizer_TileBecomesAvailable(Loc: integer);
begin
if (MyRO.Government <> gAnarchy) and MarkCitiesAround(Loc, -1) then
OptimizeCities(False);
end;
procedure CityOptimizer_ReleaseCityTiles(cix, ReleasedTiles: integer);
var
fix, dx, dy, Loc1: integer;
Done: boolean;
begin
if (MyRO.Government <> gAnarchy) and (ReleasedTiles <> 0) then
begin
Done := True;
for fix := 1 to 26 do
if ReleasedTiles and (1 shl fix) <> 0 then
begin
dy := fix shr 2 - 3;
dx := fix and 3 shl 1 - 3 + (dy + 3) and 1;
Loc1 := dLoc(MyCity[cix].Loc, dx, dy);
if MarkCitiesAround(Loc1, cix) then
Done := False;
end;
if not Done then
OptimizeCities(False);
end;
end;
procedure CityOptimizer_BeforeRemoveUnit(uix: integer);
var
uix1: integer;
begin
if MyRO.Government <> gAnarchy then
begin
if MyUn[uix].Home >= 0 then
CityNeedsOptimize[MyUn[uix].Home] := True;
// transported units are also removed
for uix1 := 0 to MyRO.nUn - 1 do
if (MyUn[uix1].Loc >= 0) and (MyUn[uix1].Master = uix) and
(MyUn[uix1].Home >= 0) then
CityNeedsOptimize[MyUn[uix1].Home] := True;
end;
end;
procedure CityOptimizer_AfterRemoveUnit;
begin
if MyRO.Government <> gAnarchy then
OptimizeCities(False);
end;
procedure CityOptimizer_EndOfTurn;
// all cities should already be optimized here -- only check this
var
cix: integer;
begin
{$IFOPT O-}
if MyRO.Government <> gAnarchy then
begin
FillChar(CityNeedsOptimize, MyRO.nCity - 1, 0); // false
for cix := 0 to MyRO.nCity - 1 do
if (MyCity[cix].Loc >= 0) and (MyCity[cix].Flags and chCaptured = 0)
then
CityNeedsOptimize[cix] := True;
OptimizeCities(True); // check all cities
end;
{$ENDIF}
end;
function GetMyCityByLoc(Loc: Integer): PCity;
var
I: Integer;
begin
I := MyRO.nCity - 1;
while (I >= 0) and (MyCity[I].Loc <> Loc) do Dec(I);
if I >= 0 then Result := @MyCity[I]
else Result := nil;
end;
function GetEnemyCityByLoc(Loc: Integer): PCityInfo;
var
I: Integer;
begin
I := MyRO.nEnemyCity - 1;
while (I >= 0) and (MyRo.EnemyCity[I].Loc <> Loc) do Dec(I);
if I >= 0 then Result := @MyRo.EnemyCity[I]
else Result := nil;
end;
function GetMyUnitByLoc(Loc: Integer): PUn;
var
I: Integer;
begin
I := MyRO.nUn - 1;
while (I >= 0) and (MyUn[I].Loc <> Loc) do Dec(I);
if I >= 0 then Result := @MyUn[I]
else Result := nil;
end;
function GetEnemyUnitByLoc(Loc: Integer): PUnitInfo;
var
I: Integer;
begin
I := MyRO.nEnemyUn - 1;
while (I >= 0) and (MyRO.EnemyUn[I].Loc <> Loc) do Dec(I);
if I >= 0 then Result := @MyRO.EnemyUn[I]
else Result := nil;
end;
initialization
Assert(nImp < 128);
CalculateAdvValues;
end.
|