Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-demo-wolf3d

Source of release 0.4.1

0.4.1 / src / wl_act1.lucb

## Doors, statics, pushwalls — WOLFSRC/WL_ACT1.C.
import wl_def
import wl_types
from wl_types import Game, Door, StaticObj

## WL_ACT1.C SpawnDoor
pub func spawn_door(game: Game*, x: i64, y: i64, vertical: bool, lock: i64):
    if game.door_count >= wl_def.max_doors:
        return
    let n = game.door_count
    game.doors[(usize)n] = Door(tilex = x, tiley = y, vertical = vertical, lock = lock, action = wl_def.dr_closed)
    game.tilemap[wl_def.cell(x, y)] = (u8)n | wl_def.bit_door
    game.actorat[wl_def.cell(x, y)] = (u16)n | (u16)wl_def.bit_door
    if vertical:
        if y > 0:
            game.tilemap[wl_def.cell(x, y - 1)] = game.tilemap[wl_def.cell(x, y - 1)] | wl_def.bit_wall
        if y + 1 < wl_def.map_size:
            game.tilemap[wl_def.cell(x, y + 1)] = game.tilemap[wl_def.cell(x, y + 1)] | wl_def.bit_wall
    else:
        if x > 0:
            game.tilemap[wl_def.cell(x - 1, y)] = game.tilemap[wl_def.cell(x - 1, y)] | wl_def.bit_wall
        if x + 1 < wl_def.map_size:
            game.tilemap[wl_def.cell(x + 1, y)] = game.tilemap[wl_def.cell(x + 1, y)] | wl_def.bit_wall
    game.door_count = n + 1

## WL_ACT1.C statinfo[] bonus kinds for SpawnStatic
func static_kind(which: i64) -> i64:
    if which == 1 or which == 2 or which == 3 or which == 5 or which == 7 or which == 8:
        return wl_def.bo_block
    if which == 10 or which == 11 or which == 12 or which == 13:
        return wl_def.bo_block
    if which == 16 or which == 17 or which == 18:
        return wl_def.bo_block
    if which == 35 or which == 36 or which == 37 or which == 39 or which == 40:
        return wl_def.bo_block
    if which == 45 or which == 46:
        return wl_def.bo_block
    if which == 6:
        return wl_def.bo_alpo
    if which == 20:
        return wl_def.bo_key1
    if which == 21:
        return wl_def.bo_key2
    if which == 24:
        return wl_def.bo_food
    if which == 25:
        return wl_def.bo_firstaid
    if which == 26:
        return wl_def.bo_clip
    if which == 27:
        return wl_def.bo_machinegun
    if which == 28:
        return wl_def.bo_chaingun
    if which == 29:
        return wl_def.bo_cross
    if which == 30:
        return wl_def.bo_chalice
    if which == 31:
        return wl_def.bo_bible
    if which == 32:
        return wl_def.bo_crown
    if which == 33:
        return wl_def.bo_fullheal
    if which == 34 or which == 38:
        return wl_def.bo_gibs
    return wl_def.bo_none

## WL_ACT1.C SpawnStatic
pub func spawn_static(game: Game*, x: i64, y: i64, which: i64):
    if game.static_count >= wl_def.max_stats:
        return
    let kind = static_kind(which)
    var obj = StaticObj(tilex = x, tiley = y, shapenum = wl_def.spr_stat_0 + which, item = (u8)kind)
    if kind == wl_def.bo_block:
        game.actorat[wl_def.cell(x, y)] = (u16)wl_def.bit_wall
    elif kind != wl_def.bo_none:
        obj.flags = wl_def.fl_bonus
        if kind == wl_def.bo_cross or kind == wl_def.bo_chalice or kind == wl_def.bo_bible or kind == wl_def.bo_crown:
            game.treasuretotal += 1
    game.statics[(usize)game.static_count] = obj
    game.static_count += 1

## WL_ACT1.C PlaceItemType
pub func place_item(game: Game*, tilex: i64, tiley: i64, kind: i64):
    if game.static_count >= wl_def.max_stats:
        return
    var shape = wl_def.spr_stat_0 + 26
    if kind == wl_def.bo_machinegun:
        shape = wl_def.spr_stat_0 + 27
    elif kind == wl_def.bo_key1:
        shape = wl_def.spr_stat_0 + 20
    game.statics[(usize)game.static_count] = StaticObj(tilex = tilex, tiley = tiley, shapenum = shape, flags = wl_def.fl_bonus, item = (u8)kind)
    game.static_count += 1

## WL_ACT1.C MoveDoors / DoorOpen / DoorOpening / DoorClosing
pub func move_doors(game: Game*):
    for i in 0..<game.door_count:
        let door = &game.doors[(usize)i]
        if door.action == wl_def.dr_open:
            door.ticcount += game.tics
            if door.ticcount >= wl_def.open_tics:
                close_door(game, i)
        elif door.action == wl_def.dr_opening:
            door.position += game.tics << 10
            if door.position >= 0xffff:
                door.position = 0xffff
                door.ticcount = 0
                door.action = wl_def.dr_open
                game.actorat[wl_def.cell(door.tilex, door.tiley)] = 0
        elif door.action == wl_def.dr_closing:
            if game.player.tilex == door.tilex and game.player.tiley == door.tiley:
                open_door(game, i)
            else:
                door.position -= game.tics << 10
                if door.position <= 0:
                    door.position = 0
                    door.action = wl_def.dr_closed
                    game.actorat[wl_def.cell(door.tilex, door.tiley)] = (u16)i | (u16)wl_def.bit_door

