| 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/object_lighting.cpp $ |
| 21 | * $Revision: 64 $ |
| 22 | * $Date: 3/20/00 12:58p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Code to deal with object lighting |
| 26 | * |
| 27 | * $Log: /DescentIII/main/object_lighting.cpp $ |
| 28 | * |
| 29 | * 64 3/20/00 12:58p Matt |
| 30 | * Merge of Duane's post-1.3 changes. |
| 31 | * Lots of lightmap stuff. Jason looked at this and said nothing jumps |
| 32 | * out at him as being wrong. |
| 33 | * |
| 34 | * 63 10/26/99 6:33p Chris |
| 35 | * Added the green flare |
| 36 | * |
| 37 | * 62 10/12/99 11:06a Jeff |
| 38 | * negative lighting effect |
| 39 | * |
| 40 | * 61 7/20/99 1:00p Jason |
| 41 | * added auto katmai support |
| 42 | * |
| 43 | * 60 4/28/99 11:28a Jason |
| 44 | * made real viseffects not cast light |
| 45 | * |
| 46 | * 59 4/26/99 4:28a Jeff |
| 47 | * reset cloak time to 0 on MakeObjectVisible() to prevent some weird |
| 48 | * anomalies (like the thief stealing cloak) |
| 49 | * |
| 50 | * 58 4/21/99 11:05a Kevin |
| 51 | * new ps_rand and ps_srand to replace rand & srand |
| 52 | * |
| 53 | * 57 4/12/99 6:15p Samir |
| 54 | * Sound priorities pass 1 |
| 55 | * |
| 56 | * 56 4/03/99 2:20p Jason |
| 57 | * added better heat damage effect |
| 58 | * |
| 59 | * 55 3/26/99 3:26p Jeff |
| 60 | * option to display hud message when cloaking |
| 61 | * |
| 62 | * 54 3/22/99 11:16a Jason |
| 63 | * Katmai enhancements |
| 64 | * |
| 65 | * 53 3/05/99 12:03p Jason |
| 66 | * fixed lighting with popup views |
| 67 | * |
| 68 | * 52 2/25/99 8:55p Jeff |
| 69 | * Inventory supports level change persistant items. Inventory supports |
| 70 | * time-out objects. Inventory Reset changed (takes a level of reset |
| 71 | * now). Quad lasers stay across level change (single player). Guidebot |
| 72 | * bug fixed (now back in ship on level start). Quads time out when |
| 73 | * spewed. Invulnerability and cloak powerups no longer use game |
| 74 | * event/callbacks, so they can be saved in game saves (moved to |
| 75 | * MakePlayerInvulnerable and MakeObjectInvisible) |
| 76 | * |
| 77 | * 51 2/15/99 4:10p Jason |
| 78 | * made non-vis objects kill all their attach sounds/objects |
| 79 | * |
| 80 | * 50 1/20/99 5:29p Jason |
| 81 | * took out thruster glow for players |
| 82 | * |
| 83 | * 49 1/19/99 4:22p Matt |
| 84 | * Added the ability for objects to have their own lighting info, |
| 85 | * different from the default lighting for that type of object. |
| 86 | * |
| 87 | */ |
| 88 | |
| 89 | #include "object_lighting.h" |
| 90 | #include "object.h" |
| 91 | #include "lighting.h" |
| 92 | #include "objinfo.h" |
| 93 | #include "weapon.h" |
| 94 | #include "descent.h" |
| 95 | #include "game.h" |
| 96 | #include "polymodel.h" |
| 97 | #include "renderobject.h" |
| 98 | #include <stdlib.h> |
| 99 | #include "lightmap_info.h" |
| 100 | #include "fireball.h" |
| 101 | #include "player.h" |
| 102 | #include "mem.h" |
| 103 | #include "config.h" |
| 104 | #include "findintersection.h" |
| 105 | #include "psrand.h" |
| 106 | |
| 107 | // How far the headlight casts light |
| 108 | #define HEADLIGHT_DISTANCE 150.0f |
| 109 | // Defines the headlight cone of light |
| 110 | #define HEADLIGHT_DOT .75f |
| 111 | |
| 112 | // How big the fast headlight is |
| 113 | #define FAST_HEADLIGHT_SIZE 20.0f |
| 114 | |
| 115 | // Defines the headlight cone of light |
| 116 | #define FAST_HEADLIGHT_DOT .80f |
| 117 | |
| 118 | // returns true if this light point is visible |
| 119 | bool IsLightPointVisible(vector *pos, float size, object *obj) { |
| 120 | if (obj != NULL) { |
| 121 | if ((obj->flags & OF_RENDERED) || IsPointVisible(pos, size, NULL)) |
| 122 | return true; |
| 123 | } else { |
| 124 | if (IsPointVisible(pos, size, NULL)) |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | // Fills in the color of the passed in players thrusters. For lighting |
| 132 | void FindPlayerThrusterColor(int slot, float *r, float *g, float *b) { |
| 133 | ASSERT(slot >= 0 && slot < MAX_PLAYERS); |
| 134 | object *obj = &Objects[Players[slot].objnum]; |
| 135 | int model_num = obj->rtype.pobj_info.model_num; |
| 136 | |
| 137 | *r = 0; |
| 138 | *g = 0; |
| 139 | *b = 0; |
| 140 | |
| 141 | if (obj->render_type != RT_POLYOBJ) |
| 142 | return; |
| 143 | |
| 144 | poly_model *pm = &Poly_models[model_num]; |
| 145 | |
| 146 | for (int i = 0; i < pm->n_models; i++) { |
| 147 | if (pm->submodel[i].flags & SOF_GLOW) { |
| 148 | *r = pm->submodel[i].glow_info->glow_r; |
| 149 | *g = pm->submodel[i].glow_info->glow_g; |
| 150 | *b = pm->submodel[i].glow_info->glow_b; |
| 151 | |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | Int3(); // No thruster glow? |
| 157 | } |
| 158 | |
| 159 | uint8_t EasterEgg = 0; |
| 160 | #define EASTER_EGG_TIMER (3600 * 4) |
| 161 | |
| 162 | void ReflectRay(vector *, vector *, vector *); |
| 163 | // Casts light from an object onto the rooms or terrain |
| 164 | void DoObjectLight(object *obj) { |
| 165 | light_info *li; |
| 166 | static int first = 1, flare_id = -1, greenflare_id = -1; |
| 167 | |
| 168 | if (obj->flags & OF_DEAD) |
| 169 | return; |
| 170 | |
| 171 | if (obj->render_type == RT_NONE) |
| 172 | return; |
| 173 | |
| 174 | float negative_light = 1.0f; |
| 175 | if (obj->effect_info && obj->effect_info->type_flags & EF_NEGATIVE_LIGHT) |
| 176 | negative_light = -1.0f; |
| 177 | |
| 178 | // Temporary hack to make explosions light up |
| 179 | if (obj->type == OBJ_FIREBALL) { |
| 180 | if (!Detail_settings.Dynamic_lighting) |
| 181 | return; |
| 182 | |
| 183 | if (obj->flags & OF_USES_LIFELEFT && Fireballs[obj->id].type == FT_EXPLOSION) { |
| 184 | float scalar; |
| 185 | |
| 186 | scalar = (obj->lifetime - obj->lifeleft) / obj->lifetime; |
| 187 | |
| 188 | if (scalar > .5) { |
| 189 | scalar -= .5; |
| 190 | |
| 191 | scalar = 1.0 - (scalar / .5); |
| 192 | } else { |
| 193 | scalar *= 2; |
| 194 | } |
| 195 | |
| 196 | if (scalar > .05) { |
| 197 | if (OBJECT_OUTSIDE(obj)) |
| 198 | ApplyLightingToTerrain(&obj->pos, CELLNUM(obj->roomnum), 40 * scalar, negative_light * 1.0f, |
| 199 | negative_light * 0.5f, negative_light * 0.0f); |
| 200 | else |
| 201 | ApplyLightingToRooms(&obj->pos, obj->roomnum, 40 * scalar, negative_light * 1.0f, negative_light * 0.5f, |
| 202 | negative_light * 0.0f); |
| 203 | } |
| 204 | } |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | switch (obj->type) { |
| 209 | case OBJ_PLAYER: { |
| 210 | if ((Players[obj->id].flags) & PLAYER_FLAGS_HEADLIGHT) { |
| 211 | |
| 212 | if (Detail_settings.Fast_headlight_on) { |
| 213 | // Do fast headlight |
| 214 | fvi_info hit_info; |
| 215 | fvi_query fq; |
| 216 | |
| 217 | // shoot a ray from the light position to the current vertex |
| 218 | vector end_pos = obj->pos + (obj->orient.fvec * HEADLIGHT_DISTANCE * 2); |
| 219 | fq.p0 = &obj->pos; |
| 220 | fq.p1 = &end_pos; |
| 221 | |
| 222 | fq.startroom = obj->roomnum; |
| 223 | |
| 224 | fq.rad = 0.0f; |
| 225 | fq.flags = FQ_CHECK_OBJS; |
| 226 | fq.thisobjnum = -1; |
| 227 | fq.ignore_obj_list = NULL; |
| 228 | |
| 229 | fvi_FindIntersection(&fq, &hit_info); |
| 230 | |
| 231 | hit_info.hit_pnt -= obj->orient.fvec / 4; |
| 232 | |
| 233 | float dist = vm_VectorDistanceQuick(&hit_info.hit_pnt, &obj->pos); |
| 234 | |
| 235 | // Now light up the hit area |
| 236 | if (ROOMNUM_OUTSIDE(hit_info.hit_room)) |
| 237 | ApplyLightingToTerrain(&hit_info.hit_pnt, CELLNUM(hit_info.hit_room), FAST_HEADLIGHT_SIZE, |
| 238 | negative_light * 1.0, negative_light * 1.0, negative_light * 1.0); |
| 239 | else { |
| 240 | ApplyLightingToRooms(&hit_info.hit_pnt, hit_info.hit_room, FAST_HEADLIGHT_SIZE, negative_light * 1.0, |
| 241 | negative_light * 1.0, negative_light * 1.0); |
| 242 | // Do stupid easter egg trick |
| 243 | face *fp = &Rooms[hit_info.hit_room].faces[hit_info.hit_face[0]]; |
| 244 | if (Gametime > EASTER_EGG_TIMER && GameTextures[fp->tmap].flags & (TF_PROCEDURAL | TF_WATER_PROCEDURAL)) { |
| 245 | EasterEgg = 1; |
| 246 | } |
| 247 | } |
| 248 | } else { |
| 249 | // Do slow headlight |
| 250 | |
| 251 | if (OBJECT_OUTSIDE(obj)) |
| 252 | ApplyLightingToTerrain(&obj->pos, CELLNUM(obj->roomnum), HEADLIGHT_DISTANCE, negative_light * 1.0, |
| 253 | negative_light * 1.0, negative_light * 1.0, &obj->orient.fvec, HEADLIGHT_DOT); |
| 254 | else |
| 255 | ApplyLightingToRooms(&obj->pos, obj->roomnum, HEADLIGHT_DISTANCE, negative_light * 1.0, negative_light * 1.0, |
| 256 | negative_light * 1.0, &obj->orient.fvec, HEADLIGHT_DOT); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | int slot = obj->id; |
| 261 | |
| 262 | /*// Do thruster glow |
| 263 | if (Players[slot].thrust_mag && Detail_settings.Dynamic_lighting) |
| 264 | { |
| 265 | float lightsize=Players[slot].thrust_mag*30.0; |
| 266 | lightsize+=Players[slot].afterburner_mag*40.0; |
| 267 | |
| 268 | float r,g,b; |
| 269 | vector negz=-obj->orient.fvec; |
| 270 | vector newpos=obj->pos-(obj->orient.fvec*(obj->size*.7)); |
| 271 | |
| 272 | if (IsLightPointVisible (&newpos,lightsize,NULL)) |
| 273 | { |
| 274 | FindPlayerThrusterColor (slot,&r,&g,&b); |
| 275 | |
| 276 | ASSERT (r>=0 && r<=1.0); |
| 277 | ASSERT (g>=0 && g<=1.0); |
| 278 | ASSERT (b>=0 && b<=1.0); |
| 279 | |
| 280 | if (r!=0 || g!=0 || b!=0) |
| 281 | { |
| 282 | if (OBJECT_OUTSIDE(obj)) |
| 283 | ApplyLightingToTerrain (&newpos,CELLNUM(obj->roomnum),lightsize,r,g,b,&negz,0); |
| 284 | else |
| 285 | ApplyLightingToRooms (&newpos,obj->roomnum,lightsize,r,g,b,&negz,0); |
| 286 | } |
| 287 | } |
| 288 | }*/ |
| 289 | |
| 290 | // If the player is glowing |
| 291 | if (Players[slot].light_dist > .5 && Detail_settings.Dynamic_lighting) { |
| 292 | if (IsLightPointVisible(&obj->pos, Players[slot].light_dist, obj)) { |
| 293 | if (OBJECT_OUTSIDE(obj)) |
| 294 | ApplyLightingToTerrain(&obj->pos, CELLNUM(obj->roomnum), Players[slot].light_dist, |
| 295 | Players[slot].r * negative_light, Players[slot].g * negative_light, |
| 296 | Players[slot].b * negative_light); |
| 297 | else |
| 298 | ApplyLightingToRooms(&obj->pos, obj->roomnum, Players[slot].light_dist, Players[slot].r * negative_light, |
| 299 | Players[slot].g * negative_light, Players[slot].b * negative_light); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Do stupid ball glow |
| 304 | if (Players[slot].num_balls > 0 && Detail_settings.Dynamic_lighting) { |
| 305 | vector ballpos; |
| 306 | for (int i = 0; i < Players[slot].num_balls; i++) { |
| 307 | float light_size = 25; |
| 308 | PlayerGetBallPosition(&ballpos, slot, i); |
| 309 | |
| 310 | if (!IsLightPointVisible(&ballpos, light_size, obj)) |
| 311 | return; |
| 312 | |
| 313 | if (OBJECT_OUTSIDE(obj)) |
| 314 | ApplyLightingToTerrain(&ballpos, CELLNUM(obj->roomnum), light_size, Players[slot].ball_r[i] * negative_light, |
| 315 | Players[slot].ball_g[i] * negative_light, Players[slot].ball_b[i] * negative_light); |
| 316 | else |
| 317 | ApplyLightingToRooms(&ballpos, obj->roomnum, light_size, Players[slot].ball_r[i] * negative_light, |
| 318 | Players[slot].ball_g[i] * negative_light, Players[slot].ball_b[i] * negative_light); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | case OBJ_WEAPON: { |
| 326 | weapon *w = &Weapons[obj->id]; |
| 327 | li = &w->lighting_info; |
| 328 | |
| 329 | // Check to see if we should have dynamic light |
| 330 | if (first) { |
| 331 | flare_id = FindWeaponName("Yellow flare"); |
| 332 | greenflare_id = FindWeaponName("GreenFlare"); |
| 333 | first = 0; |
| 334 | } |
| 335 | |
| 336 | if (!Detail_settings.Dynamic_lighting) { |
| 337 | if (obj->id != flare_id && obj->id != greenflare_id) |
| 338 | return; |
| 339 | } |
| 340 | if (obj->ctype.laser_info.casts_light == false) |
| 341 | return; |
| 342 | |
| 343 | break; |
| 344 | } |
| 345 | case OBJ_POWERUP: |
| 346 | case OBJ_ROBOT: |
| 347 | case OBJ_CLUTTER: |
| 348 | case OBJ_BUILDING: { |
| 349 | li = ObjGetLightInfo(obj); |
| 350 | if (!Detail_settings.Dynamic_lighting && (obj->type == OBJ_ROBOT || obj->type == OBJ_POWERUP)) |
| 351 | return; |
| 352 | break; |
| 353 | } |
| 354 | default: |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | float scalar = 1.0; |
| 359 | float red, green, blue; |
| 360 | float light_distance = li->light_distance; |
| 361 | vector *direction = NULL; |
| 362 | |
| 363 | // Make katmai lights bigger |
| 364 | if (Katmai) |
| 365 | light_distance *= 1.5; |
| 366 | |
| 367 | if (light_distance > 0) { |
| 368 | if (!IsLightPointVisible(&obj->pos, light_distance, obj)) |
| 369 | return; |
| 370 | |
| 371 | if (li->flags & OLF_DIRECTIONAL) |
| 372 | direction = &obj->orient.fvec; |
| 373 | |
| 374 | if (li->flags & OLF_FLICKER_SLIGHTLY) { |
| 375 | int factor = li->flicker_distance; |
| 376 | |
| 377 | if (factor == 0) { |
| 378 | mprintf(0, "You have a flicker_slightly light that has a flicker factor of zero!\n"); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | light_distance += ((ps_rand() % factor) - (factor / 2)); |
| 383 | if (light_distance < 0) |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | red = li->red_light1; |
| 388 | green = li->green_light1; |
| 389 | blue = li->blue_light1; |
| 390 | |
| 391 | if ((li->flags & OLF_PULSE) && li->time_interval > 0) { |
| 392 | float ptime = li->time_interval; |
| 393 | |
| 394 | int int_time = (int)(Gametime / (ptime * 2)); |
| 395 | float left_time = Gametime - (int_time * ptime * 2); |
| 396 | float norm_time = left_time / (ptime); |
| 397 | |
| 398 | if (norm_time > 1) |
| 399 | scalar = 1 - (norm_time - 1.0); |
| 400 | else |
| 401 | scalar = norm_time; |
| 402 | |
| 403 | scalar = li->flicker_distance + (scalar * (1.0 - li->flicker_distance)); |
| 404 | } |
| 405 | |
| 406 | if (li->flags & OLF_PULSE_TO_SECOND && li->time_interval > 0) { |
| 407 | float ptime = li->time_interval; |
| 408 | |
| 409 | int int_time = (int)(Gametime / (ptime * 2)); |
| 410 | float left_time = Gametime - (int_time * ptime * 2); |
| 411 | float norm_time = left_time / (ptime); |
| 412 | |
| 413 | if (norm_time > 1) { |
| 414 | norm_time -= 1.0; |
| 415 | |
| 416 | red = ((1.0 - norm_time) * li->red_light2) + (li->red_light1 * norm_time); |
| 417 | green = ((1.0 - norm_time) * li->green_light2) + (li->green_light1 * norm_time); |
| 418 | blue = ((1.0 - norm_time) * li->blue_light2) + (li->blue_light1 * norm_time); |
| 419 | } else { |
| 420 | red = ((1.0 - norm_time) * li->red_light1) + (li->red_light2 * norm_time); |
| 421 | green = ((1.0 - norm_time) * li->green_light1) + (li->green_light2 * norm_time); |
| 422 | blue = ((1.0 - norm_time) * li->blue_light1) + (li->blue_light2 * norm_time); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if (scalar < 0.0f) |
| 427 | scalar = 0.0f; |
| 428 | |
| 429 | // If this is object is flickering, just compare it with a random value |
| 430 | if (li->flags & OLF_FLICKERING) { |
| 431 | if (ps_rand() % 2 == 1) |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | // If this object is based on timebits, then return if the corresponding bit |
| 436 | // is zero for this gametime |
| 437 | if (li->flags & OLF_TIMEBITS) { |
| 438 | int int_time = (Gametime / li->time_interval); |
| 439 | float left_time = (Gametime - (int_time * li->time_interval)); |
| 440 | float slice_time = li->time_interval / 8; |
| 441 | |
| 442 | int timebit_num = left_time / slice_time; |
| 443 | |
| 444 | if (!(li->timebits & (1 << timebit_num))) |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | if (OBJECT_OUTSIDE(obj)) |
| 449 | ApplyLightingToTerrain(&obj->pos, CELLNUM(obj->roomnum), light_distance * scalar, red * negative_light, |
| 450 | green * negative_light, blue * negative_light, direction, li->directional_dot); |
| 451 | else |
| 452 | ApplyLightingToRooms(&obj->pos, obj->roomnum, light_distance * scalar, red * negative_light, |
| 453 | green * negative_light, blue * negative_light, direction, li->directional_dot); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Frees all the memory associated with this objects lightmap |
| 458 | void ClearObjectLightmaps(object *obj) { |
| 459 | int Mnum, Fnum; |
| 460 | |
| 461 | if (obj->lm_object.used == 1) { |
| 462 | obj->lm_object.used = 0; |
| 463 | poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num]; |
| 464 | ASSERT(pm->n_models < MAX_SUBOBJECTS); |
| 465 | |
| 466 | mprintf(1, "CLEAR %d %s", obj->handle, obj->name); |
| 467 | |
| 468 | int faceCount = 0; |
| 469 | for (Mnum = 0; Mnum < pm->n_models; Mnum++) { |
| 470 | if (!IsNonRenderableSubmodel(pm, Mnum)) { |
| 471 | mprintf(1, " %X\n", obj->lm_object.lightmap_faces[Mnum][0].u2); |
| 472 | mem_free(obj->lm_object.lightmap_faces[Mnum][0].u2); |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | for (Mnum = 0; Mnum < pm->n_models; Mnum++) { |
| 477 | if (IsNonRenderableSubmodel(pm, Mnum)) |
| 478 | continue; |
| 479 | |
| 480 | for (Fnum = 0; Fnum < pm->submodel[Mnum].num_faces; Fnum++) { |
| 481 | lightmap_object_face *lof = &obj->lm_object.lightmap_faces[Mnum][Fnum]; |
| 482 | |
| 483 | if (lof->lmi_handle != BAD_LMI_INDEX) { |
| 484 | FreeLightmapInfo(lof->lmi_handle); |
| 485 | lof->lmi_handle = BAD_LMI_INDEX; |
| 486 | } |
| 487 | lof->num_verts = 0; |
| 488 | } |
| 489 | |
| 490 | if (obj->lm_object.num_faces[Mnum]) { |
| 491 | mem_free(obj->lm_object.lightmap_faces[Mnum]); |
| 492 | obj->lm_object.lightmap_faces[Mnum] = NULL; |
| 493 | } |
| 494 | obj->lm_object.num_faces[Mnum] = 0; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // Frees all the memory associated with lightmap objects |
| 500 | void ClearAllObjectLightmaps(int terrain) { |
| 501 | int i; |
| 502 | |
| 503 | for (i = 0; i <= Highest_object_index; i++) { |
| 504 | object *obj = &Objects[i]; |
| 505 | |
| 506 | if ((OBJECT_OUTSIDE(obj) != 0) != (terrain != 0)) |
| 507 | continue; |
| 508 | |
| 509 | if (obj->lm_object.used == 1) { |
| 510 | ClearObjectLightmaps(obj); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // Sets up the memory to be used by an object for lightmaps |
| 516 | void SetupObjectLightmapMemory(object *obj) { |
| 517 | int Mnum, Fnum; |
| 518 | lightmap_object_face *lof; |
| 519 | |
| 520 | ASSERT(obj->lm_object.used == 0); |
| 521 | obj->lm_object.used = 1; |
| 522 | |
| 523 | poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num]; |
| 524 | ASSERT(pm->new_style); |
| 525 | |
| 526 | obj->lm_object.num_models = pm->n_models; |
| 527 | |
| 528 | ASSERT(pm->n_models < MAX_SUBOBJECTS); |
| 529 | int uv2size = 0; |
| 530 | |
| 531 | mprintf(1, "SETUP %d %s", obj->handle, obj->name); |
| 532 | for (Mnum = 0; Mnum < pm->n_models; Mnum++) { |
| 533 | if (IsNonRenderableSubmodel(pm, Mnum)) { |
| 534 | obj->lm_object.num_faces[Mnum] = 0; |
| 535 | obj->lm_object.lightmap_faces[Mnum] = NULL; |
| 536 | continue; |
| 537 | } |
| 538 | |
| 539 | obj->lm_object.num_faces[Mnum] = pm->submodel[Mnum].num_faces; |
| 540 | obj->lm_object.lightmap_faces[Mnum] = |
| 541 | (lightmap_object_face *)mem_malloc(obj->lm_object.num_faces[Mnum] * sizeof(lightmap_object_face)); |
| 542 | ASSERT(obj->lm_object.lightmap_faces[Mnum]); |
| 543 | |
| 544 | for (Fnum = 0; Fnum < pm->submodel[Mnum].num_faces; Fnum++) { |
| 545 | lof = &obj->lm_object.lightmap_faces[Mnum][Fnum]; |
| 546 | |
| 547 | lof->num_verts = pm->submodel[Mnum].faces[Fnum].nverts; |
| 548 | lof->lmi_handle = BAD_LMI_INDEX; |
| 549 | |
| 550 | uv2size += lof->num_verts; |
| 551 | } |
| 552 | } |
| 553 | if (uv2size) { |
| 554 | float *uvblock = (float *)mem_malloc(2 * uv2size * sizeof(float)); |
| 555 | mprintf(1, " %X %d\n", uvblock, uv2size); |
| 556 | |
| 557 | for (Mnum = 0; Mnum < pm->n_models; Mnum++) { |
| 558 | if (!IsNonRenderableSubmodel(pm, Mnum)) { |
| 559 | for (Fnum = 0; Fnum < pm->submodel[Mnum].num_faces; Fnum++) { |
| 560 | obj->lm_object.lightmap_faces[Mnum][Fnum].u2 = uvblock; |
| 561 | uvblock += pm->submodel[Mnum].faces[Fnum].nverts; |
| 562 | obj->lm_object.lightmap_faces[Mnum][Fnum].v2 = uvblock; |
| 563 | uvblock += pm->submodel[Mnum].faces[Fnum].nverts; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // SHOULDN'T THE FOLLOWING TWO FUNCTIONS REALLY BE IN OBJECT.CPP? |
| 571 | #include "hlsoundlib.h" |
| 572 | #include "soundload.h" |
| 573 | #include "hud.h" |
| 574 | #include "stringtable.h" |
| 575 | // makes the an object cloaked |
| 576 | void MakeObjectInvisible(object *obj, float time, float fade_time, bool no_hud_message) { |
| 577 | if (obj->effect_info) { |
| 578 | obj->effect_info->cloak_time = time; |
| 579 | obj->effect_info->type_flags |= EF_FADING_OUT; |
| 580 | obj->effect_info->fade_time = fade_time; |
| 581 | obj->effect_info->fade_max_time = fade_time; |
| 582 | } |
| 583 | |
| 584 | if (obj->type == OBJ_PLAYER && !no_hud_message) { |
| 585 | static int cloak_sound_on_id = -2; |
| 586 | if (cloak_sound_on_id == -2) |
| 587 | cloak_sound_on_id = FindSoundName("Cloak on"); |
| 588 | |
| 589 | if (cloak_sound_on_id != -1) |
| 590 | Sound_system.Play3dSound(cloak_sound_on_id, SND_PRIORITY_HIGHEST, obj, MAX_GAME_VOLUME / 2); |
| 591 | |
| 592 | if (obj->id == Player_num) { |
| 593 | AddHUDMessage(TXT_CLOAKON); |
| 594 | } |
| 595 | |
| 596 | if (obj->effect_info) |
| 597 | obj->effect_info->type_flags |= EF_CLOAK_WITH_MSG; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // makes an object visbile |
| 602 | void MakeObjectVisible(object *obj) { |
| 603 | if (obj->effect_info) { |
| 604 | // Play sound maybe. |
| 605 | obj->effect_info->type_flags &= ~EF_CLOAKED; |
| 606 | obj->effect_info->type_flags |= EF_FADING_IN; |
| 607 | obj->effect_info->fade_time = obj->effect_info->fade_max_time; |
| 608 | obj->effect_info->cloak_time = 0; |
| 609 | } |
| 610 | |
| 611 | if (obj->type == OBJ_PLAYER && obj->effect_info && obj->effect_info->type_flags & EF_CLOAK_WITH_MSG) { |
| 612 | static int cloak_sound_off_id = -2; |
| 613 | if (cloak_sound_off_id == -2) |
| 614 | cloak_sound_off_id = FindSoundName("Cloak off"); |
| 615 | |
| 616 | if (cloak_sound_off_id != -1) |
| 617 | Sound_system.Play3dSound(cloak_sound_off_id, SND_PRIORITY_HIGHEST, obj, MAX_GAME_VOLUME / 2); |
| 618 | |
| 619 | if (obj->id == Player_num) { |
| 620 | AddHUDMessage(TXT_MSG_CLOAKOFF); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | // Sets an object to have local lighting info |
| 626 | void ObjSetLocalLighting(object *objp) { |
| 627 | // If this object doesn't have its own lighting info, create it |
| 628 | if (objp->lighting_info == NULL) { |
| 629 | |
| 630 | // make sure this is a generic object |
| 631 | ASSERT((objp->type == OBJ_POWERUP) || (objp->type == OBJ_ROBOT) || (objp->type == OBJ_CLUTTER) || |
| 632 | (objp->type == OBJ_BUILDING)); |
| 633 | |
| 634 | // allocate a light info for this object |
| 635 | objp->lighting_info = (light_info *)mem_malloc(sizeof(*objp->lighting_info)); |
| 636 | |
| 637 | // copy the lighting info from the type for this object |
| 638 | *objp->lighting_info = Object_info[objp->id].lighting_info; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | // Returns a pointer to the lighting info for the specified object |
| 643 | light_info *ObjGetLightInfo(object *objp) { |
| 644 | if (objp->lighting_info) |
| 645 | return objp->lighting_info; |
| 646 | |
| 647 | if ((objp->type == OBJ_POWERUP) || (objp->type == OBJ_ROBOT) || (objp->type == OBJ_CLUTTER) || |
| 648 | (objp->type == OBJ_BUILDING)) |
| 649 | return &Object_info[objp->id].lighting_info; |
| 650 | |
| 651 | if (objp->type == OBJ_WEAPON) |
| 652 | return &Weapons[objp->id].lighting_info; |
| 653 | |
| 654 | Int3(); |
| 655 | |
| 656 | return NULL; |
| 657 | } |