Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-demo-wolf3d

Source of release 0.4.1

0.4.1 / src / id_ca.lucb

## Caching manager from WOLFSRC/ID_CA.C: Carmack expand, RLEW, GAMEMAPS.
import c
from luce_std import files
import interop
import memory
import wl_def

pub struct MapHeader:
    pub var plane_start: i32[3]
    pub var plane_length: u16[3]
    pub var width: u16
    pub var height: u16

pub struct Maps:
    var rlew_tag: u16
    var headers: MapHeader[60]
    var raw: const u8[]
    ## The file `raw` is read from, released on close.
    var raw_file: interop.Owned[const u8[]]?
    pub var walls: u16[4096]
    pub var info: u16[4096]

    pub func close():
        if let file = self.raw_file:
            file.release()
        var empty: u8[]
        self.raw = empty
        self.raw_file = none

pub func carmack_expand(source: const u8[], dest: u16[], expanded_bytes: i64) -> !:
    var in_off: usize = 0
    var out_off: usize = 0
    var remaining = expanded_bytes // 2
    while remaining > 0:
        if in_off + 1 >= source.length:
            error(wl_def.invalid, "Carmack input overran the compressed plane")
        let ch = wl_def.u16le(source, in_off)
        in_off += 2
        let high = ch // 256
        if high == wl_def.near_tag:
            let count = ch % 256
            if count == 0:
                if in_off >= source.length:
                    error(wl_def.invalid, "Carmack near-tag literal overran")
                dest[out_off] = ch | (u16)source[in_off]
                in_off += 1
                out_off += 1
                remaining -= 1
            else:
                if in_off >= source.length:
                    error(wl_def.invalid, "Carmack near offset overran")
                let offset = (usize)source[in_off]
                in_off += 1
                remaining -= (i64)count
                if remaining < 0:
                    return
                var n = count
                while n > 0:
                    dest[out_off] = dest[out_off - offset]
                    out_off += 1
                    n -= 1
        elif high == wl_def.far_tag:
            let count = ch % 256
            if count == 0:
                if in_off >= source.length:
                    error(wl_def.invalid, "Carmack far-tag literal overran")
                dest[out_off] = ch | (u16)source[in_off]
                in_off += 1
                out_off += 1
                remaining -= 1
            else:
                if in_off + 1 >= source.length:
                    error(wl_def.invalid, "Carmack far offset overran")
                var copy = (usize)wl_def.u16le(source, in_off)
                in_off += 2
                remaining -= (i64)count
                if remaining < 0:
                    return
                var n = count
                while n > 0:
                    dest[out_off] = dest[copy]
                    copy += 1
                    out_off += 1
                    n -= 1
        else:
            dest[out_off] = ch
            out_off += 1
            remaining -= 1

pub func rlew_expand(source: const u16[], dest: u16[], expanded_bytes: i64, tag: u16) -> !:
    var in_off: usize = 0
    var out_off: usize = 0
    let end = (usize)(expanded_bytes // 2)
    while out_off < end:
        if in_off >= source.length:
            error(wl_def.invalid, "RLEW input overran")
        let value = source[in_off]
        in_off += 1
        if value != tag:
            dest[out_off] = value
            out_off += 1
        else:
            if in_off + 1 >= source.length:
                error(wl_def.invalid, "RLEW run overran")
            let count = (usize)source[in_off]
            in_off += 1
            let run = source[in_off]
            in_off += 1
            if count == 0:
                error(wl_def.invalid, "RLEW run length is zero")
            var n: usize = 0
            while n < count:
                if out_off >= dest.length:
                    error(wl_def.invalid, "RLEW output overran")
                dest[out_off] = run
                out_off += 1
                n += 1

pub func load_maps(head_path: str, maps_path: str) -> Maps!:
    let head_file = try files.read((c.str)head_path)
    defer head_file.release()
    let head = head_file.value
    if head.length < 6:
        error(wl_def.invalid, "MAPHEAD is truncated")
    var maps = Maps()
    maps.rlew_tag = wl_def.u16le(head, 0)
    let raw = try files.read((c.str)maps_path)
    maps.raw_file = raw
    maps.raw = raw.value
    errdefer maps.close()
    var i: i64 = 0
    while i < 60:
        let off = 2 + i * 4
        if (usize)off + 4 > head.length:
            break
        let pos = wl_def.i32le(head, (usize)off)
        if pos > 0:
            maps.headers[(usize)i] = read_header(maps.raw, (usize)pos)
        i += 1
    return maps

func read_header(raw: const u8[], pos: usize) -> MapHeader:
    var header = MapHeader()
    header.plane_start[0] = wl_def.i32le(raw, pos)
    header.plane_start[1] = wl_def.i32le(raw, pos + 4)
    header.plane_start[2] = wl_def.i32le(raw, pos + 8)
    header.plane_length[0] = wl_def.u16le(raw, pos + 12)
    header.plane_length[1] = wl_def.u16le(raw, pos + 14)
    header.plane_length[2] = wl_def.u16le(raw, pos + 16)
    header.width = wl_def.u16le(raw, pos + 18)
    header.height = wl_def.u16le(raw, pos + 20)
    return header

pub func cache_map(maps: Maps*, mapnum: i64) -> !:
    if mapnum < 0 or mapnum >= 60:
        error(wl_def.invalid, "map number is out of range")
    let header = maps.headers[(usize)mapnum]
    if header.width != 64 or header.height != 64:
        error(wl_def.invalid, "map is not 64x64")
    try expand_plane(maps, header.plane_start[0], header.plane_length[0], maps.walls)
    try expand_plane(maps, header.plane_start[1], header.plane_length[1], maps.info)

func expand_plane(maps: Maps*, start: i32, length: u16, dest: u16[]) -> !:
    if length == 0 or start <= 0:
        error(wl_def.invalid, "map plane is empty")
    let pos = (usize)start
    let compressed = (usize)length
    if pos + compressed > maps.raw.length:
        error(wl_def.invalid, "map plane overruns GAMEMAPS")
    let chunk = maps.raw[pos..<pos + compressed]
    if chunk.length < 2:
        error(wl_def.invalid, "map plane is truncated")
    let expanded = (i64)wl_def.u16le(chunk, 0)
    var carmack = try new u16[(usize)(expanded // 2)] --- in memory.heap
    defer free(carmack) in memory.heap
    try carmack_expand(chunk[2..<chunk.length], carmack, expanded)
    try rlew_expand(carmack[1..<carmack.length], dest, wl_def.map_area * 2, maps.rlew_tag)

test "RLEW expands a tagged run":
    var source: u16[5] = [0x1111, 0xabcd, 4, 0x2222, 0x3333]
    var dest: u16[6] = [0, 0, 0, 0, 0, 0]
    try rlew_expand(source, dest, 12, 0xabcd)
    assert(dest[0] == 0x1111)
    assert(dest[1] == 0x2222 and dest[2] == 0x2222 and dest[3] == 0x2222 and dest[4] == 0x2222)
    assert(dest[5] == 0x3333)