Skip to content

Instantly share code, notes, and snippets.

@taibenvenuti
Created April 5, 2018 15:30
Show Gist options
  • Save taibenvenuti/63810531b564435a7e6a7163d7ff5af1 to your computer and use it in GitHub Desktop.
Save taibenvenuti/63810531b564435a7e6a7163d7ff5af1 to your computer and use it in GitHub Desktop.
Delete trees that are not in a forest resource area (To use with Ronyx69's resource from texture scripts)
var trees = TreeManager.instance.m_trees.m_buffer;
for (uint i = 0; i < trees.Length; i++)
{
var data = trees[i];
if (data.m_flags == (ushort)TreeInstance.Flags.None || data.Info == null) continue;
byte density;
NaturalResourceManager.instance.CheckForest(data.Position, out density);
double randomValue = Math.Round(UnityEngine.Random.value * 100, 0, MidpointRounding.AwayFromZero);
if (randomValue <= density || density == 0)
{
InstanceID id = default(InstanceID);
id.Tree = i;
ColossalFramework.Singleton<InstanceManager>.instance.ReleaseInstance(id);
data.m_flags |= 2;
//UpdateTree detour
if ((data.m_flags & 1) == 0)
{
return;
}
UnityEngine.Vector3 position = data.Position;
TreeInfo info = data.Info;
if (info == null)
{
return;
}
if (info.m_createRuining)
{
float minX = position.x - 4f;
float minZ = position.z - 4f;
float maxX = position.x + 4f;
float maxZ = position.z + 4f;
TerrainModify.UpdateArea(minX, minZ, maxX, maxZ, false, true, false);
}
//Singleton<NaturalResourceManager>.instance.TreesModified(position); <- this was updating resources
//
if ((data.m_flags & 64) != 0)
{
int num = Singleton<TreeManager>.instance.m_burningTrees.m_size - 1;
for (int j = 0; j <= num; j++)
{
if (Singleton<TreeManager>.instance.m_burningTrees.m_buffer[j].m_treeIndex == j)
{
Singleton<TreeManager>.instance.m_burningTrees.m_buffer[j] = Singleton<TreeManager>.instance.m_burningTrees.m_buffer[num];
Singleton<TreeManager>.instance.m_burningTrees.m_buffer[num] = default(TreeManager.BurningTree);
Singleton<TreeManager>.instance.m_burningTrees.m_size = num;
break;
}
}
}
data.m_flags = 0;
//FinalizeTree invocation
var args = new object[] { data, data };
var argTypes = new Type[] { data.GetType(), data.GetType() };
var methodInfo = TreeManager.instance.GetType().GetMethod("FinalizeTree",
System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static,
null,
argTypes,
null);
methodInfo.Invoke(TreeManager.instance, args);
//
Singleton<TreeManager>.instance.m_trees.ReleaseItem(i);
Singleton<TreeManager>.instance.m_treeCount = (int)(Singleton<TreeManager>.instance.m_trees.ItemCount() - 1u);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment