farming

package
v0.4.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package farming is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package farming contains deterministic rules shared by farming and life-sim games. Calendar and crop records are deliberately independent of devices, rendering, and storage so games can drive them from a fixed-step loop.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Calendar

type Calendar struct {
	Seasons []Season
	Day     int
	Season  int
	Hour    float32
}

Calendar is a repeating season calendar. Day is one-based and Hour is in [0, 24). Advancing past the end of a season starts the next season at day 1.

Example
package main

import (
	"fmt"
	"time"

	"github.com/nsgo-dev/game/pkg/farming"
)

func main() {
	c := farming.Calendar{
		Seasons: []farming.Season{{ID: "spring", Days: 2}, {ID: "summer", Days: 3}},
		Day:     2,
		Hour:    23,
	}
	for _, d := range []time.Duration{2 * time.Hour, 48 * time.Hour} {
		days, err := c.Advance(d)
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Printf("crossed %d: %s day %d hour %.0f\n", days, c.Seasons[c.Season].ID, c.Day, c.Hour)
	}
}
Output:
crossed 1: summer day 1 hour 1
crossed 2: summer day 3 hour 1

func (*Calendar) Advance

func (c *Calendar) Advance(elapsed time.Duration) (days int, err error)

Advance moves the clock by elapsed wall/game time and returns the number of whole days crossed. It never uses time.Now, so replay and tests stay stable.

func (Calendar) Validate

func (c Calendar) Validate() error

Validate reports whether the calendar's season, day and hour are valid.

type Crop

type Crop struct {
	Kind  string
	Stage int
	Days  int
}

Crop is the persistent state of one planted crop.

func (*Crop) WateredDay

func (c *Crop) WateredDay(k CropKind) (bool, error)

WateredDay records one day's watering. It returns true when the crop became ripe on this day.

Example
package main

import (
	"fmt"

	"github.com/nsgo-dev/game/pkg/farming"
)

func main() {
	turnip := farming.CropKind{ID: "turnip", Stages: 3, DaysPerStage: 2}
	crop := farming.Crop{Kind: "turnip"}
	// The crop grows one stage every two watered days and is ripe at its last stage.
	for day := 1; day <= 4; day++ {
		ripe, err := crop.WateredDay(turnip)
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Printf("day %d: stage %d ripe %t\n", day, crop.Stage, ripe)
	}
}
Output:
day 1: stage 0 ripe false
day 2: stage 1 ripe false
day 3: stage 1 ripe false
day 4: stage 2 ripe true

type CropKind

type CropKind struct {
	ID           string
	Stages       int
	DaysPerStage int
}

CropKind describes growth stages. A crop advances once for each watered day, and is ripe at the final stage.

func (CropKind) Validate

func (k CropKind) Validate() error

Validate reports whether the crop kind has usable IDs and positive growth values.

type Season

type Season struct {
	ID   string
	Days int
}

Season describes one calendar season and its length in days.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL