/*
* Caskey, Damon V
* 2020-09-04
*
* Simulate a lightning flash at random intervals.
*/
void dc_lightning_flash()
{
#define DC_LF_FLASH_WAIT_MIN 0 // Minimum time between lightning occurrence.
#define DC_LF_FLASH_WAIT_MAX 4000 // Maximum time between lightning occurrence.
#define DC_LF_POSITION_X 0
#define DC_LF_POSITION_Y 0
#define DC_LF_POSITION_Z 1000
#define DC_LF_FLASH_BLEND 1
#define DC_LF_FLASH_COLOR_WHITE rgbcolor(255, 255, 255)
#define DC_LF_FLASH_COLOR_YELLOW rgbcolor(255, 255, 153)
#define DC_LF_FLASH_COLOR_BLUE rgbcolor(153, 204, 204)
#define DC_LF_FRAME_0_DELAY 40
#define DC_LF_FRAME_1_DELAY DC_LF_FRAME_0_DELAY + 64
#define DC_LF_FRAME_2_DELAY DC_LF_FRAME_1_DELAY + 24
int elapsed_time = openborvariant("elapsed_time");
int time_point = getlocalvar("dc_lf_time");
int time_differential = 0;
int box_size_x = 0;
int box_size_y = 0;
int box_color = 0;
/*
* If time temp is empty, initialize it to current
* elapsed time. Then get difference between time
* point vs. current elapsed time.
*/
if (typeof(time_point) != openborconstant("VT_INTEGER"))
{
time_point = elapsed_time;
setlocalvar("dc_lf_time", time_point);
}
time_differential = elapsed_time - time_point;
/*
* If the time difference is negative, we are in the
* interval between animations. Exit and do nothing.
*/
if (time_differential < 0)
{
return;
}
/*
* Now take action based on time. We're actually just
* choosing colors for the drawbox function. By cycling
* through certain color sets, we get an initial flash,
* then a strobing effect, and finally a lingering light
* that closely approximates a lightning strike.
*/
if (time_differential < DC_LF_FRAME_0_DELAY)
{
/*
* Play thunder sound if we aren't already.
*/
int playid = getlocalvar("dc_lf_playid");
int sample_id = loadsample("data/sounds/custom/thunder_0.wav");
if (typeof(playid) == openborconstant("VT_INTEGER"))
{
if (querychannel(playid) == -1)
{
playsample(sample_id, 0, 120, 120, 100, 0);
playid = openborvariant("sample_play_id");
setlocalvar("dc_lf_playid", playid);
}
}
else
{
playsample(sample_id, 0, 120, 120, 100, 0);
playid = openborvariant("sample_play_id");
setlocalvar("dc_lf_playid", playid);
}
box_color = DC_LF_FLASH_COLOR_WHITE;
}
else if (time_differential >= DC_LF_FRAME_0_DELAY && time_differential < DC_LF_FRAME_1_DELAY)
{
/* Randomly switch between blue and yellow. */
dc_d20_set_range_min(0);
dc_d20_set_range_max(1);
if (dc_d20_random_int())
{
box_color = DC_LF_FLASH_COLOR_BLUE;
}
else
{
box_color = DC_LF_FLASH_COLOR_YELLOW;
}
}
else if (time_differential >= DC_LF_FRAME_1_DELAY && time_differential < DC_LF_FRAME_2_DELAY)
{
box_color = DC_LF_FLASH_COLOR_BLUE;
}
else
{
/*
* Animation is finished.
*
* If elapsed time is divisible by 100, then
* reset time point to empty. This gives us a
* slight chance of immediately looping back
* and instantly doing another lightning strike.
* We also delete the play ID that prevents thunder
* overlap so a second thunder sound can play.
* Just like real storms occasionally producing
* a crescendo of lightning.
*
* Otherwise set the time point to be a randomized
* period ahead of current elapsed time. This will
* cause time differential to be negative until current
* elapsed time catches up, giving us an interval
* between lightning strikes.
*/
if (elapsed_time % 100 == 0)
{
time_point = NULL();
}
else
{
dc_d20_set_range_min(DC_LF_FLASH_WAIT_MIN);
dc_d20_set_range_max(DC_LF_FLASH_WAIT_MAX);
time_point = elapsed_time + dc_d20_random_int();
}
setlocalvar("dc_lf_time", time_point);
setlocalvar("dc_lf_playid", -1);
box_color = DC_LF_FLASH_COLOR_BLUE;
}
/*
* Draw the box to create lightning effect.
*/
box_size_x = openborvariant("hresolution");
box_size_y = openborvariant("vresolution");
drawbox(DC_LF_POSITION_X, DC_LF_POSITION_Y, box_size_x, box_size_y, DC_LF_POSITION_Z, box_color, DC_LF_FLASH_BLEND);
return;
#undef DC_LF_FLASH_WAIT_MIN
#undef DC_LF_FLASH_WAIT_MAX
#undef DC_LF_POSITION_X
#undef DC_LF_POSITION_Y
#undef DC_LF_POSITION_Z
#undef DC_LF_FLASH_BLEND
#undef THUNDER_DIFFERENTIAL
#undef DC_LF_FLASH_COLOR_WHITE
#undef DC_LF_FLASH_COLOR_YELLOW
#undef DC_LF_FLASH_COLOR_BLUE
#undef DC_LF_FRAME_0_DELAY
#undef DC_LF_FRAME_1_DELAY
#undef DC_LF_FRAME_2_DELAY
}