Kingdoms and Castles

Kingdoms and Castles

Not enough ratings
Adding "Pretty Roads" support to your roads
By JPiolho
Guide on how to add support for "Pretty Roads" mod to your custom roads.
   
Award
Favorite
Favorited
Unfavorite
Introduction
This guide is for mod developers that wish to add support for "Pretty Roads" mod for their custom roads.
https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=2057959069
In the base game, roads only have 4 possible models: Straight, Corners, 3 Roads Intersection and 4 Roads Intersections. Pretty Roads adds all other models required to allow any shape of road to merge in with each other and avoid having holes in the middle.

Adding support for your roads is easy via an API. The hardest part will most likely be having to model additional pieces for your road.

Modeling road pieces
Your modded roads will require more models in order accommodate all the shapes. Let's start with the pieces you are familiar with

Original pieces
Piece
Normal road
Stone Road
Straight
Elbow
Three-way
Four-way

Not too bad eh? Now below you'll find all the extra pieces that had to be modeled in, so that you can do pretty roads.

Pretty Road pieces
Piece
Normal road
Stone Road
Single
Dead End
Elbow Filled
Four-way Filled
Four-way Half Filled
Four-way Quarter Filled
Inner Corner
Three-way Filled
Three-way Half-Filled
Three-way Quarter-Filled
Three-Way Quarter-Filled 2

You will have to model all those pieces in order to be compatible with Pretty Roads.
RECOMMENDED: When modeling, make sure that the direction are the same. But that can be adjusted later.
ALSO NOTE: it is possible to optimize. If some of the pieces can be merely rotated versions of another piece, you do not have to create a separate model. More info on that later :)
Using the API
In order to add support for your roads from your mod, you now need to integrate with Pretty Roads. This can be done easily with the API.

You can download it here: https://github.com/jpiolho/KCMods/tree/master/PrettyRoads
Simply put the .cs file in your mod, and then you're ready to start using the API.

Quick setup
At the top of your code file, add
using JPiolho.KingdomsAndCastles.PrettyRoads.API;
In your mod PreScriptLoad, add the following line:
PrettyRoadsAPI.Initialize(this.gameObject);
then on your mod OnScriptLoad:
if(PrettyRoadsAPI.IsInstalled) { // Add your road register here }

The actual code that will install your roads for use in Pretty Road will be inside the 'if' under OnScriptLoad.

Registering roads
In other to register the roads, you will need 2 things ready from your mod: A unique building hash for your roads, and the models as 'Transform' objects.

For each road building that your mod have, you have to call
PrettyRoadsAPI.RegisterRoad

The first argument is just the building hash. The second argument is a Dictionary<String,Transform> of your models. The key is merely an identifier that you will reference in the third argument. The third argument is an array of RoadEntry. These are the road piece definitions.

First start by defining the 4 built-in pieces: Straight, Elbow, Three-way and Four-way.
new RoadEntry(RoadEntry.Pieces.Straight).AsPreset(RoadEntry.Presets.Default), new RoadEntry(RoadEntry.Pieces.Elbow).AsPreset(RoadEntry.Presets.Default), new RoadEntry(RoadEntry.Pieces.ThreeWay).AsPreset(RoadEntry.Presets.Default), new RoadEntry(RoadEntry.Pieces.FourWay).AsPreset(RoadEntry.Presets.Default), // or new RoadEntry(RoadEntry.Pieces.Straight) { Preset = RoadEntry.Presets.Default }, new RoadEntry(RoadEntry.Pieces.Elbow) { Preset = RoadEntry.Presets.Default }, new RoadEntry(RoadEntry.Pieces.ThreeWay) { Preset = RoadEntry.Presets.Default }, new RoadEntry(RoadEntry.Pieces.FourWay) { Preset = RoadEntry.Presets.Default },
(There is always a setter method that supports chaining, or you can always assign properties directly).

You will notice the 'Preset' property (or AsPreset method). Since these tiles don't have different behavior with Pretty Roads, in order to optimize wasted memory you can reuse a RoadEntry that's already built-in with default behavior. This is done via Presets. Whenever possible, use them!
NOTE: The original pieces don't always have to be default behavior, as you can use variations. More on that later.

Next up, you need to start defining the extra pieces. Let's start with a simple one:
new RoadEntry(RoadEntry.Pieces.DeadEnd).UseModel("deadend")); // or new RoadEntry(RoadEntry.Pieces.DeadEnd) { Model = "deadend" }

This code defines 'DeadEnd' piece as having a specific model 'deadend', which is one of the models that you define in the RegisterRoad second argument.

Let's make things more interesting now by adding some variations. Variations are a way of breaking up repeating patterns in the roads. Consider the following road:

You can clearly see a pattern. But we can make it like this instead:

This effect can be achieved by adding a RandomModel variation. Where it will choose a random model for this piece. This can be done like this:
new RoadEntry(RoadEntry.Pieces.ThreeWayFilled) { Model = "threewayfilled", VariationMethod = new RandomModelVariationMethod() { IncludeDefault = true, Models = new string[] { "threewayfilled_1", "threewayfilled_2" } } }

This will basically say that the ThreeWayFilled piece, should use the model 'threewayfilled', but with the variation modifier which makes it possible to choose 2 additional random models. The 'IncludeDefault' option set to true means that 'threewayfilled' model is also part of the random.

An alternative to random models, is random rotation. This is used in road pieces that can be rotated and they will still match & connect nicely. This is used on Four-way filled of the Stone road for example.

new RoadEntry(RoadEntry.Pieces.FourWayFilled) { Model = "fourwayfilled", VariationMethod = new RandomRotationVariationMethod() { Preset = RandomRotationVariationMethod.Presets.Default360 } }
You will notice that there is a preset for the RandomRotation. But it is possible to specify your own angles if needed. There's a couple more presets: Default0or180 (Angle is either 0 or 180) and Default0or90. Default360 can be used for angles of 0, 90, 180 and 270.
Conclusion
I hope the guide was understandable and that you've managed to integrate your custom roads with Pretty Roads mod! I will try to keep this mod up-to-date and improve it over time!
1 Comments
rudeus 15 Jul, 2020 @ 4:05am 
im thinking of making a mod for this because of how detailed the guide is, haven't made a mod before but you seem like the greatest mod creator I've seen! :steamhappy: