aiTurretShape.h
Engine/source/T3D/turret/aiTurretShape.h
Classes:
class
class
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _AITURRETSHAPE_H_ 25#define _AITURRETSHAPE_H_ 26 27#ifndef _TURRETSHAPE_H_ 28 #include "T3D/turret/turretShape.h" 29#endif 30 31 32//---------------------------------------------------------------------------- 33 34class AITurretShapeData: public TurretShapeData { 35 36 typedef TurretShapeData Parent; 37 38public: 39 enum Constants { 40 MaxStates = 31, ///< We get one less than state bits because of 41 /// the way data is packed. 42 NumStateBits = 5, 43 }; 44 45 struct StateData { 46 StateData(); 47 const char* name; ///< State name 48 49 /// @name Transition states 50 /// 51 /// @{ 52 53 /// 54 struct Transition { 55 S32 rest[2]; ///< NotAtRest/AtRest (NotStatic/Static) 56 S32 target[2]; ///< NoTarget/Target 57 S32 activated[2]; ///< Deactivated/Activated 58 S32 timeout; ///< Transition after delay 59 } transition; 60 61 /// @} 62 63 /// @name State attributes 64 /// @{ 65 66 bool fire; ///< Can only have one fire state 67 bool scan; ///< Perform a continuous scan looking for targets 68 bool scaleAnimation; ///< Scale animation to fit the state timeout 69 bool direction; ///< Animation direction 70 bool waitForTimeout; ///< Require the timeout to pass before advancing to the next 71 /// state. 72 F32 timeoutValue; ///< A timeout value; the effect of this value is determined 73 /// by the flags scaleAnimation and waitForTimeout 74 S32 sequence; ///< Main thread sequence ID. 75 /// 76 /// 77 const char* script; ///< Function on datablock to call when we enter this state; passed the id of 78 /// the imageSlot. 79 80 /// @} 81 }; 82 83 /// @name State Data 84 /// Individual state data used to initialize struct array 85 /// @{ 86 const char* stateName [MaxStates]; 87 88 const char* stateTransitionAtRest [MaxStates]; 89 const char* stateTransitionNotAtRest [MaxStates]; 90 const char* stateTransitionTarget [MaxStates]; 91 const char* stateTransitionNoTarget [MaxStates]; 92 const char* stateTransitionActivated [MaxStates]; 93 const char* stateTransitionDeactivated [MaxStates]; 94 const char* stateTransitionTimeout [MaxStates]; 95 F32 stateTimeoutValue [MaxStates]; 96 bool stateWaitForTimeout [MaxStates]; 97 98 bool stateFire [MaxStates]; 99 bool stateScan [MaxStates]; 100 101 bool stateScaleAnimation [MaxStates]; 102 bool stateDirection [MaxStates]; 103 const char* stateSequence [MaxStates]; 104 105 const char* stateScript [MaxStates]; 106 107 /// @} 108 109 /// @name State Array 110 /// 111 /// State array is initialized onAdd from the individual state 112 /// struct array elements. 113 /// 114 /// @{ 115 StateData state[MaxStates]; ///< Array of states. 116 bool statesLoaded; ///< Are the states loaded yet? 117 S32 fireState; ///< The ID of the fire state. 118 bool isAnimated; ///< This image contains at least one animated states 119 /// @} 120 121 F32 maxScanHeading; ///< Maximum heading angle from center to scan, in degrees 122 F32 maxScanPitch; ///< Maximum pitch angle from center to scan, in degrees 123 F32 maxScanDistance; ///< Maximum distance to scan to 124 125 S32 scanTickFrequency; ///< How often should we perform a scan 126 S32 scanTickFrequencyVariance; ///< Random amount that should be added to the scan tick frequency 127 128 F32 trackLostTargetTime; ///< How long after the turret has lost the target should it still track it (in seconds) 129 130 S32 scanNode; ///< The node on the shape we will scan from 131 S32 aimNode; ///< The node on the shape we will aim from 132 133 F32 maxWeaponRange; ///< Maximum range of the weapons, which may be different than the max scan distance 134 135 F32 weaponLeadVelocity; ///< Velocity used to lead target (if value <= 0, don't lead target). 136 137public: 138 AITurretShapeData(); 139 140 DECLARE_CONOBJECT(AITurretShapeData); 141 142 static void initPersistFields(); 143 144 virtual bool onAdd(); 145 virtual bool preload(bool server, String &errorStr); 146 147 virtual void packData(BitStream* stream); 148 virtual void unpackData(BitStream* stream); 149 150 S32 lookupState(const char* name); ///< Get a state by name. 151}; 152 153//---------------------------------------------------------------------------- 154 155class AITurretShape: public TurretShape 156{ 157 typedef TurretShape Parent; 158 159protected: 160 161 enum MaskBits { 162 TurretStateMask = Parent::NextFreeMask, 163 NextFreeMask = Parent::NextFreeMask << 1 164 }; 165 166 struct TargetInfo 167 { 168 SimObjectPtr<ShapeBase> target; ///< Current target 169 Point3F lastPos; ///< The target's last known position 170 VectorF lastVel; ///< The target's last known velocity 171 SimTime lastSightTime; ///< The last time we saw the target 172 bool hadValidTarget; ///< Did we previously have a valid target? 173 174 TargetInfo() {reset();} 175 176 void reset() 177 { 178 target = NULL; 179 lastPos.zero(); 180 lastVel.zero(); 181 lastSightTime = 0; 182 hadValidTarget = false; 183 } 184 185 // Check if we currently have a valid target 186 bool isValid() const {return target != NULL;} 187 188 // Check if we used to have a target 189 bool hadTarget() const {return hadValidTarget;} 190 }; 191 192 // Static attributes 193 AITurretShapeData* mDataBlock; 194 195 F32 mScanHeading; 196 F32 mScanPitch; 197 F32 mScanDistance; 198 F32 mScanDistanceSquared; 199 Box3F mScanBox; 200 Box3F mTransformedScanBox; 201 202 S32 mScanTickFrequency; 203 S32 mScanTickFrequencyVariance; 204 S32 mTicksToNextScan; 205 206 F32 mWeaponRangeSquared; 207 F32 mWeaponLeadVelocitySquared; 208 209 SimSet mIgnoreObjects; ///< Ignore these objects when targeting 210 211 bool mScanForTargets; 212 bool mTrackTarget; 213 TargetInfo mTarget; ///< Information on the current target 214 215 SimObjectList mPotentialTargets; 216 217 AITurretShapeData::StateData *mState; 218 F32 mStateDelayTime; ///< Time till next state. 219 bool mStateActive; ///< Is the turret active? 220 TSThread *mStateAnimThread; 221 222 void _initState(); 223 void _updateTurretState(F32 dt); 224 225 /// Utility function to call state script functions on the datablock 226 /// @param function Function 227 void _scriptCallback(const char* function); 228 229 void _setScanBox(); 230 231 void _cleanupPotentialTargets(); 232 void _performScan(); 233 void _lostTarget(); 234 void _gainedTarget(ShapeBase* target); 235 void _trackTarget(F32 dt); 236 void _cleanupTargetAndTurret(); 237 bool _testTargetLineOfSight(Point3F& aimPoint, ShapeBase* target, Point3F& sightPoint); 238 239 /// ObjectRenderInst delegate hooked up in prepBatchRender 240 /// if GameBase::gShowBoundingBox is true. 241 void _renderScanner( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat ); 242 243public: 244 245 MatrixF mScanWorkspaceScanMat; 246 MatrixF mScanWorkspaceScanWorldMat; 247 248public: 249 250 AITurretShape(); 251 virtual ~AITurretShape(); 252 253 static void initPersistFields(); 254 255 bool onAdd(); 256 void onRemove(); 257 bool onNewDataBlock(GameBaseData *dptr, bool reload); 258 259 void addToIgnoreList(ShapeBase* obj); 260 void removeFromIgnoreList(ShapeBase* obj); 261 262 void setTurretStateName(const char* newState, bool force=false); 263 void setTurretState(U32 newState, bool force=false); 264 265 void activateTurret() {mStateActive = true;} 266 void deactivateTurret() {mStateActive = false;} 267 268 void startScanForTargets() {mScanForTargets = true;} 269 void stopScanForTargets() {mScanForTargets = false;} 270 271 void startTrackingTarget() {mTrackTarget = true;} 272 void stopTrackingTarget() {mTrackTarget = false;} 273 274 ShapeBase* getTarget() {return mTarget.target;} 275 bool hasTarget() {return mTarget.target != NULL;} 276 void resetTarget() {mTarget.reset();} 277 278 void addPotentialTarget(ShapeBase* shape); 279 280 void setWeaponLeadVelocity(F32 velocity) {mWeaponLeadVelocitySquared = velocity * velocity;} 281 F32 getWeaponLeadVelocity() {return mSqrt(mWeaponLeadVelocitySquared);} 282 283 void setAllGunsFiring(bool fire); 284 void setGunSlotFiring(S32 slot, bool fire); 285 286 virtual void setTransform(const MatrixF &mat); 287 void getScanTransform(MatrixF& mat); 288 void getAimTransform(MatrixF& mat); 289 290 F32 getMaxScanHeading() const {return mScanHeading;} 291 F32 getMaxScanPitch() const {return mScanPitch;} 292 F32 getMaxScanDistance() const {return mScanDistance;} 293 F32 getMaxScanDistanceSquared() const {return mScanDistanceSquared;} 294 295 void recenterTurret(); 296 297 virtual void processTick(const Move *move); 298 virtual void advanceTime(F32 dt); 299 300 virtual U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream); 301 virtual void unpackUpdate(NetConnection *conn, BitStream *stream); 302 303 void prepBatchRender( SceneRenderState *state, S32 mountedImageIndex ); 304 305 DECLARE_CONOBJECT(AITurretShape); 306}; 307 308#endif // _AITURRETSHAPE_H_ 309
