Bobarkinator
2008-08-22 23:26:38
Some quick notes before we release the code
- Right now SCAR doesn't free the objects it uses which means that over time, SCAR will will be using more memory. We don't free them because most of the time it will cause SCAR to crash, if anyone can circumvent this, please say so
- This won't work for doors, doors are off in their own little world…just use tiles for doors. I'll be working on them in the mean time
- I replaced Objects.scar
- Always check to be sure the tile that FindObject returns is on the Mainscreen, and if it's not then you need to walk to it.
Even though I have kinda left n3ss3s out of the loop for the past few major improvements, he was a big part of this. BenLand100 was also a big part in helping us weed out glitches and getting the project working.
//------------------------------------------------------------------\\
//-- Scar Non-Standard Resource Library --||
//-- ยป Reflection Routines - Objects --||
//------------------------------------------------------------------//
// * Function GetObjectType(UID: Long): Integer;
// * Function GetObjectTile(UID: Long): TPoint;
// * Function GetUIDAt(TileX, TileY: Integer): UID;
// * Function FindObject(Var Tile: TPoint; objType: Integer): Boolean;
// * Procedure DumpAllObjects;
Type
Long = Record
High, Low: Integer;
End;
UID = Record
objType: Integer;
UID: Long;
End;
Const
getGroundArray = 'ck.d';
getObject1 = 's';
getObject2 = 'z';
getObject3 = 'M';
getObject4 = 'u';
getObject5Array = 'H';
getUID1 = 'j'; //j
getUID2 = 't'; //t
getUID3 = 'm'; //m
getUID4 = 'd'; //d
getUID5 = 'k'; //k
{*******************************************************************************
Function GetObjectType(UID: Long): Integer;
By: Bobarkinator
Description: Returns object type of a UID
*******************************************************************************}
Function GetObjectType(UID: Long): Integer;
Begin
Result := UID.High And $7fffffff;
End;
{*******************************************************************************
Function GetObjectTile(UID: Long): TPoint;
By: Bobarkinator
Description: Returns tile of a UID
*******************************************************************************}
Function GetObjectTile(UID: Long): TPoint;
Begin
Result.X:= (SmartGetFieldInt(0, BaseX) + (UID.Low And $7f));
Result.Y:= (SmartGetFieldInt(0, BaseY) + ((UID.Low Shr 7) And $7f));
End;
{*******************************************************************************
Function GetUIDAt(TileX, TileY: Integer): UID;
By: n3ss3s and Bobarkinator
Description: Returns the information about the object at a give tile
*******************************************************************************}
Function GetObjectAt(TileX, TileY: Integer): UID;
Var
X, Y, Z, I, obj, C, Q: Integer;
getObj, getUIDs: TStringArray;
Begin
X := TileX - SmartGetFieldInt(0, BaseX);
Y := TileY - SmartGetFieldInt(0, BaseY);
getObj := [getObject1, getObject2, getObject3, getObject4, getObject5Array];
getUIDs := [getUID1, getUID2, getUID3, getUID4, getUID5];
For Q := 0 To 3 Do
Begin
Z := SmartGetFieldArray3DObject(0, getGroundArray, Q, X, Y);
If Z = 0 Then Continue;
For I := 0 To 4 Do
If I < 4 Then
Begin
obj := SmartGetFieldObject(Z, getObj[I]);
//WriteLn('Object is '+inttostr(obj)+' on getObject1 in '+inttostr(Q)+' ground layer');
If obj <> 0 Then
Begin
Result.UID.High := SmartGetFieldLongH(obj, getUIDs[I]);
Result.UID.Low := SmartGetFieldLongL(obj, getUIDs[I]);
Result.objType := GetObjectType(Result.UID);
//SmartFreeObject(obj);
//SmartFreeObject(Z);
Exit;
End;
End Else
For C := 0 To SmartGetFieldArraySize(Z, getObject5Array) - 1 Do
Begin
obj := SmartGetFieldArrayObject(Z, getObject5Array, C);
//WriteLn('Object is ' + IntToStr(Obj) + ' on Object5Array');
If obj <> 0 Then
Begin
Result.UID.High := SmartGetFieldLongH(obj, getUID5);
Result.UID.Low := SmartGetFieldLongL(obj, getUID5);
Result.objType := GetObjectType(Result.UID);
//SmartFreeObject(obj);
//SmartFreeObject(o5a);
//SmartFreeObject(z);
Exit;
End;
End;
End;
Result.objType := 0;
End;
{*******************************************************************************
Function FindObject(Var Tile: TPoint; objType: Integer): Boolean;
By: n3ss3s and Bobarkinator
Description: Returns True if objType is found. Tile of objType is returned in
Tile. Make sure to check thatthe Tile is on the MS, if not, walk to it
*******************************************************************************}
Function FindObject(Var Tile: TPoint; objType: Integer): Boolean;
// Yakman's spiral algorithm =]
Var
Dir, tLength, f, X, Y: Integer;
TP: TPoint;
TileObject: UID;
Begin
TP := GetMyPos;
X := TP.X;
Y := TP.Y;
Dir := 0;
tLength := 1;
While (tLength < 9) Do
Begin
For F := 0 To tLength - 1 Do
Begin
Case Dir of
0: Y := Y - 1;
1: X := X + 1;
2: Y := Y + 1;
3: X := X - 1;
End;
TileObject := GetObjectAt(X, Y);
If TileObject.objType = objType Then
Begin
//Writeln('Found Type ' + IntToStr(objType) + ' at X ' + IntToStr(X) +
//' Y ' + IntToStr(Y));
Result := True;
TP := GetObjectTile(TileObject.UID);
Tile.X := TP.X
Tile.Y := TP.Y;
Exit;
End Else
//Writeln('X: '+IntToStr(X)+' Y:'+IntToStr(Y)+ ' '+IntToStr(TileObject.objType));
End;
Dir := Dir + 1;
dir := dir Mod 4;
If Dir Mod 2 = 0 Then
tLength := tLength + 1;
End;
End;
{*******************************************************************************
Procedure DumpAllObjects;
By: Bobarkinator
Description: Prints out all the objects on the current map(104 x 104)
*******************************************************************************}
Procedure DumpAllObjects;
Var
BaseTileX, BaseTileY, X, Y: Integer;
O: UID;
Begin
BaseTileX := SmartGetFieldInt(0, BaseX);
BaseTileY := SmartGetFieldInt(0, BaseY);
For X := BaseTileX To 104 + BaseTileX Do
For Y := BaseTileY To 104 + BaseTileY Do
Begin
O := GetObjectAt(X, Y);
WriteLn('X ' + IntToStr(X) + ' Y ' + IntToStr(Y) + ' Type ' + IntToStr(O.objType));
End;
End;Enjoy
- n3ss3s and Bobarkinator