HID Scripting help needed

GPC2 script programming for Titan Two. Code examples, questions, requests.

Re: HID Scripting help needed

Postby Scachi » Mon Feb 08, 2021 8:37 am

Please remove my name as an author as your changes may break my script yours is based on and I don't want to answer questions about your script when anyone is using that.

Using HID and altering mouse output is not very easy to do.
Most important thing: don't use "mouse_passthru()" when there is no MREPORT_UPDATED reported, you have added one mouse_passthru to your script that causes unwanted mouse movement (ghost movement, movement in the last direction of mouse input).
Only use mouse_passthru in that combination like my original script is doing:
Code: Select all
if(mouse_status(MREPORT_UPDATED)) {
mouse_passthru();
}

Using mouse_passthru without MREPORT_UPDATED will reuse the last received values via that passthru.
moving the mouse does cause a "MREPORT_UPDATED", not moving your mouse or not changing any mouse_buttons manually will NOT trigger a new "MREPORT_UPDATED" so your last input is still stored and forwarded when calling mouse_passthru() and you end up in mouse movement you don't want to have when still calling mouse_passthru.

Do not save custom header files in your gtuner include directory.
Put a copy of the custom .gph header files into the same directory as your own script and use "" surrounding the name of the file instead of <>
Example: #include "xkeys.gph"
This will cause gtuner IV to search in your scripts directory for that header file.
Not adding custom .gph files to your gtuner include sub directory will keep your gtuner iv install clean and makes sure that your script will compile on another computer too with a fresh install of gtuner as long as you use your whole script directoy on your new computer then. You don't need to mess around with the gtuner install/directory tree.

Your secondary anti recoil can be merged into the existing anti recoil.
Not sure if you really want a second timer or just using or resetting the timer when pressing Y key will do the trick, depends on how you want it to work.
Example:
Code: Select all
#pragma METAINFO("Mouse Amax", 1, 0, "")
#include <keyboard.gph>
#include <mouse.gph>
#include <ps4.gph>
#define XKEYS_TO_MONITOR_MAX 5 // change 2 to the maximum number of keys you want to keep track of
#include "xkeys.gph"
#include "mouse_events.gph"
#include "key_events.gph"
 
// configuration
// advanced configuration
const int MOUSE_SPEED = 10; // <- should be around 10 or higher, a value too low can stop the mouse from working
 
// internal
fix32 antirecoil_X;
fix32 antirecoil_Y;
uint32 ARTIMER;
int32 X, Y, WHEEL;
int32 RX, RY;
fix32 ARX, ARY;
 
 
// mouse press time tracker for staged anti recoil
uint32 mb1timer;
 
uint8 KEY1, KEY2; // or any other name, this will get an index number assigned by the xkeys_add function
 
bool bStickForw = FALSE;
 
 
init {
    port_connect(PORT_USB_C, PROTOCOL_HID);
    keymapping();
    mousemapping();
      // use xkeys_add to add each key to monitor, it returns an uint8 number that has to be used with the functions
  KEY1=xkeys_add(KEY_1); // add key KEY_A to monitoring
  KEY2=xkeys_add(KEY_F); // add key KEY_S to monitoring
}
 
