Guest User

Untitled

a guest
Jul 21st, 2021
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using WindowsInput;
  5. using Patagames.Ocr;
  6. using System.Timers;
  7. using System.Drawing;
  8. using System.Linq;
  9. using WindowsInput.Native;
  10. using System.Windows.Forms;
  11. using System.Text.RegularExpressions;
  12. using System.Threading;
  13.  
  14. namespace SoD2_Reroll
  15. {
  16.     public partial class SoD2Reroll : Form
  17.     {
  18.         private System.Timers.Timer rerollTimer;
  19.         private readonly InputSimulator sim = new InputSimulator();
  20.         private bool reroll = false;
  21.         private bool firstIteration = false;
  22.         private short wait = 10000;
  23.         private readonly short interval = 50;
  24.         private Size resolution = new Size(1920, 1080);
  25.         private static readonly int heightTrait = 125, heightSkill = 35;
  26.  
  27.         //Array that holds the currently selected skills and traits in combo boxes
  28.         private string[] activeSkills = {"", "", ""};
  29.         private string[,] activeTraits = {{"", "", ""}, {"", "", ""}, {"", "", ""}};
  30.  
  31.         public SoD2Reroll()
  32.         {
  33.             InitializeComponent();
  34.         }
  35.  
  36.         private void SoD2Reroll_Load(object sender, EventArgs e)
  37.         {
  38.             //Arrays of all skills and traits obtainable by random characters, excludes red talon and heartland exlusives
  39.             string[] skills = Properties.Resources.skills.Split(new[] {'\n'}, StringSplitOptions.None);
  40.             string[] traits = Properties.Resources.traits.Split(new[] {'\n'}, StringSplitOptions.None);
  41.  
  42.             skills.Select(s => s.Trim())
  43.                 .Where(s => s.Length > 0)
  44.                 .ToList()
  45.                 .ForEach(s =>
  46.                 {
  47.                     cbSurvivor1.Items.Add(s);
  48.                     cbSurvivor2.Items.Add(s);
  49.                     cbSurvivor3.Items.Add(s);
  50.                 });
  51.  
  52.             traits.Select(s => s.Trim())
  53.                 .Where(s => s.Length > 0)
  54.                 .ToList()
  55.                 .ForEach(t =>
  56.                 {
  57.                     cbS1Trait1.Items.Add(t);
  58.                     cbS1Trait2.Items.Add(t);
  59.                     cbS1Trait3.Items.Add(t);
  60.                     cbS2Trait1.Items.Add(t);
  61.                     cbS2Trait2.Items.Add(t);
  62.                     cbS2Trait3.Items.Add(t);
  63.                     cbS3Trait1.Items.Add(t);
  64.                     cbS3Trait2.Items.Add(t);
  65.                     cbS3Trait3.Items.Add(t);
  66.                 });
  67.  
  68.             cbResolution.Items.AddRange(new string[]
  69.                 {"1280x720", "1360x720", "1366x720", "1600x900", "1920x1080", "2560x1440"});
  70.             cbResolution.SelectedIndex = 4;
  71.  
  72.             rerollTimer = new System.Timers.Timer(wait);
  73.             rerollTimer.Elapsed += RerollTimer_Elapsed;
  74.  
  75.             Stop();
  76.         }
  77.  
  78.         private void RerollTimer_Elapsed(object sender, ElapsedEventArgs e)
  79.         {
  80.             rerollTimer.Interval = interval;
  81.             lock (sender)
  82.             {
  83.                 if (firstIteration)
  84.                 {
  85.                     //Move cursor to the first button
  86.                     sim.Keyboard.KeyPress(VirtualKeyCode.UP);
  87.                     sim.Keyboard.KeyPress(VirtualKeyCode.LEFT);
  88.                     sim.Keyboard.KeyPress(VirtualKeyCode.LEFT);
  89.  
  90.                     firstIteration = false;
  91.                 }
  92.  
  93.                 //int top = (int)Math.Round(resolution.Height / 1.55);
  94.                 int topTrait = (int) Math.Round(resolution.Height / 3.65);
  95.                 int topSkill = (int) Math.Round(resolution.Height / 1.55);
  96.  
  97.                 string[] traitStrings = new string[3];
  98.                 string[] skillStrings = new string[3];
  99.                 bool[] matchingSurvivorSkills = {false, false, false};
  100.                 bool[] matchingSurvivorTraits = {false, false, false};
  101.  
  102.                 sim.Keyboard.KeyPress(VirtualKeyCode.UP);
  103.                 sim.Keyboard.KeyPress(VirtualKeyCode.LEFT);
  104.                 sim.Keyboard.KeyPress(VirtualKeyCode.LEFT);
  105.  
  106.                 //Use the screenreader to determine what text is in the screenshot
  107.                 using (OcrApi objOcr = OcrApi.Create())
  108.                 {
  109.                     objOcr.Init(Patagames.Ocr.Enums.Languages.English);
  110.  
  111.                     using (Bitmap img = Screenshot())
  112.                     {
  113.                         int leftSkill = 0;
  114.                         int leftTrait = 0;
  115.                         for (int i = 0; i < 3; ++i)
  116.                         {
  117.                             switch (i)
  118.                             {
  119.                                 //screenshot locations for different suvivors
  120.                                 case 0:
  121.                                     leftTrait = (int) Math.Round(resolution.Width / 5.47);
  122.                                     leftSkill = resolution.Width / 5;
  123.                                     break;
  124.                                 case 1:
  125.                                     leftTrait = (int) Math.Round(resolution.Width / 1.995);
  126.                                     leftSkill = (int) Math.Round(resolution.Width / 1.925);
  127.                                     break;
  128.                                 case 2:
  129.                                     leftTrait = (int) Math.Round(4.1 * resolution.Width / 5);
  130.                                     leftSkill = (int) Math.Round(4.2 * resolution.Width / 5);
  131.                                     break;
  132.                             }
  133.  
  134.                             string traitText = objOcr.GetTextFromImage(
  135.                                 img,
  136.                                 leftTrait,
  137.                                 topTrait,
  138.                                 375,
  139.                                 heightTrait
  140.                             );
  141.                             traitStrings[i] = Regex.Replace(traitText.Trim(), @"\s+", "").ToUpper();
  142.  
  143.                             string skillText = objOcr.GetTextFromImage(
  144.                                 img,
  145.                                 leftSkill,
  146.                                 topSkill,
  147.                                 375,
  148.                                 heightSkill
  149.                             );
  150.                             skillStrings[i] = Regex.Replace(skillText.Trim(), @"\s+", "").ToUpper();
  151.                         }
  152.  
  153.  
  154.                         for (int a = 0; a < 3; ++a)
  155.                         {
  156.                             if (activeSkills[a].Length == 0)
  157.                             {
  158.                                 matchingSurvivorSkills[a] = true;
  159.                             }
  160.                             else
  161.                             {
  162.                                 if (activeSkills[a].Trim().ToUpper() == skillStrings[a].Trim().ToUpper())
  163.                                 {
  164.                                     matchingSurvivorSkills[a] = true;
  165.                                 }
  166.                             }
  167.  
  168.                             List<string> wantedTraits = new List<string>();
  169.                             for (int at = 0; at < 3; ++at)
  170.                             {
  171.                                 if (activeTraits[a, at].Length > 0)
  172.                                 {
  173.                                     wantedTraits.Add(activeTraits[a, at].Trim().ToUpper());
  174.                                 }
  175.                             }
  176.  
  177.                             if (wantedTraits.Count == 0)
  178.                             {
  179.                                 matchingSurvivorTraits[a] = true;
  180.                             }
  181.                             else
  182.                             {
  183.                                 matchingSurvivorTraits[a] = wantedTraits.All(w => traitStrings[a].Contains(w));
  184.                             }
  185.                         }
  186.  
  187.  
  188.                         bool modified = false;
  189.                         for (int i = 0; i < 3; ++i)
  190.                         {
  191.                             if (matchingSurvivorSkills[i] == false || matchingSurvivorTraits[i] == false)
  192.                             {
  193.                                 sim.Keyboard.KeyPress(VirtualKeyCode.RETURN);
  194.                                 sim.Keyboard.KeyPress(VirtualKeyCode.RIGHT);
  195.                                 modified = true;
  196.                             }
  197.                         }
  198.  
  199.                         if (!modified)
  200.                         {
  201.                             rerollTimer.Enabled = false;
  202.                         }
  203.  
  204.                         sim.Keyboard.KeyPress(VirtualKeyCode.LEFT);
  205.                         sim.Keyboard.KeyPress(VirtualKeyCode.LEFT);
  206.                     }
  207.                 }
  208.             }
  209.         }
  210.  
  211.         private Bitmap Screenshot()
  212.         {
  213.             Bitmap img = new Bitmap(resolution.Width, resolution.Height);
  214.             Graphics g = Graphics.FromImage(img);
  215.             g.CopyFromScreen(0, 0, 0, 0, new Size(resolution.Width, resolution.Height), CopyPixelOperation.SourceCopy);
  216.             return img;
  217.         }
  218.  
  219.         private void EnableTimer()
  220.         {
  221.             ////Program waits 50ms before iterating to prevent misreading by the OCR
  222.             System.Threading.Thread.Sleep(50);
  223.  
  224.             //Timer is enabled to continue iterating if needed, program stops when CONTROL is pressed
  225.             if (ModifierKeys.HasFlag(Keys.Control))
  226.             {
  227.                 Stop();
  228.             }
  229.             else if (reroll)
  230.             {
  231.                 rerollTimer.Enabled = true;
  232.             }
  233.         }
  234.  
  235.         //Get the distance between two strings of different lengths
  236.         public static int ComputeStringDistance(string input1, string input2)
  237.         {
  238.             var bounds = new {Height = input1.Length + 1, Width = input2.Length + 1};
  239.             int[,] matrix = new int[bounds.Height, bounds.Width];
  240.             for (int height = 0; height < bounds.Height; height++)
  241.             {
  242.                 matrix[height, 0] = height;
  243.             }
  244.  
  245.             for (int width = 0; width < bounds.Width; width++)
  246.             {
  247.                 matrix[0, width] = width;
  248.             }
  249.  
  250.             for (int height = 1; height < bounds.Height; height++)
  251.             {
  252.                 for (int width = 1; width < bounds.Width; width++)
  253.                 {
  254.                     int cost = (input1[height - 1] == input2[width - 1]) ? 0 : 1;
  255.                     int insertion = matrix[height, width - 1] + 1;
  256.                     int deletion = matrix[height - 1, width] + 1;
  257.                     int substitution = matrix[height - 1, width - 1] + cost;
  258.                     int distance = Math.Min(insertion, Math.Min(deletion, substitution));
  259.                     if (height > 1 && width > 1 && input1[height - 1] == input2[width - 2] &&
  260.                         input1[height - 2] == input2[width - 1])
  261.                     {
  262.                         distance = Math.Min(distance, matrix[height - 2, width - 2] + cost);
  263.                     }
  264.  
  265.                     matrix[height, width] = distance;
  266.                 }
  267.             }
  268.  
  269.             return matrix[bounds.Height - 1, bounds.Width - 1];
  270.         }
  271.  
  272.         private void Stop()
  273.         {
  274.             //Reset and disable the timer
  275.             rerollTimer.Interval = wait;
  276.             reroll = false;
  277.             rerollTimer.Enabled = false;
  278.             ToggleButtons(true);
  279.         }
  280.  
  281.         private void ToggleButtons(bool toggle)
  282.         {
  283.             //Enable and disable buttons
  284.             btnStop.BeginInvoke((Action) delegate() { btnStop.Enabled = !toggle; });
  285.             btnStart.BeginInvoke((Action) delegate() { btnStart.Enabled = toggle; });
  286.         }
  287.  
  288.         private void SkillChanged(object sender, EventArgs e)
  289.         {
  290.             int index = 0;
  291.             ComboBox cb = (ComboBox) sender;
  292.  
  293.             switch (cb.Name)
  294.             {
  295.                 case "cbSurvivor1":
  296.                     index = 0;
  297.                     break;
  298.                 case "cbSurvivor2":
  299.                     index = 1;
  300.                     break;
  301.                 case "cbSurvivor3":
  302.                     index = 2;
  303.                     break;
  304.             }
  305.  
  306.             activeSkills[index] = Regex.Replace(cb.GetItemText(cb.SelectedItem).ToUpper(), @"\s+", "");
  307.         }
  308.  
  309.         private void TraitChanged(object sender, EventArgs e)
  310.         {
  311.             int i = 0, j = 0;
  312.             ComboBox cb = (ComboBox) sender;
  313.  
  314.             switch (cb.Name)
  315.             {
  316.                 case "cbS1Trait1":
  317.                     i = 0;
  318.                     j = 0;
  319.                     break;
  320.                 case "cbS1Trait2":
  321.                     i = 0;
  322.                     j = 1;
  323.                     break;
  324.                 case "cbS1Trait3":
  325.                     i = 0;
  326.                     j = 2;
  327.                     break;
  328.                 case "cbS2Trait1":
  329.                     i = 1;
  330.                     j = 0;
  331.                     break;
  332.                 case "cbS2Trait2":
  333.                     i = 1;
  334.                     j = 1;
  335.                     break;
  336.                 case "cbS2Trait3":
  337.                     i = 1;
  338.                     j = 2;
  339.                     break;
  340.                 case "cbS3Trait1":
  341.                     i = 2;
  342.                     j = 0;
  343.                     break;
  344.                 case "cbS3Trait2":
  345.                     i = 2;
  346.                     j = 1;
  347.                     break;
  348.                 case "cbS3Trait3":
  349.                     i = 2;
  350.                     j = 2;
  351.                     break;
  352.             }
  353.  
  354.             activeTraits[i, j] = Regex.Replace(cb.GetItemText(cb.SelectedItem).ToUpper(), @"\s+", "");
  355.         }
  356.  
  357.         private void btnStart_Click(object sender, EventArgs e)
  358.         {
  359.             cbResolution.Select(0, 0);
  360.             ToggleButtons(false);
  361.             firstIteration = true;
  362.             reroll = true;
  363.             rerollTimer.Enabled = true;
  364.         }
  365.  
  366.         private void SoD2Reroll_KeyDown(object sender, KeyEventArgs e)
  367.         {
  368.             e.Handled = true;
  369.         }
  370.  
  371.         private void btnStop_Click(object sender, EventArgs e)
  372.         {
  373.             Stop();
  374.         }
  375.  
  376.         private void nudWait_ValueChanged(object sender, EventArgs e)
  377.         {
  378.             wait = Convert.ToInt16(nudWait.Value * 100);
  379.         }
  380.  
  381.         private void cbResolution_SelectedIndexChanged(object sender, EventArgs e)
  382.         {
  383.             string[] dimensions = cbResolution.GetItemText(cbResolution.SelectedItem).Split('x');
  384.             resolution.Width = Convert.ToInt16(dimensions[0]);
  385.             resolution.Height = Convert.ToInt16(dimensions[1]);
  386.         }
  387.     }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment