• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ I want to make TEXTCOLOR_WHITE_EXP into a random number, How do I achieve this?

Yuggi

Member
Joined
May 18, 2017
Messages
64
Reaction score
10
This is in const.h in the forgottenserver 1.2.

Code:
enum TextColor_t : uint8_t {
    TEXTCOLOR_BLUE = 5,
    TEXTCOLOR_LIGHTGREEN = 30,
    TEXTCOLOR_LIGHTBLUE = 35,
    TEXTCOLOR_MAYABLUE = 95,
    TEXTCOLOR_DARKRED = 108,
    TEXTCOLOR_LIGHTGREY = 129,
    TEXTCOLOR_SKYBLUE = 143,
    TEXTCOLOR_PURPLE = 155,
    TEXTCOLOR_RED = 180,
    TEXTCOLOR_ORANGE = 198,
    TEXTCOLOR_YELLOW = 210,
    TEXTCOLOR_DMG = 209,
    TEXTCOLOR_WHITE_EXP = 215,
    TEXTCOLOR_NONE = 255,
};

How do I make TEXTCOLOR_WHITE_EXP into a random number between 1 and 216? I want that when player kills monsters that the experience number that pops up should always be in a random color instead of just static white all the time.

I did try some different things but I always ended up with errors.
 
This is in const.h in the forgottenserver 1.2.

Code:
enum TextColor_t : uint8_t {
    TEXTCOLOR_BLUE = 5,
    TEXTCOLOR_LIGHTGREEN = 30,
    TEXTCOLOR_LIGHTBLUE = 35,
    TEXTCOLOR_MAYABLUE = 95,
    TEXTCOLOR_DARKRED = 108,
    TEXTCOLOR_LIGHTGREY = 129,
    TEXTCOLOR_SKYBLUE = 143,
    TEXTCOLOR_PURPLE = 155,
    TEXTCOLOR_RED = 180,
    TEXTCOLOR_ORANGE = 198,
    TEXTCOLOR_YELLOW = 210,
    TEXTCOLOR_DMG = 209,
    TEXTCOLOR_WHITE_EXP = 215,
    TEXTCOLOR_NONE = 255,
};

How do I make TEXTCOLOR_WHITE_EXP into a random number between 1 and 216? I want that when player kills monsters that the experience number that pops up should always be in a random color instead of just static white all the time.

I did try some different things but I always ended up with errors.

Since the enums arn't in-line you have to use the enums in an array;
Lua:
math.randomseed(os.time())

local effects = {TEXTCOLOR_BLUE, TEXTCOLOR_LIGHTGREEN, 6TEXTCOLOR_LIGHTBLUE}
math.random(effects)

If the seed is set you don't have to include that in your code, the math.random function will return a random value from the table, (effects)
 
inside of creature.cpp, under void Creature::eek:nGainExperience(uint64_t gainExp, Creature* target)
C++:
    TextMessage message(MESSAGE_EXPERIENCE_OTHERS, ucfirst(getNameDescription()) + " gained " + std::to_string(gainExp) + (gainExp != 1 ? " experience points." : " experience point."));
    static constexpr std::array<TextColor_t, 12> colors = {
        TEXTCOLOR_BLUE,
        TEXTCOLOR_LIGHTGREEN,
        TEXTCOLOR_LIGHTBLUE,
        TEXTCOLOR_MAYABLUE,
        TEXTCOLOR_DARKRED,
        TEXTCOLOR_LIGHTGREY,
        TEXTCOLOR_SKYBLUE,
        TEXTCOLOR_PURPLE,
        TEXTCOLOR_RED,
        TEXTCOLOR_ORANGE,
        TEXTCOLOR_YELLOW,
        TEXTCOLOR_WHITE_EXP,
    };
    message.position = position;
    message.primary.color = colors[uniform_random(0, colors.size() - 1)];
    message.primary.value = gainExp;
 
