From bfd3b6e0568eeafbd4e06dff314125c886809308 Mon Sep 17 00:00:00 2001 From: TotallyMehis Date: Tue, 23 Jun 2020 18:24:58 +0300 Subject: [PATCH] Allow info_loadout to spawn other same weapon types (#294) --- mp/src/game/server/zmr/zmr_entities.cpp | 41 +++++++++++++++----- mp/src/game/server/zmr/zmr_mapitemaction.cpp | 7 ++++ mp/src/game/server/zmr/zmr_mapitemaction.h | 1 + 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/mp/src/game/server/zmr/zmr_entities.cpp b/mp/src/game/server/zmr/zmr_entities.cpp index 46e62bebe..56e3e220a 100644 --- a/mp/src/game/server/zmr/zmr_entities.cpp +++ b/mp/src/game/server/zmr/zmr_entities.cpp @@ -13,6 +13,7 @@ #include "zmr/zmr_shareddefs.h" #include "npcs/zmr_zombiebase_shared.h" #include "zmr/weapons/zmr_base.h" +#include "zmr_mapitemaction.h" #include "zmr_entities.h" @@ -1676,22 +1677,23 @@ void CZMEntLoadout::DistributeToPlayer( CZMPlayer* pPlayer ) void CZMEntLoadout::GiveWeapon( CZMPlayer* pPlayer, int loadout_wep ) { - const char* weps[] = { - "weapon_zm_pistol", - "weapon_zm_shotgun", - "weapon_zm_rifle", - "weapon_zm_mac10", + // Loadout is a mix of items and item classes. + const char* loadouts[LO_MAX] = { + "Pistol", + "Shotgun", + "Rifle", + "SMG", "weapon_zm_molotov", "weapon_zm_sledge", "weapon_zm_improvised", - "weapon_zm_revolver", + "BigPistol", }; - COMPILE_TIME_ASSERT( ARRAYSIZE( weps ) >= LO_MAX ); + COMPILE_TIME_ASSERT( ARRAYSIZE( loadouts ) >= LO_MAX ); Assert( loadout_wep >= 0 && loadout_wep < LO_MAX ); - const char* wepname = weps[loadout_wep]; + const char* loadoutClass = loadouts[loadout_wep]; bool ammo = false; @@ -1710,11 +1712,30 @@ void CZMEntLoadout::GiveWeapon( CZMPlayer* pPlayer, int loadout_wep ) } - CZMBaseWeapon* pWeapon = ToZMBaseWeapon( pPlayer->Weapon_Create( wepname ) ); + CUtlVector items; + + // See if it's a single item. + auto* pData = ZMItemAction::g_ZMMapItemSystem.GetItemData( loadoutClass ); + + if ( pData ) + { + items.AddToTail( pData ); + } + else + { + // It's a class. Get all the items. + auto flag = ZMItemAction::g_ZMMapItemSystem.GetClassFlag( loadoutClass ); + ZMItemAction::g_ZMMapItemSystem.GetMapItemsByClass( flag, items ); + } + + + // Get a random weapon from the items. + const char* wepname = items.Count() > 0 ? items[random->RandomInt( 0, items.Count() - 1 )]->m_pszClassname : ""; + auto* pWeapon = ToZMBaseWeapon( pPlayer->Weapon_Create( wepname ) ); if ( !pWeapon ) { - Warning( "Failed to give player loadout weapon %s!\n", wepname ); + Warning( "Failed to give player loadout '%s'!\n", loadoutClass ); return; } diff --git a/mp/src/game/server/zmr/zmr_mapitemaction.cpp b/mp/src/game/server/zmr/zmr_mapitemaction.cpp index 370a62995..6ea7ac46c 100644 --- a/mp/src/game/server/zmr/zmr_mapitemaction.cpp +++ b/mp/src/game/server/zmr/zmr_mapitemaction.cpp @@ -442,6 +442,13 @@ const ItemBaseData_t* CZMMapItemSystem::GetItemData( int index ) return nullptr; } +const ItemBaseData_t* CZMMapItemSystem::GetItemData( const char* itemclass ) +{ + int index = FindItemByClassname( itemclass ); + + return GetItemData( index ); +} + bool CZMMapItemSystem::AffectsItem( const char* classname ) { return FindItemByClassname( classname ) != -1; diff --git a/mp/src/game/server/zmr/zmr_mapitemaction.h b/mp/src/game/server/zmr/zmr_mapitemaction.h index 7e1956c47..47c8dc5e0 100644 --- a/mp/src/game/server/zmr/zmr_mapitemaction.h +++ b/mp/src/game/server/zmr/zmr_mapitemaction.h @@ -76,6 +76,7 @@ namespace ZMItemAction static unsigned int GetItemFlags( const char* classname ); static unsigned int GetClassFlag( const char* classname ); static const ItemBaseData_t* GetItemData( int index ); + static const ItemBaseData_t* GetItemData( const char* itemclass ); static bool GetMapItemsByClass( unsigned int flags, CUtlVector& items );