MZ Is wave function collapse possible?

Hi, I’m currently reworking a slot-based plugin for my fossil collecting RPG project, and have possibly found a solution to making adjacency rules work in my favor, I just need to know how I’d best go about it in JavaScript.

Here’s a video I found explaining how the algorithm works, for those who don’t know:

Credit goes to Martin Donald on YouTube.

I only need an example to help me continue the progress with the plugin. Thank you in advance.

Sincerely, GreenTree.
Seems like a specialized case of constraint programming, interesting.
I suggest you implement a sudoku solver first using that algorithm. I bet there are many examples out there on the internet you can use for help and reference. It will help you get into the algorithm and understand the knitty gritty details. Once that is done you can move over to your slot-based plugin.

Good luck with your plugin~
Of course, you can do it, and I can tell you why: I also know about this function and have already made some attempts to use that to generate Maps randomly. However, I eventually abandoned this project and focused on other techniques. Found out it doesn't work well with RPG Maker's Tilesets.

The video gives clear instructions on how your algorithm needs to work, the only thing you need to do is to program it. Try it a bit on your own and when you stuck you can ask us for further help.
Seems like a specialized case of constraint programming, interesting.
I suggest you implement a sudoku solver first using that algorithm. I bet there are many examples out there on the internet you can use for help and reference. It will help you get into the algorithm and understand the knitty gritty details. Once that is done you can move over to your slot-based plugin.

Good luck with your plugin~

Let me give you some context: Below is the image I'm using to create the holes in the fossil rock for my project:
CleaningLayers.png


Giving me a total of 30 unique tiles to work with including a blank tile to represent untouched pieces of rock. Each tile is 24x24 pixels. And each piece should appear according to where you have punctured said rock.

here is how the rock should look with the file I provide:
1679367444396.png



