Statistics
| Branch: | Tag: | Revision:

root / UOLib / UStatics.pas @ 157:0b95089e72d4

History | View | Annotate | Download (5.5 kB)

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
46
    { Methods }
47
    procedure SetHue(AValue: Word);
48
  public
49
    { Fields }
50
    property Hue: Word read FHue write SetHue;
51
52
    { Methods }
53
    function Clone: TStaticItem; override;
54
    function GetSize: Integer; override;
55
    procedure UpdatePriorities(ATileData: TStaticTiledata; ASolver: Integer);
56
    procedure Write(AData: TStream); override;
57
  end;
58
59
  TStaticItemList = specialize TFPGObjectList<TStaticItem>;
60
61
  { TStaticBlock}
62
63
  TStaticBlock = class(TWorldBlock)
64
    constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word);
65
      overload;
66
    constructor Create(AData: TStream; AIndex: TGenericIndex); overload;
67
    destructor Destroy; override;
68
  protected
69
    { Members }
70
    FItems: TStaticItemList;
71
  public
72
    { Fields }
73
    property Items: TStaticItemList read FItems write FItems;
74
75
    { Methods }
76
    function Clone: TStaticBlock; override;
77
    function GetSize: Integer; override;
78
    procedure ReverseWrite(AData: TStream);
79
    procedure Sort;
80
    procedure Write(AData: TStream); override;
81
  end;
82
83
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
84
85
implementation
86
87
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
88
begin
89
  Result := CompareWorldItems(AStatic1, AStatic2);
90
end;
91
92
{ TStaticItem }
93
94
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
95
  ABlockY: Word);
96
var
97
  iX, iY: Byte;
98
begin
99
  inherited Create(AOwner);
100
101
  if AData <> nil then
102
  begin
103
    AData.Read(FTileID, SizeOf(SmallInt));
104
    AData.Read(iX, SizeOf(Byte));
105
    AData.Read(iY, SizeOf(Byte));
106
    AData.Read(FZ, SizeOf(ShortInt));
107
    AData.Read(FHue, SizeOf(SmallInt));
108
109
    FX := ABlockX * 8 + iX;
110
    FY := ABlockY * 8 + iY;
111
  end;
112
end;
113
114
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream);
115
begin
116
  Create(AOwner, AData, 0, 0);
117
end;
118
119
procedure TStaticItem.SetHue(AValue: Word);
120
begin
121
  if FHue = AValue then
122
    Exit;
123
124
  FHue := AValue;
125
  DoChanged;
126
end;
127
128
function TStaticItem.Clone: TStaticItem;
129
begin
130
  Result := TStaticItem.Create(nil, nil);
131
  Result.FTileID := FTileID;
132
  Result.FX := FX;
133
  Result.FY := FY;
134
  Result.FZ := FZ;
135
  Result.FHue := FHue;
136
end;
137
138
function TStaticItem.GetSize: Integer;
139
begin
140
  Result := 7;
141
end;
142
143
procedure TStaticItem.UpdatePriorities(ATileData: TStaticTiledata;
144
  ASolver: Integer);
145
begin
146
  FPriorityBonus := 0;
147
  if not (tdfBackground in ATileData.Flags) then
148
    Inc(FPriorityBonus);
149
  if ATileData.Height > 0 then
150
    Inc(FPriorityBonus);
151
  FPriority := Z + FPriorityBonus;
152
  FPrioritySolver := ASolver;
153
end;
154
155
procedure TStaticItem.Write(AData: TStream);
156
var
157
  iX, iY: Byte;
158
begin
159
  iX := FX mod 8;
160
  iY := FY mod 8;
161
162
  AData.Write(FTileID, SizeOf(SmallInt));
163
  AData.Write(iX, SizeOf(Byte));
164
  AData.Write(iY, SizeOf(Byte));
165
  AData.Write(FZ, SizeOf(ShortInt));
166
  AData.Write(FHue, SizeOf(SmallInt));
167
end;
168
169
{ TStaticBlock }
170
171
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex;
172
  AX, AY: Word);
173
var
174
  i: Integer;
175
  block: TMemoryStream;
176
begin
177
  inherited Create;
178
  FX := AX;
179
  FY := AY;
180
181
  FItems := TStaticItemList.Create(True);
182
  if (AData <> nil) and (AIndex.Lookup > 0) and (AIndex.Size > 0) then
183
  begin
184
    AData.Position := AIndex.Lookup;
185
    block := TMemoryStream.Create;
186
    block.CopyFrom(AData, AIndex.Size);
187
    block.Position := 0;
188
    for i := 1 to (AIndex.Size div 7) do
189
      FItems.Add(TStaticItem.Create(Self, block, AX, AY));
190
    block.Free;
191
  end;
192
  FChanged := False;
193
end;
194
195
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex);
196
begin
197
  Create(AData, AIndex, 0, 0);
198
end;
199
200
destructor TStaticBlock.Destroy;
201
begin
202
  FreeAndNil(FItems);
203
  inherited;
204
end;
205
206
function TStaticBlock.Clone: TStaticBlock;
207
var
208
  i: Integer;
209
begin
210
  Result := TStaticBlock.Create(nil, nil, FX, FY);
211
  for i := 0 to FItems.Count - 1 do
212
    Result.FItems.Add(FItems.Items[i].Clone);
213
end;
214
215
function TStaticBlock.GetSize: Integer;
216
begin
217
  Result := FItems.Count * 7;
218
end;
219
220
procedure TStaticBlock.ReverseWrite(AData: TStream);
221
var
222
  i: Integer;
223
begin
224
  for i := FItems.Count - 1 downto 0 do
225
    FItems[i].Write(AData);
226
end;
227
228
procedure TStaticBlock.Sort;
229
begin
230
  FItems.Sort(@CompareStaticItems);
231
end;
232
233
procedure TStaticBlock.Write(AData: TStream);
234
var
235
  i: Integer;
236
begin
237
  for i := 0 to FItems.Count - 1 do
238
    FItems[i].Write(AData);
239
end;
240
241
end.
242