AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Find a random spawn location around an origin? (https://forums.alliedmods.net/showthread.php?t=336368)

Natsheh 02-14-2022 14:19

Find a random spawn location around an origin?
 
I recently made this function, hopefully it will be helpful, it does scan for an empty space to spawn the NPC, i was wondering if its possible to add a feature to scan randomly ?

PHP Code:

find_location_around_origin(Float:fOrigin[3], Float:fMaxs[3], Float:fMins[3], Float:fDistance)
{
    
floor_origin(fOriginfOrigin);
    
fOrigin[2] += floatabs(fMins[2]);

    static 
iTr2Float:fTestOrigin[3], Float:fStart[3], Float:fEnd[3], Float:fYShiftFloat:fXShiftidiSafe;

    static 
iOrder[][][3] =
    {
        { {  
0,  0,  }, {  0,  0, -} }, // Inner line
        
{ {  1,  1,  }, {  1,  1, -} }, // 4 square lines SIDES
        
{ { -1, -1,  }, { -1, -1, -} },
        { { -
1,  1,  }, { -1,  1, -} },
        { {  
1, -1,  }, {  1, -1, -} },
        { {  
1,  1,  }, { -1,  1,  } }, // 4 square lines TOP
        
{ {  1,  1,  }, {  1, -1,  } },
        { { -
1, -1,  }, { -1,  1,  } },
        { { -
1, -1,  }, {  1, -1,  } },
        { {  
1,  1, -}, { -1,  1, -} }, // 4 square lines BOTTOM
        
{ {  1,  1, -}, {  1, -1, -} },
        { { -
1, -1, -}, { -1,  1, -} },
        { { -
1, -1, -}, {  1, -1, -} },
        { {  
1,  1,  }, { -1,  1, -} }, // front cross
        
{ {  1,  1, -}, { -1,  1,  } },
        { {  
1, -1,  }, { -1, -1, -} }, // back cross
        
{ {  1, -1, -}, { -1, -1,  } },
        { {  
1,  1,  }, {  1, -1, -} }, // right cross
        
{ {  1,  1, -}, {  1, -1,  } },
        { { -
1,  1,  }, { -1, -1, -} }, // left cross
        
{ { -1,  1, -}, { -1, -1,  } },
        { {  
1,  1,  }, { -1, -1,  } }, // up cross
        
{ {  1, -1,  }, { -1,  1,  } },
        { {  
1,  1, -}, { -1, -1, -} }, // down cross
        
{ {  1, -1, -}, { -1,  1, -} }
    };

    
fXShift = (fMaxs[0] - fMins[0]) ;
    
fYShift = (fMaxs[1] - fMins[1]) ;

    const 
sizeofOrder sizeof iOrder;

    
iTr2 create_tr2();

    
fTestOrigin[1] = fOrigin[1] + fDistance;
    
fTestOrigin[2] = fOrigin[2];

    static 
Float:fAngleFloat:fvBegin[3];

    while( 
floatabs(fTestOrigin[1] - fOrigin[1]) <= fDistance  )
    {
        
fAngle floatasin( (fTestOrigin[1] - fOrigin[1]) / fDistancedegrees );
        
fvBegin[0] = floatcos(fAngle,degrees) * fDistance;
        
fvBegin[1] = floatsin(fAngle,degrees) * fDistance;
        
fTestOrigin[0] = fOrigin[0] + fvBegin[0];

        while( 
get_distance_f(fTestOriginfOrigin) <= fDistance )
        {
            for( 
iSafe 0sizeofOrderi++ )
            {
                for( 
03d++ )
                {
                    switch( 
iOrder[i][0][d] )
                    {
                        case -
1fStart[d] = fTestOrigin[d] + fMins[d];
                        case 
0fStart[d] = fTestOrigin[d];
                        case 
1fStart[d] = fTestOrigin[d] + fMaxs[d];
                    }

                    switch( 
iOrder[i][1][d] )
                    {
                        case -
1fEnd[d] = fTestOrigin[d] + fMins[d];
                        case 
0fEnd[d] = fTestOrigin[d];
                        case 
1fEnd[d] = fTestOrigin[d] + fMaxs[d];
                    }
                }

                
// Traces...
                
engfunc(EngFunc_TraceLinefStartfEndDONT_IGNORE_MONSTERS, -1iTr2);

                if(
get_tr2(iTr2TR_pHit) == -&& get_tr2(iTr2TR_InOpen) && !get_tr2(iTr2TR_StartSolid) && !get_tr2(iTr2TR_AllSolid))
                {
                    
iSafe++;
                }
            }

            if(
iSafe >= sizeofOrder)
            {
                
xs_vec_copy(fTestOriginfOrigin);
                
free_tr2(iTr2);
                return 
1;
            }

            
fTestOrigin[0] -= fXShift;
        }

        
fTestOrigin[1] -= fYShift;
    }

    
free_tr2(iTr2);
    return 
0;



zXCaptainXz 02-14-2022 15:39

Re: Find a random spawn location around an origin?
 
I won't pretend like I even barely understood how this function works, but maybe you could try shuffling the array randomly before scanning?

Code:

   
new randIndex, tmp;
for(new i; i < sizeof(iOrder); i++)
{
    randIndex = random(sizeof(iOrder));
    tmp = iOrder[i];
    iOrder[i] = iOrder[randIndex];
    iOrder[randIndex] = tmp;
}

EDIT: I was doing some digging and I found this function, maybe you could make some use of it instead of all this mess?

Code:

stock bool:is_monster_hull_vacant( ent, const Float:origin[ 3 ] )
{
    new iTr = 0;
    engfunc( EngFunc_TraceMonsterHull, ent, origin, origin, 0, ent, iTr );

    if( !get_tr2( iTr, TR_StartSolid ) && !get_tr2( iTr, TR_AllSolid ) && get_tr2( iTr, TR_InOpen ) )
        return true;
       
    return false;
}


EFFx 02-14-2022 16:39

Re: Find a random spawn location around an origin?
 
Maybe ? https://forums.alliedmods.net/showpo...28&postcount=8

Natsheh 02-14-2022 18:12

Re: Find a random spawn location around an origin?
 
Quote:

Originally Posted by EFFx (Post 2771484)

Sorry EFFx but your function is not efficient and not precise it doesn't get things inside a circle.



solved


Quote:

Originally Posted by zXCaptainXz
I won't pretend like I even barely understood how this function works, but maybe you could try shuffling the array randomly before scanning?

The function scans using tracelines for an empty space depending on the given NPC or entity Maxsize and minsize.

It scans from the top right circle to the left bottom of it.

+ARUKARI- 02-14-2022 22:23

Re: Find a random spawn location around an origin?
 
https://forums.alliedmods.net/showthread.php?t=309495
You have your posts, but they didn't work?

Natsheh 02-15-2022 07:47

Re: Find a random spawn location around an origin?
 
Quote:

Originally Posted by +ARUKARI- (Post 2771502)
https://forums.alliedmods.net/showthread.php?t=309495
You have your posts, but they didn't work?

Random origin generator can't be used during the game, and it only support player size places, i already solved the problem you can check post #4.

Shadows Adi 02-15-2022 08:31

Re: Find a random spawn location around an origin?
 
Quote:

Originally Posted by Natsheh (Post 2771523)
Random origin generator can't be used during the game, and it only support player size places, i already solved the problem you can check post #4.

Check if the bRandom var is true also when you are trying to push elements into array, because if bRandom is false, you'll get an invalid handle there.

Code:

L100: ArrayPushArray(pTempArray, fTestOrigin);


All times are GMT -4. The time now is 11:33.

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