Skip to content

Commit

Permalink
update explore behaviour (and scan local) for jump-gate exploration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Jan 31, 2024
1 parent a61fe55 commit 6e6f917
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 60 deletions.
69 changes: 35 additions & 34 deletions behaviours/explore_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from straders_sdk import SpaceTraders
from straders_sdk.ship import Ship
from straders_sdk.models import Waypoint, System
from straders_sdk.pathfinder import JumpGateRoute
import math
import logging
from straders_sdk.utils import try_execute_select, set_logging, waypoint_slicer
Expand Down Expand Up @@ -77,40 +78,24 @@ def _run(self):
st.system_jumpgate(jg, True)
path = self.pathfinder.astar(o_sys, d_sys, force_recalc=True)
else:
d_sys = self.route_to_unexplored_jumpgate()
if d_sys:
d_sys = st.systems_view_one(d_sys)
if not d_sys:
self.logger.error("Couldn't find system %s", d_sys)
self.end()
self.st.logging_client.log_ending(
BEHAVIOUR_NAME, ship.name, agent.credits
)
return
path = self.pathfinder.astar(o_sys, d_sys, True)
path, next_step = self.route_to_uncharted_jumpgate()
path: JumpGateRoute
if path:
d_sys = path.end_system
else:
tar_sys_sql = """SELECT w1.system_symbol, j.x, j.y, last_updated, jump_gate_waypoint
FROM public.mkt_shpyrds_systems_last_updated_jumpgates j
JOIN waypoints w1 on j.waypoint_symbol = w1.waypoint_symbol
order by last_updated, random()"""
resp = try_execute_select(self.connection, tar_sys_sql, ())

if not resp:
self.logger.error(
"Couldn't find any systems with jump gates! sleeping 10 mins then exiting!"
)
self.st.sleep(600)
return
target = resp[0]

# target = try_execute_select(self.connection, tar_sys_sql, ())[0]
d_sys = System(target[0], "", "", target[1], target[2], [])
path = self.pathfinder.astar(o_sys, d_sys, bypass_check=True)
d_sys = o_sys
self.logger.debug("Random destination selected: target %s", d_sys.symbol)

arrived = True
if ship.nav.system_symbol != d_sys.symbol:
arrived = self.ship_extrasolar_jump(d_sys.symbol, path)
if d_sys.symbol != o_sys.symbol:
if ship.nav.system_symbol != d_sys.symbol:
arrived = self.ship_extrasolar_jump(d_sys.symbol, path)
else:
local_gates = st.find_waypoints_by_type(d_sys.symbol, "JUMP_GATE")
if local_gates:
self.ship_intrasolar(local_gates[0].symbol)
arrived = self.st.ship_jump(ship, next_step)

if arrived:
self.scan_local_system()
else:
Expand All @@ -121,17 +106,33 @@ def _run(self):
# travel to target system
# scan target system

def route_to_unexplored_jumpgate(self):
def route_to_uncharted_jumpgate(
self,
) -> tuple["JumpRoute", Waypoint]:
source = self.st.find_waypoints_by_type(self.o_sys.symbol, "JUMP_GATE")
if not source:
self.st.sleep(SAFETY_PADDING)
return None

gate = self.st.system_jumpgate(source[0], True)
for connected_system in gate.connected_waypoints:
# print(connection)\
pass

source = self.st.waypoints_view_one(source[0].symbol, True)
if not source.is_charted:
return (None, source)
gate = self.st.system_jumpgate(source)
for connected_waypoint in gate.connected_waypoints:
wayp = self.st.waypoints_view_one(connected_waypoint)
if (
"UNCHARTED" in [t.symbol for t in wayp.traits] and not wayp.is_charted
) and not wayp.under_construction:
route = self.pathfinder.astar(
self.o_sys, self.st.systems_view_one(wayp.system_symbol)
)
if route:
return (route, connected_waypoint)
print(connected_waypoint)
return (None, None)