JavaScript:
//-----------------------------------------------------------------------------
    // FossilSprite
    //

    function FossilSprite() {
        this.initialize.apply(this, arguments);
    }

    FossilSprite.prototype = Object.create(Sprite.prototype);
    FossilSprite.prototype.constructor = FossilSprite;

    FossilSprite.prototype.initialize = function (bitmap) {
        Sprite.prototype.initialize.call(this, bitmap);
    };

    //-----------------------------------------------------------------------------
    // RockSprite
    //

    function RockSprite() {
        this.initialize.apply(this, arguments);
    }

    RockSprite.prototype = Object.create(Sprite.prototype);
    RockSprite.prototype.constructor = RockSprite;

    RockSprite.prototype.initialize = function (bitmap) {
        Sprite.prototype.initialize.call(this, bitmap);
    };

    //-----------------------------------------------------------------------------
    // RockDamageSprite
    //
    function RockDamageSprite() {
        this.initialize.apply(this, arguments);
    }

    RockDamageSprite.prototype = Object.create(Sprite.prototype);
    RockDamageSprite.prototype.constructor = RockDamageSprite;

    RockDamageSprite.prototype.initialize = function (bitmap) {
        Sprite.prototype.initialize.call(this, bitmap);
    };

    //------------------------------------------------------------------------------
    // Scene FossilCleaning
    //

    function Scene_FossilCleaning() {
        this.initialize(...arguments);
    }

    Scene_FossilCleaning.prototype = Object.create(Scene_MenuBase.prototype);
    Scene_FossilCleaning.prototype.constructor = Scene_FossilCleaning;

    $_$['Scene_FossilCleaning.prototype.isReady'] = Scene_FossilCleaning.prototype.isReady;
    Scene_FossilCleaning.prototype.isReady = function() {
        return $_$['Scene_FossilCleaning.prototype.isReady'].apply(this, arguments) && this._imagesLoaded >= this._imagesLoadCount;
    };

    Scene_FossilCleaning.prototype.imageLoaded = function () {
        this._imagesLoaded++;
    };

    Scene_FossilCleaning.prototype.initialize = function () {
        this._imagesLoaded = 0;
        this._imagesLoadCount = 1;
        var species = $gameVariables.value(1);
        var fossilPart = $gameVariables.value(2);
        var rockId = $gameVariables.value(3);
        $_$._fossilBitmap = ImageManager.loadBitmap("img/fossil/", "Fossil_" + fossilPart + "_" + species + "");
        $_$._fossilBitmap.addLoadListener(this.imageLoaded.bind(this));       
        $_$._rockBitmap = ImageManager.loadBitmap("img/rock/", "Rock_" + rockId + "");
        $_$._rockBitmap.addLoadListener(this.imageLoaded.bind(this));
        
        Scene_MenuBase.prototype.initialize.call(this);
    };

    Scene_FossilCleaning.prototype.create = function () {
        this.createBackground();
        this.createFossil();
        this.createRock();
        this.createRockDMG();
    };

    Scene_FossilCleaning.prototype.createFossil = function () {       
        this._fossilPieces = [];
        
        // sprite settings
        const cellSize = 24;
        const cols  = $_$._fossilBitmap.width  / cellSize;
        const rows  = $_$._fossilBitmap.height / cellSize;
    
        for (var i = 0; i < cols*rows; i++) {
            var row = Math.floor(i / cols);
            var col = i % cols;
            this._fossilPieces[i] = new FossilSprite($_$._fossilBitmap);
            this._fossilPieces[i].setFrame(col*cellSize, row*cellSize, cellSize, cellSize);
            this._fossilPieces[i].x = col * cellSize;
            this._fossilPieces[i].y = row * cellSize;
            this._fossilPieces[i].visible = false;
            this.addChild(this._fossilPieces[i]);
        };
    };

    Scene_FossilCleaning.prototype.createRock = function () {       
        this._rockPieces = [];
        
        // sprite settings
        const cellSize = 24;
        const cols  = $_$._rockBitmap.width  / cellSize;
        const rows  = $_$._rockBitmap.height / cellSize;
    
        for (var i = 0; i < cols*rows; i++) {
            var row = Math.floor(i / cols);
            var col = i % cols;
            this._rockPieces[i] = new RockSprite($_$._rockBitmap);
            this._rockPieces[i].setFrame(col*cellSize, row*cellSize, cellSize, cellSize);
            this._rockPieces[i].x = col * cellSize;
            this._rockPieces[i].y = row * cellSize;
            this._rockPieces[i].visible = false;
            this.addChild(this._rockPieces[i]);
        };
    };

    Scene_FossilCleaning.prototype.createRockDMG = function () {
        this._rockDmgPieces = [];
        const cellSize = 12;
        const cols = 28;
        const rows = 20;
        for (var i = 0; i < cols*rows; i++){
            this._rockDmgPieces[i] = {
            collapsed: false,
            options: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
            };
            var row = Math.floor(i / cols);
            var col = i % cols;
            this._rockDmgPieces[i] = new RockSprite($_$._rockBitmap);
            this._rockDmgPieces[i].setFrame(col*cellSize, row*cellSize, cellSize, cellSize);
            this._rockDmgPieces[i].x = col * cellSize;
            this._rockDmgPieces[i].y = row * cellSize;
            this._rockDmgPieces[i].visible = false;
            this.addChild(this._rockDmgPieces[i]);
        };
    
    };



    Scene_FossilCleaning.prototype.createBackground = function() {
    };

    Scene_FossilCleaning.prototype.start = function() {
        Scene_MenuBase.prototype.start.call(this);       
        FossilCleaningManager.playBgm();       
        this.makeFossil();
        this.makeRock();
        this.makeRockDamage();
    };

    Scene_FossilCleaning.prototype.makeFossil = function() {       
        // sprite settings
        const cellSize = 24;
        const cols  = $_$._fossilBitmap.width  / cellSize;
        const rows  = $_$._fossilBitmap.height / cellSize;
        var species = $gameVariables.value(1);
        var fossilPart = $gameVariables.value(2);
        
        if(species == 23 && fossilPart == 0){
            for (var i = 1; i < 4; i++) {
                this._fossilPieces[i].x = (cellSize*16)+(cellSize*i);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 4; i < 9; i++) {
                this._fossilPieces[i].x = (cellSize*12)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*1);
                this._fossilPieces[i].visible = true;
            };
            for (var i = 9; i < 14; i++) {
                this._fossilPieces[i].x = (cellSize*7)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*2);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 14; i < 17; i++) {
                this._fossilPieces[i].x = (cellSize*9)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*2);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 17; i < 29; i++) {
                this._fossilPieces[i].x = (cellSize*-3)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*3);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 29; i < 45; i++) {
                this._fossilPieces[i].x = (cellSize*-19)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*4);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 45; i < 67; i++) {
                this._fossilPieces[i].x = (cellSize*-38)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*5);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 67; i < 90; i++) {
                this._fossilPieces[i].x = (cellSize*-61)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*6);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 90; i < 97; i++) {
                this._fossilPieces[i].x = (cellSize*-85)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*7);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 97; i < 105; i++) {
                this._fossilPieces[i].x = (cellSize*-83)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*7);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 105; i < 110; i++) {
                this._fossilPieces[i].x = (cellSize*-81)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*7);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 110; i < 117; i++) {
                this._fossilPieces[i].x = (cellSize*-105)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*8);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 117; i < 120; i++) {
                this._fossilPieces[i].x = (cellSize*-103)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*8);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 120; i < 125; i++) {
                this._fossilPieces[i].x = (cellSize*-103)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*8);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 125; i < 128; i++) {
                this._fossilPieces[i].x = (cellSize*-101)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*8);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 128; i < 142; i++) {
                this._fossilPieces[i].x = (cellSize*-123)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*9);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 142; i < 150; i++) {
                this._fossilPieces[i].x = (cellSize*-123)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*9);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 150; i < 171; i++) {
                this._fossilPieces[i].x = (cellSize*-145)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*10);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 171; i < 192; i++) {
                this._fossilPieces[i].x = (cellSize*-166)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*11);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 192; i < 213; i++) {
                this._fossilPieces[i].x = (cellSize*-186)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*12);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 213; i < 235; i++) {
                this._fossilPieces[i].x = (cellSize*-206)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*13);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 235; i < 256; i++) {
                this._fossilPieces[i].x = (cellSize*-227)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*14);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 256; i < 267; i++) {
                this._fossilPieces[i].x = (cellSize*-239)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*15);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 267; i < 270; i++) {
                this._fossilPieces[i].x = (cellSize*-262)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*16);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 270; i < 282; i++) {
                this._fossilPieces[i].x = (cellSize*-255)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*16);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 282; i < 301; i++) {
                this._fossilPieces[i].x = (cellSize*-276)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*17);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 301; i < 319; i++) {
                this._fossilPieces[i].x = (cellSize*-295)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*18);
                this._fossilPieces[i].visible = true;
            };
    
            for (var i = 319; i < 335; i++) {
                this._fossilPieces[i].x = (cellSize*-313)+(cellSize*i);
                this._fossilPieces[i].y = (cellSize*19);
                this._fossilPieces[i].visible = true;
            };
        }
        
    };

    Scene_FossilCleaning.prototype.makeRock = function(){

        const cellSize = 24;
        const cols  = $_$._rockBitmap.width  / cellSize;
        const rows  = $_$._rockBitmap.height / cellSize;
        var shape = $gameVariables.value(3);

        if(shape == 11){
            for (var i = 1; i < 11; i++) {
                this._rockPieces[i].x = (cellSize*13)+(cellSize*i);
                this._rockPieces[i].visible = true;
            };
            for (var i = 12; i < 31; i++) {
                this._rockPieces[i].x = (cellSize*-6)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*1);
                this._rockPieces[i].visible = true;
            };
            for (var i = 32; i < 56; i++) {
                this._rockPieces[i].x = (cellSize*-27)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*2);
                this._rockPieces[i].visible = true;
            };
            for (var i = 57; i < 82; i++) {
                this._rockPieces[i].x = (cellSize*-52)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*3);
                this._rockPieces[i].visible = true;
            };
            for (var i = 83; i < 108; i++) {
                this._rockPieces[i].x = (cellSize*-78)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*4);
                this._rockPieces[i].visible = true;
            };
            for (var i = 109; i < 134; i++) {
                this._rockPieces[i].x = (cellSize*-104)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*5);
                this._rockPieces[i].visible = true;
            };
            for (var i = 134; i < 160; i++) {
                this._rockPieces[i].x = (cellSize*-130)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*6);
                this._rockPieces[i].visible = true;
            };
            for (var i = 160; i < 187; i++) {
                this._rockPieces[i].x = (cellSize*-157)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*7);
                this._rockPieces[i].visible = true;
            };
            for (var i = 187; i < 213; i++) {
                this._rockPieces[i].x = (cellSize*-184)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*8);
                this._rockPieces[i].visible = true;
            };
            for (var i = 213; i < 218; i++) {
                this._rockPieces[i].x = (cellSize*-209)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*9);
                this._rockPieces[i].visible = true;
            };
            for (var i = 218; i < 238; i++) {
                this._rockPieces[i].x = (cellSize*-208)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*9);
                this._rockPieces[i].visible = true;
            };
            for (var i = 238; i < 264; i++) {
                this._rockPieces[i].x = (cellSize*-234)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*10);
                this._rockPieces[i].visible = true;
            };
            for (var i = 264; i < 283; i++) {
                this._rockPieces[i].x = (cellSize*-261)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*11);
                this._rockPieces[i].visible = true;
            };
            for (var i = 283; i < 290; i++) {
                this._rockPieces[i].x = (cellSize*-260)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*11);
                this._rockPieces[i].visible = true;
            };
            for (var i = 290; i < 317; i++) {
                this._rockPieces[i].x = (cellSize*-287)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*12);
                this._rockPieces[i].visible = true;
            };
            for (var i = 317; i < 344; i++){
                this._rockPieces[i].x = (cellSize*-314)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*13);
                this._rockPieces[i].visible = true;
            };
            for (var i = 344; i < 371; i++){
                this._rockPieces[i].x = (cellSize*-341)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*14);
                this._rockPieces[i].visible = true;
            };
            for (var i = 371; i < 397; i++){
                this._rockPieces[i].x = (cellSize*-367)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*15);
                this._rockPieces[i].visible = true;
            };
            for (var i = 397; i < 406; i++){
                this._rockPieces[i].x = (cellSize*-393)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*16);
                this._rockPieces[i].visible = true;
            };
            for (var i = 406; i < 422; i++){
                this._rockPieces[i].x = (cellSize*-392)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*16);
                this._rockPieces[i].visible = true;
            };
            for (var i = 422; i < 450; i++){
                this._rockPieces[i].x = (cellSize*-419)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*17);
                this._rockPieces[i].visible = true;
            };
            for (var i = 450; i < 477; i++){
                this._rockPieces[i].x = (cellSize*-447)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*18);
                this._rockPieces[i].visible = true;
            };
            for (var i = 477; i < 490; i++){
                this._rockPieces[i].x = (cellSize*-474)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*19);
                this._rockPieces[i].visible = true;
            };
            for (var i = 490; i < 493; i++){
                this._rockPieces[i].x = (cellSize*-473)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*19);
                this._rockPieces[i].visible = true;
            };
            for (var i = 493; i < 496; i++){
                this._rockPieces[i].x = (cellSize*-469)+(cellSize*i);
                this._rockPieces[i].y = (cellSize*19);
                this._rockPieces[i].visible = true;
            };
        }

    };

    Scene_FossilCleaning.prototype.makeRockDamage = function(){
        const cols  = 28;
        const rows  = 20;

        for (var j = 0; j < cols*rows; j++){
            for (var i = 0; i < cols*rows; i++){
                var cell = this._rockDmgPieces[i + j];
            }
        }
    };



    //-----------------------------------------------------------------------------
    // GTS.FossilsMZ
    //
    
    GTS.FossilCleaning.Play = function(){
        FossilCleaningManager.saveBgmAndBgs();
        FossilCleaningManager.stopAudioOnStart();
        
        SceneManager.push(Scene_FossilCleaning);   
    };


})(GTS.FossilCleaning, this);

And finally the assets used to create both the rock and fossil:

Rock_11.png


Fossil_0_23.png


Plan on using the TouchInput functionality for the fossil cleaning system so I can simulate something along the lines of a drill piercing through the rock. All I need is a general idea of how it should work.

If you need me to explain further, let me know. Thank you

- GreenTree
There really is a Zeitgeist... every time I set my mind on something, I come to check if someone mentioned it on this forum and I find a recent post about it (this one being ~1 month old, it's pretty recent, right?).

With a friend, we're working on a game that heavily relies on the ability to generate random maps. We were working with @Aerosys 's awesome plugin (which I sincerely recommend) but for licensing reasons, we are to replacing all of our plugins with our open source ones.

I was also looking at the wave function collapse method as the best contendant. I've started implementing it but it is going to prove tricky.

Original thread: https://forums.rpgmakerweb.com/threads/is-wave-function-collapse-possible.155907/