can anyone explain this command? -
ivndosos - 22.03.2018
I saw this command, can anyone explain it?
I already know what it does but I just want to learn from it, what does the Slot = -1 do?
Код:
CMD:richlist(playerid,params[]) {
#pragma unused params
new string[128], Slot1 = -1, Slot2 = -1, Slot3 = -1, Slot4 = -1, Slot5 = -1, Slot6 = -1, Slot7 = -1, Slot8 = -1, Slot9 = -1, Slot10 = -1, HighestCash = -9999;
SendClientMessage(playerid,COLOR_WHITE,"Rich List:");
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x)) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot1 = x;
}
HighestCash = -9999;
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x) && x != Slot1) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot2 = x;
}
HighestCash = -9999;
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x) && x != Slot1 && x != Slot2) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot3 = x;
}
HighestCash = -9999;
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x) && x != Slot1 && x != Slot2 && x != Slot3) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot4 = x;
}
HighestCash = -9999;
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x) && x != Slot1 && x != Slot2 && x != Slot3 && x != Slot4) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot5 = x;
}
HighestCash = -9999;
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x) && x != Slot1 && x != Slot2 && x != Slot3 && x != Slot4 && x != Slot5) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot6 = x;
}
HighestCash = -9999;
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x) && x != Slot1 && x != Slot2 && x != Slot3 && x != Slot4 && x != Slot5 && x != Slot6) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot7 = x;
}
HighestCash = -9999;
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x) && x != Slot1 && x != Slot2 && x != Slot3 && x != Slot4 && x != Slot5 && x != Slot6 && x != Slot7) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot8 = x;
}
HighestCash = -9999;
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x) && x != Slot1 && x != Slot2 && x != Slot3 && x != Slot4 && x != Slot5 && x != Slot6 && x != Slot7 && x != Slot8) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot9 = x;
}
HighestCash = -9999;
for(new x=0; x<MAX_PLAYERS; x++) if (IsPlayerConnected(x) && x != Slot1 && x != Slot2 && x != Slot3 && x != Slot4 && x != Slot5 && x != Slot6 && x != Slot7 && x != Slot8 && x != Slot9) if (GetPlayerMoney(x) >= HighestCash) {
HighestCash = GetPlayerMoney(x);
Slot10 = x;
}
format(string, sizeof(string), "(%d) %s - $%d", Slot1,PlayerName2(Slot1),GetPlayerMoney(Slot1) );
SendClientMessage(playerid,COLOR_WHITE,string);
if(Slot2 != -1) {
format(string, sizeof(string), "(%d) %s - $%d", Slot2,PlayerName2(Slot2),GetPlayerMoney(Slot2) );
SendClientMessage(playerid,COLOR_WHITE,string);
}
if(Slot3 != -1) {
format(string, sizeof(string), "(%d) %s - $%d", Slot3,PlayerName2(Slot3),GetPlayerMoney(Slot3) );
SendClientMessage(playerid,COLOR_WHITE,string);
}
if(Slot4 != -1) {
format(string, sizeof(string), "(%d) %s - $%d", Slot4,PlayerName2(Slot4),GetPlayerMoney(Slot4) );
SendClientMessage(playerid,COLOR_WHITE,string);
}
if(Slot5 != -1) {
format(string, sizeof(string), "(%d) %s - $%d", Slot5,PlayerName2(Slot5),GetPlayerMoney(Slot5) );
SendClientMessage(playerid,COLOR_WHITE,string);
}
if(Slot6 != -1) {
format(string, sizeof(string), "(%d) %s - $%d", Slot6,PlayerName2(Slot6),GetPlayerMoney(Slot6) );
SendClientMessage(playerid,COLOR_WHITE,string);
}
if(Slot7 != -1) {
format(string, sizeof(string), "(%d) %s - $%d", Slot7,PlayerName2(Slot7),GetPlayerMoney(Slot7) );
SendClientMessage(playerid,COLOR_WHITE,string);
}
if(Slot8 != -1) {
format(string, sizeof(string), "(%d) %s - $%d", Slot8,PlayerName2(Slot8),GetPlayerMoney(Slot8) );
SendClientMessage(playerid,COLOR_WHITE,string);
}
if(Slot9 != -1) {
format(string, sizeof(string), "(%d) %s - $%d", Slot9,PlayerName2(Slot9),GetPlayerMoney(Slot9) );
SendClientMessage(playerid,COLOR_WHITE,string);
}
if(Slot10 != -1) {
format(string, sizeof(string), "(%d) %s - $%d", Slot10,PlayerName2(Slot10),GetPlayerMoney(Slot10) );
SendClientMessage(playerid,COLOR_WHITE,string);
}
return 1;
}
Is it necessary to use that?
Re: can anyone explain this command? -
OneDay - 22.03.2018
The code is bad you should use quicksort
Re: can anyone explain this command? -
ivndosos - 22.03.2018
example?
Re: can anyone explain this command? -
NaS - 22.03.2018
What it does is simply search for the richest player in the first loop.
In the second loop it does the same with the exeption of the first player.
The third, fourth etc all do the exact same thing with exceptions for all players that are already in the highscore (done manually as you can see each loop grows bigger in line length).
Slot1 = -1 etc make sure that this slot gets marked as unused, so if there are only 3 players found for the highscore, all other slots will be considered empty.
In code this indeed looks very bad and bloated (also it's not dynamic, you cannot just add more slots without adding/editing several lines of code).
The logic however is almost the same as
some sorting algorithms, so I wouldn't say it's that bad (although annoying to change number of slots and thus very inconvenient to use).