Giter Site home page Giter Site logo

loottable.js's Introduction

LootTable.js

A loot drop table implementation in JavaScript.

LootTable is used to make a random choice among a weighted list of alternatives for item drops, map generation, and many other processes. There's a good overview of loot tables on Lost Garden.

Example

var loot = new LootTable();
loot.add('sword', 20);
loot.add('shield', 5);
loot.add('gold', 5);
loot.add(null, 1);
var item = loot.choose(); // most likely a sword, sometimes null

Weights are arbitrary, not percentages, and don't need to add up to 100. If one item has a weight of 2 and another has a weight of 1, the first item is twice as likely to be chosen. If quantity is given, then calls to choose() will only return that item while some are available. Each choose() that selects that item will reduce its quantity by 1.

Items can be anything, not just strings. They can be arrays, numbers, JSON data, null, functions... even another LootTable!

Building

npm install
npm run build

Global script example

include a <script src="LootTable.js"></script> tag on your page and you can use loot table as a global variable.

ES modules example

import LootTable from 'loot-table';

CommonJS (node) example

var LootTable = require('loot-table');

loottable.js's People

Contributors

jotson avatar

Stargazers

Maxim Kovkel avatar  avatar  avatar  avatar  avatar Vladimir Lila avatar Magnus Fredlundh avatar  avatar christophe avatar  avatar  avatar Anders Wiggers avatar Kenneth P. Barquero avatar Conor avatar Gerhard Jonsson avatar  avatar  avatar Marwan Hilmi avatar Andrey Pervakov avatar Aaron avatar Alec Burgdorf avatar Andreas avatar Dave Rau avatar Bastien Tanésie avatar Joohun, Maeng avatar Vitaly P. avatar keith avatar Fabien Bourgeois avatar Nina Recchia avatar David Hayes avatar Esteban Saiz avatar Kevin Isom avatar Jonathan Levstein avatar Piotr avatar Sean Bohan avatar Casaval avatar Carl Olsen avatar Stephan avatar Kristoffer Jetmundsen avatar Hannu Jaakkola avatar Thomas Viktil avatar Michael Burnley avatar Srikar Gudipati avatar Nikolas Poklitar avatar

Watchers

James Cloos avatar Carl Olsen avatar  avatar  avatar

loottable.js's Issues

Suggestion: Improve Performance by precomputing full table

to improve the performance of choose, you should rebuild an array of the weighted choices.

This will front load all performance cost to the construction of the loot pool (ideally, once) instead of it being rebuilt every loot choice.

Then choose can be a simple return this.cachedArray[randomIndex];

Update to support latest ECMAScript

I've been looking for a good loot table to implement into a Discord bot, and so far this seems like the best option. But I can see this was last updated 3 years ago and doesn't support the latest changes in ECMAScript. E.g. Replace "var" with let and const.

Is there any plans for a future update? Would love to be able to use this without any possible issues.

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.