AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Calculating fall distance (https://forums.alliedmods.net/showthread.php?t=85033)

Xellath 02-04-2009 14:53

Calculating fall distance
 
If a player jumps from a building and land in a random place, how can you calculate the fall distance?

Can you do this with pev_origin? If so, could anyone just give me a little code snippet?

Exolent[jNr] 02-04-2009 15:00

Re: Calculating fall distance
 
Code:
#include <amxmodx> #include <amxmisc> #include <fakemeta> #include <hamsandwich> new bool:g_alive[33]; new bool:g_was_on_ground[33]; new bool:g_falling[33]; public plugin_init() {     RegisterHam(Ham_Spawn, "player", "FwdPlayerSpawn", 1);     RegisterHam(Ham_Killed, "player", "FwdPlayerDeath", 1);     RegisterHam(Ham_Player_PreThink, "player", "FwdPlayerPreThink"); } public client_disconnect(client) {     g_alive[client] = false; } public FwdPlayerSpawn(client) {     if( is_user_alive(client) )     {         g_alive[client] = true;         g_falling[client] = false;     } } public FwdPlayerDeath(client) {     g_alive[client] = false; } public FwdPlayerPreThink(client) {     if( !g_alive[client] ) return;         if( g_falling[client] && pev(client, pev_movetype) != MOVETYPE_WALK )     {         g_falling[client] = false;     }         static Float:fall_origin[33][3];         new bool:on_ground = bool:(pev(client, pev_flags) & FL_ONGROUND);         if( on_ground && !g_was_on_ground[client] && g_falling[client] )     {         static Float:origin[3];         pev(client, pev_origin, origin);                 new Float:distance = fall_origin[client][2] - origin[2];         if( distance > 0.0 )         {             client_print(client, print_chat, "You fell %f units!", distance);         }     }     else if( !on_ground && g_was_on_ground[client] )     {         g_falling[client] = true;         pev(client, pev_origin, fall_origin[client]);     }         g_was_on_ground[client] = on_ground; }

Xellath 02-05-2009 01:43

Re: Calculating fall distance
 
If the fall distance should be displayed in units, how can I make that? Lets say you jumped from a building and landed on the ground, then a HUD msg pops up and shows the fall distance.

Code:

set_hudmessage(255, 0, 0, -1.0, 0.8, 0, 6.0, 5.0, 0.1, 0.4, -1);
show_hudmessage(id, "Fall Distance: %s", distance);

Something like that would pop up. How do I calculate the distance? The snippet you gave me exo, didn't really help me, yes, I suck at scripting, but if you could explain more, I would be capable of understanding. Thanks anyway.

ConnorMcLeod 02-05-2009 02:12

Re: Calculating fall distance
 
All is done in client prethink :

When jump/fall : store player origin
when land : check origin and calculate : stored Z origin - actual Z origin, result in units
Just show your hudmessage where x-olent put "// you have the fall distance"

distance is a float; so, in your message, replace %s with %.1f

Xellath 02-05-2009 02:42

Re: Calculating fall distance
 
I did like you said and I getting errors on show_hudmessage, id is undentified.

Code:

set_hudmessage(255, 0, 0, -1.0, 0.8, 0, 6.0, 4.0, 0.1, 0.4, -1)
show_hudmessage(id,"Fall Distance: %.1f",distance);

Is that correct?

EDIT: Nevermind, I got it right by setting client instead. Thanks for all the help. +karma.

Exolent[jNr] 02-05-2009 11:33

Re: Calculating fall distance
 
In about 20 minutes Ill be in my programming class so I will be able to rewrite it more efficiently instead of posting from my iPod.

Xellath 02-05-2009 11:42

Re: Calculating fall distance
 
That sounds great, Exo. Also, did you check my PM?

Exolent[jNr] 02-05-2009 11:53

Re: Calculating fall distance
 
Code has been fixed.

Xellath 02-05-2009 12:16

Re: Calculating fall distance
 
Thanks a bunch, Exolent. Have fun at programming class. :)

Dores 02-05-2009 12:26

Re: Calculating fall distance
 
@Exolent:

Code:
new boo:g_falling[33]; // Typo ---> new bool:g_falling[33];

Also:
Code:
pev(client, pev_origin, fall_origin[client]);
Are you positive that static variables' value is stored just like global vars(except of course only for the specific function where they were created)?
I think that static vars are only to prevent creating vars multiple times like in Think forwards to decrease CPU or something.

If you use new on every client_connect forward, it will recreate it every time the player connects.
But if you use static, the variable already exists but resets.


All times are GMT -4. The time now is 01:53.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.