*** Select three sprites that will be anchor points for slope of the sector which the selected sprites belong.

// Parameter list begins
mov r95 0 // Heinums only (skip custom slope, no Z-positioning)
mov r96 4096 // Divisor (decreasing makes slope triangle larger)
mov r97 0 // Skip middle point sector search (if there is a hole)
mov r98 0 // Assign slope to floor
mov r99 1 // Assign slope to ceiling
// Parameter list ends
~~~ Default parameters:
~~~ mov r95 0 // Heinums only (skip custom slope, no Z-positioning)
~~~ mov r96 4096 // Divisor (decreasing makes slope triangle larger)
~~~ mov r97 0 // Skip middle point sector search (if there is a hole)
~~~ mov r98 1 // Assign slope to floor
~~~ mov r99 0 // Assign slope to ceiling
mov r01 20 // Pointer to nine coordinates (x1 x2 x3 y1 y2 y3 z1 z2 z3)
mov r03 30 // Pointer to sprite sectnum buffer
mov r00 -1
for each selected sprite r00
  getsprite r00 x r02
  ptr r01 2
  add r01 3
  getsprite r00 y r02
  ptr r01 2
  add r01 3
  getsprite r00 z r02
  ptr r01 2
  sub r01 5
  getsprite r00 sectnum r02
  ptr r03 2
  inc r03
  if r01 == 23  break  endif // If more than 3 sprites are selected
continue
if r01 != 23  end  endif // If less than 3 sprites are selected

mov r10 0 // Plane equation A
mov r11 0 // Plane equation B
mov r12 0 // Plane equation C

mov r01 10 // Pointer to three plane coefficients (A B C)
mov r02 0 // Coordinate pointer
while r01 < 13 // Cross product
  // Expression for A: (y2-y1)*(z3-z1)-(z2-z1)*(y3-y1)
  
  mov r03 r02
  add r03 4 // Offset (y2)
  mod r03 9
  add r03 20
  ptr 4 r03
  mov r03 r02
  add r03 3 // Offset (y1)
  mod r03 9
  add r03 20
  ptr 5 r03
 3  sub r04 r05
  mov r03 r02
  add r03 8 // Offset (z3)
  mod r03 9
  add r03 20
  ptr 5 r03
  mov r03 r02
  add r03 6 // Offset (z1)
  mod r03 9
  add r03 20
  ptr 6 r03
 3  sub r05 r06
 2  mul r04 r05
  mov r03 r02
  add r03 7 // Offset (z2)
  mod r03 9
  add r03 20
  ptr 5 r03
  mov r03 r02
  add r03 6 // Offset (z1)
  mod r03 9
  add r03 20
  ptr 6 r03
 3  sub r05 r06
  mov r03 r02
  add r03 5 // Offset (y3)
  mod r03 9
  add r03 20
  ptr 6 r03
  mov r03 r02
  add r03 3 // Offset (y1)
  mod r03 9
  add r03 20
  ptr 7 r03
 3  sub r06 r07
 2  mul r05 r06
 1  sub r04 r05
  
  div r04 r96 // Scale it down
  ptr r01 4 // Return
  
  inc r01
  add r02 3
continue

mov r00 -1

// Find a sector using the middle point
if r97 == 0
  mov r01 r20  mov r02 r23
  add r01 r21  add r02 r24
  add r01 r22  add r02 r25
  div r01 3    div r02
  
  mov r03 -1
  for each nonignored sector r03
    ispointinsector r01 r02 r03 r04
    if r04 > 0  mov r00 r03  break  endif
  continue
endif

if r00 == -1 // The middle point is in void zone
  // Find a sector by sectnum median
  if r30 <= r31
    if r30 >= r32
      mov r00 r30
    else
      mov r00 r32
    endif
  else
    if r31 >= r32
      mov r00 r32
    else
      mov r00 r31
    endif
  endif
endif
  
// Assigning geometric parameters

hypot r02 r10 r11
mul r02 256
if r12 != 0  div r02 r12  endif
if r98 != 0
  setsector r00 floorheinum r02
  setsector r00 floorz r26
endif
if r99 != 0
  setsector r00 ceilingheinum r02
  setsector r00 ceilingz r26
endif

// Creating slope direction
deselectsectors
selectsector r00 1
if r95 == 0
  settool 20 // Create a custom sector slope
  sethitpoint r20 r23
  hitspace
  neg r11
  add r20 r11
  add r23 r10
  sethitpoint r20 r23 // Perpendicular to gradient
  hitspace
endif
updatesectorslope r00
settool 0 // Failure protection