File: Enhance.pas

package info (click to toggle)
c-evo-dh 3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,548 kB
  • sloc: pascal: 57,426; xml: 256; makefile: 114; sh: 4
file content (389 lines) | stat: -rw-r--r-- 11,409 bytes parent folder | download
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
{$INCLUDE Switches.inc}
unit Enhance;

interface

uses
  ScreenTools, BaseWin, Protocol, ClientTools, Term, LCLIntf, LCLType, SysUtils,
  Classes, Graphics, Controls, Forms, IsoEngine, ButtonB, ButtonC, Menus;

type

  { TEnhanceDlg }

  TEnhanceDlg = class(TFramedDlg)
    MenuItem1: TMenuItem;
    ToggleBtn: TButtonB;
    CloseBtn: TButtonB;
    job1: TButtonC2;
    job2: TButtonC2;
    job4: TButtonC2;
    job5: TButtonC2;
    job7: TButtonC2;
    job3: TButtonC2;
    job6: TButtonC2;
    job9: TButtonC2;
    Popup: TPopupMenu;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CloseBtnClick(Sender: TObject);
    procedure ToggleBtnClick(Sender: TObject);
    procedure TerrClick(Sender: TObject);
    procedure JobClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    NoMap: TIsoMap;
  public
    procedure ShowNewContent(NewMode: TWindowMode; TerrType: integer = -1);
  protected
    Page: integer;
    procedure OffscreenPaint; override;
  end;

var
  EnhanceDlg: TEnhanceDlg;


implementation

uses
  Help, UKeyBindings;

{$R *.lfm}

procedure TEnhanceDlg.FormCreate(Sender: TObject);
var
  TerrType: integer;
  m: TMenuItem;
begin
  inherited;
  NoMap := TIsoMap.Create;
  CaptionRight := CloseBtn.Left;
  CaptionLeft := ToggleBtn.Left + ToggleBtn.Width;
  InitButtons;
  HelpContext := 'MACRO';
  Caption := Phrases.Lookup('TITLE_ENHANCE');
  ToggleBtn.Hint := Phrases.Lookup('BTN_SELECT');

  for TerrType := fGrass to fMountains do
    if TerrType <> fJungle then
    begin
      m := TMenuItem.Create(Popup);
      m.RadioItem := true;
      if TerrType = fGrass then
        m.Caption := Format(Phrases.Lookup('TWOTERRAINS'),
          [Phrases.Lookup('TERRAIN', fGrass), Phrases.Lookup('TERRAIN',
          fGrass + 12)])
      else if TerrType = fForest then
        m.Caption := Format(Phrases.Lookup('TWOTERRAINS'),
          [Phrases.Lookup('TERRAIN', fForest), Phrases.Lookup('TERRAIN',
          fJungle)])
      else
        m.Caption := Phrases.Lookup('TERRAIN', TerrType);
      m.Tag := TerrType;
      m.OnClick := TerrClick;
      Popup.Items.Add(m);
    end;
end;

procedure TEnhanceDlg.FormDestroy(Sender: TObject);
begin
  FreeAndNil(NoMap);
end;

procedure TEnhanceDlg.FormPaint(Sender: TObject);
var
  i: integer;
begin
  inherited;
  BtnFrame(Canvas, Rect(job1.Left, job1.Top, job7.Left + job7.Width,
    job1.Top + job1.Height), MainTexture);
  BtnFrame(Canvas, Rect(job3.Left, job3.Top, job9.Left + job9.Width,
    job3.Top + job3.Height), MainTexture);
  for i := 0 to ControlCount - 1 do
    if Controls[i] is TButtonC2 then
      BitBltCanvas(Canvas, Controls[i].Left + 2, Controls[i].Top - 11, 8, 8,
        HGrSystem.Data.Canvas, 121 + Controls[i].Tag mod 7 * 9,
        1 + Controls[i].Tag div 7 * 9);
end;

procedure TEnhanceDlg.FormShow(Sender: TObject);
begin
  OffscreenPaint;
end;

procedure TEnhanceDlg.ShowNewContent(NewMode: TWindowMode; TerrType: integer);
begin
  if (TerrType < fGrass) or (TerrType > fMountains) then
    Page := fGrass
  else
    Page := TerrType;
  inherited ShowNewContent(NewMode);
end;

procedure TEnhanceDlg.OffscreenPaint;
var
  i, stage, TerrType, TileImp, x, EndStage, Cost, LastJob: integer;
  s: string;
  Done: Set of jNone .. jTrans;
  TypeChanged: boolean;
begin
  OffscreenUser := self;
  offscreen.Canvas.Font.Assign(UniFont[ftSmall]);
  FillOffscreen(0, 0, InnerWidth, InnerHeight);

  EndStage := 0;
  while (EndStage < 5) and (MyData.EnhancementJobs[Page, EndStage] <> jNone) do
    inc(EndStage);
  with NoMap do
    x := InnerWidth div 2 - xxt - (xxt + 3) * EndStage;

  TerrType := Page;
  TileImp := 0;
  Done := [];
  Cost := 0;
  for stage := 0 to EndStage do
  begin
    if stage > 0 then
    begin
      Sprite(offscreen, HGrSystem, x - 10, 66, 14, 14, 80, 1);
      case MyData.EnhancementJobs[Page, stage - 1] of
        jRoad:
          begin
            inc(Cost, Terrain[TerrType].MoveCost * RoadWork);
            TileImp := TileImp or fRoad;
          end;
        jRR:
          begin
            inc(Cost, Terrain[TerrType].MoveCost * RRWork);
            TileImp := TileImp or fRR;
          end;
        jIrr:
          begin
            inc(Cost, Terrain[TerrType].IrrClearWork);
            TileImp := TileImp and not fTerImp or tiIrrigation;
          end;
        jFarm:
          begin
            inc(Cost, Terrain[TerrType].IrrClearWork * FarmWork);
            TileImp := TileImp and not fTerImp or tiFarm;
          end;
        jMine:
          begin
            inc(Cost, Terrain[TerrType].MineAfforestWork);
            TileImp := TileImp and not fTerImp or tiMine;
          end;
        jClear:
          begin
            inc(Cost, Terrain[TerrType].IrrClearWork);
            TerrType := Terrain[TerrType].ClearTerrain;
          end;
        jAfforest:
          begin
            inc(Cost, Terrain[TerrType].MineAfforestWork);
            TerrType := Terrain[TerrType].AfforestTerrain;
          end;
        jTrans:
          begin
            inc(Cost, Terrain[TerrType].TransWork);
            TerrType := Terrain[TerrType].TransTerrain;
          end;
      end;
      include(Done, MyData.EnhancementJobs[Page, stage - 1]);
    end;

    with NoMap do begin
      if TerrType < fForest then
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
          1 + TerrType * (xxt * 2 + 1), 1 + yyt)
      else
      begin
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
          1 + 2 * (xxt * 2 + 1), 1 + yyt + 2 * (yyt * 3 + 1));
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
          1 + 7 * (xxt * 2 + 1), 1 + yyt + 2 * (2 + TerrType - fForest) *
          (yyt * 3 + 1));
      end;
      if TileImp and fTerImp = tiFarm then
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
          1 + (xxt * 2 + 1), 1 + yyt + 12 * (yyt * 3 + 1))
      else if TileImp and fTerImp = tiIrrigation then
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1,
          1 + yyt + 12 * (yyt * 3 + 1));
      if TileImp and fRR <> 0 then
      begin
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
          1 + 6 * (xxt * 2 + 1), 1 + yyt + 10 * (yyt * 3 + 1));
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
          1 + 2 * (xxt * 2 + 1), 1 + yyt + 10 * (yyt * 3 + 1));
      end
      else if TileImp and fRoad <> 0 then
      begin
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
          1 + 6 * (xxt * 2 + 1), 1 + yyt + 9 * (yyt * 3 + 1));
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
          1 + 2 * (xxt * 2 + 1), 1 + yyt + 9 * (yyt * 3 + 1));
      end;
      if TileImp and fTerImp = tiMine then
        Sprite(offscreen, HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2,
          1 + 2 * (xxt * 2 + 1), 1 + yyt + 12 * (yyt * 3 + 1));
      inc(x, xxt * 2 + 6);
    end;
  end;

  s := '';
  for i := 0 to Popup.Items.Count - 1 do
    if Popup.Items[i].Tag = Page then
      s := Popup.Items[i].Caption;
  if Cost > 0 then
    s := Format(Phrases.Lookup('ENHANCE'), [s, MovementToString(Cost)]);
  LoweredTextOut(offscreen.Canvas, -1, MainTexture,
    (InnerWidth - BiColorTextWidth(offscreen.Canvas, s)) div 2, 12, s);

  if EndStage > 0 then
    LastJob := MyData.EnhancementJobs[Page, EndStage - 1]
  else
    LastJob := jNone;
  if jRoad in Done then
    job1.ButtonIndex := 3
  else
    job1.ButtonIndex := 2;
  if jRR in Done then
    job2.ButtonIndex := 3
  else
    job2.ButtonIndex := 2;
  if jIrr in Done then
    job4.ButtonIndex := 3
  else
    job4.ButtonIndex := 2;
  if jFarm in Done then
    job5.ButtonIndex := 3
  else
    job5.ButtonIndex := 2;
  if jMine in Done then
    job7.ButtonIndex := 3
  else
    job7.ButtonIndex := 2;
  if LastJob = jClear then
    job3.ButtonIndex := 3
  else
    job3.ButtonIndex := 2;
  if LastJob = jAfforest then
    job6.ButtonIndex := 3
  else
    job6.ButtonIndex := 2;
  if LastJob = jTrans then
    job9.ButtonIndex := 3
  else
    job9.ButtonIndex := 2;

  TypeChanged := LastJob in [jClear, jAfforest, jTrans];
  job1.Visible := (jRoad in Done) or not TypeChanged;
  job2.Visible := (jRR in Done) or not TypeChanged;
  job4.Visible := (jIrr in Done) or not TypeChanged and
    (Terrain[TerrType].IrrEff > 0);
  job5.Visible := (jFarm in Done) or not TypeChanged and
    (Terrain[TerrType].IrrEff > 0);
  job7.Visible := (jMine in Done) or not TypeChanged and
    (Terrain[TerrType].MineEff > 0);
  job3.Visible := not TypeChanged and (Terrain[TerrType].ClearTerrain >= 0) and
    ((TerrType <> fDesert) or (MyRO.Wonder[woGardens].EffectiveOwner = me)) or
    (LastJob = jClear);
  job6.Visible := not TypeChanged and (Terrain[TerrType].AfforestTerrain >= 0)
    or (LastJob = jAfforest);
  job9.Visible := not TypeChanged and (Terrain[TerrType].TransTerrain >= 0) or
    (LastJob = jTrans);

  MarkUsedOffscreen(InnerWidth, InnerHeight);
