;----------
;	Multiple Gun Positions by Colonel Klink
;----------
;	Attach by Red Stripe Studios
;	Put in activation field [unit1, unit2, unit3] exec "attach.sqs"
;----------
;	MG on vehicle script by joncoolx@supanet.com
;	( endCargoSession = false )
;----------
_hetzer1 = _this select 0
_hetzer2 = _this select 1

;----------
;	only build on server
;----------
?!local _hetzer1: exit

;----------
;	If you choose to end the script
;	you must place a trigger with "endCargoSession = true"
;	in the "On Activation" field.
;----------
endCargoSession = false

;[_hetzer2] join _hetzer1
_hetzercarrier = _hetzer1
_hetzercargo  = _hetzer2

;----------
; Cargo's offset (from carrier's origin) and azimuth [4-vector]
; Here set the distance from the centre of 'hetzer1' you want to attach 'hetzer2' 
; in co-ordinates [x(left/right),y(front/back),z(up/down)]
;----------
_co = 0
_co_x = -0.4
_co_y = 0.42
_co_z = 0.9
_co_a = 0

#loop
;----------
;	Get carrier's position and azimuth
;----------
_pos = getPos _hetzercarrier
_dir = getDir _hetzercarrier

;----------
;	Rotate cargo's position around carrier's z-axis
;----------
_sind = sin _dir
_cosd = cos _dir

;----------
;	the fancy maths bit, calculates the radius of the 'oval' to be mapped out by the attached object 
;	and tells hetzer2 to stick to a certain area in relation to unit1
;----------
_hetzercargo setPos [( _pos select 0 ) + _cosd * _co_x + _sind * _co_y, ( _pos select 1 ) + _cosd * _co_y - _sind * _co_x, ( _pos select 2 ) + _co_z ]

;----------
;	Set cargo's azimuth relative to carrier's
;----------
_hetzercargo setDir ( _dir + _co_a )
_hetzer2 setdammage 0

;----------
;	set refesh rate (unlimited HIGH LAG!) make something very small, e.g. ~0000000000000000000.1 for lower lag
;	however bigger values means the object jumps lots instead of sticking
;----------
~.01

?!(alive _hetzercarrier): goto "notalive"
?!(alive _hetzercargo): goto "notalive"
? (!endCargoSession) : goto "loop"

~.01
#notcargosession
@ endcargosession
#notalive
unassignVehicle _hetzercargo
_hetzercargo setpos [(getpos _hetzercarrier select 0)-(sin (getdir _hetzercarrier)-3), (getpos _hetzercarrier select 1)-(cos (getdir _hetzercarrier)-1), -0.95]
_hetzercargo setdir getdir _hetzercarrier
deleteVehicle _hetzercargo
exit