main {
    key_passthru();
    //mouse_passthru(); <--- DO NOT USE without mouse_status(MREPORT_UPDATED)
 
    X = 0; Y = 0; WHEEL = 0;
    RX = 0; RY = 0;
 
    if(mouse_status(MREPORT_UPDATED)) {
        mouse_passthru(); // only passthru when there is data to send
        X = mouse_status(MOUSE_X);
        Y = mouse_status(MOUSE_Y);
        WHEEL = mouse_status(MOUSE_WHEEL);
    }
 
    if(mouse_status(MBUTTON_1) & mouse_status(MBUTTON_2))
    {
        ARTIMER += elapsed_time(); // mouse speed timer
 
 
        mb1timer += elapsed_time(); // mouse button press timer
 
        // ADJUST VALUES & TIME and add more here as required
 
        if (!key_status(KEY_Y)) { //PRIMARY RECOIL
            if (mb1timer < 290) { antirecoil_X = 0.8; antirecoil_Y = 2.0; }
            else if (mb1timer <  830) { antirecoil_X =  -0.5; antirecoil_Y = 2.0; }
            else if (mb1timer < 1200) { antirecoil_X = -1.1; antirecoil_Y = 1.7; }
            else if (mb1timer < 2400) { antirecoil_X = -1.0; antirecoil_Y = 2.0; }
            else if (mb1timer < 3000) { antirecoil_X = 0.0; antirecoil_Y = 0.0; }
            else if (mb1timer < 6000) { antirecoil_X = 0.0; antirecoil_Y = 0.0; }
        } else { // SECONDARY RECOIL
            if (key_event_active(KEY_Y)) mb1timer=0; // remove if you don't want to reset the timer when KEY_Y is getting activated
            if (mb1timer < 290) { antirecoil_X = 0.0; antirecoil_Y = 0.0; }
            else if (mb1timer <  830) { antirecoil_X =  0.0; antirecoil_Y = 0.0; }
            else if (mb1timer < 1200) { antirecoil_X =  0.0; antirecoil_Y = 0.0; }
            else if (mb1timer < 2400) { antirecoil_X =  0.0; antirecoil_Y = 0.0; }
            else if (mb1timer < 3000) { antirecoil_X =  0.0; antirecoil_Y = 0.0; }
            else if (mb1timer < 6000) { antirecoil_X =  0.0; antirecoil_Y = 0.0; }
        }
 
        if(!(ARTIMER%MOUSE_SPEED)) {
 
            ARX += (fix32)(antirecoil_X);
            ARY += (fix32)(antirecoil_Y);
 
            if (antirecoil_X < 0.0) RX += (int)ceil((ARX));
            else if (antirecoil_X > 0.0) RX += (int)floor((ARX));
 
            if (antirecoil_Y < 0.0) RY += (int)ceil((ARY));
            else if (antirecoil_Y > 0.0) RY += (int)floor((ARY));
 
            mouse_set(MOUSE_X, X+RX);
            mouse_set(MOUSE_Y, Y+RY);
            mouse_set(MOUSE_WHEEL,WHEEL);
 
            ARX = mod(ARX,1.0);
            ARY = mod(ARY,1.0);
        }
    } else {
        ARTIMER = MOUSE_SPEED; // reset mouse speed timer
        ARX = 0.0; ARY = 0.0;
        mb1timer = 0; // reset mouse button press timer
    }
 
    //PING
    if(mouse_event_active(MBUTTON_1) & mouse_status(MBUTTON_2)) {
        //mouse_set(MBUTTON_1, 100);
        combo_run(ping);}
 
    //HOLD BREATH
    if(mouse_event_active(MBUTTON_2)){
        combo_run(HB);}
 
    //DUAL RAPID FIRE   
    if(key_status(KEY_Y) & (mouse_status(MBUTTON_1))) {
        combo_stop(TS);
        combo_run(DRF);}
 
    //SLIDE CANCEL   
    if(key_status(KEY_U) ) {
        combo_run(SC);}
 
 
 
    //    RELOAD & ACTION
    if (xkeys_is_active(KEY2) && xkeys_time_active(KEY2) > 100) {
        key_set(KEY_R, 100);}
    //Switch weapon does slide cancel
    if (xkeys_is_active(KEY1) && (get_actual(STICK_2_Y) < -98.0) && xkeys_time_active(KEY1) > 300) {
        combo_run(SC);}
 
    //Switch weapon does armour up   
    if (xkeys_is_active(KEY1) && xkeys_time_active(KEY1) > 200) {
        key_set(KEY_1, 0);
        combo_run(AU);}
 
 
// REMAPING
 
    // STICK FORWARD
    if(get_actual(STICK_2_Y) < -40.0) {
        key_set(KEY_W, 100);}
 
    if(get_actual(STICK_2_Y) < -98.0) {
        combo_run(TS);}
 
    // STICK BACKWARD   
    if(get_actual(STICK_2_Y) > 50.0) {
        key_set(KEY_S, 100);}
    // STICK RIGHT
    if(get_actual(STICK_2_X) > 50.0) {
        key_set(KEY_D, 100);}
    // STICK LEFT
    if(get_actual(STICK_2_X) < -50.0) {
        key_set(KEY_A, 100);}
 
    // PS3 NAV L2   
    if(get_val(BUTTON_8)) {
        key_set(KEY_SPACEBAR, 100);}
 
    // PS3 NAV L1   
    if(get_val(BUTTON_7)) {
        key_set(KEY_C, 100);}
 
    // PS3 NAV L3   
    if(get_val(BUTTON_9)) {
        key_set(KEY_LEFTSHIFT, 100);}
 
    // PS3 NAV CIRCLE   
    if(get_val(BUTTON_15)) {
        key_set(KEY_ESCAPE, 100);}
 
    // PS3 NAV CROSS   
    if(get_val(BUTTON_16)) {
        key_set(KEY_RETURNORENTER, 100);}
 
    // PS3 NAV PS BUTTON   
    if(get_val(BUTTON_1)) {
        key_set(KEY_M, 100);}
 
    // PS3 NAV DOWN   
    if(get_val(BUTTON_11)) {
        key_set(KEY_L, 100);}
 
    // PS3 NAV UP   
    if(get_val(BUTTON_10)) {
        key_set(KEY_LEFTALT, 100);}
 
    // PS3 NAV RIGHT   
    if(get_val(BUTTON_13)) {
        key_set(KEY_3, 100);}
 
    // PS3 NAV LEFT   
    if(get_val(BUTTON_12)) {
        key_set(KEY_M, 0);}
 
 
 
 
}
 
