[ClientCommands] Make give a non-dev command (#315)

As requested by Dss0
This commit is contained in:
Edo 2022-06-20 11:06:30 +02:00 committed by GitHub
parent e8aadc2382
commit 1ba9db613d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -161,6 +161,86 @@ namespace Components
origin[0], origin[1], origin[2], angles[0], angles[2]);
Game::TeleportPlayer(ent, origin, angles);
});
ClientCommand::Add("give", [](Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{
if (!ClientCommand::CheatsOk(ent))
return;
if (params->size() < 2)
{
Game::SV_GameSendServerCommand(ent->s.number, Game::SV_CMD_CAN_IGNORE,
Utils::String::VA("%c \"GAME_USAGE\x15: give <weapon name>\"", 0x65));
return;
}
Game::level->initializing = 1;
const auto* weaponName = params->get(1);
Logger::DebugInfo("Giving weapon {} to entity {}", weaponName, ent->s.number);
const auto weaponIndex = Game::G_GetWeaponIndexForName(weaponName);
if (weaponIndex == 0)
{
Game::level->initializing = 0;
return;
}
if (Game::BG_GetWeaponDef(weaponIndex)->inventoryType == Game::weapInventoryType_t::WEAPINVENTORY_ALTMODE)
{
Logger::PrintError(Game::CON_CHANNEL_ERROR,
"You can't directly spawn the altfire weapon '{}'. Spawn a weapon that has this altmode instead.\n", weaponName);
Game::level->initializing = 0;
return;
}
auto* weapEnt = Game::G_Spawn();
Utils::VectorCopy(weapEnt->r.currentOrigin, ent->r.currentOrigin);
Game::G_GetItemClassname(static_cast<int>(weaponIndex), weapEnt);
Game::G_SpawnItem(weapEnt, static_cast<int>(weaponIndex));
weapEnt->active = 1;
const auto offHandClass = Game::BG_GetWeaponDef(weaponIndex)->offhandClass;
if (offHandClass != Game::OFFHAND_CLASS_NONE)
{
auto* client = ent->client;
if ((client->ps.weapCommon.offhandPrimary != offHandClass) && (client->ps.weapCommon.offhandSecondary != offHandClass))
{
switch (offHandClass)
{
case Game::OFFHAND_CLASS_FRAG_GRENADE:
case Game::OFFHAND_CLASS_THROWINGKNIFE:
case Game::OFFHAND_CLASS_OTHER:
Logger::DebugInfo("Setting offhandPrimary");
client->ps.weapCommon.offhandPrimary = offHandClass;
break;
default:
Logger::DebugInfo("Setting offhandSecondary");
client->ps.weapCommon.offhandSecondary = offHandClass;
break;
}
}
}
Game::Touch_Item(weapEnt, ent, 0);
weapEnt->active = 0;
if (weapEnt->r.isInUse)
{
Logger::DebugInfo("Freeing up entity {}", weapEnt->s.number);
Game::G_FreeEntity(weapEnt);
}
Game::level->initializing = 0;
for (std::size_t i = 0; i < std::extent_v<decltype(Game::playerState_s::weaponsEquipped)>; ++i)
{
const auto index = ent->client->ps.weaponsEquipped[i];
if (index != 0)
{
Game::Add_Ammo(ent, index, 0, 998, 1);
}
}
});
}
void ClientCommand::AddDevelopmentCommands()
@ -266,86 +346,6 @@ namespace Components
Game::player_die(ent, ent, ent, 100000, 12, 0, nullptr, Game::hitLocation_t::HITLOC_NONE, 0);
}, Scheduler::Pipeline::SERVER);
});
ClientCommand::Add("give", [](Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{
if (!ClientCommand::CheatsOk(ent))
return;
if (params->size() < 2)
{
Game::SV_GameSendServerCommand(ent->s.number, Game::SV_CMD_CAN_IGNORE,
Utils::String::VA("%c \"GAME_USAGE\x15: give weapon\n\"", 0x65));
return;
}
Game::level->initializing = 1;
const auto* weaponName = params->get(1);
Logger::DebugInfo("Giving weapon {} to entity {}", weaponName, ent->s.number);
const auto weaponIndex = Game::G_GetWeaponIndexForName(weaponName);
if (weaponIndex == 0)
{
Game::level->initializing = 0;
return;
}
if (Game::BG_GetWeaponDef(weaponIndex)->inventoryType == Game::weapInventoryType_t::WEAPINVENTORY_ALTMODE)
{
Logger::PrintError(Game::CON_CHANNEL_ERROR,
"You can't directly spawn the altfire weapon '{}'. Spawn a weapon that has this altmode instead.\n", weaponName);
Game::level->initializing = 0;
return;
}
auto* weapEnt = Game::G_Spawn();
Utils::VectorCopy(weapEnt->r.currentOrigin, ent->r.currentOrigin);
Game::G_GetItemClassname(static_cast<int>(weaponIndex), weapEnt);
Game::G_SpawnItem(weapEnt, static_cast<int>(weaponIndex));
weapEnt->active = 1;
const auto offHandClass = Game::BG_GetWeaponDef(weaponIndex)->offhandClass;
if (offHandClass != Game::OFFHAND_CLASS_NONE)
{
auto* client = ent->client;
if ((client->ps.weapCommon.offhandPrimary != offHandClass) && (client->ps.weapCommon.offhandSecondary != offHandClass))
{
switch (offHandClass)
{
case Game::OFFHAND_CLASS_FRAG_GRENADE:
case Game::OFFHAND_CLASS_THROWINGKNIFE:
case Game::OFFHAND_CLASS_OTHER:
Logger::DebugInfo("Setting offhandPrimary");
client->ps.weapCommon.offhandPrimary = offHandClass;
break;
default:
Logger::DebugInfo("Setting offhandSecondary");
client->ps.weapCommon.offhandSecondary = offHandClass;
break;
}
}
}
Game::Touch_Item(weapEnt, ent, 0);
weapEnt->active = 0;
if (weapEnt->r.isInUse)
{
Logger::DebugInfo("Freeing up entity {}", weapEnt->s.number);
Game::G_FreeEntity(weapEnt);
}
Game::level->initializing = 0;
for (std::size_t i = 0; i < std::extent_v<decltype(Game::playerState_s::weaponsEquipped)>; ++i)
{
const auto index = ent->client->ps.weaponsEquipped[i];
if (index != 0)
{
Game::Add_Ammo(ent, index, 0, 998, 1);
}
}
});
}
void ClientCommand::AddScriptFunctions()