Random loot... How to do it?


(John Andersson) #1

Okay, so I got a class which I want to use when chests call for an item drop function.

How should I use FP.choose to randomly drop some loot? I have organized the weapons and armor in folders, and I’d love a function where FP.choose chooses a random weapon in that folder hierarchy. The weapons have item levels which I manually assign them, so it would be sweet to integrate this with the choosing. Any ideas?


(Elias) #2

First split your items into level categories. If you have reached a sertain level (or at least a level of the difficulty that seems alright to you) make the items with this specific level droppable. You can also make some items be dropped by a smaller chance than other, making them more diffucult to obtain.


(Ultima2876) #3

FP.choose just chooses a random element from an array. For it to work with your hierarchy you could add everything in the hierarchy (or a subset of it) to an array then choose from that. For example;

/**
  Function to pick a Loot-derived item from all items in the game, with a matching
  chestLevel/itemLevel. For example, chestLevel 5 only ever picks Loot with itemLevel 5.
*/
public function pickItem(chestLevel: int): Loot
{
  var allItems: Array = new Array; //array to contain all items
  var i: int; //avoid multiple definition warnings

  //now add all of each type (or folder, or whatever) to the all items list
  for(i = 0; i < weapons.Length; i++) { allItems.push(weapons[i]); }
  for(i = 0; i < armor.Length; i++) { allItems.push(armor[i]); }
  for(i = 0; i < potions.Length; i++) { allItems.push(potions[i]); }

  //now do some fun stuff to select possible items from the all items array
  var possibleItems: Array = new Array;
  for(i = 0; i < allItems.Length; i++)
  {
    if(allItems[i] is Loot) //only allow Loot
    {
      //make sure the Loot class has an 'itemLevel' public variable or getter
      if((allItems[i] as Loot).itemLevel == chestLevel)
      {
        //this item level is compatible with this chest. Allow the item!
        possibleItems.push(allItems[i]);
      }
    }

    //finally, choose the item from the possible items list!
    if(possibleItems.Length > 0)
    {
      return (FP.choose(possibleItems) as Loot);
    }
   return null; //there were no possible items. Handle this!
  }
}

Key thing is to make sure all Lootable items are derived from Loot and set their itemLevel variable appropriately.


(Elias) #4

Nice one Ultima, you have worked on a RPG game or somemething?


(Ultima2876) #5

Nope, though I’d love to sometime!


(Elias) #6

Best way to think about loot algorithms, is by spenting some time playing Dungeons & Dragons. :stuck_out_tongue: