*** Selected sectors must consist of one outer and one inner loop.

mov r00 -1
for each selected sector r00
  getsector r00 wallptr r01
  getsector r00 wallnum r02
  add r02 r01 // Sector end
  mov r03 r01
  while r03 < r02 // Get second loop bound
    getwall r03 point2 r04
    inc r03
    if r03 > r04  break  endif
  continue
  
  mov r12 0 // Split count
  mov r04 r01
  while r04 < r03 // Go first loop
    mov r05 r03
    mov r10 16777216 // MinDist
    mov r13 -1  mov r14
    while r05 < r02 // Go second loop
      getwall r04 x r06  getwall r04 y r07
      getwall r05 x r08  getwall r05 y r09
      sub r06 r08  sub r07 r09
      hypot r08 r06 r07
      if r08 < r10
        mov r10 r08  mov r13 r04  mov r14 r05
      endif
      inc r05
    continue
    if r13 > -1
      // pntb2 is used by splitting algorithm
      getwall r13 x r06  getwall r13 y r07
      getwall r14 x r08  getwall r14 y r09
      setpntb1x r12 r06  setpntb1y r12 r07  inc r12
      setpntb1x r12 r08  setpntb1y r12 r09  inc r12
    endif
    inc r04
  continue
  mov r04 r03
  while r04 < r02 // Go second loop
    mov r05 r01
    mov r10 16777216 // MinDist
    mov r13 -1  mov r14
    while r05 < r03 // Go first loop
      getwall r04 x r06  getwall r04 y r07
      getwall r05 x r08  getwall r05 y r09
      sub r06 r08  sub r07 r09
      hypot r08 r06 r07
      if r08 < r10
        mov r10 r08  mov r13 r04  mov r14 r05
      endif
      inc r05
    continue
    if r13 > -1
      // pntb2 is used by splitting algorithm
      getwall r13 x r06  getwall r13 y r07
      getwall r14 x r08  getwall r14 y r09
      setpntb1x r12 r06  setpntb1y r12 r07  inc r12
      setpntb1x r12 r08  setpntb1y r12 r09  inc r12
    endif
    inc r04
  continue
  mov r04 0
  while r04 < r12
    getpntb1x r04 r06  getpntb1y r04 r07  inc r04
    getpntb1x r04 r08  getpntb1y r04 r09  inc r04
    sethitpoint r06 r07
    hitspace
    sethitpoint r08 r09
    hitspace
    settool 0
  continue
continue