if __name__ == "__main__":
from dispatcherWK16 import lock_ship
Expand Down
60 changes: 34 additions & 26 deletions behaviours/generic_behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,16 @@ def scan_local_system(self):

# situation - when loading the waypoints, we get the systemWaypoint aggregate that doesn't have traits or other info.
# QUESTION
st.waypoints_view(current_system_sym, True)
target_wayps = []
wayps = st.waypoints_view(current_system_sym, True)

interesting_wayps = [
p
for sym, p in wayps.items()
if p.type in ("PLANET", "MOON", "ORBITAL_STATION", "JUMP_GATE")
]
target_wayps = [
w for w in interesting_wayps if "UNCHARTED" in [t.symbol for t in w.traits]
]
if ship.seconds_until_cooldown < 60:
wayps = st.ship_scan_waypoints(ship)
if wayps:
Expand All @@ -720,15 +728,6 @@ def scan_local_system(self):
shipyards = st.find_waypoints_by_trait(current_system_sym, "SHIPYARD") or []

gate = st.find_waypoints_by_type_one(current_system_sym, "JUMP_GATE")
uncharted_planets = st.find_waypoints_by_trait(
ship.nav.system_symbol, "UNCHARTED"
)
if uncharted_planets:
target_wayps.extend(
p
for p in uncharted_planets
if p.type in ("PLANET", "MOON", "ORBITAL_STATION")
)
target_wayps.extend(marketplaces)
target_wayps.extend(shipyards)
target_wayps.append(gate)
Expand All @@ -745,21 +744,20 @@ def scan_local_system(self):
self.ship_intrasolar(wayp_sym)
self.sleep_until_arrived()
trait_symbols = [trait.symbol for trait in waypoint.traits]
should_chart = False
if "UNCHARTED" in trait_symbols:
if len(trait_symbols) == 1:
self.sleep_until_ready()
wayps = st.ship_scan_waypoints(ship)
if wayps:
wayps = [w for w in wayps if w.symbol == waypoint.symbol]
waypoint = wayps[0]
should_chart = True
if "UNCHARTED" in trait_symbols or not waypoint.is_charted:
self.sleep_until_ready()
wayps = st.ship_scan_waypoints(ship)
if wayps:
wayps = [w for w in wayps if w.symbol == waypoint.symbol]
waypoint = wayps[0]

st.ship_create_chart(ship)
st.waypoints_view_one(wayp_sym, True)
# chart the waypoint, refresh the waypoint and trait_symbols
if "MARKETPLACE" in trait_symbols:
market = st.system_market(waypoint, True)
if market:
for listing in market.listings:
if listing.symbol in ("VALUABLES"):
should_chart = False
print(
f"item: {listing.symbol}, buy: {listing.purchase_price} sell: {listing.sell_price} - supply available {listing.supply}"
)
Expand All @@ -768,11 +766,9 @@ def scan_local_system(self):
if shipyard:
for ship_type in shipyard.ship_types:
print(ship_type)
if ship_type in ("VALUABLES"):
should_chart = False
if waypoint.type == "JUMP_GATE":
jump_gate = st.system_jumpgate(waypoint, True)
self.pathfinder._graph = self.pathfinder.load_jump_graph_from_db()
st.system_jumpgate(waypoint, True)
self.pathfinder._jump_graph = self.pathfinder.load_jump_graph_from_db()
self.pathfinder.save_graph()

def ship_extrasolar_jump(self, dest_sys_sym: str, route: JumpGateRoute = None):
Expand Down Expand Up @@ -1081,3 +1077,15 @@ def sleep_until_arrived(self):
self = bhvr
st = self.st
ship = self.ship

current_system = st.systems_view_one(ship.nav.system_symbol)

factions = bhvr.st.list_factions()
for faction in factions:
if not faction.is_recruiting:
continue
target_system = st.systems_view_one(faction.headquarters)
route = bhvr.pathfinder.astar(current_system, target_system)
if not route:
continue
print(faction.headquarters)

0 comments on commit 6e6f917

Please sign in to comment.