Raised This Month: $359 Target: $400
 89%   Almost there!

Solved Find a random spawn location around an origin?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Natsheh
Veteran Member
Join Date: Sep 2012
Old 02-14-2022 , 14:19   Find a random spawn location around an origin?
Reply With Quote #1

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;

__________________
@Jailbreak Main Mod v2.6.6 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 02-15-2022 at 07:38.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
zXCaptainXz
Member
Join Date: May 2017
Old 02-14-2022 , 15:39   Re: Find a random spawn location around an origin?
Reply With Quote #2

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;
}

Last edited by zXCaptainXz; 02-14-2022 at 15:52. Reason: Adding stuff
zXCaptainXz is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 02-14-2022 , 16:39   Re: Find a random spawn location around an origin?
Reply With Quote #3

Maybe ? https://forums.alliedmods.net/showpo...28&postcount=8
__________________
EFFx is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 02-14-2022 , 18:12   Re: Find a random spawn location around an origin?
Reply With Quote #4

Quote:
Originally Posted by EFFx View Post
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.
__________________
@Jailbreak Main Mod v2.6.6 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 02-15-2022 at 15:23.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
+ARUKARI-
AlliedModders Donor
Join Date: Jul 2004
Location: Japan
Old 02-14-2022 , 22:23   Re: Find a random spawn location around an origin?
Reply With Quote #5

https://forums.alliedmods.net/showthread.php?t=309495
You have your posts, but they didn't work?
__________________
GitHub
SteamWishlist

六四天安門事件

Last edited by +ARUKARI-; 02-14-2022 at 22:24.
+ARUKARI- is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 02-15-2022 , 07:47   Re: Find a random spawn location around an origin?
Reply With Quote #6

Quote:
Originally Posted by +ARUKARI- View Post
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.
__________________
@Jailbreak Main Mod v2.6.6 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 02-15-2022 , 08:31   Re: Find a random spawn location around an origin?
Reply With Quote #7

Quote:
Originally Posted by Natsheh View Post
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);
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]

Last edited by Shadows Adi; 02-15-2022 at 08:32. Reason: Nevermind, just saw the return.
Shadows Adi is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:47.


Powered by vBulletin®
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Theme made by Freecode