from nullptr.util import * from nullptr.analyzer import find_gas, find_metal class GeneralError(AppError): pass class General: def __init__(self, context): self.store = context.store self.api = context.api self.c = context agents = self.store.all('Agent') self.agent = next(agents, None) self.phases = { 'init': self.phase_startup, 'probes': self.phase_probes, 'trade': self.phase_trade, 'mine': self.phase_mine, 'siphon': self.phase_siphon, 'rampup': self.phase_rampup, 'gate': self.phase_gate } def setup(self): self.create_default_crews() def find_shipyard(self, stype): occ = [s.location.symbol for s in self.store.all('Ship') if s.status != 'IN_TRANSIT'] best_price = -1 best_yard = None for shipyard in self.store.all('Shipyard'): if stype in shipyard.prices: price = shipyard.prices[stype] if shipyard.symbol in occ: if best_yard is None or price < best_price: best_yard = shipyard best_price = price return best_yard, best_price def maybe_purchase(self, stype, role): sy, price = self.find_shipyard(stype) if sy is None: return False traders = [s for s in self.store.all('Ship') if s.role == 'trader'] safe_buffer = len(traders) * 100000 + 100000 #print(safe_buffer, price, sy) if self.agent.credits < safe_buffer + price: return # cant afford it! ship = self.c.api.purchase(stype, sy) ship.role = role def tick(self): phase = self.agent.phase if phase not in self.phases: raise GeneralError('Invalid phase') hdl = self.phases[phase] new_phase = hdl() if new_phase: self.agent.phase = new_phase def phase_startup(self): # * first pricing info # * probe at shipyard that sells probes ag = self.agent.symbol command = self.store.get('Ship', f'{ag}-1') probe = self.store.get('Ship', f'{ag}-2') if command.role is None: command.role = 'probe' if probe.role is None: probe.role = 'sitter' system = command.location.system markets = list(self.store.all_members(system, 'Marketplace')) discovered = len([m for m in markets if m.last_prices > 0]) if discovered > len(markets) // 2: return 'probes' def phase_probes(self): ag = self.agent.symbol command = self.store.get('Ship', f'{ag}-1') # * probes on all markets if command.role != 'trader': command.role = 'trader' self.c.captain.init_mission(command, 'none') self.maybe_purchase('SHIP_PROBE', 'sitter') sitters = [s for s in self.store.all('Ship') if s.role == 'sitter'] markets = [m for m in self.store.all('Marketplace')] if len(sitters) >= len(markets): return 'trade' def phase_trade(self): self.maybe_purchase('SHIP_LIGHT_HAULER', 'trader') traders = list([s for s in self.store.all('Ship') if s.role == 'trader']) if len(traders) >= 19: return 'mine' def phase_mine(self): # metal mining crew pass def phase_siphon(self): # siphon crew pass def phase_rampup(self): # stimulate markets for gate building pass def phase_gate(self): # build the gate pass def create_default_crews(self): system = self.api.agent.headquarters.system gas_w = find_gas(self.c, system) metal_w = find_metal(self.c, system) metal = self.store.get('Crew', 'METAL', create=True) metal.site = metal_w metal.resources = ['COPPER_ORE','IRON_ORE','ALUMINUM_ORE'] gas = self.store.get('Crew', 'GAS', create=True) gas.site = gas_w gas.resources = ['HYDROCARBON','LIQUID_HYDROGEN','LIQUID_NITROGEN'] return [gas, metal]