end;

procedure TEnhanceDlg.CloseBtnClick(Sender: TObject);
begin
  Close;
end;

procedure TEnhanceDlg.ToggleBtnClick(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to Popup.Items.Count - 1 do
    Popup.Items[i].Checked := Popup.Items[i].Tag = Page;
  Popup.Popup(Left + ToggleBtn.Left, Top + ToggleBtn.Top + ToggleBtn.Height);
end;

procedure TEnhanceDlg.TerrClick(Sender: TObject);
begin
  Page := TComponent(Sender).Tag;
  SmartUpdateContent;
end;

procedure TEnhanceDlg.JobClick(Sender: TObject);
var
  stage, NewJob: integer;
  Done: Set of jNone .. jTrans;

  procedure RemoveJob(j: integer);
  begin // remove job
    stage := 0;
    while (stage < 5) and (MyData.EnhancementJobs[Page, stage] <> jNone) do
    begin
      if (MyData.EnhancementJobs[Page, stage] = j) or (j = jRoad) and
        (MyData.EnhancementJobs[Page, stage] = jRR) or (j = jIrr) and
        (MyData.EnhancementJobs[Page, stage] = jFarm) then
      begin
        if stage < 4 then
          move(MyData.EnhancementJobs[Page, stage + 1],
            MyData.EnhancementJobs[Page, stage], 4 - stage);
        MyData.EnhancementJobs[Page, 4] := jNone;
      end
      else
        inc(stage);
    end;
  end;

begin
  NewJob := TButtonC2(Sender).Tag;
  Done := [];
  stage := 0;
  while (stage < 5) and (MyData.EnhancementJobs[Page, stage] <> jNone) do
  begin
    include(Done, MyData.EnhancementJobs[Page, stage]);
    inc(stage);
  end;
  if NewJob in Done then
    RemoveJob(NewJob)
  else
  begin // add job
    if NewJob in [jMine, jAfforest] then
      RemoveJob(jIrr);
    if NewJob in [jIrr, jFarm, jTrans] then
      RemoveJob(jMine);
    if (NewJob = jRR) and not(jRoad in Done) then
    begin
      MyData.EnhancementJobs[Page, stage] := jRoad;
      inc(stage);
    end;
    if (NewJob = jFarm) and not(jIrr in Done) then
    begin
      MyData.EnhancementJobs[Page, stage] := jIrr;
      inc(stage);
    end;
    MyData.EnhancementJobs[Page, stage] := NewJob;
  end;
  SmartUpdateContent;
end;

procedure TEnhanceDlg.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  ShortCut: TShortCut;
begin
  ShortCut := KeyToShortCut(Key, Shift);
  if BHelp.Test(ShortCut) then
    HelpDlg.ShowNewContent(WindowModeMakePersistent(FWindowMode), hkText,
      HelpDlg.TextIndex('MACRO'))
end;

end.