• Register
Forum Thread
  Posts  
Help coding a "hook-like" weapon (Forums : Coding & Scripting : Help coding a "hook-like" weapon) Locked
Thread Options
Feb 2 2015 Anchor

I'm working on a gold source mod (mortal kombat), and I need some help with my code to make it attract the people it hits.

It's currently based on the crossbow code for it, and added an extra "think", so when it hits a player it retracts and when it doesn't, well, doesn't.

void CSpear::DetectRetract( void )
{
TraceResult tr = UTIL_GetGlobalTrace( );
Vector vecSpot;// trace starts here!
vecSpot = pev->origin + Vector ( 0 , 0 , 32 );
CBaseEntity *pEntity = NULL;
pevAttacker = pev;
Vector desudesu;
desudesu = pevAttacker->origin + Vector( 16, 16, 16);

while ((pEntity = UTIL_FindEntityInSphere( pEntity, vecSpot, 64)) != NULL) // Look for entities on a radius
{
if ( pEntity->pev->takedamage != DAMAGE_NO )
{
if ( pEntity->IsPlayer() ) // Checks if the entity is a player
{
CBasePlayer *pVictim = (CBasePlayer *)pEntity;
CBasePlayerWeapon *pCurrentWeapon = (CBasePlayerWeapon *)(pVictim->m_pActiveItem);
if ( pVictim->pev->health<45) // Check if he has less than 45 hp
{
switch( RANDOM_LONG(0,4) )
{
case 0:
EMIT_SOUND(ENT(pev), CHAN_WEAPON, "scorpion/scorpion_wins1.wav", 1, ATTN_NORM); break;
case 1:
EMIT_SOUND(ENT(pev), CHAN_WEAPON, "scorpion/scorpion_wins2.wav", 1, ATTN_NORM); break;
case 2:
EMIT_SOUND(ENT(pev), CHAN_WEAPON, "scorpion/scorpion_wins3.wav", 1, ATTN_NORM); break;
case 3:
EMIT_SOUND(ENT(pev), CHAN_WEAPON, "scorpion/scorpion_wins4.wav", 1, ATTN_NORM); break;
case 4:
EMIT_SOUND(ENT(pev), CHAN_WEAPON, "scorpion/scorpion_wins5.wav", 1, ATTN_NORM); break;
};
}
else						// If his Hp is over than 10
{	
		switch( RANDOM_LONG(0,1) )
		{
		case 0:
			EMIT_SOUND(ENT(pev), CHAN_WEAPON, "scorpion/get_over_here.wav", 1, ATTN_NORM); break;
		case 1:
			EMIT_SOUND(ENT(pev), CHAN_WEAPON, "scorpion/come_here.wav", 1, ATTN_NORM); break;
		};
while (( pVictim = UTIL_FindEntityInSphere( pevAttacker, Vector( 0, 0, 0), 900)) != NULL)
{
pVictim->movetype  = MOVETYPE_PUSH;
pVictim->movedir = desudesu;
pVictim->speed = GET_OVER_HERE_SPEED * 50;
}		
			
}
}
}
}

I'm currently very noob at coding, and yes this looks like a mess. What I tried to do there was this

Spear hits player
player hit detects where the spear came from (attacker origin)
Spear forces player to move in direction of the attacker

That's pretty much what I want it to do D: But it doesn't seem to work. (Also, sounds yay)

I really need help D:

Thanks in advance.

Feb 4 2015 Anchor

Hello,
I am also noob in coding, but I still want to try to help you :)

Now you didn't declare the speed at the bottom, with the *50, is that an OOP thing? Because I only have commandline experience.

Also what does the ' -> ' mean?

And there's different ways of programming the hook(I think), you could program it as a PULL to shooter, which you did.
If you program it as a pull, make sure you keep pulling until you're at (shooters coordinates + certain distance), or else it might be an infinite hook or pull him into the shooter(just making things up, I don't know if this will actually happen).
I'm just assuming there should be a piece of code that starts the hook and a piece that stops the hook.

Or you can program it as, check what the distance traveled by hook was, PUSH the player that was hit by hook towards the shooter by:
1. (hook distance travel - distance of player width) in case you programmed the hook from the middle of the shooter //otherwise you will pull him into yourself.
2. (hook distance travel) in case you programmed the hook from where the hook visually starts. //which will be outside of the body of the shooter.

