private void GetTileResourcesImpl(ref NaturalResourceManager.AreaCell cell, ref uint ore, ref uint oil, ref uint forest, ref uint fertility, ref uint water)
 {
     ore += cell.m_finalOre;
     oil += cell.m_finalOil;
     forest += cell.m_finalForest;
     fertility += cell.m_finalFertility;
     water += cell.m_finalWater;
 }
Example #2
0
        private bool InitializeManagers()
        {
            SimulationManager sm = Singleton <SimulationManager> .instance;

            if (sm == null)
            {
                return(false);
            }

            NaturalResourceManager nm = Singleton <NaturalResourceManager> .instance;

            if (nm == null)
            {
                return(false);
            }

            this.simulationManager       = sm;
            this.naturalResourcesManager = nm;
            return(true);
        }
Example #3
0
 private static void GetTileResourcesImpl(NaturalResourceManager manager, ref NaturalResourceManager.AreaCell cell, ref uint ore, ref uint oil, ref uint forest, ref uint fertility, ref uint water)
 {
     UnityEngine.Debug.Log($"{manager}+{cell}+{ore}+{oil}+{forest}+{fertility}+{water}");
 }
Example #4
0
        public void DoTrees(bool follow, int treeNum, bool inside25, int density)
        {
            SquareDiamond sd;

            if (follow)
            {
                sd = sdResources;
            }
            else
            {
                sd = new SquareDiamond(512, new System.Random());
                sd.Generate(9, 1.0, SquareDiamond.InitMode.INIT_RANDOM, SquareDiamond.InitMode.INIT_RANDOM, SquareDiamond.InitMode.INIT_RANDOM, SquareDiamond.InitMode.INIT_RANDOM);
                sd.Blur(3);
            }

            TreeInfo tree;
            NaturalResourceManager m  = NaturalResourceManager.instance;
            TreeCollection         tc = GameObject.FindObjectOfType <TreeCollection>();
            int numTrees = tc.m_prefabs.Length;

            // TODO: Do something with TerrainManager.instance.GetShorePos() and HasWater()

            int start = 0;
            int end   = 512;

            if (inside25 == true)
            {
                // Create trees only inside center 25 tiles.
                start = (512 / 9) * 2;
                end   = 512 - (512 / 9) * 2;
            }

            int matches = 0;

            for (int y = start; y < end; y++)
            {
                for (int x = start; x < end; x++)
                {
                    int    pos = x * 512 + y;
                    double pt  = sd.Point(x, y);
                    if (follow ? m.m_naturalResources[pos].m_forest == 255 : (pt > 0.0 && pt < 0.5))
                    {
                        matches++;
                    }
                }
            }

            int max = TreeManager.MAX_MAP_TREES / matches > density ? density : TreeManager.MAX_MAP_TREES / matches;

            for (int y = start; y < end; y++)
            {
                for (int x = start; x < end; x++)
                {
                    int    pos = x * 512 + y;
                    double pt  = sd.Point(x, y);
                    if (follow ? m.m_naturalResources[pos].m_forest == 255 : (pt > 0.0 && pt < 0.5))
                    {
                        for (int n = 0; n < max; n++)
                        {
                            float xscale = 33.75f;                             // 540/16=33.75
                            float center = 270 * 32;
                            int   newx   = (int)(((float)x * xscale) - center + (frand() * xscale));
                            int   newy   = (int)(((float)y * xscale) - center + (frand() * xscale));

                            if (treeNum == 0)
                            {
                                tree = tc.m_prefabs[r.Next() & numTrees - 1];
                            }
                            else
                            {
                                tree = tc.m_prefabs[treeNum - 1];
                            }
                            SimulationManager.instance.AddAction(AddTree(newy, newx, SimulationManager.instance.m_randomizer, tree));
                        }
                    }
                }
            }
        }
