Add some console commands

This commit is contained in:
Federico Cecchetto 2022-01-03 21:05:46 +01:00
parent 5e0fb92c9d
commit c9d01afa7f
4 changed files with 118 additions and 13 deletions

View File

@ -4,6 +4,8 @@
#include "game/game.hpp" #include "game/game.hpp"
#include "game/dvars.hpp" #include "game/dvars.hpp"
#include "game/scripting/execution.hpp"
#include "command.hpp" #include "command.hpp"
#include "game_console.hpp" #include "game_console.hpp"
#include "chat.hpp" #include "chat.hpp"
@ -308,8 +310,54 @@ namespace command
return; return;
} }
try
{
const auto arg = params.get(1);
const scripting::entity player = scripting::call("getentbynum", {0}).as<scripting::entity>();
auto ps = game::g_entities[0].client; auto ps = game::g_entities[0].client;
const auto wp = game::G_GetWeaponForName(params.get(1));
if (arg == "ammo"s)
{
const auto weapon = player.call("getcurrentweapon").as<std::string>();
player.call("givemaxammo", {weapon});
}
else if (arg == "allammo"s)
{
const auto weapons = player.call("getweaponslist").as<scripting::array>();
for (auto i = 0; i < weapons.size(); i++)
{
player.call("givemaxammo", {weapons[i]});
}
}
else if (arg == "health"s)
{
if (params.size() > 3)
{
const auto amount = atoi(params.get(2));
const auto health = player.get("health").as<int>();
player.set("health", {health + amount});
}
else
{
const auto amount = game::Dvar_FindVar("g_player_maxhealth")->current.integer;
player.set("health", {amount});
}
}
else if (arg == "all"s)
{
const auto type = game::XAssetType::ASSET_TYPE_WEAPON;
fastfiles::enum_assets(type, [&player, type](const game::XAssetHeader header)
{
const auto asset = game::XAsset{type, header};
const auto* const asset_name = game::DB_GetXAssetName(&asset);
player.call("giveweapon", {asset_name});
}, true);
}
else
{
const auto wp = game::G_GetWeaponForName(arg);
if (wp) if (wp)
{ {
if (game::G_GivePlayerWeapon(ps, wp, 0, 0, 0, 0)) if (game::G_GivePlayerWeapon(ps, wp, 0, 0, 0, 0))
@ -318,6 +366,65 @@ namespace command
game::G_SelectWeapon(0, wp); game::G_SelectWeapon(0, wp);
} }
} }
else
{
game::CG_GameMessage(0, "Weapon does not exist");
}
}
}
catch (...)
{
}
});
add("dropweapon", [](const params& params)
{
if (!game::SV_Loaded())
{
return;
}
try
{
const scripting::entity player = scripting::call("getentbynum", {0}).as<scripting::entity>();
const auto weapon = player.call("getcurrentweapon");
player.call("dropitem", {weapon});
}
catch (...)
{
}
});
add("take", [](const params& params)
{
if (!game::SV_Loaded())
{
return;
}
if (params.size() < 2)
{
game::CG_GameMessage(0, "You did not specify a weapon name");
return;
}
const auto weapon = params.get(1);
try
{
const scripting::entity player = scripting::call("getentbynum", {0}).as<scripting::entity>();
if (weapon == "all"s)
{
player.call("takeallweapons");
}
else
{
player.call("takeweapon", {weapon});
}
}
catch (...)
{
}
}); });
} }
}; };

View File

@ -313,7 +313,7 @@ namespace entity_list
{ {
scripting::array result{}; scripting::array result{};
for (unsigned int i = 0; i < all.size(); i++) for (auto i = 0; i < all.size(); i++)
{ {
const auto raw = all[i].get_raw(); const auto raw = all[i].get_raw();
if (raw.type != game::SCRIPT_OBJECT) if (raw.type != game::SCRIPT_OBJECT)
@ -405,7 +405,7 @@ namespace entity_list
const auto array = value.value(); const auto array = value.value();
for (unsigned int i = 0; i < array.size(); i++) for (auto i = 0; i < array.size(); i++)
{ {
const auto raw = array[i].get_raw(); const auto raw = array[i].get_raw();
if (raw.type != game::SCRIPT_OBJECT) if (raw.type != game::SCRIPT_OBJECT)

View File

@ -165,9 +165,9 @@ namespace scripting
return result; return result;
} }
unsigned int array::size() const int array::size() const
{ {
return game::scr_VarGlob->objectVariableValue[this->id_].u.f.next; return static_cast<int>(game::scr_VarGlob->objectVariableValue[this->id_].u.f.next);
} }
unsigned int array::push(script_value value) const unsigned int array::push(script_value value) const
@ -212,8 +212,6 @@ namespace scripting
{ {
return this->get(key.as<std::string>()); return this->get(key.as<std::string>());
} }
return {};
} }
script_value array::get(const std::string& key) const script_value array::get(const std::string& key) const

View File

@ -32,7 +32,7 @@ namespace scripting
array& operator=(array&& other) noexcept; array& operator=(array&& other) noexcept;
std::vector<script_value> get_keys() const; std::vector<script_value> get_keys() const;
unsigned int size() const; int size() const;
unsigned int push(script_value) const; unsigned int push(script_value) const;
void erase(const unsigned int) const; void erase(const unsigned int) const;