*** This script aligns sector slopes and z's to connect selected walls to opposite ones.

// Parameter list begins
mov r98 1 // Align floors
mov r99 1 // Align ceilings
// Parameter list ends
~~~ Default parameters:
~~~ mov r98 1 // Align floors
~~~ mov r99 1 // Align ceilings
mov r00 -1
for each selected wall r00
  getwall r00 nextwall r01
  if r01 != -1
    getwall r00 nextsector r02
    getwall r00 x r04 // Get current wall coordinates
    getwall r00 y r05
    getwall r01 x r06
    getwall r01 y r07
    gettruefloorz r04 r05 r02 r10 // Get surface z's
    gettrueceilingz r04 r05 r02 r11
    gettruefloorz r06 r07 r02 r12
    gettrueceilingz r06 r07 r02 r13
    getwallsectnum r00 r03
    getsector r03 wallptr r03 // Get first wall coordinates
    getwall r03 x r14
    getwall r03 y r15
    getwall r03 point2 r03
    getwall r03 x r16
    getwall r03 y r17
    sub r16 r14  sub r17 r15 // Get FW normalized vector
    hypot r08 r16 r17
    mul r16 16384  mul r17
    div r16 r08  div r17  neg r16
    // Get FW offsets
    mul r14 r17  mul r15 r16
    add r14 r15  div r14 16384 // Base offset
    mul r04 r17  mul r05 r16
    add r04 r05  div r04 16384 // Left vertex offset
    mul r06 r17  mul r07 r16
    add r06 r07  div r06 16384 // Right vertex offset
    sub r06 r04  sub r04 r14 // Relative offsets
    if r06 != 0
      // Calculate new Z coordinates and heinums
      sub r12 r10 // floorz delta
      sub r13 r11 // ceilingz delta
      mul r12 -256  mul r13
      div r12 r06  div r13 // fwoffset delta
      mov r14 r04  mov r15 r04
      mul r14 r12  mul r15 r13
      div r14 256  div r15
      add r14 r10  add r15 r11
      getwallsectnum r00 r03 // Assign parameters
      if r98 != 0
        setsector r03 floorheinum r12
        setsector r03 floorz r14
      endif
      if r99 != 0
        setsector r03 ceilingheinum r13
        setsector r03 ceilingz r15
      endif
      updatesectorslope r03
    else  if r04 != 0
      // Calculate heinums only
      getwallsectnum r00 r03 // Assign parameters
      getsector r03 floorz r12 // Get first wall coordinates
      getsector r03 ceilingz r13 // Get first wall coordinates
      sub r12 r10 // floorz delta
      sub r13 r11 // ceilingz delta
      mul r12 256  mul r13
      div r12 r04  div r13 // fwoffset delta
      if r98 != 0
        setsector r03 floorheinum r12
      endif
      if r99 != 0
        setsector r03 ceilingheinum r13
      endif
      updatesectorslope r03
    endif  endif
  endif
continue