Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Forum Support

    3. Suggestions

[SA][C++]CCopPed Constructor Patch


B1ack_Wh1te
 Share

Recommended Posts

Hi.

How do I patch the CCopPed constructor?

I wrote the code, but the game crashes.

#include "plugin.h"
#include "CStreaming.h"

using namespace plugin;

void _CPed(CPed *obj, ePedType pedType) {
	((void (__thiscall *)(CPed *, ePedType))0x5E8030)(obj, pedType);
}

void CAEPedSpeechAudioEntity_Init(CAEPedSpeechAudioEntity *obj, CPed *ped) {
	((void (__thiscall *)(CAEPedSpeechAudioEntity *, CPed *))0x4E68D0)(obj, ped);
}

void _CCopPed(CCopPed *copPed, eCopType copType)
{
	_CPed(copPed, PED_TYPE_COP);
	copPed->m_copType = copType;
	switch (copType) {
	case COP_TYPE_CITYCOP:
		copPed->SetModelIndex(CStreaming::GetDefaultCopModel());
		copPed->GiveWeapon(WEAPON_NIGHTSTICK, 1000, true);
		copPed->GiveDelayedWeapon(WEAPON_PISTOL, 1000);
		copPed->m_nActiveWeaponSlot = 0;
		copPed->m_fArmour = 0.0f;
		copPed->m_nWeaponShootingRate = 30;
		copPed->m_nWeaponAccuracy = 60;
		break;
	case COP_TYPE_LAPDM1:
		copPed->SetModelIndex(MODEL_LAPDM1);
		copPed->GiveWeapon(WEAPON_NIGHTSTICK, 1000, true);
		copPed->GiveDelayedWeapon(WEAPON_PISTOL, 1000);
		copPed->m_nActiveWeaponSlot = 0;
		copPed->m_fArmour = 0.0f;
		copPed->m_nWeaponShootingRate = 30;
		copPed->m_nWeaponAccuracy = 60;
		break;
	case COP_TYPE_SWAT1:
	case COP_TYPE_SWAT2:
		copPed->SetModelIndex(MODEL_SWAT);
		copPed->GiveDelayedWeapon(WEAPON_MICRO_UZI, 1000);
		copPed->SetCurrentWeapon(WEAPON_MICRO_UZI);
		copPed->m_fArmour = 50.0f;
		copPed->m_nWeaponShootingRate = 70;
		copPed->m_nWeaponAccuracy = 68;
		break;
	case COP_TYPE_FBI:
		copPed->SetModelIndex(MODEL_FBI);
		copPed->GiveDelayedWeapon(WEAPON_MP5, 1000);
		copPed->SetCurrentWeapon(WEAPON_MP5);
		copPed->m_fArmour = 100.0f;
		copPed->m_nWeaponShootingRate = 60;
		copPed->m_nWeaponAccuracy = 76;
		break;
	case COP_TYPE_ARMY:
		copPed->SetModelIndex(MODEL_ARMY);
		copPed->GiveDelayedWeapon(WEAPON_M4, 1000);
		copPed->SetCurrentWeapon(WEAPON_M4);
		copPed->m_fArmour = 100.0f;
		copPed->m_nWeaponShootingRate = 80;
		copPed->m_nWeaponAccuracy = 84;
		break;
	case COP_TYPE_CSHER:
		copPed->SetModelIndex(MODEL_CSHER);
		copPed->GiveWeapon(WEAPON_NIGHTSTICK, 1000, true);
		copPed->GiveDelayedWeapon(WEAPON_PISTOL, 1000);
		copPed->m_nActiveWeaponSlot = 0;
		copPed->m_fArmour = 0.0f;
		copPed->m_nWeaponShootingRate = 30;
		copPed->m_nWeaponAccuracy = 60;
		break;
	default:
		copPed->SetModelIndex(copType);
		copPed->GiveWeapon(WEAPON_NIGHTSTICK, 1000, true);
		copPed->GiveDelayedWeapon(WEAPON_PISTOL, 1000);
		copPed->m_nActiveWeaponSlot = 0;
		copPed->m_fArmour = 0.0f;
		copPed->m_nWeaponShootingRate = 30;
		copPed->m_nWeaponAccuracy = 60;
	}
	copPed->field_79C = 0;
	copPed->field_74C = 0;
	copPed->field_7A4 = 0;
		CEntity *ent = copPed->m_pTargetedObject;
		if (copPed->m_pTargetedObject)
			copPed->m_pTargetedObject->CleanUpOldReference(&ent);
	copPed->m_pTargetedObject = nullptr;
	CAEPedSpeechAudioEntity_Init(&copPed->m_pedSpeech, copPed);
	copPed->m_pIntelligence->m_fDmRadius = 60.0f;
	copPed->m_pIntelligence->m_nDmNumPedsToScan = 8;

	copPed->m_pCopPartner = 0;
	copPed->m_apCriminalsToKill[0] = 0;
	copPed->m_apCriminalsToKill[1] = 0;
	copPed->m_apCriminalsToKill[2] = 0;
	copPed->m_apCriminalsToKill[3] = 0;
	copPed->m_apCriminalsToKill[4] = 0;
	copPed->field_7C0 = 0;
}

class _CopPed {
public:
	_CopPed() {
		Events::gameProcessEvent += [] {
			patch::RedirectJump(0x5DDC60, _CCopPed);
		};
    }
} _copPed;

What have I done wrong?

Link to comment
Share on other sites

I don't know (because i don't know nothing of coding (except Sanny Builder)), but what are you trying to do my friend?

Link to comment
Share on other sites

On 12/1/2018 at 11:28 AM, Gh1baudi said:

I don't know (because i don't know nothing of coding (except Sanny Builder)), but what are you trying to do my friend?

I want to rewrite the CCopPed class constructor to edit it later. This constructor is installing the model for the police.

Link to comment
Share on other sites

7 hours ago, Gh1baudi said:

What do you mean by installing the model of the police?

I mean, setting up a model id for a cop.

7 hours ago, Gh1baudi said:

Different Cops models?

Yes, I want the SWAT to have 2 models, not just one (285). To do this, I need to change other functions, but first I need to rewrite the constructor for the original code.

Link to comment
Share on other sites

Thanks, I wrote the constructor under thiscall, but the game still crashes.

// CCopPed.h

void _CCopPed(eCopType copType) {
	((void (__thiscall *)(CPed *, ePedType))0x5E8030)(this, PED_TYPE_COP); // CPed::CPed(PED_TYPE_COP);
	this->m_copType = copType;
	switch (copType) {
	case COP_TYPE_CITYCOP:
		this->SetModelIndex(CStreaming::GetDefaultCopModel());
		this->GiveWeapon(WEAPON_NIGHTSTICK, 1000, true);
		this->GiveDelayedWeapon(WEAPON_PISTOL, 1000);
		this->m_nActiveWeaponSlot = 0;
		this->m_fArmour = 0.0f;
		this->m_nWeaponShootingRate = 30;
		this->m_nWeaponAccuracy = 60;
		break;
	case COP_TYPE_LAPDM1:
		this->SetModelIndex(MODEL_LAPDM1);
		this->GiveWeapon(WEAPON_NIGHTSTICK, 1000, true);
		this->GiveDelayedWeapon(WEAPON_PISTOL, 1000);
		this->m_nActiveWeaponSlot = 0;
		this->m_fArmour = 0.0f;
		this->m_nWeaponShootingRate = 30;
		this->m_nWeaponAccuracy = 60;
		break;
	case COP_TYPE_SWAT1:
	case COP_TYPE_SWAT2:
		this->SetModelIndex(MODEL_SWAT);
		this->GiveDelayedWeapon(WEAPON_MICRO_UZI, 1000);
		this->SetCurrentWeapon(WEAPON_MICRO_UZI);
		this->m_fArmour = 50.0f;
		this->m_nWeaponShootingRate = 70;
		this->m_nWeaponAccuracy = 68;
		break;
	case COP_TYPE_FBI:
		this->SetModelIndex(MODEL_FBI);
		this->GiveDelayedWeapon(WEAPON_MP5, 1000);
		this->SetCurrentWeapon(WEAPON_MP5);
		this->m_fArmour = 100.0f;
		this->m_nWeaponShootingRate = 60;
		this->m_nWeaponAccuracy = 76;
		break;
	case COP_TYPE_ARMY:
		this->SetModelIndex(MODEL_ARMY);
		this->GiveDelayedWeapon(WEAPON_M4, 1000);
		this->SetCurrentWeapon(WEAPON_M4);
		this->m_fArmour = 100.0f;
		this->m_nWeaponShootingRate = 80;
		this->m_nWeaponAccuracy = 84;
		break;
	case COP_TYPE_CSHER:
		this->SetModelIndex(MODEL_CSHER);
		this->GiveWeapon(WEAPON_NIGHTSTICK, 1000, true);
		this->GiveDelayedWeapon(WEAPON_PISTOL, 1000);
		this->m_nActiveWeaponSlot = 0;
		this->m_fArmour = 0.0f;
		this->m_nWeaponShootingRate = 30;
		this->m_nWeaponAccuracy = 60;
		break;
	default:
		this->SetModelIndex(copType);
		this->GiveWeapon(WEAPON_NIGHTSTICK, 1000, true);
		this->GiveDelayedWeapon(WEAPON_PISTOL, 1000);
		this->m_nActiveWeaponSlot = 0;
		this->m_fArmour = 0.0f;
		this->m_nWeaponShootingRate = 30;
		this->m_nWeaponAccuracy = 60;
	}
	this->field_79C = 0;
	this->field_74C = 0;
	this->field_7A4 = 0;
	CEntity *entity = this->m_pTargetedObject;
	if (this->m_pTargetedObject)
		this->m_pTargetedObject->CleanUpOldReference(&entity);
	this->m_pTargetedObject = nullptr;
	((void (__thiscall *)(CAEPedSpeechAudioEntity *, CPed *))0x4E68D0)(&this->m_pedSpeech, this); // CAEPedSpeechAudioEntity::Initialise(CPed *)
	this->m_pIntelligence->m_fDmRadius = 60.0f;
	this->m_pIntelligence->m_nDmNumPedsToScan = 8;
	this->m_pCopPartner = 0;
	this->m_apCriminalsToKill[0] = nullptr;
	this->m_apCriminalsToKill[1] = nullptr;
	this->m_apCriminalsToKill[2] = nullptr;
	this->m_apCriminalsToKill[3] = nullptr;
	this->m_apCriminalsToKill[4] = nullptr;
	this->field_7C0 = 0;
}
// _CopPed.cpp

#include "plugin.h"
#include "CCopPed.h"

using namespace plugin;

template <typename T>
void * GetMethod(T TMethod) // return void * __thiscall function
{
	union
	{
		T TValue;
		void * Address;
	} retn = {TMethod};
	return retn.Address;
}

class _CopPed {
public:
	_CopPed() {
		Events::gameProcessEvent += [] {
			patch::RedirectJump(0x5DDC60, GetMethod(&CCopPed::_CCopPed));
		};
    }
} _copPed;
Edited by Dyabda
Link to comment
Share on other sites

I guess you should find out WHERE it crashes and see how that happened. Personally, instead of casting sh*t to thiscalls all the time I just declare the classes (as dummies if necessary) a the methods I'm using.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.