-
Notifications
You must be signed in to change notification settings - Fork 3
/
Globals.pas
2081 lines (1975 loc) · 73.9 KB
/
Globals.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 Globals;
{$MODE Delphi}
interface
uses LCLIntf, LCLType, LMessages, Graphics, Classes, uPSRuntime, uPSPreProcessor;
const
VERSION = '2.0.6D'; // TurboRisk
VERSION_TRSIM = '1.0A'; // TRSim
MAXTERRITORIES = 42; // Number of countries in the map
MAXBORDERS = 6; // Max number of borders per Territory
MAXORIGINS = 20; // Max number of floodfill points per Territory (for coloring)
MAXPLAYERS = 10; // Max number of players
MAXPLSETS = 50; // Max number of players sets
STDPLSETS = 5; // Number of standard players sets
// MAXLOGLINES = 100; // Max number of log lines
MAXBUFFER = 500; // Number of buffers for the computer players
aiDefTradeVal: array [1 .. 8] of byte = (4, 6, 8, 10, 12, 15, 20, 25);
aiDefIniArmy: array [2 .. MAXPLAYERS] of byte = (40, 35, 30, 25, 20, 20, 20,
20, 20);
atDefColor: array [1 .. MAXPLAYERS] of TColor = (clBlue, clRed, clGreen,
clYellow, clAqua, clFuchsia, clLime, clCream, clMaroon, clPurple);
type
TGameState = (gsStopped, gsAssigning, gsDistributing, gsPlaying);
THumanPhase = (hpPlacement, hpAttack);
TCard = (caInf, caArt, caCav, caJok);
TCardsSet = (csInf, csArt, csCav, csDif);
TCardsHandling = (chManual, chSmart, chAuto);
TContId = (coNA, coSA, coEU, coAF, coAS, coAU);
TSimStatus = (ssRunning, ssComplete, ssError, ssTurnLimit, ssTimeLimit,
ssAbort);
TTerritory = record // Information about a territory
// Dynamic, they change during the game
Owner: integer; // Player who owns the territory
Army: integer; // Number of occupying armies
// Static, specific of each territory
Name: string; // Name
NBord: integer; // Number of borders
Bord: array [1 .. MAXBORDERS] of integer;
// Pointers to the border territories
Contin: TContId; // Continent which the territory belongs to
// Semi-dynamic, depending on the actual map
Color: TColor; // Color in the base map
Coord: TPoint; // Coordinates for the label showing the armies
NOrig: integer; // Number of floodfill areas for coloring
Orig: array [1 .. MAXORIGINS] of TPoint; // Coordinates of floodfill areas
end;
TContInfo = record // Information about a continent
// Dynamic, they change during the game
Owner: integer; // Owner of the continent (if any)
// Static, specific of each continent
Name: string; // Name
Bonus: integer; // Bonus armies for the owner
end;
TCPUusage = record // Information about CPU usage by TRPs (for TRSim)
iTime: int64; // length of calls (in high resolution timer ticks)
iPhases, // number of phases (e.g. attack, occupation...)
iCalls: integer; // number of calls
end;
TRoutine = (rtAssignment, rtPlacement, rtAttack, rtOccupation,
rtFortification);
TPlayer = record // Information about a player
// Parameters selected by the player
Name: string; // Name
Active: boolean; // Flag, true when the player is (still) active
Color: TColor; // Color to represent the player
Computer: boolean; // Flag, true if player is a TRP
CardsHandling: TCardsHandling; // Card handling option
KeepLog: boolean; // Flag, true if the player keeps the log
PrgFile: string; // Source file name for computer players (TRP)
Code: ansistring; // Tokenized script for computer players (compiled TRP)
// Status variables
Army: integer; // Total armies (not updated in real-time)
NewArmy: integer; // Armies to be placed
Territ: integer; // Number of owned territories
Cards: array [TCard] of integer; // Count of the owned cards (number of cards per kind)
NScambi: integer; // N° scambi carte effettuati
FlConq: boolean; // Flag, true if the player conquered at least one territory during the turn
FlMove: boolean; // Flag, true if the player performed at leaste one troops move during the turn
LastTurn, // last turn played by the player
Rank: integer; // Rank at the end of the game
// Runtime variables
Buffer: array [1 .. MAXBUFFER] of double; // Buffer reserved to the TRPs for their 'static' values
USnapShotEnabled, // flag to enable USnapShot
ULogEnabled, // flag to enable ULog
UMessageEnabled, // flag to enable UMessage
UDialogEnabled: boolean; // flag to enable UDialog
// TRSim statistics
aCPU: array [TRoutine] of TCPUusage;
end;
TPlayersSet = record // Information about a set of players
Name: string; // Name
arPlSet: array [0 .. MAXPLAYERS] of record Name: string; // Name
Active: boolean; // Flag, true when the player is (still) active
Color: TColor; // Color to represent the player
Computer: boolean; // Flag, true if player is a TRP
CardsHandling: TCardsHandling; // Card handling option
KeepLog: boolean; // Flag, true if the player keeps the log
PrgFile: string; // Source file name for computer players (TRP)
end;
end;
var
// Generic global variabils
bG_TRSim: boolean; // true if the main program is TRSim
sG_AppName, // application name
sG_AppVers, // version
sG_AppPath: string; // exe file name
sG_LastNews: TDate; // last read news
iPerformanceFrequency: int64; // frequency of the high-resolution performance counter
// Global variables about the state of the game
GameState: TGameState; // phase of the game
iTurnCounter, // counts turn progression
iTurn: integer; // current player
iFirstTurn: integer; // player who play first and starts the game
iNPlayers: integer; // number of players in the game
iToAssign: integer; // number of territories non yet assigned
bHumanTurn: boolean; // flag, true if current turn is for a human player
HumanPhase: THumanPhase; // human player's phase of the turn
bEliminatedPlayer: boolean; // flag, true if opponent got eliminated during the last attack
bStopASAP: boolean; // the user required to stop the game
bCloseASAP: boolean; // the user required to close TurboRisk
iRank: integer; // rank to assign to the player
// Global variables about the map
BaseMap: TBitmap; // Hidden base map for territory recognition
arTerritory: array [1 .. MAXTERRITORIES] of TTerritory; // Territories
arContinent: array [TContId] of TContInfo; // Continents
sMapFile, // File name of the map (.trm)
sMapDesc, // Map description
sMapFontName: string; // Map font name
iMapFontSize: integer; // Map font size
tMapTextFG, tMapTextBG: TColor; // text foreground and background colors
// Global variables about players and sets of players
arPlayer: array [0 .. MAXPLAYERS] of TPlayer; // Players
arPlayersSet: array [0 .. MAXPLSETS] of TPlayersSet; // Players
aiTurnList: array [0 .. MAXPLAYERS] of integer; // Turn sequence
iPlSet, // curent players set
iPlSetCount: integer; // total players sets
// Global variables about the rules
RAssignmentType: (atRandom, atTurns); // Type of territory assignment
RInitialArmies: array [2 .. MAXPLAYERS] of integer;
// Number of initial armies to place
RCardsValueType: (cvConstant, cvProgressive); // Card trade value scheme
RSetValue: array [TCardsSet] of integer; // Value of the cards sets for the constant value scheme
RTradeValue: array [1 .. 8] of integer; // Value of the cards sets for the progressive value scheme
RValueInc: integer; // Value increment for the progressive value scheme after 8th trade
RMaxHeldCards: integer; // Max number of cards a player can hold without trading
RFinalMove: boolean; // Flag, true if troops move is allowed only at the end of a turn
RImmediateTrade: boolean; // Flag, true if immediate trade of captured cards is allowed
// Global variable about stats
bStOpTer, // flag, if true the stats show territories
bStOpArm, // flag, if true the stats show armies
bStOpCon, // flag, if true the stats show continents
bStOpCar, // flag, if true the stats show cards count
bStOpCTIV: boolean; // flag, if true the stats show cards turn in value
// The script executer
ScriptExec: TPSExec;
// Interface preferences
sPrefTlbButtons: string; // toolbar buttons
bPrefConfirmAbort, // flag, true if user is required confirmation before aborting a game in progress
bPrefShowToolbar, // flag, true if user wants the toolbar enabled
bPrefCheckUpdate, // flag, true if user wants to check for updates at startup
bPrefExpertAttack, // flag, true if user wants the Expert Attack window
bPrefMapHoover, // flag, true if user wants territory to change color when hooverd
bPrefMapSelected: boolean; // flag, true if user wants territory to change color when selected
iPrefMapHoover, // % of color intensity decrease/increase when hoovering
iPrefMapSelected: integer; // % of color intensity decrease/increase when selecting
// TRSim simulation environment
iSimCurr, // current simulation game
iSimCompl, // number of finished games
iSimGames, // number of games to simulate
iSimWinner, // number of the winner
iSimGameTime, // duration of last game, in seconds
iSimTimeLimit, // max number of seconds per game
iSimTurnLimit, // max number of turns per game
iSimPlayers, // number of players in current game
iSimMinPl, // minimum players per game
iSimMaxPl: integer; // maximum players per game
uSimStatus: TSimStatus; // termination type
bSimAbort: boolean; // flag, true to abort simulation
dtSimGameTime, // game start time
dtSimStartTime: TDateTime; // simulation start time
sSimGameLogFile, // name of the game log file
sSimCPULogFile: string; // name of the CPU log file
// Application Setup
procedure Setup;
// Application Cleanup
procedure Cleanup;
// Menu setup according to the state of the game
procedure MenuSetup;
// New game setup
procedure NewGameSetup;
// Game cleanup
procedure GameCleanup;
// Initial random assignment
procedure RandomAssignment;
// Assign new armies
procedure AssignNewArmies(bCardsOnly: boolean);
// Perform an attack
function PerformAttack(iTA, iTD: integer): boolean;
// Supervisor for the game turns and phases
procedure Supervisor;
// Update history file
procedure UpdateHistoryFile;
// Restore game from file
procedure RestoreGame(sFileName: string);
// Save game to file
procedure SaveGame(sFileName, sErrorMsg: string);
// Find best contrast text color
function BestTextColor(tBkgColor: TColor): TColor;
// Make a color lighter or darker
function ChangeColor(Color: TColor; Percent: shortint): TColor;
// functions to set and get curront from, to and hoovered territories
function GetFromTerritory: integer;
function GetToTerritory: integer;
function GetHooverTerritory: integer;
procedure SetFromTerritory(iT: integer);
procedure SetToTerritory(iT: integer);
procedure SetHooverTerritory(iT: integer);
implementation
uses Forms, SysUtils, IniFiles, Dialogs, Math, DateUtils,
uPSCompiler,
Main, Territ, Computer, Stats, Human, Cards, Log, ExpSubr, History, SimRun;
var
bAssignmentFound, // flags to check if the script
bPlacementFound, // contains all the required procedures
bAttackFound, bOccupationFound, bFortificationFound: boolean;
// private vars about territories, changed and queried through proper functions
HooverTerrit, // territory hoovered with the mouse pointer
ToTerrit, // territory choosen by curent player to attack or to move to
FromTerrit: integer; // territory choosen by curent player to attack or to move from
function ScriptOnUses(Sender: TPSPascalCompiler;
const Name: ansistring): boolean;
{ the OnUses callback function is called for each "uses" in the script.
It's always called with the parameter 'SYSTEM' at the top of the script.
For example: uses ii1, ii2;
This will call this function 3 times. First with 'SYSTEM' then 'II1' and then 'II2'.
}
begin
if Name = 'SYSTEM' then begin
// register external functions
Sender.AddDelphiFunction('function TName(T: integer): string');
Sender.AddDelphiFunction('function TOwner(T: integer): integer');
Sender.AddDelphiFunction('function TName(T: integer): string');
Sender.AddDelphiFunction('function TOwner(T: integer): integer');
Sender.AddDelphiFunction('function TArmies(T: integer): integer');
Sender.AddDelphiFunction('function TContinent(T: integer): integer');
Sender.AddDelphiFunction('function TBordersCount(T: integer): integer');
Sender.AddDelphiFunction('function TBorder(T,B: integer): integer');
Sender.AddDelphiFunction('function TIsBordering(T1,T2: integer): boolean');
Sender.AddDelphiFunction('function TIsFront(T: integer): boolean');
Sender.AddDelphiFunction('function TIsMine(T: integer): boolean');
Sender.AddDelphiFunction('function TIsEntry(T: integer): boolean');
Sender.AddDelphiFunction('function TFrontsCount(T: integer): integer');
Sender.AddDelphiFunction('function TFront(T,F: integer): integer');
Sender.AddDelphiFunction(
'function TStrongestFront(T: integer; var ET,EA: integer): boolean');
Sender.AddDelphiFunction(
'function TWeakestFront(T: integer; var ET,EA: integer): boolean');
Sender.AddDelphiFunction('function TPressure(T: integer): integer');
Sender.AddDelphiFunction('function TDistance(ST, DT: integer): integer;');
Sender.AddDelphiFunction(
'function TShortestPath(ST, DT: integer; var TT, PL: integer): boolean;');
Sender.AddDelphiFunction(
'function TWeakestPath(ST, DT: integer; var TT, PL, EA: integer): boolean;');
Sender.AddDelphiFunction('function TPathToFront(T: integer): integer;');
Sender.AddDelphiFunction('function PMe: integer');
Sender.AddDelphiFunction('function PName(P: integer): string');
Sender.AddDelphiFunction('function PProgram(P: integer): string');
Sender.AddDelphiFunction('function PActive(P: integer): boolean');
Sender.AddDelphiFunction('function PAlive(P: integer): boolean');
Sender.AddDelphiFunction('function PHuman(P: integer): boolean');
Sender.AddDelphiFunction('function PArmiesCount(P: integer): integer');
Sender.AddDelphiFunction('function PNewArmies(P: integer): integer');
Sender.AddDelphiFunction('function PTerritoriesCount(P: integer): integer');
Sender.AddDelphiFunction('function PCardCount(P: integer): integer');
Sender.AddDelphiFunction('function PCardTurnInValue(P: integer): integer');
Sender.AddDelphiFunction('function COwner(C: integer): integer');
Sender.AddDelphiFunction('function CBonus(C: integer): integer');
Sender.AddDelphiFunction('function CTerritoriesCount(C: integer): integer');
Sender.AddDelphiFunction('function CTerritory(C,T: integer): integer');
Sender.AddDelphiFunction('function CBordersCount(C: integer): integer');
Sender.AddDelphiFunction('function CBorder(C,B: integer): integer');
Sender.AddDelphiFunction('function CEntriesCount(C: integer): integer');
Sender.AddDelphiFunction('function CEntry(C,B: integer): integer');
Sender.AddDelphiFunction(
'function CAnalysis(C: integer; var PT,PA,ET,EA: integer): boolean');
Sender.AddDelphiFunction(
'function CLeader(C: integer; var P,T,A: integer): boolean');
Sender.AddDelphiFunction('function SConquest: boolean');
Sender.AddDelphiFunction('function SPlayersCount: integer');
Sender.AddDelphiFunction('function SAlivePlayersCount: integer');
Sender.AddDelphiFunction('function SCardsBasedOnCombo: boolean');
Sender.AddDelphiFunction('procedure UMessage(M: string)');
Sender.AddDelphiFunction('procedure ULog(M: string)');
Sender.AddDelphiFunction('procedure UBufferSet(B: integer; V: double)');
Sender.AddDelphiFunction('function UBufferGet(B: integer): double');
Sender.AddDelphiFunction('function URandom(R: integer): double');
Sender.AddDelphiFunction('procedure UTakeSnapshot(M: string)');
Sender.AddDelphiFunction('function UDialog(M,B: string): integer');
Sender.AddDelphiFunction('procedure UAbortGame');
Sender.AddDelphiFunction('procedure ULogOff');
Sender.AddDelphiFunction('procedure ULogOn');
Sender.AddDelphiFunction('procedure UMessageOff');
Sender.AddDelphiFunction('procedure UMessageOn');
Sender.AddDelphiFunction('procedure UDialogOff');
Sender.AddDelphiFunction('procedure UDialogOn');
Sender.AddDelphiFunction('procedure USnapShotOff');
Sender.AddDelphiFunction('procedure USnapShotOn');
Result := True;
end
else
Result := False;
end;
function ScriptOnExportCheck(Sender: TPSPascalCompiler;
Proc: TPSInternalProcedure; const ProcDecl: ansistring): boolean;
{
The OnExportCheck callback function is called for each function in the script
(Also for the main proc, with '!MAIN' as a Proc^.Name). ProcDecl contains the
result type and parameter types of a function using this format:
ProcDecl: ResultType + ' ' + Parameter1 + ' ' + Parameter2 + ' '+Parameter3 + .....
Parameter: ParameterType+TypeName
ParameterType is @ for a normal parameter and ! for a var parameter.
A result type of 0 means no result.
}
var
sExpectedPar: string;
begin
sExpectedPar := '';
if (Proc.Name = 'ASSIGNMENT') then begin
bAssignmentFound := True;
sExpectedPar := '0 !LONGINT';
end
else if (Proc.Name = 'PLACEMENT') then begin
bPlacementFound := True;
sExpectedPar := '0 !LONGINT';
end
else if (Proc.Name = 'ATTACK') then begin
bAttackFound := True;
sExpectedPar := '0 !LONGINT !LONGINT';
end
else if (Proc.Name = 'OCCUPATION') then begin
bOccupationFound := True;
sExpectedPar := '0 @LONGINT @LONGINT !LONGINT';
end
else if (Proc.Name = 'FORTIFICATION') then begin
bFortificationFound := True;
sExpectedPar := '0 !LONGINT !LONGINT !LONGINT';
end;
// check if procedure declaration matched expected declaration
if sExpectedPar <> '' then begin
if ProcDecl <> sExpectedPar then begin
Sender.MakeError('', ecCustomError,
'Type mismatch in declaration of procedure ' + Proc.Name);
Result := False;
Exit;
end;
// Proc.aExport := etExportDecl; // Export the proc; This is needed because IFPS doesn't store the name of a function by default
Result := True;
end
else begin
Result := True;
end;
end;
procedure DefaultPlayersSets;
var
i, iP, iPLCount: integer;
procedure AddPlayer(i: integer; sPrg: string);
begin
with arPlayersSet[i] do begin
inc(iPLCount);
arPlSet[iPLCount].PrgFile := sPrg;
arPlSet[iPLCount].Name := ChangeFileExt(sPrg, '');
arPlSet[iPLCount].Active := True;
arPlSet[iPLCount].Computer := True;
end;
end;
begin
iPlSetCount := STDPLSETS;
for i := 1 to STDPLSETS do
with arPlayersSet[i] do begin
for iP := 1 to MAXPLAYERS do begin
arPlSet[iP].Name := '';
arPlSet[iP].Computer := False;
arPlSet[iP].Active := False;
arPlSet[iP].CardsHandling := chSmart;
arPlSet[iP].Color := atDefColor[iP];
end;
iPLCount := 1;
arPlSet[1].Name := 'human';
arPlSet[1].Active := True;
case i of
1: begin
Name := '* Very easy *';
AddPlayer(i, 'napoleon.trp');
AddPlayer(i, 'rambo.trp');
AddPlayer(i, 'simple.trp');
end;
2: begin
Name := '* Easy *';
AddPlayer(i, 'alexander.trp');
AddPlayer(i, 'borg.trp');
AddPlayer(i, 'napoleon.trp ');
AddPlayer(i, 'rambo.trp');
AddPlayer(i, 'struggler.trp ');
end;
3: begin
Name := '* Medium *';
AddPlayer(i, 'digger.trp');
AddPlayer(i, 'era$or-bot.trp');
AddPlayer(i, 'raptorbot.trp');
AddPlayer(i, 'shy.trp');
AddPlayer(i, 'struggler.trp');
end;
4: begin
Name := '* Hard *';
AddPlayer(i, 'australian+.trp');
AddPlayer(i, 'digger.trp');
AddPlayer(i, 'era$or-bot.trp');
AddPlayer(i, 'raptorbot.trp');
AddPlayer(i, 'shy2.trp');
AddPlayer(i, 'wyrm.trp');
AddPlayer(i, 'zotob.trp');
end;
5: begin
Name := '* Very hard *';
AddPlayer(i, 'australian09.trp');
AddPlayer(i, 'descartes09.trp');
AddPlayer(i, 'frank+.trp');
AddPlayer(i, 'raptorbot.trp');
AddPlayer(i, 'shy.trp');
AddPlayer(i, 'vexer.trp');
AddPlayer(i, 'wyrm.trp');
AddPlayer(i, 'struggler+.trp');
end;
end;
end;
end;
// Application Setup
procedure Setup;
var
IniFile: TIniFile;
i, iP: integer;
sTmp: string;
begin
Randomize;
// get the frequency of the high-resolution performance counter
iPerformanceFrequency:=1;//SysUtils.GetTickCount64();
// QueryPerformanceFrequency(iPerformanceFrequency);
if iPerformanceFrequency = 0 then
iPerformanceFrequency := 1;
// generic global vars
sG_AppName := 'TurboRisk';
if bG_TRSim then
sG_AppVers := VERSION_TRSIM
else
sG_AppVers := VERSION;
sG_AppPath := ExtractFilePath(Application.ExeName);
bCloseASAP := False;
// Default players sets
DefaultPlayersSets;
// load INI file
IniFile := TIniFile.Create(sG_AppPath + sG_AppName + '.INI');
try
with IniFile do begin
// News
sTmp := ReadString('News', 'LastRead', FormatDateTime('yyyymmdd',Today));
sG_LastNews := EncodeDate(StrToInt(copy(sTmp, 1, 4)),
StrToInt(copy(sTmp, 5, 2)), StrToInt(copy(sTmp, 7, 2)));
// Windows setup
fMain.Top := ReadInteger('Windows', 'MainTop', 0);
fMain.Left := ReadInteger('Windows', 'MainLeft', 0);
fStats.Top := ReadInteger('Windows', 'StatsTop', 0);
fStats.Left := ReadInteger('Windows', 'StatsLeft', 0);
bStOpTer := ReadBool('Windows', 'StatsTer', True);
bStOpArm := ReadBool('Windows', 'StatsArm', True);
bStOpCon := ReadBool('Windows', 'StatsCon', True);
bStOpCar := ReadBool('Windows', 'StatsCar', True);
bStOpCTIV := ReadBool('Windows', 'StatsCTIV', True);
if bG_TRSim then
fStats.Visible := False
else
fStats.Visible := ReadBool('Windows', 'StatsVisible', False);
fLog.Top := ReadInteger('Windows', 'LogTop', 0);
fLog.Left := ReadInteger('Windows', 'LogLeft', 0);
fLog.Width := ReadInteger('Windows', 'LogWidth', 100);
fLog.Height := ReadInteger('Windows', 'LogHeight', 100);
fLog.Visible := ReadBool('Windows', 'LogVisible', False);
fHistory.Top := ReadInteger('Windows', 'HistoryTop', 0);
fHistory.Left := ReadInteger('Windows', 'HistoryLeft', 0);
fHistory.Width := ReadInteger('Windows', 'HistoryWidth', 500);
fHistory.Height := ReadInteger('Windows', 'HistoryHeight', 400);
// Preferences
bPrefShowToolbar := ReadBool('Pref', 'ShowToolbar', True);
fMain.tlbToolBar.Visible := bPrefShowToolbar;
sPrefTlbButtons := ReadString('Pref', 'Toolbar', '011111000000000');
for i := 0 to fMain.tlbToolBar.ButtonCount - 3 do begin
fMain.tlbToolBar.Buttons[i].Visible := (sPrefTlbButtons[i + 1] <> '0');
end;
bPrefExpertAttack := ReadBool('Pref', 'ExpertAttack', False);
bPrefMapHoover := ReadBool('Pref', 'MapHooverOn', True);
bPrefMapSelected := ReadBool('Pref', 'MapSelectedOn', True);
iPrefMapHoover := ReadInteger('Pref', 'MapHooverP', -20);
iPrefMapSelected := ReadInteger('Pref', 'MapSelectedP', -20);
bPrefConfirmAbort := ReadBool('Pref', 'ConfirmAbort', True);
bPrefCheckUpdate := ReadBool('Pref', 'CheckUpdate', False);
// Map
sMapFile := ReadString('Map', 'File', 'std_map_small.trm');
// Rules
case ReadInteger('Rules', 'Assignment', 0) of
0:
RAssignmentType := atRandom;
1:
RAssignmentType := atTurns;
end;
for iP := 2 to MAXPLAYERS do begin
RInitialArmies[iP] := ReadInteger('Rules', 'InitArmies' + IntToStr(iP),
aiDefIniArmy[iP]);
end;
case ReadInteger('Rules', 'CardUsage', 1) of
0:
RCardsValueType := cvConstant;
1:
RCardsValueType := cvProgressive;
end;
RSetValue[csArt] := ReadInteger('Rules', 'ArtSetValue', 4);
RSetValue[csInf] := ReadInteger('Rules', 'InfSetValue', 6);
RSetValue[csCav] := ReadInteger('Rules', 'CavSetValue', 8);
RSetValue[csDif] := ReadInteger('Rules', 'DifSetValue', 10);
for i := 1 to 8 do begin
RTradeValue[i] := ReadInteger('Rules', 'TradeValue' + IntToStr(i),
aiDefTradeVal[i]);
end;
RValueInc := ReadInteger('Rules', 'TradeValueInc', 5);
RMaxHeldCards := ReadInteger('Rules', 'MaxHeldCards', 5);
RImmediateTrade := ReadBool('Rules', 'TradeCapturedCards', True);
RFinalMove := ReadBool('Rules', 'AllowAttackAfterMove', True);
// Players
for iP := 1 to MAXPLAYERS do begin
with arPlayer[iP] do begin
Name := ReadString('Player' + IntToStr(iP), 'Name',
'Player' + IntToStr(iP));
Active := ReadBool('Player' + IntToStr(iP), 'Active', True);
Color := ReadInteger('Player' + IntToStr(iP), 'Color',
atDefColor[iP]);
Computer := ReadBool('Player' + IntToStr(iP), 'Computer', True);
KeepLog := ReadBool('Player' + IntToStr(iP), 'Log', False);
CardsHandling := TCardsHandling(ReadInteger('Player' + IntToStr(iP),
'AutoCards', 1));
PrgFile := ReadString('Player' + IntToStr(iP), 'PrgFile', '');
if PrgFile = '' then
PrgFile := 'simple.trp';
end;
end;
// Players sets
iPlSetCount := ReadInteger('PlSets', 'Count', STDPLSETS);
iPlSet := ReadInteger('PlSets', 'Set', 0);
for i := STDPLSETS + 1 to iPlSetCount do begin
arPlayersSet[i].Name := ReadString('PlSet' + IntToStr(i), 'Name',
'PlSet' + IntToStr(i));
for iP := 1 to MAXPLAYERS do begin
with arPlayersSet[i].arPlSet[iP] do begin
Name := ReadString('PlSet' + IntToStr(i), 'Name' + IntToStr(iP),
'');
Active := ReadBool('PlSet' + IntToStr(i), 'Active' + IntToStr(iP),
True);
Color := ReadInteger('PlSet' + IntToStr(i), 'Color' + IntToStr(iP),
atDefColor[iP]);
Computer := ReadBool('PlSet' + IntToStr(i),
'Computer' + IntToStr(iP), True);
KeepLog := ReadBool('PlSet' + IntToStr(i), 'Log' + IntToStr(iP),
False);
CardsHandling := TCardsHandling(ReadInteger('PlSet' + IntToStr(i),
'AutoCards' + IntToStr(iP), 1));
PrgFile := ReadString('PlSet' + IntToStr(i),
'PrgFile' + IntToStr(iP), '');
if PrgFile = '' then
PrgFile := 'simple.trp';
end;
end;
end;
end;
finally
IniFile.Free;
end;
// 'nil' player, which is the default for non-assigned territories
with arPlayer[0] do begin
Name := 'Nil';
Active := False;
Color := clSilver;
end;
// Setup territories
SetupTerritories;
// Creat base map used to find territory from mouse position
BaseMap := TBitmap.Create;
// Load map from file
LoadMap;
// Initial state of the game (just as a game had just been completed)
GameCleanup;
end;
// Application Cleanup
procedure Cleanup;
var
IniFile: TIniFile;
i, iP: integer;
begin
if not bG_TRSim then begin // TRSim does not save main TurboRisk ini file
// save setup on INI file
IniFile := TIniFile.Create(sG_AppPath + sG_AppName + '.INI');
try
with IniFile do begin
// News
WriteString('News', 'LastRead',
FormatDateTime('yyyymmdd', sG_LastNews));
// Windows
WriteInteger('Windows', 'MainTop', fMain.Top);
WriteInteger('Windows', 'MainLeft', fMain.Left);
WriteInteger('Windows', 'StatsTop', fStats.Top);
WriteInteger('Windows', 'StatsLeft', fStats.Left);
WriteBool('Windows', 'StatsVisible', fStats.Visible);
WriteBool('Windows', 'StatsTer', bStOpTer);
WriteBool('Windows', 'StatsArm', bStOpArm);
WriteBool('Windows', 'StatsCon', bStOpCon);
WriteBool('Windows', 'StatsCar', bStOpCar);
WriteBool('Windows', 'StatsCTIV', bStOpCTIV);
WriteInteger('Windows', 'LogTop', fLog.Top);
WriteInteger('Windows', 'LogLeft', fLog.Left);
WriteInteger('Windows', 'LogWidth', fLog.Width);
WriteInteger('Windows', 'LogHeight', fLog.Height);
WriteBool('Windows', 'LogVisible', fLog.Visible);
WriteInteger('Windows', 'HistoryTop', fHistory.Top);
WriteInteger('Windows', 'HistoryLeft', fHistory.Left);
WriteInteger('Windows', 'HistoryWidth', fHistory.Width);
WriteInteger('Windows', 'HistoryHeight', fHistory.Height);
// Preferences
WriteBool('Pref', 'ShowToolbar', bPrefShowToolbar);
WriteString('Pref', 'Toolbar', sPrefTlbButtons);
WriteBool('Pref', 'ExpertAttack', bPrefExpertAttack);
WriteBool('Pref', 'MapHooverOn', bPrefMapHoover);
WriteBool('Pref', 'MapSelectedOn', bPrefMapSelected);
WriteInteger('Pref', 'MapHooverP', iPrefMapHoover);
WriteInteger('Pref', 'MapSelectedP', iPrefMapSelected);
WriteBool('Pref', 'ConfirmAbort', bPrefConfirmAbort);
WriteBool('Pref', 'CheckUpdate', bPrefCheckUpdate);
// Map
WriteString('Map', 'File', sMapFile);
// Rules
WriteInteger('Rules', 'Assignment', ord(RAssignmentType));
for iP := 2 to MAXPLAYERS do begin
WriteInteger('Rules', 'InitArmies' + IntToStr(iP),
RInitialArmies[iP]);
end;
WriteInteger('Rules', 'CardUsage', ord(RCardsValueType));
WriteInteger('Rules', 'ArtSetValue', RSetValue[csArt]);
WriteInteger('Rules', 'InfSetValue', RSetValue[csInf]);
WriteInteger('Rules', 'CavSetValue', RSetValue[csCav]);
WriteInteger('Rules', 'DifSetValue', RSetValue[csDif]);
for i := 1 to 8 do begin
WriteInteger('Rules', 'TradeValue' + IntToStr(i), RTradeValue[i]);
end;
WriteInteger('Rules', 'TradeValueInc', RValueInc);
WriteInteger('Rules', 'MaxHeldCards', RMaxHeldCards);
WriteBool('Rules', 'TradeCapturedCards', RImmediateTrade);
WriteBool('Rules', 'AllowAttackAfterMove', RFinalMove);
// Players
for iP := 1 to MAXPLAYERS do begin
with arPlayer[iP] do begin
WriteString('Player' + IntToStr(iP), 'Name', Name);
WriteBool('Player' + IntToStr(iP), 'Active', Active);
WriteInteger('Player' + IntToStr(iP), 'Color', Color);
WriteBool('Player' + IntToStr(iP), 'Computer', Computer);
WriteBool('Player' + IntToStr(iP), 'Log', KeepLog);
WriteInteger('Player' + IntToStr(iP), 'AutoCards',
ord(CardsHandling));
WriteString('Player' + IntToStr(iP), 'PrgFile', PrgFile);
end;
end;
// Players sets
WriteInteger('PlSets', 'Count', iPlSetCount);
WriteInteger('PlSets', 'Set', iPlSet);
for i := STDPLSETS + 1 to iPlSetCount do begin
WriteString('PlSet' + IntToStr(i), 'Name', arPlayersSet[i].Name);
for iP := 1 to MAXPLAYERS do begin
with arPlayersSet[i].arPlSet[iP] do begin
WriteString('PlSet' + IntToStr(i), 'Name' + IntToStr(iP), Name);
WriteBool('PlSet' + IntToStr(i), 'Active' + IntToStr(iP), Active);
WriteInteger('PlSet' + IntToStr(i), 'Color' + IntToStr(iP),
Color);
WriteBool('PlSet' + IntToStr(i), 'Computer' + IntToStr(iP),
Computer);
WriteBool('PlSet' + IntToStr(i), 'Log' + IntToStr(iP), KeepLog);
WriteInteger('PlSet' + IntToStr(i), 'AutoCards' + IntToStr(iP),
ord(CardsHandling));
WriteString('PlSet' + IntToStr(i), 'PrgFile' + IntToStr(iP),
PrgFile);
end;
end;
end;
end;
finally
IniFile.Free;
end;
end;
BaseMap.Free;
end;
// Menu setup according to the state of the game
procedure MenuSetup;
begin
with fMain do
case GameState of
gsStopped: begin
mnuFilNew.Enabled := True;
mnuFilEnd.Enabled := False;
mnuFilSave.Enabled := False;
mnuFilRestore.Enabled := True;
mnuOptRules.Enabled := True;
mnuOptPref.Enabled := True;
cmdNewGame.Enabled := True;
cmdEndGame.Enabled := False;
cmdSaveGame.Enabled := False;
cmdRestoreGame.Enabled := True;
cmdEndTurn.Enabled := False;
cmdRules.Enabled := True;
cmdPreferences.Enabled := True;
end;
gsAssigning, gsPlaying: begin
mnuFilNew.Enabled := False;
mnuFilEnd.Enabled := True;
mnuFilSave.Enabled := False;
mnuFilRestore.Enabled := False;
mnuOptRules.Enabled := False;
mnuOptPref.Enabled := False;
cmdNewGame.Enabled := False;
cmdEndGame.Enabled := True;
cmdSaveGame.Enabled := False;
cmdRestoreGame.Enabled := False;
cmdEndTurn.Enabled := False;
cmdRules.Enabled := False;
cmdPreferences.Enabled := False;
end;
end;
end;
// Create and prepare an instance of the script executer
procedure ScriptSetup;
begin
// Create an instance of the script executer
ScriptExec := TPSExec.Create;
// Register the external functions to the executer
// RegisterStandardLibrary_R(ScriptExec); // Register the standard library
ScriptExec.RegisterDelphiFunction(@TName, 'TNAME', cdRegister);
ScriptExec.RegisterDelphiFunction(@TOwner, 'TOWNER', cdRegister);
ScriptExec.RegisterDelphiFunction(@TArmies, 'TARMIES', cdRegister);
ScriptExec.RegisterDelphiFunction(@TContinent, 'TCONTINENT', cdRegister);
ScriptExec.RegisterDelphiFunction(@TBordersCount, 'TBORDERSCOUNT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@TBorder, 'TBORDER', cdRegister);
ScriptExec.RegisterDelphiFunction(@TIsBordering, 'TISBORDERING', cdRegister);
ScriptExec.RegisterDelphiFunction(@TIsFront, 'TISFRONT', cdRegister);
ScriptExec.RegisterDelphiFunction(@TIsMine, 'TISMINE', cdRegister);
ScriptExec.RegisterDelphiFunction(@TIsEntry, 'TISENTRY', cdRegister);
ScriptExec.RegisterDelphiFunction(@TFrontsCount, 'TFRONTSCOUNT', cdRegister);
ScriptExec.RegisterDelphiFunction(@TFront, 'TFRONT', cdRegister);
ScriptExec.RegisterDelphiFunction(@TStrongestFront, 'TSTRONGESTFRONT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@TWeakestFront, 'TWEAKESTFRONT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@TPressure, 'TPRESSURE', cdRegister);
ScriptExec.RegisterDelphiFunction(@TDistance, 'TDISTANCE', cdRegister);
ScriptExec.RegisterDelphiFunction(@TShortestPath, 'TSHORTESTPATH',
cdRegister);
ScriptExec.RegisterDelphiFunction(@TWeakestPath, 'TWEAKESTPATH', cdRegister);
ScriptExec.RegisterDelphiFunction(@TPathToFront, 'TPATHTOFRONT', cdRegister);
ScriptExec.RegisterDelphiFunction(@PMe, 'PME', cdRegister);
ScriptExec.RegisterDelphiFunction(@PName, 'PNAME', cdRegister);
ScriptExec.RegisterDelphiFunction(@PProgram, 'PPROGRAM', cdRegister);
ScriptExec.RegisterDelphiFunction(@PActive, 'PACTIVE', cdRegister);
ScriptExec.RegisterDelphiFunction(@PAlive, 'PALIVE', cdRegister);
ScriptExec.RegisterDelphiFunction(@PHuman, 'PHUMAN', cdRegister);
ScriptExec.RegisterDelphiFunction(@PArmiesCount, 'PARMIESCOUNT', cdRegister);
ScriptExec.RegisterDelphiFunction(@PNewArmies, 'PNEWARMIES', cdRegister);
ScriptExec.RegisterDelphiFunction(@PTerritoriesCount, 'PTERRITORIESCOUNT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@PCardCount, 'PCARDCOUNT', cdRegister);
ScriptExec.RegisterDelphiFunction(@PCardTurnInValue, 'PCARDTURNINVALUE',
cdRegister);
ScriptExec.RegisterDelphiFunction(@COwner, 'COWNER', cdRegister);
ScriptExec.RegisterDelphiFunction(@CBonus, 'CBONUS', cdRegister);
ScriptExec.RegisterDelphiFunction(@CTerritoriesCount, 'CTERRITORIESCOUNT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@CTerritory, 'CTERRITORY', cdRegister);
ScriptExec.RegisterDelphiFunction(@CBordersCount, 'CBORDERSCOUNT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@CBorder, 'CBORDER', cdRegister);
ScriptExec.RegisterDelphiFunction(@CEntriesCount, 'CENTRIESCOUNT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@CEntry, 'CENTRY', cdRegister);
ScriptExec.RegisterDelphiFunction(@CAnalysis, 'CANALYSIS', cdRegister);
ScriptExec.RegisterDelphiFunction(@CLeader, 'CLEADER', cdRegister);
ScriptExec.RegisterDelphiFunction(@SConquest, 'SCONQUEST', cdRegister);
ScriptExec.RegisterDelphiFunction(@SPlayersCount, 'SPLAYERSCOUNT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@SAlivePlayersCount, 'SALIVEPLAYERSCOUNT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@SCardsBasedOnCombo, 'SCARDSBASEDONCOMBO',
cdRegister);
ScriptExec.RegisterDelphiFunction(@UMessage, 'UMESSAGE', cdRegister);
ScriptExec.RegisterDelphiFunction(@ULog, 'ULOG', cdRegister);
ScriptExec.RegisterDelphiFunction(@UBufferSet, 'UBUFFERSET', cdRegister);
ScriptExec.RegisterDelphiFunction(@UBufferGet, 'UBUFFERGET', cdRegister);
ScriptExec.RegisterDelphiFunction(@URandom, 'URANDOM', cdRegister);
ScriptExec.RegisterDelphiFunction(@UTakeSnapshot, 'UTAKESNAPSHOT',
cdRegister);
ScriptExec.RegisterDelphiFunction(@UDialogO, 'UDIALOGO', cdRegister);
ScriptExec.RegisterDelphiFunction(@UAbortGame, 'UABORTGAME', cdRegister);
ScriptExec.RegisterDelphiFunction(@ULogOff, 'ULOGOFF', cdRegister);
ScriptExec.RegisterDelphiFunction(@ULogOn, 'ULOGON', cdRegister);
ScriptExec.RegisterDelphiFunction(@UMessageOff, 'UMESSAGEOFF', cdRegister);
ScriptExec.RegisterDelphiFunction(@UMessageOn, 'UMESSAGEON', cdRegister);
ScriptExec.RegisterDelphiFunction(@UDialogOff, 'UDIALOGOFF', cdRegister);
ScriptExec.RegisterDelphiFunction(@UDialogOn, 'UDIALOGON', cdRegister);
ScriptExec.RegisterDelphiFunction(@USnapShotOff, 'USNAPSHOTOFF', cdRegister);
ScriptExec.RegisterDelphiFunction(@USnapShotOn, 'USNAPSHOTON', cdRegister);
end;
// Free the script executer
procedure ScriptCleanup;
begin
if ScriptExec <> nil then begin
ScriptExec.Free;
ScriptExec := nil;
end;
end;
function CompileTRP(Compiler: TPSPascalCompiler;
sName, sSource: string): boolean;
var
sCompErr: string;
iErr: integer;
sPPout: ansistring;
begin
Result := False;
sCompErr := 'Program: ' + sName + #13#10;
// invoke PS compiler
if Compiler.Compile(sSource) then begin
// compilation succeeded
if not bAssignmentFound then
sCompErr := sCompErr + 'ASSIGNMENT procedure not found'
else if not bPlacementFound then
sCompErr := sCompErr + 'PLACEMENT procedure not found'
else if not bAttackFound then
sCompErr := sCompErr + 'ATTACK procedure not found'
else if not bOccupationFound then
sCompErr := sCompErr + 'OCCUPATION procedure not found'
else if not bFortificationFound then
sCompErr := sCompErr + 'FORTIFICATION procedure not found'
else
Result := True;
end
else begin
// compilation aborted: show errors
for iErr := 0 to Compiler.MsgCount - 1 do begin
sCompErr := sCompErr + Compiler.Msg[iErr].MessageToString + #13#10;
end;
end;
// show error messages
if not Result then begin
MessageDlg(sCompErr, mtError, [mbOk], 0);
end;
end;
// New game setup
procedure NewGameSetup;
var
iCount, iT, iP, iB, // generic
iTL, iTL1, iTL2, iTL3: integer; // turn list generation
tCont: TContId;
bSwap: boolean;
PrgTemp: TstringList;
Compiler: TPSPascalCompiler; // TPSPascalCompiler is the compiler part of the scriptengine. This will
// translate a Pascal script into a compiled for the executer understands.
Preproc: TPSPreProcessor;
bCompErrors: boolean; // true if at least one compilation failed
sPPout: ansistring;
begin
ScriptSetup; // create the script executer; will be destroyed in GameCleanup
PrgTemp := TstringList.Create;
// create input buffer for source code
Compiler := TPSPascalCompiler.Create;
Preproc := TPSPreProcessor.Create;
// create an instance of the compiler.
Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
Compiler.OnExportCheck := ScriptOnExportCheck;
bCompErrors := False;
// reset global flag for compilation errors
bStopASAP := False; // reset flag for required stopping
try
// Reset log
fLog.txtLog.Lines.Clear;
// Reset rank
iRank := 0;
// Reset territories
HooverTerrit := 0;
FromTerrit := 0;
ToTerrit := 0;
for iT := 1 to MAXTERRITORIES do begin
with arTerritory[iT] do begin
Army := 0;
Owner := 0;
end;
DisplayTerritory(iT);
end;
// Reset continents
for tCont := coNA to coAU do begin
arContinent[tCont].Owner := 0;
end;
// Count initial players
iCount := 0;
for iP := 1 to MAXPLAYERS do
if arPlayer[iP].Active then
inc(iCount);
// Reset players
for iP := 1 to MAXPLAYERS do begin
with arPlayer[iP] do begin
// Reset buffers
for iB := 1 to MAXBUFFER do
Buffer[iB] := 0.0;
// Reset runtime flags
USnapShotEnabled := False;
ULogEnabled := False;
UMessageEnabled := False;
UDialogEnabled := False;
// Set active players
if Active then begin
NewArmy := RInitialArmies[iCount];
Territ := 0;