; SPDX-License-Identifier: GPL-3.0-or-later
; DOOM.EXE patch for PC-98 Doom (Imagineer, Infinity's DX386 port of Doom v1.2):
; the Doom v1.666/v1.9 map features SIGIL and other later PWADs use.
;  - linedef types 99-141 (Doom II: blazing doors, lifts and floors, turbo stairs,
;    switch-operated key doors, W1 secret exit, monster-only teleporters, silent crusher),
;    on the v1.2 door/floor/lift code (blazing = normal speed);
;  - sector type 17, fire flicker (v1.2 left it set, and standing in such a sector
;    stopped the game with "P_PlayerInSpecialSector: unknown special 17");
;  - an unknown sector special no longer stops the game;
;  - 256 drawsegs as in Doom v1.9 (v1.2 has 128: in views with more wall pieces, such as
;    SIGIL's E5M9 cages, the rest of the view was never drawn and showed old frames);
;  - on E3M8, boss deaths end the level only on Doom's own map (Dis, 17 sectors), not on
;    SIGIL's last map in that slot (303 sectors), which ends at its exit (SIGIL_COMPAT does
;    this with a DEHACKED patch; v1.2 ends E2M8/E3M8 when every Cyberdemon or Spider
;    Mastermind is dead).
; Flat addresses (file offset = address + 61A0h). Assembled with NASM -f bin; every
; section is written to DOOM.EXE at its vstart + 61A0h (the IPS holds the result). The new code lives in text the game
; never needs in full: the "this game has been modified" banner (shortened), the
; shareware order message, and the piracy banner (shortened to its first line).
[map all sigil98.map]
bits 32

; ---- v1.2 engine (Watcom register calls: eax, edx, ebx; parameter registers are not preserved)
EV_DoDoor           equ 0x09A68   ; line, type (0 raise, 1 close 30 s then open, 2 close, 3 open)
EV_VerticalDoor     equ 0x09BA0   ; line, thing
EV_DoCeiling        equ 0x096BC   ; line, type (3 crush and raise)
EV_DoFloor          equ 0x0AD50   ; line, type (4 raise to next higher floor)
EV_BuildStairs      equ 0x0B0A0   ; line (8-unit steps at 1/4 speed)
EV_DoPlat           equ 0x0E2E4   ; line, type (1 down-wait-up), amount
EV_LightTurnOn      equ 0x0C3B4   ; line, level
EV_Teleport         equ 0x1250C   ; line, side, thing
G_SecretExitLevel   equ 0x05310
G_ExitLevel         equ 0x052F8
GAMEEPISODE         equ 0x4AA14
NUMSECTORS          equ 0x4BDA8
P_ChangeSwitchTexture equ 0x11000 ; line, useAgain
S_StartSound        equ 0x1CD30   ; origin, sound
Z_Malloc            equ 0x1D070   ; size, tag, user
P_AddThinker        equ 0x13074   ; thinker
P_FindMinSurroundingLight equ 0x102E8 ; sector, max
P_Random            equ 0x078CC
CROSS_RET           equ 0x108E7   ; P_CrossSpecialLine: pop esi, pop ecx, ret
USE_TREE            equ 0x111D3   ; P_UseSpecialLine: its compare tree after cmp ax,31h
USE_TRUE            equ 0x116D1   ; P_UseSpecialLine: return true
SECT_NEXT           equ 0x10E84   ; P_SpawnSpecials: next sector
PIS_RET             equ 0x10AA3   ; P_PlayerInSpecialSector: return
LANGUAGE            equ 0x2D77D   ; message language (byte: 0/1)
KEYMSG              equ 0x46510   ; key messages, [colour * 2 + language] (blue, yellow, red)
SFX_OOF             equ 0x1B
PU_LEVSPEC          equ 0x33
PU_STATIC           equ 1
MAXDRAWSEGS         equ 256       ; v1.2 has 128 (drawsegs at 4CC6Ch), Doom v1.9 256
; EV_BuildStairs constants rewritten for 16-unit steps at 4x speed
STAIR_FIRST         equ 0x0B0EB   ; add eax, 80000h: first step height
STAIR_SPEED1        equ 0x0B11F   ; speed of the first step
STAIR_SPEED2        equ 0x0B1E0   ; speed of the next steps
STAIR_NEXT          equ 0x0B19D   ; add word [ebp-0Ah], 8: next step height

; line_t: special +12h, player of a mobj +84h; sector_t: lightlevel +0Ch, special +0Eh
K_DOOR      equ 0
K_FLOOR     equ 1
K_PLAT      equ 2
K_CEIL      equ 3
K_STAIRS    equ 4
K_SECRET    equ 5
K_LIGHT     equ 6
K_TELE      equ 7       ; monster-only teleporter (walk)
K_VDOOR     equ 8       ; manual door: EV_VerticalDoor as type 1 (raise) or 31 (open)
K_LOCKED    equ 9       ; switch-operated key door, argument = key colour (0 blue, 1 yellow, 2 red)
ONCE        equ 1       ; W1/S1: the line's special is used up

; ======================================================================= text
section banner1 start=0x0000 vstart=0x28ABC
    db "DOOM has been modified. Press Enter.", 10, 0
section banner2 start=0x0080 vstart=0x28C7C
    db 0                            ; "This version is NOT SHAREWARE ..." printed nothing new

; ======================================================================= cave A
section caveA start=0x0100 vstart=0x28AE2       ; up to 28C5Bh (the old banner's NUL)

; P_CrossSpecialLine, special above 104 or 100 (players and the specials monsters may use).
; ecx = line, ebx = thing, edx = side; esi is free (P_CrossSpecialLine saved it).
xcross:
    movzx eax, word [ecx+0x12]
    mov esi, crosstab
    call find
    jc .ret
    cmp byte [esi+1], K_TELE
    je .ret                         ; monster-only teleporters ignore players
    movzx edx, byte [esi+2]
    mov eax, ecx
    call do_kind
    test byte [esi+3], ONCE
    jz .ret
    mov word [ecx+0x12], 0
.ret:
    jmp CROSS_RET

; P_CrossSpecialLine, a monster on a line v1.2 lets only players use: monster-only teleporters.
xmon:
    movzx eax, word [ecx+0x12]
    mov esi, crosstab
    call find
    jc .ret
    cmp byte [esi+1], K_TELE
    jne .ret
    mov eax, ecx                    ; line; edx = side, ebx = thing
    call EV_Teleport
    test byte [esi+3], ONCE
    jz .ret
    mov word [ecx+0x12], 0
.ret:
    jmp CROSS_RET

; eax = line, edx = argument, esi = entry -> eax = result of the EV_ function
do_kind:
    movzx ebx, byte [esi+1]
    jmp [kinds+ebx*4]
kinds:
    dd EV_DoDoor, EV_DoFloor, k_plat, EV_DoCeiling, k_stairs, G_SecretExitLevel, EV_LightTurnOn
k_plat:
    xor ebx, ebx                    ; amount
    jmp EV_DoPlat

; turbo stairs: EV_BuildStairs with 16-unit steps at FLOORSPEED * 4, then its own constants back
k_stairs:
    mov dword [STAIR_FIRST], 0x100000
    mov dword [STAIR_SPEED1], 0x40000
    mov dword [STAIR_SPEED2], 0x40000
    mov byte [STAIR_NEXT], 16
    call EV_BuildStairs
    mov dword [STAIR_FIRST], 0x80000
    mov dword [STAIR_SPEED1], 0x4000
    mov dword [STAIR_SPEED2], 0x4000
    mov byte [STAIR_NEXT], 8
    ret

crosstab:                           ; special, kind, argument, flags
    db 100, K_STAIRS, 0, ONCE       ; W1 build stairs 16 (turbo)
    db 105, K_DOOR, 0, 0            ; WR blazing door raise
    db 106, K_DOOR, 3, 0            ; WR blazing door open
    db 107, K_DOOR, 2, 0            ; WR blazing door close
    db 108, K_DOOR, 0, ONCE         ; W1 blazing door raise
    db 109, K_DOOR, 3, ONCE         ; W1 blazing door open
    db 110, K_DOOR, 2, ONCE         ; W1 blazing door close
    db 119, K_FLOOR, 4, ONCE        ; W1 floor raise to next higher
    db 120, K_PLAT, 1, 0            ; WR blazing lift
    db 121, K_PLAT, 1, ONCE         ; W1 blazing lift
    db 124, K_SECRET, 0, 0          ; W1 secret exit
    db 125, K_TELE, 0, ONCE         ; W1 teleport, monsters only
    db 126, K_TELE, 0, 0            ; WR teleport, monsters only
    db 128, K_FLOOR, 4, 0           ; WR floor raise to next higher
    db 129, K_FLOOR, 4, 0           ; WR floor raise to next higher (turbo)
    db 130, K_FLOOR, 4, ONCE        ; W1 floor raise to next higher (turbo)
    db 141, K_CEIL, 3, ONCE         ; W1 silent crusher
    db 0
usetab:                             ; special, kind, argument, flags
    db 99, K_LOCKED, 0, 0           ; SR blue key door open
    db 111, K_DOOR, 0, ONCE         ; S1 blazing door raise
    db 112, K_DOOR, 3, ONCE         ; S1 blazing door open
    db 113, K_DOOR, 2, ONCE         ; S1 blazing door close
    db 114, K_DOOR, 0, 0            ; SR blazing door raise
    db 115, K_DOOR, 3, 0            ; SR blazing door open
    db 116, K_DOOR, 2, 0            ; SR blazing door close
    db 117, K_VDOOR, 1, 0           ; DR blazing door
    db 118, K_VDOOR, 31, 0          ; D1 blazing door open
    db 122, K_PLAT, 1, ONCE         ; S1 blazing lift
    db 123, K_PLAT, 1, 0            ; SR blazing lift
    db 127, K_STAIRS, 0, ONCE       ; S1 build stairs 16 (turbo)
    db 131, K_FLOOR, 4, ONCE        ; S1 floor raise to next higher (turbo)
    db 132, K_FLOOR, 4, 0           ; SR floor raise to next higher (turbo)
    db 133, K_LOCKED, 0, ONCE       ; S1 blue key door open
    db 134, K_LOCKED, 2, 0          ; SR red key door open
    db 135, K_LOCKED, 2, ONCE       ; S1 red key door open
    db 136, K_LOCKED, 1, 0          ; SR yellow key door open
    db 137, K_LOCKED, 1, ONCE       ; S1 yellow key door open
    db 138, K_LIGHT, 255, 0         ; SR lights to 255
    db 139, K_LIGHT, 35, 0          ; SR lights to 35
    db 0
caveA_end:

; ======================================================================= cave C
section caveC start=0x0300 vstart=0x28C7D       ; up to 28D95h

; P_UseSpecialLine, entered in place of its first compare: ebx = thing, ecx = line.
xuse:
    push esi
    movzx eax, word [ecx+0x12]
    mov esi, usetab
    call find
    jnc .ours
    pop esi
    mov ax, [ecx+0x12]
    cmp ax, 0x31
    jmp USE_TREE
.ours:
    mov al, [esi+1]
    cmp al, K_VDOOR
    je .vdoor
    cmp al, K_LOCKED
    je .locked
    movzx edx, byte [esi+2]
    mov eax, ecx
    push ebx
    call do_kind
    pop ebx
.switch:                            ; eax = result: change the switch texture if something moved
    test eax, eax
    jz .done
    mov eax, ecx
    xor edx, edx                    ; S1: useAgain 0
    test byte [esi+3], ONCE
    jnz .sw
    inc edx                         ; SR
.sw:
    call P_ChangeSwitchTexture
.done:
    pop esi
    jmp USE_TRUE
.vdoor:                             ; DR/D1 blazing doors: the v1.2 manual door as type 1 or 31
    movzx eax, byte [esi+2]
    mov [ecx+0x12], ax
    mov eax, ecx
    mov edx, ebx
    call EV_VerticalDoor
    cmp word [ecx+0x12], 0          ; type 31 used the line up
    je .done
    movzx eax, byte [esi]
    mov [ecx+0x12], ax
    jmp .done
.locked:
    mov eax, [ebx+0x84]             ; player
    test eax, eax
    jz .done
    movzx edx, byte [esi+2]         ; key colour
    cmp dword [eax+edx*4+0x44], 0   ; keycard
    jne .key
    cmp dword [eax+edx*4+0x50], 0   ; skull key
    jne .key
    add edx, edx
    movsx ebx, byte [LANGUAGE]
    add edx, ebx
    mov edx, [KEYMSG+edx*4]
    mov [eax+0xD4], edx             ; player->message
    mov edx, SFX_OOF
    xor eax, eax
    call S_StartSound
    jmp .done
.key:
    mov eax, ecx
    mov edx, 3                      ; open
    call EV_DoDoor
    jmp .switch

; eax = special, esi = table -> esi = its entry, carry clear; carry set if not listed
find:
    test eax, 0xFFFFFF00
    jnz .no
.next:
    cmp byte [esi], 0
    je .no
    cmp al, [esi]
    je .yes                         ; equal: carry clear
    add esi, 4
    jmp .next
.no:
    stc
.yes:
    ret
; R_Init's call to the empty R_InitPlanes: drawsegs[MAXDRAWSEGS] in the zone, its address written
; into the code that uses the v1.2 array (ds_p = drawsegs, the sprite and masked loops, the range
; check, and R_StoreWallRange's end test)
init_drawsegs:
    push ebx
    push edx
    mov eax, MAXDRAWSEGS * 0x30
    xor edx, edx
    inc edx                         ; PU_STATIC
    xor ebx, ebx
    call Z_Malloc
    mov [0x1361A], eax              ; R_ClearDrawSegs
    mov [0x15B99], eax              ; R_DrawPlanes range check
    mov [0x17993], eax              ; R_DrawSprite, R_DrawMasked
    mov [0x17B01], eax
    mov [0x17BBF], eax
    mov [0x17BDD], eax
    add eax, MAXDRAWSEGS * 0x30
    mov [0x163B6], eax              ; R_StoreWallRange: if (ds_p == &drawsegs[MAXDRAWSEGS]) return
    pop edx
    pop ebx
    ret
caveC_end:

; ======================================================================= cave B
section caveB start=0x0500 vstart=0x2BCBC       ; up to 2BD6Ch

; P_SpawnSpecials sector switch, special above 14: ax = special - 1, ecx = sector
xsect:
    cmp ax, 16                      ; 17: fire flicker
    jne .next
    mov eax, ecx
    push ebx
    push edx
    mov word [ecx+0x0E], 0          ; nothing special during the game
    mov eax, 0x1C
    mov edx, PU_LEVSPEC
    xor ebx, ebx
    call Z_Malloc
    mov ebx, eax
    call P_AddThinker
    mov dword [ebx+0x08], t_fire
    mov [ebx+0x0C], ecx             ; sector
    movsx edx, word [ecx+0x0C]
    mov [ebx+0x14], edx             ; maxlight
    mov eax, ecx
    call P_FindMinSurroundingLight
    add eax, 16
    mov [ebx+0x18], eax             ; minlight
    mov dword [ebx+0x10], 4         ; count
    pop edx
    pop ebx
.next:
    jmp SECT_NEXT

; T_FireFlicker: eax = thinker
t_fire:
    dec dword [eax+0x10]
    jnz .ret
    push ebx
    push ecx
    push edx
    mov ebx, eax
    call P_Random
    and eax, 3
    shl eax, 4                      ; amount
    mov ecx, [ebx+0x0C]
    movsx edx, word [ecx+0x0C]
    sub edx, eax
    cmp edx, [ebx+0x18]
    jge .bright
    mov edx, [ebx+0x18]             ; minlight
    jmp .set
.bright:
    mov edx, [ebx+0x14]
    sub edx, eax                    ; maxlight - amount
.set:
    mov [ecx+0x0C], dx
    mov dword [ebx+0x10], 4
    pop edx
    pop ecx
    pop ebx
.ret:
    ret

; A_BossDeath, all bosses of the type dead: end the level unless this is E3M8 with more than 127
; sectors (a PWAD's map 8 in episode 3, such as SIGIL's)
boss_exit:
    cmp dword [GAMEEPISODE], 3
    jne near G_ExitLevel
    cmp dword [NUMSECTORS], 127
    jbe near G_ExitLevel
    ret
caveB_end:

; ======================================================================= hooks
section hcross start=0x0700 vstart=0x1051F      ; cmp ax,66h / ja default
    ja near xcross
section htab100 start=0x0710 vstart=0x104B0     ; jump table entry for special 100
    dd xcross
section hmon start=0x0720 vstart=0x1050D        ; monster not allowed: je return
    je near xmon
section huse start=0x0730 vstart=0x111CB        ; mov ax,[ecx+12h] / cmp ax,31h
    jmp near xuse
    nop
    nop
    nop
section hsect start=0x0740 vstart=0x10DE5       ; cmp ax,0Dh / ja next sector
    ja near xsect
section hpis start=0x0750 vstart=0x109C2        ; unknown special: return instead of I_Error
    ja near PIS_RET
section hboss start=0x0760 vstart=0x0AADB        ; A_BossDeath: call G_ExitLevel
    call boss_exit
section hinit start=0x0770 vstart=0x1557B        ; R_Init: call R_InitPlanes (an empty function)
    call init_drawsegs
section hdsmax start=0x0780 vstart=0x15BA6       ; R_DrawPlanes: drawsegs range check limit (cmp eax, 80h)
    dd MAXDRAWSEGS
