Exemple #1
0
        static void Diamond(Context ctx, IntPoint2 middle, int radius)
        {
            double v1, v2, v3, v4;
            IntPoint2 p1, p2, p3, p4;

            p1 = middle.Offset(0, -radius);
            v1 = GetGridValue(ctx, p1);

            p2 = middle.Offset(-radius, 0);
            v2 = GetGridValue(ctx, p2);

            p3 = middle.Offset(0, radius);
            v3 = GetGridValue(ctx, p3);

            p4 = middle.Offset(radius, 0);
            v4 = GetGridValue(ctx, p4);

            var avg = (v1 + v2 + v3 + v4) / 4;
            var val = avg + GetRandom(ctx, ctx.Range);

            ctx.Grid[middle] = val;

            if (val < ctx.Min)
                ctx.Min = val;
            if (val > ctx.Max)
                ctx.Max = val;
        }
Exemple #2
0
 public MWCRandom(IntPoint2 p, int seed)
 {
     m_z = Hash.HashUInt32((uint)p.GetHashCode());
     m_w = (uint)seed;
     if (m_z == 0)
         m_z = 1;
     if (m_w == 0)
         m_w = 1;
 }
Exemple #3
0
        static void Main(string[] args)
        {
            Grid2D <int?> grid = new Grid2D <int?>(11, 11, 5, 5);

            var arr = IntPoint2.DiagonalSquareSpiral(new IntPoint2(0, 0), 5).ToArray();

            //var arr = IntPoint2.SquareSpiral(new IntPoint2(0, 0), 5).ToArray();

            Console.WriteLine("num values {0}", arr.Length);

            for (int i = 0; i < arr.Length; ++i)
            {
                var p = arr[i];

                if (grid[p].HasValue)
                {
                    throw new Exception();
                }

                grid[p] = i;
            }


            var sb = new StringBuilder();

            for (int y = 0; y < grid.Height; ++y)
            {
                sb.Clear();

                for (int x = 0; x < grid.Width; ++x)
                {
                    var v = grid[new IntPoint2(x, grid.Height - y - 1) - grid.Origin];

                    if (v.HasValue)
                    {
                        sb.AppendFormat("{0:000} ", v.Value);
                    }
                    else
                    {
                        sb.Append("... ");
                    }
                }

                Console.WriteLine(sb.ToString());
            }

            Console.ReadLine();
        }
Exemple #4
0
        /// <summary>
        /// Get value from the grid, or if the point is outside the grid, a value averaged between two corners
        /// </summary>
        static double GetGridValue(Context ctx, IntPoint2 p)
        {
            var grid = ctx.Grid;
            var corners = ctx.Corners;

            double v1, v2;
            double len;
            int pos;

            if (p.X < 0)
            {
                len = grid.Height;
                v1 = corners.SW;
                v2 = corners.NW;
                pos = p.Y;
            }
            else if (p.Y < 0)
            {
                len = grid.Width;
                v1 = corners.SW;
                v2 = corners.SE;
                pos = p.X;
            }
            else if (p.X >= grid.Width)
            {
                len = grid.Height;
                v1 = corners.SE;
                v2 = corners.NE;
                pos = p.Y;
            }
            else if (p.Y >= grid.Height)
            {
                len = grid.Width;
                v1 = corners.NW;
                v2 = corners.NE;
                pos = p.X;
            }
            else
            {
                return grid[p];
            }

            var m = (v2 - v1) / len;
            var h = m * pos + v1;

            return h;
        }
        public MainWindow()
        {
            m_blockerMap = new Grid2D<bool>(m_visionRange * 2 + 1, m_visionRange * 2 + 1);
            m_visionMap = new Grid2D<bool>(m_visionRange * 2 + 1, m_visionRange * 2 + 1);
            m_visionMap.Origin = new IntVector2(m_visionRange, m_visionRange);

            m_viewerLocation = new IntPoint2(m_visionRange, m_visionRange);

            m_blockerMap.Origin = new IntVector2(m_visionRange, m_visionRange);

            //m_blockerMap[2, 1] = true;

            m_blockerMap[12, 8] = true;
            m_blockerMap[12, 9] = true;

            m_blockerMap[13, 0] = true;
            m_blockerMap[13, 1] = true;

            m_blockerMap[13, 4] = true;

            m_blockerMap[13, 7] = true;
            m_blockerMap[13, 8] = true;
            m_blockerMap[13, 9] = true;

            m_blockerMap[14, 7] = true;
            m_blockerMap[14, 8] = true;
            m_blockerMap[14, 9] = true;

            m_blockerMap[15, 7] = true;
            m_blockerMap[15, 8] = true;
            m_blockerMap[15, 9] = true;

            m_blockerMap.Origin = new IntVector2();

            m_tileSize = 16;

            InitializeComponent();
        }
		public static void Calculate(IntPoint2 viewerLocation, int visionRange, Grid2D<bool> visibilityMap, IntSize2 mapSize,
			Func<IntPoint2, bool> blockerDelegate)
		{
			visibilityMap.Clear();

			if (blockerDelegate(viewerLocation) == true)
				return;

			visibilityMap[0, 0] = true;

			SCRData data = new SCRData()
			{
				ViewerLocation = viewerLocation,
				VisionRange = visionRange,
				VisionRangeSquared = (visionRange + 1) * (visionRange + 1),	// +1 to get a bit bigger view area
				VisibilityMap = visibilityMap,
				MapSize = mapSize,
				BlockerDelegate = blockerDelegate,
			};

			for (int octant = 0; octant < 8; ++octant)
				Calculate(ref data, 1, octant, 0.0, 1.0, 1);
		}
Exemple #7
0
 public int GetHeight(IntPoint2 p)
 {
     return this.HeightMap[p.Y, p.X];
 }
Exemple #8
0
        IEnumerable<IntPoint2> GetVisibleLocationsSimpleFOV()
        {
            var bounds2D = this.Environment.Size.Plane;

            for (int y = this.Y - this.VisionRange; y <= this.Y + this.VisionRange; ++y)
            {
                for (int x = this.X - this.VisionRange; x <= this.X + this.VisionRange; ++x)
                {
                    IntPoint2 loc = new IntPoint2(x, y);
                    if (!bounds2D.Contains(loc))
                        continue;

                    yield return loc;
                }
            }
        }
        void CreateStairs()
        {
            for (int z = m_size.Depth - 1; z > 0; --z)
            {
                var center = new IntPoint2(m_random.Next(m_size.Width), m_random.Next(m_size.Height));

                foreach (var p in IntPoint2.SquareSpiral(center, m_size.Width))
                {
                    if (m_size.Plane.Contains(p) == false)
                        continue;

                    var p1 = new IntPoint3(p, z);
                    var td1 = GetTileData(p1);

                    if (td1.IsClear == false)
                        continue;

                    var p2 = new IntPoint3(p, z - 1);
                    var td2 = GetTileData(p2);

                    if (td2.IsClear == false)
                        continue;

                    td1.TerrainID = TerrainID.StairsDown;
                    td1.TerrainMaterialID = MaterialID.Granite;

                    td2.InteriorID = InteriorID.Stairs;
                    td2.InteriorMaterialID = MaterialID.Granite;

                    SetTileData(p1, td1);
                    SetTileData(p2, td2);

                    break;
                }
            }
        }
        void _UpdateHoverTileInfo()
        {
            Point p;
            IntPoint2 sl;

            if (!this.IsMouseOver || !m_hoverTileMouseMove)
            {
                p = new Point();
                sl = new IntPoint2();

                this.HoverTileView.ClearTarget();
            }
            else
            {
                p = Mouse.GetPosition(this);
                sl = ScreenPointToIntScreenTile(p);
                var ml = ScreenPointToMapLocation(p);

                if (this.Environment != null && this.Environment.Contains(ml))
                {
                    this.HoverTileView.SetTarget(this.Environment, ml);
                }
                else
                {
                    this.HoverTileView.ClearTarget();
                }
            }

            if (ClientConfig.ShowMousePos)
            {
                if (p != this.MousePos)
                {
                    this.MousePos = p;
                    Notify("MousePos");
                }

                if (sl != this.ScreenLocation)
                {
                    this.ScreenLocation = sl;
                    Notify("ScreenLocation");
                }
            }

            m_updateHoverTileInfoQueued = false;
        }
        int FindNearestRoom(BSPTree bsp, int i, IntPoint2 p)
        {
            if (bsp.IsLeaf(i))
            {
                return i;
            }
            else
            {
                int left = FindNearestRoom(bsp, bsp.GetLeft(i), p);
                int right = FindNearestRoom(bsp, bsp.GetRight(i), p);

                double dl = (bsp[left].Grid.Center - p).Length;
                double rl = (bsp[right].Grid.Center - p).Length;

                if (dl < rl)
                    return left;
                else
                    return right;
            }
        }
		static IntPoint2 OctantTranslate(IntPoint2 p, int octant)
		{
			int tx = p.X * xxcomp[octant] + p.Y * xycomp[octant];
			int ty = p.X * yxcomp[octant] + p.Y * yycomp[octant];

			return new IntPoint2(tx, ty);
		}
