- Added iterator to TCacheManager

- Added access (CanBeWritten) tracking to TStaticItem
- Moved TLandscape.UpdateStaticsPriorities to TStaticItem.UpdatePriorities
- Moved ULandscape.CompareWorldItems to UWorldItem
- Renamed TSeperatedStaticBlock.RefreshList to RebuildList
- Changed TStaticBlock.Sort to use CompareWorldItems and TList.Sort
- Some code style changes
This commit is contained in:
Andreas Schneider 2009-08-02 13:45:26 +02:00
parent f3888db49c
commit b70f03ba2a
8 changed files with 470 additions and 430 deletions

View File

@ -88,11 +88,15 @@ type
constructor Create(AMap: TMapBlock; AStatics: TStaticBlock); constructor Create(AMap: TMapBlock; AStatics: TStaticBlock);
destructor Destroy; override; destructor Destroy; override;
protected protected
{ Fields }
FMapBlock: TMapBlock; FMapBlock: TMapBlock;
FStaticBlock: TStaticBlock; FStaticBlock: TStaticBlock;
public public
{ Fields }
property Map: TMapBlock read FMapBlock; property Map: TMapBlock read FMapBlock;
property Static: TStaticBlock read FStaticBlock; property Static: TStaticBlock read FStaticBlock;
{ Methods }
procedure UpdateBlockAcess;
end; end;
TLandscapeChangeEvent = procedure of object; TLandscapeChangeEvent = procedure of object;
@ -148,8 +152,7 @@ type
procedure GetNormals(AX, AY: Word; var ANormals: TNormals); procedure GetNormals(AX, AY: Word; var ANormals: TNormals);
procedure MoveStatic(AStatic: TStaticItem; AX, AY: Word); procedure MoveStatic(AStatic: TStaticItem; AX, AY: Word);
procedure PrepareBlocks(AX1, AY1, AX2, AY2: Word); procedure PrepareBlocks(AX1, AY1, AX2, AY2: Word);
procedure UpdateStaticsPriority(AStaticItem: TStaticItem; procedure UpdateBlockAccess;
APrioritySolver: Integer);
end; end;
TScreenState = (ssNormal, ssFiltered, ssGhost); TScreenState = (ssNormal, ssFiltered, ssGhost);
@ -185,8 +188,8 @@ type
procedure Delete(AItem: TWorldItem); procedure Delete(AItem: TWorldItem);
function Find(AScreenPosition: TPoint): PBlockInfo; function Find(AScreenPosition: TPoint): PBlockInfo;
function GetSerial: Cardinal; function GetSerial: Cardinal;
function Iterate(var ABlockInfo: PBlockInfo): Boolean;
function Insert(AItem: TWorldItem): PBlockInfo; function Insert(AItem: TWorldItem): PBlockInfo;
function Iterate(var ABlockInfo: PBlockInfo): Boolean;
procedure Sort; procedure Sort;
procedure UpdateShortcuts; procedure UpdateShortcuts;
{ Events } { Events }
@ -215,30 +218,6 @@ begin
Result := ((AX and $7FFF) shl 15) or (AY and $7FFF); Result := ((AX and $7FFF) shl 15) or (AY and $7FFF);
end; end;
function CompareWorldItems(AItem1, AItem2: Pointer): Integer;
begin
if TWorldItem(AItem1).X <> TWorldItem(AItem2).X then
Exit(TWorldItem(AItem1).X - TWorldItem(AItem2).X);
if TWorldItem(AItem1).Y <> TWorldItem(AItem2).Y then
Exit(TWorldItem(AItem1).Y - TWorldItem(AItem2).Y);
Result := TWorldItem(AItem1).Priority - TWorldItem(AItem2).Priority;
if Result = 0 then
begin
if (TObject(AItem1) is TMapCell) and (TObject(AItem2) is TStaticItem) then
Result := -1
else if (TObject(AItem1) is TStaticItem) and (TObject(AItem2) is TMapCell) then
Result := 1;
end;
if Result = 0 then
Result := TWorldItem(AItem1).PriorityBonus - TWorldItem(AItem2).PriorityBonus;
if Result = 0 then
Result := TWorldItem(AItem1).PrioritySolver - TWorldItem(AItem2).PrioritySolver;
end;
{ TLandTextureManager } { TLandTextureManager }
constructor TLandTextureManager.Create; constructor TLandTextureManager.Create;
@ -342,6 +321,7 @@ begin
inherited Create; inherited Create;
FMapBlock := AMap; FMapBlock := AMap;
FStaticBlock := AStatics; FStaticBlock := AStatics;
UpdateBlockAcess;
end; end;
destructor TBlock.Destroy; destructor TBlock.Destroy;
@ -351,6 +331,28 @@ begin
inherited Destroy; inherited Destroy;
end; end;
procedure TBlock.UpdateBlockAcess;
var
staticItem: TStaticItem;
i: Integer;
begin
for i := Low(FMapBlock.Cells) to High(FMapBlock.Cells) do
begin
FMapBlock.Cells[i].CanBeEdited := dmNetwork.CanWrite(
FMapBlock.Cells[i].X, FMapBlock.Cells[i].Y);
end;
if FStaticBlock is TSeperatedStaticBlock then
TSeperatedStaticBlock(FStaticBlock).RebuildList; //fill items
for i := 0 to FStaticBlock.Items.Count - 1 do
begin
staticItem := TStaticItem(FStaticBlock.Items[i]);
staticItem.CanBeEdited := dmNetwork.CanWrite(
staticItem.X, staticItem.Y);
end;
end;
{ TLandscape } { TLandscape }
constructor TLandscape.Create(AWidth, AHeight: Word); constructor TLandscape.Create(AWidth, AHeight: Word);
@ -533,7 +535,7 @@ begin
targetStaticList := block.Cells[(y mod 8) * 8 + x mod 8]; targetStaticList := block.Cells[(y mod 8) * 8 + x mod 8];
targetStaticList.Add(staticItem); targetStaticList.Add(staticItem);
for i := 0 to targetStaticList.Count - 1 do for i := 0 to targetStaticList.Count - 1 do
UpdateStaticsPriority(TStaticItem(targetStaticList.Items[i]), i); TStaticItem(targetStaticList.Items[i]).UpdatePriorities(i);
targetStaticList.Sort(@CompareWorldItems); targetStaticList.Sort(@CompareWorldItems);
staticItem.Owner := block; staticItem.Owner := block;
if Assigned(FOnChange) then FOnChange; if Assigned(FOnChange) then FOnChange;
@ -591,7 +593,7 @@ begin
begin begin
staticItem.Z := ABuffer.ReadShortInt; staticItem.Z := ABuffer.ReadShortInt;
for j := 0 to statics.Count - 1 do for j := 0 to statics.Count - 1 do
UpdateStaticsPriority(TStaticItem(statics.Items[j]), j); TStaticItem(statics.Items[j]).UpdatePriorities(j);
statics.Sort(@CompareWorldItems); statics.Sort(@CompareWorldItems);
if Assigned(FOnChange) then FOnChange; if Assigned(FOnChange) then FOnChange;
Break; Break;
@ -650,7 +652,7 @@ begin
statics := targetBlock.Cells[(newY mod 8) * 8 + newX mod 8]; statics := targetBlock.Cells[(newY mod 8) * 8 + newX mod 8];
statics.Add(staticItem); statics.Add(staticItem);
for i := 0 to statics.Count - 1 do for i := 0 to statics.Count - 1 do
UpdateStaticsPriority(TStaticItem(statics.Items[i]), i); TStaticItem(statics.Items[i]).UpdatePriorities(i);
statics.Sort(@CompareWorldItems); statics.Sort(@CompareWorldItems);
staticItem.Owner := targetBlock; staticItem.Owner := targetBlock;
end; end;
@ -724,7 +726,7 @@ begin
(TStaticItem(drawStatics[i]).Z <= AMaxZ) and (TStaticItem(drawStatics[i]).Z <= AMaxZ) and
((AStaticsFilter = nil) or AStaticsFilter(TStaticItem(drawStatics[i]))) then ((AStaticsFilter = nil) or AStaticsFilter(TStaticItem(drawStatics[i]))) then
begin begin
UpdateStaticsPriority(TStaticItem(drawStatics[i]), ADrawList.GetSerial); TStaticItem(drawStatics[i]).UpdatePriorities(ADrawList.GetSerial);
ADrawList.Add(TWorldItem(drawStatics[i])); ADrawList.Add(TWorldItem(drawStatics[i]));
end; end;
end; end;
@ -857,7 +859,7 @@ begin
targetStaticList := targetBlock.Cells[(AY mod 8) * 8 + AX mod 8]; targetStaticList := targetBlock.Cells[(AY mod 8) * 8 + AX mod 8];
targetStaticList.Add(AStatic); targetStaticList.Add(AStatic);
for i := 0 to targetStaticList.Count - 1 do for i := 0 to targetStaticList.Count - 1 do
UpdateStaticsPriority(TStaticItem(targetStaticList.Items[i]), i); TStaticItem(targetStaticList.Items[i]).UpdatePriorities(i);
targetStaticList.Sort(@CompareWorldItems); targetStaticList.Sort(@CompareWorldItems);
AStatic.UpdatePos(AX, AY, AStatic.Z); AStatic.UpdatePos(AX, AY, AStatic.Z);
AStatic.Owner := targetBlock; AStatic.Owner := targetBlock;
@ -896,19 +898,13 @@ begin
dmNetwork.Send(TRequestBlocksPacket.Create(coords)); dmNetwork.Send(TRequestBlocksPacket.Create(coords));
end; end;
procedure TLandscape.UpdateStaticsPriority(AStaticItem: TStaticItem; procedure TLandscape.UpdateBlockAccess;
APrioritySolver: Integer);
var var
staticTileData: TStaticTileData; cacheEntry: PCacheEntry;
begin begin
staticTileData := ResMan.Tiledata.StaticTiles[AStaticItem.TileID]; cacheEntry := nil;
AStaticItem.PriorityBonus := 0; while FBlockCache.Iterate(cacheEntry) do
if not ((staticTileData.Flags and tdfBackground) = tdfBackground) then TBlock(cacheEntry^.Obj).UpdateBlockAcess;
AStaticItem.PriorityBonus := AStaticItem.PriorityBonus + 1;
if staticTileData.Height > 0 then
AStaticItem.PriorityBonus := AStaticItem.PriorityBonus + 1;
AStaticItem.Priority := AStaticItem.Z + AStaticItem.PriorityBonus;
AStaticItem.PrioritySolver := APrioritySolver;
end; end;
{ TMaterial } { TMaterial }
@ -1087,15 +1083,6 @@ begin
Inc(FSerial); Inc(FSerial);
end; end;
function TScreenBuffer.Iterate(var ABlockInfo: PBlockInfo): Boolean;
begin
if ABlockInfo = nil then
ABlockInfo := FShortCuts[0]
else
ABlockInfo := ABlockInfo^.Next;
Result := ABlockInfo <> nil;
end;
function TScreenBuffer.Insert(AItem: TWorldItem): PBlockInfo; function TScreenBuffer.Insert(AItem: TWorldItem): PBlockInfo;
var var
current: PBlockInfo; current: PBlockInfo;
@ -1145,6 +1132,15 @@ begin
Inc(FCount); Inc(FCount);
end; end;
function TScreenBuffer.Iterate(var ABlockInfo: PBlockInfo): Boolean;
begin
if ABlockInfo = nil then
ABlockInfo := FShortCuts[0]
else
ABlockInfo := ABlockInfo^.Next;
Result := ABlockInfo <> nil;
end;
//Mergesort //Mergesort
procedure TScreenBuffer.Sort; procedure TScreenBuffer.Sort;

View File

@ -1,15 +1,19 @@
object dmNetwork: TdmNetwork object dmNetwork: TdmNetwork
OnCreate = DataModuleCreate OnCreate = DataModuleCreate
OnDestroy = DataModuleDestroy OnDestroy = DataModuleDestroy
OldCreateOrder = False
Height = 300 Height = 300
HorizontalOffset = 290 HorizontalOffset = 290
VerticalOffset = 171 VerticalOffset = 171
Width = 400 Width = 400
object TCPClient: TLTCPComponent object TCPClient: TLTCPComponent
Port = 0
OnReceive = TCPClientReceive OnReceive = TCPClientReceive
OnError = TCPClientError OnError = TCPClientError
OnDisconnect = TCPClientDisconnect OnDisconnect = TCPClientDisconnect
OnConnect = TCPClientConnect OnConnect = TCPClientConnect
Timeout = 0
ReuseAddress = False
left = 40 left = 40
top = 24 top = 24
end end

View File

@ -345,14 +345,16 @@ var
i: Integer; i: Integer;
pt: TPoint; pt: TPoint;
begin begin
if FWriteMap.Count = 0 then Exit(True); //TODO : still too slow if FWriteMap.Count > 0 then
begin
pt := Point(AX, AY); pt := Point(AX, AY);
for i := 0 to FWriteMap.Count - 1 do for i := 0 to FWriteMap.Count - 1 do
if PtInRect(FWriteMap.Rects[i], pt) then if PtInRect(FWriteMap.Rects[i], pt) then
Exit(True); Exit(True);
Result := False; Result := False;
end else
Result := True;
end; end;
procedure TdmNetwork.Send(APacket: TPacket); procedure TdmNetwork.Send(APacket: TPacket);

View File

@ -3,6 +3,7 @@ object frmMain: TfrmMain
Height = 603 Height = 603
Top = 126 Top = 126
Width = 766 Width = 766
ActiveControl = oglGameWindow
Caption = 'UO CentrED' Caption = 'UO CentrED'
ClientHeight = 580 ClientHeight = 580
ClientWidth = 766 ClientWidth = 766
@ -540,7 +541,7 @@ object frmMain: TfrmMain
Top = 306 Top = 306
Width = 218 Width = 218
Align = alNone Align = alNone
Anchors = [akTop, akLeft, akRight] Anchors = [akLeft, akRight, akBottom]
ResizeAnchor = akBottom ResizeAnchor = akBottom
end end
object edSearchID: TEdit object edSearchID: TEdit
@ -1110,7 +1111,7 @@ object frmMain: TfrmMain
Top = 435 Top = 435
Width = 542 Width = 542
Align = alNone Align = alNone
Anchors = [akLeft, akRight] Anchors = [akLeft, akRight, akBottom]
AutoSnap = False AutoSnap = False
ResizeAnchor = akBottom ResizeAnchor = akBottom
Visible = False Visible = False

View File

@ -1662,7 +1662,6 @@ var
tileRect: TRect; tileRect: TRect;
virtualTile: TVirtualTile; virtualTile: TVirtualTile;
staticsFilter: TStaticFilter; staticsFilter: TStaticFilter;
editing: Boolean;
intensity: GLfloat; intensity: GLfloat;
blockInfo: PBlockInfo; blockInfo: PBlockInfo;
item: TWorldItem; item: TWorldItem;
@ -1688,19 +1687,17 @@ begin
//TODO : implement CanBeEdited handling (dmNetwork.CanWrite.....) //TODO : implement CanBeEdited handling (dmNetwork.CanWrite.....)
if acSelect.Checked or item.CanBeEdited then if acSelect.Checked or item.CanBeEdited then
begin begin
editing := True;
intensity := 1.0; intensity := 1.0;
SetNormalLights; SetNormalLights;
end else end else
begin begin
editing := False;
intensity := 0.5; intensity := 0.5;
SetDarkLights; SetDarkLights;
end; end;
glColor4f(intensity, intensity, intensity, 1.0); glColor4f(intensity, intensity, intensity, 1.0);
highlight := item.CanBeEdited and blockInfo^.Highlighted; highlight := blockInfo^.Highlighted and item.CanBeEdited;
if highlight then if highlight then
begin begin
@ -2357,7 +2354,8 @@ begin
$07: //access changed $07: //access changed
begin begin
accessLevel := TAccessLevel(ABuffer.ReadByte); accessLevel := TAccessLevel(ABuffer.ReadByte);
dmNetwork.UpdateWriteMap(ABuffer); dmNetwork.UpdateWriteMap(ABuffer); //TODO : movie writemap to landscape
FLandscape.UpdateBlockAccess; //TODO : could be handled by updatewritemap
if accessLevel <> dmNetwork.AccessLevel then if accessLevel <> dmNetwork.AccessLevel then
begin begin

View File

@ -21,7 +21,7 @@
* CDDL HEADER END * CDDL HEADER END
* *
* *
* Portions Copyright 2007 Andreas Schneider * Portions Copyright 2009 Andreas Schneider
*) *)
unit UCacheManager; unit UCacheManager;
@ -48,18 +48,22 @@ type
constructor Create(ASize: Integer); constructor Create(ASize: Integer);
destructor Destroy; override; destructor Destroy; override;
protected protected
{ Members }
FSize: Integer; FSize: Integer;
FFirst: PCacheEntry; FFirst: PCacheEntry;
FLast: PCacheEntry; FLast: PCacheEntry;
FOnRemoveObject: TRemoveObjectEvent; FOnRemoveObject: TRemoveObjectEvent;
public public
{ Fields }
property OnRemoveObject: TRemoveObjectEvent read FOnRemoveObject write FOnRemoveObject;
{ Methods }
function QueryID(const AID: Integer; out AObj: TObject): Boolean; function QueryID(const AID: Integer; out AObj: TObject): Boolean;
procedure StoreID(AID: Integer; AObj: TObject); procedure StoreID(AID: Integer; AObj: TObject);
procedure DiscardID(AID: Integer); procedure DiscardID(AID: Integer);
procedure DiscardObj(AObj: TObject); procedure DiscardObj(AObj: TObject);
procedure RemoveID(AID: Integer); procedure RemoveID(AID: Integer);
procedure Clear; procedure Clear;
property OnRemoveObject: TRemoveObjectEvent read FOnRemoveObject write FOnRemoveObject; function Iterate(var ACacheEntry: PCacheEntry): Boolean;
end; end;
implementation implementation
@ -182,6 +186,15 @@ begin
end; end;
end; end;
function TCacheManager.Iterate(var ACacheEntry: PCacheEntry): Boolean;
begin
if ACacheEntry = nil then
ACacheEntry := FFirst
else
ACacheEntry := ACacheEntry^.Next;
Result := ACacheEntry <> nil;
end;
function TCacheManager.QueryID(const AID: Integer; function TCacheManager.QueryID(const AID: Integer;
out AObj: TObject): Boolean; out AObj: TObject): Boolean;
var var

