#ifndef GAMMA_MORNING
#define GAMMA_MORNING  0x234557
#endif


#ifndef MAP_GAMMA
#define MAP_GAMMA      0
#endif
#ifndef GAMMA_DAY
#define GAMMA_DAY      0
#endif
#ifndef GAMMA_EVENING
#define GAMMA_EVENING  0x624E00
#endif
#ifndef GAMMA_TIME
#define GAMMA_TIME     0
#endif
#ifndef RAIN_TIME
#define RAIN_TIME      0
#endif
#ifndef WIND_TIME
#define WIND_TIME      0             // длительность между порывами ветра в ms (если 0 то нет) (+небольшой random)
#endif
#ifndef CURRENT_GAMMA
#define CURRENT_GAMMA  GAMMA_MORNING // изначально установленное время суток
#endif
#ifndef CURRENT_RAIN
#define CURRENT_RAIN   0             // 1 - дождь идет сразу, 0 - нет
#endif


//------------------------------------------------------------------------------------------
int gamma = CURRENT_GAMMA;
int RainDuration, Rain = CURRENT_RAIN;
int WindDuration;
//
int giWaterDownTime = 0;
int giWaterDownZ1000 = 0;
int giWaterDownSpeed1000 = 0;

//------------------------------------------------------------------------------------------
WaterDownTact();

//------------------------------------------------------------------------------------------
WeatherInit()
{
    Rain = CURRENT_RAIN;
    gamma = CURRENT_GAMMA;
    if ( Rain )
        SetEnvironment(ENV_RAIN);
    if ( RAIN_TIME )
        RainDuration = GetTime() + RAIN_TIME + Random(RAIN_TIME / 3);
    if ( WIND_TIME )
        WindDuration = GetTime() + WIND_TIME + Random(WIND_TIME / 3);
    if ( GAMMA_TIME && !Z_DEBUG)
        SetLevelGamma(gamma);
	if(MAP_GAMMA != 0)
        SetLevelGamma(MAP_GAMMA);
}


//------------------------------------------------------------------------------------------
WeatherTact()
{
  if ( GAMMA_TIME && GetEffectState(EFF_SHIFT_GAMMA) == -1 )
  {
/*    int unit;
    for ( unit = FirstSprite(); unit; unit = NextSprite() )
        if ( GetUnitVid(unit) == 225 )// мигающие глаза
        {
            if ( gamma == GAMMA_DAY )
            {
                Action(unit, ACT_PAUSE, GAMMA_TIME/2, 5000);
                AddCommand(unit, ACT_SET_INVISIBLE, 0);
            }
            else if ( gamma == GAMMA_MORNING )
            {
                Action(unit, ACT_PAUSE, GAMMA_TIME/4, 5000);
                AddCommand(unit, ACT_SET_INVISIBLE, 1);
            }
//              SetVidData(225, VID_HIDE, 0);
        }*/
    if      ( gamma == GAMMA_MORNING ) { gamma = GAMMA_DAY;      }//выключить фонарик
    else if ( gamma == GAMMA_EVENING ) { gamma = GAMMA_MORNING;  }//включить фонарик
    else                                 gamma = GAMMA_EVENING;
    if(!Z_DEBUG)
	    Effect(EFF_SHIFT_GAMMA, gamma, 0, GAMMA_TIME);
  }

  if ( RAIN_TIME && GetTime() > RainDuration )
  {
    if ( Rain )
    {
        SetEnvironment(ENV_STOP_RAIN);
//        MUSIC0 = "music\\mus00.ogg";
//        if ( MusicFileNumber == 2 )
//            StopMusic(6000);
    }
    else
    {
        SetEnvironment(ENV_BORN_RAIN);
//        MUSIC0 = "music\\rain.ogg";
//        if ( MusicFileNumber == 2 )
//            StopMusic(1000);
    }
    Rain = Rain ^ 1;
    RainDuration = GetTime() + RAIN_TIME + Random(RAIN_TIME/4);
  }

  if ( WIND_TIME && GetTime() > WindDuration )
  {
    SetEnvironment(ENV_SQUALL);
//    PlaySfx();
    WindDuration = GetTime() + 10000 + Random(40000);
  }
  WaterDownTact();
}


//------------------------------------------------------------------------------------------
WaterDown()
{
	giWaterDownTime = GetTime();
	int spr = FindNearestSprite(GETSPRITE_VID_FLAG + 691, 0, 0, 20000);
	if(spr)
	{
		giWaterDownZ1000 = GetZ(spr)*1000;
		giWaterDownSpeed1000 = (giWaterDownZ1000 - (-2)*1000) * 1000 / WATER_DOWN_TIME;
	}
	else
		giWaterDownZ1000 = 0;
}


//------------------------------------------------------------------------------------------
WaterDownTact()
{
	if(giWaterDownTime)
	{
		giWaterDownZ1000 -= giLastTactTime * giWaterDownSpeed1000 / 1000;
		//
		static int zPrev = -777;
		if(zPrev != giWaterDownZ1000/1000)
		{
			zPrev = giWaterDownZ1000/1000;
			if(zPrev <= -2)
			{
				Genocide(691);
				giWaterDownTime = 0;
			}
			else
				ChangeZUnit(691, zPrev);
		}
	}
}

