new const _weathers[][] = {
"EXTRASUNNY_LA",
"SUNNY_LA",
"EXTRASUNNY_SMOG_LA",
"SUNNY_SMOG_LA",
"CLOUDY_LA",
"SUNNY_SF",
"EXTRASUNNY_SF",
"CLOUDY_SF",
"RAINY_SF",
"FOGGY_SF",
"SUNNY_VEGAS",
"EXTRASUNNY_VEGAS",
"CLOUDY_VEGAS",
"EXTRASUNNY_COUNTRYSIDE",
"SUNNY_COUNTRYSIDE",
"CLOUDY_COUNTRYSIDE",
"RAINY_COUNTRYSIDE",
"EXTRASUNNY_DESERT",
"SUNNY_DESERT"
};
stock GetWeatherSimplifiedNameFromID(weatherid) {
new weathername[30];
weathername = GetWeatherNameFromID(weatherid);
new string[20];
if (strfind(weathername, "SUNNY", true) != -1) {
string = "Sunny";
return string;
}
if (strfind(weathername, "ClOUDY", true) != -1) {
string = "Cloudy";
return string;
}
if (strfind(weathername, "RAINY", true) != -1) {
string = "Rainy";
return string;
}
if (strfind(weathername, "FOGGY", true) != -1) {
string = "Foggy";
return string;
}
else string = "Unidentified";
return string;
}
So I've made this little function, which was supposed to return a simplified name of the weatherID (https://sampwiki.blast.hk/wiki/WeatherID).
PHP код:
The problems:
I've also considered to use `strcmp` (checking the first characters of the weather) instead of `strfind()`. Any help/example/tutorial would be helpful. |
new const _weathers[][] = { // id type +bits
"EXTRASUNNY_LA", // 0 Sunny 2
"SUNNY_LA", // 1 Sunny 4
"EXTRASUNNY_SMOG_LA", // 2 Sunny 6
"SUNNY_SMOG_LA", // 3 Sunny 8
"CLOUDY_LA", // 4 Cloudy 10
"SUNNY_SF", // 5 Sunny 12
"EXTRASUNNY_SF", // 6 Sunny 14
"CLOUDY_SF", // 7 Cloudy 16
"RAINY_SF", // 8 Rainy 18
"FOGGY_SF", // 9 Foggy 20
"SUNNY_VEGAS", // a Sunny 22
"EXTRASUNNY_VEGAS", // b Sunny 24
"CLOUDY_VEGAS", // c Cloudy 26
"EXTRASUNNY_COUNTRYSIDE", // d Sunny 28
"SUNNY_COUNTRYSIDE", // e Sounny 30
"CLOUDY_COUNTRYSIDE", // f Cloudy 32
"RAINY_COUNTRYSIDE", // 10 Rainy 1 + 2
"EXTRASUNNY_DESERT", // 11 Sunny 1 + 4
"SUNNY_DESERT" // 12 Sunny 1 + 6
};
#define WEATHER_TYPES (19) // Just a quantity of _weathers array
new const _flags[] = {
0x410e4100, 0x2
};
new const simplifiedNames[][] = {
"Sunny", "Cloudy", "Rainy", "Foggy"
};
GetWeatherSimplifiedNameFromID(weatherid) {
new output[10];
if(weatherid >= WEATHER_TYPES) {
format(output, 10, "Undefined");
} else {
new flagsCell = 0, wid = weatherid;
while(wid >>= 4) flagsCell++; // We divide by 16, because that's how many types we can handle in single flag (_flags) - cells are 32 bit wide. We use 2 bits per weather, thus 32/2.
weatherid %= 16;
weatherid *= 2;
weatherid = (_flags[flagsCell] >> weatherid) & 3;
format(output, 10, "%s", simplifiedNames[weatherid]);
}
return output;
}
for(new i = 0; i < 22; i++) {
printf("GetWeat..FromId(%d): %s", i, GetWeatherSimplifiedNameFromID(i));
}
[15:24:45] GetWeat..FromId(0): Sunny [15:24:45] GetWeat..FromId(1): Sunny [15:24:45] GetWeat..FromId(2): Sunny [15:24:45] GetWeat..FromId(3): Sunny [15:24:45] GetWeat..FromId(4): Cloudy [15:24:45] GetWeat..FromId(5): Sunny [15:24:45] GetWeat..FromId(6): Sunny [15:24:45] GetWeat..FromId(7): Cloudy [15:24:45] GetWeat..FromId(8): Rainy [15:24:45] GetWeat..FromId(9): Foggy [15:24:45] GetWeat..FromId(10): Sunny [15:24:45] GetWeat..FromId(11): Sunny [15:24:45] GetWeat..FromId(12): Cloudy [15:24:45] GetWeat..FromId(13): Sunny [15:24:45] GetWeat..FromId(14): Sunny [15:24:45] GetWeat..FromId(15): Cloudy [15:24:45] GetWeat..FromId(16): Rainy [15:24:45] GetWeat..FromId(17): Sunny [15:24:45] GetWeat..FromId(18): Sunny [15:24:45] GetWeat..FromId(19): Undefined [15:24:45] GetWeat..FromId(20): Undefined [15:24:45] GetWeat..FromId(21): Undefined
States | Sunny | Cloudy | Rainy | Foggy |
Decimal | 0 | 1 | 2 | 3 |
Binary | 0 | 1 | 10 | 11 |
new const _flags[] = {
0x410E4100, 0x2
};
#define WEATHER_SUNNY (0)
#define WEATHER_CLOUDY (1)
#define WEATHER_RAINY (2)
#define WEATHER_FOGGY (3)
new const _weatherTypes[] = {
WEATHER_SUNNY, // EXTRASUNNY_LA
WEATHER_SUNNY, // SUNNY_LA
WEATHER_SUNNY // EXTRASUNNY_SMOG_LA
...
};
Method | Math | Result (in bits) | Bits that are acutally used | Bits waste(%) |
Using flags | 32*2 | 64 | 38 | 40 |
2nd method | 32*19 | 608 | 38 | 93 |
while(wid >>= 4) flagsCell++;
weatherid %= 16;
weatherid *= 2;
weatherid = (_flags[flagsCell] >> weatherid) & 3;