JS - Item generator controlled by probability

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • inusm
    New Member
    • Jan 2013
    • 1

    #1

    JS - Item generator controlled by probability

    I'm pretty new to JS but I want to make a very basic RPG item generator that is controlled by probability. This is what I have come up with.

    I have a problem with the itemtype part. After the probability roll is done for itemtype, I want to randomly generate a group of elements under weapons, accessories, and armors.

    It should be something like Rare Sword, Uncommon staff, Common gloves, or Common Chest.

    How do I go about this?

    [code=javascript]
    var rarityNum = Math.floor( 1 + Math.random() * 100 );
    var itemNum = Math.floor( 1 + Math.random() * 100 );

    var rarity;
    if ( rarityNum > 75 ) { rarity = "rare "; }
    else if ( rarityNum > 65 ) { rarity = "uncommon "; }
    else { rarity = "common "; }

    var weapons =["sword","bow"," staff"];
    var armors =["chest","leggin gs","gloves"];

    var itemtype;
    if ( itemNum > 51 ) { itemtype = "weapons"; }
    else if ( itemNum > 40 ) { itemtype = "accessorie s"; }
    else { itemtype = "armors"; }

    document.write (rarity);
    document.write (itemtype);
    [/code]
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I'm confused by what your question is because, from the code you have, it looks like you know how to pick an item by probability. Perhaps not in the way I would do it but you're doing it nonetheless.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      I think the problem is not about choosing, but how to combine each chosen category.

      currently what is missing is that you have not chosen which of the weapons/accessories/armors to use.

      for that I’d also recomment to couple your data more, instead of using globals (not to mention that document.write( ) will shoot you in the foot sooner than later).

      Code:
      var itemStore = {
        rarity: ["rare", "uncommon", "common"],
        type: {
          weapons: ["sword", "bow", "staff"],
          armors:  ["chest", "leggings", "gloves"],
          accessories: [/* ... */]
      };
      then you only need to break down the probabilities to indices and you can combine.
      Code:
      // say you have randomly chosen:
      // rarity_index  1
      // item_type     "weapons"
      // item_index    2
      var chosenItem = itemStore.rarity[rarity_index] + " " + itemStore[item_type][type_index];
      
      // which would get you: "uncommon staff"

      Comment

      Working...