@@ -700,6 +700,7 @@ BEGIN_ENT_SCRIPTDESC( CTFPlayer, CBaseMultiplayerPlayer , "Team Fortress 2 Playe
700700
701701#ifdef BDSBASE
702702 DEFINE_SCRIPTFUNC_WRAPPED(IgnitePlayer, "")
703+ DEFINE_SCRIPTFUNC_WRAPPED(GiveWeapon, "")
703704#else
704705 DEFINE_SCRIPTFUNC(IgnitePlayer, "")
705706#endif
@@ -5851,99 +5852,102 @@ void CTFPlayer::ManageRegularWeaponsLegacy( TFPlayerClassData_t *pData )
58515852}
58525853
58535854#ifdef BDSBASE
5854- CON_COMMAND_F(tf_giveweapon, "Give a weapon to the player. Format: tf_giveweapon <item definition name> or <item def index>", FCVAR_CHEAT )
5855+ bool CTFPlayer::ScriptGiveWeapon(const char* pszWeapon )
58555856{
5856- CTFPlayer* pPlayer = ToTFPlayer(UTIL_GetCommandClient());
5857- if (!pPlayer)
5858- return;
5859-
58605857 CEconItemDefinition* pItemDef = NULL;
58615858
5862- int iItemCount = args.ArgC();
5863- for (int i = 1; i < iItemCount; ++i)
5859+ const char* input = pszWeapon;
5860+
5861+ // Check to see if args[1] is a number (itemdefid) and if so, translate it to actual itemname
5862+ if (V_isdigit(input[0]))
58645863 {
5865- // Check to see if args[1] is a number (itemdefid) and if so, translate it to actual itemname
5866- if (V_isdigit(args[i][0]))
5867- {
5868- int iDef = V_atoi(args[i]);
5869- pItemDef = GetItemSchema()->GetItemDefinition(iDef);
5870- if (pItemDef)
5871- {
5872- break;
5873- }
5874- }
5875- else
5876- {
5877- pItemDef = GetItemSchema()->GetItemDefinitionByName(args[i]);
5878- if (pItemDef)
5879- {
5880- break;
5881- }
5882- }
5864+ int iDef = V_atoi(input);
5865+ pItemDef = GetItemSchema()->GetItemDefinition(iDef);
5866+ }
5867+ else
5868+ {
5869+ pItemDef = GetItemSchema()->GetItemDefinitionByName(input);
58835870 }
58845871
58855872 if (pItemDef)
58865873 {
58875874 CEconItemView* pItemData = new CEconItemView();
58885875 CSteamID ownerSteamID;
5889- pPlayer-> GetSteamID(&ownerSteamID);
5876+ GetSteamID(&ownerSteamID);
58905877 pItemData->Init(pItemDef->GetDefinitionIndex(), AE_UNIQUE, AE_USE_SCRIPT_VALUE, ownerSteamID.GetAccountID());
58915878 if (pItemData && pItemData->IsValid())
58925879 {
5893- int iClass = pPlayer-> GetPlayerClass()->GetClassIndex();
5880+ int iClass = GetPlayerClass()->GetClassIndex();
58945881
58955882 if (!pItemData->GetStaticData()->CanBeUsedByClass(iClass))
58965883 {
5897- Warning("Item %s cannot be used by this class\n", pItemData->GetItemDefinition()->GetItemDefinitionName());
5898- return;
5884+ return false;
58995885 }
59005886
59015887 int iSlot = pItemData->GetStaticData()->GetLoadoutSlot(iClass);
5902- CTFWeaponBase* pWeapon = dynamic_cast<CTFWeaponBase*>(pPlayer-> GetEntityForLoadoutSlot(iSlot));
5888+ CTFWeaponBase* pWeapon = dynamic_cast<CTFWeaponBase*>(GetEntityForLoadoutSlot(iSlot));
59035889
59045890 // we need to force translating the name here.
59055891 // GiveNamedItem will not translate if we force creating the item
59065892 const char* pTranslatedWeaponName = TranslateWeaponEntForClass(pItemData->GetStaticData()->GetItemClass(), iClass);
5907- CTFWeaponBase* pNewItem = dynamic_cast<CTFWeaponBase*>(pPlayer-> GiveNamedItem(pTranslatedWeaponName, 0, pItemData, true));
5893+ CTFWeaponBase* pNewItem = dynamic_cast<CTFWeaponBase*>(GiveNamedItem(pTranslatedWeaponName, 0, pItemData, true));
59085894 if (pNewItem)
59095895 {
59105896 CTFWeaponBuilder* pBuilder = dynamic_cast<CTFWeaponBuilder*>((CBaseEntity*)pNewItem);
59115897 if (pBuilder)
59125898 {
5913- pBuilder->SetSubType(pPlayer-> GetPlayerClass()->GetData()->m_aBuildable[0]);
5899+ pBuilder->SetSubType(GetPlayerClass()->GetData()->m_aBuildable[0]);
59145900 }
59155901
59165902 // make sure we removed our current weapon
59175903 if (pWeapon)
59185904 {
5919- pPlayer-> Weapon_Detach(pWeapon);
5905+ Weapon_Detach(pWeapon);
59205906 UTIL_Remove(pWeapon);
59215907 }
59225908
59235909 pNewItem->MarkAttachedEntityAsValidated();
5924- pNewItem->GiveTo(pPlayer );
5910+ pNewItem->GiveTo(this );
59255911
5926- pPlayer-> PostInventoryApplication();
5912+ PostInventoryApplication();
59275913
59285914 // Refills weapon clips, too
59295915 for (int i = 0; i < MAX_WEAPONS; i++)
59305916 {
5931- CTFWeaponBase* pWeapon = dynamic_cast<CTFWeaponBase*>(pPlayer-> GetWeapon(i));
5917+ CTFWeaponBase* pWeapon = dynamic_cast<CTFWeaponBase*>(GetWeapon(i));
59325918 if (!pWeapon)
59335919 continue;
59345920
59355921 pWeapon->GiveDefaultAmmo();
59365922 pWeapon->WeaponRegenerate();
59375923 }
5938-
5939- Msg("Gave %s to player %s [%i]\n", pItemData->GetItemDefinition()->GetItemDefinitionName(), pPlayer->GetPlayerName(), ownerSteamID.GetAccountID());
5940- }
5941- else
5942- {
5943- Warning("Item not found or is not a weapon\n");
5924+
5925+ return true;
59445926 }
59455927 }
59465928 }
5929+
5930+ return false;
5931+ }
5932+
5933+ CON_COMMAND_F(tf_giveweapon, "Give a weapon to the player. Format: tf_giveweapon <item definition name> or <item def index>", FCVAR_CHEAT)
5934+ {
5935+ CTFPlayer* pPlayer = ToTFPlayer(UTIL_GetCommandClient());
5936+ if (!pPlayer)
5937+ return;
5938+
5939+ bool bGiven = pPlayer->ScriptGiveWeapon(args[1]);
5940+
5941+ if (bGiven)
5942+ {
5943+ CSteamID ownerSteamID;
5944+ pPlayer->GetSteamID(&ownerSteamID);
5945+ Msg("Gave %s to player %s [%i]\n", args[1], pPlayer->GetPlayerName(), ownerSteamID.GetAccountID());
5946+ }
5947+ else
5948+ {
5949+ Warning("Item not found, is not a weapon, or cannot be used with this class.\n");
5950+ }
59475951}
59485952#endif
59495953
0 commit comments