What is a good Java data structure for storing RPG game elements?

I am creating an RPG dungeon game in Java and I am stuck in creating a data structure.

I have many Thing objects that I can copy to fill the dungeon. For example, there is a Thing object for bread and a Thing Thing object, a Thing chain Thing object and a Thing monster (s), etc. I want to store them in a central library, and then be able to retrieve an object using specific queries. I want to save them using the following fields:

int minLevel int maxLevel double probability int[] types 

Thus, a rusty sword would have minLevel 1, a maxLevel of 3, a probability rarity (3%) and [type.SWORD,type.WEAPON,type.ITEM,TYPE.EQUIP] . The best sword would have minLevel 2, maxLevel 10, rarity (1%).

Then I want to get a random type.SWORD from the library and say that I am at level 3. I should get a rusty sword more often than the best sword based on their probabilities. If I got type.SWORD from a library requesting level 10, I would only return the best sword.

Hope this makes sense.

EDIT At the initialization stage, all basic objects will be created. Things like affordable weapons, armor, food, potions, wands, all the main possible things that have a unique graphic tile in the game. Then, when I want to place the object somewhere, I just make a copy of one of the available things, slightly adjust the statistics and put it into the world. Actual elements are all subclasses of the Thing root class, such as Creature, Item, Equip (extends Item), Weapon (extends Equip), Armor (extends Equip), Food (extends Item), etc. But I want to note they are different in the library database, I want to use additional tags, such as type.RARE, type.ARTIFACT, type.CURSED, so I want to add additional tags in addition to the class.

The game uses LIBGDX for Android and Applet. I use the free Rltile suite, which has thousands of good snippets. I will use Pubnub or Google App Engine to support multi-user support.

+7
source share
2 answers

I can provide three answers:

  • write your own Library that stores these things in Map using custom methods. so you can have a Map<Type,List<Object>> , which stores lists of things by type and then a method that takes a type, extracts the list from the map and selects something in probability (it's easy to do this - you're just a little up to the probabilities , generate a random number between 0 and the sum, and then go through the list, subtracting the probability of the element from your random value until it is negative - you will return the element that made it negative [*]). you can also filter the level list first, for example.

    if you have a real combination of different things and donโ€™t want to base it on types, then another option (slower but more flexible) is to put everything in a list and then filter according to your needs. a good way to do this with guava - see Iterables.filter and Predicate at https://code.google.com/p/guava-libraries/ . you can provide an interface that uses a predicate and returns a random selection of what remains after filtering. predicates are easy to build inline with anonymous classes - see examples at https://code.google.com/p/guava-libraries/wiki/FunctionalExplained#Functions_and_Predicates

  • paste it all into the database. I may be too busy and people in games never do this, but it seems to me that a small built-in database like sqlite or H2 is perfect for this. you can select things using SQL queries (this is already a long answer, so I wonโ€™t give more details here.)

  • change your design. what you describe is not very OO. instead of having types, your things can implement interfaces. therefore, the Weapon interface would have getMinLevel() , for example. and then using this design, use a database with ORM (sleep mode).

what you do is pretty ambitious and I suspect it's more about studying than about anything else (criticism is not intended - this is how I learn things, doing things, so I just assume that you look like me) . so choose what you like best.

[*] this assumes that you always want to return something. if the probabilities are normal and you want to return nothing, select the initial value from 0-1 (or 0-100 when using percentages). and if nothing negates the value while going through the list, do not return anything.

+5
source

The easiest way is to place all the objects in one large array and use re-fetching to select the object.

The random element selection procedure is very simple:

  • Choose a random number from 0 to size ArrayList
  • Get an object from this index from the library
  • If the object does not meet any criteria you specify (for example, "is a type .SWORD or type.MACE?"), Go back to the top
  • If the subject is outside the minimum or maximum level, return to the beginning.
  • If the object has a rarity of less than 100%, create a random number from 0 to 100%. If a random number exceeds the rarity of the object, start the loop back. Most objects should have a rarity of, say, 10-100%, if you want extremely common objects, you can add them several times to the library.

This procedure will generate an object that sooner or later meets the criteria (if it exists) and will do this in accordance with the percentage of rarity.

One easy trick is that it will loop endlessly if such an object does not exist. Suppose there are no weapons in the library at level 17? To get around this, I suggest extending minLevel and maxLevel after every 100 attempts to make sure that one is finally found. Make sure you always have a level 1 object of each type.

For security reasons, you may also need help after 100,000 attempts (but don't forget to throw an exception - this is a problem if you ask for things that are not in the library!).

PS I implemented a similar library system in a game with rogains called Tyrant, which I created many years ago. The source is here if you are interersted:

https://github.com/mikera/tyrant/blob/master/src/main/java/mikera/engine/Lib.java

+2
source

All Articles