source: tags/1.3.1/LocalPlayer/Enhance.pas

Last change on this file was 442, checked in by chronos, 2 years ago
  • Modified: Code cleanup.
File size: 11.5 KB
Line 
1{$INCLUDE Switches.inc}
2unit Enhance;
3
4interface
5
6uses
7 ScreenTools, BaseWin, Protocol, ClientTools, Term, LCLIntf, LCLType, SysUtils,
8 Classes, Graphics, Controls, Forms, IsoEngine, ButtonB, ButtonC, Menus;
9
10type
11
12 { TEnhanceDlg }
13
14 TEnhanceDlg = class(TFramedDlg)
15 MenuItem1: TMenuItem;
16 ToggleBtn: TButtonB;
17 CloseBtn: TButtonB;
18 job1: TButtonC;
19 job2: TButtonC;
20 job4: TButtonC;
21 job5: TButtonC;
22 job7: TButtonC;
23 job3: TButtonC;
24 job6: TButtonC;
25 job9: TButtonC;
26 Popup: TPopupMenu;
27 procedure FormCreate(Sender: TObject);
28 procedure FormDestroy(Sender: TObject);
29 procedure FormPaint(Sender: TObject);
30 procedure FormShow(Sender: TObject);
31 procedure CloseBtnClick(Sender: TObject);
32 procedure ToggleBtnClick(Sender: TObject);
33 procedure TerrClick(Sender: TObject);
34 procedure JobClick(Sender: TObject);
35 procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
36 private
37 NoMap: TIsoMap;
38 public
39 procedure ShowNewContent(NewMode: TWindowMode; TerrType: integer = -1);
40 protected
41 Page: integer;
42 procedure OffscreenPaint; override;
43 end;
44
45var
46 EnhanceDlg: TEnhanceDlg;
47
48
49implementation
50
51uses
52 Help, UKeyBindings;
53
54{$R *.lfm}
55
56procedure TEnhanceDlg.FormCreate(Sender: TObject);
57var
58 TerrType: integer;
59 m: TMenuItem;
60begin
61 inherited;
62 NoMap := TIsoMap.Create;
63 CaptionRight := CloseBtn.Left;
64 CaptionLeft := ToggleBtn.Left + ToggleBtn.Width;
65 InitButtons;
66 HelpContext := 'MACRO';
67 Caption := Phrases.Lookup('TITLE_ENHANCE');
68 ToggleBtn.Hint := Phrases.Lookup('BTN_SELECT');
69
70 for TerrType := fGrass to fMountains do
71 if TerrType <> fJungle then
72 begin
73 m := TMenuItem.Create(Popup);
74 m.RadioItem := true;
75 if TerrType = fGrass then
76 m.Caption := Format(Phrases.Lookup('TWOTERRAINS'),
77 [Phrases.Lookup('TERRAIN', fGrass), Phrases.Lookup('TERRAIN',
78 fGrass + 12)])
79 else if TerrType = fForest then
80 m.Caption := Format(Phrases.Lookup('TWOTERRAINS'),
81 [Phrases.Lookup('TERRAIN', fForest), Phrases.Lookup('TERRAIN',
82 fJungle)])
83 else
84 m.Caption := Phrases.Lookup('TERRAIN', TerrType);
85 m.Tag := TerrType;
86 m.OnClick := TerrClick;
87 Popup.Items.Add(m);
88 end;
89end;
90
91procedure TEnhanceDlg.FormDestroy(Sender: TObject);
92begin
93 FreeAndNil(NoMap);
94end;
95
96procedure TEnhanceDlg.FormPaint(Sender: TObject);
97var
98 i: integer;
99begin
100 inherited;
101 BtnFrame(Canvas, Rect(job1.Left, job1.Top, job7.Left + job7.Width,
102 job1.Top + job1.Height), MainTexture);
103 BtnFrame(Canvas, Rect(job3.Left, job3.Top, job9.Left + job9.Width,
104 job3.Top + job3.Height), MainTexture);
105 for i := 0 to ControlCount - 1 do
106 if Controls[i] is TButtonC then
107 BitBltCanvas(Canvas, Controls[i].Left + 2, Controls[i].Top - 11, 8, 8,
108 HGrSystem.Data.Canvas, 121 + Controls[i].Tag mod 7 * 9,
109 1 + Controls[i].Tag div 7 * 9);
110end;
111
112procedure TEnhanceDlg.FormShow(Sender: TObject);
113begin
114 OffscreenPaint;
115end;
116
117procedure TEnhanceDlg.ShowNewContent(NewMode: TWindowMode; TerrType: integer);
118begin
119 if (TerrType < fGrass) or (TerrType > fMountains) then
120 Page := fGrass
121 else
122 Page := TerrType;
123 inherited ShowNewContent(NewMode);
124end;
125
126procedure TEnhanceDlg.OffscreenPaint;
127var
128 i, stage, TerrType, TileImp, x, EndStage, Cost, LastJob: integer;
129 s: string;
130 Done: Set of jNone .. jTrans;
131 TypeChanged: boolean;
132begin
133 OffscreenUser := self;
134 offscreen.Canvas.Font.Assign(UniFont[ftSmall]);
135 FillOffscreen(0, 0, InnerWidth, InnerHeight);
136
137 EndStage := 0;
138 while (EndStage < 5) and (MyData.EnhancementJobs[Page, EndStage] <> jNone) do
139 inc(EndStage);
140 with NoMap do
141 x := InnerWidth div 2 - xxt - (xxt + 3) * EndStage;
142
143 TerrType := Page;
144 TileImp := 0;
145 Done := [];
146 Cost := 0;
147 for stage := 0 to EndStage do
148 begin
149 if stage > 0 then
150 begin
151 Sprite(offscreen, HGrSystem, x - 10, 66, 14, 14, 80, 1);
152 case MyData.EnhancementJobs[Page, stage - 1] of
153 jRoad:
154 begin
155 inc(Cost, Terrain[TerrType].MoveCost * RoadWork);
156 TileImp := TileImp or fRoad;
157 end;
158 jRR:
159 begin
160 inc(Cost, Terrain[TerrType].MoveCost * RRWork);
161 TileImp := TileImp or fRR;
162 end;
163 jIrr:
164 begin
165 inc(Cost, Terrain[TerrType].IrrClearWork);
166 TileImp := TileImp and not fTerImp or tiIrrigation;
167 end;
168 jFarm:
169 begin
170 inc(Cost, Terrain[TerrType].IrrClearWork * FarmWork);
171 TileImp := TileImp and not fTerImp or tiFarm;
172 end;
173 jMine:
174 begin
175 inc(Cost, Terrain[TerrType].MineAfforestWork);
176 TileImp := TileImp and not fTerImp or tiMine;
177 end;
178 jClear:
179 begin
180 inc(Cost, Terrain[TerrType].IrrClearWork);
181 TerrType := Terrain[TerrType].ClearTerrain;
182 end;
183 jAfforest:
184 begin
185 inc(Cost, Terrain[TerrType].MineAfforestWork);
186 TerrType := Terrain[TerrType].AfforestTerrain;
187 end;
188 jTrans:
189 begin
190 inc(Cost, Terrain[TerrType].TransWork);
191 TerrType := Terrain[TerrType].TransTerrain;
192 end;
193 end;
194 include(Done, MyData.EnhancementJobs[Page, stage - 1]);
195 end;
196
197 with NoMap do begin
198 if TerrType < fForest then
199 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
200 1 + TerrType * (xxt * 2 + 1), 1 + yyt)
201 else
202 begin
203 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
204 1 + 2 * (xxt * 2 + 1), 1 + yyt + 2 * (yyt * 3 + 1));
205 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
206 1 + 7 * (xxt * 2 + 1), 1 + yyt + 2 * (2 + TerrType - fForest) *
207 (yyt * 3 + 1));
208 end;
209 if TileImp and fTerImp = tiFarm then
210 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
211 1 + (xxt * 2 + 1), 1 + yyt + 12 * (yyt * 3 + 1))
212 else if TileImp and fTerImp = tiIrrigation then
213 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1,
214 1 + yyt + 12 * (yyt * 3 + 1));
215 if TileImp and fRR <> 0 then
216 begin
217 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
218 1 + 6 * (xxt * 2 + 1), 1 + yyt + 10 * (yyt * 3 + 1));
219 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
220 1 + 2 * (xxt * 2 + 1), 1 + yyt + 10 * (yyt * 3 + 1));
221 end
222 else if TileImp and fRoad <> 0 then
223 begin
224 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
225 1 + 6 * (xxt * 2 + 1), 1 + yyt + 9 * (yyt * 3 + 1));
226 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
227 1 + 2 * (xxt * 2 + 1), 1 + yyt + 9 * (yyt * 3 + 1));
228 end;
229 if TileImp and fTerImp = tiMine then
230 Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
231 1 + 2 * (xxt * 2 + 1), 1 + yyt + 12 * (yyt * 3 + 1));
232 inc(x, xxt * 2 + 6);
233 end;
234 end;
235
236 for i := 0 to Popup.Items.Count - 1 do
237 if Popup.Items[i].Tag = Page then
238 s := Popup.Items[i].Caption;
239 if Cost > 0 then
240 s := Format(Phrases.Lookup('ENHANCE'), [s, MovementToString(Cost)]);
241 LoweredTextOut(offscreen.Canvas, -1, MainTexture,
242 (InnerWidth - BiColorTextWidth(offscreen.Canvas, s)) div 2, 12, s);
243
244 if EndStage > 0 then
245 LastJob := MyData.EnhancementJobs[Page, EndStage - 1]
246 else
247 LastJob := jNone;
248 if jRoad in Done then
249 job1.ButtonIndex := 3
250 else
251 job1.ButtonIndex := 2;
252 if jRR in Done then
253 job2.ButtonIndex := 3
254 else
255 job2.ButtonIndex := 2;
256 if jIrr in Done then
257 job4.ButtonIndex := 3
258 else
259 job4.ButtonIndex := 2;
260 if jFarm in Done then
261 job5.ButtonIndex := 3
262 else
263 job5.ButtonIndex := 2;
264 if jMine in Done then
265 job7.ButtonIndex := 3
266 else
267 job7.ButtonIndex := 2;
268 if LastJob = jClear then
269 job3.ButtonIndex := 3
270 else
271 job3.ButtonIndex := 2;
272 if LastJob = jAfforest then
273 job6.ButtonIndex := 3
274 else
275 job6.ButtonIndex := 2;
276 if LastJob = jTrans then
277 job9.ButtonIndex := 3
278 else
279 job9.ButtonIndex := 2;
280
281 TypeChanged := LastJob in [jClear, jAfforest, jTrans];
282 job1.Visible := (jRoad in Done) or not TypeChanged;
283 job2.Visible := (jRR in Done) or not TypeChanged;
284 job4.Visible := (jIrr in Done) or not TypeChanged and
285 (Terrain[TerrType].IrrEff > 0);
286 job5.Visible := (jFarm in Done) or not TypeChanged and
287 (Terrain[TerrType].IrrEff > 0);
288 job7.Visible := (jMine in Done) or not TypeChanged and
289 (Terrain[TerrType].MineEff > 0);
290 job3.Visible := not TypeChanged and (Terrain[TerrType].ClearTerrain >= 0) and
291 ((TerrType <> fDesert) or (MyRO.Wonder[woGardens].EffectiveOwner = me)) or
292 (LastJob = jClear);
293 job6.Visible := not TypeChanged and (Terrain[TerrType].AfforestTerrain >= 0)
294 or (LastJob = jAfforest);
295 job9.Visible := not TypeChanged and (Terrain[TerrType].TransTerrain >= 0) or
296 (LastJob = jTrans);
297
298 MarkUsedOffscreen(InnerWidth, InnerHeight);
299end;
300
301procedure TEnhanceDlg.CloseBtnClick(Sender: TObject);
302begin
303 Close;
304end;
305
306procedure TEnhanceDlg.ToggleBtnClick(Sender: TObject);
307var
308 i: integer;
309begin
310 for i := 0 to Popup.Items.Count - 1 do
311 Popup.Items[i].Checked := Popup.Items[i].Tag = Page;
312 Popup.Popup(Left + ToggleBtn.Left, Top + ToggleBtn.Top + ToggleBtn.Height);
313end;
314
315procedure TEnhanceDlg.TerrClick(Sender: TObject);
316begin
317 Page := TComponent(Sender).Tag;
318 SmartUpdateContent;
319end;
320
321procedure TEnhanceDlg.JobClick(Sender: TObject);
322var
323 stage, NewJob: integer;
324 Done: Set of jNone .. jTrans;
325
326 procedure RemoveJob(j: integer);
327 begin // remove job
328 stage := 0;
329 while (stage < 5) and (MyData.EnhancementJobs[Page, stage] <> jNone) do
330 begin
331 if (MyData.EnhancementJobs[Page, stage] = j) or (j = jRoad) and
332 (MyData.EnhancementJobs[Page, stage] = jRR) or (j = jIrr) and
333 (MyData.EnhancementJobs[Page, stage] = jFarm) then
334 begin
335 if stage < 4 then
336 move(MyData.EnhancementJobs[Page, stage + 1],
337 MyData.EnhancementJobs[Page, stage], 4 - stage);
338 MyData.EnhancementJobs[Page, 4] := jNone;
339 end
340 else
341 inc(stage);
342 end;
343 end;
344
345begin
346 NewJob := TButtonC(Sender).Tag;
347 Done := [];
348 stage := 0;
349 while (stage < 5) and (MyData.EnhancementJobs[Page, stage] <> jNone) do
350 begin
351 include(Done, MyData.EnhancementJobs[Page, stage]);
352 inc(stage);
353 end;
354 if NewJob in Done then
355 RemoveJob(NewJob)
356 else
357 begin // add job
358 if NewJob in [jMine, jAfforest] then
359 RemoveJob(jIrr);
360 if NewJob in [jIrr, jFarm, jTrans] then
361 RemoveJob(jMine);
362 if (NewJob = jRR) and not(jRoad in Done) then
363 begin
364 MyData.EnhancementJobs[Page, stage] := jRoad;
365 inc(stage);
366 end;
367 if (NewJob = jFarm) and not(jIrr in Done) then
368 begin
369 MyData.EnhancementJobs[Page, stage] := jIrr;
370 inc(stage);
371 end;
372 MyData.EnhancementJobs[Page, stage] := NewJob;
373 end;
374 SmartUpdateContent;
375end;
376
377procedure TEnhanceDlg.FormKeyDown(Sender: TObject; var Key: Word;
378 Shift: TShiftState);
379var
380 ShortCut: TShortCut;
381begin
382 ShortCut := KeyToShortCut(Key, Shift);
383 if BHelp.Test(ShortCut) then
384 HelpDlg.ShowNewContent(WindowModeMakePersistent(FWindowMode), hkText,
385 HelpDlg.TextIndex('MACRO'))
386end;
387
388end.
Note: See TracBrowser for help on using the repository browser.