read

One of the main parts of Builderment is being able to move cargo/items around the world via transport belts. On the surface this might look easy or straightforward but underneath on a technical level it’s anything but. In this dev log I’m going to share some of those challenges and how Builderment attempts to solve them.

Moving Cargo

Let’s start simple, how does cargo move from one end of a single belt tile to the other? Each cargo is an entity with a world position and distance along the current segment. Another system creates a list of points in the world that the cargo will follow, in this case we only need 2 points, the start and end since it’s a straight path. During the game update loop the path segments update the position of each cargo by adding speed to the distance and recalculating the position.

// kCargoBaseSpeed = distance per second
// kCargoSpacing = minimum spacing between items
var beltDistance = belt.calculateMaxDistance()
for cargo in itemsInSegment {
    cargo.distance = min(cargo.distance + kCargoBaseSpeed, beltDistance)
    beltDistance = max(0, beltDistance - kCargoSpacing)
    cargo.position = path.position(at: cargo.distance)
}

What’s nice about this system is the path segment is separate from the actual belt tiles. So with some graph traversal we can create a few segments for all of the belts. This significantly increased performance for large numbers of belts.

Merging Cargo

Getting cargo to merge onto a belt was almost trivial. Each segment has a reference to the next segment and when cargo reaches the end of the segment it’s transferred to the next one if there is enough spacing. However this check is only done when cargo is at the absolute end of a segment so as you see there is some overlap before they get transferred to the next belt.

I have not implemented a solution for this yet but the idea is to check for spacing when the cargo is close to the end and see what is on the next belt. And also the other merging belts since they might have cargo close to the end.

Subterranean Cargo

Underground belts is really cool to see working. For each belt that enters an underground tile a separate path segment is created to the exit tile, if within N tiles. I added a flag to the segments that indicates if cargo should be hidden or not. Since the path segments are separate from the belt tiles this worked perfectly

The most difficult part of all this is creating the path segments from all the belt tiles and factories. Adding explicit input and output tiles to the buildings really helps though.

Follow and Subscribe 👍

This journey is just beginning. If you like what you read I’ll be posting more of it, hopefully weekly. Currently on twitter and instagram sharing images and videos from development. Reach out via email or come join discord!

Twitter @builderment
Instagram @builderment
Discord https://discord.gg/VkH4Nq3
Email contact@builderment.com

Blog Logo

Builderment


Published