Add terrain biomes jumping and extensible gathering professions
This commit is contained in:
@@ -15,6 +15,9 @@ import (
|
||||
)
|
||||
|
||||
type Character struct {
|
||||
JumpVelocity float64 `json:"jumpVelocity"`
|
||||
GatherReadyAt float64 `json:"gatherReadyAt"`
|
||||
|
||||
View p.Character `json:"view"`
|
||||
Account string `json:"account"`
|
||||
Day string `json:"day"`
|
||||
@@ -49,7 +52,14 @@ type Mob struct {
|
||||
LastAttack float64 `json:"lastAttack"`
|
||||
RespawnAt float64 `json:"respawnAt"`
|
||||
}
|
||||
type ResourceLayout struct {
|
||||
Biome string `json:"biome"`
|
||||
Color *string `json:"color"`
|
||||
Resources []p.Entity `json:"resources"`
|
||||
}
|
||||
type World struct {
|
||||
ResourceLayouts map[string]ResourceLayout `json:"resourceLayouts,omitempty"`
|
||||
|
||||
Version int `json:"version"`
|
||||
ID string `json:"id"`
|
||||
Seed int64 `json:"seed"`
|
||||
@@ -87,7 +97,7 @@ func New(id string, seed int64, now time.Time, b content.Balance) *World {
|
||||
return w
|
||||
}
|
||||
func (w *World) Restore(now time.Time) error {
|
||||
if w.Version != GeneratorVersion {
|
||||
if w.Version != 1 && w.Version != GeneratorVersion {
|
||||
return errors.New("unsupported persisted generator version")
|
||||
}
|
||||
// Snapshots predating progression receive the YAML defaults once. Existing
|
||||
@@ -133,6 +143,22 @@ func (w *World) Restore(now time.Time) error {
|
||||
w.Balance.MaxYieldPerAction = defaults.MaxYieldPerAction
|
||||
w.dirty = true
|
||||
}
|
||||
if w.Balance.Gathering == nil || w.Balance.GatheringRules == nil {
|
||||
b, err := content.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if w.Balance.Gathering == nil {
|
||||
w.Balance.Gathering = b.Gathering
|
||||
}
|
||||
if w.Balance.GatheringRules == nil {
|
||||
w.Balance.GatheringRules = b.GatheringRules
|
||||
}
|
||||
w.dirty = true
|
||||
}
|
||||
if w.ResourceLayouts == nil {
|
||||
w.ResourceLayouts = map[string]ResourceLayout{}
|
||||
}
|
||||
if err := w.Balance.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -140,6 +166,7 @@ func (w *World) Restore(now time.Time) error {
|
||||
return errors.New("invalid world snapshot")
|
||||
}
|
||||
w.terrain = NewTerrain(w.Seed, w.Balance)
|
||||
w.terrain.Version = w.Version
|
||||
w.chunks = map[string]p.Chunk{}
|
||||
w.now = now
|
||||
w.PendingTitles = nil
|
||||
@@ -150,6 +177,9 @@ func (w *World) Restore(now time.Time) error {
|
||||
if err := w.restoreEquipment(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.restoreGathering(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.initializeStats(c); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -249,6 +279,9 @@ func (w *World) Join(account, name string) (*Character, error) {
|
||||
if err := w.restoreEquipment(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := w.restoreGathering(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := w.initializeStats(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -274,6 +307,9 @@ func (w *World) Import(c *Character) error {
|
||||
if err := w.checkImportedEquipment(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.restoreGathering(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.initializeStats(c); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -289,6 +325,8 @@ func (w *World) Import(c *Character) error {
|
||||
c.View.ACK = 0
|
||||
c.LastAttack = 0
|
||||
c.LastGather = 0
|
||||
c.GatherReadyAt = 0
|
||||
c.JumpVelocity = 0
|
||||
c.ReturnPosition = nil
|
||||
c.Sheltered = false
|
||||
c.View.HasKeystone = false
|
||||
@@ -342,6 +380,12 @@ func (w *World) chunk(x, z int) p.Chunk {
|
||||
return c
|
||||
}
|
||||
c := w.terrain.Chunk(x, z)
|
||||
if layout, ok := w.ResourceLayouts[key]; ok {
|
||||
c.Biome, c.Color, c.Resources = layout.Biome, layout.Color, layout.Resources
|
||||
} else {
|
||||
w.ResourceLayouts[key] = ResourceLayout{Biome: c.Biome, Color: c.Color, Resources: c.Resources}
|
||||
w.dirty = true
|
||||
}
|
||||
w.chunks[key] = c
|
||||
return c
|
||||
}
|
||||
@@ -388,6 +432,10 @@ func (w *World) Intent(id string, i p.Intent) error {
|
||||
c.MoveZ = i.Z / n
|
||||
c.LastMove = w.now
|
||||
return nil
|
||||
case "jump":
|
||||
return w.jump(c)
|
||||
case "consume":
|
||||
return w.consumeGathering(c, i.Target)
|
||||
case "gather":
|
||||
return w.gather(c, i.Target)
|
||||
case "craft":
|
||||
@@ -409,6 +457,7 @@ func (w *World) Intent(id string, i p.Intent) error {
|
||||
if math.Hypot(c.ReturnPosition.X, c.ReturnPosition.Z) > w.SafeRadius() {
|
||||
return errors.New("keystone destination has been destroyed")
|
||||
}
|
||||
c.JumpVelocity = 0
|
||||
c.View.Entity.Position = *c.ReturnPosition
|
||||
c.ReturnPosition = nil
|
||||
c.View.HasKeystone = false
|
||||
@@ -420,39 +469,6 @@ func (w *World) Intent(id string, i p.Intent) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (w *World) gather(c *Character, target string) error {
|
||||
if w.Age()-c.LastGather < w.Balance.GatherCooldown {
|
||||
return errors.New("gathering cooldown")
|
||||
}
|
||||
if until := w.Depleted[target]; until > w.Age() {
|
||||
return errors.New("resource depleted")
|
||||
}
|
||||
for _, chunk := range w.neighbors(c.View.Entity.Position) {
|
||||
for _, r := range chunk.Resources {
|
||||
if r.ID != target {
|
||||
continue
|
||||
}
|
||||
if distance(c.View.Entity.Position, r.Position) > w.Balance.GatherRange {
|
||||
return errors.New("resource out of reach")
|
||||
}
|
||||
item := map[string]string{"tree": "wood", "ore": "ore", "herb": "herb"}[r.Kind]
|
||||
if item == "" {
|
||||
return errors.New("not gatherable")
|
||||
}
|
||||
c.View.Inventory[item] += w.yieldAmount(c.View.Stats["gatherRate"].Final)
|
||||
c.View.Skills["gathering"] += w.Balance.SkillXpPerAction
|
||||
c.LastGather = w.Age()
|
||||
w.Depleted[target] = w.Age() + w.Balance.ResourceRespawnSeconds
|
||||
w.dirty = true
|
||||
c.Message = "Gathered " + item
|
||||
w.RefreshStats(c)
|
||||
w.grantXP(c, w.Balance.Progression.GatherXP)
|
||||
w.evaluateTitles(c)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("unknown resource")
|
||||
}
|
||||
func (w *World) craft(c *Character, name string) error {
|
||||
r, ok := w.Balance.Recipes[name]
|
||||
if !ok {
|
||||
@@ -551,6 +567,8 @@ func (w *World) kill(c *Character, reason string) {
|
||||
return
|
||||
}
|
||||
e := c.View.Entity
|
||||
e.Position = w.terrain.Position(e.Position.X, e.Position.Z)
|
||||
c.View.Entity.Position = e.Position
|
||||
e.ID = "corpse:" + e.ID
|
||||
e.Kind = "corpse"
|
||||
e.State = "dead"
|
||||
@@ -561,6 +579,7 @@ func (w *World) kill(c *Character, reason string) {
|
||||
c.View.Equipment = map[string]string{}
|
||||
c.View.CarryWeight = 0
|
||||
c.View.Entity.HP = 0
|
||||
c.JumpVelocity = 0
|
||||
c.View.Entity.State = "dead"
|
||||
c.MoveX = 0
|
||||
c.MoveZ = 0
|
||||
@@ -608,6 +627,7 @@ func (w *World) Control(op, target, value, reason string) error {
|
||||
case "heal":
|
||||
c.View.Entity.HP = c.View.Entity.MaxHP
|
||||
case "unstuck":
|
||||
c.JumpVelocity = 0
|
||||
c.View.Entity.Position = w.terrain.Position(0, 0)
|
||||
c.MoveX, c.MoveZ = 0, 0
|
||||
case "retire":
|
||||
@@ -638,6 +658,18 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
continue
|
||||
}
|
||||
w.resetDay(c)
|
||||
nowSeconds := float64(w.now.UnixMilli()) / 1000
|
||||
effects := c.View.GatheringEffects[:0]
|
||||
for _, e := range c.View.GatheringEffects {
|
||||
if e.ExpiresAt > nowSeconds {
|
||||
effects = append(effects, e)
|
||||
}
|
||||
}
|
||||
if len(effects) != len(c.View.GatheringEffects) {
|
||||
c.View.GatheringEffects = effects
|
||||
w.RefreshStats(c)
|
||||
w.dirty = true
|
||||
}
|
||||
if c.Online {
|
||||
midnight := time.Date(now.UTC().Year(), now.UTC().Month(), now.UTC().Day(), 0, 0, 0, 0, time.UTC)
|
||||
c.DailySeconds += math.Min(playedDT, now.Sub(midnight).Seconds())
|
||||
@@ -648,6 +680,7 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
c.ReturnPosition = &pos
|
||||
c.Sheltered = true
|
||||
c.View.HasKeystone = true
|
||||
c.JumpVelocity = 0
|
||||
c.View.Entity.Position = w.terrain.Position(0, 0)
|
||||
c.View.Entity.HP = math.Min(c.View.Entity.MaxHP, math.Max(50, c.View.Entity.HP))
|
||||
c.Online = false
|
||||
@@ -670,12 +703,7 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
c.MoveX = 0
|
||||
c.MoveZ = 0
|
||||
}
|
||||
pos := c.View.Entity.Position
|
||||
speed := w.playerSpeed(c)
|
||||
nx, nz := pos.X+c.MoveX*speed*dt, pos.Z+c.MoveZ*speed*dt
|
||||
if math.Hypot(nx, nz) < w.Balance.WorldRadius {
|
||||
c.View.Entity.Position = w.terrain.Position(nx, nz)
|
||||
}
|
||||
w.moveCharacter(c, dt)
|
||||
for _, ch := range w.neighbors(c.View.Entity.Position) {
|
||||
active[ch.Key] = true
|
||||
w.ensureMob(ch)
|
||||
@@ -804,14 +832,40 @@ func (w *World) updateMobs(active map[string]bool, dt float64) {
|
||||
func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
|
||||
c := w.Characters[id]
|
||||
s := p.Snapshot{Type: "snapshot", Version: 1, Tick: w.Tick, World: w.Info(), Self: c.View, Message: c.Message, Entities: []p.Entity{}, Chunks: []p.Chunk{}, ChunkKeys: []string{}, UnavailableResources: []string{}}
|
||||
s.GatherRisks = map[string]p.GatheringRisk{}
|
||||
allowed := map[string]bool{}
|
||||
for _, ch := range w.neighbors(c.View.Entity.Position) {
|
||||
allowed[ch.Key] = true
|
||||
s.ChunkKeys = append(s.ChunkKeys, ch.Key)
|
||||
if !known[ch.Key] {
|
||||
ch.Resources = append([]p.Entity{}, ch.Resources...)
|
||||
for index := range ch.Resources {
|
||||
e := &ch.Resources[index]
|
||||
id := stringValue(e.ResourceID)
|
||||
if id == "" {
|
||||
id = e.Kind
|
||||
}
|
||||
if r := w.resource(id); r != nil {
|
||||
e.ResourceID = stringPtr(r.ID)
|
||||
e.SkillID = stringPtr(r.SkillID)
|
||||
e.RequiredLevel = &r.RequiredLevel
|
||||
e.Name = r.Name
|
||||
}
|
||||
}
|
||||
s.Chunks = append(s.Chunks, ch)
|
||||
}
|
||||
for _, r := range ch.Resources {
|
||||
if w.Depleted[r.ID] <= w.Age() {
|
||||
id := stringValue(r.ResourceID)
|
||||
if id == "" {
|
||||
id = r.Kind
|
||||
}
|
||||
if def := w.resource(id); def != nil {
|
||||
if _, ok := s.GatherRisks[def.ID]; !ok {
|
||||
s.GatherRisks[def.ID] = w.gatherRisk(c, *def)
|
||||
}
|
||||
}
|
||||
}
|
||||
if w.Depleted[r.ID] > w.Age() {
|
||||
s.UnavailableResources = append(s.UnavailableResources, r.ID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user