Skip to content

Instantly share code, notes, and snippets.

@svemat01
Last active January 3, 2024 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svemat01/6496df636043dffc5c3336013b461819 to your computer and use it in GitHub Desktop.
Save svemat01/6496df636043dffc5c3336013b461819 to your computer and use it in GitHub Desktop.

Components

Train Task (DATA)

The data for what action should be taken by the system. Exists in two different types, collection and delivery.

Collection

Contains the station name and a rough estimate of how many items are going to be picked up.

Delivery

Contains the station name and a list of items to be picked up

Example type implementation

type TrainTask = {
	type: 'collection';
	station: string;
	/**
	* Rough estimate of how many items are going to be moved
	* Unused for now, but could be used to determine how many carts are needed in the future
	*/
	count: number;
} | {
	type: 'delivery';
	station: string;
	items: Item[];
}

Assigned Train Task (DATA)

Extenstion of the Train Task with additional fields for current status and last updated status time

  1. pending-acception train depot has been given task but train depot hasn't assigned it to a train yet. If this status is still set X time after last updated the task should be unassigned and added back to unassigned queue.
  2. pending-departure train has been assigned and schedule given but has not yet left the Train Depot
  3. departure-failed train failed to leave the Train Depot, likely that station name is invalid. A train found not in the Train Depot with this status should have it's schedule overwritten to return to the Train Depot.
  4. departed train has left the Train Depot and is on it's way to either station or Logistics Hub depending on Task type
  5. loading-cargo train has arrived at pickup location and is being loaded
  6. cargo-loaded cargo is done loading and train is to depart to delivery location
  7. unloading-cargo train as arrived at delivery location and is being unloaded.
  8. cargo-unloaded train is done unloading and is scheduled to go back to Train Depot and pick up the next request

Example type implementation

type AssignedTrainTask = TrainTask & {
	status: 'X' | 'Y' | 'Z';
	updated: number;
}

Coordinator (COMPUTER)

The brain for all actions, a computer somewhere in the world working via rednet. Keeps track of all data for the entire system

Data

Unassigned Tasks

List of TrainTasks that have not yet been assigned to a Train.

Assigned Tasks

A table/record keyed train id to AssignedTrainTask

Logistics Hub (COMPUTER, LOCATION)

The main storage location where all items passes thru. When a train arrives it should look up the train id by requesting the data from the Coordinator and figure out if it's a collection or delivery train.

If it's a collection train it should already have picked up items from the production site and the Logistics Hub should therefore just empty the entire train.

If it's a delivery train it should gather the requested items from the Logistics Hubs storage system and load it onto the train.

Train Depot (COMPUTER, LOCATION, MULTIPLE)

Train storage location. Cargo trains wait here for a task to be given to them. On entry they should pass by an emptying station to make sure that they are empty before going for new task.

The Train Depot regularly checks for new tasks if the station is ready. It does this via 3 triggers.

  1. By a redstone input from a train observer, aka from when a train arrives at the station.
  2. On broadcast received that a new task is available
  3. On a set interval incase no task was available when train arrived and that it missed the broadcast. The interval should not be too short to avoid spamming the Coordinator with too many requests due to multiple train depots existing.

Site (COMPUTER, LOCATION, MULTIPLE)

Commonly various factories thru out the map, they should keep track of their required items for operation and send out requests when they go too low on said items. They should also monitor their production buffer to see when they need to request item pickups to offload items to the Logistics Hub

Message Types (DATA)

Task Creation

  • NewTask
    • Request: Train to Coordinator
    • Responds With: OK
    • Broadcast: TaskAvailable (from Coordinator)

Task Assignment

  • RequestTask
    • Request: Train to Coordinator
    • Responds With: TaskAssigned (to Train)

Task Status

  • TaskUpdateStatus
    • Request: Train to Coordinator
  • GetTaskStatus
    • Request: Anyone to Coordinator
    • Responds With: TaskStatus (to Anyone)

Task List

  • GetTaskList

    • Request: Anyone to Coordinator
    • Responds With: TaskList (to Anyone)
  • GetTaskListFromStation

    • Request: Anyone to Coordinator
    • Responds With: TaskList (to Anyone)
  • GetTask

    • Request: Anyone to Coordinator
    • Responds With: Task (to Anyone)

Responses

  • OK
    • Response: Anyone to Anyone
  • TaskAvailable
    • Broadcast: Coordinator to Everyone
  • TaskAssigned
    • Response: Coordinator to Train
  • TaskStatus
    • Response: Coordinator to Anyone
  • TaskList
    • Response: Coordinator to Anyone
  • Task
    • Response: Coordinator to Anyone

Flow

Examples of how a request would go

Collection train

  1. A Site monitors their production output and notices that the items have overpassed the limit
  2. The Site sends out a collection request to the Coordinator with it's station name and a rough estimate of how many items it expects to need to offload.
  3. The coordinator adds the request to it's unassigned list and broadcasts that a new task is available.
  4. Train Depot gets triggered to attempt to fetch a task, if no task is available it waits for next trigger.
  5. It creates a schedule depending on the request type, renames the train to include the train id, gives it the schedule and updates the status at the Coordinator to pending-departure
  6. It waits for the train to depart, if it fails to do so within the set timeout, it clears the schedule and updates the request status to departure-failed. If the train leaves successfully it updates the request status to departed
  7. When the train arrives at the Site it does a lookup on the train id and identifies it as a collection train.
  8. It starts filling the train and since the train schematics is set to leave on cargo idle the train waits until it's full.
  9. When train then arrives at the Logistics Hub, the Logistics Hub does a lookup on the train id and also identifies it as a collection train.
  10. The Logistics Hub then starts offloading everything on the train into it's storage system.
  11. Since the train schematics is set to leave on cargo idle the train waits until it's empty.
  12. The train then goes to the Train Depot to pickup a new task.

Delivery train

  1. A Site monitors their required resources for production and notices that the items have gone below the limit
  2. The Site sends out a delivery request to the Coordinator with it's station name and a list of the items it wishes for.
  3. Same as the Collection Train for steps 3-6
  4. When the train arrives at the Logistics Hub it does a lookup on the train id and identifies it as a delivery train.
  5. It starts collecting the requested items from the storage system and feeds the train, since this is more complex the train is set to leave when the station is powered which the Logistics Hub handles once done.
  6. When train then arrives at the Site, it does a lookup on the train id and also identifies it as a delivery train.
  7. Same as the Collection Train for steps 10-13 except it's at the site instead of the Logistics Hub.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment