npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gacha

v0.2.1

Published

Roguelike item drop distribution calculator

Downloads

18

Readme

Node Gacha

Node Gacha (named after the Japanese onomatopoeia for the sound made by toy machines) aims to be a module providing different methods for calculating randomness in games. Randomness can include everything from enemy encounters to item drop rates, particularly useful for RPG's and Roguelikes.

Eventually we'll offer a few different systems, but for now we only support "roguelike".

npm install gacha

Roguelike

Purpose: Given a listing of items (or enemies) with variable drop rates, determine the odds of them being dropped on a particular level.

Diagram

Diagram

The above diagram gives a visualization of how Roguelike works. Each item has an ideal level (center of curve), a spread (the radius of the curve), and a weight (makes it taller). After passing data through the roguelike function, a list of probabilities of each item spawning is provided on a per-level basis.

Code Sample

var gacha = require('gacha');

// items must be an object where each property contains level, weight, and spread.
var items = {
  "101": {
    "name": "Knife",
    "type": "weapon",
    "level": 1,
    "weight": 1.0,
    "spread": 1
  },
  "102": {
    "name": "Sword",
    "type": "weapon",
    "level": 2,
    "weight": 1.0,
    "spread": 2
  },
  "103": {
    "name": "Iron Buckler",
    "type": "shield",
    "level": 3,
    "weight": 3.0,
    "spread": 3
  },
  "104": {
    "name": "Potion",
    "type": "heal",
    "level": 3,
    "weight": 1.0,
    "spread": 2
  }
};

var equip = gacha.roguelike(items, function(item) {
  // Optional filter callback, defaults to `return true;`
  return item.type !== 'heal';
});

console.log(equip); // Shown below

// Which item should we spawn on level 3?
var lvl3 = equip[3];
var strata = Math.random() * lvl3.total;
var item = null;

for (var i = 0; i < lvl3.strata.length; i++) {
  if (strata <= lvl3.strata[i]) {
    var id = lvl3.lookup[i];
    item = items[id];
    break;
  }
}

console.log("Receiving item for level three:", item);

Resulting equip object

{
  "0": {
    "lookup": [ "101", "102", "103" ],
    "strata": [ 0.24197072451914337, 0.3457475988742921, 0.4485344854101383 ],
    "total": 0.4485344854101383
  },
  "1": {
    "lookup": [ "101", "102", "103" ],
    "strata": [ 0.3989422804014327, 0.6186379251352939, 0.8551480729542124 ],
    "total": 0.8551480729542124
  },
  "2": {
    "lookup": [ "101", "102", "103" ],
    "strata": [ 0.24197072451914337, 0.5240655162930214, 0.9140048277385038 ],
    "total": 0.9140048277385038
  },
  "3": {
    "lookup": [ "102", "103" ],
    "strata": [ 0.2196956447338612, 0.6803545106956419 ],
    "total": 0.6803545106956419
  },
  "4": {
    "lookup": [ "102", "103" ],
    "strata": [ 0.10377687435514871, 0.493716185800631 ],
    "total": 0.493716185800631
  },
  "5": {
    "lookup": [ "103" ],
    "strata": [ 0.2365101478189184 ],
    "total": 0.2365101478189184
  },
  "6": {
    "lookup": [ "103" ],
    "strata": [ 0.1027868865358462 ],
    "total": 0.1027868865358462
  }
}