| | 1 | #include "system.h" |
| | 2 | #include "prototyp.h" |
| | 3 | #include "bh_types.h" |
| | 4 | #include "pheromon.h" |
| | 5 | #include "bh_far.h" |
| | 6 | #include "pfarlocs.h" |
| | 7 | #include "dynamics.h" |
| | 8 | #include "pvisible.h" |
| | 9 | #include "bh_ais.h" |
| | 10 | #include "net.h" |
| | 11 | #include <stdio.h> |
| | 12 | #include <assert.h> |
| | 13 | |
| | 14 | //static int entryPointFailures = 0; |
| | 15 | |
| | 16 | /* Patrick 2/7/97: |
| | 17 | This function turns the npc around (in y) a random amount: this is used to |
| | 18 | turn the npc around if it reaches an impasse in the environment, and the |
| | 19 | orientation is used to select a target module for wandering behaviour */ |
| | 20 | |
| | 21 | void FarNpc_FlipAround(STRATEGYBLOCK *sbPtr) |
| | 22 | { |
| | 23 | assert(sbPtr); |
| | 24 | /* get the dynamics block */ |
| | 25 | DYNAMICSBLOCK *dynPtr = sbPtr->DynPtr; |
| | 26 | assert(dynPtr); |
| | 27 | |
| | 28 | dynPtr->OrientEuler.EulerY += (1024 + FastRandom() % 1024); |
| | 29 | dynPtr->OrientEuler.EulerY &= wrap360; |
| | 30 | } |
| | 31 | |
| | 32 | AIMODULE *FarNPC_GetTargetAIModuleForRetreat(STRATEGYBLOCK *sbPtr) |
| | 33 | { |
| | 34 | extern unsigned int PlayerSmell; |
| | 35 | |
| | 36 | AIMODULE* targetModule = NULL; |
| | 37 | unsigned int targetSmell = PlayerSmell + 1; /* should be higher than any smell anywhere this frame */ |
| | 38 | unsigned int targetNumAdj = 0; |
| | 39 | |
| | 40 | assert(sbPtr); |
| | 41 | |
| | 42 | if(sbPtr->containingModule == NULL) |
| | 43 | return NULL; |
| | 44 | |
| | 45 | AIMODULE **AdjModuleRefPtr = sbPtr->containingModule->m_aimodule->m_link_ptrs; |
| | 46 | |
| | 47 | /* check that there is a list of adjacent modules, and that it is not |
| | 48 | empty (ie points to zero) */ |
| | 49 | |
| | 50 | if(AdjModuleRefPtr) |
| | 51 | { |
| | 52 | while(*AdjModuleRefPtr != 0) |
| | 53 | { |
| | 54 | /* get the index */ |
| | 55 | int AdjModuleIndex = (*AdjModuleRefPtr)->m_index; |
| | 56 | int AdjModuleSmell = PherPl_ReadBuf[AdjModuleIndex]; |
| | 57 | int AdjModuleNumAdjacencies = NumAdjacentModules((*AdjModuleRefPtr)); |
| | 58 | |
| | 59 | /* if this adjacent module's smell value is lower than |
| | 60 | the current 'highest smell' record the new module as the |
| | 61 | target. If they're equal, tie-break on number of adjacencies*/ |
| | 62 | if( (!targetModule) || |
| | 63 | (AdjModuleSmell < targetSmell)|| |
| | 64 | ((AdjModuleSmell == targetSmell) && (AdjModuleNumAdjacencies > targetNumAdj))) |
| | 65 | { |
| | 66 | targetSmell = PherPl_ReadBuf[AdjModuleIndex]; |
| | 67 | targetModule = *AdjModuleRefPtr; |
| | 68 | targetNumAdj = NumAdjacentModules((*AdjModuleRefPtr)); |
| | 69 | } |
| | 70 | /* next adjacent module reference pointer */ |
| | 71 | AdjModuleRefPtr++; |
| | 72 | } |
| | 73 | } |
| | 74 | |
| | 75 | return targetModule; |
| | 76 | } |
| | 77 | |
| | 78 | void LocateFarNPCInAIModule(STRATEGYBLOCK *sbPtr, AIMODULE *targetModule) |
| | 79 | { |
| | 80 | VECTORCH newPosition; |
| | 81 | MODULE *renderModule; |
| | 82 | int targetLocInx; |
| | 83 | |
| | 84 | /* now: a few tests for npc's that are generated... (aliens and marines) */ |
| | 85 | /* |
| | 86 | switch(sbPtr->type) |
| | 87 | { |
| | 88 | case I_BehaviourAlien: |
| | 89 | case I_BehaviourMarine: |
| | 90 | { |
| | 91 | //if(ModuleCurrVisArray[(*(targetModule->m_module_ptrs))->m_index]) |
| | 92 | if (AIModuleIsVisible(targetModule)) |
| | 93 | { |
| | 94 | if(NumGeneratorNPCsVisible() >= MAX_VISIBLEGENERATORNPCS) |
| | 95 | return; |
| | 96 | } |
| | 97 | } |
| | 98 | default: |
| | 99 | break; |
| | 100 | } |
| | 101 | */ |
| | 102 | |
| | 103 | /* now move the npc to it's target... */ |
| | 104 | int noOfAuxLocs = FALLP_AuxLocs[(targetModule->m_index)].numLocations; |
| | 105 | VECTORCH *auxLocsList = FALLP_AuxLocs[(targetModule->m_index)].locationsList; |
| | 106 | |
| | 107 | /* find the entry point for the target */ |
| | 108 | assert(sbPtr->containingModule); |
| | 109 | FARENTRYPOINT *targetEntryPoint = GetAIModuleEP(targetModule,(sbPtr->containingModule->m_aimodule)); |
| | 110 | assert(targetEntryPoint); |
| | 111 | |
| | 112 | /* if it's visible, use the entry point. |
| | 113 | if it's not visible, use an auxilary location. If there aren't any auxilary |
| | 114 | locations, use the entry point. */ |
| | 115 | |
| | 116 | //if(ModuleCurrVisArray[(*(targetModule->m_module_ptrs))->m_index]) |
| | 117 | if (AIModuleIsVisible(targetModule)) |
| | 118 | { |
| | 119 | newPosition = targetEntryPoint->position; |
| | 120 | targetLocInx = -1; |
| | 121 | } |
| | 122 | else |
| | 123 | { |
| | 124 | /* pick an auxilary location: if there aren't any, use the entry point */ |
| | 125 | if(noOfAuxLocs) |
| | 126 | { |
| | 127 | targetLocInx = FastRandom() % noOfAuxLocs; |
| | 128 | newPosition = auxLocsList[targetLocInx]; |
| | 129 | /* move up 1/2 npc height, plus a bit more(100). this only applies |
| | 130 | to auxilary locations, not eps */ |
| | 131 | |
| | 132 | if(SinglePlayer != AvP.PlayMode) |
| | 133 | { |
| | 134 | //Multiplayer game |
| | 135 | //send this new position to the other players |
| | 136 | AddNetMsg_FarAlienPosition(sbPtr, targetModule->m_index, targetLocInx, 0); |
| | 137 | } |
| | 138 | } |
| | 139 | else |
| | 140 | { |
| | 141 | newPosition = targetEntryPoint->position; |
| | 142 | |
| | 143 | if(SinglePlayer != AvP.PlayMode) |
| | 144 | { |
| | 145 | //Multiplayer game |
| | 146 | //send this new position to the other players |
| | 147 | AddNetMsg_FarAlienPosition(sbPtr, targetModule->m_index, sbPtr->containingModule->m_aimodule->m_index, 1); |
| | 148 | } |
| | 149 | } |
| | 150 | } |
| | 151 | |
| | 152 | { |
| | 153 | VECTORCH temp_Pos; |
| | 154 | |
| | 155 | temp_Pos.vx = newPosition.vx+targetModule->m_world.vx; |
| | 156 | temp_Pos.vy = newPosition.vy+targetModule->m_world.vy; |
| | 157 | temp_Pos.vz = newPosition.vz+targetModule->m_world.vz; |
| | 158 | |
| | 159 | renderModule = ModuleFromPosition(&temp_Pos, sbPtr->containingModule); |
| | 160 | |
| | 161 | if (renderModule == NULL) |
| | 162 | return; |
| | 163 | } |
| | 164 | |
| | 165 | /* now set the alien's new position and current module. |
| | 166 | NB this is world position + alien height in y + a little extra in y to make sure */ |
| | 167 | { |
| | 168 | DYNAMICSBLOCK *dynPtr = sbPtr->DynPtr; |
| | 169 | assert(dynPtr); |
| | 170 | |
| | 171 | dynPtr->Position = newPosition; |
| | 172 | dynPtr->Position.vx += targetModule->m_world.vx; |
| | 173 | dynPtr->Position.vy += targetModule->m_world.vy; |
| | 174 | dynPtr->Position.vz += targetModule->m_world.vz; |
| | 175 | dynPtr->PrevPosition = dynPtr->Position; |
| | 176 | dynPtr->OrientEuler.EulerX = dynPtr->OrientEuler.EulerZ = 0; |
| | 177 | |
| | 178 | { |
| | 179 | VECTORCH vec; |
| | 180 | vec.vx = targetModule->m_world.vx - sbPtr->containingModule->m_world.vx; |
| | 181 | vec.vz = targetModule->m_world.vz - sbPtr->containingModule->m_world.vz; |
| | 182 | vec.vy = 0; |
| | 183 | Normalise(&vec); |
| | 184 | dynPtr->OrientEuler.EulerY = ArcTan(vec.vx, vec.vz); |
| | 185 | } |
| | 186 | } |
| | 187 | |
| | 188 | /* finally, update the alien's module */ |
| | 189 | sbPtr->containingModule = renderModule; |
| | 190 | |
| | 191 | #if UseLocalAssert |
| | 192 | { |
| | 193 | VECTORCH localCoords; |
| | 194 | MODULE *thisModule = sbPtr->containingModule; |
| | 195 | |
| | 196 | assert(thisModule); |
| | 197 | |
| | 198 | localCoords = sbPtr->DynPtr->Position; |
| | 199 | localCoords.vx -= thisModule->m_world.vx; |
| | 200 | localCoords.vy -= thisModule->m_world.vy; |
| | 201 | localCoords.vz -= thisModule->m_world.vz; |
| | 202 | |
| | 203 | if(!PointIsInModule(thisModule, &localCoords)) |
| | 204 | { |
| | 205 | //printf("FAR ALIEN MODULE CONTAINMENT FAILURE \n"); |
| | 206 | |
| | 207 | printf("Alien containment failure: alien is in %s, position is %d,%d,%d:\nModule extents are: %d:%d, %d:%d, %d:%d", |
| | 208 | thisModule->name,localCoords.vx,localCoords.vy,localCoords.vz, |
| | 209 | thisModule->m_maxx,thisModule->m_minx,thisModule->m_maxy,thisModule->m_miny, |
| | 210 | thisModule->m_maxz,thisModule->m_minz); |
| | 211 | |
| | 212 | assert(1==0); |
| | 213 | } |
| | 214 | } |
| | 215 | #endif |
| | 216 | } |
| | 217 | |
| | 218 | /*--------------------Patrick 10/12/96---------------------- |
| | 219 | This function returns the status of a passed target |
| | 220 | module that an NPC might want to move to. |
| | 221 | ----------------------------------------------------------*/ |
| | 222 | |
| | 223 | NPC_TARGETMODULESTATUS GetTargetAIModuleStatus(STRATEGYBLOCK *sbPtr, AIMODULE *targetModule, int alien) |
| | 224 | { |
| | 225 | /* first check for entry point from current module */ |
| | 226 | { |
| | 227 | FARENTRYPOINT *targetEntryPoint = GetAIModuleEP(targetModule,(sbPtr->containingModule->m_aimodule)); |
| | 228 | |
| | 229 | if(NULL == targetEntryPoint) |
| | 230 | return NPCTM_NoEntryPoint; |
| | 231 | |
| | 232 | if (!alien) |
| | 233 | { |
| | 234 | if (targetEntryPoint->alien_only) |
| | 235 | return NPCTM_NoEntryPoint; |
| | 236 | } |
| | 237 | } |
| | 238 | |
| | 239 | MODULE *renderModule = *(targetModule->m_module_ptrs); |
| | 240 | |
| | 241 | MODULEDOORTYPE doorStatus = (ModuleIsADoor(renderModule)); |
| | 242 | |
| | 243 | switch(doorStatus) |
| | 244 | { |
| | 245 | case MDT_ProxDoor: |
| | 246 | { |
| | 247 | if(GetState(renderModule->m_sbptr)) |
| | 248 | return NPCTM_ProxDoorOpen; |
| | 249 | else |
| | 250 | return NPCTM_ProxDoorNotOpen; |
| | 251 | } |
| | 252 | break; |
| | 253 | case MDT_LiftDoor: |
| | 254 | { |
| | 255 | if(GetState(renderModule->m_sbptr)) |
| | 256 | return NPCTM_LiftDoorOpen; |
| | 257 | else |
| | 258 | return NPCTM_LiftDoorNotOpen; |
| | 259 | } |
| | 260 | break; |
| | 261 | case MDT_SecurityDoor: |
| | 262 | { |
| | 263 | if(GetState(renderModule->m_sbptr)) |
| | 264 | return NPCTM_SecurityDoorOpen; |
| | 265 | else |
| | 266 | return NPCTM_SecurityDoorNotOpen; |
| | 267 | } |
| | 268 | break; |
| | 269 | default: |
| | 270 | { |
| | 271 | assert(doorStatus==MDT_NotADoor); |
| | 272 | } |
| | 273 | } |
| | 274 | |
| | 275 | /* now check for lift */ |
| | 276 | if(sbPtr->type == I_BehaviourLift) |
| | 277 | return NPCTM_LiftTeleport; |
| | 278 | |
| | 279 | /* check for air duct */ |
| | 280 | if(renderModule->m_flags & MODULEFLAG_AIRDUCT) |
| | 281 | return NPCTM_AirDuct; |
| | 282 | |
| | 283 | /* at this point, we know it's a room (or stairs) ... */ |
| | 284 | return NPCTM_NormalRoom; |
| | 285 | } |
| | 286 | |
| | 287 | /* Patrick 1/7/97----------------------------------------- |
| | 288 | A suit of functions for general far NPC use which return a |
| | 289 | target module for hunting, wandering, and retreating |
| | 290 | ----------------------------------------------------------*/ |
| | 291 | |
| | 292 | AIMODULE *FarNPC_GetTargetAIModuleForHunt(STRATEGYBLOCK *sbPtr, int alien) |
| | 293 | { |
| | 294 | unsigned int highestSmell = 0; |
| | 295 | AIMODULE* targetModule = NULL; |
| | 296 | |
| | 297 | AIMODULE **AdjModuleRefPtr = sbPtr->containingModule->m_aimodule->m_link_ptrs; |
| | 298 | |
| | 299 | /* check that there is a list of adjacent modules, and that it is not |
| | 300 | empty (ie points to zero) */ |
| | 301 | |
| | 302 | if(AdjModuleRefPtr) |
| | 303 | { |
| | 304 | while(*AdjModuleRefPtr != 0) |
| | 305 | { |
| | 306 | /* get the index */ |
| | 307 | int AdjModuleIndex = (*AdjModuleRefPtr)->m_index; |
| | 308 | |
| | 309 | if (CheckAdjacencyValidity((*AdjModuleRefPtr), sbPtr->containingModule->m_aimodule, alien)) |
| | 310 | { |
| | 311 | /* if this adjacent module's smell value is higher than |
| | 312 | the current 'highest smell' record the new module as the |
| | 313 | target. */ |
| | 314 | |
| | 315 | if(PherPl_ReadBuf[AdjModuleIndex] > highestSmell) |
| | 316 | { |
| | 317 | highestSmell = PherPl_ReadBuf[AdjModuleIndex]; |
| | 318 | targetModule = *AdjModuleRefPtr; |
| | 319 | } |
| | 320 | } |
| | 321 | /* next adjacent module reference pointer */ |
| | 322 | AdjModuleRefPtr++; |
| | 323 | } |
| | 324 | } |
| | 325 | |
| | 326 | /* Consider my module being the target. */ |
| | 327 | if (PherPl_ReadBuf[sbPtr->containingModule->m_aimodule->m_index] > highestSmell) |
| | 328 | return sbPtr->containingModule->m_aimodule; |
| | 329 | else |
| | 330 | return targetModule; |
| | 331 | } |
| | 332 | |
| | 333 | /* Patrick 2/7/96: this function returns a module for wandering to */ |
| | 334 | AIMODULE *FarNPC_GetTargetAIModuleForWander(STRATEGYBLOCK *sbPtr, AIMODULE *exception, int alien) |
| | 335 | { |
| | 336 | AIMODULE* targetModule = NULL; |
| | 337 | int bestDirn = -100000; /* lower than the lowest */ |
| | 338 | VECTORCH npcDirn; |
| | 339 | |
| | 340 | /* some checks */ |
| | 341 | if(!sbPtr) |
| | 342 | return NULL; |
| | 343 | |
| | 344 | DYNAMICSBLOCK *dynPtr = sbPtr->DynPtr; |
| | 345 | |
| | 346 | if(!dynPtr) |
| | 347 | return NULL; |
| | 348 | |
| | 349 | /* get npc 2d directional vector */ |
| | 350 | npcDirn.vx = GetSin(dynPtr->OrientEuler.EulerY); |
| | 351 | npcDirn.vz = GetCos(dynPtr->OrientEuler.EulerY); |
| | 352 | npcDirn.vy = 0; |
| | 353 | Normalise(&npcDirn); |
| | 354 | |
| | 355 | /* init adjacent module pointer */ |
| | 356 | AIMODULE **AdjModuleRefPtr = sbPtr->containingModule->m_aimodule->m_link_ptrs; |
| | 357 | |
| | 358 | /* check that there is a list of adjacent modules, and that it is not |
| | 359 | empty (ie points to zero) */ |
| | 360 | |
| | 361 | if(AdjModuleRefPtr) |
| | 362 | { |
| | 363 | while(*AdjModuleRefPtr != 0) |
| | 364 | { |
| | 365 | AIMODULE *nextAdjModule = *AdjModuleRefPtr; |
| | 366 | VECTORCH moduleDirn; |
| | 367 | |
| | 368 | if (CheckAdjacencyValidity((*AdjModuleRefPtr), sbPtr->containingModule->m_aimodule,alien)) |
| | 369 | { |
| | 370 | moduleDirn.vx = nextAdjModule->m_world.vx - sbPtr->containingModule->m_world.vx; |
| | 371 | moduleDirn.vz = nextAdjModule->m_world.vz - sbPtr->containingModule->m_world.vz; |
| | 372 | moduleDirn.vy = 0; |
| | 373 | Normalise(&moduleDirn); |
| | 374 | |
| | 375 | int thisDirn = DotProduct(&npcDirn,&moduleDirn); |
| | 376 | |
| | 377 | if( (thisDirn > bestDirn) && (exception != nextAdjModule)) |
| | 378 | { |
| | 379 | targetModule = nextAdjModule; |
| | 380 | bestDirn = thisDirn; |
| | 381 | } |
| | 382 | } |
| | 383 | AdjModuleRefPtr++; |
| | 384 | } |
| | 385 | } |
| | 386 | |
| | 387 | return targetModule; |
| | 388 | } |