Ejemplo n.º 1
0
        public static void TestTCODFovTest()
        {
            int x = 1;
            int y = 1;

            RootConsole.Width = 80;
            RootConsole.Height = 50;
            RootConsole.WindowTitle = "FOV Tester";
            RootConsole.Fullscreen = false;

            using(RootConsole console = RootConsole.GetInstance())
            {
                console.Clear();

                using (TCODFov fov = new TCODFov(5, 5))
                {
                    for (int i = 0; i < 5; ++i)    //width
                        for (int j = 0; j < 5; ++j) //height
                            fov.SetCell(i, j, room[j, i] == '.', room[j, i] == '.');

                    KeyPress key;
                    do
                    {
                        PaintFOVTest(console, x, y, fov);

                        key = Keyboard.WaitForKeyPress(false);

                        switch (key.KeyCode)
                        {
                            case KeyCode.TCODK_UP:
                                if (room[y - 1, x] == '.')
                                    y--;
                                break;
                            case KeyCode.TCODK_DOWN:
                                if (room[y + 1, x] == '.')
                                    y++;
                                break;
                            case KeyCode.TCODK_LEFT:
                                if (room[y, x - 1] == '.')
                                    x--;
                                break;
                            case KeyCode.TCODK_RIGHT:
                                if (room[y, x + 1] == '.')
                                    x++;
                                break;
                        }
                    }
                    while (key.Character != 'q' && !console.IsWindowClosed());
                }
            }
        }
Ejemplo n.º 2
0
        public void updateFovMap(int level, FovMap fovMap)
        {
            TCODFov tcodLevel = new TCODFov(fovMap.Width, fovMap.Height);

            for (int j = 0; j < fovMap.Width; j++)
            {
                for (int k = 0; k < fovMap.Height; k++)
                {
                    if (fovMap.getCell(j, k) != FOVTerrain.Blocking)
                        tcodLevel.SetCell(j, k, true, false);
                    else
                        tcodLevel.SetCell(j, k, false, false);
                }
            }

            levelTCODMaps[level] = tcodLevel;
            levelTCODMapSizes[level] = new WidthHeight(fovMap.Width, fovMap.Height);
        }
Ejemplo n.º 3
0
        private static void PaintFOVTest(RootConsole console, int x, int y, TCODFov fov)
        {
            fov.CalculateFOV(x, y, 3, false, FovAlgorithm.Basic);

            for (int i = 0; i < 5; ++i)    //width
            {
                for (int j = 0; j < 5; ++j) //height
                {
                    if (room[j, i] == '.')
                    {
                        if (fov.CheckTileFOV(i, j))
                            console.PutChar(i, j, '.');
                        else
                            console.PutChar(i, j, '~');
                    }
                    else
                    {
                        console.PutChar(i, j, '#');
                    }
                }
            }
            console.PutChar(x, y, '@');
            console.Flush();
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Create new TCODPathFinding using map from TCODFov instance
 /// </summary>
 /// <param name="fovMap">Existing map</param>
 /// <param name="diagonalCost">Factor diagonal moves cost more</param>
 public TCODPathFinding(TCODFov fovMap, double diagonalCost)
 {
     m_instance = TCOD_path_new_using_map(fovMap.m_mapPtr, (float)diagonalCost);
 }
        public void updateMap(int level, PathingMap terrainMap)
        {
            TCODFov tcodLevel = new TCODFov(terrainMap.Width, terrainMap.Height);

            for (int j = 0; j < terrainMap.Width; j++)
            {
                for (int k = 0; k < terrainMap.Height; k++)
                {
                    tcodLevel.SetCell(j, k, true, terrainMap.getCell(j,k) == PathingTerrain.Walkable);
                }
            }

            levelTCODMaps[level] = tcodLevel;

            //Ignoring closed doors

            TCODFov tcodLevelNoClosedDoors = new TCODFov(terrainMap.Width, terrainMap.Height);

            for (int j = 0; j < terrainMap.Width; j++)
            {
                for (int k = 0; k < terrainMap.Height; k++)
                {
                    tcodLevelNoClosedDoors.SetCell(j, k, true, terrainMap.getCell(j,k) == PathingTerrain.Walkable || terrainMap.getCell(j,k) == PathingTerrain.ClosedDoor);
                }
            }

            levelTCODMapsIgnoringClosedDoors[level] = tcodLevelNoClosedDoors;

            //Ignoring closed doors and locks

            TCODFov tcodLevelNoClosedDoorsAndLocks = new TCODFov(terrainMap.Width, terrainMap.Height);

            for (int j = 0; j < terrainMap.Width; j++)
            {
                for (int k = 0; k < terrainMap.Height; k++)
                {
                    tcodLevelNoClosedDoorsAndLocks.SetCell(j, k, true, terrainMap.getCell(j, k) == PathingTerrain.Walkable ||
                        terrainMap.getCell(j, k) == PathingTerrain.ClosedDoor ||
                        terrainMap.getCell(j, k) == PathingTerrain.ClosedLock);
                }
            }

            levelTCODMapsIgnoringClosedDoorsAndLocks[level] = tcodLevelNoClosedDoorsAndLocks;
        }
 /// <summary>
 /// Create new TCODDijkstraPathFinding using map from TCODFov instance
 /// </summary>
 /// <param name="fovMap">Existing map</param>
 /// <param name="diagonalCost">Factor diagonal moves cost more</param>
 public TCODDijkstraPathFinding(TCODFov fovMap, double diagonalCost)
 {
     m_instance = TCOD_dijkstra_new(fovMap.m_mapPtr, (float)diagonalCost);
 }
Ejemplo n.º 7
0
        public void Init()
        {
            fov = new TCODFov(5, 5);

            for (int i = 0; i < 5; ++i)    //width
                for (int j = 0; j < 5; ++j) //height
                    fov.SetCell(i, j, room[j, i] == '.', room[j, i] == '.');

            pathFindingFOV = new TCODPathFinding(fov, 1.0);

            pathFindingCallback = new TCODPathFinding(5, 5, 1.0, new TCODPathCallback(TCODPathCallback));
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Create new TCODAStrPathFinding using map from TCODFov instance
 /// </summary>
 /// <param name="fovMap">Existing map</param>
 /// <param name="diagonalCost">Factor diagonal moves cost more</param>
 public TCODAStrPathFinding(TCODFov fovMap, double diagonalCost)
 {
     m_instance = TCOD_path_new_using_map(fovMap.m_mapPtr, (float)diagonalCost);
 }
Ejemplo n.º 9
0
        private bool ArePointsConnected(Point firstPoint, Point secondPoint)
        {
            //Build tcodmap
            TCODFov tcodMap = new TCODFov(Width, Height);
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    tcodMap.SetCell(i, j, !baseMap.mapSquares[i, j].BlocksLight, baseMap.mapSquares[i, j].Walkable);
                }
            }

            //Try to walk the path between the 2 staircases
            TCODPathFinding path = new TCODPathFinding(tcodMap, 1.0);
            path.ComputePath(firstPoint.x, firstPoint.y, secondPoint.x, secondPoint.y);

            //Find the first step. We need to load x and y with the origin of the path
            int x = upStaircase.x;
            int y = upStaircase.y;

            bool obstacleHit = false;

            //If there's no routeable path
            if (path.IsPathEmpty())
            {
                obstacleHit = true;
            }

            path.Dispose();
            tcodMap.Dispose();

            return (!obstacleHit);
        }
Ejemplo n.º 10
0
        void render_fov(bool first, KeyPress key)
        {
            if (map == null)
            {
                // initialize the map for the fov toolkit
                map = new TCODFov(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT);
                for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++)
                {
                    for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++)
                    {
                        if (smap[y][x] == ' ')
                            map.SetCell(x, y, true, true);// ground
                        else if (smap[y][x] == '=')
                            map.SetCell(x, y, true, false); // window
                    }
                }
                // 1d noise used for the torch flickering
                map_noise = new TCODNoise(1);
            }

            if (first)
            {
                TCODSystem.FPS = 30; // fps limited to 30
                // we draw the foreground only the first time.
                // during the player movement, only the @ is redrawn.
                // the rest impacts only the background color
                // draw the help text & player @
                sampleConsole.Clear();
                sampleConsole.ForegroundColor = (ColorPresets.White);
                sampleConsole.PrintLine("IJKL : move around\nT : torch fx " + (torch ? "off" : "on ") + " : light walls " + (light_walls ? "off" : "on ") + "\n+-: algo " + algo_names[algonum], 1, 1, LineAlignment.Left);
                sampleConsole.ForegroundColor = (ColorPresets.Black);
                sampleConsole.PutChar(px, py, '@');
                // draw windows
                for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++)
                {
                    for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++)
                    {
                        if (smap[y][x] == '=')
                        {
                            sampleConsole.PutChar(x, y, '=');
                        }
                    }
                }
            }

            if (recomputeFov)
            {
                // calculate the field of view from the player position
                recomputeFov = false;
                map.CalculateFOV(px, py, torch ? (int)TORCH_RADIUS : 0, light_walls, (FovAlgorithm)algonum);
            }
            // torch position & intensity variation
            float dx = 0.0f, dy = 0.0f, di = 0.0f;
            if (torch)
            {
                // slightly change the perlin noise parameter
                torchx += 0.2f;
                // randomize the light position between -1.5 and 1.5
                float[] tdx = { torchx + 20.0f };
                dx = map_noise.GetPerlinNoise(tdx) * 1.5f;
                tdx[0] += 30.0f;
                dy = map_noise.GetPerlinNoise(tdx) * 1.5f;
                // randomize the light intensity between -0.2 and 0.2
                float[] torchxArray = { torchx };
                di = 0.2f * map_noise.GetPerlinNoise(torchxArray);
            }

            // draw the dungeon
            for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++)
            {
                for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++)
                {
                    bool visible = map.CheckTileFOV(x, y);
                    bool wall = (smap[y][x] == '#');
                    if (!visible)
                    {
                        sampleConsole.SetCharBackground(x, y, (wall ? darkWall : darkGround), Background.Set);
                    }
                    else
                    {
                        if (!torch)
                        {
                            sampleConsole.SetCharBackground(x, y, wall ? lightWall : lightGround, Background.Set);
                        }
                        else
                        {
                            // torch flickering fx
                            Color baseColor = wall ? darkWall : darkGround;
                            Color light = wall ? lightWall : lightGround;
                            // cell distance to torch (squared)
                            float r = (float)((x - px + dx) * (x - px + dx) + (y - py + dy) * (y - py + dy));
                            if (r < SQUARED_TORCH_RADIUS)
                            {
                                // l = 1.0 at player position, 0.0 at a radius of 10 cells
                                float l = (SQUARED_TORCH_RADIUS - r) / SQUARED_TORCH_RADIUS + di;
                                // clamp between 0 and 1
                                if (l < 0.0f)
                                    l = 0.0f;
                                else if (l > 1.0f)
                                    l = 1.0f;
                                // interpolate the color
                                baseColor = Color.FromRGB((byte)(baseColor.Red + (light.Red - baseColor.Red) * l),
                                                    (byte)(baseColor.Green + (light.Green - baseColor.Green) * l),
                                                    (byte)(baseColor.Blue + (light.Blue - baseColor.Blue) * l));
                            }
                            sampleConsole.SetCharBackground(x, y, baseColor, Background.Set);
                        }
                    }
                }
            }

            if (key.Character == 'I' || key.Character == 'i')
            {
                // player move north
                if (smap[py - 1][px] == ' ')
                {
                    sampleConsole.PutChar(px, py, ' ', Background.None);
                    py--;
                    sampleConsole.PutChar(px, py, '@', Background.None);
                    recomputeFov = true;
                }
            }
            else if (key.Character == 'K' || key.Character == 'k')
            {
                // player move south
                if (smap[py + 1][px] == ' ')
                {
                    sampleConsole.PutChar(px, py, ' ', Background.None);
                    py++;
                    sampleConsole.PutChar(px, py, '@', Background.None);
                    recomputeFov = true;
                }
            }
            else if (key.Character == 'J' || key.Character == 'j')
            {
                // player move west
                if (smap[py][px - 1] == ' ')
                {
                    sampleConsole.PutChar(px, py, ' ', Background.None);
                    px--;
                    sampleConsole.PutChar(px, py, '@', Background.None);
                    recomputeFov = true;
                }
            }
            else if (key.Character == 'L' || key.Character == 'l')
            {
                // player move east
                if (smap[py][px + 1] == ' ')
                {
                    sampleConsole.PutChar(px, py, ' ', Background.None);
                    px++;
                    sampleConsole.PutChar(px, py, '@', Background.None);
                    recomputeFov = true;
                }
            }
            else if (key.Character == 'T' || key.Character == 't')
            {
                // enable/disable the torch fx
                torch = !torch;
                sampleConsole.ForegroundColor = (ColorPresets.White);
                sampleConsole.PrintLine("IJKL : move around\nT : torch fx " + (torch ? "off" : "on ") + " : light walls " + (light_walls ? "off" : "on ") + "\n+-: algo " + algo_names[algonum], 1, 1, LineAlignment.Left);
                sampleConsole.ForegroundColor = (ColorPresets.Black);
            }
            else if (key.Character == 'W' || key.Character == 'W')
            {
                light_walls = !light_walls;
                sampleConsole.ForegroundColor = (ColorPresets.White);
                sampleConsole.PrintLine("IJKL : move around\nT : torch fx " + (torch ? "off" : "on ") + " : light walls " + (light_walls ? "off" : "on ") + "\n+-: algo " + algo_names[algonum], 1, 1, LineAlignment.Left);
                sampleConsole.ForegroundColor = (ColorPresets.Black);
                recomputeFov = true;
            }
            else if (key.Character == '+' || key.Character == '-')
            {
                algonum += key.Character == '+' ? 1 : -1;

                if (algonum >= (int)FovAlgorithm.NB_Fov_Algorithms)
                    algonum = (int)FovAlgorithm.Restrictive;
                else if (algonum < 0)
                    algonum = 0;

                sampleConsole.ForegroundColor = (ColorPresets.White);
                sampleConsole.PrintLine("IJKL : move around\nT : torch fx " + (torch ? "off" : "on ") + " : light walls " + (light_walls ? "off" : "on ") + "\n+-: algo " + algo_names[algonum], 1, 1, LineAlignment.Left);
                sampleConsole.ForegroundColor = (ColorPresets.Black);
                recomputeFov = true;
            }
        }
