source: tags/1.3.1/LocalPlayer/Battle.pas

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