bugfix: "insert" bug (#12)

* remove magic 'et' file

* ds1 refactor: test for InsertFloors; fixed inserting bug

* ds1: global method for getting layers group; fixed Delete.. bug (group var in delete meghod wasn't pointer

* ds1: lintfix

* ds1: remove ds1.ds1Layers.Orientation

* ds1: lintfix

* ds1: addet getMaxGroupLen method

* ds1: insert now returns if after inserting group will be greater than max number of ...

* ds1: add common test for ds1Layers.Insert...

Co-authored-by: M. Sz <mszeptuch@protonmail.com>
This commit is contained in:
gucio321 2021-03-27 21:09:53 +01:00 committed by GitHub
parent 468f5682ae
commit 41c1d8e874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 124 additions and 248 deletions

View File

@ -157,7 +157,6 @@ func (ds1 *DS1) loadBody(stream *d2datautils.StreamReader) error {
for ; numWalls > 0; numWalls-- {
ds1.PushWall(&layer{})
ds1.PushOrientation(&layer{})
}
for ; numShadows > 0; numShadows-- {
@ -241,8 +240,8 @@ func (ds1 *DS1) loadObjects(br *d2datautils.StreamReader) error {
for objIdx := 0; objIdx < int(numObjects); objIdx++ {
obj := Object{}
objType, err := br.ReadInt32()
objType, err := br.ReadInt32()
if err != nil {
return fmt.Errorf("reading object's %d type: %v", objIdx, err)
}

View File

@ -2,7 +2,6 @@ package d2ds1
const (
maxWallLayers = 4
maxOrientationLayers = 4
maxFloorLayers = 2
maxShadowLayers = 1
maxSubstitutionLayers = 1
@ -13,7 +12,6 @@ type layerGroupType int
const (
floorLayerGroup layerGroupType = iota
wallLayerGroup
orientationLayerGroup
shadowLayerGroup
substitutionLayerGroup
)
@ -24,7 +22,6 @@ type ds1Layers struct {
width, height int
Floors layerGroup
Walls layerGroup
Orientations layerGroup
Shadows layerGroup
Substitutions layerGroup
}
@ -38,10 +35,6 @@ func (l *ds1Layers) ensureInit() {
l.Walls = make(layerGroup, 0)
}
if l.Orientations == nil {
l.Orientations = make(layerGroup, 0)
}
if l.Shadows == nil {
l.Shadows = make(layerGroup, 0)
}
@ -55,27 +48,14 @@ func (l *ds1Layers) ensureInit() {
func (l *ds1Layers) cull() {
l.cullNilLayers(floorLayerGroup)
l.cullNilLayers(wallLayerGroup)
l.cullNilLayers(orientationLayerGroup)
l.cullNilLayers(shadowLayerGroup)
l.cullNilLayers(substitutionLayerGroup)
}
// removes nil layers of given layer group type
func (l *ds1Layers) cullNilLayers(t layerGroupType) {
var group *layerGroup
switch t {
case floorLayerGroup:
group = &l.Floors
case wallLayerGroup:
group = &l.Walls
case orientationLayerGroup:
group = &l.Orientations
case shadowLayerGroup:
group = &l.Shadows
case substitutionLayerGroup:
group = &l.Substitutions
default:
group := l.getLayersGroup(t)
if group == nil {
return
}
@ -106,7 +86,6 @@ func (l *ds1Layers) SetSize(w, h int) {
l.enforceSize(floorLayerGroup)
l.enforceSize(wallLayerGroup)
l.enforceSize(orientationLayerGroup)
l.enforceSize(shadowLayerGroup)
l.enforceSize(substitutionLayerGroup)
}
@ -115,20 +94,8 @@ func (l *ds1Layers) enforceSize(t layerGroupType) {
l.ensureInit()
l.cull()
var group *layerGroup
switch t {
case floorLayerGroup:
group = &l.Floors
case wallLayerGroup:
group = &l.Walls
case orientationLayerGroup:
group = &l.Orientations
case shadowLayerGroup:
group = &l.Shadows
case substitutionLayerGroup:
group = &l.Substitutions
default:
group := l.getLayersGroup(t)
if group == nil {
return
}
@ -160,29 +127,9 @@ func (l *ds1Layers) push(t layerGroupType, layer *layer) {
l.ensureInit()
l.cull()
var group *layerGroup
group := l.getLayersGroup(t)
var max int
switch t {
case floorLayerGroup:
group = &l.Floors
max = maxFloorLayers
case wallLayerGroup:
group = &l.Walls
max = maxWallLayers
case orientationLayerGroup:
group = &l.Orientations
max = maxOrientationLayers
case shadowLayerGroup:
group = &l.Shadows
max = maxShadowLayers
case substitutionLayerGroup:
group = &l.Substitutions
max = maxSubstitutionLayers
default:
return
}
max := getMaxGroupLen(t)
if len(*group) < max {
*group = append(*group, layer)
@ -194,25 +141,13 @@ func (l *ds1Layers) pop(t layerGroupType) *layer {
l.ensureInit()
l.cull()
var group *layerGroup
var theLayer *layer
switch t {
case floorLayerGroup:
group = &l.Floors
case wallLayerGroup:
group = &l.Walls
case orientationLayerGroup:
group = &l.Orientations
case shadowLayerGroup:
group = &l.Shadows
case substitutionLayerGroup:
group = &l.Substitutions
default:
group := l.getLayersGroup(t)
if group == nil {
return nil
}
var theLayer *layer
// remove last layer of slice and return it
if len(*group) > 0 {
lastIdx := len(*group) - 1
@ -229,20 +164,8 @@ func (l *ds1Layers) get(t layerGroupType, idx int) *layer {
l.ensureInit()
l.cull()
var group *layerGroup
switch t {
case floorLayerGroup:
group = &l.Floors
case wallLayerGroup:
group = &l.Walls
case orientationLayerGroup:
group = &l.Orientations
case shadowLayerGroup:
group = &l.Shadows
case substitutionLayerGroup:
group = &l.Substitutions
default:
group := l.getLayersGroup(t)
if group == nil {
return nil
}
@ -261,30 +184,22 @@ func (l *ds1Layers) insert(t layerGroupType, idx int, newLayer *layer) {
return
}
var group layerGroup
switch t {
case floorLayerGroup:
group = l.Floors
case wallLayerGroup:
group = l.Walls
case orientationLayerGroup:
group = l.Orientations
case shadowLayerGroup:
group = l.Shadows
case substitutionLayerGroup:
group = l.Substitutions
default:
group := l.getLayersGroup(t)
if group == nil {
return
}
if len(group) == 0 {
group = append(group, newLayer) // nolint:staticcheck // we possibly use group later
if len(*group)+1 > getMaxGroupLen(t) {
return
}
if idx > len(group)-1 {
idx = len(group) - 1
if len(*group) == 0 {
*group = append(*group, newLayer) // nolint:staticcheck // we possibly use group later
return
}
if l := len(*group) - 1; idx > l {
idx = l
}
// example:
@ -292,36 +207,24 @@ func (l *ds1Layers) insert(t layerGroupType, idx int, newLayer *layer) {
// idx=1
// newLayer=c
// existing layerGroup is [a, b]
group = append(group, group[idx:]...) // [a, b] becomes [a, b, b]
group[idx] = newLayer // [a, b, b] becomes [a, c, b]
newGroup := append((*group)[:idx], append([]*layer{newLayer}, (*group)[idx:]...)...)
*group = newGroup
}
func (l *ds1Layers) delete(t layerGroupType, idx int) {
l.ensureInit()
l.cull()
var group layerGroup
switch t {
case floorLayerGroup:
group = l.Floors
case wallLayerGroup:
group = l.Walls
case orientationLayerGroup:
group = l.Orientations
case shadowLayerGroup:
group = l.Shadows
case substitutionLayerGroup:
group = l.Substitutions
default:
group := l.getLayersGroup(t)
if group == nil {
return
}
if idx >= len(group) || idx < 0 {
if idx >= len(*group) || idx < 0 {
return
}
group[idx] = nil
(*group)[idx] = nil
l.cull()
}
@ -368,27 +271,6 @@ func (l *ds1Layers) DeleteWall(idx int) {
l.delete(wallLayerGroup, idx)
}
func (l *ds1Layers) GetOrientation(idx int) *layer {
return l.get(orientationLayerGroup, idx)
}
func (l *ds1Layers) PushOrientation(orientation *layer) *ds1Layers {
l.push(orientationLayerGroup, orientation)
return l
}
func (l *ds1Layers) PopOrientation() *layer {
return l.pop(orientationLayerGroup)
}
func (l *ds1Layers) InsertOrientation(idx int, newOrientation *layer) {
l.insert(orientationLayerGroup, idx, newOrientation)
}
func (l *ds1Layers) DeleteOrientation(idx int) {
l.delete(orientationLayerGroup, idx)
}
func (l *ds1Layers) GetShadow(idx int) *layer {
return l.get(shadowLayerGroup, idx)
}
@ -424,9 +306,43 @@ func (l *ds1Layers) PopSubstitution() *layer {
}
func (l *ds1Layers) InsertSubstitution(idx int, newSubstitution *layer) {
l.insert(shadowLayerGroup, idx, newSubstitution)
l.insert(substitutionLayerGroup, idx, newSubstitution)
}
func (l *ds1Layers) DeleteSubstitution(idx int) {
l.delete(shadowLayerGroup, idx)
}
func (l *ds1Layers) getLayersGroup(t layerGroupType) (group *layerGroup) {
switch t {
case floorLayerGroup:
group = &l.Floors
case wallLayerGroup:
group = &l.Walls
case shadowLayerGroup:
group = &l.Shadows
case substitutionLayerGroup:
group = &l.Substitutions
default:
return nil
}
return group
}
func getMaxGroupLen(t layerGroupType) (max int) {
switch t {
case floorLayerGroup:
max = maxFloorLayers
case wallLayerGroup:
max = maxWallLayers
case shadowLayerGroup:
max = maxShadowLayers
case substitutionLayerGroup:
max = maxSubstitutionLayers
default:
return 0
}
return max
}

View File

@ -6,8 +6,6 @@ import (
func Test_ds1Layers_DeleteFloor(t *testing.T) {}
func Test_ds1Layers_DeleteOrientation(t *testing.T) {}
func Test_ds1Layers_DeleteShadow(t *testing.T) {}
func Test_ds1Layers_DeleteSubstitution(t *testing.T) {}
@ -16,28 +14,79 @@ func Test_ds1Layers_DeleteWall(t *testing.T) {}
func Test_ds1Layers_GetFloor(t *testing.T) {}
func Test_ds1Layers_GetOrientation(t *testing.T) {}
func Test_ds1Layers_GetShadow(t *testing.T) {}
func Test_ds1Layers_GetSubstitution(t *testing.T) {}
func Test_ds1Layers_GetWall(t *testing.T) {}
func Test_ds1Layers_InsertFloor(t *testing.T) {}
func Test_ds1Layers_Insert(t *testing.T) {
t.Run("Floors", func(t *testing.T) {
ds1LayersInsert(t, floorLayerGroup)
})
t.Run("Walls", func(t *testing.T) {
ds1LayersInsert(t, wallLayerGroup)
})
t.Run("Shadows", func(t *testing.T) {
ds1LayersInsert(t, shadowLayerGroup)
})
t.Run("Substitution", func(t *testing.T) {
ds1LayersInsert(t, substitutionLayerGroup)
})
}
func Test_ds1Layers_InsertOrientation(t *testing.T) {}
func ds1LayersInsert(t *testing.T, lt layerGroupType) {
ds1 := DS1{}
func Test_ds1Layers_InsertShadow(t *testing.T) {}
layers := make([]*layer, getMaxGroupLen(lt)+1)
func Test_ds1Layers_InsertSubstitution(t *testing.T) {}
for i := range layers {
i := i
layers[i] = &layer{}
layers[i].tiles = make(tileGrid, 1)
layers[i].tiles[0] = make(tileRow, 1)
layers[i].SetSize(3, 3)
layers[i].tiles[0][0].Prop1 = byte(i)
}
func Test_ds1Layers_InsertWall(t *testing.T) {}
ds1.ds1Layers = &ds1Layers{}
var insert func(i int)
group := ds1.getLayersGroup(lt)
switch lt {
case floorLayerGroup:
insert = func(i int) { ds1.InsertFloor(0, layers[i]) }
case wallLayerGroup:
insert = func(i int) { ds1.InsertWall(0, layers[i]) }
case shadowLayerGroup:
insert = func(i int) { ds1.InsertShadow(0, layers[i]) }
case substitutionLayerGroup:
insert = func(i int) { ds1.InsertSubstitution(0, layers[i]) }
default:
t.Fatal("unknown layer type given")
}
for i := range layers {
insert(i)
}
if len(*group) != getMaxGroupLen(lt) {
t.Fatal("unexpected floor len after setting")
}
idx := 0
for i := len(layers) - 2; i > 0; i-- {
if (*group)[idx].tiles[0][0].Prop1 != byte(i) {
t.Fatal("unexpected tile inserted")
}
idx++
}
}
func Test_ds1Layers_PopFloor(t *testing.T) {}
func Test_ds1Layers_PopOrientation(t *testing.T) {}
func Test_ds1Layers_PopShadow(t *testing.T) {}
func Test_ds1Layers_PopSubstitution(t *testing.T) {}
@ -53,10 +102,6 @@ func Test_ds1Layers_Push(t *testing.T) {
ds1layerTest(wallLayerGroup, t)
})
t.Run("Orientation", func(t *testing.T) {
ds1layerTest(orientationLayerGroup, t)
})
t.Run("Shadow", func(t *testing.T) {
ds1layerTest(shadowLayerGroup, t)
})
@ -106,11 +151,6 @@ func ds1layerTest(lt layerGroupType, t *testing.T) { //nolint:funlen // no biggi
get = layers.GetWall
max = maxWallLayers
group = &layers.Walls
case orientationLayerGroup:
push = func() { layers.PushOrientation(&layer{}) }
get = layers.GetOrientation
max = maxOrientationLayers
group = &layers.Orientations
case shadowLayerGroup:
push = func() { layers.PushShadow(&layer{}) }
get = layers.GetShadow

View File

@ -68,7 +68,6 @@ type Tile struct {
func (mr *Stamp) Tile(x, y int) *Tile {
t := &Tile{
Walls: make([]d2ds1.Tile, len(mr.ds1.Walls)),
Orientations: make([]d2ds1.Tile, len(mr.ds1.Orientations)),
Floors: make([]d2ds1.Tile, len(mr.ds1.Floors)),
Shadows: make([]d2ds1.Tile, len(mr.ds1.Shadows)),
Substitutions: make([]d2ds1.Tile, len(mr.ds1.Substitutions)),
@ -78,10 +77,6 @@ func (mr *Stamp) Tile(x, y int) *Tile {
t.Walls[idx] = *mr.ds1.Walls[idx].Tile(x, y)
}
for idx := range mr.ds1.Orientations {
t.Orientations[idx] = *mr.ds1.Orientations[idx].Tile(x, y)
}
for idx := range mr.ds1.Floors {
t.Floors[idx] = *mr.ds1.Floors[idx].Tile(x, y)
}
@ -147,7 +142,6 @@ func (mr *Stamp) Entities(tileOffsetX, tileOffsetY int) []d2interface.MapEntity
// nolint:gomnd // constant
entity, err := mr.entity.NewObject((tileOffsetX*5)+object.X,
(tileOffsetY*5)+object.Y, objectRecord, d2resource.PaletteUnits)
if err != nil {
panic(err)
}

73
et
View File

@ -1,73 +0,0 @@
diff --git a/d2common/d2fileformats/d2ds1/ds1_test.go b/d2common/d2fileformats/d2ds1/ds1_test.go
index 23035a50..d17527f0 100644
--- a/d2common/d2fileformats/d2ds1/ds1_test.go
+++ b/d2common/d2fileformats/d2ds1/ds1_test.go
@@ -575,6 +575,24 @@ func TestDS1_SetNumberOfFloors(t *testing.T) {
if err := testIfRestorable(ds1, test); err != nil {
t.Errorf("unable to restore: %v", err)
}
+
+ newNumber = 10
+
+ ds1.SetNumberOfFloorLayers(newNumber)
+
+ test = func(ds1 *DS1) {
+ if len(ds1.tiles[0][0].Floors) != int(newNumber) {
+ t.Fatal("unexpected floors length set")
+ }
+
+ if ds1.numberOfFloorLayers != newNumber {
+ t.Fatal("unexpected floors length set")
+ }
+ }
+
+ if err := testIfRestorable(ds1, test); err != nil {
+ t.Errorf("unable to restore: %v", err)
+ }
}

func TestDS1_NumberOfShadowLayers(t *testing.T) {
diff --git a/d2common/d2fileformats/d2ds1/layers.go b/d2common/d2fileformats/d2ds1/layers.go
index f69ef7f1..b0488a34 100644
--- a/d2common/d2fileformats/d2ds1/layers.go
+++ b/d2common/d2fileformats/d2ds1/layers.go
@@ -1 +1,39 @@
package d2ds1
+
+const (
+ maxWalls = 4
+)
+
+type WallLayer [][]*Wall
+type FloorLayer [][]*Floor
+type ShadowLayer [][]*Shadow
+
+type layers struct {
+ Walls []WallLayer
+ Floors []FloorLayer
+ Shadows []ShadowLayer
+}
+
+func (l *layers) PushWallLayer() {
+
+}
+
+func (l *layers) PopWallLayer() WallLayer {
+
+}
+
+func (l *layers) PushFloorLayer() {
+
+}
+
+func (l *layers) PopFloorLayer() FloorLayer {
+
+}
+
+func (l *layers) PushShadowLayer() {
+
+}
+
+func (l *layers) PopShadowLayer() ShadowLayer {
+
+}