Facebook
From DK, 3 Years ago, written in Plain Text.
This paste is a reply to Re: Wave Function Collapse Pseudo Code from DK - view diff
Embed
Download Paste or View Raw
Hits: 844
  1. MainFunction()
  2. function
  3.     TotalEntropy = Infinite
  4.     while(TotalEntropy > unitList.Count)
  5.         unitList.GetMinEntropy().Collapse()
  6.         TotalEntropy = unitList.TotalEntropy()
  7.     endwhile
  8. endfunction
  9.  
  10.  
  11. GetMinEntropy()
  12. function
  13.     minEntropy = Infinite
  14.     for(unit in unitList)
  15.         if(unit.Entropy < minEntropy)
  16.             minEntropy = unit.Entropy
  17.         endif
  18.     endfor
  19.     candidateList = new List
  20.     for(unit in unitList)
  21.         if(unit.Entropy = minEntropy)
  22.             candidateList.Add(unit)
  23.         endif
  24.     endfor
  25.     return candidateList[Random.Range(candidateList.Count)]
  26. endfunction
  27.  
  28. Collapse()
  29. function
  30.     index = Random.Range(Possibilies.Count)
  31.     for(possibility in Possibilies)
  32.         if(possibility.Index != index)
  33.             possibility.Remove()
  34.         endif
  35.     endfor
  36.     Entropy = 1
  37.     for(neighbor in neighbors)
  38.         neighbor.UpdatePossibility()
  39.     endfor
  40. endfunction
  41.  
  42. UpdatePossibility()
  43. function
  44.     changed = false
  45.     for(possibility in Possibilies)
  46.         for(neighbor in neighbors)
  47.             for(npossibility in neighbor.Possibilities)
  48.                 if(not npossibility.Compatible(possibility))
  49.                     possibility.Remove()
  50.                     changed = true
  51.                 endif
  52.             endfor
  53.         endfor
  54.     endfor
  55.     if(changed)
  56.         UpdateEntropy()
  57.         for(neighbor in neighbors)
  58.             neighbor.UpdatePossibility()
  59.         endfor
  60.     endif
  61. endfunction
  62.  
  63. UpdateEntropy()
  64. function
  65.     count = 0
  66.     for(possibility in possibilities)
  67.         if(!possibility.Removed)
  68.             count = count + 1
  69.         endif
  70.     endfor
  71.     Entropy = count
  72. endfunction
  73.  
  74. TotalEntropy()
  75. function
  76.     count = 0
  77.     for(unit in unitList)
  78.         count = count + unit.Entropy
  79.     endfor
  80. endfunction