Problem selecting mission which is over a set distance
#1

Hi,

I am having a little problem with my mission script.
I need to find all missions which are over a set distance, in this case it's over 500. (which isn't what my problem is)
But my problem is, selecting a random missions of those missions.

The missions are being loaded in the mInfo using an enum
The suitedmission[p] is where i tried to save the suited missions in, which i'm sure of that isn't the way to do it.
And before people tell me to save the missions in the gamemode itself, it's being loaded from the database
Код:
stock GetNextMissionCheckpoint(playerid)
{
    new msg[50];
	new Float:distance = 1000000.0, nearestID = -1;
	for(new p = 0; p < sizeof(mInfo); p++)
	{
	    if(mInfo[p][mID] != 0)
	    {
	    	distance = GetPlayerDistanceFromPoint(playerid, mInfo[p][mX], mInfo[p][mY], mInfo[p][mY]);
	    	if(distance > 500)
	    	{
				suitedmission[p] = mInfo[p][mID];
			}
		}
	} // problem down here
	new i = random(
	format(msg,sizeof(msg), "Mission ID %i, X: %f, Y: %f, Z: %f", mInfo[i][mID], mInfo[i][mX], mInfo[i][mY], mInfo[i][mZ]);
	SendClientMessage(playerid, COLOR_WHITE, msg);
	nearestID = i;
	return nearestID; // problem up here
}
I tried a lot of things already, but i can't seem to find a way to do this correct.

Thanks in advance, TheGamer
Reply
#2

Normally you would do it like that, create an array, save all ids and use random afterwards
PHP код:
stock GetNextMissionCheckpoint(playerid) {
    
#define SIZE 64
    #if sizeof mInfo > SIZE
        #undef SIZE
        #define SIZE sizeof mInfo
    #endif
    
new
        
count,
        
= -1,
        
tmp[SIZE]
    ;
    while(++
sizeof mInfo) {
        if(
mInfo[p][mID] != 0) { // \/ changed second mY to mZ
            
if(500.0 GetPlayerDistanceFromPoint(playeridmInfo[p][mX], mInfo[p][mY], mInfo[p][mZ])) {
                
tmp[count++] = mInfo[p][mID];
            }
        }
    }
    
tmp[random(count)];
    
format(tmpsizeof tmp"Mission ID %i, X: %f, Y: %f, Z: %f"mInfo[p][mID], mInfo[p][mX], mInfo[p][mY], mInfo[p][mZ]);
    
SendClientMessage(playeridCOLOR_WHITEtmp);
    return 
p;

Reply
#3

For some reason that code always ends up with mission ID 0, which doesn't exist, so it's point is at 0.0 0.0 0.0
But once i remove the mission ID checker so it can be mission id 0, then it's always comes up with mission ID 1
Reply
#4

Quote:
Originally Posted by thegamer355
Посмотреть сообщение
For some reason that code always ends up with mission ID 0, which doesn't exist, so it's point is at 0.0 0.0 0.0
But once i remove the mission ID checker so it can be mission id 0, then it's always comes up with mission ID 1
It could be possible that no matches were found ?
Because there shouldn't be an id 0

PHP код:
stock GetNextMissionCheckpoint(playerid) {
    
#define SIZE 64
    #if sizeof mInfo > SIZE
        #undef SIZE
        #define SIZE sizeof mInfo
    #endif
    
new
        
count,
        
= -1,
        
tmp[SIZE]
    ;
    while(++
sizeof mInfo) {
        if(
mInfo[p][mID] != 0) { // \/ changed second mY to mZ
            
if(500.0 GetPlayerDistanceFromPoint(playeridmInfo[p][mX], mInfo[p][mY], mInfo[p][mZ])) {
                
tmp[count++] = mInfo[p][mID];
            }
        }
    }
    if(
count == 0) {
        return 
printf("No mission found!");
    }
    
printf("count %d"count); // add some debug prints
    
for(new icount; ++iprintf("%d - %d"itmp[i]);
    
tmp[random(count)];
    
format(tmpsizeof tmp"Mission ID %i, X: %f, Y: %f, Z: %f"mInfo[p][mID], mInfo[p][mX], mInfo[p][mY], mInfo[p][mZ]);
    
SendClientMessage(playeridCOLOR_WHITEtmp);
    return 
p;

Reply
#5

The code works, but the problem with mission ID 0 still occures, in the console it shows for example:
0 - 3
1 - 4
and it still sends mission ID 0, which i really can't explain.
But that only happens rarely
Reply
#6

Can you show me
Код:
mInfo
?
Reply
#7

Quote:
Originally Posted by czerwony03
Посмотреть сообщение
Can you show me
Код:
mInfo
?
Ofcourse, here it is

Код:
enum missioninfo
{
	mID,
	Float:mX,
	Float:mY,
	Float:mZ,
	bool:loaded
};
new mInfo[MAX_Missions][missioninfo];
Reply
#8

Quote:
Originally Posted by thegamer355
Посмотреть сообщение
The code works, but the problem with mission ID 0 still occures, in the console it shows for example:
0 - 3
1 - 4
and it still sends mission ID 0, which i really can't explain.
But that only happens rarely
Its not mission id 0, its first element of tmp array that refer to mission id 3.

Change this:
Код:
 p = tmp[random(count)];
to this:
Код:
  p = tmp[random(count-1)];
And install crashdetect on your server. It will help you finding errors like that (variable over bound)
Reply
#9

Check my post again.
Reply
#10

Looks like the bug got fixed. Thanks everyone for helping
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)