Talk:Simulated Bullets: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(65 intermediate revisions by 2 users not shown)
Line 1: Line 1:
I'd really like assistance with this if anyone can do any implementation of this or any fixes to the bullet simulator... implementation includes making the gun shoot them out, making the bullets hurt, and making clientside effects... I have a feeling that other mods WILL want to use timed bullets, and this code will do the trick... I just need help (or plenty of time) getting it finished first and then it will be available to everyone!&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 23:06, 20 Nov 2005 (PST)
I'd really like assistance with this if anyone can do any implementation of this or any fixes to the bullet simulator... implementation includes making the gun shoot them out, making the bullets hurt, and making clientside effects... I have a feeling that other mods WILL want to use timed bullets, and this code will do the trick... I just need help (or plenty of time) getting it finished first and then it will be available to everyone!&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 23:06, 20 Nov 2005 (PST)
::Yes ts2do (you went away before i came back), the myth was busted, best to contact Wrayith at Nightfall mod for more info. He was interested, and was implementing something like this with the quake 2 code, and was going to port it to hl2 latter. MSN wraiyth@gmail.com --[[User:Amckern|Amckern]] 01:07, 21 Nov 2005 (PST)
::Yes ts2do (you went away before i came back), the myth was busted, best to contact Wrayith at Nightfall mod for more info. He was interested, and was implementing something like this with the quake 2 code, and was going to port it to hl2 latter. MSN wraiyth@gmail.com --[[User:Amckern|Amckern]] 01:07, 21 Nov 2005 (PST)
Does anyone know what it means if I'm getting this sort of error on the client:
  client.dll!CUtlMemory<CSimulatedBullet *>::Base()  Line 252 + 0x3
client.dll!CUtlVector<CSimulatedBullet *,CUtlMemory<CSimulatedBullet *> >::Base()  Line 54 + 0x14
client.dll!CUtlVector<CSimulatedBullet *,CUtlMemory<CSimulatedBullet *> >::AddToTail(CSimulatedBullet * const & src=0x044fc440)  Line 403 + 0x8
It seems to not like me adding an item to the utlvector on the client but it's fine with it on the server
&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 10:52, 23 Nov 2005 (PST)


:How are you calling AddBullet? Is the parameter on the heap or stack? By the way, since CSimulatedBullet has dynamically-allocated members, you need a non-default copy constructor and copy assignment operator; otherwise you'll have leaks and possibly crashes if a CSimulatedBullet is ever copied. If it's never going to be copied, make the copy constructor and copy assignment operator private and undefined. &mdash;'''[[User:Maven|Maven]]''' <sup>([[User talk:Maven|talk]])</sup> 11:31, 23 Nov 2005 (PST)
[snip]


I've tried a copy constructor b4...
[snip2]
but what do you mean dynamically allocated...what objects? the pointers?
<pre> CSimulatedBullet *pBullet = new CSimulatedBullet(pAttacker, info.m_pAdditionalIgnoreEnt,
info.m_iAmmoType, info.m_nFlags, vecDir, vecSrc, nDamageType
#ifndef CLIENT_DLL
, this, info.m_iDamage, 0.0f
#endif
);
BulletManager()->AddBullet(pBullet);</pre>
Also...I neglected to use a linked list because it would cost more allocation...&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 11:43, 23 Nov 2005 (PST)


why const ints instead of defines?&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 13:43, 23 Nov 2005 (PST)
Wait, maybe I'm talking about something else... You said that in <code>CUtlMemory<T>::operator[]( int )</code> you found <code>m_pMemory</code> to be null. ''Where'' in the function were you? I assumed (foolishly) that you meant at the second line. If you were checking as you first entered the function, though, then it's OK, because <code>m_nAllocationCount</code> will be zero and therefore the <code>Assert</code> will fail. If <code>CUtlMemory<T>::operator[]( int )</code> is being called when <code>m_nAllocationCount</code> is zero, though, then the calling code is at fault for using an illegal index. &mdash;'''[[User:Maven|Maven]]''' <sup>([[User talk:Maven|talk]])</sup> 17:57, 3 Dec 2005 (PST)


:The dynamic allocation is at the <pre>m_pIgnoreList = new CTraceFilterSimpleList(COLLISION_GROUP_NONE);</pre> and <pre>m_pTwoEnts = new CTraceFilterSkipTwoEntities(pAttacker,pAdditionalIgnoreEnt,COLLISION_GROUP_NONE);</pre> lines&mdash;the CTraceFilterSimpleList and CTraceFilterSkipTwoEntities objects are dynamically allocated. You <code>delete</code>d them in the dtor, which is good, but if a CSimulatedBullet were copied both the original and the copy would point to the same CTraceFilterSimpleList and CTraceFilterSkipTwoEntities objects. When either CSimulatedBullet gets deleted, the other one is now pointing at CTraceFilterSimpleList and CTraceFilterSkipTwoEntities objects that have been deleted. As a rule of thumb, if you have to make a custom dtor you also need to make a custom copy constructor and assignment operator.
I don't see why it's doing what it's doing...I'm so lost; I'm just gonna make the linked list global...that should solve it&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 18:30, 3 Dec 2005 (PST)


:For the <code>const int</code>s instead of <code>#define</code>s: macros were fine in C but it's good practice to avoid them in C++. There are some reasons for this, but they're not particularly relevant. The SDK has them strewn everywhere anyway, so it's probably not going to hurt anything to add a few more, though.
As I thought...it works marvelously now..time to move on to other errors&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 18:37, 3 Dec 2005 (PST)


:For the sample you gave, do you use <code>pBullet</code> anywhere else before it goes out of scope? &mdash;'''[[User:Maven|Maven]]''' <sup>([[User talk:Maven|talk]])</sup> 13:59, 23 Nov 2005 (PST)
:Erm, OK... I hope that that fixed the bug rather than just hiding it. It will be ''well'' worth your time to learn strong debugging skills, though. &mdash;'''[[User:Maven|Maven]]''' <sup>([[User talk:Maven|talk]])</sup> 12:48, 4 Dec 2005 (PST)


:If, with the private copy ctor and operator=, you get compiler errors complaining about them being private, then that means you need to either implement them or avoid the copy. Let me know if that's the case, please. &mdash;'''[[User:Maven|Maven]]''' <sup>([[User talk:Maven|talk]])</sup> 14:01, 23 Nov 2005 (PST)
I'm currently revamping part of it to use the tempent system...it'll be better!&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 19:45, 4 Dec 2005 (PST)
I don't use pBullet anywhere else...but I had a feeling that the pointers were giving errors...why would they if I was just calling AddToTail...no code was run thru where a null pointer would be a problem....&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 14:10, 23 Nov 2005 (PST)


Still looking. By the way, <code>m_pBullets</code> shouldn't have a <code>p</code> in its name since it's not a pointer&mdash;<code>m_bullets</code> or <code>m_Bullets</code> would be correct. It won't hurt the code, but it confuses people who read it. Ditto <code>m_pCompensationConsiderations</code>. &mdash;'''[[User:Maven|Maven]]''' <sup>([[User talk:Maven|talk]])</sup> 14:19, 23 Nov 2005 (PST)
Revert highlights:
[http://developer.valvesoftware.com/w/index.php?title=Simulated_Bullets&diff=0&oldid=18448] & [http://developer.valvesoftware.com/w/index.php?title=Simulated_Bullets&diff=18448&oldid=18447]&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 19:59, 4 Dec 2005 (PST)


I still don't understand why this is working perfectly on the serverdll and not on the clientdll...I've made said changes and still get the same results&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 14:48, 23 Nov 2005 (PST)
I want to reimplement this so there's a DrawModel function for the bullets...the problem is I only change the origin every centisecond...


:<code>CUtlVector</code> directly invokes ctors and dtors on its elements. Notably, <code>AddToTail</code> (indirectly) calls the copy constructor. However, since you're storing ''pointers'' in the CUtlVector, the <code>CSimulatedBullet*</code> pointer is being copied rather than the <code>CSimulatedBullet</code> object.  I'm not completely sure (I'd want to test it first), but that ''should'' be OK (ie, no crashes) except for creating massive memory leaks.  I see that Valve stores pointers in a CUtlVector elsewhere, so perhaps I'm mistaken. I suppose you could try refactoring things so that you have <code>CUtlVector<CSimulatedBullet> m_bullets;</code> instead of <code>CUtlVector<CSimulatedBullet*> m_pBullets;</code>, but I'm not sure that that would solve things, and you'd definately need a copy ctor then. An easier step would be to start sprinkling <code>Assert</code>s everywhere, especially around AddToTail and Remove.
My question is how should I go by doing this?
* Should I instead simulate the bullet every frame
* Should I make the DrawModel move at the same speed through the centisecond
** This requires direction * speed * time & could look jittery when penetration occurs
If I also made the CSimulatedBullet into an interface with this DrawModel addition, it'd be possible to show how to reimplement the crossbow so the bolts are predicted.


:I'll be away for a few days, and perhaps I'll look at it some more then. &mdash;'''[[User:Maven|Maven]]''' <sup>([[User talk:Maven|talk]])</sup> 14:56, 23 Nov 2005 (PST)
Sampley:
 
<pre>class ISimulatedBullet
well...I really can't assert anything because it's the addtotail thing that's giving me problems and nothing else...though I did try making it <CSimulatedBullet> and <SimulatedBullet_t>....all failed...a linked list works, but I don't believe it would be the best way because it allocates memory every time one is made...correct?&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 16:22, 23 Nov 2005 (PST)
{
 
public:
:Plans have changed; I've not left quite yet.
virtual bool StartSolid(trace_t &ptr, Vector &vecNewRay) = 0;
virtual bool AllSolid(trace_t &ptr) = 0;
virtual bool EndSolid(trace_t &ptr) = 0;
#ifdef CLIENT_DLL
virtual bool SimulateBullet(void) = 0;
#else
virtual void EntityImpact(trace_t &ptr) = 0;
virtual bool SimulateBullet(float flLagCompensation=-1) = 0;
#endif
};</pre>&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 06:03, 5 Dec 2005 (PST)


:Ok, I've been looking more at the code, and <code>CBulletManager::Think</code> has a problem. If during the <code>for</code> loos a bullet removes itself, that will cause the contents of the vector to be shifted (a slow, O(n), operation) which in turn will cause the following bullet to not be processed until the next Think. Unless removals happen almost always at the tail of the vector and/or the vector is always very small, a linked list would probably be more time-efficient than a vector. As a general rule, it's not a good idea to modify a collection (<code>m_pBullets</code>) while iterating over it (the <code>for</code> loop in <code>Think</code>). That's true even of a linked list, by the way. I don't see that this would cause your problem, but
New bugs:
* Multiplayer crashy stuff (Game crashes on quit command issued)
* GetTargetTick doesn't work (Tested only on localhost)
&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 19:30, 5 Dec 2005 (PST)


:I can't see ''any'' way that <code>CUtlMemory<T>::Base()</code> could possibly throw an error. It's just a member accessor, and its object should already be constructed. Unless [http://hl2doxygen.sniperumm.co.uk/d4/d41/utlmemory_8h-source.html utlmemory.h] has changed, which is doubtful. Perhaps if the vector or its allocator were corrupted, then the error might be possible (as an access violation). It's a pity that the error message doesn't say what's ''wrong''.
Maven...uncomment this:
The bullets will know when they're being fired so in <code>CHL2MP_Player::WantsLagCompensationOnEntity</code>
{| style="background: transparent;"
| Comment out || <pre> if ( !( pCmd->buttons & IN_ATTACK ) && (pCmd->command_number - m_iLastWeaponFireUsercmd > 5) )
return false;</pre>
|}
&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 19:33, 5 Dec 2005 (PST)


:Are you willing to post the complete code that uses this code? Having live code would make debugging easier. Also, does the error happen on your ''first'' <code>AddBullet</code>? Second? Does it happen every time? Does it happen after the computer's been restarted? &mdash;'''[[User:Maven|Maven]]''' <sup>([[User talk:Maven|talk]])</sup> 20:50, 23 Nov 2005 (PST)
if someone'd like to assist me in totally revamping this with a CAutoGameSystem, contact me&mdash;'''[[User:Ts2do|ts2do]]'''&nbsp;<sup>([[User talk:Ts2do|Talk]]&nbsp;|&nbsp;[mailto:tsdodo@gmail.com @])</sup> 01:17, 30 Dec 2005 (PST)

Latest revision as of 02:17, 30 December 2005

I'd really like assistance with this if anyone can do any implementation of this or any fixes to the bullet simulator... implementation includes making the gun shoot them out, making the bullets hurt, and making clientside effects... I have a feeling that other mods WILL want to use timed bullets, and this code will do the trick... I just need help (or plenty of time) getting it finished first and then it will be available to everyone!—ts2do (Talk | @) 23:06, 20 Nov 2005 (PST)

Yes ts2do (you went away before i came back), the myth was busted, best to contact Wrayith at Nightfall mod for more info. He was interested, and was implementing something like this with the quake 2 code, and was going to port it to hl2 latter. MSN wraiyth@gmail.com --Amckern 01:07, 21 Nov 2005 (PST)

[snip]

[snip2]

Wait, maybe I'm talking about something else... You said that in CUtlMemory<T>::operator[]( int ) you found m_pMemory to be null. Where in the function were you? I assumed (foolishly) that you meant at the second line. If you were checking as you first entered the function, though, then it's OK, because m_nAllocationCount will be zero and therefore the Assert will fail. If CUtlMemory<T>::operator[]( int ) is being called when m_nAllocationCount is zero, though, then the calling code is at fault for using an illegal index. —Maven (talk) 17:57, 3 Dec 2005 (PST)

I don't see why it's doing what it's doing...I'm so lost; I'm just gonna make the linked list global...that should solve it—ts2do (Talk | @) 18:30, 3 Dec 2005 (PST)

As I thought...it works marvelously now..time to move on to other errors—ts2do (Talk | @) 18:37, 3 Dec 2005 (PST)

Erm, OK... I hope that that fixed the bug rather than just hiding it. It will be well worth your time to learn strong debugging skills, though. —Maven (talk) 12:48, 4 Dec 2005 (PST)

I'm currently revamping part of it to use the tempent system...it'll be better!—ts2do (Talk | @) 19:45, 4 Dec 2005 (PST)

Revert highlights: [1] & [2]ts2do (Talk | @) 19:59, 4 Dec 2005 (PST)

I want to reimplement this so there's a DrawModel function for the bullets...the problem is I only change the origin every centisecond...

My question is how should I go by doing this?

  • Should I instead simulate the bullet every frame
  • Should I make the DrawModel move at the same speed through the centisecond
    • This requires direction * speed * time & could look jittery when penetration occurs

If I also made the CSimulatedBullet into an interface with this DrawModel addition, it'd be possible to show how to reimplement the crossbow so the bolts are predicted.

Sampley:

class ISimulatedBullet
{
public:
	virtual bool StartSolid(trace_t &ptr, Vector &vecNewRay) = 0;
	virtual bool AllSolid(trace_t &ptr) = 0;
	virtual bool EndSolid(trace_t &ptr) = 0;
#ifdef CLIENT_DLL
	virtual bool SimulateBullet(void) = 0;
#else
	virtual void EntityImpact(trace_t &ptr) = 0;
	virtual bool SimulateBullet(float flLagCompensation=-1) = 0;
#endif
};

ts2do (Talk | @) 06:03, 5 Dec 2005 (PST)

New bugs:

  • Multiplayer crashy stuff (Game crashes on quit command issued)
  • GetTargetTick doesn't work (Tested only on localhost)

ts2do (Talk | @) 19:30, 5 Dec 2005 (PST)

Maven...uncomment this: The bullets will know when they're being fired so in CHL2MP_Player::WantsLagCompensationOnEntity

Comment out
	if ( !( pCmd->buttons & IN_ATTACK ) && (pCmd->command_number - m_iLastWeaponFireUsercmd > 5) )
		return false;

ts2do (Talk | @) 19:33, 5 Dec 2005 (PST)

if someone'd like to assist me in totally revamping this with a CAutoGameSystem, contact me—ts2do (Talk | @) 01:17, 30 Dec 2005 (PST)