2006-02-24 04:48:15 +00:00
|
|
|
#include "a_lightning.h"
|
2011-07-06 14:20:54 +00:00
|
|
|
#include "doomstat.h"
|
2006-02-24 04:48:15 +00:00
|
|
|
#include "p_lnspec.h"
|
|
|
|
#include "statnums.h"
|
|
|
|
#include "m_random.h"
|
|
|
|
#include "templates.h"
|
|
|
|
#include "s_sound.h"
|
|
|
|
#include "p_acs.h"
|
2008-06-15 18:36:26 +00:00
|
|
|
#include "r_sky.h"
|
2008-09-14 23:54:38 +00:00
|
|
|
#include "g_level.h"
|
2011-07-05 10:02:38 +00:00
|
|
|
#include "r_state.h"
|
2011-07-06 14:20:54 +00:00
|
|
|
#include "farchive.h"
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
static FRandom pr_lightning ("Lightning");
|
|
|
|
|
|
|
|
IMPLEMENT_CLASS (DLightningThinker)
|
|
|
|
|
|
|
|
DLightningThinker::DLightningThinker ()
|
|
|
|
: DThinker (STAT_LIGHTNING)
|
|
|
|
{
|
2008-03-22 12:41:36 +00:00
|
|
|
Stopped = false;
|
2006-02-24 04:48:15 +00:00
|
|
|
LightningLightLevels = NULL;
|
|
|
|
LightningFlashCount = 0;
|
|
|
|
NextLightningFlash = ((pr_lightning()&15)+5)*35; // don't flash at level start
|
|
|
|
|
2011-06-21 03:38:32 +00:00
|
|
|
LightningLightLevels = new short[numsectors];
|
|
|
|
clearbufshort(LightningLightLevels, numsectors, SHRT_MAX);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DLightningThinker::~DLightningThinker ()
|
|
|
|
{
|
|
|
|
if (LightningLightLevels != NULL)
|
|
|
|
{
|
|
|
|
delete[] LightningLightLevels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DLightningThinker::Serialize (FArchive &arc)
|
|
|
|
{
|
|
|
|
int i;
|
2011-06-11 23:58:33 +00:00
|
|
|
short *lights;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
Super::Serialize (arc);
|
|
|
|
|
2008-03-22 12:41:36 +00:00
|
|
|
arc << Stopped << NextLightningFlash << LightningFlashCount;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
if (arc.IsLoading ())
|
|
|
|
{
|
2006-05-18 04:25:26 +00:00
|
|
|
if (LightningLightLevels != NULL)
|
|
|
|
{
|
|
|
|
delete[] LightningLightLevels;
|
|
|
|
}
|
2011-06-21 03:38:32 +00:00
|
|
|
LightningLightLevels = new short[numsectors];
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
lights = LightningLightLevels;
|
2011-06-21 03:38:32 +00:00
|
|
|
for (i = numsectors; i > 0; ++lights, --i)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2011-06-21 03:38:32 +00:00
|
|
|
arc << *lights;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DLightningThinker::Tick ()
|
|
|
|
{
|
|
|
|
if (!NextLightningFlash || LightningFlashCount)
|
|
|
|
{
|
|
|
|
LightningFlash();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--NextLightningFlash;
|
2008-03-22 12:41:36 +00:00
|
|
|
if (Stopped) Destroy();
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DLightningThinker::LightningFlash ()
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
sector_t *tempSec;
|
|
|
|
BYTE flashLight;
|
|
|
|
|
|
|
|
if (LightningFlashCount)
|
|
|
|
{
|
|
|
|
LightningFlashCount--;
|
|
|
|
if (LightningFlashCount)
|
|
|
|
{ // reduce the brightness of the flash
|
|
|
|
tempSec = sectors;
|
|
|
|
for (i = numsectors, j = 0; i > 0; ++j, --i, ++tempSec)
|
|
|
|
{
|
|
|
|
// [RH] Checking this sector's applicability to lightning now
|
|
|
|
// is not enough to know if we should lower its light level,
|
|
|
|
// because it might have changed since the lightning flashed.
|
|
|
|
// Instead, change the light if this sector was effected by
|
|
|
|
// the last flash.
|
2011-06-21 03:38:32 +00:00
|
|
|
if (LightningLightLevels[j] < tempSec->lightlevel-4)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2009-08-08 20:03:43 +00:00
|
|
|
tempSec->ChangeLightLevel(-4);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // remove the alternate lightning flash special
|
|
|
|
tempSec = sectors;
|
|
|
|
for (i = numsectors, j = 0; i > 0; ++j, --i, ++tempSec)
|
|
|
|
{
|
2011-06-21 03:38:32 +00:00
|
|
|
if (LightningLightLevels[j] != SHRT_MAX)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2009-08-08 20:03:43 +00:00
|
|
|
tempSec->SetLightLevel(LightningLightLevels[j]);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-21 03:38:32 +00:00
|
|
|
clearbufshort(LightningLightLevels, numsectors, SHRT_MAX);
|
2006-02-24 04:48:15 +00:00
|
|
|
level.flags &= ~LEVEL_SWAPSKIES;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LightningFlashCount = (pr_lightning()&7)+8;
|
|
|
|
flashLight = 200+(pr_lightning()&31);
|
|
|
|
tempSec = sectors;
|
|
|
|
for (i = numsectors, j = 0; i > 0; --i, ++j, ++tempSec)
|
|
|
|
{
|
|
|
|
// allow combination of the lightning sector specials with bit masks
|
2011-06-21 03:38:32 +00:00
|
|
|
int special = tempSec->special & 0xff;
|
2008-08-16 20:19:35 +00:00
|
|
|
if (tempSec->GetTexture(sector_t::ceiling) == skyflatnum
|
2006-02-24 04:48:15 +00:00
|
|
|
|| special == Light_IndoorLightning1
|
|
|
|
|| special == Light_IndoorLightning2
|
|
|
|
|| special == Light_OutdoorLightning)
|
|
|
|
{
|
|
|
|
LightningLightLevels[j] = tempSec->lightlevel;
|
|
|
|
if (special == Light_IndoorLightning1)
|
|
|
|
{
|
2009-08-08 20:03:43 +00:00
|
|
|
tempSec->SetLightLevel(MIN<int> (tempSec->lightlevel+64, flashLight));
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
else if (special == Light_IndoorLightning2)
|
|
|
|
{
|
2009-08-08 20:03:43 +00:00
|
|
|
tempSec->SetLightLevel(MIN<int> (tempSec->lightlevel+32, flashLight));
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-08 20:03:43 +00:00
|
|
|
tempSec->SetLightLevel(flashLight);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
if (tempSec->lightlevel < LightningLightLevels[j])
|
2011-06-21 03:38:32 +00:00
|
|
|
{ // The lightning is darker than this sector already is, so no lightning here.
|
2009-08-08 20:03:43 +00:00
|
|
|
tempSec->SetLightLevel(LightningLightLevels[j]);
|
2011-06-21 03:38:32 +00:00
|
|
|
LightningLightLevels[j] = SHRT_MAX;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
level.flags |= LEVEL_SWAPSKIES; // set alternate sky
|
|
|
|
S_Sound (CHAN_AUTO, "world/thunder", 1.0, ATTN_NONE);
|
|
|
|
FBehavior::StaticStartTypedScripts (SCRIPT_Lightning, NULL, false); // [RH] Run lightning scripts
|
|
|
|
|
|
|
|
// Calculate the next lighting flash
|
|
|
|
if (!NextLightningFlash)
|
|
|
|
{
|
|
|
|
if (pr_lightning() < 50)
|
|
|
|
{ // Immediate Quick flash
|
|
|
|
NextLightningFlash = (pr_lightning()&15)+16;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pr_lightning() < 128 && !(level.time&32))
|
|
|
|
{
|
|
|
|
NextLightningFlash = ((pr_lightning()&7)+2)*35;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NextLightningFlash = ((pr_lightning()&15)+5)*35;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-22 12:41:36 +00:00
|
|
|
void DLightningThinker::ForceLightning (int mode)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-22 12:41:36 +00:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
NextLightningFlash = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
NextLightningFlash = 0;
|
|
|
|
// Fall through
|
|
|
|
case 2:
|
|
|
|
Stopped = true;
|
|
|
|
break;
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static DLightningThinker *LocateLightning ()
|
|
|
|
{
|
|
|
|
TThinkerIterator<DLightningThinker> iterator (STAT_LIGHTNING);
|
|
|
|
return iterator.Next ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void P_StartLightning ()
|
|
|
|
{
|
|
|
|
DLightningThinker *lightning = LocateLightning ();
|
|
|
|
if (lightning == NULL)
|
|
|
|
{
|
|
|
|
new DLightningThinker ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-22 12:41:36 +00:00
|
|
|
void P_ForceLightning (int mode)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
DLightningThinker *lightning = LocateLightning ();
|
2008-03-22 12:41:36 +00:00
|
|
|
if (lightning == NULL)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-22 12:41:36 +00:00
|
|
|
lightning = new DLightningThinker ();
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
if (lightning != NULL)
|
|
|
|
{
|
2008-03-22 12:41:36 +00:00
|
|
|
lightning->ForceLightning (mode);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|