Advertisement
PsyOps

BaseLoader

May 1st, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6.  
  7. namespace DevaBot.BaseDuel
  8. {
  9.     class BaseLoader
  10.     {
  11.         // Holds tileID using a multi dimensional array m_MapGrid[xCoor][yCoor]
  12.         private byte[][] m_MapTileIDs;
  13.         // Holds tileType
  14.         private byte[][] m_MapTileType;
  15.  
  16.         // 4 different points to check - x and y
  17.         private int[] add_x = { -1, 0, 1, 0 };
  18.         private int[] add_y = { 0, -1, 0, 1 };
  19.  
  20.         // Load variables in
  21.         public BaseLoader(byte[][] MapTileIDs, byte[][] MapTileType)
  22.         {
  23.             this.m_MapTileIDs = MapTileIDs;
  24.             this.m_MapTileType = MapTileType;
  25.         }
  26.  
  27.         // All saved safe that were filled
  28.         private List<ushort[]> SafeFills = new List<ushort[]>();
  29.  
  30.         public void LoadBases(List<Base> BaseList, Base Lobby)
  31.         {
  32.             // Start scanning map
  33.             for (int y = 0; y < 1024; y += 1)
  34.             {
  35.                 for (int x = 0; x < 1024; x += 1)
  36.                 {
  37.                     if (m_MapTileIDs[x][y] == 171)
  38.                     {
  39.                         // Start to store the dimensions by using first point
  40.                         ushort[] fill_dim = new ushort[] { (ushort)x, (ushort)y, (ushort)(x + 1), (ushort)(y + 1) };
  41.  
  42.                         // This will hold our fill list
  43.                         Queue<Point> fillQ = new Queue<Point>();
  44.                         // add our first safe tile to list
  45.                         fillQ.Enqueue(new Point(x, y));
  46.                         // erase tile from map
  47.                         m_MapTileIDs[x][y] = 0;
  48.  
  49.                         // run the fill loop
  50.                         while (fillQ.Count > 0)
  51.                         {
  52.                             // pull the next point out of list
  53.                             Point nextQ = fillQ.Dequeue();
  54.  
  55.                             // update safe dimensions
  56.                             m_UpdateDimension(fill_dim, nextQ);
  57.  
  58.                             // 4 point check for fill
  59.                             for (int i = 0; i < 4; i++)
  60.                             {
  61.                                 // assign next point to check
  62.                                 Point fourPoint = new Point(nextQ.X + add_x[i], nextQ.Y + add_y[i]);
  63.  
  64.                                 // Make sure its a safe
  65.                                 if (m_ValidTile(fourPoint) && m_MapTileIDs[fourPoint.X][fourPoint.Y] == 171)
  66.                                 {
  67.                                     // add to q
  68.                                     fillQ.Enqueue(fourPoint);
  69.                                     // erase tile to avoid including it in q more than once
  70.                                     m_MapTileIDs[fourPoint.X][fourPoint.Y] = 0;
  71.                                 }
  72.                             }
  73.                         }
  74.                         // add safe into our list
  75.                         SafeFills.Add(fill_dim);
  76.                     }
  77.                 }
  78.             }
  79.  
  80.             // Fill the space starting at where the safe was located
  81.             while (SafeFills.Count > 0)
  82.             {
  83.                 // Create a base and start saving info
  84.                 Base NewBase = new Base();
  85.                 NewBase.AlphaSafe = SafeFills[0];
  86.  
  87.                 // Store values
  88.                 int x = SafeFills[0][0];
  89.                 int y = SafeFills[0][1];
  90.  
  91.                 // Remove it from list
  92.                 SafeFills.RemoveAt(0);
  93.  
  94.                 // This will hold our fill list
  95.                 Queue<Point> fillQ = new Queue<Point>();
  96.  
  97.                 // Start to store the dimensions by using first point
  98.                 ushort[] fill_dim = new ushort[] { (ushort)x, (ushort)y, (ushort)(x + 1), (ushort)(y + 1) };
  99.  
  100.                 // add our first safe tile to list
  101.                 fillQ.Enqueue(new Point(x, y));
  102.  
  103.                 int safes_found = 1;
  104.  
  105.                 // run the fill loop
  106.                 while (fillQ.Count > 0)
  107.                 {
  108.                     // Tile count for safe
  109.                     NewBase.TileCount++;
  110.  
  111.                     // pull the next point out of list
  112.                     Point nextQ = fillQ.Dequeue();
  113.  
  114.                     // update safe dimensions
  115.                     m_UpdateDimension(fill_dim, nextQ);
  116.  
  117.                     // 4 point check for fill
  118.                     for (int i = 0; i < 4; i++)
  119.                     {
  120.                         // assign next point to check
  121.                         Point fourPoint = new Point(nextQ.X + add_x[i], nextQ.Y + add_y[i]);
  122.  
  123.                         // Tile is "passable"
  124.                         if (m_ValidTile(fourPoint) && m_MapTileType[fourPoint.X][fourPoint.Y] == 1)
  125.                         {
  126.                             // add to q
  127.                             fillQ.Enqueue(fourPoint);
  128.                             // erase tile to avoid including it in q more than once
  129.                             m_MapTileType[fourPoint.X][fourPoint.Y] = 0;
  130.  
  131.                             // Counting safes found as we fill base
  132.                             // Checking safe list in reverse order as we will be removing some
  133.                             for (int s = SafeFills.Count - 1; s >= 0; s--)
  134.                             {
  135.                                 // Coords match
  136.                                 if (SafeFills[s][0] == fourPoint.X && SafeFills[s][1] == fourPoint.Y)
  137.                                 {
  138.                                     // Add safe to base info
  139.                                     NewBase.BravoSafe = SafeFills[s];
  140.                                     NewBase.BaseDimension = fill_dim;
  141.                                     // Remove the safe found in base
  142.                                     SafeFills.RemoveAt(s);
  143.                                     // Add to our count
  144.                                     safes_found++;
  145.                                 }
  146.                             }
  147.                         }
  148.                     }
  149.                 }
  150.                 // 2 safes make it a valid base
  151.                 if (safes_found == 2)
  152.                     BaseList.Add(NewBase);
  153.                 if (safes_found == 1)
  154.                 {
  155.                     // Assign lobby values
  156.                     Lobby.AlphaSafe = NewBase.AlphaSafe;
  157.                     Lobby.AlphaStartX = (ushort)Math.Floor((decimal)((NewBase.AlphaSafe[0] + NewBase.AlphaSafe[2]) / 2));
  158.                     Lobby.AlphaStartY = (ushort)Math.Floor((decimal)((NewBase.AlphaSafe[1] + NewBase.AlphaSafe[3]) / 2));
  159.                     Lobby.BaseDimension = new ushort[] { (ushort)(fill_dim[0] * 16), (ushort)(fill_dim[1] * 16), (ushort)(fill_dim[2] * 16), (ushort)(fill_dim[3] * 16) };
  160.                     Lobby.TileCount = NewBase.TileCount;
  161.                 }
  162.             }
  163.             // Grab high and low values from all loaded bases
  164.             int low = BaseList.ElementAt(0).TileCount;
  165.             int high = BaseList.ElementAt(0).TileCount;
  166.  
  167.             for (int i = 0; i < BaseList.Count; i++)
  168.             {
  169.                 if (BaseList[i].TileCount > high)
  170.                     high = BaseList[i].TileCount;
  171.                 else if (BaseList[i].TileCount < low)
  172.                     low = BaseList[i].TileCount;
  173.             }
  174.             // divide by 3 to get small/medium/large
  175.             int tier = (int)Math.Floor((decimal)((high - low) / 3));
  176.  
  177.             for (int i = 0; i < BaseList.Count; i++)
  178.             {
  179.                 if (BaseList[i].TileCount > low + (tier * 2))
  180.                     BaseList[i].Size = BaseSize.Large;
  181.                 else if (BaseList[i].TileCount > low + tier)
  182.                     BaseList[i].Size = BaseSize.Medium;
  183.                 else
  184.                     BaseList[i].Size = BaseSize.Small;
  185.  
  186.                 // Assign safe start points while we loop through bases - to be save in tiles not pixels
  187.                 BaseList[i].AlphaStartX = (ushort)Math.Floor((decimal)((BaseList[i].AlphaSafe[0] + BaseList[i].AlphaSafe[2]) / 2));
  188.                 BaseList[i].AlphaStartY = (ushort)Math.Floor((decimal)((BaseList[i].AlphaSafe[1] + BaseList[i].AlphaSafe[3]) / 2));
  189.                 BaseList[i].BravoStartX = (ushort)Math.Floor((decimal)((BaseList[i].BravoSafe[0] + BaseList[i].BravoSafe[2]) / 2));
  190.                 BaseList[i].BravoStartY = (ushort)Math.Floor((decimal)((BaseList[i].BravoSafe[1] + BaseList[i].BravoSafe[3]) / 2));
  191.  
  192.                 // Convert dimension from tile to pixels since we will be
  193.                 // checking player position agains it using pixel position
  194.                 ushort[] m_alphaSafe = BaseList[i].AlphaSafe;
  195.                 ushort[] m_bravoSafe = BaseList[i].BravoSafe;
  196.                 ushort[] baseDimensions = BaseList[i].BaseDimension;
  197.                 BaseList[i].AlphaSafe = new ushort[] { (ushort)(m_alphaSafe[0] * 16), (ushort)(m_alphaSafe[1] * 16), (ushort)(m_alphaSafe[2] * 16), (ushort)(m_alphaSafe[3] * 16) };
  198.                 BaseList[i].BravoSafe = new ushort[] { (ushort)(m_bravoSafe[0] * 16), (ushort)(m_bravoSafe[1] * 16), (ushort)(m_bravoSafe[2] * 16), (ushort)(m_bravoSafe[3] * 16) };
  199.                 BaseList[i].BaseDimension = new ushort[] { (ushort)(baseDimensions[0] * 16), (ushort)(baseDimensions[1] * 16), (ushort)(baseDimensions[2] * 16), (ushort)(baseDimensions[3] * 16) };
  200.             }
  201.         }
  202.  
  203.         // Checks to see if tiles are on the edge of the fill
  204.         private void m_UpdateDimension(ushort[] dim, Point p)
  205.         {
  206.             // x-coord
  207.             if (p.X < dim[0]) dim[0] = (ushort)p.X;
  208.             else if (p.X + 1 > dim[2]) dim[2] = (ushort)(p.X + 1);
  209.             // y-coord
  210.             if (p.Y < dim[1]) dim[1] = (ushort)p.Y;
  211.             else if (p.Y + 1 > dim[3]) dim[3] = (ushort)(p.Y + 1);
  212.         }
  213.  
  214.         // makes sure tile being checked isnt out of bounds for map
  215.         private bool m_ValidTile(Point p)
  216.         {
  217.             if (p.X >= 0 && p.X < 1024 && p.Y >= 0 && p.Y < 1024)
  218.                 return true;
  219.             return false;
  220.         }
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement