weapons.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/weapons.cpp $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 3/27/02 4:43p $*
  29. * *
  30. * $Revision:: 270 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "weapons.h"
  36. #include "weaponmanager.h"
  37. #include "debug.h"
  38. #include "soldier.h"
  39. #include "combat.h"
  40. #include "gameobjmanager.h"
  41. #include "line3d.h"
  42. #include "physcoltest.h"
  43. #include "pscene.h"
  44. #include "rendobj.h"
  45. #include "quat.h"
  46. #include "combatsound.h"
  47. #include "slnode.h"
  48. #include "scripts.h"
  49. #include "WWAudio.H"
  50. #include "Sound3D.H"
  51. #include "chunkio.h"
  52. #include "assets.h"
  53. #include "bullet.h"
  54. #include "crandom.h"
  55. #include "damage.h"
  56. #include "ccamera.h"
  57. #include "rbody.h"
  58. #include "timeddecophys.h"
  59. #include "wwphysids.h"
  60. #include "part_emt.h"
  61. #include "vehicle.h"
  62. #include "c4.h"
  63. #include "objlibrary.h"
  64. #include "wwprofile.h"
  65. #include "hudinfo.h"
  66. #include "projectile.h"
  67. #include "combatphysobserver.h"
  68. #include "surfaceeffects.h"
  69. #include "beacongameobj.h"
  70. #include "weaponview.h"
  71. #include "diaglog.h"
  72. #include "playerdata.h"
  73. #include "cheatmgr.h"
  74. /*
  75. **
  76. */
  77. #define RANDOM_VECTOR( spread ) Vector3( FreeRandom.Get_Float( -(spread), (spread) ), \
  78. FreeRandom.Get_Float( -(spread), (spread) ), \
  79. FreeRandom.Get_Float( -(spread), (spread) ) )
  80. /**
  81. ** EjectCasingObserverClass
  82. ** This is a class which observes all eject casings and creates sound effects when they
  83. ** collide. Since the task of this observer is so simple, I am creating a single static
  84. ** instance of it and pluging it into all eject casings.
  85. */
  86. class EjectCasingObserverClass : public CombatPhysObserverClass
  87. {
  88. public:
  89. virtual CollisionReactionType Collision_Occurred(const CollisionEventClass & event)
  90. {
  91. /*
  92. SurfaceEffectsManager::Apply_Effect(event.CollisionResult->SurfaceType,
  93. SurfaceEffectsManager::HITTER_TYPE_EJECT_CASING,
  94. Matrix3D(event.CollisionResult->ContactPoint),
  95. event.OtherObj, NULL, false);
  96. */
  97. return COLLISION_REACTION_DEFAULT;
  98. }
  99. };
  100. static EjectCasingObserverClass _TheEjectCasingObserver;
  101. /*
  102. **
  103. */
  104. WeaponClass::WeaponClass( const WeaponDefinitionClass *def ) :
  105. Definition( NULL ),
  106. PrimaryAmmoDefinition( NULL ),
  107. SecondaryAmmoDefinition( NULL ),
  108. Model( NULL ),
  109. State( STATE_IDLE ),
  110. StateTimer( 0.0f ),
  111. UpdateModel( WEAPON_MODEL_UPDATE_IS_NEEDED ),
  112. NextAnimState( WEAPON_ANIM_NOT_FIRING ),
  113. CurrentAnimState( WEAPON_ANIM_NOT_FIRING ),
  114. LastFrameIsPrimaryTriggered( false ),
  115. LastFrameIsSecondaryTriggered( false ),
  116. IsPrimaryTriggered( false ),
  117. IsSecondaryTriggered( false ),
  118. TotalRoundsFired( 0 ),
  119. ClipRounds( 0 ),
  120. InventoryRounds( 0 ),
  121. BurstDelayTimer( 0 ),
  122. BurstCount( 0 ),
  123. BulletBumpTime( 0 ),
  124. DidFire( false ),
  125. ContinuousEmitters( NULL ),
  126. ContinuousSound( NULL ),
  127. C4DetonationMode( 1 ),
  128. Target( 0, 0, 0 ),
  129. FiringSound( NULL ),
  130. FiringSoundDefID( 0 ),
  131. WeaponExists( true ),
  132. SafetySet( false ),
  133. LockTriggers( false ),
  134. EmptySoundTimer( 0 )
  135. {
  136. if ( def != NULL ) {
  137. Init( def );
  138. }
  139. }
  140. WeaponClass::~WeaponClass( void )
  141. {
  142. if (FiringSound != NULL) {
  143. FiringSound->Remove_From_Scene ();
  144. FiringSoundDefID = 0;
  145. }
  146. REF_PTR_RELEASE( Model );
  147. REF_PTR_RELEASE( FiringSound );
  148. Do_Continuous_Effects( false );
  149. }
  150. void WeaponClass::Init( const WeaponDefinitionClass *weapon_def )
  151. {
  152. WWASSERT( Definition == NULL );
  153. Definition = weapon_def;
  154. WWASSERT( Definition != NULL );
  155. // Setup the ammo defs
  156. WWASSERT( Definition->PrimaryAmmoDefID != 0 );
  157. PrimaryAmmoDefinition = WeaponManager::Find_Ammo_Definition( Definition->PrimaryAmmoDefID );
  158. WWASSERT( Definition->SecondaryAmmoDefID != 0 );
  159. SecondaryAmmoDefinition = WeaponManager::Find_Ammo_Definition( Definition->SecondaryAmmoDefID );
  160. }
  161. /*
  162. ** Weapon Save and Load
  163. */
  164. enum {
  165. CHUNKID_VARIABLES = 910991544,
  166. XXX_CHUNKID_AMMO,
  167. CHUNKID_OWNER_REF,
  168. XXXCHUNKID_TARGET_REF,
  169. XXXCHUNKID_DESIRED_TARGET_REF,
  170. CHUNKID_TARGET_OBJECT,
  171. MICROCHUNKID_IN_FLY_MODE = 1,
  172. MICROCHUNKID_STATE,
  173. MICROCHUNKID_STATE_TIMER,
  174. MICROCHUNKID_IS_PRIMARY_TRIGGERED,
  175. XXXMICROCHUNKID_IS_SNIPING,
  176. MICROCHUNKID_ROUNDS_FIRED,
  177. MICROCHUNKID_NEXT_ANIM_STATE,
  178. MICROCHUNKID_CURR_ANIM_STATE,
  179. MICROCHUNKID_UPDATE_MODEM,
  180. XXXXMICROCHUNKID_LAST_MUZZLE,
  181. XXXMICROCHUNKID_PENDING_TIMER,
  182. XXXMICROCHUNKID_TARGETING_MUZZLE,
  183. XXXMICROCHUNKID_TARGET_POSITION,
  184. XXXMICROCHUNKID_WEAPON_PARAMS_NAME,
  185. XXXMICROCHUNKID_MODEL_NAME,
  186. MICROCHUNKID_MODEL_PTR,
  187. XXXMICROCHUNKID_DYNAMIC_ERROR_ANGLE,
  188. MICROCHUNKID_CLIP_ROUNDS,
  189. MICROCHUNKID_INVENTORY_ROUNDS,
  190. MICROCHUNKID_BURST_TIMER,
  191. MICROCHUNKID_BURST_COUNT,
  192. XXX_MICROCHUNKID_AMMO_PARAMS_NAME,
  193. XXXMICROCHUNKID_AMMO_DEFINITION_NAME,
  194. XXXMICROCHUNKID_DEFINITION_NAME,
  195. XXXMICROCHUNKID_AMMO_DEFINITION_ID,
  196. MICROCHUNKID_DEFINITION_ID,
  197. MICROCHUNKID_IS_SECONDARY_TRIGGERED,
  198. MICROCHUNKID_C4_DETONATION_MODE,
  199. MICROCHUNKID_TARGET_LOCATION,
  200. MICROCHUNKID_WEAPON_EXISTS,
  201. MICROCHUNKID_SAFETY_SET,
  202. XXXMICROCHUNKID_OWNER_ERROR_ANGLE,
  203. };
  204. bool WeaponClass::Save( ChunkSaveClass & csave )
  205. {
  206. csave.Begin_Chunk( CHUNKID_VARIABLES );
  207. if ( Model ) {
  208. #if 0
  209. csave.Begin_Micro_Chunk( MICROCHUNKID_MODEL_NAME );
  210. const char * model_name = Model->Get_Name();
  211. csave.Write( model_name, strlen( model_name ) + 1 );
  212. csave.End_Micro_Chunk();
  213. #endif
  214. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_MODEL_PTR, Model );
  215. }
  216. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_STATE, State );
  217. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_STATE_TIMER, StateTimer );
  218. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_IS_PRIMARY_TRIGGERED, IsPrimaryTriggered );
  219. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_IS_SECONDARY_TRIGGERED, IsSecondaryTriggered );
  220. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ROUNDS_FIRED, TotalRoundsFired );
  221. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_NEXT_ANIM_STATE, NextAnimState );
  222. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_CURR_ANIM_STATE, CurrentAnimState );
  223. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_UPDATE_MODEM, UpdateModel );
  224. WRITE_SAFE_MICRO_CHUNK( csave, MICROCHUNKID_CLIP_ROUNDS, ClipRounds, int );
  225. WRITE_SAFE_MICRO_CHUNK( csave, MICROCHUNKID_INVENTORY_ROUNDS, InventoryRounds, int );
  226. WRITE_SAFE_MICRO_CHUNK( csave, MICROCHUNKID_BURST_TIMER, BurstDelayTimer, float );
  227. WRITE_SAFE_MICRO_CHUNK( csave, MICROCHUNKID_BURST_COUNT, BurstCount, int );
  228. int def_id = Definition->Get_ID();
  229. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DEFINITION_ID, def_id );
  230. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_C4_DETONATION_MODE, C4DetonationMode );
  231. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_TARGET_LOCATION, Target );
  232. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_WEAPON_EXISTS, WeaponExists );
  233. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_SAFETY_SET, SafetySet );
  234. csave.End_Chunk();
  235. if ( Owner != NULL ) {
  236. csave.Begin_Chunk( CHUNKID_OWNER_REF );
  237. Owner.Save( csave );
  238. csave.End_Chunk();
  239. }
  240. if ( TargetObject != NULL ) {
  241. csave.Begin_Chunk( CHUNKID_TARGET_OBJECT );
  242. TargetObject.Save( csave );
  243. csave.End_Chunk();
  244. }
  245. // Don't need to save DidFire
  246. // Don't need to save ContinuousEmitters;
  247. // Don't need to save ContinuousSound;
  248. // Don't need to save FiringSound;
  249. // Dont need to save BulletBumpTime
  250. return true;
  251. }
  252. bool WeaponClass::Load( ChunkLoadClass &cload )
  253. {
  254. while (cload.Open_Chunk()) {
  255. switch(cload.Cur_Chunk_ID()) {
  256. case CHUNKID_VARIABLES:
  257. {
  258. int def_id = 0;
  259. while (cload.Open_Micro_Chunk()) {
  260. switch(cload.Cur_Micro_Chunk_ID()) {
  261. READ_MICRO_CHUNK( cload, MICROCHUNKID_STATE, State );
  262. READ_MICRO_CHUNK( cload, MICROCHUNKID_STATE_TIMER, StateTimer );
  263. READ_MICRO_CHUNK( cload, MICROCHUNKID_IS_PRIMARY_TRIGGERED, IsPrimaryTriggered );
  264. READ_MICRO_CHUNK( cload, MICROCHUNKID_IS_SECONDARY_TRIGGERED, IsSecondaryTriggered );
  265. READ_MICRO_CHUNK( cload, MICROCHUNKID_ROUNDS_FIRED, TotalRoundsFired );
  266. READ_MICRO_CHUNK( cload, MICROCHUNKID_NEXT_ANIM_STATE, NextAnimState );
  267. READ_MICRO_CHUNK( cload, MICROCHUNKID_CURR_ANIM_STATE, CurrentAnimState );
  268. READ_MICRO_CHUNK( cload, MICROCHUNKID_UPDATE_MODEM, UpdateModel );
  269. READ_SAFE_MICRO_CHUNK( cload, MICROCHUNKID_CLIP_ROUNDS, ClipRounds, int );
  270. READ_SAFE_MICRO_CHUNK( cload, MICROCHUNKID_INVENTORY_ROUNDS, InventoryRounds, int );
  271. READ_SAFE_MICRO_CHUNK( cload, MICROCHUNKID_BURST_TIMER, BurstDelayTimer, float );
  272. READ_SAFE_MICRO_CHUNK( cload, MICROCHUNKID_BURST_COUNT, BurstCount, int );
  273. READ_MICRO_CHUNK( cload, MICROCHUNKID_DEFINITION_ID, def_id );
  274. READ_MICRO_CHUNK( cload, MICROCHUNKID_C4_DETONATION_MODE, C4DetonationMode );
  275. READ_MICRO_CHUNK( cload, MICROCHUNKID_TARGET_LOCATION, Target );
  276. READ_MICRO_CHUNK( cload, MICROCHUNKID_WEAPON_EXISTS, WeaponExists );
  277. READ_MICRO_CHUNK( cload, MICROCHUNKID_SAFETY_SET, SafetySet );
  278. #if 0
  279. case XXX_MICROCHUNKID_MODEL_NAME:
  280. {
  281. WWASSERT( Model == NULL );
  282. char model_name[80];
  283. cload.Read( model_name, cload.Cur_Micro_Chunk_Length() );
  284. RenderObjClass * robj = WW3DAssetManager::Get_Instance()->Create_Render_Obj( model_name );
  285. Set_Model( robj );
  286. cload.Close_Micro_Chunk();
  287. cload.Open_Micro_Chunk();
  288. WWASSERT( cload.Cur_Micro_Chunk_ID() == MICROCHUNKID_MODEL_PTR );
  289. void * old_ptr = NULL;
  290. cload.Read( &old_ptr, sizeof( old_ptr ) );
  291. SaveLoadSystemClass::Register_Pointer( old_ptr, robj );
  292. REF_PTR_RELEASE( robj );
  293. break;
  294. }
  295. #endif
  296. READ_MICRO_CHUNK( cload, MICROCHUNKID_MODEL_PTR, Model );
  297. default:
  298. Debug_Say(("Unhandled Micro Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  299. break;
  300. }
  301. cload.Close_Micro_Chunk();
  302. }
  303. Init( WeaponManager::Find_Weapon_Definition( def_id ) );
  304. break;
  305. }
  306. case CHUNKID_OWNER_REF:
  307. Owner.Load( cload );
  308. break;
  309. case CHUNKID_TARGET_OBJECT:
  310. TargetObject.Load( cload );
  311. break;
  312. default:
  313. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  314. break;
  315. }
  316. cload.Close_Chunk();
  317. }
  318. if (Model != NULL) {
  319. REQUEST_REF_COUNTED_POINTER_REMAP ((RefCountClass **)&Model);
  320. }
  321. // Legacy
  322. if ( ClipRounds == 0 && InventoryRounds < 0 ) {
  323. ClipRounds = Definition->ClipSize;
  324. }
  325. return true;
  326. }
  327. void WeaponClass::Set_Owner( ArmedGameObj *owner )
  328. {
  329. Owner = owner;
  330. }
  331. void WeaponClass::Set_Target_Object( PhysicalGameObj * obj )
  332. {
  333. TargetObject = obj;
  334. }
  335. PhysicalGameObj * WeaponClass::Get_Target_Object( PhysicalGameObj * obj )
  336. {
  337. return ( PhysicalGameObj * )TargetObject.Get_Ptr();
  338. }
  339. void WeaponClass::Set_Model( RenderObjClass *model )
  340. {
  341. if ( Model != NULL ) {
  342. Model->Release_Ref();
  343. }
  344. Model = model;
  345. if ( Model != NULL ) {
  346. Model->Add_Ref();
  347. Init_Muzzle_Flash( Model );
  348. }
  349. }
  350. void WeaponClass::Select( void )
  351. {
  352. // Debug_Say(( "Weapon Selected\n" ));
  353. Set_State( STATE_START_SWITCH );
  354. UpdateModel = WEAPON_MODEL_UPDATE_WILL_BE_NEEDED;
  355. }
  356. void WeaponClass::Deselect( void )
  357. {
  358. Do_Continuous_Effects( false );
  359. IsPrimaryTriggered = false;
  360. IsSecondaryTriggered = false;
  361. LastFrameIsPrimaryTriggered = false;
  362. LastFrameIsSecondaryTriggered = false;
  363. }
  364. void WeaponClass::Next_C4_Detonation_Mode( void )
  365. {
  366. #define LAST_C4_DETONATION_MODE 3
  367. if ( ++C4DetonationMode > LAST_C4_DETONATION_MODE ) {
  368. C4DetonationMode = 1;
  369. }
  370. Debug_Say(( "Weapon Next C4 Detonation Mode %d\n", C4DetonationMode ));
  371. }
  372. /*
  373. **
  374. */
  375. void WeaponClass::Set_Primary_Triggered( bool triggered )
  376. {
  377. if ( !LockTriggers ) {
  378. IsPrimaryTriggered = triggered;
  379. }
  380. }
  381. void WeaponClass::Set_Secondary_Triggered( bool triggered )
  382. {
  383. if ( !LockTriggers ) {
  384. IsSecondaryTriggered = triggered;
  385. }
  386. }
  387. /*
  388. ** AMMO
  389. */
  390. void WeaponClass::Add_Rounds( int num )
  391. {
  392. // If we have an empty clip ( first time ) load directly
  393. if ( ClipRounds == 0 ) {
  394. if ( num < 0 ) {
  395. ClipRounds = Definition->ClipSize;
  396. } else if ( InventoryRounds >= 0 ) {
  397. int count = MIN( num, (int)Definition->ClipSize );
  398. num -= count;
  399. ClipRounds = count;
  400. }
  401. }
  402. if ( CheatMgrClass::Get_Instance () != NULL &&
  403. CheatMgrClass::Get_Instance ()->Is_Cheat_Enabled( CheatMgrClass::CHEAT_INFINITE_AMMO ) && Owner == COMBAT_STAR )
  404. {
  405. InventoryRounds = Definition->MaxInventoryRounds ;
  406. } else if ( num < 0 ) {
  407. InventoryRounds = -1;
  408. } else if ( InventoryRounds >= 0 ) {
  409. InventoryRounds += num;
  410. }
  411. if ( (InventoryRounds != -1) && (InventoryRounds > (int)Definition->MaxInventoryRounds) ) {
  412. InventoryRounds = Definition->MaxInventoryRounds ;
  413. }
  414. }
  415. int WeaponClass::Get_Total_Rounds( void )
  416. {
  417. if ( InventoryRounds < 0 ) {
  418. return -1;
  419. }
  420. //
  421. // Check to see if the infinite ammo cheat is enabled
  422. //
  423. if ( CheatMgrClass::Get_Instance () != NULL &&
  424. CheatMgrClass::Get_Instance ()->Is_Cheat_Enabled( CheatMgrClass::CHEAT_INFINITE_AMMO ) && Owner == COMBAT_STAR )
  425. {
  426. return -1;
  427. }
  428. return ClipRounds + InventoryRounds;
  429. }
  430. void WeaponClass::Set_Total_Rounds( int num )
  431. {
  432. if ( CheatMgrClass::Get_Instance () != NULL &&
  433. CheatMgrClass::Get_Instance ()->Is_Cheat_Enabled( CheatMgrClass::CHEAT_INFINITE_AMMO ) && Owner == COMBAT_STAR )
  434. {
  435. InventoryRounds = Definition->MaxInventoryRounds;
  436. } else if ( num == -1 ) {
  437. InventoryRounds = -1;
  438. } else {
  439. // Assume what you have in inventory is correct
  440. num -= (int)InventoryRounds;
  441. // And the rest goes in the clip
  442. ClipRounds = num;
  443. // Fix a negative clip
  444. if ( ClipRounds < 0 ) {
  445. InventoryRounds += ClipRounds;
  446. ClipRounds = 0;
  447. }
  448. // Fix overloaded clip
  449. if ( ClipRounds > Definition->ClipSize ) {
  450. InventoryRounds += ClipRounds - Definition->ClipSize;
  451. ClipRounds -= ClipRounds - Definition->ClipSize;
  452. }
  453. // Fix Overloaded inventory
  454. if ( InventoryRounds > (int)Definition->MaxInventoryRounds ) {
  455. InventoryRounds = Definition->MaxInventoryRounds;
  456. }
  457. }
  458. }
  459. bool WeaponClass::Is_Ammo_Maxed( void )
  460. {
  461. if (InventoryRounds == -1) return true;
  462. // Special case for C4
  463. if (((int)Definition->MaxInventoryRounds == 0) && (ClipRounds == 0)) return false;
  464. return (InventoryRounds == (int)Definition->MaxInventoryRounds);
  465. }
  466. void WeaponClass::Decrement_Rounds( int rounds )
  467. {
  468. if ( ClipRounds != -1 ) {
  469. ClipRounds -= rounds;
  470. if ( ClipRounds <= 0 ) {
  471. ClipRounds = 0;
  472. }
  473. }
  474. }
  475. void WeaponClass::Do_Reload( void )
  476. {
  477. int added = (int)Definition->ClipSize - (int)ClipRounds;
  478. //
  479. // Check to see if the infinite ammo cheat is enabled
  480. //
  481. bool apply_cheat = false;
  482. if ( CheatMgrClass::Get_Instance () != NULL &&
  483. CheatMgrClass::Get_Instance ()->Is_Cheat_Enabled( CheatMgrClass::CHEAT_INFINITE_AMMO ) && Owner == COMBAT_STAR )
  484. {
  485. apply_cheat = true;
  486. }
  487. if ( apply_cheat == false && InventoryRounds >= 0 ) {
  488. if ( InventoryRounds < added ) {
  489. added = InventoryRounds;
  490. }
  491. InventoryRounds -= added;
  492. }
  493. ClipRounds += added;
  494. }
  495. float WeaponClass::Get_Range( void )
  496. {
  497. return ( PrimaryAmmoDefinition != NULL ) ? PrimaryAmmoDefinition->Range : 0;
  498. }
  499. /*
  500. ** Fire_C4
  501. */
  502. void WeaponClass::Fire_C4( const AmmoDefinitionClass *ammo_def )
  503. {
  504. #define C4_OBJECT_NAME "Tossed C4"
  505. // C4 special case
  506. C4GameObj *c4 = (C4GameObj *)ObjectLibraryManager::Create_Object( C4_OBJECT_NAME );
  507. if ( c4 ) {
  508. c4->Init_C4( ammo_def, Get_Owner()->As_SoldierGameObj(), C4DetonationMode, Get_Muzzle() );
  509. }
  510. return ;
  511. }
  512. /*
  513. ** Fire_Beacon
  514. */
  515. bool WeaponClass::Fire_Beacon( const AmmoDefinitionClass *ammo_def )
  516. {
  517. bool retval = false;
  518. // only for server
  519. if ( CombatManager::I_Am_Server() ) {
  520. //
  521. // Create the beacon
  522. //
  523. BeaconGameObj *beacon = (BeaconGameObj *)ObjectLibraryManager::Create_Object( ammo_def->BeaconDefID );
  524. if ( beacon != NULL ) {
  525. //
  526. // Get the position of the owner
  527. //
  528. Vector3 pos;
  529. Get_Owner()->Get_Position( &pos );
  530. beacon->Init_Beacon( Definition, Get_Owner()->As_SoldierGameObj(), pos );
  531. beacon->Start_Observers ();
  532. //
  533. // Begin arming the beacon
  534. //
  535. if ( beacon->Can_Place_Here ( pos ) ) {
  536. beacon->Begin_Arming();
  537. retval = true;
  538. } else {
  539. beacon->Set_Delete_Pending ();
  540. }
  541. }
  542. }
  543. return retval;
  544. }
  545. /*
  546. **
  547. */
  548. void WeaponClass::Do_Fire( bool primary )
  549. {
  550. WWPROFILE( "Do Fire" );
  551. // Stats
  552. if ( Get_Owner() &&
  553. Get_Owner()->As_SoldierGameObj() &&
  554. Get_Owner()->As_SoldierGameObj()->Get_Player_Data() ) {
  555. Get_Owner()->As_SoldierGameObj()->Get_Player_Data()->Stats_Add_Shot_Fired();
  556. Get_Owner()->As_SoldierGameObj()->Get_Player_Data()->Stats_Add_Weapon_Fired(
  557. this->Get_Definition()->Get_ID() );
  558. }
  559. // Debug_Say(( "Fire at %f\n", (float)WW3D::Get_Sync_Time() ));
  560. const AmmoDefinitionClass *ammo_def;
  561. if ( primary ) {
  562. ammo_def = PrimaryAmmoDefinition;
  563. } else {
  564. ammo_def = SecondaryAmmoDefinition;
  565. }
  566. // Debug_Say(( "Firing from %f %f %f\n", muzzle_pos.X, muzzle_pos.Y, muzzle_pos.Z ));
  567. // Debug_Say(( "Firing at %f %f %f\n", target_pos.X, target_pos.Y, target_pos.Z ));
  568. // Debug_Say(( " %d Clips %d in Clip\n", NumClips, ClipContents ));
  569. //
  570. // Handle each weapon type differently
  571. //
  572. if ( Get_Style() == WEAPON_HOLD_STYLE_BEACON ) {
  573. //
  574. // Try to fire the beacon
  575. //
  576. if ( Fire_Beacon( ammo_def ) ) {
  577. //
  578. // Decrement the rounds
  579. //
  580. // WWASSERT( BurstCount != 0 );
  581. BurstCount--;
  582. // WWASSERT( ClipRounds != 0 );
  583. Decrement_Rounds( ammo_def->SprayBulletCost );
  584. }
  585. } else {
  586. //
  587. // Decrement the rounds
  588. //
  589. // WWASSERT( BurstCount != 0 );
  590. BurstCount--;
  591. // WWASSERT( ClipRounds != 0 );
  592. Decrement_Rounds( ammo_def->SprayBulletCost );
  593. if ( Get_Style() == WEAPON_HOLD_STYLE_C4 ) {
  594. if ( CombatManager::I_Am_Server() ) { // Only on servers
  595. Fire_C4( ammo_def );
  596. }
  597. } else {
  598. Fire_Bullet( ammo_def, primary );
  599. }
  600. }
  601. return ;
  602. }
  603. /*
  604. ** Fire_Bullet
  605. */
  606. void WeaponClass::Fire_Bullet( const AmmoDefinitionClass *ammo_def, bool primary )
  607. {
  608. WWPROFILE( "Fire Bullet" );
  609. if ( Get_Owner() == COMBAT_STAR ) {
  610. Vector3 pos;
  611. COMBAT_STAR->Get_Position( &pos );
  612. DIAG_LOG(( "WFRD", "%1.2f; %1.2f; %1.2f; %s; %d; %d", pos.X, pos.Y, pos.Z, Get_Definition()->Get_Name(), !primary, Get_Total_Rounds() ));
  613. }
  614. int spray_count = ammo_def->SprayCount;
  615. if ( spray_count < 1 ) {
  616. spray_count = 1;
  617. }
  618. int muzzle_index = (Get_Total_Rounds_Fired() & 1) + ( primary ? 0 : 2 );
  619. if ( !Get_Owner()->Muzzle_Exists( muzzle_index ) ) {
  620. // if the second muzzle doesn't exist, use the first
  621. if ( muzzle_index & 1 ) {
  622. muzzle_index--;
  623. }
  624. }
  625. Matrix3D muzzle = Get_Muzzle( muzzle_index );
  626. // Special test for first person
  627. if ( CombatManager::Is_First_Person() && Get_Owner() == COMBAT_STAR ) {
  628. Vector3 fp_muzzle_pos = WeaponViewClass::Get_Muzzle_Pos();
  629. // cast to see what we hit
  630. Vector3 start = fp_muzzle_pos;
  631. Vector3 end = Get_Owner()->Get_Targeting_Pos();
  632. LineSegClass ray( start, end );
  633. CastResultStruct res;
  634. PhysRayCollisionTestClass raytest(ray,&res,BULLET_COLLISION_GROUP,COLLISION_TYPE_PROJECTILE);
  635. Ignore_Owner();
  636. { WWPROFILE( "Cast_Ray" );
  637. COMBAT_SCENE->Cast_Ray( raytest );
  638. }
  639. Unignore_Owner();
  640. // If the hit point is near the targeting_pos, use the first person muzzle
  641. if ( raytest.Result->Fraction >= 0.99F ) {
  642. muzzle.Obj_Look_At( fp_muzzle_pos, Get_Owner()->Get_Targeting_Pos(), 0 );
  643. }
  644. }
  645. Vector3 bullet_target(0,0,0);
  646. DamageableGameObj * target_obj = NULL;
  647. if ( ammo_def->IsTracking ) {
  648. // determine the target
  649. Vector3 start = muzzle.Get_Translation();
  650. // (gth) this modification makes bikes able to target anything even though their muzzle doesn't rotate...
  651. Vector3 end = Get_Owner()->Get_Targeting_Pos(); //muzzle * Vector3( ammo_def->Range, 0, 0 );
  652. LineSegClass ray( start, end );
  653. CastResultStruct res;
  654. PhysRayCollisionTestClass raytest(ray,&res,BULLET_COLLISION_GROUP,COLLISION_TYPE_PROJECTILE);
  655. Ignore_Owner();
  656. { WWPROFILE( "Cast Ray" );
  657. COMBAT_SCENE->Cast_Ray( raytest );
  658. }
  659. Unignore_Owner();
  660. ray.Compute_Point( raytest.Result->Fraction, &end );
  661. bullet_target = end;
  662. if ( raytest.CollidedPhysObj != NULL && raytest.CollidedPhysObj->Get_Observer() != NULL ) {
  663. target_obj = ((CombatPhysObserverClass *)raytest.CollidedPhysObj->Get_Observer())->As_DamageableGameObj();
  664. // No homing on buildings
  665. if ( target_obj->As_BuildingGameObj() != NULL ) {
  666. target_obj = NULL;
  667. }
  668. }
  669. }
  670. for ( int count = 0; count < spray_count; count++ ) {
  671. Vector3 velocity = muzzle.Get_X_Vector() * ammo_def->Velocity;
  672. #if 0
  673. if ( ammo_def->SprayAngle ) {
  674. // Use Spray Angle
  675. Vector3 spray_adjust = RANDOM_VECTOR( 1.0 );
  676. spray_adjust.Normalize();
  677. spray_adjust = spray_adjust * ammo_def->Velocity;
  678. spray_adjust *= ammo_def->SprayAngle / (float)(2*WWMATH_PI);
  679. velocity += spray_adjust;
  680. velocity.Normalize();
  681. velocity = velocity * ammo_def->Velocity;
  682. }
  683. if ( DynamicErrorAngle ) {
  684. velocity += RANDOM_VECTOR( DynamicErrorAngle / 2 );
  685. velocity.Normalize();
  686. velocity = velocity * ammo_def->Velocity;
  687. }
  688. #else
  689. Matrix3D VelTM;
  690. // Look at backwards with random roll
  691. VelTM.Look_At( velocity, Vector3( 0,0,0 ), FreeRandom.Get_Float( DEG_TO_RADF( 360.0f ) ) );
  692. #define SPRAY_CHEAT 0
  693. #if SPRAY_CHEAT
  694. float error_angle = 0;
  695. #else
  696. float error_angle = ammo_def->SprayAngle;
  697. #endif
  698. float rand_angle = FreeRandom.Get_Float() * error_angle;
  699. VelTM.Rotate_Y( rand_angle );
  700. velocity = VelTM.Get_Z_Vector();
  701. velocity.Normalize();
  702. velocity *= ammo_def->Velocity;
  703. #endif
  704. //Vector3 position = muzzle.Get_Translation();
  705. Vector3 position;
  706. Compute_Bullet_Start_Point(muzzle,&position);
  707. BulletBumpTime += ammo_def->AliasedSpeed * TimeManager::Get_Frame_Seconds() * 1.3f;
  708. BulletBumpTime = WWMath::Wrap( BulletBumpTime, 0, TimeManager::Get_Frame_Seconds() );
  709. float bump_time = BulletBumpTime;
  710. if ( (float)ammo_def->Velocity != 0 ) {
  711. bump_time += 1 / (float)ammo_def->Velocity; // Skip the first meter
  712. }
  713. //Debug_Say(( "%f %f\n", BulletBumpTime,bump_time ));
  714. if ( Target.Length2() != 0 || !ammo_def->IsTracking ) {
  715. bullet_target = Target;
  716. }
  717. // Bullets from vehicles belong to the gunner
  718. ArmedGameObj * bullet_owner = Get_Owner();
  719. if ( bullet_owner != NULL && bullet_owner->As_SmartGameObj() ) {
  720. VehicleGameObj * vehicle = bullet_owner->As_SmartGameObj()->As_VehicleGameObj();
  721. if ( ( vehicle ) && ( vehicle->Get_Actual_Gunner() != NULL ) ) {
  722. bullet_owner = vehicle->Get_Actual_Gunner();
  723. }
  724. }
  725. // Create a bullet
  726. BulletManager::Create_Bullet( ammo_def, position, velocity, bullet_owner, bump_time, bullet_target, target_obj );
  727. }
  728. if ( ammo_def->DisplayLaser ) {
  729. TimedDecorationPhysClass * laser_obj = NEW_REF( TimedDecorationPhysClass, () );
  730. if ( laser_obj ) {
  731. // Find where the laser should stop
  732. Vector3 start = muzzle.Get_Translation();
  733. Vector3 end = muzzle * Vector3( ammo_def->Range, 0, 0 );
  734. LineSegClass ray( start, end );
  735. CastResultStruct res;
  736. PhysRayCollisionTestClass raytest(ray,&res,BULLET_COLLISION_GROUP,COLLISION_TYPE_PROJECTILE);
  737. Ignore_Owner();
  738. { WWPROFILE( "Cast Ray" );
  739. COMBAT_SCENE->Cast_Ray( raytest );
  740. }
  741. Unignore_Owner();
  742. ray.Compute_Point( raytest.Result->Fraction, &end );
  743. // Create a laser
  744. RenderObjClass * model = NEW_REF( Line3DClass, ( start, end, 0.1f, 0.8f,0,0, 0.8f ) );
  745. laser_obj->Set_Model( model );
  746. model->Release_Ref();
  747. laser_obj->Set_Lifetime( 0.6f );
  748. laser_obj->Update_Cull_Box(); // htis may be needed due to a bug in Line3DClass
  749. COMBAT_SCENE->Add_Dynamic_Object( laser_obj );
  750. laser_obj->Release_Ref();
  751. }
  752. }
  753. // Initiate recoil effects on our parent object.
  754. Get_Owner()->Start_Recoil(muzzle_index,Get_Recoil_Scale(),Get_Recoil_Time());
  755. TotalRoundsFired++;
  756. }
  757. bool WeaponClass::Is_Muzzle_Clear()
  758. {
  759. int primary = 1;
  760. int muzzle_index = Get_Total_Rounds_Fired() & 1 + ( primary ? 0 : 2 );
  761. Matrix3D muzzle = Get_Muzzle( muzzle_index );
  762. Vector3 start_pt;
  763. Vector3 end_pt;
  764. muzzle.Get_Translation( &end_pt );
  765. if ( Get_Owner() == NULL ) {
  766. return true;
  767. }
  768. if ((Get_Owner() == COMBAT_STAR) && CombatManager::Is_First_Person()) {
  769. return true;
  770. }
  771. if (Get_Owner()->As_SoldierGameObj() != NULL) {
  772. start_pt = Get_Owner()->Get_Bullseye_Position();
  773. } else {
  774. // Estimate the distance we need to sweep back along the muzzle in order to be inside the vehicle.
  775. Vector3 owner_pos;
  776. Get_Owner()->Get_Position( &owner_pos );
  777. owner_pos -= end_pt;
  778. owner_pos.Z = 0.0f;
  779. float dist = owner_pos.Quick_Length();
  780. // Compute the starting point for a ray to test if the muzzle is inside anything.
  781. Vector3 offset;
  782. Matrix3D::Rotate_Vector(muzzle,Vector3(-dist,0.0f,0.0f),&offset);
  783. start_pt = end_pt + offset;
  784. }
  785. // Cast the ray
  786. LineSegClass ray( start_pt, end_pt );
  787. CastResultStruct res;
  788. PhysRayCollisionTestClass raytest(ray,&res,BULLET_COLLISION_GROUP,COLLISION_TYPE_PROJECTILE);
  789. raytest.CheckDynamicObjs = false;
  790. {
  791. WWPROFILE( "Cast Ray" );
  792. COMBAT_SCENE->Cast_Ray( raytest );
  793. }
  794. return raytest.Result->Fraction == 1.0f;
  795. }
  796. void WeaponClass::Compute_Bullet_Start_Point(const Matrix3D & muzzle,Vector3 * set_start_point)
  797. {
  798. //
  799. // The default behavior is to start the bullet at the position of the muzzle.
  800. //
  801. muzzle.Get_Translation(set_start_point);
  802. //
  803. // If this weapon is owned by a vehicle, try to ensure the bullet starts in front
  804. // of any dynamic objects that may be between the muzzle point and the body of the vehicle
  805. //
  806. if (Get_Owner()->As_VehicleGameObj() != NULL) {
  807. Vector3 ray_start;
  808. Vector3 ray_end;
  809. muzzle.Get_Translation( &ray_end );
  810. // Estimate the distance we need to sweep back along the muzzle in order to be inside the vehicle.
  811. Vector3 owner_pos;
  812. Get_Owner()->Get_Position( &owner_pos );
  813. owner_pos -= ray_end;
  814. owner_pos.Z = 0.0f;
  815. float dist = owner_pos.Quick_Length();
  816. // Compute the starting point for a ray to test if the muzzle is inside anything.
  817. Vector3 offset;
  818. Matrix3D::Rotate_Vector(muzzle,Vector3(-dist,0.0f,0.0f),&offset);
  819. ray_start = ray_end + offset;
  820. // Cast the ray
  821. LineSegClass ray( ray_start, ray_end );
  822. CastResultStruct res;
  823. PhysRayCollisionTestClass raytest(ray,&res,BULLET_COLLISION_GROUP,COLLISION_TYPE_PROJECTILE);
  824. raytest.CheckStaticObjs = false;
  825. {
  826. Ignore_Owner();
  827. WWPROFILE( "Cast Ray" );
  828. COMBAT_SCENE->Cast_Ray( raytest );
  829. Unignore_Owner();
  830. }
  831. if (res.Fraction < 1.0f) {
  832. ray.Compute_Point(res.Fraction,set_start_point);
  833. }
  834. }
  835. }
  836. void WeaponClass::Do_Firing_Effects( void )
  837. {
  838. if ( TimeManager::Get_Frame_Seconds() == 0 ) {
  839. return; // No sounds when time stops
  840. }
  841. WWPROFILE( "Firing Effects" );
  842. Matrix3D muzzle = Get_Muzzle();
  843. // Don't do these two if fired by the star in First Person Mode
  844. if ( (Get_Owner() != COMBAT_STAR) || !CombatManager::Is_First_Person() ) {
  845. #if 01
  846. // create muzzle flash
  847. if ( Definition->MuzzleFlashPhysDefID != 0 ) {
  848. DefinitionClass * def = DefinitionMgrClass::Find_Definition( Definition->MuzzleFlashPhysDefID );
  849. if ( def != NULL ) {
  850. WWASSERT( ((PhysDefClass *)def)->Is_Type( "TimedDecorationPhysDef" ) );
  851. TimedDecorationPhysClass * muzzle_flash = (TimedDecorationPhysClass *)def->Create();
  852. if ( muzzle_flash ) {
  853. RenderObjClass * model = muzzle_flash->Peek_Model();
  854. if ( model != NULL ) {
  855. // muzzle.Rotate_X( FreeRandom.Get_Float( DEG_TO_RAD( 360 ) ) );
  856. muzzle_flash->Set_Transform( muzzle );
  857. COMBAT_SCENE->Add_Dynamic_Object( muzzle_flash );
  858. // If this is an emitter, start it
  859. if ( model->Class_ID() == RenderObjClass::CLASSID_PARTICLEEMITTER ) {
  860. ParticleEmitterClass * pe = (ParticleEmitterClass *)model;
  861. pe->Start();
  862. }
  863. } else {
  864. Debug_Say(( "Missing muzzle flash for %s\n", Definition->Get_Name() ));
  865. }
  866. muzzle_flash->Release_Ref();
  867. }
  868. } else {
  869. //Debug_Say(( "Couldn't find muzzle flash def for %s\n", Definition->Get_Name() ));
  870. }
  871. }
  872. #endif
  873. // if this gun is fired by the STAR and they have an eject bone, eject a shell.
  874. if ((Get_Owner() == COMBAT_STAR) && (Model != NULL)) {
  875. WWPROFILE( "Eject" );
  876. int eject_index = Model->Get_Bone_Index( "eject" );
  877. if ( eject_index > 0 ) {
  878. Make_Shell_Eject( Model->Get_Bone_Transform( eject_index ) );
  879. }
  880. }
  881. }
  882. // Make the Gunshot sound
  883. //
  884. // Determine which sound to play, the primary or secondary firing sound?
  885. //
  886. int sound_id = 0;
  887. if ( IsPrimaryTriggered && PrimaryAmmoDefinition != NULL ) {
  888. sound_id = PrimaryAmmoDefinition->FireSoundDefID;
  889. } else if ( IsSecondaryTriggered && SecondaryAmmoDefinition != NULL ) {
  890. sound_id = SecondaryAmmoDefinition->FireSoundDefID;
  891. }
  892. //
  893. // Release the old firing sound (if necessary)
  894. //
  895. bool release_curr_sound = false;
  896. if ( sound_id != FiringSoundDefID && FiringSound != NULL ) {
  897. release_curr_sound = true;
  898. }
  899. //
  900. // For first-person we make the weapon sounds "2D", so check to see
  901. // if we need to re-create the sound...
  902. //
  903. if ( FiringSound != NULL ) {
  904. if ( Get_Owner() == COMBAT_STAR && CombatManager::Is_First_Person() ) {
  905. //
  906. // In first person, we need the sound to be "2D"
  907. //
  908. if ( FiringSound->Get_Class_ID () != CLASSID_2D ) {
  909. release_curr_sound = true;
  910. }
  911. } else {
  912. //
  913. // In third person, we need the sound to be "3D"
  914. //
  915. if ( FiringSound->Get_Class_ID () == CLASSID_2D ) {
  916. release_curr_sound = true;
  917. }
  918. }
  919. }
  920. //
  921. // Release the currently playing sound as necessary
  922. //
  923. if ( release_curr_sound ) {
  924. FiringSound->Remove_From_Scene ();
  925. REF_PTR_RELEASE (FiringSound);
  926. FiringSoundDefID = 0;
  927. }
  928. //
  929. // Do we need to recreate the firing sound object, or can we just
  930. // reuse the one we already have?
  931. //
  932. if ( FiringSound != NULL ) {
  933. //
  934. // Stop the current sound
  935. //
  936. //FiringSound->Set_Transform( muzzle );
  937. //FiringSound->Stop();
  938. if ( FiringSound->Get_Class_ID () == CLASSID_3D ) {
  939. FiringSound->Stop();
  940. } else {
  941. FiringSound->Seek( 0 );
  942. }
  943. //
  944. // Force the sound to play
  945. //
  946. if ( Get_Owner() != COMBAT_STAR || CombatManager::Is_First_Person() == false ) {
  947. FiringSound->Add_To_Scene( true );
  948. FiringSound->Play();
  949. } else {
  950. FiringSound->Play();
  951. }
  952. //
  953. // Play the sound (or add it to the scene)
  954. //
  955. /*if ( Get_Owner() == COMBAT_STAR && CombatManager::Is_First_Person() ) {
  956. FiringSound->Play();
  957. } else {
  958. FiringSound->Remove_From_Scene();
  959. FiringSound->Add_To_Scene( true );
  960. }*/
  961. } else {
  962. //
  963. // Create a pseudo-3d sound effect for this firing sound and
  964. // add it to the sound scene.
  965. //
  966. if ( sound_id != 0 ) {
  967. //
  968. // Determine whether to play the sound as 2D or 3D
  969. //
  970. int classid_hint = CLASSID_3D;
  971. if ( Get_Owner() == COMBAT_STAR && CombatManager::Is_First_Person() ) {
  972. classid_hint = CLASSID_2D;
  973. }
  974. //
  975. // Create the sound
  976. //
  977. RefCountedGameObjReference *owner_ref = new RefCountedGameObjReference;
  978. owner_ref->Set_Ptr( Get_Owner() );
  979. FiringSound = WWAudioClass::Get_Instance()->Create_Sound( sound_id, owner_ref, 0, classid_hint );
  980. REF_PTR_RELEASE( owner_ref );
  981. }
  982. if ( FiringSound != NULL ) {
  983. FiringSoundDefID = sound_id;
  984. FiringSound->Set_Transform( muzzle );
  985. FiringSound->Attach_To_Object( Model );
  986. FiringSound->Add_To_Scene( true );
  987. }
  988. }
  989. // (gth) Apply an Impulse to tanks when they fires a bullet
  990. // Byon, this can be done with any RigidBody derived physics object (all vehicles)
  991. // I suggest making it a parameter of the weapon and having the strength be a
  992. // multiple of the mass of the vehicle.
  993. ArmedGameObj * owner = Get_Owner();
  994. VehicleGameObj * vehicle_game_obj = owner->As_VehicleGameObj();
  995. if (vehicle_game_obj != NULL) {
  996. PhysClass * vehicle = vehicle_game_obj->Peek_Physical_Object();
  997. if (vehicle->As_TrackedVehicleClass() != NULL) {
  998. RigidBodyClass * rbody = (RigidBodyClass *)vehicle;
  999. Vector3 impulse_pos;
  1000. muzzle.Get_Translation(&impulse_pos);
  1001. Vector3 impulse;
  1002. muzzle.Get_X_Vector(&impulse);
  1003. impulse *= -Definition->RecoilImpulse * rbody->Get_Mass();
  1004. rbody->Apply_Impulse(impulse,impulse_pos);
  1005. }
  1006. }
  1007. // Play an anim on the human owner
  1008. if ( owner != NULL && owner->As_SoldierGameObj() ) {
  1009. if ( !Definition->HumanFiringAnimation.Is_Empty() ) {
  1010. owner->As_SoldierGameObj()->Set_Animation( Definition->HumanFiringAnimation, 0, 0 );
  1011. }
  1012. }
  1013. }
  1014. void WeaponClass::Make_Shell_Eject( const Matrix3D & tm )
  1015. {
  1016. DefinitionClass * def = DefinitionMgrClass::Find_Definition( Definition->EjectPhysDefID );
  1017. if ((def != NULL) && ((PhysDefClass *)def)->Is_Type( "ProjectileDef" )) {
  1018. // Create the object
  1019. ProjectileClass * PhysicalObject = (ProjectileClass *)def->Create();
  1020. // Set its transform, collision group, observer, and initial velocity
  1021. PhysicalObject->Set_Transform( tm );
  1022. PhysicalObject->Set_Observer(&_TheEjectCasingObserver);
  1023. PhysicalObject->Set_Collision_Group( DEFAULT_COLLISION_GROUP );
  1024. PhysicalObject->Set_Velocity( tm.Rotate_Vector( Vector3( 2,0,0 ) ) );
  1025. // Add it to the scene
  1026. COMBAT_SCENE->Add_Dynamic_Object( PhysicalObject );
  1027. PhysicalObject->Release_Ref();
  1028. }
  1029. }
  1030. /*
  1031. **
  1032. */
  1033. void WeaponClass::Do_Continuous_Effects( bool enable )
  1034. {
  1035. if ( !enable ) {
  1036. for ( int i = 0; i < ContinuousEmitters.Length(); i++ ) {
  1037. if ( ContinuousEmitters[i] != NULL ) {
  1038. // Check if it is in the scene, it may ahve already been remove by completion
  1039. if ( ContinuousEmitters[i]->Peek_Scene() != NULL ) {
  1040. PhysicsSceneClass::Get_Instance()->Remove_Render_Object( ContinuousEmitters[i] );
  1041. }
  1042. ContinuousEmitters[i]->Stop();
  1043. ContinuousEmitters[i]->Release_Ref();
  1044. ContinuousEmitters[i] = NULL;
  1045. }
  1046. }
  1047. if ( ContinuousSound != NULL ) {
  1048. ContinuousSound->Stop();
  1049. ContinuousSound->Remove_From_Scene();
  1050. ContinuousSound->Release_Ref();
  1051. ContinuousSound = NULL;
  1052. }
  1053. }
  1054. if ( enable ) {
  1055. const AmmoDefinitionClass *ammo_def = PrimaryAmmoDefinition;
  1056. if (IsPrimaryTriggered == false && IsSecondaryTriggered) {
  1057. ammo_def = SecondaryAmmoDefinition;
  1058. }
  1059. int i;
  1060. if ( ContinuousEmitters.Length() == 0 ) {
  1061. int num_muzzles = 1;
  1062. if ( Get_Muzzle( 0 ) != Get_Muzzle( 1 ) ) {
  1063. num_muzzles = 2;
  1064. }
  1065. ContinuousEmitters.Resize( num_muzzles );
  1066. for ( int i = 0; i < ContinuousEmitters.Length(); i++ ) {
  1067. ContinuousEmitters[i] = NULL;
  1068. }
  1069. }
  1070. for ( i = 0; i < ContinuousEmitters.Length(); i++ ) {
  1071. if ( ContinuousEmitters[i] == NULL && !ammo_def->ContinuousEmitterName.Is_Empty() ) {
  1072. RenderObjClass * renderobj = Create_Render_Obj_From_Filename( ammo_def->ContinuousEmitterName );
  1073. if ( renderobj ) {
  1074. WWASSERT( renderobj->Class_ID() == RenderObjClass::CLASSID_PARTICLEEMITTER );
  1075. ContinuousEmitters[i] = (ParticleEmitterClass *)renderobj;
  1076. ContinuousEmitters[i]->Set_Velocity_Inheritance_Factor( 1 );
  1077. }
  1078. if ( ContinuousEmitters[i] ) {
  1079. SET_REF_OWNER( ContinuousEmitters[i] );
  1080. ContinuousEmitters[i]->Start();
  1081. PhysicsSceneClass::Get_Instance()->Add_Render_Object( ContinuousEmitters[i] );
  1082. }
  1083. }
  1084. }
  1085. if ( ContinuousSound == NULL && ammo_def->ContinuousSoundDefID != 0 ) {
  1086. ContinuousSound = WWAudioClass::Get_Instance()->Create_Continuous_Sound( ammo_def->ContinuousSoundDefID );
  1087. if ( ContinuousSound != NULL ) {
  1088. ContinuousSound->Add_To_Scene( true );
  1089. }
  1090. }
  1091. // if first person star, use first muzzle
  1092. if ( (Get_Owner() == COMBAT_STAR) && CombatManager::Is_First_Person() ) {
  1093. Vector3 fp_muzzle_pos = WeaponViewClass::Get_Muzzle_Pos();
  1094. Matrix3D muzzle;
  1095. muzzle.Obj_Look_At( fp_muzzle_pos, Get_Owner()->Get_Targeting_Pos(), 0 );
  1096. for ( i = 0; i < ContinuousEmitters.Length(); i++ ) {
  1097. if ( ContinuousEmitters[i] != NULL ) {
  1098. ContinuousEmitters[i]->Set_Transform( muzzle );
  1099. }
  1100. }
  1101. if ( ContinuousSound != NULL ) {
  1102. ContinuousSound->Set_Transform( muzzle );
  1103. }
  1104. } else {
  1105. for ( i = 0; i < ContinuousEmitters.Length(); i++ ) {
  1106. if ( ContinuousEmitters[i] != NULL ) {
  1107. ContinuousEmitters[i]->Set_Transform( Get_Muzzle( i ) );
  1108. }
  1109. }
  1110. if ( ContinuousSound != NULL ) {
  1111. ContinuousSound->Set_Transform( Get_Muzzle() );
  1112. }
  1113. }
  1114. }
  1115. }
  1116. #define WEAPON_RATE_CHEAT 0
  1117. /*
  1118. ** Weapon State
  1119. */
  1120. void WeaponClass::Set_State( WeaponStateType new_state )
  1121. {
  1122. #define SWITCH_TIME 1.0f
  1123. #define RELOAD_TIME Definition->ReloadTime
  1124. #define READY_TIME 25.0f
  1125. // Debug_Say(( "Weapon switching from state %d to %d\n", State, new_state ));
  1126. State = new_state;
  1127. if ( State == STATE_START_SWITCH ) StateTimer = SWITCH_TIME/2.0f;
  1128. if ( State == STATE_END_SWITCH ) StateTimer = SWITCH_TIME/2.0f;
  1129. if ( State == STATE_RELOAD ) StateTimer = (float) RELOAD_TIME;
  1130. if ( State == STATE_READY ) StateTimer = READY_TIME; // delay til IDLE
  1131. if ( State == STATE_FIRE_PRIMARY ) StateTimer = 1.0f / PrimaryAmmoDefinition->RateOfFire;
  1132. if ( State == STATE_FIRE_SECONDARY )StateTimer = 1.0f / SecondaryAmmoDefinition->RateOfFire;
  1133. if ( State == STATE_CHARGE ) StateTimer = PrimaryAmmoDefinition->ChargeTime; // Charge Time
  1134. #if WEAPON_RATE_CHEAT
  1135. if ( Get_Owner() == COMBAT_STAR ) {
  1136. StateTimer *= 0.2f;
  1137. }
  1138. #endif
  1139. }
  1140. void WeaponClass::Update_State( float pending_time )
  1141. {
  1142. LockTriggers = false; // Unlock triggers
  1143. WWPROFILE( "Update weapon state" );
  1144. while ( pending_time > 0.0f ) {
  1145. bool trigger_ok = true;
  1146. // Only fire beacon if state is upright
  1147. if ( Get_Style() == WEAPON_HOLD_STYLE_BEACON &&
  1148. Get_Owner() && Get_Owner()->As_SoldierGameObj() &&
  1149. !Get_Owner()->As_SoldierGameObj()->Is_Upright() ) {
  1150. trigger_ok = false;
  1151. }
  1152. // If we can fire, start charging, it will fire and go back to READY
  1153. if ( (State == STATE_READY) || (State == STATE_IDLE) ) {
  1154. if ( (IsPrimaryTriggered || IsSecondaryTriggered) && (BurstCount != 0) && CombatManager::Is_Gameplay_Permitted() && trigger_ok ) {
  1155. if ( SafetySet ) {
  1156. LockTriggers = true;
  1157. } else {
  1158. if ( Is_Loaded() ) {
  1159. Set_State( STATE_CHARGE );
  1160. } else {
  1161. // We are not loaded, play the empty sound
  1162. #if 0
  1163. AudibleSoundClass * sound = WWAudioClass::Get_Instance()->Create_Sound( "Pistol_Empty_Click" );
  1164. if ( sound ) {
  1165. sound->Play();
  1166. sound->Release_Ref();
  1167. }
  1168. #else
  1169. if ( Definition->EmptySoundDefID != 0 ) {
  1170. EmptySoundTimer -= TimeManager::Get_Frame_Seconds();
  1171. if ( EmptySoundTimer <= 0 ) {
  1172. EmptySoundTimer = 0.3f;
  1173. Matrix3D muzzle = Get_Muzzle();
  1174. RefCountedGameObjReference *owner_ref = new RefCountedGameObjReference;
  1175. owner_ref->Set_Ptr( Get_Owner() );
  1176. AudibleSoundClass * sound = WWAudioClass::Get_Instance()->Create_Sound( Definition->EmptySoundDefID, owner_ref );
  1177. if ( sound != NULL ) {
  1178. sound->Set_Transform( muzzle );
  1179. sound->Attach_To_Object( Model );
  1180. sound->Add_To_Scene( true );
  1181. sound->Release_Ref();
  1182. }
  1183. REF_PTR_RELEASE( owner_ref );
  1184. }
  1185. }
  1186. #endif
  1187. }
  1188. }
  1189. }
  1190. // Force a reload when empty
  1191. if ( Is_Reload_Needed() ) {
  1192. Force_Reload();
  1193. }
  1194. }
  1195. float useable_time = min( StateTimer, pending_time );
  1196. StateTimer -= useable_time;
  1197. pending_time -= useable_time;
  1198. if ( StateTimer <= 0 ) { // Time for new state
  1199. switch ( State ) {
  1200. case STATE_IDLE:
  1201. StateTimer = 0;
  1202. pending_time = 0;
  1203. break;
  1204. case STATE_READY:
  1205. Set_State( STATE_IDLE );
  1206. break;
  1207. case STATE_CHARGE:
  1208. if ( IsPrimaryTriggered || !IsSecondaryTriggered ) {
  1209. Set_State( STATE_FIRE_PRIMARY );
  1210. } else {
  1211. Set_State( STATE_FIRE_SECONDARY );
  1212. }
  1213. if ( IsPrimaryTriggered || IsSecondaryTriggered ) {
  1214. if ( Is_Muzzle_Clear() ) {
  1215. Do_Fire( IsPrimaryTriggered );
  1216. DidFire = true;
  1217. }
  1218. }
  1219. break;
  1220. case STATE_FIRE_PRIMARY:
  1221. case STATE_FIRE_SECONDARY:
  1222. Set_State( STATE_READY );
  1223. break;
  1224. case STATE_RELOAD:
  1225. Do_Reload();
  1226. Set_State( STATE_READY );
  1227. break;
  1228. case STATE_START_SWITCH:
  1229. if ( UpdateModel == WEAPON_MODEL_UPDATE_WILL_BE_NEEDED ) {
  1230. UpdateModel = WEAPON_MODEL_UPDATE_IS_NEEDED;
  1231. }
  1232. Set_State( STATE_END_SWITCH );
  1233. break;
  1234. case STATE_END_SWITCH:
  1235. Set_State( STATE_IDLE );
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. }
  1241. void WeaponClass::Force_Reload( void )
  1242. {
  1243. // Stop reloading on last bullet to fire faster
  1244. // if ( Is_Reload_OK() && State != STATE_RELOAD ) {
  1245. if ( Is_Reload_OK() && State <= STATE_READY ) {
  1246. Set_State( STATE_RELOAD );
  1247. if ( Definition->ReloadSoundDefID != 0 ) {
  1248. // Make the reload sound
  1249. Matrix3D muzzle = Get_Muzzle();
  1250. RefCountedGameObjReference *owner_ref = new RefCountedGameObjReference;
  1251. owner_ref->Set_Ptr( Get_Owner() );
  1252. AudibleSoundClass * sound = WWAudioClass::Get_Instance()->Create_Sound( Definition->ReloadSoundDefID, owner_ref );
  1253. if ( sound != NULL ) {
  1254. sound->Set_Transform( muzzle );
  1255. sound->Attach_To_Object( Model );
  1256. sound->Add_To_Scene( true );
  1257. sound->Release_Ref();
  1258. }
  1259. REF_PTR_RELEASE( owner_ref );
  1260. }
  1261. }
  1262. }
  1263. /*
  1264. **
  1265. */
  1266. void WeaponClass::Update( void )
  1267. {
  1268. // Remove player weapons from vehicle drivers
  1269. if ( Get_Owner() &&
  1270. Get_Owner()->As_SoldierGameObj() &&
  1271. Get_Owner()->As_SoldierGameObj()->Is_In_Vehicle() ) {
  1272. IsPrimaryTriggered = false;
  1273. IsSecondaryTriggered = false;
  1274. }
  1275. if ( Get_Style() == WEAPON_HOLD_STYLE_C4 ||
  1276. Get_Style() == WEAPON_HOLD_STYLE_BEACON ||
  1277. Get_Can_Snipe() )
  1278. {
  1279. IsSecondaryTriggered = false;
  1280. }
  1281. WWASSERT( PrimaryAmmoDefinition != NULL );
  1282. // Update Burst logic
  1283. if ( (int)PrimaryAmmoDefinition->BurstMax == 0 ) { // if not using bursts
  1284. BurstCount = -1;
  1285. } else {
  1286. BurstDelayTimer -= TimeManager::Get_Frame_Seconds();
  1287. if ( (!IsPrimaryTriggered && !IsSecondaryTriggered) || ( BurstDelayTimer <= 0.0f ) ) {
  1288. BurstDelayTimer = PrimaryAmmoDefinition->BurstDelayTime * FreeRandom.Get_Float( 0.50f, 1.0f );
  1289. BurstCount = PrimaryAmmoDefinition->BurstMax;
  1290. }
  1291. }
  1292. if ( TimeManager::Get_Frame_Seconds() != 0 ) {
  1293. DidFire = false; // reset the did fire flag for this frame
  1294. }
  1295. Update_State( TimeManager::Get_Frame_Seconds() );
  1296. // If star in first person, force no muzzle flashes on 3rd person model
  1297. if ( (Get_Owner() == COMBAT_STAR) && CombatManager::Is_First_Person() ) {
  1298. Update_Muzzle_Flash( false, false );
  1299. } else {
  1300. Update_Muzzle_Flash( DidFire && (TotalRoundsFired & 1), DidFire && (~TotalRoundsFired & 1) );
  1301. }
  1302. if ( DidFire ) {
  1303. Do_Firing_Effects(); // Create muzzle flash and sounds
  1304. NextAnimState = (TotalRoundsFired & 1) ? WEAPON_ANIM_FIRING_0 : WEAPON_ANIM_FIRING_1;
  1305. } else {
  1306. NextAnimState = WEAPON_ANIM_NOT_FIRING;
  1307. }
  1308. //
  1309. // Remove any continuous effects if the trigger state has changed since last frame
  1310. //
  1311. if ( (IsPrimaryTriggered == false && LastFrameIsPrimaryTriggered == true) ||
  1312. (IsSecondaryTriggered == false && LastFrameIsSecondaryTriggered == true) )
  1313. {
  1314. Do_Continuous_Effects( false );
  1315. } else {
  1316. //
  1317. // Determine whether or not to trigger the continuous effects (emitters) for
  1318. // this weapon.
  1319. //
  1320. bool emitters_on = IsPrimaryTriggered || IsSecondaryTriggered;
  1321. if ( State == STATE_START_SWITCH || State == STATE_END_SWITCH ||
  1322. State == STATE_RELOAD || Get_Clip_Rounds() == 0 ) {
  1323. emitters_on = false;
  1324. }
  1325. Do_Continuous_Effects( emitters_on );
  1326. }
  1327. //
  1328. // Remember what our state last frame was...
  1329. //
  1330. LastFrameIsPrimaryTriggered = IsPrimaryTriggered;
  1331. LastFrameIsSecondaryTriggered = IsSecondaryTriggered;
  1332. return ;
  1333. }
  1334. /*
  1335. ** Simulate firing straight down the muzzle and see where in the world we hit
  1336. */
  1337. bool WeaponClass::Cast_Weapon_Down_Muzzle( Vector3 & hit_pos )
  1338. {
  1339. int muzzle_index = Get_Total_Rounds_Fired() & 1;
  1340. Matrix3D muzzle = Get_Muzzle( muzzle_index );
  1341. //
  1342. // Determine the start and end positions of the ray
  1343. //
  1344. Vector3 start = muzzle.Get_Translation();
  1345. Vector3 end = muzzle * Vector3( Get_Range(), 0, 0 );
  1346. LineSegClass ray( start, end );
  1347. //
  1348. // Cast the ray into the world
  1349. //
  1350. CastResultStruct res;
  1351. PhysRayCollisionTestClass raytest(ray,&res,BULLET_COLLISION_GROUP,COLLISION_TYPE_PROJECTILE);
  1352. Ignore_Owner();
  1353. { WWPROFILE( "Cast Ray" );
  1354. COMBAT_SCENE->Cast_Ray( raytest );
  1355. }
  1356. Unignore_Owner();
  1357. //
  1358. // Calculate where in the world this would hit
  1359. //
  1360. bool retval = false;
  1361. if ( res.StartBad == false ) {
  1362. hit_pos = start + (end * res.Fraction);
  1363. retval = true;
  1364. }
  1365. return retval;
  1366. }
  1367. /*
  1368. ** Simulate firing from muzzle to target, see where and if we hit
  1369. */
  1370. PhysicalGameObj * WeaponClass::Cast_Weapon( const Vector3 & target )
  1371. {
  1372. int muzzle_index = Get_Total_Rounds_Fired() & 1;
  1373. Matrix3D muzzle = Get_Muzzle( muzzle_index );
  1374. Vector3 start = muzzle.Get_Translation();
  1375. // Vector3 end = muzzle * Vector3( Get_Range(), 0, 0 );
  1376. Vector3 end = target;
  1377. LineSegClass ray( start, end );
  1378. CastResultStruct res;
  1379. PhysRayCollisionTestClass raytest(ray,&res,BULLET_COLLISION_GROUP,COLLISION_TYPE_PROJECTILE);
  1380. Ignore_Owner();
  1381. { WWPROFILE( "Cast Ray" );
  1382. COMBAT_SCENE->Cast_Ray( raytest );
  1383. }
  1384. Unignore_Owner();
  1385. // Debug_Say(( "Weapon Focus %f %f %f\n", bullet_hit_position->X, bullet_hit_position->Y, bullet_hit_position->Z ));
  1386. PhysicalGameObj * hit_obj = NULL;
  1387. if ( raytest.CollidedPhysObj ) {
  1388. if ( raytest.CollidedPhysObj->Get_Observer() != NULL ) {
  1389. hit_obj = ((CombatPhysObserverClass *)raytest.CollidedPhysObj->Get_Observer())->As_PhysicalGameObj();
  1390. }
  1391. }
  1392. return hit_obj;
  1393. }
  1394. /*
  1395. ** Check to see if this weapon can reload
  1396. */
  1397. bool
  1398. WeaponClass::Is_Reload_OK( void )
  1399. {
  1400. bool retval = (InventoryRounds != 0);
  1401. //
  1402. // Allow reloads if the infinite ammo cheat is enabled
  1403. //
  1404. if ( CheatMgrClass::Get_Instance () != NULL &&
  1405. CheatMgrClass::Get_Instance ()->Is_Cheat_Enabled( CheatMgrClass::CHEAT_INFINITE_AMMO ) && Owner == COMBAT_STAR )
  1406. {
  1407. retval = true;
  1408. }
  1409. return retval;
  1410. }
  1411. void WeaponClass::Stop_Firing_Sound( void )
  1412. {
  1413. if ( FiringSound == NULL ) {
  1414. return ;
  1415. }
  1416. //
  1417. // Stop the sound
  1418. //
  1419. FiringSound->Stop ();
  1420. FiringSound->Remove_From_Scene ();
  1421. REF_PTR_RELEASE (FiringSound);
  1422. return ;
  1423. }
  1424. // This function calcuates the weapon information that must be displayed for the star's
  1425. // weapon in the hud reticle & sniper view. This should only be called for the weapon
  1426. // we want to show a reticle for.
  1427. // The goal is to find the HitPosition, and HitPositionHot
  1428. void WeaponClass::Display_Targeting( void )
  1429. {
  1430. WWPROFILE( "Cast_Star_Weapon" );
  1431. int muzzle_index = Get_Total_Rounds_Fired() & 1;
  1432. // vehicles that don't have a turret bone and do have homing weapons target from the camera
  1433. Matrix3D muzzle;
  1434. VehicleGameObj * vehicle = Get_Owner()->As_VehicleGameObj();;
  1435. if (vehicle && (vehicle->Has_Turret() == false) && (PrimaryAmmoDefinition->IsTracking)) {
  1436. muzzle = COMBAT_CAMERA->Get_Transform();
  1437. muzzle.Rotate_Z( DEG_TO_RADF(90.0) );
  1438. muzzle.Rotate_Y( DEG_TO_RADF(90.0) );
  1439. } else {
  1440. muzzle = Get_Muzzle( muzzle_index );
  1441. }
  1442. // Cast a ray to see what the weapon is pointing at
  1443. Vector3 start = muzzle.Get_Translation();
  1444. Vector3 end = muzzle * Vector3( Get_Range(), 0, 0 );
  1445. LineSegClass ray( start, end );
  1446. CastResultStruct res;
  1447. PhysRayCollisionTestClass raytest(ray,&res,BULLET_COLLISION_GROUP,COLLISION_TYPE_PROJECTILE);
  1448. Ignore_Owner();
  1449. {
  1450. WWPROFILE( "Cast_Ray" );
  1451. COMBAT_SCENE->Cast_Ray( raytest );
  1452. }
  1453. Unignore_Owner();
  1454. Vector3 pos;
  1455. ray.Compute_Point( raytest.Result->Fraction, &pos );
  1456. HUDInfo::Set_Weapon_Target_Position( pos );
  1457. // Debug_Say(( "Weapon Focus %f %f %f\n", bullet_hit_position->X, bullet_hit_position->Y, bullet_hit_position->Z ));
  1458. if ( raytest.CollidedPhysObj && raytest.CollidedPhysObj->Get_Observer() != NULL ) {
  1459. DamageableGameObj * obj = ((CombatPhysObserverClass *)raytest.CollidedPhysObj->Get_Observer())->As_DamageableGameObj();
  1460. if ( obj->Is_Targetable() == false ) {
  1461. obj = NULL;
  1462. }
  1463. // if stealthed and enemy, it's not targetable
  1464. if ( obj && obj->As_SmartGameObj() && obj->As_SmartGameObj()->Is_Stealthed() &&
  1465. COMBAT_STAR && obj->Is_Enemy( COMBAT_STAR ) ) {
  1466. obj = NULL;
  1467. }
  1468. HUDInfo::Set_Weapon_Target_Object( obj );
  1469. // We have a problem here with a building here clearing the PT, so dont set
  1470. // buildings here, assume they will be set from the ccamera
  1471. // Mo, try pulling back the cast above
  1472. if ( obj == NULL || obj->As_BuildingGameObj() == NULL ) {
  1473. HUDInfo::Set_Info_Object( obj );
  1474. }
  1475. }
  1476. }
  1477. #if 0
  1478. // Aquire
  1479. if ( AmmoDefinition->AquireTime != 0.0f ) {
  1480. SmartGameObj *target = weapon->Pointing_At_Target();
  1481. if ( target ) {
  1482. if ( AquireTarget != target ) { // locking onto someone new
  1483. AquireTarget = target;
  1484. AquireTimer = 0.0f;
  1485. }
  1486. AquireTimer += TimeManager::Get_Frame_Seconds(); // Bump the timer forward in seconds
  1487. if (AquireTimer >= AmmoDefinition->AquireTime) {
  1488. AquireTimer = AmmoDefinition->AquireTime;
  1489. }
  1490. } else {
  1491. AquireTimer -= TimeManager::Get_Frame_Seconds(); // Bump the timer backwards in seconds
  1492. if ( AquireTimer < 0.0f ) {
  1493. AquireTimer = 0.0f;
  1494. }
  1495. }
  1496. IsAquired = (target != NULL) &&
  1497. (AquireTimer >= AmmoDefinition->AquireTime);
  1498. }
  1499. #endif
  1500. const Matrix3D & WeaponClass::Get_Muzzle( int index )
  1501. {
  1502. WWASSERT( Get_Owner() != NULL );
  1503. // Star's weapon in first person fires from the camera
  1504. if ( Get_Owner() == COMBAT_STAR && CombatManager::Is_First_Person() ) {
  1505. static Matrix3D _muzzle;
  1506. _muzzle.Obj_Look_At( COMBAT_CAMERA->Get_Transform().Get_Translation(), Get_Owner()->Get_Targeting_Pos(), 0 );
  1507. return _muzzle;
  1508. }
  1509. return Get_Owner()->Get_Muzzle( index );
  1510. }
  1511. void WeaponClass::Ignore_Owner( void )
  1512. {
  1513. if ( Get_Owner() != NULL ) {
  1514. if ( Get_Owner()->Peek_Physical_Object() != NULL ) {
  1515. Get_Owner()->Peek_Physical_Object()->Inc_Ignore_Counter();
  1516. }
  1517. // If Owner is a vehicle, inc all the occupants
  1518. if ( Get_Owner()->As_VehicleGameObj() != NULL ) {
  1519. Get_Owner()->As_VehicleGameObj()->Ignore_Occupants();
  1520. }
  1521. }
  1522. }
  1523. void WeaponClass::Unignore_Owner( void )
  1524. {
  1525. if ( Get_Owner() != NULL ) {
  1526. if ( Get_Owner()->Peek_Physical_Object() != NULL ) {
  1527. Get_Owner()->Peek_Physical_Object()->Dec_Ignore_Counter();
  1528. }
  1529. if ( Get_Owner()->As_VehicleGameObj() != NULL ) {
  1530. Get_Owner()->As_VehicleGameObj()->Unignore_Occupants();
  1531. }
  1532. }
  1533. }
  1534. /*
  1535. **
  1536. */
  1537. MuzzleFlashClass::MuzzleFlashClass( void ) :
  1538. MuzzleA0Bone( 0 ),
  1539. MuzzleA1Bone( 0 ),
  1540. Rotation( 0 ),
  1541. Model( 0 ),
  1542. LastFlashA0( true ),
  1543. LastFlashA1( true )
  1544. {
  1545. }
  1546. MuzzleFlashClass::~MuzzleFlashClass( void )
  1547. {
  1548. REF_PTR_RELEASE( Model );
  1549. }
  1550. void MuzzleFlashClass::Init( RenderObjClass * model )
  1551. {
  1552. REF_PTR_SET( Model, model );
  1553. if ( model != NULL ) {
  1554. // Find the muzzle bone ( for rotation )
  1555. MuzzleA0Bone = model->Get_Bone_Index( "muzzlea0" );
  1556. MuzzleA1Bone = model->Get_Bone_Index( "muzzlea1" );
  1557. if ( MuzzleA0Bone > 0 ) {
  1558. // model->Capture_Bone( MuzzleA0Bone );
  1559. }
  1560. if ( MuzzleA1Bone > 0 ) {
  1561. // model->Capture_Bone( MuzzleA1Bone );
  1562. }
  1563. Update( false, false );
  1564. }
  1565. }
  1566. void MuzzleFlashClass::Update( bool flashA0, bool flashA1 )
  1567. {
  1568. if ( MuzzleA1Bone == 0 ) {
  1569. flashA0 = flashA0 | flashA1;
  1570. flashA1 = false;
  1571. }
  1572. if ( Model != NULL ) {
  1573. if (( MuzzleA0Bone > 0 ) && (LastFlashA0 != flashA0)) {
  1574. LastFlashA0 = flashA0;
  1575. #if 0
  1576. if ( flash ) {
  1577. Rotation += TimeManager::Get_Frame_Seconds() * 10;
  1578. Matrix3D tm(1);
  1579. tm.Rotate_X( Rotation );
  1580. Model->Control_Bone( MuzzleA0Bone, tm );
  1581. }
  1582. #endif
  1583. for (int i=0; i<Model->Get_Num_Sub_Objects_On_Bone( MuzzleA0Bone ); i++) {
  1584. RenderObjClass * robj = Model->Get_Sub_Object_On_Bone(i, MuzzleA0Bone );
  1585. // hide all meshes named "muzzleflash"
  1586. if (strstr(robj->Get_Name(),"MUZZLEFLASH") || strstr(robj->Get_Name(),"MZ")) {
  1587. robj->Set_Hidden( !flashA0 );
  1588. }
  1589. robj->Release_Ref();
  1590. }
  1591. }
  1592. if (( MuzzleA1Bone > 0 ) && (LastFlashA1 != flashA1)) {
  1593. LastFlashA1 = flashA1;
  1594. #if 0
  1595. if ( flash ) {
  1596. Rotation += TimeManager::Get_Frame_Seconds() * 10;
  1597. Matrix3D tm(1);
  1598. tm.Rotate_X( Rotation );
  1599. Model->Control_Bone( MuzzleA1Bone, tm );
  1600. }
  1601. #endif
  1602. for (int i=0; i<Model->Get_Num_Sub_Objects_On_Bone( MuzzleA1Bone ); i++) {
  1603. RenderObjClass * robj = Model->Get_Sub_Object_On_Bone(i, MuzzleA1Bone );
  1604. // hide all meshes named "muzzleflash"
  1605. if (strstr(robj->Get_Name(),"MUZZLEFLASH") || strstr(robj->Get_Name(),"MZ")) {
  1606. robj->Set_Hidden( !flashA1 );
  1607. }
  1608. robj->Release_Ref();
  1609. }
  1610. }
  1611. }
  1612. }