From 9a7ba085acc924efd9222ad05287715813984b88 Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Sat, 29 Aug 2020 16:34:20 +0100 Subject: [PATCH] More linting, better random --- .golangci.yml | 37 ++++++++++++++++ character/character.go | 2 +- character/spells.go | 3 +- main.go | 2 - player/player.go | 2 +- player/spell.go | 97 +++++++++++++++++++++--------------------- rand/main.go | 38 +++++++++++++++++ screen/attack.go | 2 +- screen/castspell.go | 38 ++++++++--------- screen/grow.go | 14 +++--- screen/move.go | 28 +++++------- screen/players.go | 5 +-- screen/utils.go | 2 +- screen/with_board.go | 4 +- spells/main.go | 2 +- 15 files changed, 169 insertions(+), 107 deletions(-) create mode 100644 .golangci.yml create mode 100644 rand/main.go diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..7230ca6 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,37 @@ +--- +linters: + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - dupl + - errcheck + - exhaustive + - goconst + - gocritic + - gocyclo + - gofmt + - goimports + - golint + - goprintffuncname + - gosec + - gosimple + - govet + - ineffassign + - interfacer + - misspell + - nakedret + - noctx + - nolintlint + - rowserrcheck + - scopelint + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace diff --git a/character/character.go b/character/character.go index a4526d7..5709be1 100644 --- a/character/character.go +++ b/character/character.go @@ -3,7 +3,6 @@ package character import ( "fmt" "image/color" - "math/rand" "gopkg.in/yaml.v2" @@ -14,6 +13,7 @@ import ( "github.com/bobtfish/mayhem/player" "github.com/bobtfish/mayhem/render" "github.com/bobtfish/mayhem/spells" + "github.com/bobtfish/mayhem/rand" ) // Abstract character that can be created diff --git a/character/spells.go b/character/spells.go index 067cd68..94745ba 100644 --- a/character/spells.go +++ b/character/spells.go @@ -1,8 +1,7 @@ package character import ( - "math/rand" - + "github.com/bobtfish/mayhem/rand" "github.com/bobtfish/mayhem/fx" "github.com/bobtfish/mayhem/grid" "github.com/bobtfish/mayhem/logical" diff --git a/main.go b/main.go index cc9eedf..d6cea48 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io" - "math/rand" "time" "github.com/faiface/pixel/pixelgl" @@ -82,6 +81,5 @@ func run() { } func main() { - rand.Seed(int64(time.Now().Nanosecond())) pixelgl.Run(run) } diff --git a/player/player.go b/player/player.go index 2aab3da..6edd2b2 100644 --- a/player/player.go +++ b/player/player.go @@ -3,8 +3,8 @@ package player import ( "fmt" "image/color" - "math/rand" + "github.com/bobtfish/mayhem/rand" "github.com/bobtfish/mayhem/fx" "github.com/bobtfish/mayhem/logical" "github.com/bobtfish/mayhem/spells" diff --git a/player/spell.go b/player/spell.go index 6bdff41..5daf6a2 100644 --- a/player/spell.go +++ b/player/spell.go @@ -44,7 +44,7 @@ func init() { p.CharacterIcon = logical.V(1, 20) p.IsAnimated = false p.SpriteIdx = 0 - p.Defence = p.Defence + 4 + p.Defence += 4 }, }) spells.CreateSpell(PlayerSpell{ @@ -57,7 +57,7 @@ func init() { p.CharacterIcon = logical.V(0, 20) p.IsAnimated = false p.SpriteIdx = 0 - p.Defence = p.Defence + 2 + p.Defence += 2 }, }) spells.CreateSpell(PlayerSpell{ @@ -69,7 +69,7 @@ func init() { MutateFunc: func(p *Player) { p.CharacterIcon = logical.V(4, 22) p.IsAnimated = true - p.Combat = p.Combat + 2 + p.Combat += 2 p.HasMagicWeapon = true }, }) @@ -82,7 +82,7 @@ func init() { MutateFunc: func(p *Player) { p.CharacterIcon = logical.V(0, 21) p.IsAnimated = true - p.Combat = p.Combat + 4 + p.Combat += 4 p.HasMagicWeapon = true }, }) @@ -123,48 +123,49 @@ func init() { p.Movement = 6 }, }) - /*spells.CreateSpell(PlayerSpell{ - ASpell: spells.ASpell{ - Name: "Law-1", - CastingChance: 100, - LawRating: 2, - NoCastFx: true, - }, - MutateFunc: func(p *Player) { - p.LawRating++ - }, - }) - spells.CreateSpell(PlayerSpell{ - ASpell: spells.ASpell{ - Name: "Law-2", - CastingChance: 60, - LawRating: 4, - NoCastFx: true, - }, - MutateFunc: func(p *Player) { - p.LawRating = p.LawRating + 2 - }, - }) - spells.CreateSpell(PlayerSpell{ - ASpell: spells.ASpell{ - Name: "Chaos-1", - CastingChance: 80, - LawRating: -2, - NoCastFx: true, - }, - MutateFunc: func(p *Player) { - p.LawRating-- - }, - }) - spells.CreateSpell(PlayerSpell{ - ASpell: spells.ASpell{ - Name: "Chaos-2", - CastingChance: 60, - LawRating: -4, - NoCastFx: true, - }, - MutateFunc: func(p *Player) { - p.LawRating = p.LawRating - 2 - }, - }) */ } + +/*spells.CreateSpell(PlayerSpell{ + ASpell: spells.ASpell{ + Name: "Law-1", + CastingChance: 100, + LawRating: 2, + NoCastFx: true, + }, + MutateFunc: func(p *Player) { + p.LawRating++ + }, +}) +spells.CreateSpell(PlayerSpell{ + ASpell: spells.ASpell{ + Name: "Law-2", + CastingChance: 60, + LawRating: 4, + NoCastFx: true, + }, + MutateFunc: func(p *Player) { + p.LawRating = p.LawRating + 2 + }, +}) +spells.CreateSpell(PlayerSpell{ + ASpell: spells.ASpell{ + Name: "Chaos-1", + CastingChance: 80, + LawRating: -2, + NoCastFx: true, + }, + MutateFunc: func(p *Player) { + p.LawRating-- + }, +}) +spells.CreateSpell(PlayerSpell{ + ASpell: spells.ASpell{ + Name: "Chaos-2", + CastingChance: 60, + LawRating: -4, + NoCastFx: true, + }, + MutateFunc: func(p *Player) { + p.LawRating = p.LawRating - 2 + }, +}) */ diff --git a/rand/main.go b/rand/main.go new file mode 100644 index 0000000..6e95d12 --- /dev/null +++ b/rand/main.go @@ -0,0 +1,38 @@ +package rand + +import ( + crand "crypto/rand" + "encoding/binary" + "log" + mrand "math/rand" +) + +var rnd *mrand.Rand + +func init() { + rnd = mrand.New(cryptoSource{}) +} + +func Intn(i int) int { + return rnd.Intn(i) +} + +func Shuffle(n int, swap func(int, int)) { + rnd.Shuffle(n, swap) +} + +type cryptoSource struct{} + +func (s cryptoSource) Seed(seed int64) {} + +func (s cryptoSource) Int63() int64 { + return int64(s.Uint64() & ^uint64(1<<63)) +} + +func (s cryptoSource) Uint64() (v uint64) { + err := binary.Read(crand.Reader, binary.BigEndian, &v) + if err != nil { + log.Fatal(err) + } + return v +} diff --git a/screen/attack.go b/screen/attack.go index 34e8690..48c9d4c 100644 --- a/screen/attack.go +++ b/screen/attack.go @@ -2,11 +2,11 @@ package screen import ( "fmt" - "math/rand" "github.com/faiface/pixel" "github.com/faiface/pixel/pixelgl" + "github.com/bobtfish/mayhem/rand" "github.com/bobtfish/mayhem/character" "github.com/bobtfish/mayhem/grid" "github.com/bobtfish/mayhem/logical" diff --git a/screen/castspell.go b/screen/castspell.go index e343fa9..4a86843 100644 --- a/screen/castspell.go +++ b/screen/castspell.go @@ -95,29 +95,27 @@ func (screen *TargetSpellScreen) Step(ss pixel.Picture, win *pixelgl.Window) Gam target := screen.WithBoard.CursorPosition fmt.Printf("Cast spell %s (%d) on V(%d, %d)\n", spell.GetName(), spell.GetCastRange(), target.X, target.Y) return screen.AnimateAndCast() - } else { - if screen.WithBoard.MoveCursor(win) || !screen.MessageShown { - screen.MessageShown = false - screen.WithBoard.DrawCursor(ss, batch) - } - if win.JustPressed(pixelgl.KeyS) { - target := screen.WithBoard.CursorPosition - if spell.GetCastRange() < target.Distance(screen.Players[screen.PlayerIdx].BoardPosition) { - textBottom("Out of range", ss, batch) - fmt.Printf("Out of range! Spell cast range %d but distance to target is %d\n", spell.GetCastRange(), target.Distance(screen.Players[screen.PlayerIdx].BoardPosition)) + } + if screen.WithBoard.MoveCursor(win) || !screen.MessageShown { + screen.MessageShown = false + screen.WithBoard.DrawCursor(ss, batch) + } + if win.JustPressed(pixelgl.KeyS) { + target := screen.WithBoard.CursorPosition + if spell.GetCastRange() < target.Distance(screen.Players[screen.PlayerIdx].BoardPosition) { + textBottom("Out of range", ss, batch) + fmt.Printf("Out of range! Spell cast range %d but distance to target is %d\n", spell.GetCastRange(), target.Distance(screen.Players[screen.PlayerIdx].BoardPosition)) + screen.MessageShown = true + } else { + if !HaveLineOfSight(screen.Players[screen.PlayerIdx].BoardPosition, screen.WithBoard.CursorPosition, screen.WithBoard.Grid) { + textBottom("No line of sight", ss, batch) screen.MessageShown = true } else { - if !HaveLineOfSight(screen.Players[screen.PlayerIdx].BoardPosition, screen.WithBoard.CursorPosition, screen.WithBoard.Grid) { - textBottom("No line of sight", ss, batch) - screen.MessageShown = true - } else { - if spell.CanCast(screen.WithBoard.Grid.GetGameObject(target)) { - fmt.Printf("Cast spell %s (%d) on V(%d, %d)\n", spell.GetName(), spell.GetCastRange(), target.X, target.Y) - return screen.AnimateAndCast() - } else { - fmt.Printf("Cannot cast on non-empty square\n") - } + if spell.CanCast(screen.WithBoard.Grid.GetGameObject(target)) { + fmt.Printf("Cast spell %s (%d) on V(%d, %d)\n", spell.GetName(), spell.GetCastRange(), target.X, target.Y) + return screen.AnimateAndCast() } + fmt.Printf("Cannot cast on non-empty square\n") } } } diff --git a/screen/grow.go b/screen/grow.go index 92a6591..bac4b96 100644 --- a/screen/grow.go +++ b/screen/grow.go @@ -2,11 +2,11 @@ package screen import ( "fmt" - "math/rand" "github.com/faiface/pixel" "github.com/faiface/pixel/pixelgl" + "github.com/bobtfish/mayhem/rand" "github.com/bobtfish/mayhem/character" "github.com/bobtfish/mayhem/fx" "github.com/bobtfish/mayhem/logical" @@ -128,11 +128,9 @@ func (screen *GrowScreen) IterateGrowVanish() { } screen.WithBoard.Grid.PlaceGameObject(adj[adjIdx], c) screen.Grew = true - } else { - if doesItVanish() { - screen.WithBoard.Grid.GetGameObjectStack(screen.Consider).RemoveTopObject() - screen.Grew = true - } + } else if doesItVanish() { + screen.WithBoard.Grid.GetGameObjectStack(screen.Consider).RemoveTopObject() + screen.Grew = true } // Don't bother to check if we're another character type, we already matched break @@ -154,10 +152,10 @@ func (screen *GrowScreen) IterateGrowVanish() { } // Bump tile counter - screen.Consider.X = screen.Consider.X + 1 + screen.Consider.X++ if screen.Consider.X == screen.WithBoard.Grid.MaxX() { screen.Consider.X = 0 - screen.Consider.Y = screen.Consider.Y + 1 + screen.Consider.Y++ } } } diff --git a/screen/move.go b/screen/move.go index 7ba7df6..63f76a1 100644 --- a/screen/move.go +++ b/screen/move.go @@ -81,9 +81,8 @@ func (screen *MoveFindCharacterScreen) Step(ss pixel.Picture, win *pixelgl.Windo if _, movedAlready := screen.MovedCharacters[ob]; movedAlready { fmt.Printf("Not movable (has moved already)\n") return screen - } else { - screen.MovedCharacters[ob] = true } + screen.MovedCharacters[ob] = true // Are we a mount with a wizard mounted char, isChar := ob.(*character.Character) @@ -113,10 +112,8 @@ func (screen *MoveFindCharacterScreen) Step(ss pixel.Picture, win *pixelgl.Windo Character: ob, MovedCharacters: screen.MovedCharacters, } - } else { - fmt.Printf("Broke engagement, can move normally\n") } - + fmt.Printf("Broke engagement, can move normally\n") } // Not engaged, so move @@ -258,7 +255,6 @@ func (screen *MoveGroundCharacterScreen) Step(ss pixel.Picture, win *pixelgl.Win textBottom("Undead - Cannot be attacked", ss, batch) } if ms.DidMove { - screen.WithBoard.CursorPosition = newLocation if ms.MountMove { @@ -388,15 +384,14 @@ func DoAttackMaybe(from, to logical.Vec, playerIdx int, withBoard *WithBoard, mo Fx: fx, }, } - } else { - // Does belong to this player, see if it's mountable, if so we can move to it - _, isPlayer := withBoard.Grid.GetGameObject(from).(*player.Player) - if isPlayer { // We're moving the player, lets see if target is something we can mount - fmt.Printf("Moving player, check for mount\n") - if ob.IsMount() { - fmt.Printf(" Is mount\n") - return AttackStatus{NotEmpty: true, IsMount: true} - } + } + // Does belong to this player, see if it's mountable, if so we can move to it + _, isPlayer := withBoard.Grid.GetGameObject(from).(*player.Player) + if isPlayer { // We're moving the player, lets see if target is something we can mount + fmt.Printf("Moving player, check for mount\n") + if ob.IsMount() { + fmt.Printf(" Is mount\n") + return AttackStatus{NotEmpty: true, IsMount: true} } } } @@ -468,7 +463,7 @@ func (screen *MoveFlyingCharacterScreen) Enter(ss pixel.Picture, win *pixelgl.Wi ClearScreen(ss, win) screen.WithBoard.CursorSprite = CURSOR_FLY fmt.Printf("Enter move flying character screen for player %d\n", screen.PlayerIdx+1) - screen.DisplayRange = true // Set this to start to supress cursor till we move it + screen.DisplayRange = true // Set this to start to suppress cursor till we move it } type MoveStatus struct { @@ -519,7 +514,6 @@ func (screen *MoveFlyingCharacterScreen) Step(ss pixel.Picture, win *pixelgl.Win return screen.MoveFlyingCharacterScreenFinished() } } - } // Allow you to cancel movement, but that character then counts as moved diff --git a/screen/players.go b/screen/players.go index 4eeee14..0253e6e 100644 --- a/screen/players.go +++ b/screen/players.go @@ -36,7 +36,7 @@ func (screen *PlayerNameScreen) Enter(ss pixel.Picture, win *pixelgl.Window) { } func (screen *PlayerNameScreen) Step(ss pixel.Picture, win *pixelgl.Window) GameScreen { - if win.JustPressed(pixelgl.KeyEnter) && len(screen.CurrentPlayer.Name) >= 0 { + if win.JustPressed(pixelgl.KeyEnter) && len(screen.CurrentPlayer.Name) > 0 { return &PlayerAIScreen{PlayersScreen: screen.PlayersScreen} } if win.JustPressed(pixelgl.KeyBackspace) && len(screen.CurrentPlayer.Name) > 0 { @@ -153,9 +153,8 @@ func (screen *PlayerColorScreen) Step(ss pixel.Picture, win *pixelgl.Window) Gam return &StartMainGame{ Players: screen.Players, } - } else { - return &PlayerNameScreen{PlayersScreen: screen.PlayersScreen} } + return &PlayerNameScreen{PlayersScreen: screen.PlayersScreen} } return screen } diff --git a/screen/utils.go b/screen/utils.go index f1798b3..2aa798d 100644 --- a/screen/utils.go +++ b/screen/utils.go @@ -146,7 +146,7 @@ func intToChar(i int) string { return fmt.Sprint('A' + i) } -func drawMainBorder(win *pixelgl.Window, sd render.SpriteDrawer) { +func drawMainBorder(win pixel.Target, sd render.SpriteDrawer) { batch := sd.GetNewBatch() color := render.GetColor(0, 0, 255) // Bottom left diff --git a/screen/with_board.go b/screen/with_board.go index 922507a..86667dc 100644 --- a/screen/with_board.go +++ b/screen/with_board.go @@ -54,7 +54,7 @@ func (screen *WithBoard) MoveCursor(win *pixelgl.Window) bool { return false } -func (screen *WithBoard) DrawCursor(ss pixel.Picture, batch *pixel.Batch) { +func (screen *WithBoard) DrawCursor(ss pixel.Picture, batch pixel.Target) { sd := render.NewSpriteDrawer(ss).WithOffset(render.GameBoardV()) objectAtCursor := screen.Grid.GetGameObject(screen.CursorPosition) cursorColor := render.GetColor(0, 255, 255) @@ -68,7 +68,7 @@ func (screen *WithBoard) DrawCursor(ss pixel.Picture, batch *pixel.Batch) { } } -func (screen *WithBoard) MoveAndDrawCursor(ss pixel.Picture, win *pixelgl.Window, batch *pixel.Batch) { +func (screen *WithBoard) MoveAndDrawCursor(ss pixel.Picture, win *pixelgl.Window, batch pixel.Target) { screen.MoveCursor(win) screen.DrawCursor(ss, batch) } diff --git a/spells/main.go b/spells/main.go index 367fca7..c7eaf8d 100644 --- a/spells/main.go +++ b/spells/main.go @@ -3,8 +3,8 @@ package spells import ( "fmt" "image/color" - "math/rand" + "github.com/bobtfish/mayhem/rand" "github.com/bobtfish/mayhem/fx" "github.com/bobtfish/mayhem/grid" "github.com/bobtfish/mayhem/logical"