示例#1
0
        public XRectangle ExpandToInclude(XPoint2 pnt)
        {
            int l = Left;
            int r = Right;
            int t = Top;
            int b = Bottom;

            if (pnt.X < l)
            {
                l = pnt.X;
            }
            if (pnt.X > r)
            {
                r = pnt.X;
            }
            if (pnt.Y < t)
            {
                t = pnt.Y;
            }
            if (pnt.Y > b)
            {
                b = pnt.Y;
            }
            X     = l; Y = t;
            Width = r - l + 1; Height = b - t + 1;
            return(new XRectangle(l, t, r - l + 1, b - t + 1));
        }
 public void AddArea(XPoint2 pos, int size, float coherence)
 {
     EnsureMaps(1);
     var map = Maps[0];
     int fringe = (int)Math.Round(
         (size * (1 - coherence))
     );
     XRectangle range = new XRectangle(pos.X, pos.Y, 0, 0).
         Inflate(size + fringe).
         RestrictToWithin(
             new XRectangle(0, 0, sizeX, sizeY)
         );
     range = new XRectangle(0, 0, sizeX, sizeY);
     List<TerrainType> types = new List<TerrainType>();
     float noiseRnd = pos.X + pos.Y * 10;
     for (int x = range.Left; x < range.Right; ++x) {
         for (int y = range.Top; y < range.Bottom; ++y) {
             int dist2 = (x - pos.X) * (x - pos.X) + (y - pos.Y) * (y - pos.Y);
             float noise =
                 SimplexNoise.simplex_noise_2octaves(
                     (float)x / (size + 2), noiseRnd, (float)y / (size + 2)
                 ) / 2 - 0.5f;
             map[x, y] = (1 - (float)Math.Sqrt(dist2) / size) +
                 (noise - 0.5f) * (1 - coherence) * 2;
         }
     }
 }
示例#3
0
 public XRectangle ExpandToInclude(XPoint2 pnt)
 {
     int l = Left;
     int r = Right;
     int t = Top;
     int b = Bottom;
     if (pnt.X < l) l = pnt.X; if (pnt.X > r) r = pnt.X;
     if (pnt.Y < t) t = pnt.Y; if (pnt.Y > b) b = pnt.Y;
     X = l; Y = t;
     Width = r - l + 1; Height = b - t + 1;
     return new XRectangle(l, t, r - l + 1, b - t + 1);
 }
示例#4
0
 public bool Contains(XPoint2 pnt)
 {
     return(pnt.X >= Left && pnt.Y >= Top && pnt.X < Right && pnt.Y < Bottom);
 }
示例#5
0
 public bool Contains(XPoint2 pnt)
 {
     return pnt.X >= Left && pnt.Y >= Top && pnt.X < Right && pnt.Y < Bottom;
 }
示例#6
0
文件: XPoint2.cs 项目: Weesals/ModHQ
 public static int Dot(XPoint2 p1, XPoint2 p2)
 {
     return p1.X * p2.X + p1.Y * p2.Y;
 }
示例#7
0
文件: XPoint2.cs 项目: Weesals/ModHQ
 public static int Cross(XPoint2 p1, XPoint2 v2)
 {
     return p1.X * v2.Y - p1.Y * v2.X;
 }
示例#8
0
 public static int Cross(XPoint2 p1, XPoint2 v2)
 {
     return(p1.X * v2.Y - p1.Y * v2.X);
 }
示例#9
0
 public static int Dot(XPoint2 p1, XPoint2 p2)
 {
     return(p1.X * p2.X + p1.Y * p2.Y);
 }
示例#10
0
        public void AddArea(XPoint2 pos, int size, XReal coherence, Func<XPoint2, bool> validityCheck)
        {
            int origSize = size;
            List<XPoint2> stack = new List<XPoint2>();
            stack.Add(pos);
            int at = 0;
            for (int x = 0; x < Map.GetLength(0); ++x) {
                for (int y = 0; y < Map.GetLength(1); ++y) {
                    Map[x, y].Weight = int.MaxValue;
                    Map[x, y].Extra = 0;
                }
            }
            Debug.Assert(pos.X >= 0 && pos.Y >= 0 &&
                pos.X < Map.GetLength(0) && pos.Y < Map.GetLength(1),
                "Out of bounds");
            Map[pos.X, pos.Y] = new Node() { Weight = 70 };
            activeArea = new XRectangle(pos.X, pos.Y, 1, 1);
            while (size > 0 && stack.Count > 0) {
                float minV = float.MaxValue;
                for (int i = 0; i < stack.Count; ++i) {
                    var p = stack[i];
                    var node = Map[p.X, p.Y];
                    float weight = Math.Abs(node.Weight) + node.Extra;
                    if (weight < minV) { minV = weight; at = i; }
                }
                var pnt = stack[at];
                --size;
                stack.RemoveAt(at--);
                int val = Map[pnt.X, pnt.Y].Weight;
                activeArea = activeArea.ExpandToInclude(pnt);
                const float NoiseScale = 30;
                for (int p = 0; p < propogOffs.Length; ++p) {
                    XPoint2 pnt2 = pnt + propogOffs[p];
                    if (pnt2.X >= 0 && pnt2.Y >= 0 && pnt2.X < Map.GetLength(0) && pnt2.Y < Map.GetLength(1)) {
                        if (!validityCheck(pnt2)) continue;
                        if (Map[pnt2.X, pnt2.Y].Weight <= val + propogCost[p]) continue;
                        int extraW = 0;
                        for (int p2 = 0; p2 < propogOffs.Length; ++p2) {
                            XPoint2 delta = propogOffs[p2];
                            float noise = (SimplexNoise.simplex_noise_3octaves(pnt2.X / NoiseScale,
                                (delta.X + delta.Y * 2) * 5,
                                pnt2.Y / NoiseScale) / 2 - 0.5f);
                            extraW = Math.Max(
                                extraW,
                                (int)(20 * 70 * (noise - 0.0f) * (1 - coherence) / 0.5f)
                            );//origSize / 46
                        }

                        if (!stack.Contains(pnt2)) {
                            if (Map[pnt2.X, pnt2.Y].IsPositive) size++;
                            stack.Add(pnt2);
                        }

                        Map[pnt2.X, pnt2.Y].Weight = val + propogCost[p] * Math.Sign(val);
                        if (extraW > Map[pnt2.X, pnt2.Y].Extra) Map[pnt2.X, pnt2.Y].Extra = extraW;
                    }
                }
            }
            for (int s = 0; s < stack.Count; ++s) {
                var pnt = stack[s];
                Map[pnt.X, pnt.Y].Weight = int.MinValue;
            }
        }
示例#11
0
 public void SetTileArea(XPoint2 pnt, RMArea area)
 {
     Map[pnt.X, pnt.Y] = area;
 }
示例#12
0
 public int GetDistanceSquaredToArea(XPoint2 pnt, RMArea area, int minDist)
 {
     Debug.Assert(area.Built,
         "GetDistanceSquaredToArea() does not work for unbuilt areas");
     int sizeX = Map.GetLength(0), sizeY = Map.GetLength(1);
     XRectangle range = area.ActiveArea;
     range.RestrictToWithin(new XRectangle(pnt.X, pnt.Y, 1, 1));
     range.Inflate(minDist);
     range.RestrictToWithin(new XRectangle(0, 0, sizeX, sizeY));
     int minDist2 = int.MaxValue;
     for (int x = range.Left; x < range.Right; ++x) {
         for (int y = range.Top; y < range.Bottom; ++y) {
             int dx = x - pnt.X, dy = y - pnt.Y;
             int dist2 = dx * dx + dy * dy;
             if (dist2 > minDist * minDist) continue;
             var tile = Map[x, y];
             if (tile != null)
                 while (tile != area && tile.Parent != null) tile = tile.Parent;
             if (tile == area) {
                 if (dist2 < minDist2) minDist2 = dist2;
             }
         }
     }
     return minDist2;
 }