2008-08-29 12:09:26 +02: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-05-15 23:37:10 +02:00
|
|
|
* Portions Copyright 2009 Andreas Schneider
|
2008-08-29 12:09:26 +02:00
|
|
|
*)
|
|
|
|
unit ULandscape;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2009-12-05 17:26:22 +01:00
|
|
|
SysUtils, Classes, math, LCLIntf, GL, GLu, ImagingOpenGL, Imaging,
|
2009-05-17 14:12:52 +02:00
|
|
|
ImagingClasses, ImagingTypes, ImagingUtility,
|
2008-08-29 12:09:26 +02:00
|
|
|
UGenericIndex, UMap, UStatics, UArt, UTexture, UTiledata, UHue, UWorldItem,
|
|
|
|
UMulBlock,
|
2009-12-05 17:26:22 +01:00
|
|
|
UVector, UEnhancedMemoryStream, UGLFont,
|
2009-05-17 14:12:52 +02:00
|
|
|
UCacheManager;
|
2008-08-29 12:09:26 +02:00
|
|
|
|
|
|
|
type
|
2009-05-17 15:32:51 +02:00
|
|
|
PNormals = ^TNormals;
|
2008-08-29 12:09:26 +02:00
|
|
|
TNormals = array[0..3] of TVector;
|
|
|
|
PRadarBlock = ^TRadarBlock;
|
|
|
|
TRadarBlock = array[0..7, 0..7] of Word;
|
|
|
|
|
2009-05-17 22:06:42 +02:00
|
|
|
{ TMaterial } //TODO : add ref counting
|
2008-08-29 12:09:26 +02:00
|
|
|
|
2009-10-02 23:52:02 +02:00
|
|
|
TMaterial = class
|
2008-08-29 12:09:26 +02:00
|
|
|
constructor Create(AWidth, AHeight: Integer; AGraphic: TSingleImage);
|
|
|
|
destructor Destroy; override;
|
|
|
|
protected
|
|
|
|
FWidth: Integer;
|
|
|
|
FHeight: Integer;
|
|
|
|
FRealWidth: Integer;
|
|
|
|
FRealHeight: Integer;
|
|
|
|
FTexture: GLuint;
|
|
|
|
FGraphic: TSingleImage;
|
|
|
|
public
|
|
|
|
property Width: Integer read FWidth;
|
|
|
|
property Height: Integer read FHeight;
|
|
|
|
property RealWidth: Integer read FRealWidth;
|
|
|
|
property RealHeight: Integer read FRealHeight;
|
|
|
|
property Texture: GLuint read FTexture;
|
|
|
|
property Graphic: TSingleImage read FGraphic;
|
|
|
|
|
|
|
|
function HitTest(AX, AY: Integer): Boolean;
|
|
|
|
procedure UpdateTexture;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TLandTextureManager }
|
|
|
|
|
2009-10-02 23:52:02 +02:00
|
|
|
TLandTextureManager = class
|
2008-08-29 12:09:26 +02:00
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
2009-12-03 00:04:19 +01:00
|
|
|
protected
|
|
|
|
FArtCache: TCacheManager;
|
|
|
|
FFlatLandArtCache: TCacheManager;
|
|
|
|
FTexCache: TCacheManager;
|
|
|
|
public
|
2008-08-29 12:09:26 +02:00
|
|
|
function GetArtMaterial(ATileID: Word): TMaterial; overload;
|
2009-08-02 20:45:23 +02:00
|
|
|
function GetArtMaterial(ATileID: Word; AHue: THue;
|
|
|
|
APartialHue: Boolean): TMaterial; overload;
|
2008-08-29 12:09:26 +02:00
|
|
|
function GetFlatLandMaterial(ATileID: Word): TMaterial;
|
2009-12-03 00:04:19 +01:00
|
|
|
function GetStaticMaterial(AStaticItem: TStaticItem;
|
|
|
|
AOverrideHue: Integer = -1): TMaterial;
|
2008-08-29 12:09:26 +02:00
|
|
|
function GetTexMaterial(ATileID: Word): TMaterial;
|
|
|
|
end;
|
2009-08-02 19:08:56 +02:00
|
|
|
|
|
|
|
{ TSeperatedStaticBlock }
|
|
|
|
|
|
|
|
TSeperatedStaticBlock = class(TStaticBlock)
|
|
|
|
constructor Create(AData: TStream; AIndex: TGenericIndex; AX, AY: Word); overload;
|
|
|
|
constructor Create(AData: TStream; AIndex: TGenericIndex); overload;
|
|
|
|
destructor Destroy; override;
|
|
|
|
public
|
2009-12-02 19:14:00 +01:00
|
|
|
Cells: array[0..63] of TStaticItemList;
|
2009-08-02 19:08:56 +02:00
|
|
|
{ Methods }
|
|
|
|
function Clone: TSeperatedStaticBlock; override;
|
|
|
|
function GetSize: Integer; override;
|
|
|
|
procedure RebuildList;
|
|
|
|
end;
|
2009-10-02 23:52:02 +02:00
|
|
|
|
|
|
|
TLandscape = class;
|
2008-08-29 12:09:26 +02:00
|
|
|
|
|
|
|
{ TBlock }
|
|
|
|
|
2009-10-02 23:52:02 +02:00
|
|
|
TBlock = class
|
2008-08-29 12:09:26 +02:00
|
|
|
constructor Create(AMap: TMapBlock; AStatics: TStaticBlock);
|
|
|
|
destructor Destroy; override;
|
|
|
|
protected
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Fields }
|
2008-08-29 12:09:26 +02:00
|
|
|
FMapBlock: TMapBlock;
|
|
|
|
FStaticBlock: TStaticBlock;
|
|
|
|
public
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Fields }
|
2008-08-29 12:09:26 +02:00
|
|
|
property Map: TMapBlock read FMapBlock;
|
|
|
|
property Static: TStaticBlock read FStaticBlock;
|
2009-08-02 13:45:26 +02:00
|
|
|
{ Methods }
|
2009-10-02 23:52:02 +02:00
|
|
|
procedure UpdateBlockAcess(ALandscape: TLandscape);
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TLandscapeChangeEvent = procedure of object;
|
2009-08-03 19:32:03 +02:00
|
|
|
TMapChangedEvent = procedure(AMapCell: TMapCell) of object;
|
2009-08-02 20:45:23 +02:00
|
|
|
TNewBlockEvent = procedure(ABlock: TBlock) of object;
|
|
|
|
TStaticChangedEvent = procedure(AStaticItem: TStaticItem) of object;
|
2008-08-29 12:09:26 +02:00
|
|
|
|
2009-05-17 14:12:52 +02:00
|
|
|
TScreenBuffer = class;
|
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
{ TLandscape }
|
|
|
|
|
2009-10-02 23:52:02 +02:00
|
|
|
TLandscape = class
|
2008-08-29 12:09:26 +02:00
|
|
|
constructor Create(AWidth, AHeight: Word);
|
|
|
|
destructor Destroy; override;
|
|
|
|
protected
|
2009-05-15 23:37:10 +02:00
|
|
|
{ Members }
|
2008-08-29 12:09:26 +02:00
|
|
|
FWidth: Word;
|
|
|
|
FHeight: Word;
|
|
|
|
FCellWidth: Word;
|
|
|
|
FCellHeight: Word;
|
|
|
|
FBlockCache: TCacheManager;
|
|
|
|
FOnChange: TLandscapeChangeEvent;
|
2009-08-03 19:32:03 +02:00
|
|
|
FOnMapChanged: TMapChangedEvent;
|
2009-08-02 20:45:23 +02:00
|
|
|
FOnNewBlock: TNewBlockEvent;
|
|
|
|
FOnStaticInserted: TStaticChangedEvent;
|
|
|
|
FOnStaticDeleted: TStaticChangedEvent;
|
|
|
|
FOnStaticElevated: TStaticChangedEvent;
|
|
|
|
FOnStaticHued: TStaticChangedEvent;
|
2009-10-02 18:18:59 +02:00
|
|
|
FOpenRequests: TBits;
|
2009-10-02 23:52:02 +02:00
|
|
|
FWriteMap: TBits;
|
2009-05-15 23:37:10 +02:00
|
|
|
{ Methods }
|
2008-08-29 12:09:26 +02:00
|
|
|
function GetMapBlock(AX, AY: Word): TMapBlock;
|
2009-05-17 14:12:52 +02:00
|
|
|
function GetMapCell(AX, AY: Word): TMapCell;
|
|
|
|
function GetNormals(AX, AY: Word): TNormals;
|
2008-08-29 12:09:26 +02:00
|
|
|
function GetStaticBlock(AX, AY: Word): TSeperatedStaticBlock;
|
2009-12-02 19:14:00 +01:00
|
|
|
function GetStaticList(AX, AY: Word): TStaticItemList;
|
2009-05-17 14:12:52 +02:00
|
|
|
{ Events }
|
2008-08-29 12:09:26 +02:00
|
|
|
procedure OnRemoveCachedObject(AObject: TObject);
|
|
|
|
procedure OnBlocksPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
procedure OnDrawMapPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
procedure OnInsertStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
procedure OnDeleteStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
procedure OnElevateStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
procedure OnMoveStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
procedure OnHueStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
public
|
2009-05-15 23:37:10 +02:00
|
|
|
{ Fields }
|
2008-08-29 12:09:26 +02:00
|
|
|
property Width: Word read FWidth;
|
|
|
|
property Height: Word read FHeight;
|
|
|
|
property CellWidth: Word read FCellWidth;
|
|
|
|
property CellHeight: Word read FCellHeight;
|
|
|
|
property MapCell[X, Y: Word]: TMapCell read GetMapCell;
|
2009-12-02 19:14:00 +01:00
|
|
|
property StaticList[X, Y: Word]: TStaticItemList read GetStaticList;
|
2008-08-29 12:09:26 +02:00
|
|
|
property Normals[X, Y: Word]: TNormals read GetNormals;
|
|
|
|
property OnChange: TLandscapeChangeEvent read FOnChange write FOnChange;
|
2009-08-03 19:32:03 +02:00
|
|
|
property OnMapChanged: TMapChangedEvent read FOnMapChanged write FOnMapChanged;
|
2009-08-02 20:45:23 +02:00
|
|
|
property OnNewBlock: TNewBlockEvent read FOnNewBlock write FOnNewBlock;
|
|
|
|
property OnStaticInserted: TStaticChangedEvent read FOnStaticInserted
|
|
|
|
write FOnStaticInserted;
|
|
|
|
property OnStaticDeleted: TStaticChangedEvent read FOnStaticDeleted
|
|
|
|
write FOnStaticDeleted;
|
|
|
|
property OnStaticElevated: TStaticChangedEvent read FOnStaticElevated
|
|
|
|
write FOnStaticElevated;
|
|
|
|
property OnStaticHued: TStaticChangedEvent read FOnStaticHued
|
|
|
|
write FOnStaticHued;
|
2009-05-15 23:37:10 +02:00
|
|
|
{ Methods }
|
2009-10-02 23:52:02 +02:00
|
|
|
function CanWrite(AX, AY: Word): Boolean;
|
2009-05-17 14:12:52 +02:00
|
|
|
procedure FillDrawList(ADrawList: TScreenBuffer; AX, AY, AWidth,
|
2009-10-02 18:10:21 +02:00
|
|
|
AHeight: Word; AMap, AStatics: Boolean; ANoDraw: Boolean;
|
2009-12-02 19:14:00 +01:00
|
|
|
AAdditionalTiles: TWorldItemList = nil);
|
2008-08-29 12:09:26 +02:00
|
|
|
function GetEffectiveAltitude(ATile: TMapCell): ShortInt;
|
|
|
|
function GetLandAlt(AX, AY: Word; ADefault: ShortInt): ShortInt;
|
2009-05-17 22:06:42 +02:00
|
|
|
procedure GetNormals(AX, AY: Word; var ANormals: TNormals);
|
2008-08-29 12:09:26 +02:00
|
|
|
procedure MoveStatic(AStatic: TStaticItem; AX, AY: Word);
|
|
|
|
procedure PrepareBlocks(AX1, AY1, AX2, AY2: Word);
|
2009-08-02 13:45:26 +02:00
|
|
|
procedure UpdateBlockAccess;
|
2009-10-02 23:52:02 +02:00
|
|
|
procedure UpdateWriteMap(AStream: TEnhancedMemoryStream);
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
2009-05-17 15:32:51 +02:00
|
|
|
|
2009-12-05 17:26:22 +01:00
|
|
|
{ TGLText }
|
|
|
|
|
|
|
|
TGLText = class
|
|
|
|
constructor Create(AFont: TGLFont; AText: String);
|
|
|
|
protected
|
|
|
|
FFont: TGLFont;
|
|
|
|
FText: String;
|
|
|
|
FWidth: Integer;
|
|
|
|
FHeight: Integer;
|
|
|
|
public
|
|
|
|
procedure Render(AScreenRect: TRect);
|
|
|
|
end;
|
|
|
|
|
2009-05-18 00:33:59 +02:00
|
|
|
TScreenState = (ssNormal, ssFiltered, ssGhost);
|
2009-05-17 15:32:51 +02:00
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
PBlockInfo = ^TBlockInfo;
|
|
|
|
TBlockInfo = record
|
|
|
|
ScreenRect: TRect;
|
2009-12-04 15:52:27 +01:00
|
|
|
DrawQuad: array[0..3,0..1] of TGLint;
|
|
|
|
RealQuad: array[0..3,0..1] of TGLint;
|
2008-08-29 12:09:26 +02:00
|
|
|
Item: TWorldItem;
|
2009-05-17 15:32:51 +02:00
|
|
|
HighRes: TMaterial;
|
|
|
|
LowRes: TMaterial;
|
|
|
|
Normals: PNormals;
|
|
|
|
State: TScreenState;
|
2009-07-30 20:31:29 +02:00
|
|
|
Highlighted: Boolean;
|
2009-12-03 00:04:19 +01:00
|
|
|
HueOverride: Boolean;
|
2009-12-04 15:52:27 +01:00
|
|
|
CheckRealQuad: Boolean;
|
2009-12-05 01:24:58 +01:00
|
|
|
Translucent: Boolean;
|
2009-12-05 17:26:22 +01:00
|
|
|
Text: TGLText;
|
2008-08-29 12:09:26 +02:00
|
|
|
Next: PBlockInfo;
|
|
|
|
end;
|
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
{ TScreenBuffer }
|
2008-08-29 12:09:26 +02:00
|
|
|
|
2009-10-02 23:52:02 +02:00
|
|
|
TScreenBuffer = class
|
2008-08-29 12:09:26 +02:00
|
|
|
constructor Create; virtual;
|
|
|
|
destructor Destroy; override;
|
2009-05-17 14:12:52 +02:00
|
|
|
protected
|
|
|
|
{ Members }
|
2009-05-20 18:16:18 +02:00
|
|
|
FCount: Cardinal;
|
|
|
|
FShortCuts: array[-1..10] of PBlockInfo; //-1 = last, 0 = first, 1..10 = other shortcuts
|
|
|
|
FShortCutsValid: Boolean;
|
2009-05-17 14:12:52 +02:00
|
|
|
FSerial: Cardinal;
|
2008-08-29 12:09:26 +02:00
|
|
|
public
|
2009-05-17 14:12:52 +02:00
|
|
|
{ Methods }
|
2009-05-20 18:16:18 +02:00
|
|
|
function Add(AItem: TWorldItem): PBlockInfo;
|
|
|
|
procedure Clear;
|
|
|
|
procedure Delete(AItem: TWorldItem);
|
2008-08-29 12:09:26 +02:00
|
|
|
function Find(AScreenPosition: TPoint): PBlockInfo;
|
2009-05-17 14:12:52 +02:00
|
|
|
function GetSerial: Cardinal;
|
2009-05-20 18:16:18 +02:00
|
|
|
function Insert(AItem: TWorldItem): PBlockInfo;
|
2009-08-02 13:45:26 +02:00
|
|
|
function Iterate(var ABlockInfo: PBlockInfo): Boolean;
|
2009-05-20 18:16:18 +02:00
|
|
|
procedure UpdateShortcuts;
|
2009-08-03 18:40:31 +02:00
|
|
|
function UpdateSortOrder(AItem: TWorldItem): PBlockInfo;
|
2009-05-17 14:12:52 +02:00
|
|
|
{ Events }
|
|
|
|
procedure OnTileRemoved(ATile: TMulBlock);
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TStaticInfo = packed record
|
|
|
|
X: Word;
|
|
|
|
Y: Word;
|
|
|
|
Z: ShortInt;
|
|
|
|
TileID: Word;
|
|
|
|
Hue: Word;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2009-10-02 23:52:02 +02:00
|
|
|
UGameResources, UdmNetwork, UPackets, UPacketHandlers, Logging;
|
2008-08-29 12:09:26 +02:00
|
|
|
|
|
|
|
function GetID(AX, AY: Word): Integer;
|
|
|
|
begin
|
|
|
|
Result := ((AX and $7FFF) shl 15) or (AY and $7FFF);
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TLandTextureManager }
|
|
|
|
|
|
|
|
constructor TLandTextureManager.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FArtCache := TCacheManager.Create(1024);
|
|
|
|
FFlatLandArtCache := TCacheManager.Create(128);
|
|
|
|
FTexCache := TCacheManager.Create(128);
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TLandTextureManager.Destroy;
|
|
|
|
begin
|
|
|
|
if FArtCache <> nil then FreeAndNil(FArtCache);
|
|
|
|
if FFlatLandArtCache <> nil then FreeAndNil(FFlatLandArtCache);
|
|
|
|
if FTexCache <> nil then FreeAndNil(FTexCache);
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TLandTextureManager.GetArtMaterial(ATileID: Word): TMaterial;
|
|
|
|
var
|
|
|
|
artEntry: TArt;
|
|
|
|
begin
|
|
|
|
if not FArtCache.QueryID(ATileID, TObject(Result)) then
|
|
|
|
begin
|
|
|
|
artEntry := TArt(ResMan.Art.Block[ATileID]);
|
|
|
|
|
|
|
|
Result := TMaterial.Create(artEntry.Graphic.Width, artEntry.Graphic.Height,
|
|
|
|
artEntry.Graphic);
|
|
|
|
FArtCache.StoreID(ATileID, Result);
|
|
|
|
|
|
|
|
artEntry.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-12-03 00:04:19 +01:00
|
|
|
function TLandTextureManager.GetArtMaterial(ATileID: Word; AHue: THue;
|
|
|
|
APartialHue: Boolean): TMaterial;
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
|
|
|
artEntry: TArt;
|
|
|
|
id: Integer;
|
|
|
|
begin
|
|
|
|
if AHue = nil then
|
|
|
|
begin
|
|
|
|
Result := GetArtMaterial(ATileID);
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
id := ATileID or ((AHue.ID and $3FFF) shl 15) or (Byte(APartialHue) shl 29);
|
|
|
|
if not FArtCache.QueryID(id, TObject(Result)) then
|
|
|
|
begin
|
|
|
|
artEntry := ResMan.Art.GetArt(ATileID, 0, AHue, APartialHue);
|
|
|
|
|
|
|
|
Result := TMaterial.Create(artEntry.Graphic.Width, artEntry.Graphic.Height,
|
|
|
|
artEntry.Graphic);
|
|
|
|
FArtCache.StoreID(id, Result);
|
|
|
|
|
|
|
|
artEntry.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TLandTextureManager.GetFlatLandMaterial(ATileID: Word): TMaterial;
|
|
|
|
var
|
|
|
|
artEntry: TArt;
|
|
|
|
begin
|
|
|
|
if not FFlatLandArtCache.QueryID(ATileID, TObject(Result)) then
|
|
|
|
begin
|
|
|
|
artEntry := ResMan.Art.GetFlatLand(ATileID);
|
|
|
|
|
|
|
|
Result := TMaterial.Create(artEntry.Graphic.Width, artEntry.Graphic.Height,
|
|
|
|
artEntry.Graphic);
|
|
|
|
FFlatLandArtCache.StoreID(ATileID, Result);
|
|
|
|
|
|
|
|
artEntry.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-12-03 00:04:19 +01:00
|
|
|
function TLandTextureManager.GetStaticMaterial(AStaticItem: TStaticItem;
|
|
|
|
AOverrideHue: Integer = -1): TMaterial;
|
|
|
|
var
|
|
|
|
staticTiledata: TStaticTiledata;
|
|
|
|
hue: THue;
|
|
|
|
begin
|
|
|
|
staticTiledata := ResMan.Tiledata.StaticTiles[AStaticItem.TileID];
|
|
|
|
if AOverrideHue < 0 then
|
|
|
|
AOverrideHue := AStaticItem.Hue;
|
|
|
|
|
|
|
|
if AOverrideHue > 0 then
|
|
|
|
hue := ResMan.Hue.Hues[AOverrideHue]
|
|
|
|
else
|
|
|
|
hue := nil;
|
|
|
|
|
|
|
|
Result := GetArtMaterial($4000 + AStaticItem.TileID, hue,
|
|
|
|
tdfPartialHue in staticTiledata.Flags);
|
|
|
|
end;
|
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
function TLandTextureManager.GetTexMaterial(ATileID: Word): TMaterial;
|
|
|
|
var
|
|
|
|
texEntry: TTexture;
|
|
|
|
texID: Integer;
|
|
|
|
begin
|
|
|
|
if not FTexCache.QueryID(ATileID, TObject(Result)) then
|
|
|
|
begin
|
|
|
|
texID := ResMan.Tiledata.LandTiles[ATileID].TextureID;
|
|
|
|
if texID > 0 then
|
|
|
|
begin
|
|
|
|
texEntry := TTexture(ResMan.Texmaps.Block[texID]);
|
|
|
|
|
|
|
|
Result := TMaterial.Create(texEntry.Graphic.Width, texEntry.Graphic.Height,
|
|
|
|
texEntry.Graphic);
|
|
|
|
FTexCache.StoreID(ATileID, Result);
|
|
|
|
|
|
|
|
texEntry.Free;
|
|
|
|
end else
|
|
|
|
Result := nil;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-08-02 19:08:56 +02:00
|
|
|
{ TSeperatedStaticBlock }
|
|
|
|
|
|
|
|
constructor TSeperatedStaticBlock.Create(AData: TStream; AIndex: TGenericIndex;
|
|
|
|
AX, AY: Word);
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
item: TStaticItem;
|
|
|
|
block: TMemoryStream;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
2009-12-02 19:14:00 +01:00
|
|
|
FItems := TStaticItemList.Create(False);
|
2009-08-02 19:08:56 +02:00
|
|
|
|
|
|
|
FX := AX;
|
|
|
|
FY := AY;
|
|
|
|
|
|
|
|
for i := 0 to 63 do
|
2009-12-02 19:14:00 +01:00
|
|
|
Cells[i] := TStaticItemList.Create;
|
2009-08-02 19:08:56 +02:00
|
|
|
|
|
|
|
if (AData <> nil) and (AIndex.Lookup > 0) and (AIndex.Size > 0) then
|
|
|
|
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
|
|
|
|
begin
|
|
|
|
item := TStaticItem.Create(Self, block, AX, AY);
|
|
|
|
Cells[(item.Y mod 8) * 8 + (item.X mod 8)].Add(item);
|
|
|
|
end;
|
|
|
|
block.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TSeperatedStaticBlock.Create(AData: TStream; AIndex: TGenericIndex);
|
|
|
|
begin
|
|
|
|
Create(AData, AIndex, 0, 0);
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TSeperatedStaticBlock.Destroy;
|
|
|
|
var
|
2009-12-03 01:35:29 +01:00
|
|
|
i: Integer;
|
2009-08-02 19:08:56 +02:00
|
|
|
begin
|
|
|
|
FreeAndNil(FItems);
|
|
|
|
|
|
|
|
for i := 0 to 63 do
|
|
|
|
begin
|
|
|
|
if Cells[i] <> nil then
|
2009-12-02 19:14:00 +01:00
|
|
|
FreeAndNil(Cells[i]);
|
2009-08-02 19:08:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TSeperatedStaticBlock.Clone: TSeperatedStaticBlock;
|
|
|
|
begin
|
|
|
|
raise Exception.Create('TSeperatedStaticBlock.Clone is not implemented (yet).');
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TSeperatedStaticBlock.GetSize: Integer;
|
|
|
|
begin
|
|
|
|
RebuildList;
|
|
|
|
Result := inherited GetSize;
|
|
|
|
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(
|
|
|
|
ResMan.Tiledata.StaticTiles[TStaticItem(Cells[i].Items[j]).TileID],
|
|
|
|
solver);
|
|
|
|
Inc(solver);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Sort;
|
|
|
|
end;
|
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
{ TBlock }
|
|
|
|
|
|
|
|
constructor TBlock.Create(AMap: TMapBlock; AStatics: TStaticBlock);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FMapBlock := AMap;
|
|
|
|
FStaticBlock := AStatics;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TBlock.Destroy;
|
|
|
|
begin
|
|
|
|
if FMapBlock <> nil then FreeAndNil(FMapBlock);
|
|
|
|
if FStaticBlock <> nil then FreeAndNil(FStaticBlock);
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2009-10-02 23:52:02 +02:00
|
|
|
procedure TBlock.UpdateBlockAcess(ALandscape: TLandscape);
|
2009-08-02 13:45:26 +02:00
|
|
|
var
|
|
|
|
staticItem: TStaticItem;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
for i := Low(FMapBlock.Cells) to High(FMapBlock.Cells) do
|
|
|
|
begin
|
2009-10-02 23:52:02 +02:00
|
|
|
FMapBlock.Cells[i].CanBeEdited := ALandscape.CanWrite(FMapBlock.Cells[i].X,
|
|
|
|
FMapBlock.Cells[i].Y);
|
2009-08-02 13:45:26 +02:00
|
|
|
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]);
|
2009-10-02 23:52:02 +02:00
|
|
|
staticItem.CanBeEdited := ALandscape.CanWrite(staticItem.X,
|
|
|
|
staticItem.Y);
|
2009-08-02 13:45:26 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
{ TLandscape }
|
|
|
|
|
|
|
|
constructor TLandscape.Create(AWidth, AHeight: Word);
|
|
|
|
var
|
2009-12-03 01:35:29 +01:00
|
|
|
i: Integer;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FWidth := AWidth;
|
|
|
|
FHeight := AHeight;
|
|
|
|
FCellWidth := FWidth * 8;
|
|
|
|
FCellHeight := FHeight * 8;
|
|
|
|
FBlockCache := TCacheManager.Create(256);
|
|
|
|
FBlockCache.OnRemoveObject := @OnRemoveCachedObject;
|
2009-08-02 20:45:23 +02:00
|
|
|
|
|
|
|
FOnChange := nil;
|
2009-08-03 00:29:55 +02:00
|
|
|
FOnNewBlock := nil;
|
2009-08-02 20:45:23 +02:00
|
|
|
FOnStaticDeleted := nil;
|
|
|
|
FOnStaticElevated := nil;
|
|
|
|
FOnStaticHued := nil;
|
|
|
|
FOnStaticInserted := nil;
|
2009-10-02 18:18:59 +02:00
|
|
|
|
|
|
|
FOpenRequests := TBits.Create(FWidth * FHeight);
|
2009-10-02 23:52:02 +02:00
|
|
|
FOpenRequests.Clearall; //set all to 0
|
|
|
|
FWriteMap := TBits.Create(FCellWidth * FCellHeight);
|
|
|
|
for i := 0 to FWriteMap.Size - 1 do
|
|
|
|
FWriteMap[i] := True;
|
2009-05-17 14:12:52 +02:00
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
RegisterPacketHandler($04, TPacketHandler.Create(0, @OnBlocksPacket));
|
|
|
|
RegisterPacketHandler($06, TPacketHandler.Create(8, @OnDrawMapPacket));
|
|
|
|
RegisterPacketHandler($07, TPacketHandler.Create(10, @OnInsertStaticPacket));
|
|
|
|
RegisterPacketHandler($08, TPacketHandler.Create(10, @OnDeleteStaticPacket));
|
|
|
|
RegisterPacketHandler($09, TPacketHandler.Create(11, @OnElevateStaticPacket));
|
|
|
|
RegisterPacketHandler($0A, TPacketHandler.Create(14, @OnMoveStaticPacket));
|
|
|
|
RegisterPacketHandler($0B, TPacketHandler.Create(12, @OnHueStaticPacket));
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TLandscape.Destroy;
|
|
|
|
begin
|
|
|
|
if FBlockCache <> nil then
|
|
|
|
begin
|
|
|
|
FBlockCache.OnRemoveObject := nil;
|
|
|
|
FreeAndNil(FBlockCache);
|
|
|
|
end;
|
2009-10-02 23:52:02 +02:00
|
|
|
|
|
|
|
FreeAndNil(FOpenRequests);
|
|
|
|
FreeAndNil(FWriteMap);
|
2008-08-29 12:09:26 +02:00
|
|
|
|
|
|
|
RegisterPacketHandler($04, nil);
|
|
|
|
RegisterPacketHandler($06, nil);
|
|
|
|
RegisterPacketHandler($07, nil);
|
|
|
|
RegisterPacketHandler($08, nil);
|
|
|
|
RegisterPacketHandler($09, nil);
|
|
|
|
RegisterPacketHandler($0A, nil);
|
|
|
|
RegisterPacketHandler($0B, nil);
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2009-05-17 14:12:52 +02:00
|
|
|
function TLandscape.GetMapBlock(AX, AY: Word): TMapBlock;
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
2009-05-17 14:12:52 +02:00
|
|
|
block: TBlock;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
|
|
|
Result := nil;
|
2009-05-17 14:12:52 +02:00
|
|
|
if (AX >= 0) and (AX < FWidth) and (AY >= 0) and (AY < FHeight) then
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
2009-05-17 14:12:52 +02:00
|
|
|
if FBlockCache.QueryID(GetID(AX, AY), TObject(block)) then
|
|
|
|
Result := block.Map;
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-05-17 14:12:52 +02:00
|
|
|
function TLandscape.GetMapCell(AX, AY: Word): TMapCell;
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
2009-05-17 14:12:52 +02:00
|
|
|
block: TMapBlock;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
if (AX >= 0) and (AX <= FCellWidth) and (AY >= 0) and (AY <= FCellHeight) then
|
|
|
|
begin
|
2009-05-17 14:12:52 +02:00
|
|
|
block := GetMapBlock(AX div 8, AY div 8);
|
2008-08-29 12:09:26 +02:00
|
|
|
if block <> nil then
|
|
|
|
Result := block.Cells[(AY mod 8) * 8 + AX mod 8];
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TLandscape.GetNormals(AX, AY: Word): TNormals;
|
|
|
|
begin
|
2009-05-17 22:06:42 +02:00
|
|
|
GetNormals(AX, AY, Result);
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
|
2009-05-17 14:12:52 +02:00
|
|
|
function TLandscape.GetStaticBlock(AX, AY: Word): TSeperatedStaticBlock;
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
|
|
|
block: TBlock;
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
if (AX >= 0) and (AX < FWidth) and (AY >= 0) and (AY < FHeight) then
|
|
|
|
begin
|
|
|
|
if FBlockCache.QueryID(GetID(AX, AY), TObject(block)) then
|
2009-05-17 14:12:52 +02:00
|
|
|
Result := TSeperatedStaticBlock(block.Static);
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
function TLandscape.GetStaticList(AX, AY: Word): TStaticItemList;
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
2009-05-17 14:12:52 +02:00
|
|
|
block: TSeperatedStaticBlock;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
|
|
|
Result := nil;
|
2009-05-17 14:12:52 +02:00
|
|
|
if (AX >= 0) and (AX <= FCellWidth) and (AY >= 0) and (AY <= FCellHeight) then
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
2009-05-17 14:12:52 +02:00
|
|
|
block := GetStaticBlock(AX div 8, AY div 8);
|
|
|
|
if block <> nil then
|
|
|
|
Result := block.Cells[(AY mod 8) * 8 + AX mod 8];
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-05-17 14:12:52 +02:00
|
|
|
procedure TLandscape.OnRemoveCachedObject(AObject: TObject);
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
2009-05-17 14:12:52 +02:00
|
|
|
block: TBlock;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
2009-05-17 14:12:52 +02:00
|
|
|
block := AObject as TBlock;
|
|
|
|
if block <> nil then
|
|
|
|
dmNetwork.Send(TFreeBlockPacket.Create(block.Map.X, block.Map.Y));
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TLandscape.OnBlocksPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
var
|
|
|
|
index: TGenericIndex;
|
|
|
|
map: TMapBlock;
|
|
|
|
statics: TStaticBlock;
|
|
|
|
coords: TBlockCoords;
|
|
|
|
count: Word;
|
|
|
|
id: Integer;
|
2009-08-03 00:29:55 +02:00
|
|
|
block: TBlock;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
|
|
|
index := TGenericIndex.Create(nil);
|
|
|
|
while ABuffer.Position < ABuffer.Size do
|
|
|
|
begin
|
|
|
|
ABuffer.Read(coords, SizeOf(TBlockCoords));
|
|
|
|
id := GetID(coords.X, coords.Y);
|
2009-05-17 14:12:52 +02:00
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
map := TMapBlock.Create(ABuffer, coords.X, coords.Y);
|
|
|
|
count := ABuffer.ReadWord;
|
|
|
|
if count > 0 then
|
|
|
|
index.Lookup := ABuffer.Position
|
|
|
|
else
|
2009-05-17 14:12:52 +02:00
|
|
|
index.Lookup := -1;
|
2008-08-29 12:09:26 +02:00
|
|
|
index.Size := count * 7;
|
|
|
|
statics := TSeperatedStaticBlock.Create(ABuffer, index, coords.X, coords.Y);
|
|
|
|
|
|
|
|
FBlockCache.RemoveID(id);
|
2009-08-03 00:29:55 +02:00
|
|
|
block := TBlock.Create(map, statics);
|
2009-10-02 23:52:02 +02:00
|
|
|
block.UpdateBlockAcess(Self);
|
2009-08-03 00:29:55 +02:00
|
|
|
FBlockCache.StoreID(id, block);
|
2009-05-17 14:12:52 +02:00
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
FOpenRequests[coords.Y * FWidth + coords.X] := False;
|
2009-08-03 00:29:55 +02:00
|
|
|
|
|
|
|
if Assigned(FOnNewBlock) then FOnNewBlock(block);
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
index.Free;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TLandscape.OnDrawMapPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
var
|
|
|
|
x, y: Word;
|
|
|
|
cell: TMapCell;
|
|
|
|
begin
|
|
|
|
x := ABuffer.ReadWord;
|
|
|
|
y := ABuffer.ReadWord;
|
|
|
|
cell := GetMapCell(x, y);
|
|
|
|
if cell <> nil then
|
|
|
|
begin
|
|
|
|
cell.Altitude := ABuffer.ReadShortInt;
|
|
|
|
cell.TileID := ABuffer.ReadWord;
|
2009-08-03 19:32:03 +02:00
|
|
|
if Assigned(FOnMapChanged) then FOnMapChanged(cell);
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TLandscape.OnInsertStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
var
|
|
|
|
x, y: Word;
|
|
|
|
block: TSeperatedStaticBlock;
|
|
|
|
staticItem: TStaticItem;
|
2009-12-02 19:14:00 +01:00
|
|
|
targetStaticList: TStaticItemList;
|
2008-08-29 12:09:26 +02:00
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
x := ABuffer.ReadWord;
|
|
|
|
y := ABuffer.ReadWord;
|
|
|
|
block := GetStaticBlock(x div 8, y div 8);
|
|
|
|
if block <> nil then
|
|
|
|
begin
|
|
|
|
staticItem := TStaticItem.Create(nil, nil, 0, 0);
|
|
|
|
staticItem.X := x;
|
|
|
|
staticItem.Y := y;
|
|
|
|
staticItem.Z := ABuffer.ReadShortInt;
|
|
|
|
staticItem.TileID := ABuffer.ReadWord;
|
|
|
|
staticItem.Hue := ABuffer.ReadWord;
|
|
|
|
targetStaticList := block.Cells[(y mod 8) * 8 + x mod 8];
|
|
|
|
targetStaticList.Add(staticItem);
|
|
|
|
for i := 0 to targetStaticList.Count - 1 do
|
2009-12-02 19:14:00 +01:00
|
|
|
targetStaticList.Items[i].UpdatePriorities(
|
|
|
|
ResMan.Tiledata.StaticTiles[targetStaticList.Items[i].TileID],
|
2009-08-02 19:08:56 +02:00
|
|
|
i);
|
2009-12-02 19:14:00 +01:00
|
|
|
targetStaticList.Sort(@CompareStaticItems);
|
2008-08-29 12:09:26 +02:00
|
|
|
staticItem.Owner := block;
|
2009-10-02 23:52:02 +02:00
|
|
|
staticItem.CanBeEdited := CanWrite(x, y);
|
2009-08-02 20:45:23 +02:00
|
|
|
|
|
|
|
if Assigned(FOnStaticInserted) then FOnStaticInserted(staticItem);
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TLandscape.OnDeleteStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
var
|
|
|
|
block: TSeperatedStaticBlock;
|
|
|
|
i: Integer;
|
2009-12-02 19:14:00 +01:00
|
|
|
statics: TStaticItemList;
|
2008-08-29 12:09:26 +02:00
|
|
|
staticInfo: TStaticInfo;
|
|
|
|
staticItem: TStaticItem;
|
|
|
|
begin
|
|
|
|
ABuffer.Read(staticInfo, SizeOf(TStaticInfo));
|
|
|
|
block := GetStaticBlock(staticInfo.X div 8, staticInfo.Y div 8);
|
|
|
|
if block <> nil then
|
|
|
|
begin
|
|
|
|
statics := block.Cells[(staticInfo.Y mod 8) * 8 + staticInfo.X mod 8];
|
|
|
|
for i := 0 to statics.Count - 1 do
|
|
|
|
begin
|
2009-12-02 19:14:00 +01:00
|
|
|
staticItem := statics.Items[i];
|
2008-08-29 12:09:26 +02:00
|
|
|
if (staticItem.Z = staticInfo.Z) and
|
|
|
|
(staticItem.TileID = staticInfo.TileID) and
|
|
|
|
(staticItem.Hue = staticInfo.Hue) then
|
|
|
|
begin
|
2009-08-02 20:45:23 +02:00
|
|
|
if Assigned(FOnStaticDeleted) then FOnStaticDeleted(staticItem);
|
2008-08-29 12:09:26 +02:00
|
|
|
staticItem.Delete;
|
2009-12-03 00:04:19 +01:00
|
|
|
statics.Delete(i);
|
2009-08-02 20:45:23 +02:00
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TLandscape.OnElevateStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
var
|
|
|
|
block: TSeperatedStaticBlock;
|
|
|
|
i,j : Integer;
|
2009-12-02 19:14:00 +01:00
|
|
|
statics: TStaticItemList;
|
2008-08-29 12:09:26 +02:00
|
|
|
staticInfo: TStaticInfo;
|
|
|
|
staticItem: TStaticItem;
|
|
|
|
begin
|
|
|
|
ABuffer.Read(staticInfo, SizeOf(TStaticInfo));
|
|
|
|
block := GetStaticBlock(staticInfo.X div 8, staticInfo.Y div 8);
|
|
|
|
if block <> nil then
|
|
|
|
begin
|
|
|
|
statics := block.Cells[(staticInfo.Y mod 8) * 8 + staticInfo.X mod 8];
|
|
|
|
for i := 0 to statics.Count - 1 do
|
|
|
|
begin
|
2009-12-02 19:14:00 +01:00
|
|
|
staticItem := statics.Items[i];
|
2008-08-29 12:09:26 +02:00
|
|
|
if (staticItem.Z = staticInfo.Z) and
|
|
|
|
(staticItem.TileID = staticInfo.TileID) and
|
|
|
|
(staticItem.Hue = staticInfo.Hue) then
|
|
|
|
begin
|
|
|
|
staticItem.Z := ABuffer.ReadShortInt;
|
|
|
|
for j := 0 to statics.Count - 1 do
|
2009-12-02 19:14:00 +01:00
|
|
|
statics.Items[j].UpdatePriorities(
|
|
|
|
ResMan.Tiledata.StaticTiles[statics.Items[j].TileID],
|
2009-08-02 19:08:56 +02:00
|
|
|
j);
|
2009-12-02 19:14:00 +01:00
|
|
|
statics.Sort(@CompareStaticItems);
|
2009-08-02 20:45:23 +02:00
|
|
|
|
|
|
|
if Assigned(FOnStaticElevated) then FOnStaticElevated(staticItem);
|
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TLandscape.OnMoveStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
var
|
|
|
|
sourceBlock, targetBlock: TSeperatedStaticBlock;
|
|
|
|
i: Integer;
|
2009-12-02 19:14:00 +01:00
|
|
|
statics: TStaticItemList;
|
2008-08-29 12:09:26 +02:00
|
|
|
staticInfo: TStaticInfo;
|
|
|
|
staticItem: TStaticItem;
|
|
|
|
newX, newY: Word;
|
|
|
|
begin
|
|
|
|
staticItem := nil;
|
|
|
|
ABuffer.Read(staticInfo, SizeOf(TStaticInfo));
|
|
|
|
newX := EnsureRange(ABuffer.ReadWord, 0, FCellWidth - 1);
|
|
|
|
newY := EnsureRange(ABuffer.ReadWord, 0, FCellHeight - 1);
|
|
|
|
|
|
|
|
sourceBlock := GetStaticBlock(staticInfo.X div 8, staticInfo.Y div 8);
|
|
|
|
targetBlock := GetStaticBlock(newX div 8, newY div 8);
|
|
|
|
if sourceBlock <> nil then
|
|
|
|
begin
|
|
|
|
statics := sourceBlock.Cells[(staticInfo.Y mod 8) * 8 + staticInfo.X mod 8];
|
|
|
|
i := 0;
|
|
|
|
while (i < statics.Count) and (staticItem = nil) do
|
|
|
|
begin
|
2009-12-02 19:14:00 +01:00
|
|
|
staticItem := statics.Items[i];
|
2008-08-29 12:09:26 +02:00
|
|
|
if (staticItem.Z <> staticInfo.Z) or
|
|
|
|
(staticItem.TileID <> staticInfo.TileID) or
|
|
|
|
(staticItem.Hue <> staticInfo.Hue) then
|
|
|
|
begin
|
|
|
|
staticItem := nil;
|
|
|
|
end;
|
|
|
|
Inc(i);
|
|
|
|
end;
|
|
|
|
|
|
|
|
if staticItem <> nil then
|
|
|
|
begin
|
2009-08-02 20:45:23 +02:00
|
|
|
if Assigned(FOnStaticDeleted) then FOnStaticDeleted(staticItem);
|
2008-08-29 12:09:26 +02:00
|
|
|
staticItem.Delete;
|
2009-12-03 00:04:19 +01:00
|
|
|
statics.Remove(staticItem);
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
end;
|
2009-05-17 14:12:52 +02:00
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
if targetBlock <> nil then
|
|
|
|
begin
|
|
|
|
staticItem := TStaticItem.Create(nil, nil, 0, 0);
|
|
|
|
staticItem.X := newX;
|
|
|
|
staticItem.Y := newY;
|
|
|
|
staticItem.Z := staticInfo.Z;
|
|
|
|
staticItem.TileID := staticInfo.TileID;
|
|
|
|
staticItem.Hue := staticInfo.Hue;
|
|
|
|
statics := targetBlock.Cells[(newY mod 8) * 8 + newX mod 8];
|
|
|
|
statics.Add(staticItem);
|
|
|
|
for i := 0 to statics.Count - 1 do
|
2009-08-02 19:08:56 +02:00
|
|
|
TStaticItem(statics.Items[i]).UpdatePriorities(
|
|
|
|
ResMan.Tiledata.StaticTiles[TStaticItem(statics.Items[i]).TileID],
|
|
|
|
i);
|
2009-12-02 19:14:00 +01:00
|
|
|
statics.Sort(@CompareStaticItems);
|
2008-08-29 12:09:26 +02:00
|
|
|
staticItem.Owner := targetBlock;
|
2009-10-02 23:52:02 +02:00
|
|
|
staticItem.CanBeEdited := CanWrite(newX, newY);
|
2009-05-17 14:12:52 +02:00
|
|
|
|
2009-08-02 20:45:23 +02:00
|
|
|
if Assigned(FOnStaticInserted) then FOnStaticInserted(staticItem);
|
|
|
|
end;
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TLandscape.OnHueStaticPacket(ABuffer: TEnhancedMemoryStream);
|
|
|
|
var
|
|
|
|
block: TSeperatedStaticBlock;
|
2009-05-17 14:12:52 +02:00
|
|
|
i : Integer;
|
2009-12-02 19:14:00 +01:00
|
|
|
statics: TStaticItemList;
|
2008-08-29 12:09:26 +02:00
|
|
|
staticInfo: TStaticInfo;
|
|
|
|
staticItem: TStaticItem;
|
|
|
|
begin
|
|
|
|
ABuffer.Read(staticInfo, SizeOf(TStaticInfo));
|
|
|
|
block := GetStaticBlock(staticInfo.X div 8, staticInfo.Y div 8);
|
|
|
|
if block <> nil then
|
|
|
|
begin
|
|
|
|
statics := block.Cells[(staticInfo.Y mod 8) * 8 + staticInfo.X mod 8];
|
|
|
|
for i := 0 to statics.Count - 1 do
|
|
|
|
begin
|
2009-12-02 19:14:00 +01:00
|
|
|
staticItem := statics.Items[i];
|
2008-08-29 12:09:26 +02:00
|
|
|
if (staticItem.Z = staticInfo.Z) and
|
|
|
|
(staticItem.TileID = staticInfo.TileID) and
|
|
|
|
(staticItem.Hue = staticInfo.Hue) then
|
|
|
|
begin
|
|
|
|
staticItem.Hue := ABuffer.ReadWord;
|
2009-08-02 20:45:23 +02:00
|
|
|
if Assigned(FOnStaticHued) then FOnStaticHued(staticItem);
|
2008-08-29 12:09:26 +02:00
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-10-02 23:52:02 +02:00
|
|
|
function TLandscape.CanWrite(AX, AY: Word): Boolean;
|
|
|
|
begin
|
|
|
|
Result := FWriteMap[AX * FCellHeight + AY];
|
|
|
|
end;
|
|
|
|
|
2009-05-17 14:12:52 +02:00
|
|
|
procedure TLandscape.FillDrawList(ADrawList: TScreenBuffer; AX, AY, AWidth,
|
2009-10-02 18:10:21 +02:00
|
|
|
AHeight: Word; AMap, AStatics: Boolean; ANoDraw: Boolean;
|
2009-12-02 19:14:00 +01:00
|
|
|
AAdditionalTiles: TWorldItemList = nil);
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
2009-05-17 14:12:52 +02:00
|
|
|
drawMapCell: TMapCell;
|
2009-12-02 19:14:00 +01:00
|
|
|
drawStatics: TStaticItemList;
|
2009-05-17 14:12:52 +02:00
|
|
|
i, x, y: Integer;
|
2009-12-02 19:14:00 +01:00
|
|
|
tempDrawList: TWorldItemList;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
2009-08-04 18:02:39 +02:00
|
|
|
ADrawList.Clear;
|
2009-12-02 19:14:00 +01:00
|
|
|
tempDrawList := TWorldItemList.Create(False);;
|
2009-05-17 14:12:52 +02:00
|
|
|
for x := AX to AX + AWidth do
|
2009-05-20 18:16:18 +02:00
|
|
|
begin
|
2009-05-17 14:12:52 +02:00
|
|
|
for y := AY to AY + AWidth do
|
|
|
|
begin
|
|
|
|
if AMap then
|
|
|
|
begin
|
2009-10-02 18:10:21 +02:00
|
|
|
drawMapCell := GetMapCell(x, y);
|
|
|
|
if (drawMapCell <> nil) and (ANoDraw or (drawMapCell.TileID > 2)) then
|
2009-05-17 14:12:52 +02:00
|
|
|
begin
|
2009-10-02 18:10:21 +02:00
|
|
|
drawMapCell.Priority := GetEffectiveAltitude(drawMapCell);
|
|
|
|
drawMapCell.PriorityBonus := 0;
|
|
|
|
drawMapCell.PrioritySolver := 0;
|
|
|
|
tempDrawList.Add(drawMapCell);
|
2009-05-17 14:12:52 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if AStatics then
|
|
|
|
begin
|
|
|
|
drawStatics := GetStaticList(x, y);
|
|
|
|
if drawStatics <> nil then
|
|
|
|
for i := 0 to drawStatics.Count - 1 do
|
2009-10-02 18:10:21 +02:00
|
|
|
begin
|
2009-12-02 19:14:00 +01:00
|
|
|
drawStatics[i].UpdatePriorities(
|
|
|
|
ResMan.Tiledata.StaticTiles[drawStatics[i].TileID],
|
2009-10-02 18:10:21 +02:00
|
|
|
ADrawList.GetSerial);
|
|
|
|
tempDrawList.Add(drawStatics[i]);
|
|
|
|
end;
|
2009-05-17 14:12:52 +02:00
|
|
|
end;
|
|
|
|
end;
|
2009-05-20 18:16:18 +02:00
|
|
|
end;
|
2009-08-04 18:02:39 +02:00
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
for i := 0 to AAdditionalTiles.Count - 1 do
|
|
|
|
tempDrawList.Add(AAdditionalTiles[i]);
|
2009-09-29 02:25:19 +02:00
|
|
|
|
2009-08-04 18:02:39 +02:00
|
|
|
tempDrawList.Sort(@CompareWorldItems);
|
|
|
|
for i := 0 to tempDrawList.Count - 1 do
|
|
|
|
ADrawList.Add(TWorldItem(tempDrawList[i]));
|
|
|
|
tempDrawList.Free;
|
2009-05-17 14:12:52 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TLandscape.GetEffectiveAltitude(ATile: TMapCell): ShortInt;
|
|
|
|
var
|
|
|
|
north, west, south, east: ShortInt;
|
|
|
|
begin
|
|
|
|
north := ATile.Altitude;
|
|
|
|
west := GetLandAlt(ATile.X, ATile.Y + 1, north);
|
|
|
|
south := GetLandAlt(ATile.X + 1, ATile.Y + 1, north);
|
|
|
|
east := GetLandAlt(ATile.X + 1, ATile.Y, north);
|
|
|
|
|
2009-06-12 12:31:14 +02:00
|
|
|
if Abs(north - south) >= Abs(west - east) then
|
|
|
|
Result := Min(north, south) + Abs(west - east) div 2
|
2009-05-17 14:12:52 +02:00
|
|
|
else
|
2009-06-12 12:31:14 +02:00
|
|
|
Result := Min(north, south) + Abs(north - south) div 2;
|
2009-05-17 14:12:52 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TLandscape.GetLandAlt(AX, AY: Word; ADefault: ShortInt): ShortInt;
|
|
|
|
var
|
|
|
|
cell: TMapCell;
|
|
|
|
begin
|
|
|
|
cell := MapCell[AX, AY];
|
|
|
|
if cell <> nil then
|
|
|
|
Result := cell.Altitude
|
|
|
|
else
|
|
|
|
Result := ADefault;
|
|
|
|
end;
|
|
|
|
|
2009-05-17 22:06:42 +02:00
|
|
|
procedure TLandscape.GetNormals(AX, AY: Word; var ANormals: TNormals);
|
|
|
|
var
|
|
|
|
cells: array[0..2, 0..2] of TNormals;
|
|
|
|
north, west, south, east: TVector;
|
|
|
|
i, j: Integer;
|
|
|
|
|
|
|
|
function GetPlainNormals(X, Y: SmallInt): TNormals;
|
|
|
|
var
|
|
|
|
cell: TMapCell;
|
|
|
|
north, west, south, east: ShortInt;
|
|
|
|
u, v: TVector;
|
|
|
|
begin
|
|
|
|
cell := GetMapCell(X, Y);
|
2009-12-03 01:35:29 +01:00
|
|
|
if cell <> nil then
|
2009-05-17 22:06:42 +02:00
|
|
|
begin
|
|
|
|
north := cell.Altitude;
|
|
|
|
west := GetLandAlt(cell.X, cell.Y + 1, north);
|
|
|
|
south := GetLandAlt(cell.X + 1, cell.Y + 1, north);
|
|
|
|
east := GetLandAlt(cell.X + 1, cell.Y, north);
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
north := 0;
|
|
|
|
west := 0;
|
|
|
|
east := 0;
|
|
|
|
south := 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if (north = west) and (west = east) and (north = south) then
|
|
|
|
begin
|
2009-12-03 01:35:29 +01:00
|
|
|
Result[0] := Vector(0, 0, 1);
|
|
|
|
Result[1] := Vector(0, 0, 1);
|
|
|
|
Result[2] := Vector(0, 0, 1);
|
|
|
|
Result[3] := Vector(0, 0, 1);
|
2009-05-17 22:06:42 +02:00
|
|
|
end else
|
|
|
|
begin
|
|
|
|
u := Vector(-22, 22, (north - east) * 4);
|
|
|
|
v := Vector(-22, -22, (west - north) * 4);
|
2009-12-03 01:35:29 +01:00
|
|
|
Result[0] := VectorNorm(VectorCross(u, v));
|
2009-05-17 22:06:42 +02:00
|
|
|
|
|
|
|
u := Vector(22, 22, (east - south) * 4);
|
|
|
|
v := Vector(-22, 22, (north - east) * 4);
|
2009-12-03 01:35:29 +01:00
|
|
|
Result[1] := VectorNorm(VectorCross(u, v));
|
2009-05-17 22:06:42 +02:00
|
|
|
|
|
|
|
u := Vector(22, -22, (south - west) * 4);
|
|
|
|
v := Vector(22, 22, (east - south) * 4);
|
2009-12-03 01:35:29 +01:00
|
|
|
Result[2] := VectorNorm(VectorCross(u, v));
|
2009-05-17 22:06:42 +02:00
|
|
|
|
|
|
|
u := Vector(-22, -22, (west - north) * 4);
|
|
|
|
v := Vector(22, -22, (south - west) * 4);
|
2009-12-03 01:35:29 +01:00
|
|
|
Result[3] := VectorNorm(VectorCross(u, v));
|
2009-05-17 22:06:42 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
begin
|
|
|
|
for i := 0 to 2 do
|
|
|
|
for j := 0 to 2 do
|
|
|
|
cells[i, j] := GetPlainNormals(AX - 1 + i, AY - 1 + j);
|
|
|
|
|
|
|
|
north := cells[0, 0][2];
|
|
|
|
west := cells[0, 1][1];
|
|
|
|
east := cells[1, 0][3];
|
|
|
|
south := cells[1, 1][0];
|
|
|
|
ANormals[0] := VectorNorm(VectorAdd(VectorAdd(VectorAdd(north, west), east), south));
|
|
|
|
|
|
|
|
north := cells[1, 0][2];
|
|
|
|
west := cells[1, 1][1];
|
|
|
|
east := cells[2, 0][3];
|
|
|
|
south := cells[2, 1][0];
|
|
|
|
ANormals[1] := VectorNorm(VectorAdd(VectorAdd(VectorAdd(north, west), east), south));
|
|
|
|
|
|
|
|
north := cells[1, 1][2];
|
|
|
|
west := cells[1, 2][1];
|
|
|
|
east := cells[2, 1][3];
|
|
|
|
south := cells[2, 2][0];
|
|
|
|
ANormals[2] := VectorNorm(VectorAdd(VectorAdd(VectorAdd(north, west), east), south));
|
|
|
|
|
|
|
|
north := cells[0, 1][2];
|
|
|
|
west := cells[0, 2][1];
|
|
|
|
east := cells[1, 1][3];
|
|
|
|
south := cells[1, 2][0];
|
|
|
|
ANormals[3] := VectorNorm(VectorAdd(VectorAdd(VectorAdd(north, west), east), south));
|
|
|
|
end;
|
|
|
|
|
2009-05-17 14:12:52 +02:00
|
|
|
procedure TLandscape.MoveStatic(AStatic: TStaticItem; AX, AY: Word);
|
|
|
|
var
|
|
|
|
sourceBlock, targetBlock: TSeperatedStaticBlock;
|
2009-12-02 19:14:00 +01:00
|
|
|
targetStaticList: TStaticItemList;
|
2009-05-17 14:12:52 +02:00
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
if (AX >= 0) and (AX <= FCellWidth) and (AY >= 0) and (AY <= FCellHeight) then
|
|
|
|
begin
|
|
|
|
sourceBlock := AStatic.Owner as TSeperatedStaticBlock;
|
|
|
|
targetBlock := GetStaticBlock(AX div 8, AY div 8);
|
|
|
|
if (sourceBlock <> nil) and (targetBlock <> nil) then
|
|
|
|
begin
|
|
|
|
sourceBlock.Cells[(AStatic.Y mod 8) * 8 + AStatic.X mod 8].Remove(AStatic);
|
|
|
|
targetStaticList := targetBlock.Cells[(AY mod 8) * 8 + AX mod 8];
|
|
|
|
targetStaticList.Add(AStatic);
|
|
|
|
for i := 0 to targetStaticList.Count - 1 do
|
2009-12-02 19:14:00 +01:00
|
|
|
targetStaticList.Items[i].UpdatePriorities(
|
|
|
|
ResMan.Tiledata.StaticTiles[targetStaticList.Items[i].TileID],
|
2009-08-02 19:08:56 +02:00
|
|
|
i);
|
2009-12-02 19:14:00 +01:00
|
|
|
targetStaticList.Sort(@CompareStaticItems);
|
2009-05-17 14:12:52 +02:00
|
|
|
AStatic.UpdatePos(AX, AY, AStatic.Z);
|
|
|
|
AStatic.Owner := targetBlock;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TLandscape.PrepareBlocks(AX1, AY1, AX2, AY2: Word);
|
|
|
|
var
|
|
|
|
x, y, i: Integer;
|
|
|
|
coords: TBlockCoordsArray;
|
|
|
|
obj: TObject;
|
|
|
|
begin
|
|
|
|
AX1 := EnsureRange(AX1, 0, FWidth - 1);
|
|
|
|
AY1 := EnsureRange(AY1, 0, FHeight - 1);
|
|
|
|
AX2 := EnsureRange(AX2, 0, FWidth - 1);
|
|
|
|
AY2 := EnsureRange(AY2, 0, FHeight - 1);
|
|
|
|
|
|
|
|
SetLength(coords, 0);
|
|
|
|
for x := AX1 to AX2 do
|
|
|
|
begin
|
|
|
|
for y := AY1 to AY2 do
|
|
|
|
begin
|
|
|
|
if (not FOpenRequests[y * FWidth + x]) and
|
|
|
|
(not FBlockCache.QueryID(GetID(x, y), obj)) then
|
|
|
|
begin
|
|
|
|
SetLength(coords, Length(coords) + 1);
|
|
|
|
i := High(coords);
|
|
|
|
coords[i].X := x;
|
|
|
|
coords[i].Y := y;
|
|
|
|
FOpenRequests[y * FWidth + x] := True;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
if Length(coords) > 0 then
|
|
|
|
dmNetwork.Send(TRequestBlocksPacket.Create(coords));
|
|
|
|
end;
|
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
procedure TLandscape.UpdateBlockAccess;
|
2009-05-17 14:12:52 +02:00
|
|
|
var
|
2009-08-02 13:45:26 +02:00
|
|
|
cacheEntry: PCacheEntry;
|
2009-05-17 14:12:52 +02:00
|
|
|
begin
|
2009-08-02 13:45:26 +02:00
|
|
|
cacheEntry := nil;
|
|
|
|
while FBlockCache.Iterate(cacheEntry) do
|
2009-10-02 23:52:02 +02:00
|
|
|
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');
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ TMaterial }
|
|
|
|
|
|
|
|
constructor TMaterial.Create(AWidth, AHeight: Integer;
|
|
|
|
AGraphic: TSingleImage);
|
|
|
|
var
|
|
|
|
caps: TGLTextureCaps;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FRealWidth := AWidth;
|
|
|
|
FRealHeight := AHeight;
|
|
|
|
GetGLTextureCaps(caps);
|
|
|
|
if caps.NonPowerOfTwo then
|
|
|
|
begin
|
|
|
|
FWidth := AWidth;
|
|
|
|
FHeight := AHeight;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
if IsPow2(AWidth) then FWidth := AWidth else FWidth := NextPow2(AWidth);
|
|
|
|
if IsPow2(AHeight) then FHeight := AHeight else FHeight := NextPow2(AHeight);
|
|
|
|
end;
|
|
|
|
FGraphic := TSingleImage.CreateFromParams(FWidth, FHeight, ifA8R8G8B8);
|
|
|
|
AGraphic.CopyTo(0, 0, AWidth, AHeight, FGraphic, 0, 0);
|
|
|
|
UpdateTexture;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMaterial.Destroy;
|
|
|
|
begin
|
|
|
|
if FGraphic <> nil then FreeAndNil(FGraphic);
|
|
|
|
if FTexture <> 0 then glDeleteTextures(1, @FTexture);
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMaterial.HitTest(AX, AY: Integer): Boolean;
|
|
|
|
var
|
|
|
|
pixel: TColor32Rec;
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
if InRange(AX, 0, FGraphic.Width - 1) and
|
|
|
|
InRange(AY, 0, FGraphic.Height - 1) then
|
|
|
|
begin
|
|
|
|
pixel := GetPixel32(FGraphic.ImageDataPointer^, AX, AY);
|
|
|
|
if pixel.A > 0 then
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMaterial.UpdateTexture;
|
|
|
|
begin
|
|
|
|
if FTexture <> 0 then glDeleteTextures(1, @FTexture);
|
|
|
|
|
|
|
|
FTexture := CreateGLTextureFromImage(FGraphic.ImageDataPointer^, 0, 0, False, ifUnknown, @FWidth, @FHeight);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, FTexture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
|
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
|
|
|
end;
|
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
{ TScreenBuffer }
|
2008-08-29 12:09:26 +02:00
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
constructor TScreenBuffer.Create;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2009-05-20 18:16:18 +02:00
|
|
|
FCount := 0;
|
|
|
|
FSerial := 0;
|
|
|
|
UpdateShortcuts;
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
destructor TScreenBuffer.Destroy;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
|
|
|
Clear;
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
function TScreenBuffer.Add(AItem: TWorldItem): PBlockInfo;
|
|
|
|
begin
|
|
|
|
New(Result);
|
|
|
|
AItem.Locked := True;
|
|
|
|
AItem.OnDestroy.RegisterEvent(@OnTileRemoved);
|
|
|
|
Result^.Item := AItem;
|
|
|
|
Result^.HighRes := nil;
|
|
|
|
Result^.LowRes := nil;
|
|
|
|
Result^.Normals := nil;
|
|
|
|
Result^.State := ssNormal;
|
2009-07-30 20:31:29 +02:00
|
|
|
Result^.Highlighted := False;
|
2009-12-05 01:24:58 +01:00
|
|
|
Result^.Translucent := False;
|
2009-12-05 17:26:22 +01:00
|
|
|
Result^.Text := nil;
|
2009-05-20 18:16:18 +02:00
|
|
|
Result^.Next := nil;
|
|
|
|
|
|
|
|
if FShortCuts[0] = nil then //First element
|
|
|
|
begin
|
|
|
|
FShortCuts[0] := Result;
|
|
|
|
FShortCuts[-1] := Result; //Last element
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
FShortCuts[-1]^.Next := Result;
|
|
|
|
FShortCuts[-1] := Result;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Inc(FCount);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TScreenBuffer.Clear;
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
|
|
|
current, next: PBlockInfo;
|
|
|
|
begin
|
2009-05-20 18:16:18 +02:00
|
|
|
current := FShortCuts[0];
|
2008-08-29 12:09:26 +02:00
|
|
|
while current <> nil do
|
|
|
|
begin
|
|
|
|
next := current^.Next;
|
2009-05-20 18:16:18 +02:00
|
|
|
current^.Item.Locked := False;
|
|
|
|
current^.Item.OnDestroy.UnregisterEvent(@OnTileRemoved);
|
|
|
|
if current^.Normals <> nil then Dispose(current^.Normals);
|
2009-12-05 17:26:22 +01:00
|
|
|
current^.Text.Free;
|
2008-08-29 12:09:26 +02:00
|
|
|
Dispose(current);
|
|
|
|
current := next;
|
|
|
|
end;
|
2009-05-20 18:16:18 +02:00
|
|
|
FShortCuts[0] := nil;
|
|
|
|
FShortCuts[-1] := nil;
|
2008-08-29 12:09:26 +02:00
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
FCount := 0;
|
|
|
|
FSerial := 0;
|
2008-08-29 12:09:26 +02:00
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
UpdateShortcuts;
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
procedure TScreenBuffer.Delete(AItem: TWorldItem);
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
|
|
|
current, last, next: PBlockInfo;
|
|
|
|
begin
|
|
|
|
last := nil;
|
2009-05-20 18:16:18 +02:00
|
|
|
current := FShortCuts[0];
|
2008-08-29 12:09:26 +02:00
|
|
|
while current <> nil do
|
|
|
|
begin
|
|
|
|
if current^.Item = AItem then
|
|
|
|
begin
|
2009-05-20 18:16:18 +02:00
|
|
|
if FShortCuts[-1] = current then FShortCuts[-1] := last;
|
|
|
|
if FShortCuts[0] = current then FShortCuts[0] := current^.Next;
|
2008-08-29 12:09:26 +02:00
|
|
|
if last <> nil then last^.Next := current^.Next;
|
2009-05-17 15:32:51 +02:00
|
|
|
|
2009-05-17 22:06:42 +02:00
|
|
|
if current^.Normals <> nil then Dispose(current^.Normals);
|
2009-12-05 17:26:22 +01:00
|
|
|
current^.Text.Free;
|
2009-05-17 15:32:51 +02:00
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
Dispose(current);
|
2009-05-20 18:16:18 +02:00
|
|
|
Dec(FCount);
|
|
|
|
FShortCutsValid := False;
|
2008-08-29 12:09:26 +02:00
|
|
|
next := nil;
|
|
|
|
end else
|
|
|
|
next := current^.Next;
|
2009-05-17 15:32:51 +02:00
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
last := current;
|
|
|
|
current := next;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TScreenBuffer.Find(AScreenPosition: TPoint): PBlockInfo;
|
|
|
|
var
|
|
|
|
current: PBlockInfo;
|
2009-12-04 15:52:27 +01:00
|
|
|
buff: array[0..3] of GLuint;
|
|
|
|
hits: GLint;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
|
|
|
Result := nil;
|
2009-05-20 18:16:18 +02:00
|
|
|
current := FShortCuts[0];
|
2009-07-30 17:04:49 +02:00
|
|
|
while current <> nil do //search the last matching tile
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
2009-05-18 00:33:59 +02:00
|
|
|
if (current^.State = ssNormal) and
|
2009-12-04 15:52:27 +01:00
|
|
|
PtInRect(current^.ScreenRect, AScreenPosition)then
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
2009-12-04 15:52:27 +01:00
|
|
|
if current^.CheckRealQuad then
|
|
|
|
begin
|
|
|
|
//OpenGL hit test
|
|
|
|
//We use the "real quad" here to prevent the draw-preview from
|
|
|
|
//intercepting with our actual tiles (which are "hidden" then).
|
|
|
|
glSelectBuffer(4, @buff[0]);
|
|
|
|
glViewport(current^.ScreenRect.Left, current^.ScreenRect.Top,
|
|
|
|
current^.ScreenRect.Right, current^.ScreenRect.Bottom);
|
|
|
|
glRenderMode(GL_SELECT);
|
|
|
|
glInitNames;
|
|
|
|
glPushName(0);
|
|
|
|
|
|
|
|
glPushMatrix;
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity;
|
|
|
|
gluOrtho2D(AScreenPosition.x, AScreenPosition.x + 1,
|
|
|
|
AScreenPosition.y + 1, AScreenPosition.y);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity;
|
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glVertex2iv(@current^.RealQuad[0]);
|
|
|
|
glVertex2iv(@current^.RealQuad[3]);
|
|
|
|
glVertex2iv(@current^.RealQuad[2]);
|
|
|
|
glVertex2iv(@current^.RealQuad[1]);
|
|
|
|
glEnd;
|
|
|
|
glPopMatrix;
|
|
|
|
glFlush;
|
|
|
|
|
|
|
|
if glRenderMode(GL_RENDER) > 0 then //glRenderMode now returns the number of hits
|
|
|
|
Result := current;
|
|
|
|
end else
|
|
|
|
if current^.LowRes.HitTest(AScreenPosition.x - current^.ScreenRect.Left,
|
|
|
|
AScreenPosition.y - current^.ScreenRect.Top) then
|
|
|
|
Result := current;
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
current := current^.Next;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-05-17 14:12:52 +02:00
|
|
|
function TScreenBuffer.GetSerial: Cardinal;
|
|
|
|
begin
|
2009-05-17 15:32:51 +02:00
|
|
|
Result := FSerial;
|
2009-05-17 14:12:52 +02:00
|
|
|
Inc(FSerial);
|
|
|
|
end;
|
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
function TScreenBuffer.Insert(AItem: TWorldItem): PBlockInfo;
|
2008-08-29 12:09:26 +02:00
|
|
|
var
|
2009-05-17 15:32:51 +02:00
|
|
|
current: PBlockInfo;
|
2009-05-20 18:16:18 +02:00
|
|
|
shortcut: Integer;
|
2008-08-29 12:09:26 +02:00
|
|
|
begin
|
2009-05-20 18:16:18 +02:00
|
|
|
if not FShortCutsValid then
|
|
|
|
UpdateShortcuts;
|
|
|
|
|
2009-05-17 15:32:51 +02:00
|
|
|
New(Result);
|
2008-08-29 12:09:26 +02:00
|
|
|
AItem.Locked := True;
|
|
|
|
AItem.OnDestroy.RegisterEvent(@OnTileRemoved);
|
2009-05-17 15:32:51 +02:00
|
|
|
Result^.Item := AItem;
|
|
|
|
Result^.HighRes := nil;
|
|
|
|
Result^.LowRes := nil;
|
|
|
|
Result^.Normals := nil;
|
2009-05-18 00:33:59 +02:00
|
|
|
Result^.State := ssNormal;
|
2009-07-30 20:31:29 +02:00
|
|
|
Result^.Highlighted := False;
|
2009-12-05 01:24:58 +01:00
|
|
|
Result^.Translucent := False;
|
2009-12-05 17:26:22 +01:00
|
|
|
Result^.Text := nil;
|
2009-05-17 14:12:52 +02:00
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
if (FShortCuts[0] = nil) or (CompareWorldItems(AItem, FShortCuts[0]^.Item) < 0) then
|
2009-05-17 14:12:52 +02:00
|
|
|
begin
|
2009-08-06 15:04:09 +02:00
|
|
|
if FShortCuts[0] = nil then
|
|
|
|
FShortCuts[-1] := Result; //Update last item
|
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
Result^.Next := FShortCuts[0];
|
|
|
|
FShortCuts[0] := Result;
|
2009-05-17 14:12:52 +02:00
|
|
|
end else
|
|
|
|
begin
|
2009-05-20 18:16:18 +02:00
|
|
|
//find best entry point
|
|
|
|
shortcut := 0;
|
|
|
|
while (shortcut <= 10) and (FShortCuts[shortcut] <> nil) and
|
2009-12-02 19:14:00 +01:00
|
|
|
(CompareWorldItems(AItem, FShortCuts[shortcut]^.Item) >= 0) do
|
2009-05-20 18:16:18 +02:00
|
|
|
begin
|
|
|
|
current := FShortCuts[shortcut];
|
|
|
|
Inc(shortcut);
|
|
|
|
end;
|
|
|
|
|
|
|
|
//now find the real match
|
2009-05-18 00:33:59 +02:00
|
|
|
while (current^.Next <> nil) and
|
2009-05-17 15:32:51 +02:00
|
|
|
(CompareWorldItems(AItem, current^.Next^.Item) > 0) do
|
2009-05-17 14:12:52 +02:00
|
|
|
begin
|
2009-05-17 15:32:51 +02:00
|
|
|
current := current^.Next;
|
2009-05-17 14:12:52 +02:00
|
|
|
end;
|
2009-08-06 15:04:09 +02:00
|
|
|
|
|
|
|
if FShortCuts[-1] = current^.Next then
|
|
|
|
FShortCuts[-1] := Result; //Update last item
|
2009-05-17 14:12:52 +02:00
|
|
|
|
2009-05-17 15:32:51 +02:00
|
|
|
Result^.Next := current^.Next;
|
|
|
|
current^.Next := Result;
|
2009-05-17 14:12:52 +02:00
|
|
|
end;
|
2009-05-20 18:16:18 +02:00
|
|
|
|
|
|
|
Inc(FCount);
|
|
|
|
end;
|
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
function TScreenBuffer.Iterate(var ABlockInfo: PBlockInfo): Boolean;
|
|
|
|
begin
|
|
|
|
if ABlockInfo = nil then
|
|
|
|
ABlockInfo := FShortCuts[0]
|
|
|
|
else
|
|
|
|
ABlockInfo := ABlockInfo^.Next;
|
|
|
|
Result := ABlockInfo <> nil;
|
|
|
|
end;
|
|
|
|
|
2009-05-20 18:16:18 +02:00
|
|
|
procedure TScreenBuffer.UpdateShortcuts;
|
|
|
|
var
|
|
|
|
shortcut, step, nextStep, stepSize: Integer;
|
|
|
|
blockInfo: PBlockInfo;
|
|
|
|
begin
|
|
|
|
if FCount < 10 then
|
|
|
|
begin
|
|
|
|
for shortcut := 1 to 10 do
|
|
|
|
FShortCuts[shortcut] := nil;
|
|
|
|
end
|
|
|
|
else if FShortCuts[0] <> nil then
|
|
|
|
begin
|
|
|
|
stepSize := FCount div 10;
|
|
|
|
nextStep := stepSize;
|
|
|
|
step := 0;
|
|
|
|
shortcut := 1;
|
|
|
|
blockInfo := FShortCuts[0];
|
|
|
|
repeat
|
|
|
|
if step = nextStep then
|
|
|
|
begin
|
|
|
|
FShortCuts[shortcut] := blockInfo;
|
|
|
|
Inc(shortcut);
|
|
|
|
Inc(nextStep, stepSize);
|
|
|
|
end;
|
|
|
|
|
|
|
|
Inc(step);
|
|
|
|
|
|
|
|
FShortCuts[-1] := blockInfo; //update last known item
|
|
|
|
blockInfo := blockInfo^.Next;
|
|
|
|
until (blockInfo = nil);
|
|
|
|
end;
|
|
|
|
FShortCutsValid := True;
|
2009-05-17 14:12:52 +02:00
|
|
|
end;
|
|
|
|
|
2009-08-03 18:40:31 +02:00
|
|
|
function TScreenBuffer.UpdateSortOrder(AItem: TWorldItem): PBlockInfo;
|
|
|
|
var
|
|
|
|
newNodePosition, oldNode, oldNodePrev, current: PBlockInfo;
|
|
|
|
begin
|
|
|
|
newNodePosition := nil;
|
|
|
|
oldNode := nil;
|
|
|
|
oldNodePrev := nil;
|
|
|
|
current := FShortCuts[0];
|
|
|
|
|
|
|
|
while (current <> nil) and ((oldNode = nil) or (newNodePosition = nil)) do
|
|
|
|
begin
|
|
|
|
if current^.Item = AItem then
|
|
|
|
oldNode := current
|
|
|
|
else if oldNode = nil then
|
|
|
|
oldNodePrev := current;
|
|
|
|
|
|
|
|
if newNodePosition = nil then
|
|
|
|
begin
|
|
|
|
if (current^.Next = nil) or (CompareWorldItems(AItem, current^.Next^.Item) < 0) then
|
|
|
|
newNodePosition := current;
|
|
|
|
end;
|
|
|
|
|
|
|
|
current := current^.Next;
|
|
|
|
end;
|
|
|
|
|
2009-12-03 15:38:11 +01:00
|
|
|
//oldNode = nil, if the change happend out-of-screen
|
|
|
|
if (oldNode <> nil ) and (oldNode <> newNodePosition) then
|
2009-08-03 18:40:31 +02:00
|
|
|
begin
|
|
|
|
if oldNodePrev <> oldNode then
|
|
|
|
begin
|
|
|
|
if oldNodePrev = nil then
|
|
|
|
FShortCuts[0] := oldNode^.Next
|
|
|
|
else
|
|
|
|
oldNodePrev^.Next := oldNode^.Next;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if (newNodePosition = FShortCuts[0]) and (CompareWorldItems(AItem, FShortCuts[0]^.Item) < 0) then
|
|
|
|
begin
|
|
|
|
oldNode^.Next := FShortCuts[0];
|
|
|
|
FShortCuts[0] := oldNode;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
oldNode^.Next := newNodePosition^.Next;
|
|
|
|
newNodePosition^.Next := oldNode;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := oldNode;
|
|
|
|
end;
|
|
|
|
|
2009-05-17 14:12:52 +02:00
|
|
|
procedure TScreenBuffer.OnTileRemoved(ATile: TMulBlock);
|
|
|
|
begin
|
2009-05-17 15:32:51 +02:00
|
|
|
Delete(TWorldItem(ATile));
|
2008-08-29 12:09:26 +02:00
|
|
|
end;
|
|
|
|
|
2009-12-05 17:26:22 +01:00
|
|
|
{ TGLText }
|
|
|
|
|
|
|
|
constructor TGLText.Create(AFont: TGLFont; AText: String);
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
FFont := AFont;
|
|
|
|
FText := AText;
|
|
|
|
FWidth := FFont.GetTextWidth(AText);
|
|
|
|
FHeight := FFont.GetTextHeight('A');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TGLText.Render(AScreenRect: TRect);
|
|
|
|
var
|
|
|
|
x, y: Integer;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
y := AScreenRect.Top + (AScreenRect.Bottom - AScreenRect.Top - FHeight) div 2;
|
|
|
|
x := AScreenRect.Left + (AScreenRect.Right - AScreenRect.Left - FWidth) div 2;
|
|
|
|
FFont.DrawText(x, y, FText);
|
|
|
|
end;
|
|
|
|
|
2008-08-29 12:09:26 +02:00
|
|
|
end.
|
|
|
|
|