function init()
    a = GetPlayerCameraTransform()
    sb = FindBody("plane")
    maxheight = 8
    boc = 20
    absolute = false
    b = FindShape("bby")
    b2 = FindShape("bby2")
    b3 = FindShape("bby3")
    b4 = FindShape("bby4")
    b5 = FindShape("bby5")
    b6 = FindShape("bby6")
    b7 = FindShape("bby7")
    b8 = FindShape("bby8")
    f = FindShape("fuelline1")
    f2 = FindShape("fuelline2")
    f3 = FindShape("fuelline3")
    f4 = FindShape("fuelline4")
    f1 = FindShape("bottom")
    wroom1 = FindJoint("rotor1")
    wroom2 = FindJoint("rotor2")
    p1 = FindShape("particle1")
    tromer = 0
    thocc = 30 --rotor speed
    single = thocc
    single1 = thocc
    P1 = GetShapeBody(p1)
    smokeheightmax = 20
    timer = 0
    smokedie = 2
    thimer = 0
    stops = 2
    crash = LoadSound("MOD/helicrash.ogg")
    impact = LoadSound("MOD/heliimpact.ogg")
    beep = LoadSound("MOD/alarm.ogg")
    ambsnd = FindTrigger("helisnd")
    prop = LoadSound("MOD/propellerbroken.ogg")
    gono = 0
end

function tick(dt)
    -- These need to be updated each tick for their new values/positions.
    BLT = GetTriggerTransform(ambsnd)
    PL1 = GetShapeWorldTransform(p1)
    strans = GetShapeWorldTransform(sc)
    spos = strans.pos
    flydirection = TransformToParentVec(strans, Vec(0, -1, 0))
    local sbt = GetBodyTransform(sb)
    local trVec = Vec(0,0, -boc)
    local trVel = TransformToParentPoint(sbt, trVec)
    local velSub = VecSub(trVel, sbt.pos)
    local bias = Vec(0, 0, 0)
    SetTriggerTransform(ambsnd, Transform(PL1.pos,QuatEuler(0,0,0)))
    tromer = tromer + dt
    if not absolute then
    	SetJointMotor(wroom1, thocc)
SetJointMotor(wroom2, -thocc)
        if (sbt.pos[2] < maxheight) then
            bias[2] = boc
            boc = boc
        end
        local currentVelocity = GetBodyVelocity(sb)
        SetBodyVelocity(sb, velSub)
    end
    if IsShapeBroken(b) or IsShapeBroken(b2) or IsShapeBroken(b3) or IsShapeBroken(b4) or IsShapeBroken(b5) or IsShapeBroken(b6) or IsShapeBroken(b7)
    or IsShapeBroken(b8) then
    absolute = true
    DebugPrint("falling")
    	thocc = thocc / 1.005
    	SetJointMotor(wroom1, thocc)
SetJointMotor(wroom2, -thocc)
end
boc = boc + 0.0008
if boc > 50 then
	boc = 50
end
if IsShapeBroken(f) or IsShapeBroken(f2) or IsShapeBroken(f3) or IsShapeBroken(f4) then
	fuelgone=true
	absolute = true
	DebugPrint("fuelgone")
	Delete(ambsnd)
end
if IsShapeBroken(p1) then
	DebugPrint("particlegone")
end
if fuelgone then
	if not smokestop then
	smoke()

end
end
if absolute then
	thimer = thimer + dt
	if thimer > stops then
		stopspin = true
	end
	if not stopspin then
	ApplyBodyImpulse(sb, Vec(0,0,0), Vec(0,0,-3200))
end
if sbt.pos[2] < smokeheightmax then
	timer = timer + dt
	DebugPrint("Smokegondie")
	Delete(ambsnd)
		if not played1 then
	PlaySound(impact)
	played1 = true
	end
	if timer > smokedie then
		smokestop = true
end
end
if absolute and not fuelgone and not smokestop then
	if tromer > 0.3 then 
	PlaySound(beep)
	tromer = 0
end
end
end
if absolute then
    Delete(ambsnd)
	if not smokestop then
        if not played then
	PlaySound(prop)
    played = true
end
end
end
end
function smoke()
        rand = math.random(-3,3)
    randomsmoke1 = rand + PL1.pos[1]
    rand = math.random(-3,-1)
    randomsmoke2 = rand + PL1.pos[2]
    rand = math.random(-3,3)
    randomsmoke3 = rand + PL1.pos[3]

      ParticleReset()
        ParticleColor(1,0.6,0.3,0.1,0.1,0.1,"linear",0,0.5)
        ParticleEmissive(2,0,"linear",0,0.5)
        ParticleRadius(1.75,3,"linear",0.2,1)
        ParticleAlpha(1.0,0.0)
        ParticleCollide(0)
        ParticleGravity(0,-2)
		--Spawn particle at world origo with upwards velocity and a lifetime of ten seconds
		SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(randv1, randv2, randv3), 2)

             ParticleReset()
             ParticleType("plain")
        ParticleColor(1,0.6,0.3,0.1,0.1,0.1,"linear",0,0.5)
        ParticleEmissive(100,10)
        ParticleRadius(0.05, 0.01)
        ParticleAlpha(1.0,0.0)
        ParticleCollide(0)
        ParticleGravity(-10)
        ParticleDrag(0)
        ParticleStretch()
        --Spawn particle at world origo with upwards velocity and a lifetime of ten seconds
        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)
        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)
        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)
        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)
        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)
            SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)
        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)
        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)
        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)
        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(math.random(-10,10), math.random(-10,10), math.random(-10,10)), 3)



    ParticleReset()
        ParticleColor(0.16,0.15,0.15)
        ParticleRadius(1,5)
        ParticleAlpha(0.8,0.0,"linear",0.03,0.9)
        ParticleCollide(0)
        ParticleGravity(0,-2)

        SpawnParticle(Vec(randomsmoke1,randomsmoke2,randomsmoke3), Vec(randv1, randv2, randv3), 8)
	end
