SRL Archive - OSR Beginner Scripting Tutorials

[TuT]Using Pixelshift for animation


Flight

2012-02-04 00:48:45

I actually quickly wrote this out for one person, briefly explaining how you can use SRL's Pixelshift to detect animation; in this case our own player's animation.
Originally posted here: http://villavu.com/forum/showthread.php?t=72792

Animation detection through Pixelshift
By Flight


I'll give you a crash course on pixelshift, just not using those two functions. First of all, pixelshift, I believe, will count the amount of changing pixels within a certain box region over a certain amount of time. So let's say you wanted to check if your player is doing an animation, so let's use this function: (my own "Is Mining" function)

Function Mining: Boolean;
var
PBox: TBox;
begin
PBox := IntToBox(245, 130, 285, 195);
Result := (AveragePixelShift(PBox, 250, 500) > 400);
end;
In this function you can see it uses 'AveragePixelShift' with the parameters (Box, waitPerLoop, maxTime) and returns a boolean (True of False). "Box" is the box region the function will record your pixelshift (change in pixels). "waitPerLoop" I'm not 100% sure, but 250 seems to be just right. "maxTime" is the length of each loop it checks for pixelshift.

Since we're checking for player animation it would only make sense to have the 'Box' we record pixelshift be a box around our player, right? So let's use this box: (245, 130, 285, 195), it's directly around our player. So we have our box region, we have the 'waitPerLoop' (I just leave it 250), and you see I, be default, have 500 as the 'maxTime', which means each length of recording pixelshift will be 500 milliseconds (half a second).

The function 'AveragePixelShift' will return an integer, which will be our average amount of pixelshift within that box in 500 milliseconds. In the function I provided it will check if that number is over 400, in which case the function would return True that our player is using the Mining animation. Should that number be under 400 then it'll return false because chances are we're just standing idle, right?

Anyways, I hope that helps. Here's a test script I use to constantly print out the player's pixelshift. You can use it to determine what's a good number to use to detect if you're doing a certain animation:

program PrintMyPixels;
{$DEFINE SMART}

{$i SRL/srl.simba}

Procedure DebugShiftCount;
var
PBox: TBox;
begin
PBox := IntToBox(245, 130, 285, 195);
Writeln(IntToStr(AveragePixelShift(PBox, 250, 500)));
end;


begin
Smart_Server := 86;
Smart_Members := True;
Smart_Signed := True;
Smart_SuperDetail := False;
setupSRL();

Repeat
DebugShiftCount;
Until(False);
end.

~Cheerz

I Am Legend

2012-02-04 01:07:52

thanks

RyGuy

2012-02-04 01:13:03

Thanks Flight! ^^

NickMystre

2012-02-04 02:16:39

From: Flight
"waitPerLoop" I'm not 100% sure, but 250 seems to be just right.


For those of us (that would be me and any others that would like to join me) that still battle to take in the finer details of the animation includes, why don't we just ask Coh3n and/or marpis to explain these for us?

E: On the grand scale of things - I can appreciate that perhaps animation.simba is not the most complex coding out there, but I am willing to bet that there are many who would like a blow by blow breakdown on the functions.

Kyle Undefined

2012-02-05 15:18:01

Awesome tutorial Flight! I love PixelShift!

poopy2177

2012-02-05 16:12:17

Just what I was looking for! ty

NexPB

2012-02-13 16:26:01

Great tut man :D

Inception

2012-02-13 16:27:21

This is a great tutorial!

Jagex_Fagex

2012-02-22 05:43:54

Awesome, very helpful!

EDIT: Also, 650 seems to work better than 400 for the mining function

bable fittle

2012-02-29 16:27:50

This is what I have been waiting for to finally make a firemaking script! Thanks!

riwu

2012-03-20 08:21:23

just wondering..under what situation would this be more efficient than using GetXPBar?

masterBB

2012-03-20 08:24:45

From: riwu
just wondering..under what situation would this be more efficient than using GetXPBar?


When you are cutting magic trees. It may take a minute before you get a log, this one will be faster.

Flight

2012-03-20 08:53:31

From: masterBB
When you are cutting magic trees. It may take a minute before you get a log, this one will be faster.


Yep yep, rocktails / sharks as well. Low-level'ed woodcutters chopping high level trees, low-level'ed miners mining high level ores, ect…

riwu

2012-03-20 08:53:46

From: masterBB
When you are cutting magic trees. It may take a minute before you get a log, this one will be faster.


ah thx +Rep! basically any animating activity that gains multiple xp/click.

eska

2012-03-22 02:47:57

Pixel shift seems so scary at first but now it looks ridiculously easy. Maybe you made it look easier than it is though :p

P1nky

2012-03-22 03:10:22

Great to use, currently using on a upcoming script. And this tut explains it all! Good job:)

Sassakill

2012-04-18 08:29:55

Thank you Flight!
Helped me out a great bit! :D

Era Xz

2012-04-18 16:07:21

Dam genius! Thanks a lot ;)
This will replace my poor is walking function which checked for SPS location every sec

Oxygen

2012-04-18 16:15:23

Great tutorial!

Sirenia

2012-05-20 09:41:20

Helped alot!

cadet54

2012-05-25 15:46:23

Thanks very much, another method of checking if the character is cutting, mining fishing or what ever and a few other things that can come in handy.
Thanks again :)

Mat

2012-06-20 16:35:20

I'm still using this today :), I keep attempting to use it, now I am going to work on it :D
Thanks Flight :)
Mat

Footy

2012-07-26 14:30:00

Very very useful! Thanks flight!

Etrnl Fear

2012-07-29 12:51:18

Thanks for the tutorial!

But, would the variables (245, 130, 285, 195) need to be changed with the RS toolbar being imbedded into the RS applet?

Flight

2012-07-29 21:34:55

From: Etrnl Fear
Thanks for the tutorial!

But, would the variables (245, 130, 285, 195) need to be changed with the RS toolbar being imbedded into the RS applet?


Nah, the current SRL & Simba already have adjustments in place; these parameters remain correct.

Etrnl Fear

2012-07-30 21:54:23

Sweet.

Thanks again. :)
+Rep

QQu

2012-07-31 05:59:02

Nice!

I still can't get it to work for chopping.. something like look for the tree (object) after the player stops the chopping animation. Anyone?

litoris

2012-07-31 06:04:58

From: QQu
Nice!

I still can't get it to work for chopping.. something like look for the tree (object) after the player stops the chopping animation. Anyone?


What part can you not get to work? If it can't detect when it's chopping, get the average pixelshift. There was a function for that.

QQu

2012-07-31 06:45:01


Function notSmashing : Boolean;
var
PBox : TBox;
begin
PBox := IntToBox(245, 190, 285, 195);
Result := (AveragePixelShift(PBox, 5000, 500) < 300);
end;

Does that seem right? :/

riwu

2012-07-31 06:52:37

From: QQu

Function notSmashing : Boolean;
var
PBox : TBox;
begin
PBox := IntToBox(245, 190, 285, 195);
Result := (AveragePixelShift(PBox, 5000, 500) < 300);
end;

Does that seem right? :/

Swap that 5000 with 500, Your waitPerLoop is longer than maxTime.

alucard123

2012-09-25 21:40:28

started using it =3 awesome

only problem is when some1 get too close and start atking my character wont move but shhhhhh

mounty

2012-11-23 12:57:24

This works great for woodcutting scripts. I've used it in my willow cutter. Works really well after adjusting the threshold. Has some minor problems when standing right next to people or behind large trees, but overall its a pretty handy tool.

Ian

2012-11-29 20:50:11

Thanks Flight, I'm using this in my fisher and it works like a charm :)

And that PixelShift finding tool is downright handy! Rep+

Rezozo

2012-11-29 23:15:53

Yea, this is where I got my chopping and mining checks from :D
I didn't even see/ read about the pixelshift finding tool. :p
~Rez

rj

2012-12-17 16:43:18

Great tut, I got my animation detection working good, but I have one queston, how would I make it click a lot faster after it no longer see's as much pixels shifting?
My code is:
program test;
{$i srl/srl.simba}
const
TREEX=120;
TREEY=81;
TREEX2=382;
TREEY2=226;
Function Choppin: Boolean;
var
PBox: TBox;
begin
PBox := IntToBox(249, 134, 277, 180);
Result := (AveragePixelShift(PBox, 200, 300) > 150);
Writeln(IntToStr(AveragePixelShift(PBox, 500, 650)));
end;
Procedure Woodchop;
var
X, Y: Integer;
begin
if (Choppin) then
begin
//writeln('Waiting to stop Cutting');
wait(100);
end else
begin
//writeln('Lets look for a Tree');
if FindColorTolerance(X,Y,12400,TREEX,TREEY,TREEX2,TREEY2,10)then
MouseSpeed:=15;
mmouse(x, y,1,1);
Wait(350 + Random(150));
ClickMouse(X, Y, mouse_Left);
Wait(500 + Random(700));
end;
end;
begin
repeat
Woodchop;
until (IsKeyDown (114));
end.

Flight

2012-12-17 21:03:15

Try this bud:

program test;
{$i srl/srl.simba}

const
TREEX=120;
TREEY=81;
TREEX2=382;
TREEY2=226;

Function Choppin: Boolean;
var
PBox: TBox;
begin
PBox := IntToBox(249, 134, 277, 180);
Result := (AveragePixelShift(PBox, 200, 300) > 150);
Writeln(IntToStr(AveragePixelShift(PBox, 500, 650)));
end;

Procedure Woodchop;
var
X,Y: Integer;
begin
if (Choppin) then
begin
//writeln('Waiting to stop Cutting');
wait(100);
end else
begin
//writeln('Lets look for a Tree');
if FindColorTolerance(X,Y,12400,TREEX,TREEY,TREEX2,TREEY2,10)then
begin
mmouse(x,y,1,1);
ClickMouse2(mouse_left)
WaitFunc(@Choppin, 10, 3000); //Wait for 3 seconds if we've started chopping.
end;
end;
end;

begin
SetupSRL;
MouseSpeed:=15;

repeat
Woodchop;
until (IsKeyDown (114));
end.

count to potato

2013-06-17 16:06:03

Could this be used to detect if an item has spawned on the ground in a specific spot? I'm a far way from implementing anything like this but I have been thinking about things scripts I make could accomplish. Thanks for the tut!!!

westkevin12

2014-05-04 20:08:50

I actually use this same code for my mining script. it's outdated now tho but going to get it running again.