SRL Archive - SRL Archives

GetUniqueColor


nielsie95

2007-05-03 04:24:17

Hey,

I was reading through some threads (Junior VW Clay miner, Cows and FGM by Fakawi (and Wizzup?)) and I noticed that they use a specific dark treecolor that you need to set yourself.

I think that with SRL you don't have to set your own colors :p. So I thought of a way that might work: GetUniqueColor. It scans through the whole MM and gets all similar looking colors and stores them in an array. After that, it will count how many colors there are on the MM (for each color) and compare those stats to a given value (HowManyOnMM). The result will be the count that's the most close to HowManyOnMM.



type ColorMatch = Record //Set this as a type at the start of your script
Color, Pixels: Integer;
end;

{*******************************************************************************
function GetUniqueColor(var Color: integer; RefColor, Tol, HowManyOnMM, Range: integer): Boolean;
By: Nielsie95
Description: Will get a similar color with the closest count.
How to use:
-Color: Will result the color that's found
-RefColor: The color that will be used as a refenceColor
-Tol: Tolerance added to RefColor
-HowManyOnMM: A reference count of the colors
-Range: The range that will be added to the count
*******************************************************************************}
function GetUniqueColor(var Color: integer; RefColor, Tol, HowManyOnMM, Range: integer): Boolean;
var
ColorArray: Array of ColorMatch;
Colors: Array of integer;
TempCount, TempCount2, TempColor, i, j: integer;
begin
SetArrayLength(Colors, 0);
for i := MMX1 to MMX2 do
for j := MMY1 to MMY2 do
if Rs_OnMinimap(i, j) then
begin
TempColor := GetColor(i, j);
if SimilarColors(RefColor, TempColor, Tol) then
begin
SetArrayLength(Colors, Length(Colors) +1);
Colors[(Length(Colors)) -1] := TempColor;
end
end
SetArrayLength(ColorArray, 0);
for i := 0 to Length(Colors) -1 do
begin
TempCount := CountColor(Colors[i], MMX1, MMY1, MMX2, MMY2);
if TempCount = HowManyOnMM then
begin
if Colors[i] > 0 then
begin
Color := Colors[i];
Result := True;
WriteLn('Found color with range 0');
Exit;
end
end else
begin
if Colors[i] > 0 then
begin
TempCount2 := HowManyOnMM - TempCount;
if TempCount2 < 0 then TempCount2 := TempCount - HowManyOnMM;
if (TempCount2 < Range) then
begin
SetArrayLength(ColorArray, Length(ColorArray) +1);
ColorArray[(Length(ColorArray)) -1].Color := Colors[i];
ColorArray[(Length(ColorArray)) -1].Pixels := TempCount2;
end
end;
end;
end;

if Length(ColorArray) = 1 then
begin
Color := ColorArray[0].Color;
Result := True;
WriteLn('Found color with range '+IntTostr(ColorArray[0].Pixels));
Exit;
end
try
begin
TempCount := Min(ColorArray[0].Pixels, ColorArray[1].Pixels)
for i := 1 to Length(ColorArray) -2 do TempCount := Min(TempCount, ColorArray[i +1].Pixels);
for i := 0 to Length(ColorArray) -1 do if TempCount = ColorArray[i].Pixels then
begin
Color := ColorArray[i].Color;
Result := True;
WriteLn('Found color with range '+IntTostr(ColorArray[i].Pixels));
Exit;
end
end
except
Result := False;
end;
if not Result then WriteLn('Unique Color NOT found! Please set it yourself.');
end;


GetUniqueColor2 –> At the request of Freddy. This uses rs_FindMMColors, no need to set Tolerance in this one.

function GetUniqueColor2(var Color: integer; RefColor, Count, Range: integer): Boolean;
var Colors: TIntegerArray; i, TempCount, TempCount2: integer; ColorArray: Array of ColorMatch;
begin
ColorToleranceSpeed(2);
Colors := rs_FindMMColors(RefColor);
SetArrayLength(ColorArray, 0);
for i := 0 to Length(Colors) -1 do
begin
TempCount := CountColor(Colors[i], MMX1, MMY1, MMX2, MMY2);
if TempCount = Count then
begin
if Colors[i] > 0 then
begin
Color := Colors[i];
Result := True;
WriteLn('Found color with range 0');
Exit;
end
end else
begin
if Colors[i] > 0 then
begin
TempCount2 := Count - TempCount;
if TempCount2 < 0 then TempCount2 := TempCount - Count;
if (TempCount2 < Range) then
begin
SetArrayLength(ColorArray, Length(ColorArray) +1);
ColorArray[(Length(ColorArray)) -1].Color := Colors[i];
ColorArray[(Length(ColorArray)) -1].Pixels := TempCount2;
end
end;
end;
end;

if Length(ColorArray) = 1 then
begin
Color := ColorArray[0].Color;
Result := True;
WriteLn('Found color with range '+IntTostr(ColorArray[0].Pixels));
Exit;
end
try
begin
TempCount := Min(ColorArray[0].Pixels, ColorArray[1].Pixels)
for i := 1 to Length(ColorArray) -2 do TempCount := Min(TempCount, ColorArray[i +1].Pixels);
for i := 0 to Length(ColorArray) -1 do if TempCount = ColorArray[i].Pixels then
begin
Color := ColorArray[i].Color;
Result := True;
WriteLn('Found color with range '+IntTostr(ColorArray[i].Pixels));
Exit;
end
end
except
Result := False;
end;
if not Result then WriteLn('Unique Color NOT found! Please set it yourself.');
ColorToleranceSpeed(1);
end;


Example:

This will work for FGM-spot (stand near the tree) + FE and VW bank. So that means Junior's VW Clay Banker + FGM + Cows :D


