*** Select requisite walls to create section-based object using prepared contour.

// Parameter list begins
mov r96 0 // Section X-offset
mov r97 0 // Compensate shape X-offset (align to center)
mov r98 0 // Rotate cap sections
mov r99 1 // Base to path floor
// Parameter list ends
~~~ Default parameters:
~~~ mov r96 0 // Section X-offset
~~~ mov r97 0 // Compensate shape X-offset
~~~ mov r98 0 // Rotate cap sections
~~~ mov r99 1 // Base to path floor
if r97 != 0
  getcontourq r10
  // Get factual section X-bounds
  mov r11 16777216 // Left bound
  mov r12 -16777216 // Right bound
  mov r00 0
  while r00 < r10
    getcontourl r00 r01 r02 r02
    if r01 > r12  mov r12 r01  endif
    if r01 < r11  mov r11 r01  endif
    getcontourr r00 r01 r02 r02
    if r01 > r12  mov r12 r01  endif
    if r01 < r11  mov r11 r01  endif
    inc r00
  continue
  mov r00 r11  add r00 r12  div r00 2
  sub r96 r00
endif
// Generate sectors
mov r01 -1
for each selected wall r01
  getprevwall r01 r00
  getwall r01 point2 r02
  getwall r02 point2 r03
  getwall r00 x r20  getwall r00 y r21
  getwall r01 x r22  getwall r01 y r23
  getwall r02 x r24  getwall r02 y r25
  getwall r03 x r26  getwall r03 y r27
  sub r26 r24  sub r27 r25 // Next wall vector
  sub r24 r22  sub r25 r23 // Current wall vector
  sub r22 r20  sub r23 r21 // Previous wall vector

  hypot r33 r22 r23 // Previous wall length
  hypot r34 r24 r25 // Current wall length
  hypot r35 r26 r27 // Next wall length
  mul r22 16384  mul r23  mul r24  mul r25  mul r26  mul r27
  div r22 r33  div r23 r33 // Normalize vectors
  div r24 r34  div r25 r34
  div r26 r35  div r27 r35
  
  mov r04 22  mov r05 42 // Pointers
  while r04 < 28  ptr r05 r04  inc r04  inc r05  continue
  
  if r98 != 0  mov r04 1  else  getselwall r00 r04  endif
  if r04 == 0 // Assume right bisector perpendicular equal to current wall
    mov r46 r24
    mov r47 r25
  else
    add r46 r44  add r47 r45 // Right bisector perpendicular
    hypot r37 r46 r47 // Right bisector length
    mul r46 16384  mul r47
    div r46 r37  div r47 r37
  endif
  
  if r98 != 0  mov r04 1  else  getselwall r00 r04  endif
  if r04 == 0 // Assume left bisector perpendicular equal to current wall
    mov r44 r24
    mov r45 r25
  else
    add r44 r42  add r45 r43 // Left bisector perpendicular
    hypot r36 r44 r45 // Left bisector length
    mul r44 16384  mul r45
    div r44 r36  div r45 r36 // Normalize vectors
  endif

  mov r04 r44  mov r05 r45
  mul r04 r22  mul r05 r23
  add r05 r04  div r05 16384 // Dot product
  mov r38 16777216  div r38 r05 // Stretching amount (left)
  
  mov r04 r46  mov r05 r47
  mul r04 r24  mul r05 r25
  add r05 r04  div r05 16384 // Dot product
  mov r39 16777216  div r39 r05 // Stretching amount (right)
  
  mul r44 r38  mul r45
  div r44 16384  div r45
  mul r46 r39  mul r47
  div r46 16384  div r47
  
  getcontourq r10
  getwallsectnum r01 r11
  mov r05 0
  while r05 < r10
    getsectornumber r04
    setsector r04 = r11
    getwallnumber r06
    setsector r04 wallptr r06
    setsector r04 wallnum 4
    inc r04
    setsectornumber r04
    
    setwall r06 = r01  setwall r06 nextwall -1  setwall r06 nextsector -1
    mov r07 r06  inc r07
    setwall r06 point2 r07
    getwall r02 x r07  getwall r02 y r08
    getcontourl r05 r12 r13 r14
    add r12 r96
    mov r15 r47  mov r16 r46
    mul r15 r12  mul r16 r12
    div r15 1024  div r16
    add r07 r15  sub r08 r16
    setwall r06 x r07
    setwall r06 y r08
    selectwall r06 0
    inc r06
    
    setwall r06 = r01  setwall r06 nextwall -1  setwall r06 nextsector -1
    mov r07 r06  inc r07
    setwall r06 point2 r07
    getwall r01 x r07  getwall r01 y r08
    mov r15 r45  mov r16 r44
    mul r15 r12  mul r16 r12
    div r15 1024  div r16
    add r07 r15  sub r08 r16
    setwall r06 x r07
    setwall r06 y r08
    selectwall r06 0
    inc r06
    
    setwall r06 = r01  setwall r06 nextwall -1  setwall r06 nextsector -1
    mov r07 r06  inc r07
    setwall r06 point2 r07
    getwall r01 x r07  getwall r01 y r08
    getcontourr r05 r17 r18 r19
    add r17 r96
    mov r15 r45  mov r16 r44
    mul r15 r17  mul r16 r17
    div r15 1024  div r16
    add r07 r15  sub r08 r16
    setwall r06 x r07
    setwall r06 y r08
    selectwall r06 0
    inc r06
    
    setwall r06 = r01  setwall r06 nextwall -1  setwall r06 nextsector -1
    mov r07 r06  sub r07 3
    setwall r06 point2 r07
    getwall r02 x r07  getwall r02 y r08
    mov r15 r47  mov r16 r46
    mul r15 r17  mul r16 r17
    div r15 1024  div r16
    add r07 r15  sub r08 r16
    setwall r06 x r07
    setwall r06 y r08
    selectwall r06 0
    inc r06
    
    setwallnumber r06
    dec r04
    if r99 != 0  getsector r04 floorz r07  else  mov r07 0  endif
    add r13 r07  add r14 r07
    setsector r04 floorz r13
    setsector r04 ceilingz r14
    setsector r04 hitag r05
    sub r13 r07  sub r14 r07
    sub r17 r12 // Sector width
    sub r18 r13 // Floor
    sub r19 r14 // Ceiling
    mul r18 256  div r18 r17
    mul r19 256  div r19 r17
    setsector r04 floorheinum r18
    setsector r04 ceilingheinum r19
    updatesectorslope r04
    selectsector r04 0
    
    dec r06  checkwallpointer r06
    dec r06  checkwallpointer r06
    dec r06  checkwallpointer r06
    dec r06  checkwallpointer r06
    inc r05
  continue
continue