SRL Archive - Reflection Development

Restructured Players.simba


mormonman

2011-01-09 14:34:24

I added lots of GetPlayer functions including by level. I also changed the names around so it made more sense. I also added the Fighting boolean to the TPlayer type and yes it does work, I tested it. Also made AreWeInteracting. Finally, I made a DumpPlayers procedure for debugging.
Good stuff.

//------------------------------------------------------------------\\
//-- Simba Reflection Library --||
//-- Player Routines --||
//------------------------------------------------------------------//

//--- Records --//
type
{Holds information related to a player}
TPlayer = record
Index, Level, Animation, Height, HPRatio, Interacting,
Orientation: integer;// SOUTH = 0; NORTH = 180; CLOCKWISE.
Tile: TTile;
Name: String;
Moving, Fighting: boolean;
end;

{Array of TPlayer}
TPlayerArray = array of TPlayer;

{Holds information related to your character}
TMe = record
Tile: TTile;
MS: TPoint;
Animation, Speed, LoginIndex, Interacting: Integer;
HPRatio, Height, Level, Combat, TurnDirection: Integer;
Name: string;
InFight: boolean;
Experience, Levels: array[0..24] of Integer;
Equipment: TIntegerArray;
Orientation: Integer; // SOUTH = 0; NORTH = 180; CLOCKWISE.
end;

{*******************************************************************************
function NULL_PLAYER: TPlayer;
Description: Nulls a TPlayer
*******************************************************************************}
function NULL_PLAYER: TPlayer;
begin
Result.Index := NULL_INT;
Result.Name := NULL_STRING;
Result.Tile := NULL_TILE;
Result.Moving := False;
Result.Fighting := False;
Result.Animation := NULL_INT;
Result.Height := NULL_INT;
Result.HPRatio := NULL_INT;
Result.Orientation := NULL_INT;
Result.Level := NULL_INT;
Result.Interacting := NULL_INT;
end;

{*******************************************************************
Function LoginIndex: Integer;
By:
Description: Uses the LoginIndex hook.
2 = Logged Out, 4 = Logging Into Lobby, 6 = Lobbby,
8 = logging into game, 9 = Logged In. Reaches 10 at
some points, but with no significance.
********************************************************************}
function LoginIndex : Integer;
begin
Result := SmartGetFieldInt(0, hook_static_LoginIndex);
end;

{*******************************************************************************
Function GetAnimation: Integer;
By: lordsaturn
Description: Gets character animation
*******************************************************************************}
function GetAnimation: Integer;
var
MyPlayer : Integer;
begin
MyPlayer := SmartGetFieldObject(0, hook_static_MyPlayer);
Result := SmartGetFieldInt(MyPlayer, hook_character_Animation);
SmartFreeObject(MyPlayer);
end;

{*******************************************************************************
Function GetSpeed : integer;
By: Drags111
Description: Returns the character speed. 0 = not moving. > 0 = moving.
*******************************************************************************}
function GetSpeed: Integer;
var
MyPlayer: Integer;
begin
MyPlayer := SmartGetFieldObject(0, hook_static_MyPlayer);
result:= SmartGetFieldInt(MyPlayer, hook_character_Motion);
SmartFreeObject(MyPlayer);
end;

{*******************************************************************************
function IsIdle: Boolean;
By: lordsaturn
Description: Returns true if the your player is idle. (no movement or animation)
*******************************************************************************}
function IsIdle: Boolean;
begin
Result := (GetAnimation = -1) and (GetSpeed = 0);
end;

{*******************************************************************************
function CharacterMoving: Boolean;
By: lordsaturn
Description: Returns true if the your player is moving
*******************************************************************************}
function CharacterMoving: Boolean;
begin
Result := (GetSpeed > 0);
end;

{*******************************************************************************
function CharacterAnimating: Boolean;
By: lordsaturn
Description: Returns true if the your player is moving
*******************************************************************************}
function CharacterAnimating: Boolean;
begin
Result := (GetAnimation > -1);
end;

{*******************************************************************************
function WaitForAnimation(timeout: integer): boolean;
By: Drags111
Description: Waits until your character animates, or the timeout is reached.
*******************************************************************************}
function WaitForAnimation(timeout: integer): boolean;
var
t: integer;
begin
Result := false;
MarkTime(t);
while GetAnimation = -1 do
begin
if(TimeFromMark(t) >= timeout)then
Exit;
wait(50);
end;
Result := true;
end;

{*******************************************************************************
function GetAllPlayers: array of TPlayer;
By: Widget edited by lordsaturn and drags111
Description: Returns all the players on the minimap
*******************************************************************************}
function GetAllPlayers: TPlayerArray;
var
i, IndexPos, Count, Player, theName, L: integer;
begin
Count := SmartGetFieldInt(0, hook_static_PlayerCount);
SetArrayLength(result, Count);
if Count < 1 then Exit;
for i := 0 to Count-1 do
try
IndexPos := SmartGetFieldArrayInt(0, hook_static_PlayerIndexArray, i);
Player := SmartGetFieldArrayObject(0, hook_static_GetPlayers, IndexPos);
theName := SmartGetFieldObject(Player, hook_player_GetName);
Result[L].Index := IndexPos;
Result[L].Name := GetJavaString(theName, 255);
Result[L].Tile.X := (SmartGetFieldInt(0, hook_static_BaseX) + (SmartGetFieldInt(Player, hook_animable_PixelX) / 512));
Result[L].Tile.Y := (SmartGetFieldInt(0, hook_static_BaseY) + (SmartGetFieldInt(Player, hook_animable_PixelY) / 512));
Result[L].Height := iabs(SmartGetFieldInt(Player, hook_character_GetHeight));
Result[L].Animation := SmartGetFieldInt(Player, hook_character_Animation);
Result[L].Fighting := (SmartGetFieldInt(Player, hook_character_LoopCycleStatus) >
SmartGetFieldInt(0, hook_static_LoopCycle));
Result[L].Moving := (SmartGetFieldInt(Player, hook_character_Motion)>0);
Result[L].HPRatio := SmartGetFieldInt(Player, hook_character_HPRatio) * 100 / $FF;
Result[L].Level := SmartGetFieldInt(Player, hook_player_GetLevel);
Result[L].Orientation := Round(FixD(180 + (SmartGetFieldInt(Player, hook_character_Orientation)* 45) / 2048));
Result[L].Interacting := SmartGetFieldInt(Player, hook_character_GetInteracting);
Inc(L);
finally
SmartFreeObject(Player);
SmartFreeObject(theName);
end;
SetArrayLength(Result, L);
end;

{*******************************************************************************
function SortPlayers(Players: TPlayerArray): TPlayerArray;
By: lordsaturn
Description: Sorts players by distance from you.
*******************************************************************************}
function SortPlayers(Players: TPlayerArray): TPlayerArray;
var
i,i2, H: integer;
tpa: TPointArray;
begin
H := High(Players);
if H < 0 then Exit;
SetArrayLength(TPA, H+1);
SetArrayLength(Result, H+1);
for i := 0 to H do
TPA[i] := Players[i].Tile;
SortTPAFrom(TPA, GetMyPos);
for i := 0 to H do
for i2 := 0 to H do
if (TPA[i].x=Players[i2].Tile.x) and (TPA[i].y=Players[i2].Tile.y) then
Result[i] := Players[i2];
end;

{*******************************************************************************
function GetPlayersMulti(TheVarArr: TVariantArray): TPlayerArray;
By: Drags111/mormonman
Description: Returns all the players by the variants. (Names or Indexes)
*******************************************************************************}
function GetPlayersMulti(TheVarArr: TVariantArray): TPlayerArray;
var
vType, i, h, resCounter, Hi, Hig: Integer;
Players: TPlayerArray;
b: boolean;
begin
Players := SortPlayers(GetAllPlayers);
SetLength(Result, Length(Players));
Hig := High(TheVarArr);
Hi := High(Players);
for i := 0 to Hi do
for h := 0 to Hig do
begin
vType := VarType(TheVarArr[h]);
if (vType = varString) then
TheVarArr[h] := LowerCase(Trim(TheVarArr[h]));
case vType of
varInteger: B := Players[i].Index = TheVarArr[h];
varString: B := (Pos(TheVarArr[h], LowerCase(Trim(Players[i].Name))) > 0);
else Exit;
end;
if not B then
Continue;
Result[resCounter] := Players[i];
inc(resCounter);
end;

SetLength(Result, resCounter);
end;

{*******************************************************************************
function GetPlayers(TheVar: Variant): TPlayerArray;
By: Drags111/mormonman
Description: Returns all the players by the variant. (Name or Index)
*******************************************************************************}
function GetPlayers(TheVar: Variant): TPlayerArray;
begin
Result := GetPlayersMulti([TheVar]);
end;

{*******************************************************************************
function GetPlayerMulti(TheVarArr: TVariantArray):TPlayer;
By: Drags111/mormonman
Description: Gets the closest player that matches the arguments.
*******************************************************************************}
function GetPlayerMulti(TheVarArr: TVariantArray):TPlayer;
var
Players: TPlayerArray;
i, h, vType, Hi, Hig: Integer;
B: Boolean;
begin
Result := NULL_PLAYER;
Players := SortPlayers(GetAllPlayers);
Hi := High(Players);
Hig := High(TheVarArr);
for i := 0 to Hi do
for h := 0 to Hig do
begin
vType := VarType(TheVarArr[h]);
if (vType = varString) then
TheVarArr[h] := LowerCase(TheVarArr[h]);
case (vType) of
varInteger: B := Players[i].Index = TheVarArr[h];
varString: B := (Pos(TheVarArr[h], LowerCase(Players[i].Name)) > 0);
else Continue;
end;
if (B) then
begin
Result := Players[i];
Exit;
end;
end;
end;

{*******************************************************************************
function GetPlayer(TheVar: Variant):TPlayer;
By: Drags111/mormonman
Description: Gets the closest player that matches the argument.
*******************************************************************************}
function GetPlayer(TheVar: Variant):TPlayer;
begin
Result := GetPlayerMulti([TheVar]);
end;

{*******************************************************************************
function GetPlayersByLevelEx(Levels: TIntegerArray): TPlayerArray;
By: mormonman
Description: Returns all players with the defined Levels.
*Note - Levels = Combat Levels
*******************************************************************************}
function GetPlayersByLevelEx(Levels: TIntegerArray): TPlayerArray;
var
i, h, Hi, Hig, resCounter : Integer;
Players : TPlayerArray;
begin
Players := SortPlayers(GetAllPlayers);
SetLength(Result, Length(Players));
Hi := High(Players);
Hig := High(Levels);
for i := 0 to Hi do
for h := 0 to Hig do
if Players[i].Level = Levels[h] then
begin
Result[resCounter] := Players[i];
inc(resCounter);
end;
SetLength(Result, resCounter);
end;

{*******************************************************************************
function GetPlayersByLevel(Level: Integer): TPlayerArray;
By: mormonman
Description: Returns all players with the defined Level.
*******************************************************************************}
function GetPlayersByLevel(Level: Integer): TPlayerArray;
begin
Result := GetPlayersByLevelEx([Level]);
end;

{*******************************************************************************
function GetPlayerByLevelEx(Levels: TIntegerArray): TPlayer;
By: mormonman
Description: Gets the closest player that is one of the Levels.
*******************************************************************************}
function GetPlayerByLevelEx(Levels: TIntegerArray): TPlayer;
var
i, h, Hi, Hig : Integer;
Players : TPlayerArray;
begin
Result := NULL_PLAYER;
Players := SortPlayers(GetAllPlayers);
Hi := High(Players);
Hig := High(Levels);
for i := 0 to Hi do
for h := 0 to Hig do
if Players[i].Level = Levels[h] then
begin
Result := Players[i];
end;
end;

{*******************************************************************************
function GetPlayerByLevel(Level: Integer): TPlayer;
By: mormonman
Description: Gets the closest player that is the Level
*******************************************************************************}
function GetPlayerByLevel(Level: Integer): TPlayer;
begin
Result := GetPlayerByLevelEx([Level]);
end;

{*******************************************************************************
procedure DumpPlayers(Players: TPlayerArray);
By: Drags111
Description: Prints out the array of Players. Mainly for debugging purposes.
*******************************************************************************}
procedure DumpPlayers(Players: TPlayerArray);
var
i, Hi: integer;
begin
Hi := High(Players);
for i := 0 to Hi do
begin
Writeln('Player: '+Players[i].Name);
Writeln(' '+'Index: '+ ToStr(Players[i].Index));
Writeln(' '+'Tile: '+ ToStr(Players[i].Tile));
Writeln(' '+'Animation: '+ ToStr(Players[i].Animation));
Writeln(' '+'Height: '+ ToStr(Players[i].Height));
Writeln(' '+'IsMoving: '+ ToStr(Players[i].Moving));
Writeln(' '+'Fighting: '+ ToStr(Players[i].Fighting));
Writeln(' '+'HPRatio: '+ ToStr(Players[i].HPRatio));
Writeln(' '+'Level: '+ ToStr(Players[i].Level));
Writeln(' '+'Orientation: '+ ToStr(Players[i].Orientation));
Writeln(' '+'Interacting: '+ ToStr(Players[i].Interacting));
end;
end;

