Probability in terrain tool

I have a question about this page: https://github.com/bjorn/tiled/wiki/TMX-Map-Format#tile

You can optionally indicate a probability for each tile. I know you have to edit the TMX/TSX manually, but do all the probabilities have to sum to 1? or 100? If you specify a value for one competing tile, do you have to specify a value for all of them?

Good question! The code for this is in fact quite involved:

// choose a candidate at random, with consideration for terrain probability
if (!matches.isEmpty()) {
    float random = ((float)rand() / RAND_MAX) * 100.f;
    float total = 0, unassigned = 0;

    // allow the tiles with assigned probability to take their share
    for (int i = 0; i < matches.size(); ++i) {
        float probability = matches[i]->terrainProbability();
        if (probability < 0.f) {
            ++unassigned;
            continue;
        }
        if (random < total + probability)
            return matches[i];
        total += probability;
    }

    // divide the remaining percentile by the numer of unassigned tiles
    float remainingShare = (100.f - total) / (float)unassigned;
    for (int i = 0; i < matches.size(); ++i) {
        if (matches[i]->terrainProbability() >= 0.f)
            continue;
        if (random < total + remainingShare)
            return matches[i];
        total += remainingShare;
    }
}

So in summary it tries to give each tile the exact probability you actually entered (in percent). And when there are a bunch of options that did not have a probability assigned and none of the others made it, they will have equal chance to be picked.

And now that I think about this, I think this probably isn’t very nice. Because if you do enter probabilities for all tiles, and you don’t make sure that the total is exactly 100%, then this code will randomly fail to find a matching tile even though there were some available. And when the total goes over 100%, some tiles will end up never getting picked.

What about instead we make it a little more deterministic, and say that the probabilities are relative to each other, with a default probability of 1? Then I would just sum them all up, pick a random number up to the sum and iterate again to pick the match.

I’ve pushed the suggested improvement to master:

Thanks for the explanation, that makes a lot of sense! I’m fine with either the old or new way, I think going forward the new way will be better! will it be included in the next release?

I’m glad to hear you also think the new way is better! The change is already available in the daily builds and will be included in Tiled 0.12.

Great! You are awesome, thanks for the fast response. By the way, I signed up as a patron on patreon. It’s a very small amount, but it’s the least I can do to say thanks for such a great program. I also finally have a desktop mac for programming and want to look into contributing code to tiled at some point.

You’re welcome and thanks for being a patron! You’re part of the reason why I’ve had two full days to work on Tiled and can look forward to two more.

Unfortunately, dealing properly with the isometric projection in the object resizing improvements is much harder than I had anticipated, but still now it takes only days to get it working as opposed to months!

Your work is highly appreciated! I wish I could do more on patreon, if I can in the future I’ll increase my pledge. Also if I find time/energy to start learning tiled’s codebase and contributing, I’m sure you’ll hear from me then too … :wink: