2020-01-26 00:39:13 -05:00
|
|
|
package d2datadict
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2020-06-30 09:17:07 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-01-26 00:39:13 -05:00
|
|
|
)
|
|
|
|
|
2020-06-30 09:17:07 -04:00
|
|
|
// ObjectLookupRecord is a representation of a row from objects.txt
|
2020-01-26 00:39:13 -05:00
|
|
|
type ObjectLookupRecord struct {
|
|
|
|
Act int
|
2020-06-30 09:17:07 -04:00
|
|
|
Type d2enum.ObjectType
|
2020-07-07 08:56:31 -04:00
|
|
|
Id int //nolint:golint,stylecheck // Id is the right key
|
2020-06-27 23:15:20 -04:00
|
|
|
Name string
|
2020-01-26 00:39:13 -05:00
|
|
|
Description string
|
2020-07-07 08:56:31 -04:00
|
|
|
ObjectsTxtId int //nolint:golint,stylecheck // Id is the right key
|
|
|
|
MonstatsTxtId int //nolint:golint,stylecheck // Id is the right key
|
2020-01-26 00:39:13 -05:00
|
|
|
Direction int
|
|
|
|
Base string
|
|
|
|
Token string
|
|
|
|
Mode string
|
|
|
|
Class string
|
|
|
|
HD string
|
|
|
|
TR string
|
|
|
|
LG string
|
|
|
|
RA string
|
|
|
|
LA string
|
|
|
|
RH string
|
|
|
|
LH string
|
|
|
|
SH string
|
|
|
|
S1 string
|
|
|
|
S2 string
|
|
|
|
S3 string
|
|
|
|
S4 string
|
|
|
|
S5 string
|
|
|
|
S6 string
|
|
|
|
S7 string
|
|
|
|
S8 string
|
|
|
|
ColorMap string
|
|
|
|
Index int
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:17:07 -04:00
|
|
|
// LookupObject looks up an object record
|
2020-01-26 00:39:13 -05:00
|
|
|
func LookupObject(act, typ, id int) *ObjectLookupRecord {
|
|
|
|
object := lookupObject(act, typ, id, indexedObjects)
|
|
|
|
if object == nil {
|
|
|
|
log.Panicf("Failed to look up object Act: %d, Type: %d, Id: %d", act, typ, id)
|
|
|
|
}
|
2020-06-29 12:37:11 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return object
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupObject(act, typ, id int, objects [][][]*ObjectLookupRecord) *ObjectLookupRecord {
|
2020-06-30 09:17:07 -04:00
|
|
|
if len(objects) < act {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(objects[act]) < typ {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(objects[act][typ]) < id {
|
|
|
|
return nil
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
2020-06-29 12:37:11 -04:00
|
|
|
|
2020-06-30 09:17:07 -04:00
|
|
|
return objects[act][typ][id]
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 09:17:07 -04:00
|
|
|
// InitObjectRecords loads ObjectRecords
|
|
|
|
func InitObjectRecords() {
|
2020-01-26 00:39:13 -05:00
|
|
|
indexedObjects = indexObjects(objectLookups)
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexObjects(objects []ObjectLookupRecord) [][][]*ObjectLookupRecord {
|
|
|
|
// Allocating 6 to allow Acts 1-5 without requiring a -1 at every read.
|
|
|
|
indexedObjects = make([][][]*ObjectLookupRecord, 6)
|
2020-06-29 12:37:11 -04:00
|
|
|
|
2020-02-01 21:51:49 -05:00
|
|
|
for i := range objects {
|
2020-01-26 00:39:13 -05:00
|
|
|
record := &objects[i]
|
|
|
|
if indexedObjects[record.Act] == nil {
|
|
|
|
// Likewise allocating 3 so a -1 isn't necessary.
|
|
|
|
indexedObjects[record.Act] = make([][]*ObjectLookupRecord, 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
if indexedObjects[record.Act][record.Type] == nil {
|
|
|
|
// For simplicity, allocating with length 1000 then filling the values in by index.
|
|
|
|
// If ids in the dictionary ever surpass 1000, raise this number.
|
|
|
|
indexedObjects[record.Act][record.Type] = make([]*ObjectLookupRecord, 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
indexedObjects[record.Act][record.Type][record.Id] = record
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexedObjects
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:17:07 -04:00
|
|
|
// IndexedObjects is a slice of object records for quick lookups.
|
2020-01-26 00:39:13 -05:00
|
|
|
// nil checks should be done for uninitialized values at each level.
|
|
|
|
// [Act 1-5][Type 1-2][Id 0-855]
|
2020-06-30 09:17:07 -04:00
|
|
|
//nolint:gochecknoglobals // Currently global by design
|
2020-01-26 00:39:13 -05:00
|
|
|
var indexedObjects [][][]*ObjectLookupRecord
|