22.06.2014, 13:05
Problem is that we have an undefinite amount of matching relations but are unable to have an array with an undefinite amount of cells, so I'd recommend this way (Make sure to adjust MAX_RELATIONS once you update your relations array):
Result:
pawn Код:
#include "a_samp"
#define MAX_RELATIONS (2)
static const relations[][] = {
{0, 21, 25, 1},
{0, 21, 35, 22},
{1, 25, 35, 23},
{1, 27, 32, 24}
};
main() {
printf("African American, Age 21, Random SkinID: %i", getRandomSkin(0, 21));
printf("Caucasian, Age 32, Random SkinID: %i", getRandomSkin(1, 32));
}
static getRandomSkin(const ethnicity, const age) {
new relationIndexes[MAX_RELATIONS] = {-1, ...},
count = 0;
for(new i = 0; i < sizeof(relations); i++) {
if(relations[i][0] == ethnicity && relations[i][1] <= age && relations[i][2] >= age) {
relationIndexes[count] = i;
count++;
if(count >= MAX_RELATIONS) {
break;
}
}
}
if (count == 0) {
return -1;
}
return relations[relationIndexes[random(count)]][3];
}
Код:
African American, Age 21, Random SkinID: 1 Caucasian, Age 32, Random SkinID: 24