Talk:Fixing AI in multiplayer: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
Line 38: Line 38:
--Thomas
--Thomas
----
----
:I was not able to get models other than HL2DM to load without crashing the gameI'm sure I'm just not doing it right. I've resorted to changing the appid to 218, and then manually extracting all the HL2DM content OTHER THAN MODELS to my mod directory.  It's working but I'd rather use appid 320 and override just the models as described instead if I can figure it out.  Would anyone be interested in typing a more detailed (and up to date with impact of the latest SDK) explination of what needs done to get models with the correct animations for AI pulled into the mod? - '''[[User:schroe2a|schroe2a]]''' 1 July 2008
:For me the best way to deal with animations has been to replace the player modelsYou can decompile models from the singleplayer, update the .qc file to point to the hl2dm animations (they are provided in the sdk) and re-compile them and put them in your mod's directory.  Remember, if you replace the player models, then you want the HL2 singleplayer content to take precidence when mounting content, so make sure you mount them in the right order. --[[User:Schroe2a|Andy]] 22:43, 27 February 2009 (UTC)
 
== Didn't work ==
== Didn't work ==



Revision as of 15:43, 27 February 2009

I'm not 100% sure, but I think this sentence was wrong (so I corrected it, I hope): "With this table NPCs won't know which entities to hate/like" --Andreasen 07:22, 26 Aug 2006 (PDT)

You are right--dutchmega 07:36, 26 Aug 2006 (PDT)

why combine can not shoot ?? --Test 05:19, 30 Sep 2006 (PDT)

Some more info would be nice --dutchmega 12:37, 30 Sep 2006 (PDT)
Agree --Rookie 04:57, 4 Oct 2006 (PDT)

this Fixing AI in multiplayer cannot make a combine shoot ?? or u can post full code ? plz --Test 09:14, 1 Oct 2006 (PDT)

Translated: "This tutorial cannot make a combine shoot. Can you explain why they can't do this, or post the neccessary code for fixing this, please?" --Andreasen 05:19, 4 Oct 2006 (PDT)
Anyway,I want to make a combine shoot but i can't find any tutorial for my problem.Also the zombie make clash my mod when i spawn it. --Rookie 08:16, 4 Oct 2006 (PDT)
"Make clash"? You are saying that the zombie crashes the game when it is spawned? Are you from Japan or something? --Andreasen 08:37, 4 Oct 2006 (PDT)
Yes it crash my server but fast zombie doesn't.ONLY the zombie.(I am a hong kong secondary school student,what the clash and crash different? ) Rookie

I can't post the entire code.. What do the Combine do? Do they shoot with good animations but bullets don't hit? They have no animations but bullets hit? Or something else? And if you can't debug crashes, I'm not sure you should even be allowed near the HL2 modcode... :P --dutchmega 09:20, 4 Oct 2006 (PDT)

Judging by how the article ends, in some kind of "This isn't really an article about fixing the AI in multiplayer.", I think it's more a question about finishing the article. I think he has done as the article says, but that that isn't enough to fix things. --Andreasen 01:57, 5 Oct 2006 (PDT)
Agree AgreeRookie

Hey Thomas...

I found my way to this tutorial (and the previous one) after accomplishing the same basic thing, but I am still having trouble. I cant seem to get any of the combine soldiers use thier weapons. They hit (using meylee attack), but cannot use thier weapons. So this cant be a relationship problem. Is it a problem of them just not recognizing the HL2mp weapons or have i just not included the correct ai code??



Buddy

This problem is not actualy due to anything related to relationships or whatever: this is caused by HL2DM animations! Indeed, the issue is due to the Combine Soldiers' animations. You have to move default HL2DM players animations to an other directory then you add Half-Life 2 SP character models in the MODDIR\models folder. You eventually have to edit the HL2DM code to change the player models names.

NOTE: this fix is very hard to do, you have to Hex-edit the player models to make them work! Then everything should work. Good luck!

--Thomas


For me the best way to deal with animations has been to replace the player models. You can decompile models from the singleplayer, update the .qc file to point to the hl2dm animations (they are provided in the sdk) and re-compile them and put them in your mod's directory. Remember, if you replace the player models, then you want the HL2 singleplayer content to take precidence when mounting content, so make sure you mount them in the right order. --Andy 22:43, 27 February 2009 (UTC)

Didn't work

That didn't seem to work. I tried what you said, and I just repaced all the model/animation files with the right ones. That didn't work. So, I extracted all of the models from the HL2 GFC and put them in my mod's model folder. Still didn't work. Finally, I extracted all of Half-Life 2 and just left the server and client DLL in, but it still didn't work. I no longer get errors when in developer mode about missing animations on the NPC's, but the combine just stand around. The "police" will chase after me, but they won't hit me or shoot me. It could be a weapon problem, maybe an ammo problem. I think they don't know how to use thier weapons. Please help. --General Eskimo


General Eskimo

This problem is not actualy due to anything related to the code or the weapons. Metropolices won't attack you because this is a HL2DM SDK issue. Many programmers did not manage to fix it. You can make Combine Soldiers work, but Metropolices won't work! Thus I suggest you to work with others NPCs than Metropolices. Anyway, I will try to find a way to make them work! Then I'll provide you the trick! Yours.

--Thomas

No NPC's can shoot

It is not just the metropolice. All NPC's that carry weapons will not shoot. Is there some way I can make sure they have amunition? Something is definately not working other than just the Police. ^Ben

This is simply because MP code removed the NPC shot handling code...you can simply retrieve the hl2 code for the weapon and copy over the animation handling code—ts2do 17:39, 12 Feb 2006 (PST)

If anybody knows where this code is, please let us know. I have searched over the codebase but can't find any clues to where it may be. ^Ben

This code is in the singleplayer code...like I just said...what do you have a problem finding?—ts2do 16:52, 13 Feb 2006 (PST)

Well at the moment I am using the weapon code from HL2MP and I'm using it in conjunction with the HL2MP files(in the hl2mp_dll referencing all the header files for HL2MP) the NPC's run around and they "dry" fire, so they basically have the shooting animation and the muzzle flash and shells work, but no actual shots. It has me stumped. ^Ben 18:06, 13 Feb 2006 (PST)

So what I'm saying is copy over specific animation event handling stuff from the singleplayer weapon code so they do shoot..—ts2do 19:09, 13 Feb 2006 (PST)

This is where I am confused, I have got all the animation code that I can think of from single player, I am currently using the singleplayer SMG1 code, I'm also using the singleplayer combine code. Is there any other animation handling code I should belooking for.


Edit from Thomas

In have an idea. We should check if the bullet-shots are not triggered by a specific event called in the animation. I mean we could have an event called at a specific frame in the shooting animation, and then the game deals with it to make the NPC fire. What do you think about that speculation?

Yours.


(Generaleskimo)- well, why is it that the snipers in c17 will shoot at you? are they NPCs? I cant even seem to find the link between the animation in the HL2 code and the code for firing the weapon for NPCs. Under one of the "firebullet" functions it says "only the player uses this", so what shooting code does the npc use? (Generaleskimo)

The code specifically uses weapon based animations and animation handling events declared in the weapon code to fire—ts2do 11:31, 26 Feb 2006 (PST)


This is in the singleplayer SMG code.

	void Operator_HandleAnimEvent( animevent_t *pEvent, CBaseCombatCharacter *pOperator )
	{
		switch( pEvent->event )
		{
			case EVENT_WEAPON_SMG1:
			{
				Vector vecShootOrigin, vecShootDir;
				QAngle angDiscard;
				if ((pEvent->options == NULL) || (pEvent->options[0] == '\0') ||  (!pOperator->GetAttachment(pEvent->options, vecShootOrigin, angDiscard)))
				{
					vecShootOrigin = pOperator->Weapon_ShootPosition();
				}

				CAI_BaseNPC *npc = pOperator->MyNPCPointer();
				ASSERT( npc != NULL );

				vecShootDir = npc->GetActualShootTrajectory( vecShootOrigin );

				// FIXME: use the returned number of bullets to account for >10hz firerate
				WeaponSoundRealtime( SINGLE_NPC );

				CSoundEnt::InsertSound( SOUND_COMBAT, pOperator->GetAbsOrigin(), SOUNDENT_VOLUME_MACHINEGUN, 0.2,  pOperator );
				pOperator->FireBullets( 1, vecShootOrigin, vecShootDir, VECTOR_CONE_PRECALCULATED,
					MAX_TRACE_LENGTH, m_iPrimaryAmmoType, 2, entindex(), 0 );
				pOperator->DoMuzzleFlash();
				m_iClip1 = m_iClip1 - 1;
	 		}
			break;
			
			/*//FIXME: Re-enable
			case EVENT_WEAPON_AR2_GRENADE:
			{
				CAI_BaseNPC *npc = pOperator->MyNPCPointer();

				Vector vecShootOrigin, vecShootDir;
				vecShootOrigin = pOperator->Weapon_ShootPosition();
 				vecShootDir = npc->GetShootEnemyDir( vecShootOrigin );

 				Vector vecThrow = m_vecTossVelocity;

				CGrenadeAR2 *pGrenade = (CGrenadeAR2*)Create( "grenade_ar2", vecShootOrigin, vec3_angle, npc );
				pGrenade->SetAbsVelocity( vecThrow );
				pGrenade->SetLocalAngularVelocity( QAngle( 0, 400, 0 ) );
				pGrenade->SetMoveType( MOVETYPE_FLYGRAVITY ); 
				pGrenade->m_hOwner			= npc;
				pGrenade->m_pMyWeaponAR2	= this;
				pGrenade->SetDamage(sk_npc_dmg_ar2_grenade.GetFloat());

	 			// FIXME: arrgg ,this is hard coded into the weapon???
				m_flNextGrenadeCheck = gpGlobals->curtime + 6;// wait six seconds before even looking again to see  if a grenade can be thrown.

				m_iClip2--;
			}
			break;
			*/
			
			default:
				BaseClass::Operator_HandleAnimEvent( pEvent, pOperator );
				break;
	 	}
	}

^Ben 12:23, 26 Feb 2006 (PST)


Edit from Thomas

Does your code fix the problem? It doesn't fix anything for me :-(

Yours

Edit from Thomas

Probem solved. NPCs can shoot now. Report tge other bugs ;o) Yours

--

Edit by dutchmega Added some little notes and 1 include. It works now here :)

--

THOMAS: OK! Thanks ;o)

One Last Problem...

There is one last problem. How do you get the ai to bleed. It works if cl_predict is 0, so it is something as simple as adding a prediction table. I when I figured that out, I thought "oh, so it's really simple, right?", but I have been having some trouble getting it to work. Help me make my ai bleed. I love the blood. It's all... good-like. --General Eskimo

EDIT FROM THOMAS: General Eskimo, I don't know how to help, sorry :-|

Metropolice with stunstick

How the hell do you fix metropolice with a stunstick? They run towards me but it can't hit me really. I also don't see the animation for it...

EDIT FROM THOMAS: Well, there is no way to fix that damn metropolice, I hope Valve will do something.

Well, it's is fixed in the mod Synergy but it's coder isn't very helpfull.. -- dutchmega

Ts2Do's Suggestion

Thank you for your tip Ts2Do. It really helps. By the way, I can't specify that instruction

m_flexWeight[index] = value;

I must let the original one as the compiler generates errors with your code. Any idea? Yours.

--Thomas


And is the rest of the CBaseFlex class usefull? It would save more network bandwidth ;) --dutchmega

It worked for me for a couple of days, then, without changing anything (i was really tired) i recompiled and it has an error! Just leave that line alone for now, it doesn't get as much network traffic as the other one. I am not looking at the code right now, but it is just an integer right? Works fine if that transmits. --General Eskimo

The changes to the flex array sending has made certain npcs that have eyes look in the wrong direction all the time, at least from what I see in game.--Skidz 16:22, 16 May 2006 (PDT)

Thumbs up

This code works pefectly if you don't happen to make stupid misstakes like i did. :D TheSpaceMan 07:37, 24 Jun 2006 (PDT)

This article is about generally fixing AI in multiplayer. If you really want to fix all NPCs, this article gets 5-10 times longer... Anyway, if you think I've forget something, feel free to add it... --dutchmega 06:05, 5 Oct 2006 (PDT)

"Function calls". --Rookie
My combine in my mod have they animations but they don't shoot,just throw the flags and hit player with gun when player close to them.(not shooting but hurt,i don't know what word i should use--Rookie

Yes, it probably would be longer, but then we could divide it into sub pages if neccessary. Co-op is very, very fun once implemented, and Sven Coop 2 seems like it will take forever to finish. Of course we lack the coding skills to do this ourselves, which is why we turn to the wiki to tell us what we should add and not. If we could do this by ourselves, we wouldn't need the wiki for it. --Andreasen 07:47, 5 Oct 2006 (PDT)

I love being a jerk. Last time I counted, there were over 3000 calls that need fixing. Anyway, about the Combine not shooting. It should work but should is a nasty word. Try my weapon_smg1.cpp. If that doesn't work, I'll look if the article misses something after doing my Java-homework --dutchmega 09:28, 5 Oct 2006 (PDT)
3000 calls?? Then I understand you. I thought it was under 25. --Andreasen 10:00, 5 Oct 2006 (PDT)
I've put that last sentence in the article for a reason :) --dutchmega 12:29, 5 Oct 2006 (PDT)
Replace the weapon_smg1.cpp with this one the combine still don't shoot. :( -- Rookie


Is it just me or is this no longer working? I have copied HL2's AIRelationships function and it compiles fine, and made sure I have the EP1 models, but not even simple zombies work. Only seconds after adding them into the game with the npc_create command, it crashes to desktop. If I load an existing map that already has zombies, it crashes to desktop before it finishes loading the level. Just trying to get zombies to work for now. Did I miss something? --Wildfire 22:16, 12 Feb 2007 (PST)

Nope it doesn't work anymore. I could imagine it being model related - the crashes. --unsigned

This article is way too outdated and very misleading especially to new coders. Someone please either attend to it or just put a warning on top of the article. --TwL 17:08, 6 Dec 2007 (PST)
I've got simple NPCs working, but the basic zombies crash if they take melee or physics damage still and I can't figure out why. It would be great if someone who knew what they were doing could put some work into this article, it gets linked to quite a bit from the Steam forums. --IncompetentFool 13:23, 18 Dec 2007 (PST)

Patch

Hey all, I've added a link to a .patch file that implements everything described in this article, including the hundreds of function calls that the article only mentions in passing! I've done this not to remove the need for a brain or code understanding, but to save people the bother of replacing hundreds of function calls in a consistant and sensible manner, which took me a couple of 'fun-filled' days! Feedback & suggestions appreciated. Also, I've had to upload it onto my own webspace, as the wiki won't let me upload a .patch or a .zip file, it only seems to want to accept image files for upload. It doesn't bother me as such, but does anyone have any better suggestions? --Winston 10:21, 3 Apr 2008 (PDT)

Feedback & suggestion: This patch is very nice. I've applied this to the multiplayer source SDK based on OB engine as of 6/29/2008 and I'm having some success with it, as long as I DO NOT set the appID to 320 (HL2DM, which I really want to do) I'm confident this is because I need to do the steps under "Model animations" in the article. Those steps could not be accomplished by a patch file. I think a better explination in that area would do a lot to make this article useful to more people. I have a feeling I should understand what it means to "copy them over" from HL2, however my attempts are not working out thus far... schroe2a 29 June 2008
Regardless of how your mod works, using 320 as your SteamAppID should be considered very risky, as any engine updates may completely screw your mod. Its perfectly possible to reproduce all the content 320 incorporates by using the SDK Base AppID, and mounting AppID 320 (see Mounting_Other_Content). As for the models issue, this is a difficult problem involving creating copies of all the humanoid npc model and animation files, with different names to the player models, (or doing this the other way around, editing the player models), and then using a hex editor to point the models to the newly renamed animation. I've only had mixed success with this.
Thanks very much for the info. Two things about that comment are very helpful to me: 1) I didn't know that "mounting" the content was better than using the appid in the gameinfo.txt. I will use that approach. 2) While I understood that I needed to copy/rename models for either the player or NPCs, I did not understand why hex editing was needed. It has clicked now that I also need to rename the animation files and update the models to point to those names. I can see how this could be tricky, I'll give it a try...--Schroe2a 08:23, 8 Jul 2008 (PDT)


Blood

The fix that is currently proposed for the fact that NPCs do not display blood effects seems less than optimal to me. By supressing the prediction filter you cause needless network traffic for the server and some client events (such as impact sounds on clients/bots/NPCs) to register twice on the client because it first predicts them and then still receives the effect from the server as well (you can verify this with sv_soundemitter_trace 1). The solution to this problem should lie in fixing the fact that the predicted client firing logic doesn't properly execute all the way to the blood effect creation when you shoot an NPC (because shots on NPCs *are* predicted just like shooting anything else is), not in forcing the server to needlessly send the temp entities to the local player when they should be predicting them (and infact are, when it comes to the sound effects).

There are two reasons why the clientside firing logic never fully executes UTIL_BloodImpact() when firing at an NPC. They occur at the following areas in the code (I ommitted line numbers because I don't know if they match the clean source):

baseentity_shared.cpp:

void CBaseEntity::TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr )
{
    Vector vecOrigin = ptr->endpos - vecDir * 4;

    if ( m_takedamage )
    {
       [...]
       SpawnBlood( vecOrigin, vecDir, blood, info.GetDamage() );// a little surface blood.
       TraceBleed( info.GetDamage(), vecDir, ptr, info.GetDamageType() );
    }


Here it checks whether the entity that received the bullet impact can take damage and only then will it proceed to the blood impact functions. For NPCs the client never sets m_takedamage to DAMAGE_YES because m_takedamage is not networked and the NPC default code lacks a clientside spawn function that sets it on the client entity (which is what the player and physics prop classes do for instance).

However once you resolve that you still get no blood, this time the code trips out at the following line:

util_shared.cpp

void UTIL_BloodDrips( const Vector &origin, const Vector &direction, int color, int amount )
{
    if ( !UTIL_ShouldShowBlood( color ) )
       return;

    if ( color == DONT_BLEED || amount == 0 )
       return;


The reason why it fails on that line is that amount is set to 0. Why is that? Because if you look at the line where it calls SpawnBlood() in the previous code snippet you will notice it passes in the damage value to determine the amount. A damage value that gets determined in CBaseEntity::FireBullets() by calling CGameRules::GetAmmoDamage() which uses the values you set up in your GetAmmoDef() function (should be located in your custom gamerules .cpp file, in the case of default SDKs it's sdk_gamerules.cpp and for HL2MP it's hl2mp_gamerules.cpp). When you look at these functions in either the Scratch or the HL2MP SDK you'll notice the damage parameters for every single AddAmmoType entry are 0. And since the client bases the amount of blood it creates directly on the amount of damage you do it simply doesn't spawn the blood effect at all.

The solution to getting NPCs to bleed lies in fixing both these issues in one of the many ways they can be fixed. For the first issue I simply added an overloaded Spawn(void) method to C_AI_BaseNPC which sets m_takedamage to DAMAGE_YES and then calls BaseClass::Spawn() (considering all my NPCs are shootable enemies that is not a problem for me, some people might have to solve this in a more detailed fashion). For the damage issue you can either fill out damage values in the AddAmmoType() calls, you can rig it to use damage values from the weapon scripts, or you could simply rewrite the blood spawning code to not base the amount of blood on the damage to begin with (player blood effects do this in HL2MP for instance, it seems to base the amount on the distance of the shot rather than on the damage). Whatever way you choose to fix it, the way suggested by the article currently is not the way to go if you ask me. Htariac 10:32, 10 Jul 2008 (PDT)

Ballistic

hello all , how can fix npc open fire with ballistic ? [e.g combine , strider and combine gunship ...]--Test 18:43, 25 Jul 2008 (PDT)

Any reason why civilians, monks and others such as Scientists do not work with this? I can only get Combine to work fine.

Monks, Civilians and others.

Is there a reason why it seems like only Combine humans work? I can not get it to work with monks and civilians and other humanoids.

How to use the .patch file?

Uhmmm.. Can someone explain how to install the patch file?