--
-- Dynamic morning fog
-- @author: Burner
-- @date: 11.04.2012
--

MorningFog = {};

function MorningFog:loadMap(name)
	g_currentMission.environment.skyCurveFog = AnimCurve:new(linearInterpolator4)
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 0})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 1*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 2*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 3*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 4*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 0, w = 1, time = 5*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 0, w = 1, time = 6*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 0, w = 1, time = 7.7*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 8*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 9*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 10*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 11*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 12*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 13*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 14*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 15*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 16*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 17*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 18*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 1, y = 1, z = 0, w = 0, time = 19*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 1, z = 0, w = 0, time = 20*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 1, z = 0, w = 0, time = 21*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 21.5*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 22*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 23*60})
	g_currentMission.environment.skyCurveFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 24*60})
	
	g_currentMission.environment.skyCurve = g_currentMission.environment.skyCurveFog
	
	g_currentMission.environment.skyCurveNoFog = AnimCurve:new(linearInterpolator4)
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 0})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 1*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 2*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 3*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 4*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 1, z = 1, w = 0, time = 5*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 1, z = 0, w = 0, time = 6*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 1, z = 0, w = 0, time = 6.5*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 7*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 8*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 9*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 10*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 11*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 12*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 13*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 14*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 15*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 16*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 17*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 0, z = 0, w = 0, time = 18*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 1, y = 1, z = 0, w = 0, time = 19*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 1, z = 0, w = 0, time = 20*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 1, z = 0, w = 0, time = 21*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 21.5*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 22*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 23*60})
	g_currentMission.environment.skyCurveNoFog:addKeyframe({x = 0, y = 0, z = 1, w = 0, time = 24*60})
	
	local randomInt = math.ceil((g_currentMission.environment.currentDay*5.5)/3);
	if randomInt%3 == 0 then
		g_currentMission.environment.setFog = true;
	else
		g_currentMission.environment.setFog = false;
	end;
	if randomInt%4 == 0 and g_currentMission.environment.setFog == false then
		g_currentMission.environment.setFog = true;
	end;
end;

function MorningFog:deleteMap()
end;

function MorningFog:mouseEvent(posX, posY, isDown, isUp, button)
end;

function MorningFog:keyEvent(unicode, sym, modifier, isDown)
end;

function MorningFog:update(dt)
	local randomInt = math.ceil((g_currentMission.environment.currentDay*5.5)/3);
	if randomInt%3 == 0 then
		g_currentMission.environment.setFog = true;
	else
		g_currentMission.environment.setFog = false;
	end;
	if randomInt%4 == 0 and g_currentMission.environment.setFog == false then
		g_currentMission.environment.setFog = true;
	end;
	local fogDensities = {0.4, 0.8, 1.4, 2.5};
	local fogDensityIndex = g_currentMission.environment.currentDay%table.getn(fogDensities);
	if g_currentMission.environment ~= nil then
		if g_currentMission.environment.setFog then
			if g_currentMission.environment.dayTime >= 5*1000*3600 and g_currentMission.environment.dayTime <= 5.05*1000*3600 then
				setFog("exp", 0.001*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.05*1000*3600 and g_currentMission.environment.dayTime <= 5.1*1000*3600 then
				setFog("exp", 0.002*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.1*1000*3600 and g_currentMission.environment.dayTime <= 5.15*1000*3600 then
				setFog("exp", 0.003*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.15*1000*3600 and g_currentMission.environment.dayTime <= 5.2*1000*3600 then
				setFog("exp", 0.004*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.2*1000*3600 and g_currentMission.environment.dayTime <= 5.25*1000*3600 then
				setFog("exp", 0.005*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.25*1000*3600 and g_currentMission.environment.dayTime <= 5.3*1000*3600 then
				setFog("exp", 0.006*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.3*1000*3600 and g_currentMission.environment.dayTime <= 5.35*1000*3600 then
				setFog("exp", 0.007*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.35*1000*3600 and g_currentMission.environment.dayTime <= 5.4*1000*3600 then
				setFog("exp", 0.008*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.4*1000*3600 and g_currentMission.environment.dayTime <= 5.45*1000*3600 then
				setFog("exp", 0.009*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.45*1000*3600 and g_currentMission.environment.dayTime <= 5.5*1000*3600 then
				setFog("exp", 0.010*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.5*1000*3600 and g_currentMission.environment.dayTime <= 5.55*1000*3600 then
				setFog("exp", 0.011*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.55*1000*3600 and g_currentMission.environment.dayTime <= 5.6*1000*3600 then
				setFog("exp", 0.012*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.6*1000*3600 and g_currentMission.environment.dayTime <= 5.65*1000*3600 then
				setFog("exp", 0.013*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);	
			elseif g_currentMission.environment.dayTime >= 5.65*1000*3600 and g_currentMission.environment.dayTime <= 5.7*1000*3600 then
				setFog("exp", 0.014*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);	
			elseif g_currentMission.environment.dayTime >= 5.7*1000*3600 and g_currentMission.environment.dayTime <= 5.75*1000*3600 then
				setFog("exp", 0.015*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);	
			elseif g_currentMission.environment.dayTime >= 5.75*1000*3600 and g_currentMission.environment.dayTime <= 5.8*1000*3600 then
				setFog("exp", 0.016*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);	
			elseif g_currentMission.environment.dayTime >= 5.8*1000*3600 and g_currentMission.environment.dayTime <= 5.85*1000*3600 then
				setFog("exp", 0.017*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);	
			elseif g_currentMission.environment.dayTime >= 5.85*1000*3600 and g_currentMission.environment.dayTime <= 5.9*1000*3600 then
				setFog("exp", 0.018*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);	
			elseif g_currentMission.environment.dayTime >= 5.9*1000*3600 and g_currentMission.environment.dayTime <= 5.95*1000*3600 then
				setFog("exp", 0.019*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 5.95*1000*3600 and g_currentMission.environment.dayTime <= 7*1000*3600 then
				setFog("exp", 0.020*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7*1000*3600 and g_currentMission.environment.dayTime <= 7.05*1000*3600 then
				setFog("exp", 0.019*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.05*1000*3600 and g_currentMission.environment.dayTime <= 7.1*1000*3600 then
				setFog("exp", 0.018*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.1*1000*3600 and g_currentMission.environment.dayTime <= 7.15*1000*3600 then
				setFog("exp", 0.017*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.15*1000*3600 and g_currentMission.environment.dayTime <= 7.2*1000*3600 then
				setFog("exp", 0.016*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.2*1000*3600 and g_currentMission.environment.dayTime <= 7.25*1000*3600 then
				setFog("exp", 0.015*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.25*1000*3600 and g_currentMission.environment.dayTime <= 7.3*1000*3600 then
				setFog("exp", 0.014*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.3*1000*3600 and g_currentMission.environment.dayTime <= 7.35*1000*3600 then
				setFog("exp", 0.013*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.35*1000*3600 and g_currentMission.environment.dayTime <= 7.4*1000*3600 then
				setFog("exp", 0.012*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.4*1000*3600 and g_currentMission.environment.dayTime <= 7.45*1000*3600 then
				setFog("exp", 0.011*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.45*1000*3600 and g_currentMission.environment.dayTime <= 7.5*1000*3600 then
				setFog("exp", 0.010*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.5*1000*3600 and g_currentMission.environment.dayTime <= 7.55*1000*3600 then
				setFog("exp", 0.009*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.55*1000*3600 and g_currentMission.environment.dayTime <= 7.6*1000*3600 then
				setFog("exp", 0.008*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.6*1000*3600 and g_currentMission.environment.dayTime <= 7.65*1000*3600 then
				setFog("exp", 0.007*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.65*1000*3600 and g_currentMission.environment.dayTime <= 7.7*1000*3600 then
				setFog("exp", 0.006*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.7*1000*3600 and g_currentMission.environment.dayTime <= 7.75*1000*3600 then
				setFog("exp", 0.005*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.75*1000*3600 and g_currentMission.environment.dayTime <= 7.8*1000*3600 then
				setFog("exp", 0.004*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.8*1000*3600 and g_currentMission.environment.dayTime <= 7.85*1000*3600 then
				setFog("exp", 0.003*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.85*1000*3600 and g_currentMission.environment.dayTime <= 7.9*1000*3600 then
				setFog("exp", 0.002*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			elseif g_currentMission.environment.dayTime >= 7.9*1000*3600 and g_currentMission.environment.dayTime <= 7.95*1000*3600 then
				setFog("exp", 0.001*fogDensities[fogDensityIndex+1], 1, 0.67, 0.67, 0.67);
			end;
			g_currentMission.environment.skyCurve = g_currentMission.environment.skyCurveFog;
		else
			g_currentMission.environment.skyCurve = g_currentMission.environment.skyCurveNoFog;
		end;
	end;
end;

function MorningFog:draw()
end;

addModEventListener(MorningFog);