WorldGen: Difference between revisions

From Vintage Story Wiki
Line 148: Line 148:
Since we will want to create a new ShuffleBag with each chest we place lets go ahead and create a MakeShuffleBag method.
Since we will want to create a new ShuffleBag with each chest we place lets go ahead and create a MakeShuffleBag method.
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
        private ShuffleBag<string> MakeShuffleBag()
private ShuffleBag<string> MakeShuffleBag()
{
    ShuffleBag<string> shuffleBag = new ShuffleBag<string>(100, api.World.Rand);
    shuffleBag.Add("ingot-iron", 10);
    shuffleBag.Add("ingot-bismuth", 5);
    shuffleBag.Add("ingot-silver", 5);
    shuffleBag.Add("ingot-zinc", 5);
    shuffleBag.Add("ingot-titanium", 5);
    shuffleBag.Add("ingot-platinum", 5);
    shuffleBag.Add("ingot-chromium", 5);
    shuffleBag.Add("ingot-tin", 5);
    shuffleBag.Add("ingot-lead", 5);
    shuffleBag.Add("ingot-gold", 5);
    return shuffleBag;
}
</syntaxhighlight>
This loads up our ShuffleBag with various ingots. The only more common item is iron. Feel free to change the item counts as you see fit. One thing to note here is the API does give us an instance of Random that we can use so we pass it in to our ShuffleBag.
 
We are almost ready to place these items in the chest but we need to create an ItemStack for each slot we will be taking up in the chest. The chest is an instance of IBlockEntityContainer which has an Inventory property. An Inventory is made up of several IItemSlot instances. We need a utility method to create a list of ItemStacks to place in those slots.
<syntaxhighlight lang="c#">
private IEnumerable<ItemStack> MakeItemStacks()
{
    ShuffleBag<string> shuffleBag = MakeShuffleBag();
    Dictionary<string, ItemStack> itemStacks = new Dictionary<string, ItemStack>();
    int grabCount = api.World.Rand.Next(MIN_ITEMS, MAX_ITEMS);
    for (int i = 0; i < grabCount; i++)
    {
        string nextItem = shuffleBag.Next();
        Item item = api.World.GetItem(nextItem);
        if (itemStacks.ContainsKey(nextItem))
         {
         {
             ShuffleBag<string> shuffleBag = new ShuffleBag<string>(100, api.World.Rand);
             itemStacks[nextItem].StackSize++;
            shuffleBag.Add("ingot-iron", 10);
            shuffleBag.Add("ingot-bismuth", 5);
            shuffleBag.Add("ingot-silver", 5);
            shuffleBag.Add("ingot-zinc", 5);
            shuffleBag.Add("ingot-titanium", 5);
            shuffleBag.Add("ingot-platinum", 5);
            shuffleBag.Add("ingot-chromium", 5);
            shuffleBag.Add("ingot-tin", 5);
            shuffleBag.Add("ingot-lead", 5);
            shuffleBag.Add("ingot-gold", 5);
            return shuffleBag;
         }
         }
        else
        {
            itemStacks.Add(nextItem, new ItemStack(item));
        }
    }
    return itemStacks.Values;
}
</syntaxhighlight>
</syntaxhighlight>


256

edits