SRL Archive - General

Reorganizing Objects.simba


Coh3n

2011-05-30 19:28:38

So, I'm kind of getting annoyed with our object finding. Not that it's not good, but it's a pain/inconvenient to keep things properly updated. I really don't want to switch to reflection because MSI is one of SRL's only scripts that can still run on 100% color, and that would be a nice thing to keep.

I would like to learn more about how colors work and such, so I think reorganizing Objects.simba will be a neat project. A lot will probably be, I don't want to say rewritten, but at least significantly changed. Considering MSI_FindObjectsIn is 171 lines along, I think it's time to figure our shit out. :p Our ultimate goal is to have our object finding equivalent to reflection, and right now, our accuracy is shit (only in some instances, i.e. trees, fishing spots).

Some things I'm planning:
  1. Spit all the methods into smaller more manageable methods.
  2. Clean up TMSObject – get rid of unused attributes/add useful ones.
  3. Implement IPwnz's "hover next object" code.
  4. Remove the MSI_Objects global array, keeping everything local (see Animation.simba for a good example),
Please feel free to add to the list. I'd also like you to post any color tutorials you know of that may be useful.

Cheers,
Coh3n

Nava2

2011-05-30 23:30:52

Simba does not have CTS 3?

Coh3n

2011-05-30 23:31:49

God damnit. Well there goes that idea. :(

Nava2

2011-05-31 00:07:08

Freddy would not release any of the formulas… silly thing to keep hidden IMO.

Coh3n

2011-05-31 00:12:12

From: Nava2
Freddy would not release any of the formulas… silly thing to keep hidden IMO.
Of all the bright minds here at SRL, we can't make a CTS 3 formula? :rolleyes: Was it really that much more accurate?

Wizzup?

2011-05-31 08:06:43

From: Coh3n
Of all the bright minds here at SRL, we can't make a CTS 3 formula? :rolleyes: Was it really that much more accurate?


I might come up with a few formula's in a few weeks for a small paper I have to do. Can't promise anything.

NCDS

2011-05-31 09:23:59

I personally haven't really experienced the inaccuracy your talking about but, I'll support and help with this if you want to do it. I'm certainly not opposed to "upgrading" anything in MSI.

Gu3st...

2011-05-31 11:02:07

From: NCDS
I personally haven't really experienced the inaccuracy your talking about but, I'll support and help with this if you want to do it. I'm certainly not opposed to "upgrading" anything in MSI.
Same goes for me.

Coh3n

2011-05-31 16:10:05

From: Wizzup?
I might come up with a few formula's in a few weeks for a small paper I have to do. Can't promise anything.
Right on, it was a rhetorical question though. :p

From: NCDS
I personally haven't really experienced the inaccuracy your talking about but, I'll support and help with this if you want to do it. I'm certainly not opposed to "upgrading" anything in MSI.
Trees for one example. It would be nice to tell the difference rather than hovering all trees on the main screen; although, I'm still not sure what to do about that. Silver and tin rocks often get mixed up, some mithril rocks aren't found for whatever reason.

Not to mention it's terribly unorganized and the plan is to slowly get rid of the global arrays so that should help organization. From the way MSI is going, it will actually be able to be used as an include as well as a script.

Coh3n

2011-06-01 03:14:58

Here's the new FindObjectsTPA (renamed to FindObjectTPA).
  • Takes a TMSObject as a parameter rather than integer/searchBox.
  • More organized and it removes values from the original ATPA rather than exiting the function.
  • Accuracy was also being checked twice (the first time didn't work properly, and the second time was in the wrong spot).
  • BadColors and Anim have been removed as they're never used and probably never will be.
  • SMART debugging is better. It will paint all the dots of the original ATPA (after checking accuracy/findAllColors), then paint boxes around all the valid points (at the end of the function).
  • Added a lot of comments.
It's untested of course since it will cause a bunch of compiling errors, so if you look through it let me know if you see something out of the ordinary.

(*
MSI_FindObjectTPA
~~~~~~~~~~~~~~~~~

.. code-block:: pascal

function MSI_FindObjectTPA(Obj: TMSObject; var Points: TPointArray): Boolean;

Returns a TPointArray ('Points') of all the instances of the object 'Obj' that
are found in the object's ('Obj') SearchBox atribute.

.. note::

| Author: marpis
| Last Updated: 01 June 2011 by Coh3n

Example:

.. code-block:: pascal

ms_Points := function MSI_FindObjectTPA(TREE_NORMAL, tmpTPA);
*)
function MSI_FindObjectTPA(Obj: TMSObject; var Points: TPointArray): Boolean;
var
TPA: TPointArray;
ATPA: T2DPointArray;
tpaBoxes: TBoxArray;
i, j, cts: integer;
badPoint: boolean;
begin
if (not loggedIn) then
exit;

cts := GetToleranceSpeed;
setColorToleranceSpeed(2);
setToleranceSpeed2Modifiers(obj.hue, obj.sat);

// search for all the object's colors
setLength(ATPA, length(obj.colors));
for i := 0 to high(ATPA) do
begin
findColorsSpiralTolerance(MSCX, MSCY, ATPA[i], obj.colors[i],
obj.searchArea.x1, obj.searchArea.y1,
obj.searchArea.x2, obj.searchArea.y2, obj.tol);
end;

setColorToleranceSpeed(cts);
setToleranceSpeed2Modifiers(0.2, 0.2);

// check if all the colors were found
if (obj.findAllColors) then
for i := 0 to high(ATPA) do
if (length(ATPA[i]) < 1) then
begin
MSI_SubDebug('FindObjectTPA: not all colors were found');
deleteValueInATPA(ATPA, i);
end;

// split points in to boxes the size of object & check if there's enough points
TPA := MergeATPA(ATPA);
ATPA := TPAtoATPAEx(TPA, obj.w, obj.h);
for i := 0 to high(ATPA) do
if (length(ATPA[i]) < obj.accuracy) then
begin
MSI_SubDebug('FindObjectTPA: Not enough points: ' + intToStr(length(ATPA[i])));
deleteValueInATPA(ATPA, i);
end;

TPA := [];
sortATPAFrom(ATPA, point(MSCX, MSCY));
setLength(TPA, length(ATPA));

// draw points on SMART if setting enabled
if (MSI_SetupVars[SETUP_DEBUG_SMART]) then
drawDotsMulti(true, ATPA);

// get the middle point of each TPA in the ATPA + create a box for each point
setLength(tpaBoxes, length(TPA));
for i := 0 to high(ATPA) do
begin
tpaBoxes[i] := getTPABounds(ATPA[i]);
TPA[i] := middleTPA(ATPA[i]);
end;

// loop through all the points to see which are valid/invalid
for i := 0 to high(TPA) do
begin
badPoint := false;

// checks to see if the point is in the object's search area
if (not badPoint) then
if (not pointInBox(TPA[i], obj.searchArea)) then
begin
MSI_SubDebug('FindObjectTPA: Invalid point - exceeds search area');
badPoint := true;
end;

// check to see if point is in a bad box
if (not badPoint) then
if (length(MSI_NotBoxes) > 0) then
for j := 0 to high(MSI_NotBoxes) do
if (pointInBox(TPA[i], MSI_NotBoxes[j])) then
begin
MSI_SubDebug('FindObjectTPA: Point in bad box');
badPoint := true;
end;

// checks to see if the point is on the player
if (not badPoint) then
if (obj.excludeSelf) then
if (pointInBox(TPA[i], intToBox(PLBX1, PLBY1, PLBX2, PLBY2))) then
begin
MSI_SubDebug('FindObjectTPA: Point is on player');
badPoint := true;
end;

// a valid point as been found
if (not badPoint) then
begin
setLength(Points, length(Points) + 1);
Points[high(Points)] := TPA[i];

// add point's box to MSI_NotBoxes so it can be avoided next search
end else begin
setLength(MSI_NotBoxes, length(MSI_NotBoxes) + 1);
MSI_NotBoxes[high(MSI_NotBoxes)] := tpaBoxes[i];
end;
end;

if (length(points) > 0) then
begin
result := true;

for i := 0 to high(Points) do
if (MSI_SetupVars[SETUP_DEBUG_SMART]) then
SMART_DrawBoxMS((i = 0), MSI_MakeBox(point(Points[i].x, Points[i].y), obj.w, obj.h), clGreen);

MSI_SubDebug('FindObjectTPA: Points found: ' + intToStr(length(points)));
end;
end;

E: Updated and I started testing. This is what I get:


Anyone have an idea of how to "merge" the points that are on the same tree? By the way, the boxes are created around each point that's found.

E2: Updated script, and with some more tweaking I got this:


Much better I'd say. Oh and by the way, the first image is what happened with our current object finding.

Gu3st...

2011-06-01 06:01:19

Increase obj.w and obj.h

Coh3n

2011-06-01 06:05:02

From: Zyt3x
Increase obj.w and obj.h
Just updated the above post, and here's the updated FindObjectTPA:

procedure MSI_FilterTPADist(var TPA: TPointArray; maxDist: extended);
var
i, j: integer;
begin
for i := 0 to high(TPA) do
for j := i to high(TPA) do
if (distance(TPA[i].x, TPA[i].y, TPA[j].x, TPA[j].y) < maxDist) then
deleteValueInTPA(TPA, j);
end;

function MSI_FindObjectTPA(Obj: TMSObject; var Points: TPointArray): Boolean;
var
TPA: TPointArray;
ATPA: T2DPointArray;
tpaBoxes: TBoxArray;
i, j, cts: integer;
badPoint: boolean;
begin
if (not loggedIn) then
exit;

cts := GetToleranceSpeed;
setColorToleranceSpeed(2);
setToleranceSpeed2Modifiers(obj.hue, obj.sat);

// search for all the object's colors
setLength(ATPA, length(obj.colors));
for i := 0 to high(ATPA) do
begin
findColorsSpiralTolerance(MSCX, MSCY, ATPA[i], obj.colors[i],
obj.searchArea.x1, obj.searchArea.y1,
obj.searchArea.x2, obj.searchArea.y2, obj.tol);
end;

setColorToleranceSpeed(cts);
setToleranceSpeed2Modifiers(0.2, 0.2);

// check if all the colors were found
if (obj.findAllColors) then
for i := 0 to high(ATPA) do
if (length(ATPA[i]) < 1) then
begin
MSI_SubDebug('FindObjectTPA: not all colors were found');
deleteValueInATPA(ATPA, i);
end;

// split points in to boxes the size of object & check if there's enough points
TPA := MergeATPA(ATPA);
ATPA := TPAtoATPAEx(TPA, obj.w, obj.h);
for i := 0 to high(ATPA) do
if (length(ATPA[i]) < obj.accuracy) then
begin
MSI_SubDebug('FindObjectTPA: Not enough points: ' + intToStr(length(ATPA[i])));
deleteValueInATPA(ATPA, i);
end;

TPA := [];
sortATPAFrom(ATPA, point(MSCX, MSCY));
setLength(TPA, length(ATPA));

// draw points on SMART if setting enabled
if (MSI_SetupVars[SETUP_DEBUG_SMART]) then
drawDotsMulti(true, ATPA);

// get the middle point of each TPA in the ATPA + create a box for each point
setLength(tpaBoxes, length(TPA));
for i := 0 to high(ATPA) do
begin
tpaBoxes[i] := getTPABounds(ATPA[i]);
TPA[i] := middleTPA(ATPA[i]);
end;

// loop through all the points to see which are valid/invalid
for i := 0 to high(TPA) do
begin
badPoint := false;

// checks to see if the point is in the object's search area
if (not badPoint) then
if (not pointInBox(TPA[i], obj.searchArea)) then
begin
MSI_SubDebug('FindObjectTPA: Invalid point - exceeds search area');
badPoint := true;
end;

// check to see if point is in a bad box
if (not badPoint) then
if (length(MSI_NotBoxes) > 0) then
for j := 0 to high(MSI_NotBoxes) do
if (pointInBox(TPA[i], MSI_NotBoxes[j])) then
begin
MSI_SubDebug('FindObjectTPA: Point in bad box');
badPoint := true;
end;

// checks to see if the point is on the player
if (not badPoint) then
if (obj.excludeSelf) then
if (pointInBox(TPA[i], intToBox(PLBX1, PLBY1, PLBX2, PLBY2))) then
begin
MSI_SubDebug('FindObjectTPA: Point is on player');
badPoint := true;
end;

// a valid point as been found
if (not badPoint) then
begin
setLength(Points, length(Points) + 1);
Points[high(Points)] := TPA[i];

// add point's box to MSI_NotBoxes so it can be avoided next search
end else begin
setLength(MSI_NotBoxes, length(MSI_NotBoxes) + 1);
MSI_NotBoxes[high(MSI_NotBoxes)] := tpaBoxes[i];
end;
end;

MSI_FilterTPADist(points, min(obj.w, obj.h) * 2);

if (length(points) > 0) then
begin
result := true;

for i := 0 to high(Points) do
if (MSI_SetupVars[SETUP_DEBUG_SMART]) then
SMART_DrawBoxMS((i = 0), MSI_MakeBox(point(Points[i].x, Points[i].y), obj.w, obj.h), clGreen);

MSI_SubDebug('FindObjectTPA: Points found: ' + intToStr(length(points)));
end;
end;

lordsaturn

2011-06-01 09:25:30

How long does it take to complete that search in that specific spot, Coh3n?

Coh3n

2011-06-01 16:20:20

From: lordsaturn
How long does it take to complete that search in that specific spot, Coh3n?
FindObjectTPA: Took 577 ms to complete
That's probably too long, isn't it?

E: The current FindObjectsTPA:
FindObjectsTPA: Took 406 ms to complete
So then new one is a little slower, but it's much much more accurate. Also, I ran the new one a few times in a few different spots:
FindObjectTPA: Took 468 ms to complete
FindObjectTPA: Took 358 ms to complete
FindObjectTPA: Took 406 ms to complete
I would say, on average, they're pretty much the same.

Coh3n

2011-06-15 03:53:48

This has now been done. :)

http://villavu.com/forum/showthread.php?p=812251#post812251