Random Vehicle Spawn Help
#1

I tried setting up a random vehicle spawn system so that every time it spawns, it chooses a random location each time. When I tested it, every vehicle just tried to spawn in the same location.

pawn Код:
//Random Spawn Float

new Float: RandomVehicleSpawns[][] =
{
    {490.8827, 868.8945, -31.6403, 228.5856},
    {492.0695, 898.3134, -31.4417, 340.1074},
    {868.7068, 924.2509, 13.1898, 2.7240},
    {577.1639, 1076.7039, 28.1347, 88.3344},
    {586.0138, 1224.3378, 11.5540, 1.1765},
    {160.0283, 1186.5177, 14.8515, 37.8921},
    {-82.0563, 1340.0801, 10.7452, 7.0630},
    {-302.4243, 1750.6183, 42.5254, 156.3794},
    {-366.1618, 2244.6304, 42.2960, 14.4192},
    {-711.0560, 2349.3142, 126.8845, 183.0471},
    {-496.3653, 2590.5107, 53.3487, 314.6674},
    {-855.6710, 2756.6531, 45.6891, 5.4393},
    {-1307.6619, 2710.5342, 49.9003, 3.6414},
    {86.8474, 2685.2341, 52.4419, 3.3280},
    {656.5557, 2281.5239, 26.0612, 184.8369},
    {691.3392, 1946.7954, 5.3781, 358.0526},
    {372.9126, 1520.8871, 11.5116, 162.6114}
};

//Stock that holds every vehicle

stock LoadVehicles()
{
    new vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(411, RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2], 228.5856, 0, 0, 7200); //Test Veh. 1
    CreateVehicle(412, RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2], 340.1074, 0, 0, 7200); //Test Veh. 2
   
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],2.7240,0,0,7200); // Vehicle 1/
    CreateVehicle(514,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],88.3344,0,0,7200); // Vehicle 2/
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],1.1765,0,0,7200); // Vehicle 3/
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],37.8921,0,0,7200); // Vehicle 4/
    CreateVehicle(433,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],7.0630,0,0,7200); // Vehicle 5/
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],156.3794,0,0,7200); // Vehicle 6/
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],14.4192,0,0,7200); // Vehicle 7/
    CreateVehicle(514,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],183.0471,0,0,7200); // Vehicle 8/
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],314.6674,0,0,7200); // Vehicle 9/
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],5.4393,0,0,7200); // Vehicle 10/
    CreateVehicle(433,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],3.6414,0,0,7200); // Vehicle 11/
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],3.3280,0,0,7200); // Vehicle 12/
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],184.8369,0,0,7200); // Vehicle 13/
    CreateVehicle(514,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],358.0526,0,0,7200); // Vehicle 14/
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],162.6114,0,0,7200); // Vehicle 15
    return 1;
} //Stock is executed in OnGameModeInit
Reply
#2

You have to do a new random each time, before creating the vehicle.
Reply
#3

Quote:
Originally Posted by Xabi
Посмотреть сообщение
You have to do a new random each time, before creating the vehicle.
Like setting up a loop?
Reply
#4

No, his suggestion is like this:
pawn Код:
new vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(411, RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2], 228.5856, 0, 0, 7200); //Test Veh. 1
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(412, RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2], 340.1074, 0, 0, 7200); //Test Veh. 2
    vrs = random(sizeof(RandomVehicleSpawns));    
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],2.7240,0,0,7200); // Vehicle 1/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(514,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],88.3344,0,0,7200); // Vehicle 2/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],1.1765,0,0,7200); // Vehicle 3/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],37.8921,0,0,7200); // Vehicle 4/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(433,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],7.0630,0,0,7200); // Vehicle 5/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],156.3794,0,0,7200); // Vehicle 6/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],14.4192,0,0,7200); // Vehicle 7/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(514,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],183.0471,0,0,7200); // Vehicle 8/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],314.6674,0,0,7200); // Vehicle 9/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],5.4393,0,0,7200); // Vehicle 10/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(433,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],3.6414,0,0,7200); // Vehicle 11/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],3.3280,0,0,7200); // Vehicle 12/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],184.8369,0,0,7200); // Vehicle 13/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(514,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],358.0526,0,0,7200); // Vehicle 14/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],162.6114,0,0,7200); // Vehicle 15
But it's bad, because there is still a possibility, that some of the vehicles will spawn at the same location.
Reply
#5

Quote:
Originally Posted by Scottas
Посмотреть сообщение
No, his suggestion is like this:
pawn Код:
new vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(411, RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2], 228.5856, 0, 0, 7200); //Test Veh. 1
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(412, RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2], 340.1074, 0, 0, 7200); //Test Veh. 2
    vrs = random(sizeof(RandomVehicleSpawns));    
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],2.7240,0,0,7200); // Vehicle 1/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(514,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],88.3344,0,0,7200); // Vehicle 2/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],1.1765,0,0,7200); // Vehicle 3/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],37.8921,0,0,7200); // Vehicle 4/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(433,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],7.0630,0,0,7200); // Vehicle 5/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],156.3794,0,0,7200); // Vehicle 6/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],14.4192,0,0,7200); // Vehicle 7/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(514,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],183.0471,0,0,7200); // Vehicle 8/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],314.6674,0,0,7200); // Vehicle 9/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],5.4393,0,0,7200); // Vehicle 10/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(433,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],3.6414,0,0,7200); // Vehicle 11/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],3.3280,0,0,7200); // Vehicle 12/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],184.8369,0,0,7200); // Vehicle 13/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(514,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],358.0526,0,0,7200); // Vehicle 14/
    vrs = random(sizeof(RandomVehicleSpawns));
    CreateVehicle(568,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],162.6114,0,0,7200); // Vehicle 15
But it's bad, because there is still a possibility, that some of the vehicles will spawn at the same location.
There a way you can prevent that from happening?
Reply
#6

Yes. Create function that would randomise arrays indexes and then create vehicles. Like:
pawn Код:
new indexes[sizeof(RandomVehicleSpawns)];
Randomise(indexes); // so array 'indexes' would contain something like {14, 2, 8, 3, 6...}

new vrs = indexes[0];
    CreateVehicle(411, RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2], 228.5856, 0, 0, 7200); //Test Veh. 1
    vrs = indexes[1];
    CreateVehicle(412, RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2], 340.1074, 0, 0, 7200); //Test Veh. 2
    vrs = indexes[2];
    CreateVehicle(463,RandomVehicleSpawns[vrs][0], RandomVehicleSpawns[vrs][1], RandomVehicleSpawns[vrs][2],2.7240,0,0,7200); // Vehicle 1/
    //and so on
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)