#include "stdafx.h"
#include "Hud.h"
#include <string>
using namespace std;

typedef unsigned char byte;

/*Defining Our Buttons*/
#define BUTTON_A                 "n 25"
#define BUTTON_B                 "n 23"
#define BUTTON_X                 "n 11"
#define BUTTON_Y                 "n 28"
#define BUTTON_RS                "n 3"
#define BUTTON_LS                "n 9"
#define BUTTON_RB                "n 5"
#define BUTTON_LB                "n 7"
#define BUTTON_RT                "n 1"
#define BUTTON_LT                "n 13"
#define BUTTON_DPADUP            "n 16"
#define BUTTON_DPADDOWN          "n 18"
#define BUTTON_DPADLEFT          "n 19"
#define BUTTON_DPADRIGHT         "n 21"
#define BUTTON_BACK              "n 30"
#define BUTTON_START             "n 27"
/*End of Defining Our Buttons*/

/* Defining Random Shit */
#define Thread DWORD WINAPI
#define NOP 0x60000000
/* End of Random Shit Defines */

/*Declaring Our Functions*/
unsigned long (_cdecl *ClientState)(int clientNum) = (unsigned long(*)(int))0x821E66A8;
int (_cdecl *G_ModelIndex)(const char* name) = (int(*)(const char*))0x8220CAD8;
int (_cdecl *SV_SetConfigString)(int index, const char* val) = (int(*)(int,const char*))0x822568A8;
void (_cdecl *SVGSSC)(int clientNum, int type, const char *text) = (void(*)(int,int,const char*))0x82254940;
bool (_cdecl *GetBool)(const char* DvarName) = (bool(*)(const char*))0x8229EF58;
int (_cdecl *LocalizeText)(const char* Text) = (int(*)(const char*))0x8220C838;
void (_cdecl *SV_Cmd_EndTokenizedString)() = (void(*)())0x82225368;
const char* (_cdecl *GetString)(const char* Dvar) = (const char*(*)(const char*))0x8229F118;
void (_cdecl *SV_Cmd_TokenizeString)(const char * text) = (void(*)(const char*))0x82225348;
void (_cdecl *ClientCommand)(DWORD clientIndex) = (void(*)(DWORD))0x821DC638;
int (_cdecl *IRand)(int min, int max) = (int(*)(int, int))0x8220E470;
int (_cdecl *G_TempEntity)(float*origin,int Event) = (int(*)(float*,int))0x8220EE88;
static void (_cdecl *G_GetPlayerViewOrigin)(int PlayerState, float* Origin) = (void(*)(int,float*))0x821D3960;
int (_cdecl* G_Spawn)() = (int(*)())0x8220DBE8;
int (_cdecl *SL_GetString)(const char* String, int User) = (int(*)(const char*,int))0x822422D8;
int (_cdecl *SV_DObjExists)(int gEntity) = (int(*)(int))0x822554E8;
int (_cdecl *G_DObjGetWorldTagMatrix)(int gEntity, int TagID, int Level) = (int(*)(int,int,int))0x8220D5C0;
DWORD (_cdecl* PlayerStates)(int clientNum) = (DWORD(*)(int))0x821E66A8;
void (_cdecl *Scr_Notify)(int ent, unsigned __int16 stringValue, unsigned int paramcount) = (void(*)(int, unsigned __int16, unsigned int))0x822097A8;
void (_cdecl *SetBoolFromSource)(dvar_s * dvar, bool value, DvarSetSource source) = (void(*)(dvar_s*, bool, DvarSetSource))0x822A04B8;
dvar_s *(_cdecl *FindVar)(const char * dvarName) = (dvar_s*(*)(const char*))0x8229EE40;
void (_cdecl *Scr_AddEntity)(unsigned int ent) = (void(*)(unsigned int))0x82209580;
void (_cdecl *Scr_AddFloat)(float value) = (void(*)(float))0x8224C450;
void (_cdecl *Scr_AddInt)(int value) = (void(*)(int))0x8224C3D0;
void (_cdecl *Scr_AddString)(const char* value) = (void(*)(const char*))0x8224C6A8;
void (_cdecl *Scr_AddVector)(float* Value) = (void(*)(float*))0x8224C850;
void (_cdecl *Scr_GetInt)(unsigned int index) = (void(*)(unsigned int))0x8224B6E8;
char* (_cdecl *game_va)(const char*,...) = (char*(*)(const char*,...))0x822A7008;
int (_cdecl *G_EffectIndex)(char* effectName) = (int(*)(char*))0x8220CEE0;
float (_cdecl *GetFloat)(const char *Dvar) = (float(*)(const char*))0x8229F028;
void (_cdecl *SV_KickClient)(void *cl, int, int, int, char * reasonText) = (void(*)(void*, int,int,int,char*))0x82251698;
void (_cdecl *EndGame_t)(void) = (void(*)(void))0x82201A48;
unsigned __int16 (_cdecl *Scr_ExecThread)(int handle, unsigned int paramcount) = (unsigned __int16(*)(int, unsigned int))0x822502F8;
void (_cdecl *IntermissionClientEndFrame)(int ent) = (void(*)(int))0x821D10E8;
void (_cdecl *G_SetAngle)(int Entity, float *Angle) = (void(*)(int, float*))0x8220E1C8;
void (_cdecl *G_SpawnHelicopter)(int ent, int owner, const char *vehicleInfoName, const char *modelName) = (void(*)(int, int, const char*, const char*))0x82315E48;
int (_cdecl *GetInt)(const char *Dvar) = (int(*)(const char*))0x8229EFC0;
void (_cdecl *SP_Script_Model)(DWORD Entity) = (void(*)(DWORD))0x82206E20;
void (_cdecl *SetModel)(int Entity, const char* ModelName) = (void(*)(int, const char*))0x8220D310;
void (_cdecl *ClientSpawn)(int ent, const float *spawn_origin, const float *spawn_angles) = (void(*)(int, const float*,const float*))0x821D3FB0;
void (_cdecl *ScriptMover_GravityMove)(int mover,float Velocity, float Time) = (void(*)(int, float,float))0x82206AC0;
int (_cdecl *GetWeaponIndex)(const char *name) = (int(*)(const char*))0x82210640;
int (_cdecl *G_GiveAmmo)(int clientEntity, unsigned int weaponIndex, char weaponModel, int count, int fillClip) = (int(*)(int, unsigned int, char, int, int))0x821E1F90;
unsigned int (_cdecl *BG_GetViewmodelWeaponIndex)(unsigned int PS) = (unsigned int(*)(unsigned int))0x8229EFC0;
void (_cdecl *G_GivePlayerWeapon)(unsigned int PS, int WIndex, int Camo, bool Akimbo) = (void(*)(unsigned int, int, int, bool))0x82210BC8;
void (_cdecl *TakeWeapon)(int ps, int weapindex, int ammo) = (void(*)(int,int,int))0x82210A28;
int (_cdecl *WeapID4Name)(const char*weapName) = (int(*)(const char*))0x82210640;
int (_cdecl *SV_LinkEntity)(int gEnt) = (int(*)(int))0x8225F588;
int (_cdecl *G_TagIndex)(const char*name) = (int(*)(const char*))0x8220CE38;
int (_cdecl *G_EntAttach)(int ClientEntity, const char *ModelName, int Tagname, int IgnoreCollision) = (int(*)(int, const char*, int, int))0x8220E7A0;
void (_cdecl *G_EntDetachAll)(int EntIndex) = (void(*)(int))0x8220EC68;
void (_cdecl *GScr_MapRestart)(void) = (void(*)(void))0x822015B0;
unsigned int(_cdecl *BG_GetPerkIndexForName)(const char *PerkName) = (unsigned int(*)(const char*))0x820D1700;
void (_cdecl *Scr_SetFxAngles)(int givenAxisCount, float (*axis)[3], float *angles) = (void(*)(int,float(*)[3],float*))0x821FB460;
void(_cdecl *G_SetModel)(int getEntity, const char* ModelName) = (void(*)(int, const char*))0x8220D310;
const char*(_cdecl *G_SoundAliasIndex)(const char*Name) = (const char*(*)(const char*))0x8220D048;
void (_cdecl *CG_FxTest)(int localClientNum) = (void(*)(int))0x82122268;
/*End of Declaring Our Functions*/

struct PlayerState_s{
	int CommandTime;
	char Unknown[0xC];
	int OtherFlags;
	char Unknown1[0x8];
	float* position;
	char Unknown2[0x128];
	int health;
	int Unknown3;
	int maxhealth;
	char Unknown4[0x30AC];
	char OldName[0x20];
	int health2;
	char Unknown5[0x10];
	float moveSpeedScaler;
	char Unknown6[0x40];
	int team;
	char Unknown7[0xC];
	char name[0x24];
	char Unknown8[0x8];
	int Perks;
	char Unknown9[0x160];
	int mFlags;
	char Unknown10[0x8];
	int buttons;
	int OldButtons;
	char Unknown11[0x2CC];
};

struct gEntity_s{
	int ClientIndex;
	int ClassName;
	char Unknown1[0x1C];
	float* OldPosition;
	char Unknown2[0x18];
	float* Angles;
	char Unknown3[0xE4];
	float* position;
	char Unknown4[0x14];
	PlayerState_s * PlayerState;
	char Unknown5[0x40];
	int health;
	char Unknown6[0xE0];
};

int getEntity(uint ClientIndex){
    return (0x82F03600 + (ClientIndex * 0x280));
}

uint Playerstate(int ClientID){
	return 0x830CBF80 + (ClientID *0x3700);
}

void SwitchToWeap(int i, const char* Weapon, byte WeaponIndex = 0){
	if(!WeaponIndex) SVGSSC(i, 1, game_va("a \"%i\"", GetWeaponIndex(Weapon)));
	else SVGSSC(i, 1, game_va("a \"%i\"", WeaponIndex));
}

void GiveWeapon(int i, const char* Weapon, bool akimbo, bool SwitchToWeapon, byte Camo = 0){
	if(*(PINT)(getEntity(i) + 0x19C) <= 0) return;
	int WeaponID = GetWeaponIndex(Weapon);
	((void(_cdecl*)(uint, int, int, bool))0x82210BC8)(Playerstate(i), WeaponID, Camo, akimbo);
	if(SwitchToWeapon) SwitchToWeap(i, Weapon);
	((int(_cdecl*)(int, uint, char, int, int))0x821E1F90)(getEntity(i), WeaponID, 0, 999, 1);
}