| | 1 | #include "system.h" |
| | 2 | #include "stratdef.h" |
| | 3 | #include "bh_ltfx.h" |
| | 4 | #include <assert.h> |
| | 5 | #include <stdlib.h> |
| | 6 | |
| | 7 | extern struct shapeheader** mainshapelist; |
| | 8 | |
| | 9 | void LightFXBehaveInit(void * bhdata, STRATEGYBLOCK* sbptr) |
| | 10 | { |
| | 11 | MODULE * my_mod; |
| | 12 | |
| | 13 | LIGHT_FX_BEHAV_BLOCK * lfxbb = malloc(sizeof(LIGHT_FX_BEHAV_BLOCK)); |
| | 14 | |
| | 15 | if (!lfxbb) |
| | 16 | { |
| | 17 | RemoveBehaviourStrategy(sbptr); |
| | 18 | return; |
| | 19 | } |
| | 20 | |
| | 21 | sbptr->dataptr = lfxbb; |
| | 22 | LIGHT_FX_TOOLS_TEMPLATE * lfxtt = (LIGHT_FX_TOOLS_TEMPLATE *)bhdata; |
| | 23 | |
| | 24 | COPY_NAME (sbptr->SBname, lfxtt->nameID); |
| | 25 | lfxbb->bhvr_type = I_BehaviourLightFX; |
| | 26 | |
| | 27 | // Setup module ref |
| | 28 | { |
| | 29 | extern SCENEMODULE MainScene; |
| | 30 | MREF mref = lfxtt->my_module; |
| | 31 | ConvertModuleNameToPointer (&mref, MainScene.sm_marray); |
| | 32 | my_mod = mref.mref_ptr; |
| | 33 | } |
| | 34 | |
| | 35 | my_mod->m_sbptr = sbptr; |
| | 36 | sbptr->moptr = my_mod; |
| | 37 | |
| | 38 | assert (my_mod); |
| | 39 | |
| | 40 | lfxbb->type = lfxtt->light_data.type; |
| | 41 | lfxbb->current_state = lfxtt->light_data.init_state; |
| | 42 | lfxbb->fade_up_speed = lfxtt->light_data.fade_up_speed; |
| | 43 | lfxbb->fade_down_speed = lfxtt->light_data.fade_down_speed; |
| | 44 | lfxbb->post_fade_up_delay = lfxtt->light_data.post_fade_up_delay; |
| | 45 | lfxbb->post_fade_down_delay = lfxtt->light_data.post_fade_down_delay; |
| | 46 | |
| | 47 | if (!lfxbb->fade_up_speed) |
| | 48 | lfxbb->fade_up_speed = 10; |
| | 49 | |
| | 50 | if (!lfxbb->fade_down_speed) |
| | 51 | lfxbb->fade_down_speed = 10; |
| | 52 | |
| | 53 | if (!lfxbb->post_fade_up_delay) |
| | 54 | lfxbb->post_fade_up_delay = 10; |
| | 55 | |
| | 56 | if (!lfxbb->post_fade_down_delay) |
| | 57 | lfxbb->post_fade_down_delay = 10; |
| | 58 | |
| | 59 | lfxbb->fade_up_speed_multiplier = DIV_FIXED (ONE_FIXED, lfxbb->fade_up_speed); |
| | 60 | lfxbb->fade_down_speed_multiplier = DIV_FIXED (ONE_FIXED, lfxbb->fade_down_speed); |
| | 61 | lfxbb->post_fade_up_delay_multiplier = DIV_FIXED (ONE_FIXED, lfxbb->post_fade_up_delay); |
| | 62 | lfxbb->post_fade_down_delay_multiplier = DIV_FIXED (ONE_FIXED, lfxbb->post_fade_down_delay); |
| | 63 | |
| | 64 | switch (lfxbb->type) |
| | 65 | { |
| | 66 | case LFX_Strobe: |
| | 67 | case LFX_Switch: |
| | 68 | case LFX_FlickySwitch: |
| | 69 | { |
| | 70 | switch (lfxbb->current_state) |
| | 71 | { |
| | 72 | case LFXS_LightOn: |
| | 73 | { |
| | 74 | lfxbb->multiplier = ONE_FIXED; |
| | 75 | lfxbb->timer = 0; |
| | 76 | lfxbb->timer2 = 0; |
| | 77 | } |
| | 78 | break; |
| | 79 | case LFXS_LightOff: |
| | 80 | { |
| | 81 | lfxbb->multiplier = 0; |
| | 82 | lfxbb->timer = 0; |
| | 83 | lfxbb->timer2 = 0; |
| | 84 | } |
| | 85 | break; |
| | 86 | default: |
| | 87 | { |
| | 88 | lfxbb->multiplier = ONE_FIXED; |
| | 89 | lfxbb->timer = 0; |
| | 90 | lfxbb->timer2 = 0; |
| | 91 | } |
| | 92 | } |
| | 93 | } |
| | 94 | break; |
| | 95 | case LFX_RandomFlicker: |
| | 96 | { |
| | 97 | lfxbb->current_state = LFXS_Flicking; |
| | 98 | lfxbb->multiplier = ONE_FIXED; |
| | 99 | lfxbb->timer = 0; |
| | 100 | lfxbb->timer2 = 0; |
| | 101 | lfxbb->time_to_next_flicker_state = 0; |
| | 102 | } |
| | 103 | break; |
| | 104 | default: |
| | 105 | { |
| | 106 | lfxbb->multiplier = ONE_FIXED; |
| | 107 | lfxbb->timer = 0; |
| | 108 | lfxbb->timer2 = 0; |
| | 109 | lfxbb->time_to_next_flicker_state = 0; |
| | 110 | } |
| | 111 | } |
| | 112 | |
| | 113 | /* see if this module has a texture animation*/ |
| | 114 | { |
| | 115 | int item_num = 0; |
| | 116 | struct shapeheader* shptr = mainshapelist[sbptr->shapeIndex]; |
| | 117 | TXACTRLBLK **pptxactrlblk = &lfxbb->anim_control; |
| | 118 | |
| | 119 | for(; item_num < shptr->numitems; item_num++) |
| | 120 | { |
| | 121 | POLYHEADER *poly = (POLYHEADER*)(shptr->items[item_num]); |
| | 122 | assert(poly); |
| | 123 | |
| | 124 | if(poly->PolyFlags & iflag_txanim) |
| | 125 | { |
| | 126 | TXACTRLBLK *pnew_txactrlblk = malloc(sizeof(TXACTRLBLK)); |
| | 127 | |
| | 128 | if (pnew_txactrlblk) |
| | 129 | { |
| | 130 | pnew_txactrlblk->tac_flags = 0; |
| | 131 | pnew_txactrlblk->tac_item = item_num; |
| | 132 | pnew_txactrlblk->tac_sequence = 0; |
| | 133 | pnew_txactrlblk->tac_node = 0; |
| | 134 | pnew_txactrlblk->tac_txarray = GetTxAnimArrayZ(sbptr->shapeIndex, item_num); |
| | 135 | pnew_txactrlblk->tac_txah_s = GetTxAnimHeaderFromShape(pnew_txactrlblk, sbptr->shapeIndex); |
| | 136 | |
| | 137 | // change the value held in pptxactrlblk which point to the previous structures "next" pointer |
| | 138 | |
| | 139 | *pptxactrlblk = pnew_txactrlblk; |
| | 140 | pptxactrlblk = &pnew_txactrlblk->tac_next; |
| | 141 | } |
| | 142 | else |
| | 143 | { |
| | 144 | break; |
| | 145 | } |
| | 146 | } |
| | 147 | } |
| | 148 | *pptxactrlblk = NULL; |
| | 149 | } |
| | 150 | } |
| | 151 | |
| | 152 | void LightFXBehaveFun (STRATEGYBLOCK* sbptr) |
| | 153 | { |
| | 154 | assert(sbptr); |
| | 155 | |
| | 156 | DISPLAYBLOCK* dptr = sbptr->DisplayBlock; |
| | 157 | |
| | 158 | LIGHT_FX_BEHAV_BLOCK * lfxbb = (LIGHT_FX_BEHAV_BLOCK *)sbptr->dataptr; |
| | 159 | assert((lfxbb->bhvr_type == I_BehaviourLightFX)); |
| | 160 | |
| | 161 | /*deal with any texture animation*/ |
| | 162 | if(lfxbb->anim_control && dptr && !dptr->ObTxAnimCtrlBlks) |
| | 163 | dptr->ObTxAnimCtrlBlks = lfxbb->anim_control; |
| | 164 | |
| | 165 | /*now update the lighting effects*/ |
| | 166 | switch (lfxbb->type) |
| | 167 | { |
| | 168 | case LFX_RandomFlicker: |
| | 169 | { |
| | 170 | if (dptr) |
| | 171 | { |
| | 172 | switch (lfxbb->current_state) |
| | 173 | { |
| | 174 | case LFXS_Flicking: |
| | 175 | { |
| | 176 | lfxbb->time_to_next_flicker_state -= NormalFrameTime; |
| | 177 | lfxbb->timer += NormalFrameTime; |
| | 178 | |
| | 179 | if (lfxbb->timer > 1750) |
| | 180 | { |
| | 181 | int j; |
| | 182 | |
| | 183 | lfxbb->multiplier = FastRandom() & 65535; |
| | 184 | |
| | 185 | if (!((lfxbb->multiplier % 24 ) >> 3)) |
| | 186 | lfxbb->multiplier |= 0xa000; |
| | 187 | else |
| | 188 | lfxbb->multiplier &= ~0xf000; |
| | 189 | |
| | 190 | for (j=0; j < dptr->ObNumLights; j++) |
| | 191 | { |
| | 192 | LIGHTBLOCK * lp = dptr->ObLights[j]; |
| | 193 | |
| | 194 | if (!(lp->LightFlags & LFlag_PreLitSource)) |
| | 195 | lp->LightBright = MUL_FIXED (lp->LightBrightStore, lfxbb->multiplier); |
| | 196 | } |
| | 197 | |
| | 198 | lfxbb->timer = 0; |
| | 199 | } |
| | 200 | } |
| | 201 | break; |
| | 202 | case LFXS_NotFlicking: |
| | 203 | { |
| | 204 | lfxbb->time_to_next_flicker_state -= NormalFrameTime; |
| | 205 | |
| | 206 | if (lfxbb->time_to_next_flicker_state < 0) |
| | 207 | { |
| | 208 | lfxbb->current_state = LFXS_Flicking; |
| | 209 | lfxbb->time_to_next_flicker_state = FastRandom() & 65535; |
| | 210 | } |
| | 211 | |
| | 212 | } |
| | 213 | break; |
| | 214 | case LFXS_LightOff: |
| | 215 | { |
| | 216 | int j=0; |
| | 217 | for (; j < dptr->ObNumLights; j++) |
| | 218 | { |
| | 219 | LIGHTBLOCK * lp = dptr->ObLights[j]; |
| | 220 | |
| | 221 | if (!(lp->LightFlags & LFlag_PreLitSource)) |
| | 222 | lp->LightBright = 0; |
| | 223 | } |
| | 224 | } |
| | 225 | break; |
| | 226 | default: |
| | 227 | break; |
| | 228 | } |
| | 229 | } |
| | 230 | } |
| | 231 | break; |
| | 232 | case LFX_Strobe: |
| | 233 | { |
| | 234 | switch (lfxbb->current_state) |
| | 235 | { |
| | 236 | case LFXS_LightOn: |
| | 237 | { |
| | 238 | lfxbb->timer += MUL_FIXED (NormalFrameTime, lfxbb->post_fade_up_delay_multiplier); |
| | 239 | |
| | 240 | if (lfxbb->timer > ONE_FIXED) |
| | 241 | { |
| | 242 | lfxbb->current_state = LFXS_LightFadingDown; |
| | 243 | lfxbb->timer = 0; |
| | 244 | } |
| | 245 | } |
| | 246 | break; |
| | 247 | case LFXS_LightOff: |
| | 248 | { |
| | 249 | lfxbb->timer += MUL_FIXED (NormalFrameTime, lfxbb->post_fade_down_delay_multiplier); |
| | 250 | |
| | 251 | if (lfxbb->timer > ONE_FIXED) |
| | 252 | { |
| | 253 | lfxbb->current_state = LFXS_LightFadingUp; |
| | 254 | lfxbb->timer = 0; |
| | 255 | } |
| | 256 | } |
| | 257 | break; |
| | 258 | case LFXS_LightFadingUp: |
| | 259 | { |
| | 260 | int diff = MUL_FIXED (NormalFrameTime, lfxbb->fade_up_speed_multiplier); |
| | 261 | lfxbb->timer += diff; |
| | 262 | lfxbb->multiplier += diff; |
| | 263 | |
| | 264 | if (lfxbb->timer > ONE_FIXED) |
| | 265 | { |
| | 266 | lfxbb->current_state = LFXS_LightOn; |
| | 267 | lfxbb->timer = 0; |
| | 268 | lfxbb->multiplier = 65536; |
| | 269 | } |
| | 270 | } |
| | 271 | break; |
| | 272 | case LFXS_LightFadingDown: |
| | 273 | { |
| | 274 | int diff = MUL_FIXED (NormalFrameTime, lfxbb->fade_down_speed_multiplier); |
| | 275 | lfxbb->timer += diff; |
| | 276 | lfxbb->multiplier -= diff; |
| | 277 | |
| | 278 | if (lfxbb->timer > ONE_FIXED) |
| | 279 | { |
| | 280 | lfxbb->current_state = LFXS_LightOff; |
| | 281 | lfxbb->timer = 0; |
| | 282 | lfxbb->multiplier = 0; |
| | 283 | } |
| | 284 | } |
| | 285 | break; |
| | 286 | default: |
| | 287 | { |
| | 288 | assert (0 == "Light FX state not supported"); |
| | 289 | break; |
| | 290 | } |
| | 291 | } |
| | 292 | |
| | 293 | if (NULL != dptr) |
| | 294 | { |
| | 295 | int j; |
| | 296 | for (j=0; j < dptr->ObNumLights; j++) |
| | 297 | { |
| | 298 | LIGHTBLOCK * lp = dptr->ObLights[j]; |
| | 299 | |
| | 300 | if (!(lp->LightFlags & LFlag_PreLitSource)) |
| | 301 | lp->LightBright = MUL_FIXED (lp->LightBrightStore, lfxbb->multiplier); |
| | 302 | } |
| | 303 | } |
| | 304 | } |
| | 305 | break; |
| | 306 | case LFX_Switch: |
| | 307 | { |
| | 308 | switch (lfxbb->current_state) |
| | 309 | { |
| | 310 | case LFXS_LightFadingUp: |
| | 311 | { |
| | 312 | int diff = MUL_FIXED (NormalFrameTime, lfxbb->fade_up_speed_multiplier); |
| | 313 | |
| | 314 | lfxbb->timer += diff; |
| | 315 | lfxbb->multiplier += diff; |
| | 316 | |
| | 317 | if (lfxbb->timer > ONE_FIXED) |
| | 318 | { |
| | 319 | lfxbb->current_state = LFXS_LightOn; |
| | 320 | lfxbb->timer = 0; |
| | 321 | lfxbb->multiplier = 65536; |
| | 322 | } |
| | 323 | } |
| | 324 | break; |
| | 325 | case LFXS_LightFadingDown: |
| | 326 | { |
| | 327 | int diff = MUL_FIXED (NormalFrameTime, lfxbb->fade_down_speed_multiplier); |
| | 328 | lfxbb->timer += diff; |
| | 329 | lfxbb->multiplier -= diff; |
| | 330 | |
| | 331 | if (lfxbb->timer > ONE_FIXED) |
| | 332 | { |
| | 333 | lfxbb->current_state = LFXS_LightOff; |
| | 334 | lfxbb->timer = 0; |
| | 335 | lfxbb->multiplier = 0; |
| | 336 | } |
| | 337 | } |
| | 338 | break; |
| | 339 | case LFXS_LightOn: |
| | 340 | case LFXS_LightOff: |
| | 341 | break; |
| | 342 | |
| | 343 | default: |
| | 344 | { |
| | 345 | assert (0 == "Light FX state not supported"); |
| | 346 | break; |
| | 347 | } |
| | 348 | } |
| | 349 | |
| | 350 | if (dptr) |
| | 351 | { |
| | 352 | int j; |
| | 353 | for (j=0; j < dptr->ObNumLights; j++) |
| | 354 | { |
| | 355 | LIGHTBLOCK * lp = dptr->ObLights[j]; |
| | 356 | |
| | 357 | if (!(lp->LightFlags & LFlag_PreLitSource)) |
| | 358 | lp->LightBright = MUL_FIXED (lp->LightBrightStore, lfxbb->multiplier); |
| | 359 | } |
| | 360 | } |
| | 361 | } |
| | 362 | break; |
| | 363 | case LFX_FlickySwitch: |
| | 364 | { |
| | 365 | switch (lfxbb->current_state) |
| | 366 | { |
| | 367 | case LFXS_LightFadingUp: |
| | 368 | { |
| | 369 | int diff = MUL_FIXED (NormalFrameTime, lfxbb->fade_up_speed_multiplier); |
| | 370 | |
| | 371 | lfxbb->timer += diff; |
| | 372 | lfxbb->timer2 += NormalFrameTime; |
| | 373 | |
| | 374 | if (lfxbb->timer2 > 3553) |
| | 375 | { |
| | 376 | lfxbb->multiplier = FastRandom() & 65535; |
| | 377 | |
| | 378 | if (!((lfxbb->multiplier % 24 )>>3)) |
| | 379 | lfxbb->multiplier |= 0xa000; |
| | 380 | else |
| | 381 | lfxbb->multiplier &= ~0xf000; |
| | 382 | |
| | 383 | lfxbb->timer2 = 0; |
| | 384 | } |
| | 385 | |
| | 386 | if (lfxbb->timer > ONE_FIXED) |
| | 387 | { |
| | 388 | lfxbb->current_state = LFXS_LightOn; |
| | 389 | lfxbb->timer = 0; |
| | 390 | lfxbb->multiplier = 65536; |
| | 391 | } |
| | 392 | } |
| | 393 | break; |
| | 394 | case LFXS_LightFadingDown: |
| | 395 | { |
| | 396 | int diff = MUL_FIXED (NormalFrameTime, lfxbb->fade_down_speed_multiplier); |
| | 397 | lfxbb->timer += diff; |
| | 398 | lfxbb->multiplier -= diff; |
| | 399 | |
| | 400 | if (lfxbb->timer > ONE_FIXED) |
| | 401 | { |
| | 402 | lfxbb->current_state = LFXS_LightOff; |
| | 403 | lfxbb->timer = 0; |
| | 404 | lfxbb->multiplier = 0; |
| | 405 | } |
| | 406 | } |
| | 407 | break; |
| | 408 | case LFXS_LightOn: |
| | 409 | case LFXS_LightOff: |
| | 410 | break; |
| | 411 | |
| | 412 | default: |
| | 413 | { |
| | 414 | assert (0 == "Light FX state not supported"); |
| | 415 | break; |
| | 416 | } |
| | 417 | } |
| | 418 | |
| | 419 | if (dptr) |
| | 420 | { |
| | 421 | int j; |
| | 422 | for (j=0; j < dptr->ObNumLights; j++) |
| | 423 | { |
| | 424 | LIGHTBLOCK * lp = dptr->ObLights[j]; |
| | 425 | |
| | 426 | if (!(lp->LightFlags & LFlag_PreLitSource)) |
| | 427 | lp->LightBright = MUL_FIXED (lp->LightBrightStore, lfxbb->multiplier); |
| | 428 | } |
| | 429 | } |
| | 430 | } |
| | 431 | break; |
| | 432 | default: |
| | 433 | { |
| | 434 | assert (0 == "Light FX type not supported"); |
| | 435 | break; |
| | 436 | } |
| | 437 | } |
| | 438 | } |
| | 439 | |
| | 440 | /*--------------------** |
| | 441 | ** Loading and Saving ** |
| | 442 | **--------------------*/ |
| | 443 | #include "savegame.h" |
| | 444 | |
| | 445 | typedef struct light_fx_save_block |
| | 446 | { |
| | 447 | SAVE_BLOCK_STRATEGY_HEADER header; |
| | 448 | |
| | 449 | LIGHT_FX_STATE current_state; |
| | 450 | int32_t multiplier; |
| | 451 | uint32_t timer; |
| | 452 | uint32_t timer2; |
| | 453 | int32_t time_to_next_flicker_state; |
| | 454 | |
| | 455 | } LIGHT_FX_SAVE_BLOCK; |
| | 456 | |
| | 457 | //defines for load/save macros |
| | 458 | #define SAVELOAD_BLOCK block |
| | 459 | #define SAVELOAD_BEHAV lfxbb |
| | 460 | |
| | 461 | void LoadStrategy_LightFx(SAVE_BLOCK_STRATEGY_HEADER* header) |
| | 462 | { |
| | 463 | LIGHT_FX_SAVE_BLOCK* block = (LIGHT_FX_SAVE_BLOCK*) header; |
| | 464 | |
| | 465 | //check the size of the save block |
| | 466 | if(header->size != sizeof(*block)) |
| | 467 | return; |
| | 468 | |
| | 469 | //find the existing strategy block |
| | 470 | STRATEGYBLOCK* sbPtr = FindSBWithName(header->SBname); |
| | 471 | |
| | 472 | if(!sbPtr) |
| | 473 | return; |
| | 474 | |
| | 475 | //make sure the strategy found is of the right type |
| | 476 | if(sbPtr->type != I_BehaviourLightFX) |
| | 477 | return; |
| | 478 | |
| | 479 | LIGHT_FX_BEHAV_BLOCK * lfxbb = (LIGHT_FX_BEHAV_BLOCK *)sbPtr->dataptr; |
| | 480 | |
| | 481 | //start copying stuff |
| | 482 | |
| | 483 | COPYELEMENT_LOAD(current_state) |
| | 484 | COPYELEMENT_LOAD(multiplier) |
| | 485 | COPYELEMENT_LOAD(timer) |
| | 486 | COPYELEMENT_LOAD(timer2) |
| | 487 | COPYELEMENT_LOAD(time_to_next_flicker_state) |
| | 488 | |
| | 489 | //update the brightness of the lights |
| | 490 | DISPLAYBLOCK* dptr = sbPtr->DisplayBlock; |
| | 491 | |
| | 492 | if(NULL != dptr) |
| | 493 | { |
| | 494 | //I'm not sure that we will ever have a displayblock at this point, anyway. hmm. |
| | 495 | int j; |
| | 496 | for (j=0; j < dptr->ObNumLights; j++) |
| | 497 | { |
| | 498 | LIGHTBLOCK * lp = dptr->ObLights[j]; |
| | 499 | |
| | 500 | if (!(lp->LightFlags & LFlag_PreLitSource)) |
| | 501 | lp->LightBright = MUL_FIXED (lp->LightBrightStore, lfxbb->multiplier); |
| | 502 | } |
| | 503 | } |
| | 504 | } |
| | 505 | |
| | 506 | void SaveStrategy_LightFx(STRATEGYBLOCK* sbPtr) |
| | 507 | { |
| | 508 | LIGHT_FX_SAVE_BLOCK *block; |
| | 509 | LIGHT_FX_BEHAV_BLOCK * lfxbb = (LIGHT_FX_BEHAV_BLOCK *)sbPtr->dataptr; |
| | 510 | |
| | 511 | GET_STRATEGY_SAVE_BLOCK(block,sbPtr); |
| | 512 | |
| | 513 | //start copying stuff |
| | 514 | |
| | 515 | COPYELEMENT_SAVE(current_state) |
| | 516 | COPYELEMENT_SAVE(multiplier) |
| | 517 | COPYELEMENT_SAVE(timer) |
| | 518 | COPYELEMENT_SAVE(timer2) |
| | 519 | COPYELEMENT_SAVE(time_to_next_flicker_state) |
| | 520 | } |