saul / demofile

Node.js library for parsing Counter-Strike: Global Offensive demo files
https://demofile.dev
MIT License
481 stars 53 forks source link

grenade_bounce event not triggered #71

Closed jnettome closed 3 years ago

jnettome commented 6 years ago

@saul are you aware about an issue regarding grenade_bounce event not triggering? I'm trying to draw the grenades trajectory and I'm not getting the right way to accomplish that. Any help appreciated ;) Thanks again!

saul commented 6 years ago

There is no grenade_bounce event as far as I’m aware. Are you looking for hegrenade_bounce, flashbang_bounce etc.?

jnettome commented 6 years ago

Thanks for replying @saul :) So, I found the grenade_bounce event on https://wiki.alliedmods.net/Counter-Strike:_Global_Offensive_Events and I thought that was triggered when any grenade hits a wall, but it doesn't work. I've tried hegrenade_bounce and flashbang_bounce but still, it doesn't work and I didn't found any references on google nor anywhere. Do you know how should I listen to those events to get the right grenades path? Thank you so much!

saul commented 6 years ago

Okay so I was completely wrong about the hegrenade_bounce etc events. However you're right in that it looks like the grenade_bounce event isn't in the demo files. Even if it was, it has no useful information.

Instead use this code:

function printGrenadeLocation(grenade) {
  let modelParts = grenade.modelName.split('/');
  console.log('%d(%s) %O', grenade.handle, modelParts[modelParts.length-1], grenade.position);
}

demoFile.entities.on('postcreate', e => {
  if (e.entity.getProp('DT_BaseCSGrenadeProjectile', 'm_nBounces') === undefined)
    return;

  printGrenadeLocation(e.entity);
});

demoFile.entities.on('change', e => {
  if (e.varName !== 'm_nBounces')
    return;

  printGrenadeLocation(e.entity);
});

Here we exploit the fact that when a grenade bounces, its DT_BaseCSGrenadeProjectile.m_nBounces prop is incremented. When it changes, we print the grenade location along with the projectile's entity handle - this is a unique number representing this entity.

Output is like:

434552(w_eq_smokegrenade_thrown.mdl) { x: 1414.8125, y: -368.46875, z: -62.96875 }
434552(w_eq_smokegrenade_thrown.mdl) { x: -607.625, y: -472.875, z: -161.96875 }
434552(w_eq_smokegrenade_thrown.mdl) { x: -751.03125, y: -480.1875, z: -163.875 }
987443(w_eq_flashbang_dropped.mdl) { x: 518.1875, y: 593.75, z: -65.65625 }
434552(w_eq_smokegrenade_thrown.mdl) { x: -781.03125, y: -470.03125, z: -164.9375 }
434552(w_eq_smokegrenade_thrown.mdl) { x: -787.75, y: -464.15625, z: -165.65625 }
434552(w_eq_smokegrenade_thrown.mdl) { x: -789.09375, y: -462.4375, z: -165.96875 }
987443(w_eq_flashbang_dropped.mdl) { x: 164.4375, y: -185.71875, z: 43.9375 }

Note you'll need to update to demofile 0.4.18 for the above to work! I've just added BaseEntity#modelName.

Any questions let me know.

saul commented 6 years ago

Let me know if you have any other questions.

jnettome commented 6 years ago

Thank you so much @saul ! It works :)