| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "pserror.h" |
| 20 | #include "pstypes.h" |
| 21 | #include "fireball.h" |
| 22 | #include "weather.h" |
| 23 | #include "viseffect.h" |
| 24 | #include "object.h" |
| 25 | #include "terrain.h" |
| 26 | #include "room.h" |
| 27 | #include "game.h" |
| 28 | #include "soundload.h" |
| 29 | #include "hlsoundlib.h" |
| 30 | #include "sounds.h" |
| 31 | |
| 32 | #include <stdlib.h> |
| 33 | |
| 34 | #include "psrand.h" |
| 35 | weather Weather = {0}; |
| 36 | |
| 37 | int ThunderA_sound_handle = -1; |
| 38 | int ThunderB_sound_handle = -1; |
| 39 | |
| 40 | // resets the weather so there is nothing happening |
| 41 | void ResetWeather() { |
| 42 | Weather.flags = 0; |
| 43 | Weather.last_lightning_evaluation_time = 0; |
| 44 | } |
| 45 | |
| 46 | // Makes droplets appear on the windshield, plus makes rain fall in the distance |
| 47 | void DoRainEffect() { |
| 48 | // See how many droplets to create on the windshield |
| 49 | // This is dependant on how fast the player is moving forward |
| 50 | int randval = 1 + ((1.0 - Weather.rain_intensity_scalar) * MAX_RAIN_INTENSITY); |
| 51 | |
| 52 | if (randval < 20) |
| 53 | randval = 20; |
| 54 | |
| 55 | vector vel = Viewer_object->mtype.phys_info.velocity; |
| 56 | float mag = vm_GetMagnitudeFast(&vel); |
| 57 | vel /= mag; |
| 58 | |
| 59 | float scalar = vm_DotProduct(&vel, &Viewer_object->orient.fvec); |
| 60 | |
| 61 | vector upvec = {0, 1.0, 0}; |
| 62 | |
| 63 | scalar *= (1 + (mag / 100)); |
| 64 | |
| 65 | randval -= scalar * 10; |
| 66 | |
| 67 | if (randval < 2) |
| 68 | randval = 2; |
| 69 | else if (randval > 80) |
| 70 | randval = 80; |
| 71 | |
| 72 | if ((upvec * Viewer_object->orient.uvec) < 0) |
| 73 | randval = 8000; // Make sure rain does fall upwards |
| 74 | |
| 75 | if (Viewer_object->type == OBJ_PLAYER && OBJECT_OUTSIDE(Viewer_object) && (ps_rand() % randval) == 0) { |
| 76 | // Put some droplets on the windshield |
| 77 | vector pos = {0, 0, 0}; |
| 78 | |
| 79 | pos.x = ((ps_rand() % 1000) - 500) / 250.0; |
| 80 | pos.y = ((ps_rand() % 1000) - 500) / 350.0; |
| 81 | pos.z = 3.0; |
| 82 | |
| 83 | int visnum = VisEffectCreate(VIS_FIREBALL, RAINDROP_INDEX, Viewer_object->roomnum, &pos); |
| 84 | if (visnum >= 0) { |
| 85 | vis_effect *vis = &VisEffects[visnum]; |
| 86 | float life = .4 + ((ps_rand() % 500) / 1000.0); |
| 87 | vis->lifeleft = life; |
| 88 | vis->lifetime = life; |
| 89 | vis->size = .01 + ((ps_rand() % 500) / 3000.0); |
| 90 | |
| 91 | if (vis->size > .05) { |
| 92 | Sound_system.Play2dSound(SOUND_RAINDROP); |
| 93 | } |
| 94 | |
| 95 | vis->flags |= VF_WINDSHIELD_EFFECT; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Create some rain in the distance |
| 100 | |
| 101 | if (OBJECT_OUTSIDE(Viewer_object)) { |
| 102 | int num = 20 + (ps_rand() % 15); |
| 103 | angvec angs; |
| 104 | int i; |
| 105 | |
| 106 | vm_ExtractAnglesFromMatrix(&angs, &Viewer_object->orient); |
| 107 | matrix mat; |
| 108 | vm_AnglesToMatrix(&mat, 0, angs.h, 0); |
| 109 | |
| 110 | for (i = 0; i < num; i++) { |
| 111 | vector pos = Viewer_object->pos; |
| 112 | |
| 113 | float z = ((ps_rand() % 1000) / 1000.0) * 700; |
| 114 | float x = (((ps_rand() % 1000) - 500) / 500.0) * 300; |
| 115 | float y = (((ps_rand() % 1000) - 500) / 500.0) * 200; |
| 116 | |
| 117 | pos += x * mat.rvec; |
| 118 | pos += y * mat.uvec; |
| 119 | pos += z * mat.fvec; |
| 120 | |
| 121 | // Create falling rain |
| 122 | int visnum = VisEffectCreate(VIS_FIREBALL, FADING_LINE_INDEX, Viewer_object->roomnum, &pos); |
| 123 | if (visnum >= 0) { |
| 124 | vis_effect *vis = &VisEffects[visnum]; |
| 125 | float life = .001f; |
| 126 | vis->lifeleft = life; |
| 127 | vis->lifetime = life; |
| 128 | vis->lighting_color = GR_RGB16(200, 200, 255); |
| 129 | vis->end_pos = pos; |
| 130 | vis->end_pos.y += 20; |
| 131 | vis->flags |= VF_WINDSHIELD_EFFECT; |
| 132 | vis->pos -= (Viewer_object->mtype.phys_info.velocity / 2); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | num /= 2; |
| 137 | for (i = 0; i < num; i++) { |
| 138 | vector pos = Viewer_object->pos; |
| 139 | |
| 140 | float z = ((ps_rand() % 1000) / 1000.0) * 700; |
| 141 | float x = (((ps_rand() % 1000) - 500) / 500.0) * 300; |
| 142 | float y = (((ps_rand() % 1000) - 500) / 500.0) * 200; |
| 143 | |
| 144 | pos += x * mat.rvec; |
| 145 | pos += y * mat.uvec; |
| 146 | pos += z * mat.fvec; |
| 147 | |
| 148 | if (pos.z < 0 || pos.z >= TERRAIN_DEPTH * TERRAIN_SIZE) |
| 149 | continue; |
| 150 | if (pos.x < 0 || pos.x >= TERRAIN_WIDTH * TERRAIN_SIZE) |
| 151 | continue; |
| 152 | |
| 153 | // Create puddle drops on the terrain |
| 154 | vector norm; |
| 155 | float ypos = GetTerrainGroundPoint(&pos, &norm); |
| 156 | pos.y = ypos; |
| 157 | int visnum = VisEffectCreate(VIS_FIREBALL, PUDDLEDROP_INDEX, Viewer_object->roomnum, &pos); |
| 158 | if (visnum >= 0) { |
| 159 | vis_effect *vis = &VisEffects[visnum]; |
| 160 | float life = .2f; |
| 161 | float size = .7 + ((ps_rand() % 10) / 20.0); |
| 162 | vis->lifeleft = life; |
| 163 | vis->lifetime = life; |
| 164 | vis->end_pos = norm; |
| 165 | vis->flags |= VF_PLANAR; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Creates snow for this frame |
| 172 | void DoSnowEffect() { |
| 173 | if (OBJECT_OUTSIDE(Viewer_object)) { |
| 174 | int num = 20 + (ps_rand() % 15); |
| 175 | |
| 176 | if (Weather.snowflakes_to_create + num > 250) { |
| 177 | num = 250 - Weather.snowflakes_to_create; |
| 178 | |
| 179 | if (num < 1) { |
| 180 | Weather.snowflakes_to_create = 0; |
| 181 | return; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // angvec angs; |
| 186 | matrix mat; |
| 187 | |
| 188 | // vm_ExtractAnglesFromMatrix (&angs,&Viewer_object->orient); |
| 189 | // vm_AnglesToMatrix (&mat,0,angs.h,0); |
| 190 | |
| 191 | mat = Viewer_object->orient; |
| 192 | |
| 193 | for (int i = 0; i < num; i++) { |
| 194 | vector pos = Viewer_object->pos; |
| 195 | |
| 196 | float z = ((ps_rand() % 1000) / 1000.0) * 300; |
| 197 | float x = (((ps_rand() % 1000) - 500) / 500.0) * 200; |
| 198 | int y = (ps_rand() % 80); |
| 199 | |
| 200 | pos += x * mat.rvec; |
| 201 | pos += y * mat.uvec; |
| 202 | pos += z * mat.fvec; |
| 203 | |
| 204 | if (pos.z < 0 || pos.z >= TERRAIN_DEPTH * TERRAIN_SIZE) |
| 205 | continue; |
| 206 | if (pos.x < 0 || pos.x >= TERRAIN_WIDTH * TERRAIN_SIZE) |
| 207 | continue; |
| 208 | |
| 209 | // Create falling rain |
| 210 | vector down_vec = {0, -30, 0}; |
| 211 | int visnum = VisEffectCreate(VIS_FIREBALL, SNOWFLAKE_INDEX, Viewer_object->roomnum, &pos); |
| 212 | if (visnum >= 0) { |
| 213 | vis_effect *vis = &VisEffects[visnum]; |
| 214 | float life = 1.5 + ((ps_rand() % 100) / 100.0); |
| 215 | vis->lifeleft = life; |
| 216 | vis->lifetime = life; |
| 217 | vis->lighting_color = GR_RGB16(200, 200, (ps_rand() % 50) + 200); |
| 218 | vis->size = ((ps_rand() % 1000) / 1000.0) + .5; |
| 219 | |
| 220 | vis->flags |= VF_WINDSHIELD_EFFECT | VF_USES_LIFELEFT; |
| 221 | |
| 222 | vis->velocity = down_vec; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | Weather.snowflakes_to_create = 0; |
| 228 | } |
| 229 | |
| 230 | // does all the weather stuff that is going to be done for this frame |
| 231 | void DoWeatherForFrame() { |
| 232 | int i; |
| 233 | int hear_thunder = 0; |
| 234 | |
| 235 | if (OBJECT_OUTSIDE(Player_object)) |
| 236 | hear_thunder = 1; |
| 237 | else { |
| 238 | int roomnum = Player_object->roomnum; |
| 239 | room *rp = &Rooms[roomnum]; |
| 240 | |
| 241 | for (i = 0; i < rp->num_portals && hear_thunder == 0; i++) { |
| 242 | if (rp->portals[i].croom == -1) |
| 243 | hear_thunder = 1; |
| 244 | else if (Rooms[rp->portals[i].croom].flags & RF_EXTERNAL) |
| 245 | hear_thunder = 1; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (Weather.flags & WEATHER_FLAGS_RAIN) |
| 250 | DoRainEffect(); |
| 251 | |
| 252 | if (Weather.flags & WEATHER_FLAGS_SNOW) |
| 253 | DoSnowEffect(); |
| 254 | |
| 255 | if (Weather.flags & WEATHER_FLAGS_LIGHTNING) { |
| 256 | if ((Gametime - Weather.last_lightning_evaluation_time) > Weather.lightning_interval_time) { |
| 257 | // Check to see if we should draw some lightning |
| 258 | Weather.last_lightning_evaluation_time = Gametime; |
| 259 | ASSERT(Weather.lightning_rand_value > 0); |
| 260 | if (ps_rand() <= Weather.lightning_rand_value) { |
| 261 | // Start the lightning sequence |
| 262 | Weather.lightning_sequence = 1; |
| 263 | |
| 264 | if ((ps_rand() % 7) == 0 && hear_thunder) |
| 265 | Sound_system.Play2dSound(SOUND_LIGHTNING); |
| 266 | } |
| 267 | |
| 268 | if (hear_thunder && (ps_rand() % (Weather.lightning_rand_value * 3)) == 0) { |
| 269 | if (ThunderA_sound_handle == -1) |
| 270 | ThunderA_sound_handle = FindSoundName("ThunderA"); |
| 271 | if (ThunderB_sound_handle == -1) |
| 272 | ThunderB_sound_handle = FindSoundName("ThunderB"); |
| 273 | |
| 274 | if (ps_rand() % 2) |
| 275 | Sound_system.Play2dSound(ThunderA_sound_handle); |
| 276 | else |
| 277 | Sound_system.Play2dSound(ThunderB_sound_handle); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Sets the state of the rain to on or off, plus sets the intensity of the rain (0 to 1) |
| 284 | void SetRainState(int on, float intensity) { |
| 285 | if (on) { |
| 286 | Weather.flags |= WEATHER_FLAGS_RAIN; |
| 287 | Weather.rain_intensity_scalar = intensity; |
| 288 | } else { |
| 289 | Weather.flags &= ~WEATHER_FLAGS_RAIN; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // Sets the state of the rain to on or off, plus sets the intensity of the rain (0 to 1) |
| 294 | void SetSnowState(int on, float intensity) { |
| 295 | if (on) { |
| 296 | Weather.flags |= WEATHER_FLAGS_SNOW; |
| 297 | Weather.snow_intensity_scalar = intensity; |
| 298 | } else { |
| 299 | Weather.flags &= ~WEATHER_FLAGS_SNOW; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Sets the state of lightning to on or off, plus allows the setting of how often to check |
| 304 | // for lightning and the randomness at which lightning happens |
| 305 | void SetLightningState(int on, float interval_time, int randval) { |
| 306 | if (on) { |
| 307 | Weather.flags |= WEATHER_FLAGS_LIGHTNING; |
| 308 | Weather.lightning_sequence = 0; |
| 309 | Weather.lightning_rand_value = randval; |
| 310 | Weather.lightning_interval_time = interval_time; |
| 311 | } else { |
| 312 | Weather.flags &= ~WEATHER_FLAGS_LIGHTNING; |
| 313 | } |
| 314 | } |