   id: 'terrain-randomize-modal'
   template: template
   thangs = []
-
   events:
     'click .choose-option': 'onRandomize'
 

    for i in _.range(0, presetSize.x, thangSizes.floorSize.x)
      for j in _.range(0, presetSize.y, thangSizes.floorSize.y)
        @thangs.push {
          'id': @getRandomThang(preset.floors)
          'pos': {
            'x': i + thangSizes.floorSize.x/2
            'y': j + thangSizes.floorSize.y/2
          }
        }

  randomizeBorder: (preset, presetSize, noiseFactor=1) ->
    for i in _.range(0, presetSize.x, thangSizes.borderSize.x)
      for j in _.range(thangSizes.borderSize.thickness)
        while not @addThang {
          'id': @getRandomThang(preset.borders)
          'pos': {
            'x': i + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.x/2, thangSizes.borderSize.x/2)
            'y': 0 + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.y/2, thangSizes.borderSize.y)
          }
        }
          continue
        while not @addThang {
          'id': @getRandomThang(preset.borders)
          'pos': {
            'x': i + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.x/2, thangSizes.borderSize.x/2)
            'y': presetSize.y - preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.y, thangSizes.borderSize.y/2)
          }
        }
          continue

    for i in _.range(0, presetSize.y, thangSizes.borderSize.y)
      for j in _.range(3)
        while not @addThang {
          'id': @getRandomThang(preset.borders)
          'pos': {
            'x': 0 + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.x/2, thangSizes.borderSize.x)
            'y': i + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.y/2, thangSizes.borderSize.y/2)
          }
        }
          continue
        while not @addThang {
          'id': @getRandomThang(preset.borders)
          'pos': {
            'x': presetSize.x - preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.x, thangSizes.borderSize.x/2)
            'y': i + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.y/2, thangSizes.borderSize.y/2)
          }
        }
          continue


    if presetSize is presetSizes['small'] then sizeFactor = 1 else sizeFactor = 2
    for name, decoration of preset.decorations
      for num in _.range(sizeFactor * _.random(decoration.num[0], decoration.num[1]))
        rect = {
          'x':_.random(decoration.width/2 + preset.borderSize/2 + thangSizes.borderSize.x, presetSize.x - decoration.width/2 - preset.borderSize/2 - thangSizes.borderSize.x),
          'y':_.random(decoration.height/2 + preset.borderSize/2 + thangSizes.borderSize.y, presetSize.y - decoration.height/2 - preset.borderSize/2 - thangSizes.borderSize.y)
          'width':decoration.width
          'height':decoration.height
        }
        for cluster, range of decoration.clusters
          for i in _.range(_.random(range[0], range[1]))
            while not @addThang {
              'id':@getRandomThang(clusters[cluster].thangs)
              'pos':{
                'x':_.random(rect.x - rect.width/2, rect.x + rect.width/2)
                'y':_.random(rect.y - rect.height/2, rect.y + rect.height/2)
              }
              'margin':clusters[cluster].margin
            }
              continue

  addThang: (thang) ->
    for existingThang in @thangs
      if existingThang.margin is -1 or thang.margin is -1
        continue
      if Math.abs(existingThang.pos.x - thang.pos.x) <= thang.margin + existingThang.margin and Math.abs(existingThang.pos.y - thang.pos.y) <= thang.margin + existingThang.margin
        return false 
    @thangs.push thang
    true

