The World Vol 5 · Bodies and Brains
ch 45 / 105
Chapter 45

Eating What the Valley Grew

The first creature ledger

A thousand years of The Hollow replay cell for cell, and none of those years has carried a gram inside a body. Daylight lands on a square of ground. A crown catches its share. Roots pay for that share in water and nutrient. Upkeep is charged on every gram already standing, and the difference becomes new tissue.

A creature is a store of energy with two lines against it: grams bitten off something that grew, priced by a stated conversion, and a bill charged on every gram of body whether the tick was used or not. What the bill leaves is what the creature has to spend, and a bite sends the mouthful in two directions: the grams into the store, the minerals back into the ground the plant is standing on.

The plant ledger already has the arithmetic. A plant's tick is income, then upkeep, then what is left becomes mass. A creature's tick is income, then upkeep, then what is left becomes movement or mass. The plant's income falls out of the sky and is capped by one square of ground. The creature's income has to come off something that already paid for it.

The row names one browser: 40 grams of body, 400 energy units when full, 0.004 units per gram per tick, and a bite of 0.25 grams at 4.0 energy units per gram. Those numbers name a bill of 0.1600 units a tick, a full-store clock of 2,500 ticks, and one bite paying for 6.2500 ticks of standing still.

The package is internal/beast. It prices bodies, draws nothing, and does not know how long a tick lasts. Package terra is not touched here; the last run checks that the bigger module still reproduces the thousand-year valley digest.

The browser row

A kind of plant in this world is twenty-four numbers in one flat row, and nothing about a plant lives anywhere else. A kind of creature gets the same treatment for the same reason. One row, nothing nested inside it, so two creatures of different kinds run identical code and differ only in the table. Ten numbers open the account. Five of them are spent in this chapter; the other five say what a body is able to do instead of what it costs, and they go in now because a row that grows a field every chapter is a row nothing can hold on to.

▣ Build · stage 1 — the row
// internal/beast/beast.go
// Package beast keeps a creature's books. Package terra prices a plant:
// what a cell of ground offers in a tick, and what the thing rooted in
// it can afford out of that. This package prices an animal on the same
// two questions turned round: what a gram of body costs to keep, and
// what a mouthful of somebody else's tissue is worth. Nothing in here
// draws anything, nothing in here knows how long a tick is, and nothing
// in here draws a random number.
package beast

// Kind is one kind of creature written down as numbers, laid out the
// way terra.Species is: one flat row with nothing nested inside it, so
// two creatures of different kinds run the same code and differ only
// here. The first block is the body, the second is what a tick costs,
// the third is what it eats, and the fourth is what it is physically
// able to do.
type Kind struct {
	Name string

	// the body
	Bulk float64 // grams of body
	Full float64 // energy units the store holds when it is full

	// what a tick costs
	Basal float64 // energy units one gram of body costs a tick
	Work  float64 // energy units one unit of movement work costs

	// what it eats
	Bite    float64 // grams one bite takes off a stand
	Convert float64 // energy units one gram of eaten tissue is worth
	Reach   float64 // cells a bite reaches

	// what it is able to do
	Sight float64 // cells one line of sight runs
	Top   float64 // cells a tick at a sprint
	Swing float64 // the part of a whole turn one turn action covers
}

Read the four blocks as four questions. How much of it is there, and how much can it bank? What does a tick cost it standing still, and what does a unit of moving cost on top? What does one mouthful take off a plant, what is that mouthful worth once it is inside, and how far can the mouth reach? And how far can it see, how fast can it go, how far round can it turn. Work, Sight, Top and Swing are not charged anything on this page, and neither is Reach, because the creature here does not move and its food is under its feet. They are in the row because the row is the unit the rest of the volume passes around, and a struct that gains a field every time somebody needs one is a struct nobody can reason about.

▣ Build · stage 2 — the four numbers the row already knows
// internal/beast/beast.go, continued
// Bill is the energy this kind's standing body charges it every tick,
// whatever the creature is doing with the tick: its bulk times what one
// gram of it costs. It is the plant's upkeep line with a different name
// on it.
func (k Kind) Bill() float64 { return k.Bulk * k.Basal }

// Mouthful is what one bite is worth once it is inside: the grams the
// bite takes off a stand, times the energy a gram of tissue turns into.
func (k Kind) Mouthful() float64 { return k.Bite * k.Convert }

// Fast is how many ticks a full store covers with nothing whatever
// coming in: the store divided by the bill. It is worked out from the
// row and never measured from a run.
func (k Kind) Fast() float64 { return k.Full / k.Bill() }

// Rhythm is how many ticks one bite pays for: the mouthful divided by
// the bill. Bite more often than this and the store fills; bite less
// often and it empties. The number says nothing about how full the
// store is: it is a fact about the kind, not about the afternoon.
func (k Kind) Rhythm() float64 { return k.Mouthful() / k.Bill() }

// Fauna is the valley's creature list, opened here with one row. A
// browser is an animal that takes small mouthfuls off standing plants
// and keeps taking them: it is the cheapest kind of eater to price,
// because nothing it does to its food is sudden.
var Fauna = []Kind{
	{
		Name: "browser",
		Bulk: 40, Full: 400,
		Basal: 0.004, Work: 0.60,
		Bite: 0.25, Convert: 4.0, Reach: 1.0,
		Sight: 12, Top: 0.45, Swing: 1.0 / 12,
	},
}

// Row is the kind of that name, and whether the table holds one.
func Row(name string) (Kind, bool) {
	for _, k := range Fauna {
		if k.Name == name {
			return k, true
		}
	}
	return Kind{}, false
}
$ go run ./cmd/graze -mode row
graze: the browser, ten numbers and what they come to

  Bulk         40.000  grams of body
  Full        400.000  energy units the store holds full
  Basal         0.004  energy units a gram of body costs a tick
  Work          0.600  energy units one unit of movement work costs
  Bite          0.250  grams one bite takes off a stand
  Convert       4.000  energy units a gram of eaten tissue is worth
  Reach         1.000  cells a bite reaches
  Sight        12.000  cells one line of sight runs
  Top           0.450  cells a tick at a sprint
  Swing         0.083  the part of a whole turn one turn action covers

  the bill      40.00 x 0.004 = 0.1600 energy units a tick
  one bite      0.25 x 4.000 = 1.0000 energy units
  a full store  400.0000 / 0.1600 = 2500.0000 ticks of standing still
  one bite      1.0000 / 0.1600 = 6.2500 ticks of standing still
  a full store is 400 bites, and 2500.0000 ticks is 0.6944 of a year of 3600

Not one of those four methods takes a tick, a world or a creature. They take a row and divide. Bill is the plant's upkeep line with a different name on it: a charge proportional to how much of the thing there is. Fast and Rhythm are the two divisions that turn that charge into time, and both are available before anything is standing anywhere. A browser weighs forty grams, each of which costs four thousandths of an energy unit a tick, so standing still costs it 0.1600 a tick. Four hundred units in a full store buys 2,500 of those ticks. One bite of a quarter of a gram, at four units a gram, is worth exactly one unit, so one bite buys 6.2500 ticks.

∑ Math Interlude — a bill, a store, and the two divisions between them

All four numbers above came out of two multiplications and two divisions, and every one of them was done on the printed row. The shorthand earns its place because the same four are about to be asked of every creature in the valley at once.

Give the grams of body the name B and the cost of one of those grams a. A dot between two letters means multiply, so B·a is 40 × 0.004.

bill = B·a

Check it: 40 × 0.004 = 0.16 energy units a tick, and that is charged whether the creature spends the tick sprinting or asleep. Now call the full store F. The store runs down by the bill every tick, so the ticks it covers are the store divided by the bill:

fast = F ÷ (B·a)

400 ÷ 0.16 = 2,500. Call the grams one bite takes g and the energy a gram is worth c. One bite is g·c, which is 0.25 × 4.0 = 1.00, and the ticks that one bite pays for is the same division again with the mouthful on top:

rhythm = (g·c) ÷ (B·a)

1.00 ÷ 0.16 = 6.25. That last number is the one to carry: it says a browser that takes a bite more often than once every six and a quarter ticks is filling up, and one that takes a bite less often is running down, and it says so without knowing anything at all about how full the store is right now.

Bgrams of body the creature is carrying; 40 for a browser
aenergy units one gram of body costs per tick; 0.004 here
Fenergy units the store holds when it is full; 400 here
ggrams one bite takes off a plant; 0.25 here
cenergy units one gram of eaten tissue turns into; 4.0 here
a·ba multiplied by b
billenergy charged every tick for standing there: 0.16 here
fastticks a full store covers with nothing coming in: 2,500 here
rhythmticks one bite pays for: 6.25 here
A plant's tick and a creature's tick as the same four columns Two rows of four boxes joined left to right by arrows. The upper row is a plant's tick: sunlight at 12.00 a tick, income at light times a catch of 0.12 giving 1.44, upkeep at mass times 0.008, and new tissue as what is left. The lower row is a creature's tick: tissue standing at 0.25 grams a bite, into the store at grams times a conversion of 4.0 giving 1.00, upkeep at 40 grams times 0.004, and what is left going to work or mass. An arrow leaves the plant's new tissue, runs back along the figure and enters the creature's first box, labelled the grams change hands. The two rows carry the same four columns in the same order; only the first column differs, because the plant's income is capped by one square of ground and the creature's has to be taken off something that already paid for it. ONE TICK, THE SAME FOUR COLUMNS, TWO KINDS OF THING A PLANT SUNLIGHT 12.00 a tick INCOME x Catch = 1.44 UPKEEP mass x 0.008 NEW TISSUE what is left the grams change hands A CREATURE, THE SAME LEDGER FROM THE OTHER SIDE TISSUE STANDING 0.25 g a bite INTO THE STORE x Convert = 1.00 UPKEEP 40 x 0.004 WHAT IS LEFT work or mass the plant's first column is capped by one square of ground the creature's has to be taken off something that already paid for it
Figure 45.1 — the two ledgers, column for column. Third columns match exactly: a charge proportional to how much of the thing there is. Fourth columns match in role and differ in what the surplus can buy. Only the first column is a different kind of fact, and the whole of a creature's behaviour is the business of filling it.

The Beast store

A row is a kind, not an animal. The individual needs the row it was stamped from and one number that a tick is allowed to move. That number is the store, and it is the creature's whole state in this chapter. Nothing on it says where it is, because nothing on this page asks: the plant a creature eats is under its feet, and the tick is handed the plant itself.

▣ Build · stage 3 — one creature, and the five numbers of its tick
// internal/beast/body.go
package beast

import (
	"theworld/internal/terra"
)

// Beast is one creature: the row it was stamped from, and the store,
// which is the only number a tick changes. Everything else on it is a
// running total or a fact about when it started.
type Beast struct {
	Name  string
	Kind  *Kind
	Store float64 // energy units in the store right now
	Born  int     // the tick it was born on
	Ate   float64 // grams it has taken off the valley since it was born
	Dead  bool    // its store reached nothing and it stopped
	Last  Ledger

	// Greedy is the bite as it was first written: a whole mouthful
	// taken whether or not the plant has one to give. It is kept behind
	// a flag so the failure can be run instead of described, and it is
	// false in every creature this book ships.
	Greedy bool

	// Unpaid is the bite with the repayment switched off: the grams
	// still leave the plant, but the minerals they were holding go
	// nowhere instead of landing on the cell that lent them. It is
	// named for the ground's books and not for the animal, because a
	// creature carrying it eats exactly as it always did and the whole
	// of the loss is the bed's. It is kept behind a flag so what that
	// missing line costs can be run instead of described, and it is
	// false in every creature this book ships.
	Unpaid bool
}

// Ledger is one creature's tick, in the order the numbers are worked
// out.
type Ledger struct {
	Ate    float64 // grams taken off a stand
	Gained float64 // energy units those grams were worth
	Spilt  float64 // energy units of that the store had no room for
	Paid   float64 // energy units the standing body charged
	Left   float64 // what is in the store when the tick closes
}

Nothing in this file builds a creature for you. On this page one is a row and a number, and the bench writes both by hand: k := beast.Fauna[0] takes a copy of the row off the table, and &beast.Beast{Name: k.Name, Kind: &k, Store: k.Full} stands one up with a full store. The pointer goes to that copy and never into Fauna itself. A plant's Germinate is careful about the same thing for the same reason: an individual holding a pointer into the table can edit the kind it came from, and every other creature of that kind changes with it. The Ledger is five numbers in the order they get worked out, and it is returned instead of printed for the same reason a plant's is: the arithmetic and the reporting come apart, and the caller that wants a table and the caller that wants a census run the same tick underneath. Spilt is the line that keeps the energy honest. A store has a top, an animal that eats when it is already full gets nothing for it, and the units that did not fit have to be written down somewhere or they are units the world lost track of.

▣ Build · stage 4 — the bite, the bill, and the order they happen in
// internal/beast/body.go, continued
// Graze takes one bite off a stand and hands back the grams that
// actually moved. Nothing is manufactured here: the grams come off the
// plant's standing mass and no bite is bigger than the plant it is
// taken from.
//
// The grams and the minerals inside them leave in opposite directions.
// The grams go into the mouth, become energy in the store and are burned
// off as the body runs. The nutrient those grams were holding was
// borrowed out of the ground by the plant that built them, and it goes
// back onto the cell that plant is standing on the moment the bite is
// taken, through the same terra.Bed.Fall the plant's own burned upkeep
// goes through. Nothing charges a creature nutrient when it is stood up,
// so a creature has no nutrient account to hold the minerals in and
// never carries any: the loan was the plant's, and a bite repays it.
//
// A nil bed is a bench with one stand and one mouth and no valley
// underneath them, which is how the chapters that priced a bite ran it.
// There is no ground there to hand anything back to and nothing keeping
// nutrient books, so the bite is grams alone.
func (b *Beast) Graze(st *terra.Stand, bed *terra.Bed) float64 {
	grams := b.Kind.Bite
	if !b.Greedy && grams > st.Plant.Mass {
		grams = st.Plant.Mass
	}
	st.Plant.Mass -= grams
	if bed != nil && !b.Unpaid {
		bed.Fall(st.At, terra.Pile{Nutrient: grams * st.Plant.Hunger})
	}
	b.Ate += grams
	return grams
}

// Tick charges one creature one tick against one stand, or against
// nothing at all when st is nil, handing the bite the bed that stand is
// rooted in so the minerals it takes have ground to go back to. The
// order is the plant's order: income first, upkeep second, and whatever
// the upkeep leaves is what the creature carries into the next tick.
//
// The store is allowed past nothing. Clamping it at zero would be the
// world forgiving a bill, and a forgiven bill is energy nobody paid
// for; the creature is marked instead, and what it went under by stays
// on the ledger.
func (b *Beast) Tick(st *terra.Stand, bed *terra.Bed) Ledger {
	l := Ledger{}
	if st != nil {
		l.Ate = b.Graze(st, bed)
		l.Gained = l.Ate * b.Kind.Convert
		b.Store += l.Gained
		if b.Store > b.Kind.Full {
			l.Spilt = b.Store - b.Kind.Full
			b.Store = b.Kind.Full
		}
	}
	l.Paid = b.Kind.Bill()
	b.Store -= l.Paid
	if b.Store <= 0 {
		b.Dead = true
	}
	l.Left = b.Store
	b.Last = l
	return l
}
$ go run ./cmd/graze -mode starve -ticks 3000 -every 250
graze: one browser with nothing to eat, 400.00 in the store, 0.1600 charged a tick
       the row says it runs out after 400.0000 / 0.1600 = 2500.0000 ticks

    tick       paid      store   ticks left
     250     0.1600   360.0000    2250.0000
     500     0.1600   320.0000    2000.0000
     750     0.1600   280.0000    1750.0000
    1000     0.1600   240.0000    1500.0000
    1250     0.1600   200.0000    1250.0000
    1500     0.1600   160.0000    1000.0000
    1750     0.1600   120.0000     750.0000
    2000     0.1600    80.0000     500.0000
    2250     0.1600    40.0000     250.0000
    2500     0.1600    -0.0000      -0.0000

  the store reached nothing on tick 2500, having paid 400.0000 in all
  the last subtraction left -1.7737e-11 rather than exactly nothing
  that is 0.6944 of a year of 3600 ticks: full in spring, gone in autumn

The prediction is on the second line of output, before a tick has been taken, and the table walks to it in a straight line: 40.0000 units gone every 250 ticks, ten of those, and the store is empty. The right-hand column is the same number read as time, and it counts down as evenly as the store does. Nothing here is a curve. A plant's growth bends because its upkeep is charged on a mass that keeps changing; a starving creature's body does not change, so its bill does not either, and the line is straight.

Two details in that run are the code being careful. The last subtraction leaves −1.7737e−11 and not nothing, because 0.16 is not a number binary holds exactly and 2,500 of them do not add back to 400 in the last few bits. That is why the test in Tick is written as at or under nothing instead of equal to nothing, and it is also why the residue is printed: it lands on the same side of zero on every machine, so the creature dies on tick 2,500 everywhere. And the store is left holding that residue instead of being clamped to zero. Clamping would be the world forgiving a bill, and a forgiven bill is energy that nobody paid for and nobody wrote down.

