I recently tried writing a bot in python for a unity game to control the character using a virtual mouse like in pyautogui or autohotkey. The game has multiple camera modes. One that uses the cursor location and another that uses GetAxis("Mouse X/Y"). Neither the library I'm using, pyautogui, pydirectinput, or even an autohotkey macro fails to move the mouse when on the option that use GetAxis() although it works on the camera that uses the cursor location. Why is this the case?
Example of a script that fails when using GetAxis():
Yoinked from another stack overflow post since I needed an ahk example as well:
CoordMode, mouse, screen
toggle := 0, fixedY := A_ScreenHeight/2 ; choose the y you like
F1::
MouseGetPos, MouseX, MouseY
if toggle := !toggle
gosub, MoveTheMouse
else
SetTimer, MoveTheMouse, off
return
MoveTheMouse:
Random, x, 1, % A_ScreenWidth
MouseMove, %x%, %fixedY%, 100
Random, Time, 1000*60, 1000*60*5
SetTimer, MoveTheMouse, -%time% ; every 3 seconds
return
The C# code used for movement is:
case CAMERA_TYPE.CAMERA_THAT_WORKS_WITH_BOT:
if (Input.mousePosition.x < (float)Screen.width * 0.4f)
{
mainT.RotateAround(mainT.position, Vector3.up, -(((float)Screen.width * 0.4f - Input.mousePosition.x) / (float)Screen.width * 0.4f) * getSensitivityMultiWithDeltaTime() * 150f);
}
else if (Input.mousePosition.x > (float)Screen.width * 0.6f)
{
mainT.RotateAround(mainT.position, Vector3.up, (Input.mousePosition.x - (float)Screen.width * 0.6f) / (float)Screen.width * 0.4f * getSensitivityMultiWithDeltaTime() * 150f);
}
mainT.rotation = Quaternion.Euler(140f * ((float)Screen.height * 0.6f - Input.mousePosition.y) / (float)Screen.height * 0.5f, mainT.rotation.eulerAngles.y, mainT.rotation.eulerAngles.z);
mainT.position -= mainT.forward * this.distance * this.distanceMulti * this.distanceOffsetMulti;
break;
case CAMERA_TYPE.CAMERA_THAT_FAILS_WITH_BOT:
if (!CustomInputs.Inputs.menuOn)
{
Screen.lockCursor = true;
}
float num5 =0f;
float num6 =0f;
if (((int)GameManager.settings[300]) == 0)
{
num5 = (Input.GetAxis("Mouse X") * 10f) * this.getSensitivityMulti();
num6 = ((-Input.GetAxis("Mouse Y") * 10f) * this.getSensitivityMulti()) * this.getReverse();
}
else if (((int)GameManager.settings[300]) == 1)
{
num5 = (Input.GetAxisRaw("Mouse X") * 10f) * this.getSensitivityMulti();
num6 = ((-Input.GetAxisRaw("Mouse Y") * 10f) * this.getSensitivityMulti()) * this.getReverse();
}
mainT.RotateAround(mainT.position, Vector3.up, num5);
float num7 = mainT.rotation.eulerAngles.x % 360f;
float num8 = num7 + num6;
if (((num6 <= 0f) || (((num7 >= 260f) || (num8 <= 260f)) && ((num7 >= 80f) || (num8 <= 80f)))) && ((num6 >= 0f) || (((num7 <= 280f) || (num8 >= 280f)) && ((num7 <= 100f) || (num8 >= 100f)))))
{
mainT.RotateAround(mainT.position, mainT.right, num6);
}
mainT.position -= (Vector3)(((mainT.forward * this.distance) * this.distanceMulti) * this.distanceOffsetMulti);
break;
Some of this code is incomprehensible due to being decompiled. Also a note, don't worry about modding here as the game has basically been abandoned by the dev for 4 years and is now running on community servers.
After some further searching I found that GetAxis may use the mouse velocity/acceleration and I don't think these mouse move functions affect that in any way. In my use case what I would need for testing is a way to read the mouse acceleration/velocity and a way to move set my mouse's acceleration/velocity. Please let me know if I have the right idea here and any references to material related to this would be greatly appreciated.
The fix I found was to use a windows dll call:
AHK:
DllCall("mouse_event", "UInt", 0x01, "UInt", 100, "UInt", 100)
python:
import ctypes
ctypes.windll.user32.mouse_event(0x01, x, y, 0, 0)
Related
I have this plot:
WMA_point = wma(close, 9)
plot(WMA_point, title='WMA', color=#000000)
It makes a line on the chart. When the price goes up, the line moves up, and vice versa, when the price goes down, it moves down.
I know that it depends on the "close" data.
I would like to find the price data of this wma when (close == open)
EDIT:
I mean: I want to get the first data of the close line wma(close, 9), when the new candle appears, when the (close == open) . I dont need the data of the open line wma(open, 9)
You can either use WMA_point[1], which is generally where the price value of the current bar will open at. If you want to take into consideration gapping, you'll need to calculate the wma using n-1 historical close values and with the nth value the current bar's open.
//#version=5
indicator("wma open", overlay = true)
len = input.int(10)
float wtd_close_sum = 0.0
int denom = 0
for i = 1 to len - 1
wt = len - i
wtd_close_sum += close[i] * wt
denom += wt
wtd_close_sum += open * len
denom += len
wma_open = wtd_close_sum / denom
plot(wma_open)
as a function :
f_wma_open(_open, _close, _len) =>
float _wtd_close_sum = 0.0
int _denom = 0
for i = 1 to _len - 1
_wt = _len - i
_wtd_close_sum += _close[i] * _wt
_denom += _wt
_wtd_close_sum += _open * _len
_denom += _len
_wma_open = _wtd_close_sum / _denom
_wma_open
Try using open as your source:
WMA_point = wma(open, 9)
plot(WMA_point, title='WMA', color=#000000)
Cheers, and best of luck with your trading and coding
So, you need to store the price in a var when your condition is true.
var float p = na
WMA_point = wma(close, 9)
if (open == close)
p := WMA_point // This will have the wma value of the last time open == close
plot(WMA_point, title='WMA', color=#000000)
I have the plot:
//#version=4
study(title="Line", shorttitle="Line", overlay=true)
theline(src, len) => wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))
Line = theline(close, 9)
plot(Line, title='Line', color=#0066ff, linewidth=3)
That line will move up or down depending on the close value.
How do I find the first value of that line, when the new bar appears, when the first close==open.
I need that value to compare, to see if the current line is above or under that first value.
Thank you for helping me.
The answer is the same principle as your last question, you have to refactor the wma equation in order to obtain the sum of the first n-1 values as normal and replace the nth value (current bar's value) calculated using open instead of close.
//#version=5
indicator("hull open", overlay = true)
len = input.int(9)
f_wma_open(_close, _open, _len) =>
float _wtd_sum = 0.0
int _denom = 0
for i = 1 to _len - 1
_wt = _len - i
_wtd_sum += _close[i] * _wt
_denom += _wt
_wtd_sum += _open * _len
_denom += _len
_wma_open = _wtd_sum / _denom
_wma_open
f_hull_open(_close, _open, _len) =>
_a = 2 * ta.wma(_close, _len / 2) - ta.wma(_close, _len)
_b = 2 * f_wma_open(_close, _open, _len / 2) - f_wma_open(_close, _open, _len)
_slen = math.round(math.sqrt(_len))
float _wtd_sum = 0.0
float _denom = 0.0
for i = 1 to _slen - 1
_wt = _slen - i
_wtd_sum += _a[i] * _wt
_denom += _wt
_wtd_sum += _b * _slen
_denom += _slen
_hull_open = _wtd_sum / _denom
_hull_open
f_hull(_src, _len) =>
ta.wma(2 * ta.wma(_src, _len / 2) - ta.wma(_src, _len), math.round(math.sqrt(_len)))
hull = f_hull(close, len)
hull_open = f_hull_open(close, open, len)
plot(hull, color = color.gray)
plot(hull_open, color = color.yellow)
You can use varip to freeze and hold real time values and update them conditionally. I wrote a custom function for you that will freeze the real time open value of the wma line. Please note this will only work in real time or for alerts. It will only freeze the open if you are watching the open live, and alerts will begin working on the first open after adding to the chart.
//#version=4
study(title="Line", shorttitle="Line", overlay=true)
theline(src, len) => wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))
openVal(src) =>
varip float lineOpen = na
if barstate.isnew
lineOpen := src
result = barstate.islastconfirmedhistory[1] or barstate.isconfirmed ? src : lineOpen
Line = theline(close, 5)
Line2 = openVal(Line)
plot(Line, title='Line', color=#0066ff, linewidth=4)
plot(Line2, title='Alt Line', color=color.white)
cheers and best of luck
I am working with the flutter_sound in Flutter. I simply want to fade in and fade out sound file in flutter
I have created a method that allows you to fade in or fade out.
If you want to fade you must enter this:
3 seconds to increase the volume from 0.0 to 1.0.
Fade in:
fade (1.0, 0.0, 3 * 1000);
Fade out:
fade (0.0, 1.0, 3 * 1000);
If it works for you, don't forget to rate my answer, regards.
void fade( double to, double from, int len ) {
double vol = from;
double diff = to - from;
double steps = (diff / 0.01).abs() ;
int stepLen = Math.max(4, (steps > 0) ? len ~/ steps : len);
int lastTick = DateTime.now().millisecondsSinceEpoch ;
// // Update the volume value on each interval ticks
Timer.periodic(new Duration(milliseconds: stepLen), ( Timer t ) {
var now = DateTime.now().millisecondsSinceEpoch;
var tick = (now - lastTick) / len;
lastTick = now;
vol += diff * tick;
vol = Math.max(0, vol);
vol = Math.min(1, vol);
vol = (vol * 100).round() / 100;
player.setVolume(vol); // change this
if ( (to < from && vol <= to) || (to > from && vol >= to) ) {
if (t != null) {
t.cancel() ;
t = null;
}
player.setVolume(vol); // change this
}
});
}
I am writing the code in Xcode 6 Beta 6 using Swift in SpriteKit. In the code I need a picture to follow the finger when the user moves it. touchesMoved works but with glitches. If I move the finger slowly everything is fine. If I the move the finger fast going right to left then everything is fine. If I move the finger fast going left to right, then the picture follows the finger only for a fraction of a second. If I tap and hold the picture in its current position for about half a second then everything is fine when I move it fast going both from right to left or left to right. In summary I cannot move the picture fast going left to right unless I tap and hold the picture for about half a second. Anybody has a clue why this is happening? Thanks for your time. Below is the code. I am moving SKSPriteNode follow2
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let angle_area_location = touch.locationInNode(self)
if self.nodeAtPoint(angle_area_location) == self.angle_area {
if (angle_area_location.x <= 21) {
angle = 1.55681122463001
distance12 = sqrt((angle_area_location.y - 30) * (angle_area_location.y - 30) + 1)
}
if (angle_area_location.y <= 31) {
angle = 0.0102037274939542
distance12 = sqrt((31 - 30) * (31 - 30) + (angle_area_location.x - 20) * (angle_area_location.x - 20))
}
if (angle_area_location.x > 21) && (angle_area_location.y > 31) {
angle = atan((angle_area_location.y - 30) / (angle_area_location.x - 20))
distance12 = sqrt((angle_area_location.y - 30) * (angle_area_location.y - 30) + (angle_area_location.x - 20) * (angle_area_location.x - 20))
}
if (distance12 <= maxFollow2) && (distance12 >= minFollow2) {
self.cannon.zRotation = angle
self.arc.zRotation = angle
if (angle_area_location.x > 21) || (angle_area_location.y > 31) {
follow2.position = CGPointMake(angle_area_location.x , angle_area_location.y)
}
if(angle_area_location.x <= 21) {
follow2.position = CGPointMake(21 , angle_area_location.y)
}
if (angle_area_location.y <= 31) {
follow2.position = CGPointMake(angle_area_location.x , 31)
}
}
if(distance12 > maxFollow2) {
self.cannon.zRotation = angle
self.arc.zRotation = angle
delta = 290/3
arc.size = CGSizeMake(160 * (1 + delta/20) , 35)
arc.position = CGPointMake(20 - 3 * (delta) * cos(angle) , 30 - 3 * (delta) * sin(angle))
followdist = 360
follow2.position = CGPointMake(angle_area_location.x , angle_area_location.y)
velocity = vmin + (followdist - minFollow2) * (300/(maxFollow2 - minFollow2))
}
if (distance12 < minFollow2) {
self.cannon.zRotation = angle
self.arc.zRotation = angle
arc.size = CGSizeMake(160 , 6.8)
arc.position = CGPointMake(20 , 30)
follow2.position = CGPointMake( minFollow2 * cos(angle) + 20 , minFollow2 * sin(angle) + 30)
followdist = sqrt((follow2.position.y - 30) * (follow2.position.y - 30) + (follow2.position.x - 20) * (follow2.position.x - 20))
velocity = vmin + (followdist - minFollow2) * (300/(maxFollow2 - minFollow2))
}
}
}
}
Ok I figured the glitch. I had a UISwipeGestureRecognizer that calls a method when I right swipe. I deactivated that and everything works fine. I guess swiping right and moving left to right on touchesMoved are interfering with each other.
A common trick when working with distances is to avoid taking the square root and only compare the squared values. This saves quite a bit of processor resources.
Example:
let maxfollow2sqr = maxFollow2 * maxFollow2
distance12 = (angle_area_location.y - 30) * (angle_area_location.y - 30) + (angle_area_location.x - 20) * (angle_area_location.x - 20)
if (distance12 <= maxFollow2sqr) {
// do something here
}
Since all you care about is if the calculated distance is between the min and the max you can deal just with the squares. This may speed up the function quite a bit but there are probably other optimizations that can be done.
I have a 496*O(N^3) loop. I am performing a blocking optimization technique where I'm operating 2 images at a time instead of 1. In raw terms, I am unrolling the outer loop. (The non-unrolled version of the code is as shown below: ) b.t.w I'm using Intel Xeon X5365 machine that has 8 cores and it has 3GHz clock, 1333MHz bus frequency, Shared 8MB L2( 4 MB shared between every 2 core), L1-I 32KB,L1-D 32KB .
for(imageNo =0; imageNo<496;imageNo++){
for (unsigned int k=0; k<256; k++)
{
double z = O_L + (double)k * R_L;
for (unsigned int j=0; j<256; j++)
{
double y = O_L + (double)j * R_L;
for (unsigned int i=0; i<256; i++)
{
double x[1] = {O_L + (double)i * R_L} ;
double w_n = (A_n[2] * x[0] + A_n[5] * y + A_n[8] * z + A_n[11]) ;
double u_n = ((A_n[0] * x[0] + A_n[3] * y + A_n[6] * z + A_n[9] ) / w_n);
double v_n = ((A_n[1] * x[0] + A_n[4] * y + A_n[7] * z + A_n[10]) / w_n);
for(int loop=0; loop<1;loop++)
{
px_x[loop] = (int) floor(u_n);
px_y[loop] = (int) floor(v_n);
alpha[loop] = u_n - px_x[loop] ;
beta[loop] = v_n - px_y[loop] ;
}
if(px_y[0]>=0 && px_y[0]<(int)threadCopy[0].S_y)
{
if (px_x[0]>=0 && px_x[0]<(int)threadCopy[0].S_x )
///////////////////(i,j) pixels ///////////////////////////////
pixel_1[0] = threadCopy[0].I_n[px_y[0] * threadCopy[0].S_x + px_x[0]];
else
pixel_1[0] =0.0;
if (px_x[0]+1>=0 && px_x[0]+1<(int)threadCopy[0].S_x)
/////////////////// (i+1, j) pixels/////////////////////////
pixel_1[2] = threadCopy[0].I_n[px_y[0] * threadCopy[0].S_x + (px_x[0]+1)];
else
pixel_1[2] = 0.0;
}
else{
pixel_1[0] =0.0;
pixel_1[2] =0.0;
}
if( px_y[0]+1>=0 && px_y[0]+1<(int)threadCopy[0].S_y)
{
if (px_x[0]>=0 && px_x[0]<(int)threadCopy[0].S_x)
pixel_1[1] = threadCopy[0].I_n[(px_y[0]+1) * threadCopy[0].S_x + px_x[0]];
else
pixel_1[1] = 0.0;
if (px_x[0]+1>=0 && px_x[0]+1<(int)threadCopy[0].S_x)
pixel_1[3] = threadCopy[0].I_n[(px_y[0]+1) * threadCopy[0].S_x + (px_x[0]+1)];
else
pixel_1[3] = 0.0;
}
else{
pixel_1[1] = 0.0;
pixel_1[3] = 0.0;
}
pix_1 = (1.0 - alpha[0]) * (1.0 - beta[0]) * pixel_1[0] + (1.0 - alpha[0]) * beta[0] * pixel_1[1]
+ alpha[0] * (1.0 - beta[0]) * pixel_1[2] + alpha[0] * beta[0] * pixel_1[3];
f_L[k * L * L + j * L + i] += (float)(1.0 / (w_n * w_n) * pix_1);
}
}
}
I profiled the results using Intel Vtune-2013 (Using binary created from gcc-4.1) and I can see that there is 40% reduction in memory bandwidth usage which was expected because 2 images are being processed for every iteration.(f_L store operation causes 8 bytes of traffic for every voxel). This accounts to 11.7% reduction in bus cycles! Also, since the block size is increased in the inner loop, the resource stalls decrease by 25.5%. These 2 accounts for 18% reduction in response time.
The mystery question is, why are instruction retired increased by 7.9%? (Which accounts for increase in response time by 6.51%) - Possible reason I could this of is:
1. Since the number of branch instructions increase inside the block (and core architecture has 8 bit global history) retired branch instruction increased by 2.5%( Although, mis-prediction remained the same! I know, smells fishy right?!!). But I am still missing answer for the rest 5.4%! Could anyone please shed me light in any direction? I'm completely out of options and No way to think. Thanks a lot!!