Example #5
0
        public void DoResources(int smoothness, float scale, float offset, int blur, float forestlvl, float orelvl)
        {
            if (sdResources == null)
            {
                sdResources = new SquareDiamond(512, new System.Random());
            }

            if (RandomResourcesInit == true)
            {
                sdResources.Generate(smoothness, (double)scale,
                                     SquareDiamond.InitMode.INIT_RANDOM, SquareDiamond.InitMode.INIT_RANDOM,
                                     SquareDiamond.InitMode.INIT_RANDOM, SquareDiamond.InitMode.INIT_RANDOM);
            }
            else
            {
                sdResources.Generate(smoothness, (double)scale, InitNorthWest, InitNorth, InitWest, InitCenter);
            }

            NaturalResourceManager m = NaturalResourceManager.instance;

            // forest level from 0.0 to 1.0
            // in reality max 0.7 (and 3 * 0.1 for other three natural resources makes 1).
            orelvl    *= 3;
            forestlvl *= 0.7f;
            float gap    = (1.0f - forestlvl) / 3;
            float oregap = gap * orelvl;

            forestlvl /= 2;

            for (int y = 0; y < 512; y++)
            {
                for (int x = 0; x < 512; x++)
                {
                    int    pos = x * 512 + y;
                    double pt  = sdResources.Point(x, y) + 1.0 / 2.0;

                    m.m_naturalResources[pos].m_oil       = 0;
                    m.m_naturalResources[pos].m_ore       = 0;
                    m.m_naturalResources[pos].m_fertility = 0;
                    m.m_naturalResources[pos].m_forest    = 0;
                    m.m_naturalResources[pos].m_tree      = 0;

                    if (pt < gap)
                    {
                        m.m_naturalResources[pos].m_fertility = 255;
                    }
                    else if (pt > gap + forestlvl && pt < gap + forestlvl + oregap)
                    {
                        m.m_naturalResources[pos].m_ore = 255;
                    }
                    else if (pt > gap + forestlvl + oregap + forestlvl)
                    {
                        m.m_naturalResources[pos].m_oil = 255;
                    }
                    else
                    {
                        m.m_naturalResources[pos].m_forest = 255;
                        m.m_naturalResources[pos].m_tree   = 120;                       // ??
                    }
                    m.m_naturalResources[pos].m_modified = 0xff;
                }
            }
        }
Example #6
0
        static bool Prefix(NaturalResourceManager __instance)
        {
            try
            {
                int[] m_modifiedBX1 = (int[])typeof(NaturalResourceManager).GetField("m_modifiedBX1", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance);
                int[] m_modifiedBX2 = (int[])typeof(NaturalResourceManager).GetField("m_modifiedBX2", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance);

                for (int i = 0; i < 512; i++)
                {
                    if (m_modifiedBX2[i] >= m_modifiedBX1[i])
                    {
                        while (!Monitor.TryEnter(__instance.m_naturalResources, SimulationManager.SYNCHRONIZE_TIMEOUT))
                        {
                        }
                        int num1;
                        int num2;
                        try
                        {
                            num1             = m_modifiedBX1[i];
                            num2             = m_modifiedBX2[i];
                            m_modifiedBX1[i] = 10000;
                            m_modifiedBX2[i] = -10000;
                        }
                        finally
                        {
                            Monitor.Exit(__instance.m_naturalResources);
                        }
                        for (int j = num1; j <= num2; j++)
                        {
                            Color color = default(Color);
                            if (i == 0 || j == 0 || i == 511 || j == 511)
                            {
                                color = new Color(0f, 0f, 0f, 1f);
                                InfoViewManager.Instance.InfoViewDestructionTexture.SetPixel(j, i, color);
                            }
                            else
                            {
                                int pollution = 0;
                                int burned    = 0;
                                int destroyed = 0;

                                AddResource(j - 1, i - 1, 5, ref pollution, ref burned, ref destroyed);
                                AddResource(j, i - 1, 7, ref pollution, ref burned, ref destroyed);
                                AddResource(j + 1, i - 1, 5, ref pollution, ref burned, ref destroyed);
                                AddResource(j - 1, i, 7, ref pollution, ref burned, ref destroyed);
                                AddResource(j, i, 14, ref pollution, ref burned, ref destroyed);
                                AddResource(j + 1, i, 7, ref pollution, ref burned, ref destroyed);
                                AddResource(j - 1, i + 1, 5, ref pollution, ref burned, ref destroyed);
                                AddResource(j, i + 1, 7, ref pollution, ref burned, ref destroyed);
                                AddResource(j + 1, i + 1, 5, ref pollution, ref burned, ref destroyed);

                                color = CalculateColorComponents(pollution, burned, destroyed);
                                InfoViewManager.Instance.InfoViewDestructionTexture.SetPixel(j, i, color);

                                pollution = ModConfig.Instance.PollutedArea ? 0 : pollution;
                                burned    = ModConfig.Instance.BurnedArea ? 0 : burned;
                                destroyed = ModConfig.Instance.DestroyedArea ? 0 : destroyed;

                                color = CalculateColorComponents(pollution, burned, destroyed);
                            }
                            __instance.m_destructionTexture.SetPixel(j, i, color);
                        }
                    }
                }
                __instance.m_destructionTexture.Apply();

                InfoViewManager.Instance.InfoViewDestructionTexture.Apply();

                return(false);
            }
            catch (Exception e)
            {
                Debug.Log("[Hide It!] NaturalResourceManagerUpdateTextureBPatch:Prefix -> Exception: " + e.Message);
                return(true);
            }
        }
Example #7
0
        static bool Prefix(NaturalResourceManager __instance)
        {
            try
            {
                int[] m_modifiedX1 = (int[])typeof(NaturalResourceManager).GetField("m_modifiedX1", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance);
                int[] m_modifiedX2 = (int[])typeof(NaturalResourceManager).GetField("m_modifiedX2", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance);

                for (int i = 0; i < 512; i++)
                {
                    if (m_modifiedX2[i] >= m_modifiedX1[i])
                    {
                        while (!Monitor.TryEnter(__instance.m_naturalResources, SimulationManager.SYNCHRONIZE_TIMEOUT))
                        {
                        }
                        int num1;
                        int num2;
                        try
                        {
                            num1            = m_modifiedX1[i];
                            num2            = m_modifiedX2[i];
                            m_modifiedX1[i] = 10000;
                            m_modifiedX2[i] = -10000;
                        }
                        finally
                        {
                            Monitor.Exit(__instance.m_naturalResources);
                        }
                        for (int j = num1; j <= num2; j++)
                        {
                            Color color = default(Color);
                            if (i == 0 || j == 0 || i == 511 || j == 511)
                            {
                                color = new Color(0.5f, 0.5f, 0.5f, 0f);
                                InfoViewManager.Instance.InfoViewResourceTexture.SetPixel(j, i, color);
                            }
                            else
                            {
                                int ore       = 0;
                                int oil       = 0;
                                int sand      = 0;
                                int fertility = 0;
                                int forest    = 0;
                                int shore     = 0;

                                AddResource(j - 1, i - 1, 5, ref ore, ref oil, ref sand, ref fertility, ref forest, ref shore);
                                AddResource(j, i - 1, 7, ref ore, ref oil, ref sand, ref fertility, ref forest, ref shore);
                                AddResource(j + 1, i - 1, 5, ref ore, ref oil, ref sand, ref fertility, ref forest, ref shore);
                                AddResource(j - 1, i, 7, ref ore, ref oil, ref sand, ref fertility, ref forest, ref shore);
                                AddResource(j, i, 14, ref ore, ref oil, ref sand, ref fertility, ref forest, ref shore);
                                AddResource(j + 1, i, 7, ref ore, ref oil, ref sand, ref fertility, ref forest, ref shore);
                                AddResource(j - 1, i + 1, 5, ref ore, ref oil, ref sand, ref fertility, ref forest, ref shore);
                                AddResource(j, i + 1, 7, ref ore, ref oil, ref sand, ref fertility, ref forest, ref shore);
                                AddResource(j + 1, i + 1, 5, ref ore, ref oil, ref sand, ref fertility, ref forest, ref shore);

                                color = CalculateColorComponents(ore, oil, sand, fertility, forest, shore);
                                InfoViewManager.Instance.InfoViewResourceTexture.SetPixel(j, i, color);

                                ore       = ModConfig.Instance.OreArea ? 0 : ore;
                                oil       = ModConfig.Instance.OilArea ? 0 : oil;
                                sand      = ModConfig.Instance.SandArea ? 0 : sand;
                                fertility = ModConfig.Instance.FertilityArea ? 0 : fertility;
                                forest    = ModConfig.Instance.ForestArea ? 0 : forest;
                                shore     = ModConfig.Instance.ShoreArea ? 0 : shore;

                                color = CalculateColorComponents(ore, oil, sand, fertility, forest, shore);
                            }
                            __instance.m_resourceTexture.SetPixel(j, i, color);
                        }
                    }
                }
                __instance.m_resourceTexture.Apply();

                InfoViewManager.Instance.InfoViewResourceTexture.Apply();

                return(false);
            }
            catch (Exception e)
            {
                Debug.Log("[Hide It!] NaturalResourceManagerUpdateTexturePatch:Prefix -> Exception: " + e.Message);
                return(true);
            }
        }
        private static void TreesModified(NaturalResourceManager nrm, Vector3 position)
        {
            unsafe
            {
                int         num         = Mathf.Clamp((int)((double)position.x / 33.75 + 256), 0, 511);
                int         num1        = Mathf.Clamp((int)((double)position.z / 33.75 + 256), 0, 511);
                float       single      = (float)((double)(num - 256) * 33.75);
                float       single1     = (float)((double)(num1 - 256) * 33.75);
                float       single2     = (float)((double)(num + 1 - 256) * 33.75);
                float       single3     = (float)((double)(num1 + 1 - 256) * 33.75);
                int         num2        = Mathf.Max((int)(single / 32f + 270f), 0);
                int         num3        = Mathf.Max((int)(single1 / 32f + 270f), 0);
                int         num4        = Mathf.Min((int)(single2 / 32f + 270f), 539);
                int         num5        = Mathf.Min((int)(single3 / 32f + 270f), 539);
                TreeManager treeManager = Singleton <TreeManager> .instance;
                int         num6        = 0;
                int         growState   = 0;
                for (int i = num3; i <= num5; i++)
                {
                    for (int j = num2; j <= num4; j++)
                    {
                        uint mTreeGrid = treeManager.m_treeGrid[i * 540 + j];
                        int  num7      = 0;
                        while (mTreeGrid != 0)
                        {
                            if ((treeManager.m_trees.m_buffer[mTreeGrid].m_flags & 3) == 1)
                            {
                                Vector3 vector3 = treeManager.m_trees.m_buffer[mTreeGrid].Position;
                                if (vector3.x >= single && vector3.z >= single1 && vector3.x <= single2 && vector3.z <= single3)
                                {
                                    num6      = num6 + 15;
                                    growState = growState + treeManager.m_trees.m_buffer[mTreeGrid].GrowState;
                                }
                            }
                            mTreeGrid = treeManager.m_trees.m_buffer[mTreeGrid].m_nextGridTree;
                            int num8 = num7 + 1;
                            num7 = num8;
                            if (num8 < LimitTreeManager.Helper.TreeLimit)
                            {
                                continue;
                            }
                            CODebugBase <LogChannel> .Error(LogChannel.Core, string.Concat("Invalid list detected!\n", Environment.StackTrace));

                            break;
                        }
                    }
                }
                byte num9  = (byte)Mathf.Min(num6 * 4, 255);
                byte num10 = (byte)Mathf.Min(growState * 4, 255);
                NaturalResourceManager.ResourceCell mNaturalResources = nrm.m_naturalResources[num1 * 512 + num];
                if (num9 != mNaturalResources.m_forest || num10 != mNaturalResources.m_tree)
                {
                    bool mForest = num9 != mNaturalResources.m_forest;
                    mNaturalResources.m_forest = num9;
                    mNaturalResources.m_tree   = num10;
                    nrm.m_naturalResources[num1 * 512 + num] = mNaturalResources;
                    if (mForest)
                    {
                        nrm.AreaModified(num, num1, num, num1);
                    }
                }
            }
        }