roar 0.1.0

A toolkit for procedural world generation (with a focus on voxel games)
Documentation
use crate::{
    bc::{self, BytecodeValue, Op},
    runtime::{FRAME_SIZE, Runtime},
};

#[test]
fn parse_bytecode() {
    let ops = bc::parse_bytecode(b"cpc+c*c/").expect("couldn't parse bytecode");
    assert_eq!(
        ops,
        vec![
            Op::LoadConst,
            Op::Perlin,
            Op::LoadConst,
            Op::Add,
            Op::LoadConst,
            Op::Mul,
            Op::LoadConst,
            Op::Div
        ]
    );

    let ops = bc::parse_bytecode(b"lcsgwpfrv+-*/nx^!><").expect("couldn't parse bytecode");
    assert_eq!(
        ops,
        vec![
            Op::Load,
            Op::LoadConst,
            Op::Set,
            Op::Get,
            Op::Swap,
            Op::Perlin,
            Op::Fbm,
            Op::RidgedMulti,
            Op::Value,
            Op::Add,
            Op::Sub,
            Op::Mul,
            Op::Div,
            Op::Min,
            Op::Max,
            Op::Pwr,
            Op::Neg,
            Op::RightShift,
            Op::LeftShift,
        ]
    );
}

#[test]
fn parse_input_section() {
    let input = "i214124i-36436f31.0f89.31f-443.12";
    let chars = input.chars();
    let chars: Vec<char> = chars.collect();
    let input_section = bc::parse_input_section(&chars).expect("couldn't parse input section");
    assert_eq!(
        input_section,
        vec![
            BytecodeValue::I64(214124),
            BytecodeValue::I64(-36436),
            BytecodeValue::F64(31.0),
            BytecodeValue::F64(89.31),
            BytecodeValue::F64(-443.12)
        ]
    );
}

const RUNTIME_OUTPUT: [f64; FRAME_SIZE] = [
    0.0,
    0.01415578785880611,
    0.028389650567788825,
    0.042769376361024423,
    0.05735206948510527,
    0.07218473059238575,
    0.0873048249154232,
    0.10274083822261344,
    0.11851282055502127,
    0.13463291774440578,
    0.15110589071244046,
    0.167929622551128,
    0.18509561338441025,
    0.20258946301097266,
    0.22039134132824353,
    0.23847644653758848,
    -0.01415578785880611,
    0.0,
    0.014233862730608744,
    0.028613588645982364,
    0.043196282129660736,
    0.05802894402427308,
    0.07314903980136496,
    0.08858505551254026,
    0.10435704152191863,
    0.12047714401990775,
    0.1369501243182913,
    0.1537738659266314,
    0.17093986940998696,
    0.18843373502794672,
    0.2062356331549778,
    0.224320762482089,
    -0.028389650567788825,
    -0.014233862730608744,
    -1.9166167708542174e-20,
    0.014379726359640226,
    0.02896242156152245,
    0.043795087652248516,
    0.058915191649738505,
    0.07435122146330093,
    0.09012322959584584,
    0.10624336462824178,
    0.12271639048581529,
    0.1395401934869941,
    0.1567062771740934,
    0.17420024492624553,
    0.19200227035447243,
    0.2100875554789018,
    -0.042769376361024423,
    -0.028613588645982364,
    -0.014379726359640221,
    0.0,
    0.014582697518573723,
    0.02941537113355454,
    0.04453549168680224,
    0.05997155176145806,
    0.07574360930318703,
    0.09186381902576782,
    0.10833695160102945,
    0.12516090063313545,
    0.14232717741721526,
    0.15982139348234273,
    0.1776237309188619,
    0.19570940049005991,
    -0.05735206948510527,
    -0.04319628212966074,
    -0.02896242156152245,
    -0.014582697518573724,
    1.533293416683374e-19,
    0.014832680833849959,
    0.029952822550155998,
    0.04538892612087168,
    0.06116105938852248,
    0.07728138832055728,
    0.09375469605225292,
    0.11057888971817192,
    0.12774549507217334,
    0.145240138895977,
    0.1630430191962805,
    0.18112936319042977,
    -0.07218473059238575,
    -0.05802894402427309,
    -0.04379508765224851,
    -0.029415371133554538,
    -0.014832680833849962,
    0.0,
    0.015120158862479253,
    0.030556309407322194,
    0.04632853466076061,
    0.06244901803011657,
    0.07892256210774082,
    0.09574709527029694,
    0.11291416607339136,
    0.13040942544154893,
    0.1482130966535344,
    0.16630043312301926,
    -0.0873048249154232,
    -0.07314903980136495,
    -0.058915191649738505,
    -0.04453549168680224,
    -0.029952822550155994,
    -0.015120158862479253,
    0.0,
    0.015436184944305487,
    0.031208500002781817,
    0.04732915298043304,
    0.06380297390753009,
    0.08062792129748735,
    0.09779557621000852,
    0.11529162411950142,
    0.13309632458876194,
    0.15118496874792708,
    -0.10274083822261344,
    -0.08858505551254028,
    -0.07435122146330092,
    -0.05997155176145807,
    -0.045388926120871684,
    -0.030556309407322194,
    -0.015436184944305488,
    0.0,
    0.015772376544593954,
    0.031893184346343056,
    0.048367289569497596,
    0.06519269067403602,
    0.0823610120228376,
    0.09985798530768285,
    0.11766391879408193,
    0.13575415438493096,
    -0.11851282055502127,
    -0.1043570415219186,
    -0.09012322959584584,
    -0.07574360930318705,
    -0.061161059388522475,
    -0.04632853466076061,
    -0.03120850000278182,
    -0.015772376544593954,
    0.0,
    0.016120908833222588,
    0.03259526140963052,
    0.049421106608911324,
    0.0665901237451684,
    0.0840881034126938,
    0.10189541616822945,
    0.11998746904971612,
    -0.13463291774440578,
    -0.12047714401990776,
    -0.10624336462824178,
    -0.09186381902576785,
    -0.07728138832055728,
    -0.062449018030116564,
    -0.04732915298043304,
    -0.03189318434634305,
    -0.016120908833222588,
    0.0,
    0.016474508276508825,
    0.03330072619594289,
    0.05047040028741647,
    0.06796939352121686,
    0.08577815327918002,
    0.10387216718374369,
    -0.15110589071244046,
    -0.1369501243182913,
    -0.12271639048581529,
    -0.10833695160102948,
    -0.09375469605225292,
    -0.07892256210774083,
    -0.0638029739075301,
    -0.048367289569497596,
    -0.03259526140963052,
    -0.01647450827650883,
    0.0,
    0.016826446045833887,
    0.03399665626561722,
    0.05149658005132935,
    0.06930675687676105,
    0.0874027712777587,
    -0.167929622551128,
    -0.1537738659266314,
    -0.1395401934869941,
    -0.12516090063313545,
    -0.11057888971817194,
    -0.09574709527029694,
    -0.08062792129748736,
    -0.06519269067403602,
    -0.049421106608911324,
    -0.03330072619594288,
    -0.016826446045833884,
    0.0,
    0.01717053107376547,
    0.03467119740379963,
    0.05248264635525174,
    0.07058057638893825,
    -0.18509561338441025,
    -0.17093986940998696,
    -0.1567062771740934,
    -0.14232717741721523,
    -0.1277454950721733,
    -0.11291416607339133,
    -0.09779557621000855,
    -0.08236101202283759,
    -0.06659012374516839,
    -0.05047040028741646,
    -0.03399665626561722,
    -0.017170531073765472,
    0.0,
    0.01750110261336616,
    0.035313548165970314,
    0.05341316655167924,
    -0.20258946301097266,
    -0.18843373502794677,
    -0.17420024492624553,
    -0.15982139348234273,
    -0.145240138895977,
    -0.13040942544154896,
    -0.11529162411950142,
    -0.09985798530768282,
    -0.08408810341269381,
    -0.06796939352121686,
    -0.051496580051329344,
    -0.03467119740379963,
    -0.017501102613366157,
    0.0,
    0.017813022179849564,
    0.03591394308202624,
    -0.22039134132824353,
    -0.20623563315497787,
    -0.19200227035447237,
    -0.1776237309188619,
    -0.1630430191962805,
    -0.14821309665353438,
    -0.13309632458876194,
    -0.1176639187940819,
    -0.10189541616822945,
    -0.08577815327918002,
    -0.06930675687676105,
    -0.05248264635525174,
    -0.03531354816597032,
    -0.01781302217984956,
    -2.4532694666933983e-18,
    0.018101664775738983,
    -0.23847644653758848,
    -0.224320762482089,
    -0.21008755547890182,
    -0.19570940049005997,
    -0.18112936319042974,
    -0.16630043312301926,
    -0.1511849687479271,
    -0.13575415438493096,
    -0.11998746904971612,
    -0.10387216718374369,
    -0.08740277127775868,
    -0.07058057638893826,
    -0.05341316655167924,
    -0.03591394308202624,
    -0.018101664775738983,
    4.906538933386797e-18,
];

