Advertisement
Guest User

SOOQANAHOOI

a guest
Apr 28th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.08 KB | None | 0 0
  1. public class BasicMove : MoveWithJump
  2. {
  3.     /// <summary>
  4.     /// Path dictionary. Contains path list - list of cells - as keys and Tuples with target Cell, normalcy of Cell and if Cell is
  5.     /// accessed via jump
  6.     /// </summary>
  7.     Dictionary<List<Cell>, Tuple<Cell, bool, bool>> pathDict = new Dictionary<List<Cell>, Tuple<Cell, bool, bool>>();
  8.    
  9.     int strategicActCost;
  10.  
  11.     public override Dictionary<List<Cell>, Tuple<Cell, bool, bool>> Flood(int distance, int jump, int height,
  12.                                                                           int movepoints, int actionpoints, Cell originNode)
  13.     {
  14.         this.distance = distance;
  15.         this.jump = jump;
  16.         this.height = height;
  17.         this.movepoints = movepoints;
  18.         this.actionpoints = actionpoints;
  19.         this.originNode = originNode;
  20.        
  21.         pathDict.Clear();
  22.  
  23.         strategicActCost = 0; //put there some function to set action point costs for moving according to Strategic effects
  24.         int actCost = strategicActCost; //yadda yadda
  25.        
  26.         FloodSegment(originNode, new List<Cell>(), 0f, 0, false);
  27.         return pathDict;
  28.     }
  29.    
  30.     /// <summary>
  31.     /// A cell that will be the Node, Path to it (not inc itself), access type (i.e. normally unpathable/pathable, switched by ControlMode),
  32.     /// move cost accumulated by the Path, act cost. Uses class variables. Used for operating complete Dictionary of Paths
  33.     /// </summary>
  34.     protected void FloodSegment(Cell current, List<Cell> path1,
  35.                                 float moveCostSoFar1, int actCostSoFar1,
  36.                                 bool isJump)
  37.     {
  38.         List<Cell> path = new List<Cell>(path1);
  39.         float moveCostSoFar = moveCostSoFar1;
  40.         int actCostSoFar = actCostSoFar1;
  41.         if (current != originNode) //do not put originNode in path or dict
  42.         {
  43.             path.Add(current);
  44. //          string s = "";
  45. //          foreach (Cell cell in path)
  46. //          {
  47. //              s += cell.transform.position.x.ToString("F2") + cell.transform.position.y.ToString("F2");
  48. //          }
  49. //          Debug.Log("Added"+s);
  50.             pathDict.Add(path, Tuple.Create<Cell, bool, bool>(current, true, isJump)); //create pathDict keyvalue for current
  51.         }
  52.        
  53.         if (current.adjacentCell.Count != 0) //if at least 1 neighbouring Cell
  54.         {
  55.             foreach (Cell adjacent in current.adjacentCell.Keys) //for every adjacent Cell
  56.             {
  57.                 if (adjacent == originNode) continue;
  58.                 if (path.Contains(adjacent)) continue;
  59.                 int actCost = strategicActCost; //put there some function that changes actCost according to Cell/Occupier effects
  60.                 if (moveCostSoFar + adjacent.moveCost > movepoints) continue; //skip if will flood over the limit
  61.                 if (actCostSoFar + actCost > actionpoints) continue; //skip if will flood over the limit
  62.  
  63.                 moveCostSoFar += adjacent.moveCost;
  64.                 actCostSoFar += actCost;
  65.            
  66.                 //adjacent Cell is occupied, thus normal path is impossible
  67.                 //during movement paths will be cut off at this point, if in ControlMode first unnormal Cell will be highlighted                         
  68.                 if (adjacent.occupier != null)
  69.                 {
  70.                     {
  71.                         List<Cell> newPath = new List<Cell>(path);
  72.                         newPath.Add(adjacent);
  73.                         pathDict.Add(newPath, Tuple.Create<Cell, bool, bool>(adjacent, false, false));
  74.                     }
  75.                     if (CanJumpOver(current, adjacent, jump, height, 4)) //check for ability to jump over
  76.                     {                                                   //Borders and such are excluded
  77.                         //Find Cell that Unit will jump to if can jump over adjacent cell
  78.                         Vector2 vec = adjacent.transform.position;
  79.                         vec = GetVectorInDirection(vec, current.adjacentCell[adjacent]);
  80.                         string vec2 = (vec.x.ToString("F2")+vec.y.ToString("F2"));
  81.                         if (!cellGrid.CellDict.ContainsKey(vec2)) continue;
  82.                         for (int i = 0; i < cellGrid.CellDict[vec2].Count; i++)
  83.                         {
  84.                             //check 3 direction behind
  85.                             for (int j = -1; j < 2; j++)
  86.                             {
  87.                                 Vector2 vec3 = GetVectorInDirection(vec, DirectionCorrection(current.adjacentCell[adjacent] + j));
  88.                                 string vec4 = (vec3.x.ToString("F2")+vec3.y.ToString("F2"));
  89.                                 if (!cellGrid.CellDict.ContainsKey(vec4)) continue;
  90.                                 for (int k = 0; k < cellGrid.CellDict[vec4].Count; k++)
  91.                                 {
  92.                                     if (CanJumpOn(adjacent, cellGrid.CellDict[vec4][k], jump, height))
  93.                                     {
  94.                                         FloodSegment(cellGrid.CellDict[vec4][k], path, moveCostSoFar, actCostSoFar, true);
  95.                                     }
  96.                                 }
  97.                             }
  98.                         }
  99.                     }
  100.                 }
  101.                
  102.                 //checking for effects that will hurt unit if he uses this Cell for path                                                         
  103.                 //          if (special condition) //Dependant on special effects
  104.                 if (adjacent.occupier == null && adjacent.Height + jump < current.Height)
  105.                 { //accessible jumps that will hurt Unit - normal hurting condition
  106.                     List<Cell> newPath = new List<Cell>(path);
  107.                     newPath.Add(adjacent);
  108.                     pathDict.Add(newPath, Tuple.Create<Cell, bool, bool>(adjacent, false, true));
  109.                     continue;
  110.                 }
  111.                 FloodSegment(adjacent, path, moveCostSoFar, actCostSoFar, false);
  112.             }
  113.         }
  114.         if (current.jumpNode.jumpDict4.Count != 0) //if at least one jumping adjacent at 4 distances
  115.         {
  116.             foreach (Cell jumpAdjacent in current.jumpNode.jumpDict4.Keys) //for every Cell adjacent via jump
  117.             {
  118.                 foreach (var tuple in current.jumpNode.jumpDict4[jumpAdjacent])
  119.                 {
  120.                     int minJump = tuple.Item1;
  121.                     int maxHeight = tuple.Item2;
  122.                     if (jump < minJump || height > maxHeight) continue; //is valid for using this jump
  123.                     int actCost = strategicActCost; //put there some function that changes actCost according to Cell/Occupier effects
  124.                
  125.                     minJump /= 2; //every 2 heights of jump cost 1 movepoint, 1 minimum; needs to be integer
  126.                     if (moveCostSoFar + 4 + minJump > movepoints) continue; //skip if will flood over the limit
  127.                     if (actCostSoFar + actCost > actionpoints) continue; //skip if will flood over the limit
  128.                    
  129.                     moveCostSoFar += 4 + minJump;
  130.                     actCostSoFar += actCost;
  131.                
  132.                     //adjacent Cell is occupied, thus normal path is impossible
  133.                     //during movement paths will be cut off at this point, if
  134.                     //in ControlMode first unnormal Cell will be highlighted
  135.                     if (jumpAdjacent.occupier != null)
  136.                     {
  137.                         List<Cell> newPath = new List<Cell>(path);
  138.                         newPath.Add(jumpAdjacent);
  139.                         if (!pathDict.ContainsKey(newPath)) //prevents duplications of paths. Jumps sometimes have multiple possible routes
  140.                         {
  141.                             pathDict.Add(newPath, Tuple.Create<Cell, bool, bool>(jumpAdjacent, false, true));
  142.                             continue;
  143.                         }
  144.                     }
  145.                     //checking for effects that will hurt unit if he uses this Cell for path
  146.                     //              if (special condition) //Dependant on special effects
  147.                     if (jumpAdjacent.occupier == null && jumpAdjacent.Height + jump < current.Height)
  148.                     { //accessible jumps that will hurt Unit - normal hurting condition
  149.                         List<Cell> newPath = new List<Cell>(path);
  150.                         newPath.Add(jumpAdjacent);
  151.                         if (!pathDict.ContainsKey(newPath))
  152.                         {
  153.                             pathDict.Add(newPath, Tuple.Create<Cell, bool, bool>(jumpAdjacent, false, true));
  154.                             continue;
  155.                         }
  156.                     }
  157.                     List<Cell> newPath2 = new List<Cell>(path);
  158.                     newPath2.Add(jumpAdjacent);
  159.                     if (!pathDict.ContainsKey(newPath2))
  160.                     {
  161.                         FloodSegment(jumpAdjacent, path, moveCostSoFar, actCostSoFar, true);
  162.                     }
  163.                 }          
  164.             }
  165.         }
  166.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement