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]
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]