public void DoTerrain(int smoothness, float scale, float offset, int blur) { if (sdTerrain == null) { sdTerrain = new SquareDiamond(1024, new System.Random()); } sdTerrain.Generate(smoothness, (double)scale, InitNorthWest, InitNorth, InitWest, InitCenter); if (blur >= 3) { sdTerrain.Blur(blur); } int[] hmap = new int[1081 * 1081]; for (int y = 0; y < 1081; y++) { for (int x = 0; x < 1081; x++) { // Offset 28 px 1081-1024/2 float pt = (float)sdTerrain.Point(x - 28, y - 28); int pos = x + y * 1081; int val = (int)((pt + 1.0 + (double)offset) * 32768); val = Mathf.Clamp(val, 0, 65535); hmap[pos] = val; } } // doRiver(); byte[] map = new byte[1081 * 1081 * 2]; for (int y = 0; y < 1081; y++) { for (int x = 0; x < 1081; x++) { int pos = x + y * 1081; byte[] bytes = BitConverter.GetBytes(hmap[pos]); if (!BitConverter.IsLittleEndian) { Array.Reverse(bytes); } map[pos * 2] = bytes[0]; map[1 + pos * 2] = bytes[1]; } } SimulationManager.instance.AddAction(LoadHeightMap(map)); }
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)); } } } } }