Coh3n
2009-11-26 17:15:18
AutoColor.scar
So as a result of the autocoloring functions not working for me, Nava has graciously made an awesome function for me to use that can be used for any color by editing only one line. This gave me a cool idea (well, cool to me at least :tongue:). Basically, it consists of a single AutoColor funtion instead of a whole function for every color. It has a second function that loads the necessary RGB ranges for the AutoColor function to work.
This way is very fast (takes 19ms to find the color), works 99.9% of the time, and is much for efficient then the current AutoColor.scar. Anyway, I just wanted to see what people think.
program AutoColor;
{.Include SRL/SRL.scar}
{ const AutoColor Constants;
Description: Constants representing the different types of AutoColors. }
const
DraynorRoadColor = 0;
DraynorBankColor = 1;
{
.
.
.
}
{ 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
RoadColor:
Result := [{minR} 73, {maxR} 100, {minG} 75, {maxG} 100, {minB} 72, {maxB} 97];
DraynorBankColor:
Result := [{minR} 49, {maxR} 63, {minG} 51, {maxG} 64, {minB} 47, {maxB} 60];
{
.
.
.
}
else
srl_Warn('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 Find(Which: Integer): Integer;
By: Nava2
Description: Finds best color on the minimap, depending on the RGB ranges.
*******************************************************************************}
function 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;
begin
SetupSRL;
ActivateClient;
ClearDebug;
Find(DraynorBankColor);
Find(DraynorRoadColor);
end.