Path of Exile Mini-Map Navigation & Loot Collection AI
Program Overview
I'll create a comprehensive solution that monitors Path of Exile gameplay, learns to navigate using the mini-map, and collects loot based on color detection. This will use reinforcement learning to improve navigation decisions over time.
import cv2
import numpy as np
import pyautogui
import time
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import random
import tkinter as tk
from tkinter import ttk
from collections import deque
import threading
import os
from PIL import Image, ImageTk
# Constants
MINIMAP_REGION = (0, 0, 0, 0) # (x, y, width, height) - to be set in UI
GAME_CENTER = (0, 0) # To be set in UI
MAX_EPISODE_LENGTH = 120 # 2 minutes in seconds
MOVEMENT_MULTIPLIER = 1.0 # To be set in UI
BACKTRACK_THRESHOLD = 5 # Number of seconds with no map discovery before backtracking
DATA_FOLDER = "training_data"
MODEL_FOLDER = "models"
# Create necessary directories
os.makedirs(DATA_FOLDER, exist_ok=True)
os.makedirs(MODEL_FOLDER, exist_ok=True)
# Global variables
running = False
loot_colors = [] # List of RGB values for loot
undiscovered_color_range = {
'lower': np.array([0, 0, 0]),
'upper': np.array([255, 255, 255])
}
visited_locations = deque(maxlen=50) # For backtracking
last_discovery_time = 0
model = None
Navigation DQN Model
class DQN(nn.Module):
def __init__(self, input_shape, num_actions):
super(DQN, self).__init__()
# Input will be the processed minimap
self.conv1 = nn.Conv2d(1, 32, kernel_size=8, stride=4)
self.conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2)
self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=1)
# Calculate the size after convolutions
conv_output_size = self._get_conv_output(input_shape)
self.fc1 = nn.Linear(conv_output_size, 512)
self.fc2 = nn.Linear(512, num_actions) # Actions are movement directions
def _get_conv_output(self, shape):
bs = 1
x = torch.zeros(bs, *shape)
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = self.conv3(x)
x = F.relu(x)
return int(np.prod(x.size()))
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
return self.fc2(x)
Game Interaction Functions
def capture_minimap():
"""Capture the minimap region of the screen"""
minimap = pyautogui.screenshot(region=MINIMAP_REGION)
return cv2.cvtColor(np.array(minimap), cv2.COLOR_RGB2BGR)
def find_player_position(minimap):
"""Find the player's position on the minimap (typically center)"""
h, w = minimap.shape[:2]
return (w // 2, h // 2)
def detect_undiscovered_areas(minimap):
"""Detect undiscovered areas on the minimap using color range"""
hsv = cv2.cvtColor(minimap, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, undiscovered_color_range['lower'], undiscovered_color_range['upper'])
# Find contours of undiscovered areas
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if not contours:
return []
# Find centroids of contours
undiscovered_points = []
for contour in contours:
M = cv2.moments(contour)
if M["m00"] != 0:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
undiscovered_points.append((cx, cy))
return undiscovered_points
def detect_loot():
"""Detect loot on screen based on configured colors"""
screen = pyautogui.screenshot()
screen_np = np.array(screen)
loot_positions = []
for color in loot_colors:
# Create mask for this color with some tolerance
lower_bound = np.array([max(0, c - 15) for c in color])
upper_bound = np.array([min(255, c + 15) for c in color])
mask = cv2.inRange(screen_np, lower_bound, upper_bound)
# Find contours of loot
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Get centroids of contours
for contour in contours:
if cv2.contourArea(contour) > 20: # Minimum area to avoid noise
M = cv2.moments(contour)
if M["m00"] != 0:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
loot_positions.append((cx, cy))
return loot_positions
def calculate_movement(player_pos, target_pos):
"""Calculate movement vector from player to target"""
dx = target_pos[0] - player_pos[0]
dy = target_pos[1] - player_pos[1]
# Calculate angle and distance
angle = np.arctan2(dy, dx)
distance = np.sqrt(dx*dx + dy*dy)
return angle, distance
def move_to_point(target_x, target_y, movement_multiplier=1.0):
"""Move the player towards a point in the game world"""
# Translate minimap coordinates to screen coordinates
game_x = GAME_CENTER[0] + (target_x - MINIMAP_REGION[0] - MINIMAP_REGION[2]//2) * movement_multiplier
game_y = GAME_CENTER[1] + (target_y - MINIMAP_REGION[1] - MINIMAP_REGION[3]//2) * movement_multiplier
# Ensure within screen bounds
screen_width, screen_height = pyautogui.size()
game_x = max(0, min(game_x, screen_width))
game_y = max(0, min(game_y, screen_height))
# Move mouse and click
pyautogui.moveTo(game_x, game_y)
pyautogui.click()
# Add to visited locations
visited_locations.append((game_x, game_y))
# Small delay to allow game to respond
time.sleep(0.1)
def backtrack():
"""Move back to a previously visited location"""
if len(visited_locations) > 1:
# Pop the current location
visited_locations.pop()
# Get previous location
prev_location = visited_locations[-1]
# Move to the previous location
pyautogui.moveTo(prev_location[0], prev_location[1])
pyautogui.click()
# Small delay
time.sleep(0.2)
return True
return False