Facebook
From DK, 3 Years ago, written in Plain Text.
This paste is a reply to Re: Wave Function Collapse Pseudo Code from DK - go back
Embed
MainFunction()
function
    TotalEntropy = Infinite
    while(TotalEntropy > 1)
unitList.Count)
        unitList.GetMinEntropy().Collapse()
        TotalEntropy = unitList.TotalEntropy()
    endwhile
endfunction


GetMinEntropy()
function
    minEntropy = Infinite
    for(unit in unitList)
        if(unit.Entropy < minEntropy)
            minEntropy = unit.Entropy
        endif
    endfor
    candidateList = new List
    for(unit in unitList)
        if(unit.Entropy = minEntropy)
            candidateList.Add(unit)
        endif
    endfor
    return candidateList[Random.Range(candidateList.Count)]
endfunction

Collapse()
function
    index = Random.Range(Possibilies.Count)
    for(possibility in Possibilies)
        if(possibility.index Index != index)
            possibility.Remove()
        endif
    endfor
    Entropy = 1
    for(neighbor in neighbors)
        neighbor.UpdatePossibility()
    endfor
endfunction

UpdatePossibility()
function
    changed = false
    for(possibility in Possibilies)
        for(neighbor in neighbors)
            for(npossibility in neighbor.Possibilities)
                if(not npossibility.Compatible(possibility))
                    possibility.Remove()
                    changed = true
                endif
            endfor
        endfor
    endfor
    if(changed)
        UpdateEntropy()
        for(neighbor in neighbors)
            neighbor.UpdatePossibility()
        endfor
    endif
endfunction

UpdateEntropy()
function
    count = 0
    for(possibility in possibilities)
        if(!possibility.Removed)
            count = count + 1
        endif
    endfor
    Entropy = count
endfunction

TotalEntropy()
function
    count = 0
    for(unit in unitList)
        count = count + unit.Entropy
    endfor
endfunction