source: tags/1.3.4/LocalPlayer/Battle.pas

Last change on this file was 565, checked in by chronos, 7 months ago
  • Modified: Start form changed to use Offscreen bitmap for drawing as Qt5 doesn't support copying from form canvas.
File size: 10.0 KB
Line 
1{$INCLUDE Switches.inc}
2unit Battle;
3
4interface
5
6uses
7 ScreenTools, Protocol, ButtonBase, ButtonA, Types, LCLIntf, LCLType,
8 SysUtils, Classes, DrawDlg, IsoEngine,
9 {$IFDEF DPI}Dpi.Graphics, Dpi.Controls, Dpi.Forms, System.UITypes{$ELSE}
10 Graphics, Controls, Forms{$ENDIF};
11
12type
13
14 { TBattleDlg }
15
16 TBattleDlg = class(TDrawDlg)
17 OKBtn: TButtonA;
18 CancelBtn: TButtonA;
19 procedure FormDestroy(Sender: TObject);
20 procedure FormPaint(Sender: TObject);
21 procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
22 Shift: TShiftState; X, Y: Integer);
23 procedure FormDeactivate(Sender: TObject);
24 procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
25 procedure FormCreate(Sender: TObject);
26 procedure FormShow(Sender: TObject);
27 procedure OKBtnClick(Sender: TObject);
28 procedure CancelBtnClick(Sender: TObject);
29 procedure PaintBattleOutcome(ca: TCanvas; xm, ym, uix, ToLoc: Integer;
30 Forecast: TBattleForecastEx);
31 private
32 IsoMap: TIsoMap;
33 public
34 uix, ToLoc: Integer;
35 Forecast: TBattleForecastEx;
36 IsSuicideQuery: Boolean;
37 end;
38
39
40implementation
41
42uses
43 Term, ClientTools;
44
45{$R *.lfm}
46
47const
48 Border = 3;
49 MessageLineSpacing = 20;
50
51 DamageColor = $0000E0;
52 FanaticColor = $800080;
53 FirstStrikeColor = $A0A0A0;
54
55procedure TBattleDlg.PaintBattleOutcome(ca: TCanvas; xm, ym, uix, ToLoc: Integer;
56 Forecast: TBattleForecastEx);
57var
58 euix, ADamage, DDamage, StrMax, DamageMax, MaxBar, LAStr, LDStr, LADamage,
59 LDDamage, LABaseDamage, LAAvoidedDamage, LDBaseDamage: Integer;
60 // TerrType: Cardinal;
61 UnitInfo: TUnitInfo;
62 TextSize: TSize;
63 LabelText: string;
64 FirstStrike: Boolean;
65begin
66 MaxBar := 65;
67
68 // TerrType:=MyMap[ToLoc] and fTerrain;
69 GetUnitInfo(ToLoc, euix, UnitInfo);
70
71 FirstStrike := (MyModel[MyUn[uix].mix].Cap[mcFirst] > 0) and
72 (Forecast.DBaseDamage >= UnitInfo.Health);
73 ADamage := MyUn[uix].Health - Forecast.EndHealthAtt;
74 if FirstStrike then
75 ADamage := ADamage + Forecast.ABaseDamage div 2;
76 DDamage := UnitInfo.Health - Forecast.EndHealthDef;
77 if Forecast.AStr > Forecast.DStr then
78 StrMax := Forecast.AStr
79 else
80 StrMax := Forecast.DStr;
81 if ADamage > DDamage then
82 DamageMax := ADamage
83 else
84 DamageMax := DDamage;
85 if Forecast.ABaseDamage > Forecast.DBaseDamage then
86 StrMax := StrMax * DamageMax div Forecast.ABaseDamage
87 else
88 StrMax := StrMax * DamageMax div Forecast.DBaseDamage;
89
90 LAStr := Forecast.AStr * MaxBar div StrMax;
91 LDStr := Forecast.DStr * MaxBar div StrMax;
92 LADamage := ADamage * MaxBar div DamageMax;
93 LABaseDamage := Forecast.ABaseDamage * MaxBar div DamageMax;
94 if FirstStrike then
95 LAAvoidedDamage := LABaseDamage div 2
96 else
97 LAAvoidedDamage := 0;
98 LDDamage := DDamage * MaxBar div DamageMax;
99 LDBaseDamage := Forecast.DBaseDamage * MaxBar div DamageMax;
100
101 DarkGradient(ca, xm - 8 - LAStr, ym - 8, LAStr, 2);
102 VDarkGradient(ca, xm - 8, ym - 8 - LDStr, LDStr, 2);
103 LightGradient(ca, xm + 8, ym - 8, LDBaseDamage, DamageColor);
104 if LDDamage > LDBaseDamage then
105 LightGradient(ca, xm + 8 + LDBaseDamage, ym - 8, LDDamage - LDBaseDamage,
106 FanaticColor);
107 if LAAvoidedDamage > 0 then
108 VLightGradient(ca, xm - 8, ym + 8, LAAvoidedDamage, FirstStrikeColor);
109 VLightGradient(ca, xm - 8, ym + 8 + LAAvoidedDamage,
110 LABaseDamage - LAAvoidedDamage, DamageColor);
111 if LADamage > LABaseDamage then
112 VLightGradient(ca, xm - 8, ym + 8 + LABaseDamage, LADamage - LABaseDamage,
113 FanaticColor);
114 Sprite(ca, HGrSystem, xm - 12, ym - 12, 24, 24, 26, 146);
115
116 LabelText := Format('%d', [Forecast.AStr]);
117 TextSize := ca.TextExtent(LabelText);
118 if TextSize.cx div 2 + 2 > LAStr div 2 then
119 RisedTextOut(ca, xm - 10 - TextSize.cx, ym - (TextSize.cy + 1) div 2,
120 LabelText)
121 else
122 RisedTextOut(ca, xm - 8 - (LAStr + TextSize.cx) div 2,
123 ym - (TextSize.cy + 1) div 2, LabelText);
124
125 LabelText := Format('%d', [Forecast.DStr]);
126 TextSize := ca.TextExtent(LabelText);
127 if TextSize.cy div 2 > LDStr div 2 then
128 RisedTextOut(ca, xm - (TextSize.cx + 1) div 2, ym - 8 - TextSize.cy,
129 LabelText)
130 else
131 RisedTextOut(ca, xm - (TextSize.cx + 1) div 2,
132 ym - 8 - (LDStr + TextSize.cy) div 2, LabelText);
133
134 if Forecast.EndHealthDef <= 0 then
135 begin
136 BitBltCanvas(ca, xm + 9 + LDDamage - 7, ym - 6, 14, 17,
137 HGrSystem.Mask.Canvas, 51, 153, SRCAND);
138 Sprite(ca, HGrSystem, xm + 8 + LDDamage - 7, ym - 7, 14, 17, 51, 153);
139 end;
140 LabelText := Format('%d', [DDamage]);
141 TextSize := ca.TextExtent(LabelText);
142 if TextSize.cx div 2 + 2 > LDDamage div 2 then
143 begin
144 if Forecast.EndHealthDef > 0 then
145 RisedTextOut(ca, xm + 10, ym - (TextSize.cy + 1) div 2, LabelText);
146 end
147 else
148 RisedTextOut(ca, xm + 8 + (LDDamage - TextSize.cx) div 2,
149 ym - (TextSize.cy + 1) div 2, LabelText);
150
151 if Forecast.EndHealthAtt <= 0 then
152 begin
153 BitBltCanvas(ca, xm - 6, ym + 9 + LADamage - 7, 14, 17,
154 HGrSystem.Mask.Canvas, 51, 153, SRCAND);
155 Sprite(ca, HGrSystem, xm - 7, ym + 8 + LADamage - 7, 14, 17, 51, 153);
156 end;
157 LabelText := Format('%d', [MyUn[uix].Health - Forecast.EndHealthAtt]);
158 TextSize := ca.TextExtent(LabelText);
159 if TextSize.cy div 2 > (LADamage - LAAvoidedDamage) div 2 + LAAvoidedDamage
160 then
161 begin
162 if Forecast.EndHealthAtt > 0 then
163 RisedTextOut(ca, xm - (TextSize.cx + 1) div 2, ym + 8 + LAAvoidedDamage,
164 LabelText);
165 end
166 else
167 RisedTextOut(ca, xm - (TextSize.cx + 1) div 2, ym + 8 + LAAvoidedDamage +
168 (LADamage - LAAvoidedDamage - TextSize.cy) div 2, LabelText);
169
170 IsoMap.SetOutput(Buffer);
171 UnshareBitmap(Buffer);
172 BitBltCanvas(Buffer.Canvas, 0, 0, 66, 48, ca, xm + 8 + 4, ym - 8 - 12 - 48);
173 { if TerrType<fForest then
174 Sprite(Buffer,HGrTerrain,0,16,66,32,1+TerrType*(xxt*2+1),1+yyt)
175 else
176 begin
177 Sprite(Buffer,HGrTerrain,0,16,66,32,1+2*(xxt*2+1),1+yyt+2*(yyt*3+1));
178 if (TerrType=fForest) and IsJungle(ToLoc div G.lx) then
179 Sprite(Buffer,HGrTerrain,0,16,66,32,1+7*(xxt*2+1),1+yyt+19*(yyt*3+1))
180 else Sprite(Buffer,HGrTerrain,0,16,66,32,1+7*(xxt*2+1),1+yyt+2*(2+TerrType-fForest)*(yyt*3+1));
181 end; }
182 IsoMap.PaintUnit(1, 0, UnitInfo, 0);
183 BitBltCanvas(ca, xm + 8 + 4, ym - 8 - 12 - 48, 66, 48, Buffer.Canvas, 0, 0);
184
185 UnshareBitmap(Buffer);
186 BitBltCanvas(Buffer.Canvas, 0, 0, 66, 48, ca, xm - 8 - 4 - 66, ym + 8 + 12);
187 MakeUnitInfo(Me, MyUn[uix], UnitInfo);
188 UnitInfo.Flags := UnitInfo.Flags and not unFortified;
189 IsoMap.PaintUnit(1, 0, UnitInfo, 0);
190 BitBltCanvas(ca, xm - 8 - 4 - 66, ym + 8 + 12, 66, 48, Buffer.Canvas, 0, 0);
191end;
192
193procedure TBattleDlg.FormCreate(Sender: TObject);
194begin
195 IsoMap := TIsoMap.Create;
196 OKBtn.Caption := Phrases.Lookup('BTN_YES');
197 CancelBtn.Caption := Phrases.Lookup('BTN_NO');
198 InitButtons;
199end;
200
201procedure TBattleDlg.FormShow(Sender: TObject);
202begin
203 if IsSuicideQuery then
204 begin
205 ClientWidth := 300;
206 ClientHeight := 288;
207 OKBtn.Visible := True;
208 CancelBtn.Visible := True;
209 Left := (Screen.Width - ClientWidth) div 2; // center on screen
210 Top := (Screen.Height - ClientHeight) div 2;
211 end
212 else
213 begin
214 ClientWidth := 178;
215 ClientHeight := 178;
216 OKBtn.Visible := False;
217 CancelBtn.Visible := False;
218 end;
219end;
220
221procedure TBattleDlg.FormPaint(Sender: TObject);
222var
223 ym, cix, P: Integer;
224 S, s1: string;
225begin
226 with Canvas do
227 begin
228 Brush.Color := 0;
229 FillRect(Rect(0, 0, ClientWidth, ClientHeight));
230 Brush.Style := TBrushStyle.bsClear;
231 PaintBackground(Canvas, 3 + Border, 3 + Border,
232 ClientWidth - (6 + 2 * Border), ClientHeight - (6 + 2 * Border),
233 ClientWidth, ClientHeight);
234 end;
235 Frame(Canvas, Border + 1, Border + 1, ClientWidth - (2 + Border),
236 ClientHeight - (2 + Border), MainTexture.ColorBevelLight,
237 MainTexture.ColorBevelShade);
238 Frame(Canvas, 2 + Border, 2 + Border, ClientWidth - (3 + Border),
239 ClientHeight - (3 + Border), MainTexture.ColorBevelLight,
240 MainTexture.ColorBevelShade);
241
242 if IsSuicideQuery then
243 begin
244 Canvas.Font.Assign(UniFont[ftCaption]);
245 S := Phrases.Lookup('TITLE_SUICIDE');
246 RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, S)) div 2,
247 7 + Border, S);
248 Canvas.Font.Assign(UniFont[ftNormal]);
249 S := Phrases.Lookup('SUICIDE');
250 P := Pos('\', S);
251 if P = 0 then
252 RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, S))
253 div 2, 205, S)
254 else
255 begin
256 s1 := Copy(S, 1, P - 1);
257 RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, s1)) div 2,
258 205 - MessageLineSpacing div 2, s1);
259 s1 := Copy(S, P + 1, 255);
260 RisedTextOut(Canvas, (ClientWidth - BiColorTextWidth(Canvas, s1)) div 2,
261 205 + (MessageLineSpacing - MessageLineSpacing div 2), s1);
262 end;
263 ym := 110;
264 end
265 else
266 ym := ClientHeight div 2;
267 Canvas.Font.Assign(UniFont[ftSmall]);
268 PaintBattleOutcome(Canvas, ClientWidth div 2, ym, uix, ToLoc, Forecast);
269
270 for cix := 0 to ControlCount - 1 do
271 if (Controls[cix].Visible) and (Controls[cix] is TButtonBase) then
272 BtnFrame(Canvas, Controls[cix].BoundsRect, MainTexture);
273end;
274
275procedure TBattleDlg.FormDestroy(Sender: TObject);
276begin
277 FreeAndNil(IsoMap);
278end;
279
280procedure TBattleDlg.FormMouseDown(Sender: TObject; Button: TMouseButton;
281 Shift: TShiftState; X, Y: Integer);
282begin
283 if not IsSuicideQuery then
284 Close;
285end;
286
287procedure TBattleDlg.FormDeactivate(Sender: TObject);
288begin
289 if not IsSuicideQuery then
290 Close;
291end;
292
293procedure TBattleDlg.FormKeyDown(Sender: TObject; var Key: Word;
294 Shift: TShiftState);
295begin
296 if not IsSuicideQuery and (Key <> VK_SHIFT) then
297 begin
298 Close;
299 MainScreen.Update;
300 if Key <> VK_ESCAPE then
301 MainScreen.FormKeyDown(Sender, Key, Shift);
302 end;
303end;
304
305procedure TBattleDlg.OKBtnClick(Sender: TObject);
306begin
307 ModalResult := mrOK;
308end;
309
310procedure TBattleDlg.CancelBtnClick(Sender: TObject);
311begin
312 ModalResult := mrCancel;
313end;
314
315end.
Note: See TracBrowser for help on using the repository browser.