Revision 119:66352054ce4d UOLib/UStatics.pas

b/UOLib/UStatics.pas
1
(*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License, Version 1.0 only
6
 * (the "License").  You may not use this file except in compliance
7
 * with the License.
8
 *
9
 * You can obtain a copy of the license at
10
 * http://www.opensource.org/licenses/cddl1.php.
11
 * See the License for the specific language governing permissions
12
 * and limitations under the License.
13
 *
14
 * When distributing Covered Code, include this CDDL HEADER in each
15
 * file and include the License file at
16
 * http://www.opensource.org/licenses/cddl1.php.  If applicable,
17
 * add the following below this CDDL HEADER, with the fields enclosed
18
 * by brackets "[]" replaced with your own identifying * information:
19
 *      Portions Copyright [yyyy] [name of copyright owner]
20
 *
21
 * CDDL HEADER END
22
 *
23
 *
24
 *      Portions Copyright 2009 Andreas Schneider
25
 *)
26
unit UStatics;
27

  
28
{$mode objfpc}{$H+}
29

  
30
interface
31

  
32
uses
33
  SysUtils, Classes, fgl, UGenericIndex, UWorldItem, UTiledata;
34

  
35
type
36
  { TStaticItem }
37

  
38
  TStaticItem = class(TWorldItem)
39
    constructor Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
40
      ABlockY: Word); overload;
41
    constructor Create(AOwner: TWorldBlock; AData: TStream); overload;
42
  protected
43
    { Members }
44
    FHue: Word;
45
    FOrgHue: Word;
46

  
47
    { Methods }
48
    function HasChanged: Boolean; override;
49
    procedure SetHue(AHue: Word);
50
  public
51
    { Fields }
52
    property Hue: Word read FHue write SetHue;
53

  
54
    { Methods }
55
    function Clone: TStaticItem; override;
56
    function GetSize: Integer; override;
57
    procedure InitOriginalState; override;
58
    procedure UpdatePriorities(ATileData: TStaticTiledata; ASolver: Integer);
59
    procedure Write(AData: TStream); override;
60
  end;
61

  
62
  TStaticItemList = specialize TFPGObjectList<TStaticItem>;
63

  
64
  { TStaticBlock}
65

  
66
  TStaticBlock = class(TWorldBlock)
67
    constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word);
68
      overload;
69
    constructor Create(AData: TStream; AIndex: TGenericIndex); overload;
70
    destructor Destroy; override;
71
  protected
72
    { Members }
73
    FItems: TStaticItemList;
74
  public
75
    { Fields }
76
    property Items: TStaticItemList read FItems write FItems;
77

  
78
    { Methods }
79
    function Clone: TStaticBlock; override;
80
    function GetSize: Integer; override;
81
    procedure ReverseWrite(AData: TStream);
82
    procedure Sort;
83
    procedure Write(AData: TStream); override;
84
  end;
85

  
86
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
87

  
88
implementation
89

  
90
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
91
begin
92
  Result := CompareWorldItems(AStatic1, AStatic2);
93
end;
94

  
95
{ TStaticItem }
96

  
97
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
98
  ABlockY: Word);
99
var
100
  iX, iY: Byte;
101
begin
102
  inherited Create(AOwner);
103

  
104
  if AData <> nil then
105
  begin
106
    AData.Read(FTileID, SizeOf(SmallInt));
107
    AData.Read(iX, SizeOf(Byte));
108
    AData.Read(iY, SizeOf(Byte));
109
    AData.Read(FZ, SizeOf(ShortInt));
110
    AData.Read(FHue, SizeOf(SmallInt));
111

  
112
    FX := ABlockX * 8 + iX;
113
    FY := ABlockY * 8 + iY;
114
  end;
115

  
116
  InitOriginalState;
117
end;
118

  
119
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream);
120
begin
121
  Create(AOwner, AData, 0, 0);
122
end;
123

  
124
function TStaticItem.HasChanged: Boolean;
125
begin
126
  Result := (FHue <> FOrgHue) or inherited HasChanged;