Also, it looks to me like you programmed it to pull ONLY when the player that gets hooked is under 45 hp.
Or does the hook do 45 damage and kill when you pull them?
You programmed win the round sounds for under 45, but pull sounds for over 10 hp?

Try using more enters bro, a tip from a fellow noob :)
Also, if you use 2 if statements, shouldn't the second one be "else if"?

Also, I think you need to declare:
pVictim->movetype = MOVETYPE_PUSH;
pVictim->movedir = desudesu;
pVictim->speed = GET_OVER_HERE_SPEED * 50;
and move it up, because now you put it in the codeblock where it will only execute if people are over 10 hp, that means it should work, but I would move it up.

I really hope this helps, maybe they're bad idea's, but I usually get idea's from other people's idea's.
At least I tried :P good luck!

Feb 4 2015 Anchor

I really really appreciate your help. I'm going to use the second idea, the one that brings the victim to the player but the first statement.

I would like to know how to get the distance that the hook has traveled minus the origin (which I already know).

Also the -> is used to attribute something to an entity.

pVictim->movetype = MOVETYPE_PUSH

Would mean that the way the victim is going to move from now on is as if it's being pushed.
I really hope to get more help on this topic. So I can know more. Thank you so much.

Feb 16 2015 Anchor

Hello Ian,
Sorry for not replying for such a long time, my account got banned falsely, it just got unbanned.
I still want to help you though.
Now the way I see it there's 2 ways of doing this.
1. you hit someone? calculate distance the hook travelled, use that distance to push victim forward (less handy imo)
2. you hit someone? pull him infront of yourself, which will always be your coordinates + a certain length infront of you (this is handier imo)

So I'll give some examples per method:
1. i hit my target, my hook traveled 6 meters, my victim moves(gets dragged) 6 meters
2. i hit my target, pull that target 0,5 meter infront of me

I hope this is clear enough :)

also I think you misplaced a ";" right after case 4, you put it after the first "}", shouldn't that be after the 2nd "}" ?

If you have any questions, feel free to ask here or pm :D

Feb 16 2015 Anchor

Oh ok.

thanks for it, I still don't know how to track the distance traveled, and can't get the player who shot coordinates from a weapon ".cpp", nor the enemy's coordinates.

I would like some help about this.

Mar 1 2015 Anchor

Hello Ian,

I'm sorry bro, but that knowledge is out of my reach, like I said before, I'm new to programming as well.
Have you tried looking it up? or asking someone else? :)

If you have any other questions, I'm still willing to help :)

Mar 1 2015 Anchor

I have no idea how to do it, nor where to find help for it, as most Half Life/Gold Source Coding tutorials are mostly dead, and the ones I could find where to add things I don't really need like, flashbang in Half Life, TFC styled Grenades and stuff like that. That's why I made this thread to see if people in here have knowledge about this specific thing D:

Mar 7 2015 Anchor

Dizastor wrote: Hello Ian,
Sorry for not replying for such a long time, my account got banned falsely, it just got unbanned.
I still want to help you though.
Now the way I see it there's 2 ways of doing this.
1. you hit someone? calculate distance the hook travelled, use that distance to push victim forward (less handy imo)
2. you hit someone? pull him infront of yourself, which will always be your coordinates + a certain length infront of you (this is handier imo)

So I'll give some examples per method:
1. i hit my target, my hook traveled 6 meters, my victim moves(gets dragged) 6 meters
2. i hit my target, pull that target 0,5 meter infront of me

I hope this is clear enough :)

also I think you misplaced a ";" right after case 4, you put it after the first "}", shouldn't that be after the 2nd "}" ?

If you have any questions, feel free to ask here or pm :D


Dosnt the Barnacle does the "calculate distance" thingy? Maybe theres something in its code about "get player close to me" that can be used or taken as reference for this?

Mar 8 2015 Anchor
I checked the barnacle code before, as it was the first thought that came to my mind when looking for something that "Attracts" in the Half life's engine. Sadly I couldn't reproduce it's effect as it has other things to count. For example the "attract function" in the barnacle code, checks the (barnacle's) model size to know where the neck of the prey is to know where to begin damaging the prey. I might not be that hard to turn it into a weapon, but considering the barnacle is a separate entity, and a weapon is an entity held by(or that depends on) another separate entity. It sounds quite hard. Remember I'm still learning, and would like to get as much help as I can.

I made it work :D

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.