SRL Archive - SRL Suggestions

function SetScreenMouse(Direction, CameraAngle: Variant): Boolean;


Home

2011-10-27 12:01:49

Hello.
Me and euphemism have been working on this function for a while to provide very accurate and time saving ability to use your middlemouse to change Minimap Direction and / or Mainscreen camerangle and vice versa.

Enjoy!


This function sets the compass direction and camera angle by dragging the mouse while holding down the middle mouse button.

[Background]

Movement across the X-axis results in compass change, and movement across the Y-axis results in change of camera angle.

If one swipes from the very left or right of the screen, to the very edge of the other side, the compass swings 180 degrees.

Armed with this knowledge, we can divide that pixel distance (the 'X_Axis' constant) by 180 to find out how far to move along the X-axis, in pixels, to change one degree. This is where the 'DegToPx' constant to come from.

Camera angle is a bit different. The Y-axis change from the top of the screen to the bottom is, if I recall, 333 pixels. If the camera is at either extreme (all the way up, all the way down), and you swipe from the very top or bottom to the other side, you will find that the camera reaches the other extreme after roughly 278 pixels of change, meaning there is a further distance of 55 pixels to cover before reaching the other side of the screen. This means that their is a dead space where camera angle does not change.

Armed with this information, we can divide 278 by however many steps we want available to get from one camera extreme to the other. This is how the variable 'AngleStep' is determined within the function. The variable 'AngleStep' is how far to move along the Y-axis, in pixels, to change the camera angle one camera step. Home and I decided on 10.

[/Background]

The function first gets the compass direction in degrees, and checks to see if either of the parameters have been set to an empty string (''). If the direction is within 3.0 degrees, I think it is, of the direction set by the user, and CameraAngle is '', then it exits. Otherwise, if it is within 3.0 degrees, or is '', and the CameraAngle is a valid number, then the function knows that it is not changing the direction.

If setting the compass direction, the function sees what the shortest way to get to the specified compass direction is and decides to swipe left-to-right, or right-to-left accordingly. It then determines the distance to change across the X-axis to get to the specified compass direction. If setting camera angle, the function decides to start somewhere randomly within 55 pixels of the top or bottom of the mainscreen.

The function takes two mouse swipes to complete. The function takes the determined X change, and splits it randomly into two values: 'XChange1' and 'XChange2'. The first swipe's X change is equal to XChange1', and the Y change is that 278 pixels required to get to one of the camera extremes (It has to do this because we are not able to determine the camera angle). The second swipe's X change is equal to 'XChange2', and the function calculates the Y change required to get to the specified camera angle, and moves that distance across the Y-axis.

Once the function has set the compass direction and camera angle, it then goes back and checks to see if the compass direction is within 3.0 degrees of the specified direction. If not, it performs a third swipe across the X-axis to correct the compass direction.





program New;

{$i SRL\SRL.scar}

procedure Setup;
begin
SetupSRL;
ClearDebug;
end;

const
DegToPx = 2.838;
X_Axis = 511;

{*******************************************************************************
function SetScreenMouse(Direction, CameraAngle: Variant) :Boolean;
By: Home & euphemism
Description: Aligns RS to specified direction and camera angle.
Direction: Enter a compass angle ('n', 's', 'e', 'w') or degree (23.3, 25, 205.2)
It will move the compass to that angle using Middle Mouse. Also accepts
'random', and 'rand' to make the compass a random direction.
Use ('') for no movement.
CameraAngle: Enter a camera angle (0 - 10) Ex. ('1') or (1) It will move the
mainscreen to that camera angle using Middle Mouse. (0 lowest, 10 highest)
Use ('') for no movement.
*******************************************************************************}
function SetScreenMouse(Direction, CameraAngle: Variant) :Boolean;
var
ii: Integer;
i, DirectionDeg, StartAngle, Fraction: Extended;
AngleStep, DirFalseSet, FractionInt, NumOfSteps, WaitTime, TempMouse, YStart,
OffX, X, XChange1, XChange2, Y, YChange1, YChange2: Integer;
Left, SetCamera, SetDirection, TopOfScreen: Boolean;

begin

SetCamera := True;
SetDirection := True;

for ii := 0 to 2 do
begin
Result := False;
StartAngle := rs_GetCompassAngleDegrees;
DirFalseSet := 0;

if (StartAngle < 0) or (not LoggedIn) then Exit;
case VarType(Direction) of
varInteger, varDouble: DirectionDeg := FixD(Direction + 0.0);
varString: begin
i := StrToFloatDef(Direction,-1337);
if i = -1337 then
begin
case LowerCase(Direction) of
'n': DirectionDeg := 0;
'w': DirectionDeg := 270;
's': DirectionDeg := 180;
'e': DirectionDeg := 90;
'random', 'rand': DirectionDeg := RandomRange(0, 360);
'' : SetDirection := False;
end;
end else
DirectionDeg := FixD(i);
end;
end;

case VarType(CameraAngle) of
varString:
begin
if CameraAngle = '' then
SetCamera := False
else
CameraAngle := StrToInt(CameraAngle);
end;
end;

i := 0.0;

if ((not SetDirection) and (not SetCamera)) then
begin
Result := True;
Exit
end;

if (((MinE(Abs(StartAngle - DirectionDeg),
MinE(Abs(StartAngle - (DirectionDeg + 360)),
Abs((StartAngle + 360) - DirectionDeg)))) <= 3.0) and (SetCamera)) then
begin
SetDirection := False;
end;

if (((MinE(Abs(StartAngle - DirectionDeg),
MinE(Abs(StartAngle - (DirectionDeg + 360)),
Abs((StartAngle + 360) - DirectionDeg)))) <= 3.0) and (not SetCamera)) then
begin
Result := True;
Exit;
end;

if SetDirection then
begin
Left := (Round((360 - StartAngle) + DirectionDeg)
mod 360 <= Round((StartAngle + 360) - DirectionDeg) mod 360);
WaitTime := RandomRange(30, 90);
if Left then
begin
DirectionDeg := 360 + DirectionDeg - StartAngle;
OffX := - 5;
end else
begin
DirectionDeg := 360 - DirectionDeg + StartAngle;
OffX := 5;
end;
end else
begin
Left := RBool;
end;

if MouseSpeed > 14 then
begin
TempMouse := MouseSpeed
MouseSpeed := RandomRange(14, 15);
end;

if not SetDirection then
Fraction := 1
else
Fraction := DirectionDeg * DegToPx / X_Axis
- Floor(DirectionDeg * DegToPx / X_Axis);

if Fraction > 0 then
begin
FractionInt := Round(MSX2 * Fraction);


if SetDirection then
begin
XChange1 := RandomRange(0, FractionInt);
XChange2 := FractionInt - XChange1;
DirFalseSet := 0;
end else
begin
DirFalseSet := RandomRange(7, 511);
XChange1 := DirFalseSet;
XChange2 := 0;
OffX := 0;
end;

NumOfSteps := 10;
AngleStep := Ceil((278 / NumOfSteps));
TopOfScreen := RBool;

if SetCamera then
begin
CameraAngle := NumOfSteps - CameraAngle;
if TopOfScreen then
begin
YStart := RandomRange(MSY1, MSY1 + 55);
YChange1 := YStart + RandomRange(278, 280);
YChange2 := (CameraAngle * AngleStep) * -1;
end else
begin
YStart := RandomRange(MSY2, MSY2 - 55);
YChange1 := YStart - RandomRange(278, 280);
YChange2 := ((NumOfSteps - CameraAngle) * AngleStep);
end;
end else
begin
YStart := RandomRange(10, 330);
YChange1 := YStart;
YChange2 := 0;
end;

GetMousePos(X, Y);

if ii <> 0 then
begin
MMouse(X, Y, 0, 0);
end else
begin
if not Left then
begin
if not ii <> 0 then
MMouse(MSX2 - DirFalseSet, YStart, 0, 0)
else
MMouse(MSX2, Y, 0, 0);
end else
begin
if not ii <> 0 then
MMouse(MSX1 + DirFalseSet, YStart, 0, 0)
else
MMouse(MSX1, Y, 0, 0);
end;
end;

GetMousePos(X, Y);
HoldMouse(X, Y, mouse_Middle);
Wait(WaitTime);

