| 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 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/scorch.cpp $ |
| 21 | * $Revision: 20 $ |
| 22 | * $Date: 5/13/99 3:42p $ |
| 23 | * $Author: Ardussi $ |
| 24 | * |
| 25 | * System for drawing scorch marks on walls |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/scorch.cpp $ |
| 28 | * |
| 29 | * 20 5/13/99 3:42p Ardussi |
| 30 | * changes for compiling on the Mac |
| 31 | * |
| 32 | * 19 4/30/99 12:10p Matt |
| 33 | * Don't add a scorch if it will overlap an edge of a face. |
| 34 | * |
| 35 | * 18 4/21/99 11:05a Kevin |
| 36 | * new ps_rand and ps_srand to replace rand & srand |
| 37 | * |
| 38 | * 17 4/16/99 11:31p Jeff |
| 39 | * added max() for Linux |
| 40 | * |
| 41 | * 16 1/26/99 5:06p Jason |
| 42 | * no scorch marks on procedurals |
| 43 | * |
| 44 | * 15 1/21/99 11:15p Jeff |
| 45 | * pulled out some structs and defines from header files and moved them |
| 46 | * into seperate header files so that multiplayer dlls don't require major |
| 47 | * game headers, just those new headers. Side effect is a shorter build |
| 48 | * time. Also cleaned up some header file #includes that weren't needed. |
| 49 | * This affected polymodel.h, object.h, player.h, vecmat.h, room.h, |
| 50 | * manage.h and multi.h |
| 51 | * |
| 52 | * 14 12/10/98 12:27p Jason |
| 53 | * added cooler specular mapping for objects |
| 54 | * |
| 55 | * 13 12/03/98 12:26p Jason |
| 56 | * fixed some scorch slowdowns with direct3d |
| 57 | * |
| 58 | * 12 6/24/98 10:19p Matt |
| 59 | * When too many scorches per face, don't add new ones, rather than not |
| 60 | * drawing old ones. |
| 61 | * |
| 62 | * 11 5/26/98 6:15p Matt |
| 63 | * Cleaned up and finished scorch mark limiting code. |
| 64 | * |
| 65 | * 10 5/26/98 3:59p Jason |
| 66 | * added LIGHTMAP_BLEND_CONSTANT alpha type |
| 67 | * |
| 68 | * 9 5/26/98 2:38p Matt |
| 69 | * Added code (not really done yet) to fade out far-away marks, and to |
| 70 | * limit the number of marks drawn. |
| 71 | * |
| 72 | * 8 5/25/98 8:36p Matt |
| 73 | * Added code to set different sizes for different weapon scorch marks. |
| 74 | * Also, don't leave scorch marks on lights. |
| 75 | * |
| 76 | * 7 5/22/98 5:58p Matt |
| 77 | * Bail (for now) if try to make a scorch mark on the ground. |
| 78 | * |
| 79 | * 6 5/22/98 5:20p Matt |
| 80 | * Added randomized rotations for scorces |
| 81 | * |
| 82 | * 5 5/22/98 4:32p Jason |
| 83 | * fixed stupid z buffer write mask bug |
| 84 | * |
| 85 | * 4 5/22/98 3:57p Jason |
| 86 | * fixed a zbuffer problem with scorches |
| 87 | * |
| 88 | * 3 5/22/98 1:16p Matt |
| 89 | * Fixed stupid bug |
| 90 | * |
| 91 | * 2 5/22/98 12:34p Matt |
| 92 | * Added scorch mark/bullet hole system. |
| 93 | * |
| 94 | * 1 5/21/98 11:32p Matt |
| 95 | * |
| 96 | */ |
| 97 | |
| 98 | #include <stdlib.h> |
| 99 | |
| 100 | #include "pserror.h" |
| 101 | #include "renderer.h" |
| 102 | #include "3d.h" |
| 103 | #include "scorch.h" |
| 104 | #include "room.h" |
| 105 | #include "config.h" |
| 106 | #include "object_external_struct.h" //for ROOMNUM_OUTSIDE macro |
| 107 | #include "psrand.h" |
| 108 | |
| 109 | #include <algorithm> |
| 110 | |
| 111 | // Structure for storing scorch marks |
| 112 | struct scorch { |
| 113 | int roomface; // room number & face number combined into an int |
| 114 | vector pos; // the position of the center of the scorch mark |
| 115 | uint8_t handle_index; // which mark? |
| 116 | int8_t rx, ry, rz; // right vector |
| 117 | int8_t ux, uy, uz; // up vector |
| 118 | uint8_t size; // floating-point size times 16 |
| 119 | }; |
| 120 | |
| 121 | // How many scorch marks |
| 122 | #define MAX_SCORCHES 500 |
| 123 | |
| 124 | // Buffer of scorch marks |
| 125 | scorch Scorches[MAX_SCORCHES]; |
| 126 | |
| 127 | // Indices into scorch buffer. Start is oldest item, end is newest item |
| 128 | int Scorch_start, Scorch_end; |
| 129 | |
| 130 | // Bitmap handles for scorches |
| 131 | #define MAX_SCORCH_TEXTURES 10 |
| 132 | int Num_scorch_textures = 0; |
| 133 | int Scorch_texture_handles[MAX_SCORCH_TEXTURES]; |
| 134 | |
| 135 | // Pack & unpack roomnum & facenum into an int |
| 136 | #define ROOMFACE(r, f) ((r << 16) + f) |
| 137 | #define RF_ROOM(rf) (rf >> 16) |
| 138 | #define RF_FACE(rf) (rf & 0xffff) |
| 139 | |
| 140 | // Value used to limit the number of scorches drawn per face |
| 141 | #define MAX_VIS_COUNT 30 |
| 142 | |
| 143 | // Called when a new level is started to reset the scorch list |
| 144 | void ResetScorches() { Scorch_start = Scorch_end = -1; } |
| 145 | |
| 146 | // Delete the specified scorch mark |
| 147 | void DeleteScorch(int index) { |
| 148 | int roomface = Scorches[index].roomface; |
| 149 | scorch *sp; |
| 150 | int i; |
| 151 | |
| 152 | // mprintf(0,"Deleting scorch %d\n",index); |
| 153 | |
| 154 | // Look through all the scorches to see if there are other scorches on the same face |
| 155 | for (i = Scorch_start, sp = &Scorches[Scorch_start];;) { |
| 156 | |
| 157 | if ((sp->roomface == roomface) && (i != index)) // Found another scorch |
| 158 | return; |
| 159 | |
| 160 | // Check for end of loop |
| 161 | if (i == Scorch_end) |
| 162 | break; |
| 163 | |
| 164 | // Increment & wrap |
| 165 | if (++i == MAX_SCORCHES) { |
| 166 | i = 0; |
| 167 | sp = Scorches; |
| 168 | } else |
| 169 | sp++; |
| 170 | } |
| 171 | |
| 172 | // If we're here, there are no other scorches on the face, so clear the flag |
| 173 | Rooms[RF_ROOM(roomface)].faces[RF_FACE(roomface)].flags &= ~FF_SCORCHED; |
| 174 | // mprintf(0,"Clearing scorch flag from %d:%d\n",RF_ROOM(roomface),RF_FACE(roomface)); |
| 175 | } |
| 176 | |
| 177 | // Add a scorch mark |
| 178 | // Parameters: roomnum,facenum - the face that the scorch is on |
| 179 | // pos - where the scorch goes |
| 180 | // texture_handle - the texture for the scorch mark |
| 181 | // size - how big the scorch is |
| 182 | void AddScorch(int roomnum, int facenum, vector *pos, int texture_handle, float size) { |
| 183 | // Bail if on terrain. We should probably add support for this later. |
| 184 | if (ROOMNUM_OUTSIDE(roomnum)) |
| 185 | return; |
| 186 | |
| 187 | room *rp = &Rooms[roomnum]; |
| 188 | face *fp = &rp->faces[facenum]; |
| 189 | |
| 190 | // Check if face is a light, and if so don't add the scorch |
| 191 | if (GameTextures[fp->tmap].flags & (TF_LIGHT | TF_PROCEDURAL)) |
| 192 | return; |
| 193 | |
| 194 | int roomface = ROOMFACE(roomnum, facenum); |
| 195 | |
| 196 | // Count the number of scorches on this face, and bail if already at max |
| 197 | int count = 0; |
| 198 | int i; |
| 199 | scorch *sp; |
| 200 | for (i = Scorch_start, sp = &Scorches[Scorch_start];;) { |
| 201 | float size = (float)sp->size / 16.0; |
| 202 | |
| 203 | // Increment count, and stop drawing if hit limit |
| 204 | if (sp->roomface == roomface) { // Found one! |
| 205 | count += std::max((int)size, 1); // count large marks more |
| 206 | if (count >= MAX_VIS_COUNT) |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | // Check for end of loop |
| 211 | if (i == Scorch_end) |
| 212 | break; |
| 213 | |
| 214 | // Increment & wrap |
| 215 | if (++i == MAX_SCORCHES) { |
| 216 | i = 0; |
| 217 | sp = Scorches; |
| 218 | } else |
| 219 | sp++; |
| 220 | } |
| 221 | |
| 222 | // Check for scorch going off the edge of the face, and bail if does |
| 223 | vector *v0, *v1 = &rp->verts[fp->face_verts[fp->num_verts - 1]]; |
| 224 | for (int v = 0; v < fp->num_verts; v++) { |
| 225 | vector edgevec, checkvec, checkp; |
| 226 | float dot, dist; |
| 227 | |
| 228 | v0 = v1; |
| 229 | v1 = &rp->verts[fp->face_verts[v]]; |
| 230 | vm_GetNormalizedDir(&edgevec, v1, v0); |
| 231 | |
| 232 | checkvec = *pos - *v0; |
| 233 | dot = checkvec * edgevec; |
| 234 | checkp = *v0 + edgevec * dot; |
| 235 | dist = vm_VectorDistance(&checkp, pos); |
| 236 | if (dist < size) |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | // Get next item in circular list, freeing oldest item if necessary |
| 241 | int new_end = Scorch_end + 1; |
| 242 | if (new_end == MAX_SCORCHES) |
| 243 | new_end = 0; |
| 244 | if (new_end == Scorch_start) { |
| 245 | DeleteScorch(Scorch_start); |
| 246 | Scorch_start++; |
| 247 | if (Scorch_start == MAX_SCORCHES) |
| 248 | Scorch_start = 0; |
| 249 | } |
| 250 | Scorch_end = new_end; |
| 251 | if (Scorch_start == -1) |
| 252 | Scorch_start = 0; |
| 253 | |
| 254 | // Get a pointer to our struct |
| 255 | sp = &Scorches[Scorch_end]; |
| 256 | |
| 257 | // Fill in the data |
| 258 | sp->roomface = roomface; |
| 259 | sp->pos = *pos; |
| 260 | ASSERT((size >= 0.0) && (size < 15.0)); |
| 261 | sp->size = size * 16.0; |
| 262 | |
| 263 | int handle_index; |
| 264 | |
| 265 | // Get index for handle |
| 266 | for (handle_index = 0; handle_index < Num_scorch_textures; handle_index++) |
| 267 | if (Scorch_texture_handles[handle_index] == texture_handle) |
| 268 | break; |
| 269 | if (handle_index == Num_scorch_textures) { // didn't find one, so add |
| 270 | ASSERT(Num_scorch_textures < MAX_SCORCH_TEXTURES); |
| 271 | Scorch_texture_handles[handle_index] = texture_handle; |
| 272 | Num_scorch_textures++; |
| 273 | } |
| 274 | sp->handle_index = handle_index; |
| 275 | |
| 276 | // Calculate the vectors |
| 277 | matrix m; |
| 278 | vm_VectorAngleToMatrix(&m, &fp->normal, ps_rand() * 2); |
| 279 | |
| 280 | // Store the vectors as signed 8-bit values |
| 281 | sp->rx = -m.rvec.x * 127; |
| 282 | sp->ry = -m.rvec.y * 127; |
| 283 | sp->rz = -m.rvec.z * 127; |
| 284 | |
| 285 | sp->ux = m.uvec.x * 127; |
| 286 | sp->uy = m.uvec.y * 127; |
| 287 | sp->uz = m.uvec.z * 127; |
| 288 | |
| 289 | // Flag this face as being scorched |
| 290 | fp->flags |= FF_SCORCHED; |
| 291 | } |
| 292 | |
| 293 | // The UVs for the blob bitmap |
| 294 | struct { |
| 295 | float u, v; |
| 296 | } scorch_uvs[4] = {{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}}; |
| 297 | |
| 298 | // Values used to get rid of far-away scorches |
| 299 | #define MAX_VIS_DISTANCE 200 // if mark farther than this, don't draw |
| 300 | #define FADE_START_DISTANCE 170 // when mark is this far away, start fading |
| 301 | |
| 302 | // Draw the scorch(es) for a given face |
| 303 | void DrawScorches(int roomnum, int facenum) { |
| 304 | int i; |
| 305 | scorch *sp; |
| 306 | |
| 307 | if (!Detail_settings.Scorches_enabled) |
| 308 | return; |
| 309 | |
| 310 | int roomface = ROOMFACE(roomnum, facenum); |
| 311 | |
| 312 | // Set alpha, transparency, & lighting for this face |
| 313 | if (!StateLimited) { |
| 314 | rend_SetAlphaType(AT_LIGHTMAP_BLEND); |
| 315 | rend_SetAlphaValue(255); |
| 316 | rend_SetLighting(LS_NONE); |
| 317 | rend_SetColorModel(CM_MONO); |
| 318 | rend_SetOverlayType(OT_NONE); |
| 319 | rend_SetZBias(-.5); |
| 320 | rend_SetZBufferWriteMask(0); |
| 321 | |
| 322 | // Select texture type |
| 323 | rend_SetTextureType(TT_LINEAR); |
| 324 | } |
| 325 | |
| 326 | ASSERT(Scorch_end != -1); |
| 327 | |
| 328 | // Loop through all the scorches, and draw the ones for this face |
| 329 | for (i = Scorch_start, sp = &Scorches[Scorch_start];;) { |
| 330 | |
| 331 | if (sp->roomface == roomface) { // Found one! |
| 332 | vector right, up; |
| 333 | vector corners[4]; |
| 334 | g3Point points[4]; |
| 335 | g3Point *pointlist[4]; |
| 336 | float size = (float)sp->size / 16.0; |
| 337 | |
| 338 | // Don't draw mark if too far away, and make smaller those marks that are almost too far |
| 339 | float depth = g3_CalcPointDepth(&sp->pos); |
| 340 | if (depth > MAX_VIS_DISTANCE) |
| 341 | goto skip_draw; |
| 342 | if (depth > FADE_START_DISTANCE) |
| 343 | size *= 1.0 - ((depth - FADE_START_DISTANCE) / (MAX_VIS_DISTANCE - FADE_START_DISTANCE)); |
| 344 | |
| 345 | // Calculate vectors to corners |
| 346 | right.x = (float)sp->rx * (size / 127.0); |
| 347 | right.y = (float)sp->ry * (size / 127.0); |
| 348 | right.z = (float)sp->rz * (size / 127.0); |
| 349 | |
| 350 | up.x = (float)sp->ux * (size / 127.0); |
| 351 | up.y = (float)sp->uy * (size / 127.0); |
| 352 | up.z = (float)sp->uz * (size / 127.0); |
| 353 | |
| 354 | // Compute four corners |
| 355 | corners[0] = sp->pos - right + up; |
| 356 | corners[1] = sp->pos + right + up; |
| 357 | corners[2] = sp->pos + right - up; |
| 358 | corners[3] = sp->pos - right - up; |
| 359 | |
| 360 | // Rotate the points. Set uv values |
| 361 | for (int p = 0; p < 4; p++) { |
| 362 | g3_RotatePoint(&points[p], &corners[p]); |
| 363 | pointlist[p] = &points[p]; |
| 364 | points[p].p3_uvl.u = scorch_uvs[p].u; |
| 365 | points[p].p3_uvl.v = scorch_uvs[p].v; |
| 366 | points[p].p3_l = 1.0; |
| 367 | points[p].p3_flags |= PF_UV | PF_L; |
| 368 | } |
| 369 | |
| 370 | // Get the bitmap handle |
| 371 | int bm_handle = GetTextureBitmap(Scorch_texture_handles[sp->handle_index], 0); |
| 372 | |
| 373 | // Draw the polygon |
| 374 | g3_DrawPoly(4, pointlist, bm_handle); |
| 375 | } |
| 376 | skip_draw:; |
| 377 | |
| 378 | // Check for end of loop |
| 379 | if (i == Scorch_end) |
| 380 | break; |
| 381 | |
| 382 | // Increment & wrap |
| 383 | if (++i == MAX_SCORCHES) { |
| 384 | i = 0; |
| 385 | sp = Scorches; |
| 386 | } else |
| 387 | sp++; |
| 388 | } |
| 389 | |
| 390 | // Reset rendering states |
| 391 | if (!StateLimited) { |
| 392 | rend_SetZBias(0); |
| 393 | rend_SetZBufferWriteMask(1); |
| 394 | } |
| 395 | } |