-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.pas
493 lines (456 loc) · 13.2 KB
/
Main.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
unit Main;
{$MODE Delphi}
interface
uses
LCLIntf, LCLType, LMessages, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, ExtCtrls, StdCtrls, ComCtrls, {JvComponentBase, JvPropertyStore,
JvProgramVersionCheck, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdFTP,} ImgList, ToolWin, LHelpControl, LazFileUtils, LResources;
type
TfMain = class(TForm)
panMap: TPanel;
imgMap: TImage;
mnuMain: TMainMenu;
mnuFil: TMenuItem;
mnuFilSep1: TMenuItem;
mnuFilExit: TMenuItem;
mnuFilNew: TMenuItem;
mnuFilEnd: TMenuItem;
mnuOpt: TMenuItem;
mnuOptRules: TMenuItem;
mnuVie: TMenuItem;
mnuVieStats: TMenuItem;
panStatus: TStatusBar;
mnuVieLog: TMenuItem;
mnuHel: TMenuItem;
mnuHelReadme: TMenuItem;
mnuHelAbout: TMenuItem;
mnuOptMap: TMenuItem;
mnuVieHist: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
mnuHelCheckUpdates: TMenuItem;
{IdAntiFreeze: TIdAntiFreeze; }
mnuHelHomepage: TMenuItem;
mnuFilSave: TMenuItem;
mnuFilRestore: TMenuItem;
N4: TMenuItem;
dlgOpen: TOpenDialog;
dlgSave: TSaveDialog;
tlbToolBar: TToolBar;
cmdNewGame: TToolButton;
imlButtons: TImageList;
cmdEndGame: TToolButton;
cmdSaveGame: TToolButton;
cmdRestoreGame: TToolButton;
cmdStatistics: TToolButton;
cmdEndTurn: TToolButton;
imlDisButtons: TImageList;
Panel1: TPanel;
Label1: TLabel;
panTurn: TPanel;
mnuOptPref: TMenuItem;
cmdExit: TToolButton;
cmdLog: TToolButton;
cmdHistory: TToolButton;
cmdRules: TToolButton;
cmdMap: TToolButton;
cmdPreferences: TToolButton;
// cmdHelp: TToolButton;
cmdHomePage: TToolButton;
cmdUpdate: TToolButton;
cmdAbout: TToolButton;
mnuHelGroup: TMenuItem;
N5: TMenuItem;
procedure mnuFilExitClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure imgMapMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure mnuFilNewClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure mnuFilEndClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure mnuVieStatsClick(Sender: TObject);
procedure mnuOptRulesClick(Sender: TObject);
procedure mnuOptPlayersClick(Sender: TObject);
procedure mnuVieLogClick(Sender: TObject);
procedure mnuHelReadmeClick(Sender: TObject);
procedure mnuHelAboutClick(Sender: TObject);
procedure mnuOptMapClick(Sender: TObject);
procedure mnuVieHistClick(Sender: TObject);
{ procedure mnuHelCheckUpdatesClick(Sender: TObject); }
procedure mnuHelHomepageClick(Sender: TObject);
procedure mnuFilSaveClick(Sender: TObject);
procedure mnuFilRestoreClick(Sender: TObject);
procedure cmdEndTurnClick(Sender: TObject);
procedure imgMapMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
procedure mnuOptPrefClick(Sender: TObject);
procedure mnuHelGroupClick(Sender: TObject);
function GetLHelpFilename: string;
private
public
{ Public declarations }
end;
var
fMain: TfMain;
Help: TLHelpConnection;
implementation
{$R *.lfm}
uses Globals, Territ, NewGame, Stats, Human, Rules, Players, Log,
Readme,
About, ExpSubr, Map, History, Pref, SplashScreen;
procedure TfMain.FormShow(Sender: TObject);
var
LHelp: String;
begin
bG_TRSim := false;
Setup;
Caption := 'TurboRisk ' + sG_AppVers;
LHelp := GetLHelpFilename;
if not FileExistsUTF8(LHelp) then
MessageDlg('Missing lhelp','Can not find the lhelp application "'+LHelp+'"',
mtError,[mbOk],0);
Help := TLHelpConnection.Create;
//Help.ProcessWhileWaiting := @Application.ProcessMessages;
// remove splash screen
fSplashScreen.Hide;
fSplashScreen.Free;
end;
procedure TfMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := false;
if GameState = gsStopped then begin
CanClose := true;
exit;
end;
if bHumanTurn then begin
GameCleanup;
CanClose := true;
exit;
end
else begin
bStopASAP := true;
bCloseASAP := true;
end;
end;
procedure TfMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Cleanup;
end;
procedure TfMain.mnuFilNewClick(Sender: TObject);
begin
if GameState <> gsStopped then
exit;
if fNewGame.ShowModal = mrOK then begin
Screen.Cursor := crHourGlass;
panStatus.Panels[2].Text := 'Preparing new game, please wait...';
Application.ProcessMessages;
NewGameSetup;
panStatus.Panels[2].Text := '';
Screen.Cursor := crDefault;
if GameState <> gsStopped then
Supervisor
else
GameCleanup;
end;
end;
procedure TfMain.mnuFilEndClick(Sender: TObject);
begin
if GameState = gsStopped then
exit;
if bPrefConfirmAbort and (MessageDlg(
'Are you sure you want to quit this game?', mtConfirmation,
[mbYes, mbNo], 0) <> mrYes) then
exit;
if bHumanTurn then begin
GameCleanup;
exit;
end
else begin
bStopASAP := true;
end;
end;
procedure TfMain.mnuFilSaveClick(Sender: TObject);
begin
if not bHumanTurn then
exit;
dlgSave.InitialDir := sG_AppPath;
dlgSave.FileName := 'my_game.trg';
if dlgSave.Execute then begin
SaveGame(dlgSave.FileName, '');
panStatus.Panels[2].Text := 'Game saved.';
end;
end;
procedure TfMain.mnuFilRestoreClick(Sender: TObject);
begin
dlgOpen.InitialDir := sG_AppPath;
if dlgOpen.Execute then begin
Screen.Cursor := crHourGlass;
panStatus.Panels[2].Text := 'Restoring game, please wait...';
Application.ProcessMessages;
GameCleanup;
RestoreGame(dlgOpen.FileName);
Screen.Cursor := crDefault;
panStatus.Panels[2].Text := '';
if GameState = gsStopped then begin
GameCleanup;
exit;
end;
MostraIstruzioni;
UpdateGridPos;
// save and restore enabled (though meaningless)
mnuFilSave.Enabled := true;
cmdSaveGame.Enabled := true;
mnuFilRestore.Enabled := true;
cmdRestoreGame.Enabled := true;
end;
end;
procedure TfMain.mnuFilExitClick(Sender: TObject);
begin
Close;
end;
procedure TfMain.mnuVieStatsClick(Sender: TObject);
begin
if fStats.Visible then begin
fStats.Close;
end
else begin
fStats.Show;
end;
end;
procedure TfMain.mnuVieHistClick(Sender: TObject);
begin
fHistory.ShowModal;
end;
procedure TfMain.mnuVieLogClick(Sender: TObject);
begin
if fLog.Visible then begin
fLog.Close;
end
else begin
fLog.Show;
end;
end;
procedure TfMain.mnuOptPlayersClick(Sender: TObject);
begin
fPlayers.ShowModal;
end;
procedure TfMain.mnuOptPrefClick(Sender: TObject);
begin
fPref.ShowModal;
end;
procedure TfMain.mnuOptRulesClick(Sender: TObject);
begin
fRules.ShowModal;
end;
procedure TfMain.mnuOptMapClick(Sender: TObject);
begin
fMap.ShowModal;
end;
procedure TfMain.mnuHelReadmeClick(Sender: TObject);
var
Res: TLHelpResponse;
begin
try
if Help.ServerRunning = false then
Help.StartHelpServer('letters', GetLHelpFilename);
Res := Help.OpenFile(sG_AppPath + 'TurboRisk.chm');
finally
Screen.Cursor := crDefault;
end;
//Label1.Caption := ResponseToString(Res);
end;
function TfMain.GetLHelpFilename: string;
begin
Result := './lhelp/lhelp';
{$IFDEF Windows}
Result := '.\lhelp\lhelp.exe';
{$ENDIF}
{$IFDEF darwin} //OS X
Result:=Result+'.app/Contents/MacOS/'+ExtractFilename(Result);
{$ENDIF}
end;
{
procedure TfMain.mnuHelReadmeClick(Sender: TObject);
begin
// Application.HelpSystem.ShowTableOfContents; // doesn'twork!
Application.HelpSystem.ShowContextHelp(100, sG_AppPath + 'TurboRisk.chm');
end;
}
procedure TfMain.mnuHelAboutClick(Sender: TObject);
begin
fAbout.ShowModal;
end;
procedure TfMain.mnuHelGroupClick(Sender: TObject);
begin
OpenURL(pChar(
'http://groups.google.com/group/turborisk')); { *Converted from ShellExecute* }
end;
procedure TfMain.mnuHelHomepageClick(Sender: TObject);
begin
OpenURL(pChar(
'http://www.marioferrari.org/freeware/turborisk/turborisk.html')); { *Converted from ShellExecute* }
end;
procedure TfMain.imgMapMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
iT: Integer;
begin
if GameState = gsStopped then
exit;
// search territory hoovered by mouse
iT := TrovaTerritorio(X, Y);
// if changed...
if iT <> GetHooverTerritory then begin
// set new hoovered territory
SetHooverTerritory(iT);
end;
end;
procedure TfMain.imgMapMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
iT: Integer;
iColl: Integer;
begin
if not bHumanTurn then
exit;
iT := TrovaTerritorio(X, Y);
cmdEndTurn.Enabled := false; // disable "end turn" button
// Puntamento su territorio
if iT > 0 then begin
case GameState of
gsAssigning: begin
if arTerritory[iT].Owner = 0 then begin
AssegnaTerritorio(iT, iTurn);
inc(arTerritory[iT].Army);
dec(arPlayer[iTurn].NewArmy);
DisplayTerritory(iT);
if arPlayer[iTurn].KeepLog then
ScriviLog(arTerritory[iT].Name + ' assigned.');
// fine turno
Supervisor;
exit;
end;
end;
gsDistributing: begin
if (arTerritory[iT].Owner = iTurn) and (arPlayer[iTurn].NewArmy > 0)
then begin
CollocaArmata(iT, iTurn, 1);
// fine turno
Supervisor;
exit;
end;
end;
gsPlaying: begin
case HumanPhase of
hpPlacement: begin
if (arTerritory[iT].Owner = iTurn) and
(arPlayer[iTurn].NewArmy > 0) then begin
iColl := 1;
if ssShift in Shift then
inc(iColl);
if ssAlt in Shift then
inc(iColl);
if ssCtrl in Shift then
inc(iColl);
case iColl of
4:
iColl := 25;
3:
iColl := 10;
2:
iColl := 5;
end;
CollocaArmata(iT, iTurn, iColl);
mnuFilSave.Enabled := false; // game save allowed only at the beginning of a turn
cmdSaveGame.Enabled := false;
if arPlayer[iTurn].NewArmy = 0 then begin
HumanPhase := hpAttack;
cmdEndTurn.Enabled := true; // enable "end turn" button
end;
MostraIstruzioni;
end;
end;
hpAttack: begin
cmdEndTurn.Enabled := true; // enable "end turn" button
if GetFromTerritory = 0 then begin
if (arTerritory[iT].Owner = iTurn) and
(arTerritory[iT].Army >= 2) then begin
SetFromTerritory(iT);
MostraIstruzioni;
end;
end
else begin
if Confinante(GetFromTerritory, iT) then begin
if arTerritory[iT].Owner <> iTurn then begin
SetToTerritory(iT);
fMain.panStatus.Panels[2].Text := 'Attack in progress...';
UomoAttacca(GetFromTerritory, iT);
if iNPlayers < 2 then begin
// last opponent eliminated, the game ends
Supervisor;
exit;
end;
end
else if (arTerritory[iT].Owner = iTurn)
and not arPlayer[iTurn].FlMove then begin
SetToTerritory(iT);
fMain.panStatus.Panels[2].Text :=
'Troops move in progress...';
UomoSposta(GetFromTerritory, iT, true);
if arPlayer[iTurn].FlMove and RFinalMove then begin
// turn ends
SetFromTerritory(0);
SetToTerritory(0);
Supervisor;
end;
exit;
end;
end;
SetFromTerritory(0);
SetToTerritory(0);
MostraIstruzioni;
end;
end;
end;
end;
end;
end
else begin
SetFromTerritory(0);
SetToTerritory(0);
MostraIstruzioni;
if bHumanTurn and (GameState = gsPlaying) and (HumanPhase > hpPlacement)
then begin
cmdEndTurn.Enabled := true; // enable "end turn" button
end;
end;
end;
procedure TfMain.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then
Application.Minimize
else if Key = VK_SPACE then begin
if bHumanTurn and (GameState = gsPlaying) and (HumanPhase > hpPlacement)
then begin
SetFromTerritory(0);
SetToTerritory(0);
Supervisor;
exit;
end;
end;
end;
procedure TfMain.cmdEndTurnClick(Sender: TObject);
begin
if bHumanTurn and (GameState = gsPlaying) and (HumanPhase > hpPlacement) then
begin
SetFromTerritory(0);
SetToTerritory(0);
Supervisor;
end;
end;
end.