Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Imports a natural resource texture and applies the resource values to all cells on the map.
// Natural Resource Texture Importer
// Imports a natural resource texture and applies the resource values to all cells on the map.
// Texture name and location must be gamefolder/textures/res.png
// Resolution is 512x512
// Channel explanation:
// Default value for all channels is 128.
// Red (128-0) Oil (inverted)
// Red (128-255) Ore
// Green (128-0) Fertility (inverted)
// Green (128-255) Sand
// Blue (128-0) Forest (inverted)
// Blue (128-255) Unused AFAIK
// Warning: if the blue channel is below 128, fertile will not work at all,
// also placing/deleting trees will modify the forest value in that area.
var texturePath = "textures/res.png";
if (File.Exists(texturePath)) {
Texture2D texture2D = new Texture2D(1, 1); texture2D.LoadImage(File.ReadAllBytes(texturePath));
var orig = UnityEngine.Object.FindObjectOfType<NaturalResourceManager>().m_naturalResources;
NaturalResourceManager.ResourceCell[] temp = new NaturalResourceManager.ResourceCell[orig.Length];
orig.CopyTo(temp, 0);
for(uint i = 0; i < orig.Length; i++) {
Color pixel = texture2D.GetPixel(Convert.ToInt32(i%512), Convert.ToInt32(i/512));
temp[i].m_ore = Convert.ToByte(Mathf.Clamp((pixel.r-0.5f)*2f, 0, 1)*255);
temp[i].m_oil = Convert.ToByte(Mathf.Clamp(((1-pixel.r)-0.5f)*2f, 0, 1)*255);
temp[i].m_sand = Convert.ToByte(Mathf.Clamp((pixel.g-0.5f)*2f, 0, 1)*255);
temp[i].m_fertility = Convert.ToByte(Mathf.Clamp(((1-pixel.g)-0.5f)*2f, 0, 1)*255);
temp[i].m_forest = Convert.ToByte(Mathf.Clamp(((1-pixel.b)-0.5f)*2f, 0, 1)*255);
temp[i].m_tree = Convert.ToByte(Mathf.Clamp(((1-pixel.b)-0.5f)*2f, 0, 1)*255); }
UnityEngine.Object.FindObjectOfType<NaturalResourceManager>().m_naturalResources = temp;
UnityEngine.Object.FindObjectOfType<NaturalResourceManager>().AreaModified(0, 0, 511, 511);
UnityEngine.Object.FindObjectOfType<NaturalResourceManager>().AreaModifiedB(0, 0, 511, 511); }
Copy link

taibenvenuti commented Apr 5, 2018

Any particular reason why you are using the Cities.exe directory to put your textures folder in?

I think you'll agree it's more convenient to put it in the same location where the local Addons folder is.

On a side note, you should use System.IO.Path.Combine instead of just building folder structure with slashes; otherwise users on different platforms will get an error when trying to use the script because some platforms use forward slash and others backslash.

var texturesFolder = Path.Combine(ColossalFramework.IO.DataLocation.localApplicationData, "Textures");

var texturePath = Path.Combine(texturesFolder, "NaturalResourceTexture.png");

Copy link

Deeheks commented Apr 5, 2018

I had to use TPB's tip on this..
For some reason, I wasnt able to get the script to load the image until I moved it to localapp.

Off track a little, but is there a way to possibly export the data currently in game as an image?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment