GithubHelp home page GithubHelp logo

knagaitsev / cave-automata-2d Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hughsk/cave-automata-2d

0.0 0.0 0.0 22 KB

Generate 2D cave layouts in JavaScript.

License: Other

JavaScript 100.00%

cave-automata-2d's Introduction

cave-automata-2d stable

Generate 2D cave layouts in JavaScript: an implementation of the first half of this paper using ndarrays.

Installation

npm install cave-automata-2d

Usage

iterate = cave(ndarray[, opts])

Takes an ndarray and prepares it for cave generation. Options include:

  • density: The probability of each filled cell being set to 1 ("on"), as opposed to 0 ("off"). Defaults to 0.5.
  • threshold: The minimum surrounding cells required for a cell to survive an iteration. Defaults to 5.
  • hood: The maximum distance of neighboring cells to check. Defaults to 1.
  • fill: By default, the ndarray is populated with random values. If you need to disable this, pass fill: false.
  • iterations: The amount of iterations to apply immediately. Defaults to 0.
  • border: If truthy, will include a solid border around the results. Defaults to 1.
  • buffer: An ndarray buffer to use between iterations, which must be the same shape as the original array. If none is supplied, one will be created.

Note that this function will modify the array directly.

iterate([iterations])

The above function will return an "iterate" function, which will step the simulation forward a specific number of ticks. This will generate the cave layout - the best amount of iterations to run varies depending on the parameters above.

Because of the way the module works under the hood, there's less overhead by iterating an even number of times.

Example

var cave = require('cave-automata-2d')
  , ndarray = require('ndarray')
  , width = 50
  , height = 50

var grid = ndarray.zeros([ width, height ])

// Fill the grid with random points,
// returning an "iterate" method.
var iterate = cave(grid, {
    density: 0.5
  , threshold: 5
  , hood: 1
  , fill: true
})

// Iterate the grid five times to generate
// a smooth-ish layout.
iterate(5)

You can see the generator at work by running eyeball-test.js in Node, which will spit out something similar to this:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@...........@@@@@..@@@@@...@@@@@@@@@@@@@@@@@@@@@@@...@@@@@@@@@@@@@@.....@@@@@@@@@@@@@@@@@.....@@@@@@
@@@@@@@@@@@@@@@@@@@@@............@@@....@@@.....@@@@.....@@@..@@@@@@@.....@@@@@..@@@@@.......@@@@@@@@@.............@@@@@
@@@@@@@@@@@@@@..@@@@@............@@@....@@@......@@............@@@@@@.....@@@@....@@@@........@@@..@@..............@@@@@
@@@@@@@@@@@@@....@@@..............@.............................@@@@@......@@......@@..............................@@@@@
@@@@@@@@@@@@.....................................................@@@..............................................@@@@@@
@@@@@@@...........................................................................................................@@@@@@
@@@@@@.................@@@.........................................................................................@@@@@
@@@@@.................@@@@@.................................@@......................................................@@@@
@@@@..............@@@@@@@@@@...............................@@@@..............................................@@@....@@@@
@@@..............@@@@@@@@@@@@@............................@@@@@..............................................@@@@..@@@@@
@@@............@@@@@@@@...@@@@@.....@@....................@@@@..................@@..........................@@@@@@@@@@@@
@@@...........@@@@@@@@......@@@@...@@@@....................@@@.................@@@@........................@@@@@@@@@@@@@
@@@........@@@@@@@@@@........@@@@@@@@@@@...................@@@@..@@@...........@@@@........................@@@@@@@@@@@@@
@@@.......@@@@@@@@............@@@@@@@@@@@@.................@@@@@@@@@@...........@@..........................@@@.....@@@@
@@@........@@@@..........@@...@@@@@@@@@@@@@.................@@@@@@@@@................................................@@@
@@@@........@@..........@@@@..@@@@@@@@@@@@@...................@@..@@@................................................@@@
@@@@@@..................@@@@..@@@@@@@@@@@@......@@.................@@@...............................................@@@
@@@@@@@................@@@@@@.@@@@@@@@@........@@@@.................@@@................@@............................@@@
@@@@@@@............@@@@@@@@@@..@@@@@@@........@@@@@@@@@@............@@@@..............@@@@...................@@......@@@
@@@@@@@............@@@@@@@@@@...@@@@@@........@@@@@@@@@@@..........@@@@@@.............@@@@..................@@@@....@@@@
@@@@@@..............@@@@@@@@@@.....@@@........@@@@@@@@@@@@.........@@@@@@@@..........@@@@@@.................@@@@....@@@@
@@@@@@....................@@@@......@@@........@@@@@@@@@@@@.........@@@@@@@@@.......@@@@@@@@.................@@@....@@@@
@@@@@@@....................@@@......@@@@........@@@@@@@@@@@@.............@@@@@......@@@@@@@@@................@@@....@@@@
@@@@@@@@@@..................@@......@@@@@.......@@@@@@@@@@@@..............@@@@@.....@@@@@@@@@................@@@....@@@@
@@@@@@@@@@@.................@@......@@@@@@......@@@@@@@@@@@@@..............@@@@.....@@@@@@@@@................@@@@..@@@@@
@@@@@@@@@@@.................@@@....@@@@@@@.......@@@@@@@@@@@@..............@@@@@....@@@@@@@@...........@@....@@@@@@@@@@@
@@@@@@@@@@@@................@@@@..@@@@@@@..............@@@@@@...............@@@@@...@@@@@@@@......@@@@@@@.....@@@@@@@@@@
@@@@@@@@@@@@...............@@@@@@@@@@@@@................@@@@@@..@@@...@@.....@@@.....@@@@@@@.....@@@@@@@@.....@@@@@@@@@@
@@@@@@@@@@@@..........@@@@@@@@@@@@@@@@@@.................@@@@@@@@@@@@@@@@..............@@@@@@....@@@@@@@@....@@@@@@@@@@@
@@@@..@@@@@@.........@@@@@@@..@@@@@@@@@@....................@@@@@@@@@@@@@.................@@@@....@@@@@@...@@@@@@@@@@@@@
@@@....@@@@.........@@@@@@@....@@@@@@@@@.....................@@@@@@@@@@@@@.................@@@@...@@@@@...@@@@@@@@@@@@@@
@@@....@@@........@@@@@@@@.....@@@@@@@@@......................@@@@@.....@@@.................@@@@@@@@@@....@@@@@@@@@@@@@@
@@@@..@@@@.......@@@@@@........@@@@@@@@@........@@............@@@@.......@@@................@@@@@@@@@......@@@@@@@..@@@@
@@@@@@@@@@.......@@@@@@.........@@@@@@@........@@@@..........@@@@@........@@@..............@@@@@@@@@@................@@@
@@@@@@@@@@@......@@@@@@.........@@@@@@........@@@@@......@@@@@@@@@........@@@@....@@@@@@..@@@@@@@@@@@@...............@@@
@@@@@@@@@@@@....@@@@@@@@@@.....@@@@@@@....@@@@@@@@@.....@@@@@@@@@@.......@@@@@@..@@@@@@@@@@@@@@@@@@@@@@@@@..........@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@..@@@@@@@@@@@...@@@@@@@@@@@@..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Neat, huh?

cave-automata-2d's People

Contributors

hughsk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.