Skip to content

Instantly share code, notes, and snippets.

@zafercavdar
Last active March 29, 2023 05:04
Show Gist options
  • Save zafercavdar/86206d0a79586c6c4398bfcf62e9880c to your computer and use it in GitHub Desktop.
Save zafercavdar/86206d0a79586c6c4398bfcf62e9880c to your computer and use it in GitHub Desktop.
PhBot HWT script
from math import sqrt
from phBot import log, set_training_radius, get_drops, generate_path, move_to, get_position, set_training_position, start_bot, stop_bot, get_monsters
import phBotChat
import QtBind
SCRIPT_ACTIVE = False
FLOOR_INDEX = None
REGISTERED_WALK = []
floors = {
"Beginner": 1,
"Intermediate": 2,
"Advanced": 3
}
uniques = [
{
"name": "Sphinx",
"status": "LIVE",
"coordinates": [
(10995, 4594, 661),
(0, 0, 0),
(8458, 5937, 661),
],
},
{
"name": "Nephthys",
"status": "LIVE",
"coordinates": [
(11122, 4290, 818),
(0, 0, 0),
(8627, 5621, 818),
],
},
{
"name": "Sekhmet",
"status": "LIVE",
"coordinates": [
(11126, 4905, 845),
(0, 0, 0),
(8630, 6247, 845),
],
},
{
"name": "Horus",
"status": "LIVE",
"coordinates": [
(11651, 4636, 840),
(0, 0, 0),
(9156, 5979, 840),
],
},
{
"name": "Osiris",
"status": "LIVE",
"coordinates": [
(11763, 4408, 697),
(0, 0, 0),
(9268, 5732, 697),
],
}
]
# GUI
gui = QtBind.init(__name__, 'Holy Water Temple')
QtBind.createLabel(gui, "Floor", 10, 13)
floorSelector = QtBind.createCombobox(gui, 40, 10, 100, 20)
for name, index in floors.items():
QtBind.append(gui, floorSelector, name)
QtBind.createLabel(gui, "Unique selection", 10, 35)
checkboxes = []
for ind, unique in enumerate(uniques):
cb = QtBind.createCheckBox(gui, '', unique["name"], 10, 60 + ind * 15)
QtBind.setChecked(gui, cb, True)
checkboxes.append(cb)
start = QtBind.createButton(gui, 'start_function', 'Start', 10, 60 + len(uniques) * 15 + 10)
stop = QtBind.createButton(gui, 'stop_function', 'Stop', 90, 60 + len(uniques) * 15 + 10)
# Helper functions
def get_x():
return round(get_position()["x"])
def get_y():
return round(get_position()["y"])
def get_z():
return round(get_position()["z"])
def get_region():
return int(get_position()["region"])
def calculate_distance_to(x, y):
cx = get_x()
cy = get_y()
dx = (cx - x) ** 2
dy = (cy - y) ** 2
return sqrt(dx + dy)
def get_next_unique():
for unique in uniques:
if unique["status"] in ["LIVE", "WALKING", "KILLING"]:
return unique
return None
def update_unique_status(name, status):
global uniques
for unique in uniques:
if unique["name"] == name:
unique["status"] = status
break
def start_bot_here():
x = get_x()
y = get_y()
z = get_z()
region = get_region()
set_training_position(region, x, y, z)
set_training_radius(70.0)
start_bot()
def ensure_at_coordinate(nx, ny):
return calculate_distance_to(nx, ny) < 3
def any_ac_on_the_floor():
items = get_drops()
any_ac = any([item["name"] == "Arena Coin" for item in items.values()])
if any_ac:
log(f"There are still some AC on the floor.")
return any_ac
def event_loop():
# every 500 ms
global REGISTERED_WALK, SCRIPT_ACTIVE
if not SCRIPT_ACTIVE:
return
next_unique = get_next_unique()
if next_unique is None:
log("All uniques are killed. Job completed")
exit()
monsters = get_monsters()
mons_names = list(map(lambda m: m["name"], monsters.values()))
unique_name = next_unique['name']
mons_count = mons_names.count(unique_name)
if next_unique["status"] == "LIVE":
if mons_count > 0:
log(f"Monster is here, count = {mons_count}")
log("Starting bot")
update_unique_status(unique_name, "KILLING")
start_bot_here()
return
else:
log(f"{unique_name} is NOT here. Generating walk script (it may take a while)")
stop_bot()
unique_coords = next_unique["coordinates"][FLOOR_INDEX - 1]
distance = calculate_distance_to(unique_coords[0], unique_coords[1])
path = (generate_path(unique_coords[0], unique_coords[1]) or [])
if distance >= 10 and len(path) == 0:
log(f"Failed generating path to next unique. Stopping script")
SCRIPT_ACTIVE = False
return
else:
update_unique_status(unique_name, "WALKING")
log(f"Succesfully generated path with {len(path)} steps")
REGISTERED_WALK = path
return
if next_unique["status"] == "KILLING":
if mons_count > 0 or any_ac_on_the_floor():
log(f"Monster count: {mons_count}")
log("Still killing unique")
return
else:
log("Killed unique")
update_unique_status(unique_name, "KILLED")
stop_bot()
return
if next_unique["status"] == "WALKING":
if len(REGISTERED_WALK) == 0:
log("Arrived to destination")
# update_unique_status(unique_name, "LIVE")
update_unique_status(unique_name, "KILLING")
start_bot_here()
return
else:
target_coord = REGISTERED_WALK[0]
# special handling for caves
if len(target_coord) == 3:
target_coord = (target_coord[1], target_coord[2])
if ensure_at_coordinate(target_coord[0], target_coord[1]):
REGISTERED_WALK.pop(0)
log(f"Reached to {target_coord}")
return
else:
log(f"Moving to {target_coord}")
move_to(float(target_coord[0]), float(target_coord[1]), 0.0)
return
def start_function():
global SCRIPT_ACTIVE, FLOOR_INDEX, uniques, checkboxes
floor_name = QtBind.text(gui, floorSelector)
floor_index = floors[floor_name]
log(f'Selected {floor_index}th floor in HTW. Starting ...')
FLOOR_INDEX = floor_index
for cb_ind, cb in enumerate(checkboxes):
if not QtBind.isChecked(gui, cb):
log(f"Bot will skip {uniques[cb_ind]['name']}")
uniques[cb_ind]["status"] = "SKIPPED"
SCRIPT_ACTIVE = True
def stop_function():
global SCRIPT_ACTIVE, FLOOR_INDEX
stop_bot()
SCRIPT_ACTIVE = False
FLOOR_INDEX = None
log('[%s] Loaded' % __name__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment