Skip to content

Commit

Permalink
More linting, better random
Browse files Browse the repository at this point in the history
  • Loading branch information
bobtfish committed Aug 29, 2020
1 parent 1df4d9a commit 9a7ba08
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 107 deletions.
37 changes: 37 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion character/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package character
import (
"fmt"
"image/color"
"math/rand"

"gopkg.in/yaml.v2"

Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions character/spells.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"io"
"math/rand"
"time"

"github.com/faiface/pixel/pixelgl"
Expand Down Expand Up @@ -82,6 +81,5 @@ func run() {
}

func main() {
rand.Seed(int64(time.Now().Nanosecond()))
pixelgl.Run(run)
}
2 changes: 1 addition & 1 deletion player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
97 changes: 49 additions & 48 deletions player/spell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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
},
})
Expand All @@ -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
},
})
Expand Down Expand Up @@ -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
},
}) */
38 changes: 38 additions & 0 deletions rand/main.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion screen/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 18 additions & 20 deletions screen/castspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions screen/grow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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++
}
}
}
Loading

0 comments on commit 9a7ba08

Please sign in to comment.