File: NoTerm.pas

package info (click to toggle)
c-evo-dh 3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,540 kB
  • sloc: pascal: 57,645; xml: 243; makefile: 114; sh: 4
file content (397 lines) | stat: -rw-r--r-- 11,804 bytes parent folder | download | duplicates (2)
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
{$INCLUDE Switches.inc}
unit NoTerm;

interface

uses
  ScreenTools, Protocol, Messg, LCLIntf, LCLType, dateutils, Platform,
  SysUtils, Classes, Graphics, Controls, Forms, ButtonB, DrawDlg;

type
  TRunMode = (rmStop, rmStopped, rmRunning, rmQuit);

  TNoTermDlg = class(TDrawDlg)
    QuitBtn: TButtonB;
    GoBtn: TButtonB;
    procedure GoBtnClick(Sender: TObject);
    procedure QuitBtnClick(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
  public
    procedure Client(Command, Player: integer; var Data);
  private
    Me: Integer;
    Active: Integer;
    ToldAlive: Integer;
    Round: Integer;
    LastShowYearTime: TDateTime;
    LastShowTurnChange: TDateTime;
    LastNewTurn: TDateTime;
    TurnTime: Extended;
    TotalStatTime: Extended;
    G: TNewGameData;
    Server: TServerCall;
    Shade: TBitmap;
    State: TBitmap;
    WinStat: array [0 .. nPl - 1] of Integer;
    ExtStat: array [0 .. nPl - 1] of Integer;
    AloneStat: array [0 .. nPl - 1] of Integer;
    DisallowShowActive: array [0 .. nPl - 1] of Boolean;
    TimeStat: array [0 .. nPl - 1] of Extended;
    Mode: TRunMode;
    procedure NewStat;
    procedure EndPlaying;
    procedure ShowActive(p: integer; Active: boolean);
    procedure ShowYear;
  end;

var
  NoTermDlg: TNoTermDlg;

procedure Client(Command, Player: integer; var Data); stdcall;


implementation

uses
  GameServer, Log;

{$R *.lfm}

const
  UpdateInterval = 0.1; // seconds
  ShowActiveThreshold = 0.05; // seconds

  nPlOffered = 9;
  x0Brain = 109 + 48 + 23;
  y0Brain = 124 + 48 + 7 + 16;
  dxBrain = 128;
  dyBrain = 128;
  xBrain: array [0 .. nPlOffered - 1] of integer = (x0Brain, x0Brain,
    x0Brain + dxBrain, x0Brain + dxBrain, x0Brain + dxBrain, x0Brain,
    x0Brain - dxBrain, x0Brain - dxBrain, x0Brain - dxBrain);
  yBrain: array [0 .. nPlOffered - 1] of integer = (y0Brain, y0Brain - dyBrain,
    y0Brain - dyBrain, y0Brain, y0Brain + dyBrain, y0Brain + dyBrain,
    y0Brain + dyBrain, y0Brain, y0Brain - dyBrain);
  xActive: array [0 .. nPlOffered - 1] of integer = (0, 0, 36, 51, 36, 0,
    -36, -51, -36);
  yActive: array [0 .. nPlOffered - 1] of integer = (0, -51, -36, 0, 36, 51,
    36, 0, -36);

var
  FormsCreated: boolean;

procedure TNoTermDlg.FormCreate(Sender: TObject);
begin
  Left := Screen.Width - Width - 8;
  Top := 8;
  Caption := Phrases.Lookup('AIT');
  Canvas.Brush.Style := bsClear;
  Canvas.Font.Assign(UniFont[ftSmall]);
  TitleHeight := 36;
  InitButtons;
  LastShowYearTime := 0;
end;

procedure TNoTermDlg.NewStat;
begin
  Round := 0;
  FillChar(WinStat, SizeOf(WinStat), 0);
  FillChar(ExtStat, SizeOf(ExtStat), 0);
  FillChar(AloneStat, SizeOf(AloneStat), 0);
  FillChar(TimeStat, SizeOf(TimeStat), 0);
  TotalStatTime := 0;
  Mode := rmStop;
end;

procedure TNoTermDlg.EndPlaying;
var
  EndCommand: integer;
begin
  NewStat;
  if G.RO[me].Turn > 0 then
    with MessgDlg do
    begin
      MessgText := Phrases.Lookup('ENDTOUR');
      Kind := mkYesNo;
      ShowModal;
      if ModalResult = mrIgnore then
        EndCommand := sResign
      else
        EndCommand := sBreak;
    end
  else
    EndCommand := sResign;
  Server(EndCommand, Me, 0, nil^);
end;

procedure TNoTermDlg.ShowActive(p: integer; Active: boolean);
begin
  if p < nPlOffered then
    Sprite(Canvas, HGrSystem, x0Brain + 28 + xActive[p],
      y0Brain + 28 + yActive[p], 8, 8, 81 + 9 * Byte(Active), 16);
end;

procedure TNoTermDlg.ShowYear;
begin
  Fill(State.Canvas, 0, 0, 192, 20, 64, 287 + 138);
  RisedTextOut(State.Canvas, 0, 0, Format(Phrases.Lookup('AIT_ROUND'), [Round])
    + ' ' + TurnToString(G.RO[me].Turn));
  BitBltCanvas(Canvas, 64, 287 + 138, 192, 20, State.Canvas, 0, 0);
end;

procedure TNoTermDlg.Client(Command, Player: integer; var Data);
var
  i, x, y, p: integer;
  ActiveDuration: extended;
  ShipComplete: boolean;
  r: TRect;
  nowt: TDateTime;
begin
  case Command of
    cDebugMessage:
      LogDlg.Add(Player, G.RO[0].Turn, pchar(@Data));

    cInitModule:
      begin
        Server := TInitModuleData(Data).Server;
        TInitModuleData(Data).Flags := aiThreaded;
        Shade := TBitmap.Create;
        Shade.SetSize(64, 64);
        for x := 0 to 63 do
          for y := 0 to 63 do
            if Odd(x + y) then
              Shade.Canvas.Pixels[x, y] := $FFFFFF
            else
              Shade.Canvas.Pixels[x, y] := $000000;
        State := TBitmap.Create;
        State.SetSize(192, 20);
        State.Canvas.Brush.Style := bsClear;
        State.Canvas.Font.Assign(UniFont[ftSmall]);
        NewStat;
      end;

    cReleaseModule:
      begin
        FreeAndNil(Shade);
        FreeAndNil(State);
      end;

    cNewGame, cLoadGame:
      begin
        inc(Round);
        if Mode = rmRunning then
        begin
          Invalidate;
          Update;
        end
        else
          Show;
        G := TNewGameData(Data);
        LogDlg.mSlot.Visible := false;
        LogDlg.Host := nil;
        ToldAlive := G.RO[me].Alive;
        Active := -1;
        FillChar(DisallowShowActive, SizeOf(DisallowShowActive), 0); // false
        LastShowTurnChange := 0;
        LastNewTurn := 0;
        TurnTime := 1.0;
      end;

    cBreakGame:
      begin
        LogDlg.List.Clear;
        if Mode <> rmRunning then
        begin
          if LogDlg.Visible then
            LogDlg.Close;
          Close;
        end;
      end;

    cTurn, cResume, cContinue:
      begin
        me := Player;
        if Active >= 0 then
        begin
          ShowActive(Active, false);
          Active := -1;
        end; // should not happen

        nowt := NowPrecise;
        if SecondOf(nowt - LastShowYearTime) >= UpdateInterval then
        begin
          ShowYear;
          LastShowYearTime := nowt;
        end;
        TurnTime := SecondOf(nowt - LastNewTurn);
        LastNewTurn := nowt;
        if (G.RO[me].Alive <> ToldAlive) then
        begin
          for p := 1 to nPlOffered - 1 do
            if 1 shl p and (G.RO[me].Alive xor ToldAlive) <> 0 then
            begin
              r := Rect(xBrain[p], yBrain[p] - 16, xBrain[p] + 64,
                yBrain[p] - 16 + 64);
              InvalidateRect(Handle, @r, false);
            end;
          ToldAlive := G.RO[me].Alive;
        end;
        Application.ProcessMessages;
        if Mode = rmQuit then
          EndPlaying
        else if G.RO[me].Happened and phGameEnd <> 0 then
        begin // game ended, update statistics
          for p := 1 to nPlOffered - 1 do
            if Assigned(PlayersBrain[p]) then
              if 1 shl p and G.RO[me].Alive = 0 then
                inc(ExtStat[p]) // extinct
              else if G.RO[me].Alive = 1 shl p then
                inc(AloneStat[p]) // only player alive
              else
              begin // alive but not alone -- check colony ship
                ShipComplete := true;
                for i := 0 to nShipPart - 1 do
                  if G.RO[me].Ship[p].Parts[i] < ShipNeed[i] then
                    ShipComplete := false;
                if ShipComplete then
                  inc(WinStat[p]);
              end;
          if Mode = rmRunning then
            Server(sNextRound, me, 0, nil^);
        end
        else if Mode = rmRunning then
          Server(sTurn, me, 0, nil^);
        if Mode = rmStop then
        begin
          GoBtn.ButtonIndex := 22;
          Mode := rmStopped;
        end;
      end;

    cShowTurnChange:
      begin
        nowt := NowPrecise;
        if Active >= 0 then
        begin
          ActiveDuration := SecondOf(nowt - LastShowTurnChange);
          TimeStat[Active] := TimeStat[Active] + ActiveDuration;
          TotalStatTime := TotalStatTime + ActiveDuration;
          if not DisallowShowActive[Active] then
            ShowActive(Active, false);
          DisallowShowActive[Active] := (ActiveDuration < TurnTime * 0.25) and
            (ActiveDuration < ShowActiveThreshold);
        end;
        LastShowTurnChange := nowt;

        Active := integer(Data);
        if (Active >= 0) and not DisallowShowActive[Active] then
          ShowActive(Active, true);
      end;
  end;
end;

procedure TNoTermDlg.GoBtnClick(Sender: TObject);
begin
  if Mode = rmRunning then
    Mode := rmStop
  else if Mode = rmStopped then
  begin
    Mode := rmRunning;
    GoBtn.ButtonIndex := 23;
    GoBtn.Update;
    Server(sTurn, me, 0, nil^);
  end;
end;

procedure TNoTermDlg.QuitBtnClick(Sender: TObject);
begin
  if Mode = rmStopped then EndPlaying
    else Mode := rmQuit;
end;

procedure TNoTermDlg.FormPaint(Sender: TObject);
var
  i, TimeShare: integer;
begin
  Fill(Canvas, 3, 3, ClientWidth - 6, ClientHeight - 6, 0, 0);
  Frame(Canvas, 0, 0, ClientWidth - 1, ClientHeight - 1, $000000, $000000);
  Frame(Canvas, 1, 1, ClientWidth - 2, ClientHeight - 2,
    MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
  Frame(Canvas, 2, 2, ClientWidth - 3, ClientHeight - 3,
    MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
  Corner(Canvas, 1, 1, 0, MainTexture);
  Corner(Canvas, ClientWidth - 9, 1, 1, MainTexture);
  Corner(Canvas, 1, ClientHeight - 9, 2, MainTexture);
  Corner(Canvas, ClientWidth - 9, ClientHeight - 9, 3, MainTexture);
  Canvas.Font.Assign(UniFont[ftCaption]);
  RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, Caption)) div 2,
    7, Caption);
  Canvas.Font.Assign(UniFont[ftSmall]);
  for i := 1 to nPlOffered - 1 do
    if Assigned(PlayersBrain[i]) then
    begin
      Frame(Canvas, xBrain[i] - 24, yBrain[i] - 8 - 16, xBrain[i] - 24 + 111,
        yBrain[i] - 8 - 16 + 111, MainTexture.ColorBevelShade,
        MainTexture.ColorBevelShade);
      FrameImage(Canvas, PlayersBrain[i].Picture, xBrain[i],
        yBrain[i] - 16, 64, 64, 0, 0);
      if 1 shl i and G.RO[me].Alive = 0 then
        BitBltCanvas(Canvas, xBrain[i], yBrain[i] - 16, 64, 64,
          Shade.Canvas, 0, 0, SRCAND);
      Sprite(Canvas, HGrSystem, xBrain[i] + 30 - 14, yBrain[i] + 53, 14,
        14, 1, 316);
      RisedTextOut(Canvas, xBrain[i] + 30 - 16 - BiColorTextWidth(Canvas,
        IntToStr(WinStat[i])), yBrain[i] + 51, IntToStr(WinStat[i]));
      Sprite(Canvas, HGrSystem, xBrain[i] + 34, yBrain[i] + 53, 14, 14,
        1 + 15, 316);
      RisedTextOut(Canvas, xBrain[i] + 34 + 16, yBrain[i] + 51,
        IntToStr(AloneStat[i]));
      Sprite(Canvas, HGrSystem, xBrain[i] + 30 - 14, yBrain[i] + 53 + 16, 14,
        14, 1 + 30, 316);
      RisedTextOut(Canvas, xBrain[i] + 30 - 16 - BiColorTextWidth(Canvas,
        IntToStr(ExtStat[i])), yBrain[i] + 51 + 16, IntToStr(ExtStat[i]));
      Sprite(Canvas, HGrSystem, xBrain[i] + 34, yBrain[i] + 53 + 16, 14, 14,
        1 + 45, 316);
      if TotalStatTime > 0 then
      begin
        TimeShare := trunc(TimeStat[i] / TotalStatTime * 100 + 0.5);
        RisedTextOut(Canvas, xBrain[i] + 34 + 16, yBrain[i] + 51 + 16,
          IntToStr(TimeShare) + '%');
      end;
      ShowActive(i, i = Active);
    end;
  Sprite(Canvas, HGrSystem2, x0Brain + 32 - 20, y0Brain + 32 - 20, 40,
    40, 115, 1);
  ShowYear;
  BtnFrame(Canvas, GoBtn.BoundsRect, MainTexture);
  BtnFrame(Canvas, QuitBtn.BoundsRect, MainTexture);
  // BtnFrame(Canvas,StatBtn.BoundsRect,MainTexture);
end;

procedure Client(Command, Player: Integer; var Data);
begin
  if not FormsCreated then
  begin
    FormsCreated := True;
    Application.CreateForm(TNoTermDlg, NoTermDlg);
  end;
  NoTermDlg.Client(Command, Player, Data);
end;

procedure TNoTermDlg.FormKeyDown(Sender: TObject; var Key: word;
  Shift: TShiftState);
begin
  if (char(Key) = 'M') and (ssCtrl in Shift) then
    if LogDlg.Visible then
      LogDlg.Close
    else
      LogDlg.Show;
end;

initialization

FormsCreated := False;

end.