SA-MP Forums Archive
Problem selecting mission which is over a set distance - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Problem selecting mission which is over a set distance (/showthread.php?tid=602643)



Problem selecting mission which is over a set distance - thegamer355 - 10.03.2016

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


Re: Problem selecting mission which is over a set distance - Nero_3D - 11.03.2016

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;




Re: Problem selecting mission which is over a set distance - thegamer355 - 11.03.2016

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


Re: Problem selecting mission which is over a set distance - Nero_3D - 11.03.2016

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;




Re: Problem selecting mission which is over a set distance - thegamer355 - 11.03.2016

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


Re: Problem selecting mission which is over a set distance - czerwony03 - 11.03.2016

Can you show me
Код:
mInfo
?


Re: Problem selecting mission which is over a set distance - thegamer355 - 11.03.2016

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];



Re: Problem selecting mission which is over a set distance - czerwony03 - 11.03.2016

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)


Re: Problem selecting mission which is over a set distance - czerwony03 - 11.03.2016

Check my post again.


Re: Problem selecting mission which is over a set distance - thegamer355 - 11.03.2016

Looks like the bug got fixed. Thanks everyone for helping