2022-05-25 12:03:26 -04:00
|
|
|
#include <STDInclude.hpp>
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
Dvar::Var MapRotation::SVRandomMapRotation;
|
|
|
|
Dvar::Var MapRotation::SVDontRotate;
|
|
|
|
|
|
|
|
Game::dvar_t** MapRotation::SVMapRotation = reinterpret_cast<Game::dvar_t**>(0x62C7C44);
|
2022-06-24 15:22:46 -04:00
|
|
|
Game::dvar_t** MapRotation::SVMapRotationCurrent = reinterpret_cast<Game::dvar_t**>(0x2098DF0);
|
2022-05-25 12:03:26 -04:00
|
|
|
Game::dvar_t** MapRotation::SVMapname = reinterpret_cast<Game::dvar_t**>(0x2098DDC);
|
|
|
|
|
|
|
|
MapRotation::RotationData MapRotation::DedicatedRotation;
|
|
|
|
|
|
|
|
MapRotation::RotationData::RotationData()
|
|
|
|
:index_(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapRotation::RotationData::randomize()
|
|
|
|
{
|
|
|
|
// Code from https://en.cppreference.com/w/cpp/algorithm/random_shuffle
|
|
|
|
std::random_device rd;
|
|
|
|
std::mt19937 gen(rd());
|
|
|
|
|
2022-06-11 05:55:12 -04:00
|
|
|
std::ranges::shuffle(this->rotationEntries_, gen);
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapRotation::RotationData::addEntry(const std::string& key, const std::string& value)
|
|
|
|
{
|
2022-06-11 05:55:12 -04:00
|
|
|
this->rotationEntries_.emplace_back(std::make_pair(key, value));
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t MapRotation::RotationData::getEntriesSize() const
|
|
|
|
{
|
2022-06-11 05:55:12 -04:00
|
|
|
return this->rotationEntries_.size();
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MapRotation::RotationData::rotationEntry& MapRotation::RotationData::getNextEntry()
|
|
|
|
{
|
|
|
|
const auto index = this->index_;
|
2022-06-11 05:55:12 -04:00
|
|
|
++this->index_ %= this->rotationEntries_.size(); // Point index_ to the next entry
|
|
|
|
return this->rotationEntries_.at(index);
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapRotation::RotationData::parse(const std::string& data)
|
|
|
|
{
|
|
|
|
const auto tokens = Utils::String::Split(data, ' ');
|
|
|
|
|
|
|
|
for (std::size_t i = 0; !tokens.empty() && i < (tokens.size() - 1); i += 2)
|
|
|
|
{
|
|
|
|
const auto& key = tokens[i];
|
|
|
|
const auto& value = tokens[i + 1];
|
|
|
|
|
|
|
|
if (key == "map" || key == "gametype")
|
|
|
|
{
|
|
|
|
this->addEntry(key, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw ParseRotationError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:32:45 -04:00
|
|
|
json11::Json MapRotation::RotationData::to_json() const
|
|
|
|
{
|
|
|
|
std::vector<std::string> mapVector;
|
|
|
|
std::vector<std::string> gametypeVector;
|
|
|
|
|
|
|
|
for (const auto& [key, val] : this->rotationEntries_)
|
|
|
|
{
|
|
|
|
if (key == "map")
|
|
|
|
{
|
|
|
|
mapVector.emplace_back(val);
|
|
|
|
}
|
|
|
|
else if (key == "gametype")
|
|
|
|
{
|
|
|
|
gametypeVector.emplace_back(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
json11::Json mapRotationJson = json11::Json::object
|
|
|
|
{
|
|
|
|
{"maps", mapVector},
|
|
|
|
{"gametypes", gametypeVector},
|
|
|
|
};
|
|
|
|
|
|
|
|
return mapRotationJson;
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:03:26 -04:00
|
|
|
void MapRotation::LoadRotation(const std::string& data)
|
|
|
|
{
|
|
|
|
static auto loaded = false;
|
|
|
|
|
|
|
|
if (loaded)
|
|
|
|
{
|
|
|
|
// Load the rotation once
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-24 15:22:46 -04:00
|
|
|
loaded = true;
|
|
|
|
|
2022-05-25 12:03:26 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
DedicatedRotation.parse(data);
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
2022-06-24 15:22:46 -04:00
|
|
|
Logger::PrintError(Game::CON_CHANNEL_ERROR, "{}: {} contains invalid data!\n", ex.what(), (*SVMapRotation)->name);
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
2022-06-22 04:58:51 -04:00
|
|
|
Logger::Debug("DedicatedRotation size after parsing is '{}'", DedicatedRotation.getEntriesSize());
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
2022-06-13 11:32:45 -04:00
|
|
|
void MapRotation::AddMapRotationCommands()
|
|
|
|
{
|
2022-06-14 14:43:19 -04:00
|
|
|
Command::Add("addMap", [](Command::Params* params)
|
2022-06-13 11:32:45 -04:00
|
|
|
{
|
|
|
|
if (params->size() < 2)
|
|
|
|
{
|
|
|
|
Logger::Print("{} <map name> : add a map to the map rotation\n", params->get(0));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DedicatedRotation.addEntry("map", params->get(1));
|
|
|
|
});
|
|
|
|
|
2022-06-14 14:43:19 -04:00
|
|
|
Command::Add("addGametype", [](Command::Params* params)
|
2022-06-13 11:32:45 -04:00
|
|
|
{
|
|
|
|
if (params->size() < 2)
|
|
|
|
{
|
|
|
|
Logger::Print("{} <gametype> : add a game mode to the map rotation\n", params->get(0));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DedicatedRotation.addEntry("gametype", params->get(1));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:03:26 -04:00
|
|
|
bool MapRotation::ShouldRotate()
|
|
|
|
{
|
|
|
|
if (!Dedicated::IsEnabled() && SVDontRotate.get<bool>())
|
|
|
|
{
|
|
|
|
Logger::Print(Game::CON_CHANNEL_SERVER, "Not performing map rotation as sv_dontRotate is true\n");
|
|
|
|
SVDontRotate.set(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Dvar::Var("party_enable").get<bool>() && Dvar::Var("party_host").get<bool>())
|
|
|
|
{
|
|
|
|
Logger::Print(Game::CON_CHANNEL_SERVER, "Not performing map rotation as we are hosting a party!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-24 15:22:46 -04:00
|
|
|
void MapRotation::ApplyMap(const std::string& map)
|
|
|
|
{
|
|
|
|
assert(!map.empty());
|
|
|
|
|
|
|
|
if (Dvar::Var("sv_cheats").get<bool>())
|
|
|
|
{
|
|
|
|
Command::Execute(Utils::String::VA("devmap %s", map.data()), true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Command::Execute(Utils::String::VA("map %s", map.data()), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapRotation::ApplyGametype(const std::string& gametype)
|
|
|
|
{
|
|
|
|
assert(!gametype.empty());
|
|
|
|
Dvar::Var("g_gametype").set(gametype.data());
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:03:26 -04:00
|
|
|
void MapRotation::RestartCurrentMap()
|
|
|
|
{
|
|
|
|
std::string svMapname = (*SVMapname)->current.string;
|
|
|
|
|
|
|
|
if (svMapname.empty())
|
|
|
|
{
|
|
|
|
Logger::Print(Game::CON_CHANNEL_SERVER, "mapname dvar is empty! Defaulting to mp_afghan\n");
|
|
|
|
svMapname = "mp_afghan";
|
|
|
|
}
|
|
|
|
|
2022-06-24 15:22:46 -04:00
|
|
|
ApplyMap(svMapname);
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
2022-06-24 15:22:46 -04:00
|
|
|
void MapRotation::ApplyRotation(RotationData& rotation)
|
2022-05-25 12:03:26 -04:00
|
|
|
{
|
2022-06-24 15:22:46 -04:00
|
|
|
assert(rotation.getEntriesSize() != 0);
|
|
|
|
|
2022-06-13 11:32:45 -04:00
|
|
|
// Continue to apply gametype until a map is found
|
2022-05-25 12:03:26 -04:00
|
|
|
auto foundMap = false;
|
|
|
|
|
|
|
|
std::size_t i = 0;
|
2022-06-24 15:22:46 -04:00
|
|
|
while (!foundMap && i < rotation.getEntriesSize())
|
2022-05-25 12:03:26 -04:00
|
|
|
{
|
2022-06-24 15:22:46 -04:00
|
|
|
const auto& entry = rotation.getNextEntry();
|
2022-05-25 12:03:26 -04:00
|
|
|
|
|
|
|
if (entry.first == "map")
|
|
|
|
{
|
2022-06-22 04:58:51 -04:00
|
|
|
Logger::Debug("Loading new map: '{}'", entry.second);
|
2022-06-24 15:22:46 -04:00
|
|
|
ApplyMap(entry.second);
|
2022-05-25 12:03:26 -04:00
|
|
|
|
|
|
|
// Map was found so we exit the loop
|
|
|
|
foundMap = true;
|
|
|
|
}
|
2022-06-13 11:32:45 -04:00
|
|
|
else if (entry.first == "gametype")
|
2022-05-25 12:03:26 -04:00
|
|
|
{
|
2022-06-22 04:58:51 -04:00
|
|
|
Logger::Debug("Applying new gametype: '{}'", entry.second);
|
2022-06-24 15:22:46 -04:00
|
|
|
ApplyGametype(entry.second);
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 15:22:46 -04:00
|
|
|
void MapRotation::ApplyMapRotationCurrent(const std::string& data)
|
|
|
|
{
|
|
|
|
assert(!data.empty());
|
|
|
|
|
|
|
|
// Ook, ook, eek
|
|
|
|
Logger::Warning(Game::CON_CHANNEL_SERVER, "You are using deprecated {}", (*SVMapRotationCurrent)->name);
|
|
|
|
|
2022-06-25 14:22:13 -04:00
|
|
|
RotationData rotationCurrent;
|
2022-06-24 15:22:46 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Logger::Debug("Parsing {}", (*SVMapRotationCurrent)->name);
|
2022-06-25 14:22:13 -04:00
|
|
|
rotationCurrent.parse(data);
|
2022-06-24 15:22:46 -04:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Logger::PrintError(Game::CON_CHANNEL_ERROR, "{}: {} contains invalid data!\n", ex.what(), (*SVMapRotationCurrent)->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::Dvar_SetString(*SVMapRotationCurrent, "");
|
|
|
|
|
2022-06-25 14:22:13 -04:00
|
|
|
if (rotationCurrent.getEntriesSize() == 0)
|
2022-06-24 15:22:46 -04:00
|
|
|
{
|
|
|
|
Logger::Print(Game::CON_CHANNEL_SERVER, "{} is empty or contains invalid data. Restarting map\n", (*SVMapRotationCurrent)->name);
|
|
|
|
RestartCurrentMap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-25 14:22:13 -04:00
|
|
|
ApplyRotation(rotationCurrent);
|
2022-06-24 15:22:46 -04:00
|
|
|
}
|
|
|
|
|
2022-06-16 12:19:51 -04:00
|
|
|
void MapRotation::RandomizeMapRotation()
|
|
|
|
{
|
|
|
|
if (SVRandomMapRotation.get<bool>())
|
|
|
|
{
|
|
|
|
Logger::Print(Game::CON_CHANNEL_SERVER, "Randomizing the map rotation\n");
|
|
|
|
DedicatedRotation.randomize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-22 04:58:51 -04:00
|
|
|
Logger::Debug("Map rotation was not randomized");
|
2022-06-16 12:19:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:03:26 -04:00
|
|
|
void MapRotation::SV_MapRotate_f()
|
|
|
|
{
|
|
|
|
if (!ShouldRotate())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::Print(Game::CON_CHANNEL_SERVER, "Rotating map...\n");
|
|
|
|
|
2022-06-24 15:22:46 -04:00
|
|
|
// This takes priority because of backwards compatibility
|
|
|
|
const std::string mapRotationCurrent = (*SVMapRotationCurrent)->current.string;
|
|
|
|
if (!mapRotationCurrent.empty())
|
|
|
|
{
|
|
|
|
Logger::Debug("Applying {}", (*SVMapRotationCurrent)->name);
|
|
|
|
ApplyMapRotationCurrent(mapRotationCurrent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string mapRotation = (*SVMapRotation)->current.string;
|
2022-06-22 04:58:51 -04:00
|
|
|
// People may have sv_mapRotation empty because they only use 'addMap' or 'addGametype'
|
2022-06-16 12:02:22 -04:00
|
|
|
if (!mapRotation.empty())
|
2022-05-25 12:03:26 -04:00
|
|
|
{
|
2022-06-22 04:58:51 -04:00
|
|
|
Logger::Debug("sv_mapRotation is not empty. Parsing...");
|
2022-06-16 12:02:22 -04:00
|
|
|
LoadRotation(mapRotation);
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DedicatedRotation.getEntriesSize() == 0)
|
|
|
|
{
|
2022-06-24 15:22:46 -04:00
|
|
|
Logger::Print(Game::CON_CHANNEL_SERVER, "{} is empty or contains invalid data. Restarting map\n", (*SVMapRotation)->name);
|
2022-05-25 12:03:26 -04:00
|
|
|
RestartCurrentMap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-16 12:19:51 -04:00
|
|
|
RandomizeMapRotation();
|
|
|
|
|
2022-06-24 15:22:46 -04:00
|
|
|
ApplyRotation(DedicatedRotation);
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MapRotation::MapRotation()
|
|
|
|
{
|
2022-06-13 11:32:45 -04:00
|
|
|
AddMapRotationCommands();
|
2022-05-25 12:03:26 -04:00
|
|
|
Utils::Hook::Set<void(*)()>(0x4152E8, SV_MapRotate_f);
|
|
|
|
|
|
|
|
SVRandomMapRotation = Dvar::Register<bool>("sv_randomMapRotation", false,
|
2022-07-02 13:52:57 -04:00
|
|
|
Game::DVAR_ARCHIVE, "Randomize map rotation when true");
|
2022-05-25 12:03:26 -04:00
|
|
|
SVDontRotate = Dvar::Register<bool>("sv_dontRotate", false,
|
2022-07-02 13:52:57 -04:00
|
|
|
Game::DVAR_NONE, "Do not perform map rotation");
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|
2022-06-13 11:32:45 -04:00
|
|
|
|
|
|
|
bool MapRotation::unitTest()
|
|
|
|
{
|
|
|
|
RotationData rotation;
|
|
|
|
|
2022-06-22 04:58:51 -04:00
|
|
|
Logger::Debug("Testing map rotation parsing...");
|
2022-06-13 11:32:45 -04:00
|
|
|
|
|
|
|
const auto* normal = "map mp_highrise map mp_terminal map mp_firingrange map mp_trailerpark gametype dm map mp_shipment_long";
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DedicatedRotation.parse(normal);
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Logger::PrintError(Game::CON_CHANNEL_ERROR, "{}: parsing of 'normal' failed", ex.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* mistake = "spdevmap mp_dome";
|
|
|
|
auto success = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DedicatedRotation.parse(mistake);
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Logger::Debug("{}: parsing of 'normal' failed as expected", ex.what());
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
2022-05-25 12:03:26 -04:00
|
|
|
}
|