Random location problem -
IceBilizard - 25.12.2015
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:gx, Float:gy, Float:gz, GiftModels;
if(!CreatedGift)
{
if(ongift == 1)
{
giftloc = random(dini_Int(AddDirFile(dir_datafiles, FILE_TOTALSTAT), "giftlocs"));
format(gifttmp2, 10, "gloc%d", giftloc);
//giftloc2 = giftloc;
if(dini_Exists(AddDirFile(dir_glocationfiles, gifttmp2)))
{
GiftModels = dini_Int(AddDirFile(dir_glocationfiles, gifttmp2), "GModel");
gx = dini_Int(AddDirFile(dir_glocationfiles, gifttmp2), "LocationX");
gy = dini_Int(AddDirFile(dir_glocationfiles, gifttmp2), "LocationY");
gz = dini_Int(AddDirFile(dir_glocationfiles, gifttmp2), "LocationZ");
GiftPickup = CreatePickup(GiftModels, 1, gx, gy, gz, -1);
format(giftstring, sizeof(giftstring), "{FF0000}Santa Claus: {FFFFFF}There is a present for you in %s. Merry Christmas, HO HO HO!", dini_Get(AddDirFile(dir_datafiles, FILE_GLOCSS), gifttmp2));
SendClientMessageToAll2(COLOR_RED, giftstring);
CreatedGift = 1;
}
}
}
else
{
format(gifttmp3, 10, "gloc%d", giftloc);
if(CreatedGift == 1)
{
format(giftstring2, sizeof(giftstring2), "{FF0000}Santa Claus: {FFFFFF}My present is still waiting for you in %s. Merry Christmas, HO HO HO!", dini_Get(AddDirFile(dir_datafiles, FILE_GLOCSS), gifttmp3));
SendClientMessageToAll2(COLOR_RED, giftstring2);
}
}
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:gx, Float:gy, Float:gz, GiftModels;
giftloc = random(dini_Int(AddDirFile(dir_datafiles, FILE_TOTALSTAT), "giftlocs"));
format(gifttmp3, 10, "gloc%d", giftloc);
//giftloc2 = giftloc;
if(CreatedGift == 0)
{
if(dini_Exists(AddDirFile(dir_glocationfiles, gifttmp2)))
{
GiftModels = dini_Int(AddDirFile(dir_glocationfiles, gifttmp3), "GModel");
gx = dini_Int(AddDirFile(dir_glocationfiles, gifttmp3), "LocationX");
gy = dini_Int(AddDirFile(dir_glocationfiles, gifttmp3), "LocationY");
gz = dini_Int(AddDirFile(dir_glocationfiles, gifttmp3), "LocationZ");
GiftPickup = CreatePickup(GiftModels, 1, gx, gy, gz, -1);
format(giftstring, sizeof(giftstring), "{FF0000}Santa Claus: {FFFFFF}The next present is located in %s. HO HO HO!", dini_Get(AddDirFile(dir_datafiles, FILE_GLOCSS), gifttmp3));
SendClientMessageToAll2(COLOR_RED, giftstring);
CreatedGift = 1;
}
}
}
Re: Random location problem -
prineside - 25.12.2015
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:giftPoints[ MAX_GIFT_POINTS ][3] = { // Gift spawn points (exactly the same number of points as you defined in MAX_GIFT_POINTS )
{100.0, 200.0, 10.0},
//...
};
new giftPointsKeys[ MAX_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 == 0 ) {
// We are now looking on a beginning of the keys array, let's shuffle it
new tmp;
for ( new i=0; i<MAX_GIFT_POINTS ; i++ ) {
for ( new i=0; i<MAX_GIFT_POINTS ; i++ ) {
tmp = giftPointsKeys[ i ];
giftPointsKeys[ i ] = giftPointsKeys[ j ];
giftPointsKeys[ j ] = tmp;
}
}
}
// Get next array key from suffled array
new pointArrayKey = giftPointsKeys[ currentGiftPoint ];
// Now spawn a gift
SpawnNewGift( giftPoints[ pointArrayKey ][ 0 ], giftPoints[ pointArrayKey ][ 1 ], giftPoints[ pointArrayKey ][ 2 ] );
// And increase iteration pointer
currentGiftPoint ++;
if ( currentGiftPoint == MAX_GIFT_POINTS ) {
// Reached the end of the array, start over
currentGiftPoint = 0;
}
}
Re: Random location problem -
xTURBOx - 26.12.2015
you are storing the gift locations in your script or in a separate file?
Re: Random location problem -
IceBilizard - 26.12.2015
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