Modding:WorldGen API: Difference between revisions

From Vintage Story Wiki
Line 357: Line 357:


== Detecting trees ==
== Detecting trees ==
Remember the treeTypes variable we created at the top of our class? Now it's time to populate that with the block codes of the tree logs we will be looking for. Let's add a method called LoadTreeTypes.
Remember the '''treeTypes''' variable we created at the top of our class? Now it's time to populate that with the block codes of the tree logs we will be looking for. Let's add a method called '''LoadTreeTypes'''.
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
private void LoadTreeTypes(ISet<string> treeTypes)
private void LoadTreeTypes(ISet<string> treeTypes)
Line 368: Line 368:
}
}
</syntaxhighlight>
</syntaxhighlight>
This method reads the log types from worldproperties/block/wood.json and adds them to our treeTypes set. Since we are looking for logs we prepend "log-" and we pick the variant "-ud" which means "Up/Down" since that's the variant of the tree log that is at the base of trees. Now to initialize our treeTypes set just add the following to StartServerSide:
This method reads the log types from '''worldproperties/block/wood.json''' and adds them to our '''treeTypes''' set. Since we are looking for logs we prepend "log-" and we pick the variant "-ud" which means "Up/Down" since that's the variant of the tree log that is at the base of trees. Now to initialize our '''treeTypes''' set just add the following to '''StartServerSide''':
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
this.treeTypes = new HashSet<string>();
this.treeTypes = new HashSet<string>();
Line 374: Line 374:
</syntaxhighlight>
</syntaxhighlight>


Now to detect our logs we can simply compare the Block.Code property to values in our treeTypes set. Lets write a quick helper method for this:
Now to detect our logs we can simply compare the '''Block.Code''' property to values in our '''treeTypes''' set. Lets write a quick helper method for this:
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
private bool IsTreeLog(Block block)
private bool IsTreeLog(Block block)
Line 382: Line 382:
</syntaxhighlight>
</syntaxhighlight>


The algorithm we will use to detect the tree log is pretty simple. We will see if the current block is a tree log, if so we will iterate downward to find the bottom log by detecting the first non-log type. Once we find it we simply find an adjacent air Block. Air blocks have a Block.Id value of 0. Here's the code:
The algorithm we will use to detect the tree log is pretty simple. We will see if the current block is a tree log, if so we will iterate downward to find the bottom log by detecting the first non-log type. Once we find it we simply find an adjacent air Block. Air blocks have a '''Block.Id''' value of 0. Here's the code:
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
private BlockPos TryGetChestLocation(BlockPos pos)
private BlockPos TryGetChestLocation(BlockPos pos)
256

edits