datablock StaticShapeData(RaycastHealthTracer)
{
	shapefile = "./raycastline_healthbeam.dts";
};

datablock StaticShapeData(RaycastHealthBeam)
{
    shapefile = "./health_beam.dts";
};

function drawHealthTracer(%pos1, %pos2, %type)
{
    //create it
    %shape = new StaticShape() {
        datablock = RaycastHealthTracer;
    };
    MissionCleanup.add(%shape);

    %dir1 = vectorScale(vectorNormalize(vectorSub(%pos2, %pos1)), 0);
    %dir2 = vectorScale(vectorNormalize(vectorSub(%pos2, %pos1)), 0.5);

    if(!%type)
    {
        %shape.LaserFade(%pos2, %pos1, "1 0.0 0.0", 1, 0.05);
    }
    else if (%type == 1)
    {
        %shape.HeavyLaserFade(%pos2, %pos1, "1 0.0 0.0", 1, 0.15);
    }
    else
    {
        %shape.MedLaserFade(%pos2, %pos1, "1 0.0 0.0", 1, 0.1);
    }
}


function StaticShape::LaserFade(%this, %a, %b, %color, %alpha, %scale)
{
    if (%alpha <= 0)
    {
        %this.delete();
        return;
    }

    %size = %scale + %alpha * 0.055;
    %vector = vectorNormalize(vectorSub(%b, %a));

    %xyz = vectorNormalize(vectorCross("1 0 0", %vector));
    %u = mACos(vectorDot("1 0 0", %vector)) * -1;

    %this.setTransform(vectorScale(vectorAdd(%a, %b), 0.5) SPC %xyz SPC %u);
    %this.setScale(vectorDist(%a, %b) SPC %size SPC %size);
    %this.setNodeColor("ALL", %color SPC %alpha);

    if (%this.repeat)
    {
        %this.a = %a;
        %this.b = %b;
        %this.color = %color;
        return;
    }

    %this.schedule(5, "LaserFade", %a, %b, %color, %alpha - 0.2, %scale);
    //talk(%a SPC %b);
}

function StaticShape::MedLaserFade(%this, %a, %b, %color, %alpha, %scale)
{
    if (%alpha <= 0)
    {
        %this.delete();
        return;
    }

    %size = %scale + %alpha * 0.055;
    %vector = vectorNormalize(vectorSub(%b, %a));

    %xyz = vectorNormalize(vectorCross("1 0 0", %vector));
    %u = mACos(vectorDot("1 0 0", %vector)) * -1;

    %this.setTransform(vectorScale(vectorAdd(%a, %b), 0.5) SPC %xyz SPC %u);
    %this.setScale(vectorDist(%a, %b) SPC %size SPC %size);
    %this.setNodeColor("ALL", %color SPC %alpha);

    if (%this.repeat)
    {
        %this.a = %a;
        %this.b = %b;
        %this.color = %color;
        return;
    }

    %this.schedule(5, "MedLaserFade", %a, %b, %color, %alpha - 0.1, %scale);
}


function StaticShape::HeavyLaserFade(%this, %a, %b, %color, %alpha, %scale)
{
    if (%alpha <= 0)
    {
        %this.delete();
        return;
    }

    %size = %scale + %alpha * 0.055;
    %vector = vectorNormalize(vectorSub(%b, %a));

    %xyz = vectorNormalize(vectorCross("1 0 0", %vector));
    %u = mACos(vectorDot("1 0 0", %vector)) * -1;

    %this.setTransform(vectorScale(vectorAdd(%a, %b), 0.5) SPC %xyz SPC %u);
    %this.setScale(vectorDist(%a, %b) SPC %size SPC %size);
    %this.setNodeColor("ALL", %color SPC %alpha);

    if (%this.repeat)
    {
        %this.a = %a;
        %this.b = %b;
        %this.color = %color;
        return;
    }

    %this.schedule(5, "HeavyLaserFade", %a, %b, %color, %alpha - 0.08, %scale);
}