GTA RP Nopixel Thermite Fast Click Bot
Tags: python, bot, gta, computervision
Published: 2023-03-19 17:20:36

If you've played GTA RP Nopixel, you know it has very challenging minigames, one of which is called "Thermite".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wekxg8mzvv82avz5kqk4.png)

In this minigame you've to memorize the pattern in 5 seconds only, after which you're asked to repeat it very quickly, and as you might imagine this is pretty challenging.

But no worries, this is where my script comes to the rescue, all you've to do is press a hotkey to the record the pattern, then press another one to make the script repeat what it just recorded.

Clone the Github repo [here](https://github.com/Possessed-Player/GTARPthermiteBot).

Check out the bot video [here](https://youtu.be/rNOSrVSqzcE).

## Code Explained

**Line (12) record function**
```python
def record():
    img_bgr = pag.screenshot()
    img_bgr = np.array(img_bgr)
    img_rgb = img_bgr[:,:,::-1].copy()
    img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
    template = cv.imread('obj/' + WIMG + '.jpg',0)
    w, h = template.shape[::-1]
    
    res = cv.matchTemplate(img_gray, template,cv.TM_SQDIFF_NORMED)
    mn,mx,mnLoc,mxLoc = cv.minMaxLoc(res)
```

This function is called when the user presses 
"CTRL + SHIFT + ALT + O"

Which initiates the pattern recording process.

It works by taking a screenshot of the whole screen using pyautogui.screenshot function, then looks for parts of the screen that looks exactly like a white square, by comparing a template image of the white square to the screenshot using opencv "cv.matchTemplate" function.

**Line (23) record function cont.**
```python
    print(mx)
    threshold = 0.05
    loc = np.where(res <= threshold)
    points = []
    for pt in zip(*loc[::-1]):
        close = False
        for p in points:
            if abs(pt[0] - p[0]) < 10 and abs(pt[1] - p[1]) < 10:
                close = True
                break
        
        if close: 
            continue
        points.append(pt)
        cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
    
    global Whites
    Whites = points
```

In this part of the record function it looks for matches, and saves them in the global list "Whites", after processing them into a screen position tuples.

**Line (43) go function**
```python
def go():
    global Whites
    print(Whites)
    for pt in Whites:
        pag.click(pt[0] + 30, pt[1] + 30)
        sleep(0.05)
```

This function is executed when the user presses
"CTRL + SHIFT + ALT + P"

It works by clicking on recorded screen locations, which are saved by the 'record' function.

It goes over the global list "Whites", and clicks on each screen location, using pyautogui.click function.

```python
def kb_listener():
    def on_release(key):
        print(str(key))
        if str(key) == "<79>":
            record()
        elif str(key) == "<80>":
            go()

    with Listener(on_release=on_release) as listener:
        listener.join()
```

This is our usual keyboard listener function, which executes 'record' and 'go' functions when the user presses the right combinations(Check pynput documentation for more info about this function).

**Line (62) script main code**
```python
if __name__ == '__main__':
    WIMG = input("Type 7 for 7x7, 8 for 8x8 then press Enter\n")
    if not (WIMG in ("7", "8")):
        print("Type either 7 or 8 to run this bot, Exiting...")
        quit()
		
    print("Running...")
	
    while True:
        kb_listener()
```

This part is responsible of running the whole script and loads the global variable 'WIMG', which a selector for the minigame square size, 7x7 and 8x8 are supported.


Thanks for reading.