Jump to content
  • Advertisement
  • entries
    6
  • comments
    0
  • views
    1115

Migrating to Weighted Randomness

Whistling Alpaca Studios

862 views

When we started working on this project we had the mindset of "Random is good enough". Most of our systems were running off of pure randomness, which for our very small data set was perfectly fine. If you had a list of craftable items to pick from, and you had 3 or 4 items, just doing at Random.Range() over the list would be perfectly acceptable. I noticed this would become a big issue when we introduced randomized quest requirements. Of course, randomizing over the full set of items made the requirements total nonsense. Even when we narrowed down the blacksmith, he could be asking you to bring him 1 copper ore, or 1 Epic Adamantine Mace, he didn't care. One of the easiest and most useful solutions for the problem were: loot tables (and other tables which I'll get to later). In the future I'm sure I'll increase the following logic to allow for more than 100 percentage points.


public class LootTable
{
    List<LootTableItem> items = new List<LootTableItem>();

    public LootTable(List<LootTableItem> _i)
    {
        BuildListFromPartials(_i);

    }

    public void BuildListFromPartials(List<LootTableItem> _i)
    {
        int previous = 0;
        foreach(LootTableItem item in _i)
        {
            item.LowEnd = previous;
            item.HighEnd = previous + item.PercentChance;
            previous += item.PercentChance;
        }
        items = _i;
    }

    public Item RollForItem()
    {
        int roll = Random.Range(0, 101);

        foreach (LootTableItem item in items)
        {
            if(roll == 0 && item.LowEnd == 0)
            {
                return item.GetAnItem();
            }

            if(item.LowEnd < roll && roll <= item.HighEnd)
            {
                return item.GetAnItem();
            }
        }

        return null;
    }
}

And the supporting class:

public class LootTableItem
{
    //The chance that this item will be rolled (out of 100).
    public int PercentChance;
    public int LowEnd;
    public int HighEnd;

    public List<Item> PossibleItems;

    public LootTableItem(string item, int percentChance, int quality = 0)
    {
        PercentChance = percentChance;
        PossibleItems = new List<Item>();
        PossibleItems.Add(ItemManager.GetItem(item, quality));
    }

    public Item GetAnItem()
    {
        return PossibleItems[Random.Range(0, PossibleItems.Count)];
    }
}

The declaration of a loot table can look a little gnarly, but it's pretty easy to understand. Before long I was creating loot tables for everything, including items you'd get from harvesting:

new LootTable(new List<LootTableItem>(){ new LootTableItem("Copper Ore", 60), new LootTableItem("Iron Ore", 40)});

You can call lootTable.RollForItem() as many times as you want, which is why you can easily augment this strategy by adding rolls. I also stripped down the loot table to simply support weighted chance, allowing me to give the player multiple rolls on harvesting, so the better their equipment the better their chance of getting good items. The difference in this example is that instead of just saying "if the user has an +2 axe equiped, give them Random.Range(0,2) extra rolls", you can weight the outcomes. So you wouldn't have a 1/3 chance to get a 0, 1 or 2. You could have a 50% to get 0, a 30% chance to get 1 and a 20% chance to get 2. With this concept you can easily tweak your item collection outcomes to allow your players to have a significantly more fair experience. You can even tweak the rates if you keep getting really bad results.




0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By N1H1LU5_k
      Greetings. Thank you for opening this thread. Long post ahead. I am a High School student trying to get into game development. I would like to ask you a set of questions, but I think giving a context first will be the best. So, this is what's happening:
       
      In recent time, about 3 weeks ago, I started learning how to use Unity Game Engine and Blender. I always loved to draw and design all kinds of weapons, armors but also levels (unfortunately, all of those are on paper only, FOR NOW, of course) and when I started to think about how to apply this trait of mine, I concluded I could try creating a game. Since it's only been three weeks, my game-making skills are extremelly poor (as expected). Fast forward to yesterday. We were supposed to pick up a topic for our graduation thesis which we will hand in closely to our school-leaving examinations in year 2021. There is a TON of stuff to choose from. As long as it is part of IT, it's fine. And I chose to make a game. Well, it's more of a one level of a game in my case. More students before me have taken this topic and passed totally OK with Flappy Bird-style game they made. They were satisfied with it. I wouldn't be. I want this game to be best possible, that is why I also want to start working on it as soon as possible. In fact, I am starting right now, but I figured it would be better to ask here first. I have roughly 17 months to make a game worthy of passing. Of course, it could be some Flappy Bird. But I want to go further. Excuse me if this all sounds stupid, but I simply would not be satisfied with just any game I would make. I am well aware I won't be able to make a game like Battlefield alone, that there is a ton of people and budget behind such games. But I want it to be as best as possible.
       
      Now, my question is: What kind of game would I be able to make in this time of 17 months ? You have probably rolled your eyes after this question, don't worry, I understand how subjective the answer is. But I will at least try to state some metrics that can help with answer. I believe I have some fundamentals of programming, willing to learn the language needed (I believe in this case it's c#), I will be hooked on creating, animating and further improving 3D assets as needed for sure. I am able to dedicate at least 2 hours every day, a lot more so on weekends. I have to prepare for school and I also work, so that would be 2 hours of time on workdays right now. I would be able to get music and voice samples for characters in the game. 
       
      Here is my current plan. Please, if you see this as unreal to make in my deadline, tell me about it. I want to make one level of a FPS shooter, kinda sci-fi game with approximatelly 4-5 cutscenes and a small bossfight at the end of the level. Graphical quality would be that of a current CS:GO. Favored play-time would be, I think, around 15-20 minutes. Map would be a destroyed urban area. I have a lot of concepts (characters, weapons, rooms and so on) already drawn and if not, envisioned. If this is not possible, what would be the best game I would be able to make in 17 months ?
       
      Thank you for reading. Please, be honest. I know my idea can sound stupid, and answer I am looking might seem too subjective, but I want to get corrected if I am wrong with all of this. I highly value any reply that will be able to help in the slightest.
       
      Until then, have a nice day.
       
    • By Trexioas Xavier
      I want to know how would i go about making a 3D line renderer, it can be either a 3D or 2D-billboard line renderer. I don't want it to be static, i would like to add vertices to the line and extend it however i want to. To give an idea of what i want it to be/look like, Think of the Gauss Gun from Half life 1 and Half Life 2. I would want to make it reflect off the walls. I know i would need to do calculations to determine how to reflect off walls, i am just asking how would i go about making a 2D/3D line renderer. Please send me some online resources i can read upon.
      Thank you for your help!
    • By RoKabium Games
      Weapons - Magnacer is the first basic weapon that your Alien has equipped from the start. It shoots single bullets with a medium range that makes moderate damage to most enemies.
    • By JeremyAlessi
      In episode 12, Jeremy chats with Nathan Jester and Derek Hampton at Brown Chicken Brown Cow in downtown Hampton.  Nathan and Derek are local indie developers and long-time PixelFest participants. Nathan recently built a shoe-box arcade controller amongst a variety of gaming projects. Meanwhile, Derek talks Bouncy Bear and personal skill development.
       
       


      View full story
    • By JeremyAlessi
      In episode 12, Jeremy chats with Nathan Jester and Derek Hampton at Brown Chicken Brown Cow in downtown Hampton.  Nathan and Derek are local indie developers and long-time PixelFest participants. Nathan recently built a shoe-box arcade controller amongst a variety of gaming projects. Meanwhile, Derek talks Bouncy Bear and personal skill development.
       
       

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!