WorldGen: Difference between revisions

From Vintage Story Wiki
no edit summary
No edit summary
Line 66: Line 66:
The first line of code asks the '''IWorldManagerAPI''' to get the numeric block id for the block code "chest-south". Next we get the '''Block''' class that represents that chest. And finally we call a method called '''TryPlaceBlockForWorldGen''' and pass in an IBlockAccessor. There are many implementations of '''IBlockAccessor''' and we will cover that in a little more detail later. For now we are using this one. The second argument just calculates the world coordinates for 2 blocks in front of the player.
The first line of code asks the '''IWorldManagerAPI''' to get the numeric block id for the block code "chest-south". Next we get the '''Block''' class that represents that chest. And finally we call a method called '''TryPlaceBlockForWorldGen''' and pass in an IBlockAccessor. There are many implementations of '''IBlockAccessor''' and we will cover that in a little more detail later. For now we are using this one. The second argument just calculates the world coordinates for 2 blocks in front of the player.


Now if you run the mod from Visual Studio by pressing "Start" at the top you should be able to execute the '''/treasure''' command in the game. Once you do that you will have a fancy new chest appear in front of you with no items.  
Now if you run the mod from Visual Studio by pressing "Start" at the top you should be able to execute the '''/treasure''' command in the game. Once you do that you will have a fancy new chest appear in front of you with no items. Well I guess it's time for us to add some items.
 
 
== Adding items to our chest ==
We want to add some items to the chest since after all it is a "treasure" chest. For now we are going to add various ingots to the chest. However there are lots of items in Vintagestory and I encourage you to play with this and feel free to add other items. We also want to add items at random. Not only the type of items should be random but also the ingots we add should be random. There are all kinds of ways to do this but lets write a data structure that represents a grab bag full of items and lets pull items out of that bag and place them in the chest like Santa Claus! We will create a ShuffleBag class so in Visual Studio right click on your project and go to Add->Class. Call it ShuffleBag.cs and press Add. Here's the code to place in the ShuffleBag class.
<syntaxhighlight lang="c#">
using System;
using System.Collections.Generic;
 
namespace Vintagestory.Mods.TreasureChest
{
    /// <summary>
    /// Data structure for picking random items
    /// </summary>
    public class ShuffleBag<T>
    {
        private Random random = new Random();
        private List<T> data;
 
        private T currentItem;
        private int currentPosition = -1;
 
        private int Capacity { get { return data.Capacity; } }
        public int Size { get { return data.Count; } }
 
        public ShuffleBag(int initCapacity)
        {
            this.data = new List<T>(initCapacity);
            this.random = new Random();
        }
 
        public ShuffleBag(int initCapacity, Random random)
        {
            this.random = random;
            this.data = new List<T>(initCapacity);
        }
 
        /// <summary>
        /// Adds the specified number of the given item to the bag
        /// </summary>
        public void Add(T item, int amount)
        {
            for (int i = 0; i < amount; i++)
                data.Add(item);
 
            currentPosition = Size - 1;
        }
 
        /// <summary>
        /// Returns the next random item from the bag
        /// </summary>
        public T Next()
        {
            if (currentPosition < 1)
            {
                currentPosition = Size - 1;
                currentItem = data[0];
 
                return currentItem;
            }
 
            var pos = random.Next(currentPosition);
 
            currentItem = data[pos];
            data[pos] = data[currentPosition];
            data[currentPosition] = currentItem;
            currentPosition--;
 
            return currentItem;
        }
    }
}
</syntaxhighlight>


Well I guess it's time for us to add some items.


== Hooking in to the world gen API ==
== Hooking in to the world gen API ==
256

edits