Changeset 330 for trunk/LocalPlayer/IsoEngine.pas
- Timestamp:
- Mar 26, 2021, 2:16:04 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LocalPlayer/IsoEngine.pas
r327 r330 5 5 6 6 uses 7 Protocol, ClientTools, ScreenTools, Tribes, {$IFNDEF SCR}Term, {$ENDIF}7 Protocol, ClientTools, ScreenTools, Tribes, 8 8 LCLIntf, LCLType, SysUtils, Classes, Graphics, UPixelPointer, UGraphicSet; 9 10 const 11 TerrainIconLines = 21; 12 TerrainIconCols = 9; 9 13 10 14 type 11 15 TInitEnemyModelEvent = function(emix: integer): boolean; 16 TTileSize = (tsSmall, tsMedium, tsBig); 17 18 TTerrainSpriteSize = array of TRect; 19 20 { TCitiesPictures } 21 22 TCitiesPictures = class 23 Pictures: array [2..3, 0..3] of TCityPicture; 24 procedure Prepare(HGrCities: TGraphicSet; xxt, yyt: Integer); 25 end; 12 26 13 27 { TIsoMap } … … 15 29 TIsoMap = class 16 30 private 31 FTileSize: TTileSize; 17 32 const 18 33 Dirx: array [0..7] of Integer = (1, 2, 1, 0, -1, -2, -1, 0); … … 21 36 function IsShoreTile(Loc: integer): boolean; 22 37 procedure MakeDark(Line: PPixelPointer; Length: Integer); 38 procedure SetTileSize(AValue: TTileSize); 23 39 procedure ShadeOutside(x0, y0, Width, Height, xm, ym: integer); 24 40 protected … … 36 52 DataCanvas: TCanvas; 37 53 MaskCanvas: TCanvas; 54 LandPatch: TBitmap; 55 OceanPatch: TBitmap; 56 Borders: TBitmap; 57 BordersOK: PInteger; 58 CitiesPictures: TCitiesPictures; 38 59 function Connection4(Loc, Mask, Value: integer): integer; 39 60 function Connection8(Loc, Mask: integer): integer; … … 48 69 procedure Sprite(HGr: TGraphicSet; xDst, yDst, Width, Height, xGr, yGr: integer); 49 70 procedure TSprite(xDst, yDst, grix: integer; PureBlack: boolean = false); 71 procedure ApplyTileSize(ATileSize: TTileSize); 50 72 public 73 xxt: Integer; // half of tile size x/y 74 yyt: Integer; // half of tile size x/y 75 TSpriteSize: TTerrainSpriteSize; 76 HGrTerrain: TGraphicSet; 77 HGrCities: TGraphicSet; 78 MapOptions: TMapOptions; 79 pDebugMap: Integer; // -1 for off 51 80 constructor Create; 81 destructor Destroy; override; 82 procedure Reset; 52 83 procedure SetOutput(Output: TBitmap); 53 84 procedure SetPaintBounds(Left, Top, Right, Bottom: integer); … … 63 94 procedure AttackEffect(const ShowMove: TShowMove); 64 95 procedure AttackEnd; 96 procedure ReduceTerrainIconsSize; 65 97 property AdviceLoc: integer read FAdviceLoc write FAdviceLoc; 66 end; 67 68 var 69 NoMap: TIsoMap; 70 MapOptions: TMapOptions; 71 pDebugMap: Integer; // -1 for off 98 property TileSize: TTileSize read FTileSize write SetTileSize; 99 end; 100 101 { TIsoMapCache } 102 103 TIsoMapCache = class 104 LandPatch: TBitmap; 105 OceanPatch: TBitmap; 106 Borders: TBitmap; 107 BordersOk: Integer; 108 TSpriteSize: TTerrainSpriteSize; 109 HGrTerrain: TGraphicSet; 110 HGrCities: TGraphicSet; 111 CitiesPictures: TCitiesPictures; 112 procedure AssignToIsoMap(IsoMap: TIsoMap); 113 constructor Create; 114 destructor Destroy; override; 115 end; 116 117 const 118 DefaultTileSize: TTileSize = tsMedium; 119 TileSizes: array [TTileSize] of TPoint = ((X: 33; Y: 16), (X: 48; Y: 24), 120 (X: 72; Y: 36)); 72 121 73 122 function IsJungle(y: integer): boolean; 74 123 procedure Init(InitEnemyModelHandler: TInitEnemyModelEvent); 75 function ApplyTileSize(ATileSize: TTileSize): boolean; 76 procedure Done; 77 procedure IsoEngineReset; 124 78 125 79 126 implementation 127 128 uses 129 Term; 80 130 81 131 const 82 132 ShoreDither = fGrass; 83 TerrainIconLines = 21;84 TerrainIconCols = 9;85 133 86 134 // sprites indexes … … 111 159 112 160 var 113 BordersOK: integer;114 161 OnInitEnemyModel: TInitEnemyModelEvent; 115 LandPatch: TBitmap;116 OceanPatch: TBitmap;117 Borders: TBitmap;118 TSpriteSize: array [0 .. TerrainIconLines * TerrainIconCols - 1] of TRect;119 162 DebugMap: ^TTileList; 120 CitiesPictures: array [2 .. 3, 0 .. 3] of TCityPicture;121 163 FoW: Boolean; 122 164 ShowLoc: Boolean; … … 127 169 ShowGrWall: Boolean; 128 170 ShowDebug: Boolean; 171 IsoMapCache: array[TTileSize] of TIsoMapCache; 129 172 130 173 function IsJungle(y: integer): boolean; … … 136 179 begin 137 180 OnInitEnemyModel := InitEnemyModelHandler; 138 if NoMap <> nil then 139 FreeAndNil(NoMap); 140 NoMap := TIsoMap.Create; 141 end; 142 143 procedure ReduceTerrainIconsSize; 181 end; 182 183 { TCitiesPictures } 184 185 procedure TCitiesPictures.Prepare(HGrCities: TGraphicSet; xxt, yyt: Integer); 186 var 187 Age: Integer; 188 Size: Integer; 189 begin 190 // prepare age 2+3 cities 191 for age := 2 to 3 do 192 for size := 0 to 3 do 193 with Pictures[Age, Size] do 194 FindPosition(HGrCities, Size * (xxt * 2 + 1), (Age - 2) * (yyt * 3 + 1), 195 xxt * 2 - 1, yyt * 3 - 1, $00FFFF, xShield, yShield); 196 end; 197 198 { TIsoMapCache } 199 200 procedure TIsoMapCache.AssignToIsoMap(IsoMap: TIsoMap); 201 begin 202 IsoMap.HGrTerrain := HGrTerrain; 203 IsoMap.HGrCities := HGrCities; 204 IsoMap.Borders := Borders; 205 IsoMap.BordersOK := @BordersOk; 206 IsoMap.LandPatch := LandPatch; 207 IsoMap.OceanPatch := OceanPatch; 208 IsoMap.TSpriteSize := TSpriteSize; 209 IsoMap.CitiesPictures := CitiesPictures; 210 end; 211 212 constructor TIsoMapCache.Create; 213 begin 214 LandPatch := TBitmap.Create; 215 LandPatch.PixelFormat := pf24bit; 216 OceanPatch := TBitmap.Create; 217 OceanPatch.PixelFormat := pf24bit; 218 Borders := TBitmap.Create; 219 Borders.PixelFormat := pf24bit; 220 HGrTerrain := nil; 221 HGrCities := nil; 222 SetLength(TSpriteSize, TerrainIconLines * TerrainIconCols); 223 CitiesPictures := TCitiesPictures.Create; 224 end; 225 226 destructor TIsoMapCache.Destroy; 227 begin 228 FreeAndNil(CitiesPictures); 229 FreeAndNil(LandPatch); 230 FreeAndNil(OceanPatch); 231 FreeAndNil(Borders); 232 inherited; 233 end; 234 235 procedure TIsoMap.ReduceTerrainIconsSize; 144 236 var 145 237 MaskLine: array of TPixelPointer; … … 206 298 end; 207 299 208 function ApplyTileSize(ATileSize: TTileSize): boolean;300 procedure TIsoMap.ApplyTileSize(ATileSize: TTileSize); 209 301 var 210 302 x: Integer; … … 212 304 xSrc: Integer; 213 305 ySrc: Integer; 214 HGrTerrainNew: TGraphicSet;215 HGrCitiesNew: TGraphicSet;216 Age: Integer;217 Size: Integer;218 306 LandMore: TBitmap; 219 307 OceanMore: TBitmap; 220 308 DitherMask: TBitmap; 221 xxtNew: Integer; 222 yytNew: Integer; 223 begin 224 xxtNew := TileSizes[ATileSize].X; 225 yytNew := TileSizes[ATileSize].Y; 226 result := false; 227 HGrTerrainNew := LoadGraphicSet(Format('Terrain%dx%d.png', 228 [xxtNew * 2, yytNew * 2])); 229 if not Assigned(HGrTerrainNew) then 230 exit; 231 HGrCitiesNew := LoadGraphicSet(Format('Cities%dx%d.png', 232 [xxtNew * 2, yytNew * 2])); 233 if not Assigned(HGrCitiesNew) then 234 exit; 235 xxt := xxtNew; 236 yyt := yytNew; 237 TileSize := ATileSize; 238 HGrTerrain := HGrTerrainNew; 239 HGrCities := HGrCitiesNew; 240 Result := true; 241 242 // prepare age 2+3 cities 243 for age := 2 to 3 do 244 for size := 0 to 3 do 245 with CitiesPictures[age, size] do 246 FindPosition(HGrCities, size * (xxt * 2 + 1), (age - 2) * (yyt * 3 + 1), 247 xxt * 2 - 1, yyt * 3 - 1, $00FFFF, xShield, yShield); 309 FileName: string; 310 begin 311 FTileSize := ATileSize; 312 xxt := TileSizes[ATileSize].X; 313 yyt := TileSizes[ATileSize].Y; 314 315 if Assigned(IsoMapCache[ATileSize]) then begin 316 IsoMapCache[ATileSize].AssignToIsoMap(Self); 317 Exit; 318 end; 319 IsoMapCache[ATileSize] := TIsoMapCache.Create; 320 321 FileName := Format('Terrain%dx%d.png', [xxt * 2, yyt * 2]); 322 IsoMapCache[ATileSize].HGrTerrain := LoadGraphicSet(FileName); 323 if not Assigned(IsoMapCache[ATileSize].HGrTerrain) then 324 raise Exception.Create(FileName + ' not found.'); 325 326 FileName := Format('Cities%dx%d.png', [xxt * 2, yyt * 2]); 327 IsoMapCache[ATileSize].HGrCities := LoadGraphicSet(FileName); 328 if not Assigned(IsoMapCache[ATileSize].HGrCities) then 329 raise Exception.Create(FileName + ' not found.'); 330 331 IsoMapCache[ATileSize].AssignToIsoMap(Self); 332 333 CitiesPictures.Prepare(HGrCities, xxt, yyt); 248 334 249 335 { prepare dithered ground tiles } 250 if not Assigned(LandPatch) then begin251 LandPatch := TBitmap.Create;252 LandPatch.PixelFormat := pf24bit;253 end;254 336 LandPatch.Canvas.Brush.Color := 0; 255 337 LandPatch.SetSize(xxt * 18, yyt * 9); 256 338 LandPatch.Canvas.FillRect(0, 0, LandPatch.Width, LandPatch.Height); 257 if not Assigned(OceanPatch) then begin258 OceanPatch := TBitmap.Create;259 OceanPatch.PixelFormat := pf24bit;260 end;261 339 OceanPatch.Canvas.Brush.Color := 0; 262 340 OceanPatch.SetSize(xxt * 8, yyt * 4); … … 437 515 ReduceTerrainIconsSize; 438 516 439 if not Assigned(Borders) then begin440 Borders := TBitmap.Create;441 Borders.PixelFormat := pf24bit;442 end;443 517 Borders.SetSize(xxt * 2, (yyt * 2) * nPl); 444 518 Borders.Canvas.FillRect(0, 0, Borders.Width, Borders.Height); 445 BordersOK := 0; 446 end; 447 448 procedure Done; 449 begin 450 FreeAndNil(NoMap); 451 FreeAndNil(LandPatch); 452 FreeAndNil(OceanPatch); 453 FreeAndNil(Borders); 454 end; 455 456 procedure IsoEngineReset; 457 begin 458 BordersOK := 0; 519 BordersOK^ := 0; 520 end; 521 522 procedure TIsoMap.Reset; 523 begin 524 BordersOK^ := 0; 459 525 end; 460 526 … … 469 535 DefLoc := -1; 470 536 FAdviceLoc := -1; 537 TileSize := DefaultTileSize; 538 end; 539 540 destructor TIsoMap.Destroy; 541 begin 542 inherited; 471 543 end; 472 544 … … 711 783 else 712 784 begin 713 cpic := CitiesPictures [age, xGr];785 cpic := CitiesPictures.Pictures[age, xGr]; 714 786 xShield := x - xxt + cpic.xShield; 715 787 yShield := y - 2 * yyt + cpic.yShield; … … 1024 1096 begin 1025 1097 if ShowBorder and (Loc >= 0) and (Loc < G.lx * G.ly) and 1026 (Tile and fTerrain <> fUNKNOWN) then 1027 begin 1098 (Tile and fTerrain <> fUNKNOWN) then begin 1028 1099 p1 := MyRO.Territory[Loc]; 1029 if (p1 >= 0) and (ShowMyBorder or (p1 <> me)) then 1030 begin 1031 if BordersOK and (1 shl p1) = 0 then 1032 begin 1100 if (p1 >= 0) and (ShowMyBorder or (p1 <> me)) then begin 1101 if BordersOK^ and (1 shl p1) = 0 then begin 1033 1102 UnshareBitmap(Borders); 1034 1103 BitBltCanvas(Borders.Canvas, 0, p1 * (yyt * 2), xxt * 2, … … 1049 1118 end; 1050 1119 Borders.EndUpdate; 1051 BordersOK := BordersOKor 1 shl p1;1120 BordersOK^ := BordersOK^ or 1 shl p1; 1052 1121 end; 1053 1122 for dy := 0 to 1 do 1054 for dx := 0 to 1 do 1055 begin 1123 for dx := 0 to 1 do begin 1056 1124 Loc1 := dLoc(Loc, dx * 2 - 1, dy * 2 - 1); 1057 1125 begin … … 1071 1139 end; 1072 1140 end; 1073 end 1141 end; 1074 1142 end; 1075 1143 end; … … 1347 1415 Line^.NextPixel; 1348 1416 end; 1417 end; 1418 1419 procedure TIsoMap.SetTileSize(AValue: TTileSize); 1420 begin 1421 if FTileSize = AValue then Exit; 1422 FTileSize := AValue; 1423 ApplyTileSize(AValue); 1349 1424 end; 1350 1425 … … 1641 1716 end; 1642 1717 1643 initialization 1644 1645 NoMap := nil; 1646 LandPatch := nil; 1647 OceanPatch := nil; 1648 Borders := nil; 1718 procedure IsoEngineDone; 1719 var 1720 I: TTileSize; 1721 begin 1722 for I := Low(IsoMapCache) to High(IsoMapCache) do 1723 FreeAndNil(IsoMapCache[I]); 1724 end; 1725 1726 finalization 1727 1728 IsoEngineDone; 1649 1729 1650 1730 end.
Note:
See TracChangeset
for help on using the changeset viewer.