Exemple #13
0
        static void Rectangle(Context ctx, IntPoint2 middle, int radius)
        {
            var grid = ctx.Grid;
            double v1, v2, v3, v4;
            IntPoint2 p1, p2, p3, p4;

            p1 = middle.Offset(radius, radius);
            v1 = grid[p1];

            p2 = middle.Offset(-radius, radius);
            v2 = grid[p2];

            p3 = middle.Offset(radius, -radius);
            v3 = grid[p3];

            p4 = middle.Offset(-radius, -radius);
            v4 = grid[p4];

            var avg = (v1 + v2 + v3 + v4) / 4;
            var val = avg + GetRandom(ctx, ctx.Range);

            grid[middle] = val;

            if (val < ctx.Min)
                ctx.Min = val;
            if (val > ctx.Max)
                ctx.Max = val;
        }
Exemple #14
0
        static void HeightMap(Context ctx)
        {
            var grid = ctx.Grid;

            for (int pass = 0; pass < MyMath.Log2(grid.Width); ++pass)
            {
                var parts = MyMath.Pow2(pass);
                var size = (grid.Width - 1) / parts;
                int half = size / 2;

                Debug.Assert(half != 0);

                for (int y = half; y < grid.Height; y += size)
                {
                    for (int x = half; x < grid.Width; x += size)
                    {
                        var p = new IntPoint2(x, y);
                        Rectangle(ctx, p, half);
                    }
                }

                bool odd = true;

                for (int y = 0; y < grid.Height; y += half)
                {
                    for (int x = odd ? half : 0; x < grid.Width; x += size)
                    {
                        var p = new IntPoint2(x, y);
                        Diamond(ctx, p, half);
                    }

                    odd = !odd;
                }

                ctx.Range *= Math.Pow(2, -ctx.H);
            }
        }
        void CreateCorridor(IntPoint2 from, IntPoint2 to, int z, bool horiz)
        {
            var td = new TileData();
            td.TerrainID = TerrainID.NaturalFloor;
            td.TerrainMaterialID = MaterialID.Granite;
            td.InteriorID = InteriorID.Empty;
            td.InteriorMaterialID = MaterialID.Undefined;

            if (horiz)
            {
                int yinc = from.Y < to.Y ? 1 : -1;

                int middle = from.Y + (to.Y - from.Y) / 2;

                for (int y = from.Y; y != middle; y += yinc)
                    SetTileData(new IntPoint3(from.X, y, z), td);

                int x1 = Math.Min(from.X, to.X);
                int x2 = Math.Max(from.X, to.X);

                for (int x = x1; x <= x2; ++x)
                    SetTileData(new IntPoint3(x, middle, z), td);

                for (int y = middle; y != to.Y; y += yinc)
                    SetTileData(new IntPoint3(to.X, y, z), td);
            }
            else
            {
                int xinc = from.X < to.X ? 1 : -1;

                int middle = from.X + (to.X - from.X) / 2;

                for (int x = from.X; x != middle; x += xinc)
                    SetTileData(new IntPoint3(x, from.Y, z), td);

                int y1 = Math.Min(from.Y, to.Y);
                int y2 = Math.Max(from.Y, to.Y);

                for (int y = y1; y <= y2; ++y)
                    SetTileData(new IntPoint3(middle, y, z), td);

                for (int x = middle; x != to.X; x += xinc)
                    SetTileData(new IntPoint3(x, to.Y, z), td);
            }
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            var dir = KeyToDir(e.Key);

            if (dir == Direction.None)
            {
                base.OnKeyDown(e);
                return;
            }

            m_viewerLocation += dir;
            UpdateFOV();
        }
        void m_map_AStarDone(AStarResult res)
        {
            var l = res.LastNode.Loc;
            var dirs = res.GetPathReverse();

            m_path1.Points.Clear();
            IntPoint2 p = new IntPoint2(l.X, l.Y);
            m_path1.Points.Add(new Point(p.X, p.Y));
            foreach (var d in dirs)
            {
                var v = IntVector2.FromDirection(d);
                p += v;
                m_path1.Points.Add(new Point(p.X, p.Y));
            }

            SetZ(m_path1, l.Z);
        }