-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExpSubr.pas
1248 lines (1172 loc) · 32.3 KB
/
ExpSubr.pas
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
unit ExpSubr;
{$MODE Delphi}
// Routines rese disponibili all'interprete
interface
function TName(T: integer): string;
function TOwner(T: integer): integer;
function TArmies(T: integer): integer;
function TContinent(T: integer): integer;
function TBordersCount(T: integer): integer;
function TBorder(T, B: integer): integer;
function TIsBordering(T1, T2: integer): boolean;
function TIsFront(T: integer): boolean;
function TIsMine(T: integer): boolean;
function TIsEntry(T: integer): boolean;
function TFrontsCount(T: integer): integer;
function TFront(T, F: integer): integer;
function TStrongestFront(T: integer; var ET, EA: integer): boolean;
function TWeakestFront(T: integer; var ET, EA: integer): boolean;
function TPressure(T: integer): integer;
function TDistance(ST, DT: integer): integer;
function TShortestPath(ST, DT: integer; var TT, PL: integer): boolean;
function TWeakestPath(ST, DT: integer; var TT, PL, EA: integer): boolean;
function TPathToFront(T: integer): integer;
function PMe: integer;
function PName(P: integer): string;
function PProgram(P: integer): string;
function PActive(P: integer): boolean;
function PAlive(P: integer): boolean;
function PHuman(P: integer): boolean;
function PArmiesCount(P: integer): integer;
function PNewArmies(P: integer): integer;
function PTerritoriesCount(P: integer): integer;
function PCardCount(P: integer): integer;
function PCardTurnInValue(P: integer): integer;
function COwner(C: integer): integer;
function CBonus(C: integer): integer;
function CTerritoriesCount(C: integer): integer;
function CTerritory(C, T: integer): integer;
function CBordersCount(C: integer): integer;
function CBorder(C, B: integer): integer;
function CEntriesCount(C: integer): integer;
function CEntry(C, B: integer): integer;
function CAnalysis(C: integer; var PT, PA, ET, EA: integer): boolean;
function CLeader(C: integer; var P, T, A: integer): boolean;
function SConquest: boolean;
function SPlayersCount: integer;
function SAlivePlayersCount: integer;
function SCardsBasedOnCombo: boolean;
procedure UMessage(M: string);
procedure ULog(M: string);
procedure UBufferSet(B: integer; V: double);
function UBufferGet(B: integer): double;
function URandom(R: integer): double;
procedure UTakeSnapshot(M: string);
function UDialogO(M, B: string): integer;
procedure UAbortGame;
procedure ULogOff;
procedure ULogOn;
procedure UMessageOff;
procedure UMessageOn;
procedure UDialogOff;
procedure UDialogOn;
procedure USnapShotOff;
procedure USnapShotOn;
implementation
uses Clipbrd, SysUtils, Classes, Dialogs, Globals, Territ, Log, Stats, UDialog;
type
TPrioCont = array [0 .. 5] of record // Lista priorità conquista continenti
Cont: TContId;
Valore :
double;
end;
// ************************
// * ROUTINES "TERRITORY" *
// ************************
function TName(T: integer): string;
// returns the name of territory T, '?' if T is out of range
begin
if (T > 0) and (T <= MAXTERRITORIES) then
result := arTerritory[T].Name
else
result := '?';
end;
function TOwner(T: integer): integer;
// returns the owner of territory T, 0 if T is unassigned, -1 if T is out of range
begin
if (T > 0) and (T <= MAXTERRITORIES) then
result := arTerritory[T].Owner
else
result := -1;
end;
function TArmies(T: integer): integer;
// returns the number of armies on territory T, 0 if T is unassigned, -1 if T is out of range
begin
if (T > 0) and (T <= MAXTERRITORIES) then
result := arTerritory[T].Army
else
result := -1;
end;
function TContinent(T: integer): integer;
// returns the ID of the continent which territory T belongs to, -1 if T is out of range
begin
if (T > 0) and (T <= MAXTERRITORIES) then
result := ord(arTerritory[T].Contin) + 1
else
result := -1;
end;
function TBordersCount(T: integer): integer;
// returns the number of borders of territory T (max 6), -1 if T is out of range
begin
if (T > 0) and (T <= MAXTERRITORIES) then
result := arTerritory[T].NBord
else
result := -1;
end;
function TBorder(T, B: integer): integer;
// returns bordering territory #B of territory T, -1 if T or B are out of range
begin
if (T > 0) and (T <= MAXTERRITORIES) and (B > 0) and
(B <= arTerritory[T].NBord) then
result := arTerritory[T].Bord[B]
else
result := -1;
end;
function TIsBordering(T1, T2: integer): boolean;
// returns true if territory T1 is bordering on territory T2, false if not or T or B are out of range
begin
if (T1 > 0) and (T1 <= MAXTERRITORIES) and (T2 > 0) and
(T2 <= MAXTERRITORIES) then
result := Confinante(T1, T2)
else
result := false;
end;
function TIsFront(T: integer): boolean;
// returns true if territory T is owned by current player and has at least one bordering
// territory occupied by opponents, false if not or T is out of range
begin
if (T > 0) and (T <= MAXTERRITORIES) then
result := Confine(T)
else
result := false;
end;
function TIsMine(T: integer): boolean;
// returns true if territory T is owned by current player,
// false if not or T is out of range
begin
if (T > 0) and (T <= MAXTERRITORIES) then
result := (arTerritory[T].Owner = iTurn)
else
result := false;
end;
function TIsEntry(T: integer): boolean;
// returns true if territory T is an Entry Territory
// Entry territories are territories which are part of a continent but
// may be accessed from another continent
begin
if T in [1, 3, 9, 10, 12, 14, 15, 16, 20, 22, 25 .. 29, 31, 37, 39] then
result := true
else
result := false;
end;
function TFrontsCount(T: integer): integer;
// returns the number of territories bordering on territory T which are occupied
// by opponents, -1 if T is out of range
var
C, F: integer;
begin
if (T > 0) and (T <= MAXTERRITORIES) then begin
F := 0;
for C := 1 to arTerritory[T].NBord do begin
if arTerritory[arTerritory[T].Bord[C]].Owner <> iTurn then begin
inc(F);
end;
end;
result := F;
end
else
result := -1;
end;
function TFront(T, F: integer): integer;
// returns bordering enemy territory #F of territory T, -1 if T or F are out of range
var
C, F2: integer;
begin
if (T > 0) and (T <= MAXTERRITORIES) then begin
F2 := 0;
for C := 1 to arTerritory[T].NBord do begin
if arTerritory[arTerritory[T].Bord[C]].Owner <> iTurn then begin
inc(F2);
if F2 = F then begin
result := arTerritory[T].Bord[C];
exit;
end;
end;
end;
result := -1;
end
else
result := -1;
end;
function TStrongestFront(T: integer; var ET, EA: integer): boolean;
// returns false if T is out of range, otherwise true
// the "var" variables will contain information on territory T:
// ET: number of the strongest enemy's territory bordering on T, 0 if T has not fronts
// EA: enemy's armies on territory ET
// (unassigned territories are not counted)
var
C: integer;
begin
if (T > 0) and (T <= MAXTERRITORIES) then begin
ET := 0;
EA := 0;
for C := 1 to arTerritory[T].NBord do begin
if arTerritory[arTerritory[T].Bord[C]].Owner <> iTurn then begin
if arTerritory[arTerritory[T].Bord[C]].Army > EA then begin
ET := arTerritory[T].Bord[C];
EA := arTerritory[ET].Army;
end;
end;
end;
result := true;
end
else
result := false;
end;
function TWeakestFront(T: integer; var ET, EA: integer): boolean;
// returns false if T is out of range, otherwise true
// the "var" variables will contain information on territory T:
// ET: number of the weakest enemy's territory bordering on T, 0 if T has not fronts
// EA: enemy's armies on territory ET
// (unassigned territories are not counted)
var
C: integer;
begin
if (T > 0) and (T <= MAXTERRITORIES) then begin
ET := 0;
EA := MaxInt;
for C := 1 to arTerritory[T].NBord do begin
if arTerritory[arTerritory[T].Bord[C]].Owner <> iTurn then begin
if arTerritory[arTerritory[T].Bord[C]].Army < EA then begin
ET := arTerritory[T].Bord[C];
EA := arTerritory[ET].Army;
end;
end;
end;
if ET = 0 then
EA := 0;
result := true;
end
else
result := false;
end;
function TPressure(T: integer): integer;
// returns the total number of enemy armies bordering on territory T, -1 if T is out of range
var
C: integer;
begin
if (T > 0) and (T <= MAXTERRITORIES) then begin
result := 0;
for C := 1 to arTerritory[T].NBord do begin
if arTerritory[arTerritory[T].Bord[C]].Owner <> iTurn then begin
result := result + arTerritory[arTerritory[T].Bord[C]].Army;
end;
end;
end
else
result := -1;
end;
function TPath(const iFrom, iTo: integer; const bAvoidOwned, bUseOwned,
bArmies: boolean; var iLength, iCost: integer; var aPath: array of integer)
: boolean;
{
TPath: Finds best path using Dijkstra's algorithm. Not released to TRPs, just for inner use
iFrom: initial territory
iTo: destination territory
bAvoidOwned: set this to true to make path avoid territories owned by current player
bUseOwned: set this to true to make path move only along territories owned by current player
bArmies: if true, TPath will use armies as "distance", otherwise 1 for each territory
iLength: returns the length of the Path, -1 if the path does not exists
iCost: returns the cost of the path (if bArmies = total armies to defeat,
else the length of the path);
aPath: returns the path: a 1-based array of territory numbers,
from iFrom (excluded) to iTo (included)
returns true if the path exists, false if not
}
var
i, iNode, iChild, iCostFromStart: integer;
bFound, bIsOpen, bIsClosed: boolean;
lstOpen: TList; // OPEN list (FIFO)
aNode: array [1 .. MAXTERRITORIES] of record bClosed: boolean;
// node is in the CLOSED list
iParent: integer; // previous node in the optimal path
iCostFromStart: integer; // cost to arrive to this node
end;
begin
// initialize
lstOpen := TList.Create;
for i := 1 to MAXTERRITORIES do
aNode[i].bClosed := false;
// start node
aNode[iFrom].iParent := 0;
aNode[iFrom].iCostFromStart := 0;
lstOpen.Add(Pointer(iFrom));
// begin A* search
bFound := false;
while lstOpen.Count > 0 do begin
// get first node from OPEN list
iNode := integer(lstOpen.Items[0]);
lstOpen.Delete(0);
// test if it is the target
if iNode = iTo then begin
bFound := true;
// break;
end;
// examine connected nodes
if iNode <> iTo then begin
for i := 1 to arTerritory[iNode].NBord do begin
iChild := arTerritory[iNode].Bord[i];
// skip owned territories if required
if bAvoidOwned and (arTerritory[iChild].Owner = iTurn) then
continue
else if bUseOwned and (arTerritory[iChild].Owner <> iTurn) then
continue;
// check if child territory is in OPEN and CLOSED lists
bIsOpen := lstOpen.IndexOf(Pointer(iChild)) >= 0;
bIsClosed := aNode[iChild].bClosed;
// compute cost from node to child
if bArmies then begin
// cost = armies + 1
iCostFromStart := aNode[iNode].iCostFromStart + arTerritory[iChild]
.Army + 1;
end
else begin
// cost = distance (1000) + territory number
iCostFromStart := aNode[iNode].iCostFromStart + 1000 + iChild;
end;
// assign cost to child if node has not yet been already reached
// or if reached with higher cost
if (not bIsOpen and not bIsClosed) or
(iCostFromStart < aNode[iChild].iCostFromStart) then begin
aNode[iChild].iParent := iNode;
aNode[iChild].iCostFromStart := iCostFromStart;
if bIsClosed then
aNode[iChild].bClosed := false; // remove child from CLOSED list
if not bIsOpen then
lstOpen.Add(Pointer(iChild));
end;
end;
end; //
aNode[iNode].bClosed := true; // node has been examined, add to CLOSED list
end;
if not bFound then begin
// return with empty path and error if target was not reached
result := false;
iLength := -1;
iCost := MaxInt;
end
else begin
// compute length and cost of path
iLength := 0;
iCost := aNode[iTo].iCostFromStart;
iNode := iTo;
while aNode[iNode].iParent > 0 do begin
inc(iLength);
iNode := aNode[iNode].iParent;
end;
// build path array
if length(aPath) >= iLength then begin // check that array is large enough
i := iLength - 1; // dinamic array receceived as par is always 0-based
iNode := iTo;
while aNode[iNode].iParent > 0 do begin
aPath[i] := iNode;
dec(i);
iNode := aNode[iNode].iParent;
end;
result := true;
end
else begin // array is not large enough, return error
result := false;
iLength := -1;
iCost := MaxInt;
end;
end;
// free resources
lstOpen.Free;
end;
function TDistance(ST, DT: integer): integer;
// returns the distance (number of borders to cross) between territories ST and DT,
// -1 if ST or DT are out of range
var
iLength, iCost: integer;
aPath: array [1 .. 42] of integer;
begin
if (ST < 1) or (ST > MAXTERRITORIES) or (DT < 1) or (DT > MAXTERRITORIES)
then begin
result := -1;
exit
end;
if TPath(ST, DT, false, false, false, iLength, iCost, aPath) then begin
result := iLength;
end
else begin
result := -1; // this should never happen
end;
end;
function TShortestPath(ST, DT: integer; var TT, PL: integer): boolean;
// finds the shortest path to go from the start territory ST to the destination territory DT
// returns false if ST or DT are out of range, otherwise true
// the "var" variables will contain information on the path from ST to DT:
// TT: first territory in the path
// PL: path length (number of borders to cross)
var
iLength, iCost: integer;
aPath: array [1 .. 42] of integer;
begin
TT := 0;
PL := 0;
if (ST < 1) or (ST > MAXTERRITORIES) or (DT < 1) or (DT > MAXTERRITORIES)
then begin
result := false;
exit
end;
result := TPath(ST, DT, false, false, false, iLength, iCost, aPath);
if iLength > 0 then
TT := aPath[1]
else
TT := ST;
PL := iLength;
end;
function TWeakestPath(ST, DT: integer; var TT, PL, EA: integer): boolean;
// finds the weakest path to go from the start territory ST to the destination territory DT
// the "weakest path" is the path that contains the minimum number of enemy armies
// and that doesn't cross territories already owned by the current player
// returns false if ST or DT are out of range or if the path doesn't exist
// the "var" variables will contain information on the path from ST to DT:
// TT: first territory in the path
// PL: path length (number of territories to conquer)
// EA: total number of enemy armies along the path
var
iLength, iCost: integer;
aPath: array [1 .. 42] of integer;
begin
TT := 0;
PL := 0;
EA := 0;
if (ST < 1) or (ST > MAXTERRITORIES) or (DT < 1) or (DT > MAXTERRITORIES) or
(ST = DT) then begin
result := false;
exit
end;
result := TPath(ST, DT, true, false, true, iLength, iCost, aPath);
if iLength > 0 then
TT := aPath[1]
else
TT := ST;
PL := iLength;
EA := iCost - PL;
end;
function TPathToFront(T: integer): integer;
// finds the path to go from territory T to the closest front territory
// returns the number of the first territory in the path
// returns 0 if T is itself a front, -1 if T is out of range
var
iDest, iLength, iCost, iMinLength: integer;
aPath: array [1 .. 42] of integer;
begin
if (T < 1) or (T > MAXTERRITORIES) then begin
result := -1;
exit;
end;
if TIsFront(T) then begin
result := 0;
exit;
end;
// search for the closest front
iMinLength := MaxInt;
result := 0;
for iDest := 1 to MAXTERRITORIES do begin
if TIsFront(iDest) then begin
if TPath(T, iDest, false, true, false, iLength, iCost, aPath) then begin
if (iLength > 0) and (iLength < iMinLength) then begin
result := aPath[1];
iMinLength := iLength;
end;
end;
end;
end;
end;
// *********************
// * ROUTINES "PLAYER" *
// *********************
function PMe: integer;
// returns the ID of the player in turn
begin
result := iTurn;
end;
function PName(P: integer): string;
// returns player's P name, '?' if P is out of range
begin
if (P > 0) and (P <= MAXPLAYERS) then
result := arPlayer[P].Name
else
result := '?';
end;
function PProgram(P: integer): string;
// returns player's program file name (lowercase), 'human' if human,
// '?' if P is out of range
begin
if (P > 0) and (P <= MAXPLAYERS) then begin
if arPlayer[P].Computer then
result := lowercase(arPlayer[P].PrgFile)
else
result := 'human';
end
else begin
result := '?';
end;
end;
function PActive(P: integer): boolean;
// returns true if player P was active at the beginning of the game, false if not or P is out of range
begin
if (P > 0) and (P <= MAXPLAYERS) then
result := arPlayer[P].Active
else
result := false;
end;
function PAlive(P: integer): boolean;
// returns true if player P is alive, false if not or P is out of range
begin
if (P > 0) and (P <= MAXPLAYERS) then begin
if GameState = gsAssigning then
result := arPlayer[P].Active
else
result := arPlayer[P].Active and (arPlayer[P].Territ > 0);
end
else begin
result := false;
end;
end;
function PHuman(P: integer): boolean;
// returns true if P is a human player, false if P is computer player or P is out of range
begin
if (P > 0) and (P <= MAXPLAYERS) then
result := not arPlayer[P].Computer
else
result := false;
end;
function PArmiesCount(P: integer): integer;
// returns the number of armies totally controlled by player P on all
// of its territories, -1 if P is out of range
var
T, A: integer;
begin
if (P > 0) and (P <= MAXPLAYERS) then begin
A := 0;
for T := 1 to MAXTERRITORIES do
if arTerritory[T].Owner = P then
inc(A, arTerritory[T].Army);
result := A;
end
else
result := -1;
end;
function PNewArmies(P: integer): integer;
// returns the number of armies which player P still has to place on his territories,
// -1 if P is out of range
begin
if (P > 0) and (P <= MAXPLAYERS) then
result := arPlayer[P].NewArmy
else
result := -1;
end;
function PTerritoriesCount(P: integer): integer;
// returns the number of territories controlled by player P, -1 if P is out of range
var
T, O: integer;
begin
if (P > 0) and (P <= MAXPLAYERS) then begin
O := 0;
for T := 1 to MAXTERRITORIES do
if arTerritory[T].Owner = P then
inc(O);
result := O;
end
else
result := -1;
end;
function PCardCount(P: integer): integer;
// returns the number of cards Player P has, -1 if P is out of range
begin
if (P > 0) and (P <= MAXPLAYERS) then begin
with arPlayer[P] do begin
result := Cards[caInf] + Cards[caArt] + Cards[caCav] + Cards[caJok];
end;
end
else
result := -1;
end;
function PCardTurnInValue(P: integer): integer;
// if the trade in value is based on progression, returns the value of Player P's
// next card turn in
// returns 0 if the trade in value is based on combination, -1 if P is out of range
begin
if (P <= 0) or (P > MAXPLAYERS) then begin
result := -1;
exit;
end;
if RCardsValueType = cvConstant then begin
result := 0;
exit;
end;
with arPlayer[P] do begin
if NScambi < 8 then
result := RTradeValue[NScambi + 1]
else
result := RTradeValue[8] + (NScambi - 7) * RValueInc;
end;
end;
// ************************
// * ROUTINES "CONTINENT" *
// ************************
function COwner(C: integer): integer;
// returns the owner of continent C, 0 if C has more then one occupant, -1 if C is out of range
begin
if (C > 0) and (C <= 6) then
result := arContinent[TContId(C - 1)].Owner
else
result := -1;
end;
function CBonus(C: integer): integer;
// returns the number of armies (per turn) which the control of continent C entitles to, -1 if C is out of range
begin
if (C > 0) and (C <= 6) then
result := arContinent[TContId(C - 1)].Bonus
else
result := -1;
end;
function CTerritoriesCount(C: integer): integer;
// returns the number of territories belonging to continent C, -1 if C is out of range
begin
if (C > 0) and (C <= 6) then
case TContId(C - 1) of
coNA:
result := 9;
coSA:
result := 4;
coAF:
result := 6;
coEU:
result := 7;
coAS:
result := 12;
coAU:
result := 4;
else
result := -1
end
else
result := -1;
end;
function CTerritory(C, T: integer): integer;
// returns the ID of territory #T belonging to continent C, -1 if T or C are out of range
begin
if (C > 0) and (C <= 6) and (T > 0) then
case TContId(C - 1) of
coNA:
if T > 9 then
result := -1
else
result := T;
coSA:
if T > 4 then
result := -1
else
result := T + 9;
coAF:
if T > 6 then
result := -1
else
result := T + 13;
coEU:
if T > 7 then
result := -1
else
result := T + 19;
coAS:
if T > 12 then
result := -1
else
result := T + 26;
coAU:
if T > 4 then
result := -1
else
result := T + 38;
else
result := -1
end
else
result := -1;
end;
function CBordersCount(C: integer): integer;
// returns the number of border territories on continent C (max 6), -1 if C is out of range
// Border territories are territories which are outside the continent but
// may be accessed from it
begin
if (C > 0) and (C <= 6) then
case TContId(C - 1) of
coNA:
result := 3;
coSA:
result := 2;
coAF:
result := 4;
coEU:
result := 6;
coAS:
result := 6;
coAU:
result := 1;
else
result := -1
end
else
result := -1;
end;
function CBorder(C, B: integer): integer;
// returns border territory #B of continent C, -1 if C or B are out of range
// Border territories are territories which are outside the continent but
// may be accessed from it
begin
if (C > 0) and (C <= 6) and (B > 0) then
case TContId(C - 1) of
coNA:
case B of
1:
result := 20;
2:
result := 10;
3:
result := 37;
else
result := -1;
end;
coSA:
case B of
1:
result := 9;
2:
result := 14;
else
result := -1;
end;
coAF:
case B of
1:
result := 12;
2:
result := 25;
3:
result := 26;
4:
result := 27;
else
result := -1;
end;
coEU:
case B of
1:
result := 3;
2:
result := 15;
3:
result := 14;
4:
result := 27;
5:
result := 28;
6:
result := 29;
else
result := -1;
end;
coAS:
case B of
1:
result := 1;
2:
result := 15;
3:
result := 16;
4:
result := 22;
5:
result := 26;
6:
result := 39;
else
result := -1;
end;
coAU:
case B of
1:
result := 31;
else
result := -1;
end;
else
result := -1
end
else
result := -1;
end;
function CAnalysis(C: integer; var PT, PA, ET, EA: integer): boolean;
// returns false if C is out of range, otherwise true
// the "var" variables will contain information on continent C:
// PT: player in turn's number of territories on continent C
// PA: player in turn's armies on continent C
// ET: enemy's number of territories on continent C
// EA: enemy's armies on continent C
// (unassigned territories are not counted)
var
T: integer;
tCont: TContId;
begin
if (C < 1) or (C > 6) then begin
result := false;
exit;
end;
tCont := TContId(C - 1);
PT := 0;
PA := 0;
ET := 0;
EA := 0;
// analisi
for T := 1 to MAXTERRITORIES do begin
if arTerritory[T].Contin = tCont then begin
if arTerritory[T].Owner = iTurn then begin
inc(PT);
inc(PA, arTerritory[T].Army);
end
else if arTerritory[T].Owner <> 0 then begin
inc(ET);
inc(EA, arTerritory[T].Army);
end;
end;
end;
result := true;
end;
function CLeader(C: integer; var P, T, A: integer): boolean;
// returns false if C is out of range, otherwise true
// the "var" variables will contain information on continent C:
// P: leader = player who controls greatest number of territories
// T: leader's number of territories on continent C
// A: leader's armies on continent C
// if two or more players own the same number of territories, the leader is the
// one which has more armies. If continent is empty (all territories unissegned)
// P returns 0.
// (unassigned territories are not counted)
var
iT, iG, iMax, iVal: integer;
arP: array [1 .. MAXPLAYERS] of record T, A: integer end;
tCont: TContId;
begin
if (C < 1) or (C > 6) then begin
result := false;
exit;
end;
tCont := TContId(C - 1);
P := 0;
T := 0;
A := 0;
// reset vettore
for iG := 1 to MAXPLAYERS do begin
arP[iG].T := 0;
arP[iG].A := 0;
end;
// analisi
for iT := 1 to MAXTERRITORIES do begin
with arTerritory[iT] do begin
if Contin = tCont then begin
if Owner <> 0 then begin
inc(arP[Owner].T);
inc(arP[Owner].A, Army);
end;
end;
end;
end;
// trova leader
iMax := 0;
for iG := 1 to MAXPLAYERS do begin
iVal := arP[iG].T * 1000 + arP[iG].A;
if iVal > iMax then begin
iMax := iVal;
P := iG;
T := arP[iG].T;
A := arP[iG].A;
end;
end;
result := true;
end;
function CEntriesCount(C: integer): integer;
// returns the number of entry territories on continent C (max 6), -1 if C is out of range
// Entry territories are territories which are part of the continent but
// may be accessed from another continent
begin
if (C > 0) and (C <= 6) then
case TContId(C - 1) of
coNA:
result := 3;
coSA:
result := 2;
coAF:
result := 3;
coEU:
result := 4;
coAS:
result := 5;
coAU:
result := 1;
else
result := -1
end
else
result := -1;
end;
function CEntry(C, B: integer): integer;
// returns entry territory #B of continent C, -1 if C or B are out of range
// Entry territories are territories which are part of the continent but
// may be accessed from another continent
begin
if (C > 0) and (C <= 6) and (B > 0) then
case TContId(C - 1) of
coNA:
case B of
1:
result := 1;
2:
result := 3;
3:
result := 9;
else
result := -1;
end;
coSA:
case B of
1:
result := 10;