## WL_ACT1.C OperateDoor / OpenDoor / CloseDoor
pub func operate_door(game: Game*, door: i64):
    if door < 0 or door >= game.door_count:
        return
    let lock = game.doors[(usize)door].lock
    if lock >= 1 and lock <= 4:
        if (game.keys & (1 << (lock - 1))) == 0:
            if game.doors[(usize)door].position == 0:
                wl_types.play_sound(game, wl_def.snd_no_way)
            return
    let action = game.doors[(usize)door].action
    if action == wl_def.dr_closed or action == wl_def.dr_closing:
        open_door(game, door)
    elif action == wl_def.dr_open or action == wl_def.dr_opening:
        close_door(game, door)

pub func open_door(game: Game*, door: i64):
    if door < 0 or door >= game.door_count:
        return
    let d = &game.doors[(usize)door]
    if d.action == wl_def.dr_open:
        d.ticcount = 0
    elif d.action != wl_def.dr_opening:
        if d.position == 0:
            wl_types.play_sound(game, wl_def.snd_door_open)
        d.action = wl_def.dr_opening

pub func close_door(game: Game*, door: i64):
    if door < 0 or door >= game.door_count:
        return
    let tilex = game.doors[(usize)door].tilex
    let tiley = game.doors[(usize)door].tiley
    if game.actorat[wl_def.cell(tilex, tiley)] != 0:
        return
    if game.player.tilex == tilex and game.player.tiley == tiley:
        return
    if game.doors[(usize)door].action != wl_def.dr_closing:
        wl_types.play_sound(game, wl_def.snd_door_close)
    game.doors[(usize)door].action = wl_def.dr_closing
    game.actorat[wl_def.cell(tilex, tiley)] = (u16)door | (u16)wl_def.bit_door

## WL_ACT1.C PushWall
pub func push_wall(game: Game*, checkx: i64, checky: i64, dir: i64):
    if game.pwallstate != 0:
        return
    let oldtile = (i64)game.tilemap[wl_def.cell(checkx, checky)]
    if oldtile == 0 or (oldtile & (i64)wl_def.bit_door) != 0:
        return
    var dx: i64 = 0
    var dy: i64 = 0
    if dir == wl_def.east:
        dx = 1
    elif dir == wl_def.west:
        dx = -1
    elif dir == wl_def.north:
        dy = -1
    else:
        dy = 1
    let nx = checkx + dx
    let ny = checky + dy
    if nx < 1 or ny < 1 or nx >= wl_def.map_size - 1 or ny >= wl_def.map_size - 1:
        return
    if game.actorat[wl_def.cell(nx, ny)] != 0:
        return
    game.secretcount += 1
    game.pwallx = checkx
    game.pwally = checky
    game.pwalldir = dir
    game.pwallstate = 1
    game.pwallpos = 0
    game.pwalltile = oldtile
    game.tilemap[wl_def.cell(checkx, checky)] = wl_def.bit_wall
    game.tilemap[wl_def.cell(nx, ny)] = wl_def.bit_wall
    game.actorat[wl_def.cell(nx, ny)] = (u16)oldtile
    game.maps.info[wl_def.plane_cell(checkx, checky)] = 0
    wl_types.play_sound(game, wl_def.snd_pushwall)

## WL_ACT1.C MovePWalls
pub func move_pwalls(game: Game*):
    if game.pwallstate == 0:
        return
    let oldblock = game.pwallstate // 128
    game.pwallstate += game.tics
    if game.pwallstate // 128 != oldblock:
        let oldtile = game.pwalltile
        game.tilemap[wl_def.cell(game.pwallx, game.pwally)] = 0
        game.actorat[wl_def.cell(game.pwallx, game.pwally)] = 0
        var dx: i64 = 0
        var dy: i64 = 0
        if game.pwalldir == wl_def.east:
            dx = 1
        elif game.pwalldir == wl_def.west:
            dx = -1
        elif game.pwalldir == wl_def.north:
            dy = -1
        else:
            dy = 1
        if game.pwallstate >= 256:
            game.pwallstate = 0
            game.tilemap[wl_def.cell(game.pwallx + dx, game.pwally + dy)] = (u8)oldtile
            game.actorat[wl_def.cell(game.pwallx + dx, game.pwally + dy)] = (u16)oldtile
            return
        game.pwallx += dx
        game.pwally += dy
        let nx = game.pwallx + dx
        let ny = game.pwally + dy
        if game.actorat[wl_def.cell(nx, ny)] != 0:
            game.pwallstate = 0
            game.tilemap[wl_def.cell(game.pwallx, game.pwally)] = (u8)oldtile
            game.actorat[wl_def.cell(game.pwallx, game.pwally)] = (u16)oldtile
            return
        game.actorat[wl_def.cell(nx, ny)] = (u16)oldtile
        game.tilemap[wl_def.cell(nx, ny)] = wl_def.bit_wall
        game.tilemap[wl_def.cell(game.pwallx, game.pwally)] = wl_def.bit_wall
    game.pwallpos = (game.pwallstate // 2) % 64