if ((ii = 0) and (SetCamera))then
begin
if not Left then
begin
MMouse(MSX2 - XChange1, YChange1, 0, 0);
Wait(WaitTime);
GetMousePos(X, Y);
ReleaseMouse(X, Y, mouse_Middle);
Wait(WaitTime);
HoldMouse(MSX2 - XChange1, Y, mouse_Middle);
MMouse(((MSX2 - XChange1) - XChange2 + OffX), Y + YChange2, 0, 0);
end else
begin
MMouse(MSX1 + XChange1, YChange1, 0, 0);
Wait(WaitTime);
GetMousePos(X, Y);
ReleaseMouse(X, Y, mouse_Middle);
Wait(WaitTime);
HoldMouse(MSX1 + XChange1, Y, mouse_Middle);
MMouse(((MSX1 + XChange1) + XChange2 + OffX), Y + YChange2, 0, 0);
end;
end else
begin
if not Left then
MMouse(X - (FractionInt + 5), Y, 0, 0)
else
MMouse(X + (FractionInt - 5), Y, 0, 0);
end;

Wait(WaitTime);
GetMousePos(X, Y);
ReleaseMouse(X, Y, mouse_Middle);
Wait(WaitTime);
end;
SetCamera := False;
end;
MouseSpeed := TempMouse;
end;

var
Direc: String;
Angl: Integer;

begin
Setup;
Direc := IntToStr(RandomRange(0, 360));
Angl := RandomRange(0, 10);
WriteLn('Rotating compass to ' + Direc + ' and setting camera angle to ' + IntToStr(Angl));
SetScreenMouse(Direc, Angl);
end.




~Home & euphemism

Nava2

2011-10-27 12:06:27

Home!

Very nice, perhaps removing the use of "magic numbers" would be a nice step. I really like this and would love to place it into SRL. :)

(If you don't know what magic numbers are, they're just the use of straight numbers instead of constants or variables. Thus you don' knwo what they represent, and they are magic. :P)

Home

2011-10-27 12:10:58

From: Nava2
Home!

Very nice, perhaps removing the use of "magic numbers" would be a nice step. I really like this and would love to place it into SRL. :)

(If you don't know what magic numbers are, they're just the use of straight numbers instead of constants or variables. Thus you don' knwo what they represent, and they are magic. :P)


Hey. Thanks. I'v added Magic numbers in to Main post.

~Home

Nava2

2011-10-27 12:13:32

From: Home
Hey. Thanks. I'v added Magic numbers in to Main post.

~Home


You have the idea, now make them into constants and put them into the code ;). The derivations of them is also excellent. This helps to update down the road if necessary.

Flight

2011-10-27 12:14:05

And that feature was only just recently added in Runescape. You're definitely the first to simulate it via a script. Very well done, have some rep!

Jakkle

2011-10-27 12:49:42

Very nice :thumbsup:

Heavenzeyez1

2011-10-27 14:20:38

Good job, friend! :)

~Eerik.

DeSnob

2011-10-27 22:12:05

Wow looks complicated! How'd you do it?

Narcle

2011-10-27 22:27:00

Nice! Did you debug to figure out the pixel to degree ratio? Actually guess that would be easy with compass measurements.

Home

2011-10-28 07:41:03

From: Narcle
Nice! Did you debug to figure out the pixel to degree ratio? Actually guess that would be easy with compass measurements.


Yep. I did loads of useless stuff at start :)

There can be probaly some tweaking done. But it does work, and it's fast.


P.S If you find something odd, please tell me.


Edit: Tweaked code a bit. Made it look cleaner, removed useless begin / end;

Added Const's.

~Home

Nava2

2011-10-28 08:34:34

Oh, and I'm being knit-picky because I know you can do better and I want you to do your best. :)

Narcle

2011-10-28 11:10:14

Can the middle mouse be used to rotate up/down? That would be sweet. (can't check atm, at work xD)

euphemism

2011-10-28 11:45:27

From: Narcle
Can the middle mouse be used to rotate up/down? That would be sweet. (can't check atm, at work xD)


Yes, I am adding that functionality to this. Only, Home, it is still exhibiting incorrect direction setting?

Home

2011-10-28 12:46:03

From: euphemism
Yes, I am adding that functionality to this. Only, Home, it is still exhibiting incorrect direction setting?


Really. With what Angle?

Edit: Yes it was a Little bit with a Very rare sitations ;) And I found out the bug!
Ohh and shortening code with about -30 %
And it will be even more accurate.

Thanks Nava2 for kicking my ass ;)




~Home

euphemism

2011-10-28 13:06:52

From: Home
Really. With what Angle?

Edit: Yes it was a Little bit with a Very rare sitations ;) And I found out the bug!
Ohh and shortening code with about -30 %
And it will be even more accurate.

Thanks Nava2 for kicking my ass ;)




~Home


Home, depending on where the compass was at the start, when I had it set the angle to, say, 355 it instead set it to 185. So, the correct angle, but mirrored across x-axis? It did weird things with other angles occasionally.

Home

2011-10-30 12:10:33

Final version is there.

Changes: Fixed annoying bug where the angle would go off (Rare situations) Shotened code something like 20 %. Removed useless variables. Changed const numbers to extremely accurate. Optimized in everyway.

Note: I have tested it a lot. I mean A LOT!

Have fun. Add it or leave it.

~Home

Narcle

2011-10-30 17:37:42

If the vertical angle could be added to this, so that it could rotate right/left and move up/down at same time, this would be invaluable at saving time.

euphemism

2011-10-30 19:34:35

From: Narcle
If the vertical angle could be added to this, so that it could rotate right/left and move up/down at same time, this would be invaluable at saving time.


I have already added this. Home and I are collaborating on this function. We just have to do some cleanup and testing, and it should be done.

Home

2011-11-01 12:40:03

Uploaded the most final version.

This works extremely well. Read Main post for more information. More information how everything works will be added later.

~Home

euphemism

2011-11-01 13:28:47

Okay, explanation:

This function sets the compass direction and camera angle by dragging the mouse while holding down the middle mouse button.

[Background]

Movement across the X-axis results in compass change, and movement across the Y-axis results in change of camera angle.

If one swipes from the very left or right of the screen, to the very edge of the other side, the compass swings 180 degrees.

Armed with this knowledge, we can divide that pixel distance (the 'X_Axis' constant) by 180 to find out how far to move along the X-axis, in pixels, to change one degree. This is where the 'DegToPx' constant to come from.

Camera angle is a bit different. The Y-axis change from the top of the screen to the bottom is, if I recall, 333 pixels. If the camera is at either extreme (all the way up, all the way down), and you swipe from the very top or bottom to the other side, you will find that the camera reaches the other extreme after roughly 278 pixels of change, meaning there is a further distance of 55 pixels to cover before reaching the other side of the screen. This means that there is a dead space where camera angle does not change.

Armed with this information, we can divide 278 by however many steps we want available to get from one camera extreme to the other. This is how the variable 'AngleStep' is determined within the function. The variable 'AngleStep' is how far to move along the Y-axis, in pixels, to change the camera angle one camera step. Home and I decided on 10.

[/Background]

The function first gets the compass direction in degrees, and checks to see if either of the parameters have been set to an empty string (''). If the direction is within 5.0 degrees of the direction set by the user, and CameraAngle is '', then it exits. Otherwise, if it is within 5.0 degrees, or is '', and the CameraAngle is a valid number, then the function knows that it is not changing the direction.

If setting the compass direction, the function sees what the shortest way to get to the specified compass direction is and decides to swipe left-to-right, or right-to-left accordingly. It then determines the distance to change across the X-axis to get to the specified compass direction. If setting camera angle, the function decides to start somewhere randomly within 55 pixels of the top or bottom of the mainscreen.

The function takes two mouse swipes to complete. The function takes the determined X change, and splits it randomly into two values: 'XChange1' and 'XChange2'. The first swipe's X change is equal to XChange1', and the Y change is that 278 pixels required to get to one of the camera extremes (It has to do this because we are not able to determine the camera angle). The second swipe's X change is equal to 'XChange2', and the function calculates the Y change required to get to the specified camera angle, and moves that distance across the Y-axis.

Once the function has set the compass direction and camera angle, it then goes back and checks to see if the compass direction is within 5.0 degrees of the specified direction. If not, it performs a third swipe across the X-axis to correct the compass direction.

——–
E: It didn't keep my formatting. Sorry for readability issues, everybody.

