|
| 1 | +(* |
| 2 | +# HitSplats |
| 3 | +*) |
| 4 | +{$DEFINE SRLT_HITSPLATS_INCLUDED} |
| 5 | +{$INCLUDE_ONCE SRLT/osrs.simba} |
| 6 | + |
| 7 | +type |
| 8 | +(* |
| 9 | +(ERSHitSplat)= |
| 10 | +## type ERSHitSplat |
| 11 | +```pascal |
| 12 | +ERSHitSplat = enum(BLUE, RED, MAX); |
| 13 | +``` |
| 14 | +Enum representing the main hitsplat colors. |
| 15 | +*) |
| 16 | + ERSHitSplat = enum(BLUE, RED, MAX); |
| 17 | + |
| 18 | +(* |
| 19 | +(TRSHitsplat)= |
| 20 | +## type TRSHitSplat |
| 21 | +Hitsplat record that holds the methods used to find hitsplats. |
| 22 | +*) |
| 23 | + TRSHitsplat = record |
| 24 | + Position: TPoint; |
| 25 | + Value: Integer; |
| 26 | + Color: ERSHitSplat; |
| 27 | + const TEXT_WHITE: TColor = $E3E3E3; |
| 28 | + const TEXT_TOLERANCE: Double = 10.981; |
| 29 | + end; |
| 30 | + |
| 31 | + TRSHitSplatArray = array of TRSHitsplat; |
| 32 | + |
| 33 | + |
| 34 | +function TRSHitSplat._Find(area: TBox): TRSHitSplat; static; |
| 35 | +var |
| 36 | + blue, red, maxhit: TPointArray; |
| 37 | +begin |
| 38 | + blue := Target.FindColor(ColorTolerance($E03E33, 0.600, EColorSpace.HSV, [1.435, 1.435, 0.131]), area); |
| 39 | + if blue <> [] then |
| 40 | + begin |
| 41 | + Result.Position := area.TopLeft; |
| 42 | + Result.Value := OCR.RecognizeNumber(area, RSFonts.PLAIN_11, [Self.TEXT_WHITE], Self.TEXT_TOLERANCE); |
| 43 | + Result.Color := ERSHitSplat.BLUE; |
| 44 | + Exit; |
| 45 | + end; |
| 46 | + |
| 47 | + red := Target.FindColor(ColorTolerance($0000A5, 0.498, EColorSpace.HSV, [1.413, 1.413, 0.176]), area); |
| 48 | + if red <> [] then |
| 49 | + begin |
| 50 | + Result.Position := area.TopLeft; |
| 51 | + Result.Value := OCR.RecognizeNumber(area, RSFonts.PLAIN_11, [Self.TEXT_WHITE], Self.TEXT_TOLERANCE); |
| 52 | + Result.Color := ERSHitSplat.RED; |
| 53 | + Exit; |
| 54 | + end; |
| 55 | + |
| 56 | + maxhit := Target.FindColor(ColorTolerance($0000D8, 0.488, EColorSpace.HSV, [1.433, 1.433, 0.136]), area); |
| 57 | + if maxhit <> [] then |
| 58 | + begin |
| 59 | + Result.Position := area.TopLeft; |
| 60 | + Result.Value := OCR.RecognizeNumber(area, RSFonts.PLAIN_11, [Self.TEXT_WHITE], Self.TEXT_TOLERANCE); |
| 61 | + Result.Color := ERSHitSplat.MAX; |
| 62 | + Exit; |
| 63 | + end; |
| 64 | +end; |
| 65 | + |
| 66 | +(* |
| 67 | +## TRSHitSplat.Find |
| 68 | +```pascal |
| 69 | +function TRSHitSplat.Find(out splats: TRSHitSplatArray; areas: TBoxArray): Boolean; static; |
| 70 | +function TRSHitSplat.Find(out splats: TRSHitSplatArray; areas: TPolygonArray): Boolean; static; overload; |
| 71 | +function TRSHitSplat.Find(out splats: TRSHitSplatArray; areas: TCuboidArray): Boolean; static; overload; |
| 72 | +function TRSHitSplat.Find(out splats: TRSHitSplatArray): Boolean; static; overload; |
| 73 | +``` |
| 74 | +Attempts to find hitsplats on the given `area` a {ref}`TColorFinder`. |
| 75 | +If `area` is not specified, {ref}`MainScreen` Bounds are used. |
| 76 | +The function returns true if we find at least one hitsplat and found splats are |
| 77 | +returned via `splats`. |
| 78 | +*) |
| 79 | +function TRSHitSplat.Find(out splats: TRSHitSplatArray; areas: TBoxArray): Boolean; static; |
| 80 | +var |
| 81 | + area, b: TBox; |
| 82 | + white, black, tpa: TPointArray; |
| 83 | + clusters: T2DPointArray; |
| 84 | + hit: TRSHitSplat; |
| 85 | +begin |
| 86 | + splats := []; |
| 87 | + for area in areas do |
| 88 | + begin |
| 89 | + white := Target.FindColor($E3E3E3, 10.981, area); |
| 90 | + black := Target.FindColor($0, 0, area); |
| 91 | + clusters := white.PointsNearby(black, 1,3).Cluster(4); |
| 92 | + |
| 93 | + if clusters = [] then Exit; |
| 94 | + |
| 95 | + for tpa in clusters do |
| 96 | + begin |
| 97 | + with tpa.Bounds() do b := [X1-1, Y1-1, X2+2, Y2+3]; |
| 98 | + |
| 99 | + hit := TRSHitsplat._Find(b); |
| 100 | + if hit.Position <> Default(TPoint) then |
| 101 | + splats += hit; |
| 102 | + end; |
| 103 | + end; |
| 104 | + Result := splats <> []; |
| 105 | +end; |
| 106 | + |
| 107 | +function TRSHitSplat.Find(out splats: TRSHitSplatArray; areas: TPolygonArray): Boolean; static; overload; |
| 108 | +var |
| 109 | + poly: TPolygon; |
| 110 | + area, b: TBox; |
| 111 | + white, black, tpa: TPointArray; |
| 112 | + clusters: T2DPointArray; |
| 113 | + hit: TRSHitSplat; |
| 114 | +begin |
| 115 | + splats := []; |
| 116 | + for poly in areas do |
| 117 | + begin |
| 118 | + area := poly.Bounds(); |
| 119 | + white := Target.FindColor($E3E3E3, 10.981, area); |
| 120 | + black := Target.FindColor($0, 0, area); |
| 121 | + clusters := white.PointsNearby(black, 1,3).Cluster(4); |
| 122 | + |
| 123 | + if clusters = [] then Exit; |
| 124 | + |
| 125 | + for tpa in clusters do |
| 126 | + begin |
| 127 | + with tpa.Bounds() do |
| 128 | + begin |
| 129 | + if not poly.Contains(Center) then Continue; |
| 130 | + b := [X1-1, Y1-1, X2+2, Y2+3]; |
| 131 | + end; |
| 132 | + |
| 133 | + hit := TRSHitsplat._Find(b); |
| 134 | + if hit.Position <> Default(TPoint) then |
| 135 | + splats += hit; |
| 136 | + end; |
| 137 | + end; |
| 138 | + Result := splats <> []; |
| 139 | +end; |
| 140 | + |
| 141 | +function TRSHitSplat.Find(out splats: TRSHitSplatArray; areas: TCuboidArray): Boolean; static; overload; |
| 142 | +var |
| 143 | + poly: TPolygon; |
| 144 | + area: TCuboid; |
| 145 | + bounds, b: TBox; |
| 146 | + white, black, tpa: TPointArray; |
| 147 | + clusters: T2DPointArray; |
| 148 | + hit: TRSHitSplat; |
| 149 | +begin |
| 150 | + splats := []; |
| 151 | + for area in areas do |
| 152 | + begin |
| 153 | + bounds := area.Bounds(); |
| 154 | + white := Target.FindColor($E3E3E3, 10.981, bounds); |
| 155 | + black := Target.FindColor($0, 0, bounds); |
| 156 | + clusters := white.PointsNearby(black, 1,3).Cluster(4); |
| 157 | + |
| 158 | + if clusters = [] then Exit; |
| 159 | + |
| 160 | + poly := area.Polygon(); |
| 161 | + |
| 162 | + for tpa in clusters do |
| 163 | + begin |
| 164 | + with tpa.Bounds() do |
| 165 | + begin |
| 166 | + if not poly.Contains(Center) then Continue; |
| 167 | + b := [X1-1, Y1-1, X2+2, Y2+3]; |
| 168 | + end; |
| 169 | + |
| 170 | + hit := TRSHitsplat._Find(b); |
| 171 | + if hit.Position <> Default(TPoint) then |
| 172 | + splats += hit; |
| 173 | + end; |
| 174 | + end; |
| 175 | + Result := splats <> []; |
| 176 | +end; |
| 177 | + |
| 178 | +function TRSHitSplat.Find(out splats: TRSHitSplatArray): Boolean; static; overload; |
| 179 | +begin |
| 180 | + Result := TRSHitSplat.Find(splats, [MainScreen.Bounds]); |
| 181 | +end; |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | +procedure TRSHitsplat.Draw(img: TImage); |
| 186 | +begin |
| 187 | + case Self.Color of |
| 188 | + ERSHitSplat.BLUE: img.DrawColor := $FF0000; |
| 189 | + ERSHitSplat.RED: img.DrawColor := $0000FF; |
| 190 | + ERSHitSplat.MAX: img.DrawColor := $00FFFF; |
| 191 | + end; |
| 192 | + img.DrawCircle(Self.Position, 10); |
| 193 | +end; |
| 194 | + |
| 195 | +procedure TRSHitSplatArray.Draw(img: TImage); |
| 196 | +var |
| 197 | + splat: TRSHitSplat; |
| 198 | +begin |
| 199 | + for splat in Self do splat.Draw(img); |
| 200 | +end; |
| 201 | + |
| 202 | +procedure ShowOnClient(splats: TRSHitSplatArray); overload; |
| 203 | +var |
| 204 | + img: TImage; |
| 205 | +begin |
| 206 | + img := TImage.CreateFromTarget(); |
| 207 | + |
| 208 | + splats.Draw(img); |
| 209 | + |
| 210 | + img.Show(); |
| 211 | + img.Free(); |
| 212 | +end; |
0 commit comments