AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Why does this code give this error? Can you fix it please? (https://forums.alliedmods.net/showthread.php?t=121914)

cs1.7 03-20-2010 23:48

Why does this code give this error? Can you fix it please?
 
Hi,

what is wrong in the below code?

It gives this error in console:

Quote:

PM Got a NaN velocity 0
PM Got a NaN velocity 1
PHP Code:

stock create_velocity_vector(victim,attacker,Float:velocity[3])
{
    if(!
is_user_alive(attacker) || is_user_bot(attacker))
        return 
0;

    new 
Float:vicorigin[3];
    new 
Float:attorigin[3];
    
pevvictimpev_originvicorigin);
    
pevattackerpev_originattorigin);
    
    new 
Float:cheese floatpower(vicorigin[0]-attorigin[0], 2.0) + floatpower(vicorigin[1]-attorigin[1] ,2.0) + floatpower(vicorigin[2]-attorigin[2], 2.0 );
    new 
distance floatroundfloatpowercheese0.5 ) );

    new 
Float:origin2[3]
    
origin2[0] = vicorigin[0] - attorigin[0];
    
origin2[1] = vicorigin[1] - attorigin[1];

    new 
Float:largestnum 0.0;

    if(
floatabs(origin2[0])>largestnumlargestnum floatabs(origin2[0]);
    if(
floatabs(origin2[1])>largestnumlargestnum floatabs(origin2[1]);

    
origin2[0] /= largestnum;
    
origin2[1] /= largestnum;

    
velocity[0] = ( origin2[0] * 200000 ) / (distance 50);
    
velocity[1] = ( origin2[1] * 200000 ) / (distance 50);
    if(
velocity[0] <= 20.0 || velocity[1] <= 20.0)
        
velocity[2] = random_float(275.0 300.0);

    return 
1;



cs1.7 04-15-2010 06:28

Re: Why does this code give this error?
 
can no one help?

i am not sure but i think it also freezes / crashes the server but i have to test if it's really this code which causes the server freeze.

Xanimos 04-15-2010 14:43

Re: Why does this code give this error? Can you fix it please?
 
Id check to see if the victim is alive

Correct me if I'm wrong but with the following:
PHP Code:

    new Float:cheese floatpower(vicorigin[0]-attorigin[0], 2.0) + floatpower(vicorigin[1]-attorigin[1] ,2.0) + floatpower(vicorigin[2]-attorigin[2], 2.0 );
    new 
distance floatroundfloatpowercheese0.5 ) ); 

You're doing
Quote:

Originally Posted by Algebra
( ( vx - ax )² + (vy - ay)² + ( vz - az )² )½

Which would simplify to
Quote:

Originally Posted by Algebra
(vx - ax) + (vy - ay) + (vz - az)

So in code you can simplify to:
PHP Code:

    new distance floatround( (vicorigin[0]-attorigin[0]) + (vicorigin[1]-attorigin[1] ) + ( vicorigin[2]-attorigin[2] ) ); 

-------------

The following code can be simplified:
PHP Code:

    new Float:largestnum 0.0;

    if(
floatabs(origin2[0])>largestnumlargestnum floatabs(origin2[0]);
    if(
floatabs(origin2[1])>largestnumlargestnum floatabs(origin2[1]); 

to
PHP Code:

    new Float:largestnum floatabs(origin2[0]);
    if(
floatabs(origin2[1])>largestnumlargestnum floatabs(origin2[1]); 

because when largetsnum is 0 the first if will always be true.


To answer your question, I don't see anything in your code that is causing an error. I'm assuming in your code surrounding this function there is some problem.

Exolent[jNr] 04-15-2010 15:34

Re: Why does this code give this error? Can you fix it please?
 
I think it means the PM_* functions (engine) are receiving an invalid (NaN = Not a Number) velocity value for the X (0) and Y (1) values.

PHP Code:

// this
velocity[0] = ( origin2[0] * 200000 ) / (distance 50);
// can be simplified to
velocity[0] = ( origin2[0] * 200000 ) / distance 50;
// which can be simplified to
velocity[0] = ( origin2[0] * 200000 50 ) / distance;
// which can be simplified to
velocity[0] = ( origin2[0] * 10000000 ) / distance

Now, since Pawn limits values to 32 bits, you are probably breaking this barrier.
I would suggest writing it like this:

PHP Code:

velocity[0] = origin2[0] * ( 10000000 distance ); 

This way, you are multiplying by a smaller number so you don't try to break the barrier and then reduce.

So, with what Xanimos and I both said:

PHP Code:

stock create_velocity_vector(victim,attacker,Float:velocity[3])
{
    if(!
is_user_alive(attacker) || is_user_bot(attacker))
        return 
0;

    new 
Float:vicorigin[3];
    new 
Float:attorigin[3];
    
pevvictimpev_originvicorigin);
    
pevattackerpev_originattorigin);
    
    new 
distance floatround( (vicorigin[0]-attorigin[0]) + (vicorigin[1]-attorigin[1] ) + ( vicorigin[2]-attorigin[2] ) );  

    new 
Float:origin2[3]
    
origin2[0] = vicorigin[0] - attorigin[0];
    
origin2[1] = vicorigin[1] - attorigin[1];

    new 
Float:largestnum floatabs(origin2[0]);
    if(
floatabs(origin2[1])>largestnumlargestnum floatabs(origin2[1]);

    
origin2[0] /= largestnum;
    
origin2[1] /= largestnum;

    
velocity[0] = origin2[0] * ( 10000000 distance );  
    
velocity[1] = origin2[1] * ( 10000000 distance );  
    if(
velocity[0] <= 20.0 || velocity[1] <= 20.0)
        
velocity[2] = random_float(275.0 300.0);

    return 
1;




All times are GMT -4. The time now is 08:38.

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