Repository navigation
Reading the Sea
Vanilla has no place for the wind's direction, for waves or for currents, so Rough Seas publishes them from its own code. This page is for a mod that reads them: a helm that shows the sea, a fish that swims with the current, a ship that rides our swell.
Rough Seas keeps the sea twice, for two different jobs.
The sea state is the sea as a number: at any point, how hard the wind blows and from where, how high the waves run and how long between them, how much of a storm is there, and where the current flows. The server computes it, and everything that has consequences comes from it: the waves that come aboard a vessel, the breakers that strike it, the current that carries it. It knows no single wave. A wave that reaches a vessel is drawn by chance, once a wave period, from the state's sea as the bed at the vessel's place grows, turns and breaks it, and two vessels side by side meet different waves.
The drawn sea is the moving surface a client draws, built from the sea map the server sends it and the world's clock. The server builds the same surface where it needs one: over each region with a player in or near the water, or a vessel nobody steers. Its crests have a place and a moment, so a hull can ride them: lift on a crest, roll with its slope. Every client draws the same surface: two players in one region see the same crests. It is not the wave the server struck a vessel with.
So a mod decides by the sea state and shows by the drawn sea. A helm, a fish's swimming, a sail's push, anything that harms on the server: the sea state. A hull's pose on the screen, and the water that sways it to and fro: the drawn sea. Nothing on the server should wait for a crest a client reports.
Where a decision is about what the screen shows, the server builds the drawn sea itself from the same map and clock, and the client sends nothing. Ours does so for a swimmer: under a crest a swimmer has no air, in a trough the head is clear, and the server reads whether a player is in the water by the same surface the client floats them on. It does so for a vessel nobody steers too, so the waves carry it as they carry one a client steers. Every vessel's waves aboard and breakers are read there too, from WaveHere at its place.
Compile against RoughSeas.dll from our release, referenced but not shipped in your mod (<Private>false</Private>).
If Rough Seas is optional for you, check api.ModLoader.IsModEnabled("roughseas") and keep every use of our types in a class of its own that you create only then. A method that names one of our types fails to compile at run time when our assembly isn't there, even if the line never runs.
var sea = api.ModLoader.GetModSystem<SeaWeatherSystem>();
SeaState state = sea.At(x, z);
SeaCurrent current = sea.CurrentAt(x, z);SeaWeatherSystem, in RoughSeas.SeaAndWeather, implements ISeaWeather. Positions are absolute block coordinates, and the answer is for now: on the server exactly, on a client a few seconds behind.
The wind is the background wind of the region and season, plus every storm that covers the point. The waves grow from that wind as a real sea grows: higher the longer the open water upwind, the longer the wind has blown (up to three days of it), and the deeper the water. So a sea doesn't follow a gust: it builds behind a storm and dies down slowly after it. The current is the ocean's circulation, as on Currents. How each is built, and from which measurements, is on The science behind it.
On the server the state is computed where you ask. It isn't free: growing the waves walks back through the wind's last three days. Ours read it twice a second for each vessel, which is as often as the sea changes. Read it on a timer of your own, not every frame.
On a client the state is read from a map the server sends: the nine map regions around its player, a cell every 32 blocks, refreshed every few seconds. A client blends the last two maps it received, so its answer trails the server's by a few seconds and is cheap to ask. Where it holds no map younger than half a game hour, a client knows nothing and answers zeros, a water temperature of zero included. A client mod reads the sea near its own player.
ISeaWeather:
| Member | What it is |
|---|---|
At(x, z) |
the sea state there, a SeaState
|
CurrentAt(x, z) |
the current and the water's temperature there, a SeaCurrent
|
Clock |
the time the sea keeps, a SeaClock: The sea's time
|
SeaState:
| Field | Unit | What it is |
|---|---|---|
WindX, WindZ
|
m/s | the wind, toward east and toward south |
WindSpeed |
m/s | its strength |
WindFromDegrees |
° | where it blows from: 0 north, 90 east |
Beaufort |
0–12 | its force on the Beaufort scale |
WaveHeight |
m | the significant height of the sea: the mean of its highest third |
WavePeriod |
s | its peak period, the time between waves |
StormPresence |
0–1 | how much of a storm is here, all storms together |
StormCode |
the strongest storm's type (squall, gale, hurricane), or null; on the server only |
SeaCurrent:
| Field | Unit | What it is |
|---|---|---|
X, Z
|
m/s | the current, toward east and toward south |
Speed |
m/s | its strength |
TowardDegrees |
° | where it flows to: 0 north, 90 east |
WaterCelsius |
°C | the sea's surface temperature |
WarmthCelsius |
°C | how much the current warms the water, cooling below zero; on the server only, null on a client |
Three things to keep in mind when you use the numbers:
-
A height is the sea, not a wave. Single waves scatter around
WaveHeight: most lower, one in a hundred half again as high (Every wave is different). A mod that throws waves at something draws them the same way, one perWavePeriod. Ours do it withRoughSeas.Storms.PassingWaves, for vessels and swimmers alike: a wave a dominant period of the world's clock, its height from the Rayleigh spread ofWaveHere, in surf breaking when it is among the tallest the breaking share cuts off, and elsewhere by the wind's whitecap share. -
The wind is in m/s here. Vanilla's
GetWindSpeedAtgives the same wind on its own scale, 1.0 for 25 m/s (Reading the weather). -
The state doesn't know about danger. Wind, waves and currents run whatever the world settings. A mod that harms something by them reads the setting for that harm first, as ours do:
api.World.Config.GetBool("hullDamage", true), with the codes on World settings.
A client mod that shows the sea at its player reads the state on a tick listener; once a second is plenty, as the map behind it changes every few seconds.
var sea = capi.ModLoader.GetModSystem<SeaWeatherSystem>();
capi.Event.RegisterGameTickListener(_ =>
{
var pos = capi.World.Player.Entity.Pos;
var state = sea.At(pos.X, pos.Z);
var current = sea.CurrentAt(pos.X, pos.Z);
helm.Show(state.Beaufort, state.WindFromDegrees, state.WaveHeight, current.TowardDegrees, current.Speed);
}, 1000);The current is a velocity in m/s. Vanilla's water drag slows whatever swims every tick, so adding the current's speed to an entity's motion once isn't enough, and adding all of it every tick is too much. Add, each tick, as much of the current as the drag has just taken away, so the drag pulls the entity toward the current's speed rather than to rest:
var current = sea.CurrentAt(pos.X, pos.Z);
var carried = (1 - Math.Pow(physics.WaterDragValue, deltaTime * 33)) / 60;
pos.Motion.X += current.X * carried;
pos.Motion.Z += current.Z * carried;physics is the entity's EntityBehaviorPassivePhysics, whose drag is applied 33 times a second; 60 turns m/s into blocks per tick. An entity of yours that moves by other physics adds the current to its own motion its own way.
Our vessels don't use this: their behaviour carries the hull by the water's drag on its velocity relative to the current, on the server for a vessel nobody steers and on the rider's client for a steered one (Building a vessel). A mod that sails its own vessels adds CurrentAt to its own motion on the side that moves them.
Everything of ours at sea keeps one clock, sea.Clock, on either side: the waves that come aboard, rain into a bilge, leaks and seeping, lightning, a hull soaking or losing its grease, and the drawn waves. It is the world's calendar, so it stands still under /time stop, while the game is paused and while the calendar is stopped (/time calendarspeedmul 0), races while everyone sleeps, and leaps when the calendar is moved on with /time add. Motion keeps vanilla's tick, as vanilla's boats do: rowing, sailing, a current's push and a mooring's hold go on while time stands still. A mechanic of yours at sea that keeps the same clock happens in step with ours.
It tells time in two units. Game hours suit what the calendar counts: days afloat, rain by the hour. Seconds at vanilla's pace suit what the real world times in seconds, a wave's period or a leak's litres a second: they are real seconds at the normal speed of time, 120 to a game hour in a default world, and they stand and race with the calendar.
SeaClock:
| Member | What it is |
|---|---|
Hours |
the game time, in total game hours; 0 before the calendar exists |
Seconds |
the same time in seconds at vanilla's pace |
SecondsAt(hours), HoursAt(seconds)
|
one turned into the other |
StartWatch() |
a new SeaClock.Watch, for one mechanic of one thing |
A watch's Lap() gives the time since its last lap, a Lap:
| Field | What it is |
|---|---|
Hours |
the game hours since the last lap: none at the first lap, nor when the calendar was set back |
Seconds |
the same time in seconds at vanilla's pace |
A mechanic keeps a watch of its own and laps it each time it acts, on its tick or a timer, and takes the whole lap, as vanilla's food rots through a /time add: what builds up and what happens now and then alike. A net that fills while it soaks, and is washed by every wave that reaches it:
watch ??= sea.Clock.StartWatch();
var lap = watch.Lap();
fish += FishPerHour * lap.Hours;
var state = sea.At(pos.X, pos.Z);
sinceWave += lap.Seconds;
for (; state.WavePeriod > 0 && sinceWave >= state.WavePeriod; sinceWave -= state.WavePeriod) Wash(net, state);Lap at least twice a second, as ours do. While everyone sleeps that is an hour of game time a lap, and what a mechanic reads at the end of a longer one, a storm or the waves, may have passed within it.
Our drawn sea for other mods is the client's:
IDrawnSea? drawn = capi.ModLoader.GetModSystem<StormsModSystem>().DrawnSea;StormsModSystem and IDrawnSea live in RoughSeas.Storms. DrawnSea is null on the server and before the client side starts.
The server raises the waves. Every three game hours it takes, for each region a player is in, the highest sea around it (the region and its eight neighbours) as it stood when those hours began, and sends that sea's direction and period with the sea map. A client draws a few trains of waves crossing near that direction, of that length, and the new trains take over from the last hours' over half a minute. Trains less than a heading (20°) or a quarter of their length apart are the same trains, and nothing changes.
Their height is the highest sea around the player now, and each place draws its own share of it, from the state there: a sheltered bay draws a lower swell than the open sea beside it, and a calm lake draws none.
Near the shore the waves read the seabed around the player: they grow as the water shallows, turn toward the shore and break where the depth can't carry them. Every member below answers with those waves, and with no swell over land; the sheet that runs up a beach is drawn on the screen alone. Water also lies under the beach, there only to carry that surface: a position holding a full solid block is land, so ask OpenWater for water, not the fluid layer.
All of it follows from the world's clock and the state the server sends, so every client in a region draws the same waves, and a player who joins sees what the others already see. A player who sails into another region sees its waves take over the same way, without a jump. The waves keep the world's time as vanilla's swaying plants do: real time at the normal pace, still while time stands still or the game is paused, fast while everyone sleeps.
Our own vessels are posed by the same calls this page describes, so a ship of yours that uses them rides exactly as ours do.
| Member | What it is |
|---|---|
Swell |
whether any swell is drawn now |
SwellAt(x, z) |
whether swell is drawn there now: none outside the waves' field or where the sea is calm |
Seconds |
the time the waves run on, in seconds: the world's clock at vanilla's pace, the same on every client |
HeightAt(x, z) |
the surface's height above still water there now, in metres, the reflection off faces and the short waves included |
WaveHere(x, z) |
the waves there now, a WaveReading: their significant height over the bed, the dominant period, where they run as the bed turns them, the depth they feel, the bed's slope, the share of them breaking, and the height of the sea coming into that breaking water |
PoseOf(x, z, yaw, length, width) |
the pose of a hull there now, a SeaPose: on the reflection, not on the short waves a hull averages out |
HullVelocityAt(x, z, lift, draft, yaw, length, beam) |
the water's horizontal velocity there now, in metres per second of Seconds, as the waves move it, averaged over a hull's underwater body; the reflection doesn't push it |
HullGradientAt(x, z, lift, draft, yaw, length, beam) |
how that velocity changes from one metre to the next over the same body, per second of Seconds, a FlowGradient (XX, XZ, ZZ; Moving(x, z) is its change along a velocity) |
RiseVelocityAt(x, z, below) |
the water's vertical velocity there now, in metres per second of Seconds, below metres under the drawn surface: rising ahead of a crest, sinking behind it. At the surface it is how fast the water drawn there rises, the reflection and the short waves included; deeper down it fades as under a real wave, to nothing at the bed |
RiseAccelerationAt(x, z, below) |
the water's vertical acceleration there now, in metres per second squared of Seconds, below metres under the drawn surface: slowing under a crest, gathering in a trough |
PoseOf samples the surface at the ends and sides of a hull of length by width blocks centred at x, z, and turns those four heights into how the hull would lie on them. So the hull's size does part of the work: a long hull on short waves has its bow and stern on different crests and barely rocks, while a small one takes every wave. Its length runs along the shape's X axis. yaw is its heading in radians as its renderer turns it: the entity's Pos.Yaw plus its shape's rotateY, which is in degrees, so Pos.Yaw + rotateY * GameMath.DEG2RAD.
HullVelocityAt is the to and fro of the water under the waves as a hull feels it: forward, along each wave's travel, under its crest, back in its trough. The water's pressure on a hull sums over its underwater body, so the velocity is averaged over its length along its heading yaw and its beam across, as PoseOf takes them, and over its draft under the height lift it floats at, in metres over still water. A hull short against the waves takes the water's velocity; one as long as a wave, or lying along short waves, feels much less of it.
HullGradientAt is what a hull feels when it moves through that water rather than with it. A hull moving with the water at u keeps moving with it; one moving at u + s is pushed by −(s·∇)u on top of the water's own change, s its velocity through the water, ∇u this gradient. On a wave's front face the water ahead runs slower than the water behind, so a hull running with the wave is pushed on down the face: near the wave's speed it stays on the face and is carried, as a surfer is. Ours step a hull this way, and a swimmer too.
Its seconds are the waves' (Seconds), not your tick's: they race while everyone sleeps and run faster under /time speed. To move a hull by it each tick, scale it by the waves' seconds in the tick first, sea.Clock.SecondsIn(dt, world.Calendar.SpeedOfTime) / dt, as ours do. Then one wave carries a hull as far at any speed of time. RiseVelocityAt and HullGradientAt keep the same seconds, and RiseAccelerationAt takes that rate squared.
RiseVelocityAt and RiseAccelerationAt are what lifts a body in the water with the waves: ours read them for a swimmer, at the middle of the part of the body under water (Swimming). The formulas are on The science of a body in the water.
SeaPose:
| Field | Unit | What it is |
|---|---|---|
Hold |
0–1 | how much of the drawn swell is under the hull: 1 where the sea runs as high as the highest drawn around the player, less where it runs lower, as in a sheltered bay, 0 where none is drawn |
Lift |
m | how far the surface lifts it, added to its model's height |
Roll |
rad | its roll, about its length, as the renderer's xangle
|
Pitch |
rad | its pitch, about its width, as the renderer's zangle
|
Lift, Roll and Pitch are the water's: where a hull lies on it exactly as the surface lies. Two things turn that into a hull that looks right:
-
Blend by
Hold. Where no swell is drawn, your vessel rocks however it rocked before. Ours keep vanilla's rocking there, and take the pose where the swell holds them fully, blending between byHold. So a boat rowed from a calm river into the surf passes from one to the other without a jump. -
Scale it as your hull would. Ours multiply roll and pitch by a vessel's
rollResponseandpitchResponse: a raft lies flatter on the water than its slope, a narrow canoe rolls more.
The pose is only the picture, and it changes as the waves move: read it every frame, in your render path.
A real hull doesn't take the water's pose at once. It has mass, and lifts, rolls and pitches toward the water on periods of its own: a light canoe follows a long swell as the water lies, while a heavy, deep hull lets a short chop pass under it and rolls slowly on its own. HullResponse, in RoughSeas.Storms, gives a hull those periods. Ours keep one per vessel and pass every pose through it; yours can do the same.
| Member | What it is |
|---|---|
HullResponse.Of(lengthMetres, beamMetres, draftMetres, depthMetres, config) |
the response of a hull of these dimensions, floating at this draft, its waterplane taken as the ellipse of its length and beam |
new HullResponse(heavePeriodSeconds, rollPeriodSeconds, config) |
one on periods you give; a period of 0 follows the water as it lies |
Follow(pose, seconds) |
the pose the hull takes seconds after the last call, chasing pose; where no swell is drawn it comes to rest at once |
HeavePeriodSeconds, RollPeriodSeconds
|
its periods; set them as its draft changes |
HullResponse.HeavePeriod(kg, addedKg, kgPerMetre) |
the heave and pitch period of a hull of this mass, dragging addedKg of water and displacing kgPerMetre for each metre it sinks, in seconds |
HullResponse.RollPeriod(lengthMetres, beamMetres, draftMetres, depthMetres) |
its roll period, in seconds |
Follow returns the pose with its Lift, Roll and Pitch answered and its Hold as it was, so blend by it as before. Ours refresh the periods from the hull's draft every frame, so a laden hull answers slower than an empty one. A long gap between calls, as when a hull returns to the water, is caught up as one period at most, so the hull doesn't leap. Where the periods come from is on The science of the hull.
config is the response block of our config/storms.json, reached as StormsModSystem.Config.Response: heaveDamping (0.3) and rollDamping (0.1), how fast a swing dies, as shares of critical damping. The water a hull drags as it heaves is RoughSeas.Water.AddedMass: OfPlate(halfLength, halfBeam, kgPerCubicMetre) or OfWaterplane(squareMetres, halfBeam, kgPerCubicMetre), in kilograms, and OfSpheroid(length, cubicMetres, kgPerCubicMetre) along and across for a body wholly under water; a vessel with a hull of ours gives hers as IBuoyant.AddedKg.
A vessel that is an EntityBoat, or derives from one, needs none of this: give it roughseas:seaforces and, while it swims, it is posed as ours are (Taking the sea). Give it roughseas:currentdrift as well and the water under the waves moves it, by the client that steers it or by the server while nobody does: HullVelocityAt over its hull, at the heave it is drawn at, plus its own velocity through that water, which the forces on it change (Currents). A floating hull displaces its own mass, so lying still it takes the water's velocity whatever it weighs. The formulas are on The science of a body in the water. Its seats, its selection box and its rider's position follow our pose whatever draws it; its hull follows when the renderer is vanilla's EntityShapeRenderer, or derives from it and calls the base loadModelMatrix. So an EntityBoat with a renderer of its own derives it from vanilla's, rather than posing itself against seats we already lift.
The calls above are for a vessel that isn't an EntityBoat and poses itself. Each frame, in its render path:
var storms = capi.ModLoader.GetModSystem<StormsModSystem>();
response ??= HullResponse.Of(length, beam, draft, depth, storms.Config.Response);
var rotateY = entity.Properties.Client.Shape?.rotateY ?? 0;
var yaw = entity.Pos.Yaw + rotateY * GameMath.DEG2RAD;
var pose = response.Follow(storms.DrawnSea!.PoseOf(entity.Pos.X, entity.Pos.Z, yaw, length, beam), frameSeconds);
lift = (float)pose.Lift;
roll = (float)GameMath.Lerp(ownRoll, pose.Roll * rollResponse, pose.Hold);
pitch = (float)GameMath.Lerp(ownPitch, pose.Pitch * pitchResponse, pose.Hold);The lift moves only what you draw with it. The server's vessel doesn't rise, so ours lift their seats and selection box by the same amount on the client, and take it back out of the position a rider's client sends the server. A vessel of yours that carries riders does the same, or its riders sit below a hull that rose.
What the vessel meets on the server, waves aboard and breakers on the hull, our behaviours take care of for any vessel that carries them, from the waves at its place.
Getting started
The sea
Your vessel
Leaving it
Rivers and lakes
At sea