*** This script creates direct light projections of selected sprites for their sectors. Sector surfaces must have no slope. Facing-oriented, masked or one-sided sprites aren't supported. You might do some trasformations before you can join shadows cast from multiple sprites without glitches.

// Parameter list begins
mov r95 589 // Light direction vector X
mov r96 837 // Light direction vector Y
mov r97 -6786 // Light direction vector Z
mov r98 0 // Ignore sprite offsets
mov r99 0 // 1 - project to ceilings, 0 - project to floors
// Parameter list ends
~~~ Default parameters:
~~~ mov r95 1 // Light direction vector X
~~~ mov r96 1 // Light direction vector Y
~~~ mov r97 -10 // Light direction vector Z
~~~ mov r98 0 // Ignore sprite offsets
~~~ mov r99 0 // 1 - project to ceilings, 0 - project to floors
setselectionmode 6 // Figured sector modeling
setfsmbooleanmode 2 // Union
settool 0 // General
mov r50 1
mov r00 -1
for each selected sprite r00
  deselectsectors // Start new one
  inc r50
  getsprite r00 cstat r12
  and r12 48
  if r12 >= 0 // Sprite is not facing
    getsprite r00 x r01
    getsprite r00 y r02
    getsprite r00 z r03
    getsprite r00 yrepeat r04
    getsprite r00 xrepeat r05
    if r98 == 0
      getsprite r00 yoffset r06
      getsprite r00 xoffset r07
      getsprite r00 cstat r08  and r08 8
      if r08 != 0  neg r06  endif
      getsprite r00 cstat r08  and r08 4
      if r08 != 0  neg r07  endif
      mul r06 r04
      mul r07 r05
    else
      mov r06 0  mov r07
    endif
    getsprite r00 picnum r08
    gettiledim r08 r09 r08
    mul r08 r04
    mul r09 r05
    getsprite r00 cstat r10  and r10 144
    if r10 == 144
      mov r10 r08
      div r10 2
      sub r06 r10
    endif
    getsprite r00 ang r10
    mul r10 32
    sin r11 r10 // Circle Y
    cos r10 r10 // Circle X
    if r12 >= 32 // Horizontal
      mov r13 r10     mov r14 r11
      mul r13 r06     mul r14
      div r13 262144  div r14
      add r01 r13     add r02 r14 // Y offset
      mov r13 r11     mov r14 r10     neg r14
      mul r13 r07     mul r14
      div r13 262144  div r14
      add r01 r13     add r02 r14 // X offset
      
      mov r13 r10     mov r14 r11
      div r13 2       div r14
      mul r13 r08     mul r14
      div r13 262144  div r14
      mov r15 r13     mov r16 r14 // Y side
      mov r13 r11     mov r14 r10     neg r14
      div r13 2       div r14
      mul r13 r09     mul r14
      div r13 262144  div r14
      mov r17 r13     mov r18 r14 // X side
      mov r19 r01   mov r20 r02
      add r19 r15   add r20 r16
      add r19 r17   add r20 r18
      mov r30 r19   mov r31 r20   mov r32 r03 // Spatial coordinates
      mov r19 r01   mov r20 r02
      add r19 r15   add r20 r16
      sub r19 r17   sub r20 r18
      mov r33 r19   mov r34 r20   mov r35 r03 // Spatial coordinates
      mov r19 r01   mov r20 r02
      sub r19 r15   sub r20 r16
      sub r19 r17   sub r20 r18
      mov r36 r19   mov r37 r20   mov r38 r03 // Spatial coordinates
      mov r19 r01   mov r20 r02
      sub r19 r15   sub r20 r16
      add r19 r17   add r20 r18
      mov r39 r19   mov r40 r20   mov r41 r03 // Spatial coordinates
    else // Vertical
    
      mov r13 r11     mov r14 r10     neg r14
      mul r13 r07     mul r14
      div r13 262144  div r14
      sub r01 r13     sub r02 r14 // X offset
      
      mov r13 r11     mov r14 r10     neg r14
      div r13 2       div r14
      mul r13 r09     mul r14
      div r13 262144  div r14 // X side
      
      mov r30 r01   mov r31 r02
      add r30 r13   add r31 r14
      mov r39 r30   mov r40 r31
      
      mov r33 r01   mov r34 r02
      sub r33 r13   sub r34 r14
      mov r36 r33   mov r37 r34
      
      mul r06 -4
      add r03 r06
      mov r32 r03   mov r35   mov r38   mov r41
      mul r08 -4
      add r38 r08   add r41
    endif
    
    getsprite r00 sectnum r01
    if r99 != 0
      getsector r01 ceilingz r02
    else
      getsector r01 floorz r02
    endif
    mov r03 30 // Pointer
    while r03 < 42
      ptr 4 r03     inc r03 // X
      ptr 5 r03     inc r03 // Y
      ptr 6 r03     inc r03 // Z
      sub r06 r02   mov r07 r06 // Z relative to sector
      mul r06 r95   mul r07 r96 // Multiply by XY
      div r06 r97   div r07 // Divide by Z
      add r04 r06   add r05 r07 // Add displacement to XY
      sethitpoint r04 r05
      hitspace
    continue
    gettoolvertx 0 r01
    gettoolverty 0 r02
    sethitpoint r01 r02
    hitspace // Close contour
    settool 0 // Prevent glitches
  endif
continue