1var a10weapons    = props.globals.getNode("sim/model/A-10/weapons");
2var arm_sw        = props.globals.getNode("sim/model/A-10/weapons/master-arm-switch");
3var aim9_knob     = props.globals.getNode("sim/model/A-10/weapons/dual-AIM-9/aim9-knob");
4var gun_running   = props.globals.getNode("sim/model/A-10/weapons/gun/running[0]");
5var gr_switch     = props.globals.getNode("sim/model/A-10/weapons/gun-rate-switch");
6var gun_count     = props.globals.getNode("ai/submodels/submodel[1]/count");
7var GunReady      = props.globals.getNode("sim/model/A-10/weapons/gun/ready");
8var GunHydrDriveL = props.globals.getNode("sim/model/A-10/weapons/gun/hydr-drive-serviceable[0]");
9var GunHydrDriveR = props.globals.getNode("sim/model/A-10/weapons/gun/hydr-drive-serviceable[1]");
10var HydrPsiL      = props.globals.getNode("systems/A-10-hydraulics/hyd-psi[0]");
11var HydrPsiR      = props.globals.getNode("systems/A-10-hydraulics/hyd-psi[1]");
12var z_pov         = props.globals.getNode("sim/current-view/z-offset-m");
13
14var lastGunCount = 0;
15var z_povhold = 0;
16
17# Init
18var initialize = func() {
19	setlistener("controls/armament/trigger", func( Trig ) {
20		if ( Trig.getBoolValue()) {
21			A10weapons.fire_gau8a();
22		} else {
23			A10weapons.cfire_gau8a(); }
24	});
25	# gun vibrations effect
26	z_povhold = z_pov.getValue();
27}
28
29var fire_gau8a = func {
30	var gunRun = gun_running.getValue();
31	var gready = update_gun_ready();
32	if (gready and !gunRun) {
33		gun_running.setBoolValue(1);
34		gunRun = 1;
35		gau8a_vibs(0.002, z_pov.getValue());
36	} elsif(!gready) {
37		gun_running.setBoolValue(0);
38		return;
39	}
40	if (gunRun) {
41		# update gun count and yasim weight
42		var realGunCount = gun_count.getValue();
43		# init lastGunCount
44		if((lastGunCount == 0) and (realGunCount > 0)) { lastGunCount = realGunCount + 1; }
45		realGunCount -= (lastGunCount - realGunCount) * 7;
46		if(realGunCount < 0) { realGunCount = 0 }
47		gun_count.setValue(realGunCount);
48		setprop("yasim/weights/ammunition-weight-lbs", (realGunCount*0.9369635));
49		# for the next loop
50		lastGunCount = realGunCount;
51	}
52}
53
54var cfire_gau8a = func {
55	gun_running.setBoolValue(0);
56	update_gun_ready();
57}
58
59var gau8a_vibs = func(v, zpov) {
60	if (getprop("sim/current-view/view-number") == 0) {
61		if (gun_running.getBoolValue()) {
62			var newZpos = v+zpov;
63			z_pov.setValue(newZpos);
64			settimer( func { gau8a_vibs(-v, zpov) }, 0.02);
65		} else { z_pov.setValue(z_povhold); }
66	}
67}
68
69var update_gun_ready = func() {
70	var ready = 0;
71	# TODO: electrical bus should be DC ARM BUS
72	if (gr_switch.getValue() and arm_sw.getValue() == 1 and gun_count.getValue() > 0) {
73		var drive_l = GunHydrDriveL.getValue();
74		var drive_r = GunHydrDriveR.getValue();
75		var psi_l   = HydrPsiL.getValue();
76		var psi_r   = HydrPsiR.getValue();
77		if (electrical.R_DC_bus_volts >= 24 and ((drive_l == 1 and psi_l > 900) or (drive_r == 1 and psi_r > 900))) {
78			ready = 1;
79		}
80	}
81	GunReady.setBoolValue(ready);
82	return ready;
83}
84
85# station selection
86# -----------------
87# Selects one or several stations. Each has to be loaded with the same type of
88# ordnance. Selecting a new station loaded with a different type deselects the
89# former ones. Selecting  an allready selected station deselect it.
90# Activates the search sound flag for AIM-9s (wich will be played only if the AIM-9
91# knob is on the correct position). Ask for deactivation of the search sound flag
92# in case of station deselection.
93var stations      = props.globals.getNode("sim/model/A-10/weapons/stations");
94var stations_list = stations.getChildren("station");
95var weights       = props.globals.getNode("sim").getChildren("weight");
96var aim9_knob     = a10weapons.getNode("dual-AIM-9/aim9-knob");
97var aim9_sound    = a10weapons.getNode("dual-AIM-9/search-sound");
98var cdesc = "";
99
100var select_station = func {
101	var target_idx = arg[0];
102	setprop("controls/armament/station-select", target_idx);
103	var desc_node = "sim/model/A-10/weapons/stations/station[" ~ target_idx ~ "]/description";
104	#print("sim/model/A-10/weapons/stations/station[" ~ target_idx ~ "]/description");
105	cdesc = props.globals.getNode(desc_node).getValue();
106	#print("select_station.cdesc: " ~ cdesc);
107	var sel_list = props.globals.getNode("sim/model/A-10/weapons/selected-stations");
108	foreach (var s; stations_list) {
109		idx = s.getIndex();
110		var sdesc = s.getNode("description").getValue();
111		var ssel = s.getNode("selected");
112		var tsnode = "s" ~ idx;
113		if ( idx == target_idx ) {
114			if (ssel.getBoolValue()) {
115				ssel.setBoolValue(0);
116				sel_list.removeChildren(tsnode);
117				if ( sdesc == "dual-AIM-9" ) {
118					deactivate_aim9_sound();
119				}
120			} else {
121				ssel.setBoolValue(1);
122				var ts = sel_list.getNode(tsnode, 1);
123				ts.setValue(target_idx);
124				if ( sdesc == "dual-AIM-9") {
125					aim9_sound.setBoolValue(1);
126				}
127			}
128		} elsif ( cdesc != sdesc ) {
129			# TODO: code triple and single MK82 mixed release ?
130			ssel.setBoolValue(0);
131			sel_list.removeChildren(tsnode);
132			if ( sdesc == "dual-AIM-9" ) {
133				deactivate_aim9_sound();
134			}
135		}
136	}
137}
138
139
140# station release
141# ---------------
142# Handles ripples and intervales.
143# Handles the availability lights (3 green lights each station).
144# LAU-68, with 7 ammos by station turns only one light until the dispenser is empty.
145# Releases and substract the released weight from the station weight.
146# Ask for deactivation of the search sound flag after the last AIM-9 has been released.
147var sl_list = 0;
148
149var release = func {
150	var arm_volts = props.globals.getNode("systems/electrical/R-AC-volts").getValue();
151	var asw = arm_sw.getValue();
152	if ( asw != 1 or arm_volts < 24 )	{ return; }
153	sl_list = a10weapons.getNode("selected-stations").getChildren();
154	var rip = a10weapons.getNode("rip").getValue();
155	var interval = a10weapons.getNode("interval").getValue();
156	# FIXME: riple compatible release types should be defined in the foo-set.file
157	if ( cdesc == "LAU-68" or cdesc == "triple-MK-82-LD" or cdesc == "single-MK-82-LD") {
158		release_operate(rip, interval);
159	} else {
160		release_operate(1, interval);
161	}
162}
163
164var release_operate = func(rip_counter, interval) {
165	foreach(sl; sl_list) {
166		var slidx = sl.getValue();
167		var snode = "sim/model/A-10/weapons/stations/station[" ~ slidx ~ "]";
168		var s = props.globals.getNode(snode);
169		var wnode = "sim/weight[" ~ slidx ~ "]";
170		var w = props.globals.getNode(wnode);
171		var wght = w.getNode("weight-lb").getValue();
172		var awght = s.getNode("ammo-weight-lb").getValue();
173		if ( cdesc == "LAU-68" ) { var lau68ready = s.getNode("ready-0"); }
174		var avail = s.getNode("available");
175		var a = avail.getValue();
176		if ( a != 0 ) {
177			if ( cdesc == "dual-AIM-9"  and aim9_knob.getValue() != 2 ) { return; }
178			turns = a10weapons.getNode(cdesc).getNode("available").getValue();
179			for( i = 0; i <= turns; i = i + 1 ) {
180				var it = cdesc ~ "/trigger[" ~ i ~"]";
181				var itrigger = s.getNode(it);
182				var iready_node = "ready-" ~ i;
183				var a = avail.getValue();
184				if ( cdesc != "LAU-68" ) { var iready = s.getNode(iready_node); }
185				var t = itrigger.getBoolValue();
186				if ( !t and a > 0) {
187					itrigger.setBoolValue(1);
188					a -= 1;
189					avail.setValue(a);
190					rip_counter -= 1;
191					wght -= awght;
192					w.getNode("weight-lb").setValue(wght);
193					if ( cdesc != "LAU-68" ) { iready.setBoolValue(0); }
194					if ( a == 0 ) {
195						if ( cdesc == "LAU-68" ) {
196							lau68ready.setBoolValue(0);
197						} elsif ( cdesc == "dual-AIM-9" ) {
198							deactivate_aim9_sound();
199						}
200						s.getNode("error").setBoolValue(1);
201					}
202					if (rip_counter > 0 ) {
203						settimer( func { release_operate(rip_counter, interval); }, interval);
204					}
205					return;
206				}
207			}
208		}
209	}
210}
211
212
213# Searchs if there isn't a remainning AIM-9 on a selected station before
214# deactivating the search sound flag.
215var deactivate_aim9_sound = func {
216	aim9_sound.setBoolValue(0);
217	var a = 0;
218	foreach (s; stations.getChildren("station")) {
219		var ssel = s.getNode("selected").getBoolValue();
220		var desc = s.getNode("description").getValue();
221		var avail = s.getNode("available");
222		if ( ssel and desc == "dual-AIM-9"  ) {
223			a += avail.getValue();
224		}
225		if ( a ) {
226			aim9_sound.setBoolValue(1);
227		}
228	}
229}
230
231
232# link from the Fuel and Payload menu (gui.nas)
233# ---------------------------------------------
234# Called from the F&W dialog when the user selects a weight option
235# and hijacked from gui.nas so we can call our update_stations().
236# TODO: make the call of a custom func possible from inside gui.nas
237gui.weightChangeHandler = func {
238	var tankchanged = gui.setWeightOpts();
239
240	# This is unfortunate.  Changing tanks means that the list of
241	# tanks selected and their slider bounds must change, but our GUI
242	# isn't dynamic in that way.  The only way to get the changes on
243	# screen is to pop it down and recreate it.
244	# TODO: position the recreated window where it was before.
245	if(tankchanged) {
246		update_stations();
247		var p = props.Node.new({"dialog-name" : "WeightAndFuel"});
248		fgcommand("dialog-close", p);
249		gui.showWeightDialog();
250	}
251}
252
253var update_stations = func {
254	var a = nil;
255	foreach (w; weights) {
256		var idx = w.getIndex();
257		var weight = 0;
258		var desc = w.getNode("selected").getValue();
259		if ( desc == "600 Gallons Fuel Tank" ) {
260			desc = "tank-600-gals";
261		}
262		var type = a10weapons.getNode(desc);
263		var snode = "sim/model/A-10/weapons/stations/station[" ~ idx ~ "]";
264		var s = props.globals.getNode(snode);
265		if ( desc != "none" ) {
266			station_load(s, w, type);
267		} else {
268			station_unload(s, w);
269		}
270	}
271}
272
273
274
275# station load
276# ------------
277# Sets the station properties from the type definition in the current station.
278# Prepares the error light or the 3 ready lights, then sets to false the
279# necessary number of triggers (useful in the case of the submodels weren't
280# already defined).
281# Creates a node attached to the station's one and containing the triggers.
282var station_load = func(s, w, type) {
283	var weight = type.getNode("weight-lb").getValue();
284	var ammo_weight = type.getNode("ammo-weight-lb").getValue();
285	var desc = type.getNode("description").getValue();
286	var avail = type.getNode("available").getValue();
287	var readyn = type.getNode("ready-number").getValue();
288	w.getNode("weight-lb").setValue(weight);
289	s.getNode("ammo-weight-lb", 1).setValue(ammo_weight);
290	s.getNode("description").setValue(desc);
291	s.getNode("available").setValue(avail);
292	if ( readyn == 0 ) {
293		# non-armable payload case. (ECM pod, external tank...)
294		s.getNode("error").setBoolValue(1);
295		return;
296	} else {
297		s.getNode("error").setBoolValue(0);
298	}
299	if ( readyn == 1 ) {
300		# single ordnance case.
301		s.getNode("ready-0").setBoolValue(1);
302	} elsif( readyn == 2 ) {
303		# double ordnances case
304		s.getNode("ready-0").setBoolValue(1);
305		s.getNode("ready-1").setBoolValue(1);
306	} else {
307		# triple ordnances case
308		s.getNode("ready-0").setBoolValue(1);
309		s.getNode("ready-1").setBoolValue(1);
310		s.getNode("ready-2").setBoolValue(1);
311	}
312	for( i = 0; i < avail; i = i + 1 ) {
313		# TODO: here to add submodels reload
314		itrigger_node = desc ~ "/trigger[" ~ i ~ "]";
315		t = s.getNode(itrigger_node, 1);
316		t.setBoolValue(0);
317	}
318}
319
320
321# station unload
322# --------------
323var station_unload = func(s, w) {
324	w.getNode("weight-lb").setValue(0);
325	s.getNode("ammo-weight-lb").setValue(0);
326	#desc = s.getNode("description").getValue();
327	s.getNode("description").setValue("none");
328	s.getNode("available").setValue(0);
329	s.getNode("ready-0").setBoolValue(0);
330	s.getNode("ready-1").setBoolValue(0);
331	s.getNode("ready-2").setBoolValue(0);
332	s.getNode("error").setBoolValue(1);
333}
334
335
336# Armament panel switches
337# -----------------------
338
339var master_arm_switch = func(swPos=0) {
340	# 3 positions MASTER ARM switch
341	var mastArmSw = arm_sw.getValue();
342	if((mastArmSw < 1) and (swPos == 1)) {
343		mastArmSw += 1;
344	} elsif((mastArmSw > -1) and (swPos == -1)) {
345		mastArmSw -= 1;
346	}
347	arm_sw.setIntValue(mastArmSw);
348	update_gun_ready();
349}
350
351var gun_rate_switch = func() {
352	# Toggle gun rate switch and update GUN READY light
353	var gunRateSw = gr_switch.getValue();
354	if(gunRateSw == 0) {
355		gr_switch.setBoolValue(1);
356	} else {
357		gr_switch.setBoolValue(0);
358	}
359	update_gun_ready();
360}
361
362var aim9_knob_switch = func {
363	var input = arg[0];
364	var a_knob = aim9_knob.getValue();
365	if ( input == 1 ) {
366		if ( a_knob == 0 ) {
367			aim9_knob.setValue(1);
368		} elsif ( a_knob == 1 ) {
369			aim9_knob.setValue(2);
370		}
371	} else {
372		if ( a_knob == 2 ) {
373			aim9_knob.setValue(1);
374		} elsif ( a_knob == 1 ) {
375			aim9_knob.setValue(0);
376		}
377	}
378}
379
380
381
382# reloads ammos for GAU-8
383# -----------------------
384# Thanks to Bombable/Brent Hugh, 2011-09
385
386reload_guns = func {
387	var gau8_ammo_count="/ai/submodels/submodel[1]/count";
388	var a10_ammo_weight="/yasim/weights/ammunition-weight-lbs";
389	groundspeed=getprop("velocities/groundspeed-kt");
390	engine_rpm=getprop("engines/engine/rpm");
391
392	#only allow it if on ground and stopped.
393	if (  groundspeed < 5  ) {
394		setprop ( gau8_ammo_count, 1174); #ammo loaded
395		var bweight=1174*0.9369635; #.9369635 = weight of one round in lbs
396		setprop(a10_ammo_weight, bweight);
397		gui.popupTip ("GAU-8 reloaded--1174 rounds.", 5);
398	} else {
399	gui.popupTip ("You must be on the ground and at a dead stop to re-load ammo.",5);
400	}
401}
402
403