Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 74 additions & 3 deletions src/client/cg_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "../server/monsters/m_flash.hpp"
#include "../shared/logger.hpp"

#include <span>

cgame_import_t cgi;
cgame_export_t cglobals;

Expand All @@ -41,6 +43,13 @@ static void InitClientLogging()
cgi.Com_Print = worr::LoggerPrint;
}

/*
=============
CG_GetExtension

Returns nullptr for unsupported extensions.
=============
*/
static void* CG_GetExtension(const char* name) {
return nullptr;
}
Expand All @@ -64,38 +73,100 @@ void CG_DrawHUD(int32_t isplit, const cg_server_data_t* data, vrect_t hud_vrect,
void CG_TouchPics();
layout_flags_t CG_LayoutFlags(const player_state_t* ps);

/*
=============
CG_GetAmmoStatsSpan

Returns a span view over the compressed ammo stats block.
=============
*/
static std::span<const uint16_t> CG_GetAmmoStatsSpan(const player_state_t* ps) {
const auto stats_span = std::span<const int16_t>{ps->stats};
const auto ammo_span = stats_span.subspan(STAT_AMMO_INFO_START, NUM_AMMO_STATS);

return std::span<const uint16_t>{reinterpret_cast<const uint16_t*>(ammo_span.data()), ammo_span.size()};
}

/*
=============
CG_GetPowerupStatsSpan

Returns a span view over the compressed powerup stats block.
=============
*/
static std::span<const uint16_t> CG_GetPowerupStatsSpan(const player_state_t* ps) {
const auto stats_span = std::span<const int16_t>{ps->stats};
const auto powerup_span = stats_span.subspan(STAT_POWERUP_INFO_START, NUM_POWERUP_STATS);

return std::span<const uint16_t>{reinterpret_cast<const uint16_t*>(powerup_span.data()), powerup_span.size()};
}

/*
=============
CG_GetActiveWeaponWheelWeapon
=============
*/
static int32_t CG_GetActiveWeaponWheelWeapon(const player_state_t* ps) {
return ps->stats[STAT_ACTIVE_WHEEL_WEAPON];
}

/*
=============
CG_GetOwnedWeaponWheelWeapons
=============
*/
static uint32_t CG_GetOwnedWeaponWheelWeapons(const player_state_t* ps) {
return ((uint32_t)(uint16_t)ps->stats[STAT_WEAPONS_OWNED_1]) | ((uint32_t)(uint16_t)(ps->stats[STAT_WEAPONS_OWNED_2]) << 16);
}

/*
=============
CG_GetWeaponWheelAmmoCount
=============
*/
static int16_t CG_GetWeaponWheelAmmoCount(const player_state_t* ps, int32_t ammoID) {
uint16_t ammo = GetAmmoStat((uint16_t*)&ps->stats[STAT_AMMO_INFO_START], ammoID);
const auto ammo_stats = CG_GetAmmoStatsSpan(ps);
const uint16_t ammo = GetAmmoStat(ammo_stats.data(), ammoID);

if (ammo == AMMO_VALUE_INFINITE)
return -1;

return ammo;
}

/*
=============
CG_GetPowerupWheelCount
=============
*/
static int16_t CG_GetPowerupWheelCount(const player_state_t* ps, int32_t powerup_id) {
return GetPowerupStat((uint16_t*)&ps->stats[STAT_POWERUP_INFO_START], powerup_id);
const auto powerup_stats = CG_GetPowerupStatsSpan(ps);

return GetPowerupStat(powerup_stats.data(), powerup_id);
}

/*
=============
CG_GetHitMarkerDamage
=============
*/
static int16_t CG_GetHitMarkerDamage(const player_state_t* ps) {
return ps->stats[STAT_HIT_MARKER];
}

/*
=============
CG_ParseConfigString

Updates cached configuration values when configstrings change.
=============
*/
static void CG_ParseConfigString(int32_t i, const char* s) {
if (i == CONFIG_N64_PHYSICS_MEDAL)
pm_config.n64Physics = !!strtoul(s, nullptr, 10);
else if (i == CS_AIRACCEL)
pm_config.airAccel = strtoul(s, nullptr, 10);
}

void CG_ParseCenterPrint(const char* str, int isplit, bool instant);
void CG_ClearNotify(int32_t isplit);
void CG_ClearCenterprint(int32_t isplit);
Expand Down
10 changes: 5 additions & 5 deletions src/shared/bg_local.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ constexpr void set_compressed_integer(uint16_t* start, uint8_t id, uint16_t coun
}

template<size_t bits_per_value>
constexpr uint16_t get_compressed_integer(uint16_t* start, uint8_t id) {
constexpr uint16_t get_compressed_integer(const uint16_t* start, uint8_t id) {
uint16_t bit_offset = bits_per_value * id;
uint16_t byte = bit_offset / 8;
uint16_t bit_shift = bit_offset % 8;
uint16_t mask = (bit_v<bits_per_value> -1) << bit_shift;
uint16_t* base = (uint16_t*)((uint8_t*)start + byte);
const uint16_t* base = reinterpret_cast<const uint16_t*>(reinterpret_cast<const uint8_t*>(start) + byte);
return (*base & mask) >> bit_shift;
}

Expand All @@ -180,7 +180,7 @@ constexpr void SetAmmoStat(uint16_t* start, uint8_t ammoID, uint16_t count) {
set_compressed_integer<NUM_BITS_FOR_AMMO>(start, ammoID, count);
}

constexpr uint16_t GetAmmoStat(uint16_t* start, uint8_t ammoID) {
constexpr uint16_t GetAmmoStat(const uint16_t* start, uint8_t ammoID) {
return get_compressed_integer<NUM_BITS_FOR_AMMO>(start, ammoID);
}

Expand All @@ -194,7 +194,7 @@ constexpr void SetPowerupStat(uint16_t* start, uint8_t powerup_id, uint16_t coun
set_compressed_integer<NUM_BITS_PER_POWERUP>(start, powerup_id, count);
}

constexpr uint16_t GetPowerupStat(uint16_t* start, uint8_t powerup_id) {
constexpr uint16_t GetPowerupStat(const uint16_t* start, uint8_t powerup_id) {
return get_compressed_integer<NUM_BITS_PER_POWERUP>(start, powerup_id);
}

Expand Down Expand Up @@ -282,4 +282,4 @@ enum player_stat_t {
STAT_LAST
};

static_assert(STAT_LAST <= MAX_STATS + 1, "stats list overflow");
static_assert(STAT_LAST <= MAX_STATS + 1, "stats list overflow");