redkardevs

Untitled

Feb 6th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | Gaming | 0 0
  1.  
  2.     //initialise the thing...
  3.     public void Initialise()
  4.     {
  5.         Nodes = new List<RGNode>();
  6.         Columns = new List<RGNodeColumn>();
  7.  
  8.         //go through num of acts, and generate each act
  9.         for (int i = 0; i < ArmyData.Acts.Count; i++)
  10.         {
  11.             GenerateAct(i);
  12.         }
  13.  
  14.         //add a start node and insert it at the beginning
  15.         var startNode = new RGNode() { NodeType = NodeType.Start };
  16.         var firstColumn = new RGNodeColumn();
  17.         firstColumn.Nodes = new List<RGNode>();
  18.         firstColumn.Nodes.Add(startNode);
  19.         Nodes.Insert(0, startNode);
  20.         Columns.Insert(0, firstColumn);
  21.  
  22.         var lastAct = GetLastAct();     // gets the last possible act
  23.         var lastMove = GetLastMove();   // gets the last possible move number
  24.  
  25.         //add a last node and insert it at the end
  26.         var lastColumn = new RGNodeColumn();
  27.         lastColumn.Nodes = new List<RGNode>();
  28.         var armies = ArmyData.levels.Where(n => n.CurrentAct == lastAct && n.CurrentMove == lastMove).ToList();
  29.         var bossFight = armies.GetRandomElement();
  30.         var lastNode = new RGNode()
  31.         {
  32.             NodeType = NodeType.End, ArmyData = bossFight,
  33.             Reward = new BattleReward() { RewardClass = RewardClass.HighTierAugment }
  34.         };
  35.        
  36.         lastColumn.Nodes.Add(lastNode);
  37.  
  38.         lastColumn.Act = lastAct;
  39.         Columns.Add(lastColumn);
  40.         Nodes.Add(lastNode);
  41.     }
  42.  
  43.     private void GenerateAct(int actNum)
  44.     {
  45.        
  46.         //just a wrapper to get the act out of ArmyData object because it has a very bad structure...
  47.         Act act = GetActByCurrentAct(actNum);
  48.  
  49.         if (act == null)
  50.         {
  51.             // Handle act being null (e.g., log an error, return, or handle the exception in another way)
  52.             return;
  53.         }
  54.  
  55.         var maxShops = GetRandomRangeAsInt16(act.Shops);
  56.         var maxEvents = GetRandomRangeAsInt16(act.Events);
  57.         var moves = GetRandomRangeAsInt16(act.Moves);
  58.  
  59.         var cols = GenerateInitialColumns(actNum, moves);
  60.  
  61.         RandomlyAssignNodes(cols, NodeType.Event, maxEvents);
  62.         RandomlyAssignNodes(cols, NodeType.Shop, maxShops);
  63.  
  64.         //all this method does is finds all remaining unassigned nodes, and assigns them combat node
  65.         AssignUnassignedNodesToCombat(actNum);
  66.     }
  67.  
  68.     //wrapper because lazy
  69.     private short GetRandomRangeAsInt16(Vector2 range)
  70.     {
  71.         return Convert.ToInt16(UnityEngine.Random.Range(range.x, range.y));
  72.     }
  73.  
  74.     private List<RGNodeColumn> GenerateInitialColumns(int actNum, int moves)
  75.     {
  76.         var columns = new List<RGNodeColumn>();
  77.  
  78.         for (int i = 0; i < moves; i++)
  79.         {
  80.  
  81.             var act = actNum + 1;
  82.             var move = i + 1;
  83.            
  84.             var column = new RGNodeColumn
  85.             {
  86.                 Nodes = GenerateRandomNodes(actNum, i, moves),
  87.                 Act = act,
  88.                 Move = move
  89.             };
  90.            
  91.             Nodes.AddRange(column.Nodes);
  92.  
  93.             if (i > 0 && i < moves - 1)
  94.             {
  95.                 columns.Add(column);
  96.             }
  97.  
  98.             Columns.Add(column);
  99.         }
  100.  
  101.         return columns;
  102.     }
  103.  
  104.     private List<RGNode> GenerateRandomNodes(int actNum, int colNum, int lastMoves)
  105.     {
  106.         var nodes = new List<RGNode>();
  107.         var nodeCount = GetRandomRangeAsInt16(new Vector2(2, 3));
  108.        
  109.         for (int i = 0; i < nodeCount; i++)
  110.         {
  111.             nodes.Add(new RGNode() { NodeType = NodeType.Unassigned});
  112.         }
  113.  
  114.         return nodes;
  115.     }
  116.  
  117.     //this method goes through each column, shuffles the nodes, and assigns them a type
  118.     private void RandomlyAssignNodes(List<RGNodeColumn> cols, NodeType nodeType, int maxCount)
  119.     {
  120.         int currentCount = 0;
  121.         cols.Shuffle();
  122.  
  123.         foreach (var column in cols)
  124.         {
  125.             if (currentCount >= maxCount)
  126.             {
  127.                 break;
  128.             }
  129.  
  130.             //create a new list because we want to shuffle it
  131.             var nodes = column.Nodes.ToList();
  132.  
  133.             //shuffle is an extension function - does exactly what it says, shuffles the list
  134.             nodes.Shuffle();
  135.  
  136.             var node = nodes.FirstOrDefault(n => n.NodeType == NodeType.Unassigned);
  137.             if (node != null)
  138.             {
  139.                 currentCount++;
  140.                 node.NodeType = nodeType;
  141.             }
  142.         }
  143.     }
Advertisement
Add Comment
Please, Sign In to add comment