Advertisement
Guest User

austisticscreeching.cs

a guest
Mar 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Audio;
  10. using System.IO;
  11. namespace Pathfinder
  12. {
  13.     class Dijkstra
  14.     {
  15.         public Dijkstra()
  16.         {
  17.             closed = new bool[40, 40];
  18.             cost = new double[40, 40];
  19.         }
  20.  
  21.         //find which grid locations are closed
  22.         //Find a cost value for each node
  23.         //A link for each node - find the best way to get there
  24.         //Which nodes form the final path
  25.  
  26.  
  27.         public bool[,] closed; //whether or not location is closed
  28.         public double[,] cost; //cost value for each location
  29.         public Coord2[,] link; //link for each location = coords of a neighbouring location
  30.         public bool[,] inPath; //whether or not a location is in the final path
  31.  
  32.         public void Build(Level level, AiBotBase bot, Player plr)
  33.         {
  34.             //Set all costs to 1,000,000
  35.             //Set all nodes to open
  36.             for (int i = 0; i < 40; i++)
  37.             {
  38.                 for (int n = 0; n < 40; n++)
  39.                 {
  40.                     cost[i, n] = 1000000.0;
  41.                     closed[i, n] = false;
  42.                     inPath[i, n] = false;
  43.                     link[i, n] = new Coord2(-1, -1);
  44.                    
  45.                 }
  46.             }
  47.  
  48.             //bot's node = open
  49.             closed[bot.GridPosition.X, bot.GridPosition.Y] = false;
  50.  
  51.             //bot's node cost = 0
  52.             cost[bot.GridPosition.X, bot.GridPosition.Y] = 0;
  53.  
  54.             //Starts at bots' location
  55.             //Bot assigns next 4 nodes to 1 - now the lowest value, closes own node
  56.             //Moves to one of the lowest nodes and continues
  57.  
  58.             //Set initial surrounding node values
  59.             cost[bot.GridPosition.X + 1, bot.GridPosition.Y] = 1;
  60.             cost[bot.GridPosition.X - 1, bot.GridPosition.Y] = 1;
  61.             cost[bot.GridPosition.X, bot.GridPosition.Y + 1] = 1;
  62.             cost[bot.GridPosition.X, bot.GridPosition.Y - 1] = 1;
  63.  
  64.             cost[bot.GridPosition.X + 1, bot.GridPosition.Y + 1] = 1.4;
  65.             cost[bot.GridPosition.X + 1, bot.GridPosition.Y - 1] = 1.4;
  66.             cost[bot.GridPosition.X - 1, bot.GridPosition.Y + 1] = 1.4;
  67.             cost[bot.GridPosition.X - 1, bot.GridPosition.Y - 1] = 1.4;
  68.  
  69.  
  70.             //While the player's position is open
  71.             while (closed[plr.GridPosition.X, plr.GridPosition.Y] == false)
  72.             {
  73.                 //Increment X by 1
  74.                 for (int y = 0; y < 40; y++)
  75.                 {
  76.                     //Lowest cost value
  77.                     var minCost = System.Linq.Enumerable.Range(0, 4).Select(i => cost[i, 1]).Min();
  78.  
  79.                     //Increment Y by 1
  80.                     for (int n = 0; n < 40; n++)
  81.                     {
  82.                         //If lowest cost and open
  83.                         if (cost[y, n] == minCost && closed[y, n] == false)
  84.                         {
  85.  
  86.                             //Mark current position as closed
  87.                             closed[y, n] = true;
  88.  
  89.                             //If neighours' new cost is lower - and not blocked on the grid
  90.                             //N, +/
  91.                             if ((cost[y + 1, n] = (cost[y, n] + 1)) < (cost[y + 1, n]) && level.ValidPosition(true))
  92.                             {
  93.                                 cost[y + 1, n] = (cost[y, n] + 1);
  94.                                 link[y + 1, n] = (y, n);
  95.                             }
  96.  
  97.                             //S, -/
  98.                             if ((cost[y - 1, n] = (cost[y, n] + 1)) < (cost[y - 1, n]) && (level.ValidPosition == true))
  99.                             {
  100.                                 cost[y - 1, n] = (cost[y, n] + 1);
  101.                                 link[y - 1, n] = (y, n);
  102.                             }
  103.  
  104.                             //E, /+
  105.                             if ((cost[y, n + 1] = (cost[y, n] + 1)) < (cost[y, n + 1]) && (level.ValidPosition == true))
  106.                             {
  107.                                 cost[y, n + 1] = (cost[y, n] + 1);
  108.                                 link[y, n + 1] = (y, n);
  109.                             }
  110.  
  111.                             //W /-
  112.                             if ((cost[y, n - 1] = (cost[y, n] + 1)) < (cost[y, n - 1]) && (level.ValidPosition == true))
  113.                             {
  114.                                 cost[y, n - 1] = (cost[y, n] + 1);
  115.                                 link[y, n - 1] = (y, n);
  116.                             }
  117.  
  118.  
  119.                             //NE, +/+
  120.                             if ((cost[y + 1, n + 1] = (cost[y, n] + 1.4)) < (cost[y + 1, n + 1]) && (level.ValidPosition == true))
  121.                             {
  122.                                 cost[y + 1, n + 1] = (cost[y, n] + 1.4);
  123.                                 link[y + 1, n + 1] = (y, n);
  124.                             }
  125.  
  126.                             //NW, +/-
  127.                             if ((cost[y + 1, n - 1] = (cost[y, n] + 1.4)) < (cost[i + 1, n - 1]) && (level.ValidPosition == true))
  128.                             {
  129.                                 cost[y + 1, n - 1] = (cost[y, n] + 1.4);
  130.                                 link[y + 1, n - 1] = (y, n);
  131.                             }
  132.  
  133.                             //SE, -/+
  134.                             if ((cost[y - 1, n + 1] = (cost[y, n] + 1.4)) < (cost[y - 1, n + 1]) && (level.ValidPosition == true))
  135.                             {
  136.                                 cost[y - 1, n + 1] = (cost[y, n] + 1.4);
  137.                                 link[y - 1, n + 1] = (y, n);
  138.                             }
  139.  
  140.                             //SW, -/-
  141.                             if ((cost[y - 1, n - 1] = (cost[y, n] + 1.4)) < (cost[y - 1, n - 1]) && (level.ValidPosition == true))
  142.                             {
  143.                                 cost[y - 1, n - 1] = (cost[y, n] + 1.4);
  144.                                 link[y - 1, n - 1] = (y, n);
  145.                             }
  146.                         }
  147.                     }
  148.                 }
  149.             }
  150.  
  151.             bool done = false; //set to true when we are back at the bot position
  152.             Coord2 nextClosed = plr.GridPosition; //start of path
  153.             while (!done)
  154.             {
  155.                 inPath[nextClose.X, nextClose.Y] = true;
  156.                 nextClose = link[nextClose.X, nextClose.Y];
  157.                 if (nextClose == bot.GridPosition) done = true;
  158.             }
  159.  
  160.         }
  161.  
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement