File: Battle.pas

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

interface

uses
  ScreenTools, Protocol, ButtonBase, ButtonA, Types, LCLIntf, LCLType,
  SysUtils, Classes, Graphics, Controls, Forms, DrawDlg, IsoEngine;

type

  { TBattleDlg }

  TBattleDlg = class(TDrawDlg)
    OKBtn: TButtonA;
    CancelBtn: TButtonA;
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormDeactivate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure OKBtnClick(Sender: TObject);
    procedure CancelBtnClick(Sender: TObject);
    procedure PaintBattleOutcome(ca: TCanvas; xm, ym, uix, ToLoc: Integer;
      Forecast: TBattleForecastEx);
  private
    IsoMap: TIsoMap;
  public
    uix, ToLoc: Integer;
    Forecast: TBattleForecastEx;
    IsSuicideQuery: boolean;
  end;

var
  BattleDlg: TBattleDlg;


implementation

uses
  Term, ClientTools;

{$R *.lfm}

const
  Border = 3;
  MessageLineSpacing = 20;

  DamageColor = $0000E0;
  FanaticColor = $800080;
  FirstStrikeColor = $A0A0A0;

procedure TBattleDlg.PaintBattleOutcome(ca: TCanvas; xm, ym, uix, ToLoc: Integer;
  Forecast: TBattleForecastEx);
var
  euix, ADamage, DDamage, StrMax, DamageMax, MaxBar, LAStr, LDStr, LADamage,
    LDDamage, LABaseDamage, LAAvoidedDamage, LDBaseDamage: Integer;
  // TerrType: Cardinal;
  UnitInfo: TUnitInfo;
  TextSize: TSize;
  LabelText: string;
  FirstStrike: boolean;
begin
  MaxBar := 65;

  // TerrType:=MyMap[ToLoc] and fTerrain;
  GetUnitInfo(ToLoc, euix, UnitInfo);

  FirstStrike := (MyModel[MyUn[uix].mix].Cap[mcFirst] > 0) and
    (Forecast.DBaseDamage >= UnitInfo.Health);
  ADamage := MyUn[uix].Health - Forecast.EndHealthAtt;
  if FirstStrike then
    ADamage := ADamage + Forecast.ABaseDamage div 2;
  DDamage := UnitInfo.Health - Forecast.EndHealthDef;
  if Forecast.AStr > Forecast.DStr then
    StrMax := Forecast.AStr
  else
    StrMax := Forecast.DStr;
  if ADamage > DDamage then
    DamageMax := ADamage
  else
    DamageMax := DDamage;
  if Forecast.ABaseDamage > Forecast.DBaseDamage then
    StrMax := StrMax * DamageMax div Forecast.ABaseDamage
  else
    StrMax := StrMax * DamageMax div Forecast.DBaseDamage;

  LAStr := Forecast.AStr * MaxBar div StrMax;
  LDStr := Forecast.DStr * MaxBar div StrMax;
  LADamage := ADamage * MaxBar div DamageMax;
  LABaseDamage := Forecast.ABaseDamage * MaxBar div DamageMax;
  if FirstStrike then
    LAAvoidedDamage := LABaseDamage div 2
  else
    LAAvoidedDamage := 0;
  LDDamage := DDamage * MaxBar div DamageMax;
  LDBaseDamage := Forecast.DBaseDamage * MaxBar div DamageMax;

  DarkGradient(ca, xm - 8 - LAStr, ym - 8, LAStr, 2);
  VDarkGradient(ca, xm - 8, ym - 8 - LDStr, LDStr, 2);
  LightGradient(ca, xm + 8, ym - 8, LDBaseDamage, DamageColor);
  if LDDamage > LDBaseDamage then
    LightGradient(ca, xm + 8 + LDBaseDamage, ym - 8, LDDamage - LDBaseDamage,
      FanaticColor);
  if LAAvoidedDamage > 0 then
    VLightGradient(ca, xm - 8, ym + 8, LAAvoidedDamage, FirstStrikeColor);
  VLightGradient(ca, xm - 8, ym + 8 + LAAvoidedDamage,
    LABaseDamage - LAAvoidedDamage, DamageColor);
  if LADamage > LABaseDamage then
    VLightGradient(ca, xm - 8, ym + 8 + LABaseDamage, LADamage - LABaseDamage,
      FanaticColor);
  BitBltCanvas(ca, xm - 12, ym - 12, 24, 24,
    HGrSystem.Mask.Canvas, 26, 146, SRCAND);
  BitBltCanvas(ca, xm - 12, ym - 12, 24, 24,
    HGrSystem.Data.Canvas, 26, 146, SRCPAINT);

  LabelText := Format('%d', [Forecast.AStr]);
  TextSize := ca.TextExtent(LabelText);
  if TextSize.cx div 2 + 2 > LAStr div 2 then
    RisedTextOut(ca, xm - 10 - TextSize.cx, ym - (TextSize.cy + 1) div 2,
      LabelText)
  else
    RisedTextOut(ca, xm - 8 - (LAStr + TextSize.cx) div 2,
      ym - (TextSize.cy + 1) div 2, LabelText);

  LabelText := Format('%d', [Forecast.DStr]);
  TextSize := ca.TextExtent(LabelText);
  if TextSize.cy div 2 > LDStr div 2 then
    RisedTextOut(ca, xm - (TextSize.cx + 1) div 2, ym - 8 - TextSize.cy,
      LabelText)
  else
    RisedTextOut(ca, xm - (TextSize.cx + 1) div 2,
      ym - 8 - (LDStr + TextSize.cy) div 2, LabelText);

  if Forecast.EndHealthDef <= 0 then
  begin
    BitBltCanvas(ca, xm + 9 + LDDamage - 7, ym - 6, 14, 17,
      HGrSystem.Mask.Canvas, 51, 153, SRCAND);
    BitBltCanvas(ca, xm + 8 + LDDamage - 7, ym - 7, 14, 17,
      HGrSystem.Mask.Canvas, 51, 153, SRCAND);
    BitBltCanvas(ca, xm + 8 + LDDamage - 7, ym - 7, 14, 17,
      HGrSystem.Data.Canvas, 51, 153, SRCPAINT);
  end;
  LabelText := Format('%d', [DDamage]);
  TextSize := ca.TextExtent(LabelText);
  if TextSize.cx div 2 + 2 > LDDamage div 2 then
  begin
    if Forecast.EndHealthDef > 0 then
      RisedTextOut(ca, xm + 10, ym - (TextSize.cy + 1) div 2, LabelText);
  end
  else
    RisedTextOut(ca, xm + 8 + (LDDamage - TextSize.cx) div 2,
      ym - (TextSize.cy + 1) div 2, LabelText);

  if Forecast.EndHealthAtt <= 0 then
  begin
    BitBltCanvas(ca, xm - 6, ym + 9 + LADamage - 7, 14, 17,
      HGrSystem.Mask.Canvas, 51, 153, SRCAND);
    BitBltCanvas(ca, xm - 7, ym + 8 + LADamage - 7, 14, 17,
      HGrSystem.Mask.Canvas, 51, 153, SRCAND);
    BitBltCanvas(ca, xm - 7, ym + 8 + LADamage - 7, 14, 17,
      HGrSystem.Data.Canvas, 51, 153, SRCPAINT);
  end;
  LabelText := Format('%d', [MyUn[uix].Health - Forecast.EndHealthAtt]);
  TextSize := ca.TextExtent(LabelText);
  if TextSize.cy div 2 > (LADamage - LAAvoidedDamage) div 2 + LAAvoidedDamage
  then
  begin
    if Forecast.EndHealthAtt > 0 then
      RisedTextOut(ca, xm - (TextSize.cx + 1) div 2, ym + 8 + LAAvoidedDamage,
        LabelText);
  end
  else
    RisedTextOut(ca, xm - (TextSize.cx + 1) div 2, ym + 8 + LAAvoidedDamage +
      (LADamage - LAAvoidedDamage - TextSize.cy) div 2, LabelText);

  IsoMap.SetOutput(Buffer);
  BitBltCanvas(Buffer.Canvas, 0, 0, 66, 48, ca, xm + 8 + 4,
    ym - 8 - 12 - 48);
  { if TerrType<fForest then
    Sprite(Buffer,HGrTerrain,0,16,66,32,1+TerrType*(xxt*2+1),1+yyt)
    else
    begin
    Sprite(Buffer,HGrTerrain,0,16,66,32,1+2*(xxt*2+1),1+yyt+2*(yyt*3+1));
    if (TerrType=fForest) and IsJungle(ToLoc div G.lx) then
    Sprite(Buffer,HGrTerrain,0,16,66,32,1+7*(xxt*2+1),1+yyt+19*(yyt*3+1))
    else Sprite(Buffer,HGrTerrain,0,16,66,32,1+7*(xxt*2+1),1+yyt+2*(2+TerrType-fForest)*(yyt*3+1));
    end; }
  IsoMap.PaintUnit(1, 0, UnitInfo, 0);
  BitBltCanvas(ca, xm + 8 + 4, ym - 8 - 12 - 48, 66, 48, Buffer.Canvas,
    0, 0);

  BitBltCanvas(Buffer.Canvas, 0, 0, 66, 48, ca, xm - 8 - 4 - 66,
    ym + 8 + 12);
  MakeUnitInfo(me, MyUn[uix], UnitInfo);
  UnitInfo.Flags := UnitInfo.Flags and not unFortified;
  IsoMap.PaintUnit(1, 0, UnitInfo, 0);
  BitBltCanvas(ca, xm - 8 - 4 - 66, ym + 8 + 12, 66, 48, Buffer.Canvas, 0, 0);
end;

procedure TBattleDlg.FormCreate(Sender: TObject);
begin
  IsoMap := TIsoMap.Create;
  OKBtn.Caption := Phrases.Lookup('BTN_YES');
  CancelBtn.Caption := Phrases.Lookup('BTN_NO');
  InitButtons;
end;

procedure TBattleDlg.FormShow(Sender: TObject);
begin
  if IsSuicideQuery then
  begin
    ClientWidth := 300;
    ClientHeight := 288;
    OKBtn.Visible := true;
    CancelBtn.Visible := true;
    Left := (Screen.Width - ClientWidth) div 2; // center on screen
    Top := (Screen.Height - ClientHeight) div 2;
  end
  else
  begin
    ClientWidth := 178;
    ClientHeight := 178;
    OKBtn.Visible := false;
    CancelBtn.Visible := false;
  end;
end;

procedure TBattleDlg.FormPaint(Sender: TObject);
var
  ym, cix, p: Integer;
  s, s1: string;
begin
  with Canvas do
  begin
    Brush.Color := 0;
    FillRect(Rect(0, 0, ClientWidth, ClientHeight));
    Brush.Style := bsClear;
    PaintBackground(self, 3 + Border, 3 + Border,
      ClientWidth - (6 + 2 * Border), ClientHeight - (6 + 2 * Border));
  end;
  Frame(Canvas, Border + 1, Border + 1, ClientWidth - (2 + Border),
    ClientHeight - (2 + Border), MainTexture.ColorBevelLight,
    MainTexture.ColorBevelShade);
  Frame(Canvas, 2 + Border, 2 + Border, ClientWidth - (3 + Border),
    ClientHeight - (3 + Border), MainTexture.ColorBevelLight,
    MainTexture.ColorBevelShade);

  if IsSuicideQuery then
  begin
    Canvas.Font.Assign(UniFont[ftCaption]);
    s := Phrases.Lookup('TITLE_SUICIDE');
    RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, s)) div 2,
      7 + Border, s);
    Canvas.Font.Assign(UniFont[ftNormal]);
    s := Phrases.Lookup('SUICIDE');
    p := pos('\', s);
    if p = 0 then
      RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, s))
        div 2, 205, s)
    else
    begin
      s1 := copy(s, 1, p - 1);
      RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, s1)) div 2,
        205 - MessageLineSpacing div 2, s1);
      s1 := copy(s, p + 1, 255);
      RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, s1)) div 2,
        205 + (MessageLineSpacing - MessageLineSpacing div 2), s1);
    end;
    ym := 110;
  end
  else
    ym := ClientHeight div 2;
  Canvas.Font.Assign(UniFont[ftSmall]);
  PaintBattleOutcome(Canvas, ClientWidth div 2, ym, uix, ToLoc, Forecast);

  for cix := 0 to ControlCount - 1 do
    if (Controls[cix].Visible) and (Controls[cix] is TButtonBase) then
      BtnFrame(Canvas, Controls[cix].BoundsRect, MainTexture);
end;

procedure TBattleDlg.FormDestroy(Sender: TObject);
begin
  FreeAndNil(IsoMap);
end;

procedure TBattleDlg.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if not IsSuicideQuery then
    Close;
end;

procedure TBattleDlg.FormDeactivate(Sender: TObject);
begin
  if not IsSuicideQuery then
    Close;
end;

procedure TBattleDlg.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if not IsSuicideQuery and (Key <> VK_SHIFT) then
  begin
    Close;
    MainScreen.Update;
    if Key <> VK_ESCAPE then
      MainScreen.FormKeyDown(Sender, Key, Shift);
  end;
end;

procedure TBattleDlg.OKBtnClick(Sender: TObject);
begin
  ModalResult := mrOK;
end;

procedure TBattleDlg.CancelBtnClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

end.