#[test]
fn runtime() {
    let code = bc::parse_bytecode(b"c4c5lp").unwrap();
    let input_section = vec![
        BytecodeValue::F64(1.0 / 100.0),
        BytecodeValue::F64(1.0 / 100.0),
    ];
    let input_buffer = vec![BytecodeValue::I64(32521)];

    let runtime = Runtime::new(code, input_section, input_buffer);

    let mut frame = runtime.create_frame([0.0, 0.0]);

    assert_eq!(
        frame.run().map(|x| x.as_f64().map(|x| x.arr())),
        Ok(Ok(RUNTIME_OUTPUT))
    );
}

#[test]
fn curves() {
    let code = bc::parse_bytecode(b"c cccccu cccccu cccccu cg").unwrap();
    let input_section = vec![
        BytecodeValue::F64(18.13),

        BytecodeValue::I64(0),
        BytecodeValue::F64(0.0),
        BytecodeValue::F64(-2.0),
        BytecodeValue::F64(5.0),
        BytecodeValue::F64(-1.5),

        BytecodeValue::I64(0),
        BytecodeValue::F64(5.0),
        BytecodeValue::F64(-1.5),
        BytecodeValue::F64(10.0),
        BytecodeValue::F64(0.5),

        BytecodeValue::I64(0),
        BytecodeValue::F64(10.0),
        BytecodeValue::F64(0.5),
        BytecodeValue::F64(20.0),
        BytecodeValue::F64(1.0),

        BytecodeValue::I64(0),
    ];
    let input_buffer = vec![];

    let runtime = Runtime::new(code, input_section, input_buffer);

    let mut frame = runtime.create_frame([0.0, 0.0]);

    assert_eq!(
        frame.run().map(|x| x.as_f64().map(|x| x.unanymous_val())),
        Ok(Ok(Ok(0.9065)))
    );
}