Advertisement
Guest User

Untitled

a guest
Mar 8th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. canCarveAt: aPoint
  2.     ^ ((board at: aPoint) = #wall)
  3.         and: [ ((board at: aPoint + (0 @ 1)) = #wall)
  4.             and: [ ((board at: aPoint + (0 @ -1)) = #wall)
  5.                 and: [ ((board at: aPoint + (1 @ 0)) = #wall)
  6.                     and: [ ((board at: aPoint + (-1 @ 0)) = #wall) ] ] ] ]
  7.  
  8. canCarveAt: aPoint towards: aDirection
  9.     ^ ((board at: aPoint + aDirection ifAbsent: [ #none ]) = #wall)
  10.         and: [ (board at: aPoint + (aDirection * 2) ifAbsent: [ #none ]) = #wall ]
  11.  
  12. carve: aNumber towards: aDirection at: aPoint
  13.     | newPosition |
  14.    
  15.     newPosition := aPoint.
  16.    
  17.     aNumber timesRepeat: [
  18.         newPosition := newPosition + aDirection.
  19.         board at: newPosition put: #floor ].
  20.    
  21.     ^ newPosition
  22.  
  23. carveFromStack: aStack
  24.     | top position dir |
  25.            
  26.     top := aStack last.
  27.     position := top key.
  28.     dir := top value remove: top value atRandom.
  29.  
  30.     top value ifEmpty: [ aStack removeLast ].
  31.     ((board at: position + dir ifAbsent: [ #none ]) = #wall)
  32.         and: [ self canCarveAt: position towards: dir ]
  33.             ifTrue: [
  34.                 | newPosition newDirs |
  35.                
  36.                 newPosition := self carve: 2 towards: dir at: position.
  37.                 newDirs := self directions remove: (dir * -1); yourself.
  38.                 aStack add: newPosition -> newDirs ]
  39.  
  40. generatePassageAt: aPoint
  41.     | stack |
  42.    
  43.     stack := Array with: aPoint -> self directions.
  44.     board at: aPoint put: #floor.
  45.  
  46.     [ stack notEmpty ] whileTrue: [ self carveFromStack: stack ]
  47.  
  48. generatePassages
  49.     (self container origin y + 1 to: self container corner y by: 2)
  50.         do: [ : y |
  51.             (self container origin x + 1 to: self container corner x by: 2)
  52.                 do: [ :x |
  53.                     (self canCarveAt: x@y)
  54.                         ifTrue: [ self generatePassageAt: x@y ] ] ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement