- Moved WriteMap handling from TdmNetwork to TLandscape

- Added repainting when accesslevel display changes (toolstate, update write map)
This commit is contained in:
Andreas Schneider 2009-10-02 23:52:02 +02:00
parent f888443ed0
commit c3bdc98219
3 changed files with 74 additions and 67 deletions

View File

@ -45,7 +45,7 @@ type
{ TMaterial } //TODO : add ref counting { TMaterial } //TODO : add ref counting
TMaterial = class(TObject) TMaterial = class
constructor Create(AWidth, AHeight: Integer; AGraphic: TSingleImage); constructor Create(AWidth, AHeight: Integer; AGraphic: TSingleImage);
destructor Destroy; override; destructor Destroy; override;
protected protected
@ -69,7 +69,7 @@ type
{ TLandTextureManager } { TLandTextureManager }
TLandTextureManager = class(TObject) TLandTextureManager = class
constructor Create; constructor Create;
destructor Destroy; override; destructor Destroy; override;
function GetArtMaterial(ATileID: Word): TMaterial; overload; function GetArtMaterial(ATileID: Word): TMaterial; overload;
@ -96,10 +96,12 @@ type
function GetSize: Integer; override; function GetSize: Integer; override;
procedure RebuildList; procedure RebuildList;
end; end;
TLandscape = class;
{ TBlock } { TBlock }
TBlock = class(TObject) TBlock = class
constructor Create(AMap: TMapBlock; AStatics: TStaticBlock); constructor Create(AMap: TMapBlock; AStatics: TStaticBlock);
destructor Destroy; override; destructor Destroy; override;
protected protected
@ -111,7 +113,7 @@ type
property Map: TMapBlock read FMapBlock; property Map: TMapBlock read FMapBlock;
property Static: TStaticBlock read FStaticBlock; property Static: TStaticBlock read FStaticBlock;
{ Methods } { Methods }
procedure UpdateBlockAcess; procedure UpdateBlockAcess(ALandscape: TLandscape);
end; end;
TLandscapeChangeEvent = procedure of object; TLandscapeChangeEvent = procedure of object;
@ -123,7 +125,7 @@ type
{ TLandscape } { TLandscape }
TLandscape = class(TObject) TLandscape = class
constructor Create(AWidth, AHeight: Word); constructor Create(AWidth, AHeight: Word);
destructor Destroy; override; destructor Destroy; override;
protected protected
@ -141,6 +143,7 @@ type
FOnStaticElevated: TStaticChangedEvent; FOnStaticElevated: TStaticChangedEvent;
FOnStaticHued: TStaticChangedEvent; FOnStaticHued: TStaticChangedEvent;
FOpenRequests: TBits; FOpenRequests: TBits;
FWriteMap: TBits;
{ Methods } { Methods }
function GetMapBlock(AX, AY: Word): TMapBlock; function GetMapBlock(AX, AY: Word): TMapBlock;
function GetMapCell(AX, AY: Word): TMapCell; function GetMapCell(AX, AY: Word): TMapCell;
@ -177,6 +180,7 @@ type
property OnStaticHued: TStaticChangedEvent read FOnStaticHued property OnStaticHued: TStaticChangedEvent read FOnStaticHued
write FOnStaticHued; write FOnStaticHued;
{ Methods } { Methods }
function CanWrite(AX, AY: Word): Boolean;
procedure FillDrawList(ADrawList: TScreenBuffer; AX, AY, AWidth, procedure FillDrawList(ADrawList: TScreenBuffer; AX, AY, AWidth,
AHeight: Word; AMap, AStatics: Boolean; ANoDraw: Boolean; AHeight: Word; AMap, AStatics: Boolean; ANoDraw: Boolean;
AAdditionalTiles: TList = nil); AAdditionalTiles: TList = nil);
@ -186,6 +190,7 @@ type
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 UpdateBlockAccess; procedure UpdateBlockAccess;
procedure UpdateWriteMap(AStream: TEnhancedMemoryStream);
end; end;
TScreenState = (ssNormal, ssFiltered, ssGhost); TScreenState = (ssNormal, ssFiltered, ssGhost);
@ -205,7 +210,7 @@ type
{ TScreenBuffer } { TScreenBuffer }
TScreenBuffer = class(TObject) TScreenBuffer = class
constructor Create; virtual; constructor Create; virtual;
destructor Destroy; override; destructor Destroy; override;
protected protected
@ -240,7 +245,7 @@ type
implementation implementation
uses uses
UGameResources, UdmNetwork, UPackets, UPacketHandlers; UGameResources, UdmNetwork, UPackets, UPacketHandlers, Logging;
const const
mMap = 0; mMap = 0;
@ -452,7 +457,6 @@ begin
inherited Create; inherited Create;
FMapBlock := AMap; FMapBlock := AMap;
FStaticBlock := AStatics; FStaticBlock := AStatics;
UpdateBlockAcess;
end; end;
destructor TBlock.Destroy; destructor TBlock.Destroy;
@ -462,15 +466,15 @@ begin
inherited Destroy; inherited Destroy;
end; end;
procedure TBlock.UpdateBlockAcess; procedure TBlock.UpdateBlockAcess(ALandscape: TLandscape);
var var
staticItem: TStaticItem; staticItem: TStaticItem;
i: Integer; i: Integer;
begin begin
for i := Low(FMapBlock.Cells) to High(FMapBlock.Cells) do for i := Low(FMapBlock.Cells) to High(FMapBlock.Cells) do
begin begin
FMapBlock.Cells[i].CanBeEdited := dmNetwork.CanWrite( FMapBlock.Cells[i].CanBeEdited := ALandscape.CanWrite(FMapBlock.Cells[i].X,
FMapBlock.Cells[i].X, FMapBlock.Cells[i].Y); FMapBlock.Cells[i].Y);
end; end;
if FStaticBlock is TSeperatedStaticBlock then if FStaticBlock is TSeperatedStaticBlock then
@ -479,8 +483,8 @@ begin
for i := 0 to FStaticBlock.Items.Count - 1 do for i := 0 to FStaticBlock.Items.Count - 1 do
begin begin
staticItem := TStaticItem(FStaticBlock.Items[i]); staticItem := TStaticItem(FStaticBlock.Items[i]);
staticItem.CanBeEdited := dmNetwork.CanWrite( staticItem.CanBeEdited := ALandscape.CanWrite(staticItem.X,
staticItem.X, staticItem.Y); staticItem.Y);
end; end;
end; end;
@ -488,7 +492,7 @@ end;
constructor TLandscape.Create(AWidth, AHeight: Word); constructor TLandscape.Create(AWidth, AHeight: Word);
var var
blockID: Integer; blockID, i: Integer;
begin begin
inherited Create; inherited Create;
FWidth := AWidth; FWidth := AWidth;
@ -506,7 +510,10 @@ begin
FOnStaticInserted := nil; FOnStaticInserted := nil;
FOpenRequests := TBits.Create(FWidth * FHeight); FOpenRequests := TBits.Create(FWidth * FHeight);
FOpenRequests.Clearall; FOpenRequests.Clearall; //set all to 0
FWriteMap := TBits.Create(FCellWidth * FCellHeight);
for i := 0 to FWriteMap.Size - 1 do
FWriteMap[i] := True;
RegisterPacketHandler($04, TPacketHandler.Create(0, @OnBlocksPacket)); RegisterPacketHandler($04, TPacketHandler.Create(0, @OnBlocksPacket));
RegisterPacketHandler($06, TPacketHandler.Create(8, @OnDrawMapPacket)); RegisterPacketHandler($06, TPacketHandler.Create(8, @OnDrawMapPacket));
@ -524,6 +531,9 @@ begin
FBlockCache.OnRemoveObject := nil; FBlockCache.OnRemoveObject := nil;
FreeAndNil(FBlockCache); FreeAndNil(FBlockCache);
end; end;
FreeAndNil(FOpenRequests);
FreeAndNil(FWriteMap);
RegisterPacketHandler($04, nil); RegisterPacketHandler($04, nil);
RegisterPacketHandler($06, nil); RegisterPacketHandler($06, nil);
@ -627,6 +637,7 @@ begin
FBlockCache.RemoveID(id); FBlockCache.RemoveID(id);
block := TBlock.Create(map, statics); block := TBlock.Create(map, statics);
block.UpdateBlockAcess(Self);
FBlockCache.StoreID(id, block); FBlockCache.StoreID(id, block);
FOpenRequests[coords.Y * FWidth + coords.X] := False; FOpenRequests[coords.Y * FWidth + coords.X] := False;
@ -650,7 +661,6 @@ begin
cell.TileID := ABuffer.ReadWord; cell.TileID := ABuffer.ReadWord;
if Assigned(FOnMapChanged) then FOnMapChanged(cell); if Assigned(FOnMapChanged) then FOnMapChanged(cell);
end; end;
//TODO : update surrounding normals
end; end;
procedure TLandscape.OnInsertStaticPacket(ABuffer: TEnhancedMemoryStream); procedure TLandscape.OnInsertStaticPacket(ABuffer: TEnhancedMemoryStream);
@ -680,7 +690,7 @@ begin
i); i);
targetStaticList.Sort(@CompareWorldItems); targetStaticList.Sort(@CompareWorldItems);
staticItem.Owner := block; staticItem.Owner := block;
staticItem.CanBeEdited := dmNetwork.CanWrite(x, y); staticItem.CanBeEdited := CanWrite(x, y);
if Assigned(FOnStaticInserted) then FOnStaticInserted(staticItem); if Assigned(FOnStaticInserted) then FOnStaticInserted(staticItem);
end; end;
@ -807,7 +817,7 @@ begin
i); i);
statics.Sort(@CompareWorldItems); statics.Sort(@CompareWorldItems);
staticItem.Owner := targetBlock; staticItem.Owner := targetBlock;
staticItem.CanBeEdited := dmNetwork.CanWrite(newX, newY); staticItem.CanBeEdited := CanWrite(newX, newY);
if Assigned(FOnStaticInserted) then FOnStaticInserted(staticItem); if Assigned(FOnStaticInserted) then FOnStaticInserted(staticItem);
end; end;
@ -841,6 +851,11 @@ begin
end; end;
end; end;
function TLandscape.CanWrite(AX, AY: Word): Boolean;
begin
Result := FWriteMap[AX * FCellHeight + AY];
end;
procedure TLandscape.FillDrawList(ADrawList: TScreenBuffer; AX, AY, AWidth, procedure TLandscape.FillDrawList(ADrawList: TScreenBuffer; AX, AY, AWidth,
AHeight: Word; AMap, AStatics: Boolean; ANoDraw: Boolean; AHeight: Word; AMap, AStatics: Boolean; ANoDraw: Boolean;
AAdditionalTiles: TList = nil); AAdditionalTiles: TList = nil);
@ -1063,7 +1078,41 @@ var
begin begin
cacheEntry := nil; cacheEntry := nil;
while FBlockCache.Iterate(cacheEntry) do while FBlockCache.Iterate(cacheEntry) do
TBlock(cacheEntry^.Obj).UpdateBlockAcess; if cacheEntry^.Obj <> nil then
TBlock(cacheEntry^.Obj).UpdateBlockAcess(Self);
end;
procedure TLandscape.UpdateWriteMap(AStream: TEnhancedMemoryStream);
var
x1, y1, x2, y2: Word;
i, areaCount, cellX, cellY: Integer;
begin
Logger.EnterMethod([lcLandscape, lcDebug], 'TLandscape.UpdateWriteMap');
areaCount := AStream.ReadWord;
Logger.Send([lcLandscape, lcDebug], 'AreaCount', areaCount);
if areaCount > 0 then
begin
FWriteMap.Clearall;
for i := 0 to areaCount - 1 do
begin
x1 := AStream.ReadWord;
y1 := AStream.ReadWord;
x2 := AStream.ReadWord;
y2 := AStream.ReadWord;
for cellX := x1 to x2 do
for cellY := y1 to y2 do
FWriteMap[cellX * FCellHeight + cellY] := True;
end;
end else
for i := 0 to FWriteMap.Size - 1 do
FWriteMap[i] := True;
Logger.Send([lcLandscape, lcDebug], 'WriteMap @ 0,0', FWriteMap[0]);
UpdateBlockAccess;
Logger.ExitMethod([lcLandscape, lcDebug], 'TLandscape.UpdateWriteMap');
end; end;
{ TMaterial } { TMaterial }

View File

@ -31,8 +31,7 @@ interface
uses uses
Classes, SysUtils, LResources, Forms, Controls, Dialogs, lNetComponents, lNet, Classes, SysUtils, LResources, Forms, Controls, Dialogs, lNetComponents, lNet,
UEnhancedMemoryStream, UPacket, UEnums, ExtCtrls, dateutils, URectList, UEnhancedMemoryStream, UPacket, UEnums, ExtCtrls, dateutils;
LCLIntf;
type type
@ -57,7 +56,6 @@ type
FAccessLevel: TAccessLevel; FAccessLevel: TAccessLevel;
FDataDir: string; FDataDir: string;
FLastPacket: TDateTime; FLastPacket: TDateTime;
FWriteMap: TRectList;
procedure OnCanSend(ASocket: TLSocket); procedure OnCanSend(ASocket: TLSocket);
procedure OnConnectionHandlingPacket(ABuffer: TEnhancedMemoryStream); procedure OnConnectionHandlingPacket(ABuffer: TEnhancedMemoryStream);
procedure ProcessQueue; procedure ProcessQueue;
@ -65,11 +63,9 @@ type
public public
property Username: string read FUsername; property Username: string read FUsername;
property AccessLevel: TAccessLevel read FAccessLevel write FAccessLevel; property AccessLevel: TAccessLevel read FAccessLevel write FAccessLevel;
function CanWrite(AX, AY: Word): Boolean;
procedure Send(APacket: TPacket); procedure Send(APacket: TPacket);
procedure Disconnect; procedure Disconnect;
procedure CheckClose(ASender: TForm); procedure CheckClose(ASender: TForm);
procedure UpdateWriteMap(AStream: TEnhancedMemoryStream);
end; end;
var var
@ -102,7 +98,6 @@ begin
if FSendQueue <> nil then FreeAndNil(FSendQueue); if FSendQueue <> nil then FreeAndNil(FSendQueue);
if FReceiveQueue <> nil then FreeAndNil(FReceiveQueue); if FReceiveQueue <> nil then FreeAndNil(FReceiveQueue);
if PacketHandlers[$02] <> nil then FreeAndNil(PacketHandlers[$02]); if PacketHandlers[$02] <> nil then FreeAndNil(PacketHandlers[$02]);
if FWriteMap <> nil then FreeAndNil(FWriteMap);
end; end;
procedure TdmNetwork.TCPClientConnect(aSocket: TLSocket); procedure TdmNetwork.TCPClientConnect(aSocket: TLSocket);
@ -199,11 +194,7 @@ begin
width := ABuffer.ReadWord; width := ABuffer.ReadWord;
height := ABuffer.ReadWord; height := ABuffer.ReadWord;
ResMan.InitLandscape(width, height); ResMan.InitLandscape(width, height);
ResMan.Landscape.UpdateWriteMap(ABuffer);
{FWriteMap := TBits.Create(ResMan.Landscape.CellWidth * ResMan.Landscape.CellHeight);
FWriteMap.XorBits(FWriteMap); //set all to 1}
FWriteMap := TRectList.Create;
UpdateWriteMap(ABuffer);
frmMain := TfrmMain.Create(dmNetwork); frmMain := TfrmMain.Create(dmNetwork);
frmRadarMap := TfrmRadarMap.Create(frmMain); frmRadarMap := TfrmRadarMap.Create(frmMain);
@ -320,7 +311,6 @@ begin
frmMain.ApplicationProperties1.OnIdle := nil; frmMain.ApplicationProperties1.OnIdle := nil;
FreeAndNil(frmMain); FreeAndNil(frmMain);
end; end;
if FWriteMap <> nil then FreeAndNil(FWriteMap);
if GameResourceManager <> nil then FreeAndNil(GameResourceManager); if GameResourceManager <> nil then FreeAndNil(GameResourceManager);
frmInitialize.Hide; frmInitialize.Hide;
while frmLogin.ShowModal = mrOK do while frmLogin.ShowModal = mrOK do
@ -340,23 +330,6 @@ begin
FreeAndNil(frmLogin); FreeAndNil(frmLogin);
end; end;
function TdmNetwork.CanWrite(AX, AY: Word): Boolean;
var
i: Integer;
pt: TPoint;
begin
if FWriteMap.Count > 0 then
begin
pt := Point(AX, AY);
for i := 0 to FWriteMap.Count - 1 do
if PtInRect(FWriteMap.Rects[i], pt) then
Exit(True);
Result := False;
end else
Result := True;
end;
procedure TdmNetwork.Send(APacket: TPacket); procedure TdmNetwork.Send(APacket: TPacket);
var var
source: TEnhancedMemoryStream; source: TEnhancedMemoryStream;
@ -386,23 +359,6 @@ begin
end; end;
end; end;
procedure TdmNetwork.UpdateWriteMap(AStream: TEnhancedMemoryStream);
var
x1, y1, x2, y2: Word;
i, areaCount: Integer;
begin
FWriteMap.Clear;
areaCount := AStream.ReadWord;
for i := 0 to areaCount - 1 do
begin
x1 := AStream.ReadWord;
y1 := AStream.ReadWord;
x2 := AStream.ReadWord;
y2 := AStream.ReadWord;
FWriteMap.Add(x1, y1, x2, y2);
end;
end;
initialization initialization
{$I UdmNetwork.lrs} {$I UdmNetwork.lrs}

View File

@ -2296,6 +2296,8 @@ begin
end; end;
end; end;
end; end;
FRepaintNeeded := True;
end; end;
procedure TfrmMain.ProcessAccessLevel; procedure TfrmMain.ProcessAccessLevel;
@ -2546,8 +2548,8 @@ begin
$07: //access changed $07: //access changed
begin begin
accessLevel := TAccessLevel(ABuffer.ReadByte); accessLevel := TAccessLevel(ABuffer.ReadByte);
dmNetwork.UpdateWriteMap(ABuffer); //TODO : movie writemap to landscape FLandscape.UpdateWriteMap(ABuffer);
FLandscape.UpdateBlockAccess; //TODO : could be handled by updatewritemap FRepaintNeeded := True;
if accessLevel <> dmNetwork.AccessLevel then if accessLevel <> dmNetwork.AccessLevel then
begin begin