#########################################################
############## General initializers #####################
#########################################################
#                                                        Guns
var n_gun1_ammo_count = "ai/submodels/submodel[0]/count";
var n_gun2_ammo_count = "ai/submodels/submodel[1]/count";
#                                                        Bomb
var n_bom1_ammo_count = "ai/submodels/submodel[2]/count";

#########################################################
##################### reload guns #######################
#########################################################
# Guns generally cannot be reloaded in flight.  It requires landing,
# stop, and reload before taking off again.
#
# Note that tracers are modeled as bullets that include a visual model but
# no impact.  They fire along with 1 of every three or four bullets to simulate
# a tracer round every 4 rounds.

reload_guns  = func {

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

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

    #                                           Guns
    setprop(n_gun1_ammo_count, 400);
    setprop(n_gun2_ammo_count, 400);
    #                                           Bomb
    setprop(n_bom1_ammo_count, 7);

    gui.popupTip ("Guns reloaded--400 rounds in each gun and rockets.", 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");

  #                                           Guns
  setprop ( n_gun1_ammo_count, -1);
  setprop ( n_gun2_ammo_count, -1);
  #                                           Bomb
  setprop(n_bom1_ammo_count, 7);

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

######################################################################
###                                                                ###
###                         Guns fire                              ###
###                                                                ###
### BARANGER Emmanuel aka Helijah 05/03/2026                       ###
######################################################################

fire_MG = func {
  var CurrentView = getprop("sim/current-view/view-number-raw");
  if ( CurrentView == 102 ) {
    # Nose                     sim/model/show-gunner1
    var Gunner1On   = getprop("sim/multiplay/generic/bool[0]");
    if ( Gunner1On ) {
      # setprop("/controls/armament/trigger0", 1);
      setprop("sim/multiplay/generic/int[10]", 1);
    }
  } elsif ( CurrentView == 101 ) {
    # Tail                     sim/model/show-gunner2
    var Gunner2On   = getprop("sim/multiplay/generic/bool[1]");
    if ( Gunner2On ) {
      #setprop("/controls/armament/trigger1", 1);
      setprop("sim/multiplay/generic/int[11]", 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[11]", 0);
}

fire_bombs = func {
  # setprop("/controls/armament/trigger2", 1);
  setprop("sim/multiplay/generic/int[12]", 1);
}

stop_bombs = func {
  # setprop("/controls/armament/trigger2", 0);
  setprop("sim/multiplay/generic/int[12]", 0);
}