127
end;
128

  
129
procedure TStaticItem.SetHue(AHue: Word);
130
begin
131
  FHue := AHue;
132
  DoChanged;
133
end;
134

  
135
function TStaticItem.Clone: TStaticItem;
136
begin
137
  Result := TStaticItem.Create(nil, nil);
138
  Result.FTileID := FTileID;
139
  Result.FX := FX;
140
  Result.FY := FY;
141
  Result.FZ := FZ;
142
  Result.FHue := FHue;
143
end;
144

  
145
function TStaticItem.GetSize: Integer;
146
begin
147
  Result := 7;
148
end;
149

  
150
procedure TStaticItem.InitOriginalState;
151
begin
152
  FOrgHue := FHue;
153
  inherited InitOriginalState;
154
end;
155

  
156
procedure TStaticItem.UpdatePriorities(ATileData: TStaticTiledata;
157
  ASolver: Integer);
158
begin
159
  FPriorityBonus := 0;
160
  if not (tdfBackground in ATileData.Flags) then
161
    Inc(FPriorityBonus);
162
  if ATileData.Height > 0 then
163
    Inc(FPriorityBonus);
164
  FPriority := Z + FPriorityBonus;
165
  FPrioritySolver := ASolver;
166
end;
167

  
168
procedure TStaticItem.Write(AData: TStream);
169
var
170
  iX, iY: Byte;
171
begin
172
  iX := FX mod 8;
173
  iY := FY mod 8;
174

  
175
  AData.Write(FTileID, SizeOf(SmallInt));
176
  AData.Write(iX, SizeOf(Byte));
177
  AData.Write(iY, SizeOf(Byte));
178
  AData.Write(FZ, SizeOf(ShortInt));
179
  AData.Write(FHue, SizeOf(SmallInt));
180
end;
181

  
182
{ TStaticBlock }
183

  
184
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex;
185
  AX, AY: Word);
186
var
187
  i: Integer;
188
  block: TMemoryStream;
189
begin
190
  inherited Create;
191
  FX := AX;
192
  FY := AY;
193

  
194
  FItems := TStaticItemList.Create(True);
195
  if (AData <> nil) and (AIndex.Lookup > 0) and (AIndex.Size > 0) then
196
  begin
197
    AData.Position := AIndex.Lookup;
198
    block := TMemoryStream.Create;
199
    block.CopyFrom(AData, AIndex.Size);
200
    block.Position := 0;
201
    for i := 1 to (AIndex.Size div 7) do
202
      FItems.Add(TStaticItem.Create(Self, block, AX, AY));
203
    block.Free;
204
  end;
205
end;
206

  
207
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex);
208
begin
209
  Create(AData, AIndex, 0, 0);
210
end;
211

  
212
destructor TStaticBlock.Destroy;
213
var
214
  i: Integer;
215
begin
216
  FreeAndNil(FItems);
217
  inherited;
218
end;
219

  
220
function TStaticBlock.Clone: TStaticBlock;
221
var
222
  i: Integer;
223
begin
224
  Result := TStaticBlock.Create(nil, nil, FX, FY);
225
  for i := 0 to FItems.Count - 1 do
226
    Result.FItems.Add(FItems.Items[i].Clone);
227
end;
228

  
229
function TStaticBlock.GetSize: Integer;
230
begin
231
  Result := FItems.Count * 7;
232
end;
233

  
234
procedure TStaticBlock.ReverseWrite(AData: TStream);
235
var
236
  i: Integer;
237
begin
238
  for i := FItems.Count - 1 downto 0 do
239
    FItems[i].Write(AData);
240
end;
241

  
242
procedure TStaticBlock.Sort;
243
begin
244
  FItems.Sort(@CompareStaticItems);
245
end;
246

  
247
procedure TStaticBlock.Write(AData: TStream);
248
var
249
  i: Integer;
250
begin
251
  for i := 0 to FItems.Count - 1 do
252
    FItems[i].Write(AData);
253
end;
254

  
255
end.
256

  
1
(*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License, Version 1.0 only
6
 * (the "License").  You may not use this file except in compliance
7
 * with the License.
8
 *
9
 * You can obtain a copy of the license at
10
 * http://www.opensource.org/licenses/cddl1.php.
11
 * See the License for the specific language governing permissions
12
 * and limitations under the License.
13
 *
14
 * When distributing Covered Code, include this CDDL HEADER in each
15
 * file and include the License file at
16
 * http://www.opensource.org/licenses/cddl1.php.  If applicable,
17
 * add the following below this CDDL HEADER, with the fields enclosed
18
 * by brackets "[]" replaced with your own identifying * information:
19
 *      Portions Copyright [yyyy] [name of copyright owner]
20
 *
21
 * CDDL HEADER END
22
 *
23
 *
24
 *      Portions Copyright 2009 Andreas Schneider
25
 *)
26
unit UStatics;
27

  
28
{$mode objfpc}{$H+}
29

  
30
interface
31

  
32
uses
33
  SysUtils, Classes, fgl, UGenericIndex, UWorldItem, UTiledata;
34

  
35
type
36
  { TStaticItem }
37

  
38
  TStaticItem = class(TWorldItem)
39
    constructor Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
40
      ABlockY: Word); overload;
41
    constructor Create(AOwner: TWorldBlock; AData: TStream); overload;
42
  protected
43
    { Members }
44
    FHue: Word;
45
    FOrgHue: Word;
46

  
47
    { Methods }
48
    function HasChanged: Boolean; override;
49
    procedure SetHue(AHue: Word);
50
  public
51
    { Fields }
52
    property Hue: Word read FHue write SetHue;
53

  
54
    { Methods }
55
    function Clone: TStaticItem; override;
56
    function GetSize: Integer; override;
57
    procedure InitOriginalState; override;
58
    procedure UpdatePriorities(ATileData: TStaticTiledata; ASolver: Integer);
59
    procedure Write(AData: TStream); override;
60
  end;
61

  
62
  TStaticItemList = specialize TFPGObjectList<TStaticItem>;
63

  
64
  { TStaticBlock}
65

  
66
  TStaticBlock = class(TWorldBlock)
67
    constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word);
68
      overload;
69
    constructor Create(AData: TStream; AIndex: TGenericIndex); overload;
70
    destructor Destroy; override;
71
  protected
72
    { Members }
73
    FItems: TStaticItemList;
74
  public
75
    { Fields }
76
    property Items: TStaticItemList read FItems write FItems;
77

  
78
    { Methods }
79
    function Clone: TStaticBlock; override;
80
    function GetSize: Integer; override;
81
    procedure ReverseWrite(AData: TStream);
82
    procedure Sort;
83
    procedure Write(AData: TStream); override;
84
  end;
85

  
86
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
87

  
88
implementation
89

  
90
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
91
begin
92
  Result := CompareWorldItems(AStatic1, AStatic2);
93
end;
94

  
95
{ TStaticItem }
96

  
97
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
98
  ABlockY: Word);
99
var
100
  iX, iY: Byte;
101
begin
102
  inherited Create(AOwner);
103

  
104
  if AData <> nil then
105
  begin
106
    AData.Read(FTileID, SizeOf(SmallInt));
107
    AData.Read(iX, SizeOf(Byte));
108
    AData.Read(iY, SizeOf(Byte));
109
    AData.Read(FZ, SizeOf(ShortInt));
110
    AData.Read(FHue, SizeOf(SmallInt));
111

  
112
    FX := ABlockX * 8 + iX;
113
    FY := ABlockY * 8 + iY;
114
  end;
115

  
116
  InitOriginalState;
117
end;
118

  
119
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream);
120
begin
121
  Create(AOwner, AData, 0, 0);
122
end;
123

  
124
function TStaticItem.HasChanged: Boolean;
125
begin
126
  Result := (FHue <> FOrgHue) or inherited HasChanged;
127
end;
128

  
129
procedure TStaticItem.SetHue(AHue: Word);
130
begin
131
  FHue := AHue;
132
  DoChanged;
133
end;
134

  
135
function TStaticItem.Clone: TStaticItem;
136
begin
137
  Result := TStaticItem.Create(nil, nil);
138
  Result.FTileID := FTileID;
139
  Result.FX := FX;
140
  Result.FY := FY;
141
  Result.FZ := FZ;
142
  Result.FHue := FHue;
143
end;
144

  
145
function TStaticItem.GetSize: Integer;
146
begin
147
  Result := 7;
148
end;
149

  
150
procedure TStaticItem.InitOriginalState;
151
begin
152
  FOrgHue := FHue;
153
  inherited InitOriginalState;
154
end;
155

  
156
procedure TStaticItem.UpdatePriorities(ATileData: TStaticTiledata;
157
  ASolver: Integer);
158
begin
159
  FPriorityBonus := 0;
160
  if not (tdfBackground in ATileData.Flags) then
161
    Inc(FPriorityBonus);
162
  if ATileData.Height > 0 then
163
    Inc(FPriorityBonus);
164
  FPriority := Z + FPriorityBonus;
165
  FPrioritySolver := ASolver;
166
end;
167

  
168
procedure TStaticItem.Write(AData: TStream);
169
var
170
  iX, iY: Byte;
171
begin
172
  iX := FX mod 8;
173
  iY := FY mod 8;
174

  
175
  AData.Write(FTileID, SizeOf(SmallInt));
176
  AData.Write(iX, SizeOf(Byte));
177
  AData.Write(iY, SizeOf(Byte));
178
  AData.Write(FZ, SizeOf(ShortInt));
179
  AData.Write(FHue, SizeOf(SmallInt));
180
end;
181

  
182
{ TStaticBlock }
183

  
184
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex;
185
  AX, AY: Word);
186
var
187
  i: Integer;
188
  block: TMemoryStream;
189
begin
190
  inherited Create;
191
  FX := AX;
192
  FY := AY;
193

  
194
  FItems := TStaticItemList.Create(True);
195
  if (AData <> nil) and (AIndex.Lookup > 0) and (AIndex.Size > 0) then
196
  begin
197
    AData.Position := AIndex.Lookup;
198
    block := TMemoryStream.Create;
199
    block.CopyFrom(AData, AIndex.Size);
200
    block.Position := 0;
201
    for i := 1 to (AIndex.Size div 7) do
202
      FItems.Add(TStaticItem.Create(Self, block, AX, AY));
203
    block.Free;
204
  end;
205
end;
206

  
207
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex);
208
begin
209
  Create(AData, AIndex, 0, 0);
210
end;
211

  
212
destructor TStaticBlock.Destroy;
213
begin
214
  FreeAndNil(FItems);
215
  inherited;
216
end;
217

  
218
function TStaticBlock.Clone: TStaticBlock;
219
var
220
  i: Integer;
221
begin
222
  Result := TStaticBlock.Create(nil, nil, FX, FY);
223
  for i := 0 to FItems.Count - 1 do
224
    Result.FItems.Add(FItems.Items[i].Clone);
225
end;
226

  
227
function TStaticBlock.GetSize: Integer;
228
begin
229
  Result := FItems.Count * 7;
230
end;
231

  
232
procedure TStaticBlock.ReverseWrite(AData: TStream);
233
var
234
  i: Integer;
235
begin
236
  for i := FItems.Count - 1 downto 0 do
237
    FItems[i].Write(AData);
238
end;
239

  
240
procedure TStaticBlock.Sort;
241
begin
242
  FItems.Sort(@CompareStaticItems);
243
end;
244

  
245
procedure TStaticBlock.Write(AData: TStream);
246
var
247
  i: Integer;
248
begin
249
  for i := 0 to FItems.Count - 1 do
250
    FItems[i].Write(AData);
251
end;
252

  
253
end.
254

  

Also available in: Unified diff