[A83] raycast / wolfenstein


[Prev][Next][Index][Thread]

[A83] raycast / wolfenstein




A guy called Coelurus made a engine like wolfenstein,
the prog (TI-83(+)) is raycast. But the Ti-83- version
doesn't work properly, the Ti-83+ version does. Could
somebody find the problem??

   SUCKER



;
; Raycast example for ION   http://seawasp.hjorten.nu/~coelurus
;                           thorntwig@telia.com
;
; Sourcecode for a simple semi-3D world for ION! My classmates got
; astonished, so why not try it out? Further documentation can be found in
; raycast.txt.
;
; Keys:
;  Del          -> Exit
;  Up           -> Walk forward
;  Down         -> Walk backward
;  Right        -> Turn right
;  Left         -> Turn left
;  Y=           -> Sidestep left
;  Window       -> Sidestep right
;
; Missions, enemies, weapons etc will be a future topic.
;

.nolist

#include "ion.inc"
#include "extras.inc"

; Use the 9 first bits as decimals
#define FPM             512

; Some trig-stuff
#define CIRCLE          512
#define QUARTER         128
#define HALF            256

; Player-specific definitions
#define TURNS           32
#define RIGHT_TURN      16      ; CIRCLE / TURNS
#define LEFT_TURN       496     ; CIRCLE - RIGHT_TURN

; Main program flags
done            = saferam3;             ; Main program loop
move            = saferam3 + 1;         ; Do we have to redraw?

; Player data
playerX         = saferam3 + 2;         ; Location of player
playerY         = saferam3 + 4;
newPlayerX      = saferam3 + 6;         ; New location of player
newPlayerY      = saferam3 + 8;
playerDir       = saferam3 + 10;        ; Wheas his/her nose?

; Raycasting help
angle           = saferam3 + 12;        ; Current casting angle
column          = saferam3 + 14;        ; Current column
x               = saferam3 + 15;        ; Ray location
y               = saferam3 + 17;
cos             = saferam3 + 19;        ; Lots of uses...
sin             = saferam3 + 20;
distance        = saferam3 + 21;        ; Distance to found wall
mapX            = saferam3 + 22;        ; Non-fpm location
mapY            = saferam3 + 23;
zBuffer         = saferam3 + 24;        ; Distance to every column

; Output variables
address         = saferam3 + 120;       ; Address to zBuffer
upper           = saferam3 + 122;       ; Upper pixel of sliver
lower           = saferam3 + 123;       ; Add to upper for lower pixel

.list

; ==== ;
; Main ;
; ==== ;
#ifdef TI83P
        .org    progstart - 2
        .db     0BBh, 6Dh
#else
        .org progstart
#endif
        ret
        jr      nc, begin
        .db     "Raycast demo!", 0
begin:
        ; Lots of resetting...
        xor     a
        ld      (done), a
        inc     a
        ld      (move), a
        ld      bc, 2 * FPM
        ld      (playerX), bc
        ld      (playerY), bc
        ld      bc, 0
        ld      (playerDir), bc
mainLoop:
        ld      a, (move)
        or      a
        jr      z, noRaycast
        bcall(_grbufclr)
        call    raycast
        call    draw
        bcall(_grbufcpy)
noRaycast:

        call    update

        ld      a, (done)
        or      a
        jr      z, mainLoop

        ret

.end
END


; =================================== ;
; Raycasting routine (update zBuffer) ;
; =================================== ;
raycast:
        ; Convert player direction (angle - (FOV / 2))
        ld      hl, (playerDir)
        ld      bc, 464
        add     hl, bc
        call    clampAngle
        ld      (angle), hl

        ; Reset column counter
        xor     a
        ld      (column), a
raycastColumnLoop:

        ; Copy location of player
        ld      hl, (playerX)
        ld      (x), hl
        ld      hl, (playerY)
        ld      (y), hl

        ; Get trig steps
        ld      hl, (angle)
        call    getTrigs

        ; Reset distance
        xor     a
        ld      (distance), a
raycastingLoop:

        ; Update variables
        ; x coordinate
        ld      hl, (x)
        ld      a, (cos)
        call    step
        ld      (x), hl
        ; y coordinate
        ld      hl, (y)
        ld      a, (sin)
        call    step
        ld      (y), hl
        ; distance
        ld      a, (distance)
        inc     a
        ld      (distance), a

        ; Save non-fpm location
        ld      hl, (x)
        call    removeFPM
        ld      (mapX), a
        ld      hl, (y)
        call    removeFPM
        ld      (mapY), a

        ; Check wallhit
        call    checkHit
        or      a
        jr      z, raycastingLoop

        ; Store distance
        ld      hl, zBuffer
        ld      a, (column)
        ld      b, 0
        ld      c, a
        add     hl, bc
        ld      a, (distance)
        ld      (hl), a

        ; Update angle
        ld      hl, (angle)
        inc     hl
        call    clampAngle
        ld      (angle), hl

        ; Update column loop
        ld      a, (column)
        inc     a
        ld      (column), a
        cp      96
        jp      nz, raycastColumnLoop

        ret


; ============= ;
; Draws zBuffer ;
; ============= ;
draw:
        ; Reset column counter
        xor     a
        ld      (column), a

        ; Copy zBuffer address
        ld      hl, zBuffer
        ld      (address), hl

drawColumnLoop:

        ; Get zBuffer value
        ld      hl, (address)
        ld      a, (hl)

        ; Calculate wall sliver (rather GET wall sliver...)
        ld      hl, height
        ld      b, 0
        ld      c, a
        add     hl, bc
        ld      a, (hl)
        ld      c, a
        cp      32
        jr      z, outOfDisplay
        ld      a, 32
        sub     c
        ld      (upper), a
        sla     c
        ld      a, c
        ld      (lower), a

        ; Draw upper pixel (altered, from Olle Hedman)
        ld      a, (upper)
        sla     a
        sla     a
        ld      c, a
        ld      hl, graph_mem
        add     hl, bc
        add     hl, bc
        add     hl, bc
        ld      a, (column)
        ld      c, a
        srl     c
        srl     c
        srl     c
        add     hl, bc
        and     7
        ld      c, a
        ld      ix, mask
        add     ix, bc
        ld      a, (ix + 0)
        ld      d, a
        or      (hl)
        ld      (hl), a

        ; Advance to draw lower pixel
        ld      a, (lower)
        sla     a
        sla     a
        ld      c, a
        add     hl, bc
        add     hl, bc
        add     hl, bc
        ld      a, d
        or      (hl)
        ld      (hl), a
outOfDisplay:

        ; Increase zBuffer address
        ld      hl, address
        inc     (hl)

        ; Increase column counter
        ld      a, (column)
        inc     a
        ld      (column), a
        cp      96
        jr      nz, drawColumnLoop

        ret


; ============================================= ;
; Update player input (bigger than raycasting!) ;
; ============================================= ;
update:
        ; Skip drawing to spare your calc...
        xor     a
        ld      (move), a

        ; Check upper row of keys
        ld      a, KeysReset
        out     (Keys), a
        ld      a, UpperRow
        out     (Keys), a
        in      a, (Keys)
        ld      d, a

        ; Check exit [Del]
        or      K_Del
        cp      K_Del
        jr      nz, dontExit
        ld      a, 1
        ld      (done), a
        ret
dontExit:

        ; Check strafing/sidestep [Y=] & [WINDOW]
        ld      a, d
        or      K_Y
        cp      K_Y
        jr      nz, notY
        ld      hl, (playerDir)
        ld      bc, QUARTER + HALF
        add     hl, bc
        call    clampAngle
        call    getTrigs
        ; Update location
        ld      hl, (playerX)
        ld      a, (cos)
        sra     a
        sra     a
        call    step
        ld      (newPlayerX), hl
        ld      hl, (playerY)
        ld      a, (sin)
        sra     a
        sra     a
        call    step
        ld      (newPlayerY), hl
        ; Force raycast
        ld      a, 1
        ld      (move), a
notY:

        ld      a, d
        or      K_Window
        cp      K_Window
        jr      nz, notWindow
        ld      hl, (playerDir)
        ld      bc, QUARTER
        add     hl, bc
        call    clampAngle
        call    getTrigs
        ; Update location
        ld      hl, (playerX)
        ld      a, (cos)
        sra     a
        sra     a
        call    step
        ld      (newPlayerX), hl
        ld      hl, (playerY)
        ld      a, (sin)
        sra     a
        sra     a
        call    step
        ld      (newPlayerY), hl
        ; Force raycast
        ld      a, 1
        ld      (move), a
notWindow:

        ; Check arrows
        ld      a, KeysReset
        out     (Keys), a
        ld      a, Arrows
        out     (Keys), a
        in      a, (Keys)
        ld      d, a

        ; Check walking [^] & [v]
        or      K_Up
        cp      K_Up
        jr      nz, notUp
        ld      hl, (playerDir)
        call    getTrigs
        ; Update location
        ld      a, (cos)
        ld      hl, (playerX)
        sra     a
        sra     a
        call    step
        ld      (newPlayerX), hl
        ld      a, (sin)
        ld      hl, (playerY)
        sra     a
        sra     a
        call    step
        ld      (newPlayerY), hl
        ; Force raycast
        ld      a, 1
        ld      (move), a
notUp:

        ld      a, d
        or      K_Down
        cp      K_Down
        jr      nz, notDown
        ld      hl, (playerDir)
        ld      bc, HALF
        add     hl, bc
        call    clampAngle
        call    getTrigs
        ; Update location
        ld      hl, (playerX)
        ld      a, (cos)
        sra     a
        sra     a
        call    step
        ld      (newPlayerX), hl
        ld      hl, (playerY)
        ld      a, (sin)
        sra     a
        sra     a
        call    step
        ld      (newPlayerY), hl
        ; Force raycast
        ld      a, 1
        ld      (move), a
notDown:

        ld      a, (move)
        or      a
        jr      z, invalidY

        ; Is player going X'ily into a wall?
        ld      hl, (newPlayerX)
        call    removeFPM
        ld      (mapX), a
        ld      hl, (playerY)
        call    removeFPM
        ld      (mapY), a
        call    checkHit
        or      a
        jr      nz, invalidX
        ; X OK!
        ld      hl, (newPlayerX)
        ld      (playerX), hl
invalidX:
        ; Is player going "Y'ily" into a wall?
        ld      hl, (playerX)
        call    removeFPM
        ld      (mapX), a
        ld      hl, (newPlayerY)
        call    removeFPM
        ld      (mapY), a
        call    checkHit
        or      a
        jr      nz, invalidY
        ; Y OK!
        ld      hl, (newPlayerY)
        ld      (playerY), hl
invalidY:

        ; Check turning [<] & [>]
        ld      a, d
        or      K_Left
        cp      K_Left
        jr      nz, notLeft
        ld      hl, (playerDir)
        ld      bc, LEFT_TURN
        add     hl, bc
        call    clampAngle
        ld      (playerDir), hl
        ld      a, 1
        ld      (move), a
notLeft:

        ld      a, d
        or      K_Right
        cp      K_Right
        jr      nz, notRight
        ld      hl, (playerDir)
        ld      bc, RIGHT_TURN
        add     hl, bc
        call    clampAngle
        ld      (playerDir), hl
        ld      a, 1
        ld      (move), a
notRight:

        ret


; =========================== ;
; Clamp hl inside angle range ;
; =========================== ;
clampAngle:
        ld      a, h
        and     1
        ld      h, a
        ld      a, l
        and     255
        ld      l, a

        ret


; ====================== ;
; Get trigs for angle hl ;
; ====================== ;
getTrigs:
        ; Sin
        ld      bc, trigs
        add     hl, bc
        ld      a, (hl)
        ld      (cos), a
        ; Cos
        ld      bc, QUARTER
        add     hl, bc
        ld      a, (hl)
        ld      (sin), a

        ret


; ============ ;
; Step hl by a ;
; ============ ;
step:
        ld      b, 0
        ld      c, a
        and     80h
        or      a
        jr      z, addStep
        ld      b, 255
addStep:
        add     hl, bc

        ret


; =================== ;
; Removes FPM from hl ;
; =================== ;
removeFPM:
        ld      a, h
        sra     a

        ret


; =================== ;
; Have we hit a wall? ;
; =================== ;
checkHit:
        ; Offset (x + y * 16)
        ld      a, (mapY)
        sla     a
        sla     a
        sla     a
        sla     a
        ld      c, a
        ld      a, (mapX)
        add     a, c
        ld      c, a
        ; Check
        ld      b, 0
        ld      hl, level
        add     hl, bc
        ld      a, (hl)

        ret


; Level structure (simple to make own levels...)
level:
        .db     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
        .db     1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1
        .db     1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1
        .db     1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1
        .db     1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1
        .db     1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1
        .db     1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
        .db     1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1
        .db     1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1
        .db     1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1
        .db     1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1
        .db     1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1
        .db     1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1
        .db     1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1
        .db     1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1
        .db     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1

; Trig table. 0..511 -> Sin, 128..639 -> Cos : 640 bytes!
trigs:
        .db     0, 1, 3, 4, 6, 7, 9, 10
        .db     12, 13, 15, 17, 18, 20, 21, 23
        .db     24, 26, 27, 29, 30, 32, 33, 35
        .db     36, 38, 39, 41, 42, 44, 45, 47
        .db     48, 50, 51, 52, 54, 55, 57, 58
        .db     59, 61, 62, 63, 65, 66, 67, 69
        .db     70, 71, 73, 74, 75, 76, 78, 79
        .db     80, 81, 82, 84, 85, 86, 87, 88
        .db     89, 90, 91, 93, 94, 95, 96, 97
        .db     98, 99, 100, 101, 102, 102, 103, 104
        .db     105, 106, 107, 108, 108, 109, 110, 111
        .db     112, 112, 113, 114, 114, 115, 116, 116
        .db     117, 117, 118, 119, 119, 120, 120, 121
        .db     121, 121, 122, 122, 123, 123, 123, 124
        .db     124, 124, 125, 125, 125, 125, 126, 126
        .db     126, 126, 126, 126, 126, 126, 126, 126
        .db     127, 126, 126, 126, 126, 126, 126, 126
        .db     126, 126, 126, 125, 125, 125, 125, 124
        .db     124, 124, 123, 123, 123, 122, 122, 121
        .db     121, 121, 120, 120, 119, 119, 118, 117
        .db     117, 116, 116, 115, 114, 114, 113, 112
        .db     112, 111, 110, 109, 108, 108, 107, 106
        .db     105, 104, 103, 102, 102, 101, 100, 99
        .db     98, 97, 96, 95, 94, 93, 91, 90
        .db     89, 88, 87, 86, 85, 84, 82, 81
        .db     80, 79, 78, 76, 75, 74, 73, 71
        .db     70, 69, 67, 66, 65, 63, 62, 61
        .db     59, 58, 57, 55, 54, 52, 51, 50
        .db     48, 47, 45, 44, 42, 41, 39, 38
        .db     36, 35, 33, 32, 30, 29, 27, 26
        .db     24, 23, 21, 20, 18, 17, 15, 13
        .db     12, 10, 9, 7, 6, 4, 3, 1
        .db     0, -1, -3, -4, -6, -7, -9, -10
        .db     -12, -13, -15, -17, -18, -20, -21, -23
        .db     -24, -26, -27, -29, -30, -32, -33, -35
        .db     -36, -38, -39, -41, -42, -44, -45, -47
        .db     -48, -50, -51, -52, -54, -55, -57, -58
        .db     -59, -61, -62, -63, -65, -66, -67, -69
        .db     -70, -71, -73, -74, -75, -76, -78, -79
        .db     -80, -81, -82, -84, -85, -86, -87, -88
        .db     -89, -90, -91, -93, -94, -95, -96, -97
        .db     -98, -99, -100, -101, -102, -102, -103, -104
        .db     -105, -106, -107, -108, -108, -109, -110, -111
        .db     -112, -112, -113, -114, -114, -115, -116, -116
        .db     -117, -117, -118, -119, -119, -120, -120, -121
        .db     -121, -121, -122, -122, -123, -123, -123, -124
        .db     -124, -124, -125, -125, -125, -125, -126, -126
        .db     -126, -126, -126, -126, -126, -126, -126, -126
        .db     -127, -126, -126, -126, -126, -126, -126, -126
        .db     -126, -126, -126, -125, -125, -125, -125, -124
        .db     -124, -124, -123, -123, -123, -122, -122, -121
        .db     -121, -121, -120, -120, -119, -119, -118, -117
        .db     -117, -116, -116, -115, -114, -114, -113, -112
        .db     -112, -111, -110, -109, -108, -108, -107, -106
        .db     -105, -104, -103, -102, -102, -101, -100, -99
        .db     -98, -97, -96, -95, -94, -93, -91, -90
        .db     -89, -88, -87, -86, -85, -84, -82, -81
        .db     -80, -79, -78, -76, -75, -74, -73, -71
        .db     -70, -69, -67, -66, -65, -63, -62, -61
        .db     -59, -58, -57, -55, -54, -52, -51, -50
        .db     -48, -47, -45, -44, -42, -41, -39, -38
        .db     -36, -35, -33, -32, -30, -29, -27, -26
        .db     -24, -23, -21, -20, -18, -17, -15, -13
        .db     -12, -10, -9, -7, -6, -4, -3, -1
        .db     0, 1, 3, 4, 6, 7, 9, 10
        .db     12, 13, 15, 17, 18, 20, 21, 23
        .db     24, 26, 27, 29, 30, 32, 33, 35
        .db     36, 38, 39, 41, 42, 44, 45, 47
        .db     48, 50, 51, 52, 54, 55, 57, 58
        .db     59, 61, 62, 63, 65, 66, 67, 69
        .db     70, 71, 73, 74, 75, 76, 78, 79
        .db     80, 81, 82, 84, 85, 86, 87, 88
        .db     89, 90, 91, 93, 94, 95, 96, 97
        .db     98, 99, 100, 101, 102, 102, 103, 104
        .db     105, 106, 107, 108, 108, 109, 110, 111
        .db     112, 112, 113, 114, 114, 115, 116, 116
        .db     117, 117, 118, 119, 119, 120, 120, 121
        .db     121, 121, 122, 122, 123, 123, 123, 124
        .db     124, 124, 125, 125, 125, 125, 126, 126
        .db     126, 126, 126, 126, 126, 126, 126, 126

; Height table for slivers (focus = 127, height = 1, distance = 0..90)
height:
        .db     32, 32, 32, 31, 25, 21, 18, 15, 14, 14
        .db     11, 10, 9, 9, 8, 7, 7, 7, 6, 6
        .db     6, 5, 5, 5, 5, 4, 4, 4, 4, 4
        .db     4, 3, 3, 3, 3, 3, 3, 3, 3, 3
        .db     3, 3, 2, 2, 2, 2, 2, 2, 2, 2
        .db     2, 2, 2, 2, 2, 2, 2, 2, 2, 2
        .db     2, 2, 2, 1, 1, 1, 1, 1, 1, 1
        .db     1, 1, 1, 1, 1, 1, 1, 1, 1, 1
        .db     1, 1, 1, 1, 1, 1, 1, 1, 1, 1

; Bit masks for pixel routine
mask:
        .db     128, 64, 32, 16, 8, 4, 2, 1


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp