# A Journey Through Time

<img src="docs/img/icon.png" width="96" align="right" alt="Penrose triangle">

A Fabric mod for Minecraft 26.3. It does two things:

1. **It keeps the world's history.** The server takes a snapshot of the world **once a day**. Every chunk is stored only
   once, so a day costs only what changed that day.
2. **It opens passages through time.** An operator picks a span of time *x* and two spots. Two iron doors appear there,
   each with a vortex inside. Behind door **I** lies the world as it was **x days ago**, behind door **II** the world
   as it was **2x days ago**. Both past worlds are real, separate worlds (via the [Worlds](https://modrinth.com/mod/mc-worlds)
   multiverse mod), and the doors stand at the same place in all three times. So from any one of the three times, the
   other two are a single step away.

![The farmyard of Gut Funkenhof today: the barn burns, two passage doors stand in the yard](docs/img/map_present.jpg)

## Passages through time

```
/journey open 7d                    doors 5 blocks in front of you: I -> 7 days ago, II -> 14 days ago
/journey open 36h <door I> <door II>   at two spots you choose (each: the lower left block of the door)
/journey list                       open passages, their doors and the times they lead to
/journey close <id>                 closes a passage: travellers are pulled back, the past worlds are deleted
/journey go <id> <0|1|2>            sends you (an operator) straight to the present, x ago or 2x ago
```

Time can be given as `7` or `7d` (days), `36h`, `2w`, `90m` or `30s`. The mod picks the snapshot taken closest to
*x* ago for door I, and the one closest to *2x* ago (and older than the first) for door II. The chat tells you which
dates you got. At least two snapshots are needed. Up to three passages may be open at a time (`maxPassages`).

| You are in... | Door **I** leads to | Door **II** leads to |
|---|---|---|
| the present | x days ago | 2x days ago |
| x days ago | **the present** | 2x days ago |
| 2x days ago | x days ago | **the present** |

| Shut | Open |
|---|---|
| ![Two Sic Mundus doors in the farmyard, shut](docs/img/map_doors.jpg) | ![The same doors open, a violet and an amber vortex in the doorways](docs/img/map_vortices.jpg) |

**The doors.** Every passage puts a door in the present and the same door in both past worlds. A **shut door seals
its vortex**: nothing shows and nobody gets through. Right-click the door to open it; both leaves swing out and the
vortex swirls in the doorway (violet for I, amber for II). Walk in and you come out of the same door in the other
time. That door swings open on its own and the time and date appear on screen. Near an open door, your action bar
shows where it leads.

**The past is frozen at the moment the passage opens.** When it opens, the two past worlds are built from the
snapshots. After that, they are never updated: new daily snapshots do not change them, and they never go into
snapshots themselves. A passage opened with `7d` on the 1st keeps showing the 24th of the month before, for as long as
it stays open. Its labels count from that date, so a week later door I reads "14 days ago". For a newer past, close
the passage and open a new one.

**What happens in the past stays there.** A past world is a copy. You can build, dig, take things and leave things,
and none of it changes the present. It keeps your changes while the passage is open, even when everyone has left
(Worlds puts an empty world to sleep after a minute and wakes it when someone comes through). `/journey close` destroys
both past worlds for good; reopening builds fresh ones from the snapshots. Your inventory travels with you, so whatever you carry home from the past stays in the present.

| x days ago: the harvest is in | 2x days ago: the fields still full |
|---|---|
| ![The barn intact, before the fire](docs/img/map_era1.jpg) | ![The farm as it was](docs/img/map_era2.jpg) |
| ![Empty fields](docs/img/map_era1_fields.jpg) | ![Fields full of wheat, carrots and pumpkins](docs/img/map_era2_fields.jpg) |

*(The pictures come from the test tour on the DARC Funkstadt map, where x was 11 seconds. In real use it is days.)*

## The Sic Mundus door

<img src="docs/img/door_face.jpg" width="256" align="right" alt="The door's face: a triquetra in a ring, SIC MVNDVS above, CREATVS EST below">

A heavy double door of dark cast iron, two blocks wide and three high. Its face shows the **triquetra** in a ring
between the banners **SIC MVNDVS** and **CREATVS EST** ("thus the world was created"), framed by lilies, scrolled
hinge straps and rivets.

The door that belongs to a passage is put up and taken down by the mod. Survival players cannot break it. You can
also place it yourself as decoration: it is the *Sic Mundus Door* under *Functional Blocks* in the creative menu, and
it faces you when you place it.

## Daily snapshots

Every day at `dailyAt` (default 04:00 server time), the server saves the world and stores a snapshot. A server that
was down at that time catches up as soon as it is back up. `/journey snapshot [description]` takes one right away.
`/journey snapshots` lists them, with what each one added to the store.

**How it stays small.** Region files are taken apart into their chunks (terrain, entities, points of interest), and
every chunk is stored once, under the hash of its bytes. Other files are stored whole, the same way. A snapshot is
only a manifest: for each file of the world, the hashes it is made of. Minecraft rewrites a chunk only when something
in it changed, so a new snapshot adds just the chunks that changed since the last one. Files whose size and date are
unchanged are not even read again.

| DARC Funkstadt (101 MB world) | Stored |
|---|---|
| first snapshot | 72 MB (sector padding and duplicate chunks fall away) |
| a day without changes | **0 B** |
| the fields of Gut Funkenhof harvested (≈ 400 chunks) | 2.4 MB |
| 40 changed chunks | 206 KB |

Objects go into append-only pack files (`timejourney/<world>/packs/`), one per snapshot, with an index of where each
hash lives. The packs describe themselves, so the index can always be rebuilt from them. Taking a snapshot of
Funkstadt takes about one second. The game keeps running meanwhile; only saving is paused while the files are read.

**Thinning out.** Daily snapshots younger than `keepDailyDays` (60) are all kept. After that, one per week is kept
until `keepWeeklyDays` (730), then they go. Manual snapshots and snapshots an open passage uses are never deleted
automatically. Space no snapshot needs any more is freed: packs nobody uses are deleted, and packs that are mostly
unused are rewritten.

```
/journey snapshot [description]     take a snapshot now
/journey snapshots                  list them
/journey export <snapshot>          write a snapshot out as a complete world folder (timejourney/exports/)
/journey delete <snapshot>          delete one (and free what only it used)
```

All `/journey` commands need operator rights (level 2), or being the host in singleplayer.

## Configuration

`config/timejourney.json`:

| Key | Default | What it does |
|---|---|---|
| `storeDir` | `timejourney` | Folder of the snapshot store, relative to the server directory; one subfolder per world |
| `dailySnapshots` | `true` | Take the daily snapshot |
| `dailyAt` | `"04:00"` | When (local server time) |
| `keepDailyDays` | `60` | Keep every daily snapshot this long (0 = for ever) |
| `keepWeeklyDays` | `730` | Then one per week until this age (0 = for ever) |
| `compactBelow` | `0.5` | Rewrite a pack when less than this share of it is still used |
| `exclude` | `session.lock`, `dimensions/timejourney/` | Path prefixes (in the world folder) left out of snapshots; the past worlds themselves are always left out |
| `maxPassages` | `3` | Passages open at the same time |
| `travellers` | `"everyone"` | Who may pass through a vortex: `everyone` or `ops` |
| `cooldownTicks` | `60` | Ticks after a trip before the same player is pulled through again |

## Requirements

- Minecraft 26.3, Fabric Loader 0.19.5+, Fabric API
- **[Worlds](https://modrinth.com/mod/mc-worlds) 1.10.0+** by Wroud (MIT), the multiverse mod the past worlds run
  in. It was the most actively maintained multiverse mod for 26.3, with an API for creating worlds from other mods,
  so there was no need to write a new one. Worlds also gives you `/worlds` for worlds of your own.
- Install both on the server and on every client (the door has its own model and textures). The Funkstadt Prism
  instance installs both by itself.

## Good to know

- A passage leads to the past of the dimension it is opened in. The overworld is what has been tested; the Nether and
  the End are built the same way. Passages cannot be opened from inside a past world.
- The past worlds are made from the snapshot's terrain, entities and points of interest. Forced chunks are not copied,
  so a past world only runs where players are.
- Other mods' data that lives outside the region files (radiation sources, power networks, radio settings) belongs to
  the present. In a past world, blocks from those mods are there, but they start afresh. Radios in a past world join
  the radio network like any other radio.
- Snapshots are made to look back in time, not to restore the world. Use [Rewind](https://github.com/Lamisator/mc_rewind)
  for that, or `/journey export` and swap the folder by hand.

## Building and testing

```
./gradlew build                     # build/libs/timejourney-<version>.jar
./gradlew runClientGameTest         # flat-world tour: two snapshots, a passage, all four trips, unload/reload, close
./gradlew runClientGameTest -Pmap=<copy of DARC_Funkstadt> -PmapMods=<the map's mod jars>   # the pictures above
java -cp build/classes/java/main:<gson>:<slf4j> tools/StoreCheck.java <world> <scratch>    # the store on a real world
```

The flat-world tour checks that a shut door seals its vortex and that every trip lands in the right time (by the
tower each time has). It also checks that arriving opens the far door, that changes in the past leave the present
alone, that a resting past world wakes up with its changes kept, and that closing pulls travellers back and deletes
the worlds. `StoreCheck` takes four snapshots of a real world and puts them back together chunk by chunk, then
collects garbage and rebuilds the index.

`tools/make_icon.py` draws the icon (a Penrose triangle), `tools/make_door.py` the door textures, and
`tools/make_models.py` the door's block states and models.

## License

MIT
