burst fire script - need help to improve

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

burst fire script - need help to improve

Postby umpmanjoe » Thu Sep 07, 2017 4:19 am

I've been teaching my self how to write scripts and made this burst fire mod. It works well but I need help with an improvement. The script allows you to specify how long the gun will fire and how long the delay is between bursts. The problem is I don't know how to make it stop firing if I let go of the trigger. Currently, if I let go of the trigger while it is in the middle of a burst, the gun will keep firing until it completes it. Any help would be appreciated.

Code: Select all
#pragma METAINFO("Burst Fire", 1, 0, "")
 
/*******************************************************************************
<cfgdesc>
 
 
 
 
[Burst Fire]
color       = #000060
shortdesc   = <<<MULTILINE
 
MULTILINE
collapsible = 2
 
[Burst Fire]
byteoffset    = 8
bitsize        = 1
bitoffset    = 0
control        = checkbox
default        = 0
item            = Enable "Burst Fire"
 
[Burst Adjustment]
group       = true
shortdesc    = <br><b><font color="blue">Length of Burst Adjustment (in milliseconds):</font></b>
byteoffset    = 18
bitsize        = 16
control        = spinbox
default        = 400
minimum        = 100
maximum        = 2000
step        = 1
 
[No fire Adjustment]
group       = true
shortdesc    = <br><b><font color="blue">Time Between Bursts (in milliseconds):</font></b>
byteoffset    = 16
bitsize        = 16
control        = spinbox
default        = 100
minimum        = 250
maximum        = 1000
step        = 1
 
 
</cfgdesc>
 
/*************************************************************************
                         *
**************************************************************************/

 
bool burst_fire;
uint16 burst_lengthAdjustment;
uint16 burstDelay;
init {
    pmem_load();
    burst_fire = pmem_read(8);
    pmem_read(18, &burst_lengthAdjustment);
    pmem_read(16, &burstDelay);
}
 
main {
        ColorLED('P');
//burst fire
 
        if(get_val(BUTTON_5) && event_active(BUTTON_11)) {
        if(burst_fire == 0) {
        burst_fire = 1;
        combo_run(RUMBLE_ON);
        combo_run(flash_on);
    }else if(burst_fire == 1) {
        burst_fire = 0;
        combo_run(RUMBLE_ON);
        combo_run(flash_off);
    }
    }
    if(burst_fire == 1) {
        if(get_val(BUTTON_5)) {
                combo_run(BURST);
            }
        }
}
 
combo BURST {
set_val(BUTTON_5, 100);
wait(burst_lengthAdjustment);
set_val(BUTTON_5, 0);
wait(burstDelay);
}
 
combo RUMBLE_ON {
    ffb_set(FFB_1, 66.0, 320);
    wait(0); wait(320);
}
 
combo flash_on {
    ColorLED('G'); wait(400);
    ColorLED('N'); wait(400);
    ColorLED('G'); wait(400);
    ColorLED('N'); wait(400);
    ColorLED('G'); wait(400);
}
 
combo flash_off {
    ColorLED('R'); wait(400);
    ColorLED('N'); wait(400);
    ColorLED('R'); wait(400);
    ColorLED('N'); wait(400);
    ColorLED('R'); wait(400);   
}
 
// Sets the LED color.  Definable in the declarations at the top of the script.
void ColorLED(char Color) {
    fix32 Color1, Color2, Color3, Color4;
 
    if(Color == 'B'){Color1 = 100.0;    Color2 = 0.00;    Color3 = 0.00;    Color4 = 0.00;}
    if(Color == 'R'){Color1 = 0.00;    Color2 = 100.0;    Color3 = 0.00;    Color4 = 0.00;}
    if(Color == 'G'){Color1 = 0.00;    Color2 = 0.00;    Color3 = 100.0;    Color4 = 0.00;}
    if(Color == 'P'){Color1 = 0.00;    Color2 = 0.00;    Color3 = 0.00;    Color4 = 100.0;}
    if(Color == 'C'){Color1 = 100.0;    Color2 = 0.00;    Color3 = 100.0;    Color4 = 0.00;}
    if(Color == 'A'){Color1 = 0.00;    Color2 = 100.0;    Color3 = 100.0;    Color4 = 0.00;}
    if(Color == 'W'){Color1 = 100.0;    Color2 = 100.0;    Color3 = 100.0;    Color4 = 100.0;}
    if(Color == 'N'){Color1 = 0.00;    Color2 = 0.00;    Color3 = 0.00;    Color4 = 0.00;}
 
    led_set(LED_1, Color1, 0);
    led_set(LED_2, Color2, 0);
    led_set(LED_3, Color3, 0);
    led_set(LED_4, Color4, 0);
 
 
    return;
}
Trying to teach myself how to program these scripts even though I'm old
User avatar
umpmanjoe
First Sergeant
First Sergeant
 
Posts: 45
Joined: Mon May 25, 2015 1:23 am

Re: burst fire script - need help to improve

Postby Scachi » Thu Sep 07, 2017 8:37 am

Hello,

