1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-12-25 19:46:50 -05:00

Merge pull request #1117 from gucio321/d2ds1-fixes

d2ds1: bugfixes
This commit is contained in:
gravestench 2021-04-07 09:42:47 -07:00 committed by GitHub
commit 7ce4583fe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 36 deletions

View File

@ -15,9 +15,25 @@ const (
FloorLayerGroup LayerGroupType = iota
WallLayerGroup
ShadowLayerGroup
Substitutionlayergroup
SubstitutionLayerGroup
)
func (l LayerGroupType) String() string {
switch l {
case FloorLayerGroup:
return "floor"
case WallLayerGroup:
return "wall"
case ShadowLayerGroup:
return "shadow"
case SubstitutionLayerGroup:
return "substitution"
}
// should not be reached
return "unknown"
}
type layerGroup []*Layer
type ds1Layers struct {
@ -51,12 +67,12 @@ func (l *ds1Layers) cull() {
l.cullNilLayers(FloorLayerGroup)
l.cullNilLayers(WallLayerGroup)
l.cullNilLayers(ShadowLayerGroup)
l.cullNilLayers(Substitutionlayergroup)
l.cullNilLayers(SubstitutionLayerGroup)
}
// removes nil layers of given layer group type
func (l *ds1Layers) cullNilLayers(t LayerGroupType) {
group := l.getLayersGroup(t)
group := l.GetLayersGroup(t)
if group == nil {
return
}
@ -65,9 +81,9 @@ func (l *ds1Layers) cullNilLayers(t LayerGroupType) {
// exit culling procedure when no nil layers are found in entire group.
culling:
for {
for idx := len(*group) - 1; idx > 0; idx-- {
for idx := len(*group) - 1; idx >= 0; idx-- {
if (*group)[idx] == nil {
*group = (*group)[:idx]
*group = append((*group)[:idx], (*group)[idx+1:]...)
continue culling
}
}
@ -89,14 +105,14 @@ func (l *ds1Layers) SetSize(w, h int) {
l.enforceSize(FloorLayerGroup)
l.enforceSize(WallLayerGroup)
l.enforceSize(ShadowLayerGroup)
l.enforceSize(Substitutionlayergroup)
l.enforceSize(SubstitutionLayerGroup)
}
func (l *ds1Layers) enforceSize(t LayerGroupType) {
l.ensureInit()
l.cull()
group := l.getLayersGroup(t)
group := l.GetLayersGroup(t)
if group == nil {
return
}
@ -130,9 +146,9 @@ func (l *ds1Layers) push(t LayerGroupType, layer *Layer) {
l.cull()
layer.SetSize(l.Size())
group := l.getLayersGroup(t)
group := l.GetLayersGroup(t)
max := getMaxGroupLen(t)
max := GetMaxGroupLen(t)
if len(*group) < max {
*group = append(*group, layer)
@ -144,7 +160,7 @@ func (l *ds1Layers) pop(t LayerGroupType) *Layer {
l.ensureInit()
l.cull()
group := l.getLayersGroup(t)
group := l.GetLayersGroup(t)
if group == nil {
return nil
}
@ -167,7 +183,7 @@ func (l *ds1Layers) get(t LayerGroupType, idx int) *Layer {
l.ensureInit()
l.cull()
group := l.getLayersGroup(t)
group := l.GetLayersGroup(t)
if group == nil {
return nil
}
@ -189,12 +205,12 @@ func (l *ds1Layers) insert(t LayerGroupType, idx int, newLayer *Layer) {
newLayer.SetSize(l.Size())
group := l.getLayersGroup(t)
group := l.GetLayersGroup(t)
if group == nil {
return
}
if len(*group)+1 > getMaxGroupLen(t) {
if len(*group)+1 > GetMaxGroupLen(t) {
return
}
@ -220,7 +236,7 @@ func (l *ds1Layers) delete(t LayerGroupType, idx int) {
l.ensureInit()
l.cull()
group := l.getLayersGroup(t)
group := l.GetLayersGroup(t)
if group == nil {
return
}
@ -298,27 +314,28 @@ func (l *ds1Layers) DeleteShadow(idx int) {
}
func (l *ds1Layers) GetSubstitution(idx int) *Layer {
return l.get(Substitutionlayergroup, idx)
return l.get(SubstitutionLayerGroup, idx)
}
func (l *ds1Layers) PushSubstitution(sub *Layer) *ds1Layers {
l.push(Substitutionlayergroup, sub)
l.push(SubstitutionLayerGroup, sub)
return l
}
func (l *ds1Layers) PopSubstitution() *Layer {
return l.pop(Substitutionlayergroup)
return l.pop(SubstitutionLayerGroup)
}
func (l *ds1Layers) InsertSubstitution(idx int, newSubstitution *Layer) {
l.insert(Substitutionlayergroup, 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) {
// GetLayersGroup returns layer group depending on type given
func (l *ds1Layers) GetLayersGroup(t LayerGroupType) (group *layerGroup) {
switch t {
case FloorLayerGroup:
group = &l.Floors
@ -326,7 +343,7 @@ func (l *ds1Layers) getLayersGroup(t LayerGroupType) (group *layerGroup) {
group = &l.Walls
case ShadowLayerGroup:
group = &l.Shadows
case Substitutionlayergroup:
case SubstitutionLayerGroup:
group = &l.Substitutions
default:
return nil
@ -335,7 +352,8 @@ func (l *ds1Layers) getLayersGroup(t LayerGroupType) (group *layerGroup) {
return group
}
func getMaxGroupLen(t LayerGroupType) (max int) {
// GetMaxGroupLen returns maximum length of layer group of type given
func GetMaxGroupLen(t LayerGroupType) (max int) {
switch t {
case FloorLayerGroup:
max = maxFloorLayers
@ -343,7 +361,7 @@ func getMaxGroupLen(t LayerGroupType) (max int) {
max = maxWallLayers
case ShadowLayerGroup:
max = maxShadowLayers
case Substitutionlayergroup:
case SubstitutionLayerGroup:
max = maxSubstitutionLayers
default:
return 0

View File

@ -15,7 +15,7 @@ func Test_ds1Layers_Delete(t *testing.T) {
ds1LayersDelete(t, ShadowLayerGroup)
})
t.Run("Substitution", func(t *testing.T) {
ds1LayersDelete(t, Substitutionlayergroup)
ds1LayersDelete(t, SubstitutionLayerGroup)
})
}
@ -42,7 +42,7 @@ func ds1LayersDelete(t *testing.T, lt LayerGroupType) {
del = func(i int) { ds1.DeleteWall(0) }
case ShadowLayerGroup:
del = func(i int) { ds1.DeleteShadow(0) }
case Substitutionlayergroup:
case SubstitutionLayerGroup:
del = func(i int) { ds1.DeleteSubstitution(0) }
default:
t.Fatal("unknown layer type given")
@ -67,7 +67,7 @@ func Test_ds1Layers_Get(t *testing.T) {
ds1LayersGet(t, ShadowLayerGroup)
})
t.Run("Substitution", func(t *testing.T) {
ds1LayersGet(t, Substitutionlayergroup)
ds1LayersGet(t, SubstitutionLayerGroup)
})
}
@ -83,7 +83,7 @@ func ds1LayersGet(t *testing.T, lt LayerGroupType) {
get = func(i int) *Layer { return ds1.GetWall(0) }
case ShadowLayerGroup:
get = func(i int) *Layer { return ds1.GetShadow(0) }
case Substitutionlayergroup:
case SubstitutionLayerGroup:
get = func(i int) *Layer { return ds1.GetSubstitution(0) }
default:
t.Fatal("unknown layer type given")
@ -93,7 +93,7 @@ func ds1LayersGet(t *testing.T, lt LayerGroupType) {
layer := get(0)
// example has nil substitution layer, maybe we need another test
if layer == nil && lt != Substitutionlayergroup {
if layer == nil && lt != SubstitutionLayerGroup {
t.Errorf("layer expected")
}
}
@ -109,14 +109,14 @@ func Test_ds1Layers_Insert(t *testing.T) {
ds1LayersInsert(t, ShadowLayerGroup)
})
t.Run("Substitution", func(t *testing.T) {
ds1LayersInsert(t, Substitutionlayergroup)
ds1LayersInsert(t, SubstitutionLayerGroup)
})
}
func ds1LayersInsert(t *testing.T, lt LayerGroupType) {
ds1 := DS1{}
layers := make([]*Layer, getMaxGroupLen(lt)+1)
layers := make([]*Layer, GetMaxGroupLen(lt)+1)
for i := range layers {
i := i
@ -131,7 +131,7 @@ func ds1LayersInsert(t *testing.T, lt LayerGroupType) {
var insert func(i int)
group := ds1.getLayersGroup(lt)
group := ds1.GetLayersGroup(lt)
switch lt {
case FloorLayerGroup:
@ -140,7 +140,7 @@ func ds1LayersInsert(t *testing.T, lt LayerGroupType) {
insert = func(i int) { ds1.InsertWall(0, layers[i]) }
case ShadowLayerGroup:
insert = func(i int) { ds1.InsertShadow(0, layers[i]) }
case Substitutionlayergroup:
case SubstitutionLayerGroup:
insert = func(i int) { ds1.InsertSubstitution(0, layers[i]) }
default:
t.Fatal("unknown layer type given")
@ -150,7 +150,7 @@ func ds1LayersInsert(t *testing.T, lt LayerGroupType) {
insert(i)
}
if len(*group) != getMaxGroupLen(lt) {
if len(*group) != GetMaxGroupLen(lt) {
t.Fatal("unexpected floor len after setting")
}
@ -177,7 +177,7 @@ func Test_ds1Layers_Pop(t *testing.T) {
})
t.Run("Substitution", func(t *testing.T) {
ds1layerPop(Substitutionlayergroup, t)
ds1layerPop(SubstitutionLayerGroup, t)
})
}
@ -213,7 +213,7 @@ func ds1layerPop(lt LayerGroupType, t *testing.T) {
return l
}
case Substitutionlayergroup:
case SubstitutionLayerGroup:
numBefore = len(ds1.Substitutions)
pop = func() *Layer {
l := ds1.PopSubstitution()
@ -257,7 +257,7 @@ func Test_ds1Layers_Push(t *testing.T) {
})
t.Run("Substitution", func(t *testing.T) {
ds1layerPush(Substitutionlayergroup, t)
ds1layerPush(SubstitutionLayerGroup, t)
})
}
@ -306,7 +306,7 @@ func ds1layerPush(lt LayerGroupType, t *testing.T) { //nolint:funlen // no biggi
get = layers.GetShadow
max = maxShadowLayers
group = &layers.Shadows
case Substitutionlayergroup:
case SubstitutionLayerGroup:
push = func() { layers.PushSubstitution(&Layer{}) }
get = layers.GetSubstitution
max = maxSubstitutionLayers