forked from brian-scott-andrews/TurboRisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputer.pas
539 lines (489 loc) · 16.3 KB
/
Computer.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
unit Computer;
{$MODE Delphi}
interface
// Esecuzione mossa computer
procedure EseguiTurnoComputer;
function strTran(ctext, cfor, cwith: string): string;
implementation
uses LCLIntf, LCLType, LMessages, Forms, Controls, SysUtils, Dialogs,
uPSRuntime, uPSUtils, {StdPas,}
Main, Globals, Territ, Stats, Log, TRPError, Sim;
var
// Varibles used to pass parameters to the script executer
tParamToTerritory, tParamFromTerritory, tParamArmies: PPSVariant;
tParamList: TPSList; // The parameter list
iStartTime, iEndTime: QWord; // CPU timing for TRSim
{ Character by Character String Replacement }
function StrTran(ctext, cfor, cwith: string): string;
var
ntemp : word ;
nreplen: word ;
begin
cfor := upperCase(cfor) ;
nreplen := length(cfor) ;
for ntemp := 1 to length(ctext) do begin
if (upperCase(copy(ctext, ntemp, nreplen)) = cfor) then
begin
delete(ctext, ntemp, nreplen);
insert(cwith, ctext, ntemp);
end;
end;
result := ctext;
end;
procedure ShowError(sMsg: string);
begin
if not bG_TRSim then begin // TurboRisk
fTRPError.txtMsg.Text := sMsg;
case fTRPError.ShowModal of
mrYes: begin
// dump memory if required
sMsg := StrTran(sMsg, #13#10, '\n');
if not SysUtils.DirectoryExists(sG_AppPath + PathDelim + 'Dump') then begin
if not CreateDir(sG_AppPath + PathDelim + 'Dump') then
raise Exception.Create('Cannot create ' + sG_AppPath + PathDelim + 'Dump');
end;
SaveGame(sG_AppPath + 'Dump' + PathDelim + 'trdump_' + FormatDateTime
('yyyymmdd_hhmmss', Now) + '.trd', sMsg);
end;
mrAbort: begin
bStopASAP := true;
bCloseASAP := true;
end;
end;
end
else begin // TRSim
if fSim.chkErrorDump.Checked then begin
// dump memory if required
sMsg := StrTran(sMsg, #13#10, '\n');
if not SysUtils.DirectoryExists(sG_AppPath + PathDelim +'Dump') then begin
if not CreateDir(sG_AppPath + PathDelim + 'Dump') then
raise Exception.Create('Cannot create ' + sG_AppPath + PathDelim + 'Dump');
end;
SaveGame(sG_AppPath + 'Dump'+PathDelim+'trdump_' + FormatDateTime('yyyymmdd_hhmmss',
Now) + '.trd', sMsg);
end;
if fSim.chkErrorAbort.Checked then begin
uSimStatus := ssError;
bStopASAP := true;
end;
end;
end;
procedure CPU_Phase(uRoutine: TRoutine);
begin
inc(arPlayer[iTurn].aCPU[uRoutine].iPhases);
end;
procedure CPU_Call(uRoutine: TRoutine; iTicks: QWord);
begin
arPlayer[iTurn].aCPU[uRoutine].iTime := arPlayer[iTurn].aCPU[uRoutine]
.iTime + iTicks;
inc(arPlayer[iTurn].aCPU[uRoutine].iCalls);
end;
// ************************************************************
// * ROUTINES DI ASSEGNAZIONE INIZIALE TERRITORI - ASSIGNMENT *
// ************************************************************
// Scelta territorio per assegnazione iniziale
procedure CmpAssegnazione;
var
iTo: integer;
sMsg: string;
begin
// prepare parameters to pass to the script executer
VSetInt(tParamToTerritory, 0);
tParamList.Clear;
tParamList.Add(tParamToTerritory);
// run the script
try
CPU_Phase(rtAssignment);
iStartTime := SysUtils.GetTickCount64();
// QueryPerformanceCounter(iStartTime); // get initial time
ScriptExec.RunProc(tParamList, ScriptExec.GetProc('ASSIGNMENT'));
iEndTime := gettickcount64;
// QueryPerformanceCounter(iEndTime); // get final time
CPU_Call(rtAssignment, iEndTime - iStartTime);
// get back value of var parameters
iTo := VGetInt(tParamToTerritory);
except
on e: Exception do begin
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 +
'Routine: Assignment' + #13#10 + 'Error: ' +
e.message;
ShowError(sMsg);
exit;
end;
end;
// Preparazione messaggio di errore
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 + 'Routine: Assignment' +
#13#10 + 'To territory: ' + IntToStr(iTo) + #13#10 + 'Error: ';
// Verifica validità richiesta
if (iTo < 1) or (iTo > MAXTERRITORIES) or (arTerritory[iTo].Owner <> 0) then
begin
sMsg := sMsg + 'Invalid "To" territory';
ShowError(sMsg);
iTo := 0;
end;
if iTo > 0 then begin
// Assegnazione
AssegnaTerritorio(iTo, iTurn);
inc(arTerritory[iTo].Army);
dec(arPlayer[iTurn].NewArmy);
// Log
if arPlayer[iTurn].KeepLog then
ScriviLog(arTerritory[iTo].Name + ' assigned.');
// Aggiornamento display e statistiche
DisplayTerritory(iTo);
UpdateStats;
Application.ProcessMessages;
end;
end;
// ***********************************************
// * ROUTINES DI COLLOCAZIONE ARMATE - PLACEMENT *
// ***********************************************
// Scelta collocazione nuova armata sui propri territori
procedure CmpCollocaArmate(iDaCollocare: integer);
var
iTo: integer;
sMsg: string;
begin
CPU_Phase(rtPlacement);
// repeat placement procedure
while iDaCollocare > 0 do begin
iTo := 0;
try
// prepare parameters to pass to the script executer
VSetInt(tParamToTerritory, iTo);
tParamList.Clear;
tParamList.Add(tParamToTerritory);
// run the script
iStartTime := SysUtils.GetTickCount64();
// QueryPerformanceCounter(iStartTime); // get initial time
ScriptExec.RunProc(tParamList, ScriptExec.GetProc('PLACEMENT'));
iEndTime := SysUtils.GetTickCount64();
// QueryPerformanceCounter(iEndTime); // get final time
CPU_Call(rtPlacement, iEndTime - iStartTime);
// get back value of var parameters
iTo := VGetInt(tParamToTerritory);
except
on e: Exception do begin
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 +
'Routine: Placement' + #13#10 + 'Error: ' + e.message;
ShowError(sMsg);
exit;
end;
end;
// Preparazione messaggio di errore
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 + 'Routine: Placement' +
#13#10 + 'To territory: ' + IntToStr(iTo) + #13#10 + 'Error: ';
// Verifica validità richiesta
if (iTo < 1) or (iTo > MAXTERRITORIES) or (arTerritory[iTo].Owner <> iTurn)
then begin
sMsg := sMsg + 'Invalid "To" territory';
ShowError(sMsg);
iTo := 0;
end;
if iTo > 0 then begin
// Log
if arPlayer[iTurn].KeepLog then
ScriviLog('Army placement in ' + arTerritory[iTo].Name);
// Collocazione
CollocaArmata(iTo, iTurn, 1);
end
else begin
arPlayer[iTurn].NewArmy := 0; // force 0 new army to place
break; // interruzione ciclo per errori
end;
dec(iDaCollocare);
end;
end;
// ***************************************************************
// * ROUTINES DI OCCUPAZIONE TERRITORIO CONQUISTATO - OCCUPATION *
// ***************************************************************
procedure CmpOccupa(iFrom, iTo: integer);
var
iArmies: integer;
sMsg: string;
begin
// prepare parameters to pass to the script executer
VSetInt(tParamFromTerritory, iFrom);
VSetInt(tParamToTerritory, iTo);
VSetInt(tParamArmies, 0);
tParamList.Clear;
tParamList.Add(tParamArmies);
tParamList.Add(tParamToTerritory);
tParamList.Add(tParamFromTerritory);
try
// run script
CPU_Phase(rtOccupation);
iStartTime := SysUtils.GetTickCount64();
// QueryPerformanceCounter(iStartTime); // get initial time
ScriptExec.RunProc(tParamList, ScriptExec.GetProc('OCCUPATION'));
iEndTime := SysUtils.GetTickCount64();
// QueryPerformanceCounter(iEndTime); // get final time
CPU_Call(rtOccupation, iEndTime - iStartTime);
// get back value of var parameters
iArmies := VGetInt(tParamArmies);
except
on e: Exception do begin
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 +
'Routine: Occupation' + #13#10 + 'Error: ' +
e.message;
ShowError(sMsg);
exit;
end;
end;
// Se richiesto spostamento...
if iArmies > 0 then begin
// Preparazione messaggio di errore
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 +
'Routine: Occupation' + #13#10 + 'From territory: ' + arTerritory[iFrom]
.Name + #13#10 + 'To territory: ' + arTerritory[iTo].Name + #13#10 +
'Armies: ' + IntToStr(iArmies) + #13#10 + 'Error: ';
// Verifica validità richiesta
if iArmies > arTerritory[iFrom].Army - 1 then begin
sMsg := sMsg + 'Invalid number of armies';
ShowError(sMsg);
exit;
end;
// Log
if arPlayer[iTurn].KeepLog then
ScriviLog('Occupation: troops move (' + IntToStr(iArmies)
+ ') from ' + arTerritory[iFrom].Name + ' to ' + arTerritory[iTo]
.Name);
// Spostamento
inc(arTerritory[iTo].Army, iArmies);
dec(arTerritory[iFrom].Army, iArmies);
// Aggiornamento video
DisplayTerritory(iFrom);
DisplayTerritory(iTo);
UpdateStats;
Application.ProcessMessages;
end;
end;
// ********************************
// * ROUTINES DI ATTACCO - ATTACK *
// ********************************
procedure CmpAttacco;
var
iFrom, iTo: integer;
sMsg: string;
bEsito: boolean;
begin
CPU_Phase(rtAttack);
// repeat attack procedure
repeat
iFrom := 0;
iTo := 0;
try
// prepare parameters to pass to the script executer
VSetInt(tParamFromTerritory, iFrom);
VSetInt(tParamToTerritory, iTo);
tParamList.Clear;
tParamList.Add(tParamToTerritory);
tParamList.Add(tParamFromTerritory);
// run the script
iStartTime := SysUtils.GetTickCount64();
// QueryPerformanceCounter(iStartTime); // get initial time
ScriptExec.RunProc(tParamList, ScriptExec.GetProc('ATTACK'));
iEndTime := SysUtils.GetTickCount64();
// QueryPerformanceCounter(iEndTime); // get final time
CPU_Call(rtAttack, iEndTime - iStartTime);
// get back value of var parameters
iFrom := VGetInt(tParamFromTerritory);
iTo := VGetInt(tParamToTerritory);
except
on e: Exception do begin
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 +
'Routine: Attack' + #13#10 + 'Error: ' + e.message;
ShowError(sMsg);
MessageDlg(sMsg, mtError, [mbOk], 0);
exit;
end;
end;
// Se richiesto attacco...
if iFrom > 0 then begin
// Preparazione messaggio di errore
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 + 'Routine: Attack' +
#13#10 + 'From territory: ' + IntToStr(iFrom)
+ #13#10 + 'To territory: ' + IntToStr(iTo)
+ #13#10 + 'Error: ';
// Verifica validità richiesta
if (iFrom > MAXTERRITORIES) or (arTerritory[iFrom].Owner <> iTurn) then
begin
sMsg := sMsg + 'Invalid "From" territory';
ShowError(sMsg);
exit;
end;
if (iTo < 1) or (iTo > MAXTERRITORIES) or
(arTerritory[iTo].Owner = iTurn) then begin
sMsg := sMsg + 'Invalid "To" territory';
ShowError(sMsg);
exit;
end;
if not Confinante(iFrom, iTo) then begin
sMsg := sMsg + 'Invalid "From->To"';
ShowError(sMsg);
exit;
end;
if arTerritory[iFrom].Army < 2 then begin
sMsg := sMsg + 'From territory has not enough armies';
ShowError(sMsg);
exit;
end;
// Attacco
bEsito := PerformAttack(iFrom, iTo);
// Aggiornamento display
UpdateStats;
Application.ProcessMessages;
// Conseguenze dellattacco riuscito
if bEsito then begin
// uscita immediata se vittoria
if iNPlayers < 2 then
exit;
// territorio conquistato, spostamento conseguente
if arTerritory[iFrom].Army > 1 then begin
CmpOccupa(iFrom, iTo);
end;
// collocazione eventuali armate conquistate
if bEliminatedPlayer then begin
if RImmediateTrade then
AssignNewArmies(true);
if arPlayer[iTurn].NewArmy > 0 then begin
CmpCollocaArmate(arPlayer[iTurn].NewArmy);
end;
end;
end;
end;
until iFrom = 0;
end;
// *********************************************
// * ROUTINES DI TRASFERIMENTO - FORTIFICATION *
// *********************************************
procedure CmpTrasferimento;
var
iFrom, iTo, iArmies: integer;
sMsg: string;
begin
// protezione contro trasferimenti multipli
if arPlayer[iTurn].FlMove then
exit;
// prepare parameters to pass to the script executer
VSetInt(tParamFromTerritory, 0);
VSetInt(tParamToTerritory, 0);
VSetInt(tParamArmies, 0);
tParamList.Clear;
tParamList.Add(tParamArmies);
tParamList.Add(tParamToTerritory);
tParamList.Add(tParamFromTerritory);
try
// run the script
CPU_Phase(rtFortification);
iStartTime := SysUtils.GetTickCount64();
// QueryPerformanceCounter(iStartTime); // get initial time
ScriptExec.RunProc(tParamList, ScriptExec.GetProc('FORTIFICATION'));
iEndTime := SysUtils.GetTickCount64();
// QueryPerformanceCounter(iEndTime); // get final time
CPU_Call(rtFortification, iEndTime - iStartTime);
// get back value of var parameters
iFrom := VGetInt(tParamFromTerritory);
iTo := VGetInt(tParamToTerritory);
iArmies := VGetInt(tParamArmies);
except
on e: Exception do begin
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 +
'Routine: Fortification' + #13#10 + 'Error: ' + e.message;
ShowError(sMsg);
exit;
end;
end;
// Se richiesto spostamento...
if iFrom > 0 then begin
// Preparazione messaggio di errore
sMsg := 'Player: ' + arPlayer[iTurn].Name + #13#10 +
'Routine: Fortification' + #13#10 + 'From territory: ' + IntToStr(iFrom)
+ #13#10 + 'To territory: ' + IntToStr(iTo)
+ #13#10 + 'Armies: ' + IntToStr(iArmies) + #13#10 + 'Error: ';
// Verifica validità richiesta
if (iFrom > MAXTERRITORIES) or (arTerritory[iFrom].Owner <> iTurn) then
begin
sMsg := sMsg + 'Invalid "From" territory';
ShowError(sMsg);
exit;
end;
if (iTo < 1) or (iTo > MAXTERRITORIES) or (arTerritory[iTo].Owner <> iTurn)
then begin
sMsg := sMsg + 'Invalid "To" territory';
ShowError(sMsg);
exit;
end;
if not Confinante(iFrom, iTo) then begin
sMsg := sMsg + 'Invalid "From->To"';
ShowError(sMsg);
exit;
end;
if (iArmies <= 0) or (iArmies > arTerritory[iFrom].Army - 1) then begin
sMsg := sMsg + 'Invalid number of armies';
ShowError(sMsg);
exit;
end;
// Log
if arPlayer[iTurn].KeepLog then
ScriviLog('Fortification: troops move (' + IntToStr(iArmies)
+ ') from ' + arTerritory[iFrom].Name + ' to ' + arTerritory[iTo]
.Name);
// Spostamento
inc(arTerritory[iTo].Army, iArmies);
dec(arTerritory[iFrom].Army, iArmies);
arPlayer[iTurn].FlMove := true;
// Aggiornamento video
DisplayTerritory(iFrom);
DisplayTerritory(iTo);
UpdateStats;
Application.ProcessMessages;
end;
end;
// ******************************
// * SUPERVISORE GIOCO COMPUTER *
// ******************************
// Esecuzione mossa computer
procedure EseguiTurnoComputer;
begin
// update LastTurn for TRSim statistics
arPlayer[iTurn].LastTurn := iTurnCounter;
// load the script
if not ScriptExec.LoadData(arPlayer[iTurn].Code) then begin
MessageDlg('Player: ' + arPlayer[iTurn].Name + #13#10 +
'Error: script loading failed', mtError, [mbOk], 0);
exit;
end;
// Create variables to pass parameters to the script executer
tParamList := TIfList.Create; // Create the parameter list
tParamToTerritory := CreateHeapVariant(ScriptExec.FindType2(btS32));
tParamFromTerritory := CreateHeapVariant(ScriptExec.FindType2(btS32));
tParamArmies := CreateHeapVariant(ScriptExec.FindType2(btS32));
if (tParamToTerritory = nil) or (tParamFromTerritory = nil) or
(tParamArmies = nil) then begin
MessageDlg('Could not create script parameters.', mtError, [mbOk], 0);
exit;
end;
// Play a move according to state of the game
case GameState of
gsAssigning:
CmpAssegnazione;
gsDistributing:
if arPlayer[iTurn].NewArmy > 0 then
CmpCollocaArmate(1);
gsPlaying: begin
if arPlayer[iTurn].NewArmy > 0 then
CmpCollocaArmate(arPlayer[iTurn].NewArmy);
CmpAttacco;
if iNPlayers > 1 then
CmpTrasferimento;
end;
end;
// Free parameter passing variables
tParamList.Clear;
tParamList.Add(tParamArmies);
tParamList.Add(tParamToTerritory);
tParamList.Add(tParamFromTerritory);
FreePIFVariantList(tParamList);
end;
end.