source: tags/1.3.0/LocalPlayer/Enhance.pas

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