Ejemplo n.º 11
0
        void render_path(bool first, KeyPress key)
        {
            if (map == null)
            {
                // initialize the map
                map = new TCODFov(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT);
                for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++)
                {
                    for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++)
                    {
                        if (smap[y][x] == ' ')
                            map.SetCell(x, y, true, true);// ground
                        else if (smap[y][x] == '=')
                            map.SetCell(x, y, true, false); // window
                    }
                }
            }

            if (first)
            {
                TCODSystem.FPS = 30; // fps limited to 30
                // we draw the foreground only the first time.
                // during the player movement, only the @ is redrawn.
                // the rest impacts only the background color
                // draw the help text & player @
                sampleConsole.Clear();
                sampleConsole.ForegroundColor = (ColorPresets.White);
                sampleConsole.PrintLine("IJKL / mouse :\nmove destination\nTAB : A*/dijkstra", 1, 1, LineAlignment.Left);
                sampleConsole.PrintLine("Using : A*", 1, 4, LineAlignment.Left);
                sampleConsole.ForegroundColor = (ColorPresets.Black);
                sampleConsole.PutChar(px, py, '@');
                // draw windows
                for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++)
                {
                    for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++)
                    {
                        if (smap[y][x] == '=')
                        {
                            sampleConsole.PutChar(x, y, '=');
                        }
                    }
                }
                recalculatePath = true;
            }

            if (recalculatePath)
            {
                if (usingAstar)
                {
                    if (AStrPath == null)
                        AStrPath = new TCODAStrPathFinding(map, 1.41);

                    AStrPath.ComputePath(px, py, dx, dy);
                }
                else
                {
                    if (DijkstraPath == null)
                        DijkstraPath = new TCODDijkstraPathFinding(map, 1.41);

                    dijkstraDist = 0.0f;
                    /* compute the distance grid */
                    DijkstraPath.Compute(px, py);
                    /* get the maximum distance (needed for ground shading only) */
                    for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++)
                    {
                        for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++)
                        {
                            float d = DijkstraPath.GetDistance(x, y);
                            if (d > dijkstraDist)
                                dijkstraDist = d;
                        }
                    }
                    // compute the path
                    DijkstraPath.SetPath(dx, dy);
                }
                recalculatePath = false;
                busy = .2f;
            }

            // draw the dungeon
            for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++)
            {
                for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++)
                {
                    bool wall = smap[y][x] == '#';
                    sampleConsole.SetCharBackground(x, y, (wall ? darkWall : darkGround), Background.Set);
                }
            }

            // draw the path
            if (usingAstar)
            {
                for (int i = 0; i < AStrPath.GetPathSize(); i++)
                {
                    int x, y;
                    AStrPath.GetPointOnPath(i, out x, out y);
                    sampleConsole.SetCharBackground(x, y, lightGround, Background.Set);
                }
            }
            else
            {
                for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; y++)
                {
                    for (int x = 0; x < SAMPLE_SCREEN_WIDTH; x++)
                    {
                        bool wall = smap[y][x] == '#';
                        if (!wall)
                        {
                            float d = DijkstraPath.GetDistance(x, y);
                            sampleConsole.SetCharBackground(x, y, Color.Interpolate(lightGround, darkGround, 0.9 * d / dijkstraDist), Background.Set);
                        }
                    }
                }
                for (int i = 0; i < DijkstraPath.PathLength(); i++)
                {
                    int x, y;
                    DijkstraPath.GetPointOnPath(i, out x, out y);
                    sampleConsole.SetCharBackground(x, y, lightGround, Background.Set);
                }
            }

            // move the creature
            busy -= TCODSystem.LastFrameLength;
            if (busy <= 0.0f)
            {
                busy += 0.2f;
                if (usingAstar)
                {
                    if (!AStrPath.IsPathEmpty())
                    {
                        sampleConsole.PutChar(px, py, ' ', Background.None);
                        AStrPath.WalkPath(ref px, ref py, true);
                        sampleConsole.PutChar(px, py, '@', Background.None);
                    }
                }
                else
                {
                    if (!DijkstraPath.IsPathEmpty())
                    {
                        sampleConsole.PutChar(px, py, ' ', Background.None);
                        DijkstraPath.WalkPath(ref px, ref py);
                        sampleConsole.PutChar(px, py, '@', Background.None);
                        recalculatePath = true;
                    }
                }
            }

            if ((key.Character == 'I' || key.Character == 'i') && dy > 0)
            {
                // destination move north
                sampleConsole.PutChar(dx, dy, oldChar, Background.None);
                dy--;
                oldChar = sampleConsole.GetChar(dx, dy);
                sampleConsole.PutChar(dx, dy, '+', Background.None);
                if (smap[dy][dx] == ' ')
                {
                    recalculatePath = true;
                }
            }
            else if ((key.Character == 'K' || key.Character == 'k') && dy < SAMPLE_SCREEN_HEIGHT - 1)
            {
                // destination move south
                sampleConsole.PutChar(dx, dy, oldChar, Background.None);
                dy++;
                oldChar = sampleConsole.GetChar(dx, dy);
                sampleConsole.PutChar(dx, dy, '+', Background.None);
                if (smap[dy][dx] == ' ')
                {
                    recalculatePath = true;
                }
            }
            else if ((key.Character == 'J' || key.Character == 'j') && dx > 0)
            {
                // destination move west
                sampleConsole.PutChar(dx, dy, oldChar, Background.None);
                dx--;
                oldChar = sampleConsole.GetChar(dx, dy);
                sampleConsole.PutChar(dx, dy, '+', Background.None);
                if (smap[dy][dx] == ' ')
                {
                    recalculatePath = true;
                }
            }
            else if ((key.Character == 'L' || key.Character == 'l') && dx < SAMPLE_SCREEN_WIDTH - 1)
            {
                // destination move east
                sampleConsole.PutChar(dx, dy, oldChar, Background.None);
                dx++;
                oldChar = sampleConsole.GetChar(dx, dy);
                sampleConsole.PutChar(dx, dy, '+', Background.None);
                if (smap[dy][dx] == ' ')
                {
                    recalculatePath = true;
                }
            }
            else if (key.KeyCode == KeyCode.TCODK_TAB)
            {
                usingAstar = !usingAstar;
                sampleConsole.ForegroundColor = (ColorPresets.White);
                if (usingAstar)
                    sampleConsole.PrintLine("Using : A*      ", 1, 4, LineAlignment.Left);
                else
                    sampleConsole.PrintLine("Using : Dijkstra", 1, 4, LineAlignment.Left);
                sampleConsole.ForegroundColor = (ColorPresets.Black);
                recalculatePath = true;
            }

            Mouse mouse = Mouse.GetStatus();
            int mx = mouse.CellX - SAMPLE_SCREEN_X;
            int my = mouse.CellY - SAMPLE_SCREEN_Y;

            if (mx >= 0 && mx < SAMPLE_SCREEN_WIDTH && my >= 0 && my < SAMPLE_SCREEN_HEIGHT && (dx != mx || dy != my))
            {
                sampleConsole.PutChar(dx, dy, oldChar, Background.None);
                dx = mx; dy = my;
                oldChar = sampleConsole.GetChar(dx, dy);
                sampleConsole.PutChar(dx, dy, '+', Background.None);
                if (smap[dy][dx] == ' ')
                {
                    recalculatePath = true;
                }
            }
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Create new TCODDijkstraPathFinding using map from TCODFov instance
 /// </summary>
 /// <param name="fovMap">Existing map</param>
 /// <param name="diagonalCost">Factor diagonal moves cost more</param>
 public TCODDijkstraPathFinding(TCODFov fovMap, double diagonalCost)
 {
     m_instance = TCOD_dijkstra_new(fovMap.m_mapPtr, (float)diagonalCost);
 }