function init()	
	fan = FindBody("fan")
	pos = GetBodyTransform(fan).pos
	fanSound = LoadLoop("MOD/snd/fan.ogg")
end

function rnd(mi, ma)
	return math.random()*(ma-mi)+mi
end

function limitVelocity(body, lin, ang)
	local linVel = GetBodyVelocity(body)
	local linVelLength = VecLength(linVel)
	if linVelLength > lin then 
		linVel = VecScale(linVel, lin/linVelLength)
		SetBodyVelocity(body, linVel)
	end
	
	local angVel = GetBodyAngularVelocity(body)
	local angVelLength = VecLength(angVel)
	if angVelLength > ang then 
		angVel = VecScale(angVel, ang/angVelLength)
		SetBodyAngularVelocity(body, angVel)
	end
end


function tick(dt)
	SetBodyAngularVelocity(fan, Vec(0, 20, 0))
	PlayLoop(fanSound, pos, 0.3)
	ParticleRadius(1,3)
	ParticleAlpha(0.3, 0)
	ParticleCollide(0)
	SpawnParticle(VecAdd(pos, Vec(rnd(-1,1), 1, rnd(-1,1))), Vec(rnd(-5,5), 10, rnd(-5,5)), 1.0)

	local strength = 200
	local maxDist = 7.0
	QueryRejectBody(fan)
	QueryRequire("dynamic")
	local aabbMin = VecAdd(pos, Vec(-maxDist, 0, -maxDist))
	local aabbMax = VecAdd(pos, Vec(maxDist, maxDist, maxDist))
	local shapes = QueryAabbShapes(aabbMin, aabbMax)
	
	for i=1, #shapes do
		local s = shapes[i]
		local hit, cp = GetShapeClosestPoint(s, pos)

		local d = VecSub(cp, pos)
		local l = VecLength(d)
		if l > 0 and l < maxDist then
			local scale = 1.0-l/maxDist
			
			local imp = Vec(0, strength*scale, 0)

			local b = GetShapeBody(s)
			ApplyBodyImpulse(b, cp, imp)

			limitVelocity(b, 10, 10)		
		end
	end
end