One line of Graze has nothing to do with the store. A gram of leaf is two things at once: tissue a mouth can burn, and the nutrient a root drew out of the bed to build that tissue with. The mouth wants only the first. So the bite hands the second straight back, as a pile carrying minerals and no mass, onto the cell the plant is standing on, through the same terra.Bed.Fall that plant's own burned upkeep has gone through since the terrarium learned to rot. The figure is grams * st.Plant.Hunger, which is what the terrarium already calls the nutrient locked up in that much of that row's tissue. Nothing ever charges a creature nutrient, not for the body it was stood up with and not for a mouthful, so it has no account to hold minerals in and carrying them about would mean opening one. The flag on that line is Unpaid, and it is named for the ground and not for the animal: a creature carrying it eats exactly what it always ate, and every unit that goes missing goes missing from the bed. Like Greedy it is false in everything this book ships, and it is there so that what one line of handback is holding up can be measured instead of asserted.

On this page the bed is always nil, and that is what a nil bed means: a bench holding one stand and one mouth and no valley underneath them. There is no ground to hand anything back to and nothing keeping nutrient books, so the bite is grams alone. The starvation run above never takes one, and the browsing bench further down takes thousands with nothing under the plant but the numbers the bench made up. A creature standing in The Hollow is handed the real bed, and the minerals land on it.

◆ Note — what this body does not do, said plainly

Bulk never moves. A real animal that stops eating burns its own body, gets lighter, and gets cheaper to run as it goes, so its store empties a little more slowly every day and it survives longer than a flat bill predicts. Here the store is the only thing a tick changes and the body is a constant, which makes the starvation clock exactly divisible and slightly pessimistic. Nothing in the row has an age, a sex, a stomach that takes time to empty, or a temperature. The one thing the model has to get right is that the bill scales with how much creature there is, because that is what makes a big animal expensive and a small one cheap, and it is the same reason a plant has a ceiling.

One scrub and one browser

Now put the two ledgers on the same cell. The bench holds one scrub and one browser, grows the plant first and feeds the creature second, and varies exactly one thing between runs: how many ticks pass between bites. Growing the plant first is the order the valley already uses, so a creature grazes tissue that has finished growing on this tick and not tissue that is about to.

▣ Build · stage 5 — one plant, one animal, one rhythm at a time
// cmd/graze/main.go — the bench's tick: the plant eats, then the animal
// bites and gone are the bench function's own named results, and the loop
// below is quoted at the indentation it has inside that function
	for t := 1; t <= ticks; t++ {
		st.Plant.Grow(g, 1)
		if b.Dead {
			continue
		}
		if n > 0 && (t-1)%n == 0 {
			b.Tick(st, nil)
			bites++
		} else {
			b.Tick(nil, nil)
		}
		if b.Dead {
			gone = t
		}
	}
$ go run ./cmd/graze -mode browse -ticks 3000 -store 100
graze: one browser on one scrub, 12.00 light and 2.50 water a tick
       the plant fixes 1.4400 a tick, light-limited, and tops out at 180.0000 grams
       the creature is charged 0.1600 a tick and one bite is worth 1.0000,
       so one bite every 6.2500 ticks is the rhythm that pays for itself

    a bite    bites      grams      plant    settles      store     died      drift
     every    taken      eaten      grams         at       left  on tick     a tick
         1     3000   750.0000   148.7500   148.7500   399.8400        -     0.8400
         2     1500   375.0000   164.4378   164.3750   399.6800        -     0.3400
         4      750   187.5000   172.2814   172.1875   370.0000        -     0.0900
         6      500   125.0000   174.8957   174.7917   120.0000        -     0.0067
         7      429   107.2500   175.5363   175.5357    49.0000        -    -0.0171
         8      358    89.5000   178.7263   176.0938    -0.0800     2863    -0.0350
        12      109    27.2500   180.0000   177.3958    -0.1200     1307    -0.0767
         0        0     0.0000   180.0000   180.0000    -0.1600      626    -0.1600

  settles at is worked out, not measured: (1.4400 - bite/every) / 0.0080
  drift a tick is the same: 1.0000 / every - 0.1600
  a creature that died stopped biting; the plant kept growing to tick 3000

Two columns of that table are predictions and the rest are measurements, and the point of the run is how close they sit. settles at is the mass where the scrub's income covers its own upkeep and the browsing together, worked out as (1.4400 − bite ÷ every) ÷ 0.0080 with nothing measured in it. At one bite a tick the prediction is 148.7500 grams and the plant is standing at 148.7500. At one bite every seven it predicts 175.5357 and measures 175.5363. The gaps in between come from lumpiness: a plant losing a quarter of a gram every seventh tick is never quite at the mass a smooth drain would hold it at, and the coarser the rhythm the wider it swings.

The drift column is the other prediction, and it is where the 6.2500 from the row does its work. One bite every six ticks earns 0.1667 a tick against a bill of 0.1600, so the store creeps up by 0.0067 a tick: over three thousand ticks that is a climb from 100 to 120.0000, which is what the run shows. One bite every seven earns 0.1429 against the same bill, drifts down 0.0171 a tick, and the store falls from 100 to 49.0000. Between six and seven lies 6.25, and nothing about the plant, the light or the store decides where that line falls. Two numbers on the row do.

The last three rows are the same arithmetic run past the point where it holds. At one bite every eight the store is falling 0.0350 a tick and the creature reaches nothing on tick 2863; at every twelfth tick it falls 0.0767 and stops on tick 1307; taking nothing at all it falls the whole 0.1600 and stops on 626. That last figure deserves a second look, because 100 ÷ 0.16 is 625 and not 626. The subtraction that should have landed on nothing at tick 625 landed 1.4e−12 above it instead, the creature was not yet under, and it bought one more tick. The residue fell the other way in the run before this one, where a full store emptied on 2,500 exactly.

One claim from the first section is still unpaid: that none of this touched the terrarium. The valley draws its founding cells, its germination coin and its plant mortality from numbered streams on the world seed, and the fastest way to break a world like this is to add something that quietly takes a draw off one of those streams. Then every plant lands somewhere else and the collapse looks like ecology instead of book-keeping. So the volume claims its own stream numbers, 12 for where a founding population is scattered, 13 for the jitter on a wandering creature and 14 for the weights a brain is born with, and it leaves 8 through 11 alone. The check is the whole thousand years, run out of the module that now carries internal/beast.

▣ Build · stage 6 — the same thousand years, out of a bigger module
$ go run ./cmd/graze -mode valley -years 1000
graze: seed 5, 1000 years, brake 1 on K = 75, creep 0.0010, rim walled and cracked
       internal/beast is in the module and nothing in the valley imports it

  world df1c117e69bdf157, census 75c77a67f6f430bb after 3600000 ticks
  2662.2 grams standing on 44 plants
  that is 10649 bites of 0.25 grams, and 66555 ticks of one browser standing still
  or 18.49 years of one, in a valley that took 1000 to grow it

Both digests match the ones the terrarium closed on: the world digest over the bed's two numbers per cell and every stand's cell and mass, and the census digest folded over all thousand midsummers. A package that draws no random numbers cannot move a valley, and this is what that looks like when it is checked instead of asserted. The last two lines are the hinge in one figure. Everything standing in The Hollow after a thousand years comes to 10,649 mouthfuls, which is 66,555 ticks of one browser doing nothing at all, which is eighteen and a half years of one animal. A millennium of light, arithmetic and weather, spent in under two decades by a single forty-gram body that never moves.

Neither count is whole before it is printed, and the bench rounds them in opposite directions on purpose: 2,662.2 grams is 10,648.8 bites, and the bites round up because the last bite of a stand takes whatever is left of it and that part-bite still has to be taken, while the ticks truncate because a fraction of a tick buys the browser nothing. Floor them both and the line reads 10,648 instead. The rule sits in the comment over the two lines that print those counts, so a reader who redoes the division on 2,662.2 finds the answer beside the arithmetic that produced it.

The bounded bite

Graze has a clamp in it, and the clamp costs two lines to write and one branch to run on every bite in the world. The argument against it is easy to make. A bite is a quarter of a gram; the plants in this valley stand at tens and hundreds of grams; a bite bigger than the plant it comes off is not a situation the valley produces. Take the branch out and the code is shorter and reads better.

⚠ Worked failure — a mouthful bigger than the meal
// internal/beast/body.go — the whole of the bite
func (b *Beast) Graze(st *terra.Stand, bed *terra.Bed) float64 {
	grams := b.Kind.Bite
	if !b.Greedy && grams > st.Plant.Mass {
		grams = st.Plant.Mass
	}
	st.Plant.Mass -= grams
	if bed != nil && !b.Unpaid {
		bed.Fall(st.At, terra.Pile{Nutrient: grams * st.Plant.Hunger})
	}
	b.Ate += grams
	return grams
}

With Greedy set, those two middle lines are skipped and every bite takes a full quarter of a gram whatever the plant is holding. The valley does produce the situation, of course. It produces it every time a seed germinates: a moss seedling stands up holding a quarter of a gram, and in the shade under a crown it fixes 0.0360 a tick, which is a seventh of a mouthful.

$ go run ./cmd/graze -mode nibble -plant moss -light 0.6 -ticks 20 -store 10
graze: a browser biting a moss seedling every tick, on 0.60 light
       the seedling opens at 0.2500 grams and fixes 0.0360 a tick,
       against a bite of 0.2500 grams

    tick     caught       paid      eaten      plant      store
       1     0.0360     0.0050     0.2500     0.0310    10.8400
       2     0.0360     0.0006     0.2500    -0.1836    11.6800
       3     0.0360    -0.0037     0.2500    -0.3939    12.5200
       4     0.0360    -0.0079     0.2500    -0.6001    13.3600
       5     0.0360    -0.0120     0.2500    -0.8021    14.2000
      10     0.0360    -0.0314     0.2500    -1.7531    18.4000
      15     0.0360    -0.0490     0.2500    -2.6127    22.6000
      20     0.0360    -0.0648     0.2500    -3.3897    26.8000

  the books in grams
    the seedling opened at     0.250000
    built out of light         0.720000
    burned as upkeep          -0.640307
    eaten                      5.000000
    standing                  -3.389693
    difference                 0.000000

  the plant's standing mass went under nothing on tick 2, and its
  upkeep went under nothing with it: a plant paid for being there
  the creature ate 5.0000 grams off a plant that never held more than 0.9700:
  4.0300 grams nothing in this world grew, worth 16.1200 energy units

Look at what the books say. The difference is 0.000000. Opening stock plus what was built out of light, less what was burned as upkeep, less what was eaten, less what is standing, comes to nothing left over, and every gram in the run is accounted for to six places. A balanced ledger is not a correct one. It balances because the standing line has gone to −3.389693 grams and is absorbing the error, and a negative mass is a plant that owes the world tissue.

The paid column is the symptom that names the cause. It reads 0.0050 on the first tick, 0.0006 on the second, and from the third tick on it is negative and getting more so. Upkeep is mass times a rate, so a negative mass produces a negative upkeep, and a negative upkeep is income. The plant is now being paid for standing there, which is why it goes on manufacturing after it has been eaten out of existence: 5.0000 grams taken off something that never held more than 0.9700, and 16.1200 energy units in a creature that nothing in the world ever grew.

The rule the mistake teaches is one line long. A transfer is bounded by what the source is holding, and the bound belongs where the transfer happens. It cannot live in the caller, because the next caller will not know about it, and it cannot live in a check afterwards, because by then the number that would have caught it is already corrupt. The valley's own soil has had this bound since roots were written: a cell hands over a share of what it holds and never more than it has. A mouth needs the same sentence, and it is the same two lines.

Why one gram is counted once

Strip the animal out and what is left is a pattern the whole volume runs on. A quantity is held in a store. Something adds to it in lumps, at times decided elsewhere. Something else takes away at a rate that does not care what the store is doing. Two divisions then tell you everything about the long run without simulating any of it: the store over the drain gives you how long you have if the lumps stop, and one lump over the drain gives you how often a lump has to arrive. Neither division needs a run, and a model whose important numbers can only be found by running it is a model you cannot argue with.

The other half of why this works is that the grams are conserved and the energy is not. A gram exists in exactly one place at a time: standing in a plant, lying in the litter, or gone because something burned it. When a creature bites, the gram moves from the plant's mass to the creature's total and the sum is unchanged, which is what makes the books closeable at all. Energy behaves differently on purpose. It is manufactured out of a gram at the stated conversion, it is spent on staying alive, and it is gone. That asymmetry is the reason an ecology has more plants in it than animals, and it comes out of two lines of arithmetic rather than out of a rule anybody wrote.

It is also the reason a creature is a brake. The browsing table has the scrub settling lower the harder it is browsed, at a mass that comes out of the plant's own income and the rate the animal takes from it, and that is a negative feedback of exactly the sort the terrarium needed to stop crashing. Grazing pressure is a density-dependent term wearing a different hat. The difference is that this one arrives with an appetite of its own, and what happens when the brake can starve is the arithmetic the rest of the volume is about.

