add rare prefix+suffix loaders, d2items use rare item names (#702)

* adding loaders for rare prefix/suffix records

* switch to slices instead of maps for storing rare prefix/suffix records

* rare items now use the rare prefix/suffix names
This commit is contained in:
lord 2020-08-06 19:32:40 -07:00 committed by GitHub
parent 16b8a6467f
commit 0ea6bd5b92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 6 deletions

View File

@ -289,6 +289,8 @@ func (a *App) loadDataDict() error {
{d2resource.ObjectGroup, d2datadict.LoadObjectGroups},
{d2resource.CompCode, d2datadict.LoadComponentCodes},
{d2resource.MonsterAI, d2datadict.LoadMonsterAI},
{d2resource.RarePrefix, d2datadict.LoadRareItemPrefixRecords},
{d2resource.RareSuffix, d2datadict.LoadRareItemSuffixRecords},
}
d2datadict.InitObjectRecords()

View File

@ -0,0 +1,59 @@
package d2datadict
import (
"fmt"
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
)
const (
numRarePrefixInclude = 7
fmtRarePrefixInclude = "itype%d"
numRarePrefixExclude = 4
fmtRarePrefixExclude = "etype%d"
)
// RareItemPrefixRecord is a name prefix for rare items (items with more than 2 affixes)
type RareItemPrefixRecord struct {
Name string
IncludedTypes []string
ExcludedTypes []string
}
// RarePrefixes is where all RareItemPrefixRecords are stored
var RarePrefixes []*RareItemPrefixRecord // nolint:gochecknoglobals // global by design
// LoadRareItemPrefixRecords loads the rare item prefix records from rareprefix.txt
func LoadRareItemPrefixRecords(file []byte) {
d := d2common.LoadDataDictionary(file)
RarePrefixes = make([]*RareItemPrefixRecord, 0)
for d.Next() {
record := &RareItemPrefixRecord{
Name: d.String("name"),
IncludedTypes: make([]string, 0),
ExcludedTypes: make([]string, 0),
}
for idx := 1; idx <= numRarePrefixInclude; idx++ {
column := fmt.Sprintf(fmtRarePrefixInclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.IncludedTypes = append(record.IncludedTypes, typeCode)
}
}
for idx := 1; idx <= numRarePrefixExclude; idx++ {
column := fmt.Sprintf(fmtRarePrefixExclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.ExcludedTypes = append(record.ExcludedTypes, typeCode)
}
}
RarePrefixes = append(RarePrefixes, record)
}
log.Printf("Loaded %d RarePrefix records", len(RarePrefixes))
}

View File

@ -0,0 +1,59 @@
package d2datadict
import (
"fmt"
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
)
const (
numRareSuffixInclude = 7
fmtRareSuffixInclude = "itype%d"
numRareSuffixExclude = 4
fmtRareSuffixExclude = "etype%d"
)
// RareItemSuffixRecord is a name suffix for rare items (items with more than 2 affixes)
type RareItemSuffixRecord struct {
Name string
IncludedTypes []string
ExcludedTypes []string
}
// RareSuffixes is where all RareItemSuffixRecords are stored
var RareSuffixes []*RareItemSuffixRecord // nolint:gochecknoglobals // global by design
// LoadRareItemSuffixRecords loads the rare item suffix records from raresuffix.txt
func LoadRareItemSuffixRecords(file []byte) {
d := d2common.LoadDataDictionary(file)
RareSuffixes = make([]*RareItemSuffixRecord, 0)
for d.Next() {
record := &RareItemSuffixRecord{
Name: d.String("name"),
IncludedTypes: make([]string, 0),
ExcludedTypes: make([]string, 0),
}
for idx := 1; idx <= numRareSuffixInclude; idx++ {
column := fmt.Sprintf(fmtRareSuffixInclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.IncludedTypes = append(record.IncludedTypes, typeCode)
}
}
for idx := 1; idx <= numRareSuffixExclude; idx++ {
column := fmt.Sprintf(fmtRareSuffixExclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.ExcludedTypes = append(record.ExcludedTypes, typeCode)
}
}
RareSuffixes = append(RareSuffixes, record)
}
log.Printf("Loaded %d RareSuffix records", len(RareSuffixes))
}

View File

@ -241,11 +241,10 @@ const (
MagicPrefix = "/data/global/excel/MagicPrefix.txt"
MagicSuffix = "/data/global/excel/MagicSuffix.txt"
RarePrefix = "/data/global/excel/RarePrefix.txt" // these are for item names
RareSuffix = "/data/global/excel/RareSuffix.txt"
// --- Monster Prefix/Suffixes (?) ---
RarePrefix = "/data/global/excel/RarePrefix.txt"
RareSuffix = "/data/global/excel/RareSuffix.txt"
UniquePrefix = "/data/global/excel/UniquePrefix.txt"
UniqueSuffix = "/data/global/excel/UniqueSuffix.txt"

View File

@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"sort"
"strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
@ -655,20 +656,42 @@ func (i *Item) generateName() {
name := d2common.TranslateString(i.CommonRecord().NameString)
if i.PrefixRecords() != nil {
numAffixes := 0
if prefixes := i.PrefixRecords(); prefixes != nil {
numAffixes += len(prefixes)
}
if suffixes := i.SuffixRecords(); suffixes != nil {
numAffixes += len(suffixes)
}
// if it has 1 to 2 affixes, it's a magic item, and we just put the current item
// name between the prefix and suffix strings
if numAffixes > 0 && numAffixes < 3 {
if len(i.PrefixRecords()) > 0 {
affix := i.PrefixRecords()[i.rand.Intn(len(i.PrefixRecords()))]
name = fmt.Sprintf("%s %s", affix.Name, name)
}
}
if i.SuffixRecords() != nil {
if len(i.SuffixRecords()) > 0 {
affix := i.SuffixRecords()[i.rand.Intn(len(i.SuffixRecords()))]
name = fmt.Sprintf("%s %s", name, affix.Name)
}
}
// if it has more than 2 affixes, it's a rare item
// rare items use entries from rareprefix.txt and raresuffix.txt to make their names,
// and the prefix and suffix actually go before thec current item name
if numAffixes >= 3 {
i.rand.Seed(i.Seed)
numPrefix, numSuffix := len(d2datadict.RarePrefixes), len(d2datadict.RareSuffixes)
preIdx, sufIdx := i.rand.Intn(numPrefix), i.rand.Intn(numSuffix)
prefix := d2datadict.RarePrefixes[preIdx].Name
suffix := d2datadict.RareSuffixes[sufIdx].Name
name = fmt.Sprintf("%s %s\n%s", strings.Title(prefix), strings.Title(suffix), name)
}
i.name = name
}
@ -795,10 +818,13 @@ func findMatchingAffixes(
}
// these functions are to satisfy the inventory grid item interface
// GetInventoryItemName returns the item name
func (i *Item) GetInventoryItemName() string {
return i.Label()
}
// GetInventoryItemType returns whether the item is a weapon, armor, or misc item
func (i *Item) GetInventoryItemType() d2enum.InventoryItemType {
typeCode := i.TypeRecord().Code