mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-03 15:17:04 -05:00
cf6029eb95
* adding ranged number type, for use in stats * Loaded Skills.txt * asset manager only binds terminal commands if terminal != nil * WIP stats * cache getter and clear methods were not implemented * asset manager handles a nil terminal pointer * adding skilldesc.txt loader (needs work) * ctc stat descriptions functions working * moving description functionality out of itemstatcost loader and into stats * stats seem like a central part of diablo, moving into d2core. * stats seem like a central part of diablo, moving into d2core. * delint * adding statlist, statlist reduction, unit tests * minor edits to stat.go * lint error in statlist.go * Remove dependency on actual data from mpq files stats unit tests now use mock data * fixing some lint errors, formatting Co-authored-by: Maxime Lavigne (malavv) <duguigne@gmail.com>
83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package d2stats
|
|
|
|
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
|
|
|
// CreateStatList creates a stat list
|
|
func CreateStatList(stats ...*Stat) *StatList {
|
|
return &StatList{stats}
|
|
}
|
|
|
|
// StatList is a list that contains stats
|
|
type StatList struct {
|
|
stats []*Stat
|
|
}
|
|
|
|
// Clone returns a deep copy of the stat list
|
|
func (sl *StatList) Clone() *StatList {
|
|
clone := &StatList{}
|
|
clone.stats = make([]*Stat, len(sl.stats))
|
|
|
|
for idx := range sl.stats {
|
|
clone.stats[idx] = sl.stats[idx].Clone()
|
|
}
|
|
|
|
return clone
|
|
}
|
|
|
|
// Reduce returns a new stat list, combining like stats
|
|
func (sl *StatList) Reduce() *StatList {
|
|
clone := sl.Clone()
|
|
reduction := make([]*Stat, 0)
|
|
|
|
// for quick lookups
|
|
lookup := make(map[*d2datadict.ItemStatCostRecord]int)
|
|
|
|
for len(clone.stats) > 0 {
|
|
applied := false
|
|
stat := clone.removeStat(0)
|
|
|
|
// use lookup, may have found it already
|
|
if idx, found := lookup[stat.Record]; found {
|
|
if success := reduction[idx].combine(stat); success {
|
|
continue
|
|
}
|
|
|
|
reduction = append(reduction, stat)
|
|
}
|
|
|
|
for idx := range reduction {
|
|
if reduction[idx].combine(stat) {
|
|
lookup[stat.Record] = idx
|
|
applied = true
|
|
|
|
break
|
|
}
|
|
}
|
|
|
|
if !applied {
|
|
reduction = append(reduction, stat)
|
|
}
|
|
}
|
|
|
|
clone.stats = reduction
|
|
|
|
return clone
|
|
}
|
|
|
|
func (sl *StatList) removeStat(idx int) *Stat {
|
|
picked := sl.stats[idx]
|
|
sl.stats[idx] = sl.stats[len(sl.stats)-1]
|
|
sl.stats[len(sl.stats)-1] = nil
|
|
sl.stats = sl.stats[:len(sl.stats)-1]
|
|
|
|
return picked
|
|
}
|
|
|
|
// Append returns a new stat list, combining like stats
|
|
func (sl *StatList) Append(other *StatList) *StatList {
|
|
clone := sl.Clone()
|
|
clone.stats = append(clone.stats, other.stats...)
|
|
|
|
return clone
|
|
}
|