✓ Checkpoint — the store, and the two lines against it
  • Handed a creature row, compute its bill per tick, the ticks a full store covers, and the ticks one bite pays for, and say which of the ten numbers were needed for each.
  • Explain why the starving creature's store falls in a straight line while a growing plant's mass bends, and point at the field that differs.
  • Given a browsing rhythm, predict the mass the plant settles at and the store's drift per tick, and say which side of 6.2500 a rhythm has to be on to hold.
  • Shown a run whose gram books balance to six places, check whether any standing mass has gone negative before trusting the total.
  • Name the bound a transfer needs, say where it belongs, and show the two lines in Graze that enforce it.
  • State which numbered streams the terrarium owns, which ones this volume takes, and what the thousand-year census digest is testing.
⚡ Exercises — try first, then reveal
Exercise 1 — half a tank. Predict the tick a browser starting on 200 units instead of 400 runs out on, then check with go run ./cmd/graze -mode starve -store 200 -ticks 2000 -every 500.

200 ÷ 0.16 = 1,250, and the run reports tick 1251. The prediction is not wrong and neither is the program. Subtracting 0.16 twelve hundred and fifty times from 200 leaves a hair above nothing rather than a hair below it, the test in Tick asks whether the store is at or under nothing, and the creature buys one more tick before the next subtraction takes it clearly under. Halving the store halves the time the line predicts, exactly, which is the one thing a straight line guarantees you; the extra tick is that test and not the arithmetic.

Exercise 2 — the same browser on worse food. Run go run ./cmd/graze -mode browse -plant moss -store 100 -ticks 3000 and explain why every column about the creature is unchanged while every column about the plant is different.

The bites taken, the grams eaten, the store left and the tick each creature died on are identical to the scrub run, down to the digit: 500 bites at one every six, a store of 120.0000, deaths on 2863, 1307 and 626. Nothing about a mouthful depends on what it was a mouthful of, because Convert is a number on the creature's row and not on the plant's. The plant columns are the ones that move. Moss catches 0.06 of the light against the scrub's 0.12 and pays 0.020 a gram against 0.008, so its ceiling is 36.0000 grams instead of 180.0000, and at one bite a tick it settles at 23.5000. The same animal takes the same living out of a plant a fifth the size, and takes a much larger share of it doing so.

Exercise 3 — the darkest cell that can still feed one. Work out the daylight at which one whole scrub, eaten as fast as it grows, exactly pays a browser's bill. Then run -mode browse -beat 1 -store 100 -ticks 40000 at one hundredth above and below it.

A scrub eaten flat holds almost no mass, so it pays almost no upkeep and hands over its entire income every tick. That income is the light times a catch of 0.12, and it is worth four energy units a gram, so the creature earns L × 0.12 × 4.0 and owes 0.16. Setting them equal gives L = 0.16 ÷ 0.48 = 0.3333. At -light 0.34 the store drifts up 0.0032 a tick and stands at 229.9724 after forty thousand ticks; at -light 0.33 it drifts down 0.0016 and has fallen to 37.9725 with the end in sight; at -light 0.30 it drifts down 0.0160 and the creature is finished on tick 6374.

All three land a shade under two units to the good of the drift taken on its own. 0.0032 × 40,000 on a store of 100 predicts 228 and the run says 229.9724; −0.0016 × 40,000 predicts 36 and the run says 37.9725; 100 ÷ 0.0160 predicts the end at 6,250 and the browser lasts to 6374. The extra is opening stock. A scrub germinates already holding half a gram, and that half gram goes down the same mouth as everything the plant makes afterwards. The grams eaten column has it in plain sight: 1632.4931 grams taken off a plant that fixed 0.0408 a tick for forty thousand ticks, which is 1632.0000 of them. The 0.4931 is the seedling, less the trace of upkeep it burned in the few ticks before it was grazed flat, and at four units a gram it is 1.9724 energy units. That is the gap between 228 and 229.9724, it is the gap between 36 and 37.9725, and at 0.0160 a tick it is the 124 ticks the third browser lives past 6,250.

Two columns of the run cannot be read straight in these three cases, and both go wrong the same way. drift a tick prints 0.8400, because it is 1.0000 ÷ 1 − 0.1600 and it assumes every bite lands a full quarter of a gram; on 0.34 light the scrub cannot make a quarter of a gram in a tick, so the real drift is the income sum above. settles at prints a negative mass for the same reason, which is the prediction telling you honestly that no mass at all satisfies it and the plant is being asked for more than it can make.

Every creature on this page has been standing on the plant it eats. Reach was never spent, the movement price on the row was never charged, and the store went up and down without the animal going anywhere. The moment a mouthful is a cell away, the arithmetic acquires a second outgoing: crossing ground costs energy out of the same store the eating fills, and the price of crossing it quickly is not the price of crossing it slowly.