|
7楼
发表于 2024-1-13 11:51:31
|
只看该作者
浙江省杭州市
function TEnvirnoment.LoadMapData(sMapFile: string): Boolean; //004B54E0
var
fHandle : Integer;
Header : TMapHeader;
nMapSize : Integer;
n24, nW, nH : Integer;
MapBuffer : pTMap;
Point : Integer;
Door : pTDoorInfo;
i : Integer;
MapCellInfo : pTMapCellinfo;
begin
Result := False;
try
if FileExists(sMapFile) then begin
fHandle := FileOpen(sMapFile, fmOpenRead or fmShareExclusive);
if fHandle > 0 then begin
FileRead(fHandle, Header, SizeOf(TMapHeader));
m_nWidth := Header.wWidth;
m_nHeight := Header.wHeight;
Initialize(m_nWidth, m_nHeight);
nMapSize := m_nWidth * SizeOf(TMapUnitInfo) * m_nHeight;
MapBuffer := AllocMem(nMapSize);
FileRead(fHandle, MapBuffer^, nMapSize);
FileClose(fHandle);
SetLength(m_MapData, Header.wWidth, Header.wHeight); //路点
for nW := 0 to m_nWidth - 1 do begin
n24 := nW * m_nHeight;
for nH := 0 to m_nHeight - 1 do begin
if (MapBuffer[n24 + nH].wBkImg) and $8000 = 0 then
m_MapData[nW, nH] := ttNormal
else
m_MapData[nW, nH] := ttObstacle;
if (MapBuffer[n24 + nH].wBkImg) and $8000 <> 0 then begin //背景图
MapCellInfo := @m_MapCellArray[n24 + nH];
MapCellInfo.chFlag := 1;
end;
if MapBuffer[n24 + nH].wFrImg and $8000 <> 0 then begin //前景图(障碍物)
MapCellInfo := @m_MapCellArray[n24 + nH];
MapCellInfo.chFlag := 2;
end;
if MapBuffer[n24 + nH].btDoorIndex and $80 <> 0 then begin //门点
Point := (MapBuffer[n24 + nH].btDoorIndex and $7F);
if Point > 0 then begin
New(Door);
Door.nX := nW;
Door.nY := nH;
Door.n081 := Point;
Door.Status := nil;
for i := 0 to m_DoorList.Count - 1 do begin
if abs(pTDoorInfo(m_DoorList.Items[i]).nX - Door.nX) <= 10 then begin
if abs(pTDoorInfo(m_DoorList.Items[i]).nY - Door.nY) <= 10 then begin
if pTDoorInfo(m_DoorList.Items[i]).n081 = Point then begin
Door.Status := pTDoorInfo(m_DoorList.Items[i]).Status;
Inc(Door.Status.nRefCount);
Break;
end;
end;
end;
end;
if Door.Status = nil then begin
New(Door.Status);
Door.Status.boOpened := False;
Door.Status.bo01 := False;
Door.Status.n04 := 0;
Door.Status.dwOpenTick := 0;
Door.Status.nRefCount := 1;
end;
m_DoorList.Add(Door);
end;
end;
end;
end;
FreeMem(MapBuffer);
Result := True;
end;
end;
except
Result := False;
end;
end;
function TEnvirnoment.LoadNmpData(sMapFile: string): Boolean;
var
fHandle : Integer;
Header : TMapHeader_Wol;
nMapSize : Integer;
nPoint, nW, nH : Integer;
MapBuffer : pTMap_Wol;
MapCellInfo : pTMapCellinfo;
nMapLattice, nMapLattice2 : byte;
nCount : Integer;
WolMapLattice : pTWolMapLattice;
wMapLattice : word;
begin
Result := False;
if FileExists(sMapFile) then begin
fHandle := FileOpen(sMapFile, fmOpenRead or fmShareExclusive);
if fHandle > 0 then begin
FileRead(fHandle, Header, SizeOf(TMapHeader_Wol)); {Read NmpFile Header}
if (Header.nMapType in [1, 3, 5..7, 9, $A, $B, $C]) then begin {Filtrate}
m_nWidth := Header.nWidth; {Evaluate Width}
m_nHeight := Header.nHeight; {Evaluate Height}
Initialize(m_nWidth, m_nHeight); {Initialize X Y}
nMapSize := FileSize(sMapFile) - SizeOf(TMapHeader_Wol); {Get MapFile Size}
MapBuffer := AllocMem(nMapSize);
FileRead(fHandle, MapBuffer^, nMapSize);
FileClose(fHandle);
SetLength(m_MapData, m_nWidth, m_nHeight); //路点
nCount := 0;
for nH := 0 to m_nHeight - 1 do begin
for nW := 0 to m_nWidth - 1 do begin
nPoint := nW * m_nHeight;
MapCellInfo := @m_MapCellArray[nPoint + nH];
if nCount <= nMapSize then begin
case Header.nMapType of
1: begin {TG MapFile}
WolMapLattice := @MapBuffer[nCount];
if WolMapLattice.nMapFlag > 1000 then
MapCellInfo.chFlag := 0
else
MapCellInfo.chFlag := 1;
Inc(nCount, 12);
end;
3: begin {Norm MapFile}
nMapLattice := MapBuffer[nCount];
MapCellInfo.chFlag := (nMapLattice and $1);
nMapLattice := nMapLattice and $1F;
if nMapLattice > 0 then
Inc(nCount, g_MapLattice1[nMapLattice])
else
Inc(nCount);
end; {HighSpeed MapFile}
5: begin
nMapLattice := MapBuffer[nCount];
MapCellInfo.chFlag := (nMapLattice and $1);
nMapLattice := nMapLattice and $1F;
if nMapLattice > 0 then
Inc(nCount, g_MapLattice2[nMapLattice])
else
Inc(nCount);
end;
6: begin {Woool 1.978 MapFile}
nMapLattice := MapBuffer[nCount];
MapCellInfo.chFlag := (nMapLattice and $1);
nMapLattice := nMapLattice and $1F;
if nMapLattice > 0 then
Inc(nCount, g_MapLattice3[nMapLattice])
else
Inc(nCount, 3);
end;
7: begin {Woool 1.996 MapFile}
nMapLattice := MapBuffer[nCount];
MapCellInfo.chFlag := (nMapLattice and $1);
nMapLattice := nMapLattice and $1F;
if nCount + 1 <= nMapSize then
nMapLattice2 := MapBuffer[nCount + 1]
else
nMapLattice2 := 0;
Inc(nCount, g_MapLattice4[nMapLattice and $1F]);
if nMapLattice2 > 0 then begin
if nMapLattice2 and 1 > 0 then
Inc(nCount, 2);
if nMapLattice2 and 2 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 4 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 8 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 16 > 0 then
Inc(nCount, 4);
end;
end;
9: begin {Woool 1.998 MapFile}
nMapLattice := MapBuffer[nCount];
MapCellInfo.chFlag := (nMapLattice and $1);
if nCount + 1 <= nMapSize then begin
nMapLattice2 := MapBuffer[nCount + 1];
Move(MapBuffer[nCount + 1], wMapLattice, 2);
end else
nMapLattice2 := 0;
Inc(nCount, 3);
if nMapLattice and 2 > 0 then
Inc(nCount, 4);
if nMapLattice and 4 > 0 then
Inc(nCount, 4);
if nMapLattice and 8 > 0 then
Inc(nCount, 4);
if nMapLattice and 16 > 0 then
Inc(nCount);
if nMapLattice and 32 > 0 then
Inc(nCount, 4);
if nMapLattice and 64 > 0 then
Inc(nCount, 4);
if nMapLattice2 > 0 then begin
if nMapLattice2 and 1 > 0 then
Inc(nCount, 2);
if nMapLattice2 and 2 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 4 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 8 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 16 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 32 > 0 then
Inc(nCount, 4);
end;
if (wMapLattice and $2000) > 0 then
Inc(nCount, 4);
end;
10: begin {Woool 1.999 MapFile}
nMapLattice := MapBuffer[nCount];
MapCellInfo.chFlag := (nMapLattice and $1);
if nCount + 1 <= nMapSize then begin
nMapLattice2 := MapBuffer[nCount + 1];
Move(MapBuffer[nCount + 1], wMapLattice, 2);
end else
nMapLattice2 := 0;
Inc(nCount, 3);
if nMapLattice and 2 > 0 then
Inc(nCount, 4);
if nMapLattice and 4 > 0 then
Inc(nCount, 4);
if nMapLattice and 8 > 0 then
Inc(nCount, 4);
if nMapLattice and 16 > 0 then
Inc(nCount);
if nMapLattice and 32 > 0 then
Inc(nCount, 4);
if nMapLattice and 64 > 0 then
Inc(nCount, 4);
if nMapLattice2 > 0 then begin
if nMapLattice2 and 1 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 2 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 4 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 8 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 16 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 32 > 0 then
Inc(nCount, 4);
end;
if (wMapLattice and $2000) > 0 then
Inc(nCount, 4);
end;
$B: begin
nMapLattice := MapBuffer[nCount];
MapCellInfo.chFlag := (nMapLattice and $1);
if nCount + 1 <= nMapSize then begin
nMapLattice2 := MapBuffer[nCount + 1];
Move(MapBuffer[nCount + 1], wMapLattice, 2);
end else
nMapLattice2 := 0;
Inc(nCount, 3);
if nMapLattice and 2 > 0 then
Inc(nCount, 4);
if nMapLattice and 4 > 0 then
Inc(nCount, 4);
if nMapLattice and 8 > 0 then
Inc(nCount, 4);
if nMapLattice and $10 > 0 then
Inc(nCount);
if nMapLattice and $20 > 0 then
Inc(nCount, 4);
if nMapLattice and $40 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 1 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 2 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 4 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 8 > 0 then
Inc(nCount, 4);
if nMapLattice2 and $10 > 0 then
Inc(nCount, 4);
if nMapLattice2 and $20 > 0 then
Inc(nCount, 4);
if nMapLattice2 and $80 > 0 then
Inc(nCount, 4);
if wMapLattice and $2000 > 0 then
Inc(nCount, 4);
if wMapLattice and $4000 > 0 then
Inc(nCount);
end;
$C: begin
nMapLattice := MapBuffer[nCount];
MapCellInfo.chFlag := (nMapLattice and $1);
if nCount + 1 <= nMapSize then begin
nMapLattice2 := MapBuffer[nCount + 1];
Move(MapBuffer[nCount + 1], wMapLattice, 2);
end else
nMapLattice2 := 0;
Inc(nCount, 3);
if nMapLattice and 2 > 0 then
Inc(nCount, 4);
if nMapLattice and 4 > 0 then
Inc(nCount, 4);
if nMapLattice and 8 > 0 then
Inc(nCount, 4);
if nMapLattice and $10 > 0 then
Inc(nCount);
if nMapLattice and $20 > 0 then
Inc(nCount, 4);
if nMapLattice and $40 > 0 then
Inc(nCount, 4);
if nMapLattice and $80 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 1 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 2 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 4 > 0 then
Inc(nCount, 4);
if nMapLattice2 and 8 > 0 then
Inc(nCount, 4);
if nMapLattice2 and $10 > 0 then
Inc(nCount, 4);
if nMapLattice2 and $20 > 0 then
Inc(nCount, 4);
if nMapLattice2 and $80 > 0 then
Inc(nCount, 4);
if wMapLattice and $2000 > 0 then
Inc(nCount, 4);
if wMapLattice and $4000 > 0 then
Inc(nCount);
end;
end;
end else
Break;
//路点
if MapCellInfo.chFlag = 0 then
m_MapData[nW, nH] := ttNormal
else
m_MapData[nW, nH] := ttObstacle;
end;
end;
FreeMem(MapBuffer);
Result := True;
end else
FileClose(fHandle);
end;
end;
end;
procedure TEnvirnoment.Initialize(nWidth, nHeight: Integer);
var
nW, nH : Integer;
MapCellInfo : pTMapCellinfo;
begin
if (nWidth > 1) and (nHeight > 1) then begin
if m_MapCellArray <> nil then begin
for nW := 0 to m_nWidth - 1 do begin
for nH := 0 to m_nHeight - 1 do begin
MapCellInfo := @m_MapCellArray[nW * m_nHeight + nH];
if MapCellInfo.ObjList <> nil then begin
//MapCellInfo.ObjList.free;
//MapCellInfo.ObjList := nil;
FreeAndNil(MapCellInfo);
end;
end;
end;
FreeMem(Pointer(m_MapCellArray));
Pointer(m_MapCellArray) := nil;
end;
m_nWidth := nWidth;
m_nHeight := nHeight;
Pointer(m_MapCellArray) := AllocMem((m_nWidth * m_nHeight) * SizeOf(TMapCellinfo));
end;
end; |
|