######################################################################
###                                                                ###
###                Guns fire only if view is done                  ###
###                                                                ###
### BARANGER Emmanuel aka Heluijah 04/10/2024                      ###
###                                03/01/2025                      ###
###                                04/11/2025 for SM 79            ###
######################################################################

fire_MG = func {
  var CurrentView = getprop("sim/current-view/view-number-raw");
  # Roof
  if ( CurrentView == 104 ) {
    #setprop("/controls/armament/trigger1", 1);
    setprop("sim/multiplay/generic/int[13]", 1);
  # Left
  } elsif ( CurrentView == 103 ) {
    #setprop("/controls/armament/trigger2", 1);
    setprop("sim/multiplay/generic/int[14]", 1);
  # Right
  } elsif ( CurrentView == 102 ) {
    #setprop("/controls/armament/trigger3", 1);
    setprop("sim/multiplay/generic/int[15]", 1);
  # Down
  } elsif ( CurrentView == 101 ) {
    #setprop("/controls/armament/trigger4", 1);
    setprop("sim/multiplay/generic/int[16]", 1);
  # Front
  } elsif ( CurrentView == 0 ) {
    #var FrontGun = getprop("sim/model/show-FrontGun");
    var FrontGun = getprop("sim/multiplay/generic/bool[1]");
    if ( FrontGun == 1 ) {
      #setprop("/controls/armament/trigger0", 1);
      setprop("sim/multiplay/generic/int[10]", 1);
    }
  }
}

stop_MG = func {
  #setprop("/controls/armament/trigger0", 0);
  setprop("sim/multiplay/generic/int[10]", 0);
  #setprop("/controls/armament/trigger1", 0);
  setprop("sim/multiplay/generic/int[13]", 0);
  #setprop("/controls/armament/trigger2", 0);
  setprop("sim/multiplay/generic/int[14]", 0);
  #setprop("/controls/armament/trigger3", 0);
  setprop("sim/multiplay/generic/int[15]", 0);
  #setprop("/controls/armament/trigger4", 0);
  setprop("sim/multiplay/generic/int[16]", 0);
}

#########################################################
################ unlimited ammo #########################
#########################################################

reload_guns  = func {

  var  groundspeed = getprop("velocities/groundspeed-kt");

  # only allow it if on ground or if it's already set to unlimited mode
  # or getprop( r_gun1_ammo_count) == -1 ) {
  if ( groundspeed < 5 ) {

    setprop("sim/ai/submodels/Bomb_100-number", 10);

    setprop("sim/multiplay/generic/int[24]", 0);
    setprop("sim/multiplay/generic/int[25]", 0);
    setprop("sim/multiplay/generic/int[26]", 0);
    setprop("sim/multiplay/generic/int[27]", 0);
    setprop("sim/multiplay/generic/int[28]", 0);
    setprop("sim/multiplay/generic/int[29]", 0);
    setprop("sim/multiplay/generic/int[30]", 0);
    setprop("sim/multiplay/generic/int[31]", 0);
    setprop("sim/multiplay/generic/int[32]", 0);
    setprop("sim/multiplay/generic/int[33]", 0);

    setprop("sim/ai/submodels/Bomb_250-number", 5);

    setprop("sim/multiplay/generic/int[17]", 0);
    setprop("sim/multiplay/generic/int[18]", 0);
    setprop("sim/multiplay/generic/int[19]", 0);
    setprop("sim/multiplay/generic/int[20]", 0);
    setprop("sim/multiplay/generic/int[21]", 0);

    setprop("sim/ai/submodels/Bomb_500-number", 2);

    setprop("sim/multiplay/generic/int[22]", 0);
    setprop("sim/multiplay/generic/int[23]", 0);

    gui.popupTip("Guns reloaded--400 rounds in each gun and bombs.", 5)
  } else {
    gui.popupTip("You must be on the ground and engines dead stopped to re-load guns.",5)
  }
}

#########################################################
################ unlimited ammo #########################
#########################################################
# For testing only, of course!
#

unlimited_guns = func {

  var groundspeed = getprop("velocities/groundspeed-kt");

  setprop("sim/ai/submodels/Bomb_100-number", 10);

  setprop("sim/multiplay/generic/int[24]", 0);
  setprop("sim/multiplay/generic/int[25]", 0);
  setprop("sim/multiplay/generic/int[26]", 0);
  setprop("sim/multiplay/generic/int[27]", 0);
  setprop("sim/multiplay/generic/int[28]", 0);
  setprop("sim/multiplay/generic/int[29]", 0);
  setprop("sim/multiplay/generic/int[30]", 0);
  setprop("sim/multiplay/generic/int[31]", 0);
  setprop("sim/multiplay/generic/int[32]", 0);
  setprop("sim/multiplay/generic/int[33]", 0);

  setprop("sim/ai/submodels/Bomb_250-number",  5);

  setprop("sim/multiplay/generic/int[17]", 0);
  setprop("sim/multiplay/generic/int[18]", 0);
  setprop("sim/multiplay/generic/int[19]", 0);
  setprop("sim/multiplay/generic/int[20]", 0);
  setprop("sim/multiplay/generic/int[21]", 0);

  setprop("sim/ai/submodels/Bomb_500-number",  2);

  setprop("sim/multiplay/generic/int[22]", 0);
  setprop("sim/multiplay/generic/int[23]", 0);

  gui.popupTip("Guns set to unlimited mode--definitely not realistic and only for testing!  Select 'Reload Guns' to revert to limited ammo.",7)
}

Bomb_Release = func {

  # 100 Kg Bombs     sim/models/show-Bomb1_100
  if (getprop("/sim/multiplay/generic/bool[21]") and !getprop("/sim/models/launching-Bomb_100")) {
    var Bomb_100Count = getprop("sim/ai/submodels/Bomb_100-number");
    if (Bomb_100Count > 0 and Bomb_100Count <= 10) {
      setprop("/sim/launching-Bomb_100", 1);

      # Se (23 + 10) = 33 allora "int[33]", se (23 + 1) = 24 allora "int[24]"
      var mpIndex = 23 + Bomb_100Count;
      var propPath = "sim/multiplay/generic/int[" ~ mpIndex ~ "]";
      setprop(propPath, 1);

      settimer(func() { setprop(propPath, 0); }, 0.2);
      settimer(func() {
        setprop("sim/ai/submodels/Bomb_100-number", Bomb_100Count - 1);
        setprop("/sim/models/launching-Bomb_100", 0);
      }, 0.3);
    }
  }

  # 250 Kg Bombs     sim/models/show-Bomb1_250
  if (getprop("/sim/multiplay/generic/bool[8]") and !getprop("/sim/models/launching-Bomb_250")) {
    var Bomb_250Count = getprop("sim/ai/submodels/Bomb_250-number");
    if (Bomb_250Count > 0 and Bomb_250Count <= 5) {
      setprop("/sim/models/launching-Bomb_250", 1);

      # Se (16 + 5) = 21 allora "int[21]", se (16 + 1) = 17 allora "int[17]"
      var mpIndex = 16 + Bomb_250Count;
      var propPath = "sim/multiplay/generic/int[" ~ mpIndex ~ "]";
      setprop(propPath, 1);

      settimer(func() { setprop(propPath, 0); }, 0.2);
      settimer(func() {
        setprop("sim/ai/submodels/Bomb_250-number", Bomb_250Count - 1);
        setprop("/sim/models/launching-Bomb_250", 0);
      }, 0.3);
    }
  }

  # 500 Kg Bombs     sim/models/show-Bomb1_500
  if (getprop("/sim/multiplay/generic/bool[19]") and !getprop("/sim/models/launching-Bomb_500")) {
    var Bomb_500Count = getprop("sim/ai/submodels/Bomb_500-number");
    if (Bomb_500Count > 0 and Bomb_500Count <= 2) {
      setprop("/sim/modelslaunching-Bomb_500", 1);

      # Se (21 + 2) = 23 allora "int[23]", se (21 + 1) = 22 allora "int[22]"
      var mpIndex = 21 + Bomb_500Count;
      var propPath = "sim/multiplay/generic/int[" ~ mpIndex ~ "]";
      setprop(propPath, 1);

      settimer(func() { setprop(propPath, 0); }, 0.2);
      settimer(func() {
        setprop("sim/ai/submodels/Bomb_500-number", Bomb_500Count - 1);
        setprop("/sim/models/launching-Bomb_500", 0);
      }, 0.3);
    }
  }
}
