-
Автор темы
- #1
ну опять я )))
prediction_system.cpp
prediction_system.h
m_flThirdpersonRecoil():
structs.hpp
get_spread_virtual():
structs.cpp
get_inaccuracy_virtual()
structs.cpp:
prediction_system.cpp
Код:
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#include "prediction_system.h"
void engineprediction::store_netvars()
{
auto data = &netvars_data[m_clientstate()->iCommandAck % MULTIPLAYER_BACKUP];
data->tickbase = g_ctx.local()->m_nTickBase();
data->m_aimPunchAngle = g_ctx.local()->m_aimPunchAngle();
data->m_aimPunchAngleVel = g_ctx.local()->m_aimPunchAngleVel();
data->m_viewPunchAngle = g_ctx.local()->m_viewPunchAngle();
data->m_vecViewOffset = g_ctx.local()->m_vecViewOffset();
data->m_duckAmount = g_ctx.local()->m_flDuckAmount();
data->m_duckSpeed = g_ctx.local()->m_flDuckSpeed();
}
void engineprediction::store_data()
{
int tickbase;
StoredData_t* data;
if (!g_ctx.local() && !g_ctx.local()->is_alive()) {
reset_data();
return;
}
tickbase = g_ctx.local()->m_nTickBase();
data = &m_data[tickbase % MULTIPLAYER_BACKUP];
data->m_tickbase = tickbase;
data->m_punch = g_ctx.local()->m_aimPunchAngle();
data->m_punch_vel = g_ctx.local()->m_aimPunchAngleVel();
data->m_view_offset = g_ctx.local()->m_vecViewOffset();
data->m_velocity_modifier = g_ctx.local()->m_flVelocityModifier();
}
void engineprediction::reset_data()
{
m_data.fill(StoredData_t());
}
void engineprediction::restore_netvars()
{
int time;
auto data = &netvars_data[time % MULTIPLAYER_BACKUP]; //-V807
if (!data || !data->m_bIsFilled || data->command_number != time || (time - data->command_number) > 150)
return;
auto aim_punch_angle_delta = data->m_aimPunchAngle - g_ctx.local()->m_aimPunchAngle();
auto aim_punch_angle_vel_delta = data->m_aimPunchAngleVel - g_ctx.local()->m_aimPunchAngleVel();
auto view_punch_angle_delta = data->m_viewPunchAngle - g_ctx.local()->m_viewPunchAngle();
auto view_offset_delta = data->m_vecViewOffset - g_ctx.local()->m_vecViewOffset();
auto velocity_delta = data->m_vecVelocity - g_ctx.local()->m_vecVelocity();
auto thirdperson_recoil_delta = data->m_flThirdpersonRecoil - g_ctx.local()->m_flThirdpersonRecoil();
auto duck_amount_delta = data->m_flDuckAmount - g_ctx.local()->m_flDuckAmount();
auto fall_velocity_delta = data->m_flFallVelocity - g_ctx.local()->m_flFallVelocity();
auto velocity_modifier_delta = data->m_flVelocityModifier - g_ctx.local()->m_flVelocityModifier();
if (std::fabs(aim_punch_angle_delta.x) <= 0.03125f && std::fabs(aim_punch_angle_delta.y) <= 0.03125f && std::fabs(aim_punch_angle_delta.z) <= 0.03125f)
g_ctx.local()->m_aimPunchAngle() = data->m_aimPunchAngle;
if (std::fabs(aim_punch_angle_vel_delta.x) <= 0.03125f && std::fabs(aim_punch_angle_vel_delta.y) <= 0.03125f && std::fabs(aim_punch_angle_vel_delta.z) <= 0.03125f)
g_ctx.local()->m_aimPunchAngleVel() = data->m_aimPunchAngleVel;
if (std::fabs(view_punch_angle_delta.x) <= 0.03125f && std::fabs(view_punch_angle_delta.y) <= 0.03125f && std::fabs(view_punch_angle_delta.z) <= 0.03125f)
g_ctx.local()->m_viewPunchAngle() = data->m_viewPunchAngle;
if (std::fabs(view_offset_delta.x) <= 0.03125f && std::fabs(view_offset_delta.y) <= 0.03125f && std::fabs(view_offset_delta.z) <= 0.03125f)
g_ctx.local()->m_vecViewOffset() = data->m_vecViewOffset;
if (std::fabs(velocity_delta.x) <= 0.03125f && std::fabs(velocity_delta.y) <= 0.03125f && std::fabs(velocity_delta.z) <= 0.03125f)
g_ctx.local()->m_vecVelocity() = data->m_vecVelocity;
if (std::fabs(thirdperson_recoil_delta) <= 0.03125f)
g_ctx.local()->m_flThirdpersonRecoil() = data->m_flThirdpersonRecoil;
if (std::fabs(duck_amount_delta) <= 0.03125f)
g_ctx.local()->m_flDuckAmount() = data->m_flDuckAmount;
if (std::fabs(fall_velocity_delta) <= 0.03125f)
g_ctx.local()->m_flFallVelocity() = data->m_flFallVelocity;
if (std::fabs(velocity_modifier_delta) <= 0.00625f)
g_ctx.local()->m_flVelocityModifier() = data->m_flVelocityModifier;
}
void engineprediction::update_vel()
{
static int m_iLastCmdAck = 0;
static float m_flNextCmdTime = 0.f;
if (m_clientstate() && (m_iLastCmdAck != m_clientstate()->nLastCommandAck || m_flNextCmdTime != m_clientstate()->flNextCmdTime))
{
if (g_ctx.globals.last_velocity_modifier != g_ctx.local()->m_flVelocityModifier())
{
*reinterpret_cast<int*>(reinterpret_cast<uintptr_t>(m_prediction() + 0x24)) = 1;
g_ctx.globals.last_velocity_modifier = g_ctx.local()->m_flVelocityModifier();
}
m_iLastCmdAck = m_clientstate()->nLastCommandAck;
m_flNextCmdTime = m_clientstate()->flNextCmdTime;
}
}
void engineprediction::setup()
{
if (prediction_data.prediction_stage != SETUP)
return;
backup_data.flags = g_ctx.local()->m_fFlags(); //-V807
backup_data.velocity = g_ctx.local()->m_vecVelocity();
prediction_data.old_curtime = m_globals()->m_curtime; //-V807
prediction_data.old_frametime = m_globals()->m_frametime;
m_prediction()->InPrediction = true;
m_prediction()->IsFirstTimePredicted = false;
m_globals()->m_curtime = TICKS_TO_TIME(g_ctx.globals.fixed_tickbase);
m_globals()->m_frametime = m_prediction()->EnginePaused ? 0.0f : m_globals()->m_intervalpertick;
prediction_data.prediction_stage = PREDICT;
}
void engineprediction::predict(CUserCmd* m_pcmd)
{
if (prediction_data.prediction_stage != PREDICT)
return;
const auto backup_footsteps = m_cvar()->FindVar(crypt_str("sv_footsteps"))->GetInt();
m_cvar()->FindVar(crypt_str("sv_footsteps"))->m_nFlags &= ~(1 << 14);
m_cvar()->FindVar(crypt_str("sv_footsteps"))->m_nFlags &= ~(1 << 8);
m_cvar()->FindVar(crypt_str("sv_footsteps"))->SetValue(0);
if (m_clientstate()->iDeltaTick > 0)
m_prediction()->Update(m_clientstate()->iDeltaTick, true, m_clientstate()->nLastCommandAck, m_clientstate()->nLastOutgoingCommand + m_clientstate()->iChokedCommands);
if (!prediction_data.prediction_random_seed)
prediction_data.prediction_random_seed = *reinterpret_cast <int**> (util::FindSignature(crypt_str("client.dll"), crypt_str("A3 ? ? ? ? 66 0F 6E 86")) + 0x1);
*prediction_data.prediction_random_seed = MD5_PseudoRandom(m_pcmd->m_command_number % 150);
if (!prediction_data.prediction_player)
prediction_data.prediction_player = *reinterpret_cast <int**> (util::FindSignature(crypt_str("client.dll"), crypt_str("89 35 ? ? ? ? F3 0F 10 48")) + 0x2);
*prediction_data.prediction_player = reinterpret_cast <int> (g_ctx.local());
m_movehelper()->set_host(g_ctx.local());
m_gamemovement()->StartTrackPredictionErrors(g_ctx.local()); //-V807
static auto m_nImpulse = util::find_in_datamap(g_ctx.local()->GetPredDescMap(), crypt_str("m_nImpulse"));
static auto m_nButtons = util::find_in_datamap(g_ctx.local()->GetPredDescMap(), crypt_str("m_nButtons"));
static auto m_afButtonLast = util::find_in_datamap(g_ctx.local()->GetPredDescMap(), crypt_str("m_afButtonLast"));
static auto m_afButtonPressed = util::find_in_datamap(g_ctx.local()->GetPredDescMap(), crypt_str("m_afButtonPressed"));
static auto m_afButtonReleased = util::find_in_datamap(g_ctx.local()->GetPredDescMap(), crypt_str("m_afButtonReleased"));
if (m_pcmd->m_impulse)
*reinterpret_cast<uint32_t*>(uint32_t(g_ctx.local()) + m_nImpulse) = m_pcmd->m_impulse;
CMoveData move_data;
memset(&move_data, 0, sizeof(CMoveData));
m_prediction()->SetupMove(g_ctx.local(), m_pcmd, m_movehelper(), &move_data);
m_gamemovement()->ProcessMovement(g_ctx.local(), &move_data);
m_prediction()->FinishMove(g_ctx.local(), m_pcmd, &move_data);
m_gamemovement()->FinishTrackPredictionErrors(g_ctx.local());
m_movehelper()->set_host(nullptr);
auto viewmodel = g_ctx.local()->m_hViewModel().Get();
if (viewmodel)
{
viewmodel_data.weapon = viewmodel->m_hWeapon().Get();
viewmodel_data.viewmodel_index = viewmodel->m_nViewModelIndex();
viewmodel_data.sequence = viewmodel->m_nSequence();
viewmodel_data.animation_parity = viewmodel->m_nAnimationParity();
viewmodel_data.cycle = viewmodel->m_flCycle();
viewmodel_data.animation_time = viewmodel->m_flAnimTime();
}
const auto weapon = g_ctx.local()->m_hActiveWeapon().Get();
if (!weapon) {
m_spread = m_inaccuracy = 0.f;
return;
}
weapon->update_accuracy_penality();
m_spread = weapon->get_spread_virtual();
m_inaccuracy = weapon->get_inaccuracy_virtual();
m_cvar()->FindVar(crypt_str("sv_footsteps"))->SetValue(backup_footsteps);
prediction_data.prediction_stage = FINISH;
}
void engineprediction::finish()
{
if (prediction_data.prediction_stage != FINISH)
return;
*prediction_data.prediction_random_seed = -1;
*prediction_data.prediction_player = 0;
m_globals()->m_curtime = prediction_data.old_curtime;
m_globals()->m_frametime = prediction_data.old_frametime;
}
Код:
#pragma once
#include "..\..\includes.hpp"
#include "..\..\sdk\structs.hpp"
enum Prediction_stage
{
SETUP,
PREDICT,
FINISH
};
class StoredData_t {
public:
int m_tickbase;
Vector m_punch;
Vector m_punch_vel;
Vector m_view_offset;
float m_velocity_modifier;
public:
__forceinline StoredData_t() : m_tickbase{ }, m_punch{ }, m_punch_vel{ }, m_view_offset{ }, m_velocity_modifier{ } {};
};
std::array< StoredData_t, MULTIPLAYER_BACKUP > m_data;
struct Netvars_data
{
Netvars_data() {
reset();
};
~Netvars_data() {
reset();
};
float m_duckAmount = 0.f;
float m_duckSpeed = 0.f;
int tickbase = INT_MIN;
void reset()
{
command_number = INT_MAX;
m_aimPunchAngle.Zero();
m_aimPunchAngleVel.Zero();
m_viewPunchAngle.Zero();
m_vecViewOffset.Zero();
m_vecVelocity.Zero();
m_flDuckAmount = FLT_MIN;
m_flThirdpersonRecoil = FLT_MIN;
m_flFallVelocity = FLT_MIN;
m_flVelocityModifier = FLT_MIN;
m_bIsFilled = false;
}
int command_number = INT_MAX;
Vector m_aimPunchAngle = ZERO;
Vector m_aimPunchAngleVel = ZERO;
Vector m_viewPunchAngle = ZERO;
Vector m_vecViewOffset = ZERO;
Vector m_vecVelocity = ZERO;
float m_flDuckAmount = FLT_MIN;
float m_flThirdpersonRecoil = FLT_MIN;
float m_flFallVelocity = FLT_MIN;
float m_flVelocityModifier = FLT_MIN;
bool m_bIsFilled = false;
};
class engineprediction : public singleton <engineprediction>
{
struct Backup_data
{
int flags = 0;
Vector velocity = ZERO;
};
struct Prediction_data
{
void reset()
{
prediction_stage = SETUP;
old_curtime = 0.0f;
old_frametime = 0.0f;
}
Prediction_stage prediction_stage = SETUP;
float old_curtime = 0.0f;
float old_frametime = 0.0f;
int* prediction_random_seed = nullptr;
int* prediction_player = nullptr;
};
float m_spread, m_inaccuracy;
struct Viewmodel_data
{
weapon_t* weapon = nullptr;
int viewmodel_index = 0;
int sequence = 0;
int animation_parity = 0;
float cycle = 0.0f;
float animation_time = 0.0f;
};
public:
Netvars_data netvars_data[MULTIPLAYER_BACKUP] = {};
Backup_data backup_data;
Prediction_data prediction_data;
Viewmodel_data viewmodel_data;
float get_spread() const { return m_spread; }
float get_inaccuracy() const { return m_inaccuracy; }
void store_netvars();
void store_data();
void reset_data();
void update_vel();
void restore_netvars();
void setup();
void predict(CUserCmd* m_pcmd);
void finish();
};
structs.hpp
Код:
NETVAR(float, m_flThirdpersonRecoil, crypt_str("CCSPlayer"), crypt_str("m_flThirdpersonRecoil"));
structs.cpp
Код:
loat weapon_t::get_spread_virtual()
{
if (!this) //-V704
return 0.0f;
return call_virtual<float(__thiscall*)(void*)>(this, g_ctx.indexes.at(10))(this);
}
structs.cpp:
Код:
loat weapon_t::get_inaccuracy_virtual()
{
if (!this) //-V704
return 0.0f;
return call_virtual<float(__thiscall*)(void*)>(this, g_ctx.indexes.at(9))(this);
}
Последнее редактирование: