loading...

Why does my mach-up world gen only generate "block 5", or generate every other block?

calin92540842 profile image PDS OWNER CALIN (Calin Baenen) ・1 min read

So I'm making a mach-up world generation for an MC clone in JS, and I have this code:

while(blocksPlaced < (16*16*16)) {
    let xIndex = 0;
    let yIndex = 0;
    for(let y of world.blocks) {
        for(let z of y) {
            xIndex = 0;
            for(let x of z) {
                let nextBlock = 
                    (
                        parseInt || window.parseInt
                    )(String(seed)[xIndex]);
                if(nextBlock === undefined) {
                    nextBlock = 
                        Nath.round(
                            Math.random()*Math.random()
                        )*10;
                } else {
                    nextBlock = 
                        Math.round(
                            nextBlock*Math.random()
                        );
                }
                console.log(yIndex);
                if(yIndex < 13) {
                    nextBlock = 5;
                }
                z[xIndex] = nextBlock || 0;
                blocksPlaced++;
                xIndex++;
            }
        }
        yIndex++;
    }
    blocksPlaced++;
    if(blocksPlaced < (16*16*16)) blocksPlaced = Infinity;
}

in my world creator, a basic function.
the main problem is these lines:

if(yIndex > 13) {
    nextBlock = 5;
}

for some reason, it sets every block to 5, no matter the y-index, and when I remove it, or do yIndex < 13, 5 doesn't appear at all, unless it's randomly generated by the seed.

So, why is this bug happening, and how can I fix it?
Thanks so much!
Cheers!

Posted on by:

calin92540842 profile

PDS OWNER CALIN (Calin Baenen)

@calin92540842

I am a 13 (as of Oct 30 of 2019) yr/o developer (I have been developing mini-projects for 4, years now, since I was 9), who makes projects in languages like: Java, HTML, Python 3, JS, CSS, and C#.

Discussion

markdown guide
 

First of all, what values does your console.log write out for yIndex? My only hunch is that it never has a value which fulfills the condition or always does. So maybe it is always less than 13

 

It does logs

(256) 0

and it does this until 14, in which it continues as normal, I put the same condition statement in console.log, but guess what!?
It said false when it should be false, and true when it should be true??

 

If you log inside the if statement that does that get run as well?

Could it be a issue where you are getting the correct value but it then gets overwritten?

Yes it does log inside the if statement, but when I put that log statement inside an if(yIndex < 13) it doesn't at all

Am I understanding you correctly that:

console.log(yIndex < 13)

correctly prints true when it should and false when it should be false?
And at the same time

if( yIndex < 13){
    console.log("here")
} 

this log does NOT get triggered.
Cause that sounds really really weird bug

Yes, you understood me correctly, since then, I've removed that, and replaced the world gen w/ a mach-up seed machine, though, it doesn't really change the problem, if you want, I can link you to a solo-learn of world.js to see. Though, the bug is weird, and I'm not exactly sure what's going wrong.
Thanks for your help.
Cheers.

Hello!
I have had some busy days. Did you solve the issue?
Otherwise, I think I could take a look at the code and see if I can see anything. I am quite new to JS myself but I can contribute a second pair of eyes at least. If you don't want to share the link here you could try to send it over dev.to chat-feature (never tried it before).

I'm not saying you have to, but that would be nice 😋.
You can visually see the point (quite literally), but I can't work out what's wrong.
It moved to JSFiddle, and here's the project if you'd be intrigued:
jsfiddle.net/Calin_B/qgko30eu/9/

So I rewrote some of the code when trying to figure out the issue. My guess at what the core problem is that [].repeat when used on new arrays copies the reference instead of creating a copy of the array. Because it is a reference all of the arrays contain the same information and as the last value written is "16." all rows get that value. Below I have pasted the function which contains all my changes, it was basically just the first creation of blocks and the looping I have changed. Now I instead create a new array during the looping. Try it out and see if it works as you want. You might need to clean it up a bit as well, I e.g. just hardcoded 16 for all layers.

let World = function(seed=0) {
    if(typeof seed === "string") seed = (
        parseInt || window.parseInt
    )(seed,36);
    seed = 
        seed && typeof seed === "number" ? seed :
        Math.round(Math.random()*100000.95);
    while(seed < 100000 && seed > -100000) seed *= 10;
    const generator = SeedGenerator(seed);
    const world = {
        seed: seed,
        generator: SeedGenerator(seed),
        blocks: []
    };
    console.log(world.blocks)
    let blocksPlaced = 0;

    for (let yIndex = 0; yIndex < 16; yIndex++) {
        const y = [];
        world.blocks[yIndex] = y;

        for (let zIndex = 0; zIndex < 16; zIndex++) {
            const z = [];
            y[zIndex] = z;

            for (let xIndex = 0; xIndex < 16; xIndex++) {
                let block = generator(0,15)+".";
                if(yIndex > 13){block = "16."};
                console.log([yIndex, zIndex, xIndex, block]);
                z[xIndex] = block;
            }
        }
    }
    return Object.freeze(world);
};

I fixed the issue, and it works perfectly well, thank you for your contribution.
I don't know what's so different to make it work (atleast when I try to eval it in my head, as if my brain had a JS interpreter in it), but it works, and that's one of the strangest JS bugs I've ever seen.

Thanks for your help, Fredrik.
Cheers.