Skip to content

Commit

Permalink
add behaviour that takes probes to the nearest uncharted gate and cha…
Browse files Browse the repository at this point in the history
…rts it.
  • Loading branch information
Ctri-The-Third committed Feb 2, 2024
1 parent f61026f commit 677f27b
Show file tree
Hide file tree
Showing 5 changed files with 1,014 additions and 3 deletions.
6 changes: 6 additions & 0 deletions behaviour_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
BEHAVIOUR_NAME as BHVR_EVOLVE_SUPPLY_CHAIN,
)

from behaviours.explore_jumpgate_network import (
ExploreJumpgates,
BEHAVIOUR_NAME as BHVR_EXPLORE_JUMP_GATE_NETWORK,
)

behaviours_and_classes = {
BHVR_EXTRACT_AND_GO_SELL: ExtractAndGoSell,
BHVR_RECEIVE_AND_FULFILL: ReceiveAndFulfillOrSell_3,
Expand Down Expand Up @@ -154,4 +159,5 @@
BHVR_WARP_TO_SYSTEM: WarpToSystem,
BHVR_TRADE_BEST_INTRASOLAR: TradeBestInSystem,
BHVR_EVOLVE_SUPPLY_CHAIN: EvolveSupplyChain,
BHVR_EXPLORE_JUMP_GATE_NETWORK: ExploreJumpgates,
}
147 changes: 147 additions & 0 deletions behaviours/explore_jumpgate_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# takes a tradegood. Then trades that to ensure that the market is LIMITED
# if the export activity hits RESTRICTED, it switches to finding profitable import goods until that clears.

# script is happy to work to 0 profit, but will not work at a loss.


import time
import sys


sys.path.append(".")
from behaviours.generic_behaviour import Behaviour
import logging
from straders_sdk.ship import Ship
from straders_sdk.models import Market, Waypoint
from straders_sdk.utils import waypoint_slicer, set_logging, try_execute_select
from straders_sdk.constants import SUPPLY_LEVELS
from behaviours.generic_behaviour import Behaviour
import random

BEHAVIOUR_NAME = "EXPLORE_JUMPGATES"
SAFETY_PADDING = 180


class ExploreJumpgates(Behaviour):
def __init__(
self,
agent_name,
ship_name,
behaviour_params: dict = ...,
config_file_name="user.json",
session=None,
) -> None:
super().__init__(
agent_name,
ship_name,
behaviour_params,
config_file_name,
session,
)
self.agent = self.st.view_my_self()
self.logger = logging.getLogger(BEHAVIOUR_NAME)
self.start_system = self.behaviour_params.get("start_system", None)

def run(self):
self.ship = self.st.ships_view_one(self.ship_name)
self.sleep_until_ready()
self.st.logging_client.log_beginning(
BEHAVIOUR_NAME,
self.ship.name,
self.agent.credits,
behaviour_params=self.behaviour_params,
)
self._run()
self.end()

def end(self):
if self.ship:
self.st.logging_client.log_ending(
BEHAVIOUR_NAME, self.ship.name, self.st.view_my_self().credits
)
super().end()

def default_params_obj(self):
return_obj = super().default_params_obj()
return_obj["start_system"] = "X1-SYST"
return return_obj

def _run(self):
st = self.st
ship = self.ship # = st.ships_view_one(self.ship_name, True)
ship: Ship
agent = self.agent
if not self.start_system:
self.start_system = ship.nav.system_symbol
current_gate = self.st.find_waypoints_by_type_one(
self.start_system, "JUMP_GATE"
)

starting_gate, next_gate = self.find_uncharted_waypoints([current_gate.symbol])
starting_gate_sys = self.st.systems_view_one(waypoint_slicer(starting_gate))

arrived = self.ship_extrasolar_jump(starting_gate_sys.symbol)
if not arrived:
self.logger.error(f"Failed to jump to {starting_gate_sys.symbol}")
st.sleep(SAFETY_PADDING)
return
arrived = self.ship_intrasolar(starting_gate)
if not arrived:
self.logger.error(f"Failed to navigate to {starting_gate}")
st.sleep(SAFETY_PADDING)
return
self.st.ship_jump(ship, next_gate)
self.st.ship_create_chart(ship)
self.pathfinder.validate_and_refresh_jump_graph(starting_gate_sys, next_gate)
# find the jumpgate in the system

# your code goes here

def find_uncharted_waypoints(
self, jumpgates_symbols: list[str], checked_symbols: list[str] = None
) -> tuple[str, str]:
"returns a tuple of two waypoints, the second is the nearest uncharted waypoint - the first is the known link that leads to it."
new_gates = []
if not checked_symbols:
checked_symbols = []
# sort jumpgate_symbols randomly
jumpgates_symbols.sort(key=lambda x: random.random())
for gate in jumpgates_symbols:
wayp = self.st.waypoints_view_one(gate)
jumpgate = self.st.system_jumpgate(wayp)
checked_symbols.append(gate)
for connected_jumpgate in jumpgate.connected_waypoints:
destination_wayp = self.st.waypoints_view_one(connected_jumpgate)
# skip gates being built, we can't go there
if destination_wayp.under_construction:

checked_symbols.append(connected_jumpgate)
continue
destination = self.st.system_jumpgate(destination_wayp)
if not destination:
return (gate, connected_jumpgate)
if connected_jumpgate not in checked_symbols:
new_gates.append(connected_jumpgate)
if new_gates:
return self.find_uncharted_waypoints(new_gates, checked_symbols)


#
# to execute from commandline, run the script with the agent and ship_symbol as arguments, or edit the values below
#
if __name__ == "__main__":
from dispatcherWK16 import lock_ship

set_logging(level=logging.DEBUG)
agent = sys.argv[1] if len(sys.argv) > 2 else "CTRI-U-"
ship_number = sys.argv[2] if len(sys.argv) > 2 else "1"
ship = f"{agent}-{ship_number}"
behaviour_params = {
"priority": 3,
}

bhvr = ExploreJumpgates(agent, ship, behaviour_params or {})

lock_ship(ship, "MANUAL", 60 * 24)
bhvr.run()
lock_ship(ship, "MANUAL", 0)
12 changes: 11 additions & 1 deletion behaviours/trade_best_intrasolar.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(
session,
)
self.agent = self.st.view_my_self()
self.target_sys = self.behaviour_params.get("target_sys")
self.logger = logging.getLogger(BEHAVIOUR_NAME)

def default_params_obj(self):
Expand All @@ -53,6 +54,7 @@ def run(self):
self.agent.credits,
behaviour_params=self.behaviour_params,
)

self.sleep_until_ready()

self._run()
Expand All @@ -63,6 +65,13 @@ def _run(self):
ship = self.ship # = st.ships_view_one(self.ship_name, True)
ship: Ship
agent = self.agent

if self.ship.nav.system_symbol != self.target_sys:
arrived = self.ship_extrasolar_jump(self.target_sys)
if not arrived:
self.logger.info(f"Failed to jump to {self.target_sys}")
self.st.sleep(SAFETY_PADDING)
return
self.sleep_until_arrived()
best_trade = self.get_best_trade()
if not best_trade:
Expand Down Expand Up @@ -123,10 +132,11 @@ def get_best_trade(self):

set_logging(level=logging.DEBUG)
agent = sys.argv[1] if len(sys.argv) > 2 else "CTRI-U-"
ship_number = sys.argv[2] if len(sys.argv) > 2 else "11"
ship_number = sys.argv[2] if len(sys.argv) > 2 else "1"
ship = f"{agent}-{ship_number}"
behaviour_params = {
"priority": 3,
"target_sys": "X1-DM68",
}

bhvr = TradeBestInSystem(agent, ship, behaviour_params or {})
Expand Down
3 changes: 1 addition & 2 deletions conductorWK25.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@


class BehaviourConductor:

"""This behaviour manager is for when we're in low fuel desperate situations"""

def __init__(
Expand Down Expand Up @@ -312,7 +311,7 @@ def system_quarterly_update(self, system: "ConductorSystem"):
system.system_symbol,
1,
self.current_agent_symbol,
{"ship_type": system._next_ship_to_buy},
{"ship_type": system._next_ship_to_buy, "priority": 3},
self.next_quarterly_update + timedelta(minutes=15),
best_ship.name,
)
Expand Down
Loading

0 comments on commit 677f27b

Please sign in to comment.