SRL Archive - OSR Help

Find highest number in array


J J

2012-05-21 14:52:02

EDIT: Got it, thanks for the help.

I want to know in which box the biggest amount of yellow NPC colors are found. I've split the minimap up in 9 boxes and yeah this is the code:
procedure NPCWalk;
var
i: Integer;
NPCDots: Array[0..8] of Integer;
// North, Middle, South | North/east, East, South/east | North/west, West, South/west
begin
// NPC WALKING
SMART_ClearCanvas;
SMART_DrawBoxEx(False, IntToBox(MMCX-25, MMCY-75, MMCX+25, MMCY-25), clRed); // 0
SMART_DrawBoxEx(False, IntToBox(MMCX-25, MMCY-25, MMCX+25, MMCY+25), clRed); // 1
SMART_DrawBoxEx(False, IntToBox(MMCX-25, MMCY+25, MMCX+25, MMCY+75), clRed); // 2
SMART_DrawBoxEx(False, IntToBox(MMCX+25, MMCY-75, MMCX+75, MMCY-25), clRed); // 3
SMART_DrawBoxEx(False, IntToBox(MMCX+25, MMCY-25, MMCX+75, MMCY+25), clRed); // 4
SMART_DrawBoxEx(False, IntToBox(MMCX+25, MMCY+25, MMCX+75, MMCY+75), clRed); // 5
SMART_DrawBoxEx(False, IntToBox(MMCX-75, MMCY+25, MMCX-25, MMCY+75), clRed); // 6
SMART_DrawBoxEx(False, IntToBox(MMCX-75, MMCY-25, MMCX-25, MMCY+25), clRed); // 7
SMART_DrawBoxEx(False, IntToBox(MMCX-75, MMCY-75, MMCX-25, MMCY-25), clRed); // 8

ColorToleranceSpeed(1);
NPCDots[0] := CountColorTolerance(645100, MMCX-25, MMCY-75, MMCX+25, MMCY-25, 35);
NPCDots[1] := CountColorTolerance(645100, MMCX-25, MMCY-25, MMCX+25, MMCY+25, 35);
NPCDots[2] := CountColorTolerance(645100, MMCX-25, MMCY+25, MMCX+25, MMCY+75, 35);
NPCDots[3] := CountColorTolerance(645100, MMCX+25, MMCY-75, MMCX+75, MMCY-25, 35);
NPCDots[4] := CountColorTolerance(645100, MMCX+25, MMCY-25, MMCX+75, MMCY+25, 35);
NPCDots[5] := CountColorTolerance(645100, MMCX+25, MMCY+25, MMCX+75, MMCY+75, 35);
NPCDots[6] := CountColorTolerance(645100, MMCX-75, MMCY+25, MMCX-25, MMCY+75, 35);
NPCDots[7] := CountColorTolerance(645100, MMCX-75, MMCY-25, MMCX-25, MMCY+25, 35);
NPCDots[8] := CountColorTolerance(645100, MMCX-75, MMCY-75, MMCX-25, MMCY-25, 35);

for i:=0 to 8 do
WriteLn(''+IntToStr(NPCDots[i])+'');
end;

It's probably extremely easy but yeah. How do I find the highest integer in this array? High(NPCDots) gives 8 because it is [0..8]

Runaway

2012-05-21 15:05:38

Try this:


for i := 1 to 8 do
begin
Highest := NPCDots[0];
if (NPCDots[i] > Highest) then
Highest := NPCDots[i];
end;

Haven't tested it, but it makes sense :p

EDIT: Also, use a really high tolerance. I used NPC dots for walking in my miner and the NPC dot colors change wildy. They can go from 59xxx -> 21xxxxx.

J J

2012-05-21 15:08:44

From: Runaway
Try this:


for i := 1 to 8 do
begin
Highest := NPCDots[0];
if (NPCDots[i] > Highest) then
Highest := NPCDots[i];
end;

Haven't tested it, but it makes sense :p

Hm thanks will test
Also rewrote mine to an array so yeah

procedure NPCWalk;
var
i: Integer;
NPCDots: Array[0..8] of Integer;
X1, Y1, X2, Y2: Array of Integer;

begin
// North, Middle, South | North/east, East, South/east | North/west, West, South/west
X1 := [-25, -25, -25, 25, 25, 25, -75, -75, -75];
Y1 := [-75, -25, 25, -75, -25, 25, 25, -25, -75];
X2 := [25, 25, 25, 75, 75, 75, -25, -25, -25];
Y2 := [-25, 25, 75, -25, 25, 75, 75, 25, -25];

ColorToleranceSpeed(1);
SMART_ClearCanvas;
for i:=0 to 8 do
begin
SMART_DrawBoxEx(False, IntToBox(MMCX+X1[i], MMCY+Y1[i], MMCX+X2[i], MMCY+Y2[i]), clRed); // 0
NPCDots[i] := CountColorTolerance(645100, MMCX+X1[i], MMCY+Y1[i], MMCX+X2[i], MMCY+Y2[i], 35);
WriteLn(''+IntToStr(NPCDots[i])+'');
end;
end;

Will edit it now :P

EDIT: your idea will output the highest number of colors found. That way I still don't know in which box it finds the highest number of colors, but it helped anyway. Trying to figure it out now.

EDIT 2: By simply declaring HighestBox as i it will work :)

function NPCWalk: String;
var
i, Highest, HighestBox: Integer;
NPCDots: Array[0..8] of Integer;
X1, Y1, X2, Y2: Array of Integer;

begin
// North, Middle, South | North/east, East, South/east | North/west, West, South/west
X1 := [-25, -25, -25, 25, 25, 25, -75, -75, -75];
Y1 := [-75, -25, 25, -75, -25, 25, 25, -25, -75];
X2 := [25, 25, 25, 75, 75, 75, -25, -25, -25];
Y2 := [-25, 25, 75, -25, 25, 75, 75, 25, -25];

ColorToleranceSpeed(1);
SMART_ClearCanvas;
Highest:=0;
for i:=0 to 8 do
begin
SMART_DrawBoxEx(False, IntToBox(MMCX+X1[i], MMCY+Y1[i], MMCX+X2[i], MMCY+Y2[i]), clRed); // 0
NPCDots[i] := CountColorTolerance(645100, MMCX+X1[i], MMCY+Y1[i], MMCX+X2[i], MMCY+Y2[i], 35);
if (NPCDots[i] > Highest) then
begin
Highest := NPCDots[i];
HighestBox := i;
end;
end;
case i of
0: Result := 'North';
1: Result := 'Middle';
2: Result := 'South';
3: Result := 'NorthEast';
4: Result := 'East';
5: Result := 'SouthEast';
6: Result := 'NorthWest';
7: Result := 'West';
8: Result := 'SouthWest';
end;
end;

Thanks, +rep :)
EDIT3: You must spread some Reputation around before giving it to Runaway again
Hm, I guess you were the last person I gave rep for your TPACluster.

EDIT4: Might aswell rewrite the results to an array
function NPCWalk: String;
var
i, Highest, HighestBox: Integer;
NPCDots: Array[0..8] of Integer;
X1, Y1, X2, Y2: Array of Integer;
PossibleResults: Array of String;

begin
// North, Middle, South | North/east, East, South/east | North/west, West, South/west
PossibleResults := ['North', 'Middle', 'South', 'NorthEast', 'East', 'SouthEast', 'NorthWest', 'West', 'SouthWest'];
X1 := [-25, -25, -25, 25, 25, 25, -75, -75, -75];
Y1 := [-75, -25, 25, -75, -25, 25, 25, -25, -75];
X2 := [25, 25, 25, 75, 75, 75, -25, -25, -25];
Y2 := [-25, 25, 75, -25, 25, 75, 75, 25, -25];

ColorToleranceSpeed(1);
SMART_ClearCanvas;
Highest:=0;
for i:=0 to 8 do
begin
SMART_DrawBoxEx(False, IntToBox(MMCX+X1[i], MMCY+Y1[i], MMCX+X2[i], MMCY+Y2[i]), clRed); // 0
NPCDots[i] := CountColorTolerance(645100, MMCX+X1[i], MMCY+Y1[i], MMCX+X2[i], MMCY+Y2[i], 35);
if (NPCDots[i] > Highest) then
begin
Highest := NPCDots[i];
HighestBox := i;
end;
end;
Result := PossibleResults[HighestBox];
end;

Anyways, thanks.

Runaway

2012-05-21 15:22:32

Of course :duh:


var
Highest: array[0..1] of Integer;
...

for i := 0 to 8 do
begin
if (i = 0) then
begin
Highest[0] := NPCDots[i];
Highest[1] := i;
Continue;
end;
if (NPCDots[i] > Highest[0]) then
begin
Highest[0] := NPCDots[i];
Highest[1] := i;
end;
end;

EDIT: aww you ninja'd me :p

J J

2012-05-21 15:26:05

From: Runaway
Of course :duh:


var
Highest: array[0..1] of Integer;
...

DONT LOOK
IM SILLY

EDIT: aww you ninja'd me :p

Haha sorry :P I saw your post before you edited it though and that would spare out one line to declare on top. But that might be getting a bit extreme with all the different arrays lol

EDIT: Nvm it doesn't save a line because I already have integers declared so it's a line extra anyways, either Highest: Array[0..1] of Integer or declaring another integer. It's actually a line less but still the same amount of variables.. Oh nvm you can delete Highest as an integer then so it would save 1 variable.

Runaway

2012-05-21 15:31:02

From: J J
Haha sorry :P I saw your post before you edited it though and that would spare out one line to declare on top. But that might be getting a bit extreme with all the different arrays lol


Extreme? Bah, just wait til you see my latest function…


var
ClanToMine: array[0..3] of array[0..1] of array of Integer;
DungToBank: array[0..1] of array[0..1] of array of Integer;
MineToSpot: array[0..1] of array of Integer;

^ will look like mild array usage :stirthepot:

Janilabo

2012-05-21 15:35:52

Simba has function for this - MaxA(a: TIntegerArray): Integer;

Also, there is MinA(a: TIntegerArray): Integer; for getting the smallest value.

Ahh, you are using array[0..8] of Integer. Wont work for those, sadly. :\

Kyle Undefined

2012-05-21 15:45:47

Wouldn't something like this be a lot easier?


program new;
{$i srl/srl.simba}

var
NPCDots : array[0..8] of Integer;
tmpArr : TIntegerArray;
i : Integer;

begin
setupsrl;
ClearDebug;

for i := 0 to 7 do
NPCDots[i] := Random(9); // Adds random values for now, can make it anything

for i := 0 to High(NPCDots) do
WriteLn(NPCDots[i]); // Writes the current order

SetLength(tmpArr, Length(NPCDots)); // Sets the tmpArr length
tmpArr := NPCDots; // Sets the tmpArr values to NPCDots

Quicksort(tmpArr); // Sorts the TIA, from lowest to highest
InvertTIA(tmpArr); // Inverts the TIA, so it's highest to lowest

WriteLn('=============');

for i := 0 to High(tmpArr) do
WriteLn(tmpArr[i]); // Prints out the new order

WriteLn('Highest # in array is: ' + IntToStr(tmpArr[0]));
end.

J J

2012-05-21 15:52:53

@ Kyle that doesn't count the amount of NPC's in each box though? What I wanted to do is to count the amount of NPC colors (or dots..) in 9 different boxes. When I know in what box the most NPC colors/dots are found I can let my character walk there. It's useful for many things and especially for fighting monsters or even things such as walking throught the Abyss.


Added a mousecircle click & mousecircle draw.
It clicks somewhere randomly in the circle and the circle is where the most NPC's are found.
At first I wanted to use a mousebox but north/west, north/east, south/east and south/west you will run into some problems are half of the box isn't clickable. So I calculated the middle of the points and draw a circle small enough to make it clickable from every spot.

function NPCWalk: String;
var
i, Highest, HighestBox: Integer;
NPCDots: Array[0..8] of Integer;
X1, Y1, X2, Y2: Array of Integer;
PossibleResults: Array of String;

begin
PossibleResults := ['North', 'South', 'NorthEast', 'East', 'SouthEast', 'NorthWest', 'West', 'SouthWest'];
X1 := [-25, -25, 25, 25, 25, -75, -75, -75];
Y1 := [-75, 25, -75, -25, 25, 25, -25, -75];
X2 := [25, 25, 75, 75, 75, -25, -25, -25];
Y2 := [-25, 75, -25, 25, 75, 75, 25, -25];

ColorToleranceSpeed(1);
SMART_ClearCanvas;
Highest:=0;
for i:=0 to 7 do
begin
SMART_DrawBoxEx(False, IntToBox(MMCX+X1[i], MMCY+Y1[i], MMCX+X2[i], MMCY+Y2[i]), clRed);
NPCDots[i] := CountColorTolerance(645100, MMCX+X1[i], MMCY+Y1[i], MMCX+X2[i], MMCY+Y2[i], 35);
if (NPCDots[i] > Highest) then
begin
Highest := NPCDots[i];
HighestBox := i;
end;
end;
WriteLn(''+IntToStr(HighestBox)+'');
SMART_DrawCircle(False, Point(((MMCX+X1[HighestBox]+MMCX+X2[HighestBox])/2), ((MMCY+Y1[HighestBox]+MMCY+Y2[HighestBox])/2)), 5, True, clRed);
MouseCircle(((MMCX+X1[HighestBox]+MMCX+X2[HighestBox])/2), ((MMCY+Y1[HighestBox]+MMCY+Y2[HighestBox])/2), 5, 1);
Result := PossibleResults[HighestBox];
end;

