2009-03-09 17:24:53 +01:00
|
|
|
(*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License, Version 1.0 only
|
|
|
|
* (the "License"). You may not use this file except in compliance
|
|
|
|
* with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at
|
|
|
|
* http://www.opensource.org/licenses/cddl1.php.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at
|
|
|
|
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
|
|
|
* add the following below this CDDL HEADER, with the fields enclosed
|
|
|
|
* by brackets "[]" replaced with your own identifying * information:
|
|
|
|
* Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*
|
|
|
|
*
|
2009-08-02 13:45:26 +02:00
|
|
|
* Portions Copyright 2009 Andreas Schneider
|
2009-03-09 17:24:53 +01:00
|
|
|
*)
|
|
|
|
unit UStatics;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2009-12-02 19:14:00 +01:00
|
|
|
SysUtils, Classes, fgl, UGenericIndex, UWorldItem, UTiledata;
|
2009-03-09 17:24:53 +01:00
|
|
|
|
|
|
|
type
|
2009-08-02 13:45:26 +02:00
|
|
|
{ TStaticItem }
|
|
|
|
|
2009-03-09 17:24:53 +01:00
|
|
|
TStaticItem = class(TWorldItem)
|
2009-12-03 00:04:19 +01:00
|
|
|
constructor Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
|
|
|
|
ABlockY: Word); overload;
|
2009-03-09 17:24:53 +01:00
|
|
|
constructor Create(AOwner: TWorldBlock; AData: TStream); overload;
|
|
|
|
protected
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Members }
|
|
|
|
FHue: Word;
|
|
|
|
FOrgHue: Word;
|
2009-08-02 19:08:56 +02:00
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Methods }
|
2009-03-09 17:24:53 +01:00
|
|
|
function HasChanged: Boolean; override;
|
2009-08-02 13:45:26 +02:00
|
|
|
procedure SetHue(AHue: Word);
|
2009-03-09 17:24:53 +01:00
|
|
|
public
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Fields }
|
2009-03-09 17:24:53 +01:00
|
|
|
property Hue: Word read FHue write SetHue;
|
2009-08-02 19:08:56 +02:00
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Methods }
|
|
|
|
function Clone: TStaticItem; override;
|
|
|
|
function GetSize: Integer; override;
|
|
|
|
procedure InitOriginalState; override;
|
2009-08-02 19:08:56 +02:00
|
|
|
procedure UpdatePriorities(ATileData: TStaticTiledata; ASolver: Integer);
|
2009-08-02 13:45:26 +02:00
|
|
|
procedure Write(AData: TStream); override;
|
2009-03-09 17:24:53 +01:00
|
|
|
end;
|
2009-08-02 13:45:26 +02:00
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
TStaticItemList = specialize TFPGObjectList<TStaticItem>;
|
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
{ TStaticBlock}
|
|
|
|
|
2009-03-09 17:24:53 +01:00
|
|
|
TStaticBlock = class(TWorldBlock)
|
2009-12-03 00:04:19 +01:00
|
|
|
constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word);
|
|
|
|
overload;
|
2009-03-09 17:24:53 +01:00
|
|
|
constructor Create(AData: TStream; AIndex: TGenericIndex); overload;
|
|
|
|
destructor Destroy; override;
|
|
|
|
protected
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Members }
|
2009-12-02 19:14:00 +01:00
|
|
|
FItems: TStaticItemList;
|
2009-03-09 17:24:53 +01:00
|
|
|
public
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Fields }
|
2009-12-02 19:14:00 +01:00
|
|
|
property Items: TStaticItemList read FItems write FItems;
|
2009-08-02 19:08:56 +02:00
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Methods }
|
|
|
|
function Clone: TStaticBlock; override;
|
|
|
|
function GetSize: Integer; override;
|
|
|
|
procedure ReverseWrite(AData: TStream);
|
|
|
|
procedure Sort;
|
|
|
|
procedure Write(AData: TStream); override;
|
2009-03-09 17:24:53 +01:00
|
|
|
end;
|
2009-08-02 13:45:26 +02:00
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
|
|
|
|
|
2009-03-09 17:24:53 +01:00
|
|
|
implementation
|
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
function CompareStaticItems(const AStatic1, AStatic2: TStaticItem): Integer;
|
|
|
|
begin
|
|
|
|
Result := CompareWorldItems(AStatic1, AStatic2);
|
|
|
|
end;
|
|
|
|
|
2009-03-09 17:24:53 +01:00
|
|
|
{ TStaticItem }
|
|
|
|
|
2009-12-03 00:04:19 +01:00
|
|
|
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream; ABlockX,
|
|
|
|
ABlockY: Word);
|
2009-03-09 17:24:53 +01:00
|
|
|
var
|
|
|
|
iX, iY: Byte;
|
|
|
|
begin
|
|
|
|
inherited Create(AOwner);
|
2009-12-03 00:04:19 +01:00
|
|
|
|
|
|
|
if AData <> nil then
|
2009-03-09 17:24:53 +01:00
|
|
|
begin
|
|
|
|
AData.Read(FTileID, SizeOf(SmallInt));
|
|
|
|
AData.Read(iX, SizeOf(Byte));
|
|
|
|
AData.Read(iY, SizeOf(Byte));
|
|
|
|
AData.Read(FZ, SizeOf(ShortInt));
|
|
|
|
AData.Read(FHue, SizeOf(SmallInt));
|
|
|
|
|
|
|
|
FX := ABlockX * 8 + iX;
|
|
|
|
FY := ABlockY * 8 + iY;
|
|
|
|
end;
|
2009-12-03 00:04:19 +01:00
|
|
|
|
2009-03-09 17:24:53 +01:00
|
|
|
InitOriginalState;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream);
|
|
|
|
begin
|
|
|
|
Create(AOwner, AData, 0, 0);
|
|
|
|
end;
|
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
function TStaticItem.HasChanged: Boolean;
|
|
|
|
begin
|
|
|
|
Result := (FHue <> FOrgHue) or inherited HasChanged;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStaticItem.SetHue(AHue: Word);
|
|
|
|
begin
|
|
|
|
FHue := AHue;
|
|
|
|
DoChanged;
|
|
|
|
end;
|
|
|
|
|
2009-03-09 17:24:53 +01:00
|
|
|
function TStaticItem.Clone: TStaticItem;
|
|
|
|
begin
|
|
|
|
Result := TStaticItem.Create(nil, nil);
|
|
|
|
Result.FTileID := FTileID;
|
|
|
|
Result.FX := FX;
|
|
|
|
Result.FY := FY;
|
|
|
|
Result.FZ := FZ;
|
|
|
|
Result.FHue := FHue;
|
|
|
|
end;
|
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
function TStaticItem.GetSize: Integer;
|
|
|
|
begin
|
|
|
|
Result := 7;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStaticItem.InitOriginalState;
|
2009-03-09 17:24:53 +01:00
|
|
|
begin
|
2009-08-02 13:45:26 +02:00
|
|
|
FOrgHue := FHue;
|
|
|
|
inherited InitOriginalState;
|
|
|
|
end;
|
|
|
|
|
2009-08-02 19:08:56 +02:00
|
|
|
procedure TStaticItem.UpdatePriorities(ATileData: TStaticTiledata;
|
|
|
|
ASolver: Integer);
|
2009-08-02 13:45:26 +02:00
|
|
|
begin
|
|
|
|
FPriorityBonus := 0;
|
2009-12-03 00:04:19 +01:00
|
|
|
if not (tdfBackground in ATileData.Flags) then
|
2009-08-02 13:45:26 +02:00
|
|
|
Inc(FPriorityBonus);
|
2009-08-02 19:08:56 +02:00
|
|
|
if ATileData.Height > 0 then
|
2009-08-02 13:45:26 +02:00
|
|
|
Inc(FPriorityBonus);
|
|
|
|
FPriority := Z + FPriorityBonus;
|
|
|
|
FPrioritySolver := ASolver;
|
2009-03-09 17:24:53 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStaticItem.Write(AData: TStream);
|
|
|
|
var
|
|
|
|
iX, iY: Byte;
|
|
|
|
begin
|
|
|
|
iX := FX mod 8;
|
|
|
|
iY := FY mod 8;
|
|
|
|
|
|
|
|
AData.Write(FTileID, SizeOf(SmallInt));
|
|
|
|
AData.Write(iX, SizeOf(Byte));
|
|
|
|
AData.Write(iY, SizeOf(Byte));
|
|
|
|
AData.Write(FZ, SizeOf(ShortInt));
|
|
|
|
AData.Write(FHue, SizeOf(SmallInt));
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TStaticBlock }
|
|
|
|
|
2009-12-03 00:04:19 +01:00
|
|
|
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex;
|
|
|
|
AX, AY: Word);
|
2009-03-09 17:24:53 +01:00
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
block: TMemoryStream;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FX := AX;
|
|
|
|
FY := AY;
|
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
FItems := TStaticItemList.Create(True);
|
|
|
|
if (AData <> nil) and (AIndex.Lookup > 0) and (AIndex.Size > 0) then
|
2009-03-09 17:24:53 +01:00
|
|
|
begin
|
|
|
|
AData.Position := AIndex.Lookup;
|
|
|
|
block := TMemoryStream.Create;
|
|
|
|
block.CopyFrom(AData, AIndex.Size);
|
|
|
|
block.Position := 0;
|
|
|
|
for i := 1 to (AIndex.Size div 7) do
|
|
|
|
FItems.Add(TStaticItem.Create(Self, block, AX, AY));
|
|
|
|
block.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex);
|
|
|
|
begin
|
|
|
|
Create(AData, AIndex, 0, 0);
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TStaticBlock.Destroy;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
2009-12-02 19:14:00 +01:00
|
|
|
FreeAndNil(FItems);
|
2009-03-09 17:24:53 +01:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStaticBlock.Clone: TStaticBlock;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
Result := TStaticBlock.Create(nil, nil, FX, FY);
|
|
|
|
for i := 0 to FItems.Count - 1 do
|
2009-12-02 19:14:00 +01:00
|
|
|
Result.FItems.Add(FItems.Items[i].Clone);
|
2009-03-09 17:24:53 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TStaticBlock.GetSize: Integer;
|
|
|
|
begin
|
|
|
|
Result := FItems.Count * 7;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStaticBlock.ReverseWrite(AData: TStream);
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
for i := FItems.Count - 1 downto 0 do
|
2009-12-02 19:14:00 +01:00
|
|
|
FItems[i].Write(AData);
|
2009-03-09 17:24:53 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStaticBlock.Sort;
|
2009-08-02 13:45:26 +02:00
|
|
|
begin
|
2009-12-02 19:14:00 +01:00
|
|
|
FItems.Sort(@CompareStaticItems);
|
2009-08-02 13:45:26 +02:00
|
|
|
end;
|
2009-03-09 17:24:53 +01:00
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
procedure TStaticBlock.Write(AData: TStream);
|
|
|
|
var
|
|
|
|
i: Integer;
|
2009-03-09 17:24:53 +01:00
|
|
|
begin
|
2009-08-02 13:45:26 +02:00
|
|
|
for i := 0 to FItems.Count - 1 do
|
2009-12-02 19:14:00 +01:00
|
|
|
FItems[i].Write(AData);
|
2009-03-09 17:24:53 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|