Route extractor ore through transporters

This commit is contained in:
Loïc Wisniewski 2026-08-13 16:38:41 +02:00
commit 5755568346
3 changed files with 141 additions and 140 deletions

View file

@ -767,26 +767,13 @@ func rotate_formation(radians: float) -> void:
formation[i] = formation[i].rotated(radians)
# --- Extractors and their haulers -----------------------------------------
# --- Extractors ------------------------------------------------------------
# An extractor sits on a planet and runs a single automated hauler back and
# forth to the mothership, delivering ore on each arrival.
#
# The round trip is SIMULATED HERE, in the autoload, rather than by any
# node: it advances whether the player is in space, on a planet's surface,
# or far enough away that nothing about the route is loaded. `progress` is
# a fraction of one full there-and-back cycle, so a hauler's drawn position
# is derived from the same number that decides when ore is delivered - the
# visual arrival and the payout cannot drift apart, because they are the
# same event.
#
# Entries are {"position": Vector2 (the planet's position in SPACE),
# "planet_name": String, "progress": float}.
const HAULER_SPEED: float = 260.0
const HAULER_YIELD: int = 4
# Progress value at which the hauler reaches the mothership: exactly half
# way through the cycle, the outbound leg being the first half.
const HAULER_ARRIVAL: float = 0.5
# Extractors mine into their OWN local stock. They do not own a magical
# hauler: ore becomes spendable only when a hub-stationed transporter picks
# up a full load and physically delivers it to the mothership. Entries are
# {"position", "planet_name", "stock", "mining_progress"}.
const EXTRACTOR_RATE: float = 2.0 # ore per second
var extractors: Array = []
@ -805,7 +792,8 @@ func add_extractor(planet_name: String, at_position: Vector2) -> void:
extractors.append({
"position": at_position,
"planet_name": planet_name,
"progress": 0.0,
"stock": 0,
"mining_progress": 0.0,
})
# An extractor is built from SPACE, with the fleet parked on the planet -
# so the Planet node is already in the tree with its _ready() long since
@ -814,49 +802,23 @@ func add_extractor(planet_name: String, at_position: Vector2) -> void:
extractor_added.emit(planet_name)
func get_hauler_route_time(extractor: Dictionary) -> float:
# Full cycle: out to the mothership and back again.
var mothership: Variant = get_mothership_position()
if mothership == null:
return 0.0
var distance: float = extractor.position.distance_to(mothership)
if distance < 1.0:
return 0.0
return (distance * 2.0) / HAULER_SPEED
func get_hauler_position(extractor: Dictionary) -> Variant:
# Where the hauler is right now, derived purely from `progress`. Returns
# null when there is no route (no mothership built yet).
var mothership: Variant = get_mothership_position()
if mothership == null:
return null
var t: float = extractor.progress
if t <= HAULER_ARRIVAL:
return extractor.position.lerp(mothership, t / HAULER_ARRIVAL)
return mothership.lerp(extractor.position, (t - HAULER_ARRIVAL) / (1.0 - HAULER_ARRIVAL))
func extractor_stock(index: int) -> int:
if index < 0 or index >= extractors.size():
return 0
return int(extractors[index].get("stock", 0))
func _process(delta: float) -> void:
# Runs in the autoload, so extraction keeps earning - and queued orders
# keep completing - while the player is on a planet's surface or nowhere
# near either.
# Runs in the autoload, so mining and transport continue while the player
# is on a planet's surface or nowhere near either.
_advance_production(delta)
_advance_transporters(delta)
if extractors.is_empty():
return
for e in extractors:
var cycle: float = get_hauler_route_time(e)
if cycle <= 0.0:
continue # nothing to deliver to yet
var before: float = e.progress
e.progress += delta / cycle
# Ore lands the instant the hauler reaches the mothership - the same
# instant get_hauler_position() puts it there.
if before < HAULER_ARRIVAL and e.progress >= HAULER_ARRIVAL:
rocks += HAULER_YIELD
while e.progress >= 1.0:
e.progress -= 1.0
e.mining_progress = float(e.get("mining_progress", 0.0)) + delta * EXTRACTOR_RATE
var whole: int = int(floor(float(e.mining_progress)))
if whole > 0:
e.stock = int(e.get("stock", 0)) + whole
e.mining_progress = float(e.mining_progress) - whole
_advance_transporters(delta)
# --- transporters (#19) ---------------------------------------------------
@ -882,6 +844,8 @@ const TRANSPORTER_IDLE: String = "idle"
const TRANSPORTER_TO_MOTHERSHIP: String = "to_mothership"
const TRANSPORTER_TO_BUILDING: String = "to_building"
const TRANSPORTER_RETURNING: String = "returning"
const TRANSPORTER_TO_EXTRACTOR: String = "to_extractor"
const TRANSPORTER_TO_MOTHERSHIP_FROM_EXTRACTOR: String = "extractor_to_mothership"
var transporters: Array = []
@ -980,10 +944,42 @@ func _transporter_inbound_to(building_id: int) -> bool:
return false
func _transporter_inbound_to_extractor(index: int) -> bool:
for t in transporters:
if t.state == TRANSPORTER_TO_EXTRACTOR \
or t.state == TRANSPORTER_TO_MOTHERSHIP_FROM_EXTRACTOR:
if int(t.get("extractor_index", -1)) == index:
return true
return false
func _dispatch_transporters() -> void:
var mothership: Variant = get_mothership_position()
if mothership == null:
return # nowhere to load from
# A distant extractor is worth visiting only once it has a full hold ready.
# This keeps the transporter system meaningful: mining is local until a
# vessel physically carries a researched-sized load home.
var load: int = transporter_load_limit()
for i in range(extractors.size()):
var extractor: Dictionary = extractors[i]
if int(extractor.get("stock", 0)) < load or _transporter_inbound_to_extractor(i):
continue
var chosen_extractor: Dictionary = {}
var best_extractor_distance: float = INF
for t in transporters:
if t.state != TRANSPORTER_IDLE:
continue
var d: float = transporter_position(t).distance_squared_to(extractor.position)
if d < best_extractor_distance:
best_extractor_distance = d
chosen_extractor = t
if chosen_extractor.is_empty():
return
chosen_extractor.extractor_index = i
chosen_extractor.target_id = 0
_begin_leg(chosen_extractor, transporter_position(chosen_extractor), extractor.position,
TRANSPORTER_TO_EXTRACTOR)
for b in buildings:
if outstanding_cost(int(b.id)) <= 0:
continue
@ -1049,6 +1045,26 @@ func _arrive(t: Dictionary) -> void:
t.carrying = 0
t.target_id = 0
_begin_leg(t, here, hub_pos if hub_pos != null else here, TRANSPORTER_RETURNING)
TRANSPORTER_TO_EXTRACTOR:
var extractor_index: int = int(t.get("extractor_index", -1))
if extractor_index < 0 or extractor_index >= extractors.size():
_begin_leg(t, here, hub_pos if hub_pos != null else here, TRANSPORTER_RETURNING)
return
var extractor: Dictionary = extractors[extractor_index]
var load: int = transporter_load_limit()
if int(extractor.get("stock", 0)) < load:
t.extractor_index = -1
_begin_leg(t, here, hub_pos if hub_pos != null else here, TRANSPORTER_RETURNING)
return
extractor.stock = int(extractor.stock) - load
t.carrying = load
_begin_leg(t, here, get_mothership_position(), TRANSPORTER_TO_MOTHERSHIP_FROM_EXTRACTOR)
TRANSPORTER_TO_MOTHERSHIP_FROM_EXTRACTOR:
# Bank only on arrival; while in flight the ore remains in carrying.
rocks += int(t.carrying)
t.carrying = 0
t.extractor_index = -1
_begin_leg(t, here, hub_pos if hub_pos != null else here, TRANSPORTER_RETURNING)
TRANSPORTER_RETURNING:
t.state = TRANSPORTER_IDLE
t.progress = 1.0