How would I do this in an array or static const?
#1

I've got a script in mind but I'm not sure how I would go about making this. I want a little script to return certain IDs, skin IDs, according to the player's information.

Let's say I have a players "origin", and "age".
Then I have something like this:
pawn Код:
// Origin, Age, Skin ID
{"African American", {16..25}, {1}}; // 16..25 as in ranging from through to 25
{"African American", {16..25}, {22}};
{"Caucasian", {25..35}, {23}};
The players origin would be African American, and the Age would be 22. How would I loop through the information above (and would it be an array, static const..?), and return the last number, skin id?
Reply
#2

First of all, you have to distinguish the terms from each other. They all mean something in particular and can be combined all together:

Static: The static keyword makes your variables only available in the file they get declared in. The values therefore can't be used in any other file and also it avoids name clashing of variables between different files.

Const: The keyword const defines a constant value which can't be changed. From time to time it is useful to make use of constant values for a more compact source code and to avoid logic programming faults.

Array: An array is a set of values and therefore cells. You can combine an array with the const and static keyword to make the values constant and to reduce the array's scope to the file only.


In your case I wouldn't use strings to determine which skins are reserved for which origins as string comparissons are simply more complicated than simple integer comparissons. In case you are in need of the origins' names aswell, simply create a secondary array with them. You may think of database normalisation in this case as the concept behind it is exactly the same.

Instead of setting a range of ages, I would simply set the minimum and maximum of the range and do two comparissons later on.

pawn Код:
#include "a_samp"

static const originData[][] = {
    "African American", // Index: 0
    "Caucasian" // Index: 1
};

static const relations[][] = {
    {0, 16, 25, 1}, // Ethnicity/Origin, Minimum age, Maximum age, Skin
    {0, 26, 35, 22},
    {1, 25, 35, 23}
};

main() {
    // Two characters (1: African American, Age 21; 2: Caucasian, Age 35)
    new data[2][2] = {{0, 21}, {1, 35}};
    for(new i = 0; i < sizeof(data); i++) {
        for(new j = 0; j < sizeof(relations); j++) {
            if(relations[j][0] == data[i][0] && relations[j][1] <= data[i][1] && relations[j][2] >= data[i][1]) {
                printf("SkinID for dataset %i: %i", i, relations[j][3]);
            }
        }
    }
}
Result:

Код:
SkinID for dataset 0: 1
SkinID for dataset 1: 23
Make sure to set a default skin to cover logic gaps in your relation array.
Reply
#3

Quote:
Originally Posted by Campbell-
Посмотреть сообщение
First of all, you have to distinguish the terms from each other. They all mean something in particular and can be combined all together:

Static: The static keyword makes your variables only available in the file they get declared in. The values therefore can't be used in any other file and also it avoids name clashing of variables between different files.

Const: The keyword const defines a constant value which can't be changed. From time to time it is useful to make use of constant values for a more compact source code and to avoid logic programming faults.

Array: An array is a set of values and therefore cells. You can combine an array with the const and static keyword to make the values constant and to reduce the array's scope to the file only.


In your case I wouldn't use strings to determine which skins are reserved for which origins as string comparissons are simply more complicated than simple integer comparissons. In case you are in need of the origins' names aswell, simply create a secondary array with them. You may think of database normalisation in this case as the concept behind it is exactly the same.

Instead of setting a range of ages, I would simply set the minimum and maximum of the range and do two comparissons later on.

pawn Код:
#include "a_samp"

static const originData[][] = {
    "African American", // Index: 0
    "Caucasian" // Index: 1
};

static const relations[][] = {
    {0, 16, 25, 1}, // Ethnicity/Origin, Minimum age, Maximum age, Skin
    {0, 26, 35, 22},
    {1, 25, 35, 23}
};

main() {
    // Two characters (1: African American, Age 21; 2: Caucasian, Age 35)
    new data[2][2] = {{0, 21}, {1, 35}};
    for(new i = 0; i < sizeof(data); i++) {
        for(new j = 0; j < sizeof(relations); j++) {
            if(relations[j][0] == data[i][0] && relations[j][1] <= data[i][1] && relations[j][2] >= data[i][1]) {
                printf("SkinID for dataset %i: %i", i, relations[j][3]);
            }
        }
    }
}
Result:

Код:
SkinID for dataset 0: 1
SkinID for dataset 1: 23
Make sure to set a default skin to cover logic gaps in your relation array.
Thank you for going to that much effort.

I think I'd only need to use:
pawn Код:
static const relations[][] = {
    {0, 16, 25, 1}, // Ethnicity/Origin, Minimum age, Maximum age, Skin
    {0, 26, 35, 22},
    {1, 25, 35, 23}
};
How would I get a random 'skin' from that depending on the characters stats? So let's say I want to use a custom public, we'll say IsValidCharacterSkin for example, and then we have Origin and Age as player data, how would I match the Origin, and Age, to a relation, and then return the Skin? Also in mind there could be multiple Skins returned.

EDIT:
Would this work?
pawn Код:
forward IsValidCharacterSkin(playerid, skinid);
public IsValidCharacterSkin(playerid, skinid)
{
    for(new j = 0; j < sizeof(relations); j++) {
        if(skinid == relations[j][3]) skinid = j;
    }
    if(PlayerData[playerid][Origin] == relations[skinid][0] && PlayerData[playerid][Age] >= relations[skinid][1] || PlayerData[playerid][Age] <= relations[skinid][2]) return 1;

    return 0;
}
Reply
#4

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):

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];
}
Result:

Код:
African American, Age 21, Random SkinID: 1
Caucasian, Age 32, Random SkinID: 24
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)