123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "T3D/turret/aiTurretShape.h"
- #include "console/console.h"
- #include "console/consoleTypes.h"
- #include "console/engineAPI.h"
- #include "core/stream/bitStream.h"
- #include "math/mMath.h"
- #include "math/mathIO.h"
- #include "math/mathUtils.h"
- #include "gfx/gfxDrawUtil.h"
- #include "ts/tsShapeInstance.h"
- #include "math/mRandom.h"
- static U32 sScanTypeMask = PlayerObjectType |
- VehicleObjectType;
- static U32 sAimTypeMask = TerrainObjectType |
- WaterObjectType |
- PlayerObjectType |
- StaticShapeObjectType |
- VehicleObjectType |
- ItemObjectType;
- //----------------------------------------------------------------------------
- AITurretShapeData::StateData::StateData()
- {
- name = 0;
- transition.rest[0] = transition.rest[1] = -1;
- transition.target[0] = transition.target[1] = -1;
- transition.activated[0] = transition.activated[1] = -1;
- transition.timeout = -1;
- waitForTimeout = true;
- timeoutValue = 0;
- fire = false;
- scan = false;
- script = 0;
-
- scaleAnimation = false;
- direction = false;
- sequence = -1;
- }
- static AITurretShapeData::StateData gDefaultStateData;
- //----------------------------------------------------------------------------
- IMPLEMENT_CO_DATABLOCK_V1(AITurretShapeData);
- ConsoleDocClass( AITurretShapeData,
- "@brief Defines properties for an AITurretShape object.\n\n"
- "@see AITurretShape\n"
- "@see TurretShapeData\n"
- "@ingroup gameObjects\n"
- );
- AITurretShapeData::AITurretShapeData()
- {
- maxScanHeading = 90;
- maxScanPitch = 90;
- maxScanDistance = 20;
- // Do a full scan every three ticks
- scanTickFrequency = 3;
- // Randomly add 0 to 1 ticks to the scan frequency as
- // chosen every scan period.
- scanTickFrequencyVariance = 1;
- trackLostTargetTime = 0;
- scanNode = -1;
- aimNode = -1;
- maxWeaponRange = 100;
- weaponLeadVelocity = 0;
- for (S32 i = 0; i < MaxStates; i++) {
- stateName[i] = 0;
- stateTransitionAtRest[i] = 0;
- stateTransitionNotAtRest[i] = 0;
- stateTransitionTarget[i] = 0;
- stateTransitionNoTarget[i] = 0;
- stateTransitionActivated[i] = 0;
- stateTransitionDeactivated[i] = 0;
- stateTransitionTimeout[i] = 0;
- stateWaitForTimeout[i] = true;
- stateTimeoutValue[i] = 0;
- stateFire[i] = false;
- stateScan[i] = false;
- stateScaleAnimation[i] = true;
- stateDirection[i] = true;
- stateSequence[i] = 0;
- stateScript[i] = 0;
- }
- isAnimated = false;
- statesLoaded = false;
- fireState = -1;
- }
- void AITurretShapeData::initPersistFields()
- {
- addField("maxScanHeading", TypeF32, Offset(maxScanHeading, AITurretShapeData),
- "@brief Maximum number of degrees to scan left and right.\n\n"
- "@note Maximum scan heading is 90 degrees.\n");
- addField("maxScanPitch", TypeF32, Offset(maxScanPitch, AITurretShapeData),
- "@brief Maximum number of degrees to scan up and down.\n\n"
- "@note Maximum scan pitch is 90 degrees.\n");
- addField("maxScanDistance", TypeF32, Offset(maxScanDistance, AITurretShapeData),
- "@brief Maximum distance to scan.\n\n"
- "When combined with maxScanHeading and maxScanPitch this forms a 3D scanning wedge used to initially "
- "locate a target.\n");
- addField("scanTickFrequency", TypeS32, Offset(scanTickFrequency, AITurretShapeData),
- "@brief How often should we perform a full scan when looking for a target.\n\n"
- "Expressed as the number of ticks between full scans, but no less than 1.\n");
- addField("scanTickFrequencyVariance", TypeS32, Offset(scanTickFrequencyVariance, AITurretShapeData),
- "@brief Random amount that should be added to the scan tick frequency each scan period.\n\n"
- "Expressed as the number of ticks to randomly add, but no less than zero.\n");
- addField("trackLostTargetTime", TypeF32, Offset(trackLostTargetTime, AITurretShapeData),
- "@brief How long after the turret has lost the target should it still track it.\n\n"
- "Expressed in seconds.\n");
- addField("maxWeaponRange", TypeF32, Offset(maxWeaponRange, AITurretShapeData),
- "@brief Maximum distance that the weapon will fire upon a target.\n\n");
- addField("weaponLeadVelocity", TypeF32, Offset(weaponLeadVelocity, AITurretShapeData),
- "@brief Velocity used to lead target.\n\n"
- "If value <= 0, don't lead target.\n");
- // State arrays
- addArray( "States", MaxStates );
- addField( "stateName", TypeCaseString, Offset(stateName, AITurretShapeData), MaxStates,
- "Name of this state." );
- addField( "stateTransitionOnAtRest", TypeString, Offset(stateTransitionAtRest, AITurretShapeData), MaxStates,
- "Name of the state to transition to when the turret is at rest (static).");
- addField( "stateTransitionOnNotAtRest", TypeString, Offset(stateTransitionNotAtRest, AITurretShapeData), MaxStates,
- "Name of the state to transition to when the turret is not at rest (not static).");
- addField( "stateTransitionOnTarget", TypeString, Offset(stateTransitionTarget, AITurretShapeData), MaxStates,
- "Name of the state to transition to when the turret gains a target." );
- addField( "stateTransitionOnNoTarget", TypeString, Offset(stateTransitionNoTarget, AITurretShapeData), MaxStates,
- "Name of the state to transition to when the turret loses a target." );
- addField( "stateTransitionOnActivated", TypeString, Offset(stateTransitionActivated, AITurretShapeData), MaxStates,
- "Name of the state to transition to when the turret goes from deactivated to activated.");
- addField( "stateTransitionOnDeactivated", TypeString, Offset(stateTransitionDeactivated, AITurretShapeData), MaxStates,
- "Name of the state to transition to when the turret goes from activated to deactivated");
- addField( "stateTransitionOnTimeout", TypeString, Offset(stateTransitionTimeout, AITurretShapeData), MaxStates,
- "Name of the state to transition to when we have been in this state "
- "for stateTimeoutValue seconds." );
- addField( "stateTimeoutValue", TypeF32, Offset(stateTimeoutValue, AITurretShapeData), MaxStates,
- "Time in seconds to wait before transitioning to stateTransitionOnTimeout." );
- addField( "stateWaitForTimeout", TypeBool, Offset(stateWaitForTimeout, AITurretShapeData), MaxStates,
- "If false, this state ignores stateTimeoutValue and transitions "
- "immediately if other transition conditions are met." );
- addField( "stateFire", TypeBool, Offset(stateFire, AITurretShapeData), MaxStates,
- "The first state with this set to true is the state entered by the "
- "client when it receives the 'fire' event." );
- addField( "stateScan", TypeBool, Offset(stateScan, AITurretShapeData), MaxStates,
- "Indicates the turret should perform a continuous scan looking for targets." );
- addField( "stateDirection", TypeBool, Offset(stateDirection, AITurretShapeData), MaxStates,
- "@brief Direction of the animation to play in this state.\n\n"
- "True is forward, false is backward." );
- addField( "stateSequence", TypeString, Offset(stateSequence, AITurretShapeData), MaxStates,
- "Name of the sequence to play on entry to this state." );
- addField( "stateScaleAnimation", TypeBool, Offset(stateScaleAnimation, AITurretShapeData), MaxStates,
- "If true, the timeScale of the stateSequence animation will be adjusted "
- "such that the sequence plays for stateTimeoutValue seconds. " );
- addField( "stateScript", TypeCaseString, Offset(stateScript, AITurretShapeData), MaxStates,
- "@brief Method to execute on entering this state.\n\n"
- "Scoped to AITurretShapeData.");
- endArray( "States" );
- Parent::initPersistFields();
- }
- bool AITurretShapeData::onAdd()
- {
- if (!Parent::onAdd())
- return false;
- // Copy state data from the scripting arrays into the
- // state structure array. If we have state data already,
- // we are on the client and need to leave it alone.
- for (U32 i = 0; i < MaxStates; i++) {
- StateData& s = state[i];
- if (statesLoaded == false) {
- s.name = stateName[i];
- s.transition.rest[0] = lookupState(stateTransitionNotAtRest[i]);
- s.transition.rest[1] = lookupState(stateTransitionAtRest[i]);
- s.transition.target[0] = lookupState(stateTransitionNoTarget[i]);
- s.transition.target[1] = lookupState(stateTransitionTarget[i]);
- s.transition.activated[0] = lookupState(stateTransitionDeactivated[i]);
- s.transition.activated[1] = lookupState(stateTransitionActivated[i]);
- s.transition.timeout = lookupState(stateTransitionTimeout[i]);
- s.waitForTimeout = stateWaitForTimeout[i];
- s.timeoutValue = stateTimeoutValue[i];
- s.fire = stateFire[i];
- s.scan = stateScan[i];
- s.scaleAnimation = stateScaleAnimation[i];
- s.direction = stateDirection[i];
- s.script = stateScript[i];
- // Resolved at load time
- s.sequence = -1;
- }
- // The first state marked as "fire" is the state entered on the
- // client when it recieves a fire event.
- if (s.fire && fireState == -1)
- fireState = i;
- }
- // Always preload images, this is needed to avoid problems with
- // resolving sequences before transmission to a client.
- return true;
- }
- bool AITurretShapeData::preload(bool server, String &errorStr)
- {
- if (!Parent::preload(server, errorStr))
- return false;
- // We have mShape at this point. Resolve nodes.
- scanNode = mShape->findNode("scanPoint");
- aimNode = mShape->findNode("aimPoint");
- if (scanNode == -1) scanNode = pitchNode;
- if (scanNode == -1) scanNode = headingNode;
- if (aimNode == -1) aimNode = pitchNode;
- if (aimNode == -1) aimNode = headingNode;
- // Resolve state sequence names & emitter nodes
- isAnimated = false;
- for (U32 j = 0; j < MaxStates; j++) {
- StateData& s = state[j];
- if (stateSequence[j] && stateSequence[j][0])
- s.sequence = mShape->findSequence(stateSequence[j]);
- if (s.sequence != -1)
- {
- // This state has an animation sequence
- isAnimated = true;
- }
- }
- return true;
- }
- void AITurretShapeData::packData(BitStream* stream)
- {
- Parent::packData(stream);
- stream->write(maxScanHeading);
- stream->write(maxScanPitch);
- stream->write(maxScanDistance);
- stream->write(maxWeaponRange);
- stream->write(weaponLeadVelocity);
- for (U32 i = 0; i < MaxStates; i++)
- if (stream->writeFlag(state[i].name && state[i].name[0])) {
- StateData& s = state[i];
- // States info not needed on the client:
- // s.scriptNames
- // Transitions are inc. one to account for -1 values
- stream->writeString(state[i].name);
- stream->writeInt(s.transition.rest[0]+1,NumStateBits);
- stream->writeInt(s.transition.rest[1]+1,NumStateBits);
- stream->writeInt(s.transition.target[0]+1,NumStateBits);
- stream->writeInt(s.transition.target[1]+1,NumStateBits);
- stream->writeInt(s.transition.activated[0]+1,NumStateBits);
- stream->writeInt(s.transition.activated[1]+1,NumStateBits);
- stream->writeInt(s.transition.timeout+1,NumStateBits);
- if(stream->writeFlag(s.timeoutValue != gDefaultStateData.timeoutValue))
- stream->write(s.timeoutValue);
- stream->writeFlag(s.waitForTimeout);
- stream->writeFlag(s.fire);
- stream->writeFlag(s.scan);
- stream->writeFlag(s.scaleAnimation);
- stream->writeFlag(s.direction);
- if(stream->writeFlag(s.sequence != gDefaultStateData.sequence))
- stream->writeSignedInt(s.sequence, 16);
- }
- }
- void AITurretShapeData::unpackData(BitStream* stream)
- {
- Parent::unpackData(stream);
- stream->read(&maxScanHeading);
- stream->read(&maxScanPitch);
- stream->read(&maxScanDistance);
- stream->read(&maxWeaponRange);
- stream->read(&weaponLeadVelocity);
- for (U32 i = 0; i < MaxStates; i++) {
- if (stream->readFlag()) {
- StateData& s = state[i];
- // States info not needed on the client:
- // s.scriptNames
- // Transitions are dec. one to restore -1 values
- s.name = stream->readSTString();
- s.transition.rest[0] = stream->readInt(NumStateBits) - 1;
- s.transition.rest[1] = stream->readInt(NumStateBits) - 1;
- s.transition.target[0] = stream->readInt(NumStateBits) - 1;
- s.transition.target[1] = stream->readInt(NumStateBits) - 1;
- s.transition.activated[0] = stream->readInt(NumStateBits) - 1;
- s.transition.activated[1] = stream->readInt(NumStateBits) - 1;
- s.transition.timeout = stream->readInt(NumStateBits) - 1;
- if(stream->readFlag())
- stream->read(&s.timeoutValue);
- else
- s.timeoutValue = gDefaultStateData.timeoutValue;
- s.waitForTimeout = stream->readFlag();
- s.fire = stream->readFlag();
- s.scan = stream->readFlag();
- s.scaleAnimation = stream->readFlag();
- s.direction = stream->readFlag();
- if(stream->readFlag())
- s.sequence = stream->readSignedInt(16);
- }
- }
- statesLoaded = true;
- }
- S32 AITurretShapeData::lookupState(const char* name)
- {
- if (!name || !name[0])
- return -1;
- for (U32 i = 0; i < MaxStates; i++)
- if (stateName[i] && !dStricmp(name,stateName[i]))
- return i;
- Con::errorf(ConsoleLogEntry::General,"AITurretShapeData:: Could not resolve state \"%s\" for image \"%s\"",name,getName());
- return 0;
- }
- //----------------------------------------------------------------------------
- // Used to build potential target list
- static void _scanCallback( SceneObject* object, void* data )
- {
- AITurretShape* turret = (AITurretShape*)data;
- ShapeBase* shape = dynamic_cast<ShapeBase*>(object);
- if (shape && shape->getDamageState() == ShapeBase::Enabled)
- {
- Point3F targetPos = shape->getBoxCenter();
- // Put target position into the scan node's space
- turret->mScanWorkspaceScanWorldMat.mulP(targetPos);
- // Is the target within scanning distance
- if (targetPos.lenSquared() > turret->getMaxScanDistanceSquared())
- return;
- // Make sure the target is in front and within the maximum
- // heading range
- Point2F targetXY(targetPos.x, targetPos.y);
- targetXY.normalizeSafe();
- F32 headingDot = mDot(Point2F(0, 1), targetXY);
- F32 heading = mAcos(headingDot);
- if (headingDot < 0 || heading > turret->getMaxScanHeading())
- return;
- // Make sure the target is in front and within the maximum
- // pitch range
- Point2F targetZY(targetPos.z, targetPos.y);
- targetZY.normalizeSafe();
- F32 pitchDot = mDot(Point2F(0, 1), targetZY);
- F32 pitch = mAcos(pitchDot);
- if (pitchDot < 0 || pitch > turret->getMaxScanPitch())
- return;
- turret->addPotentialTarget(shape);
- }
- }
- // Used to sort potential target list based on distance
- static Point3F comparePoint;
- static S32 QSORT_CALLBACK _sortCallback(const void* a,const void* b)
- {
- const ShapeBase* s1 = (*reinterpret_cast<const ShapeBase* const*>(a));
- const ShapeBase* s2 = (*reinterpret_cast<const ShapeBase* const*>(b));
- F32 s1Len = (s1->getPosition() - comparePoint).lenSquared();
- F32 s2Len = (s2->getPosition() - comparePoint).lenSquared();
- return s1Len - s2Len;
- }
- IMPLEMENT_CO_NETOBJECT_V1(AITurretShape);
- ConsoleDocClass( AITurretShape,
- "@ingroup gameObjects"
- );
- AITurretShape::AITurretShape()
- {
- mTypeMask |= VehicleObjectType | DynamicShapeObjectType;
- mDataBlock = 0;
- mScanHeading = 0;
- mScanPitch = 0;
- mScanDistance = 0;
- mScanDistanceSquared = 0;
- mScanBox = Box3F::Zero;
- mScanTickFrequency = 1;
- mScanTickFrequencyVariance = 0;
- mTicksToNextScan = mScanTickFrequency;
- mWeaponRangeSquared = 0;
- mWeaponLeadVelocitySquared = 0;
- mScanForTargets = false;
- mTrackTarget = false;
- mState = NULL;
- mStateDelayTime = 0;
- mStateActive = true;
- mStateAnimThread = NULL;
- // For the TurretShape class
- mSubclassTurretShapeHandlesScene = true;
- }
- AITurretShape::~AITurretShape()
- {
- _cleanupPotentialTargets();
- }
- //----------------------------------------------------------------------------
- void AITurretShape::initPersistFields()
- {
- Parent::initPersistFields();
- }
- bool AITurretShape::onAdd()
- {
- if( !Parent::onAdd() )
- return false;
- // Add this object to the scene
- addToScene();
- _setScanBox();
- if (isServerObject())
- _initState();
- if (isServerObject())
- scriptOnAdd();
- return true;
- }
- void AITurretShape::onRemove()
- {
- Parent::onRemove();
- scriptOnRemove();
- mIgnoreObjects.clear();
- // Remove this object from the scene
- removeFromScene();
- }
- bool AITurretShape::onNewDataBlock(GameBaseData* dptr, bool reload)
- {
- mDataBlock = dynamic_cast<AITurretShapeData*>(dptr);
- if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
- return false;
- mScanHeading = mDegToRad(mDataBlock->maxScanHeading);
- if (mIsZero(mScanHeading))
- mScanHeading = M_PI_F;
- mScanPitch = mDegToRad(mDataBlock->maxScanPitch);
- if (mIsZero(mScanPitch))
- mScanPitch = M_PI_F;
- mScanDistance = mDataBlock->maxScanDistance;
- mScanDistanceSquared = mScanDistance * mScanDistance;
- mWeaponRangeSquared = mDataBlock->maxWeaponRange * mDataBlock->maxWeaponRange;
- mWeaponLeadVelocitySquared = (mDataBlock->weaponLeadVelocity > 0) ? (mDataBlock->weaponLeadVelocity * mDataBlock->weaponLeadVelocity) : 0;
- // The scan box is built such that the scanning origin is at (0,0,0) and the scanning distance
- // is out along the y axis. When this is transformed to the turret's location is provides a
- // scanning volume in front of the turret.
- F32 scanX = mScanDistance*mSin(mScanHeading);
- F32 scanY = mScanDistance;
- F32 scanZ = mScanDistance*mSin(mScanPitch);
- mScanBox.set(-scanX, 0, -scanZ, scanX, scanY, scanZ);
- mScanTickFrequency = mDataBlock->scanTickFrequency;
- if (mScanTickFrequency < 1)
- mScanTickFrequency = 1;
- mScanTickFrequencyVariance = mDataBlock->scanTickFrequencyVariance;
- if (mScanTickFrequencyVariance < 0)
- mScanTickFrequencyVariance = 0;
- mTicksToNextScan = mScanTickFrequency;
- // For states
- mStateAnimThread = 0;
- if (mDataBlock->isAnimated)
- {
- mStateAnimThread = mShapeInstance->addThread();
- mShapeInstance->setTimeScale(mStateAnimThread,0);
- }
- scriptOnNewDataBlock();
- return true;
- }
- //----------------------------------------------------------------------------
- void AITurretShape::addToIgnoreList(ShapeBase* obj)
- {
- mIgnoreObjects.addObject(obj);
- }
- void AITurretShape::removeFromIgnoreList(ShapeBase* obj)
- {
- mIgnoreObjects.removeObject(obj);
- }
- void AITurretShape::clearIgnoreList()
- {
- mIgnoreObjects.clear();
- }
- S32 AITurretShape::ignoreListCount()
- {
- return mIgnoreObjects.size();
- }
- SimObject* AITurretShape::getIgnoreListObject(S32 index)
- {
- return mIgnoreObjects.at(index);
- }
- //----------------------------------------------------------------------------
- void AITurretShape::_initState()
- {
- // Set the turret to its starting state.
- setTurretState(0, true);
- }
- void AITurretShape::setTurretStateName(const char* newState, bool force)
- {
- S32 stateVal = mDataBlock->lookupState(newState);
- if (stateVal >= 0)
- {
- setTurretState(stateVal, force);
- }
- }
- void AITurretShape::setTurretState(U32 newState, bool force)
- {
- setMaskBits(TurretStateMask);
- // If going back into the same state, just reset the timer
- // and invoke the script callback
- if (!force && mState == &mDataBlock->state[newState])
- {
- mStateDelayTime = mState->timeoutValue;
- if (mState->script && !isGhost())
- _scriptCallback(mState->script);
- return;
- }
- mState = &mDataBlock->state[newState];
- // Reset cyclic sequences back to the first frame to turn it off
- // (the first key frame should be it's off state).
- if (mStateAnimThread && mStateAnimThread->getSequence()->isCyclic())
- {
- mShapeInstance->setPos(mStateAnimThread,0);
- mShapeInstance->setTimeScale(mStateAnimThread,0);
- }
- AITurretShapeData::StateData& stateData = *mState;
- // Check for immediate transitions
- S32 ns;
- if ((ns = stateData.transition.rest[mAtRest]) != -1)
- {
- setTurretState(ns);
- return;
- }
- if ((ns = stateData.transition.target[mTarget.isValid()]) != -1)
- {
- setTurretState(ns);
- return;
- }
- if ((ns = stateData.transition.activated[mStateActive]) != -1)
- {
- setTurretState(ns);
- return;
- }
- //
- // Initialize the new state...
- //
- mStateDelayTime = stateData.timeoutValue;
- // Play animation
- if (mStateAnimThread && stateData.sequence != -1)
- {
- mShapeInstance->setSequence(mStateAnimThread,stateData.sequence, stateData.direction ? 0.0f : 1.0f);
- F32 timeScale = (stateData.scaleAnimation && stateData.timeoutValue) ?
- mShapeInstance->getDuration(mStateAnimThread) / stateData.timeoutValue : 1.0f;
- mShapeInstance->setTimeScale(mStateAnimThread, stateData.direction ? timeScale : -timeScale);
- }
- // Script callback on server
- if (stateData.script && stateData.script[0] && !isGhost())
- _scriptCallback(stateData.script);
- // If there is a zero timeout, and a timeout transition, then
- // go ahead and transition imediately.
- if (!mStateDelayTime)
- {
- if ((ns = stateData.transition.timeout) != -1)
- {
- setTurretState(ns);
- return;
- }
- }
- }
- void AITurretShape::_updateTurretState(F32 dt)
- {
- AITurretShapeData::StateData& stateData = *mState;
- mStateDelayTime -= dt;
- // Check for transitions. On some states we must wait for the
- // full timeout value before moving on.
- if (mStateDelayTime <= 0 || !stateData.waitForTimeout)
- {
- S32 ns;
- if ((ns = stateData.transition.rest[mAtRest]) != -1)
- setTurretState(ns);
- else if ((ns = stateData.transition.target[mTarget.isValid()]) != -1)
- setTurretState(ns);
- else if ((ns = stateData.transition.activated[mStateActive]) != -1)
- setTurretState(ns);
- else if (mStateDelayTime <= 0 && (ns = stateData.transition.timeout) != -1)
- setTurretState(ns);
- }
- }
- void AITurretShape::_scriptCallback(const char* function)
- {
- Con::executef( mDataBlock, function, getIdString() );
- }
- //----------------------------------------------------------------------------
- void AITurretShape::_cleanupPotentialTargets()
- {
- mPotentialTargets.clear();
- }
- void AITurretShape::_performScan()
- {
- // Only on server
- if (isClientObject())
- return;
- // Are we ready for a scan?
- --mTicksToNextScan;
- if (mTicksToNextScan > 0)
- return;
- _cleanupPotentialTargets();
- _setScanBox();
- // Set up for the scan
- getScanTransform(mScanWorkspaceScanMat);
- mScanWorkspaceScanWorldMat = mScanWorkspaceScanMat;
- mScanWorkspaceScanWorldMat.affineInverse();
- disableCollision();
- for ( SimSetIterator iter(&mIgnoreObjects); *iter; ++iter )
- {
- ShapeBase* obj = static_cast<ShapeBase*>( *iter );
- obj->disableCollision();
- }
- gServerContainer.findObjects( mTransformedScanBox, sScanTypeMask, _scanCallback, (void*)this );
- for ( SimSetIterator iter(&mIgnoreObjects); *iter; ++iter )
- {
- ShapeBase* obj = static_cast<ShapeBase*>( *iter );
- obj->enableCollision();
- }
- enableCollision();
- if (mPotentialTargets.size() == 0)
- {
- // No targets in range. Clear out our current target, if necessary.
- _lostTarget();
- }
- else
- {
- // Sort the targets
- comparePoint = getPosition();
- dQsort(mPotentialTargets.address(),mPotentialTargets.size(),sizeof(SimObjectList::value_type),_sortCallback);
- // Go through the targets in order to find one that is not blocked from view
- Point3F start;
- mScanWorkspaceScanMat.getColumn(3, &start);
- S32 index = 0;
- bool los = false;
- disableCollision();
- for (index=0; index < mPotentialTargets.size(); ++index)
- {
- ShapeBase* shape = (ShapeBase*)mPotentialTargets[index];
- Point3F sightPoint;
- los = _testTargetLineOfSight(start, shape, sightPoint);
- // Check if we have a clear line of sight
- if (los)
- break;
- }
- enableCollision();
- // If we found a valid, visible target (no hits between here and there), latch on to it
- if (los)
- {
- _gainedTarget((ShapeBase*)mPotentialTargets[index]);
- }
- }
- // Prepare for next scan period
- mTicksToNextScan = mScanTickFrequency;
- if (mScanTickFrequencyVariance > 0)
- {
- mTicksToNextScan += gRandGen.randI(0, mScanTickFrequencyVariance);
- }
- }
- void AITurretShape::addPotentialTarget(ShapeBase* shape)
- {
- mPotentialTargets.push_back(shape);
- }
- void AITurretShape::_lostTarget()
- {
- mTarget.target = NULL;
- }
- void AITurretShape::_gainedTarget(ShapeBase* target)
- {
- mTarget.target = target;
- if (target)
- {
- mTarget.hadValidTarget = true;
- }
- }
- void AITurretShape::_trackTarget(F32 dt)
- {
- // Only on server
- if (isClientObject())
- return;
- // We can only track a target if we have one
- if (!mTarget.isValid())
- return;
- Point3F targetPos = mTarget.target->getBoxCenter();
- // Can we see the target?
- MatrixF aimMat;
- getAimTransform(aimMat);
- Point3F start;
- aimMat.getColumn(3, &start);
- RayInfo ri;
- Point3F sightPoint;
- disableCollision();
- bool los = _testTargetLineOfSight(start, mTarget.target, sightPoint);
- enableCollision();
- if (!los)
- {
- // Target is blocked. Should we try to track from its last
- // known position and velocity?
- SimTime curTime = Sim::getCurrentTime();
- if ( (curTime - mTarget.lastSightTime) > (mDataBlock->trackLostTargetTime * 1000.0f) )
- {
- // Time's up. Stop tracking.
- _cleanupTargetAndTurret();
- return;
- }
-
- // Use last known information to attempt to
- // continue to track target for a while.
- targetPos = mTarget.lastPos + mTarget.lastVel * F32(curTime - mTarget.lastSightTime) / 1000.0f;
- }
- else
- {
- // Target is visible
- // We only track targets that are alive
- if (mTarget.target->getDamageState() != Enabled)
- {
- // We can't track any more
- _cleanupTargetAndTurret();
- return;
- }
- targetPos = sightPoint;
- // Store latest target info
- mTarget.lastPos = targetPos;
- mTarget.lastVel = mTarget.target->getVelocity();
- mTarget.lastSightTime = Sim::getCurrentTime();
- }
- // Calculate angles to face the target, specifically the part that we can see
- VectorF toTarget;
- MatrixF mat;
- S32 node = mDataBlock->aimNode;
- if (node != -1)
- {
- // Get the current position of our node
- MatrixF* nodeTrans = &mShapeInstance->mNodeTransforms[node];
- Point3F currentPos;
- nodeTrans->getColumn(3, ¤tPos);
- // Turn this into a matrix we can use to put the target
- // position into our space.
- MatrixF nodeMat(true);
- nodeMat.setColumn(3, currentPos);
- mat.mul(mObjToWorld, nodeMat);
- mat.affineInverse();
- }
- else
- {
- mat = mWorldToObj;
- }
- mat.mulP(targetPos, &toTarget);
- // lead the target
- F32 timeToTargetSquared = (mWeaponLeadVelocitySquared > 0) ? toTarget.lenSquared() / mWeaponLeadVelocitySquared : 0;
- if (timeToTargetSquared > 1.0)
- {
- targetPos = targetPos + (mTarget.lastVel * mSqrt(timeToTargetSquared));
- mat.mulP(targetPos, &toTarget);
- }
- F32 yaw, pitch;
- MathUtils::getAnglesFromVector(toTarget, yaw, pitch);
- if (yaw > M_PI_F)
- yaw = yaw - M_2PI_F;
- //if (pitch > M_PI_F)
- // pitch = -(pitch - M_2PI_F);
- Point3F rot(-pitch, 0.0f, yaw);
- // If we have a rotation rate make sure we follow it
- if (mHeadingRate > 0)
- {
- F32 rate = mHeadingRate * dt;
- F32 rateCheck = mFabs(rot.z - mRot.z);
- if (rateCheck > rate)
- {
- // This will clamp the new value to the rate regardless if it
- // is increasing or decreasing.
- rot.z = mClampF(rot.z, mRot.z-rate, mRot.z+rate);
- }
- }
- if (mPitchRate > 0)
- {
- F32 rate = mPitchRate * dt;
- F32 rateCheck = mFabs(rot.x - mRot.x);
- if (rateCheck > rate)
- {
- // This will clamp the new value to the rate regardless if it
- // is increasing or decreasing.
- rot.x = mClampF(rot.x, mRot.x-rate, mRot.x+rate);
- }
- }
- // Test if the rotation to the target is outside of our limits
- if (_outsideLimits(rot))
- {
- // We can't track any more
- _cleanupTargetAndTurret();
- return;
- }
- // Test if the target is out of weapons range
- if (toTarget.lenSquared() > mWeaponRangeSquared)
- {
- // We can't track any more
- _cleanupTargetAndTurret();
- return;
- }
- mRot = rot;
- _setRotation( mRot );
- setMaskBits(TurretUpdateMask);
- }
- void AITurretShape::_cleanupTargetAndTurret()
- {
- _lostTarget();
- resetTarget();
- }
- bool AITurretShape::_testTargetLineOfSight(Point3F& aimPoint, ShapeBase* target, Point3F& sightPoint)
- {
- Point3F targetCenter = target->getBoxCenter();
- RayInfo ri;
- bool hit = false;
- target->disableCollision();
-
- // First check for a clear line of sight to the target's center
- Point3F testPoint = targetCenter;
- hit = gServerContainer.castRay(aimPoint, testPoint, sAimTypeMask, &ri);
- if (hit)
- {
- // No clear line of sight to center, so try to the target's right. Players holding
- // a gun in their right hand will tend to stick their right shoulder out first if
- // they're peering around some cover to shoot, like a wall.
- Box3F targetBounds = target->getObjBox();
- F32 radius = targetBounds.len_x() > targetBounds.len_y() ? targetBounds.len_x() : targetBounds.len_y();
- radius *= 0.5;
- VectorF toTurret = aimPoint - targetCenter;
- toTurret.normalizeSafe();
- VectorF toTurretRight = mCross(toTurret, Point3F::UnitZ);
- testPoint = targetCenter + toTurretRight * radius;
- hit = gServerContainer.castRay(aimPoint, testPoint, sAimTypeMask, &ri);
- if (hit)
- {
- // No clear line of sight to right, so try the target's left
- VectorF toTurretLeft = toTurretRight * -1.0f;
- testPoint = targetCenter + toTurretLeft * radius;
- hit = gServerContainer.castRay(aimPoint, testPoint, sAimTypeMask, &ri);
- }
- if (hit)
- {
- // No clear line of sight to left, so try the target's top
- testPoint = targetCenter;
- testPoint.z += targetBounds.len_z() * 0.5f;
- hit = gServerContainer.castRay(aimPoint, testPoint, sAimTypeMask, &ri);
- }
- if (hit)
- {
- // No clear line of sight to top, so try the target's bottom
- testPoint = targetCenter;
- testPoint.z -= targetBounds.len_z() * 0.5f;
- hit = gServerContainer.castRay(aimPoint, testPoint, sAimTypeMask, &ri);
- }
- }
-
- target->enableCollision();
- if (!hit)
- {
- // Line of sight point is that last one we tested
- sightPoint = testPoint;
- }
- return !hit;
- }
- //----------------------------------------------------------------------------
- void AITurretShape::setAllGunsFiring(bool fire)
- {
- setImageTriggerState(0,fire);
- setImageTriggerState(1,fire);
- setImageTriggerState(2,fire);
- setImageTriggerState(3,fire);
- }
- void AITurretShape::setGunSlotFiring(S32 slot, bool fire)
- {
- if (slot < 0 || slot > 3)
- return;
- setImageTriggerState(slot, fire);
- }
- //----------------------------------------------------------------------------
- void AITurretShape::setTransform(const MatrixF& mat)
- {
- Parent::setTransform(mat);
- // Set the scanning box
- _setScanBox();
- }
- void AITurretShape::recenterTurret()
- {
- mRot.set(0,0,0);
- _setRotation( mRot );
- setMaskBits(TurretUpdateMask);
- }
- void AITurretShape::_setScanBox()
- {
- mTransformedScanBox = mScanBox;
- MatrixF mat;
- getScanTransform(mat);
- mat.mul(mTransformedScanBox);
- }
- void AITurretShape::getScanTransform(MatrixF& mat)
- {
- if (mDataBlock && mDataBlock->scanNode != -1)
- {
- if (getNodeTransform(mDataBlock->scanNode, mat))
- {
- return;
- }
- }
- mat = mObjToWorld;
- }
- void AITurretShape::getAimTransform(MatrixF& mat)
- {
- if (mDataBlock && mDataBlock->aimNode != -1)
- {
- if (getNodeTransform(mDataBlock->aimNode, mat))
- {
- return;
- }
- }
- mat = mObjToWorld;
- }
- //----------------------------------------------------------------------------
- void AITurretShape::processTick(const Move* move)
- {
- Parent::processTick(move);
- if (isServerObject() && mDamageState == Enabled)
- {
- _updateTurretState(TickSec);
- if (mScanForTargets)
- {
- // Perform a scan for targets
- _performScan();
- // If we found one, turn off the scan
- if (mTarget.isValid())
- {
- mScanForTargets = false;
- }
- }
- if (mTrackTarget)
- {
- _trackTarget(TickSec);
- // If the target is lost, no longer track it
- if (!mTarget.isValid())
- {
- mTrackTarget = false;
- }
- }
- }
- }
- void AITurretShape::advanceTime(F32 dt)
- {
- // If there were any ShapeBase script threads that
- // have played, then we need to update all code
- // controlled nodes. This is done before the Parent
- // call as script threads may play and be destroyed
- // before our code is called.
- bool updateNodes = false;
- for (U32 i = 0; i < MaxScriptThreads; i++)
- {
- Thread& st = mScriptThread[i];
- if (st.thread)
- {
- updateNodes = true;
- break;
- }
- }
- Parent::advanceTime(dt);
- // Update any state thread
- AITurretShapeData::StateData& stateData = *mState;
- if (mStateAnimThread && stateData.sequence != -1)
- {
- mShapeInstance->advanceTime(dt,mStateAnimThread);
- updateNodes = true;
- }
- if (updateNodes)
- {
- // Update all code controlled nodes
- _updateNodes(mRot);
- }
- }
- //----------------------------------------------------------------------------
- U32 AITurretShape::packUpdate(NetConnection *connection, U32 mask, BitStream *bstream)
- {
- U32 retMask = Parent::packUpdate(connection,mask,bstream);
- // Indicate that the transform has changed to update the scan box
- bstream->writeFlag(mask & (PositionMask | ExtendedInfoMask));
- // Handle any state changes that need to be passed along
- if (bstream->writeFlag(mask & TurretStateMask))
- {
- bstream->write(mDataBlock->lookupState(mState->name));
- }
- return retMask;
- }
- void AITurretShape::unpackUpdate(NetConnection *connection, BitStream *bstream)
- {
- Parent::unpackUpdate(connection,bstream);
- // Transform has changed
- if (bstream->readFlag())
- {
- _setScanBox();
- }
- //TurretStateMask
- if (bstream->readFlag())
- {
- S32 newstate;
- bstream->read( &newstate );
- setTurretState(newstate);
- }
- }
- //----------------------------------------------------------------------------
- void AITurretShape::prepBatchRender( SceneRenderState *state, S32 mountedImageIndex )
- {
- Parent::prepBatchRender( state, mountedImageIndex );
- if ( !gShowBoundingBox )
- return;
- ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
- ri->renderDelegate.bind( this, &AITurretShape::_renderScanner );
- ri->type = RenderPassManager::RIT_Editor;
- state->getRenderPass()->addInst( ri );
- }
- void AITurretShape::_renderScanner( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
- {
- GFXStateBlockDesc desc;
- desc.setBlend(false, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
- desc.setZReadWrite(false,true);
- desc.fillMode = GFXFillWireframe;
- // Render the scan box
- GFX->getDrawUtil()->drawCube(desc, mTransformedScanBox.getExtents(), mTransformedScanBox.getCenter(), ColorI(255, 0, 0));
- // Render a line from the scan node to the max scan distance
- MatrixF nodeMat;
- if (getNodeTransform(mDataBlock->scanNode, nodeMat))
- {
- Point3F start;
- nodeMat.getColumn(3, &start);
- Point3F end(0.0f, mScanDistance, 0.0f);
- nodeMat.mulP(end);
- GFX->getDrawUtil()->drawLine(start, end, ColorI(255, 255, 0));
- }
- }
- //----------------------------------------------------------------------------
- DefineEngineMethod( AITurretShape, addToIgnoreList, void, (ShapeBase* obj),,
- "@brief Adds object to the turret's ignore list.\n\n"
- "All objects in this list will be ignored by the turret's targeting.\n"
- "@param obj The ShapeBase object to ignore.\n")
- {
- object->addToIgnoreList(obj);
- }
- DefineEngineMethod( AITurretShape, removeFromIgnoreList, void, (ShapeBase* obj),,
- "@brief Removes object from the turret's ignore list.\n\n"
- "All objects in this list will be ignored by the turret's targeting.\n"
- "@param obj The ShapeBase object to once again allow for targeting.\n")
- {
- object->removeFromIgnoreList(obj);
- }
- DefineEngineMethod( AITurretShape, clearIgnoreList, void, (),,
- "@brief Removes all objects from the turret's ignore list.\n\n"
- "All objects in this list will be ignored by the turret's targeting.\n")
- {
- object->clearIgnoreList();
- }
- DefineEngineMethod( AITurretShape, ignoreListCount, S32, (),,
- "@brief Returns the number of objects in the turrets ignore list.\n\n"
- "All objects in this list will be ignored by the turret's targeting.\n")
- {
- return object->ignoreListCount();
- }
- DefineEngineMethod( AITurretShape, getIgnoreListObject, SimObject*, (S32 index),,
- "@brief Returns the object in the ignore list at index.\n\n"
- "All objects in this list will be ignored by the turret's targeting.\n"
- "@param index The index of the object in the ignore list being retrieved.\n")
- {
- return object->getIgnoreListObject(index);
- }
- DefineEngineMethod( AITurretShape, setTurretState, void, (const char* newState, bool force), (false),
- "@brief Set the turret's current state.\n\n"
- "Normally the turret's state comes from updating the state machine but this method "
- "allows you to override this and jump to the requested state immediately.\n"
- "@param newState The name of the new state.\n"
- "@param force Is true then force the full processing of the new state even if it is the "
- "same as the current state. If false then only the time out value is reset and the state's "
- "script method is called, if any.\n")
- {
- object->setTurretStateName(newState, force);
- }
- DefineEngineMethod( AITurretShape, activateTurret, void, ( ),,
- "@brief Activate a turret from a deactive state.\n\n")
- {
- object->activateTurret();
- }
- DefineEngineMethod( AITurretShape, deactivateTurret, void, ( ),,
- "@brief Deactivate a turret from an active state.\n\n")
- {
- object->deactivateTurret();
- }
- DefineEngineMethod( AITurretShape, startScanForTargets, void, ( ),,
- "@brief Begin scanning for a target.\n\n")
- {
- object->startScanForTargets();
- }
- DefineEngineMethod( AITurretShape, stopScanForTargets, void, ( ),,
- "@brief Stop scanning for targets.\n\n"
- "@note Only impacts the scanning for new targets. Does not effect a turret's current "
- "target lock.\n")
- {
- object->stopScanForTargets();
- }
- DefineEngineMethod( AITurretShape, startTrackingTarget, void, ( ),,
- "@brief Have the turret track the current target.\n\n")
- {
- object->startTrackingTarget();
- }
- DefineEngineMethod( AITurretShape, stopTrackingTarget, void, ( ),,
- "@brief Stop the turret from tracking the current target.\n\n")
- {
- object->stopTrackingTarget();
- }
- DefineEngineMethod( AITurretShape, hasTarget, bool, (),,
- "@brief Indicates if the turret has a target.\n\n"
- "@returns True if the turret has a target.\n")
- {
- return object->hasTarget();
- }
- DefineEngineMethod( AITurretShape, getTarget, SimObject*, (),,
- "@brief Get the turret's current target.\n\n"
- "@returns The object that is the target's current target, or 0 if no target.\n")
- {
- return object->getTarget();
- }
- DefineEngineMethod( AITurretShape, resetTarget, void, ( ),,
- "@brief Resets the turret's target tracking.\n\n"
- "Only resets the internal target tracking. Does not modify the turret's facing.\n")
- {
- object->resetTarget();
- }
- DefineEngineMethod( AITurretShape, setWeaponLeadVelocity, void, (F32 velocity),,
- "@brief Set the turret's projectile velocity to help lead the target.\n\n"
- "This value normally comes from AITurretShapeData::weaponLeadVelocity but this method "
- "allows you to override the datablock value. This can be useful if the turret changes "
- "ammunition, uses a different weapon than the default, is damaged, etc.\n"
- "@note Setting this to 0 will disable target leading.\n")
- {
- object->setWeaponLeadVelocity(velocity);
- }
- DefineEngineMethod( AITurretShape, getWeaponLeadVelocity, F32, (),,
- "@brief Get the turret's defined projectile velocity that helps with target leading.\n\n"
- "@returns The defined weapon projectile speed, or 0 if leading is disabled.\n")
- {
- return object->getWeaponLeadVelocity();
- }
- DefineEngineMethod( AITurretShape, setAllGunsFiring, void, (bool fire),,
- "@brief Set the firing state of the turret's guns.\n\n"
- "@param fire Set to true to activate all guns. False to deactivate them.\n")
- {
- object->setAllGunsFiring(fire);
- }
- DefineEngineMethod( AITurretShape, setGunSlotFiring, void, (S32 slot, bool fire),,
- "@brief Set the firing state of the given gun slot.\n\n"
- "@param slot The gun to modify. Valid range is 0-3 that corresponds to the weapon mount point.\n"
- "@param fire Set to true to activate the gun. False to deactivate it.\n")
- {
- object->setGunSlotFiring(slot, fire);
- }
- DefineEngineMethod( AITurretShape, recenterTurret, void, ( ),,
- "@brief Recenter the turret's weapon.\n\n")
- {
- object->recenterTurret();
- }
|