SRL Archive - OSR Help

Arrays..


DeSnob

2011-06-18 12:04:09

Hey, I'm fixing up my yew script to make it faster so I need to convert a few things to arrays. This really slows the script down:
  if (GetObjectIdAt(Tile(2941 ,3233)) = 38755) then
TreeTile := Tile(2941 ,3233);
if (GetObjectIdAt(Tile(2934 ,3234)) = 38755) then
TreeTile := Tile(2934 ,3234);
if (GetObjectIdAt(Tile(2935 ,3226)) = 38755) then
TreeTile := Tile(2935 ,3226);
if (GetObjectIdAt(Tile(2928 ,3231)) = 38755) then
TreeTile := Tile(2928 ,3231);

How do I convert that into a working array? I'm not familiar with it because it deals with tiles.
~Thanks,
DeSnob.

Coh3n

2011-06-18 12:14:49


var
i: integer;
tiles: TTileArray;
begin
tiles := [Tile(2941 ,3233), Tile(2934 ,3234), Tile(2928 ,3231), Tile(2928 ,3231)];
for i := 0 to high(tiles) do
if (getObjectIDAt(tiles[i]) = 38755)) then
begin
treeTile := tiles[i];
break;
end;
end;
That what you mean?

That's not going to speed up your script. I'm pretty sure it's the object functions that are slow. I remember reading in the Reflection 2 thread that they have been significantly sped up though.

DeSnob

2011-06-18 12:23:31

From: Coh3n

var
i: integer;
tiles: TTileArray;
begin
tiles := [Tile(2941 ,3233), Tile(2934 ,3234), Tile(2928 ,3231), Tile(2928 ,3231)];
for i := 0 to high(tiles) do
if (getObjectIDAt(tiles[i]) = 38755)) then
begin
treeTile := tiles[i];
break;
end;
end;
That what you mean?

That's not going to speed up your script. I'm pretty sure it's the object functions that are slow. I remember reading in the Reflection 2 thread that they have been significantly sped up though.

Here's the function I made:
Function GetObjectIdAt(ObjTile: TTile): Integer;
var
TheObject: TRSObject;
begin
TheObject := GetObjectAt(ObjTile, 0);
Result := TheObject.ID;
end;
I has a noticeable delay (a second or two, which is horrible when competing against other bots).

Coh3n

2011-06-18 12:26:42

Yeah it's the Reflection object routines that are slow. I'm not sure what else you could do as I'm not really an expert on reflection. Here's some info. about the speed of object finding though.

Floor66

2011-06-18 12:43:58

Kinda offtopic but DeSnob, weren't you SRL members once? :P I r confused

DeSnob

2011-06-18 12:49:44

From: Floor66
Kinda offtopic but DeSnob, weren't you SRL members once? :P I r confused


I was never an SRL Member. I can make a script and apply with it, but I don't have much time to do so.

Floor66

2011-06-18 12:51:16

oh my bad ;)

masterBB

2011-06-19 07:21:21

This one should be a bit faster:
function R_GetTrees(treeTiles:TTileArray;treeIDs:IntegerArray): TTile;
var
Plane, GroundObj, BaseX, BaseY: Integer;
Temp: TRSObject;
begin
BaseX := SmartGetFieldInt(0, hook_static_BaseX);
BaseY := SmartGetFieldInt(0, hook_static_BaseY);
Plane := SmartGetFieldInt(0, hook_static_LoadedPlane);

for i:= 0 to Length(treeTiles) do
begin
GroundObj := SmartGetFieldArray3DObject(0, hook_static_GroundTiles, Plane, treeTiles[i].y - BaseX, treeTiles[i].y - BaseX);
Temp := NULL_RSOBJECT;

Temp := R_GetInteractableObject(GroundObj, BaseX, BaseY);

if (not(Temp = NULL_RSOBJECT) and Temp.ID = treeIDs[i])then
begin
SmartFreeObject(GroundObj);
Result := Temp.Tile;
Exit;
end;
SmartFreeObject(GroundObj)
end;
end;

Edit: Almost forgot, its based on drags work, so credits.

Edit: edit:
And use reflection 2_1 or just include this function:
(*
R_GetInteractableObject
~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: pascal

function R_GetInteractableObject(GroundObj, BaseX, BaseY: Integer): TRSObject;

Loads the information from the inputed GroundObj and returns a TRSObject. For
use in object searching functions in this include, not in scripts.

.. note::

by Drags111

*)
function R_GetInteractableObject(GroundObj, BaseX, BaseY: Integer): TRSObject;
var
Node, Last, Obj: Integer;
ID, SecondLevel: Integer;
begin
Node := SmartGetFieldObject(GroundObj, hook_ground_AnimableList);
while(Node <> 0)do
begin
Obj := SmartGetFieldObject(Node, hook_AnimableNode_GetAnimable);
if(Obj <> 0)then
Break;
Last := Node;
Node := SmartGetFieldObject(Last, hook_AnimableNode_GetNext);
SmartFreeObject(Last);
end;
SmartFreeObject(Node);

if(Obj <= 0)then
Exit;

ID := SmartGetFieldShort(Obj, hook_InteractiveObject_GetID) and $FFFF;
if(ID = -1) or (ID = 65535)then //65535 is an unsigned -1...
begin
SecondLevel := SmartGetFieldObject(Obj, hook_interactiveobject2_GetObjectData);
ID := SmartGetFieldInt(SecondLevel, hook_secondlevelobject_GetID) and $FFFF;
if(ID = -1) or (ID = 65535)then
begin
SmartFreeObject(SecondLevel);
SmartFreeObject(Obj);
Exit;
end;
end;

if(ID <> -1) and (ID <> 65535)then
begin
Result.ID := ID;
Result.ObjType := OBJ_INTERACTABLE;

Result.Tile.X := BaseX + (SmartGetFieldInt(Obj, hook_animable_PixelX) / 512);
Result.Tile.Y := BaseY + (SmartGetFieldInt(Obj, hook_animable_PixelY) / 512);

Result.TileArea.X1 := (BaseX + SmartGetFieldShort(Obj, hook_animableentity_getAreaX1));
Result.TileArea.Y1 := (BaseY + SmartGetFieldShort(Obj, hook_animableentity_getAreaY1));
Result.TileArea.X2 := (BaseX + SmartGetFieldShort(Obj, hook_animableentity_getAreaX2));
Result.TileArea.Y2 := (BaseY + SmartGetFieldShort(Obj, hook_animableentity_getAreaY2));
end;

SmartFreeObject(SecondLevel);
SmartFreeObject(Obj);
end;

DeSnob

2011-06-19 09:15:14

Ah, thanks. I'll just include the function. I'll see how it works out.
E: I'd rather just use the complete ref 2.1. Where can I get it, and what kind of "set up" for the includes at the top of the script must be used?

masterBB

2011-06-19 09:18:50

It was typed in browser, so probably still contains some errors.

Edit:
This one makes more sense:
function R_GetInteractableObject(GroundObj, BaseX, BaseY: Integer): TRSObject;
var
Node, Last, Obj: Integer;
ID, SecondLevel: Integer;
begin
Node := SmartGetFieldObject(GroundObj, hook_ground_AnimableList);
while(Node <> 0)do
begin
Obj := SmartGetFieldObject(Node, hook_AnimableNode_GetAnimable);
if(Obj <> 0)then
Break;
Last := Node;
Node := SmartGetFieldObject(Last, hook_AnimableNode_GetNext);
SmartFreeObject(Last);
end;
SmartFreeObject(Node);

if(Obj <= 0)then
Exit;

ID := SmartGetFieldShort(Obj, hook_InteractiveObject_GetID) and $FFFF;
if(ID = -1) or (ID = 65535)then //65535 is an unsigned -1...
begin
SecondLevel := SmartGetFieldObject(Obj, hook_interactiveobject2_GetObjectData);
ID := SmartGetFieldInt(SecondLevel, hook_secondlevelobject_GetID) and $FFFF;
if(ID = -1) or (ID = 65535)then
begin
SmartFreeObject(SecondLevel);
SmartFreeObject(Obj);
Exit;
end;
end;

if(ID <> -1) and (ID <> 65535)then
begin
Result.ID := ID;
Result.ObjType := 1;

Result.Tile.X := BaseX + (SmartGetFieldInt(Obj, hook_animable_PixelX) / 512);
Result.Tile.Y := BaseY + (SmartGetFieldInt(Obj, hook_animable_PixelY) / 512);

end;

SmartFreeObject(SecondLevel);
SmartFreeObject(Obj);
end;


function R_GetTrees(treeTiles:TTileArray;treeIDs:TIntegerArray): TTile;
var
Plane, GroundObj, i, BaseX, BaseY: Integer;
Temp: TRSObject;
begin
BaseX := SmartGetFieldInt(0, hook_static_BaseX);
BaseY := SmartGetFieldInt(0, hook_static_BaseY);
Plane := SmartGetFieldInt(0, hook_static_LoadedPlane);

for i:= 0 to Length(treeTiles) do
begin
GroundObj := SmartGetFieldArray3DObject(0, hook_static_GroundTiles, Plane, treeTiles[i].y - BaseX, treeTiles[i].y - BaseX);
Temp := NULL_RSOBJECT;

Temp := R_GetInteractableObject(GroundObj, BaseX, BaseY);

if (not(Temp = NULL_RSOBJECT) and (Temp.ID = treeIDs[i]))then
begin
SmartFreeObject(GroundObj);
Result := Temp.Tile;
Exit;
end;
SmartFreeObject(GroundObj);
end;
end;

Edit: Edit: Last edit, I fixed most errors. Could you try this?

DeSnob

2011-06-19 10:10:03

Ah, there's something out of range. Can't tell what's wrong since I don't understand most of the SMART field grabbing procedures.
Error: Out Of Range at line 249
The following DTMs were not freed: [SRL - Lamp bitmap, 1]
The following bitmaps were not freed: [SRL - Mod bitmap, SRL - Admin bitmap]

Here's the issue:
    GroundObj := SmartGetFieldArray3DObject(0, hook_static_GroundTiles, Plane, treeTiles[i].y - BaseX, treeTiles[i].y - BaseX);    
Thanks.

masterBB

2011-06-19 10:13:28

Found it:
[SIMBA]function R_GetInteractableObject(GroundObj, BaseX, BaseY: Integer): TRSObject;
var
Node, Last, Obj: Integer;
ID, SecondLevel: Integer;
begin
Node := SmartGetFieldObject(GroundObj, hook_ground_AnimableList);
while(Node <> 0)do
begin
Obj := SmartGetFieldObject(Node, hook_AnimableNode_GetAnimable);
if(Obj <> 0)then
Break;
Last := Node;
Node := SmartGetFieldObject(Last, hook_AnimableNode_GetNext);
SmartFreeObject(Last);
end;
SmartFreeObject(Node);

if(Obj <= 0)then
Exit;

ID := SmartGetFieldShort(Obj, hook_InteractiveObject_GetID) and $FFFF;
if(ID = -1) or (ID = 65535)then //65535 is an unsigned -1...
begin
SecondLevel := SmartGetFieldObject(Obj, hook_interactiveobject2_GetObjectData);
ID := SmartGetFieldInt(SecondLevel, hook_secondlevelobject_GetID) and $FFFF;
if(ID = -1) or (ID = 65535)then
begin
SmartFreeObject(SecondLevel);
SmartFreeObject(Obj);
Exit;
end;
end;

if(ID <> -1) and (ID <> 65535)then
begin
Result.ID := ID;
Result.ObjType := 1;

Result.Tile.X := BaseX + (SmartGetFieldInt(Obj, hook_animable_PixelX) / 512);
Result.Tile.Y := BaseY + (SmartGetFieldInt(Obj, hook_animable_PixelY) / 512);

end;

SmartFreeObject(SecondLevel);
SmartFreeObject(Obj);
end;


function R_GetTrees(treeTiles:TTileArray;treeIDs:TIntegerArray): TTile;
var
Plane, GroundObj, i, BaseX, BaseY: Integer;
Temp: TRSObject;
begin
BaseX := SmartGetFieldInt(0, hook_static_BaseX);
BaseY := SmartGetFieldInt(0, hook_static_BaseY);
Plane := SmartGetFieldInt(0, hook_static_LoadedPlane);

for i:= 0 to High(treeTiles) do
begin
GroundObj := SmartGetFieldArray3DObject(0, hook_static_GroundTiles, Plane, treeTiles[i].x - BaseX, treeTiles[i].y - BaseY);
Temp := NULL_RSOBJECT;

Temp := R_GetInteractableObject(GroundObj, BaseX, BaseY);

if (not(Temp = NULL_RSOBJECT) and (Temp.ID = treeIDs[i]))then
begin
SmartFreeObject(GroundObj);
Result := Temp.Tile;
Exit;
end;
SmartFreeObject(GroundObj);
end;
end;

Sorry I can't test it right now…

DeSnob

2011-06-19 10:18:47

It worked! TYYY. It's a lot faster too. That small delay is gone and now it can compete with the other bots for logs. :D REP++. Now.. just gotta find the issue where it doesn't go to the fourth tree.

E: Found the problem.. wrong tiles D: lol

masterBB

2011-06-19 10:30:32

From: masterBB
Found it:
[SIMBA]function R_GetTreeObjectID(GroundObj: Integer): Integer;
var
Node, Last, Obj: Integer;
ID, SecondLevel: Integer;
begin
Node := SmartGetFieldObject(GroundObj, hook_ground_AnimableList);
while(Node <> 0)do
begin
Obj := SmartGetFieldObject(Node, hook_AnimableNode_GetAnimable);
if(Obj <> 0)then
Break;
Last := Node;
Node := SmartGetFieldObject(Last, hook_AnimableNode_GetNext);
SmartFreeObject(Last);
end;
SmartFreeObject(Node);

if(Obj <= 0)then
Exit;

ID := SmartGetFieldShort(Obj, hook_InteractiveObject_GetID) and $FFFF;
if(ID = -1) or (ID = 65535)then //65535 is an unsigned -1...
begin
SecondLevel := SmartGetFieldObject(Obj, hook_interactiveobject2_GetObjectData);
ID := SmartGetFieldInt(SecondLevel, hook_secondlevelobject_GetID) and $FFFF;
if(ID = -1) or (ID = 65535)then
begin
SmartFreeObject(SecondLevel);
SmartFreeObject(Obj);
Exit;
end;
end;

if(ID <> -1) and (ID <> 65535)then
begin
Result := ID;
end;

SmartFreeObject(SecondLevel);
SmartFreeObject(Obj);
end;


function R_GetTrees(treeTiles:TTileArray;treeIDs:TIntegerArray): TTile;
var
Plane, GroundObj, i, BaseX, BaseY,TreeID: Integer;
Temp: TRSObject;
begin
BaseX := SmartGetFieldInt(0, hook_static_BaseX);
BaseY := SmartGetFieldInt(0, hook_static_BaseY);
Plane := SmartGetFieldInt(0, hook_static_LoadedPlane);

for i:= 0 to High(treeTiles) do
begin
GroundObj := SmartGetFieldArray3DObject(0, hook_static_GroundTiles, Plane, treeTiles[i].x - BaseX, treeTiles[i].y - BaseY);

TreeID:= R_GetTreeObjectID(GroundObj);

if (TreeID = treeIDs[i])then
begin
SmartFreeObject(GroundObj);
Result := treeTiles[i];
Exit;
end;
SmartFreeObject(GroundObj);
end;
end;

Sorry I can't test it right now…


Maybe this is faster, and again, it's created in browser so.. :P

^Don't forget to rep drags, for his work on reflection2_1. I just customized it.

DeSnob

2011-06-19 11:02:54

From: masterBB
Maybe this is faster, and again, it's created in browser so.. :P

^Don't forget to rep drags, for his work on reflection2_1. I just customized it.

This is just a private script, but I'm grateful that he and the others are working on Ref2. Can't wait for its release.