1 | unit UCore;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, XMLConf, FileUtil, ActnList, Controls, Dialogs, Forms,
|
---|
9 | UGame, UApplicationInfo, UPersistentForm, UScaleDPI, UCoolTranslator, URegistry,
|
---|
10 | Registry;
|
---|
11 |
|
---|
12 | type
|
---|
13 |
|
---|
14 | { TCore }
|
---|
15 |
|
---|
16 | TCore = class(TDataModule)
|
---|
17 | AAbout: TAction;
|
---|
18 | AHelp: TAction;
|
---|
19 | AGameSave: TAction;
|
---|
20 | AGameLoad: TAction;
|
---|
21 | ApplicationInfo: TApplicationInfo;
|
---|
22 | ASettings: TAction;
|
---|
23 | ActionList1: TActionList;
|
---|
24 | AExit: TAction;
|
---|
25 | AGameEnd: TAction;
|
---|
26 | AGameEndTurn: TAction;
|
---|
27 | AGameNew: TAction;
|
---|
28 | AGameRestart: TAction;
|
---|
29 | CoolTranslator1: TCoolTranslator;
|
---|
30 | ImageListLarge: TImageList;
|
---|
31 | ImageListSmall: TImageList;
|
---|
32 | OpenDialog1: TOpenDialog;
|
---|
33 | PersistentForm: TPersistentForm;
|
---|
34 | SaveDialog1: TSaveDialog;
|
---|
35 | ScaleDPI1: TScaleDPI;
|
---|
36 | XMLConfig1: TXMLConfig;
|
---|
37 | procedure AAboutExecute(Sender: TObject);
|
---|
38 | procedure AExitExecute(Sender: TObject);
|
---|
39 | procedure AGameEndExecute(Sender: TObject);
|
---|
40 | procedure AGameEndTurnExecute(Sender: TObject);
|
---|
41 | procedure AGameLoadExecute(Sender: TObject);
|
---|
42 | procedure AGameNewExecute(Sender: TObject);
|
---|
43 | procedure AGameRestartExecute(Sender: TObject);
|
---|
44 | procedure AGameSaveExecute(Sender: TObject);
|
---|
45 | procedure AHelpExecute(Sender: TObject);
|
---|
46 | procedure ASettingsExecute(Sender: TObject);
|
---|
47 | procedure CoolTranslator1Translate(Sender: TObject);
|
---|
48 | procedure DataModuleCreate(Sender: TObject);
|
---|
49 | procedure DataModuleDestroy(Sender: TObject);
|
---|
50 | private
|
---|
51 | FInitialized: Boolean;
|
---|
52 | StoredDimension: TControlDimension;
|
---|
53 | RegistryContext: TRegistryContext;
|
---|
54 | procedure DoOnMove(CellFrom, CellTo: TCell; var CountOnce,
|
---|
55 | CountRepeat: Integer; Update: Boolean; var Confirm: Boolean);
|
---|
56 | procedure DoOnWin(Player: TPlayer);
|
---|
57 | procedure Delay(Time: Integer);
|
---|
58 | procedure GameNewTurnExecute(Sender: TObject);
|
---|
59 | procedure AutoSave;
|
---|
60 | procedure LoadConfig;
|
---|
61 | procedure SaveConfig;
|
---|
62 | procedure CommandLineParams;
|
---|
63 | procedure ScaleDPI;
|
---|
64 | public
|
---|
65 | Game: TGame;
|
---|
66 | Player: TPlayer;
|
---|
67 | UseSingleView: Boolean;
|
---|
68 | DevelMode: Boolean;
|
---|
69 | LastMapFileName: string;
|
---|
70 | AnimationSpeed: Integer;
|
---|
71 | AutoSaveEnabled: Boolean;
|
---|
72 | procedure UpdateActions;
|
---|
73 | procedure Init;
|
---|
74 | property Initialized: Boolean read FInitialized;
|
---|
75 | end;
|
---|
76 |
|
---|
77 | var
|
---|
78 | Core: TCore;
|
---|
79 |
|
---|
80 |
|
---|
81 | implementation
|
---|
82 |
|
---|
83 | {$R *.lfm}
|
---|
84 |
|
---|
85 | uses
|
---|
86 | UFormMove, UFormMain, UFormNew, UFormSettings, UFormAbout, UFormPlayer,
|
---|
87 | UFormHelp;
|
---|
88 |
|
---|
89 | const
|
---|
90 | DefaultRegKey = '\Software\Chronosoft\xTactics';
|
---|
91 |
|
---|
92 | resourcestring
|
---|
93 | SPlayerWins = 'Player %s wins';
|
---|
94 | SEndGame = 'End game?';
|
---|
95 | SEndGameQuestion = 'Do you want to end current game?';
|
---|
96 | SRestartGame = 'Restart game?';
|
---|
97 | SRestartGameQuestion = 'Do you want to restart current game?';
|
---|
98 |
|
---|
99 |
|
---|
100 | { TCore }
|
---|
101 |
|
---|
102 | procedure TCore.DoOnMove(CellFrom, CellTo: TCell; var CountOnce,
|
---|
103 | CountRepeat: Integer; Update: Boolean; var Confirm: Boolean);
|
---|
104 | var
|
---|
105 | I: Integer;
|
---|
106 | begin
|
---|
107 | if Update then FormMove.SpinEditOnce.MaxValue := CellFrom.GetAvialPower + CountOnce
|
---|
108 | else FormMove.SpinEditOnce.MaxValue := CellFrom.GetAvialPower;
|
---|
109 | FormMove.SpinEditOnce.Value := CountOnce;
|
---|
110 | FormMove.TrackBarOnce.Max := FormMove.SpinEditOnce.MaxValue;
|
---|
111 | FormMove.TrackBarOnce.Position := FormMove.SpinEditOnce.Value;
|
---|
112 | FormMove.SpinEditRepeat.MaxValue := Game.Map.MaxPower;
|
---|
113 | FormMove.SpinEditRepeat.Value := CountRepeat;
|
---|
114 | FormMove.TrackBarRepeat.Max := FormMove.SpinEditRepeat.MaxValue;
|
---|
115 | FormMove.TrackBarRepeat.Position := FormMove.SpinEditRepeat.Value;
|
---|
116 | FormMove.DefendCount := CellTo.Power;
|
---|
117 | // Attack count from other surrounding cells without current move if already exists
|
---|
118 | FormMove.AttackCount := 0;
|
---|
119 | for I := 0 to CellTo.MovesTo.Count - 1 do
|
---|
120 | if TUnitMove(CellTo.MovesTo[I]).CellFrom <> CellFrom then
|
---|
121 | FormMove.AttackCount := FormMove.AttackCount + TUnitMove(CellTo.MovesTo[I]).CountOnce;
|
---|
122 | FormMove.ShowWinProbability := CellTo.Player <> CellFrom.Player;
|
---|
123 |
|
---|
124 | if FormMove.ShowModal = mrOk then begin
|
---|
125 | CountOnce := FormMove.SpinEditOnce.Value;
|
---|
126 | CountRepeat := FormMove.SpinEditRepeat.Value;
|
---|
127 | Confirm := True;
|
---|
128 | end else Confirm := False;
|
---|
129 | end;
|
---|
130 |
|
---|
131 | procedure TCore.DoOnWin(Player: TPlayer);
|
---|
132 | begin
|
---|
133 | FormMain.Redraw;
|
---|
134 | ShowMessage(Format(SPlayerWins, [Player.Name]));
|
---|
135 | end;
|
---|
136 |
|
---|
137 | procedure TCore.Delay(Time: Integer);
|
---|
138 | const
|
---|
139 | Slice = 50; // ms
|
---|
140 | begin
|
---|
141 | while Time > 0 do begin
|
---|
142 | Application.ProcessMessages;
|
---|
143 | if Time > Slice then Sleep(Slice) else Sleep(Time);
|
---|
144 | Dec(Time, Slice);
|
---|
145 | end;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | procedure TCore.GameNewTurnExecute(Sender: TObject);
|
---|
149 | begin
|
---|
150 | if AutoSaveEnabled then AutoSave;
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TCore.AutoSave;
|
---|
154 | var
|
---|
155 | OldFileName: string;
|
---|
156 | begin
|
---|
157 | OldFileName := Game.FileName;
|
---|
158 | Game.SaveToFile(GetAppConfigDir(False) + 'AutoSave.xtg');
|
---|
159 | Game.FileName := OldFileName;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | procedure TCore.LoadConfig;
|
---|
163 | begin
|
---|
164 | RegistryContext := RegContext(HKEY_CURRENT_USER, DefaultRegKey);
|
---|
165 | PersistentForm.RegistryContext := RegistryContext;
|
---|
166 | XMLConfig1.Filename := GetAppConfigDir(False) + 'Config.xml';
|
---|
167 |
|
---|
168 | LastMapFileName := XMLConfig1.GetValue('LastMapFileName', '');
|
---|
169 | DevelMode := XMLConfig1.GetValue('DevelMode', false);
|
---|
170 | AnimationSpeed := XMLConfig1.GetValue('AnimationSpeed', 50);
|
---|
171 | AutoSaveEnabled := XMLConfig1.GetValue('AutoSave', True);
|
---|
172 | CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode(XMLConfig1.GetValue('Language', ''));
|
---|
173 | ScaleDPI1.DPI := Point(XMLConfig1.GetValue('DPIX', 96), XMLConfig1.GetValue('DPIY', 96));
|
---|
174 | ScaleDPI1.AutoDetect := XMLConfig1.GetValue('DPIAuto', True);
|
---|
175 | end;
|
---|
176 |
|
---|
177 | procedure TCore.SaveConfig;
|
---|
178 | begin
|
---|
179 | XMLConfig1.SetValue('Language', CoolTranslator1.Language.Code);
|
---|
180 | XMLConfig1.SetValue('LastMapFileName', LastMapFileName);
|
---|
181 | XMLConfig1.SetValue('DevelMode', DevelMode);
|
---|
182 | XMLConfig1.SetValue('AnimationSpeed', AnimationSpeed);
|
---|
183 | XMLConfig1.SetValue('AutoSave', AutoSaveEnabled);
|
---|
184 | XMLConfig1.SetValue('DPIX', ScaleDPI1.DPI.X);
|
---|
185 | XMLConfig1.SetValue('DPIY', ScaleDPI1.DPI.Y);
|
---|
186 | XMLConfig1.SetValue('DPIAuto', ScaleDPI1.AutoDetect);
|
---|
187 | end;
|
---|
188 |
|
---|
189 | procedure TCore.CommandLineParams;
|
---|
190 | var
|
---|
191 | FileName: string;
|
---|
192 | begin
|
---|
193 | // Command line parameter handling
|
---|
194 | if (ParamCount > 0) then begin
|
---|
195 | FileName := UTF8Encode(ParamStr(1));
|
---|
196 | if FileExists(FileName) then begin
|
---|
197 | Game.LoadFromFile(FileName);
|
---|
198 | Player := Game.Players.GetFirstHuman;
|
---|
199 | LastMapFileName := OpenDialog1.FileName;
|
---|
200 | with Core.Game.CurrentPlayer do
|
---|
201 | View.DestRect := Bounds(0, 0, FormMain.PaintBox1.Width, FormMain.PaintBox1.Height);
|
---|
202 | FormMain.AZoomAll.Execute;
|
---|
203 | FormMain.Redraw;
|
---|
204 | end;
|
---|
205 | end;
|
---|
206 | end;
|
---|
207 |
|
---|
208 | procedure TCore.ScaleDPI;
|
---|
209 | var
|
---|
210 | I: Integer;
|
---|
211 | begin
|
---|
212 | {$ifdef DEBUG}
|
---|
213 | with Core.ScaleDPI1 do begin
|
---|
214 | //DesignDPI := Point(144, 144);
|
---|
215 | if (DesignDPI.X <> DPI.X) or (DesignDPI.Y <> DPI.Y) then begin
|
---|
216 | //ApplyToAll(DesignDPI);
|
---|
217 | FormNew.Show;
|
---|
218 | FormNew.Hide;
|
---|
219 | for I := 0 to Screen.FormCount - 1 do begin
|
---|
220 | StoreDimensions(Screen.Forms[I], StoredDimension);
|
---|
221 | ScaleDimensions(Screen.Forms[I], StoredDimension);
|
---|
222 | end;
|
---|
223 | ScaleImageList(Core.ImageListSmall, DesignDPI);
|
---|
224 | ScaleImageList(Core.ImageListLarge, DesignDPI);
|
---|
225 | end;
|
---|
226 | end;
|
---|
227 | {$endif}
|
---|
228 | end;
|
---|
229 |
|
---|
230 | procedure TCore.UpdateActions;
|
---|
231 | begin
|
---|
232 | Core.AGameEndTurn.Enabled := Core.Game.Running;
|
---|
233 | Core.AGameEnd.Enabled := Core.Game.Running;
|
---|
234 | end;
|
---|
235 |
|
---|
236 | procedure TCore.AExitExecute(Sender: TObject);
|
---|
237 | begin
|
---|
238 | Game.Running := False;
|
---|
239 | Application.Terminate;
|
---|
240 | end;
|
---|
241 |
|
---|
242 | procedure TCore.AAboutExecute(Sender: TObject);
|
---|
243 | begin
|
---|
244 | FormAbout := TFormAbout.Create(Self);
|
---|
245 | try
|
---|
246 | FormAbout.ShowModal;
|
---|
247 | finally
|
---|
248 | FreeAndNil(FormAbout);
|
---|
249 | end;
|
---|
250 | end;
|
---|
251 |
|
---|
252 | procedure TCore.AGameEndExecute(Sender: TObject);
|
---|
253 | begin
|
---|
254 | if MessageDlg(SEndGame, SEndGameQuestion, mtConfirmation, mbYesNo, 0) = mrYes then begin
|
---|
255 | Game.Running := False;
|
---|
256 | FormMain.Redraw;
|
---|
257 | UpdateActions;
|
---|
258 | end;
|
---|
259 | end;
|
---|
260 |
|
---|
261 | procedure TCore.AGameEndTurnExecute(Sender: TObject);
|
---|
262 | var
|
---|
263 | Computer: TComputer;
|
---|
264 | begin
|
---|
265 | Game.NextTurn;
|
---|
266 | FormMain.Redraw;
|
---|
267 | while Game.Running and (Game.CurrentPlayer.Mode <> pmHuman) do begin
|
---|
268 | if Game.CurrentPlayer.Mode = pmComputer then begin
|
---|
269 | Computer := TComputer.Create;
|
---|
270 | Computer.Game := Game;
|
---|
271 | Computer.Process;
|
---|
272 | Computer.Free;
|
---|
273 | FormMain.Redraw;
|
---|
274 | Delay(Trunc((100 - AnimationSpeed) / 100 * 2000));
|
---|
275 | end;
|
---|
276 | Game.NextTurn;
|
---|
277 | FormMain.Redraw;
|
---|
278 | Application.ProcessMessages;
|
---|
279 | Sleep(1);
|
---|
280 | end;
|
---|
281 | UpdateActions;
|
---|
282 | end;
|
---|
283 |
|
---|
284 | procedure TCore.AGameLoadExecute(Sender: TObject);
|
---|
285 | begin
|
---|
286 | if Game.FileName = '' then
|
---|
287 | OpenDialog1.FileName := LastMapFileName
|
---|
288 | else OpenDialog1.FileName := Game.FileName;
|
---|
289 | if OpenDialog1.Execute then begin
|
---|
290 | Game.LoadFromFile(OpenDialog1.FileName);
|
---|
291 | Player := Game.Players.GetFirstHuman;
|
---|
292 | LastMapFileName := OpenDialog1.FileName;
|
---|
293 | with Core.Game.CurrentPlayer do
|
---|
294 | View.DestRect := Bounds(0, 0, FormMain.PaintBox1.Width, FormMain.PaintBox1.Height);
|
---|
295 | FormMain.AZoomAll.Execute;
|
---|
296 | FormMain.Redraw;
|
---|
297 | end;
|
---|
298 | end;
|
---|
299 |
|
---|
300 | procedure TCore.AGameNewExecute(Sender: TObject);
|
---|
301 | begin
|
---|
302 | FormNew.Load(Game);
|
---|
303 | if FormNew.ShowModal = mrOk then begin
|
---|
304 | FormNew.Save(Game);
|
---|
305 | Game.New;
|
---|
306 | Player := Game.Players.GetFirstHuman;
|
---|
307 | Game.Running := True;
|
---|
308 | FormMain.AZoomAll.Execute;
|
---|
309 | UpdateActions;
|
---|
310 | end;
|
---|
311 | end;
|
---|
312 |
|
---|
313 | procedure TCore.AGameRestartExecute(Sender: TObject);
|
---|
314 | begin
|
---|
315 | if MessageDlg(SRestartGame, SRestartGameQuestion, mtConfirmation, mbYesNo, 0) = mrYes then begin
|
---|
316 | Game.New;
|
---|
317 | Game.Running := True;
|
---|
318 | FormMain.AZoomAll.Execute;
|
---|
319 | FormMain.Redraw;
|
---|
320 | UpdateActions;
|
---|
321 | end;
|
---|
322 | end;
|
---|
323 |
|
---|
324 | procedure TCore.AGameSaveExecute(Sender: TObject);
|
---|
325 | begin
|
---|
326 | if Game.FileName = '' then
|
---|
327 | SaveDialog1.FileName := ExtractFileDir(LastMapFileName)
|
---|
328 | else SaveDialog1.FileName := Game.FileName;
|
---|
329 | if SaveDialog1.Execute then begin
|
---|
330 | Game.SaveToFile(SaveDialog1.FileName);
|
---|
331 | LastMapFileName := SaveDialog1.FileName;
|
---|
332 | end;
|
---|
333 | end;
|
---|
334 |
|
---|
335 | procedure TCore.AHelpExecute(Sender: TObject);
|
---|
336 | begin
|
---|
337 | FormHelp := TFormHelp.Create(Self);
|
---|
338 | try
|
---|
339 | FormHelp.ShowModal;
|
---|
340 | finally
|
---|
341 | FreeAndNil(FormHelp);
|
---|
342 | end;
|
---|
343 | end;
|
---|
344 |
|
---|
345 | procedure TCore.ASettingsExecute(Sender: TObject);
|
---|
346 | begin
|
---|
347 | FormSettings.Load;
|
---|
348 | if FormSettings.ShowModal = mrOk then begin
|
---|
349 | FormSettings.Save;
|
---|
350 | Game.SaveConfig(XMLConfig1, 'Game');
|
---|
351 | end;
|
---|
352 | end;
|
---|
353 |
|
---|
354 | procedure TCore.CoolTranslator1Translate(Sender: TObject);
|
---|
355 | begin
|
---|
356 | UGame.InitStrings;
|
---|
357 | FormNew.Translate;
|
---|
358 | FormPlayer.Translate;
|
---|
359 | end;
|
---|
360 |
|
---|
361 | procedure TCore.DataModuleCreate(Sender: TObject);
|
---|
362 | begin
|
---|
363 | Game := TGame.Create;
|
---|
364 | Game.OnMove := DoOnMove;
|
---|
365 | Game.OnWin := DoOnWin;
|
---|
366 | Game.OnNewTurn := GameNewTurnExecute;
|
---|
367 | StoredDimension := TControlDimension.Create;
|
---|
368 | XMLConfig1.Filename := GetAppConfigDir(False) + 'Config.xml';
|
---|
369 | ForceDirectories(GetAppConfigDir(False));
|
---|
370 | end;
|
---|
371 |
|
---|
372 | procedure TCore.DataModuleDestroy(Sender: TObject);
|
---|
373 | begin
|
---|
374 | StoredDimension.Free;;
|
---|
375 | Game.SaveConfig(XMLConfig1, 'Game');
|
---|
376 | SaveConfig;
|
---|
377 | FreeAndNil(Game);
|
---|
378 | end;
|
---|
379 |
|
---|
380 | procedure TCore.Init;
|
---|
381 | var
|
---|
382 | I: Integer;
|
---|
383 | begin
|
---|
384 | {$IFDEF Linux}
|
---|
385 | // If installed in Linux system then use installation directory for po files
|
---|
386 | if Application.ExeName = '/usr/bin/xtactics' then
|
---|
387 | CoolTranslator1.POFilesFolder := '/usr/share/xtactics/languages';
|
---|
388 | {$ENDIF}
|
---|
389 | FInitialized := True;
|
---|
390 | LoadConfig;
|
---|
391 | for I := 0 to Game.Players.Count - 1 do
|
---|
392 | TPlayer(Game.Players[I]).View.DestRect := Rect(0, 0, FormMain.PaintBox1.Width,
|
---|
393 | FormMain.PaintBox1.Height);
|
---|
394 | Game.LoadConfig(XMLConfig1, 'Game');
|
---|
395 |
|
---|
396 | CommandLineParams;
|
---|
397 | ScaleDPI;
|
---|
398 |
|
---|
399 | if Game.FileName = '' then begin
|
---|
400 | Game.New;
|
---|
401 | Player := Game.Players.GetFirstHuman;
|
---|
402 | Game.Running := True;
|
---|
403 | FormMain.AZoomAll.Execute;
|
---|
404 | end;
|
---|
405 | end;
|
---|
406 |
|
---|
407 | end.
|
---|
408 |
|
---|