Jump to content

[SOLVED] Need help with some code, please.


 Share

Recommended Posts

What I am trying to do is giving back health to NPCs if they got hit in certain bones - this way I try to implement body part specific damage.

Example: if NPC01 was hit in the leg, check how much health it lost and give it back a part of it.

Somehow it wont work, though.

Here is the code I am using:

//modifiers for health not deducted for the affected body parts
float ini_legdamagemod = 0.99; // 99% of deducted health will be given back
float ini_armdamagemod = 0.99; // 99% of deducted health will be given back
float ini_torsodamagemod = 0.0; // 0% of deducted health will be given back
float ini_headdamagemod = 0.0; // 0% of deducted health will be given back
float ini_neckdamagemod = 0.0; // 0% of deducted health will be given back

//container for peds and a health value to calculate the body part damage
std::map<Ped, int> pedmaplimbhealth;

//container for affected bones
vector<int> legbones, armbones, torsobones, headbones, neckbones;

legbones.push_back(6884);
legbones.push_back(65478);
legbones.push_back(51120);
legbones.push_back(33646);
legbones.push_back(43312);

armbones.push_back(54187);
armbones.push_back(53675);
armbones.push_back(41357);
armbones.push_back(41341);
armbones.push_back(41405);
armbones.push_back(41326);
armbones.push_back(41325);
armbones.push_back(35876);
armbones.push_back(16829);
armbones.push_back(16781);
armbones.push_back(16765);
armbones.push_back(16749);
armbones.push_back(11300);

torsobones.push_back(14410);
torsobones.push_back(14411);
torsobones.push_back(14412);
torsobones.push_back(14413);
torsobones.push_back(14414);
torsobones.push_back(14415);

headbones.push_back(21030);

neckbones.push_back(14284);
neckbones.push_back(14285);
neckbones.push_back(14286);
neckbones.push_back(14287);
neckbones.push_back(14288);


while(true)
{
	if (MOD IS ENABLED)
    {
		//get all peds near player
		const int ARR_SIZE = 1024;
		Ped peds[ARR_SIZE];
		int count = worldGetAllPeds(peds, ARR_SIZE);
		
      	//filling the ped container
		for (int i = 0; i < count; i++)
        {
        	if (pedmaplimbhealth.find(peds[i]) == pedmaplimbhealth.end())
			{
				pedmaplimbhealth[peds[i]] = ENTITY::GET_ENTITY_HEALTH(peds[i]);
			}
        }
      
      	//checking if ped health has decreased and if so, give health back according to modifiers
		if (pedmaplimbhealth[peds[i]] > ENTITY::GET_ENTITY_HEALTH(peds[i]))
		{
			float damagedone = pedmaplimbhealth[peds[i]] - ENTITY::GET_ENTITY_HEALTH(peds[i]);
			int act_Bone;
			if (PED::GET_PED_LAST_DAMAGE_BONE(peds[i], &act_Bone))
			{
				for (vector<int>::size_type i = 0; i != legbones.size(); i++)
				{
					if (act_Bone == PED::GET_PED_BONE_INDEX(peds[i], legbones[i]))
					{
						int healthback = (int)(damagedone * ini_legdamagemod);
						if (ENTITY::GET_ENTITY_HEALTH(peds[i]) < ini_dyingmovementthreshold && (healthback + ENTITY::GET_ENTITY_HEALTH(peds[i])) > ini_dyingmovementthreshold) ENTITY::SET_ENTITY_HEALTH(peds[i], ini_dyingthreshold - 1, 0);
						else ENTITY::SET_ENTITY_HEALTH(peds[i], ENTITY::GET_ENTITY_HEALTH(peds[i]) + healthback, 0);
						pedmaplimbhealth[peds[i]] = ENTITY::GET_ENTITY_HEALTH(peds[i]);
						PED::CLEAR_PED_LAST_DAMAGE_BONE(peds[i]);
					}
				}
						
				for (vector<int>::size_type i = 0; i != armbones.size(); i++)
				{
					if (act_Bone == PED::GET_PED_BONE_INDEX(peds[i], armbones[i]))
					{
						int healthback = (int)(damagedone * ini_armdamagemod);
						if (ENTITY::GET_ENTITY_HEALTH(peds[i]) < ini_dyingmovementthreshold && (healthback + ENTITY::GET_ENTITY_HEALTH(peds[i])) > ini_dyingmovementthreshold) ENTITY::SET_ENTITY_HEALTH(peds[i], ini_dyingthreshold - 1, 0);
						else ENTITY::SET_ENTITY_HEALTH(peds[i], ENTITY::GET_ENTITY_HEALTH(peds[i]) + healthback, 0);
						pedmaplimbhealth[peds[i]] = ENTITY::GET_ENTITY_HEALTH(peds[i]);
						PED::CLEAR_PED_LAST_DAMAGE_BONE(peds[i]);
					}
				}
							
				for (vector<int>::size_type i = 0; i != torsobones.size(); i++)
				{
					if (act_Bone == PED::GET_PED_BONE_INDEX(peds[i], torsobones[i]))
					{
						int healthback = (int)(damagedone * ini_torsodamagemod);
						if (ENTITY::GET_ENTITY_HEALTH(peds[i]) < ini_dyingmovementthreshold && (healthback + ENTITY::GET_ENTITY_HEALTH(peds[i])) > ini_dyingmovementthreshold) ENTITY::SET_ENTITY_HEALTH(peds[i], ini_dyingthreshold - 1, 0);
						else ENTITY::SET_ENTITY_HEALTH(peds[i], ENTITY::GET_ENTITY_HEALTH(peds[i]) + healthback, 0);
						pedmaplimbhealth[peds[i]] = ENTITY::GET_ENTITY_HEALTH(peds[i]);
						PED::CLEAR_PED_LAST_DAMAGE_BONE(peds[i]);
					}
				}
							
				for (vector<int>::size_type i = 0; i != headbones.size(); i++)
				{
					if (act_Bone == PED::GET_PED_BONE_INDEX(peds[i], headbones[i]))
					{
						int healthback = (int)(damagedone * ini_headdamagemod);
						if (ENTITY::GET_ENTITY_HEALTH(peds[i]) < ini_dyingmovementthreshold && (healthback + ENTITY::GET_ENTITY_HEALTH(peds[i])) > ini_dyingmovementthreshold) ENTITY::SET_ENTITY_HEALTH(peds[i], ini_dyingthreshold - 1, 0);
						else ENTITY::SET_ENTITY_HEALTH(peds[i], ENTITY::GET_ENTITY_HEALTH(peds[i]) + healthback, 0);
						pedmaplimbhealth[peds[i]] = ENTITY::GET_ENTITY_HEALTH(peds[i]);
						PED::CLEAR_PED_LAST_DAMAGE_BONE(peds[i]);
					}
				}
							
				for (vector<int>::size_type i = 0; i != neckbones.size(); i++)
				{
					if (act_Bone == PED::GET_PED_BONE_INDEX(peds[i], neckbones[i]))
					{
						int healthback = (int)(damagedone * ini_neckdamagemod);
						if (ENTITY::GET_ENTITY_HEALTH(peds[i]) < ini_dyingmovementthreshold && (healthback + ENTITY::GET_ENTITY_HEALTH(peds[i])) > ini_dyingmovementthreshold) ENTITY::SET_ENTITY_HEALTH(peds[i], ini_dyingthreshold - 1, 0);
						else ENTITY::SET_ENTITY_HEALTH(peds[i], ENTITY::GET_ENTITY_HEALTH(peds[i]) + healthback, 0);
						pedmaplimbhealth[peds[i]] = ENTITY::GET_ENTITY_HEALTH(peds[i]);
						PED::CLEAR_PED_LAST_DAMAGE_BONE(peds[i]);
					}
				}
            }
        }
    }
}

 

 

For some reason the NPCs still keep dying from three or four leg shots (if health is set to 100).

Does anyone have any idea why that is the case?

Edited by HughJanus
Link to comment
Share on other sites

To better understand: You are setting the health back to 100 after every shot (and have verified it gets reset to it), but they will still die from a leg shot?

Link to comment
Share on other sites

44 minutes ago, LMS said:

To better understand: You are setting the health back to 100 after every shot (and have verified it gets reset to it), but they will still die from a leg shot?

 

I am first getting the health of the NPC and am saving it to the map (the map does not get touched after that).

Then, every script iteration I do a "get health" and check if its less than the value remembered in the map.

If so, I calculate the delta. Then I check the last bone that was damaged and multiply the delta according to the modifier for the specific body part and give that number of health back. (so e.g. if an NPC lost 10 health and the leg was last damaged, 9 health should be given back to him, if the modifier for the leg is 90% or 0.9).

After that, I set the map to the current health and the whole thing triggers again when the health is lowered.

Link to comment
Share on other sites

Just now, LMS said:

Are you able to confirm that when they die, their health is still high?

 

I havent checked, but I dont suppose that is the case.

I guess I made a mistake in the logic I use, but I cant seem to find it. I even wrote it down on paper, but it all checks out^^

Link to comment
Share on other sites

Now I have set some values to be shown on screen for the current target (the one the player is free aiming at / the last NPC he free aimed at).

Unbenannt.thumb.png.6de20e88c400195a4726e64485c62263.png

 

The damage done is the delta between the map and the current health - this increases steadily, the current health (ped health) decreases steadily. So it seems neither the map is updated, nor is the ped health increased.

The max health is the max health of the ped (is correct), the last dam bone is also correct (the "Legbone" is just a value of the legbones vector, to see if it even aligns with the last dam bone, so the code for increasing the health is (should be) executed).

To see if the code in those brackets (increasing health, updating the map) even triggers, I wrote a draw text function in each bracket (one for leg (YOLOYOLOYOLO_LEG), one for arm (YOLOYOLOYOLO_ARM), etc.) and the text gets drawn, so the health should also be increased and the map should be updated. But it doesnt happen for some reason.

I even tried setting the health to a fixed value to increase it to (ENTITY::SET_ENTITY_HEALTH(peds, 100)), but that doesnt change anything. Those two methods dont seem to work in those brackets:

pedmaplimbhealth[peds[i]] = ENTITY::GET_ENTITY_HEALTH(peds[i]);
ENTITY::SET_ENTITY_HEALTH
Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...