SRL Archive - MSI Suggestions

AutoColors.scar


Coh3n

2009-12-08 11:53:17

AutoColors.scar


I tried to organize it so you can easily add new colors. Look at the bottom of the post for a script that will get the RGB ranges for the colors you place in the array.


{==============================================================================\
| MSI Group Scripting Include |
| AutoColors.scar |
|==============================================================================|
| AutoColor routines for whatever fits our needs. |
| |
| * function ChooseAutoColor(Which: Integer): TIntegerArray; | * by Coh3n
| * function CircleTPA(cX, cY, r: Integer): TPointArray; | * by Nava2
| * function srl_ReturnMMTPA: TPointArray; | * by Nava2
| * function MSI_Find(Which: Integer): Integer; | * by Nava2
| |
\_____________________________________________________________________________}

{ const AutoColor Constants;
Description: Constants representing the different types of AutoColors. }
const
VarrockRoadColor = 0;

{
.
.
.
}

{ const RGB Constants;
Description: Constants representing the placement of RGB ranges in the
ChooseAutoColor array. }
const
minR = 0;
maxR = 1;
minG = 2;
maxG = 3;
minB = 4;
maxB = 5;

var
srl_MMTPA: TPointArray;

{*******************************************************************************
function ChooseAutoColor(Which: Integer): TIntegerArray;
By: Coh3n
Description: Creates an array of the RGB ranges to use in the autocolor function.
*******************************************************************************}
function ChooseAutoColor(Which: Integer): TIntegerArray;
begin
case Which of

VarrockRoadColor:
Result := [{minR} 128, {maxR} 145, {minG} 129, {maxG} 143, {minB} 123, {maxB} 139];

{
.
.
.
}

else
MSI_Debug('ChooseAutoColor', 'Not a vaild minimap color. ', warn_Debug);
end;
end;

{*******************************************************************************
function CircleTPA(cX, cY, r: Integer): TPointArray;
By: Nava2
Description: Creates a TPA of a circle with radius 'r' and center point cX, cY.
*******************************************************************************}
function CircleTPA(cX, cY, r: Integer): TPointArray;
var
i, j, l, tX, tY: Integer;
begin
setLength(Result, 4 * r * r);
l := 2 * r - 1;
tX := cX - r;
tY := cY - r;
for i := 0 to l do
for j := 0 to l do
Result[j + l * i] := Point(tX + i, tY + j);

Result := RemoveDistTPointArray(cX, cY, r, Result, True);
end;

{*******************************************************************************
function srl_ReturnMMTPA: TPointArray;
By: Nava2
Description: Performs CircleTPA for the RS minimap.
*******************************************************************************}
function srl_ReturnMMTPA: TPointArray;
begin
if High(srl_MMTPA) < 0 then
begin
Result := CircleTPA(MMCX, MMCY, (MMX2 - MMX1) div 2);
srl_MMTPA := Result;
end else
Result := srl_MMTPA;
end;

{*******************************************************************************
function MSI_Find(Which: Integer): Integer;
By: Nava2, edited by Coh3n
Description: Finds best color on the minimap, depending on the RGB ranges.
*******************************************************************************}
function MSI_Find(Which: Integer): Integer;
var
R, G, B: Integer;
i, h, c, curMax, j: Integer;
t: TPointArray;
CycleCols, newCols, colorCounts, Color: TIntegerArray;
begin
Color := ChooseAutoColor(Which);

{ Get all the colours in the minimap. }
CycleCols := GetColors(srl_ReturnMMTPA);
ClearSameIntegers(CycleCols);

{ Cycle through creating a new array based on the inputted RGB max/mins. }
h := High(CycleCols);
setLength(newCols, h + 1);
for i := 0 to h do
begin
ColorToRGB(CycleCols[i], R, G, B);

if (InRange(R, Color[minR], Color[maxR]) and
InRange(G, Color[minG], Color[maxG]) and
InRange(B, Color[minB], Color[maxB])) then
begin
newCols[c] := CycleCols[i];
Inc(c);
end;

end;

setLength(newCols, c);
setLength(colorCounts, c);
Dec(c);

{
Use the colour which is most common. Could be substituted to use other
means to pick the right colour post-RGB checks. (Density, TPA spread, etc.)
}
for i := 0 to c do
begin
FindColors(t, newCols[i], MMX1, MMY1, MMX2, MMY2);
h := Length(t);
if (h > curMax) then
begin
j := i;
curMax := h;
end;
end;

Result := newCols[j];
Writeln('Found Color: ' + IntToStr(Result));
end;

Now, to get the RGB ranges, place an array of colors in the Colors array in the main loop, and hit run. The ranges will appear in the debug box.

program AutoColoring;
{.Include SRL/SRL.scar}

var
srl_MMTPA: TPointArray;

function CircleTPA(cX, cY, r: Integer): TPointArray;
var
i, j, l, tX, tY: Integer;
begin
setLength(Result, 4 * r * r);
l := 2 * r - 1;
tX := cX - r;
tY := cY - r;
for i := 0 to l do
for j := 0 to l do
Result[j + l * i] := Point(tX + i, tY + j);

Result := RemoveDistTPointArray(cX, cY, r, Result, True);
end;

function srl_ReturnMMTPA: TPointArray;
begin
if High(srl_MMTPA) < 0 then
begin
Result := CircleTPA(MMCX, MMCY, (MMX2 - MMX1) div 2);
srl_MMTPA := Result;
end else
Result := srl_MMTPA;
end;

function AutoColorFunction: Integer;
var
R, G, B: Integer;
i, h, c, curMax, j: Integer;
t: TPointArray;
CycleCols, newCols, colorCounts: TIntegerArray;
begin
{ Get all the colours in the minimap. }
CycleCols := GetColors(srl_ReturnMMTPA);
ClearSameIntegers(CycleCols);

{ Cycle through creating a new array based on the inputted RGB max/mins. }
h := High(CycleCols);
setLength(newCols, h + 1);
for i := 0 to h do
begin
ColorToRGB(CycleCols[i], R, G, B);
if (InRange(R, 73, 100) and InRange(G, 75, 100)
and InRange(B, 72, 97)) then
begin
newCols[c] := CycleCols[i];
Inc(c);
end;
end;

setLength(newCols, c);
setLength(colorCounts, c);
Dec(c);

{
Use the colour which is most common. Could be substituted to use other
means to pick the right colour post-RGB checks. (Density, TPA spread, etc.)
}
for i := 0 to c do
begin
FindColors(t, newCols[i], MMX1, MMY1, MMX2, MMY2);
h := Length(t);
if (h > curMax) then
begin
j := i;
curMax := h;
end;
end;

Result := newCols[j];
Writeln('Found color: ' + IntToStr(Result));
end;

var
Colors: TIntegerArray;
minR, maxR, minG, maxG, minB, maxB, R, G, B: Integer;
i: Integer;
begin
ClearDebug;
{ Fill in RGB values }
Colors := [];

minR := 255;
minB := 255;
minG := 255;
for i := 0 to High(Colors) do
begin
ColorToRGB(Colors[i], R, G, B);
minR := Min(R, minR);
maxR := Max(R, maxR);
minG := Min(G, minG);
maxG := Max(G, maxG);
minB := Min(B, minB);
maxB := Max(B, maxB);

end;

Writeln('minR: ' + IntToStr(minR) + ', maxR: ' + IntToStr(maxR) +
' | minG: ' + IntToStr(minG) + ', maxG: ' + IntToStr(maxG) +
' | minB: ' + IntToStr(minB) + ', maxB: ' + IntToStr(maxB));
end.


minR: 255, maxR: 0 | minG: 255, maxG: 0 | minB: 255, maxB: 0
Successfully executed

marpis

2009-12-08 12:55:36

Looks good, but why not use TAutoColorInfo?

