| | 1 | #include "system.h" |
| | 2 | #include "stratdef.h" |
| | 3 | #include "bh_types.h" |
| | 4 | #include "binaryswitch.h" |
| | 5 | #include "pldghost.h" |
| | 6 | #include <assert.h> |
| | 7 | #include <stdlib.h> |
| | 8 | #include "bh_types.h" |
| | 9 | |
| | 10 | extern int RealFrameTime; |
| | 11 | extern int ReturnPlayerSecurityClearance(unsigned int securityLevel); |
| | 12 | |
| | 13 | void BinarySwitchBehaveInit(void* bhdata, STRATEGYBLOCK* sbptr) |
| | 14 | { |
| | 15 | BINARY_SWITCH_BEHAV_BLOCK *bs_bhv = malloc(sizeof(BINARY_SWITCH_BEHAV_BLOCK)); |
| | 16 | int i; |
| | 17 | |
| | 18 | if (!bs_bhv) |
| | 19 | { |
| | 20 | RemoveBehaviourStrategy(sbptr); |
| | 21 | return; |
| | 22 | } |
| | 23 | |
| | 24 | sbptr->dataptr = bs_bhv; |
| | 25 | bs_bhv->bhvr_type = I_BehaviourBinarySwitch; |
| | 26 | |
| | 27 | // from loaders |
| | 28 | // 1 rest_state - on or off |
| | 29 | // 2 mode |
| | 30 | // 3 timer switch - time for reset |
| | 31 | // 4 security clerance to operate |
| | 32 | // 5 copy the target name |
| | 33 | |
| | 34 | BIN_SWITCH_TOOLS_TEMPLATE *bs_tt = (BIN_SWITCH_TOOLS_TEMPLATE*)bhdata; |
| | 35 | |
| | 36 | COPY_NAME(sbptr->SBname, bs_tt->nameID); |
| | 37 | |
| | 38 | bs_bhv->bs_mode = bs_tt->mode; |
| | 39 | bs_bhv->time_for_reset = bs_tt->time_for_reset; |
| | 40 | bs_bhv->security_clerance = bs_tt->security_clearance; |
| | 41 | bs_bhv->trigger_volume_min = bs_tt->trigger_volume_min; |
| | 42 | bs_bhv->trigger_volume_max = bs_tt->trigger_volume_max; |
| | 43 | bs_bhv->switch_flags = bs_tt->switch_flags; |
| | 44 | bs_bhv->num_targets = bs_tt->num_targets; |
| | 45 | |
| | 46 | if(bs_tt->num_targets) |
| | 47 | { |
| | 48 | bs_bhv->target_names = malloc(sizeof(SBNAMEBLOCK) * bs_tt->num_targets); |
| | 49 | |
| | 50 | if (!bs_bhv->target_names) |
| | 51 | { |
| | 52 | RemoveBehaviourStrategy(sbptr); |
| | 53 | return; |
| | 54 | } |
| | 55 | |
| | 56 | bs_bhv->request_messages = malloc(sizeof(int) * bs_tt->num_targets); |
| | 57 | |
| | 58 | if (!bs_bhv->request_messages) |
| | 59 | { |
| | 60 | free(bs_bhv->target_names); |
| | 61 | RemoveBehaviourStrategy(sbptr); |
| | 62 | return; |
| | 63 | } |
| | 64 | |
| | 65 | bs_bhv->bs_targets = malloc(sizeof(STRATEGYBLOCK *) * bs_tt->num_targets); |
| | 66 | |
| | 67 | if (!bs_bhv->bs_targets) |
| | 68 | { |
| | 69 | free(bs_bhv->target_names); |
| | 70 | free(bs_bhv->request_messages); |
| | 71 | RemoveBehaviourStrategy(sbptr); |
| | 72 | return; |
| | 73 | } |
| | 74 | } |
| | 75 | else |
| | 76 | { |
| | 77 | bs_bhv->target_names = NULL; |
| | 78 | bs_bhv->request_messages = NULL; |
| | 79 | bs_bhv->bs_targets = NULL; |
| | 80 | } |
| | 81 | |
| | 82 | for (i=0; i < bs_tt->num_targets; i++) |
| | 83 | { |
| | 84 | COPY_NAME(bs_bhv->target_names[i].name, bs_tt->target_names[i].name); |
| | 85 | bs_bhv->request_messages[i] = bs_tt->request_messages[i]; |
| | 86 | bs_bhv->bs_targets[i] = 0; |
| | 87 | } |
| | 88 | |
| | 89 | // set up the animation control |
| | 90 | if(sbptr->shapeIndex != -1) |
| | 91 | { |
| | 92 | /*Allow for switches that have no shape*/ |
| | 93 | sbptr->DynPtr = AllocateDynamicsBlock(DYNAMICS_TEMPLATE_STATIC); |
| | 94 | |
| | 95 | if(NULL == sbptr->DynPtr) |
| | 96 | { |
| | 97 | RemoveBehaviourStrategy(sbptr); |
| | 98 | return; |
| | 99 | } |
| | 100 | else //there may be no shape |
| | 101 | { |
| | 102 | sbptr->DynPtr->Position = sbptr->DynPtr->PrevPosition = bs_tt->position; |
| | 103 | sbptr->DynPtr->OrientEuler = bs_tt->orientation; |
| | 104 | CreateEulerMatrix(&sbptr->DynPtr->OrientEuler, &sbptr->DynPtr->OrientMat); |
| | 105 | TransposeMatrixCH(&sbptr->DynPtr->OrientMat); |
| | 106 | sbptr->containingModule = ModuleFromPosition(&sbptr->DynPtr->Position, NULL); |
| | 107 | |
| | 108 | if(NULL == sbptr->containingModule) |
| | 109 | { |
| | 110 | RemoveBehaviourStrategy(sbptr); |
| | 111 | printf("no containing module for binaryswitch\n"); |
| | 112 | return; |
| | 113 | } |
| | 114 | |
| | 115 | sbptr->maintainVisibility = 1; |
| | 116 | } |
| | 117 | |
| | 118 | int item_num = 0; |
| | 119 | struct shapeheader* shptr = mainshapelist[sbptr->shapeIndex]; |
| | 120 | TXACTRLBLK **pptxactrlblk = &bs_bhv->bs_tac; |
| | 121 | |
| | 122 | for(; item_num < shptr->numitems; item_num++) |
| | 123 | { |
| | 124 | POLYHEADER *poly = (POLYHEADER*)(shptr->items[item_num]); |
| | 125 | assert(poly); |
| | 126 | |
| | 127 | if(poly->PolyFlags & iflag_txanim) |
| | 128 | { |
| | 129 | int num_seq = 0; |
| | 130 | TXACTRLBLK *pnew_txactrlblk = malloc(sizeof(TXACTRLBLK)); |
| | 131 | |
| | 132 | if (pnew_txactrlblk) |
| | 133 | { |
| | 134 | pnew_txactrlblk->tac_flags = 0; |
| | 135 | pnew_txactrlblk->tac_item = item_num; |
| | 136 | pnew_txactrlblk->tac_sequence = bs_tt->starts_on; |
| | 137 | pnew_txactrlblk->tac_node = 0; |
| | 138 | pnew_txactrlblk->tac_txarray = GetTxAnimArrayZ(sbptr->shapeIndex, item_num); |
| | 139 | pnew_txactrlblk->tac_txah_s = GetTxAnimHeaderFromShape(pnew_txactrlblk, sbptr->shapeIndex); |
| | 140 | |
| | 141 | while(pnew_txactrlblk->tac_txarray[num_seq+1]) |
| | 142 | num_seq++; |
| | 143 | |
| | 144 | // Assert does not work at this point so |
| | 145 | assert(num_seq==2); |
| | 146 | |
| | 147 | /* set the flags in the animation header */ |
| | 148 | // we only ever have one frame of animation per sequence - |
| | 149 | // nb this can change talk to richard - one sequence with two frames |
| | 150 | // or mutliple sequences??? |
| | 151 | |
| | 152 | pnew_txactrlblk->tac_txah.txa_flags |= txa_flag_play; |
| | 153 | |
| | 154 | /* change the value held in pptxactrlblk |
| | 155 | which point to the previous structures "next" |
| | 156 | pointer*/ |
| | 157 | |
| | 158 | *pptxactrlblk = pnew_txactrlblk; |
| | 159 | pptxactrlblk = &pnew_txactrlblk->tac_next; |
| | 160 | } |
| | 161 | else |
| | 162 | { |
| | 163 | memoryInitialisationFailure = 1; |
| | 164 | } |
| | 165 | } |
| | 166 | } |
| | 167 | *pptxactrlblk = NULL; |
| | 168 | } |
| | 169 | else |
| | 170 | { |
| | 171 | //no shape - so there won't be any animation |
| | 172 | bs_bhv->bs_tac = NULL; |
| | 173 | } |
| | 174 | |
| | 175 | bs_bhv->bs_dtype = binswitch_no_display; |
| | 176 | |
| | 177 | if (bs_bhv->bs_tac) |
| | 178 | bs_bhv->bs_dtype = binswitch_animate_me; |
| | 179 | |
| | 180 | bs_bhv->bs_track = bs_tt->track; |
| | 181 | |
| | 182 | if (bs_bhv->bs_track) |
| | 183 | { |
| | 184 | bs_bhv->bs_track->sbptr=sbptr; |
| | 185 | |
| | 186 | if (bs_bhv->bs_dtype == binswitch_animate_me) |
| | 187 | bs_bhv->bs_dtype = binswitch_animate_and_move_me; |
| | 188 | else |
| | 189 | bs_bhv->bs_dtype = binswitch_move_me; |
| | 190 | } |
| | 191 | |
| | 192 | // assert(bs_bhv->bs_dtype != binswitch_no_display); |
| | 193 | |
| | 194 | // fill in the rest ourselves |
| | 195 | |
| | 196 | bs_bhv->request = 0; |
| | 197 | bs_bhv->state = bs_tt->starts_on; |
| | 198 | bs_bhv->timer = 0; |
| | 199 | bs_bhv->switch_off_message_same = bs_tt->switch_off_message_same; |
| | 200 | bs_bhv->switch_off_message_none = bs_tt->switch_off_message_none; |
| | 201 | bs_bhv->soundHandle = SOUND_NOACTIVEINDEX; |
| | 202 | bs_bhv->triggered_last = 0; |
| | 203 | |
| | 204 | if(bs_bhv->bs_mode == I_bswitch_time_delay_autoexec) |
| | 205 | { |
| | 206 | //set request and then treat as normal time delay switch |
| | 207 | bs_bhv->bs_mode = I_bswitch_time_delay; |
| | 208 | bs_bhv->request = I_request_on; |
| | 209 | } |
| | 210 | |
| | 211 | if(bs_bhv->state) |
| | 212 | { |
| | 213 | bs_bhv->timer = bs_bhv->time_for_reset; |
| | 214 | |
| | 215 | if(bs_bhv->bs_track) |
| | 216 | { |
| | 217 | //set the track to the end position |
| | 218 | bs_bhv->bs_track->current_section = (bs_bhv->bs_track->num_sections-1); |
| | 219 | bs_bhv->bs_track->timer = bs_bhv->bs_track->sections[bs_bhv->bs_track->current_section].time_for_section; |
| | 220 | bs_bhv->bs_track->playing = 1; |
| | 221 | Update_Track_Position(bs_bhv->bs_track); |
| | 222 | } |
| | 223 | } |
| | 224 | |
| | 225 | bs_bhv->TimeUntilNetSynchAllowed = 0; |
| | 226 | } |
| | 227 | |
| | 228 | void BinarySwitchBehaveFun(STRATEGYBLOCK* sbptr) |
| | 229 | { |
| | 230 | BINARY_SWITCH_BEHAV_BLOCK *bs_bhv = (BINARY_SWITCH_BEHAV_BLOCK*)sbptr->dataptr; |
| | 231 | assert((bs_bhv->bhvr_type == I_BehaviourBinarySwitch)); |
| | 232 | |
| | 233 | /****** |
| | 234 | What I need to do - check to see if we have |
| | 235 | a request - requests have different effects depending on |
| | 236 | the mode - so we have to switch on the mode |
| | 237 | *****/ |
| | 238 | |
| | 239 | switch(bs_bhv->bs_dtype) |
| | 240 | { |
| | 241 | case binswitch_animate_me: |
| | 242 | case binswitch_animate_and_move_me: |
| | 243 | { |
| | 244 | DISPLAYBLOCK* dptr = sbptr->DisplayBlock; |
| | 245 | |
| | 246 | if(dptr) |
| | 247 | dptr->ObTxAnimCtrlBlks = bs_bhv->bs_tac; |
| | 248 | } |
| | 249 | default: |
| | 250 | break; |
| | 251 | } |
| | 252 | |
| | 253 | if (!ReturnPlayerSecurityClearance(bs_bhv->security_clerance) && bs_bhv->security_clerance) |
| | 254 | { |
| | 255 | bs_bhv->request = I_no_request; |
| | 256 | return; |
| | 257 | } |
| | 258 | |
| | 259 | if(SinglePlayer != AvP.PlayMode) |
| | 260 | { |
| | 261 | /* |
| | 262 | Every time a switch is updated there is a time delay of 5 seconds before the |
| | 263 | switch can next be changed by the host sending synch messages. |
| | 264 | This prevents the host machine from resetting a switch before it learns that |
| | 265 | it has been pressed |
| | 266 | */ |
| | 267 | |
| | 268 | if(bs_bhv->request == I_no_request) |
| | 269 | { |
| | 270 | bs_bhv->TimeUntilNetSynchAllowed -= RealFrameTime; |
| | 271 | |
| | 272 | if(bs_bhv->TimeUntilNetSynchAllowed < 0) |
| | 273 | bs_bhv->TimeUntilNetSynchAllowed = 0; |
| | 274 | } |
| | 275 | else |
| | 276 | { |
| | 277 | bs_bhv->TimeUntilNetSynchAllowed = 5*ONE_FIXED; |
| | 278 | } |
| | 279 | } |
| | 280 | |
| | 281 | if(bs_bhv->switch_flags && SwitchFlag_UseTriggerVolume) |
| | 282 | { |
| | 283 | /*See if switch has been set off*/ |
| | 284 | int i; |
| | 285 | for (i=0; i < NumActiveStBlocks; i++) |
| | 286 | { |
| | 287 | int needToTest = 0; |
| | 288 | STRATEGYBLOCK *sbPtr = ActiveStBlockList[i]; |
| | 289 | |
| | 290 | if (sbPtr->DynPtr) |
| | 291 | { |
| | 292 | if (sbPtr->DisplayBlock == PlayerStatus.DisplayBlock) |
| | 293 | { |
| | 294 | needToTest = 1; |
| | 295 | } |
| | 296 | else if (sbPtr->type == I_BehaviourNetGhost) |
| | 297 | { |
| | 298 | NETGHOSTDATABLOCK *ghostData = (NETGHOSTDATABLOCK *)sbPtr->dataptr; |
| | 299 | |
| | 300 | switch(ghostData->type) |
| | 301 | { |
| | 302 | case I_BehaviourMarinePlayer: |
| | 303 | case I_BehaviourAlienPlayer: |
| | 304 | case I_BehaviourPredatorPlayer: |
| | 305 | needToTest = 1; |
| | 306 | default: |
| | 307 | break; |
| | 308 | } |
| | 309 | } |
| | 310 | } |
| | 311 | |
| | 312 | if(needToTest && |
| | 313 | sbPtr->DynPtr->Position.vx > bs_bhv->trigger_volume_min.vx && |
| | 314 | sbPtr->DynPtr->Position.vx < bs_bhv->trigger_volume_max.vx && |
| | 315 | sbPtr->DynPtr->Position.vy > bs_bhv->trigger_volume_min.vy && |
| | 316 | sbPtr->DynPtr->Position.vy < bs_bhv->trigger_volume_max.vy && |
| | 317 | sbPtr->DynPtr->Position.vz > bs_bhv->trigger_volume_min.vz && |
| | 318 | sbPtr->DynPtr->Position.vz < bs_bhv->trigger_volume_max.vz) |
| | 319 | { |
| | 320 | bs_bhv->request = I_request_on; |
| | 321 | break; |
| | 322 | } |
| | 323 | } |
| | 324 | } |
| | 325 | |
| | 326 | if (bs_bhv->request == I_request_on) |
| | 327 | { |
| | 328 | if (bs_bhv->triggered_last) |
| | 329 | bs_bhv->request = I_no_request; |
| | 330 | else |
| | 331 | bs_bhv->triggered_last = 1; |
| | 332 | } |
| | 333 | else |
| | 334 | { |
| | 335 | bs_bhv->triggered_last = 0; |
| | 336 | } |
| | 337 | |
| | 338 | switch(bs_bhv->bs_mode) |
| | 339 | { |
| | 340 | case I_bswitch_timer: |
| | 341 | { |
| | 342 | if(bs_bhv->request == I_request_on && !bs_bhv->state) |
| | 343 | { |
| | 344 | bs_bhv->timer = bs_bhv->time_for_reset; |
| | 345 | |
| | 346 | switch(bs_bhv->bs_dtype) |
| | 347 | { |
| | 348 | case binswitch_move_me: |
| | 349 | case binswitch_animate_and_move_me: |
| | 350 | { |
| | 351 | // moving switch |
| | 352 | bs_bhv->new_state = 1; |
| | 353 | bs_bhv->new_request = 1; |
| | 354 | bs_bhv->bs_track->reverse = 0; |
| | 355 | Start_Track_Playing(bs_bhv->bs_track); |
| | 356 | bs_bhv->mode_store = bs_bhv->bs_mode; |
| | 357 | bs_bhv->bs_mode = I_bswitch_moving; |
| | 358 | } |
| | 359 | break; |
| | 360 | default: |
| | 361 | { |
| | 362 | int i; |
| | 363 | for (i=0; i < bs_bhv->num_targets; i++) |
| | 364 | { |
| | 365 | if (bs_bhv->bs_targets[i]) |
| | 366 | RequestState(bs_bhv->bs_targets[i], bs_bhv->request_messages[i], sbptr); |
| | 367 | } |
| | 368 | |
| | 369 | bs_bhv->state = 1; |
| | 370 | } |
| | 371 | } |
| | 372 | |
| | 373 | // swap sequence |
| | 374 | if(bs_bhv->bs_tac) |
| | 375 | { |
| | 376 | bs_bhv->bs_tac->tac_sequence = 1; |
| | 377 | bs_bhv->bs_tac->tac_txah_s = GetTxAnimHeaderFromShape(bs_bhv->bs_tac, sbptr->shapeIndex); |
| | 378 | } |
| | 379 | } |
| | 380 | else if(bs_bhv->timer > 0) |
| | 381 | { |
| | 382 | bs_bhv->timer -= NormalFrameTime; |
| | 383 | |
| | 384 | if(bs_bhv->timer <= 0) |
| | 385 | { |
| | 386 | if(SinglePlayer != AvP.PlayMode) |
| | 387 | bs_bhv->TimeUntilNetSynchAllowed = 5 * ONE_FIXED; |
| | 388 | |
| | 389 | bs_bhv->state = 0; |
| | 390 | |
| | 391 | switch(bs_bhv->bs_dtype) |
| | 392 | { |
| | 393 | case binswitch_move_me: |
| | 394 | case binswitch_animate_and_move_me: |
| | 395 | { |
| | 396 | // moving switch |
| | 397 | bs_bhv->new_state = 0; |
| | 398 | bs_bhv->new_request = -1; //the 'off' message will be sent as soon as the timer finishes |
| | 399 | bs_bhv->bs_track->reverse = 1; |
| | 400 | Start_Track_Playing(bs_bhv->bs_track); |
| | 401 | bs_bhv->mode_store = bs_bhv->bs_mode; |
| | 402 | bs_bhv->bs_mode = I_bswitch_moving; |
| | 403 | } |
| | 404 | default: |
| | 405 | break; |
| | 406 | } |
| | 407 | |
| | 408 | if(!bs_bhv->switch_off_message_none) |
| | 409 | { |
| | 410 | //send the 'off' message |
| | 411 | int i; |
| | 412 | for (i=0; i < bs_bhv->num_targets; i++) |
| | 413 | { |
| | 414 | if (bs_bhv->bs_targets[i]) |
| | 415 | RequestState(bs_bhv->bs_targets[i], bs_bhv->request_messages[i]^(!bs_bhv->switch_off_message_same), sbptr); |
| | 416 | } |
| | 417 | } |
| | 418 | |
| | 419 | if(bs_bhv->bs_tac) |
| | 420 | { |
| | 421 | bs_bhv->bs_tac->tac_sequence = 0; |
| | 422 | bs_bhv->bs_tac->tac_txah_s = GetTxAnimHeaderFromShape(bs_bhv->bs_tac, sbptr->shapeIndex); |
| | 423 | } |
| | 424 | } |
| | 425 | } |
| | 426 | } |
| | 427 | break; |
| | 428 | case I_bswitch_toggle: |
| | 429 | { |
| | 430 | if(bs_bhv->request == I_no_request) |
| | 431 | return; |
| | 432 | |
| | 433 | /* change the state and request the new state in the target */ |
| | 434 | |
| | 435 | switch(bs_bhv->bs_dtype) |
| | 436 | { |
| | 437 | case binswitch_move_me: |
| | 438 | case binswitch_animate_and_move_me: |
| | 439 | { |
| | 440 | // moving switch |
| | 441 | bs_bhv->new_state = !bs_bhv->state; |
| | 442 | |
| | 443 | if(bs_bhv->new_state) |
| | 444 | { |
| | 445 | bs_bhv->new_request = 1; |
| | 446 | } |
| | 447 | else |
| | 448 | { |
| | 449 | //check to see what the switch off request is |
| | 450 | if(bs_bhv->switch_off_message_same) |
| | 451 | bs_bhv->new_request = 1; |
| | 452 | else if(bs_bhv->switch_off_message_none) |
| | 453 | bs_bhv->new_request = -1; |
| | 454 | else |
| | 455 | bs_bhv->new_request = 0; |
| | 456 | } |
| | 457 | |
| | 458 | bs_bhv->mode_store = bs_bhv->bs_mode; |
| | 459 | bs_bhv->bs_mode = I_bswitch_moving; |
| | 460 | bs_bhv->bs_track->reverse = bs_bhv->state; |
| | 461 | Start_Track_Playing(bs_bhv->bs_track); |
| | 462 | } |
| | 463 | break; |
| | 464 | default: |
| | 465 | { |
| | 466 | int i; |
| | 467 | bs_bhv->state = !bs_bhv->state; |
| | 468 | |
| | 469 | for (i=0; i < bs_bhv->num_targets; i++) |
| | 470 | { |
| | 471 | if (bs_bhv->bs_targets[i]) |
| | 472 | { |
| | 473 | if(bs_bhv->state) |
| | 474 | { |
| | 475 | RequestState(bs_bhv->bs_targets[i], bs_bhv->request_messages[i], sbptr); |
| | 476 | } |
| | 477 | else if(!bs_bhv->switch_off_message_none) |
| | 478 | { |
| | 479 | RequestState(bs_bhv->bs_targets[i], bs_bhv->request_messages[i]^(!bs_bhv->switch_off_message_same), sbptr); |
| | 480 | } |
| | 481 | } |
| | 482 | } |
| | 483 | } |
| | 484 | } |
| | 485 | |
| | 486 | if(bs_bhv->bs_tac) |
| | 487 | { |
| | 488 | bs_bhv->bs_tac->tac_sequence = bs_bhv->state; |
| | 489 | bs_bhv->bs_tac->tac_txah_s = GetTxAnimHeaderFromShape(bs_bhv->bs_tac, (sbptr->shapeIndex)); |
| | 490 | } |
| | 491 | } |
| | 492 | break; |
| | 493 | case I_bswitch_wait: |
| | 494 | { |
| | 495 | if(bs_bhv->request == I_no_request) |
| | 496 | return; |
| | 497 | |
| | 498 | //int oldState = bs_bhv->state; |
| | 499 | |
| | 500 | if(bs_bhv->request == I_request_on) |
| | 501 | { |
| | 502 | if(bs_bhv->state) |
| | 503 | break;//switch cannot be activated again until it is reset |
| | 504 | |
| | 505 | switch(bs_bhv->bs_dtype) |
| | 506 | { |
| | 507 | case binswitch_move_me: |
| | 508 | case binswitch_animate_and_move_me: |
| | 509 | { |
| | 510 | // moving switch |
| | 511 | bs_bhv->new_state = 1; |
| | 512 | bs_bhv->new_request = 1; |
| | 513 | bs_bhv->mode_store = bs_bhv->bs_mode; |
| | 514 | bs_bhv->bs_mode = I_bswitch_moving; |
| | 515 | bs_bhv->bs_track->reverse = 0; |
| | 516 | Start_Track_Playing(bs_bhv->bs_track); |
| | 517 | } |
| | 518 | break; |
| | 519 | default: |
| | 520 | { |
| | 521 | int i; |
| | 522 | bs_bhv->state = 1; |
| | 523 | for (i=0; i < bs_bhv->num_targets; i++) |
| | 524 | { |
| | 525 | if (bs_bhv->bs_targets[i]) |
| | 526 | RequestState(bs_bhv->bs_targets[i], bs_bhv->request_messages[i] , sbptr); |
| | 527 | } |
| | 528 | } |
| | 529 | } |
| | 530 | } |
| | 531 | else |
| | 532 | { |
| | 533 | if(!bs_bhv->state) |
| | 534 | break; //can only be deactivated if currently on |
| | 535 | |
| | 536 | switch(bs_bhv->bs_dtype) |
| | 537 | { |
| | 538 | case binswitch_move_me: |
| | 539 | case binswitch_animate_and_move_me: |
| | 540 | { |
| | 541 | // moving switch |
| | 542 | bs_bhv->new_state = 0; |
| | 543 | |
| | 544 | //check to see what the switch off request is |
| | 545 | if(bs_bhv->switch_off_message_same) |
| | 546 | bs_bhv->new_request = 1; |
| | 547 | else if(bs_bhv->switch_off_message_none) |
| | 548 | bs_bhv->new_request = -1; |
| | 549 | else |
| | 550 | bs_bhv->new_request = 0; |
| | 551 | |
| | 552 | bs_bhv->mode_store = bs_bhv->bs_mode; |
| | 553 | bs_bhv->bs_mode = I_bswitch_moving; |
| | 554 | bs_bhv->bs_track->reverse = 1; |
| | 555 | Start_Track_Playing(bs_bhv->bs_track); |
| | 556 | } |
| | 557 | break; |
| | 558 | default: |
| | 559 | { |
| | 560 | bs_bhv->state = 0; |
| | 561 | |
| | 562 | if(!bs_bhv->switch_off_message_none) |
| | 563 | { |
| | 564 | int i; |
| | 565 | for (i=0; i < bs_bhv->num_targets; i++) |
| | 566 | { |
| | 567 | if (bs_bhv->bs_targets[i]) |
| | 568 | RequestState(bs_bhv->bs_targets[i], bs_bhv->request_messages[i]^(!bs_bhv->switch_off_message_same), sbptr); |
| | 569 | } |
| | 570 | } |
| | 571 | } |
| | 572 | } |
| | 573 | } |
| | 574 | |
| | 575 | if(bs_bhv->bs_tac) |
| | 576 | { |
| | 577 | bs_bhv->bs_tac->tac_sequence = bs_bhv->state ? 1 : 0; |
| | 578 | bs_bhv->bs_tac->tac_txah_s = GetTxAnimHeaderFromShape(bs_bhv->bs_tac, sbptr->shapeIndex); |
| | 579 | } |
| | 580 | } |
| | 581 | break; |
| | 582 | case I_bswitch_moving: |
| | 583 | { |
| | 584 | Update_Track_Position(bs_bhv->bs_track); |
| | 585 | |
| | 586 | if(!bs_bhv->bs_track->playing) |
| | 587 | { |
| | 588 | bs_bhv->bs_mode = bs_bhv->mode_store; |
| | 589 | bs_bhv->state = bs_bhv->new_state; |
| | 590 | |
| | 591 | if(bs_bhv->bs_tac) |
| | 592 | { |
| | 593 | bs_bhv->bs_tac->tac_sequence = bs_bhv->state ? 1 : 0; |
| | 594 | bs_bhv->bs_tac->tac_txah_s = GetTxAnimHeaderFromShape(bs_bhv->bs_tac, sbptr->shapeIndex); |
| | 595 | } |
| | 596 | |
| | 597 | if (bs_bhv->new_request != -1) |
| | 598 | { |
| | 599 | int i; |
| | 600 | for (i=0; i < bs_bhv->num_targets; i++) |
| | 601 | { |
| | 602 | if (bs_bhv->bs_targets[i]) |
| | 603 | { |
| | 604 | if(bs_bhv->new_request) |
| | 605 | RequestState(bs_bhv->bs_targets[i], bs_bhv->request_messages[i] , sbptr); |
| | 606 | else |
| | 607 | RequestState(bs_bhv->bs_targets[i], bs_bhv->request_messages[i]^1, sbptr); |
| | 608 | } |
| | 609 | } |
| | 610 | } |
| | 611 | } |
| | 612 | } |
| | 613 | break; |
| | 614 | case I_bswitch_time_delay: |
| | 615 | { |
| | 616 | if(bs_bhv->timer > 0) |
| | 617 | { |
| | 618 | bs_bhv->timer -= NormalFrameTime; |
| | 619 | |
| | 620 | if(bs_bhv->timer <= 0 || bs_bhv->request == I_request_off) |
| | 621 | { |
| | 622 | //time to send the request |
| | 623 | bs_bhv->timer = 0; |
| | 624 | |
| | 625 | //only send message if we got here through the time running out |
| | 626 | if(bs_bhv->request != I_request_off) |
| | 627 | { |
| | 628 | int i; |
| | 629 | for (i=0; i < bs_bhv->num_targets; i++) |
| | 630 | { |
| | 631 | if (bs_bhv->bs_targets[i]) |
| | 632 | RequestState(bs_bhv->bs_targets[i], bs_bhv->request_messages[i] , sbptr); |
| | 633 | } |
| | 634 | } |
| | 635 | |
| | 636 | bs_bhv->state = 0; |
| | 637 | |
| | 638 | // swap sequence |
| | 639 | if(bs_bhv->bs_tac) |
| | 640 | { |
| | 641 | bs_bhv->bs_tac->tac_sequence = 0; |
| | 642 | bs_bhv->bs_tac->tac_txah_s = GetTxAnimHeaderFromShape(bs_bhv->bs_tac, (sbptr->shapeIndex)); |
| | 643 | } |
| | 644 | |
| | 645 | if(SinglePlayer != AvP.PlayMode) |
| | 646 | bs_bhv->TimeUntilNetSynchAllowed = 5 * ONE_FIXED; |
| | 647 | } |
| | 648 | } |
| | 649 | else |
| | 650 | { |
| | 651 | if(bs_bhv->request == I_request_on && !bs_bhv->state) |
| | 652 | { |
| | 653 | bs_bhv->timer = bs_bhv->time_for_reset; |
| | 654 | bs_bhv->state = 1; |
| | 655 | |
| | 656 | // swap sequence |
| | 657 | if(bs_bhv->bs_tac) |
| | 658 | { |
| | 659 | bs_bhv->bs_tac->tac_sequence = 1; |
| | 660 | bs_bhv->bs_tac->tac_txah_s = GetTxAnimHeaderFromShape(bs_bhv->bs_tac, (sbptr->shapeIndex)); |
| | 661 | } |
| | 662 | } |
| | 663 | } |
| | 664 | } |
| | 665 | break; |
| | 666 | default: |
| | 667 | assert(2 < 1); |
| | 668 | } |
| | 669 | |
| | 670 | bs_bhv->request = I_no_request; |
| | 671 | } |
| | 672 | |
| | 673 | #define BINARYSWITCHSYNCH_ON 0 |
| | 674 | #define BINARYSWITCHSYNCH_OFF 1 |
| | 675 | #define BINARYSWITCHSYNCH_IGNORE 2 |
| | 676 | |
| | 677 | int BinarySwitchGetSynchData(STRATEGYBLOCK* sbPtr) |
| | 678 | { |
| | 679 | assert(sbPtr); |
| | 680 | BINARY_SWITCH_BEHAV_BLOCK *bs_bhv = (BINARY_SWITCH_BEHAV_BLOCK*)sbPtr->dataptr; |
| | 681 | assert((bs_bhv->bhvr_type == I_BehaviourBinarySwitch)); |
| | 682 | |
| | 683 | //don't try to synch moving switches |
| | 684 | if(bs_bhv->bs_mode == I_bswitch_moving) |
| | 685 | return BINARYSWITCHSYNCH_IGNORE; |
| | 686 | |
| | 687 | return ((bs_bhv->state) ? BINARYSWITCHSYNCH_ON : BINARYSWITCHSYNCH_OFF); |
| | 688 | } |
| | 689 | |
| | 690 | void BinarySwitchSetSynchData(STRATEGYBLOCK* sbPtr,int status) |
| | 691 | { |
| | 692 | assert(sbPtr); |
| | 693 | BINARY_SWITCH_BEHAV_BLOCK *bs_bhv = (BINARY_SWITCH_BEHAV_BLOCK*)sbPtr->dataptr; |
| | 694 | assert((bs_bhv->bhvr_type == I_BehaviourBinarySwitch)); |
| | 695 | |
| | 696 | if((bs_bhv->TimeUntilNetSynchAllowed > 0) || (bs_bhv->bs_mode == I_bswitch_moving)) |
| | 697 | return; //ignore this attempt to synch the switch, don't try to synch moving switches |
| | 698 | |
| | 699 | switch(status) |
| | 700 | { |
| | 701 | case BINARYSWITCHSYNCH_ON : |
| | 702 | if(!bs_bhv->state) |
| | 703 | { |
| | 704 | //this switch should be on |
| | 705 | RequestState(sbPtr,1,0); |
| | 706 | } |
| | 707 | break; |
| | 708 | case BINARYSWITCHSYNCH_OFF : |
| | 709 | if(bs_bhv->state) |
| | 710 | { |
| | 711 | //this switch should be off |
| | 712 | RequestState(sbPtr,0,0); |
| | 713 | } |
| | 714 | } |
| | 715 | } |
| | 716 | |
| | 717 | /*--------------------** |
| | 718 | ** Loading and Saving ** |
| | 719 | **--------------------*/ |
| | 720 | #include "savegame.h" |
| | 721 | typedef struct binary_switch_save_block |
| | 722 | { |
| | 723 | SAVE_BLOCK_STRATEGY_HEADER header; |
| | 724 | |
| | 725 | BINARY_SWITCH_REQUEST_STATE request; |
| | 726 | int state; |
| | 727 | BSWITCH_MODE bs_mode; |
| | 728 | int timer; |
| | 729 | |
| | 730 | BSWITCH_MODE mode_store; |
| | 731 | |
| | 732 | int new_state; |
| | 733 | int new_request; |
| | 734 | int triggered_last; |
| | 735 | int txanim_sequence; |
| | 736 | |
| | 737 | } BINARY_SWITCH_SAVE_BLOCK; |
| | 738 | |
| | 739 | //defines for load/save macros |
| | 740 | #define SAVELOAD_BLOCK block |
| | 741 | #define SAVELOAD_BEHAV bs_bhv |
| | 742 | |
| | 743 | void LoadStrategy_BinarySwitch(SAVE_BLOCK_STRATEGY_HEADER* header) |
| | 744 | { |
| | 745 | BINARY_SWITCH_SAVE_BLOCK* block = (BINARY_SWITCH_SAVE_BLOCK*) header; |
| | 746 | |
| | 747 | //check the size of the save block |
| | 748 | if(header->size != sizeof(*block)) |
| | 749 | return; |
| | 750 | |
| | 751 | //find the existing strategy block |
| | 752 | STRATEGYBLOCK* sbPtr = FindSBWithName(header->SBname); |
| | 753 | |
| | 754 | if(!sbPtr) |
| | 755 | return; |
| | 756 | |
| | 757 | //make sure the strategy found is of the right type |
| | 758 | if(sbPtr->type != I_BehaviourBinarySwitch) |
| | 759 | return; |
| | 760 | |
| | 761 | BINARY_SWITCH_BEHAV_BLOCK *bs_bhv = (BINARY_SWITCH_BEHAV_BLOCK*)sbPtr->dataptr; |
| | 762 | |
| | 763 | //start copying stuff |
| | 764 | |
| | 765 | COPYELEMENT_LOAD(request) |
| | 766 | COPYELEMENT_LOAD(state) |
| | 767 | COPYELEMENT_LOAD(bs_mode) |
| | 768 | COPYELEMENT_LOAD(timer) |
| | 769 | COPYELEMENT_LOAD(mode_store) |
| | 770 | COPYELEMENT_LOAD(new_state) |
| | 771 | COPYELEMENT_LOAD(new_request) |
| | 772 | COPYELEMENT_LOAD(triggered_last) |
| | 773 | |
| | 774 | //set the texture animation sequence |
| | 775 | if(bs_bhv->bs_tac) |
| | 776 | { |
| | 777 | bs_bhv->bs_tac->tac_sequence = block->txanim_sequence; |
| | 778 | bs_bhv->bs_tac->tac_txah_s = GetTxAnimHeaderFromShape(bs_bhv->bs_tac, (sbPtr->shapeIndex)); |
| | 779 | } |
| | 780 | |
| | 781 | //load the track position , if the switch has one |
| | 782 | if(bs_bhv->bs_track) |
| | 783 | { |
| | 784 | SAVE_BLOCK_HEADER* track_header = GetNextBlockIfOfType(SaveBlock_Track); |
| | 785 | |
| | 786 | if(track_header) |
| | 787 | LoadTrackPosition(track_header,bs_bhv->bs_track); |
| | 788 | } |
| | 789 | } |
| | 790 | |
| | 791 | void SaveStrategy_BinarySwitch(STRATEGYBLOCK* sbPtr) |
| | 792 | { |
| | 793 | BINARY_SWITCH_SAVE_BLOCK *block; |
| | 794 | BINARY_SWITCH_BEHAV_BLOCK *bs_bhv = (BINARY_SWITCH_BEHAV_BLOCK*)sbPtr->dataptr; |
| | 795 | |
| | 796 | GET_STRATEGY_SAVE_BLOCK(block, sbPtr); |
| | 797 | |
| | 798 | //start copying stuff |
| | 799 | |
| | 800 | COPYELEMENT_SAVE(request) |
| | 801 | COPYELEMENT_SAVE(state) |
| | 802 | COPYELEMENT_SAVE(bs_mode) |
| | 803 | COPYELEMENT_SAVE(timer) |
| | 804 | COPYELEMENT_SAVE(mode_store) |
| | 805 | COPYELEMENT_SAVE(new_state) |
| | 806 | COPYELEMENT_SAVE(new_request) |
| | 807 | COPYELEMENT_SAVE(triggered_last) |
| | 808 | |
| | 809 | //get the animation sequence |
| | 810 | if(bs_bhv->bs_tac) |
| | 811 | block->txanim_sequence = bs_bhv->bs_tac->tac_sequence; |
| | 812 | else |
| | 813 | block->txanim_sequence = 0; |
| | 814 | |
| | 815 | //save the track position , if the switch has one |
| | 816 | if(bs_bhv->bs_track) |
| | 817 | SaveTrackPosition(bs_bhv->bs_track); |
| | 818 | } |