weaponbag.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/weaponbag.cpp $*
  25. * *
  26. * $Author:: Tom_s $*
  27. * *
  28. * $Modtime:: 1/02/02 4:21p $*
  29. * *
  30. * $Revision:: 55 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "weaponbag.h"
  36. #include "weapons.h"
  37. #include "debug.h"
  38. #include "weaponmanager.h"
  39. #include "wwpacket.h"
  40. #include "armedgameobj.h"
  41. #include "inventory.h"
  42. /*
  43. **
  44. */
  45. WeaponBagClass::WeaponBagClass( ArmedGameObj * owner ) :
  46. Owner( owner ),
  47. WeaponIndex( 0 ),
  48. IsChanged( true ),
  49. HUDIsChanged( true )
  50. {
  51. Mark_Owner_Dirty();
  52. WeaponList.Add( NULL ); // Index 0 is no weapon
  53. }
  54. WeaponBagClass::~WeaponBagClass( void )
  55. {
  56. while ( WeaponList.Count() ) {
  57. delete WeaponList[0];
  58. WeaponList.Delete( 0 );
  59. }
  60. }
  61. /*
  62. ** WeaponBagClass Save and Load
  63. */
  64. enum {
  65. CHUNKID_VARIABLES = 921991503,
  66. CHUNKID_WEAPON_LIST,
  67. CHUNKID_WEAPON_ENTRY,
  68. MICROCHUNKID_WEAPON_INDEX = 1,
  69. };
  70. bool WeaponBagClass::Save( ChunkSaveClass & csave )
  71. {
  72. int i;
  73. csave.Begin_Chunk( CHUNKID_VARIABLES );
  74. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_WEAPON_INDEX, WeaponIndex );
  75. csave.End_Chunk();
  76. csave.Begin_Chunk( CHUNKID_WEAPON_LIST );
  77. for( i = 1; i < WeaponList.Count(); i++ ) {
  78. csave.Begin_Chunk( CHUNKID_WEAPON_ENTRY );
  79. WeaponList[i]->Save( csave );
  80. csave.End_Chunk();
  81. }
  82. csave.End_Chunk();
  83. // Don't need to save Owner or IsChanged;
  84. return true;
  85. }
  86. bool WeaponBagClass::Load( ChunkLoadClass &cload )
  87. {
  88. while (cload.Open_Chunk()) {
  89. switch(cload.Cur_Chunk_ID()) {
  90. case CHUNKID_VARIABLES:
  91. while (cload.Open_Micro_Chunk()) {
  92. switch(cload.Cur_Micro_Chunk_ID()) {
  93. READ_MICRO_CHUNK( cload, MICROCHUNKID_WEAPON_INDEX, WeaponIndex );
  94. default:
  95. Debug_Say(("Unhandled Micro Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  96. break;
  97. }
  98. cload.Close_Micro_Chunk();
  99. }
  100. break;
  101. case CHUNKID_WEAPON_LIST:
  102. WWASSERT( WeaponList.Count() == 1 );
  103. while (cload.Open_Chunk()) {
  104. WWASSERT( cload.Cur_Chunk_ID() == CHUNKID_WEAPON_ENTRY );
  105. WeaponClass *weapon = new WeaponClass;
  106. weapon->Load( cload );
  107. WeaponList.Add( weapon );
  108. cload.Close_Chunk();
  109. }
  110. break;
  111. default:
  112. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  113. break;
  114. }
  115. cload.Close_Chunk();
  116. }
  117. IsChanged = true;
  118. HUDIsChanged = true;
  119. return true;
  120. }
  121. /*
  122. **
  123. */
  124. bool WeaponBagClass::Is_Weapon_Owned( int weapon_id )
  125. {
  126. WeaponClass * weapon = Find_Weapon( WeaponManager::Find_Weapon_Definition( weapon_id ) );
  127. return ( weapon != NULL && weapon->Does_Weapon_Exist() );
  128. }
  129. bool WeaponBagClass::Is_Ammo_Full( int weapon_id )
  130. {
  131. WeaponClass * weapon = Find_Weapon( WeaponManager::Find_Weapon_Definition( weapon_id ) );
  132. return weapon && weapon->Is_Ammo_Maxed();
  133. }
  134. /*
  135. **
  136. */
  137. void WeaponBagClass::Remove_Weapon( int index )
  138. {
  139. //
  140. // Simply destroy the weapon if its in our list
  141. //
  142. if ( index >= 0 && index < WeaponList.Count() ) {
  143. delete WeaponList[index];
  144. WeaponList.Delete( index );
  145. }
  146. return ;
  147. }
  148. /*
  149. **
  150. */
  151. void WeaponBagClass::Clear_Weapons( void )
  152. {
  153. // find the next existing weapons
  154. while ( WeaponList.Count() > 1 ) {
  155. int index = WeaponList.Count()-1;
  156. delete WeaponList[index];
  157. WeaponList.Delete( index );
  158. }
  159. Select_Index( 0 );
  160. return ;
  161. }
  162. /*
  163. **
  164. */
  165. WeaponClass * WeaponBagClass::Add_Weapon( const WeaponDefinitionClass * def, int rounds, bool give_weapon )
  166. {
  167. if ( def == NULL ) {
  168. Debug_Say(( "Failed to create Weapon\n" ));
  169. return NULL;
  170. }
  171. WeaponClass * weapon = Find_Weapon( def );
  172. if ( weapon == NULL ) {
  173. // Debug_Say(( "Adding new weapon %s\n", weapon->Get_Name() ));
  174. weapon = new WeaponClass( def );
  175. if ( weapon ) {
  176. weapon->Set_Owner( Owner );
  177. weapon->Add_Rounds( rounds );
  178. weapon->Set_Weapon_Exists( give_weapon ); // Assume it doesn't exist
  179. // add this weapon to the weapon list in order (0 at end)
  180. int index;
  181. for ( index = 1; index < WeaponList.Count(); index++ ) {
  182. if ( WeaponList[ index ]->Get_Key_Number() > weapon->Get_Key_Number() ) {
  183. break;
  184. }
  185. }
  186. if ( WeaponIndex >= index ) {
  187. WeaponIndex++;
  188. }
  189. WeaponList.Insert( index, weapon );
  190. IsChanged = true;
  191. HUDIsChanged = true;
  192. #if 0 // Don't auto select
  193. bool use = true;
  194. for ( int i = 1; i < WeaponList.Count(); i++ ) {
  195. if ( i != index && WeaponList[ i ]->Does_Weapon_Exist() ) {
  196. use = false;
  197. }
  198. }
  199. // If no current weapon, make this weapon current
  200. if ( give_weapon && use ) {
  201. // Debug_Say(( "Commando selecting first weapon %s\n", weapon->Get_Name() ));
  202. Select_Index( index );
  203. }
  204. #endif
  205. Mark_Owner_Dirty();
  206. }
  207. } else {
  208. if ( give_weapon ) { // Make sure we have it if we are supposed to
  209. weapon->Set_Weapon_Exists( true );
  210. Mark_Owner_Dirty();
  211. }
  212. weapon->Add_Rounds( rounds ); // if we already have this weapon, just take the ammo
  213. if ( rounds != 0 ) {
  214. Mark_Owner_Dirty();
  215. }
  216. }
  217. return weapon;
  218. }
  219. WeaponClass * WeaponBagClass::Add_Weapon( const char *weapon_name, int rounds, bool give_weapon )
  220. {
  221. return Add_Weapon( WeaponManager::Find_Weapon_Definition( weapon_name ), rounds, give_weapon );
  222. }
  223. WeaponClass * WeaponBagClass::Add_Weapon( int id, int rounds, bool give_weapon )
  224. {
  225. return Add_Weapon( WeaponManager::Find_Weapon_Definition( id ), rounds, give_weapon );
  226. }
  227. WeaponClass * WeaponBagClass::Get_Next_Weapon( void )
  228. {
  229. // find the next existing weapons
  230. for ( int i = 0; i < WeaponList.Count(); i++ ) {
  231. int index = ( WeaponIndex + i + 1 ) % WeaponList.Count();
  232. // BMG remove no weapon slot
  233. // if ( WeaponList[ index ] == NULL || WeaponList[ index ]->Does_Weapon_Exist() ) {
  234. if ( WeaponList[ index ] != NULL && WeaponList[ index ]->Does_Weapon_Exist() ) {
  235. return WeaponList[ index ];
  236. }
  237. }
  238. return NULL;
  239. }
  240. void WeaponBagClass::Select_Next( void )
  241. {
  242. // Find the next existing weapons
  243. for ( int i = 1; i < WeaponList.Count(); i++ ) {
  244. int index = ( WeaponIndex + i ) % WeaponList.Count();
  245. // BMG remove no weapon slot
  246. // if ( WeaponList[ index ] == NULL || WeaponList[ index ]->Does_Weapon_Exist() ) {
  247. if ( WeaponList[ index ] != NULL && WeaponList[ index ]->Does_Weapon_Exist() ) {
  248. Select_Index( index );
  249. break;
  250. }
  251. }
  252. }
  253. void WeaponBagClass::Select_Prev( void )
  254. {
  255. // Find the next existing weapons
  256. for ( int i = 1; i < WeaponList.Count(); i++ ) {
  257. int index = ( WeaponIndex - i + WeaponList.Count()) % WeaponList.Count();
  258. // BMG remove no weapon slot
  259. // if ( WeaponList[ index ] == NULL || WeaponList[ index ]->Does_Weapon_Exist() ) {
  260. if ( WeaponList[ index ] != NULL && WeaponList[ index ]->Does_Weapon_Exist() ) {
  261. Select_Index( index );
  262. break;
  263. }
  264. }
  265. }
  266. void WeaponBagClass::Select_Key_Number( int key_number )
  267. {
  268. if ( key_number == -1 ) {
  269. Select_Index( 0 );
  270. } else {
  271. // Start from the current, find the next existing weapon with the right key_number
  272. for ( int i = 1; i < WeaponList.Count(); i++ ) {
  273. int index = ( WeaponIndex + i ) % WeaponList.Count();
  274. if ( WeaponList[ index ] != NULL &&
  275. WeaponList[ index ]->Does_Weapon_Exist() &&
  276. (int)WeaponList[ index ]->Get_Key_Number() == key_number ) {
  277. Select_Index( index );
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. void WeaponBagClass::Select_Weapon_ID( int weapon_id )
  284. {
  285. for( int i = 1; i < WeaponList.Count(); i++ ) {
  286. if ( WeaponList[i]->Get_ID() == weapon_id ) {
  287. Select_Index ( i );
  288. return;
  289. }
  290. }
  291. }
  292. void WeaponBagClass::Select_Weapon_Name( const char * name )
  293. {
  294. if ( ( name == NULL ) || ( *name == 0 ) ) {
  295. Select_Index ( 0 );
  296. } else {
  297. for( int i = 1; i < WeaponList.Count(); i++ ) {
  298. if ( strcmp( WeaponList[i]->Get_Name(), name ) == 0 ) {
  299. Select_Index ( i );
  300. return;
  301. }
  302. }
  303. Debug_Say(( "Unable to select weapon %s\n", name ));
  304. }
  305. }
  306. void WeaponBagClass::Select_Weapon( WeaponClass * weapon )
  307. {
  308. for( int i = 1; i < WeaponList.Count(); i++ ) {
  309. if ( WeaponList[i] == weapon ) {
  310. Select_Index ( i );
  311. return;
  312. }
  313. }
  314. }
  315. void WeaponBagClass::Select_Index( int index )
  316. {
  317. if ( index >= WeaponList.Count() ) {
  318. index = 0;
  319. }
  320. if ( WeaponIndex != index ) {
  321. if ( Get_Weapon() ) {
  322. Get_Weapon()->Deselect();
  323. }
  324. WeaponIndex = index;
  325. IsChanged = true;
  326. HUDIsChanged = true;
  327. if ( Get_Weapon() ) {
  328. Get_Weapon()->Select();
  329. }
  330. Mark_Owner_Dirty();
  331. }
  332. }
  333. void WeaponBagClass::Deselect( void )
  334. {
  335. // Need to be able to tell client to put weapon away
  336. Select_Index( 0 );
  337. }
  338. //-----------------------------------------------------------------------------
  339. void WeaponBagClass::Import_Weapon_List(BitStreamClass & packet)
  340. {
  341. int weapon_count = packet.Get(weapon_count);
  342. int weapon_id;
  343. for (int weapon = 0; weapon < weapon_count; weapon++) {
  344. weapon_id = packet.Get(weapon_id);
  345. int total_rounds = packet.Get(total_rounds);
  346. Add_Weapon(weapon_id, 0);
  347. WeaponClass * weapon = NULL;
  348. for( int i = 1; i < WeaponList.Count(); i++ ) {
  349. if ( (int) WeaponList[i]->Get_Definition()->Get_ID() == weapon_id ) {
  350. weapon = WeaponList[i];
  351. }
  352. }
  353. if ( weapon != NULL ) {
  354. weapon->Set_Total_Rounds( total_rounds );
  355. }
  356. }
  357. }
  358. //-----------------------------------------------------------------------------
  359. void WeaponBagClass::Export_Weapon_List(BitStreamClass & packet)
  360. {
  361. packet.Add((int) (WeaponList.Count() - 1));
  362. for( int i = 1; i < WeaponList.Count(); i++ ) {
  363. packet.Add(WeaponList[i]->Get_ID());
  364. packet.Add(WeaponList[i]->Get_Total_Rounds());
  365. }
  366. }
  367. //-----------------------------------------------------------------------------
  368. WeaponClass * WeaponBagClass::Find_Weapon( const WeaponDefinitionClass * def )
  369. {
  370. if ( def == NULL ) {
  371. return NULL;
  372. }
  373. for( int i = 1; i < WeaponList.Count(); i++ ) {
  374. if ( (unsigned)WeaponList[i]->Get_ID() == def->Get_ID() ) {
  375. return WeaponList[i];
  376. }
  377. }
  378. // Debug_Say(( "Didn't Find weapon %s\n", name ));
  379. return NULL;
  380. }
  381. /*
  382. **
  383. */
  384. bool WeaponBagClass::Move_Contents( WeaponBagClass * source )
  385. {
  386. // Was anyhting actually moved?
  387. bool moved = false;
  388. // Move all the weapons and ammo from the source to me
  389. // For each weapon in the source bag...
  390. for( int i = 1; i < source->WeaponList.Count(); i++ ) {
  391. WeaponClass * weapon = source->WeaponList[i];
  392. // If I already have it,
  393. WeaponClass * my_weapon = Find_Weapon( weapon->Get_Definition() );
  394. if ( my_weapon ) {
  395. // Copy the ammo and the weapon
  396. if ( !my_weapon->Is_Ammo_Maxed() && weapon->Get_Total_Rounds() != 0 ) {
  397. my_weapon->Add_Rounds( weapon->Get_Total_Rounds() );
  398. moved = true;
  399. }
  400. if ( weapon->Does_Weapon_Exist() && !my_weapon->Does_Weapon_Exist() ) {
  401. my_weapon->Set_Weapon_Exists( true );
  402. moved = true;
  403. }
  404. // Debug_Say(( "Add %s %d\n", weapon->Get_Definition()->Get_Name(), weapon->Get_Total_Rounds() ));
  405. } else {
  406. // else, give it to me
  407. Add_Weapon( weapon->Get_Definition(), weapon->Get_Total_Rounds(), weapon->Does_Weapon_Exist() );
  408. if ( weapon->Does_Weapon_Exist() ) {
  409. moved = true;
  410. }
  411. // Debug_Say(( "Give %s %d\n", weapon->Get_Definition()->Get_Name(), weapon->Get_Total_Rounds() ));
  412. }
  413. }
  414. Mark_Owner_Dirty();
  415. return moved;
  416. }
  417. /*
  418. **
  419. */
  420. void WeaponBagClass::Store_Inventory( InventoryClass * inventory )
  421. {
  422. for( int i = 1; i < WeaponList.Count(); i++ ) {
  423. WeaponClass * weapon = WeaponList[i];
  424. inventory->Add_Weapon( weapon->Get_ID(), weapon->Get_Total_Rounds(), weapon->Does_Weapon_Exist() );
  425. }
  426. }
  427. /*
  428. **
  429. */
  430. void WeaponBagClass::Mark_Owner_Dirty( void )
  431. {
  432. if ( Owner != NULL ) {
  433. Owner->Set_Object_Dirty_Bit( NetworkObjectClass::BIT_OCCASIONAL, true );
  434. }
  435. }