Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411430 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 05:02:58 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Good way to calculate which game item spawns?
Pages: [1]
Print
Author Topic: Good way to calculate which game item spawns?  (Read 1005 times)
DrFrankenstein
Level 0
*


View Profile WWW
« on: May 15, 2015, 05:37:58 AM »

Hey everyone,

I'm working on my game and try to figure out what would be the best way to spawn the next game item. My game is about collecting stuff. So if the user collected an item, a new one will spawn. Is there a good way to calculate which item should spawn next?

Thanks for your answer!  Smiley
Logged

Prodigga
Level 0
***


Programmer


View Profile
« Reply #1 on: May 15, 2015, 05:39:59 AM »

Might need more info about your game, because the obvious answer right now is "pick one at random"? Haha. Do the items have a level of rarity? Do they need them in a certain order?
Logged

DrFrankenstein
Level 0
*


View Profile WWW
« Reply #2 on: May 15, 2015, 06:22:06 AM »

Ah, you need more information. Sorry! Here we go:

You have to collect ressources. At the moment only ressources from yourLevel and (yourLevel - 1) will spawn. General rocks, will spawn more often but are kinda useless. Some power ups will spawn rarely(is this word correct?).

And now I don't know how to do the logic like a pro Grin

Here is an older prototype of my game



Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #3 on: May 19, 2015, 04:57:54 AM »

You could have a two dimensional array holding the probabilities of each item spawning for each level. Then, you could generate a random number ranging from 0 to 100 and spawn the item according to the number that you got. However, for this to work you need to make sure that each level's probabilities add up to 100%, so maybe you should have some part of your code verify that for you.

The advantage to this is that you can decide the exact probability of an item apearing at each level, so you don't even need to have that "yourlevel and yourlevel - 1" stuff coded into the game itself. The drawback is that you need to manually make sure that your probabilities add up, so if your game has a lot of levels, this might now be the most effective solution.

Does this help you out?
Logged

Sgt. Pepper
Level 1
*


View Profile
« Reply #4 on: May 19, 2015, 01:49:12 PM »

I see you're using Java (nice to see more people using it  Smiley), so what I'd do is use java.util's Random class, and write code that looks like this:

Code:
Random rand;

//....
rand = new Random();
int item = rand.nextInt( [Your max number of items here] );

if (item == [something] ) {
spawnItem();
}
else if (item == [something] ) {
spawnOtherItem();
}

The only downside to this code is that it shows a bias towards 0 if your max number of items is too low... (so if your max number is like 3, it will output 0 a good percent of the time)

Hope this helps.

-Sgt. Pepper
Logged
Fallsburg
Level 10
*****


Fear the CircleCat


View Profile
« Reply #5 on: May 19, 2015, 02:52:29 PM »

Well, I still have no clue what you want, but the two best ways of doing this in my opinion are:

Code:
//Weighted Random

items = [(10,rock), (5,ore), (2,coal), (1,diamond)]

totalOddsRatio = sum(i[0] for i in items)

func GetRandom():
   prob = Random() // 0-1 floating point
   for i in items:
     if prob < i[0]/totalOddsRatio:
        return i[1]
     else :
        prob -= i[0]/totalOddsRatio
   
This is essentially what ProgramGamer recommended except you don't have to keep track of probabilities, just relative ratios of how much more (or less likely) one item is to another.  e.g. rock is 10 times more likely than diamond, ore is 2.5 times more likely than coal, etc.

Code:
//Bag without replacement

items = [rock,rock,rock,rock,rock,ore,ore,coal,diamond]

defaultItems = deep_copy(items)

func GetRandom():
  if items.IsEmpty():
    items = deep_copy(defaultItems)
  index = RandomIndex(items.length())
  item = items[index]
  items.RemoveAt(index)
  return item
This has the added benefit that you guarantee that what the absolute worst string can be (i.e. the player is guaranteed to see at least 1 diamond every 9 items).  This is what is used in most Tetris games so as to prevent the game from being horrible (i.e. only getting Z pieces).

Excuse the horrible pseudo-code

Logged
ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #6 on: May 19, 2015, 03:59:18 PM »

Oh okay, so you use a bag of probabilities and make the game pick from it to make sure that things get chosen at least so often, and you generate this bag by dividing each probability by the sum of all possibilities so that you can use a range of 0 to 1 for your random() function.

This discussion is neat!
Logged

DrFrankenstein
Level 0
*


View Profile WWW
« Reply #7 on: May 26, 2015, 03:34:21 AM »

Hey guys, thank you very much for your answers! I didn't expect that there would come some more responses Grin.
I'll try to find the best solution now. If I might have some questions I'll write again Smiley
Thanks
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic