1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-22 23:25:23 +00:00

anim data: lintfix

This commit is contained in:
M. Sz 2021-02-26 15:11:56 +01:00
parent 25ea801cd4
commit 62c58468e9
3 changed files with 18 additions and 53 deletions

View File

@ -56,6 +56,7 @@ func (ad *AnimationData) GetRecords(name string) []*AnimationDataRecord {
return ad.entries[name]
}
// GetRecordsCount returns number of animation data records
func (ad *AnimationData) GetRecordsCount() int {
return len(ad.entries)
}
@ -160,8 +161,10 @@ func (ad *AnimationData) Marshal() []byte {
// keys - all entries in animationData
keys := make([]string, len(ad.entries))
// we must manually add index
idx := 0
for i := range ad.entries {
keys[idx] = i
idx++
@ -169,11 +172,13 @@ func (ad *AnimationData) Marshal() []byte {
// name terminates current name
name := 0
// recordIdx determinates current record index
recordIdx := 0
// numberOfEntries is a number of entries in all map indexes
var numberOfEntries int = 0
for i := 0; i < len(keys); i++ {
numberOfEntries += len(ad.entries[keys[i]])
}
@ -181,16 +186,18 @@ func (ad *AnimationData) Marshal() []byte {
for idx := 0; idx < numBlocks; idx++ {
// number of records (max is maxRecordsPerObject)
l := 0
switch {
// first condition: end up with all this and push 0 to dhe end
if numberOfEntries == 0 {
case numberOfEntries == 0:
sw.PushUint32(0)
continue
case numberOfEntries < maxRecordsPerBlock:
// second condition - if number of entries left is smaller than
// maxRecordsPerBlock, push...
} else if numberOfEntries < maxRecordsPerBlock {
l = int(numberOfEntries)
l = numberOfEntries
sw.PushUint32(uint32(l))
} else {
default:
// else use maxRecordsPerBlock
l = maxRecordsPerBlock
sw.PushUint32(maxRecordsPerBlock)
@ -198,6 +205,7 @@ func (ad *AnimationData) Marshal() []byte {
for currentRecordIdx := 0; currentRecordIdx < l; currentRecordIdx++ {
numberOfEntries--
if recordIdx == len(ad.entries[keys[name]]) {
recordIdx = 0
name++
@ -208,8 +216,8 @@ func (ad *AnimationData) Marshal() []byte {
name := animationRecord.name
missingZeroBytes := byteCountName - len(name)
fmt.Println(name)
sw.PushBytes([]byte(name)...)
for i := 0; i < missingZeroBytes; i++ {
sw.PushBytes(0)
}

View File

@ -1,57 +1,11 @@
package d2animdata
import (
"fmt"
"log"
"os"
"testing"
)
func exampleData() *AnimationData {
testEntries := []*AnimationDataRecord{
&AnimationDataRecord{
"TST",
5, 8,
map[int]AnimationEvent{
1: AnimationEventNone,
},
},
&AnimationDataRecord{
"TST",
8, 3,
map[int]AnimationEvent{
2: AnimationEventNone,
},
},
}
testEntries2 := []*AnimationDataRecord{
&AnimationDataRecord{
"TTT",
7, 8,
map[int]AnimationEvent{
1: AnimationEventNone,
},
},
&AnimationDataRecord{
"TTT",
8, 9,
map[int]AnimationEvent{
8: AnimationEventNone,
},
},
}
result := &AnimationData{
entries: map[string][]*AnimationDataRecord{
"TST": testEntries,
"TTT": testEntries2,
},
}
return result
}
func TestLoad(t *testing.T) {
testFile, fileErr := os.Open("testdata/AnimData.d2")
if fileErr != nil {
@ -225,7 +179,9 @@ func TestAnimationDataRecord_Marshal(t *testing.T) {
if err != nil {
t.Error(err)
}
newData := ad.Marshal()
newAd, err := Load(newData)
if err != nil {
t.Error(err)
@ -235,6 +191,7 @@ func TestAnimationDataRecord_Marshal(t *testing.T) {
for i := range ad.entries {
keys1 = append(keys1, i)
}
keys2 := make([]string, 0)
for i := range newAd.entries {
keys2 = append(keys2, i)
@ -244,10 +201,8 @@ func TestAnimationDataRecord_Marshal(t *testing.T) {
t.Fatalf("unexpected length of keys in first and second dict: %d, %d", len(keys1), len(keys2))
}
fmt.Println(len(ad.entries["TST"]))
for key := range newAd.entries {
for n, i := range newAd.entries[key] {
fmt.Println(i.speed, ad.entries[key][n].speed)
if i.speed != ad.entries[key][n].speed {
t.Fatal("unexpected record set")
}

View File

@ -8,10 +8,12 @@ type AnimationDataRecord struct {
events map[int]AnimationEvent
}
// FramesPerDirection returns frames per direction value
func (r *AnimationDataRecord) FramesPerDirection() int {
return int(r.framesPerDirection)
}
// Speed returns animation's speed
func (r *AnimationDataRecord) Speed() int {
return int(r.speed)
}