File: CityScreen.pas

package info (click to toggle)
c-evo-dh 3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,548 kB
  • sloc: pascal: 57,426; xml: 256; makefile: 114; sh: 4
file content (1814 lines) | stat: -rw-r--r-- 58,456 bytes parent folder | download | duplicates (2)
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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
{$INCLUDE Switches.inc}
unit CityScreen;

interface

uses
  {$IFDEF UNIX}LMessages,{$ENDIF}
  Protocol, ClientTools, Term, ScreenTools, IsoEngine, BaseWin,
  LCLIntf, LCLType, Messages, SysUtils, Classes, Graphics, Controls, Forms, ExtCtrls,
  ButtonA, ButtonC, Area, GraphType, UTexture;

const
  WM_PLAYSOUND = WM_USER;

type
  TCityCloseAction = (None, RestoreFocus, StepFocus);
  TSmallMapMode = (smSupportedUnits, smImprovements);

  TCityDlg = class(TBufferedDrawDlg)
    Timer1: TTimer;
    CloseBtn: TButtonA;
    PrevCityBtn: TButtonC2;
    NextCityBtn: TButtonC2;
    PageUpBtn: TButtonC;
    PageDownBtn: TButtonC;
    BuyBtn: TButtonC2;
    ProjectArea: TArea;
    PrimacyArea: TArea;
    Imp2Area: TArea;
    Imp4Area: TArea;
    Imp0Area: TArea;
    Imp3Area: TArea;
    Imp5Area: TArea;
    Imp1Area: TArea;
    Pop0Area: TArea;
    Pop1Area: TArea;
    SupportArea: TArea;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; x, y: integer);
    procedure BuyClick(Sender: TObject);
    procedure CloseBtnClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Timer1Timer(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure NextCityBtnClick(Sender: TObject);
    procedure PrevCityBtnClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    // procedure AdviceBtnClick(Sender: TObject);
    procedure PageUpBtnClick(Sender: TObject);
    procedure PageDownBtnClick(Sender: TObject);
  private
    c: TCity;
    Report: TCityReportNew;
    cOwner: Integer;
    cGov: Integer;
    emix: Integer; { enemy model index of produced unit }
    cix: Integer;
    cLoc: Integer;
    SmallMapMode: TSmallMapMode;
    ZoomArea: Integer;
    Page: Integer;
    PageCount: Integer;
    BlinkTime: Integer;
    OpenSoundEvent: Integer;
    SizeClass: Integer;
    AgePrepared: Integer;
    Optimize_cixTileChange: Integer;
    Optimize_TilesBeforeChange: Integer;
    Happened: cardinal;
    imix: array [0 .. 15] of integer;
    CityAreaInfo: TCityAreaInfo;
    AreaMap: TIsoMap;
    NoMap: TIsoMap;
    CityMapTemplate: TBitmap;
    SmallCityMapTemplate: TBitmap;
    Back: TBitmap;
    SmallCityMap: TBitmap;
    ZoomCityMap: TBitmap;
    Template: TBitmap;
    IsPort: Boolean;
    ProdHint: Boolean;
    AllowChange: Boolean;
    RedTex: TTexture;
    BarTex: TTexture;
    procedure InitSmallCityMap;
    procedure InitZoomCityMap;
    procedure ChooseProject;
    procedure ChangeCity(d: integer);
    procedure ChangeResourceWeights(iResourceWeights: integer);
    procedure OnPlaySound(var Msg: TMessage); message WM_PLAYSOUND;
  public
    RestoreUnFocus: Integer;
    CloseAction: TCityCloseAction;
    procedure OffscreenPaint; override;
    procedure ShowNewContent(NewMode: TWindowMode; Loc: integer; ShowEvent: cardinal);
    procedure Reset;
    procedure CheckAge;
  end;

var
  CityDlg: TCityDlg;


implementation

uses
  Select, Messg, MessgEx, Help, Tribes, Directories, Math, Sound;

{$R *.lfm}

const
  wBar = 106;
  xDiv = 400;
  xService = 296;
  xmArea = 197;
  ymArea = 170;
  xView = 326;
  yView = 275;
  dxBar = wBar + 12;
  dyBar = 39;
  xHapp = 404;
  yHapp = 9;
  xFood = 404;
  yFood = yHapp + 3 * dyBar + 6;
  xProd = 404;
  yProd = yFood + 3 * dyBar + 6;
  xTrade = 404;
  yTrade = yProd + 2 * dyBar + 22;
  xPoll = xmArea - 186;
  yPoll = ymArea + 64;
  xmOpt = 40;
  ymOpt = ymArea + 96 + 34;
  xSmallMap = 271;
  ySmallMap = 339;
  wSmallMap = 98;
  hSmallMap = 74;
  xSupport = xSmallMap;
  ySupport = ySmallMap + hSmallMap + 2;
  wSupport = 64;
  hSupport = 18;
  xZoomMap = 34;
  yZoomMap = 338;
  wZoomMap = 228;
  hZoomMap = 124;
  wZoomEnvironment = 68;

  ImpPosition: array [28 .. nImp - 1] of Integer = (
    -1, // imTrGoods
    21, // imBarracks
    6, // imGranary
    1, // imTemple
    7, // imMarket
    14, // imLibrary
    8, // imCourt
    18, // imWalls
    10, // imAqueduct
    11, // imBank
    5, // imCathedral
    13, // imUniversity
    29, // imHarbor
    2, // imTheater
    24, // imFactory
    25, // imMfgPlant
    28, // imRecycling
    27, // imPower
    27, // imHydro
    27, // imNuclear
    26, // imPlatform
    8, // imTownHall
    10, // imSewer
    3, // imSupermarket
    17, // imHighways
    15, // imResLab
    19, // imMissileBat
    23, // imCoastalFort
    22, // imAirport
    20, // imDockyard
    8, // imPalace
    -1, // imGrWall
    4, // imColosseum
    16, // imObservatory
    21, // imMilAcademy
    -1, // imBunker
    -1, // imAlgae
    9, // imStockEx
    -1, // imSpacePort
    -1, // imShipComp
    -1, // imShipPow
    -1); // imShipHab

var
  ImpSorted: array [0 .. nImp - 1] of Integer;

procedure TCityDlg.FormCreate(Sender: TObject);
begin
  inherited;
  RedTex := TTexture.Create;
  BarTex := TTexture.Create;
  NoMap := TIsoMap.Create;
  AreaMap := TIsoMap.Create;
  AreaMap.SetOutput(offscreen);
  AreaMap.SetPaintBounds(xmArea - 192, ymArea - 96 - 32, xmArea + 192,
    ymArea + 96);
  SmallMapMode := smImprovements;
  ZoomArea := 1;
  ProdHint := False;
  RestoreUnFocus := -1;
  OpenSoundEvent := -1;
  AgePrepared := -2;
  Optimize_cixTileChange := -1;
  InitButtons;
  // InitWindowRegion;
  CloseBtn.Caption := Phrases.Lookup('BTN_OK');
  BuyBtn.Hint := Phrases.Lookup('BTN_BUY');
  if not Phrases2FallenBackToEnglish then
    SupportArea.Hint := Phrases2.Lookup('TIP_SUPUNITS')
  else
    SupportArea.Hint := Phrases.Lookup('SUPUNITS');
  if not Phrases2FallenBackToEnglish then
  begin
    Pop0Area.Hint := Phrases2.Lookup('TIP_WORKING');
    Pop1Area.Hint := Phrases2.Lookup('TIP_CIVIL');
    PrimacyArea.Hint := Phrases2.Lookup('TIP_PRIMACY');
    ProjectArea.Hint := Phrases2.Lookup('TIP_PROJECT');
  end;

  Back := TBitmap.Create;
  Back.PixelFormat := pf24bit;
  Back.SetSize(Width, Height);
  Back.Canvas.FillRect(0, 0, Back.Width, Back.Height);
  Template := TBitmap.Create;
  Template.PixelFormat := pf24bit;
  LoadGraphicFile(Template, GetGraphicsDir + DirectorySeparator + 'City.png',
    [gfNoGamma]);
  CityMapTemplate := TBitmap.Create;
  CityMapTemplate.PixelFormat := pf24bit;
  LoadGraphicFile(CityMapTemplate, GetGraphicsDir + DirectorySeparator + 'BigCityMap.png',
    [gfNoGamma]);
  SmallCityMapTemplate := TBitmap.Create;
  SmallCityMapTemplate.PixelFormat := pf24bit;
  LoadGraphicFile(SmallCityMapTemplate, GetGraphicsDir + DirectorySeparator + 'SmallCityMap.png',
    [gfNoGamma]);
  SmallCityMap := TBitmap.Create;
  SmallCityMap.PixelFormat := pf24bit;
  SmallCityMap.SetSize(98, 74);
  SmallCityMap.Canvas.FillRect(0, 0, SmallCityMap.Width, SmallCityMap.Height);
  ZoomCityMap := TBitmap.Create;
  ZoomCityMap.PixelFormat := pf24bit;
  ZoomCityMap.SetSize(228, 124);
  ZoomCityMap.Canvas.FillRect(0, 0, ZoomCityMap.Width, ZoomCityMap.Height);
end;

procedure TCityDlg.FormDestroy(Sender: TObject);
begin
  FreeAndNil(NoMap);
  FreeAndNil(AreaMap);
  FreeAndNil(SmallCityMap);
  FreeAndNil(ZoomCityMap);
  FreeAndNil(SmallCityMapTemplate);
  FreeAndNil(CityMapTemplate);
  FreeAndNil(Template);
  FreeAndNil(Back);
  FreeAndNil(RedTex);
  FreeAndNil(BarTex);
end;

procedure TCityDlg.Reset;
begin
  SmallMapMode := smImprovements;
  ZoomArea := 1;
end;

procedure TCityDlg.CheckAge;
begin
  if MainTexture.Age <> AgePrepared then begin
    AgePrepared := MainTexture.Age;

    UnshareBitmap(Back);
    BitBltCanvas(Back.Canvas, 0, 0, ClientWidth, ClientHeight,
      MainTexture.Image.Canvas, 0, 0);
    ImageOp_B(Back, Template, 0, 0, 0, 0, ClientWidth, ClientHeight);
  end;
end;

procedure TCityDlg.CloseBtnClick(Sender: TObject);
begin
  Close;
end;

procedure TCityDlg.InitSmallCityMap;
var
  i, iix, cli1, Color0, Color1, Color2: integer;
begin
  if cix >= 0 then
    c := MyCity[cix];
  case MyMap[cLoc] and fTerrain of
    fPrairie: cli1 := cliPrairie;
    fHills: cli1 := cliHills;
    fTundra: cli1 := cliTundra;
  else
    cli1 := cliPlains;
  end;
  Color0 := Colors.Canvas.Pixels[clkAge0 + Age, cliRoad];
  Color1 := Colors.Canvas.Pixels[clkCity, cli1];
  Color2 := Colors.Canvas.Pixels[clkAge0 + Age, cliHouse];
  SmallCityMap.Canvas.FillRect(0, 0, SmallCityMap.Width, SmallCityMap.Height);
  BitBltCanvas(SmallCityMap.Canvas, 0, 0, 83, hSmallMap,
    SmallCityMapTemplate.Canvas, 83 * SizeClass, 0);
  if IsPort then
  begin
    BitBltCanvas(SmallCityMap.Canvas, 83, 0, 15, hSmallMap,
      SmallCityMapTemplate.Canvas, 332 + 15, 0);
    ImageOp_CCC(SmallCityMap, 0, 0, 83, hSmallMap, Color0, Color1, Color2);
    Color2 := Colors.Canvas.Pixels[clkCity, cliWater];
    ImageOp_CCC(SmallCityMap, 83, 0, 15, hSmallMap, Color0, Color1, Color2);
  end
  else
  begin
    BitBltCanvas(SmallCityMap.Canvas, 83, 0, 15, hSmallMap,
      SmallCityMapTemplate.Canvas, 332, 0);
    ImageOp_CCC(SmallCityMap, 0, 0, wSmallMap, hSmallMap, Color0,
      Color1, Color2);
  end;

  with SmallCityMap.Canvas do
  begin
    Brush.Color := ScreenTools.Colors.Canvas.Pixels[clkAge0 + Age, cliImp];
    for i := 0 to 29 do
    begin
      for iix := nWonder to nImp - 1 do
        if (ImpPosition[iix] = i) and (c.Built[iix] > 0) then
        begin
          FillRect(Rect(5 + 16 * (i mod 3) + 48 * (i div 18),
            3 + 12 * (i mod 18 div 3), 13 + 16 * (i mod 3) + 48 * (i div 18),
            11 + 12 * (i mod 18 div 3)));
          break;
        end;
    end;
    i := 30;
    for iix := 0 to nImp do
      if (c.Built[iix] > 0) and ((iix < nWonder) or (ImpPosition[iix] < 0)) then
      begin
        FillRect(Rect(5 + 16 * (i mod 3) + 48 * (i div 18),
          3 + 12 * (i mod 18 div 3), 13 + 16 * (i mod 3) + 48 * (i div 18),
          11 + 12 * (i mod 18 div 3)));
        inc(i);
        if i = 36 then
          break; // area is full
      end;
    if c.Project and cpImp <> 0 then
    begin
      iix := c.Project and cpIndex;
      if iix <> imTrGoods then
      begin
        if (iix >= nWonder) and (ImpPosition[iix] >= 0) then
          i := ImpPosition[iix];
        if i < 36 then
        begin
          brush.Color := ScreenTools.Colors.Canvas.Pixels[clkAge0 + Age, cliImpProject];
          FillRect(Rect(5 + 16 * (i mod 3) + 48 * (i div 18),
            3 + 12 * (i mod 18 div 3), 13 + 16 * (i mod 3) + 48 * (i div 18),
            11 + 12 * (i mod 18 div 3)));
        end;
      end;
    end;
    brush.style := bsClear;
  end;
end;

procedure TCityDlg.InitZoomCityMap;
begin
  UnshareBitmap(ZoomCityMap);
  BitBltCanvas(ZoomCityMap.Canvas, 0, 0, wZoomMap, hZoomMap,
    Back.Canvas, xZoomMap, yZoomMap);
  if SmallMapMode = smImprovements then begin
    if ZoomArea < 3 then begin
      ImageOp_B(ZoomCityMap, CityMapTemplate, 0, 0, 376 * SizeClass,
        112 * ZoomArea, wZoomMap, hZoomMap);
    end else begin
      ImageOp_B(ZoomCityMap, CityMapTemplate, 0, 0, 376 * SizeClass + 216,
        112 * (ZoomArea - 3), wZoomMap - wZoomEnvironment, hZoomMap);
      ImageOp_B(ZoomCityMap, CityMapTemplate, wZoomMap - wZoomEnvironment, 0,
        1504 + wZoomEnvironment * byte(IsPort), 112 * (ZoomArea - 3),
        wZoomEnvironment, hZoomMap);
    end;
  end;
end;

procedure TCityDlg.OffscreenPaint;

  procedure FillBar(x, y, pos, Growth, max, Kind: integer;
    IndicateComplete: boolean);
  begin
    BarTex.Assign(MainTexture);
    if Kind = 3 then begin
      BarTex.ColorBevelLight := HGrSystem.Data.Canvas.Pixels[104, 36];
      BarTex.ColorBevelShade := BarTex.ColorBevelLight;
    end;
    PaintRelativeProgressBar(offscreen.Canvas, Kind, x - 3, y, wBar - 4, pos,
      Growth, max, IndicateComplete, BarTex);
  end;

  procedure PaintResources(x, y, Loc: integer; Add4Happy: boolean);
  var
    d, i, Total, xGr, yGr: integer;
    TileInfo: TTileInfo;
    rare: boolean;
  begin
    with AreaMap do begin
    if Server(sGetCityTileInfo, me, Loc, TileInfo) <> eOk then
    begin
      assert(cix < 0);
      exit
    end;
    Total := TileInfo.Food + TileInfo.Prod + TileInfo.Trade;
    rare := MyMap[Loc] and $06000000 > 0;
    if rare then
      inc(Total);
    if Add4Happy then
      inc(Total, 4);
    if Total > 1 then
      d := (xxt - 11) div (Total - 1)
    else
      d := 0;

    if d < 1 then
      d := 1;
    if d > 4 then
      d := 4;
    for i := 0 to Total - 1 do
    begin
      yGr := 115;
      if Add4Happy and (i >= Total - 4) then
      begin
        xGr := 132;
        yGr := 126
      end
      else if rare and (i = Total - 1) then
        xGr := 66 + 110
      else if i >= TileInfo.Food + TileInfo.Prod then
        xGr := 66 + 44
      else if i >= TileInfo.Prod then
        xGr := 66
      else
        xGr := 66 + 22;
      Sprite(offscreen, HGrSystem, x + xxt - 5 + d * (2 * i + 1 - Total),
        y + yyt - 5, 10, 10, xGr, yGr);
    end;
    end;
  end;
var
  line, MessageCount: integer;

  procedure CheckMessage(Flag: integer);
  var
    i, test: integer;
    s: string;
  begin
    if Happened and Flag <> 0 then
    begin
      i := 0;
      test := 1;
      while test < Flag do
      begin
        inc(i);
        inc(test, test)
      end;

      if AllowChange and (Sounds <> nil) and (OpenSoundEvent = -1) then
      begin
        s := CityEventSoundItem[i];
        if s <> '' then
          s := Sounds.Lookup(s);
        if (Flag = chProduction) or (s <> '') and (s[1] <> '*') and (s[1] <> '[')
        then
          OpenSoundEvent := i
      end;

      s := CityEventName(i);
      { if Flag=chNoGrowthWarning then
        if c.Built[imAqueduct]=0 then
        s:=Format(s,[Phrases.Lookup('IMPROVEMENTS',imAqueduct)])
        else s:=Format(s,[Phrases.Lookup('IMPROVEMENTS',imSewer)]); }
      RisedTextOut(offscreen.Canvas, xmOpt + 40, ymOpt - 1 - 8 * MessageCount +
        16 * line, s);
      inc(line)
    end;
  end;

var
  x, y, xGr, i, j, iix, d, dx, dy, PrCost, Cnt, Loc1, FreeSupp, Paintiix,
    HappyGain, OptiType, rx, ry, TrueFood, TrueProd, TruePoll: Integer;
  av: Integer;
  PrName, s: string;
  UnitInfo: TUnitInfo;
  UnitReport: TUnitReport;
  IsCityAlive, CanGrow: Boolean;
begin
  inherited;
  if cix >= 0 then
    c := MyCity[cix];
  Report.HypoTiles := -1;
  Report.HypoTaxRate := -1;
  Report.HypoLuxuryRate := -1;
  if cix >= 0 then
    Server(sGetCityReportNew, me, cix, Report) // own city
  else
    Server(sGetEnemyCityReportNew, me, cLoc, Report); // enemy city
  TrueFood := c.Food;
  TrueProd := c.Prod;
  TruePoll := c.Pollution;
  if Supervising or (cix < 0) then
  begin // normalize city from after-turn state
    Dec(TrueFood, Report.FoodSurplus);
    if TrueFood < 0 then
      TrueFood := 0; // shouldn't happen
    Dec(TrueProd, Report.Production);
    if TrueProd < 0 then
      TrueProd := 0; // shouldn't happen
    Dec(TruePoll, Report.AddPollution);
    if TruePoll < 0 then
      TruePoll := 0; // shouldn't happen
  end;
  IsCityAlive := (cGov <> gAnarchy) and (c.Flags and chCaptured = 0);
  if not IsCityAlive then
    Report.Working := c.Size;

  RedTex.Assign(MainTexture);
  RedTex.ColorBevelLight := $0000FF;
  RedTex.ColorBevelShade := $000000;
  RedTex.ColorTextLight := $000000;
  RedTex.ColorTextShade := $0000FF;

  BitBltCanvas(offscreen.Canvas, 0, 0, 640, 480, Back.Canvas, 0, 0);

  Offscreen.Canvas.Font.Assign(UniFont[ftCaption]);
  RisedTextOut(Offscreen.Canvas, 42, 7, Caption);
  with Offscreen.Canvas do
  begin // city size
    Brush.Color := $000000;
    FillRect(Rect(8 + 1, 7 + 1, 36 + 1, 32 + 1));
    Brush.Color := $FFFFFF;
    FillRect(Rect(8, 7, 36, 32));
    Brush.style := bsClear;
    Font.Color := $000000;
    s := IntToStr(c.Size);
    TextOut(8 + 14 - TextWidth(s) div 2, 7, s);
  end;
  Offscreen.Canvas.Font.Assign(UniFont[ftSmall]);

  if not IsCityAlive then
  begin
    MakeRed(Offscreen, 18, 280, 298, 40);
    if cGov = gAnarchy then
      s := Phrases.Lookup('GOVERNMENT', gAnarchy)
    else { if c.Flags and chCaptured<>0 then }
      s := Phrases.Lookup('CITYEVENTS', 14);
    RisedTextOut(offscreen.Canvas, 167 - BiColorTextWidth(offscreen.Canvas, s)
      div 2, ymOpt - 9, s);
  end
  else if AllowChange then
  begin
    OptiType := c.Status shr 4 and $0F;
    Sprite(offscreen, HGrSystem2, xmOpt - 32, ymOpt - 32, 64, 64,
      1 + OptiType mod 3 * 64, 217 + OptiType div 3 * 64);

    { display messages now }
    MessageCount := 0;
    for i := 0 to 31 do
      if Happened and ($FFFFFFFF - chCaptured) and (1 shl i) <> 0 then
        inc(MessageCount);
    if MessageCount > 3 then
      MessageCount := 3;
    if MessageCount > 0 then
    begin
      MakeBlue(offscreen, 74, 280, 242, 40);
      line := 0;
      for i := 0 to nCityEventPriority - 1 do
        if line < MessageCount then
          CheckMessage(CityEventPriority[i]);
    end
    else
    begin
      s := Phrases.Lookup('CITYMANAGETYPE', OptiType);
      j := pos('\', s);
      if j = 0 then
        LoweredTextout(offscreen.Canvas, -1, MainTexture, xmOpt + 40,
          ymOpt - 9, s)
      else
      begin
        LoweredTextout(offscreen.Canvas, -1, MainTexture, xmOpt + 40,
          ymOpt - 17, copy(s, 1, j - 1));
        LoweredTextout(offscreen.Canvas, -1, MainTexture, xmOpt + 40, ymOpt - 1,
          copy(s, j + 1, 255));
      end;
    end;
  end;

  with AreaMap do begin
    rx := (192 + xxt * 2 - 1) div (xxt * 2);
    ry := (96 + yyt * 2 - 1) div (yyt * 2);
    AreaMap.Paint(xmArea - xxt * 2 * rx, ymArea - yyt * 2 * ry - 3 * yyt,
      dLoc(cLoc, -2 * rx + 1, -2 * ry - 1), 4 * rx - 1, 4 * ry + 1, cLoc, cOwner,
      false, AllowChange and IsCityAlive and
      (c.Status and csResourceWeightsMask = 0));
    BitBltCanvas(offscreen.Canvas, xmArea + 102, 42, 90, 33, Back.Canvas,
      xmArea + 102, 42);

    if IsCityAlive then
      for dy := -3 to 3 do
        for dx := -3 to 3 do
          if ((dx + dy) and 1 = 0) and (dx * dx * dy * dy < 81) then begin
            Loc1 := dLoc(cLoc, dx, dy);
            av := CityAreaInfo.Available[(dy + 3) shl 2 + (dx + 3) shr 1];
            if ((av = faNotAvailable) or (av = faTreaty) or (av = faInvalid)) and
              ((Loc1 < 0) or (Loc1 >= G.lx * G.ly) or (MyMap[Loc1] and fCity = 0))
            then
              Sprite(offscreen, HGrTerrain, xmArea - xxt + xxt * dx,
                ymArea - yyt + yyt * dy, xxt * 2, yyt * 2, 1 + 5 * (xxt * 2 + 1),
                1 + yyt + 15 * (yyt * 3 + 1));
            if (1 shl ((dy + 3) shl 2 + (dx + 3) shr 1) and c.Tiles <> 0) then
              PaintResources(xmArea - xxt + xxt * dx, ymArea - yyt + yyt * dy,
                Loc1, (dx = 0) and (dy = 0));
          end;
  end;

  if Report.Working > 1 then
    d := (xService - (xmArea - 192) - 8 - 32) div (Report.Working - 1)
  else
    d := 0;

  if d > 28 then
    d := 28;
  for i := Report.Working - 1 downto 0 do
  begin
    if IsCityAlive then
      xGr := 29
    else
      xGr := 141;
    BitBltCanvas(offscreen.Canvas, xmArea - 192 + 5 + i * d, ymArea - 96 - 29,
      27, 30, HGrSystem.Mask.Canvas, xGr, 171, SRCAND); { shadow }
    Sprite(offscreen, HGrSystem, xmArea - 192 + 4 + i * d, ymArea - 96 - 30, 27,
      30, xGr, 171);
  end;
  if c.Size - Report.Working > 1 then
    d := (xmArea + 192 - xService - 32) div (c.Size - Report.Working - 1);
  if d > 28 then
    d := 28;
  for i := 0 to c.Size - Report.Working - 1 do
  begin
    xGr := 1 + 112;
    BitBltCanvas(offscreen.Canvas, xmArea + 192 - 27 + 1 - i * d, 29 + 1, 27,
      30, HGrSystem.Mask.Canvas, xGr, 171, SRCAND); { shadow }
    Sprite(offscreen, HGrSystem, xmArea + 192 - 27 - i * d, 29, 27, 30,
      xGr, 171);
    Sprite(offscreen, HGrSystem, xmArea + 192 - 27 + 4 - i * d, 29 + 32, 10,
      10, 121, 126);
    Sprite(offscreen, HGrSystem, xmArea + 192 - 27 + 13 - i * d, 29 + 32, 10,
      10, 121, 126);
    // Sprite(offscreen,HGrSystem,xmArea+192-31+18-i*d,ymArea-96-80+32,10,10,88,115);
  end;

  if c.Project and cpImp = 0 then
    PrName := Tribe[cOwner].ModelName[c.Project and cpIndex]
  else
    PrName := Phrases.Lookup('IMPROVEMENTS', c.Project and cpIndex);
  PrCost := Report.ProjectCost;

  // happiness section
  if IsCityAlive then
  begin
    if cGov = gFundamentalism then
      CountBar(offscreen, xHapp, yHapp + dyBar, wBar, 17,
        Phrases.Lookup('FAITH'), Report.CollectedControl, MainTexture)
    else
    begin
      CountBar(offscreen, xHapp, yHapp + dyBar, wBar, 17,
        Phrases.Lookup('HAPPINESS'), Report.Morale, MainTexture);
      CountBar(offscreen, xHapp, yHapp + 2 * dyBar, wBar, 16,
        Phrases.Lookup('CONTROL'), Report.CollectedControl, MainTexture);
    end;
    CountBar(offscreen, xHapp, yHapp, wBar, 8, Phrases.Lookup('LUX'),
      Report.Luxury, MainTexture);
    CountBar(offscreen, xHapp + dxBar, yHapp, wBar, 19,
      Phrases.Lookup('UNREST'), 2 * Report.Deployed, MainTexture);
    CountBar(offscreen, xHapp + dxBar, yHapp + dyBar, wBar, 17,
      Phrases.Lookup('HAPPINESSDEMAND'), c.Size, MainTexture);
    if Report.HappinessBalance >= 0 then
      CountBar(offscreen, xHapp + dxBar, yHapp + 2 * dyBar, wBar, 17,
        Phrases.Lookup('HAPPINESSPLUS'), Report.HappinessBalance, MainTexture)
    else
    begin
      MakeRed(Offscreen, xHapp + dxBar - 6, yHapp + 2 * dyBar, wBar + 10, 38);
      CountBar(offscreen, xHapp + dxBar, yHapp + 2 * dyBar, wBar, 18,
        Phrases.Lookup('LACK'), -Report.HappinessBalance, RedTex);
    end;
  end;

  // food section
  if IsCityAlive then
  begin
    CountBar(offscreen, xFood, yFood + dyBar div 2, wBar, 0,
      Phrases.Lookup('FOOD'), Report.CollectedFood, MainTexture);
    CountBar(offscreen, xFood + dxBar, yFood + dyBar, wBar, 0,
      Phrases.Lookup('DEMAND'), 2 * c.Size, MainTexture);
    CountBar(offscreen, xFood + dxBar, yFood, wBar, 0,
      Phrases.Lookup('SUPPORT'), Report.FoodSupport, MainTexture);
    if Report.FoodSurplus >= 0 then
      if (cGov = gFuture) or (c.Size >= NeedAqueductSize) and
        (Report.FoodSurplus < 2) then
        CountBar(offscreen, xFood + dxBar, yFood + 2 * dyBar, wBar, 6,
          Phrases.Lookup('PROFIT'), Report.FoodSurplus, MainTexture)
      else
        CountBar(offscreen, xFood + dxBar, yFood + 2 * dyBar, wBar, 0,
          Phrases.Lookup('SURPLUS'), Report.FoodSurplus, MainTexture)
    else
    begin
      MakeRed(Offscreen, xFood + dxBar - 6, yFood + 2 * dyBar, wBar + 10, 38);
      CountBar(offscreen, xFood + dxBar, yFood + 2 * dyBar, wBar, 1,
        Phrases.Lookup('LACK'), -Report.FoodSurplus, RedTex);
    end;
  end;
  CanGrow := (c.Size < MaxCitySize) and (cGov <> gFuture) and
    (Report.FoodSurplus > 0) and ((c.Size < NeedAqueductSize) or
    (c.Built[imAqueduct] = 1) and (c.Size < NeedSewerSize) or
    (c.Built[imSewer] = 1));
  FillBar(xFood + 3, yFood + 102, TrueFood,
    CutCityFoodSurplus(Report.FoodSurplus, IsCityAlive, cGov, c.Size),
    Report.Storage, 1, CanGrow);
  LoweredTextout(offscreen.Canvas, -1, MainTexture, xFood + 3 - 5,
    yFood + 102 - 20, Format('%d/%d', [TrueFood, Report.Storage]));
  LoweredTextout(offscreen.Canvas, -1, MainTexture, xFood - 2, yFood + 66,
    Phrases.Lookup('STORAGE'));

  // production section
  if IsCityAlive then
  begin
    CountBar(offscreen, xProd, yProd, wBar, 2, Phrases.Lookup('MATERIAL'),
      Report.CollectedMaterial, MainTexture);
    CountBar(offscreen, xProd + dxBar, yProd, wBar, 2,
      Phrases.Lookup('SUPPORT'), Report.MaterialSupport, MainTexture);
    if Report.Production >= 0 then
      if c.Project and (cpImp + cpIndex) = cpImp + imTrGoods then
        CountBar(offscreen, xProd + dxBar, yProd + dyBar + 16, wBar, 6,
          Phrases.Lookup('PROFIT'), Report.Production, MainTexture)
      else
        CountBar(offscreen, xProd + dxBar, yProd + dyBar + 16, wBar, 2,
          Phrases.Lookup('PROD'), Report.Production, MainTexture)
    else
    begin
      MakeRed(Offscreen, xProd + dxBar - 6, yProd + dyBar + 17, wBar + 10, 38);
      CountBar(offscreen, xProd + dxBar, yProd + dyBar + 16, wBar, 3,
        Phrases.Lookup('LACK'), -Report.Production, RedTex);
    end;
  end;
  if c.Project and (cpImp + cpIndex) <> cpImp + imTrGoods then
    with offscreen.Canvas do
    begin
      i := Report.Production;
      if (i < 0) or not IsCityAlive then
        i := 0;
      FillBar(xProd + 3, yProd + 16 + 69, TrueProd, i, PrCost, 4, true);
      LoweredTextout(offscreen.Canvas, -1, MainTexture, xProd + 3 - 5,
        yProd + 16 + 45, Format('%d/%d', [TrueProd, PrCost]));
      if BiColorTextWidth(offscreen.Canvas, PrName) > wBar + dxBar then
      begin
        repeat
          Delete(PrName, length(PrName), 1)
        until BiColorTextWidth(offscreen.Canvas, PrName) <= wBar + dxBar;
        PrName := PrName + '.'
      end;
    end;
  RisedTextOut(offscreen.Canvas, xProd - 2, yProd + 36, PrName);

  // pollution section
  if IsCityAlive and (Report.AddPollution > 0) then
  begin
    FillBar(xPoll + 3, yPoll + 20, TruePoll, Report.AddPollution,
      MaxPollution, 3, true);
    RisedTextOut(offscreen.Canvas, xPoll + 3 - 5, yPoll + 20 - 20,
      Phrases.Lookup('POLL'));
  end;

  // trade section
  if IsCityAlive and (Report.CollectedTrade > 0) then
  begin
    CountBar(offscreen, xTrade, yTrade + dyBar div 2, wBar, 4,
      Phrases.Lookup('TRADE'), Report.CollectedTrade, MainTexture);
    CountBar(offscreen, xTrade + dxBar, yTrade + 2 * dyBar, wBar, 5,
      Phrases.Lookup('CORR'), Report.Corruption, MainTexture);
    CountBar(offscreen, xTrade + dxBar, yTrade, wBar, 6, Phrases.Lookup('TAX'),
      Report.Tax, MainTexture);
    CountBar(offscreen, xTrade + dxBar, yTrade + dyBar, wBar, 12,
      Phrases.Lookup('SCIENCE'), Report.Science, MainTexture);
  end;

  // small map
  BitBltCanvas(offscreen.Canvas, xSmallMap, ySmallMap, wSmallMap, hSmallMap,
    SmallCityMap.Canvas, 0, 0);
  if SmallMapMode = smImprovements then
    Frame(offscreen.Canvas, xSmallMap + 48 * (ZoomArea div 3),
      ySmallMap + 24 * (ZoomArea mod 3), xSmallMap + 48 * (ZoomArea div 3) + 49,
      ySmallMap + 24 * (ZoomArea mod 3) + 25, MainTexture.ColorMark,
      MainTexture.ColorMark);
  Frame(offscreen.Canvas, xSmallMap - 1, ySmallMap - 1, xSmallMap + wSmallMap,
    ySmallMap + hSmallMap, $B0B0B0, $FFFFFF);
  RFrame(offscreen.Canvas, xSmallMap - 2, ySmallMap - 2, xSmallMap + wSmallMap +
    1, ySmallMap + hSmallMap + 1, $FFFFFF, $B0B0B0);

  Frame(offscreen.Canvas, xSupport - 1, ySupport - 1, xSupport + wSupport,
    ySupport + hSupport, $B0B0B0, $FFFFFF);
  RFrame(offscreen.Canvas, xSupport - 2, ySupport - 2, xSupport + wSupport + 1,
    ySupport + hSupport + 1, $FFFFFF, $B0B0B0);
  x := xSupport + wSupport div 2;
  y := ySupport + hSupport div 2;
  if SmallMapMode = smSupportedUnits then
  begin
    offscreen.Canvas.brush.Color := MainTexture.ColorMark;
    offscreen.Canvas.FillRect(Rect(x - 27, y - 6, x + 27, y + 6));
    offscreen.Canvas.brush.style := bsClear;
  end;
  Sprite(offscreen, HGrSystem, x - 16, y - 5, 10, 10, 88, 115);
  Sprite(offscreen, HGrSystem, x - 5, y - 5, 10, 10, 66, 115);
  Sprite(offscreen, HGrSystem, x + 6, y - 5, 10, 10, 154, 126);

  BitBltCanvas(offscreen.Canvas, xZoomMap, yZoomMap, wZoomMap, hZoomMap,
    ZoomCityMap.Canvas, 0, 0);

  for i := 0 to 5 do
    imix[i] := -1;
  if SmallMapMode = smImprovements then
  begin
    if ZoomArea = 5 then
    begin
      Cnt := 0;
      for iix := 0 to nImp - 1 do
        if ((iix < nWonder) or (ImpPosition[iix] < 0)) and (c.Built[iix] > 0) then
        begin
          i := Cnt - Page * 6;
          if (i >= 0) and (i < 6) then
            imix[i] := iix;
          inc(Cnt);
        end;
      PageCount := (Cnt + 5) div 6;
    end
    else
    begin
      for iix := nWonder to nImp - 1 do
      begin
        i := ImpPosition[iix] - 6 * ZoomArea;
        if (i >= 0) and (i < 6) and (c.Built[iix] > 0) then
          imix[i] := iix;
      end;
      PageCount := 0;
    end;
    for i := 0 to 5 do
      if imix[i] >= 0 then
      begin
        iix := imix[i];
        x := xZoomMap + 14 + 72 * (i mod 3);
        y := yZoomMap + 14 + 56 * (i div 3);
        ImpImage(offscreen.Canvas, x, y, iix, cGov, AllowChange and
          (ClientMode < scContact));
        if IsCityAlive then
        begin
          if iix = imColosseum then
          begin
            Sprite(offscreen, HGrSystem, x + 46, y, 14, 14, 82, 100);
          end
          else
          begin
            HappyGain := 0;
            case iix of
              0 .. 27, imTemple:
                HappyGain := 2;
              imTheater:
                HappyGain := 4;
              imCathedral:
                if MyRO.Wonder[woBach].EffectiveOwner = cOwner then
                  HappyGain := 6
                else
                  HappyGain := 4;
            end;
            if HappyGain > 1 then
            begin
              d := 30 div (HappyGain - 1);
              if d > 10 then
                d := 10
            end;
            for j := 0 to HappyGain - 1 do
              Sprite(offscreen, HGrSystem, x + 50, y + d * j, 10, 10, 132, 126);
          end;
          for j := 0 to Imp[iix].Maint - 1 do
            Sprite(offscreen, HGrSystem, x - 4, y + 29 - 3 * j, 10, 10,
              132, 115);
        end
      end;
    if imix[0] >= 0 then
      Imp0Area.Hint := Phrases.Lookup('IMPROVEMENTS', imix[0])
    else
      Imp0Area.Hint := '';
    if imix[1] >= 0 then
      Imp1Area.Hint := Phrases.Lookup('IMPROVEMENTS', imix[1])
    else
      Imp1Area.Hint := '';
    if imix[2] >= 0 then
      Imp2Area.Hint := Phrases.Lookup('IMPROVEMENTS', imix[2])
    else
      Imp2Area.Hint := '';
    if imix[3] >= 0 then
      Imp3Area.Hint := Phrases.Lookup('IMPROVEMENTS', imix[3])
    else
      Imp3Area.Hint := '';
    if imix[4] >= 0 then
      Imp4Area.Hint := Phrases.Lookup('IMPROVEMENTS', imix[4])
    else
      Imp4Area.Hint := '';
    if imix[5] >= 0 then
      Imp5Area.Hint := Phrases.Lookup('IMPROVEMENTS', imix[5])
    else
      Imp5Area.Hint := '';
  end
  else { if SmallMapMode = smSupportedUnits then }
  begin
    LoweredTextout(offscreen.Canvas, -1, MainTexture, xZoomMap + 6,
      yZoomMap + 2, Phrases.Lookup('SUPUNITS'));
    FreeSupp := c.Size * SupportFree[cGov] shr 1;
    Cnt := 0;
    for i := 0 to MyRO.nUn - 1 do
      if (MyUn[i].Loc >= 0) and (MyUn[i].Home = cix) then
        with MyModel[MyUn[i].mix] do
        begin
          Server(sGetUnitReport, me, i, UnitReport);
          if (Cnt >= 6 * Page) and (Cnt < 6 * (Page + 1)) then
          begin // unit visible in display
            imix[Cnt - 6 * Page] := i;
            x := ((Cnt - 6 * Page) mod 3) * 64 + xZoomMap;
            y := ((Cnt - 6 * Page) div 3) * 52 + yZoomMap + 20;
            MakeUnitInfo(me, MyUn[i], UnitInfo);
            NoMap.SetOutput(offscreen);
            NoMap.PaintUnit(x, y, UnitInfo, MyUn[i].Status);

            for j := 0 to UnitReport.FoodSupport - 1 do
              Sprite(offscreen, HGrSystem, x + 38 + 11 * j, y + 40, 10,
                10, 66, 115);
            for j := 0 to UnitReport.ProdSupport - 1 do
            begin
              if (FreeSupp > 0) and
                (UnitReport.ReportFlags and urfAlwaysSupport = 0) then
              begin
                Sprite(offscreen, HGrSystem, x + 16 - 11 * j, y + 40, 10,
                  10, 143, 115);
                dec(FreeSupp);
              end
              else
                Sprite(offscreen, HGrSystem, x + 16 - 11 * j, y + 40, 10,
                  10, 88, 115);
            end;
            if UnitReport.ReportFlags and urfDeployed <> 0 then
              for j := 0 to 1 do
                Sprite(offscreen, HGrSystem, x + 27 + 11 * j, y + 40, 10,
                  10, 154, 126)
          end // unit visible in display
          else
            Dec(FreeSupp, UnitReport.ProdSupport);
          Inc(Cnt);
        end;
    PageCount := (Cnt + 5) div 6;
    Imp0Area.Hint := '';
    Imp1Area.Hint := '';
    Imp2Area.Hint := '';
    Imp3Area.Hint := '';
    Imp4Area.Hint := '';
    Imp5Area.Hint := '';
  end;
  PageUpBtn.Visible := PageCount > 1;
  PageDownBtn.Visible := PageCount > 1;

  with Offscreen.Canvas do
  begin
    { display project now }
    DLine(offscreen.Canvas, xView + 9 + xSizeBig, xProd + 2 * wBar + 10,
      yProd + dyBar + 16, $FFFFFF, $B0B0B0);
    if ProdHint then
    begin
      ScreenTools.Frame(offscreen.Canvas, xView + 9 - 1, yView + 5 - 1,
        xView + 9 + xSizeBig, yView + 5 + ySizeBig, $B0B0B0, $FFFFFF);
      RFrame(offscreen.Canvas, xView + 9 - 2, yView + 5 - 2,
        xView + 9 + xSizeBig + 1, yView + 5 + ySizeBig + 1, $FFFFFF, $B0B0B0);
      with offscreen.Canvas do
      begin
        brush.Color := $000000;
        FillRect(Rect(xView + 9, yView + 5, xView + 1 + 72 - 8,
          yView + 5 + 40));
        brush.style := bsClear;
      end;
    end
    else if AllowChange and (c.Status and 7 <> 0) then
    begin // city type autobuild
      FrameImage(offscreen.Canvas, bigimp, xView + 9, yView + 5, xSizeBig,
        ySizeBig, (c.Status and 7 - 1 + 3) * xSizeBig, 0, (cix >= 0) and
        (ClientMode < scContact));
    end
    else if c.Project and cpImp = 0 then
    begin // project is unit
      FrameImage(offscreen.Canvas, bigimp, xView + 9, yView + 5, xSizeBig,
        ySizeBig, 0, 0, AllowChange and (ClientMode < scContact));
      with Tribe[cOwner].ModelPicture[c.Project and cpIndex] do
        Sprite(offscreen, HGr, xView + 5, yView + 1, 64, 44,
          pix mod 10 * 65 + 1, pix div 10 * 49 + 1);
    end
    else
    begin // project is building
      if ProdHint then
        Paintiix := c.Project0 and cpIndex
      else
        Paintiix := c.Project and cpIndex;
      ImpImage(offscreen.Canvas, xView + 9, yView + 5, Paintiix, cGov,
        AllowChange and (ClientMode < scContact));
    end;
  end;

  if AllowChange and (ClientMode < scContact) then
  begin
    i := Server(sBuyCityProject - sExecute, me, cix, nil^);
    BuyBtn.Visible := (i = eOk) or (i = eViolation);
  end
  else
    BuyBtn.Visible := false;

  MarkUsedOffscreen(ClientWidth, ClientHeight);
end;

procedure TCityDlg.FormShow(Sender: TObject);
var
  dx, dy, Loc1: integer;
  GetCityData: TGetCityData;
begin
  BlinkTime := 5;
  if cix >= 0 then
  begin { own city }
    c := MyCity[cix];
    cOwner := me;
    cGov := MyRO.Government;
    ProdHint := (cGov <> gAnarchy) and
      (Happened and (chProduction or chFounded or chCaptured or
      chAllImpsMade) <> 0);
    Server(sGetCityAreaInfo, me, cix, CityAreaInfo);
    NextCityBtn.Visible := WindowMode = wmPersistent;
    PrevCityBtn.Visible := WindowMode = wmPersistent;
  end
  else { enemy city }
  begin
    SmallMapMode := smImprovements;
    Server(sGetCity, me, cLoc, GetCityData);
    c := GetCityData.c;
    cOwner := GetCityData.Owner;
    cGov := MyRO.EnemyReport[cOwner].Government;
    Happened := c.Flags and $7FFFFFFF;
    ProdHint := false;
    Server(sGetEnemyCityAreaInfo, me, cLoc, CityAreaInfo);

    if c.Project and cpImp = 0 then
    begin
      emix := MyRO.nEnemyModel - 1;
      while (emix > 0) and ((MyRO.EnemyModel[emix].Owner <> cOwner) or
        (integer(MyRO.EnemyModel[emix].mix) <> c.Project and cpIndex)) do
        dec(emix);
      if not Assigned(Tribe[cOwner].ModelPicture[c.Project and cpIndex].HGr) then
        InitEnemyModel(emix);
    end;

    NextCityBtn.Visible := False;
    PrevCityBtn.Visible := False;
  end;
  Page := 0;

  if c.Size < 5 then
    SizeClass := 0
  else if c.Size < 9 then
    SizeClass := 1
  else if c.Size < 13 then
    SizeClass := 2
  else
    SizeClass := 3;

  // check if port
  IsPort := False;
  for dx := -2 to 2 do
    for dy := -2 to 2 do
      if Abs(dx) + Abs(dy) = 2 then
      begin
        Loc1 := dLoc(cLoc, dx, dy);
        if (Loc1 >= 0) and (Loc1 < G.lx * G.ly) and
          (MyMap[Loc1] and fTerrain < fGrass) then
          IsPort := True;
      end;

  if WindowMode = wmModal then
  begin { center on screen }
    Left := (Screen.Width - Width) div 2;
    Top := (Screen.Height - Height) div 2;
  end;

  Caption := CityName(c.ID);

  InitSmallCityMap;
  InitZoomCityMap;
  OpenSoundEvent := -1;
  OffscreenPaint;
  Timer1.Enabled := True;
end;

procedure TCityDlg.ShowNewContent(NewMode: TWindowMode; Loc: integer; ShowEvent: cardinal);
begin
  if MyMap[Loc] and fOwned <> 0 then
  begin // own city
    cix := MyRO.nCity - 1;
    while (cix >= 0) and (MyCity[cix].Loc <> Loc) do
      dec(cix);
    assert(cix >= 0);
    if (Optimize_cixTileChange >= 0) and
      (Optimize_TilesBeforeChange and not MyCity[Optimize_cixTileChange].Tiles
      <> 0) then
    begin
      CityOptimizer_ReleaseCityTiles(Optimize_cixTileChange,
        Optimize_TilesBeforeChange and
        not MyCity[Optimize_cixTileChange].Tiles);
      if WindowMode <> wmModal then
        MainScreen.UpdateViews;
    end;
    Optimize_cixTileChange := cix;
    Optimize_TilesBeforeChange := MyCity[cix].Tiles;
  end
  else
    cix := -1;
  AllowChange := not supervising and (cix >= 0);
  cLoc := Loc;
  Happened := ShowEvent;
  inherited ShowNewContent(NewMode);
end;

procedure TCityDlg.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; x, y: integer);
var
  i, qx, qy, dx, dy, fix, NewTiles, Loc1, iix, SellResult: integer;
  Rebuild: boolean;
begin
  if (ssLeft in Shift) and (x >= xSmallMap) and (x < xSmallMap + wSmallMap) and
    (y >= ySmallMap) and (y < ySmallMap + hSmallMap) then
  begin
    SmallMapMode := smImprovements;
    ZoomArea := (y - ySmallMap) * 3 div hSmallMap + 3 *
      ((x - xSmallMap) * 2 div wSmallMap);
    Page := 0;
    InitZoomCityMap;
    SmartUpdateContent;
    Exit;
  end;
  if (ssLeft in Shift) and (x >= xSupport) and (x < xSupport + wSupport) and
    (y >= ySupport) and (y < ySupport + hSupport) then
  begin
    SmallMapMode := smSupportedUnits;
    Page := 0;
    InitZoomCityMap;
    SmartUpdateContent;
    Exit;
  end;
  if not AllowChange then
    Exit; // Not an own city

  if (ssLeft in Shift) then
    if (ClientMode < scContact) and (x >= xView) and (y >= yView) and
      (x < xView + 73) and (y < yView + 50) then
      if cGov = gAnarchy then
        with MessgExDlg do
        begin
          { MessgText:=Phrases.Lookup('OUTOFCONTROL');
            if c.Project and cpImp=0 then
            MessgText:=Format(MessgText,[Tribe[cOwner].ModelName[c.Project and cpIndex]])
            else MessgText:=Format(MessgText,[Phrases.Lookup('IMPROVEMENTS',c.Project and cpIndex)]); }
          MessgText := Phrases.Lookup('NOCHANGEINANARCHY');
          Kind := mkOk;
          ShowModal;
        end
      else
      begin
        if ProdHint then
        begin
          ProdHint := false;
          SmartUpdateContent
        end;
        ChooseProject;
      end
    else if (SmallMapMode = smImprovements) and (x >= xZoomMap) and (x < xZoomMap + wZoomMap) and
      (y >= yZoomMap) and (y < yZoomMap + hZoomMap) then
    begin
      i := 5;
      while (i >= 0) and not((x >= xZoomMap + 14 + 72 * (i mod 3)) and
        (x < xZoomMap + 14 + 56 + 72 * (i mod 3)) and
        (y >= yZoomMap + 14 + 56 * (i div 3)) and
        (y < yZoomMap + 14 + 40 + 56 * (i div 3))) do
        dec(i);
      if i >= 0 then
      begin
        iix := imix[i];
        if iix >= 0 then
          if ssShift in Shift then
            HelpDlg.ShowNewContent(WindowModeMakePersistent(FWindowMode), hkImp, iix)
          else if (ClientMode < scContact) then
            with MessgExDlg do
            begin
              IconKind := mikImp;
              IconIndex := iix;
              if (iix = imPalace) or (Imp[iix].Kind = ikWonder) then
              begin
                MessgText := Phrases.Lookup('IMPROVEMENTS', iix);
                if iix = woOracle then
                  MessgText := MessgText + '\' +
                    Format(Phrases.Lookup('ORACLEINCOME'), [MyRO.OracleIncome]);
                Kind := mkOk;
                ShowModal;
              end
              else
              begin
                SellResult := Server(sSellCityImprovement - sExecute, me,
                  cix, iix);
                if SellResult < rExecuted then
                begin
                  if SellResult = eOnlyOnce then
                    MessgText := Phrases.Lookup('NOSELLAGAIN')
                  else
                    MessgText := Phrases.Lookup('OUTOFCONTROL');
                  MessgText := Format(MessgText,
                    [Phrases.Lookup('IMPROVEMENTS', iix)]);
                  Kind := mkOk;
                  ShowModal;
                end
                else
                begin
                  if Server(sRebuildCityImprovement - sExecute, me, cix, iix) < rExecuted
                  then
                  begin // no rebuild possible, ask for sell only
                    Rebuild := false;
                    MessgText := Phrases.Lookup('IMPROVEMENTS', iix);
                    if not Phrases2FallenBackToEnglish then
                      MessgText := Format(Phrases2.Lookup('SELL2'),
                        [MessgText, Imp[iix].Cost * BuildCostMod
                        [G.Difficulty[me]] div 12])
                    else
                      MessgText := Format(Phrases.Lookup('SELL'), [MessgText]);
                    if iix = imSpacePort then
                      with MyRO.Ship[me] do
                        if Parts[0] + Parts[1] + Parts[2] > 0 then
                          MessgText := MessgText + ' ' +
                            Phrases.Lookup('SPDESTRUCTQUERY');
                    Kind := mkYesNo;
                    ShowModal;
                    if ModalResult <> mrOK then
                      iix := -1
                  end
                  else
                  begin
                    Rebuild := true;
                    MessgText := Phrases.Lookup('IMPROVEMENTS', iix);
                    if not Phrases2FallenBackToEnglish then
                      MessgText := Format(Phrases2.Lookup('DISPOSE2'),
                        [MessgText, Imp[iix].Cost * BuildCostMod
                        [G.Difficulty[me]] div 12 * 2 div 3])
                    else
                      MessgText := Format(Phrases.Lookup('DISPOSE'),
                        [MessgText]);
                    if iix = imSpacePort then
                      with MyRO.Ship[me] do
                        if Parts[0] + Parts[1] + Parts[2] > 0 then
                          MessgText := MessgText + ' ' +
                            Phrases.Lookup('SPDESTRUCTQUERY');
                    Kind := mkYesNo;
                    ShowModal;
                    if ModalResult <> mrOK then
                      iix := -1
                  end;
                  if iix >= 0 then
                  begin
                    if Rebuild then
                    begin
                      Play('CITY_REBUILDIMP');
                      Server(sRebuildCityImprovement, me, cix, iix);
                    end
                    else
                    begin
                      Play('CITY_SELLIMP');
                      Server(sSellCityImprovement, me, cix, iix);
                    end;
                    CityOptimizer_CityChange(cix);
                    InitSmallCityMap;
                    SmartUpdateContent;
                    if WindowMode <> wmModal then
                      MainScreen.UpdateViews;
                  end;
                end;
              end;
            end;
      end;
    end
    else if (SmallMapMode = smSupportedUnits) and (x >= xZoomMap) and (x < xZoomMap + wZoomMap) and
      (y >= yZoomMap) and (y < yZoomMap + hZoomMap) then
    begin
      i := 5;
      while (i >= 0) and not((x >= xZoomMap + 64 * (i mod 3)) and
        (x < xZoomMap + 64 + 64 * (i mod 3)) and
        (y >= yZoomMap + 20 + 48 * (i div 3)) and
        (y < yZoomMap + 20 + 52 + 48 * (i div 3))) do
        dec(i);
      if (i >= 0) and (imix[i] >= 0) then
        if ssShift in Shift then
        else if (cix >= 0) and (ClientMode < scContact) and
          (WindowMode <> wmModal) then
        begin
          CloseAction := None;
          Close;
          MainScreen.CityClosed(imix[i], false, true);
        end;
    end
    else if (x >= xmArea - 192) and (x < xmArea + 192) and (y >= ymArea - 96)
      and (y < ymArea + 96) then
    with AreaMap do begin
      qx := ((4000 * xxt * yyt) + (x - xmArea) * (yyt * 2) + (y - ymArea + yyt)
        * (xxt * 2)) div (xxt * yyt * 4) - 1000;
      qy := ((4000 * xxt * yyt) + (y - ymArea + yyt) * (xxt * 2) - (x - xmArea)
        * (yyt * 2)) div (xxt * yyt * 4) - 1000;
      dx := qx - qy;
      dy := qx + qy;
      if (dx >= -3) and (dx <= 3) and (dy >= -3) and (dy <= 3) and
        (dx * dx * dy * dy < 81) and ((dx <> 0) or (dy <> 0)) then
        if ssShift in Shift then
        begin // terrain help
          Loc1 := dLoc(cLoc, dx, dy);
          if (Loc1 >= 0) and (Loc1 < G.lx * G.ly) then
            HelpOnTerrain(Loc1, WindowModeMakePersistent(FWindowMode))
        end
        else if (ClientMode < scContact) and (cGov <> gAnarchy) and
          (c.Flags and chCaptured = 0) then
        begin // toggle exploitation
          assert(not supervising);
          if c.Status and csResourceWeightsMask <> 0 then
          begin
            with MessgExDlg do
            begin
              MessgText := Phrases.Lookup('CITYMANAGEOFF');
              OpenSound := 'MSG_DEFAULT';
              Kind := mkOkCancel;
              IconKind := mikFullControl;
              ShowModal;
            end;
            if MessgExDlg.ModalResult = mrOK then
            begin
              MyCity[cix].Status := MyCity[cix].Status and
                not csResourceWeightsMask; // off
              c.Status := MyCity[cix].Status;
              SmartUpdateContent;
            end;
            exit;
          end;
          fix := (dy + 3) shl 2 + (dx + 3) shr 1;
          NewTiles := MyCity[cix].Tiles xor (1 shl fix);
          if Server(sSetCityTiles, me, cix, NewTiles) >= rExecuted then
          begin
            SmartUpdateContent;
            if WindowMode <> wmModal then
              MainScreen.UpdateViews;
          end;
        end;
    end
    else if (ClientMode < scContact) and (cGov <> gAnarchy) and
      (c.Flags and chCaptured = 0) and (x >= xmOpt - 32) and (x < xmOpt + 32)
      and (y >= ymOpt - 32) and (y < ymOpt + 32) then
    begin
      i := sqr(x - xmOpt) + sqr(y - ymOpt); // click radius
      if i <= 32 * 32 then
      begin
        if i < 16 * 16 then // inner area clicked
          if c.Status and csResourceWeightsMask <> 0 then
            i := (c.Status shr 4 and $0F) mod 5 + 1 // rotate except off
          else
            i := 3 // rwGrowth
        else
          case trunc(arctan2(x - xmOpt, ymOpt - y) * 180 / pi) of
            - 25 - 52 * 2 .. -26 - 52:
              i := 1;
            -25 - 52 .. -26:
              i := 2;
            -25 .. 25:
              i := 3;
            26 .. 25 + 52:
              i := 4;
            26 + 52 .. 25 + 52 * 2:
              i := 5;
            180 - 26 .. 180, -180 .. -180 + 26:
              i := 0;
          else
            i := -1;
          end;
        if i >= 0 then
        begin
          ChangeResourceWeights(i);
          SmartUpdateContent;
          if WindowMode <> wmModal then
            MainScreen.UpdateViews;
        end;
      end;
    end;
end;

procedure TCityDlg.ChooseProject;
type
  TProjectType = (
    ptSelect = 0,
    ptTrGoods = 1,
    ptUn = 2,
    ptCaravan = 3,
    ptImp = 4,
    ptWonder = 6,
    ptShip = 7,
    ptInvalid = 8
  );

  function ProjectType(Project: integer): TProjectType;
  begin
    if Project and cpCompleted <> 0 then
      Result := ptSelect
    else if Project and (cpImp + cpIndex) = cpImp + imTrGoods then
      Result := ptTrGoods
    else if Project and cpImp = 0 then begin
      if MyModel[Project and cpIndex].Kind = mkCaravan then
        Result := ptCaravan
      else Result := ptUn;
    end
    else if Project and cpIndex >= nImp then
      Result := ptInvalid
    else if Imp[Project and cpIndex].Kind = ikWonder then
      Result := ptWonder
    else if Imp[Project and cpIndex].Kind = ikShipPart then
      Result := ptShip
    else
      Result := ptImp;
  end;

var
  NewProject, OldMoney, cix1: integer;
  pt0, pt1: TProjectType;
  QueryOk: boolean;
begin
  Assert(not supervising);
  ModalSelectDlg.ShowNewContent_CityProject(wmModal, cix);
  if ModalSelectDlg.result <> -1 then
  begin
    if ModalSelectDlg.result and cpType <> 0 then
    begin
      MyCity[cix].Status := MyCity[cix].Status and not 7 or
        (1 + ModalSelectDlg.result and cpIndex);
      AutoBuild(cix, MyData.ImpOrder[ModalSelectDlg.result and cpIndex]);
    end
    else
    begin
      NewProject := ModalSelectDlg.Result;
      QueryOk := True;
      if (NewProject and cpImp <> 0) and (NewProject and cpIndex >= 28) and
        (MyRO.NatBuilt[NewProject and cpIndex] > 0) then
        with MessgExDlg do
        begin
          cix1 := MyRO.nCity - 1;
          while (cix1 >= 0) and
            (MyCity[cix1].Built[NewProject and cpIndex] = 0) do
            Dec(cix1);
          MessgText := Format(Phrases.Lookup('DOUBLESTATEIMP'),
            [Phrases.Lookup('IMPROVEMENTS', NewProject and cpIndex),
            CityName(MyCity[cix1].ID)]);
          OpenSound := 'MSG_DEFAULT';
          Kind := mkOkCancel;
          IconKind := mikImp;
          IconIndex := NewProject and cpIndex;
          Gtk2Fix;
          ShowModal;
          QueryOk := ModalResult = mrOK;
        end;
      if not QueryOk then
        Exit;

      if (MyCity[cix].Prod > 0) then
      begin
        pt0 := ProjectType(MyCity[cix].Project0);
        pt1 := ProjectType(NewProject);
        if (pt0 <> ptSelect) and (pt1 <> ptTrGoods) then
        begin
          if NewProject and (cpImp or cpIndex) <> MyCity[cix].Project0 and
            (cpImp or cpIndex) then
          begin // loss of material -- do query
            Gtk2Fix;
            if (pt1 = ptTrGoods) or (pt1 = ptShip) or (pt1 <> pt0) and
              (pt0 <> ptCaravan) then begin
              QueryOk := SimpleQuery(mkOkCancel,
                Format(Phrases.Lookup('LOSEMAT'), [MyCity[cix].Prod0,
                MyCity[cix].Prod0]), 'MSG_DEFAULT') = mrOK
            end else
            if MyCity[cix].Project and (cpImp or cpIndex) = MyCity[cix]
              .Project0 and (cpImp or cpIndex) then begin
                QueryOk := SimpleQuery(mkOkCancel, Phrases.Lookup('LOSEMAT3'),
                  'MSG_DEFAULT') = mrOK;
            end;
          end;
        end;
      end;
      if not QueryOk then
        Exit;

      OldMoney := MyRO.Money;
      MyCity[cix].Status := MyCity[cix].Status and not 7;
      if (NewProject and cpImp = 0) and
        ((MyCity[cix].Size < 4) and
        (MyModel[NewProject and cpIndex].Kind = mkSettler) or
        (MyCity[cix].Size < 3) and
        ((MyModel[NewProject and cpIndex].Kind = mkSlaves) or
        (NewProject and cpConscripts <> 0))) then
        begin
          Gtk2Fix;
          if SimpleQuery(mkYesNo, Phrases.Lookup('EMIGRATE'), 'MSG_DEFAULT') <> mrOK then
            NewProject := NewProject or cpDisbandCity;
        end;
      Server(sSetCityProject, me, cix, NewProject);
      c.Project := MyCity[cix].Project;
      if MyRO.Money > OldMoney then
        Play('CITY_SELLIMP');
    end;
    CityOptimizer_CityChange(cix);

    if WindowMode <> wmModal then
      MainScreen.UpdateViews;
    InitSmallCityMap;
    SmartUpdateContent;
  end;
end;

procedure TCityDlg.BuyClick(Sender: TObject);
var
  NextProd, Cost: integer;
begin
  if (cix < 0) or (ClientMode >= scContact) then
    exit;
  with MyCity[cix], MessgExDlg do
  begin
    Cost := Report.ProjectCost;
    NextProd := Report.Production;
    if NextProd < 0 then
      NextProd := 0;
    Cost := Cost - Prod - NextProd;
    if (MyRO.Wonder[woMich].EffectiveOwner = me) and (Project and cpImp <> 0)
    then
      Cost := Cost * 2
    else
      Cost := Cost * 4;
    if (Cost <= 0) and (Report.HappinessBalance >= 0) { no disorder } then
    begin
      MessgText := Phrases.Lookup('READY');
      Kind := mkOk;
    end
    else if Cost > MyRO.Money then
    begin
      OpenSound := 'MSG_DEFAULT';
      MessgText := Format(Phrases.Lookup('NOMONEY'), [Cost, MyRO.Money]);
      Kind := mkOk;
    end
    else
    begin
      MessgText := Format(Phrases.Lookup('BUY'), [Cost]);
      Kind := mkYesNo;
    end;
    ShowModal;
    if (Kind = mkYesNo) and (ModalResult = mrOK) then
    begin
      if Server(sBuyCityProject, me, cix, nil^) >= rExecuted then
      begin
        Play('CITY_BUYPROJECT');
        SmartUpdateContent;
        if WindowMode <> wmModal then
          MainScreen.UpdateViews;
      end;
    end;
  end;
end;

procedure TCityDlg.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Timer1.Enabled := False;
  ProdHint := False;
  MarkCityLoc := -1;
  if Optimize_cixTileChange >= 0 then
  begin
    if Optimize_TilesBeforeChange and not MyCity[Optimize_cixTileChange]
      .Tiles <> 0 then
    begin
      CityOptimizer_ReleaseCityTiles(Optimize_cixTileChange,
        Optimize_TilesBeforeChange and
        not MyCity[Optimize_cixTileChange].Tiles);
      if WindowMode <> wmModal then
        MainScreen.UpdateViews;
    end;
    Optimize_cixTileChange := -1;
  end;
  if CloseAction > None then
    MainScreen.CityClosed(RestoreUnFocus, CloseAction = StepFocus);
  RestoreUnFocus := -1;
  inherited;
end;

procedure TCityDlg.Timer1Timer(Sender: TObject);
begin
  if ProdHint then
  begin
    BlinkTime := (BlinkTime + 1) mod 12;
    if BlinkTime = 0 then
      with Canvas do
      begin
        BitBltCanvas(Canvas, xView + 5, yView + 1, 64, 2, Back.Canvas,
          xView + 5, yView + 1);
        BitBltCanvas(Canvas, xView + 5, yView + 3, 2, 42, Back.Canvas,
          xView + 5, yView + 3);
        BitBltCanvas(Canvas, xView + 5 + 62, yView + 3, 2, 42,
          Back.Canvas, xView + 5 + 62, yView + 3);
        ScreenTools.Frame(Canvas, xView + 9 - 1, yView + 5 - 1, xView + 9 + xSizeBig,
          yView + 5 + ySizeBig, $B0B0B0, $FFFFFF);
        RFrame(Canvas, xView + 9 - 2, yView + 5 - 2, xView + 9 + xSizeBig + 1,
          yView + 5 + ySizeBig + 1, $FFFFFF, $B0B0B0);
        Brush.Color := $000000;
        FillRect(Rect(xView + 9, yView + 5, xView + 1 + 72 - 8,
          yView + 5 + 40));
        Brush.style := bsClear;
      end
    else if BlinkTime = 6 then
    begin
      if AllowChange and (c.Status and 7 <> 0) then
      begin // city type autobuild
        FrameImage(Canvas, bigimp, xView + 9, yView + 5, xSizeBig, ySizeBig,
          (c.Status and 7 - 1 + 3) * xSizeBig, 0, true);
      end
      else if c.Project and cpImp = 0 then
      begin // project is unit
        BitBltCanvas(Canvas, xView + 9, yView + 5, xSizeBig, ySizeBig,
          Bigimp.Canvas, 0, 0);
        with Tribe[cOwner].ModelPicture[c.Project and cpIndex] do
          Sprite(Canvas, HGr, xView + 5, yView + 1, 64, 44, pix mod 10 * 65 + 1,
            pix div 10 * 49 + 1);
      end
      else
        ImpImage(Canvas, xView + 9, yView + 5, c.Project0 and cpIndex,
          cGov, true);
    end;
  end;
end;

procedure TCityDlg.FormPaint(Sender: TObject);
begin
  inherited;
  if OpenSoundEvent >= 0 then
    PostMessage(Handle, WM_PLAYSOUND, 0, 0);
end;

procedure TCityDlg.OnPlaySound(var Msg: TMessage);
begin
  if 1 shl OpenSoundEvent = chProduction then
  begin
    if c.Project0 and cpImp <> 0 then
    begin
      if c.Project0 and cpIndex >= 28 then
      // wonders have already extra message with sound
        if Imp[c.Project0 and cpIndex].Kind = ikShipPart then
          Play('SHIP_BUILT')
        else
          Play('CITY_IMPCOMPLETE')
    end
    else
      Play('CITY_UNITCOMPLETE');
  end
  else
  if OpenSoundEvent >= 0 then
    Play(CityEventSoundItem[OpenSoundEvent]);
  OpenSoundEvent := -2;
end;

function Prio(iix: integer): integer;
begin
  case Imp[iix].Kind of
    ikWonder:
      result := iix + 10000;
    ikNatLocal, ikNatGlobal:
      case iix of
        imPalace:
          result := 0;
      else
        result := iix + 20000;
      end;
  else
    case iix of
      imTownHall, imCourt:
        result := iix + 30000;
      imAqueduct, imSewer:
        result := iix + 40000;
      imTemple, imTheater, imCathedral:
        result := iix + 50000;
    else
      result := iix + 90000;
    end;
  end;
end;

procedure TCityDlg.NextCityBtnClick(Sender: TObject);
begin
  ChangeCity(+1);
end;

procedure TCityDlg.PrevCityBtnClick(Sender: TObject);
begin
  ChangeCity(-1);
end;

procedure TCityDlg.ChangeCity(d: integer);
var
  cixNew: integer;
begin
  cixNew := cix;
  repeat
    cixNew := (cixNew + MyRO.nCity + d) mod MyRO.nCity;
  until (MyCity[cixNew].Loc >= 0) or (cixNew = cix);
  if cixNew <> cix then
    MainScreen.ZoomToCity(MyCity[cixNew].Loc);
end;

procedure TCityDlg.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if ((Key = VK_UP) or (Key = VK_NUMPAD8)) and (cix >= 0) and
    (WindowMode = wmPersistent) then
    ChangeCity(-1)
  else if ((Key = VK_DOWN) or (Key = VK_NUMPAD2)) and (cix >= 0) and
    (WindowMode = wmPersistent) then
    ChangeCity(+1)
  else
    inherited;
end;

{ procedure TCityDlg.AdviceBtnClick(Sender: TObject);
  begin
  AdvisorDlg.GiveCityAdvice(cix);
  end; }

procedure TCityDlg.PageUpBtnClick(Sender: TObject);
begin
  if Page > 0 then
  begin
    Dec(Page);
    SmartUpdateContent;
  end;
end;

procedure TCityDlg.PageDownBtnClick(Sender: TObject);
begin
  if Page < PageCount - 1 then
  begin
    Inc(Page);
    SmartUpdateContent;
  end;
end;

procedure TCityDlg.ChangeResourceWeights(iResourceWeights: integer);
var
  Advice: TCityTileAdviceData;
begin
  assert(not supervising);
  assert(cix >= 0);
  MyCity[cix].Status := MyCity[cix].Status and not csResourceWeightsMask or
    (iResourceWeights shl 4);
  c.Status := MyCity[cix].Status;
  if iResourceWeights > 0 then
  begin
    Advice.ResourceWeights := OfferedResourceWeights[iResourceWeights];
    Server(sGetCityTileAdvice, me, cix, Advice);
    if Advice.Tiles <> MyCity[cix].Tiles then
      Server(sSetCityTiles, me, cix, Advice.Tiles);
  end;
end;

procedure SortImprovements;
var
  i, j, k: integer;
begin
  for i := 0 to nImp - 1 do
    ImpSorted[i] := i;
  for i := 0 to nImp - 2 do
    for j := i + 1 to nImp - 1 do
      if Prio(ImpSorted[i]) > Prio(ImpSorted[j]) then begin
        k := ImpSorted[i];
        ImpSorted[i] := ImpSorted[j];
        ImpSorted[j] := k;
      end;
end;

initialization

SortImprovements;

end.