diff --git a/d2common/d2fileformats/d2ds1/ds1.go b/d2common/d2fileformats/d2ds1/ds1.go index fd9ecf40..5d809c9e 100644 --- a/d2common/d2fileformats/d2ds1/ds1.go +++ b/d2common/d2fileformats/d2ds1/ds1.go @@ -30,7 +30,7 @@ type DS1 struct { *ds1Layers Files []string // FilePtr table of file string pointers Objects []Object // Objects - substitutionGroups []SubstitutionGroup // Substitution groups for the DS1 + SubstitutionGroups []SubstitutionGroup // Substitution groups for the DS1 version ds1version Act int32 // Act, from 1 to 5. This tells which Act table to use for the Objects list @@ -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) @@ -287,7 +287,7 @@ func (ds1 *DS1) loadSubstitutions(br *d2datautils.StreamReader) error { (ds1.SubstitutionType == subType1 || ds1.SubstitutionType == subType2) if !hasSubstitutions { - ds1.substitutionGroups = make([]SubstitutionGroup, 0) + ds1.SubstitutionGroups = make([]SubstitutionGroup, 0) return nil } @@ -303,7 +303,7 @@ func (ds1 *DS1) loadSubstitutions(br *d2datautils.StreamReader) error { return fmt.Errorf("reading number of sub groups: %w", err) } - ds1.substitutionGroups = make([]SubstitutionGroup, numberOfSubGroups) + ds1.SubstitutionGroups = make([]SubstitutionGroup, numberOfSubGroups) for subIdx := 0; subIdx < int(numberOfSubGroups); subIdx++ { newSub := SubstitutionGroup{} @@ -333,7 +333,7 @@ func (ds1 *DS1) loadSubstitutions(br *d2datautils.StreamReader) error { return fmt.Errorf("reading substitution's %d unknown: %v", subIdx, err) } - ds1.substitutionGroups[subIdx] = newSub + ds1.SubstitutionGroups[subIdx] = newSub } return err @@ -599,9 +599,9 @@ func (ds1 *DS1) Marshal() []byte { if hasSubstitutions { sw.PushUint32(ds1.unknown2) - sw.PushUint32(uint32(len(ds1.substitutionGroups))) + sw.PushUint32(uint32(len(ds1.SubstitutionGroups))) - for _, i := range ds1.substitutionGroups { + for _, i := range ds1.SubstitutionGroups { sw.PushInt32(i.TileX) sw.PushInt32(i.TileY) sw.PushInt32(i.WidthInTiles) diff --git a/d2common/d2fileformats/d2ds1/ds1_layers.go b/d2common/d2fileformats/d2ds1/ds1_layers.go index b1ea93ee..65636c95 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,9 +123,10 @@ 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() + layer.SetSize(l.Size()) group := l.getLayersGroup(t) @@ -137,7 +138,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 +147,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 +161,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 +177,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() @@ -184,6 +185,8 @@ func (l *ds1Layers) insert(t layerGroupType, idx int, newLayer *layer) { return } + newLayer.SetSize(l.Size()) + group := l.getLayersGroup(t) if group == nil { return @@ -207,7 +210,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 +232,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 +253,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 +274,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 +295,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..64edbe9a 100644 --- a/d2common/d2fileformats/d2ds1/layer.go +++ b/d2common/d2fileformats/d2ds1/layer.go @@ -19,14 +19,18 @@ 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 { +// Layer is an abstraction of a tile grid with some helper methods +type Layer struct { tiles tileGrid } -func (l *layer) Tile(x, y int) *Tile { +// Tile returns the tile at the given x,y coordinate in the grid, or nil if empty. +func (l *Layer) Tile(x, y int) *Tile { if l.Width() < x || l.Height() < y { return nil } @@ -34,7 +38,8 @@ func (l *layer) Tile(x, y int) *Tile { return &l.tiles[y][x] } -func (l *layer) SetTile(x, y int, t *Tile) { +// SetTile sets the tile at the given x,y coordinate in the tile grid +func (l *Layer) SetTile(x, y int, t *Tile) { if l.Width() > x || l.Height() > y { return } @@ -42,7 +47,8 @@ func (l *layer) SetTile(x, y int, t *Tile) { l.tiles[y][x] = *t } -func (l *layer) Width() int { +// Width returns the width of the tile grid +func (l *Layer) Width() int { if len(l.tiles[0]) < 1 { l.SetWidth(1) } @@ -50,7 +56,8 @@ func (l *layer) Width() int { return len(l.tiles[0]) } -func (l *layer) SetWidth(w int) *layer { +// SetWidth sets the width of the tile grid, minimum of 1 +func (l *Layer) SetWidth(w int) *Layer { if w < 1 { w = 1 } @@ -80,7 +87,8 @@ func (l *layer) SetWidth(w int) *layer { return l } -func (l *layer) Height() int { +// Height returns the height of the tile grid +func (l *Layer) Height() int { if len(l.tiles) < 1 { l.SetHeight(1) } @@ -88,7 +96,8 @@ func (l *layer) Height() int { return len(l.tiles) } -func (l *layer) SetHeight(h int) *layer { +// SetHeight sets the height of the tile grid, minimum of 1 +func (l *Layer) SetHeight(h int) *Layer { if h < 1 { h = 1 } @@ -120,10 +129,12 @@ func (l *layer) SetHeight(h int) *layer { return l } -func (l *layer) Size() (w, h int) { +// Size returns the width and height of the tile grid +func (l *Layer) Size() (w, h int) { return l.Width(), l.Height() } -func (l *layer) SetSize(w, h int) *layer { +// SetSize sets the width and height of the tile grid +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)