| | 1 | #include "system.h" |
| | 2 | #include "stratdef.h" |
| | 3 | #include "bh_types.h" |
| | 4 | #include "bh_track.h" |
| | 5 | #include "dynamics.h" |
| | 6 | #include "pvisible.h" |
| | 7 | #include "debris.h" |
| | 8 | #include <assert.h> |
| | 9 | #include <stdlib.h> |
| | 10 | |
| | 11 | extern int RealFrameTime; |
| | 12 | |
| | 13 | void NotifyTargetsForTrackPoint(TRACK_OBJECT_BEHAV_BLOCK* to_bhv,int point_num,int reversing) |
| | 14 | { |
| | 15 | int i = 0; |
| | 16 | assert(to_bhv); |
| | 17 | |
| | 18 | for(; i < to_bhv->num_special_track_points; i++) |
| | 19 | { |
| | 20 | if(to_bhv->special_track_points[i].track_point_no == point_num) |
| | 21 | { |
| | 22 | SPECIAL_TRACK_POINT* stp = &to_bhv->special_track_points[i]; |
| | 23 | int j = 0; |
| | 24 | |
| | 25 | for(; j < stp->num_targets;j++) |
| | 26 | { |
| | 27 | TRACK_POINT_TARGET* tpt = &stp->targets[j]; |
| | 28 | |
| | 29 | if(reversing) |
| | 30 | { |
| | 31 | if(tpt->flags & TrackRequestFlag_ActiveBackward) |
| | 32 | { |
| | 33 | if(tpt->flags & TrackRequestFlag_OppositeBackward) |
| | 34 | RequestState(tpt->target_sbptr,!tpt->request,0); |
| | 35 | else |
| | 36 | RequestState(tpt->target_sbptr,tpt->request,0); |
| | 37 | } |
| | 38 | } |
| | 39 | else |
| | 40 | { |
| | 41 | if(tpt->flags & TrackRequestFlag_ActiveForward) |
| | 42 | RequestState(tpt->target_sbptr,tpt->request,0); |
| | 43 | } |
| | 44 | } |
| | 45 | break; |
| | 46 | } |
| | 47 | } |
| | 48 | } |
| | 49 | |
| | 50 | void TrackObjectBehaveInit(void* bhdata, STRATEGYBLOCK* sbptr) |
| | 51 | { |
| | 52 | int i; |
| | 53 | TRACK_OBJECT_TOOLS_TEMPLATE* to_tt = (TRACK_OBJECT_TOOLS_TEMPLATE*)bhdata; |
| | 54 | TRACK_OBJECT_BEHAV_BLOCK* to_bhv = malloc(sizeof(TRACK_OBJECT_BEHAV_BLOCK)); |
| | 55 | sbptr->DynPtr = AllocateDynamicsBlock(DYNAMICS_TEMPLATE_STATIC); |
| | 56 | sbptr->dataptr = to_bhv; |
| | 57 | sbptr->containingModule = ModuleFromPosition(&to_tt->position, NULL); |
| | 58 | |
| | 59 | if((NULL == to_bhv) || (NULL == sbptr->DynPtr))// || (NULL == sbptr->containingModule)) |
| | 60 | { |
| | 61 | RemoveBehaviourStrategy(sbptr); |
| | 62 | return; |
| | 63 | } |
| | 64 | |
| | 65 | sbptr->maintainVisibility = 1; |
| | 66 | to_bhv->bhvr_type = I_BehaviourTrackObject; |
| | 67 | |
| | 68 | COPY_NAME(sbptr->SBname, to_tt->nameID); |
| | 69 | |
| | 70 | to_bhv->to_track = to_tt->track; |
| | 71 | to_bhv->to_track->sbptr = sbptr; |
| | 72 | |
| | 73 | sbptr->DynPtr->IsStatic = 0; |
| | 74 | sbptr->DynPtr->IsNetGhost = 1; |
| | 75 | sbptr->DynPtr->Position = sbptr->DynPtr->PrevPosition = to_tt->position; |
| | 76 | sbptr->DynPtr->OrientEuler = to_tt->orientation; |
| | 77 | CreateEulerMatrix(&sbptr->DynPtr->OrientEuler, &sbptr->DynPtr->OrientMat); |
| | 78 | TransposeMatrixCH(&sbptr->DynPtr->OrientMat); |
| | 79 | |
| | 80 | |
| | 81 | /*check to see if object is animated.*/ |
| | 82 | { |
| | 83 | int item_num = 0; |
| | 84 | struct shapeheader* shptr = mainshapelist[sbptr->shapeIndex]; |
| | 85 | TXACTRLBLK **pptxactrlblk = &to_bhv->to_tac; |
| | 86 | |
| | 87 | for(; item_num < shptr->numitems; item_num++) |
| | 88 | { |
| | 89 | POLYHEADER *poly = (POLYHEADER*)(shptr->items[item_num]); |
| | 90 | assert(poly); |
| | 91 | |
| | 92 | if(poly->PolyFlags & iflag_txanim) |
| | 93 | { |
| | 94 | TXACTRLBLK *pnew_txactrlblk = malloc(sizeof(TXACTRLBLK)); |
| | 95 | |
| | 96 | if(pnew_txactrlblk) |
| | 97 | { |
| | 98 | pnew_txactrlblk->tac_flags = 0; |
| | 99 | pnew_txactrlblk->tac_item = item_num; |
| | 100 | pnew_txactrlblk->tac_sequence = 0; |
| | 101 | pnew_txactrlblk->tac_node = 0; |
| | 102 | pnew_txactrlblk->tac_txarray = GetTxAnimArrayZ(sbptr->shapeIndex, item_num); |
| | 103 | pnew_txactrlblk->tac_txah_s = GetTxAnimHeaderFromShape(pnew_txactrlblk, sbptr->shapeIndex); |
| | 104 | |
| | 105 | *pptxactrlblk = pnew_txactrlblk; |
| | 106 | pptxactrlblk = &pnew_txactrlblk->tac_next; |
| | 107 | } |
| | 108 | else |
| | 109 | { |
| | 110 | *pptxactrlblk = NULL; |
| | 111 | } |
| | 112 | } |
| | 113 | } |
| | 114 | *pptxactrlblk = NULL; |
| | 115 | } |
| | 116 | |
| | 117 | to_bhv->request = track_no_request; |
| | 118 | to_bhv->num_special_track_points = to_tt->num_special_track_points; |
| | 119 | to_bhv->special_track_points = to_tt->special_track_points; |
| | 120 | to_bhv->destruct_target_request = to_tt->destruct_target_request; |
| | 121 | |
| | 122 | for(i=0; i < SB_NAME_LENGTH; i++) |
| | 123 | to_bhv->destruct_target_ID[i] = to_tt->destruct_target_ID[i]; |
| | 124 | |
| | 125 | to_bhv->destruct_target_sbptr = 0; |
| | 126 | |
| | 127 | /* Initialise object's stats */ |
| | 128 | { |
| | 129 | const NPC_DATA *NpcData = &NpcDataList[I_NPC_DefaultInanimate]; |
| | 130 | |
| | 131 | sbptr->DamageBlock = NpcData->StartingStats; |
| | 132 | sbptr->DamageBlock.Health = NpcData->StartingStats.Health << ONE_FIXED_SHIFT; |
| | 133 | sbptr->DamageBlock.Armour = NpcData->StartingStats.Armour << ONE_FIXED_SHIFT; |
| | 134 | } |
| | 135 | |
| | 136 | sbptr->DamageBlock.Health *= to_tt->integrity; |
| | 137 | |
| | 138 | sbptr->DamageBlock.Indestructable = (to_tt->integrity > 20); |
| | 139 | |
| | 140 | Update_Track_Position_Only(to_bhv->to_track); |
| | 141 | |
| | 142 | to_bhv->TimeUntilNetSynchAllowed = 0; |
| | 143 | } |
| | 144 | |
| | 145 | void TrackObjectBehaveFun(STRATEGYBLOCK* sbptr) |
| | 146 | { |
| | 147 | TRACK_OBJECT_BEHAV_BLOCK *to_bhv = (TRACK_OBJECT_BEHAV_BLOCK*)sbptr->dataptr; |
| | 148 | assert((to_bhv->bhvr_type == I_BehaviourTrackObject)); |
| | 149 | TRACK_CONTROLLER* track = to_bhv->to_track; |
| | 150 | |
| | 151 | if(sbptr->DamageBlock.IsOnFire) |
| | 152 | { |
| | 153 | CauseDamageToObject(sbptr, &TemplateAmmo[AMMO_FLAMETHROWER].MaxDamage, ONE_FIXED/400, NULL); |
| | 154 | |
| | 155 | if(sbptr->please_destroy_me) |
| | 156 | return; |
| | 157 | } |
| | 158 | |
| | 159 | //update texture animation if object has one |
| | 160 | if(to_bhv->to_tac) |
| | 161 | { |
| | 162 | DISPLAYBLOCK* dptr = sbptr->DisplayBlock; |
| | 163 | |
| | 164 | if(dptr && !dptr->ObTxAnimCtrlBlks) |
| | 165 | dptr->ObTxAnimCtrlBlks = to_bhv->to_tac; |
| | 166 | } |
| | 167 | |
| | 168 | //check to see if any request have come through. |
| | 169 | |
| | 170 | if(SinglePlayer != AvP.PlayMode) |
| | 171 | { |
| | 172 | if(to_bhv->request == track_no_request) |
| | 173 | { |
| | 174 | to_bhv->TimeUntilNetSynchAllowed -= RealFrameTime; |
| | 175 | |
| | 176 | if(to_bhv->TimeUntilNetSynchAllowed < 0) |
| | 177 | to_bhv->TimeUntilNetSynchAllowed = 0; |
| | 178 | } |
| | 179 | else |
| | 180 | { |
| | 181 | to_bhv->TimeUntilNetSynchAllowed = 5 * ONE_FIXED; |
| | 182 | } |
| | 183 | } |
| | 184 | |
| | 185 | switch(to_bhv->request) |
| | 186 | { |
| | 187 | case track_request_start: |
| | 188 | { |
| | 189 | Start_Track_Playing(track); |
| | 190 | |
| | 191 | to_bhv->request = track_no_request; |
| | 192 | } |
| | 193 | break; |
| | 194 | case track_request_startforward: |
| | 195 | { |
| | 196 | track->reverse = 0; |
| | 197 | Start_Track_Playing(track); |
| | 198 | |
| | 199 | to_bhv->request = track_no_request; |
| | 200 | } |
| | 201 | break; |
| | 202 | case track_request_startbackward: |
| | 203 | { |
| | 204 | track->reverse = 1; |
| | 205 | Start_Track_Playing(track); |
| | 206 | |
| | 207 | to_bhv->request = track_no_request; |
| | 208 | } |
| | 209 | break; |
| | 210 | case track_request_stop: |
| | 211 | { |
| | 212 | Stop_Track_Playing(track); |
| | 213 | |
| | 214 | to_bhv->request = track_no_request; |
| | 215 | } |
| | 216 | default: |
| | 217 | break; |
| | 218 | } |
| | 219 | |
| | 220 | if(!track->playing) |
| | 221 | return; |
| | 222 | |
| | 223 | int current_section = track->current_section; |
| | 224 | int reversing = track->reverse; |
| | 225 | |
| | 226 | Update_Track_Position(track); |
| | 227 | |
| | 228 | if(to_bhv->special_track_points) |
| | 229 | { |
| | 230 | //see which track points the track has gone through, and notify the |
| | 231 | //targets for those points |
| | 232 | |
| | 233 | /* |
| | 234 | if(reversing) |
| | 235 | { |
| | 236 | while(current_section != track->current_section) |
| | 237 | { |
| | 238 | NotifyTargetsForTrackPoint(to_bhv,current_section,1); |
| | 239 | current_section--; |
| | 240 | |
| | 241 | if(current_section < 0) |
| | 242 | { |
| | 243 | //looped to end , so passed last point |
| | 244 | current_section = track->num_sections-1; |
| | 245 | NotifyTargetsForTrackPoint(to_bhv,track->num_sections,1); |
| | 246 | } |
| | 247 | } |
| | 248 | |
| | 249 | if(!current_section && !track->playing) |
| | 250 | { |
| | 251 | //stopped at start , so passed point 0 |
| | 252 | NotifyTargetsForTrackPoint(to_bhv,0,1); |
| | 253 | } |
| | 254 | } |
| | 255 | else |
| | 256 | { |
| | 257 | while(current_section != track->current_section) |
| | 258 | { |
| | 259 | NotifyTargetsForTrackPoint(to_bhv,current_section+1,0); |
| | 260 | current_section++; |
| | 261 | |
| | 262 | if(current_section >= track->num_sections) |
| | 263 | { |
| | 264 | //looped to start , so passed first point |
| | 265 | current_section = 0; |
| | 266 | NotifyTargetsForTrackPoint(to_bhv,0,0); |
| | 267 | } |
| | 268 | } |
| | 269 | |
| | 270 | if(current_section == (track->num_sections-1) && !track->playing) |
| | 271 | { |
| | 272 | //stopped at end , so passed last point |
| | 273 | NotifyTargetsForTrackPoint(to_bhv,track->num_sections,0); |
| | 274 | } |
| | 275 | } |
| | 276 | */ |
| | 277 | while(reversing != track->reverse || current_section != track->current_section) |
| | 278 | { |
| | 279 | if(reversing) |
| | 280 | { |
| | 281 | NotifyTargetsForTrackPoint(to_bhv,current_section,1); |
| | 282 | current_section--; |
| | 283 | |
| | 284 | if(current_section < 0) |
| | 285 | { |
| | 286 | if(track->loop) |
| | 287 | { |
| | 288 | //looped to end , so passed last point |
| | 289 | current_section = track->num_sections - 1; |
| | 290 | NotifyTargetsForTrackPoint(to_bhv,track->num_sections, 1); |
| | 291 | } |
| | 292 | else |
| | 293 | { |
| | 294 | current_section = reversing = 0; |
| | 295 | } |
| | 296 | } |
| | 297 | } |
| | 298 | else |
| | 299 | { |
| | 300 | NotifyTargetsForTrackPoint(to_bhv,current_section+1, 0); |
| | 301 | current_section++; |
| | 302 | |
| | 303 | if(current_section >= track->num_sections) |
| | 304 | { |
| | 305 | if(track->loop) |
| | 306 | { |
| | 307 | //looped to start , so passed first point |
| | 308 | current_section = 0; |
| | 309 | NotifyTargetsForTrackPoint(to_bhv, 0, 0); |
| | 310 | } |
| | 311 | else |
| | 312 | { |
| | 313 | current_section = track->num_sections - 1; |
| | 314 | reversing = 1; |
| | 315 | } |
| | 316 | } |
| | 317 | } |
| | 318 | } |
| | 319 | |
| | 320 | if(!track->playing) |
| | 321 | { |
| | 322 | if(track->reverse) |
| | 323 | { |
| | 324 | //stopped at end , so passed last point |
| | 325 | NotifyTargetsForTrackPoint(to_bhv,track->num_sections,0); |
| | 326 | } |
| | 327 | else |
| | 328 | { |
| | 329 | //stopped at start , so passed point 0 |
| | 330 | NotifyTargetsForTrackPoint(to_bhv,0,1); |
| | 331 | } |
| | 332 | } |
| | 333 | } |
| | 334 | } |
| | 335 | |
| | 336 | void TrackObjectIsDamaged(STRATEGYBLOCK *sbPtr, const DAMAGE_PROFILE *damage, int multiple) |
| | 337 | { |
| | 338 | TRACK_OBJECT_BEHAV_BLOCK* to_bhv = sbPtr->dataptr; |
| | 339 | assert(to_bhv); |
| | 340 | |
| | 341 | if(SinglePlayer != AvP.PlayMode) |
| | 342 | { |
| | 343 | //I don't want to consider destructable track objects in net games for the moment |
| | 344 | //would screw up the strategy synching |
| | 345 | return; |
| | 346 | } |
| | 347 | |
| | 348 | if(sbPtr->DamageBlock.Health <= 0) |
| | 349 | { |
| | 350 | //notify target of destruction |
| | 351 | if(to_bhv->destruct_target_sbptr) |
| | 352 | RequestState(to_bhv->destruct_target_sbptr,to_bhv->destruct_target_request,0); |
| | 353 | |
| | 354 | MakeFragments(sbPtr); |
| | 355 | sbPtr->please_destroy_me = 1; |
| | 356 | } |
| | 357 | } |
| | 358 | |
| | 359 | #define TRACKSYNCH_ON_FORWARD 0 |
| | 360 | #define TRACKSYNCH_OFF_FORWARD 1 |
| | 361 | #define TRACKSYNCH_ON_REVERSE 2 |
| | 362 | #define TRACKSYNCH_OFF_REVERSE 3 |
| | 363 | |
| | 364 | int TrackObjectGetSynchData(STRATEGYBLOCK* sbPtr) |
| | 365 | { |
| | 366 | TRACK_OBJECT_BEHAV_BLOCK* to_bhv = sbPtr->dataptr; |
| | 367 | assert(to_bhv); |
| | 368 | |
| | 369 | if(to_bhv->to_track->playing) |
| | 370 | return (to_bhv->to_track->reverse) ? TRACKSYNCH_ON_REVERSE : TRACKSYNCH_ON_FORWARD; |
| | 371 | else |
| | 372 | return (to_bhv->to_track->reverse) ? TRACKSYNCH_OFF_REVERSE : TRACKSYNCH_OFF_FORWARD; |
| | 373 | } |
| | 374 | |
| | 375 | void TrackObjectSetSynchData(STRATEGYBLOCK* sbPtr,int status) |
| | 376 | { |
| | 377 | TRACK_OBJECT_BEHAV_BLOCK* to_bhv = sbPtr->dataptr; |
| | 378 | assert(to_bhv); |
| | 379 | |
| | 380 | if(to_bhv->TimeUntilNetSynchAllowed > 0) |
| | 381 | return; //ignore this attempt to synch the switch |
| | 382 | |
| | 383 | if(to_bhv->to_track->playing) |
| | 384 | return; //don't bother stopping moving tracks; |
| | 385 | |
| | 386 | switch(status) |
| | 387 | { |
| | 388 | case TRACKSYNCH_ON_FORWARD: |
| | 389 | to_bhv->request = track_request_startforward; |
| | 390 | break; |
| | 391 | case TRACKSYNCH_OFF_FORWARD: |
| | 392 | if(to_bhv->to_track->reverse && !to_bhv->to_track->loop) |
| | 393 | { |
| | 394 | //track is at the end , but should be at the start |
| | 395 | to_bhv->request = track_request_startbackward; |
| | 396 | } |
| | 397 | break; |
| | 398 | case TRACKSYNCH_ON_REVERSE: |
| | 399 | to_bhv->request = track_request_startbackward; |
| | 400 | break; |
| | 401 | case TRACKSYNCH_OFF_REVERSE: |
| | 402 | if(!to_bhv->to_track->reverse && !to_bhv->to_track->loop) |
| | 403 | { |
| | 404 | //track is at the start , but should be at the end |
| | 405 | to_bhv->request = track_request_startforward; |
| | 406 | } |
| | 407 | } |
| | 408 | } |
| | 409 | |
| | 410 | /*--------------------** |
| | 411 | ** Loading and Saving ** |
| | 412 | **--------------------*/ |
| | 413 | #include "savegame.h" |
| | 414 | typedef struct track_object_save_block |
| | 415 | { |
| | 416 | SAVE_BLOCK_STRATEGY_HEADER header; |
| | 417 | |
| | 418 | TRACK_OBJECT_REQUEST_STATE request; |
| | 419 | |
| | 420 | //strategyblock stuff |
| | 421 | DAMAGEBLOCK DamageBlock; |
| | 422 | |
| | 423 | } TRACK_OBJECT_SAVE_BLOCK; |
| | 424 | |
| | 425 | //defines for load/save macros |
| | 426 | #define SAVELOAD_BLOCK block |
| | 427 | #define SAVELOAD_BEHAV to_bhv |
| | 428 | |
| | 429 | void LoadStrategy_TrackObject(SAVE_BLOCK_STRATEGY_HEADER* header) |
| | 430 | { |
| | 431 | TRACK_OBJECT_SAVE_BLOCK* block = (TRACK_OBJECT_SAVE_BLOCK*) header; |
| | 432 | |
| | 433 | //check the size of the save block |
| | 434 | if(header->size != sizeof(*block)) |
| | 435 | return; |
| | 436 | |
| | 437 | //find the existing strategy block |
| | 438 | STRATEGYBLOCK* sbPtr = FindSBWithName(header->SBname); |
| | 439 | |
| | 440 | if(!sbPtr) |
| | 441 | return; |
| | 442 | |
| | 443 | //make sure the strategy found is of the right type |
| | 444 | if(sbPtr->type != I_BehaviourTrackObject) |
| | 445 | return; |
| | 446 | |
| | 447 | TRACK_OBJECT_BEHAV_BLOCK *to_bhv = (TRACK_OBJECT_BEHAV_BLOCK*)sbPtr->dataptr; |
| | 448 | |
| | 449 | //start copying stuff |
| | 450 | |
| | 451 | COPYELEMENT_LOAD(request) |
| | 452 | |
| | 453 | sbPtr->DamageBlock = block->DamageBlock; |
| | 454 | |
| | 455 | //load the track position |
| | 456 | if(to_bhv->to_track) |
| | 457 | { |
| | 458 | SAVE_BLOCK_HEADER* track_header = GetNextBlockIfOfType(SaveBlock_Track); |
| | 459 | |
| | 460 | if(track_header) |
| | 461 | LoadTrackPosition(track_header,to_bhv->to_track); |
| | 462 | } |
| | 463 | } |
| | 464 | |
| | 465 | void SaveStrategy_TrackObject(STRATEGYBLOCK* sbPtr) |
| | 466 | { |
| | 467 | TRACK_OBJECT_SAVE_BLOCK* block; |
| | 468 | TRACK_OBJECT_BEHAV_BLOCK *to_bhv = (TRACK_OBJECT_BEHAV_BLOCK*)sbPtr->dataptr; |
| | 469 | |
| | 470 | GET_STRATEGY_SAVE_BLOCK(block,sbPtr); |
| | 471 | |
| | 472 | //start copying stuff |
| | 473 | |
| | 474 | COPYELEMENT_SAVE(request) |
| | 475 | |
| | 476 | block->DamageBlock = sbPtr->DamageBlock; |
| | 477 | |
| | 478 | //save the track position |
| | 479 | if(to_bhv->to_track) |
| | 480 | SaveTrackPosition(to_bhv->to_track); |
| | 481 | } |