View File

@ -21,7 +21,7 @@
* CDDL HEADER END * CDDL HEADER END
* *
* *
* Portions Copyright 2007 Andreas Schneider * Portions Copyright 2009 Andreas Schneider
*) *)
unit UStatics; unit UStatics;
@ -30,52 +30,71 @@ unit UStatics;
interface interface
uses uses
SysUtils, Classes, UMulBlock, UGenericIndex, UTiledata, UWorldItem; SysUtils, Classes, UGenericIndex, UWorldItem, UTiledata;
type type
{ TStaticItem }
TStaticItem = class(TWorldItem) TStaticItem = class(TWorldItem)
constructor Create(AOwner: TWorldBlock; AData: TStream; ABlockX, ABlockY: Word); overload; constructor Create(AOwner: TWorldBlock; AData: TStream; ABlockX, ABlockY: Word); overload;
constructor Create(AOwner: TWorldBlock; AData: TStream); overload; constructor Create(AOwner: TWorldBlock; AData: TStream); overload;
function Clone: TStaticItem; override;
function GetIdentifier: Integer;
function GetSize: Integer; override;
procedure Write(AData: TStream); override;
protected protected
FHue, FOrgHue: Word; { Members }
procedure SetHue(AHue: Word); FHue: Word;
FOrgHue: Word;
{ Methods }
function HasChanged: Boolean; override; function HasChanged: Boolean; override;
procedure SetHue(AHue: Word);
public public
procedure InitOriginalState; override; { Fields }
property Hue: Word read FHue write SetHue; property Hue: Word read FHue write SetHue;
{ Methods }
function Clone: TStaticItem; override;
function GetSize: Integer; override;
procedure InitOriginalState; override;
procedure UpdatePriorities(ASolver: Integer);
procedure Write(AData: TStream); override;
end; end;
{ TStaticBlock}
TStaticBlock = class(TWorldBlock) TStaticBlock = class(TWorldBlock)
constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word); overload; constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word); overload;
constructor Create(AData: TStream; AIndex: TGenericIndex); overload; constructor Create(AData: TStream; AIndex: TGenericIndex); overload;
destructor Destroy; override; destructor Destroy; override;
function Clone: TStaticBlock; override;
function GetSize: Integer; override;
procedure Write(AData: TStream); override;
procedure ReverseWrite(AData: TStream);
procedure Sort;
protected protected
{ Members }
FItems: TList; FItems: TList;
public public
{ Fields }
property Items: TList read FItems write FItems; property Items: TList read FItems write FItems;
{ Methods }
function Clone: TStaticBlock; override;
function GetSize: Integer; override;
procedure ReverseWrite(AData: TStream);
procedure Sort;
procedure Write(AData: TStream); override;
end; end;
{ TSeperatedStaticBlock }
TSeperatedStaticBlock = class(TStaticBlock) TSeperatedStaticBlock = class(TStaticBlock)
constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word); overload; constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word); overload;
constructor Create(AData: TStream; AIndex: TGenericIndex); overload; constructor Create(AData: TStream; AIndex: TGenericIndex); overload;
destructor Destroy; override; destructor Destroy; override;
function Clone: TSeperatedStaticBlock; override;
function GetSize: Integer; override;
protected
procedure RefreshList;
public public
Cells: array[0..63] of TList; Cells: array[0..63] of TList;
{ Methods }
function Clone: TSeperatedStaticBlock; override;
function GetSize: Integer; override;
procedure RebuildList;
end; end;
implementation implementation
uses
UGameResources; //Used for priority calculation
{ TStaticItem } { TStaticItem }
constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream; ABlockX, ABlockY: Word); constructor TStaticItem.Create(AOwner: TWorldBlock; AData: TStream; ABlockX, ABlockY: Word);
@ -102,6 +121,17 @@ begin
Create(AOwner, AData, 0, 0); Create(AOwner, AData, 0, 0);
end; end;
function TStaticItem.HasChanged: Boolean;
begin
Result := (FHue <> FOrgHue) or inherited HasChanged;
end;
procedure TStaticItem.SetHue(AHue: Word);
begin
FHue := AHue;
DoChanged;
end;
function TStaticItem.Clone: TStaticItem; function TStaticItem.Clone: TStaticItem;
begin begin
Result := TStaticItem.Create(nil, nil); Result := TStaticItem.Create(nil, nil);
@ -112,9 +142,29 @@ begin
Result.FHue := FHue; Result.FHue := FHue;
end; end;
function TStaticItem.GetIdentifier: Integer; function TStaticItem.GetSize: Integer;
begin begin
Result := 0 or (((FX mod 8) shl 28) and $F0000000) or (((FY mod 8) shl 24) and $0F000000) or ((Byte(FZ) shl 16) and $00FF0000) or (Word(FTileID) and $0000FFFF); Result := 7;
end;
procedure TStaticItem.InitOriginalState;
begin
FOrgHue := FHue;
inherited InitOriginalState;
end;
procedure TStaticItem.UpdatePriorities(ASolver: Integer);
var
staticTileData: TStaticTileData;
begin
staticTileData := ResMan.Tiledata.StaticTiles[FTileID];
FPriorityBonus := 0;
if not ((staticTileData.Flags and tdfBackground) = tdfBackground) then
Inc(FPriorityBonus);
if staticTileData.Height > 0 then
Inc(FPriorityBonus);
FPriority := Z + FPriorityBonus;
FPrioritySolver := ASolver;
end; end;
procedure TStaticItem.Write(AData: TStream); procedure TStaticItem.Write(AData: TStream);
@ -131,28 +181,6 @@ begin
AData.Write(FHue, SizeOf(SmallInt)); AData.Write(FHue, SizeOf(SmallInt));
end; end;
function TStaticItem.GetSize: Integer;
begin
Result := 7;
end;
function TStaticItem.HasChanged: Boolean;
begin
Result := (FHue <> FOrgHue) or inherited HasChanged;
end;
procedure TStaticItem.InitOriginalState;
begin
FOrgHue := FHue;
inherited InitOriginalState;
end;
procedure TStaticItem.SetHue(AHue: Word);
begin
FHue := AHue;
DoChanged;
end;
{ TStaticBlock } { TStaticBlock }
constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word); constructor TStaticBlock.Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word);
@ -214,14 +242,6 @@ begin
Result := FItems.Count * 7; Result := FItems.Count * 7;
end; end;
procedure TStaticBlock.Write(AData: TStream);
var
i: Integer;
begin
for i := 0 to FItems.Count - 1 do
TStaticItem(FItems[i]).Write(AData);
end;
procedure TStaticBlock.ReverseWrite(AData: TStream); procedure TStaticBlock.ReverseWrite(AData: TStream);
var var
i: Integer; i: Integer;
@ -233,49 +253,16 @@ begin
end; end;
procedure TStaticBlock.Sort; procedure TStaticBlock.Sort;
var
iMin, iMax: Integer;
procedure sift;
var
i, j: integer;
begin
i := iMin;
j := 2 * i;
FItems[0] := FItems[i];
while j <= iMax do
begin
if j < iMax then
if TStaticItem(FItems[j]).GetIdentifier < TStaticItem(FItems[j + 1]).GetIdentifier then inc(j);
if TStaticItem(FItems[0]).GetIdentifier >= TStaticItem(FItems[j]).GetIdentifier then break;
FItems[i] := FItems[j];
i := j;
j := 2 * i;
end;
FItems[i] := FItems[0];
end;
begin begin
if FItems.Count > 0 then FItems.Sort(@CompareWorldItems);
begin end;
iMax := FItems.Count;
iMin := iMax div 2 + 1; procedure TStaticBlock.Write(AData: TStream);
FItems.Insert(0, nil); var
while iMin > 1 do i: Integer;
begin begin
dec(iMin); for i := 0 to FItems.Count - 1 do
sift; TStaticItem(FItems[i]).Write(AData);
end;
while iMax > 1 do
begin
FItems[0] := FItems[iMin];
FItems[iMin] := FItems[iMax];
FItems[iMax] := FItems[0];
dec(iMax);
sift;
end;
FItems.Delete(0);
end;
end; end;
{ TSeperatedStaticBlock } { TSeperatedStaticBlock }
@ -339,6 +326,27 @@ begin
inherited Destroy; inherited Destroy;
end; end;
procedure TSeperatedStaticBlock.RebuildList;
var
i, j, solver: Integer;
begin
FItems.Clear;
solver := 0;
for i := 0 to 63 do
begin
if Cells[i] <> nil then
begin
for j := 0 to Cells[i].Count - 1 do
begin
FItems.Add(Cells[i].Items[j]);
TStaticItem(Cells[i].Items[j]).UpdatePriorities(solver);
Inc(solver);
end;
end;
end;
Sort;
end;
function TSeperatedStaticBlock.Clone: TSeperatedStaticBlock; function TSeperatedStaticBlock.Clone: TSeperatedStaticBlock;
var var
i, j: Integer; i, j: Integer;
@ -352,26 +360,9 @@ end;
function TSeperatedStaticBlock.GetSize: Integer; function TSeperatedStaticBlock.GetSize: Integer;
begin begin
RefreshList; RebuildList;
Result := inherited GetSize; Result := inherited GetSize;
end; end;
procedure TSeperatedStaticBlock.RefreshList;
var
i, j: Integer;
begin
FItems.Clear;
for i := 0 to 63 do
begin
if Cells[i] <> nil then
begin
for j := 0 to Cells[i].Count - 1 do
if Cells[i].Items[j] <> nil then
FItems.Add(Cells[i].Items[j]);
end;
end;
Sort;
end;
end. end.

View File

@ -34,6 +34,9 @@ uses
type type
TWorldBlock = class; TWorldBlock = class;
{ TWorldItem }
TWorldItem = class(TMulBlock) TWorldItem = class(TMulBlock)
constructor Create(AOwner: TWorldBlock); constructor Create(AOwner: TWorldBlock);
protected protected
@ -75,6 +78,9 @@ type
property PriorityBonus: ShortInt read FPriorityBonus write FPriorityBonus; property PriorityBonus: ShortInt read FPriorityBonus write FPriorityBonus;
property PrioritySolver: Integer read FPrioritySolver write FPrioritySolver; property PrioritySolver: Integer read FPrioritySolver write FPrioritySolver;
end; end;
{ TWorldBlock }
TWorldBlock = class(TMulBlock) TWorldBlock = class(TMulBlock)
constructor Create; constructor Create;
protected protected
@ -95,8 +101,37 @@ type
procedure CleanUp; procedure CleanUp;
end; end;
function CompareWorldItems(AItem1, AItem2: Pointer): Integer;
implementation implementation
uses
UMap, UStatics;
function CompareWorldItems(AItem1, AItem2: Pointer): Integer;
begin
if TWorldItem(AItem1).X <> TWorldItem(AItem2).X then
Exit(TWorldItem(AItem1).X - TWorldItem(AItem2).X);
if TWorldItem(AItem1).Y <> TWorldItem(AItem2).Y then
Exit(TWorldItem(AItem1).Y - TWorldItem(AItem2).Y);
Result := TWorldItem(AItem1).Priority - TWorldItem(AItem2).Priority;
if Result = 0 then
begin
if (TObject(AItem1) is TMapCell) and (TObject(AItem2) is TStaticItem) then
Result := -1
else if (TObject(AItem1) is TStaticItem) and (TObject(AItem2) is TMapCell) then
Result := 1;
end;
if Result = 0 then
Result := TWorldItem(AItem1).PriorityBonus - TWorldItem(AItem2).PriorityBonus;
if Result = 0 then
Result := TWorldItem(AItem1).PrioritySolver - TWorldItem(AItem2).PrioritySolver;
end;
{ TWorldItem } { TWorldItem }
constructor TWorldItem.Create(AOwner: TWorldBlock); constructor TWorldItem.Create(AOwner: TWorldBlock);