{*******************************************************************************
function GetMe: TMe;
By: Widget, lordsaturn, Drags111
Description: Returns you
*******************************************************************************}
function GetMe: TMe; // Sounds funny =P
var
Player, theName, pdef, i: integer;
begin
try
Player := SmartGetFieldObject(0, hook_static_MyPlayer);
pdef := SmartGetFieldObject(Player, hook_player_PlayerDef);
theName := SmartGetFieldObject(Player, hook_player_GetName);
Result.Name := GetJavaString(theName, 255);
Result.Tile.X := (SmartGetFieldInt(0, hook_static_BaseX) + (SmartGetFieldInt(Player, hook_animable_PixelX) / 512));
Result.Tile.Y := (SmartGetFieldInt(0, hook_static_BaseY) + (SmartGetFieldInt(Player, hook_animable_PixelY) / 512));
Result.Height := iabs(SmartGetFieldInt(Player, hook_character_GetHeight)) / 4;
Result.Animation := SmartGetFieldInt(Player, hook_character_Animation);
Result.Speed := SmartGetFieldInt(Player, hook_character_Motion);
Result.HPRatio := SmartGetFieldInt(Player, hook_character_HPRatio) * 100 / $FF;
Result.Level := SmartGetFieldInt(Player, hook_player_GetLevel);
Result.InFight := (SmartGetFieldInt(0,hook_static_LoopCycle) < SmartGetFieldInt(Player,hook_character_LoopCycleStatus));
Result.Orientation := Round(FixD(180 + (SmartGetFieldInt(Player, hook_character_Orientation)* 45) / 2048));
Result.Interacting := SmartGetFieldInt(Player, hook_character_GetInteracting);
for i := 0 to 24 do
begin
Result.Experience[i] := SmartGetFieldArrayInt(0, hook_static_GetSkillExperiences, i);
Result.Levels[i] := SmartGetFieldArrayInt(0, hook_static_GetSkillLevels, i);
end;
finally
SmartFreeObject(theName);
SmartFreeObject(Player);
SmartFreeObject(pdef);
end;
end;

{*******************************************************************************
function InteractingWithMe(NPC: TNPC): Boolean;
By: Drags111
Description: Checks if the NPC is currently interacting with your character.
*******************************************************************************}
function InteractingWithMe(NPC: TNPC): Boolean;
var
Me: TMe;
Player: TPlayer;
PIdx: integer;
begin
NPC := GetNPC(NPC.ID);
Me := GetMe;
PIdx := (NPC.Interacting - 32768);
Player := GetPlayer(PIdx);
Result := (Player.Name = Me.Name);
end;

{*******************************************************************************
function AreWeInteracting(var NPC: TNPC): Boolean;
By: mormonman
Description: Returns true if we are interacting. If we are, then it returns npc
we are interacting with.
*******************************************************************************}
function AreWeInteracting(var NPC: TNPC): Boolean;
var
i, index : integer;
npcs : TNPCArray;
Me : TMe;
Player : TPlayer;
begin
NPC := NULL_NPC;
Me := GetMe;
npcs := GetAllNPCs(False);
for i := 0 to length(npcs)-1 do
begin
index := npcs[i].Interacting - 32768;
if index > 0 then
begin
Player := GetPlayer(index);
Result := Player.Name = Me.Name;
if Result then
begin
NPC := npcs[i];
Exit;
end;
end;
end;
end;

kingarabian

2011-01-09 14:47:47

Interesting mormon, Its easier to add player fighting if one decides to make SoulWars script…

mormonman

2011-01-09 14:49:58

From: kingarabian
Interesting mormon, Its easier to add player fighting if one decides to make SoulWars script…


Mhmm. I'm not going to, just giving people the incentive xD

Method

2011-01-09 19:05:23

From: mormonman
Mhmm. I'm not going to, just giving people the incentive xD


Assuming the field you used is what I think it is, it'll only work if the player is under attack. Trying to use it when attacking anything that won't attack back (e.g. if your player is in a safe spot) won't work.

mormonman

2011-01-09 20:16:52

From: Method
Assuming the field you used is what I think it is, it'll only work if the player is under attack. Trying to use it when attacking anything that won't attack back (e.g. if your player is in a safe spot) won't work.


Ya, its just the loop cycle. You can use Interacting if you want to check if it is fighting.

mormonman

2011-01-19 13:10:01

If there are no objections I'm going to commit this later today.

Edit: Committed and locked.