Skip to content

Commit

Permalink
player code updates/refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nukeulater committed Nov 4, 2023
1 parent 298d44a commit f722c36
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 198 deletions.
103 changes: 62 additions & 41 deletions xlive/Blam/Engine/game/players.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,102 +14,123 @@
#include "Util/Hooks/Hook.h"

/*
- TO NOTE:
- This functions work only after the game has started (game life cycle is in_game or after map has been loaded).
- NOTES:
- This gets the player data from the game state, thus it is available only during a match or gameplay (game life cycle is in_game or after map has been loaded)
- If you need to do something in the pregame lobby, use the functions available in Network Session (Blam/Networking/Session)
*/

s_data_array* s_player::GetArray()
s_data_array* s_player::get_data()
{
return *Memory::GetAddress<s_data_array**>(0x4A8260, 0x4D64C4);
}

bool s_player::IndexValid(int playerIndex)
bool s_player::is_index_valid(datum player_index)
{
return playerIndex >= 0 && playerIndex < k_maximum_players;
int32 player_abs_index = DATUM_INDEX_TO_ABSOLUTE_INDEX(player_index);
return player_abs_index >= 0 && player_abs_index < k_maximum_players;
}

s_player* s_player::GetPlayer(int playerIndex)
s_player* s_player::get(datum player_index)
{
if (!IndexValid(playerIndex))
if (!is_index_valid(player_index))
{
return nullptr;
}
return (s_player*)&GetArray()->data[playerIndex * GetArray()->datum_element_size];
return (s_player*)&get_data()->data[DATUM_INDEX_TO_ABSOLUTE_INDEX(player_index) * get_data()->datum_element_size];
}

e_game_team s_player::GetTeam(int playerIndex)
e_game_team s_player::get_team(datum player_index)
{
if (!IndexValid(playerIndex))
if (!is_index_valid(player_index))
{
return (e_game_team)NONE;
}
return (e_game_team)GetPlayer(playerIndex)->properties[0].team_index;
return (e_game_team)get(player_index)->properties[0].team_index;
}

void s_player::SetTeam(int playerIndex, e_game_team team)
void s_player::set_team(datum player_index, e_game_team team)
{
if (!IndexValid(playerIndex))
if (!is_index_valid(player_index))
{
return;
}
GetPlayer(playerIndex)->properties[0].team_index = (int8)team;
get(player_index)->properties[0].team_index = (int8)team;
}

void s_player::SetUnitBipedType(int playerIndex, e_character_type bipedType)
void s_player::set_unit_character_type(datum player_index, e_character_type character_type)
{
if (!IndexValid(playerIndex))
if (!is_index_valid(player_index))
{
return;
}
s_player* player = GetPlayer(playerIndex);
player->properties[0].profile_traits.profile.player_character_type = bipedType;
player->properties[1].profile_traits.profile.player_character_type = bipedType;
s_player* player = get(player_index);
player->properties[0].profile_traits.profile.player_character_type = character_type;
player->properties[1].profile_traits.profile.player_character_type = character_type;
return;
}

void s_player::SetBipedSpeed(int playerIndex, float speed)
void s_player::set_unit_speed(datum player_index, float speed)
{
if (!IndexValid(playerIndex))
if (!is_index_valid(player_index))
{
return;
}
GetPlayer(playerIndex)->unit_speed = speed;
get(player_index)->unit_speed = speed;
}

const wchar_t* s_player::GetName(int playerIndex)
const wchar_t* s_player::get_name(datum player_index)
{
if (!IndexValid(playerIndex))
if (!is_index_valid(player_index))
{
return L"";
}
return GetPlayer(playerIndex)->properties[0].player_name;
return get(player_index)->properties[0].player_name;
}

datum s_player::GetPlayerUnitDatumIndex(int playerIndex)
datum s_player::get_unit_index(datum player_index)
{
if (!IndexValid(playerIndex))
if (!is_index_valid(player_index))
{
return DATUM_INDEX_NONE;
}

return get(player_index)->unit_index;
}