inside of creature.cpp, under void Creature::eek:nGainExperience(uint64_t gainExp, Creature* target)
C++:
    TextMessage message(MESSAGE_EXPERIENCE_OTHERS, ucfirst(getNameDescription()) + " gained " + std::to_string(gainExp) + (gainExp != 1 ? " experience points." : " experience point."));
    static constexpr std::array<TextColor_t, 12> colors = {
        TEXTCOLOR_BLUE,
        TEXTCOLOR_LIGHTGREEN,
        TEXTCOLOR_LIGHTBLUE,
        TEXTCOLOR_MAYABLUE,
        TEXTCOLOR_DARKRED,
        TEXTCOLOR_LIGHTGREY,
        TEXTCOLOR_SKYBLUE,
        TEXTCOLOR_PURPLE,
        TEXTCOLOR_RED,
        TEXTCOLOR_ORANGE,
        TEXTCOLOR_YELLOW,
        TEXTCOLOR_WHITE_EXP,
    };
    message.position = position;
    message.primary.color = colors[uniform_random(0, colors.size() - 1)];
    message.primary.value = gainExp;

Thanks this will definitely work but it's not 100% what I wanted because it just gives a few colors, and there's 216 different textcolors that you can have in tibia. I have tried myself adding new text colors and confirmed it works like that. All available colors come from here: 216 Web Safe Colors List - by WebSafeColors.design as you can see the 5th color is blue, 210th is yellow, last color is white etc. so it's the same colors TEXTCOLOR uses.

Maybe to actually get this to work I would have to add all of these colors inside const.h then I could also just use what you posted but adding all of the colors would be kind of painful. I think it should just be possible to for example add a new TEXTCOLOR_RANDOM that rolls between 0,215 everytime that color is called. However I have no idea how to fix this myself.
 
Thanks this will definitely work but it's not 100% what I wanted because it just gives a few colors, and there's 216 different textcolors that you can have in tibia. I have tried myself adding new text colors and confirmed it works like that. All available colors come from here: 216 Web Safe Colors List - by WebSafeColors.design as you can see the 5th color is blue, last color is white etc. so it's the same colors TEXTCOLOR uses.

Maybe to actually get this to work I would have to add all of these colors inside const.h then I could also just use what you posted but adding all of the colors would be kind of painful. I think it should just be possible to for example add a new TEXTCOLOR_RANDOM that rolls between 0,215 everytime that color is called. However I have no idea how to fix this myself.
easiest solution would to just add the colors you want to const.h and add them to the std::array i provided
message.primary.color has a type of TextColor_t which only accepts values from inside that enum
if you try to assign a number to it you'll get an error
 
inside of creature.cpp, under void Creature::eek:nGainExperience(uint64_t gainExp, Creature* target)
C++:
    TextMessage message(MESSAGE_EXPERIENCE_OTHERS, ucfirst(getNameDescription()) + " gained " + std::to_string(gainExp) + (gainExp != 1 ? " experience points." : " experience point."));
    static constexpr std::array<TextColor_t, 12> colors = {
        TEXTCOLOR_BLUE,
        TEXTCOLOR_LIGHTGREEN,
        TEXTCOLOR_LIGHTBLUE,
        TEXTCOLOR_MAYABLUE,
        TEXTCOLOR_DARKRED,
        TEXTCOLOR_LIGHTGREY,
        TEXTCOLOR_SKYBLUE,
        TEXTCOLOR_PURPLE,
        TEXTCOLOR_RED,
        TEXTCOLOR_ORANGE,
        TEXTCOLOR_YELLOW,
        TEXTCOLOR_WHITE_EXP,
    };
    message.position = position;
    message.primary.color = colors[uniform_random(0, colors.size() - 1)];
    message.primary.value = gainExp;
How do you paste this code so that it works properly?
C++:
void Creature::onGainExperience(uint64_t gainExp, Creature* target)
{
    if (gainExp == 0 || !master) {
        return;
    }

    gainExp /= 2;
    master->onGainExperience(gainExp, target);

    std::ostringstream strExp;
    strExp << gainExp;
    g_game.addAnimatedText(strExp.str(), _position, TEXTCOLOR_WHITE_EXP);
}

I try but dont work
 
Back
Top