From 2cfacf8ee54e1a74d9656b9ddf39232645bc6078 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Wed, 31 Mar 2021 13:50:40 +0200 Subject: [PATCH] ds1: exported layer type --- d2common/d2fileformats/d2ds1/ds1.go | 10 ++-- d2common/d2fileformats/d2ds1/ds1_layers.go | 46 +++++++++---------- .../d2fileformats/d2ds1/ds1_layers_test.go | 34 +++++++------- d2common/d2fileformats/d2ds1/ds1_test.go | 2 +- d2common/d2fileformats/d2ds1/layer.go | 24 +++++----- d2common/d2fileformats/d2ds1/layer_test.go | 2 +- 6 files changed, 60 insertions(+), 58 deletions(-) diff --git a/d2common/d2fileformats/d2ds1/ds1.go b/d2common/d2fileformats/d2ds1/ds1.go index c2bcdf0a..5d809c9e 100644 --- a/d2common/d2fileformats/d2ds1/ds1.go +++ b/d2common/d2fileformats/d2ds1/ds1.go @@ -111,7 +111,7 @@ func (ds1 *DS1) loadHeader(br *d2datautils.StreamReader) error { switch ds1.SubstitutionType { case subType1, subType2: - ds1.PushSubstitution(&layer{}) + ds1.PushSubstitution(&Layer{}) } } @@ -158,19 +158,19 @@ func (ds1 *DS1) loadBody(stream *d2datautils.StreamReader) error { } for ; numWalls > 0; numWalls-- { - ds1.PushWall(&layer{}) + ds1.PushWall(&Layer{}) } for ; numShadows > 0; numShadows-- { - ds1.PushShadow(&layer{}) + ds1.PushShadow(&Layer{}) } for ; numFloors > 0; numFloors-- { - ds1.PushFloor(&layer{}) + ds1.PushFloor(&Layer{}) } for ; numSubstitutions > 0; numSubstitutions-- { - ds1.PushSubstitution(&layer{}) + ds1.PushSubstitution(&Layer{}) } ds1.SetSize(ds1.width, ds1.height) diff --git a/d2common/d2fileformats/d2ds1/ds1_layers.go b/d2common/d2fileformats/d2ds1/ds1_layers.go index b1ea93ee..0a8b7aab 100644 --- a/d2common/d2fileformats/d2ds1/ds1_layers.go +++ b/d2common/d2fileformats/d2ds1/ds1_layers.go @@ -16,7 +16,7 @@ const ( substitutionLayerGroup ) -type layerGroup []*layer +type layerGroup []*Layer type ds1Layers struct { width, height int @@ -123,7 +123,7 @@ func (l *ds1Layers) SetHeight(h int) { } // generic push func for all layer types -func (l *ds1Layers) push(t layerGroupType, layer *layer) { +func (l *ds1Layers) push(t layerGroupType, layer *Layer) { l.ensureInit() l.cull() @@ -137,7 +137,7 @@ func (l *ds1Layers) push(t layerGroupType, layer *layer) { } // generic pop func for all layer types -func (l *ds1Layers) pop(t layerGroupType) *layer { +func (l *ds1Layers) pop(t layerGroupType) *Layer { l.ensureInit() l.cull() @@ -146,7 +146,7 @@ func (l *ds1Layers) pop(t layerGroupType) *layer { return nil } - var theLayer *layer + var theLayer *Layer // remove last layer of slice and return it if len(*group) > 0 { @@ -160,7 +160,7 @@ func (l *ds1Layers) pop(t layerGroupType) *layer { return nil } -func (l *ds1Layers) get(t layerGroupType, idx int) *layer { +func (l *ds1Layers) get(t layerGroupType, idx int) *Layer { l.ensureInit() l.cull() @@ -176,7 +176,7 @@ func (l *ds1Layers) get(t layerGroupType, idx int) *layer { return (*group)[idx] } -func (l *ds1Layers) insert(t layerGroupType, idx int, newLayer *layer) { +func (l *ds1Layers) insert(t layerGroupType, idx int, newLayer *Layer) { l.ensureInit() l.cull() @@ -207,7 +207,7 @@ func (l *ds1Layers) insert(t layerGroupType, idx int, newLayer *layer) { // idx=1 // newLayer=c // existing layerGroup is [a, b] - newGroup := append((*group)[:idx], append([]*layer{newLayer}, (*group)[idx:]...)...) + newGroup := append((*group)[:idx], append([]*Layer{newLayer}, (*group)[idx:]...)...) *group = newGroup } @@ -229,20 +229,20 @@ func (l *ds1Layers) delete(t layerGroupType, idx int) { l.cull() } -func (l *ds1Layers) GetFloor(idx int) *layer { +func (l *ds1Layers) GetFloor(idx int) *Layer { return l.get(floorLayerGroup, idx) } -func (l *ds1Layers) PushFloor(floor *layer) *ds1Layers { +func (l *ds1Layers) PushFloor(floor *Layer) *ds1Layers { l.push(floorLayerGroup, floor) return l } -func (l *ds1Layers) PopFloor() *layer { +func (l *ds1Layers) PopFloor() *Layer { return l.pop(floorLayerGroup) } -func (l *ds1Layers) InsertFloor(idx int, newFloor *layer) { +func (l *ds1Layers) InsertFloor(idx int, newFloor *Layer) { l.insert(floorLayerGroup, idx, newFloor) } @@ -250,20 +250,20 @@ func (l *ds1Layers) DeleteFloor(idx int) { l.delete(floorLayerGroup, idx) } -func (l *ds1Layers) GetWall(idx int) *layer { +func (l *ds1Layers) GetWall(idx int) *Layer { return l.get(wallLayerGroup, idx) } -func (l *ds1Layers) PushWall(wall *layer) *ds1Layers { +func (l *ds1Layers) PushWall(wall *Layer) *ds1Layers { l.push(wallLayerGroup, wall) return l } -func (l *ds1Layers) PopWall() *layer { +func (l *ds1Layers) PopWall() *Layer { return l.pop(wallLayerGroup) } -func (l *ds1Layers) InsertWall(idx int, newWall *layer) { +func (l *ds1Layers) InsertWall(idx int, newWall *Layer) { l.insert(wallLayerGroup, idx, newWall) } @@ -271,20 +271,20 @@ func (l *ds1Layers) DeleteWall(idx int) { l.delete(wallLayerGroup, idx) } -func (l *ds1Layers) GetShadow(idx int) *layer { +func (l *ds1Layers) GetShadow(idx int) *Layer { return l.get(shadowLayerGroup, idx) } -func (l *ds1Layers) PushShadow(shadow *layer) *ds1Layers { +func (l *ds1Layers) PushShadow(shadow *Layer) *ds1Layers { l.push(shadowLayerGroup, shadow) return l } -func (l *ds1Layers) PopShadow() *layer { +func (l *ds1Layers) PopShadow() *Layer { return l.pop(shadowLayerGroup) } -func (l *ds1Layers) InsertShadow(idx int, newShadow *layer) { +func (l *ds1Layers) InsertShadow(idx int, newShadow *Layer) { l.insert(shadowLayerGroup, idx, newShadow) } @@ -292,20 +292,20 @@ func (l *ds1Layers) DeleteShadow(idx int) { l.delete(shadowLayerGroup, idx) } -func (l *ds1Layers) GetSubstitution(idx int) *layer { +func (l *ds1Layers) GetSubstitution(idx int) *Layer { return l.get(substitutionLayerGroup, idx) } -func (l *ds1Layers) PushSubstitution(sub *layer) *ds1Layers { +func (l *ds1Layers) PushSubstitution(sub *Layer) *ds1Layers { l.push(substitutionLayerGroup, sub) return l } -func (l *ds1Layers) PopSubstitution() *layer { +func (l *ds1Layers) PopSubstitution() *Layer { return l.pop(substitutionLayerGroup) } -func (l *ds1Layers) InsertSubstitution(idx int, newSubstitution *layer) { +func (l *ds1Layers) InsertSubstitution(idx int, newSubstitution *Layer) { l.insert(substitutionLayerGroup, idx, newSubstitution) } diff --git a/d2common/d2fileformats/d2ds1/ds1_layers_test.go b/d2common/d2fileformats/d2ds1/ds1_layers_test.go index 62a139da..47bfe127 100644 --- a/d2common/d2fileformats/d2ds1/ds1_layers_test.go +++ b/d2common/d2fileformats/d2ds1/ds1_layers_test.go @@ -74,17 +74,17 @@ func Test_ds1Layers_Get(t *testing.T) { func ds1LayersGet(t *testing.T, lt layerGroupType) { ds1 := exampleData() - var get func(i int) *layer + var get func(i int) *Layer switch lt { case floorLayerGroup: - get = func(i int) *layer { return ds1.GetFloor(0) } + get = func(i int) *Layer { return ds1.GetFloor(0) } case wallLayerGroup: - get = func(i int) *layer { return ds1.GetWall(0) } + get = func(i int) *Layer { return ds1.GetWall(0) } case shadowLayerGroup: - get = func(i int) *layer { return ds1.GetShadow(0) } + get = func(i int) *Layer { return ds1.GetShadow(0) } case substitutionLayerGroup: - get = func(i int) *layer { return ds1.GetSubstitution(0) } + get = func(i int) *Layer { return ds1.GetSubstitution(0) } default: t.Fatal("unknown layer type given") return @@ -116,11 +116,11 @@ func Test_ds1Layers_Insert(t *testing.T) { 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 - layers[i] = &layer{} + layers[i] = &Layer{} layers[i].tiles = make(tileGrid, 1) layers[i].tiles[0] = make(tileRow, 1) layers[i].SetSize(3, 3) @@ -184,14 +184,14 @@ func Test_ds1Layers_Pop(t *testing.T) { func ds1layerPop(lt layerGroupType, t *testing.T) { ds1 := exampleData() - var pop func() *layer + var pop func() *Layer var numBefore, numAfter int switch lt { case floorLayerGroup: numBefore = len(ds1.Floors) - pop = func() *layer { + pop = func() *Layer { l := ds1.PopFloor() numAfter = len(ds1.Floors) @@ -199,7 +199,7 @@ func ds1layerPop(lt layerGroupType, t *testing.T) { } case wallLayerGroup: numBefore = len(ds1.Walls) - pop = func() *layer { + pop = func() *Layer { l := ds1.PopWall() numAfter = len(ds1.Walls) @@ -207,7 +207,7 @@ func ds1layerPop(lt layerGroupType, t *testing.T) { } case shadowLayerGroup: numBefore = len(ds1.Shadows) - pop = func() *layer { + pop = func() *Layer { l := ds1.PopShadow() numAfter = len(ds1.Shadows) @@ -215,7 +215,7 @@ func ds1layerPop(lt layerGroupType, t *testing.T) { } case substitutionLayerGroup: numBefore = len(ds1.Substitutions) - pop = func() *layer { + pop = func() *Layer { l := ds1.PopSubstitution() numAfter = len(ds1.Substitutions) @@ -271,7 +271,7 @@ func ds1layerPush(lt layerGroupType, t *testing.T) { //nolint:funlen // no biggi // we need to set up some shit to handle the test in a generic way var push func() - var get func(idx int) *layer + var get func(idx int) *Layer var max int @@ -292,22 +292,22 @@ func ds1layerPush(lt layerGroupType, t *testing.T) { //nolint:funlen // no biggi switch lt { case floorLayerGroup: - push = func() { layers.PushFloor(&layer{}) } + push = func() { layers.PushFloor(&Layer{}) } get = layers.GetFloor max = maxFloorLayers group = &layers.Floors case wallLayerGroup: - push = func() { layers.PushWall(&layer{}) } + push = func() { layers.PushWall(&Layer{}) } get = layers.GetWall max = maxWallLayers group = &layers.Walls case shadowLayerGroup: - push = func() { layers.PushShadow(&layer{}) } + push = func() { layers.PushShadow(&Layer{}) } get = layers.GetShadow max = maxShadowLayers group = &layers.Shadows case substitutionLayerGroup: - push = func() { layers.PushSubstitution(&layer{}) } + push = func() { layers.PushSubstitution(&Layer{}) } get = layers.GetSubstitution max = maxSubstitutionLayers group = &layers.Substitutions diff --git a/d2common/d2fileformats/d2ds1/ds1_test.go b/d2common/d2fileformats/d2ds1/ds1_test.go index 2ba80a0a..27738e49 100644 --- a/d2common/d2fileformats/d2ds1/ds1_test.go +++ b/d2common/d2fileformats/d2ds1/ds1_test.go @@ -174,7 +174,7 @@ func exampleData() *DS1 { //nolint:funlen // not a big deal if this is long func {0, 2, 0, 0, 0, nil}, {0, 3, 0, 0, 0, nil}, }, - substitutionGroups: nil, + SubstitutionGroups: nil, version: 17, Act: 1, SubstitutionType: 0, diff --git a/d2common/d2fileformats/d2ds1/layer.go b/d2common/d2fileformats/d2ds1/layer.go index d77b86fd..aacc180c 100644 --- a/d2common/d2fileformats/d2ds1/layer.go +++ b/d2common/d2fileformats/d2ds1/layer.go @@ -19,14 +19,16 @@ const ( layerStreamSubstitute1 ) -type tileRow []Tile // index is x coordinate -type tileGrid []tileRow // index is y coordinate +type ( + tileRow []Tile // index is x coordinate + tileGrid []tileRow // index is y coordinate +) -type layer struct { +type Layer struct { tiles tileGrid } -func (l *layer) Tile(x, y int) *Tile { +func (l *Layer) Tile(x, y int) *Tile { if l.Width() < x || l.Height() < y { return nil } @@ -34,7 +36,7 @@ func (l *layer) Tile(x, y int) *Tile { return &l.tiles[y][x] } -func (l *layer) SetTile(x, y int, t *Tile) { +func (l *Layer) SetTile(x, y int, t *Tile) { if l.Width() > x || l.Height() > y { return } @@ -42,7 +44,7 @@ func (l *layer) SetTile(x, y int, t *Tile) { l.tiles[y][x] = *t } -func (l *layer) Width() int { +func (l *Layer) Width() int { if len(l.tiles[0]) < 1 { l.SetWidth(1) } @@ -50,7 +52,7 @@ func (l *layer) Width() int { return len(l.tiles[0]) } -func (l *layer) SetWidth(w int) *layer { +func (l *Layer) SetWidth(w int) *Layer { if w < 1 { w = 1 } @@ -80,7 +82,7 @@ func (l *layer) SetWidth(w int) *layer { return l } -func (l *layer) Height() int { +func (l *Layer) Height() int { if len(l.tiles) < 1 { l.SetHeight(1) } @@ -88,7 +90,7 @@ func (l *layer) Height() int { return len(l.tiles) } -func (l *layer) SetHeight(h int) *layer { +func (l *Layer) SetHeight(h int) *Layer { if h < 1 { h = 1 } @@ -120,10 +122,10 @@ func (l *layer) SetHeight(h int) *layer { return l } -func (l *layer) Size() (w, h int) { +func (l *Layer) Size() (w, h int) { return l.Width(), l.Height() } -func (l *layer) SetSize(w, h int) *layer { +func (l *Layer) SetSize(w, h int) *Layer { return l.SetWidth(w).SetHeight(h) } diff --git a/d2common/d2fileformats/d2ds1/layer_test.go b/d2common/d2fileformats/d2ds1/layer_test.go index 92c37ea6..055c2056 100644 --- a/d2common/d2fileformats/d2ds1/layer_test.go +++ b/d2common/d2fileformats/d2ds1/layer_test.go @@ -7,7 +7,7 @@ func Test_layers(t *testing.T) { fmtWidthHeightError = "unexpected wall layer width/height: %dx%d" ) - l := &layer{} + l := &Layer{} l.SetSize(0, 0)