lint and minor refactor of d2common (#690)

- moved contents of `d2common/math.go` into `d2math/math.go`
- removed lint errors from files in d2common
This commit is contained in:
lord 2020-08-04 21:03:33 -07:00 committed by GitHub
parent 319e1f0245
commit 8e41133f39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 136 additions and 140 deletions

View File

@ -6,6 +6,7 @@ import (
"container/ring"
"errors"
"fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"image"
"image/gif"
"image/png"
@ -547,7 +548,7 @@ func (a *App) convertFramesToGif() error {
framesPal[j] = framePal.(*image.Paletted)
frameDelays[j] = 5
}
}(i, d2common.MinInt(i+framesPerCPU, framesTotal))
}(i, d2math.MinInt(i+framesPerCPU, framesTotal))
}
waitGroup.Wait()

View File

@ -5,4 +5,5 @@ package d2common
// source, for instance a missile might have a movement speed of lvl*2
type CalcString string
// todo: the logic for parsing these should exist here
// Issue #689
// info about calcstrings can be found here: https://d2mods.info/forum/kb/viewarticle?a=371

View File

@ -1,6 +1,7 @@
package d2dcc
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
@ -57,10 +58,10 @@ func CreateDCCDirection(bm *d2common.BitMuncher,
// Load the frame headers
for frameIdx := 0; frameIdx < file.FramesPerDirection; frameIdx++ {
result.Frames[frameIdx] = CreateDCCDirectionFrame(bm, result)
minx = int(d2common.MinInt32(int32(result.Frames[frameIdx].Box.Left), int32(minx)))
miny = int(d2common.MinInt32(int32(result.Frames[frameIdx].Box.Top), int32(miny)))
maxx = int(d2common.MaxInt32(int32(result.Frames[frameIdx].Box.Right()), int32(maxx)))
maxy = int(d2common.MaxInt32(int32(result.Frames[frameIdx].Box.Bottom()), int32(maxy)))
minx = int(d2math.MinInt32(int32(result.Frames[frameIdx].Box.Left), int32(minx)))
miny = int(d2math.MinInt32(int32(result.Frames[frameIdx].Box.Top), int32(miny)))
maxx = int(d2math.MaxInt32(int32(result.Frames[frameIdx].Box.Right()), int32(maxx)))
maxy = int(d2math.MaxInt32(int32(result.Frames[frameIdx].Box.Bottom()), int32(maxy)))
}
result.Box = d2common.Rectangle{Left: minx, Top: miny, Width: maxx - minx, Height: maxy - miny}

View File

@ -4,6 +4,7 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
)
@ -42,7 +43,7 @@ func LoadDS1(fileData []byte) (*DS1, error) {
ds1.Height = br.GetInt32() + 1
if ds1.Version >= 8 { //nolint:gomnd // Version number
ds1.Act = d2common.MinInt32(maxActNumber, br.GetInt32()+1)
ds1.Act = d2math.MinInt32(maxActNumber, br.GetInt32()+1)
}
if ds1.Version >= 10 { //nolint:gomnd // Version number

View File

@ -6,12 +6,12 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"log"
"strings"
"github.com/JoshVarga/blast"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2compression"
)
@ -120,7 +120,7 @@ func (v *Stream) readInternalSingleUnit(buffer []byte, offset, count uint32) uin
v.loadSingleUnit()
}
bytesToCopy := d2common.Min(uint32(len(v.CurrentData))-v.CurrentPosition, count)
bytesToCopy := d2math.Min(uint32(len(v.CurrentData))-v.CurrentPosition, count)
copy(buffer[offset:offset+bytesToCopy], v.CurrentData[v.CurrentPosition:v.CurrentPosition+bytesToCopy])
@ -133,7 +133,7 @@ func (v *Stream) readInternal(buffer []byte, offset, count uint32) uint32 {
v.bufferData()
localPosition := v.CurrentPosition % v.BlockSize
bytesToCopy := d2common.MinInt32(int32(len(v.CurrentData))-int32(localPosition), int32(count))
bytesToCopy := d2math.MinInt32(int32(len(v.CurrentData))-int32(localPosition), int32(count))
if bytesToCopy <= 0 {
return 0
@ -153,7 +153,7 @@ func (v *Stream) bufferData() {
return
}
expectedLength := d2common.Min(v.BlockTableEntry.UncompressedFileSize-(requiredBlock*v.BlockSize), v.BlockSize)
expectedLength := d2math.Min(v.BlockTableEntry.UncompressedFileSize-(requiredBlock*v.BlockSize), v.BlockSize)
v.CurrentData = v.loadBlock(requiredBlock, expectedLength)
v.CurrentBlockIndex = requiredBlock
}

View File

@ -1,5 +1,7 @@
package d2math
import "math"
const (
// Epsilon is used as the threshold for 'almost equal' operations.
Epsilon float64 = 0.0001
@ -88,3 +90,80 @@ func WrapInt(x, max int) int {
return wrapped
}
// MinInt returns the minimum of the given values
func MinInt(a, b int) int {
if a < b {
return a
}
return b
}
// MaxInt returns the maximum of the given values
func MaxInt(a, b int) int {
if a > b {
return a
}
return b
}
// Min returns the lower of two values
func Min(a, b uint32) uint32 {
if a < b {
return a
}
return b
}
// Max returns the higher of two values
func Max(a, b uint32) uint32 {
if a > b {
return a
}
return b
}
// MaxInt32 returns the higher of two values
func MaxInt32(a, b int32) int32 {
if a > b {
return a
}
return b
}
// AbsInt32 returns the absolute of the given int32
func AbsInt32(a int32) int32 {
if a < 0 {
return -a
}
return a
}
// MinInt32 returns the higher of two values
func MinInt32(a, b int32) int32 {
if a < b {
return a
}
return b
}
// BytesToInt32 converts 4 bytes to int32
// IsoToScreen converts isometric coordinates to screenspace coordinates
// ScreenToIso converts screenspace coordinates to isometric coordinates
// GetRadiansBetween returns the radians between two points. 0rad is facing to the right.
func GetRadiansBetween(p1X, p1Y, p2X, p2Y float64) float64 {
deltaY := p2Y - p1Y
deltaX := p2X - p1X
return math.Atan2(deltaY, deltaX)
}

View File

@ -1,87 +0,0 @@
package d2common
import (
"math"
)
// MinInt returns the minimum of the given values
func MinInt(a, b int) int {
if a < b {
return a
}
return b
}
// MaxInt returns the maximum of the given values
func MaxInt(a, b int) int {
if a > b {
return a
}
return b
}
// Min returns the lower of two values
func Min(a, b uint32) uint32 {
if a < b {
return a
}
return b
}
// Max returns the higher of two values
func Max(a, b uint32) uint32 {
if a > b {
return a
}
return b
}
// MaxInt32 returns the higher of two values
func MaxInt32(a, b int32) int32 {
if a > b {
return a
}
return b
}
// AbsInt32 returns the absolute of the given int32
func AbsInt32(a int32) int32 {
if a < 0 {
return -a
}
return a
}
// MinInt32 returns the higher of two values
func MinInt32(a, b int32) int32 {
if a < b {
return a
}
return b
}
// BytesToInt32 converts 4 bytes to int32
// IsoToScreen converts isometric coordinates to screenspace coordinates
// ScreenToIso converts screenspace coordinates to isometric coordinates
// GetRadiansBetween returns the radians between two points. 0rad is facing to the right.
func GetRadiansBetween(p1X, p1Y, p2X, p2Y float64) float64 {
deltaY := p2Y - p1Y
deltaX := p2X - p1X
return math.Atan2(deltaY, deltaX)
}
// AlmostEqual returns true if two values are within threshold from each other
func AlmostEqual(a, b, threshold float64) bool {
return math.Abs(a-b) <= threshold
}

View File

@ -14,7 +14,7 @@ type textDictionaryHashEntry struct {
NameLength uint16
}
var lookupTable map[string]string
var lookupTable map[string]string //nolint:gochecknoglobals // currently global by design
const (
crcByteCount = 2

View File

@ -2,6 +2,7 @@ package d2asset
import (
"errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"image"
"image/color"
"math"
@ -10,7 +11,6 @@ import (
d2iface "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc"
)
@ -207,8 +207,8 @@ func (a *animation) GetFrameBounds() (maxWidth, maxHeight int) {
direction := a.directions[a.directionIndex]
for _, frame := range direction.frames {
maxWidth = d2common.MaxInt(maxWidth, frame.width)
maxHeight = d2common.MaxInt(maxHeight, frame.height)
maxWidth = d2math.MaxInt(maxWidth, frame.width)
maxHeight = d2math.MaxInt(maxHeight, frame.height)
}
return maxWidth, maxHeight

View File

@ -2,11 +2,11 @@ package d2asset
import (
"errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"math"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc"
d2iface "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
)
@ -90,10 +90,10 @@ func (a *DCCAnimation) decodeDirection(directionIndex int) error {
maxX, maxY := math.MinInt32, math.MinInt32
for _, dccFrame := range direction.Frames {
minX = d2common.MinInt(minX, dccFrame.Box.Left)
minY = d2common.MinInt(minY, dccFrame.Box.Top)
maxX = d2common.MaxInt(maxX, dccFrame.Box.Right())
maxY = d2common.MaxInt(maxY, dccFrame.Box.Bottom())
minX = d2math.MinInt(minX, dccFrame.Box.Left)
minY = d2math.MinInt(minY, dccFrame.Box.Top)
maxX = d2math.MaxInt(maxX, dccFrame.Box.Right())
maxY = d2math.MaxInt(maxY, dccFrame.Box.Bottom())
}
frameWidth := maxX - minX

View File

@ -3,10 +3,10 @@ package d2asset
import (
"encoding/binary"
"errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"image/color"
"strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
)
@ -80,17 +80,17 @@ func (f *Font) GetTextMetrics(text string) (width, height int) {
for _, c := range text {
if c == '\n' {
totalWidth = d2common.MaxInt(totalWidth, lineWidth)
totalWidth = d2math.MaxInt(totalWidth, lineWidth)
totalHeight += lineHeight
lineWidth = 0
lineHeight = 0
} else if glyph, ok := f.glyphs[c]; ok {
lineWidth += glyph.width
lineHeight = d2common.MaxInt(lineHeight, glyph.height)
lineHeight = d2math.MaxInt(lineHeight, glyph.height)
}
}
totalWidth = d2common.MaxInt(totalWidth, lineWidth)
totalWidth = d2math.MaxInt(totalWidth, lineWidth)
totalHeight += lineHeight
return totalWidth, totalHeight
@ -122,7 +122,7 @@ func (f *Font) RenderText(text string, target d2interface.Surface) error {
return err
}
lineHeight = d2common.MaxInt(lineHeight, glyph.height)
lineHeight = d2math.MaxInt(lineHeight, glyph.height)
lineLength++
target.PushTranslation(glyph.width, 0)

View File

@ -2,11 +2,11 @@ package d2gui
import (
"errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"image/color"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
)
@ -40,7 +40,7 @@ func renderSegmented(animation d2interface.Animation, segmentsX, segmentsY, fram
}
width, height := animation.GetCurrentFrameSize()
maxHeight = d2common.MaxInt(maxHeight, height)
maxHeight = d2math.MaxInt(maxHeight, height)
currentX += width
}

View File

@ -3,6 +3,7 @@ package d2gui
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
)
type layoutEntry struct {
@ -258,14 +259,14 @@ func (l *Layout) getContentSize() (width, height int) {
switch l.positionType {
case PositionTypeVertical:
width = d2common.MaxInt(width, w)
width = d2math.MaxInt(width, w)
height += h
case PositionTypeHorizontal:
width += w
height = d2common.MaxInt(height, h)
height = d2math.MaxInt(height, h)
case PositionTypeAbsolute:
width = d2common.MaxInt(width, x+w)
height = d2common.MaxInt(height, y+h)
width = d2math.MaxInt(width, x+w)
height = d2math.MaxInt(height, y+h)
}
}
@ -274,7 +275,7 @@ func (l *Layout) getContentSize() (width, height int) {
func (l *Layout) getSize() (width, height int) {
width, height = l.getContentSize()
return d2common.MaxInt(width, l.width), d2common.MaxInt(height, l.height)
return d2math.MaxInt(width, l.width), d2math.MaxInt(height, l.height)
}
func (l *Layout) onMouseButtonDown(event d2interface.MouseEvent) bool {
@ -348,8 +349,8 @@ func (l *Layout) AdjustEntryPlacement() {
expanderWidth = (width - contentWidth) / expanderCount
}
expanderWidth = d2common.MaxInt(0, expanderWidth)
expanderHeight = d2common.MaxInt(0, expanderHeight)
expanderWidth = d2math.MaxInt(0, expanderWidth)
expanderHeight = d2math.MaxInt(0, expanderHeight)
}
var offsetX, offsetY int

View File

@ -1,9 +1,9 @@
package d2maprenderer
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2ds1"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dt1"
@ -77,11 +77,11 @@ func (mr *MapRenderer) generateFloorCache(tile *d2ds1.FloorShadowRecord) {
tileYMinimum := int32(0)
for _, block := range tileData[i].Blocks {
tileYMinimum = d2common.MinInt32(tileYMinimum, int32(block.Y))
tileYMinimum = d2math.MinInt32(tileYMinimum, int32(block.Y))
}
tileYOffset := d2common.AbsInt32(tileYMinimum)
tileHeight := d2common.AbsInt32(tileData[i].Height)
tileYOffset := d2math.AbsInt32(tileYMinimum)
tileHeight := d2math.AbsInt32(tileData[i].Height)
image, _ := mr.renderer.NewSurface(int(tileData[i].Width), int(tileHeight), d2enum.FilterNearest)
indexData := make([]byte, tileData[i].Width*tileHeight)
d2dt1.DecodeTileGfxData(tileData[i].Blocks, &indexData, tileYOffset, tileData[i].Width)
@ -111,8 +111,8 @@ func (mr *MapRenderer) generateShadowCache(tile *d2ds1.FloorShadowRecord) {
tileMaxY := int32(0)
for _, block := range tileData.Blocks {
tileMinY = d2common.MinInt32(tileMinY, int32(block.Y))
tileMaxY = d2common.MaxInt32(tileMaxY, int32(block.Y+32))
tileMinY = d2math.MinInt32(tileMinY, int32(block.Y))
tileMaxY = d2math.MaxInt32(tileMaxY, int32(block.Y+32))
}
tileYOffset := -tileMinY
@ -160,11 +160,11 @@ func (mr *MapRenderer) generateWallCache(tile *d2ds1.WallRecord) {
}
for _, block := range target.Blocks {
tileMinY = d2common.MinInt32(tileMinY, int32(block.Y))
tileMaxY = d2common.MaxInt32(tileMaxY, int32(block.Y+32))
tileMinY = d2math.MinInt32(tileMinY, int32(block.Y))
tileMaxY = d2math.MaxInt32(tileMaxY, int32(block.Y+32))
}
realHeight := d2common.MaxInt32(d2common.AbsInt32(tileData.Height), tileMaxY-tileMinY)
realHeight := d2math.MaxInt32(d2math.AbsInt32(tileData.Height), tileMaxY-tileMinY)
tileYOffset := -tileMinY
if tile.Type == 15 {

View File

@ -3,6 +3,7 @@ package d2term
import (
"errors"
"fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"image/color"
"log"
"math"
@ -11,7 +12,6 @@ import (
"strconv"
"strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
)
@ -114,9 +114,9 @@ func (t *terminal) OnKeyDown(event d2interface.KeyEvent) bool {
case d2enum.KeyEnd:
t.outputIndex = 0
case d2enum.KeyHome:
t.outputIndex = d2common.MaxInt(0, len(t.outputHistory)-t.lineCount)
t.outputIndex = d2math.MaxInt(0, len(t.outputHistory)-t.lineCount)
case d2enum.KeyPageUp:
maxOutputIndex := d2common.MaxInt(0, len(t.outputHistory)-t.lineCount)
maxOutputIndex := d2math.MaxInt(0, len(t.outputHistory)-t.lineCount)
if t.outputIndex += t.lineCount; t.outputIndex >= maxOutputIndex {
t.outputIndex = maxOutputIndex
}
@ -168,7 +168,7 @@ func (t *terminal) handleControlKey(eventKey d2enum.Key, keyMod d2enum.KeyMod) {
switch eventKey {
case d2enum.KeyUp:
if keyMod == d2enum.KeyModControl {
t.lineCount = d2common.MaxInt(0, t.lineCount-1)
t.lineCount = d2math.MaxInt(0, t.lineCount-1)
} else if len(t.commandHistory) > 0 {
t.command = t.commandHistory[t.commandIndex]
if t.commandIndex == 0 {
@ -179,7 +179,7 @@ func (t *terminal) handleControlKey(eventKey d2enum.Key, keyMod d2enum.KeyMod) {
}
case d2enum.KeyDown:
if keyMod == d2enum.KeyModControl {
t.lineCount = d2common.MinInt(t.lineCount+1, termRowCountMax)
t.lineCount = d2math.MinInt(t.lineCount+1, termRowCountMax)
}
}
}

View File

@ -2,13 +2,12 @@ package d2ui
import (
"errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"image"
"image/color"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
)
// Sprite is a positioned visual object.
@ -67,7 +66,7 @@ func (s *Sprite) RenderSegmented(target d2interface.Surface, segmentsX, segments
}
frameWidth, frameHeight := s.GetCurrentFrameSize()
maxFrameHeight = d2common.MaxInt(maxFrameHeight, frameHeight)
maxFrameHeight = d2math.MaxInt(maxFrameHeight, frameHeight)
currentX += frameWidth
}

View File

@ -2,10 +2,10 @@ package d2client
import (
"fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
"log"
"os"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
@ -240,7 +240,7 @@ func (g *GameClient) handleCastSkillPacket(packet d2netpacket.NetPacket) error {
return err
}
rads := d2common.GetRadiansBetween(
rads := d2math.GetRadiansBetween(
player.Position.X(),
player.Position.Y(),
playerCast.TargetX*numSubtilesPerTile,