Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
7.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Weapons Resource implementation
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #include "cbase.h"
  10. #include "history_resource.h"
  11. #include <vgui_controls/Controls.h>
  12. #include <vgui/ISurface.h>
  13. #include "c_baseplayer.h"
  14. #include "hud.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. WeaponsResource gWR;
  18. void FreeHudTextureList( CUtlDict< CHudTexture *, int >& list );
  19. static CHudTexture *FindHudTextureInDict( CUtlDict< CHudTexture *, int >& list, const char *psz )
  20. {
  21. int idx = list.Find( psz );
  22. if ( idx == list.InvalidIndex() )
  23. return NULL;
  24. return list[ idx ];
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. // Output :
  29. //-----------------------------------------------------------------------------
  30. WeaponsResource::WeaponsResource( void )
  31. {
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Purpose:
  35. // Output :
  36. //-----------------------------------------------------------------------------
  37. WeaponsResource::~WeaponsResource( void )
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Purpose:
  42. //-----------------------------------------------------------------------------
  43. void WeaponsResource::Init( void )
  44. {
  45. Reset();
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose:
  49. //-----------------------------------------------------------------------------
  50. void WeaponsResource::Reset( void )
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose: Load all the sprites needed for all registered weapons
  55. //-----------------------------------------------------------------------------
  56. void WeaponsResource::LoadAllWeaponSprites( void )
  57. {
  58. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  59. if ( !player )
  60. return;
  61. for (int i = 0; i < MAX_WEAPONS; i++)
  62. {
  63. if ( player->GetWeapon(i) )
  64. {
  65. LoadWeaponSprites( player->GetWeapon(i)->GetWeaponFileInfoHandle() );
  66. }
  67. }
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose:
  71. //-----------------------------------------------------------------------------
  72. void WeaponsResource::LoadWeaponSprites( WEAPON_FILE_INFO_HANDLE hWeaponFileInfo )
  73. {
  74. // WeaponsResource is a friend of C_BaseCombatWeapon
  75. FileWeaponInfo_t *pWeaponInfo = GetFileWeaponInfoFromHandle( hWeaponFileInfo );
  76. if ( !pWeaponInfo )
  77. return;
  78. // Already parsed the hud elements?
  79. if ( pWeaponInfo->bLoadedHudElements )
  80. return;
  81. pWeaponInfo->bLoadedHudElements = true;
  82. pWeaponInfo->iconActive = NULL;
  83. pWeaponInfo->iconInactive = NULL;
  84. pWeaponInfo->iconAmmo = NULL;
  85. pWeaponInfo->iconAmmo2 = NULL;
  86. pWeaponInfo->iconCrosshair = NULL;
  87. pWeaponInfo->iconAutoaim = NULL;
  88. pWeaponInfo->iconSmall = NULL;
  89. char sz[128];
  90. Q_snprintf(sz, sizeof( sz ), "scripts/%s", pWeaponInfo->szClassName);
  91. CUtlDict< CHudTexture *, int > tempList;
  92. LoadHudTextures( tempList, sz, g_pGameRules->GetEncryptionKey() );
  93. if ( !tempList.Count() )
  94. {
  95. // no sprite description file for weapon, use default small blocks
  96. pWeaponInfo->iconActive = HudIcons().GetIcon( "selection" );
  97. pWeaponInfo->iconInactive = HudIcons().GetIcon( "selection" );
  98. pWeaponInfo->iconAmmo = HudIcons().GetIcon( "bucket1" );
  99. return;
  100. }
  101. CHudTexture *p;
  102. p = FindHudTextureInDict( tempList, "crosshair" );
  103. if ( p )
  104. {
  105. pWeaponInfo->iconCrosshair = HudIcons().AddUnsearchableHudIconToList( *p );
  106. }
  107. p = FindHudTextureInDict( tempList, "autoaim" );
  108. if ( p )
  109. {
  110. pWeaponInfo->iconAutoaim = HudIcons().AddUnsearchableHudIconToList( *p );
  111. }
  112. p = FindHudTextureInDict( tempList, "zoom" );
  113. if ( p )
  114. {
  115. pWeaponInfo->iconZoomedCrosshair = HudIcons().AddUnsearchableHudIconToList( *p );
  116. }
  117. else
  118. {
  119. pWeaponInfo->iconZoomedCrosshair = pWeaponInfo->iconCrosshair; //default to non-zoomed crosshair
  120. }
  121. p = FindHudTextureInDict( tempList, "zoom_autoaim" );
  122. if ( p )
  123. {
  124. pWeaponInfo->iconZoomedAutoaim = HudIcons().AddUnsearchableHudIconToList( *p );
  125. }
  126. else
  127. {
  128. pWeaponInfo->iconZoomedAutoaim = pWeaponInfo->iconZoomedCrosshair; //default to zoomed crosshair
  129. }
  130. p = FindHudTextureInDict( tempList, "weapon" );
  131. if ( p )
  132. {
  133. pWeaponInfo->iconInactive = HudIcons().AddUnsearchableHudIconToList( *p );
  134. if ( pWeaponInfo->iconInactive )
  135. {
  136. pWeaponInfo->iconInactive->Precache();
  137. }
  138. }
  139. p = FindHudTextureInDict( tempList, "weapon_s" );
  140. if ( p )
  141. {
  142. pWeaponInfo->iconActive = HudIcons().AddUnsearchableHudIconToList( *p );
  143. if ( pWeaponInfo->iconActive )
  144. {
  145. pWeaponInfo->iconActive->Precache();
  146. }
  147. }
  148. p = FindHudTextureInDict( tempList, "weapon_small" );
  149. if ( p )
  150. {
  151. pWeaponInfo->iconSmall = HudIcons().AddUnsearchableHudIconToList( *p );
  152. if ( pWeaponInfo->iconSmall )
  153. {
  154. pWeaponInfo->iconSmall->Precache();
  155. }
  156. }
  157. p = FindHudTextureInDict( tempList, "ammo" );
  158. if ( p )
  159. {
  160. pWeaponInfo->iconAmmo = HudIcons().AddUnsearchableHudIconToList( *p );
  161. if ( pWeaponInfo->iconAmmo )
  162. {
  163. pWeaponInfo->iconAmmo->Precache();
  164. }
  165. }
  166. p = FindHudTextureInDict( tempList, "ammo2" );
  167. if ( p )
  168. {
  169. pWeaponInfo->iconAmmo2 = HudIcons().AddUnsearchableHudIconToList( *p );
  170. if ( pWeaponInfo->iconAmmo2 )
  171. {
  172. pWeaponInfo->iconAmmo2->Precache();
  173. }
  174. }
  175. #if !defined( CSTRIKE15 )
  176. // Blast data into all of the Huds
  177. for ( int hh = 0; hh < MAX_SPLITSCREEN_PLAYERS; ++hh )
  178. {
  179. ACTIVE_SPLITSCREEN_PLAYER_GUARD( hh );
  180. CHudHistoryResource *pHudHR = GET_HUDELEMENT( CHudHistoryResource );
  181. if( !pHudHR )
  182. continue;
  183. if ( pWeaponInfo->iconInactive )
  184. {
  185. pHudHR->SetHistoryGap( pWeaponInfo->iconInactive->Height() );
  186. }
  187. if ( pWeaponInfo->iconActive )
  188. {
  189. pHudHR->SetHistoryGap( pWeaponInfo->iconActive->Height() );
  190. }
  191. if ( pWeaponInfo->iconAmmo )
  192. {
  193. pHudHR->SetHistoryGap( pWeaponInfo->iconAmmo->Height() );
  194. }
  195. if ( pWeaponInfo->iconAmmo2 )
  196. {
  197. pHudHR->SetHistoryGap( pWeaponInfo->iconAmmo2->Height() );
  198. }
  199. }
  200. #endif // !CSTRIKE15
  201. FreeHudTextureList( tempList );
  202. }
  203. //-----------------------------------------------------------------------------
  204. // Purpose: Helper function to return a Ammo pointer from id
  205. //-----------------------------------------------------------------------------
  206. CHudTexture *WeaponsResource::GetAmmoIconFromWeapon( int iAmmoId )
  207. {
  208. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  209. if ( !player )
  210. return NULL;
  211. for ( int i = 0; i < MAX_WEAPONS; i++ )
  212. {
  213. C_BaseCombatWeapon *weapon = player->GetWeapon( i );
  214. if ( !weapon )
  215. continue;
  216. if ( weapon->GetPrimaryAmmoType() == iAmmoId )
  217. {
  218. return weapon->GetWpnData().iconAmmo;
  219. }
  220. else if ( weapon->GetSecondaryAmmoType() == iAmmoId )
  221. {
  222. return weapon->GetWpnData().iconAmmo2;
  223. }
  224. }
  225. return NULL;
  226. }
  227. //-----------------------------------------------------------------------------
  228. // Purpose: Get a pointer to a weapon using this ammo
  229. //-----------------------------------------------------------------------------
  230. const FileWeaponInfo_t *WeaponsResource::GetWeaponFromAmmo( int iAmmoId )
  231. {
  232. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  233. if ( !player )
  234. return NULL;
  235. for ( int i = 0; i < MAX_WEAPONS; i++ )
  236. {
  237. C_BaseCombatWeapon *weapon = player->GetWeapon( i );
  238. if ( !weapon )
  239. continue;
  240. if ( weapon->GetPrimaryAmmoType() == iAmmoId )
  241. {
  242. return &weapon->GetWpnData();
  243. }
  244. else if ( weapon->GetSecondaryAmmoType() == iAmmoId )
  245. {
  246. return &weapon->GetWpnData();
  247. }
  248. }
  249. return NULL;
  250. }