1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-13 20:16:41 -05:00
OpenDiablo2/d2common/d2data/d2datadict/npc.go
lord 0218cad717
organize d2common pakage (#716)
* move music path enumerations into d2resource

* move text dictionary (.tbl) loader into d2fileformats sub-package d2tbl

* lint fix, add doc file for d2tbl

* moved data_dictionary.go into d2fileformats sub-package d2txt, added doc file

* added sub-packages d2geom for geometry-related things, and d2path for path-related things

* moved calcstring.go to d2calculation

* move bitmuncher, bitstream, stream reader/writer from d2common into sub-package d2datautils

* fix lint errors in d2datadict loaders (caused by moving stuf around in d2common)

* move size.go into d2geom

* move d2common/cache.go into sub-package d2common/d2cache

* renamed d2debugutil to d2util, moved utility functions from d2common into d2util
2020-09-08 15:58:35 -04:00

108 lines
2.8 KiB
Go

package d2datadict
import (
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt"
)
const (
costDivisor = 1024.
)
// NPCRecord represents a single line in NPC.txt
// The information has been gathered from [https:// d2mods.info/forum/kb/viewarticle?a=387]
type NPCRecord struct {
// Name is an ID pointer to row of this npc in monstats.txt
Name string
Multipliers *costMultiplier
QuestMultipliers map[int]*costMultiplier
// MaxBuy is the maximum amount of gold an NPC will pay for an item for the corresponding
// difficulty
MaxBuy struct {
Normal int
Nightmare int
Hell int
}
}
type costMultiplier struct {
// Buy is a percentage of base item price used when an item is bought by NPC
Buy float64
// Sell is a percentage of base item price used when an item is sold by NPC
Sell float64
// Repair is a percentage of base item price used to calculate the base repair price
Repair float64
}
// NPCs stores the NPCRecords
var NPCs map[string]*NPCRecord // nolint:gochecknoglobals // Currently global by design
// LoadNPCs loads NPCRecords into NPCs
func LoadNPCs(file []byte) {
NPCs = make(map[string]*NPCRecord)
d := d2txt.LoadDataDictionary(file)
for d.Next() {
record := &NPCRecord{
Name: d.String("npc"),
Multipliers: &costMultiplier{
Buy: float64(d.Number("buy mult")) / costDivisor,
Sell: float64(d.Number("sell mult")) / costDivisor,
Repair: float64(d.Number("rep mult")) / costDivisor,
},
MaxBuy: struct {
Normal int
Nightmare int
Hell int
}{
Normal: d.Number("max buy"),
Nightmare: d.Number("max buy (N)"),
Hell: d.Number("max buy (H)"),
},
}
record.QuestMultipliers = make(map[int]*costMultiplier)
if flagStr := d.String("questflag A"); flagStr != "" {
flag := d.Number("questflag A")
record.QuestMultipliers[flag] = &costMultiplier{
float64(d.Number("questbuymult A")) / costDivisor,
float64(d.Number("questsellmult A")) / costDivisor,
float64(d.Number("questrepmult A")) / costDivisor,
}
}
if flagStr := d.String("questflag B"); flagStr != "" {
flag := d.Number("questflag B")
record.QuestMultipliers[flag] = &costMultiplier{
float64(d.Number("questbuymult B")) / costDivisor,
float64(d.Number("questsellmult B")) / costDivisor,
float64(d.Number("questrepmult B")) / costDivisor,
}
}
if flagStr := d.String("questflag C"); flagStr != "" {
flag := d.Number("questflag C")
record.QuestMultipliers[flag] = &costMultiplier{
float64(d.Number("questbuymult C")) / costDivisor,
float64(d.Number("questsellmult C")) / costDivisor,
float64(d.Number("questrepmult C")) / costDivisor,
}
}
NPCs[record.Name] = record
}
if d.Err != nil {
panic(d.Err)
}
log.Printf("Loaded %d NPC records", len(NPCs))
}