Home

2011-11-01 14:50:57

Added background information to first post. Thanks Euph.

~Home

Coh3n

2011-11-26 02:42:36

Wow I can't believe I just saw this now! I can't commit it now, but I will after my road trip this weekend. I'm definitely going to add this to MSI. :)

euphemism

2011-11-26 03:35:30

From: Coh3n
Wow I can't believe I just saw this now! I can't commit it now, but I will after my road trip this weekend. I'm definitely going to add this to MSI. :)


Yes, it kind of got buried. We would rather it made its way into SRL for everyone to use. You do need to change


if MouseSpeed > 14 then
begin
TempMouse := MouseSpeed
MouseSpeed := RandomRange(14, 15);
end;
to

TempMouse := MouseSpeed;

if MouseSpeed > 14 then
MouseSpeed := RandomRange(14, 15);

If you are adding it anywhere, I would suggest waiting until ReadySteadyGo's compass fix is committed.

Never mind. Do try it out though.

Yago

2011-11-26 17:47:37

Its a bit buggy maybe cause of the old getCompassAngle… but I noticed it was buggy when you kept on setting the same angle and camera height.

euphemism

2011-11-28 01:48:54

From: Yago
Its a bit buggy maybe cause of the old getCompassAngle… but I noticed it was buggy when you kept on setting the same angle and camera height.


Buggy in what way? In regards to setting the same angle and camera height.

Home

2011-11-28 03:16:42

From: euphemism
Buggy in what way? In regards to setting the same angle and camera height.


It should not be buggy at all :S Very odd.


Mabey new GetCompass causes this?


~Home

Yago

2011-11-28 17:13:00

From: euphemism
Buggy in what way? In regards to setting the same angle and camera height.


Yes I have a script which tried this and it would constantly set the same angle/height but would be buggy even though it shouldn't have technically not moved… And when it moved it was extremely bot-like.

Coh3n

2011-12-09 18:36:01

So what's the deal with this? It is working/not working? Is it buggy? I'd like to get this into SRL. :)

euphemism

2011-12-09 19:31:05

From: Coh3n
So what's the deal with this? It is working/not working? Is it buggy? I'd like to get this into SRL. :)



I haven't experienced any problems. Coh3n, copy the test script from the main post and try it out, but make sure you change the below code:

From: euphemism
Yes, it kind of got buried. We would rather it made its way into SRL for everyone to use. You do need to change


if MouseSpeed > 14 then
begin
TempMouse := MouseSpeed
MouseSpeed := RandomRange(14, 15);
end;
to

TempMouse := MouseSpeed;

if MouseSpeed > 14 then
MouseSpeed := RandomRange(14, 15);

If you are adding it anywhere, I would suggest waiting until ReadySteadyGo's compass fix is committed.

Never mind. Do try it out though.

Coh3n

2011-12-12 19:18:57

Great, I'll try it out and if everything goes well, commit it when I can.

E: Added to SRL5.

Narcle

2011-12-12 19:49:16

I believe the max angle has changed recently? I had to tweak some functions in scripts to combat this.

euphemism

2011-12-12 19:52:34

From: Narcle
I believe the max angle has changed recently? I had to tweak some functions in scripts to combat this.


Oh? Could you elaborate?

Narcle

2011-12-12 19:54:15

From: euphemism
Oh? Could you elaborate?


The max angle you can view has changed I believe. You can't really see it via eyeing it, but with functions I noticed it.

euphemism

2011-12-12 19:55:41

Hmm. I will test this out then. It might need fixing.

Coh3n

2011-12-12 20:05:32

I tested before I committed and it worked well. :) If it needs some tweaking, it's no problem.

Gushers

2011-12-16 00:42:47

Hey man I added it into my script and all it did was move really fast and say this:
Rotating compass to 201 and setting camera angle to 8
Successfully executed.

Any help?

euphemism

2011-12-16 01:56:53

From: Gushers
Hey man I added it into my script and all it did was move really fast and say this:
Rotating compass to 201 and setting camera angle to 8
Successfully executed.

Any help?


Erm, you copied the code out of the first post? That is a complete test script to test out the function. You need to copy out the constants, and then just the function. You also need to change the lines mentioned in one of my previous posts.