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

function rndVec(t)
	return Vec(rnd(-t, t), rnd(-t, t), rnd(-t, t))
end

function init()
	exhausts = FindLocations("exhaust")
	cracks = FindLocations("crack")
	turbines = LoadLoop("MOD/assets/script/dam/turbines.ogg")
	water = LoadLoop("MOD/assets/script/dam/water.ogg")
	sndPos = GetLocationTransform(FindLocation("watersound")).pos
end

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

function tick(dt)
	if GetBool("level.tubinesgone") then
		for i=1, #cracks do
			local t = GetLocationTransform(cracks[i])
			local dir = TransformToParentVec(t, Vec(rnd(-0.1, 0.1), rnd(-0.1, 0.1), 1))
			local r = GetTagValue(cracks[i], "crack")
			for j=1, 5 do
				SpawnParticle("water", VecAdd(t.pos, rndVec(0.1)), VecAdd(VecScale(dir, 5+10*r), rndVec(0.2)), r, 2)
			end
		end
		PlayLoop(water, sndPos)
	else
		for i=1, #exhausts do
			local t = GetLocationTransform(exhausts[i])
			local dir = TransformToParentVec(t, Vec(rnd(-0.1, 0.1), rnd(-0.1, 0.0), 1))
			for j=1, 4 do
				SpawnParticle("water", VecAdd(t.pos, rndVec(0.1)), VecAdd(VecScale(dir, 7), rndVec(0.2)), 0.3, 2)
			end
		end
		PlayLoop(turbines, sndPos, 0.3)
	end
end