program GetTreePixel;
{.Include SRL/SRL.SCAR}

var TreePixel: integer;

type ColorMatch = Record
Color, Pixels: Integer;
end;

function GetUniqueColor(var Color: integer; RefColor, Tol, HowManyOnMM, Range: integer): Boolean;
var
ColorArray: Array of ColorMatch;
Colors: Array of integer;
TempCount, TempCount2, TempColor, i, j: integer;
begin
SetArrayLength(Colors, 0);
for i := MMX1 to MMX2 do
for j := MMY1 to MMY2 do
if Rs_OnMinimap(i, j) then
begin
TempColor := GetColor(i, j);
if SimilarColors(RefColor, TempColor, Tol) then
begin
SetArrayLength(Colors, Length(Colors) +1);
Colors[(Length(Colors)) -1] := TempColor;
end
end
SetArrayLength(ColorArray, 0);
for i := 0 to Length(Colors) -1 do
begin
TempCount := CountColor(Colors[i], MMX1, MMY1, MMX2, MMY2);
if TempCount = HowManyOnMM then
begin
if Colors[i] > 0 then
begin
Color := Colors[i];
Result := True;
WriteLn('Found color with range 0');
Exit;
end
end else
begin
if Colors[i] > 0 then
begin
TempCount2 := HowManyOnMM - TempCount;
if TempCount2 < 0 then TempCount2 := TempCount - HowManyOnMM;
if (TempCount2 < Range) then
begin
SetArrayLength(ColorArray, Length(ColorArray) +1);
ColorArray[(Length(ColorArray)) -1].Color := Colors[i];
ColorArray[(Length(ColorArray)) -1].Pixels := TempCount2;
end
end;
end;
end;

if Length(ColorArray) = 1 then
begin
Color := ColorArray[0].Color;
Result := True;
WriteLn('Found color with range '+IntTostr(ColorArray[0].Pixels));
Exit;
end
try
begin
TempCount := Min(ColorArray[0].Pixels, ColorArray[1].Pixels)
for i := 1 to Length(ColorArray) -2 do TempCount := Min(TempCount, ColorArray[i +1].Pixels);
for i := 0 to Length(ColorArray) -1 do if TempCount = ColorArray[i].Pixels then
begin
Color := ColorArray[i].Color;
Result := True;
WriteLn('Found color with range '+IntTostr(ColorArray[i].Pixels));
Exit;
end
end
except
Result := False;
end;
if not Result then WriteLn('Unique Color NOT found! Please set it yourself.');
end;

begin
SetupSRl;
ActivateClient;
if GetUniqueColor(TreePixel, 1587244, 15, 30, 15) then WriteLn('Color: '+IntTostr(TreePixel));
end.


If you want to test this or get the CountValues I made a little script which I added (Set DebugCount True and fill in the color you want to count).

For now this needs Divi, but if there are people who want/need this in 2.03 I will make it :).

Tell me what you think,
Nielsie95

Bobarkinator

2007-05-03 08:12:47

Sweet. This will make autocoloring much more easy. I'm going to test this after school.

nielsie95

2007-05-03 08:30:24

Great :). You might have to play a bit with tolerance and range :)

Lorax

2007-05-03 09:16:41

Looks fine, could be done in another way, but great job.. keep up the GOoD work..

Freddy1990

2007-05-03 10:23:30

Heh, the rs_ScanMiniMap function in SCAR Divi also finds colors on the minimap similar to an entered color. I might still need to tweak it a bit, but it works pretty good, it returns an array of the found colors… Pretty good for road colors :)

nielsie95

2007-05-03 10:41:52

I think you mean function rs_FindMMColors? Yes, I could do that :). The only thing is that you can't enter tolerance. Do you think I need tolerance in this kind of functions? If not, I think I'll convert it to rs_FindMMColors :).

Lorax

2007-05-05 09:15:18

If you're correct Nielsie – Freddy got pwned ;)
lol

Wizzup?

2007-05-05 10:01:09

Is this what you made?
I know it won't compile.. cause I just grabbed it out of my script.

You could add:
-MinCount of colors Colors[].Amount.
-Tol, is now set to 15.
-Change result to boolean.
-Add Color input, lol it uses an global var runeshopcolor.


Type
AColor = Record
Color: Integer;
Amount: Integer;
End;

Var
Colors: Array Of AColor;

Function AuburyFloorColor: Integer;

Var
Points: TPointArray;
CColors: Array Of Integer;
I, iColor, C, MAmount: Integer;

Begin
ColorToleranceSpeed(2);
FindColorsTolerance(Points, RuneShopColor, 540, 5, 740, 155, 15);
CColors := GetColors(Points);
SetArrayLength(Colors, GetArrayLength(CColors));
For I := 0 To GetArrayLength(CColors) - 1 Do
Colors[I].Color := CColors[I];

For I := 0 To GetArrayLength(CColors) - 1 Do
Begin
For C := 0 To GetArrayLength(Colors) - 1 Do
If CColors[I] = Colors[C].Color Then
Colors[C].Amount := Colors[C].Amount + 1;
End;

For I := 0 To GetArrayLength(Colors) - 1 Do
Begin
If MAmount <> Max(Colors[I].Amount, MAmount) Then
iColor := I;
MAmount := Max(Colors[I].Amount, MAmount);
End;

If I <> 0 Then Result := Colors[iColor].Color;
WriteLn('Color: '+IntToStr(Result));
ColorToleranceSpeed(1);
End;

nielsie95

2007-05-06 05:14:01

@Wizzup: Yes, that's somekind of what I made :p.

Added GetUniqueColor2. This uses rs_FindMMColors! No need to set the tolerance with this one.

Maybe I'll make a GetUniqueColor3 based on Wizzup's version :)