Team Fortress 2 Source Code as on 22/4/2020
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.

270 lines
7.0 KiB

  1. //========= Copyright 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 = gHUD.GetIcon( "selection" );
  97. pWeaponInfo->iconInactive = gHUD.GetIcon( "selection" );
  98. pWeaponInfo->iconAmmo = gHUD.GetIcon( "bucket1" );
  99. return;
  100. }
  101. CHudTexture *p;
  102. p = FindHudTextureInDict( tempList, "crosshair" );
  103. if ( p )
  104. {
  105. pWeaponInfo->iconCrosshair = gHUD.AddUnsearchableHudIconToList( *p );
  106. }
  107. p = FindHudTextureInDict( tempList, "autoaim" );
  108. if ( p )
  109. {
  110. pWeaponInfo->iconAutoaim = gHUD.AddUnsearchableHudIconToList( *p );
  111. }
  112. p = FindHudTextureInDict( tempList, "zoom" );
  113. if ( p )
  114. {
  115. pWeaponInfo->iconZoomedCrosshair = gHUD.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 = gHUD.AddUnsearchableHudIconToList( *p );
  125. }
  126. else
  127. {
  128. pWeaponInfo->iconZoomedAutoaim = pWeaponInfo->iconZoomedCrosshair; //default to zoomed crosshair
  129. }
  130. CHudHistoryResource *pHudHR = GET_HUDELEMENT( CHudHistoryResource );
  131. if( pHudHR )
  132. {
  133. p = FindHudTextureInDict( tempList, "weapon" );
  134. if ( p )
  135. {
  136. pWeaponInfo->iconInactive = gHUD.AddUnsearchableHudIconToList( *p );
  137. if ( pWeaponInfo->iconInactive )
  138. {
  139. pWeaponInfo->iconInactive->Precache();
  140. pHudHR->SetHistoryGap( pWeaponInfo->iconInactive->Height() );
  141. }
  142. }
  143. p = FindHudTextureInDict( tempList, "weapon_s" );
  144. if ( p )
  145. {
  146. pWeaponInfo->iconActive = gHUD.AddUnsearchableHudIconToList( *p );
  147. if ( pWeaponInfo->iconActive )
  148. {
  149. pWeaponInfo->iconActive->Precache();
  150. }
  151. }
  152. p = FindHudTextureInDict( tempList, "weapon_small" );
  153. if ( p )
  154. {
  155. pWeaponInfo->iconSmall = gHUD.AddUnsearchableHudIconToList( *p );
  156. if ( pWeaponInfo->iconSmall )
  157. {
  158. pWeaponInfo->iconSmall->Precache();
  159. }
  160. }
  161. p = FindHudTextureInDict( tempList, "ammo" );
  162. if ( p )
  163. {
  164. pWeaponInfo->iconAmmo = gHUD.AddUnsearchableHudIconToList( *p );
  165. if ( pWeaponInfo->iconAmmo )
  166. {
  167. pWeaponInfo->iconAmmo->Precache();
  168. pHudHR->SetHistoryGap( pWeaponInfo->iconAmmo->Height() );
  169. }
  170. }
  171. p = FindHudTextureInDict( tempList, "ammo2" );
  172. if ( p )
  173. {
  174. pWeaponInfo->iconAmmo2 = gHUD.AddUnsearchableHudIconToList( *p );
  175. if ( pWeaponInfo->iconAmmo2 )
  176. {
  177. pWeaponInfo->iconAmmo2->Precache();
  178. pHudHR->SetHistoryGap( pWeaponInfo->iconAmmo2->Height() );
  179. }
  180. }
  181. }
  182. FreeHudTextureList( tempList );
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Purpose: Helper function to return a Ammo pointer from id
  186. //-----------------------------------------------------------------------------
  187. CHudTexture *WeaponsResource::GetAmmoIconFromWeapon( int iAmmoId )
  188. {
  189. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  190. if ( !player )
  191. return NULL;
  192. for ( int i = 0; i < MAX_WEAPONS; i++ )
  193. {
  194. C_BaseCombatWeapon *weapon = player->GetWeapon( i );
  195. if ( !weapon )
  196. continue;
  197. if ( weapon->GetPrimaryAmmoType() == iAmmoId )
  198. {
  199. return weapon->GetWpnData().iconAmmo;
  200. }
  201. else if ( weapon->GetSecondaryAmmoType() == iAmmoId )
  202. {
  203. return weapon->GetWpnData().iconAmmo2;
  204. }
  205. }
  206. return NULL;
  207. }
  208. //-----------------------------------------------------------------------------
  209. // Purpose: Get a pointer to a weapon using this ammo
  210. //-----------------------------------------------------------------------------
  211. const FileWeaponInfo_t *WeaponsResource::GetWeaponFromAmmo( int iAmmoId )
  212. {
  213. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  214. if ( !player )
  215. return NULL;
  216. for ( int i = 0; i < MAX_WEAPONS; i++ )
  217. {
  218. C_BaseCombatWeapon *weapon = player->GetWeapon( i );
  219. if ( !weapon )
  220. continue;
  221. if ( weapon->GetPrimaryAmmoType() == iAmmoId )
  222. {
  223. return &weapon->GetWpnData();
  224. }
  225. else if ( weapon->GetSecondaryAmmoType() == iAmmoId )
  226. {
  227. return &weapon->GetWpnData();
  228. }
  229. }
  230. return NULL;
  231. }