How to create a loot system for a game...

I'm working on a loot-based dungeon crawler. For those that don't play games, that basically means the game involves exploring rooms or areas in search of items, be it weapons, armor, and so on. I have a system in place that randomizes what kind of "loot" is generated for each area, which is based on how far the player is in the game, and what their level is. In this particular case, the "loot" happens to be books. For each book that is generated, I need to identify the author.

In a spreadsheet I have a list of about 120 authors, with about 30 authors in 4 separate "Classes", which includes their name, a range of book values, and the years they wrote (used for setting date of the book)

I'd like to know the best method of saying "If book 1's author class is A..... go pick one of these class A guys at random."

The way I've handled this in the past is by using a switch. So I would get a random int i and say:

1
2
3
4
5
6
7
8
9
10
11
12
switch(i)
{
case 1: bookAuthor = "Johnny Babaganoush";
        bookValue = valueCalc(4000, 5000);
        bookYear = yearCalc(1941, 1965);
        break;

case 2: bookAuthor = "That guy over there";
        bookValue = valueCalc(2000, 3000);
        bookYear = yearCalc(1921, 1945);
        break;
}


... yadda yadda yadda. I'm still pretty new to c++, so I haven't learned anything about databases or referencing other files, but at the same time I don't want to go through the trouble of creating 4 massive 30-case switch functions if I'm going to learn a better technique anyway. So my question is: what way would you accomplish this? How would you set all of that information you've already established in code? Is using these switch classes totally amateurish?

edit: wording
Last edited on
If you could assign a numeric index value to all the books you could use that to pull it from a map or vector (that would probably involve making at least a common base class) then the call could be a simple function instead of an expansive switch statement.

Something akin to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class foo
{
  int level;
  string title;
};

vector<foo> available_foos; // assume this is filled in some way so the lower level objects are at the start and the higher level objects are at the end...

foo drop_foo(int player_level, int range)
{
  // assume I have 120 foo objects from levels 1-120 loaded into available_foos
  // range is some random integer that describes the range of available foo index numbers for this drop (+/- 4, +/- 2, etc)
  return available_foos.at(player_level + ( rand() % range));
}


So as level increases the dropped foo will be level-appropriate.
Last edited on
Cool, thanks for the response. I'll try re-writing it using vectors and see how it turns out.
One more question: where would you actually declare all of those classes? Within that function? Should they go in a header file?
Topic archived. No new replies allowed.