Advertisement
bolodev

sod2 community generator

Nov 26th, 2023 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. import time
  2. import pyautogui
  3. import pygetwindow as gw
  4. import pytesseract
  5. import re
  6. import keyboard
  7.  
  8. def read_word(window_title, region):
  9. # Activate the specified window
  10. window = gw.getWindowsWithTitle(window_title)[0]
  11. window.activate()
  12.  
  13. # Calculate the absolute coordinates of the specified region inside the window
  14. absolute_region = (
  15. window.left + region[0],
  16. window.top + region[1],
  17. window.left + region[2],
  18. window.top + region[3]
  19. )
  20.  
  21. pyautogui.moveTo(absolute_region[0], absolute_region[1]-50)
  22. # Capture the screenshot based on the absolute coordinates
  23. screenshot = pyautogui.screenshot(region=(absolute_region[0], absolute_region[1], absolute_region[2]-absolute_region[0], absolute_region[3]-absolute_region[1]))
  24. screenshot.save("window_region_screenshot.png")
  25.  
  26. # Use OCR (Optical Character Recognition) to extract text from the image
  27. word = pytesseract.image_to_string(screenshot)
  28.  
  29. return word.strip()
  30.  
  31. def clean_and_compare_strings(compareTo, strList):
  32. # Remove special characters and numbers, leaving only letters
  33. clean_compareTo = re.sub('[^a-zA-Z]', '', compareTo)
  34. # Compare the cleaned strings ignoring case
  35. for str in strList:
  36. clean_str = re.sub('[^a-zA-Z]', '', str)
  37. if(clean_compareTo.lower() == clean_str.lower()):
  38. return str
  39. return None
  40.  
  41. def main():
  42. expected_words = ["lichenology", "Recycling", "Mechanics"]
  43. #expected_words = ["cooking", "medicine", "Mechanics"]
  44. window_title = "StateOfDecay2"
  45. xModifier = [0, 300, 590]
  46. survivor = 0
  47. survivorBool = [False, False, False]
  48. print(xModifier[survivor])
  49.  
  50. time.sleep(2)
  51. print('Starting...')
  52. completed_all_survivors = False
  53.  
  54. # check all 3 survivors to see if any is desired survivor is already there
  55. for i in range(3):
  56. # Read the word from the specified region of the window
  57. target_region = (290 + xModifier[i], 695, 400 + xModifier[i], 715)
  58. word = read_word(window_title, target_region)
  59.  
  60. result = clean_and_compare_strings(word, expected_words)
  61. # Check if the read word is correct
  62. if result != None:
  63. print("Word " + word + " is correct.")
  64. survivorBool[i] = True
  65. expected_words.remove(result)
  66. pyautogui.press('right')
  67. pyautogui.press('left')
  68. pyautogui.press('left')
  69.  
  70. completed_all_survivors = True
  71. for bool in survivorBool:
  72. if bool == False:
  73. completed_all_survivors = False
  74.  
  75. for i in range(3):
  76. if survivorBool[i] == False:
  77. survivor = i
  78. break
  79.  
  80. while completed_all_survivors != True:
  81. # Read the word from the specified region of the window
  82. target_region = (290 + xModifier[survivor], 695, 400 + xModifier[survivor], 715)
  83. word = read_word(window_title, target_region)
  84.  
  85. result = clean_and_compare_strings(word, expected_words)
  86. # Check if the read word is correct
  87. if result != None:
  88. print("Word " + word + " is correct.")
  89. expected_words.remove(result)
  90. survivorBool[survivor] = True
  91.  
  92. completed_all_survivors = True
  93. for bool in survivorBool:
  94. if bool == False:
  95. completed_all_survivors = False
  96.  
  97. while survivorBool[survivor] == True:
  98. survivor = survivor + 1
  99. if survivor >= len(survivorBool):
  100. break
  101.  
  102. pyautogui.press('right')
  103. if survivor >= 3:
  104. print("All survivors found, exiting.")
  105. break
  106. else:
  107. print(word)
  108. pyautogui.press('t')
  109.  
  110. time.sleep(0.01) # Adjust the sleep duration based on your requirements
  111. if keyboard.is_pressed('esc'):
  112. print("Escape key pressed. Exiting loop.")
  113. break
  114.  
  115. if __name__ == "__main__":
  116. main()
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement