231 lines
6.7 KiB
Go
231 lines
6.7 KiB
Go
package game
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"slices"
|
|
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
)
|
|
|
|
func protocolModifiers(in []content.StatModifier) []p.StatModifier {
|
|
out := make([]p.StatModifier, 0, len(in))
|
|
for _, m := range in {
|
|
out = append(out, p.StatModifier{Skill: stringPtr(m.Skill), Stat: m.Stat, Flat: m.Flat, Percent: m.Percent})
|
|
}
|
|
return out
|
|
}
|
|
func (w *World) statValues(c *Character, equipment bool) map[string]p.StatValue {
|
|
modifiers := []p.StatModifier{}
|
|
seen := map[string]bool{}
|
|
for _, title := range c.View.Titles {
|
|
if seen[title.ID] {
|
|
continue
|
|
}
|
|
seen[title.ID] = true
|
|
modifiers = append(modifiers, title.Effects.Modifiers...)
|
|
modifiers = append(modifiers, p.StatModifier{Stat: "meleeDamage", Flat: title.Effects.Damage}, p.StatModifier{Stat: "moveSpeed", Flat: title.Effects.MoveSpeed})
|
|
}
|
|
if equipment {
|
|
ids := map[string]bool{}
|
|
for _, id := range c.View.Equipment {
|
|
ids[id] = true
|
|
}
|
|
for _, item := range c.View.Items {
|
|
if ids[item.ID] {
|
|
modifiers = append(modifiers, item.Modifiers...)
|
|
}
|
|
}
|
|
}
|
|
values := map[string]p.StatValue{}
|
|
finish := func(key string, base float64) {
|
|
v := p.StatValue{Base: base}
|
|
for _, m := range modifiers {
|
|
if m.Stat == key {
|
|
v.Flat += m.Flat
|
|
v.Percent += m.Percent
|
|
}
|
|
}
|
|
v.Final = math.Max(0, (v.Base+v.Flat)*math.Max(0, 1+v.Percent/100))
|
|
if key == "healthPoints" {
|
|
v.Final = math.Max(50, v.Final)
|
|
}
|
|
if key == "criticalChance" || key == "dodgeChance" {
|
|
v.Final = math.Min(100, v.Final)
|
|
}
|
|
values[key] = v
|
|
}
|
|
for _, key := range content.PrimaryStats {
|
|
finish(key, w.Balance.Stats.Base[key]+float64(c.View.AllocatedStats[key]))
|
|
}
|
|
for _, key := range content.DerivedStats {
|
|
base := w.Balance.Stats.Base[key]
|
|
for _, primary := range content.PrimaryStats {
|
|
base += values[primary].Final * w.Balance.Stats.Coefficients[primary][key]
|
|
}
|
|
switch key {
|
|
case "healthPoints":
|
|
base += float64(c.View.HealthChoices * c.Progression.VitalPointsPerChoice)
|
|
case "manaPoints":
|
|
base += float64(c.View.ManaChoices * c.Progression.VitalPointsPerChoice)
|
|
case "stamina":
|
|
base += float64((c.View.Level - 1) * c.Progression.StaminaPerLevel)
|
|
}
|
|
finish(key, base)
|
|
}
|
|
return values
|
|
}
|
|
|
|
// Requirements exclude every equipment bonus to prevent self/cyclic enabling.
|
|
func (w *World) RequirementStats(c *Character) map[string]p.StatValue { return w.statValues(c, false) }
|
|
func (w *World) RefreshStats(c *Character) {
|
|
if c.View.Entity.State == "dead" && len(c.View.Stats) > 0 {
|
|
return
|
|
}
|
|
w.revalidateEquipment(c)
|
|
c.View.Stats = w.statValues(c, true)
|
|
w.refreshGathering(c)
|
|
w.refreshCrafting(c)
|
|
w.unlockPowers(c)
|
|
attack := w.playerAttack(c)
|
|
c.View.Attack = &attack
|
|
c.View.Entity.MaxHP = c.View.Stats["healthPoints"].Final
|
|
c.View.Entity.HP = math.Min(c.View.Entity.HP, c.View.Entity.MaxHP)
|
|
c.View.Mana = math.Max(0, math.Min(c.View.Mana, c.View.Stats["manaPoints"].Final))
|
|
c.View.Stamina = math.Max(0, math.Min(c.View.Stamina, c.View.Stats["stamina"].Final))
|
|
c.View.CarryWeight = 0
|
|
for _, item := range c.View.Items {
|
|
c.View.CarryWeight += item.Weight
|
|
}
|
|
// Stack quantities have unit weight in this first inventory version.
|
|
for _, n := range c.View.Inventory {
|
|
c.View.CarryWeight += float64(n)
|
|
}
|
|
factor := w.EncumbranceFactor(c)
|
|
c.View.MovementFactor = &factor
|
|
}
|
|
func (w *World) initializeStats(c *Character) error {
|
|
newResources := c.View.Stats == nil
|
|
if c.View.AllocatedStats == nil {
|
|
c.View.AllocatedStats = map[string]int64{}
|
|
}
|
|
spent := int64(0)
|
|
for key, n := range c.View.AllocatedStats {
|
|
if !slices.Contains(content.PrimaryStats, key) || n < 0 || n > 10000000 {
|
|
return errors.New("invalid allocated stats")
|
|
}
|
|
}
|
|
for _, n := range c.View.AllocatedStats {
|
|
spent += n
|
|
}
|
|
if spent+c.View.StatPoints != (c.View.Level-1)*c.Progression.StatPointsPerLevel {
|
|
return errors.New("allocated and unspent points do not match earned levels")
|
|
}
|
|
if c.View.PendingVitalChoices < 0 || c.View.HealthChoices < 0 || c.View.ManaChoices < 0 || c.View.PendingVitalChoices+c.View.HealthChoices+c.View.ManaChoices != c.View.Level-1 {
|
|
return errors.New("invalid level reward choices")
|
|
}
|
|
w.RefreshStats(c)
|
|
if newResources && c.View.Entity.State != "dead" {
|
|
c.View.Mana = c.View.Stats["manaPoints"].Final
|
|
c.View.Stamina = c.View.Stats["stamina"].Final
|
|
}
|
|
return nil
|
|
}
|
|
func (w *World) allocateStat(c *Character, key string) error {
|
|
if !slices.Contains(content.PrimaryStats, key) || c.View.StatPoints < 1 {
|
|
return errors.New("choose a primary stat with an available point")
|
|
}
|
|
c.View.AllocatedStats[key]++
|
|
c.View.StatPoints--
|
|
w.RefreshStats(c)
|
|
w.evaluateTitles(c)
|
|
w.dirty = true
|
|
return nil
|
|
}
|
|
func (w *World) chooseVital(c *Character, key string) error {
|
|
if c.View.PendingVitalChoices < 1 || (key != "health" && key != "mana") {
|
|
return errors.New("no pending health or mana choice")
|
|
}
|
|
if key == "health" {
|
|
c.View.HealthChoices++
|
|
} else {
|
|
c.View.ManaChoices++
|
|
}
|
|
c.View.PendingVitalChoices--
|
|
w.RefreshStats(c)
|
|
w.evaluateTitles(c)
|
|
w.dirty = true
|
|
return nil
|
|
}
|
|
func (w *World) ApplyCatalog(catalog content.Catalog) error {
|
|
if catalog.Version <= w.CatalogVersion {
|
|
return nil
|
|
}
|
|
if err := catalog.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if catalog.Gathering != nil {
|
|
w.Balance.Gathering = catalog.Gathering
|
|
w.terrain.B.Gathering = catalog.Gathering
|
|
}
|
|
if catalog.Combat != nil {
|
|
w.Balance.Combat = catalog.Combat
|
|
}
|
|
if catalog.Building != nil {
|
|
w.Balance.Building = catalog.Building
|
|
}
|
|
if catalog.Powers != nil {
|
|
w.Balance.Powers = catalog.Powers
|
|
}
|
|
if catalog.Crafting != nil {
|
|
w.Balance.Crafting = catalog.Crafting
|
|
}
|
|
w.Balance.Titles = catalog.Titles
|
|
w.Balance.Items = catalog.Items
|
|
w.Balance.Stats = &catalog.Stats
|
|
w.CatalogVersion = catalog.Version
|
|
for _, c := range w.orderedCharacters() {
|
|
w.RefreshStats(c)
|
|
}
|
|
w.dirty = true
|
|
return nil
|
|
}
|
|
func (w *World) roll(chance float64) bool {
|
|
if chance <= 0 {
|
|
return false
|
|
}
|
|
if chance >= 100 {
|
|
return true
|
|
}
|
|
return w.randomUnit()*100 < chance
|
|
}
|
|
func (w *World) randomUnit() float64 {
|
|
w.RandomCounter++
|
|
sum := sha256.Sum256([]byte(fmt.Sprintf("%d:%d", w.Seed, w.RandomCounter)))
|
|
return float64(binary.LittleEndian.Uint64(sum[:8])>>11) / float64(uint64(1)<<53)
|
|
}
|
|
func (w *World) yieldAmount(rate float64) int64 {
|
|
amount := math.Max(0, math.Min(float64(w.Balance.MaxYieldPerAction), rate/100))
|
|
n := int64(math.Floor(amount))
|
|
if w.roll((amount - float64(n)) * 100) {
|
|
n++
|
|
}
|
|
return n
|
|
}
|
|
func (w *World) damageCharacter(c *Character, amount float64) {
|
|
if w.roll(c.View.Stats["dodgeChance"].Final) {
|
|
return
|
|
}
|
|
damage := math.Max(0, amount-c.View.Stats["defence"].Final)
|
|
if damage > 0 && c.PendingCast != nil {
|
|
c.PendingCast = nil
|
|
c.View.Casting = nil
|
|
c.Message = "Casting interrupted by damage"
|
|
}
|
|
c.View.Entity.HP -= damage
|
|
}
|