Random location problem
#1

Guys i have a gift script for my server but i got some problem in it whenever i get gift sometimes it repeat the location and it happen alot of times spawning on same location if you take one here is my code and tell me how to fix to spawn at every new and random location

This is timer of Gift
PHP код:
forward GiftLoc();
public 
GiftLoc()
{
        if(
gifttime <= 0)
        {
            new 
gifttmp2[10], giftstring[170], giftstring2[170], gifttmp3[10], Float:gxFloat:gyFloat:gzGiftModels;
            if(!
CreatedGift)
               {
                if(
ongift == 1)
                {
                    
giftloc random(dini_Int(AddDirFile(dir_datafilesFILE_TOTALSTAT), "giftlocs"));
                    
format(gifttmp210"gloc%d"giftloc);
                    
//giftloc2 = giftloc;
                    
if(dini_Exists(AddDirFile(dir_glocationfilesgifttmp2)))
                       {
                            
GiftModels dini_Int(AddDirFile(dir_glocationfilesgifttmp2), "GModel");
                              
gx dini_Int(AddDirFile(dir_glocationfilesgifttmp2), "LocationX");
                            
gy dini_Int(AddDirFile(dir_glocationfilesgifttmp2), "LocationY");
                            
gz dini_Int(AddDirFile(dir_glocationfilesgifttmp2), "LocationZ");
                            
GiftPickup CreatePickup(GiftModels1gxgygz, -1);
                            
format(giftstringsizeof(giftstring), "{FF0000}Santa Claus: {FFFFFF}There is a present for you in %s. Merry Christmas, HO HO HO!"dini_Get(AddDirFile(dir_datafilesFILE_GLOCSS), gifttmp2));
                            
SendClientMessageToAll2(COLOR_REDgiftstring);
                            
CreatedGift 1;
                    }
                 }
            }
            else
            {
                      
format(gifttmp310"gloc%d"giftloc);
                    if(
CreatedGift == 1)
                    {
                        
format(giftstring2sizeof(giftstring2), "{FF0000}Santa Claus: {FFFFFF}My present is still waiting for you in %s. Merry Christmas, HO HO HO!"dini_Get(AddDirFile(dir_datafilesFILE_GLOCSS), gifttmp3));
                        
SendClientMessageToAll2(COLOR_REDgiftstring2);
                       }
            }
            
gifttime GetGiftInfo("Gifttime");
        }
        else if(
gifttime GetGiftInfo("Gifttime")) gifttime=0;
        else 
gifttime--;

This is pickup code of gift
PHP код:
new giftstring[120], gifttmp3[7], Float:gxFloat:gyFloat:gzGiftModels;
        
giftloc random(dini_Int(AddDirFile(dir_datafilesFILE_TOTALSTAT), "giftlocs"));
        
format(gifttmp310"gloc%d"giftloc);
        
//giftloc2 = giftloc;
        
if(CreatedGift == 0)
        {
            if(
dini_Exists(AddDirFile(dir_glocationfilesgifttmp2)))
            {
                        
GiftModels dini_Int(AddDirFile(dir_glocationfilesgifttmp3), "GModel");
                          
gx dini_Int(AddDirFile(dir_glocationfilesgifttmp3), "LocationX");
                        
gy dini_Int(AddDirFile(dir_glocationfilesgifttmp3), "LocationY");
                        
gz dini_Int(AddDirFile(dir_glocationfilesgifttmp3), "LocationZ");
                        
GiftPickup CreatePickup(GiftModels1gxgygz, -1);
                        
format(giftstringsizeof(giftstring), "{FF0000}Santa Claus: {FFFFFF}The next present is located in %s. HO HO HO!"dini_Get(AddDirFile(dir_datafilesFILE_GLOCSS), gifttmp3));
                        
SendClientMessageToAll2(COLOR_REDgiftstring);
                        
CreatedGift 1;
            }
        }
    } 
Reply
#2

If you need to spawn a gift each time on random unused place, the easiest way is
1. Store all position IDs in one array
2. Shuffle it
3. On each gift spawn iteration use the next item in this array
4. When you reach the end of array just shuffle it again and start over

For example

PHP код:
#define MAX_GIFT_POINTS (10) // Number of points your gifts will be spawned on
new Float:giftPointsMAX_GIFT_POINTS ][3] = { // Gift spawn points (exactly the same number of points as you defined in MAX_GIFT_POINTS )
    
{100.0200.010.0},
    
//...
};
new 
giftPointsKeysMAX_GIFT_POINTS  ]; // We'll store all point array keys here (we'll shuffle this array)
new currentGiftPoint 0// Current gift spawn iteration

// Spawns one gift in a random unused point
// Call this function in timer
public GiftSpawnHandler() {
    if ( 
currentGiftPoint  == ) {
        
// We are now looking on a beginning of the keys array, let's shuffle it
        
new tmp;
        for ( new 
i=0i<MAX_GIFT_POINTS i++ ) {
            for ( new 
i=0i<MAX_GIFT_POINTS i++ ) {
                
tmp giftPointsKeys];
                
giftPointsKeys] = giftPointsKeys];
                
giftPointsKeys] = tmp;
            }
        }
    }
 
    
// Get next array key from suffled array
    
new pointArrayKey giftPointsKeyscurrentGiftPoint ];

    
// Now spawn a gift
    
SpawnNewGiftgiftPointspointArrayKey  ][ ], giftPointspointArrayKey  ][ ], giftPointspointArrayKey  ][ ] );

    
// And increase iteration pointer
    
currentGiftPoint ++;
    if ( 
currentGiftPoint == MAX_GIFT_POINTS  ) {
        
// Reached the end of the array, start over
        
currentGiftPoint 0;
    }

Reply
#3

you are storing the gift locations in your script or in a separate file?
Reply
#4

Well i have dynamic location saving system so currently it will save location file in a folder and location name to another file but i have fixed the issue thanks to prineside his code works fine now

+Rep for prineside
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)