Skip to content

Commit 1d1e9db

Browse files
committed
fix(webgraph): read notes
- legacy code ported from 1400 was offsetting webgraph wrongly as it's not needed anymore - optimized TRSMap cache setup slightly - docs
1 parent ddeaf14 commit 1d1e9db

File tree

6 files changed

+124
-109
lines changed

6 files changed

+124
-109
lines changed

osrs/interfaces/minimap.simba

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Main record used to interact with the {ref}`Minimap`.
4545
Orbs: array of TRSMinimapOrb;
4646
Bounds: TBox;
4747
Center: TPoint;
48+
Radius: Integer;
4849
Polygon: TPolygon;
4950

5051
Compass: record
@@ -90,6 +91,8 @@ begin
9091
Self.Bounds.Y1 := Y1 + 8;
9192
Self.Bounds.Y2 := Y1 + 159;
9293
end;
94+
95+
Self.Radius := 71;
9396
end;
9497

9598
ERSMode.FIXED:
@@ -104,6 +107,8 @@ begin
104107
Self.Bounds.Y1 := Y1 + 9;
105108
Self.Bounds.Y2 := Y1 + 159;
106109
end;
110+
111+
Self.Radius := 65;
107112
end;
108113
end;
109114

@@ -844,12 +849,7 @@ begin
844849
if angle = $FFFF then angle := Self.GetCompassAngle(False);
845850

846851
if radius = -1 then
847-
case RSClient.Mode of
848-
ERSMode.FIXED:
849-
radius := 65;
850-
ERSMode.RESIZABLE, ERSMode.MODERN_COMPACT, ERSMode.MODERN_WIDE:
851-
radius := 71;
852-
end;
852+
radius := Self.Radius;
853853

854854
img := Target.GetImage(Self.Bounds);
855855
img.ReplaceColor($0, $1);
@@ -903,12 +903,7 @@ begin
903903
Result := img.Copy();
904904

905905
if radius = -1 then
906-
case RSClient.Mode of
907-
ERSMode.FIXED:
908-
radius := 65;
909-
ERSMode.RESIZABLE, ERSMode.MODERN_COMPACT, ERSMode.MODERN_WIDE:
910-
radius := 71;
911-
end;
906+
radius := Self.Radius;
912907

913908
Result.Crop(TBox.Create(Result.Center, radius, radius));
914909
tpa := Result.FindColor($0, 0).SortFrom(Result.Center);

osrs/interfaces/setup.simba

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ begin
115115
FormUtils.Setup();
116116
RSFonts.Setup();
117117
Logger.Setup();
118+
WebGraphGenerator.Setup();
118119

119120
{$IFDEF WL_TEST}
120121
RSClient.Client := ERSClient.RUNELITE;

osrs/position/map/map.simba

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ begin
8585
if Self.Sample.Amount = 0 then Self.Sample.Amount := 4;
8686
if Self.Sample.Radius = 0 then Self.Sample.Radius := -1;
8787

88-
img := Minimap.GetCleanImage($FFFF, Self.Sample.Radius);
88+
if Minimap.Radius = 0 then
89+
raise GetDebugLn('Minimap.Radius is set to 0, did you target the client?');
90+
img := new TImage(Minimap.Radius*2+1,Minimap.Radius*2+1);
91+
img.ReplaceColor($0, $FFFFFF);
8992
sample := Minimap.ScaleMinimap(img, Self.Loader.Downscale, Self.Sample.Radius);
9093

9194
Self.Cache.MatchTemplate := TMatchTemplateCache.Create(Self.Loader.DownscaledMap, sample, ETMFormula.TM_CCOEFF_NORMED);

osrs/position/map/maploader.simba

Lines changed: 58 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,11 @@ var
488488
region: TRSMapRegion;
489489
i, j, k, plane, regionCount: Integer;
490490
maps, heightmaps, collisionmaps: TImageArray;
491-
topLeft, btmRight: TPoint;
491+
topLeft, btmRight, offset: TPoint;
492492
graphs: array of TWebGraph;
493-
boxes: TBoxArray;
493+
boxes, oldRegions: TBoxArray;
494494
newMap, newHeightmap, newCollision: TImage;
495-
existingBox: TBox;
496-
oldRegions: array of TBox;
497-
offset: TPoint;
495+
oldMapBox: TBox;
498496
begin
499497
if Self.Downscale <> downscale then
500498
raise GetDebugLn('MapLoader', 'Cannot add chunks with different downscale value');
@@ -529,7 +527,6 @@ begin
529527
maps += Self.GetMap(region.Chunks, plane, ERSMap.NORMAL);
530528
maps.Last.Pad(padding);
531529

532-
//heightmap are shared accross planes
533530
heightmaps += Self.GetMap(region.Chunks, plane, ERSMap.HEIGHT);
534531
heightmaps.Last.Pad(padding);
535532

@@ -544,18 +541,20 @@ begin
544541
Self.Regions += region;
545542
end;
546543

547-
if Length(Self.Regions) = regionCount then Exit;
544+
if Length(Self.Regions) = regionCount then
545+
Exit;
548546

549547
SetLength(oldRegions, regionCount);
550548
for i := 0 to regionCount - 1 do
549+
begin
551550
oldRegions[i] := Self.Regions[i].Region;
552-
553-
for i := 0 to regionCount - 1 do
554551
boxes.Insert(Self.Regions[i].Region, i);
552+
end;
555553

556554
boxes := boxes.Pack();
557-
existingBox := Self.MapBox;
555+
oldMapBox := Self.MapBox;
558556
Self.MapBox := [];
557+
559558
for i := 0 to High(boxes) do
560559
begin
561560
Self.Regions[i].Region := boxes[i];
@@ -566,97 +565,74 @@ begin
566565
newHeightmap := new TImage(Self.MapBox.Width, Self.MapBox.Height);
567566
newCollision := new TImage(Self.MapBox.Width, Self.MapBox.Height);
568567

569-
if (existingBox.Width > 0) and (existingBox.Height > 0) then
568+
for i := 0 to regionCount - 1 do
570569
begin
571-
for i := 0 to regionCount - 1 do
572-
begin
573-
newMap.DrawImage(Self.Map.Copy(oldRegions[i]), Self.Regions[i].Region.TopLeft);
574-
if Assigned(Self.Heightmap) then
575-
newHeightmap.DrawImage(Self.Heightmap.Copy(oldRegions[i]), Self.Regions[i].Region.TopLeft);
576-
newCollision.DrawImage(Self.Collision.Copy(oldRegions[i]), Self.Regions[i].Region.TopLeft);
577-
end;
570+
topLeft := Self.Regions[i].Region.TopLeft;
571+
newMap.DrawImage(Self.Map.Copy(oldRegions[i]), topLeft);
572+
newHeightmap.DrawImage(Self.Heightmap.Copy(oldRegions[i]), topLeft);
573+
newCollision.DrawImage(Self.Collision.Copy(oldRegions[i]), topLeft);
578574
end;
579575

580576
for i := regionCount to High(Self.Regions) do
581577
begin
582-
newMap.DrawImage(maps[i - regionCount], Self.Regions[i].Region.TopLeft);
583-
if i - regionCount < Length(heightmaps) then
584-
newHeightmap.DrawImage(heightmaps[i - regionCount], Self.Regions[i].Region.TopLeft);
585-
newCollision.DrawImage(collisionmaps[i - regionCount], Self.Regions[i].Region.TopLeft);
578+
j := i - regionCount;
579+
topLeft := Self.Regions[i].Region.TopLeft;
580+
newMap.DrawImage(maps[j], topLeft);
581+
newHeightmap.DrawImage(heightmaps[j], topLeft);
582+
newCollision.DrawImage(collisionmaps[j], topLeft);
586583
end;
587584

588585
Self.Map := newMap;
589-
if Length(heightmaps) > 0 then
590-
Self.Heightmap := newHeightmap;
586+
Self.Heightmap := newHeightmap;
591587
Self.Collision := newCollision;
592588

593589
Self.HalfMap := Self.Map.Downsample(2);
594590
Self.DownscaledMap := Self.Map.Downsample(Self.Downscale);
595591

596-
if not WebGraphGenerator.Disabled then
592+
if WebGraphGenerator.Disabled then Exit;
593+
594+
for i := 0 to regionCount - 1 do
597595
begin
598-
for i := 0 to regionCount - 1 do
599-
begin
600-
offset := Self.Regions[i].Region.TopLeft - oldRegions[i].TopLeft;
601-
602-
if offset <> [0, 0] then
603-
begin
604-
for j := 0 to High(Self.Graph.Nodes) do
605-
if Self.Graph.Nodes[j] in oldRegions[i] then
606-
Self.Graph.Nodes[j] := Self.Graph.Nodes[j] + offset;
607-
608-
for j := 0 to High(Self.Graph.WalkableSpace) do
609-
if Self.Graph.WalkableSpace[j] in oldRegions[i] then
610-
Self.Graph.WalkableSpace[j] := Self.Graph.WalkableSpace[j] + offset;
611-
612-
for j := 0 to High(Self.Graph.WalkableClusters) do
613-
for k := 0 to High(Self.Graph.WalkableClusters[j]) do
614-
if Self.Graph.WalkableClusters[j][k] in oldRegions[i] then
615-
Self.Graph.WalkableClusters[j][k] := Self.Graph.WalkableClusters[j][k] + offset;
616-
617-
for j := 0 to High(Self.Graph.ObjectClusters) do
618-
for k := 0 to High(Self.Graph.ObjectClusters[j]) do
619-
if Self.Graph.ObjectClusters[j][k] in oldRegions[i] then
620-
Self.Graph.ObjectClusters[j][k] := Self.Graph.ObjectClusters[j][k] + offset;
621-
622-
for j := 0 to High(Self.Graph.Doors) do
623-
if Self.Graph.Doors[j].Center in oldRegions[i] then
624-
begin
625-
Self.Graph.Doors[j].Before := Self.Graph.Doors[j].Before + offset;
626-
Self.Graph.Doors[j].After := Self.Graph.Doors[j].After + offset;
627-
Self.Graph.Doors[j].Center := Self.Graph.Doors[j].Center + offset;
628-
end;
629-
end;
630-
end;
596+
offset := Self.Regions[i].Region.TopLeft - oldRegions[i].TopLeft;
631597

632-
for i := 0 to High(graphs) do
598+
if offset <> [0, 0] then
633599
begin
634-
offset := Self.Regions[regionCount + i].Region.TopLeft;
635-
636-
graphs[i].Nodes := graphs[i].Nodes.Offset(offset);
637-
graphs[i].WalkableSpace := graphs[i].WalkableSpace.Offset(offset);
638-
639-
for j := 0 to High(graphs[i].WalkableClusters) do
640-
graphs[i].WalkableClusters[j] := graphs[i].WalkableClusters[j].Offset(offset);
641-
642-
for j := 0 to High(graphs[i].ObjectClusters) do
643-
graphs[i].ObjectClusters[j] := graphs[i].ObjectClusters[j].Offset(offset);
600+
for j := 0 to High(Self.Graph.Nodes) do
601+
if Self.Graph.Nodes[j] in oldRegions[i] then
602+
Self.Graph.Nodes[j] := Self.Graph.Nodes[j] + offset;
603+
604+
for j := 0 to High(Self.Graph.WalkableSpace) do
605+
if Self.Graph.WalkableSpace[j] in oldRegions[i] then
606+
Self.Graph.WalkableSpace[j] := Self.Graph.WalkableSpace[j] + offset;
607+
608+
for j := 0 to High(Self.Graph.WalkableClusters) do
609+
for k := 0 to High(Self.Graph.WalkableClusters[j]) do
610+
if Self.Graph.WalkableClusters[j][k] in oldRegions[i] then
611+
Self.Graph.WalkableClusters[j][k] := Self.Graph.WalkableClusters[j][k] + offset;
612+
613+
for j := 0 to High(Self.Graph.ObjectClusters) do
614+
for k := 0 to High(Self.Graph.ObjectClusters[j]) do
615+
if Self.Graph.ObjectClusters[j][k] in oldRegions[i] then
616+
Self.Graph.ObjectClusters[j][k] := Self.Graph.ObjectClusters[j][k] + offset;
617+
618+
for j := 0 to High(Self.Graph.Doors) do
619+
if Self.Graph.Doors[j].Center in oldRegions[i] then
620+
begin
621+
Self.Graph.Doors[j].Before := Self.Graph.Doors[j].Before + offset;
622+
Self.Graph.Doors[j].After := Self.Graph.Doors[j].After + offset;
623+
Self.Graph.Doors[j].Center := Self.Graph.Doors[j].Center + offset;
624+
end;
625+
end;
644626

645-
for j := 0 to High(graphs[i].Doors) do
646-
begin
647-
graphs[i].Doors[j].Before := graphs[i].Doors[j].Before + offset;
648-
graphs[i].Doors[j].After := graphs[i].Doors[j].After + offset;
649-
graphs[i].Doors[j].Center := graphs[i].Doors[j].Center + offset;
650-
end;
627+
end;
651628

652-
Self.Graph.Merge(graphs[i]);
653-
end;
629+
for i := 0 to High(graphs) do
630+
Self.Graph.Merge(graphs[i]);
654631

655-
for i := 0 to High(Self.Graph.Doors) do
656-
begin
657-
Self.Graph.Doors[i].Before := RSTranslator.Normalize(Self.Graph.Doors[i].Before);
658-
Self.Graph.Doors[i].After := RSTranslator.Normalize(Self.Graph.Doors[i].After);
659-
end;
632+
for i := 0 to High(Self.Graph.Doors) do
633+
begin
634+
Self.Graph.Doors[i].Before := RSTranslator.Normalize(Self.Graph.Doors[i].Before);
635+
Self.Graph.Doors[i].After := RSTranslator.Normalize(Self.Graph.Doors[i].After);
660636
end;
661637
end;
662638

utils/webgraphgen.simba

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ begin
2424
Result := HashData(EHashAlgo.CRC32, @data[0], Length(data));
2525
end;
2626

27+
(*
28+
## WebGraphGenerator.Setup
29+
```pascal
30+
procedure TWebGraphGenerator.Setup();
31+
```
32+
Sets up the `TWebGraphGenerator` with the following settings which are the
33+
defaults:
34+
```pascal
35+
WebGraphGenerator.Spacing := 18; //low = better/slower, high = worse/faster
36+
WebGraphGenerator.MinimumTiles := 4; //spaces with less tiles than this will be ignored.
37+
WebGraphGenerator.NodeRadius := 50; //closed space less than NodeRadius pixels will have a single node.
38+
WebGraphGenerator.MaxConnections := 6; //Max connections per node.
39+
WebGraphGenerator.WallCrossings := True; //Whether to allow wall crossings when there's a close path around it.
40+
```
41+
42+
Feel free to change the settings to suit your need and use the
43+
{ref}`Map Debugger` to see what the results look like.
44+
45+
To do so you **MUST DO IT BEFORE** setting up your {ref}`Map`.
46+
*)
47+
procedure TWebGraphGenerator.Setup();
48+
begin
49+
Self.Spacing := 18;
50+
Self.MinimumTiles := 4;
51+
Self.NodeRadius := 50;
52+
Self.MaxConnections := 6;
53+
Self.WallCrossings := True;
54+
end;
2755

2856
function TWebGraphGenerator.GetDoorDirection(center: TPoint; img: TImage): TPoint;
2957
var
@@ -192,12 +220,11 @@ begin
192220
end;
193221

194222

195-
196223
(*
197-
## TRSChunkLoader.BuildGraph
224+
## WebGraphGenerator.BuildGraph
198225
```pascal
199-
function TRSChunkLoader._BuildGraph(map: TMufasaBitmap; white, red: TPointArray): TWebGraphV2;
200-
function TRSChunkLoader.BuildGraph(name: String; map: TMufasaBitmap): TWebGraphV2;
226+
function TWebGraphGenerator._BuildGraph(map: TImage; white, red: TPointArray): TWebGraph;
227+
function TWebGraphGenerator.BuildGraph(name: String; map: TImage): TWebGraph;
201228
```
202229
Magically builds a webgraph for you for a given collision map passed into `map`.
203230

@@ -210,6 +237,28 @@ The collision map can only have 4 colors:
210237
```{note}
211238
This is an internal method. Don't use it if you don't know what you are doing.
212239
```
240+
241+
It's hard to go into detail on how this works but if you want to see the result
242+
run this example code:
243+
```pascal
244+
{$I WaspLib/osrs.simba}
245+
begin
246+
Map.Setup([Chunk(Box(49,54,50,53), 0)]);
247+
Map.Debug();
248+
end.
249+
```
250+
251+
This will setup a map with a small piece of varrock and then open the
252+
{ref}`Map Debugger`.
253+
254+
On the {ref}`Map Debugger` change the dropdown to the collision map if you want
255+
to see what it looks like, that is what will go through this as the `map`
256+
parameter and it should look something like this:
257+
258+
```{figure} ../../images/map_collision.png
259+
```
260+
261+
As a bonus you can also see the resulting webgraph!
213262
*)
214263
function TWebGraphGenerator._BuildGraph(map: TImage; white, red: TPointArray): TWebGraph;
215264
var
@@ -344,7 +393,6 @@ begin
344393
SetLength(Result.Names, Length(Result.Nodes));
345394
end;
346395

347-
348396
function TWebGraphGenerator.BuildGraph(name: String; map: TImage): TWebGraph;
349397
var
350398
white, red, gray, merged, graySubset: TPointArray;
@@ -393,11 +441,3 @@ var
393441
Global {ref}`TWebGraphGenerator` variable.
394442
*)
395443
WebGraphGenerator: TWebGraphGenerator;
396-
397-
begin
398-
WebGraphGenerator.Spacing := 18; //low = better/slower, high = worse/faster
399-
WebGraphGenerator.MinimumTiles := 4; //spaces with less tiles than this will be ignored.
400-
WebGraphGenerator.NodeRadius := 50; //closed space less than NodeRadius pixels will have a single node.
401-
WebGraphGenerator.MaxConnections := 6; //Max connections per node.
402-
WebGraphGenerator.WallCrossings := True; //Whether to allow wall crossings when there's a close path around it.
403-
end;

wasplib-docs

0 commit comments

Comments
 (0)