source: branches/delphi/NoTerm.pas

Last change on this file was 6, checked in by chronos, 7 years ago
  • Modified: Formated all project source files using Delphi formatter as original indentation and other formatting was really bad.
File size: 12.0 KB
Line 
1{$INCLUDE switches}
2unit NoTerm;
3
4interface
5
6uses
7 ScreenTools, Protocol, Messg,
8
9 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
10 ButtonBase, ButtonB;
11
12type
13 TNoTermDlg = class(TDrawDlg)
14 QuitBtn: TButtonB;
15 GoBtn: TButtonB;
16 procedure GoBtnClick(Sender: TObject);
17 procedure QuitBtnClick(Sender: TObject);
18 procedure FormPaint(Sender: TObject);
19 procedure FormCreate(Sender: TObject);
20 procedure FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
21 public
22 procedure Client(Command, Player: integer; var Data);
23 private
24 me, Active, ToldAlive, Round: integer;
25 PerfFreq, LastShowYearTime, LastShowTurnChange, LastNewTurn: int64;
26 TurnTime, TotalStatTime: extended;
27 G: TNewGameData;
28 Server: TServerCall;
29 Shade, State: TBitmap;
30 WinStat, ExtStat, AloneStat: array [0 .. nPl - 1] of integer;
31 DisallowShowActive: array [0 .. nPl - 1] of boolean;
32 TimeStat: array [0 .. nPl - 1] of extended;
33 Mode: (Stop, Stopped, Running, Quit);
34 procedure NewStat;
35 procedure EndPlaying;
36 procedure ShowActive(p: integer; Active: boolean);
37 procedure ShowYear;
38 end;
39
40var
41 NoTermDlg: TNoTermDlg;
42
43procedure Client(Command, Player: integer; var Data); stdcall;
44
45implementation
46
47uses GameServer, log, Start;
48
49{$R *.DFM}
50
51const
52 UpdateInterval = 0.1; // seconds
53 ShowActiveThreshold = 0.05; // seconds
54
55 nPlOffered = 9;
56 x0Brain = 109 + 48 + 23;
57 y0Brain = 124 + 48 + 7 + 16;
58 dxBrain = 128;
59 dyBrain = 128;
60 xBrain: array [0 .. nPlOffered - 1] of integer = (x0Brain, x0Brain,
61 x0Brain + dxBrain, x0Brain + dxBrain, x0Brain + dxBrain, x0Brain,
62 x0Brain - dxBrain, x0Brain - dxBrain, x0Brain - dxBrain);
63 yBrain: array [0 .. nPlOffered - 1] of integer = (y0Brain, y0Brain - dyBrain,
64 y0Brain - dyBrain, y0Brain, y0Brain + dyBrain, y0Brain + dyBrain,
65 y0Brain + dyBrain, y0Brain, y0Brain - dyBrain);
66 xActive: array [0 .. nPlOffered - 1] of integer = (0, 0, 36, 51, 36, 0,
67 -36, -51, -36);
68 yActive: array [0 .. nPlOffered - 1] of integer = (0, -51, -36, 0, 36, 51,
69 36, 0, -36);
70
71var
72 FormsCreated: boolean;
73
74procedure TNoTermDlg.FormCreate(Sender: TObject);
75begin
76 Left := Screen.Width - Width - 8;
77 Top := 8;
78 Caption := Phrases.Lookup('AIT');
79 Canvas.Brush.Style := bsClear;
80 Canvas.Font.Assign(UniFont[ftSmall]);
81 TitleHeight := 36;
82 InitButtons();
83 QueryPerformanceFrequency(PerfFreq);
84 LastShowYearTime := 0;
85end;
86
87procedure TNoTermDlg.NewStat;
88begin
89 Round := 0;
90 FillChar(WinStat, SizeOf(WinStat), 0);
91 FillChar(ExtStat, SizeOf(ExtStat), 0);
92 FillChar(AloneStat, SizeOf(AloneStat), 0);
93 FillChar(TimeStat, SizeOf(TimeStat), 0);
94 TotalStatTime := 0;
95 Mode := Stop;
96end;
97
98procedure TNoTermDlg.EndPlaying;
99var
100 EndCommand: integer;
101begin
102 NewStat;
103 if G.RO[me].Turn > 0 then
104 with MessgDlg do
105 begin
106 MessgText := Phrases.Lookup('ENDTOUR');
107 Kind := mkYesNo;
108 ShowModal;
109 if ModalResult = mrIgnore then
110 EndCommand := sResign
111 else
112 EndCommand := sBreak
113 end
114 else
115 EndCommand := sResign;
116 Server(EndCommand, me, 0, nil^)
117end;
118
119procedure TNoTermDlg.ShowActive(p: integer; Active: boolean);
120begin
121 if p < nPlOffered then
122 Sprite(Canvas, HGrSystem, x0Brain + 28 + xActive[p],
123 y0Brain + 28 + yActive[p], 8, 8, 81 + 9 * Byte(Active), 16);
124end;
125
126procedure TNoTermDlg.ShowYear;
127begin
128 Fill(State.Canvas, 0, 0, 192, 20, 64, 287 + 138);
129 RisedTextOut(State.Canvas, 0, 0, Format(Phrases.Lookup('AIT_ROUND'), [Round])
130 + ' ' + TurnToString(G.RO[me].Turn));
131 BitBlt(Canvas.Handle, 64, 287 + 138, 192, 20, State.Canvas.Handle, 0,
132 0, SRCCOPY);
133end;
134
135procedure TNoTermDlg.Client(Command, Player: integer; var Data);
136var
137 i, x, y, p: integer;
138 ActiveDuration: extended;
139 ShipComplete: boolean;
140 r: TRect;
141 now: int64;
142begin
143 case Command of
144 cDebugMessage:
145 LogDlg.Add(Player, G.RO[0].Turn, pchar(@Data));
146
147 cInitModule:
148 begin
149 Server := TInitModuleData(Data).Server;
150 TInitModuleData(Data).Flags := aiThreaded;
151 Shade := TBitmap.Create;
152 Shade.Width := 64;
153 Shade.Height := 64;
154 for x := 0 to 63 do
155 for y := 0 to 63 do
156 if Odd(x + y) then
157 Shade.Canvas.Pixels[x, y] := $FFFFFF
158 else
159 Shade.Canvas.Pixels[x, y] := $000000;
160 State := TBitmap.Create;
161 State.Width := 192;
162 State.Height := 20;
163 State.Canvas.Brush.Style := bsClear;
164 State.Canvas.Font.Assign(UniFont[ftSmall]);
165 NewStat;
166 end;
167
168 cReleaseModule:
169 begin
170 Shade.Free;
171 State.Free
172 end;
173
174 cNewGame, cLoadGame:
175 begin
176 inc(Round);
177 if Mode = Running then
178 begin
179 Invalidate;
180 Update
181 end
182 else
183 Show;
184 G := TNewGameData(Data);
185 LogDlg.mSlot.Visible := false;
186 LogDlg.Host := nil;
187 ToldAlive := G.RO[me].Alive;
188 Active := -1;
189 FillChar(DisallowShowActive, SizeOf(DisallowShowActive), 0); // false
190 LastShowTurnChange := 0;
191 LastNewTurn := 0;
192 TurnTime := 1.0;
193 end;
194
195 cBreakGame:
196 begin
197 LogDlg.List.Clear;
198 if Mode <> Running then
199 begin
200 if LogDlg.Visible then
201 LogDlg.Close;
202 Close;
203 end
204 end;
205
206 cTurn, cResume, cContinue:
207 begin
208 me := Player;
209 if Active >= 0 then
210 begin
211 ShowActive(Active, false);
212 Active := -1
213 end; // should not happen
214
215 QueryPerformanceCounter(now);
216 if {$IFDEF VER100}(now.LowPart - LastShowYearTime.LowPart){$ELSE}(now - LastShowYearTime){$ENDIF} / PerfFreq >= UpdateInterval then
217 begin
218 ShowYear;
219 LastShowYearTime := now;
220 end;
221 TurnTime :=
222 {$IFDEF VER100}(now.LowPart - LastNewTurn.LowPart){$ELSE}(now - LastNewTurn){$ENDIF} / PerfFreq;
223 LastNewTurn := now;
224 if (G.RO[me].Alive <> ToldAlive) then
225 begin
226 for p := 1 to nPlOffered - 1 do
227 if 1 shl p and (G.RO[me].Alive xor ToldAlive) <> 0 then
228 begin
229 r := Rect(xBrain[p], yBrain[p] - 16, xBrain[p] + 64,
230 yBrain[p] - 16 + 64);
231 InvalidateRect(Handle, @r, false);
232 end;
233 ToldAlive := G.RO[me].Alive;
234 end;
235 Application.ProcessMessages;
236 if Mode = Quit then
237 EndPlaying
238 else if G.RO[me].Happened and phGameEnd <> 0 then
239 begin // game ended, update statistics
240 for p := 1 to nPlOffered - 1 do
241 if bixView[p] >= 0 then
242 if 1 shl p and G.RO[me].Alive = 0 then
243 inc(ExtStat[p]) // extinct
244 else if G.RO[me].Alive = 1 shl p then
245 inc(AloneStat[p]) // only player alive
246 else
247 begin // alive but not alone -- check colony ship
248 ShipComplete := true;
249 for i := 0 to nShipPart - 1 do
250 if G.RO[me].Ship[p].Parts[i] < ShipNeed[i] then
251 ShipComplete := false;
252 if ShipComplete then
253 inc(WinStat[p])
254 end;
255 if Mode = Running then
256 Server(sNextRound, me, 0, nil^)
257 end
258 else if Mode = Running then
259 Server(sTurn, me, 0, nil^);
260 if Mode = Stop then
261 begin
262 GoBtn.ButtonIndex := 22;
263 Mode := Stopped
264 end
265 end;
266
267 cShowTurnChange:
268 begin
269 QueryPerformanceCounter(now);
270 if Active >= 0 then
271 begin
272 ActiveDuration :=
273 {$IFDEF VER100}(now.LowPart - LastShowTurnChange.LowPart){$ELSE}(now - LastShowTurnChange){$ENDIF} / PerfFreq;
274 TimeStat[Active] := TimeStat[Active] + ActiveDuration;
275 TotalStatTime := TotalStatTime + ActiveDuration;
276 if not DisallowShowActive[Active] then
277 ShowActive(Active, false);
278 DisallowShowActive[Active] := (ActiveDuration < TurnTime * 0.25) and
279 (ActiveDuration < ShowActiveThreshold);
280 end;
281 LastShowTurnChange := now;
282
283 Active := integer(Data);
284 if (Active >= 0) and not DisallowShowActive[Active] then
285 ShowActive(Active, true);
286 end
287
288 end
289end;
290
291procedure TNoTermDlg.GoBtnClick(Sender: TObject);
292begin
293 if Mode = Running then
294 Mode := Stop
295 else if Mode = Stopped then
296 begin
297 Mode := Running;
298 GoBtn.ButtonIndex := 23;
299 GoBtn.Update;
300 Server(sTurn, me, 0, nil^);
301 end
302end;
303
304procedure TNoTermDlg.QuitBtnClick(Sender: TObject);
305begin
306 if Mode = Stopped then
307 EndPlaying
308 else
309 Mode := Quit
310end;
311
312procedure TNoTermDlg.FormPaint(Sender: TObject);
313var
314 i, TimeShare: integer;
315begin
316 Fill(Canvas, 3, 3, ClientWidth - 6, ClientHeight - 6, 0, 0);
317 Frame(Canvas, 0, 0, ClientWidth - 1, ClientHeight - 1, $000000, $000000);
318 Frame(Canvas, 1, 1, ClientWidth - 2, ClientHeight - 2,
319 MainTexture.clBevelLight, MainTexture.clBevelShade);
320 Frame(Canvas, 2, 2, ClientWidth - 3, ClientHeight - 3,
321 MainTexture.clBevelLight, MainTexture.clBevelShade);
322 Corner(Canvas, 1, 1, 0, MainTexture);
323 Corner(Canvas, ClientWidth - 9, 1, 1, MainTexture);
324 Corner(Canvas, 1, ClientHeight - 9, 2, MainTexture);
325 Corner(Canvas, ClientWidth - 9, ClientHeight - 9, 3, MainTexture);
326 Canvas.Font.Assign(UniFont[ftCaption]);
327 RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, Caption)) div 2,
328 7, Caption);
329 Canvas.Font.Assign(UniFont[ftSmall]);
330 for i := 1 to nPlOffered - 1 do
331 if bixView[i] >= 0 then
332 begin
333 Frame(Canvas, xBrain[i] - 24, yBrain[i] - 8 - 16, xBrain[i] - 24 + 111,
334 yBrain[i] - 8 - 16 + 111, MainTexture.clBevelShade,
335 MainTexture.clBevelShade);
336 FrameImage(Canvas, StartDlg.BrainPicture[bixView[i]], xBrain[i],
337 yBrain[i] - 16, 64, 64, 0, 0);
338 if 1 shl i and G.RO[me].Alive = 0 then
339 BitBlt(Canvas.Handle, xBrain[i], yBrain[i] - 16, 64, 64,
340 Shade.Canvas.Handle, 0, 0, SRCAND);
341 Sprite(Canvas, HGrSystem, xBrain[i] + 30 - 14, yBrain[i] + 53, 14,
342 14, 1, 316);
343 RisedTextOut(Canvas, xBrain[i] + 30 - 16 - BiColorTextWidth(Canvas,
344 IntToStr(WinStat[i])), yBrain[i] + 51, IntToStr(WinStat[i]));
345 Sprite(Canvas, HGrSystem, xBrain[i] + 34, yBrain[i] + 53, 14, 14,
346 1 + 15, 316);
347 RisedTextOut(Canvas, xBrain[i] + 34 + 16, yBrain[i] + 51,
348 IntToStr(AloneStat[i]));
349 Sprite(Canvas, HGrSystem, xBrain[i] + 30 - 14, yBrain[i] + 53 + 16, 14,
350 14, 1 + 30, 316);
351 RisedTextOut(Canvas, xBrain[i] + 30 - 16 - BiColorTextWidth(Canvas,
352 IntToStr(ExtStat[i])), yBrain[i] + 51 + 16, IntToStr(ExtStat[i]));
353 Sprite(Canvas, HGrSystem, xBrain[i] + 34, yBrain[i] + 53 + 16, 14, 14,
354 1 + 45, 316);
355 if TotalStatTime > 0 then
356 begin
357 TimeShare := trunc(TimeStat[i] / TotalStatTime * 100 + 0.5);
358 RisedTextOut(Canvas, xBrain[i] + 34 + 16, yBrain[i] + 51 + 16,
359 IntToStr(TimeShare) + '%');
360 end;
361 ShowActive(i, i = Active);
362 end;
363 Sprite(Canvas, HGrSystem2, x0Brain + 32 - 20, y0Brain + 32 - 20, 40,
364 40, 115, 1);
365 ShowYear;
366 BtnFrame(Canvas, GoBtn.BoundsRect, MainTexture);
367 BtnFrame(Canvas, QuitBtn.BoundsRect, MainTexture);
368 // BtnFrame(Canvas,StatBtn.BoundsRect,MainTexture);
369end;
370
371procedure Client;
372begin
373 if not FormsCreated then
374 begin
375 FormsCreated := true;
376 Application.CreateForm(TNoTermDlg, NoTermDlg);
377 end;
378 NoTermDlg.Client(Command, Player, Data);
379end;
380
381procedure TNoTermDlg.FormKeyDown(Sender: TObject; var Key: word;
382 Shift: TShiftState);
383begin
384 if (char(Key) = 'M') and (ssCtrl in Shift) then
385 if LogDlg.Visible then
386 LogDlg.Close
387 else
388 LogDlg.Show;
389end;
390
391initialization
392
393FormsCreated := false;
394
395end.
Note: See TracBrowser for help on using the repository browser.