PDA

View Full Version : Annoyed - confused - Befuzzled


Prokill911
05/02/2015, 08:10 PM
Okay, So I want it to search for a house thats within 2500..
But it just keeps giving the same house...... Literally 35 attempts it gives the same place.
getClosestPizzaDelivery(playerid) {
new Float:pDistance;
new index;
for(new i=0;i<sizeof(Houses);i++) {
pDistance = GetPlayerDistanceFromPoint(playerid, Houses[i][EHouseX], Houses[i][EHouseY], Houses[i][EHouseZ]);
if(pDistance > random(2500)) {
continue;
}
index = i;
}
SetPVarFloat(playerid, "PizzaTravelDistance", pDistance);
return index;
}

Schneider
05/02/2015, 08:27 PM
Try this:


getClosestPizzaDelivery(playerid)
{
new Float:pDistance, Float:nDistance = 9999.9, index = -1;
for(new i=0;i<sizeof(Houses);i++)
{
pDistance = GetPlayerDistanceFromPoint(playerid, Houses[i][EHouseX], Houses[i][EHouseY], Houses[i][EHouseZ]);
if(pDistance < nDistance)
{
nDistance = pDistance;
index = i;
}
}
SetPVarFloat(playerid, "PizzaTravelDistance", nDistance);
return index;
}

Prokill911
05/02/2015, 08:52 PM
Try this:


getClosestPizzaDelivery(playerid)
{
new Float:pDistance, Float:nDistance = 9999.9, index = -1;
for(new i=0;i<sizeof(Houses);i++)
{
pDistance = GetPlayerDistanceFromPoint(playerid, Houses[i][EHouseX], Houses[i][EHouseY], Houses[i][EHouseZ]);
if(pDistance < nDistance)
{
nDistance = pDistance;
index = i;
}
}
SetPVarFloat(playerid, "PizzaTravelDistance", pDistance);
return index;
}


I'll give it a try an let you know :).

Edit:
Didn't work.. still keeps going to the same place: Distance == 2764.433349

Schneider
05/02/2015, 09:00 PM
I made one little error: In the SetPVarFloat-function, change: pDistance to nDistance.

Prokill911
05/02/2015, 09:03 PM
I made one little error: In the SetPVarFloat-function, change: pDistance to nDistance.

trying it now :), I'll edit this after


Didn't work Q_Q...

Still sending to the exact same spot over an over.
IF there any way I can make it so If It's !< (100.0) !> (2000)

Keyhead
05/02/2015, 09:41 PM
Anyone know how to fix this?

Schneider
05/02/2015, 09:48 PM
I just tested it with some random generated coordinates.and the function works good as it should...
Are you sure the house-coordinates are stored correctly and are you using the function correctly? Can you show us the command where you use this function?

Edit: This is the code I used to test it:

//Top of script:

enum hinfo
{
Float:EHouseX,
Float:EHouseY,
Float:EHouseZ
};
new Houses[90][hinfo];

//@ OnGameModeInit
for(new i; i<90; i++)
{
Houses[i][EHouseX] = (random(6000)-3000);
Houses[i][EHouseY] = (random(6000)-3000);
Houses[i][EHouseZ] = (random(30));
}

//@ OnPlayerConnect:

for(new i; i<90; i++)
{
SetPlayerMapIcon(playerid, i, Houses[i][EHouseX], Houses[i][EHouseY], Houses[i][EHouseZ], 31, 0, MAPICON_GLOBAL);
}

//Command:

COMMAND:getnearesthouse(playerid, params[])
{
new house = getClosestPizzaDelivery(playerid);
if(house != -1)
{
SetPlayerCheckpoint(playerid, Houses[house][EHouseX], Houses[house][EHouseY], Houses[house][EHouseZ], 6.0);
}
return 1;
}

It works as it should, It shows a checkpoint at the nearest 'house'.

Prokill911
05/02/2015, 10:02 PM
I just tested it with some random generated coordinates.and the function works good as it should...
Are you sure the house-coordinates are stored correctly and are you using the function correctly? Can you show us the command where you use this function?

Edit: This is the code I used to test it:

//Top of script:

enum hinfo
{
Float:EHouseX,
Float:EHouseY,
Float:EHouseZ
};
new Houses[90][hinfo];

//@ OnGameModeInit
for(new i; i<90; i++)
{
Houses[i][EHouseX] = (random(6000)-3000);
Houses[i][EHouseY] = (random(6000)-3000);
Houses[i][EHouseZ] = (random(30));
}

//@ OnPlayerConnect:

for(new i; i<90; i++)
{
SetPlayerMapIcon(playerid, i, Houses[i][EHouseX], Houses[i][EHouseY], Houses[i][EHouseZ], 31, 0, MAPICON_GLOBAL);
}

//Command:

COMMAND:getnearesthouse(playerid, params[])
{
new house = getClosestPizzaDelivery(playerid);
if(house != -1)
{
SetPlayerCheckpoint(playerid, Houses[house][EHouseX], Houses[house][EHouseY], Houses[house][EHouseZ], 6.0);
}
return 1;
}

It works as it should, It shows a checkpoint at the nearest 'house'.



That's just selecting the closest house to your current location...
Also, My information saves via SQL, An selects all houses on loading an saves the info.;

How I need it = IT selects a random house >> GREATER than 500 distance an then returns it..

Prokill911
05/02/2015, 10:05 PM
Try this:


getClosestPizzaDelivery(playerid)
{
new Float:pDistance, Float:nDistance = 9999.9, index = -1;
for(new i=0;i<sizeof(Houses);i++)
{
pDistance = GetPlayerDistanceFromPoint(playerid, Houses[i][EHouseX], Houses[i][EHouseY], Houses[i][EHouseZ]);
if(pDistance < nDistance)
{
nDistance = pDistance;
index = i;
}
}
SetPVarFloat(playerid, "PizzaTravelDistance", nDistance);
return index;
}



THIS is the code which I'm using.
I've tried multiple things... But it wont select a house OVER 500 meters away...

getMaxPizzaDeliveryTime(playerid, index) {
new Float: pDistance = GetPlayerDistanceFromPoint(playerid, Houses[index][EHouseX], Houses[index][EHouseY], Houses[index][EHouseZ]);
return floatround(pDistance/10, floatround_round);
}
initiatePizzaDelivery(playerid) {
new job = GetPVarInt(playerid, "Job");
if(job != EJobType_PizzaMan) {
return 1;
}
new string[128];
new houseid = getClosestPizzaDelivery(playerid);
new deliverytime = getMaxPizzaDeliveryTime(playerid, houseid);
SetPlayerCheckpoint(playerid, Houses[houseid][EHouseX],Houses[houseid][EHouseY],Houses[houseid][EHouseZ],5.0);
SetPVarInt(playerid, "IsOnPizzaRoute", 1);
SetPVarFloat(playerid, "PizzaDeliveryTime", deliverytime);
format(string, sizeof(string), "You have %d seconds to deliver the pizza to the destination.", deliverytime);
SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
return 1;
}
getClosestPizzaDelivery(playerid) {
new Float:pDistance;
new index;
for(new i=0;i<sizeof(Houses);i++) {
pDistance = GetPlayerDistanceFromPoint(playerid, Houses[i][EHouseX], Houses[i][EHouseY], Houses[i][EHouseZ]);
if(pDistance > random(1000)) {
continue;
}
index = i;
}
SetPVarFloat(playerid, "PizzaTravelDistance", pDistance);
return index;
}

Schneider
05/02/2015, 10:37 PM
Well, you didn't mention it had to be at least 500 units away and since you called your function getClosestPizzaDelivery I assumed you wanted to get the closest location...

Anyway, try this one. This should select a random house between 500 and 2500 units away:

Make sure, when you use this function, it doesn't return '-1'. (No house found). I added this to prevent the server from getting in an infinite loop.

getClosestPizzaDelivery(playerid)
{
new Float:pDistance, i, index = -1, loop;
while(index == -1)
{
loop++;
i = random(sizeof(Houses));
pDistance = GetPlayerDistanceFromPoint(playerid, Houses[i][EHouseX], Houses[i][EHouseY], Houses[i][EHouseZ]);
if(500.0 < pDistance < 2500.0)
{
index = i;
}
if(loop == 500) return -1;
}
SetPVarFloat(playerid, "PizzaTravelDistance", pDistance );
return index;
}

Keyhead
07/02/2015, 01:01 AM
It seems to only be selecting two houses, always the same two. Any ideas?

Prokill911
17/04/2015, 05:58 PM
Fixed.