bLEADIN     .equ    0
bSHOT       .equ    1
bLOOP       .equ    2
bLEADOUT    .equ    3
bEND_TWIG   .equ    4
bEND_PATH   .equ    5
bSEX        .equ    8
bVIOLENCE   .equ    9
bCENSORED   .equ    16
bVIEWED     .equ    17

LEADIN      .equ    (2<<bLEADIN)
SHOT        .equ    (2<<bSHOT)
LOOP        .equ    (2<<bLOOP)
LEADOUT     .equ    (2<<bLEADOUT)
END_TWIG    .equ    (2<<bEND_TWIG)      ; Leaf is at end of twig
END_PATH    .equ    (2<<bEND_PATH)      ; Leaf is end of game path
SEX         .equ    (2<<bSEX)           ; Shot has sexual content
VIOLENCE    .equ    (2<<bVIOLENCE)      ; Shot contains violence
CENSORED    .equ    (2<<bCENSORED)      ; Shot is censored
VIEWED      .equ    (2<<bVIEWED)        ; Shot has been viewed

BODY0006    .equ    11
LOOP0007    .equ    12
LEADIN08    .equ    13
BODY0911    .equ    14
BODY1213    .equ    15
LOOP0014    .equ    16
LEADIN15    .equ    17
LEADIN18    .equ    18
LEADIN09    .equ    19
BODY0018    .equ    20
BODY1517    .equ    21
BODY1620    .equ    22
LOOP0021    .equ    23
LEADIN22    .equ    24
BODY0022    .equ    25      ; Thugs take Jack to director's lab
BODY2324    .equ    26      ; Jack fights with thugs ... escapes down hall
BODY0025    .equ    27      ; Thugs strap Jack to table
ENDPATH1    .equ    28
ENDPATH2    .equ    29
ENDPATH3    .equ    30
ENDPATH4    .equ    31
ENDPATH5    .equ    32
ENDPATH6    .equ    33
ENDPATH7    .equ    34
ENDPATH8    .equ    35
ENDPATH9    .equ    36

ALWAYS      .equ    0
NEVER       .equ    1

CONTEXT_TBD .equ    0
PSYPHY_TBD  .equ    0
SCORE_TBD   .equ    0
WEIGHT_TBD  .equ    0

        .offset 0               ; Dummy address

attrib:         ds.l    1       ; Attributes
index:          ds.w    1       ; Index on CD
permit:         ds.w    1       ; Permission code
context:        ds.w    1       ; Context for this instance

LEAF_SIZE:

        .offset 0               ; Dummy address

weight:         ds.l    1       ; Psychophysical weight
pruned:         ds.w    1       ; Twig has been pruned
nextNode:       ds.l    1       ; Pointer to next node
tLeaves:                        ; Variable number of leaves ...

        .text

;-----------------------------------------------------------------------------

GetNextLeaf:
        movea.l leafPointer,a0          ; Pointer to current leaf
        move.l  attrib(a0),d0           ; Get attributes
        btst.l  #END_TWIG,d0            ; Are we at end of a twig?
        beq.s   NotEnd                  ; Skip if not

        btst.l  #END_PATH,d0            ; Are we at end of a path?
        beq.s   NextNode                ; No, advance to next node

        movea.l #0,a0                   ; Indicate end of path
        rts

NextNode:

NotEnd:
        lea     LEAF_SIZE(a0),a0        ; Skip to next leaf
        rts

;-----------------------------------------------------------------------------

GetPermssion:
        lea     permissionTable,a0      ; Base of table
        lsl     #2,d0                   ; Index by permission code
        movea.l 0(a0,d0),a0             ; Address of handler
        jmp     (a0)                    ; Jump to it

Always:
        move    #1,d0                   ; Always true
        rts

Never:
        move    #0,d0                   ; Never true
        rts

permissionTable:
        dc.l    Always
        dc.l    Never

;-----------------------------------------------------------------------------

nWakeUp:
;leaf (body)
    dc.l    SHOT            ; attributes
    dc.w    BODY0006        ; CD index
    dc.w    ALWAYS          ; permission
    dc.w    CONTEXT_TBD     ; context
;leaf (loop)
    dc.l    LOOP            ; attributes
    dc.w    LOOP0007        ; CD index
    dc.w    ALWAYS          ; permission
    dc.w    CONTEXT_TBD     ; context
;branch table
    dc.l    bRun            ; option 0
    dc.l    bGrab           ; option 1
    dc.l    bWakeUpX        ; option 2 (default)

nAttackNurse:
;leaf (body)
    dc.l    SHOT            ; attributes
    dc.w    BODY1213        ; CD index
    dc.w    ALWAYS          ; permission
    dc.w    CONTEXT_TBD     ; context
;leaf (loop)
    dc.l    LOOP            ; attributes
    dc.w    LOOP0014        ; CD index
    dc.w    ALWAYS          ; permission
;branch table
    dc.l    bDie            ; option 0
    dc.l    bEnough         ; option 1
    dc.l    bAttackNurseX   ; option 2 (default)

nHallWalk:
;leaf (body)
    dc.l    SHOT            ; attributes
    dc.w    BODY1620        ; CD index
    dc.w    ALWAYS          ; permission
    dc.w    CONTEXT_TBD     ; context
;leaf (loop)
    dc.l    LOOP            ; attributes
    dc.w    LOOP0021        ; CD index
    dc.w    ALWAYS          ; permission
;branch table
    dc.l    bPlayAlong      ; option 0
    dc.l    bBustLoose      ; option 1
    dc.l    bHallWalkX      ; option 2 (default)

nEnd:

bRun:
    dc.l    SCORE_TBD       ; delta score
    dc.l    PSYPHY_TBD      ; delta psycho-physical
;leaf (leadin)
    dc.l    LEADIN          ; attributes
    dc.w    LEADIN08        ; CD index
    dc.w    ALWAYS          ; permission
    dc.w    CONTEXT_TBD     ; context
;twig table
    dc.l    tRun0
    dc.l    tRun1
    dc.l    tRun2
tRun0:
    dc.l    WEIGHT_TBD      ; weight
    dc.w    0               ; leaf count
    dc.l    nHallWalk       ; next node
tRun1:
    dc.l    WEIGHT_TBD      ; weight
    dc.w    0               ; leaf count
    dc.l    nHallWalk       ; next node
tRun2:
    dc.l    WEIGHT_TBD      ; weight
    dc.w    0               ; leaf count
    dc.l    nHallWalk       ; next node

bGrab:
    dc.l    SCORE_TBD       ; delta score
    dc.l    PSYPHY_TBD      ; delta psycho-physical
;leaf (leadin)
    dc.l    LEADIN          ; attributes
    dc.w    LEADIN08        ; CD index
    dc.w    ALWAYS          ; permission
    dc.w    CONTEXT_TBD     ; context
;twig table
    dc.l    tGrab0
    dc.l    tGrab1
    dc.l    tGrab2
tGrab0:
    dc.l    WEIGHT_TBD      ; weight
    dc.w    0               ; leaf count
    dc.l    nAttackNurse    ; next node
tGrab1:
    dc.l    WEIGHT_TBD      ; weight
    dc.w    0               ; leaf count
    dc.l    nAttackNurse    ; next node
tGrab2:
    dc.l    WEIGHT_TBD      ; weight
    dc.w    0               ; leaf count
    dc.l    nAttackNurse    ; next node

bWakeUpX:
    dc.l    SCORE_TBD       ; delta score
    dc.l    PSYPHY_TBD      ; delta psycho-physical
;leaf (leadin)
    dc.l    LEADIN          ; attributes
    dc.w    LEADIN08        ; CD index
    dc.w    0               ; permission
    dc.w    CONTEXT_TBD     ; context
;twig table
    dc.l    tWakeUpX0
    dc.l    tWakeUpX1
    dc.l    tWakeUpX2
tWakeUpX0:
tWakeUpX1:
tWakeUpX2:

bDie:

bEnough:

bAttackNurseX:

bPlayAlong:
    dc.l    SCORE_TBD       ; delta score
    dc.l    PSYPHY_TBD      ; delta psycho-physical
;leaf (leadin)
    dc.l    LEADIN          ; attributes
    dc.w    LEADIN22        ; CD index
    dc.w    ALWAYS          ; permission
    dc.w    CONTEXT_TBD     ; context
;twig table
    dc.l    tPlayAlong0
    dc.l    tPlayAlong1
    dc.l    tPlayAlong2
tPlayAlong0:
    dc.l    WEIGHT_TBD      ; weight
    dc.w    0               ; leaf count
    dc.l    nEnd            ; next node
tPlayAlong1:
    dc.l    WEIGHT_TBD      ; weight
    dc.w    0               ; leaf count
    dc.l    nEnd            ; next node
tPlayAlong2:
    dc.l    WEIGHT_TBD      ; weight
    dc.w    0               ; leaf count
    dc.l    nEnd            ; next node

bBustLoose:

bHallWalkX:

        .bss

leafPointer:    ds.l    1               ; Pointer to current leaf

        .end