uint8* s_player::get_player_unit(datum player_index)
{
datum unit_datum = s_player::get_unit_index(player_index);
if (DATUM_IS_NONE(unit_datum))
return nullptr;

return GetPlayer(playerIndex)->unit_index;
return (uint8*)object_get_fast_unsafe(unit_datum);
}

unsigned long long s_player::GetId(int playerIndex)
real_vector3d* s_player::get_unit_coords(datum player_index)
{
if (!IndexValid(playerIndex))
uint8* player_unit = get_player_unit(player_index);
if (player_unit != nullptr)
return reinterpret_cast<real_point3d*>(player_unit + 0x64);

return nullptr;
}

uint64 s_player::get_id(datum player_index)
{
if (!is_index_valid(player_index))
{
return 0ull;
}

return GetPlayer(playerIndex)->identifier;
return get(player_index)->identifier;
}

PlayerIterator::PlayerIterator()
: s_data_iterator(s_player::GetArray())
player_iterator::player_iterator()
: s_data_iterator(s_player::get_data())
{
}

bool PlayerIterator::get_next_active_player()
bool player_iterator::get_next_active_player()
{
m_current_player = get_next_datum();

Expand All @@ -124,24 +145,24 @@ bool PlayerIterator::get_next_active_player()
return m_current_player != nullptr;
}

s_player* PlayerIterator::get_current_player_data()
s_player* player_iterator::get_current_player_data()
{
return m_current_player;
}

int PlayerIterator::get_current_player_index()
int player_iterator::get_current_player_index()
{
return get_current_absolute_index();
}

wchar_t* PlayerIterator::get_current_player_name()
wchar_t* player_iterator::get_current_player_name()
{
return m_current_player->properties[0].player_name;
}

unsigned long long PlayerIterator::get_current_player_id()
unsigned long long player_iterator::get_current_player_id()
{
return s_player::GetId(this->get_current_player_index());
return s_player::get_id(this->get_current_player_index());
}

s_players_globals* get_players_globals()
Expand Down Expand Up @@ -185,7 +206,7 @@ void __cdecl player_configuration_validate_character_type(s_player_properties* c
configuration_data->profile_traits.profile.player_character_type = (e_character_type)player_starting_location->campaign_player_type;
found = true;
break;
}
}
}

// If a campaign_player_type type wasn't found in any of the starting locations set default values
Expand Down Expand Up @@ -329,7 +350,7 @@ void player_representation_get_orig_fn(int player_index, int* out_variant_index,

void __cdecl player_representation_get(datum player_datum, int* out_variant_index, int* a3)
{
s_player* player = s_player::GetPlayer(DATUM_INDEX_TO_ABSOLUTE_INDEX(player_datum));
s_player* player = s_player::get(player_datum);

player_configuration_validate_character_type(&player->properties[0]);

Expand Down
32 changes: 17 additions & 15 deletions xlive/Blam/Engine/game/players.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct s_player
{
WORD datum_salt;
WORD flags;
unsigned long long identifier;
uint64 identifier;
DWORD player_creation_tick;
s_machine_identifier machine_identifier; // also known as abEnet
__int16 machine_index;
Expand Down Expand Up @@ -162,25 +162,27 @@ struct s_player
- These functions work only after game has started, if you need to do something in the pregame lobby, use the functions available in Network Session (Blam/Engine/Networking/Session)
*/

static s_data_array* GetArray();
static bool IndexValid(int playerIndex);
static s_player* GetPlayer(int playerIndex);
static e_game_team GetTeam(int playerIndex);
static void SetTeam(int playerIndex, e_game_team team);
static void SetUnitBipedType(int playerIndex, e_character_type bipedType);
static void SetBipedSpeed(int playerIndex, float speed);
static const wchar_t* GetName(int playerIndex);
static datum GetPlayerUnitDatumIndex(int playerIndex);
static unsigned long long GetId(int playerIndex);
static s_data_array* get_data();
static bool is_index_valid(datum player_index);
static s_player* get(datum player_index);
static e_game_team get_team(datum player_index);
static void set_team(datum player_index, e_game_team team);
static void set_unit_character_type(datum player_index, e_character_type character_type);
static void set_unit_speed(datum player_index, float speed);
static const wchar_t* get_name(datum player_index);
static datum get_unit_index(datum player_index);
static uint8* get_player_unit(datum player_index);
static real_vector3d* get_unit_coords(datum player_index);
static uint64 get_id(datum player_index);
};
CHECK_STRUCT_SIZE(s_player, 516);
#pragma pack(pop)

class PlayerIterator : private s_data_iterator<s_player>
class player_iterator : private s_data_iterator<s_player>
{
public:

PlayerIterator();
player_iterator();

bool get_next_active_player();

Expand Down Expand Up @@ -210,7 +212,7 @@ struct s_persistent_campaign_player
s_persistent_weapon_data weapon_0;
s_persistent_weapon_data weapon_1;
s_persistent_weapon_data weapon_2;
WORD grenade_counts_mask;
uint16 grenade_counts_mask;
};
CHECK_STRUCT_SIZE(s_persistent_campaign_player, 28);

Expand Down Expand Up @@ -239,7 +241,7 @@ struct s_players_globals
short unk_AE;
int player_datum_that_triggered_bsp_switch;
int teleported_unit_datum;
byte end_padding[128];
int8 end_padding[128];
};
CHECK_STRUCT_SIZE(s_players_globals, 312);

Expand Down
2 changes: 1 addition & 1 deletion xlive/Blam/Engine/interface/new_hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool __cdecl render_ingame_chat_check()
if (H2Config_hide_ingame_chat)
{
datum local_player_datum_index = player_index_from_user_index(0);
if (s_player::GetPlayer(DATUM_INDEX_TO_ABSOLUTE_INDEX(local_player_datum_index))->is_chatting == 2)
if (s_player::get(local_player_datum_index)->is_chatting == 2)
{
hotkeyFuncToggleHideIngameChat();
}
Expand Down
2 changes: 1 addition & 1 deletion xlive/Blam/Engine/objects/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static s_data_array* get_objects_header()
static s_object_header* get_objects_header(datum object_idx)
{
auto objects_header = get_objects_header();
return (s_object_header*)(&objects_header->data[objects_header->datum_element_size * DATUM_INDEX_TO_ABSOLUTE_INDEX(object_idx)]);
return (s_object_header*)(&objects_header->data[DATUM_INDEX_TO_ABSOLUTE_INDEX(object_idx) * objects_header->datum_element_size]);
}

// Get the object fast, with no validation from datum index
Expand Down
39 changes: 4 additions & 35 deletions xlive/H2MOD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,42 +140,11 @@ int __cdecl stringDisplayHook(int a1, unsigned int a2, wchar_t* a3, int a4) {

#pragma region PlayerFunctions

float H2MOD::get_distance(int playerIndex1, int playerIndex2) {
real_point3d points_distance;
real_point3d* player1 = nullptr;
real_point3d* player2 = nullptr;

player1 = h2mod->get_player_unit_coords(playerIndex1);
player2 = h2mod->get_player_unit_coords(playerIndex2);

points_distance.x = abs(player1->x - player2->x);
points_distance.y = abs(player1->y - player2->y);
points_distance.z = abs(player1->z - player2->z);

return sqrt(pow(points_distance.x, 2) + pow(points_distance.y, 2) + pow(points_distance.z, 2));
}

real_point3d* H2MOD::get_player_unit_coords(int playerIndex) {
BYTE* player_unit = get_player_unit_from_player_index(playerIndex);
if (player_unit != nullptr)
return reinterpret_cast<real_point3d*>(player_unit + 0x64);

return nullptr;
}

BYTE* H2MOD::get_player_unit_from_player_index(int playerIndex) {
datum unit_datum = s_player::GetPlayerUnitDatumIndex(playerIndex);
if (DATUM_IS_NONE(unit_datum))
return nullptr;

return (BYTE*)object_get_fast_unsafe(unit_datum);
}

void call_give_player_weapon(int playerIndex, datum weaponId, bool resetLoadout)
{
//LOG_TRACE_GAME("GivePlayerWeapon(PlayerIndex: %08X, WeaponId: %08X)", PlayerIndex, WeaponId);

datum unit_datum = s_player::GetPlayerUnitDatumIndex(playerIndex);
datum unit_datum = s_player::get_unit_index(playerIndex);
if (!DATUM_IS_NONE(unit_datum))
{
object_placement_data nObject;
Expand All @@ -193,12 +162,12 @@ void call_give_player_weapon(int playerIndex, datum weaponId, bool resetLoadout)

const wchar_t* H2MOD::get_local_player_name(int local_player_index)
{
return s_player::GetName(DATUM_INDEX_TO_ABSOLUTE_INDEX(player_index_from_user_index(local_player_index)));
return s_player::get_name(player_index_from_user_index(local_player_index));
}

int H2MOD::get_player_index_from_unit_datum_index(datum unit_datum_index)
{
PlayerIterator playersIt;
player_iterator playersIt;
while (playersIt.get_next_active_player())
{
datum unit_datum_index_check = playersIt.get_current_player_data()->unit_index;
Expand Down Expand Up @@ -256,7 +225,7 @@ void H2MOD::set_player_unit_grenades_count(int playerIndex, e_grenades type, BYT
"objects\\weapons\\grenade\\plasma_grenade\\plasma_grenade"
};

datum unit_datum_index = s_player::GetPlayerUnitDatumIndex(playerIndex);
datum unit_datum_index = s_player::get_unit_index(playerIndex);
//datum grenade_eqip_tag_datum_index = tags::find_tag(blam_tag::tag_group_type::equipment, grenadeEquipamentTagName[type]);

char* unit_object = (char*)object_try_and_get_and_verify_type(unit_datum_index, FLAG(_object_type_biped));
Expand Down
3 changes: 0 additions & 3 deletions xlive/H2MOD.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ class H2MOD
static void RegisterEvents();

void team_player_indicator_visibility(bool toggle);
BYTE* get_player_unit_from_player_index(int playerIndex);
const wchar_t* get_local_player_name(int local_player_index);
real_point3d* get_player_unit_coords(int player_index);
float get_distance(int, int);
int get_player_index_from_unit_datum_index(datum unit_datum_index);
BYTE get_unit_team_index(datum unit_datum_index);
void set_unit_speed_patch(bool hackit);
Expand Down
10 changes: 5 additions & 5 deletions xlive/H2MOD/GUI/XLiveRendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,13 @@ HRESULT WINAPI XLiveRender()

if (displayXyz && (NetworkSession::LocalPeerIsSessionHost() || game_is_campaign())) {
int text_y_coord = 60;
PlayerIterator playerIt;
while (playerIt.get_next_active_player())
player_iterator player_it;
while (player_it.get_next_active_player())
{
real_point3d* player_position = h2mod->get_player_unit_coords(playerIt.get_current_player_index());
object_datum* biped_unit = (object_datum*)h2mod->get_player_unit_from_player_index(playerIt.get_current_player_index());
real_point3d* player_position = s_player::get_unit_coords(player_it.get_current_player_index());
object_datum* biped_unit = (object_datum*)s_player::get_player_unit(player_it.get_current_player_index());
if (player_position != nullptr) {
std::wstring playerNameWide(playerIt.get_current_player_name());
std::wstring playerNameWide(player_it.get_current_player_name());
std::string playerName(playerNameWide.begin(), playerNameWide.end());
std::string xyzText =
"Player name: " + playerName +
Expand Down
Loading

0 comments on commit f722c36

Please sign in to comment.