Skip to content

Commit

Permalink
Added more vanilla density function examples and fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mschae23 committed Jul 21, 2022
1 parent 7e4dd3b commit dcc0396
Show file tree
Hide file tree
Showing 26 changed files with 544 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "density_function_lang"
version = "2.0.0"
version = "2.0.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Debug for Stmt {

#[derive(Clone, PartialEq)]
pub enum Expr {
ConstantFloat(f32),
ConstantFloat(f64),
ConstantInt(i32),
ConstantString(String),
Identifier(Token),
Expand Down Expand Up @@ -152,7 +152,7 @@ pub struct Module {

#[derive(Clone)]
pub enum JsonElement {
ConstantFloat(f32),
ConstantFloat(f64),
ConstantInt(i32),
ConstantString(String),

Expand Down
36 changes: 31 additions & 5 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,7 @@ impl Compiler {
self.error_at(*name.start(), "Member functions are not implemented yet", false);
}

let current_module = self.current_module();
let current_module = current_module.borrow();

let template = match current_module.templates.iter().find(|template| *template.borrow().name == *name.source() && template.borrow().args.len() == args.len()) {
let template = match self.find_template(&name, args.len()) {
Some(template) => template,
None => {
self.error_at(*name.start(), &format!("Unresolved function call: {}", name.source()), false);
Expand All @@ -330,17 +327,46 @@ impl Compiler {

let template_expr = template_borrow.expr.clone();
drop(template_borrow);
drop(current_module);

self.current_module.append(&mut template_current_modules);

// TODO Change path to the template's original file path for better error messages

let expr = self.compile_expr(template_expr.clone());
self.template_args.truncate(self.template_args.len().saturating_sub(arg_count));
self.current_module.truncate(self.current_module.len().saturating_sub(template_current_modules_count));

expr
}

fn find_template(&mut self, name: &Token, arg_count: usize) -> Option<Rc<RefCell<Template>>> {
let mut module_index: isize = self.current_module.len() as isize - 1;

while module_index >= -1 {
let module = Rc::clone(if module_index >= 0 {
&self.current_module[module_index as usize]
} else { &self.top_level_module });

if let Some(template) = Self::find_template_on(&module, &name, arg_count) {
return Some(template);
}

module_index -= 1;
}

None
}

fn find_template_on(module: &Rc<RefCell<Module>>, name: &Token, arg_count: usize) -> Option<Rc<RefCell<Template>>> {
for template in &module.borrow().templates {
if *template.borrow().name == *name.source() && template.borrow().args.len() == arg_count {
return Some(Rc::clone(template));
}
}

None
}

fn current_module(&self) -> Rc<RefCell<Module>> {
self.current_module.last().map(|module| Rc::clone(&module)).unwrap_or_else(|| Rc::clone(&self.top_level_module))
}
Expand Down
14 changes: 11 additions & 3 deletions src/compiler/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,17 @@ impl<'source> Lexer<'source> {
'+' => Ok(if self.expect('=')? { self.make_token(TokenType::PlusAssign) } else {
self.make_token(TokenType::Plus)
}),
'-' => Ok(if self.expect('=')? { self.make_token(TokenType::MinusAssign) } else {
self.make_token(TokenType::Minus)
}),
'-' => {
let next = self.peek()?;

if next >= '0' && next <= '9' {
self.scan_number()
} else {
Ok(if self.expect('=')? { self.make_token(TokenType::MinusAssign) } else {
self.make_token(TokenType::Minus)
})
}
},
'*' => Ok(if self.expect('=')? { self.make_token(TokenType::MultiplyAssign) } else {
self.make_token(TokenType::Multiply)
}),
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl<'source> Parser<'source> {
fn parse_primary(&mut self) -> Expr {
if self.matches(TokenType::Float) {
let number = self.previous.clone();
let parsed: Result<f32, _> = number.source().parse();
let parsed: Result<f64, _> = number.source().parse();

return match parsed {
Ok(number) => Expr::ConstantFloat(number),
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl JsonWriter {
pub fn write_element(&mut self, element: &JsonElement, out: &mut impl Write) -> Result<(), std::io::Error> {
match element {
JsonElement::ConstantFloat(value) =>
if *value as i32 as f32 == *value { write!(out, "{:.1}", value) } else { write!(out, "{}", value) },
if *value as i32 as f64 == *value { write!(out, "{:.1}", value) } else { write!(out, "{:.}", value) },
JsonElement::ConstantInt(value) => write!(out, "{}", value),
JsonElement::ConstantString(value) => write!(out, "\"{}\"", value),

Expand Down
75 changes: 71 additions & 4 deletions test/main.densityfunction
Original file line number Diff line number Diff line change
@@ -1,8 +1,75 @@
include "minecraft.densityfunction";
import df.*;

// Density functions
function shift_x = flat_cache(cache_2d(shift_a("minecraft:offset")));
function shift_z = flat_cache(cache_2d(shift_b("minecraft:offset")));
function y = y_clamped_gradient(-4064, 4062, -4064.0, 4062.0);
function zero = 0.0;

function sloped_cheese = 4.0 * quarter_negative(
("minecraft:overworld/depth" + "minecraft:overworld/jaggedness" * half_negative(noise("minecraft:jagged", 1500.0, 0.0)))
* "minecraft:overworld/factor") + "minecraft:overworld/base_3d_noise";
module overworld {
function base_3d_noise = old_blended_noise(0.25, 0.125, 80.0, 160.0, 8.0);

function continents = flat_cache(shifted_noise("minecraft:shift_x", 0.0, "minecraft:shift_z", 0.25, 0.0, "minecraft:continentalness"));
function depth = y_clamped_gradient(-64, 320, 1.5, -1.5) + "minecraft:overworld/offset";
function erosion = flat_cache(shifted_noise("minecraft:shift_x", 0.0, "minecraft:shift_z", 0.25, 0.0, "minecraft:erosion"));

// function offset = flat_cache(cache_2d(...));
// function factor = flat_cache(cache_2d(...));
// function jaggedness = flat_cache(cache_2d(...));

function ridges = flat_cache(shifted_noise("minecraft:shift_x", 0.0, "minecraft:shift_z", 0.25, 0.0, "minecraft:ridge"));
function ridges_folded = -3.0 * (-0.3333333333333333 + abs(-0.6666666666666666 + abs("minecraft:overworld/ridges")));

function sloped_cheese = 4.0 * quarter_negative(
("minecraft:overworld/depth" + "minecraft:overworld/jaggedness" * half_negative(noise("minecraft:jagged", 1500.0, 0.0)))
* "minecraft:overworld/factor") + "minecraft:overworld/base_3d_noise";

module caves {
function entrances = cache_once(min(
(0.37 + noise("minecraft:cave_entrance", 0.75, 0.5)) + y_clamped_gradient(-10, -30, 0.3, 0.0),
"minecraft:overworld/caves/spaghetti_roughness_function" +
clamp(
max(
weird_scaled_sampler(cache_once(noise("minecraft:spaghetti_3d_rarity", 2.0, 1.0)), "minecraft:spaghetti_3d_1", "type_1"),
weird_scaled_sampler(cache_once(noise("minecraft:spaghetti_3d_rarity", 2.0, 1.0)), "minecraft:spaghetti_3d_2", "type_1")
) + (
-0.0765 + -0.011499999999999996 * noise("minecraft:spaghetti_3d_thickness", 1.0, 1.0)
), -1.0, 1.0)
));

function noodle = range_choice(
interpolated(range_choice("minecraft:y", -60.0, 321.0, noise("minecraft:noodle", 1.0, 1.0), -1.0)),
-1000000.0, 0.0,
64.0,
interpolated(range_choice("minecraft:y", -60.0, 321.0, -0.07500000000000001 + -0.025 * noise("minecraft:noodle_thickness", 1.0, 1.0), 0.0))
+ 1.5 * max(
abs(interpolated(range_choice("minecraft:y", -60.0, 321.0, noise("minecraft:noodle_ridge_a", 1.0, 1.0), 0.0))),
abs(interpolated(range_choice("minecraft:y", -60.0, 321.0, noise("minecraft:noodle_ridge_b", 1.0, 1.0), 0.0)))
)
);

function pillars = cache_once(
(2.0 * noise("minecraft:pillar", 25.0, 0.3) + (-1.0 + -1.0 * noise("minecraft:pillar_rareness", 1.0, 1.0)))
* cube(0.55 + 0.55 * noise("minecraft:pillar_thickness", 1.0, 1.0))
);

function spaghetti_2d_thickness_modulator = cache_once(
-0.95 + -0.35000000000000003 * noise("minecraft:spaghetti_2d_thickness", 2.0, 1.0)
);

function spaghetti_2d = clamp(
max(
weird_scaled_sampler(noise("minecraft:spaghetti_2d_modulator", 2.0, 1.0), "minecraft:spaghetti_2d", "type_2")
+ 0.083 * "minecraft:overworld/caves/spaghetti_2d_thickness_modulator",
cube(abs(0.0 + 8.0 * noise("minecraft:spaghetti_2d_elevation", 1.0, 0.0) +
y_clamped_gradient(-64, 320, 8.0, -40.0)) + "minecraft:overworld/caves/spaghetti_2d_thickness_modulator")
),
-1.0, 1.0
);

function spaghetti_roughness_function = cache_once(
(-0.05 + -0.05 * noise("minecraft:spaghetti_roughness_modulator", 1.0, 1.0)) *
(-0.4 + abs(noise("minecraft:spaghetti_roughness", 1.0, 1.0)))
);
};
};
9 changes: 9 additions & 0 deletions test/minecraft.densityfunction
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ module df {
template blend_offset() = simple_0("minecraft:blend_offset");
template blend_density(arg) = simple_1("minecraft:blend_density", arg);

template beardifier() = simple_0("minecraft:beardifier");

template old_blended_noise(scale_xz, scale_y, factor_xz, factor_y, smear_scale_multiplier) = {
"type": "minecraft:old_blended_noise",
"xz_scale": scale_xz, "y_scale": scale_y,
"xz_factor": factor_xz, "y_factor": factor_y,
"smear_scale_multiplier": smear_scale_multiplier
};

template clamp(arg, min, max) = { "type": "minecraft:clamp", "input": arg, "min": min, "max": max };

template constant(value) = simple_0("minecraft:constant", value);
Expand Down
8 changes: 8 additions & 0 deletions test/target/overworld/base_3d_noise.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "minecraft:old_blended_noise",
"xz_scale": 0.25,
"y_scale": 0.125,
"xz_factor": 80.0,
"y_factor": 160.0,
"smear_scale_multiplier": 8.0
}
65 changes: 65 additions & 0 deletions test/target/overworld/caves/entrances.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"type": "minecraft:cache_once",
"argument": {
"type": "minecraft:min",
"argument1": {
"type": "minecraft:add",
"argument1": {
"type": "minecraft:add",
"argument1": 0.37,
"argument2": {
"type": "minecraft:noise",
"noise": "minecraft:cave_entrance",
"xz_scale": 0.75,
"y_scale": 0.5
}
},
"argument2": {
"type": "minecraft:y_clamped_gradient",
"from_y": -10,
"to_y": -30,
"from_value": 0.3,
"to_value": 0.0
}
},
"argument2": {
"type": "minecraft:add",
"argument1": "minecraft:overworld/caves/spaghetti_roughness_function",
"argument2": {
"type": "minecraft:clamp",
"input": {
"type": "minecraft:add",
"argument1": {
"type": "minecraft:max",
"argument1": {
"type": "minecraft:weird_scaled_sampler",
"noise": "minecraft:spaghetti_3d_1",
"rarity_value_mapper": "type_1"
},
"argument2": {
"type": "minecraft:weird_scaled_sampler",
"noise": "minecraft:spaghetti_3d_2",
"rarity_value_mapper": "type_1"
}
},
"argument2": {
"type": "minecraft:add",
"argument1": -0.0765,
"argument2": {
"type": "minecraft:mul",
"argument1": -0.011499999999999996,
"argument2": {
"type": "minecraft:noise",
"noise": "minecraft:spaghetti_3d_thickness",
"xz_scale": 1.0,
"y_scale": 1.0
}
}
}
},
"min": -1.0,
"max": 1.0
}
}
}
}
94 changes: 94 additions & 0 deletions test/target/overworld/caves/noodle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"type": "minecraft:range_choice",
"input": {
"type": "minecraft:interpolated",
"argument": {
"type": "minecraft:range_choice",
"input": "minecraft:y",
"min_inclusive": -60.0,
"max_inclusive": 321.0,
"when_in_range": {
"type": "minecraft:noise",
"noise": "minecraft:noodle",
"xz_scale": 1.0,
"y_scale": 1.0
},
"when_out_of_range": -1.0
}
},
"min_inclusive": -1000000.0,
"max_inclusive": 0.0,
"when_in_range": 64.0,
"when_out_of_range": {
"type": "minecraft:add",
"argument1": {
"type": "minecraft:interpolated",
"argument": {
"type": "minecraft:range_choice",
"input": "minecraft:y",
"min_inclusive": -60.0,
"max_inclusive": 321.0,
"when_in_range": {
"type": "minecraft:add",
"argument1": -0.07500000000000001,
"argument2": {
"type": "minecraft:mul",
"argument1": -0.025,
"argument2": {
"type": "minecraft:noise",
"noise": "minecraft:noodle_thickness",
"xz_scale": 1.0,
"y_scale": 1.0
}
}
},
"when_out_of_range": 0.0
}
},
"argument2": {
"type": "minecraft:mul",
"argument1": 1.5,
"argument2": {
"type": "minecraft:max",
"argument1": {
"type": "minecraft:abs",
"argument": {
"type": "minecraft:interpolated",
"argument": {
"type": "minecraft:range_choice",
"input": "minecraft:y",
"min_inclusive": -60.0,
"max_inclusive": 321.0,
"when_in_range": {
"type": "minecraft:noise",
"noise": "minecraft:noodle_ridge_a",
"xz_scale": 1.0,
"y_scale": 1.0
},
"when_out_of_range": 0.0
}
}
},
"argument2": {
"type": "minecraft:abs",
"argument": {
"type": "minecraft:interpolated",
"argument": {
"type": "minecraft:range_choice",
"input": "minecraft:y",
"min_inclusive": -60.0,
"max_inclusive": 321.0,
"when_in_range": {
"type": "minecraft:noise",
"noise": "minecraft:noodle_ridge_b",
"xz_scale": 1.0,
"y_scale": 1.0
},
"when_out_of_range": 0.0
}
}
}
}
}
}
}
Loading

0 comments on commit dcc0396

Please sign in to comment.