SRL Archive - OSR Utility Scripts

GetHPPercent


Shatterhand

2013-03-23 18:17:45

This is a function I made on P06 server. Changed some coordinates and it works on 07Scape too.

What does it do?
Useful function for fighter scripts, checks current hp without looking at stats tab.
Finds avatar's hp bar and calculates hp percentage of it. Camera angle must be on highest.


program GetHPPercent;
{$I SRL/SRL.simba}
{$I SRL/SRL/misc/Debug.simba}



procedure SetBoxToBounds(var Box : TBox; X1,Y1,X2,Y2 : Integer);
begin
if (Box.x1 < X1) then Box.x1 := X1;
if (Box.x2 > X2) then Box.x2 := X2;
if (Box.y1 < Y1) then Box.y1 := Y1;
if (Box.y2 > Y2) then Box.y2 := Y2;
end;



procedure DebugTPA_ATPA(TPA: TPointArray; ATPA: Array of TPointArray; First : Integer);
var
BMP, Col, x, y, i, width, height: Integer;
TPABox : TBox;
begin

GetClientDimensions(width,height);
BMP := BitmapFromClient(0,0,width-1,height-1);
DrawTPABitmap(BMP,TPA,ClWhite);

for i:=0 to High(ATPA) do
begin
if i = First then
Col := ClRed
else
Col := ClPurple;

TPABox := GetTPABounds(ATPA[i]);
SetBoxToBounds(TPABox,0,0,width-1,height-1);

for x := TPABox.x1 to TPABox.x2 do
begin
FastSetPixel(BMP,x,TPABox.y1,Col)
FastSetPixel(BMP,x,TPABox.y2,Col)
end;
for y := TPABox.y1 to TPABox.y2 do
begin
FastSetPixel(BMP,TPABox.x1,y,Col)
FastSetPixel(BMP,TPABox.x2,y,Col)
end;
end;

TPABox := IntToBox(223,127,295,175);
for x := TPABox.x1 to TPABox.x2 do
begin
FastSetPixel(BMP,x,TPABox.y1,Col)
FastSetPixel(BMP,x,TPABox.y2,Col)
end;
for y := TPABox.y1 to TPABox.y2 do
begin
FastSetPixel(BMP,TPABox.x1,y,Col)
FastSetPixel(BMP,TPABox.x2,y,Col)
end;

DebugBitmap(BMP);
FreeBitmap(BMP);
end;



function GetHpPercent : Integer;
var
CombinedTPA, GreenTPA, RedTPA : TPointArray;
CombinedATPA, GreenATPA, FinalATPA : Array of TPointArray;
X1,Y1,X2,Y2, i,j,k, BoxWidth,BoxHeight : Integer;
ExcludeBoxes : Array of TBox;
TempBox : TBox;
BadTPA : Boolean;
begin
Result := -1;

X1 := 223;
Y1 := 127;
X2 := 295;
Y2 := 175;

FindColorsTolerance(GreenTPA,65280,X1,Y1,X2,Y2,0);
FindColorsTolerance(RedTPA,255,X1,Y1,X2,Y2,0);

CombinedTPA := CombineTPA(GreenTPA,RedTPA);

if (High(CombinedTPA) > 0) then
begin

SplitTPAWrap(CombinedTPA,1,CombinedATPA);

SetArrayLength(ExcludeBoxes,0);
j := -1;
for i:=0 to High(CombinedATPA) do
begin
TempBox := GetTPABounds(CombinedATPA[i]);
BoxWidth := TempBox.x2-TempBox.x1;
BoxHeight := TempBox.y2-TempBox.y1;
if not (BoxWidth = 29) or not (BoxHeight = 4) then
begin
Inc(j);
SetArrayLength(ExcludeBoxes,j+1);
ExcludeBoxes[j] := TempBox;
end;
end;

SplitTPAWrap(GreenTPA,1,GreenATPA);

SetArrayLength(FinalATPA,0);
k := 0;
for i:=0 to High(GreenATPA) do
begin
BadTPA := False;
for j:=0 to High(ExcludeBoxes) do
if PointInBox(MiddleTPA(GreenATPA[i]),ExcludeBoxes[j]) then
BadTPA := True;
if not BadTPA then
begin
SetArrayLength(FinalATPA,k+1);
FinalATPA[k] := GreenATPA[i];
end;
end;

SortATPAFrom(FinalATPA,Point(MSCX,MSCY));

DebugTPA_ATPA(GreenTPA,FinalATPA,-1);

if (Length(FinalATPA) < 1) then Exit;

TempBox := GetTPABounds(FinalATPA[0]);
BoxWidth := TempBox.x2-TempBox.x1;

Result := Round(BoxWidth*100/29);

end;
end;



var
HPLevel, HPPerc, HP : integer;

begin
ClearDebug;

HPLevel := 59;
repeat
HPPerc := GetHPPercent;
ClearDebug;
if not (HPPerc = -1) then
begin
HP := Round(HPLevel*HPPerc/100)
Writeln('HP: ' + IntToStr(HP));
if HP < 30 then
PlaySound('C:/Simba/sound2.wav');
end;
Wait(100);
until False;
end.

Hexless

2013-03-23 18:32:31

Nice

Parapa6

2013-03-23 18:33:47

I'll tried it real quick, does it just check HP or sumthin ?

I always remember back in the day there were scripts that could benefit PKers (and everyone else for that matter) that were capable of displaying your next hit like a half a second before you hit. I don't have the knowledge to recreate that but that would be a neat tool to use.

Ashaman88

2013-03-23 18:52:32

Would there be chances for false positives if someone was underattack below you or right next to you? We have a general run away function for randoms when they detect the hp bar right around where your player is, but since angles and such can change a bit the search zone might pick up somebody else's from time to time and cause you to run for no reason. Have you tested this with several people in combat right next to/on top of your player?


Only ask b/c it might be good for randoms

Shatterhand

2013-03-23 19:05:35

From: Ashaman88
Would there be chances for false positives if someone was underattack below you or right next to you? We have a general run away function for randoms when they detect the hp bar right around where your player is, but since angles and such can change a bit the search zone might pick up somebody else's from time to time and cause you to run for no reason. Have you tested this with several people in combat right next to/on top of your player?

Only ask b/c it might be good for randoms

It only works on highest angle.
It will return with false data if people are under you.
If they are next to you, it should work.

Flight

2013-03-23 20:09:51

This works very nicely. I didn't try it around other players but I did try it at all angles and it seems to work quite accurately. Nice job buddy. :)

litoris

2013-03-24 04:08:20

This is nice! Maybe make its name ApproxHPPercent or something to differentiate from the "real" percentage function.

AIex

2013-03-24 04:16:31

Couldn't it be possible to add a function if monsters or people are underneath you, it would check the stats tab for reinsurance? Otherwise it looks awesome :)

l6bustank

2013-03-24 04:20:16

Been using sth like this myself but this looks way better than mine at first glance. Thanks for sharing :) , will test sometime today/tomorrow.

Shatterhand

2013-03-24 05:50:10

From: AIex
Couldn't it be possible to add a function if monsters or people are underneath you, it would check the stats tab for reinsurance? Otherwise it looks awesome :)

The usage of this should be: if you get under 50% HP with this, you check stats tab too then eat.

Updated the code, had some silly bugs in it.

Hoodz

2013-03-24 09:46:23

thank you so much!
my fighter is now completed thanks to this.
can i use this in my script if i give credits to you?

(keep my script private first for some days)

Shatterhand

2013-03-24 15:20:20

From: hoodz
thank you so much!
my fighter is now completed thanks to this.
can i use this in my script if i give credits to you?

(keep my script private first for some days)

Sure. That's why I shared it.

Nubby

2013-03-26 16:19:17

Been trying to figure out how to incorporate this into a fighting script but no luck as of yet :confused:. I know it's probably fairly simple, so hints would be appreciated!

litoris

2013-03-26 16:27:46

From: Nubby
Been trying to figure out how to incorporate this into a fighting script but no luck as of yet :confused:. I know it's probably fairly simple, so hints would be appreciated!


if (GetHPPercent < 50) then
EatingProcedure;

rcheat6

2013-03-26 18:01:18

Awesome add, got it to work with the auto fight script perfectly with neat windows alerts.

Question! How do I get DebugImgForm to stop popping up? I have commented out the world and it keeps popping up whenever I close it

Shatterhand

2013-03-27 01:40:24

From: rcheat6
Awesome add, got it to work with the auto fight script perfectly with neat windows alerts.

Question! How do I get DebugImgForm to stop popping up? I have commented out the world and it keeps popping up whenever I close it

//DebugTPA_ATPA(GreenTPA,FinalATPA,-1);

Total

2013-03-27 01:53:10

Looks pretty useful. Maybe I will make a combat script sometime. Thanks for sharing!

samerdl

2013-06-01 12:41:16

i thought i already posted but i guess i didnt, thanks alot for this, works great for combat, really really useful, thank you - using it in my fcaver script

okokokok

2013-06-12 09:22:59

I'm trying to use the utility, but when i'm out of combat and you call it, it will still give a positive. So as long as there is no hpbar it will always say < percentage.

Could be the way I've implemented it?

Shatterhand

2013-06-12 09:34:38

From: okokokok
I'm trying to use the utility, but when i'm out of combat and you call it, it will still give a positive. So as long as there is no hpbar it will always say < percentage.

Could be the way I've implemented it?

If it finds a hp bar in the box, it should return with a 0-100 (calculated from the length of the bar). If it doesnt find, it should return with -1.

okokokok

2013-06-12 09:47:14

From: Shatterhand
If it finds a hp bar in the box, it should return with a 0-100 (calculated from the length of the bar). If it doesnt find, it should return with -1.


The problem is that I have a Teleport when health is below 40. Now it keeps teleporting.
I'll post the piece of the code.

////////////////////////////////////////////////////////
/////////// GetHPPercent Procedures & Functions ////////
/////////// Made by Shatterhand ////////
/////////// Credits to Shatterhand ////////
////////////////////////////////////////////////////////

procedure SetBoxToBounds(var Box : TBox; X1,Y1,X2,Y2 : Integer);
begin
if (Box.x1 < X1) then Box.x1 := X1;
if (Box.x2 > X2) then Box.x2 := X2;
if (Box.y1 < Y1) then Box.y1 := Y1;
if (Box.y2 > Y2) then Box.y2 := Y2;
end;



procedure DebugTPA_ATPA(TPA: TPointArray; ATPA: Array of TPointArray; First : Integer);
var
BMP, Col, x, y, i, width, height: Integer;
TPABox : TBox;
begin

GetClientDimensions(width,height);
BMP := BitmapFromClient(0,0,width-1,height-1);
DrawTPABitmap(BMP,TPA,ClWhite);

for i:=0 to High(ATPA) do
begin
if i = First then
Col := ClRed
else
Col := ClPurple;

TPABox := GetTPABounds(ATPA[i]);
SetBoxToBounds(TPABox,0,0,width-1,height-1);

for x := TPABox.x1 to TPABox.x2 do
begin
FastSetPixel(BMP,x,TPABox.y1,Col)
FastSetPixel(BMP,x,TPABox.y2,Col)
end;
for y := TPABox.y1 to TPABox.y2 do
begin
FastSetPixel(BMP,TPABox.x1,y,Col)
FastSetPixel(BMP,TPABox.x2,y,Col)
end;
end;

TPABox := IntToBox(223,127,295,175);
for x := TPABox.x1 to TPABox.x2 do
begin
FastSetPixel(BMP,x,TPABox.y1,Col)
FastSetPixel(BMP,x,TPABox.y2,Col)
end;
for y := TPABox.y1 to TPABox.y2 do
begin
FastSetPixel(BMP,TPABox.x1,y,Col)
FastSetPixel(BMP,TPABox.x2,y,Col)
end;

DebugBitmap(BMP);
FreeBitmap(BMP);
end;

function GetHpPercent : Integer;
var
CombinedTPA, GreenTPA, RedTPA : TPointArray;
CombinedATPA, GreenATPA, FinalATPA : Array of TPointArray;
X1,Y1,X2,Y2, i,j,k, BoxWidth,BoxHeight : Integer;
ExcludeBoxes : Array of TBox;
TempBox : TBox;
BadTPA : Boolean;
begin
Result := -1;

X1 := 223;
Y1 := 127;
X2 := 295;
Y2 := 175;

FindColorsTolerance(GreenTPA,65280,X1,Y1,X2,Y2,0);
FindColorsTolerance(RedTPA,255,X1,Y1,X2,Y2,0);

CombinedTPA := CombineTPA(GreenTPA,RedTPA);

if (High(CombinedTPA) > 0) then
begin

SplitTPAWrap(CombinedTPA,1,CombinedATPA);

SetArrayLength(ExcludeBoxes,0);
j := -1;
for i:=0 to High(CombinedATPA) do
begin
TempBox := GetTPABounds(CombinedATPA[i]);
BoxWidth := TempBox.x2-TempBox.x1;
BoxHeight := TempBox.y2-TempBox.y1;
if not (BoxWidth = 29) or not (BoxHeight = 4) then
begin
Inc(j);
SetArrayLength(ExcludeBoxes,j+1);
ExcludeBoxes[j] := TempBox;
end;
end;

SplitTPAWrap(GreenTPA,1,GreenATPA);

SetArrayLength(FinalATPA,0);
k := 0;
for i:=0 to High(GreenATPA) do
begin
BadTPA := False;
for j:=0 to High(ExcludeBoxes) do
if PointInBox(MiddleTPA(GreenATPA[i]),ExcludeBoxes[j]) then
BadTPA := True;
if not BadTPA then
begin
SetArrayLength(FinalATPA,k+1);
FinalATPA[k] := GreenATPA[i];
end;
end;

SortATPAFrom(FinalATPA,Point(MSCX,MSCY));

DebugTPA_ATPA(GreenTPA,FinalATPA,-1);

if (Length(FinalATPA) < 1) then Exit;

TempBox := GetTPABounds(FinalATPA[0]);
BoxWidth := TempBox.x2-TempBox.x1;

Result := Round(BoxWidth*100/29);

end;
end;

procedure HPPercent1;
var
HPLevel, HPPerc, HP : integer;

begin
ClearDebug;

HPLevel := 59;
repeat
HPPerc := GetHPPercent;
ClearDebug;
if not (HPPerc = -1) then
begin
HP := Round(HPLevel*HPPerc/100)
Writeln('HP: ' + IntToStr(HP));
if HP < 30 then
Writeln('HP is FuckedUP');
end;
Wait(100);
until False;
end;
///////////////////////////////////////////////////////
/////////// Combat Procedures & Functions ///////////
/////////// Made by OkOkOkOk ///////////
///////////////////////////////////////////////////////

procedure Fight;
Var Count: Integer;
begin
FindNormalRandoms;
SetAngle(SRL_ANGLE_HIGH);
ClearDebug;
AntiBan;
FindNormalRandoms;

repeat
begin

FindNormalRandoms;
Writeln('Looking for victims');

repeat
Inc(Count);
If (FightingGiants) or
(FightingGiants2) then

Wait(2000 + random(50));
While (CombatCheck = True) do
begin

If (GetHpPercent < 50) then
begin
Writeln('well fuck...');
Count:=0;
end
else

FindNormalRandoms;
If (CombatCheck = True) then
begin
FindNormalRandoms;
Writeln('Still Fighting');
Writeln('Checking for Randoms #1');
Wait(300 + Random(400));
FindNormalRandoms;
Writeln('Checking for Randoms #2');
Wait(300 + Random(400));
FindNormalRandoms;
Writeln('Checking for Randoms #3');
Wait(300 + Random(400));
FindNormalRandoms;
Writeln('Checking for Randoms #4');
Wait(300 + Random(400));
FindNormalRandoms;
Writeln('Checking for Randoms #5');
Wait(300 + Random(400));
FindNormalRandoms;
Writeln('Checking for Randoms #6');
Wait(300 + Random(400));
FindNormalRandoms;
Writeln('Checking for Randoms #7');
Wait(300 + Random(400));
FindNormalRandoms;
Writeln('Checking for Randoms #8');
Wait(300 + Random(400));
FindNormalRandoms;
Writeln('Checking for Randoms #9');
Wait(300 + Random(400));
FindNormalRandoms;
Writeln('Checking for Randoms #10');
Wait(300 + Random(400));
FindNormalRandoms;
Count:=0;

While (CombatCheck = True) do
begin

If (GetHpPercent < 50) then
begin
Writeln('wellf fuck...');
Count:=0;
end;
end;
end;
end;

If (Count > 3) then
begin
FindNormalRandoms;
Writeln('Need to find Monsters');
GetBackToMonsters;
Count:=0;
Wait(2000 + random(100));
end;
FindNormalRandoms;

until (CombatCheck = False);
Looting;
end;
until (InvFull);
end;

Tried messing around with the spot a bit.

Shatterhand

2013-06-12 10:10:50

From: okokokok
The problem is that I have a Teleport when health is below 40. Now it keeps teleporting.
I'll post the piece of the code.

Tried messing around with the spot a bit.

Well, this is not 100% accurate, moving npcs or players can caues false results. So this should be like a secondary checker. Primary should be checking numbers on stats tab.
The quote myself from previous post:
The usage of this should be: if you get under 50% HP with this, you check stats tab too then eat.

Ill look into my code and your modified version after like a week, im busy with exams atm.

okokokok

2013-06-12 10:17:34

From: Shatterhand
Well, this is not 100% accurate, moving npcs or players can caues false results. So this should be like a secondary checker. Primary should be checking numbers on stats tab.
The quote myself from previous post:

Ill look into my code and your modified version after like a week, im busy with exams atm.


Ok thanks! ;)
Gl with your exams!

bob_dole

2013-06-12 12:20:02

When you are checking for randoms simply use

FNRwait(time);

It does exactly what you are doing, but in one simple function. M

Riylo69

2013-06-14 13:50:15

sweeeeeeeet

Le_don

2013-07-23 12:20:12

So how do I put this in a script? Do I just take the function and procedure?

Ian

2013-07-23 12:47:20

From: Le_don
So how do I put this in a script? Do I just take the function and procedure?


Yes, copy/paste it into your script. Make sure it is somewhere above where you plan to use it and not below :)