combo ping {
    wait(50);
    key_set(KEY_LEFTALT, 100);
    wait(30);
    key_set(KEY_LEFTALT, 0);
    wait(80);
    key_set(KEY_LEFTALT, 100);
    wait(30);
    key_set(KEY_LEFTALT, 0);
    wait(300);
}
 
 
combo TS {
 
    key_set(KEY_LEFTSHIFT, 100);
    wait(40);
    key_set(KEY_LEFTSHIFT, 0);
    wait(40);
    key_set(KEY_LEFTSHIFT, 100);
    wait(40);
    key_set(KEY_LEFTSHIFT, 0);
    wait(40);
}
 
combo HB {
    wait(100);
    key_set(KEY_LEFTSHIFT, 100);
    wait(5000);
}
 
combo AU {
    key_set(KEY_4, 100);
    wait(100);
}
 
combo DRF {
    mouse_set(MBUTTON_1, 100);
    mouse_set(MBUTTON_2, 100);
    wait(20);
    mouse_set(MBUTTON_1, 0);
    mouse_set(MBUTTON_2, 0);
    wait(20);
}
 
combo SC {
    key_set(KEY_C, 100);
    wait(40);
    key_set(KEY_C, 0);
    wait(200);
    key_set(KEY_C, 100);
    wait(40);
    key_set(KEY_C, 0);
    wait(40);
    key_set(KEY_SPACEBAR, 100);
    wait(40);
    key_set(KEY_SPACEBAR, 0);
    wait(800);
}


I never had issue with manual mouse input causing trouble with anti recoil in usb hid mode.
You had mouse ghost movement because of your add mouse_passthru() directly at the start of the main[ section, perhaps that was causing your issues.
If you really want to stop anti recoil during mouse input you have to add some code yourself that checks the real reported mouse_input and above some value stop applying anti recoil then. Like your other code did but with using the correct functions to read mouse data.
The mouse input is very different to stick input so you may need some custom code that works for you instead of a simple value range check like your stick code did.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: HID Scripting help needed

Postby TT3000 » Tue Feb 09, 2021 1:02 pm

thank you so much for all your help scachi
I didn't mean to upset you with leaving your name in there.
since the codes were all yours I wanted to give credit where it's due if I ever share it online once it's ready.
I still want to make sure others know you helped so what's the best way?
again I really do appreciate your time and patience.
User avatar
TT3000
Master Sergeant
Master Sergeant
 
Posts: 35
Joined: Fri Nov 20, 2020 7:58 pm

Re: HID Scripting help needed

Postby Scachi » Tue Feb 09, 2021 5:21 pm

Its all fine. You didn't do anything wrong. :smile0517:
You can mention me somewhere in your comments or description or something if you want to, just not as the author of your work :wink:
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: HID Scripting help needed

Postby TT3000 » Mon Feb 15, 2021 12:27 pm

understood :)
thank you again for all the help scachi you are an awesome human being :smile0202: .
User avatar
TT3000
Master Sergeant
Master Sergeant
 
Posts: 35
Joined: Fri Nov 20, 2020 7:58 pm

Previous

Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 45 guests