2021-09-14 12:05:36 +03:00
|
|
|
#include "framework.h"
|
2021-11-08 10:55:12 +03:00
|
|
|
#include "camera.h"
|
2021-09-14 12:05:36 +03:00
|
|
|
#include "savegame.h"
|
2021-09-15 11:13:47 +03:00
|
|
|
#include "weather.h"
|
2021-11-08 21:25:52 +03:00
|
|
|
#include "collide.h"
|
2021-11-10 15:31:10 +03:00
|
|
|
#include "effects/effects.h"
|
|
|
|
#include "Sound/sound.h"
|
|
|
|
#include "Specific/prng.h"
|
|
|
|
#include "Specific/setup.h"
|
|
|
|
#include "Scripting/GameScriptLevel.h"
|
|
|
|
|
|
|
|
using namespace TEN::Math::Random;
|
2021-09-14 12:05:36 +03:00
|
|
|
|
|
|
|
namespace TEN {
|
|
|
|
namespace Effects {
|
2021-09-15 11:13:47 +03:00
|
|
|
namespace Environment
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 11:13:47 +03:00
|
|
|
EnvironmentController Weather;
|
2021-09-14 12:05:36 +03:00
|
|
|
|
2021-11-10 15:31:10 +03:00
|
|
|
float WeatherParticle::Transparency()
|
|
|
|
{
|
|
|
|
if (Life <= WEATHER_PARTICLES_NEAR_DEATH_LIFE_VALUE)
|
|
|
|
return Life / (float)WEATHER_PARTICLES_NEAR_DEATH_LIFE_VALUE;
|
|
|
|
|
|
|
|
if ((StartLife - Life) < (float)WEATHER_PARTICLES_NEAR_DEATH_LIFE_VALUE)
|
|
|
|
return (StartLife - Life) / (float)WEATHER_PARTICLES_NEAR_DEATH_LIFE_VALUE;
|
|
|
|
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
2021-11-08 10:55:12 +03:00
|
|
|
EnvironmentController::EnvironmentController()
|
|
|
|
{
|
|
|
|
Particles.reserve(WEATHER_PARTICLES_COUNT);
|
|
|
|
}
|
|
|
|
|
2021-09-15 11:13:47 +03:00
|
|
|
void EnvironmentController::Update()
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
|
|
|
GameScriptLevel* level = g_GameFlow->GetLevel(CurrentLevel);
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
UpdateSky(level);
|
|
|
|
UpdateStorm(level);
|
|
|
|
UpdateWind(level);
|
|
|
|
UpdateFlash(level);
|
2021-11-08 10:55:12 +03:00
|
|
|
UpdateWeather(level);
|
2021-09-15 12:58:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentController::Clear()
|
|
|
|
{
|
|
|
|
// Clear storm vars
|
|
|
|
StormTimer = 0;
|
|
|
|
StormSkyColor = 1;
|
|
|
|
StormSkyColor2 = 1;
|
|
|
|
|
|
|
|
// Clear wind vars
|
|
|
|
WindCurrent = WindFinalX = WindFinalZ = 0;
|
|
|
|
WindAngle = WindDAngle = 2048;
|
|
|
|
|
|
|
|
// Clear flash vars
|
|
|
|
FlashProgress = 0.0f;
|
2021-09-15 14:24:03 +03:00
|
|
|
FlashColorBase = Vector3::Zero;
|
2021-09-15 12:58:07 +03:00
|
|
|
}
|
2021-09-15 11:13:47 +03:00
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
void EnvironmentController::Flash(int r, int g, int b, float speed)
|
|
|
|
{
|
|
|
|
FlashProgress = 1.0f;
|
2021-09-15 13:16:14 +03:00
|
|
|
FlashSpeed = std::clamp(speed, 0.005f, 1.0f);
|
2021-09-15 14:24:03 +03:00
|
|
|
FlashColorBase = Vector3(std::clamp(r, 0, UCHAR_MAX) / (float)UCHAR_MAX,
|
2021-09-15 12:58:07 +03:00
|
|
|
std::clamp(g, 0, UCHAR_MAX) / (float)UCHAR_MAX,
|
2021-09-15 14:24:03 +03:00
|
|
|
std::clamp(b, 0, UCHAR_MAX) / (float)UCHAR_MAX);
|
2021-09-15 12:58:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentController::UpdateSky(GameScriptLevel* level)
|
|
|
|
{
|
2021-09-14 12:05:36 +03:00
|
|
|
if (level->Layer1.Enabled)
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyPosition1 += level->Layer1.CloudSpeed;
|
|
|
|
if (SkyPosition1 <= 9728)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
if (SkyPosition1 < 0)
|
|
|
|
SkyPosition1 += 9728;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyPosition1 -= 9728;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level->Layer2.Enabled)
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyPosition2 += level->Layer2.CloudSpeed;
|
|
|
|
if (SkyPosition2 <= 9728)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
if (SkyPosition2 < 0)
|
|
|
|
SkyPosition2 += 9728;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyPosition2 -= 9728;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-15 12:58:07 +03:00
|
|
|
}
|
2021-09-14 12:05:36 +03:00
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
void EnvironmentController::UpdateStorm(GameScriptLevel* level)
|
|
|
|
{
|
2021-09-14 12:05:36 +03:00
|
|
|
auto color = Vector4(level->Layer1.R / 255.0f, level->Layer1.G / 255.0f, level->Layer1.B / 255.0f, 1.0f);
|
|
|
|
|
|
|
|
if (level->Storm)
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
if (StormCount || StormRand)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
UpdateLightning();
|
2021-09-14 12:05:36 +03:00
|
|
|
if (StormTimer > -1)
|
|
|
|
StormTimer--;
|
|
|
|
if (!StormTimer)
|
|
|
|
SoundEffect(SFX_TR4_THUNDER_RUMBLE, NULL, 0);
|
|
|
|
}
|
|
|
|
else if (!(rand() & 0x7F))
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
StormCount = (rand() & 0x1F) + 16;
|
2021-09-14 12:05:36 +03:00
|
|
|
StormTimer = (rand() & 3) + 12;
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
auto flashBrightness = StormSkyColor / 255.0f;
|
2021-09-14 12:05:36 +03:00
|
|
|
auto r = std::clamp(color.x + flashBrightness, 0.0f, 1.0f);
|
|
|
|
auto g = std::clamp(color.y + flashBrightness, 0.0f, 1.0f);
|
|
|
|
auto b = std::clamp(color.z + flashBrightness, 0.0f, 1.0f);
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyCurrentColor = Vector4(r, g, b, color.w);
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
else
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyCurrentColor = color;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
void EnvironmentController::UpdateLightning()
|
2021-09-14 16:00:06 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
StormCount--;
|
2021-09-15 11:13:47 +03:00
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
if (StormCount <= 0)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
StormSkyColor = 0;
|
|
|
|
StormRand = 0;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
2021-09-15 12:58:07 +03:00
|
|
|
else if (StormCount < 5 && StormSkyColor < 50)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
auto newColor = StormSkyColor - StormCount * 2;
|
2021-09-14 12:05:36 +03:00
|
|
|
if (newColor < 0)
|
|
|
|
newColor = 0;
|
2021-09-15 12:58:07 +03:00
|
|
|
StormSkyColor = newColor;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
2021-09-15 12:58:07 +03:00
|
|
|
else if (StormCount)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
StormRand = ((rand() & 0x1FF - StormRand) >> 1) + StormRand;
|
|
|
|
StormSkyColor2 += StormRand * StormSkyColor2 >> 8;
|
|
|
|
StormSkyColor = StormSkyColor2;
|
|
|
|
if (StormSkyColor > 255)
|
|
|
|
StormSkyColor = 255;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
void EnvironmentController::UpdateWind(GameScriptLevel* level)
|
|
|
|
{
|
|
|
|
WindCurrent += (GetRandomControl() & 7) - 3;
|
|
|
|
if (WindCurrent <= -2)
|
|
|
|
WindCurrent++;
|
|
|
|
else if (WindCurrent >= 9)
|
|
|
|
WindCurrent--;
|
|
|
|
|
|
|
|
WindDAngle = (WindDAngle + 2 * (GetRandomControl() & 63) - 64) & 0x1FFE;
|
|
|
|
|
|
|
|
if (WindDAngle < 1024)
|
|
|
|
WindDAngle = 2048 - WindDAngle;
|
|
|
|
else if (WindDAngle > 3072)
|
|
|
|
WindDAngle += 6144 - 2 * WindDAngle;
|
|
|
|
|
|
|
|
WindAngle = (WindAngle + ((WindDAngle - WindAngle) >> 3)) & 0x1FFE;
|
|
|
|
|
|
|
|
WindFinalX = WindCurrent * phd_sin(WindAngle << 3);
|
|
|
|
WindFinalZ = WindCurrent * phd_cos(WindAngle << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentController::UpdateFlash(GameScriptLevel* level)
|
|
|
|
{
|
|
|
|
if (FlashProgress > 0.0f)
|
|
|
|
{
|
|
|
|
FlashProgress -= FlashSpeed;
|
|
|
|
if (FlashProgress < 0.0f)
|
|
|
|
FlashProgress = 0.0f;
|
|
|
|
}
|
2021-09-15 14:24:03 +03:00
|
|
|
|
|
|
|
if (FlashProgress == 0.0f)
|
|
|
|
FlashColorBase = Vector3::Zero;
|
2021-09-15 12:58:07 +03:00
|
|
|
}
|
2021-11-08 10:55:12 +03:00
|
|
|
|
|
|
|
void EnvironmentController::UpdateWeather(GameScriptLevel* level)
|
|
|
|
{
|
|
|
|
int newParticlesCount = 0;
|
|
|
|
|
|
|
|
while (Particles.size() < WEATHER_PARTICLES_COUNT)
|
|
|
|
{
|
2021-11-10 15:31:10 +03:00
|
|
|
if (newParticlesCount > WEATHER_PARTICLES_SPAWN_DENSITY)
|
2021-11-08 10:55:12 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
newParticlesCount++;
|
|
|
|
|
2021-11-08 22:01:50 +03:00
|
|
|
auto radius = (GetRandomDraw() & (WALL_SIZE * 8 - 1)) - WALL_SIZE * 4;
|
|
|
|
short angle = (GetRandomDraw() & 0xFFFF);
|
2021-11-08 10:55:12 +03:00
|
|
|
|
2021-11-08 21:25:52 +03:00
|
|
|
auto xPos = Camera.pos.x + ((int)(phd_cos(angle) * radius));
|
|
|
|
auto zPos = Camera.pos.z + ((int)(phd_sin(angle) * radius));
|
2021-11-08 22:01:50 +03:00
|
|
|
auto yPos = Camera.pos.y - (WALL_SIZE * 2 + GetRandomDraw() & (WALL_SIZE * 2 - 1));
|
2021-11-08 10:55:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
if (IsRoomOutside(xPos, yPos, zPos) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (g_Level.Rooms[IsRoomOutsideNo].flags & ENV_FLAG_WATER)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto part = WeatherParticle();
|
|
|
|
|
|
|
|
part.Room = IsRoomOutsideNo;
|
|
|
|
part.Position.x = xPos;
|
|
|
|
part.Position.y = yPos;
|
|
|
|
part.Position.z = zPos;
|
|
|
|
part.Stopped = false;
|
2021-11-08 21:25:52 +03:00
|
|
|
part.Enabled = true;
|
2021-11-08 10:55:12 +03:00
|
|
|
part.Velocity.x = (GetRandomDraw() & 7) - 4;
|
|
|
|
part.Velocity.y = ((GetRandomDraw() & 15) + 8) << 3;
|
|
|
|
part.Velocity.z = (GetRandomDraw() & 7) - 4;
|
|
|
|
part.Life = 48 + (64 - ((int)part.Velocity.y >> 2));
|
2021-11-10 15:31:10 +03:00
|
|
|
part.StartLife = part.Life;
|
2021-11-08 10:55:12 +03:00
|
|
|
|
2021-11-08 21:25:52 +03:00
|
|
|
part.Type = WeatherType::Snow;
|
|
|
|
part.Size = 16 + (GetRandomDraw() & 7 - 4);
|
|
|
|
|
2021-11-08 10:55:12 +03:00
|
|
|
Particles.push_back(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < Particles.size(); i++)
|
|
|
|
{
|
|
|
|
auto& p = Particles[i];
|
|
|
|
|
|
|
|
auto oldPos = p.Position;
|
|
|
|
|
2021-11-08 21:25:52 +03:00
|
|
|
if (!p.Stopped)
|
|
|
|
{
|
|
|
|
p.Position.x += p.Velocity.x;
|
|
|
|
p.Position.y += ((int)p.Velocity.y & (~7)) >> 1;
|
|
|
|
p.Position.z += p.Velocity.z;
|
|
|
|
}
|
2021-11-08 10:55:12 +03:00
|
|
|
|
|
|
|
auto& r = g_Level.Rooms[p.Room];
|
|
|
|
|
|
|
|
if (p.Position.y <= r.maxceiling || p.Position.y >= r.minfloor ||
|
2021-11-08 21:25:52 +03:00
|
|
|
p.Position.z <= (r.z + WALL_SIZE) || p.Position.z >= (r.z + ((r.zSize - 1) << 10)) ||
|
|
|
|
p.Position.x <= (r.x + WALL_SIZE) || p.Position.x >= (r.x + ((r.xSize - 1) << 10)))
|
2021-11-08 10:55:12 +03:00
|
|
|
{
|
2021-11-08 21:25:52 +03:00
|
|
|
auto coll = GetCollisionResult(p.Position.x, p.Position.y, p.Position.z, p.Room);
|
|
|
|
bool inSubstance = g_Level.Rooms[coll.RoomNumber].flags & (ENV_FLAG_WATER | ENV_FLAG_SWAMP);
|
|
|
|
bool landed = coll.Position.Floor < p.Position.y;
|
2021-11-08 10:55:12 +03:00
|
|
|
|
2021-11-08 21:25:52 +03:00
|
|
|
if (coll.RoomNumber == p.Room)
|
2021-11-08 10:55:12 +03:00
|
|
|
{
|
2021-11-08 21:39:38 +03:00
|
|
|
p.Enabled = false; // Spawned in same room, needs to be on portal
|
2021-11-08 10:55:12 +03:00
|
|
|
continue;
|
|
|
|
}
|
2021-11-08 21:25:52 +03:00
|
|
|
else if (inSubstance || landed)
|
2021-11-08 10:55:12 +03:00
|
|
|
{
|
2021-11-08 21:25:52 +03:00
|
|
|
p.Stopped = true;
|
2021-11-08 10:55:12 +03:00
|
|
|
p.Position = oldPos;
|
2021-11-10 15:31:10 +03:00
|
|
|
p.Life = (p.Life > WEATHER_PARTICLES_NEAR_DEATH_LIFE_VALUE) ? WEATHER_PARTICLES_NEAR_DEATH_LIFE_VALUE : p.Life;
|
|
|
|
|
|
|
|
if (inSubstance)
|
|
|
|
{
|
|
|
|
SetupRipple(p.Position.x, p.Position.y, p.Position.z, GenerateFloat(16, 24),
|
|
|
|
RIPPLE_FLAG_SHORT_LIFE | RIPPLE_FLAG_RAND_ROT | RIPPLE_FLAG_LOW_OPACITY,
|
|
|
|
Objects[ID_DEFAULT_SPRITES].meshIndex + SPR_RIPPLES);
|
|
|
|
}
|
2021-11-08 10:55:12 +03:00
|
|
|
}
|
|
|
|
else
|
2021-11-08 21:25:52 +03:00
|
|
|
p.Room = coll.RoomNumber;
|
2021-11-08 10:55:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!p.Life ||
|
|
|
|
abs(Camera.pos.x - p.Position.x) > 6000 ||
|
|
|
|
abs(Camera.pos.z - p.Position.z) > 6000)
|
|
|
|
{
|
|
|
|
if (!p.Life)
|
|
|
|
{
|
2021-11-08 21:25:52 +03:00
|
|
|
p.Enabled = false; // Turn it off.
|
2021-11-08 10:55:12 +03:00
|
|
|
continue;
|
|
|
|
}
|
2021-11-10 15:31:10 +03:00
|
|
|
else if (p.Life > WEATHER_PARTICLES_NEAR_DEATH_LIFE_VALUE)
|
|
|
|
p.Life = WEATHER_PARTICLES_NEAR_DEATH_LIFE_VALUE;
|
2021-11-08 10:55:12 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 21:25:52 +03:00
|
|
|
if (!p.Stopped)
|
|
|
|
{
|
|
|
|
if (p.Velocity.x < (WindFinalX << 2))
|
|
|
|
p.Velocity.x += 2;
|
|
|
|
else if (p.Velocity.x > (WindFinalX << 2))
|
|
|
|
p.Velocity.x -= 2;
|
|
|
|
|
|
|
|
if (p.Velocity.z < (WindFinalZ << 2))
|
|
|
|
p.Velocity.z += 2;
|
|
|
|
else if (p.Velocity.z > (WindFinalZ << 2))
|
|
|
|
p.Velocity.z -= 2;
|
|
|
|
|
|
|
|
if (((int)p.Velocity.y & 7) < 7)
|
|
|
|
p.Velocity.y++;
|
|
|
|
}
|
2021-11-08 10:55:12 +03:00
|
|
|
|
|
|
|
p.Life -= 2;
|
|
|
|
}
|
|
|
|
|
2021-11-08 21:39:38 +03:00
|
|
|
// Clean up dead particles
|
|
|
|
if (Particles.size() > 0)
|
|
|
|
Particles.erase(std::remove_if(Particles.begin(), Particles.end(), [](const WeatherParticle& part) { return !part.Enabled; }), Particles.end());
|
|
|
|
// std::erase_if(Particles, [](const WeatherParticle& part) { return !part.Enabled; });
|
2021-11-08 10:55:12 +03:00
|
|
|
}
|
2021-09-14 12:05:36 +03:00
|
|
|
}}}
|