Random distribution with Position list

Aus EnigmaWiki

Wechseln zu: Navigation, Suche
Available languages: Deutsch, English, Русский

Inhaltsverzeichnis

Random distribution of objects by means of a Position list

Problem definition

You have one (or several) object(s), that can randomly appear at different positions.

Solution

As long as there are not too many different positions and there are rather single positions and not complete surfaces of possible positions, it isn't worthwhile to use zones.

You are better to provide an array with all possible positions and then select a random array element.

Note: With several objects the problem of the overwriting always exists! We must determine somehow whether the position just selected has already been used. You could ignore this, but you no longer have a fixed number of objects. A good example are Oxyd stones - if one is missing because it has been overwritten, the level is unsolvable! But there are 2 approaches: either you remove each position after use from the array, or you examine each position immediately before use to check whether an object is already present. We select here method 1.

Example solution

positions = {{1,6},{4,8},{2,7},{3,6},{4,4},{3,7]}
 
function obstacle()
    local n = table.getn(positions)
 
    r = random(1,n)
    p = positions[r]
 
    set_stone("st-woven",p[1],p[2])
    table.remove(positions,r)
end
 

Analysis

Persönliche Werkzeuge