Esempio n. 1
0
        private void UpdateScenes()
        {
            List <D3Scene> d3Scenes = memReader.GetScenes();

            // Check if the current scene list changed since last time
            uint curCRC = 0;

            for (int i = 0; i < d3Scenes.Count; i++)
            {
                curCRC ^= d3Scenes[i].SceneID;
            }
            if (this.scenesCRC == curCRC)
            {
                return;
            }
            this.scenesCRC = curCRC;

            Scene[] scenes = new Scene[d3Scenes.Count];
            for (int i = 0; i < d3Scenes.Count; i++)
            {
                D3Scene   d3Scene  = d3Scenes[i];
                NavCell[] navCells = NavCells.SceneNavCells[(int)d3Scene.SnoID];
                scenes[i] = new Scene(d3Scene.Name, d3Scene.Active, d3Scene.SceneID,
                                      d3Scene.WorldID, (int)d3Scene.SnoID, d3Scene.Position);
            }

            // Find the world bounding box
            AABB boundingBox = new AABB(
                new Vector3f(Single.MaxValue, Single.MaxValue, 0f),
                new Vector3f(Single.MinValue, Single.MinValue, 0f));

            for (int i = 0; i < scenes.Length; i++)
            {
                AABB aabb = scenes[i].BoundingBox;
                if (aabb.Min.X < BoundingBox.Min.X)
                {
                    boundingBox.Min.X = aabb.Min.X;
                }
                if (aabb.Min.Y < boundingBox.Min.Y)
                {
                    boundingBox.Min.Y = aabb.Min.Y;
                }
                if (aabb.Max.X > boundingBox.Max.X)
                {
                    boundingBox.Max.X = aabb.Max.X;
                }
                if (aabb.Max.Y > boundingBox.Max.Y)
                {
                    boundingBox.Max.Y = aabb.Max.Y;
                }
            }
            this.BoundingBox = boundingBox;

            // Create the walkable grid
            int width  = NextPowerOfTwo((int)boundingBox.Max.X / 4);
            int height = NextPowerOfTwo((int)boundingBox.Max.Y / 4);

            byte[,] walkableGrid = new byte[width, height];

            for (int i = 0; i < scenes.Length; i++)
            {
                Scene scene = scenes[i];

                for (int j = 0; j < scene.NavCells.Length; j++)
                {
                    NavCell cell = scene.NavCells[j];
                    if (!cell.Flags.HasFlag(NavCellFlags.AllowWalk))
                    {
                        continue;
                    }

                    Vector2i min = new Vector2i(
                        (int)((scene.BoundingBox.Min.X + cell.BoundingBox.Min.X) * 0.25f),
                        (int)((scene.BoundingBox.Min.Y + cell.BoundingBox.Min.Y) * 0.25f));
                    Vector2i max = new Vector2i(
                        (int)((scene.BoundingBox.Min.X + cell.BoundingBox.Max.X) * 0.25f),
                        (int)((scene.BoundingBox.Min.Y + cell.BoundingBox.Max.Y) * 0.25f));

                    for (int x = min.X; x <= max.X; x++)
                    {
                        for (int y = min.Y; y <= max.Y; y++)
                        {
                            walkableGrid[x, y] = 1;
                        }
                    }
                }
            }

            this.Scenes       = scenes;
            this.WalkableGrid = walkableGrid;
        }