Coh3n

2009-12-08 13:27:13

From: marpis
Looks good, but why not use TAutoColorInfo?

Thanks. :smile:

Do you mean having the AutoColor function return a TAutoColorInfo record? Or something completely different? Lol.

EDIT: Or do you mean create a TAutoColorInfo record based on an array of colors, and then use that record in the AutoColor function? Because I think that would make more sense then my last guess.

EDIT2: If we did the latter, we could have an array of colors in the ChooseAutoColor function (instead of the RGB rangers), which would make it much easier for updating if the MM colors were to change.

Nava2

2009-12-08 15:55:34

Why not just keep a bunch of integers? Then, just use my function in Colors.scar and create a TAutoColorInfo on startup. :)

Coh3n

2009-12-08 15:58:38

From: Nava2
Why not just keep a bunch of integers? Then, just use my function in Colors.scar and create a TAutoColorInfo on startup. :)

That's what I meant to say in my EDIT2.

NCDS

2009-12-08 18:02:49

From: Nava2
Why not just keep a bunch of integers? Then, just use my function in Colors.scar and create a TAutoColorInfo on startup. :)


Wouldn't an array be quicker than doing separate Integers though? Unless you already meant TIntegerArray; :confused:

Nava2

2009-12-08 20:10:23

From: NCDS
Wouldn't an array be quicker than doing separate Integers though? Unless you already meant TIntegerArray; :confused:


I meant an array.

Just use an array then pass it to my CreateTAutoColor function and then you can have TAutoColorInfo's from just a constant array which is EASILY updated ;)

This might be one of the first things to ever use my hard work :rolleyes:

Coh3n

2009-12-08 20:11:58

From: Nava2
I meant an array.

Just use an array then pass it to my CreateTAutoColor function and then you can have TAutoColorInfo's from just a constant array which is EASILY updated ;)

This might be one of the first things to ever use my hard work :rolleyes:

Yeah that's exactly what I meant in my EDIT2 in a post further up. Or at least that's I tried to mean. :p If it was my choice, it would already be in SRL.

I'm going to work on this tomorrow probably.

Nava2

2009-12-08 20:13:18

From: Coh3n
Yeah that's exactly what I meant in my EDIT2 in a post further up. Or at least that's I tried to mean. :p

I'm going to work on this tomorrow probably.


Sounds good :)

Could possibly use SmartColors as well, after its been autocoloured. You could dynamically create the colour array to use with SmartColors.. might just be over kill though.

The TAutoColorInfo should be accurate enough.

Coh3n

2009-12-08 20:14:30

From: Nava2
The TAutoColorInfo should be accurate enough.

But will it be faster?

Nava2

2009-12-08 22:32:10

From: Coh3n
But will it be faster?


Which?

Coh3n

2009-12-08 23:15:01

From: Nava2
Which?

Using TAutoColorInfo.

Nava2

2009-12-08 23:45:45

From: Coh3n
Using TAutoColorInfo.


Initializing it? No, but after, would be the same speed. Not to mention cleaner than having multiple functions or using crazy arrays. :)

NCDS

2009-12-09 00:26:05

From: Nava2
Initializing it? No, but after, would be the same speed. Not to mention cleaner than having multiple functions or using crazy arrays. :)


[continued]
As well as being easier to code, and more organized for user readability
[/continued]

lordsaturn

2009-12-09 02:23:56

Why not have an array of smartcolors defined for each road color?

Nava2

2009-12-09 10:05:50

From: lordsaturn
Why not have an array of smartcolors defined for each road color?


The SmartColors function depends on certain colours being accurate. The roads are far from single static colours, so, to my understanding this wouldn't work very well.

Sir R. M8gic1an

2009-12-11 10:34:19

From: Nava2
The SmartColors function depends on certain colours being accurate. The roads are far from single static colours, so, to my understanding this wouldn't work very well.


SmartColors also depends on being able to check if it hit the right target or not, which isn't quite possible on MM….

~RM