Kyle Undefined

2012-05-21 15:55:16

I know, it was an example of getting the highest number from the array. Much easier than looping through them to find the highest one.

NCDS

2012-05-21 15:59:05

You may find this link helpful.

J J

2012-05-21 16:01:11

From: Kyle Undefined
I know, it was an example of getting the highest number from the array. Much easier than looping through them to find the highest one.

Hm I see. Quicksort & InvertTIA might come in handy in the future hehe :P

Thanks all, :)

EDIT: Damn I have edited every post now?
@ NCDS, I'll check it out, looks interesting.

bg5

2012-05-21 18:25:57

Basically ,if you use static arrays you exclude use of most simba function ,which work only with dynamic arrays.

I would make in in another way :

function SplitBox (Box : Tbox ; row ,col : integer) : array of TBox;
var
a ,l,w,h :integer;

begin
l := row*col;
w := (Box.x2-Box.x1)/col;
h := (Box.y2-box.y1)/row;

SetLength(Result,l);
for a:= 0 to l-1 do
begin
Result[a].x1 := Box.x1 + w * (a mod col);
Result[a].x2 := Result[a].x1 + w;
Result[a].y1 := Box.y1 + h * (a div col);
Result[a].y2 := Result[a].y1 + h;
end;
end;

var
NPCDots :TintegerArray;
BA : array of TBox;
a ,l ,max: integer;
begin
BA := SplitBox(IntToBox(MMX1,MMY1,MMX2,MMY2),3,3);
l := length(BA);
SetLength(NPCdots,l);
for a:=0 to l-1 do
NPCDots[a] := CountColorTolerance(645100, ba[a].x1 , ba[a].y1, ba[a].x2, ba[a].y2, 35);

max := maxA(NPCdots);
for a:= 0 to l-1 do
if ( NPCdots[a] = max ) then break;

writeln('Most monsters in box number : ' + tostr (a) );
end;

You can also change resolution in SplitBox

E :I forgot about debug:
SMART_DrawBoxes(FALSE,BA ,clred);

Sin

2012-05-21 18:56:37

You working on a Abyss Crafter?

J J

2012-05-22 02:31:18

From: beginner5
Basically ,if you use static arrays you exclude use of most simba function ,which work only with dynamic arrays.

I would make in in another way :

function SplitBox (Box : Tbox ; row ,col : integer) : array of TBox;
var
a ,l,w,h :integer;

begin
l := row*col;
w := (Box.x2-Box.x1)/col;
h := (Box.y2-box.y1)/row;

SetLength(Result,l);
for a:= 0 to l-1 do
begin
Result[a].x1 := Box.x1 + w * (a mod col);
Result[a].x2 := Result[a].x1 + w;
Result[a].y1 := Box.y1 + h * (a div col);
Result[a].y2 := Result[a].y1 + h;
end;
end;

var
NPCDots :TintegerArray;
BA : array of TBox;
a ,l ,max: integer;
begin
BA := SplitBox(IntToBox(MMX1,MMY1,MMX2,MMY2),3,3);
l := length(BA);
SetLength(NPCdots,l);
for a:=0 to l-1 do
NPCDots[a] := CountColorTolerance(645100, ba[a].x1 , ba[a].y1, ba[a].x2, ba[a].y2, 35);

max := maxA(NPCdots);
for a:= 0 to l-1 do
if ( NPCdots[a] = max ) then break;

writeln('Most monsters in box number : ' + tostr (a) );
end;

You can also change resolution in SplitBox

E :I forgot about debug:
SMART_DrawBoxes(FALSE,BA ,clred);

Thanks, I'll check this out aswell :P

From: Sin
You working on a Abyss Crafter?

No, that was just an example of where it could be used. One of the first things that came to my mind. I'm thinking about making one though but I'm kind of fed up with runecrafting right now haha. Also tested it at some other locations, it's pretty useful for a fighting script. Or possibly even my great orb project script :P