combo_stop is the function you'll need.
To run something on release of the trigger you'll use
Code: Select all
if (event_release(button_nr)) {


Calling ColorLED all the time may get you into trouble, make sure you only call this function when the color should change.
Another option is to check if the color to set is the color it was set last call already, using a static variable in your ColorLED function to remember the last value.
Code: Select all
static char LastColor = 'Z';
 
    if ( Color == LastColor ) return;
    LastColor = Color;


I've added it to your script:
Code: Select all
#pragma METAINFO("Burst Fire", 1, 0, "")
 
/*******************************************************************************
<cfgdesc>
 
 
 
 
[Burst Fire]
color       = #000060
shortdesc   = <<<MULTILINE
 
MULTILINE
collapsible = 2
 
[Burst Fire]
byteoffset    = 8
bitsize        = 1
bitoffset    = 0
control        = checkbox
default        = 0
item            = Enable "Burst Fire"
 
[Burst Adjustment]
group       = true
shortdesc    = <br><b><font color="blue">Length of Burst Adjustment (in milliseconds):</font></b>
byteoffset    = 18
bitsize        = 16
control        = spinbox
default        = 400
minimum        = 100
maximum        = 2000
step        = 1
 
[No fire Adjustment]
group       = true
shortdesc    = <br><b><font color="blue">Time Between Bursts (in milliseconds):</font></b>
byteoffset    = 16
bitsize        = 16
control        = spinbox
default        = 100
minimum        = 250
maximum        = 1000
step        = 1
 
 
</cfgdesc>
 
/*************************************************************************
                         *
**************************************************************************/

 
bool burst_fire;
uint16 burst_lengthAdjustment;
uint16 burstDelay;
init {
    pmem_load();
    burst_fire = pmem_read(8);
    pmem_read(18, &burst_lengthAdjustment);
    pmem_read(16, &burstDelay);
}
 
main {
        ColorLED('P');
//burst fire
 
        if(get_val(BUTTON_5) && event_active(BUTTON_11)) {
        if(burst_fire == 0) {
        burst_fire = 1;
        combo_run(RUMBLE_ON);
        combo_run(flash_on);
    }else if(burst_fire == 1) {
        burst_fire = 0;
        combo_run(RUMBLE_ON);
        combo_run(flash_off);
    }
    }
    if(burst_fire == 1) {
        if(get_val(BUTTON_5)) {
                combo_run(BURST);
            }
          if (event_release(BUTTON_5)) combo_stop(BURST);
        }
}
 
combo BURST {
set_val(BUTTON_5, 100);
wait(burst_lengthAdjustment);
set_val(BUTTON_5, 0);
wait(burstDelay);
}
 
combo RUMBLE_ON {
    ffb_set(FFB_1, 66.0, 320);
    wait(0); wait(320);
}
 
combo flash_on {
    ColorLED('G'); wait(400);
    ColorLED('N'); wait(400);
    ColorLED('G'); wait(400);
    ColorLED('N'); wait(400);
    ColorLED('G'); wait(400);
}
 
combo flash_off {
    ColorLED('R'); wait(400);
    ColorLED('N'); wait(400);
    ColorLED('R'); wait(400);
    ColorLED('N'); wait(400);
    ColorLED('R'); wait(400);   
}
 
// Sets the LED color.  Definable in the declarations at the top of the script.
void ColorLED(char Color) {
    static char LastColor = 'Z';
 
    if ( Color == LastColor ) return;
    LastColor = Color;
 
    fix32 Color1, Color2, Color3, Color4;
 
    if(Color == 'B'){Color1 = 100.0;    Color2 = 0.00;    Color3 = 0.00;    Color4 = 0.00;}
    if(Color == 'R'){Color1 = 0.00;    Color2 = 100.0;    Color3 = 0.00;    Color4 = 0.00;}
    if(Color == 'G'){Color1 = 0.00;    Color2 = 0.00;    Color3 = 100.0;    Color4 = 0.00;}
    if(Color == 'P'){Color1 = 0.00;    Color2 = 0.00;    Color3 = 0.00;    Color4 = 100.0;}
    if(Color == 'C'){Color1 = 100.0;    Color2 = 0.00;    Color3 = 100.0;    Color4 = 0.00;}
    if(Color == 'A'){Color1 = 0.00;    Color2 = 100.0;    Color3 = 100.0;    Color4 = 0.00;}
    if(Color == 'W'){Color1 = 100.0;    Color2 = 100.0;    Color3 = 100.0;    Color4 = 100.0;}
    if(Color == 'N'){Color1 = 0.00;    Color2 = 0.00;    Color3 = 0.00;    Color4 = 0.00;}
 
    led_set(LED_1, Color1, 0);
    led_set(LED_2, Color2, 0);
    led_set(LED_3, Color3, 0);
    led_set(LED_4, Color4, 0);
 
 
    return;
}
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: burst fire script - need help to improve

Postby antithesis » Thu Sep 07, 2017 10:34 am

Tie it to event_release and/or to reload. I do that to reset burst hold and wait values back to 0 so I can manually override burst-fire settings.

It'd look something like

Code: Select all
 
if (event_release (BUTTON_5) || event_active (BUTTON_17)) { combo_stop (BURST); burstDelay = 0; }
 

The reason for the burstDelay reset is to avoid a wait delay before initiating a new burst.
Official Australian retailer for Titan One, Titan Two and XIM APEX at Mod Squad
User avatar
antithesis
Colonel
Colonel
 
Posts: 1912
Joined: Sat May 28, 2016 10:45 pm


Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 250 guests