SRL Archive - SRL Archives

A Motion Detector!!!


Roberty Bob

2006-07-03 13:24:53

Have you ever wondered how to implement real time motion sensing in your scripts.?

Am I currently moving ?

Is that a fire?

Am I mining?

How many players are moving around on the MM for dynamic chat responses?

Is the tree shaking?


This newly created function from Roberty Bob is a must for all you motion detecting fanatics… it works like a treat, but give me suggestions on what to improve.!!
Throw this code in a main loop and login to test it in a large area in the top left.
[PHP]
begin
repeat
x:=50;
y:=50;
until DetectMotionInBox(x,y,40,10,80,300);
end.
[/PHP]



[PHP]
{*******************************************************************************
Function DetectMotionInBox(var x,y:integer; BoxDimension,PointsHV,Accuracy,TimeFrame:integer):boolean;
By: Roberty Bob.
Description: Generates a grid of size (PointsHV X PointsHV) within a box of BoxDimension
around the x,y coords inputed. Accuracy (0-100) reflects how many pixels changing color will trigger the detector.
Timeframe is the time in ms which the change will occur over. This will be improved in future version.
*******************************************************************************}
Function DetectMotionInBox(var x,y:integer; BoxDimension,PointsHV,Accuracy,TimeFrame:integer):boolean;
var
f,g,h,i,j,k, ArraySize,TempColor,
x1,y1,x2,y2 :integer;
var
Store1, Store2: array of integer;

begin
ArraySize:= PointsHV*PointsHV;
SetArrayLength(Store1,Arraysize);
SetArrayLength(Store2,Arraysize);

x1:= x-(BoxDimension/2);
y1:= y-(BoxDimension/2);
x2:= x+(BoxDimension/2);
y2:= y+(BoxDimension/2);

g:= 0;
h:= 0;
//—————————
i:= 0;
j:= x1;
k:= y1;

f:= 0;
repeat
Store1[f]:= 0;
f:= f+1;
until f=ArraySize-1

f:= 0;
repeat
Store2[f]:= 0;
f:= f+1;
until f=ArraySize-1


repeat
j:=x1;
repeat
begin
TempColor:= GetColor(j,k);

Store1:= TempColor;

i:= i+1;
j:= j+(BoxDimension/(PointsHV-1));
end;
until j>=x2
k:= k+(BoxDimension/(PointsHV-1));
until k>=y2

wait(TimeFrame);
//—————————
i:= 0;
j:= x1;
k:= y1;
repeat
j:=x1;
repeat
begin
TempColor:= GetColor(j,k);
Store2:= TempColor;

if Store1 = Store2 then
begin
g:= g+1;
h:= h+1;
end
else
h:= h+1;

i:= i+1;
j:= j+(BoxDimension/(PointsHV-1));
end;
until j>=x2
k:= k+(BoxDimension/(PointsHV-1));
until k>=y2
//—————————

if (Round(g/h*100))>=Accuracy then
Result:= False
else
Result:= True;
end;
[/PHP]

WT-Fakawi

2006-07-03 14:41:29

thats not bad at all….
but if you have come at this level, you might look into canvas copy routines and FastGetPixel, since those are infinitely more fast then GetColor.

Pyro

2006-07-03 16:46:41

From: WT-Fakawi
thats not bad at all….
but if you have come at this level, you might look into canvas copy routines and FastGetPixel, since those are infinitely more fast then GetColor.


Hmmmm are you sure. Go to srl snippets and my post, second script. It times scar functions and reports back with times. I ran getcolor like 100 times and average time it took was 0.02 ms with a max time of 1 ms.

But nice work roberty bob. Alot of peopel have made there own ones though

Roberty Bob

2006-07-03 19:02:22

Firstly thanks both for the encouragement… :) I wouldn't bother putting stuff here if i thought no one would want it.

as for speed… I'll look into it Fawki coz i'm an efficiency freak… I did try it with motion over 100*100 arrays with PointsHV as 100… so 20000 getcolors and 10000 getcolor comparisons and it still only takes 1/2 second for me thats giving you incredibly powerful motion detection I can almost tell you whether it's a chicken or a goblin moving by its accuracy threshold then :P

if you only use it with PointsHV at like 10… still pretty lively… and instant.

as for "lots of people have their own"… my vision of SRL is anything "common to man/woman" should be in the library… people shouldn't be having to go and make their own if a common one can cover majority of functionality.

You've given me inspiration to build a new one that reports the color change accuracy threshold for a detailed array… then i can know the exact change threshold for a specific event (me swinging the the pick etc…) hmmm

Cruel

2006-07-03 19:45:44

Make a plugin if you really want to make good motion detecting functions.

c0de

2006-07-03 20:39:56

all eyes on pups

YoHoJo

2006-10-07 13:48:49

wow this is so useful iv been looking for one of these.
Can you please give a little better explination of what the variables are please?

thanks

Flyboy

2006-10-07 16:43:37

If efficency is what your after, consider looking at a grid within the box…
if you are looking at a box of (100,100) at a 10 point grid then check points
0,0; 10,0; 20,0 … 100,0; 0,10; 10,10 … 100,100

Adjustable grid size will allow for full scan as you are doing now, up to a lighting fast partial scan. I like the idea of it returning a percentage of movement… Maybe something like:
Function DetectMotionInBox(var x,y:integer; BoxDimension,PointsHV:integer; var Accuracy:integer; TimeFrame:integer):boolean;

Yakman

2006-10-07 18:26:51

i agree with fakawi, i really think fastgetpixel or maybe canvas.pixel[x,y] would be better for this since it saves two pictures instead of reading directly from the client canvas

maybe something like this

function ismotioninbox(x1,y1,x2,y2,waitbetween:integer;percentmovement:extended):boolean;
var bmp1,bmp2,count,x3,y3:integer
begin
bmp1:=bitmapfromstring(x2-x1,y2-y1,'')
bmp2:=bitmapfromstring(x2-x1,y2-y1,'')
safecopycanvas(getclientcanvas,getbitmapcanvas(bmp1),x1,y1,x2,y2,0,0,x2-x1.y2-y1)
wait(waitbetween)
safecopycanvas(getclientcanvas,getbitmapcanvas(bmp2),x1,y1,x2,y2,0,0,x2-x1.y2-y1)
for y3:=0 to y2-y1 do
for x3:=0 to x2-x1 do
if(fastgetpixel(bmp1,x3,y3)<>fastgetpixel(bmp2,x3,y3))then
count:=count+1
if((count/((x2-x1)*(y2-y1))*100)>=percentmovement)then
result:=true
freebitmap(bmp1)
freebitmap(bmp2)

ruler

2006-10-08 04:08:34

first how fast does pup's plugin cycle?

To do the box, how large is the window of a capture for a pic that will be useful.

Also does this just compare the previous frame to the current?

Also what would be the best way to determine if you are moving. I was thinking of using 40 random pixles and comapring them each frame. if 30 are diff your moving? with a frame being 1 secound?

masquerader

2006-10-08 14:27:55

im just gonna post a snippet of a rune essence portal detecting script here, the original author is from sythe i think, something like habedabei

type
ary = array[1..21] of extended;
arx = array[1..34] of ary;

procedure FindPortal(var Portalx, Portaly : Integer);
var
lx, ly : Integer;
matrixA, matrixB : arx;
begin
if (LoggedIn) then
begin
Wait(1000 + Random(2000));
for lx := 1 to 34 do
begin
for ly := 1 to 21 do
begin
MatrixA[lx][ly] := GetColor(lx * 15, ly * 15);
end;
end;
Wait(250);
for lx := 1 to 34 do
begin
for ly := 1 to 21 do
begin
MatrixB[lx][ly] := GetColor(lx * 15, ly * 15);
end;
end;
for lx := 1 to 34 do
begin
for ly := 1 to 21 do
begin
if (MatrixA[lx][ly] <> MatrixB[lx][ly]) then
begin
MMouse((lx * 15), (ly * 15), 0, 0);
Wait(500 + random(50));
if (IsTextAt2(9, 9, 'Use', 100)) then
begin
Mouse((lx * 15), (ly * 15), 0, 0, False);
Wait(350 + Random(150));
if (PopUp('Use')) then
begin
PortalFound := True;
FFlag(0);
Exit;
end;
end;
end;
end;
end;
end;
end;
that might be a little useful, i dunno

i might start looking into this ;)

lardmaster

2006-10-24 18:18:45

hopefully eventually this might be used to track the movement of objects, so you could say the coordinates from where it moved, and where it moved to!

Roberty Bob

2006-10-26 21:10:02

I obviously made this a long while back, and it does have uses.
the biggest problem in its current form is that to compare between frames SCAR has to just "twidle it's thumbs"…
I was toying with the idea of some time marks or something.
it needs development to be really useful. I think it has great potential.

lardmaster

2006-10-27 14:29:54

^^^
agreed, i will help if needed