

;  Smart Connection and Towing System v1.0
;  In order to connect vehicles one to another and tow smartly!


;  Mathematical core of this towing script was provided by FragMASTER
;  User Actions Menu Integration was constituted by Gokhan Tandogan
;  Artificial intelligence of the system* was improved by Gokhan Tandogan

;  (*Here system refers to the sum of corresponding lines in the config.cpp 
;    file of the vehicle to be towed and the towing script, which was 
;     provided by FragMASTER, modified by Gokhan Tandogan.)

;  How to Use

;  What You Need

;  The Principle of the System

;  IQ of the System
  
;  This system is able to detect if the towable vehicle is not alive!
;  This system is able to detect if the nearest towing vehicle is not alive!
;  and take defined actions as follows;
 

;  1) If any of the vehicles gets down, after the connection is established,
;     now these vehicles can not be disconnected anymore!

;  2) If any of the vehicles gets down, before the connection is established,
;     then these vehicles can not be connected naturally. However, if the   
;     towable vehicle is alive in this case, then a new towing vehicle can 
;     be driven close to the towable vehicle and a fresh connection and
;     towing can be possible!

;  WARNING

;  Never use global varibles in add-on scope! this causes problems when
;  there are more than one add-on of the same type in the game.
;  Instead, use animationphase type of condition statements with
;  "fake animations". Here, fake animations refers to animations which are 
;  invisible in game and simply 0 and 1 type of animations.
;  DISCLAIMER

;  This version to be used with SB_Kenworth trailer and SB_Kenworth
;  sized trailers.

;_truckdis = -2.5
;_trailerdis = 4
;_trailerlength = 11.3
;0.0015)

;  Here, the script begins;

_truck = _this select 0

_trailer = _this select 1

_trailer animate ["ani_trailer", 1];

_truckdis = -5.3
_trailerdis = 3
_trailerlength = 6



#loop

_dirtruck = getdir _truck
_dirtrailer = getdir _trailer

_x = getpos _truck select 0
_y = getpos _truck select 1

_xx = getpos _trailer select 0
_yy = getpos _trailer select 1

_speed = speed _truck

_truckx = sin(_dirtruck) * _truckdis
_trucky = cos(_dirtruck) * _truckdis

_trailerx = sin(_dirtrailer) * _trailerdis
_trailery = cos(_dirtrailer) * _trailerdis


_dir = (_dirtruck - _dirtrailer)
? _dir < -180 : _dir = _dir + 360
? _dir > 180 :  _dir = _dir - 360
_trailer setdir (getdir _trailer + _dir * _speed * 0.0010)

_trailer setpos [_xx + (_x + _truckx) - (_xx + _trailerx), _yy + (_y + _trucky) - (_yy + _trailery), getpos _truck select 2]

_trailer setvelocity [velocity _truck select 0, velocity _truck select 1, velocity _truck select 2] 

? _trailer animationPhase "ani_trailer" == 0 : goto "exitnow"

~0.01
goto "loop"



#exitnow


exit



