// Arm Transport ship

piece base,turret,arm1,arm2,arm3,link,door1,door2,wake1,wake2;

static-var boom_len,boom_heading,boom_pitch,boom_ok;

#define	SIG_WAKE 2
#define SIG_DOORHOLD 4
#define SMOKEPIECE1 base

#include "smokeunit.h"
#include "EXPtype.h"
#include "SFXtype.h"

Create()
	{
	start-script SmokeUnit();
	}


StartMoving()
	{
	signal SIG_WAKE;
	set-signal-mask SIG_WAKE;
	while (TRUE)
		{
		emit-sfx SFXTYPE_WAKE1 from wake1;
		emit-sfx SFXTYPE_WAKE1 from wake2;
		sleep 300;
		}
	}

StopMoving()
	{
	signal SIG_WAKE;				// Kill anybody that may be active
	}


SweetSpot(piecenum)
	{
	piecenum=base;
	}

StartDoorOpen()
	{
	signal SIG_DOORHOLD;
	set-signal-mask SIG_DOORHOLD;

	// open the doors
	turn door1 to z-axis <-90> speed <50>;
	turn door2 to z-axis <90> speed <50>;

	// wait 5 seconds (this will be reset if someone needs them opened again)
	sleep 5000;

	// close 'em again
	turn door1 to z-axis <0> speed <20>;
	turn door2 to z-axis <0> speed <20>;
	}


BoomCalc(posxz,posy)
	{
	var dxz,dy,lenxz;

	dxz = get PIECE_XZ(turret) - posxz;
	dy = posy - get PIECE_Y(turret) - [20];
	lenxz = get XZ_HYPOT(dxz);

	boom_len = get HYPOT(lenxz,dy) - [20];
	boom_heading = get XZ_ATAN(dxz);
	boom_pitch = get ATAN(dy,lenxz);
	boom_ok = (boom_len < [80]);
	}


BoomExtend()
	{
	start-script StartDoorOpen();

	turn turret to y-axis boom_heading speed <80>;
	turn magnet to x-axis boom_pitch speed <40>;
	turn arm1 to x-axis 0-boom_pitch speed <40>;
	move arm2 to z-axis boom_len/4 speed [30];
	move arm3 to z-axis boom_len/4 speed [30];
	move arm4 to z-axis boom_len/4 speed [30];
	move arm5 to z-axis boom_len/4 speed [30];

	wait-for-turn turret around y-axis;
	wait-for-turn arm1 around x-axis;
	wait-for-move arm2 along z-axis;
	wait-for-move arm3 along z-axis;
	wait-for-move arm4 along z-axis;
	wait-for-move arm5 along z-axis;
	}


BoomReset()
	{
	turn turret to y-axis <0> speed <40>;
	turn magnet to x-axis <0> speed <30>;
	turn arm1 to x-axis <0> speed <30>;
	move arm2 to z-axis [0] speed [25];
	move arm3 to z-axis [0] speed [25];
	move arm4 to z-axis [0] speed [25];
	move arm5 to z-axis [0] speed [25];
	}


BoomToPad()
	{
	start-script StartDoorOpen();

	turn turret to y-axis <0> speed <50>;
	turn magnet to x-axis <0> speed <30>;
	turn arm1 to x-axis <0> speed <30>;
	move arm2 to z-axis [0] speed [30];
	move arm3 to z-axis [0] speed [30];
	move arm4 to z-axis [0] speed [30];
	move arm5 to z-axis [0] speed [30];

	wait-for-turn turret around y-axis;
	wait-for-turn arm1 around x-axis;
	wait-for-move arm2 along z-axis;
	wait-for-move arm3 along z-axis;
	wait-for-move arm4 along z-axis;
	wait-for-move arm5 along z-axis;

	turn magnet to x-axis <-30> speed <30>;
	turn arm1 to x-axis <30> speed <30>;
	wait-for-turn arm1 around x-axis;
	}


TransportPickup(unitid)
	{
	call-script BoomCalc( get UNIT_XZ(unitid),get UNIT_Y(unitid) + get UNIT_HEIGHT(unitid) );
	if(boom_ok)
		{
		set BUSY to TRUE;
		call-script BoomExtend();
		move link to y-axis 0-get UNIT_HEIGHT(unitid) now;
		attach-unit unitid to link;
		call-script BoomToPad();
		attach-unit unitid to 0-1;
		call-script BoomReset();
		set BUSY to FALSE;
		}
	}


TransportDrop(unitid,position)
	{
	call-script BoomCalc(position,get GROUND_HEIGHT(position) + get UNIT_HEIGHT(unitid));
	if(boom_ok)
		{
		set BUSY to TRUE;
		call-script BoomToPad();
		move link to y-axis 0-get UNIT_HEIGHT(unitid) now;
		attach-unit unitid to link;
		call-script BoomExtend();
		drop-unit unitid;
		call-script BoomReset();
		set BUSY to FALSE;
		}
	}


Killed( severity, corpsetype )
	{
	if (severity <= 25)
		{
		corpsetype = 1;
		explode arm1 type	BITMAPONLY | BITMAP1;
		explode arm2 type	BITMAPONLY | BITMAP2;
		explode arm3 type	BITMAPONLY | BITMAP5;
		explode base type	BITMAPONLY | BITMAP3;
		explode door1 type	BITMAPONLY | BITMAP4;
		explode door2 type	BITMAPONLY | BITMAP5;
		explode link type	BITMAPONLY | BITMAP1;
		explode turret type	BITMAPONLY | BITMAP2;
		explode wake1 type	BITMAPONLY | BITMAP3;
		explode wake2 type	BITMAPONLY | BITMAP4;
		return( 0 );
		}

	if (severity <= 50)
		{
		corpsetype = 2;
		explode arm1 type	BITMAPONLY | BITMAP1;
		explode arm2 type	SHATTER | BITMAP2;
		explode arm3 type	SHATTER | BITMAP4;
		explode base type	BITMAPONLY | BITMAP3;
		explode door1 type	BITMAPONLY | BITMAP4;
		explode door2 type	BITMAPONLY | BITMAP5;
		explode link type	FALL | BITMAP1;
		explode turret type	BITMAPONLY | BITMAP2;
		explode wake1 type	FALL | BITMAP3;
		explode wake2 type	FALL | BITMAP4;
		return( 0 );
		}

	if (severity <= 99)
		{
		corpsetype = 3;
		explode arm1 type	BITMAPONLY | BITMAP1;
		explode arm2 type	SHATTER | BITMAP2;
		explode arm3 type	SHATTER | BITMAP4;
		explode base type	BITMAPONLY | BITMAP3;
		explode door1 type	BITMAPONLY | BITMAP4;
		explode door2 type	BITMAPONLY | BITMAP5;
		explode link type	FALL | SMOKE | FIRE | EXPLODE_ON_HIT | BITMAP1;
		explode turret type	BITMAPONLY | BITMAP2;
		explode wake1 type	FALL | SMOKE | FIRE | EXPLODE_ON_HIT | BITMAP3;
		explode wake2 type	FALL | SMOKE | FIRE | EXPLODE_ON_HIT | BITMAP4;
		return( 0 );
		}

	corpsetype = 3;
	explode arm1 type	BITMAPONLY | BITMAP1;
	explode arm2 type	SHATTER | EXPLODE_ON_HIT | BITMAP2;
	explode arm3 type	SHATTER | BITMAP4;
	explode base type	BITMAPONLY | BITMAP3;
	explode door1 type	BITMAPONLY | BITMAP4;
	explode door2 type	BITMAPONLY | BITMAP5;
	explode link type	FALL | SMOKE | FIRE | EXPLODE_ON_HIT | BITMAP1;
	explode turret type	BITMAPONLY | BITMAP2;
	explode wake1 type	FALL | SMOKE | FIRE | EXPLODE_ON_HIT | BITMAP3;
	explode wake2 type	FALL | SMOKE | FIRE | EXPLODE_ON_HIT | BITMAP4;
	return( 0 );
	}
