Fix all go vet issues
This commit is contained in:
parent
204c786103
commit
b5707ab71c
13 changed files with 195 additions and 170 deletions
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/mdiluz/rove/pkg/version"
|
"github.com/mdiluz/rove/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Status returns the status of the current server to a gRPC request
|
||||||
func (s *Server) Status(context.Context, *rove.StatusRequest) (*rove.StatusResponse, error) {
|
func (s *Server) Status(context.Context, *rove.StatusRequest) (*rove.StatusResponse, error) {
|
||||||
response := &rove.StatusResponse{
|
response := &rove.StatusResponse{
|
||||||
Ready: true,
|
Ready: true,
|
||||||
|
@ -26,6 +27,7 @@ func (s *Server) Status(context.Context, *rove.StatusRequest) (*rove.StatusRespo
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register registers a new account for a gRPC request
|
||||||
func (s *Server) Register(ctx context.Context, req *rove.RegisterRequest) (*rove.RegisterResponse, error) {
|
func (s *Server) Register(ctx context.Context, req *rove.RegisterRequest) (*rove.RegisterResponse, error) {
|
||||||
if len(req.Name) == 0 {
|
if len(req.Name) == 0 {
|
||||||
return nil, fmt.Errorf("empty account name")
|
return nil, fmt.Errorf("empty account name")
|
||||||
|
@ -44,6 +46,7 @@ func (s *Server) Register(ctx context.Context, req *rove.RegisterRequest) (*rove
|
||||||
return &rove.RegisterResponse{}, nil
|
return &rove.RegisterResponse{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rover returns rover information for a gRPC request
|
||||||
func (s *Server) Rover(ctx context.Context, req *rove.RoverRequest) (*rove.RoverResponse, error) {
|
func (s *Server) Rover(ctx context.Context, req *rove.RoverRequest) (*rove.RoverResponse, error) {
|
||||||
response := &rove.RoverResponse{}
|
response := &rove.RoverResponse{}
|
||||||
if len(req.Account) == 0 {
|
if len(req.Account) == 0 {
|
||||||
|
@ -70,6 +73,7 @@ func (s *Server) Rover(ctx context.Context, req *rove.RoverRequest) (*rove.Rover
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Radar returns the radar information for a rover
|
||||||
func (s *Server) Radar(ctx context.Context, req *rove.RadarRequest) (*rove.RadarResponse, error) {
|
func (s *Server) Radar(ctx context.Context, req *rove.RadarRequest) (*rove.RadarResponse, error) {
|
||||||
if len(req.Account) == 0 {
|
if len(req.Account) == 0 {
|
||||||
return nil, fmt.Errorf("empty account name")
|
return nil, fmt.Errorf("empty account name")
|
||||||
|
@ -95,6 +99,7 @@ func (s *Server) Radar(ctx context.Context, req *rove.RadarRequest) (*rove.Radar
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Commands issues commands to the world based on a gRPC request
|
||||||
func (s *Server) Commands(ctx context.Context, req *rove.CommandsRequest) (*rove.CommandsResponse, error) {
|
func (s *Server) Commands(ctx context.Context, req *rove.CommandsRequest) (*rove.CommandsResponse, error) {
|
||||||
if len(req.Account) == 0 {
|
if len(req.Account) == 0 {
|
||||||
return nil, fmt.Errorf("empty account")
|
return nil, fmt.Errorf("empty account")
|
||||||
|
|
|
@ -208,10 +208,12 @@ func (s *Server) LoadWorld() error {
|
||||||
|
|
||||||
// SpawnRoverForAccount spawns the rover rover for an account
|
// SpawnRoverForAccount spawns the rover rover for an account
|
||||||
func (s *Server) SpawnRoverForAccount(account string) (string, error) {
|
func (s *Server) SpawnRoverForAccount(account string) (string, error) {
|
||||||
if inst, err := s.world.SpawnRover(); err != nil {
|
inst, err := s.world.SpawnRover()
|
||||||
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
} else {
|
}
|
||||||
err := s.accountant.AssignData(account, "rover", inst)
|
|
||||||
|
err = s.accountant.AssignData(account, "rover", inst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to assign rover to account, %s", err)
|
log.Printf("Failed to assign rover to account, %s", err)
|
||||||
|
|
||||||
|
@ -221,8 +223,7 @@ func (s *Server) SpawnRoverForAccount(account string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", err
|
return "", err
|
||||||
} else {
|
}
|
||||||
|
|
||||||
return inst, nil
|
return inst, nil
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ var data = os.Getenv("DATA_PATH")
|
||||||
// The tick rate of the server in seconds
|
// The tick rate of the server in seconds
|
||||||
var tick = os.Getenv("TICK_RATE")
|
var tick = os.Getenv("TICK_RATE")
|
||||||
|
|
||||||
|
// InnerMain is our main function so tests can run it
|
||||||
func InnerMain() {
|
func InnerMain() {
|
||||||
// Ensure we've seeded rand
|
// Ensure we've seeded rand
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
|
|
@ -5,8 +5,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const kAccountsFileName = "rove-accounts.json"
|
|
||||||
|
|
||||||
// Account represents a registered user
|
// Account represents a registered user
|
||||||
type Account struct {
|
type Account struct {
|
||||||
// Name simply describes the account and must be unique
|
// Name simply describes the account and must be unique
|
||||||
|
@ -51,7 +49,7 @@ func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssignRover assigns data to an account
|
// AssignData assigns data to an account
|
||||||
func (a *Accountant) AssignData(account string, key string, value string) error {
|
func (a *Accountant) AssignData(account string, key string, value string) error {
|
||||||
|
|
||||||
// Find the account matching the ID
|
// Find the account matching the ID
|
||||||
|
@ -65,12 +63,12 @@ func (a *Accountant) AssignData(account string, key string, value string) error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRover gets the rover rover for the account
|
// GetValue gets the rover rover for the account
|
||||||
func (a *Accountant) GetValue(account string, key string) (string, error) {
|
func (a *Accountant) GetValue(account string, key string) (string, error) {
|
||||||
// Find the account matching the ID
|
// Find the account matching the ID
|
||||||
if this, ok := a.Accounts[account]; !ok {
|
this, ok := a.Accounts[account]
|
||||||
|
if !ok {
|
||||||
return "", fmt.Errorf("no account found for id: %s", account)
|
return "", fmt.Errorf("no account found for id: %s", account)
|
||||||
} else {
|
|
||||||
return this.Data[key], nil
|
|
||||||
}
|
}
|
||||||
|
return this.Data[key], nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,15 +72,15 @@ func (a *Atlas) SetTile(v vector.Vector, tile byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
local := a.worldSpaceToChunkLocal(v)
|
local := a.worldSpaceToChunkLocal(v)
|
||||||
tileId := local.X + local.Y*a.ChunkSize
|
tileID := local.X + local.Y*a.ChunkSize
|
||||||
|
|
||||||
// Sanity check
|
// Sanity check
|
||||||
if tileId >= len(chunk.Tiles) || tileId < 0 {
|
if tileID >= len(chunk.Tiles) || tileID < 0 {
|
||||||
log.Fatalf("Local tileID is not in valid chunk, somehow, this means something is very wrong")
|
log.Fatalf("Local tileID is not in valid chunk, somehow, this means something is very wrong")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the chunk back
|
// Set the chunk back
|
||||||
chunk.Tiles[tileId] = tile
|
chunk.Tiles[tileID] = tile
|
||||||
a.Chunks[c] = chunk
|
a.Chunks[c] = chunk
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,14 +94,14 @@ func (a *Atlas) GetTile(v vector.Vector) byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
local := a.worldSpaceToChunkLocal(v)
|
local := a.worldSpaceToChunkLocal(v)
|
||||||
tileId := local.X + local.Y*a.ChunkSize
|
tileID := local.X + local.Y*a.ChunkSize
|
||||||
|
|
||||||
// Sanity check
|
// Sanity check
|
||||||
if tileId >= len(chunk.Tiles) || tileId < 0 {
|
if tileID >= len(chunk.Tiles) || tileID < 0 {
|
||||||
log.Fatalf("Local tileID is not in valid chunk, somehow, this means something is very wrong")
|
log.Fatalf("Local tileID is not in valid chunk, somehow, this means something is very wrong")
|
||||||
}
|
}
|
||||||
|
|
||||||
return chunk.Tiles[tileId]
|
return chunk.Tiles[tileID]
|
||||||
}
|
}
|
||||||
|
|
||||||
// worldSpaceToChunkLocal gets a chunk local coordinate for a tile
|
// worldSpaceToChunkLocal gets a chunk local coordinate for a tile
|
||||||
|
|
|
@ -11,13 +11,21 @@ import (
|
||||||
type Bearing int
|
type Bearing int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// North describes a 0,1 vector
|
||||||
North Bearing = iota
|
North Bearing = iota
|
||||||
|
// NorthEast describes a 1,1 vector
|
||||||
NorthEast
|
NorthEast
|
||||||
|
// East describes a 1,0 vector
|
||||||
East
|
East
|
||||||
|
// SouthEast describes a 1,-1 vector
|
||||||
SouthEast
|
SouthEast
|
||||||
|
// South describes a 0,-1 vector
|
||||||
South
|
South
|
||||||
|
// SouthWest describes a -1,-1 vector
|
||||||
SouthWest
|
SouthWest
|
||||||
|
// West describes a -1,0 vector
|
||||||
West
|
West
|
||||||
|
// NorthWest describes a -1,1 vector
|
||||||
NorthWest
|
NorthWest
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Moves the rover in the chosen bearing
|
// CommandMove Moves the rover in the chosen bearing
|
||||||
CommandMove = "move"
|
CommandMove = "move"
|
||||||
|
|
||||||
// Will attempt to stash the object at the current location
|
// CommandStash Will attempt to stash the object at the current location
|
||||||
CommandStash = "stash"
|
CommandStash = "stash"
|
||||||
|
|
||||||
// Will attempt to repair the rover with an inventory object
|
// CommandRepair Will attempt to repair the rover with an inventory object
|
||||||
CommandRepair = "repair"
|
CommandRepair = "repair"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -124,25 +124,26 @@ func (w *World) GetRover(rover string) (Rover, error) {
|
||||||
w.worldMutex.RLock()
|
w.worldMutex.RLock()
|
||||||
defer w.worldMutex.RUnlock()
|
defer w.worldMutex.RUnlock()
|
||||||
|
|
||||||
if i, ok := w.Rovers[rover]; ok {
|
i, ok := w.Rovers[rover]
|
||||||
return i, nil
|
if !ok {
|
||||||
} else {
|
|
||||||
return Rover{}, fmt.Errorf("Failed to find rover with name: %s", rover)
|
return Rover{}, fmt.Errorf("Failed to find rover with name: %s", rover)
|
||||||
}
|
}
|
||||||
|
return i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes an rover from the game
|
// DestroyRover Removes an rover from the game
|
||||||
func (w *World) DestroyRover(rover string) error {
|
func (w *World) DestroyRover(rover string) error {
|
||||||
w.worldMutex.Lock()
|
w.worldMutex.Lock()
|
||||||
defer w.worldMutex.Unlock()
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
if i, ok := w.Rovers[rover]; ok {
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
// Clear the tile
|
// Clear the tile
|
||||||
w.Atlas.SetTile(i.Pos, objects.Empty)
|
w.Atlas.SetTile(i.Pos, objects.Empty)
|
||||||
delete(w.Rovers, rover)
|
delete(w.Rovers, rover)
|
||||||
} else {
|
|
||||||
return fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,11 +152,11 @@ func (w *World) RoverPosition(rover string) (vector.Vector, error) {
|
||||||
w.worldMutex.RLock()
|
w.worldMutex.RLock()
|
||||||
defer w.worldMutex.RUnlock()
|
defer w.worldMutex.RUnlock()
|
||||||
|
|
||||||
if i, ok := w.Rovers[rover]; ok {
|
i, ok := w.Rovers[rover]
|
||||||
return i.Pos, nil
|
if !ok {
|
||||||
} else {
|
|
||||||
return vector.Vector{}, fmt.Errorf("no rover matching id")
|
return vector.Vector{}, fmt.Errorf("no rover matching id")
|
||||||
}
|
}
|
||||||
|
return i.Pos, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRoverPosition sets the position of the rover
|
// SetRoverPosition sets the position of the rover
|
||||||
|
@ -163,13 +164,14 @@ func (w *World) SetRoverPosition(rover string, pos vector.Vector) error {
|
||||||
w.worldMutex.Lock()
|
w.worldMutex.Lock()
|
||||||
defer w.worldMutex.Unlock()
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
if i, ok := w.Rovers[rover]; ok {
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
i.Pos = pos
|
i.Pos = pos
|
||||||
w.Rovers[rover] = i
|
w.Rovers[rover] = i
|
||||||
return nil
|
return nil
|
||||||
} else {
|
|
||||||
return fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoverInventory returns the inventory of a requested rover
|
// RoverInventory returns the inventory of a requested rover
|
||||||
|
@ -177,11 +179,11 @@ func (w *World) RoverInventory(rover string) ([]byte, error) {
|
||||||
w.worldMutex.RLock()
|
w.worldMutex.RLock()
|
||||||
defer w.worldMutex.RUnlock()
|
defer w.worldMutex.RUnlock()
|
||||||
|
|
||||||
if i, ok := w.Rovers[rover]; ok {
|
i, ok := w.Rovers[rover]
|
||||||
return i.Inventory, nil
|
if !ok {
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("no rover matching id")
|
return nil, fmt.Errorf("no rover matching id")
|
||||||
}
|
}
|
||||||
|
return i.Inventory, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WarpRover sets an rovers position
|
// WarpRover sets an rovers position
|
||||||
|
@ -189,7 +191,10 @@ func (w *World) WarpRover(rover string, pos vector.Vector) error {
|
||||||
w.worldMutex.Lock()
|
w.worldMutex.Lock()
|
||||||
defer w.worldMutex.Unlock()
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
if i, ok := w.Rovers[rover]; ok {
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
// Nothing to do if these positions match
|
// Nothing to do if these positions match
|
||||||
if i.Pos == pos {
|
if i.Pos == pos {
|
||||||
return nil
|
return nil
|
||||||
|
@ -204,17 +209,17 @@ func (w *World) WarpRover(rover string, pos vector.Vector) error {
|
||||||
i.Pos = pos
|
i.Pos = pos
|
||||||
w.Rovers[rover] = i
|
w.Rovers[rover] = i
|
||||||
return nil
|
return nil
|
||||||
} else {
|
|
||||||
return fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPosition sets an rovers position
|
// MoveRover attempts to move a rover in a specific direction
|
||||||
func (w *World) MoveRover(rover string, b bearing.Bearing) (vector.Vector, error) {
|
func (w *World) MoveRover(rover string, b bearing.Bearing) (vector.Vector, error) {
|
||||||
w.worldMutex.Lock()
|
w.worldMutex.Lock()
|
||||||
defer w.worldMutex.Unlock()
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
if i, ok := w.Rovers[rover]; ok {
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return vector.Vector{}, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
// Try the new move position
|
// Try the new move position
|
||||||
newPos := i.Pos.Added(b.Vector())
|
newPos := i.Pos.Added(b.Vector())
|
||||||
|
|
||||||
|
@ -235,9 +240,6 @@ func (w *World) MoveRover(rover string, b bearing.Bearing) (vector.Vector, error
|
||||||
}
|
}
|
||||||
|
|
||||||
return i.Pos, nil
|
return i.Pos, nil
|
||||||
} else {
|
|
||||||
return vector.Vector{}, fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoverStash will stash an item at the current rovers position
|
// RoverStash will stash an item at the current rovers position
|
||||||
|
@ -245,20 +247,20 @@ func (w *World) RoverStash(rover string) (byte, error) {
|
||||||
w.worldMutex.Lock()
|
w.worldMutex.Lock()
|
||||||
defer w.worldMutex.Unlock()
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
if r, ok := w.Rovers[rover]; ok {
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return objects.Empty, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
tile := w.Atlas.GetTile(r.Pos)
|
tile := w.Atlas.GetTile(r.Pos)
|
||||||
if objects.IsStashable(tile) {
|
if !objects.IsStashable(tile) {
|
||||||
|
return objects.Empty, nil
|
||||||
|
}
|
||||||
|
|
||||||
r.Inventory = append(r.Inventory, tile)
|
r.Inventory = append(r.Inventory, tile)
|
||||||
w.Rovers[rover] = r
|
w.Rovers[rover] = r
|
||||||
w.Atlas.SetTile(r.Pos, objects.Empty)
|
w.Atlas.SetTile(r.Pos, objects.Empty)
|
||||||
return tile, nil
|
return tile, nil
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return objects.Empty, fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
|
|
||||||
return objects.Empty, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RadarFromRover can be used to query what a rover can currently see
|
// RadarFromRover can be used to query what a rover can currently see
|
||||||
|
@ -266,7 +268,11 @@ func (w *World) RadarFromRover(rover string) ([]byte, error) {
|
||||||
w.worldMutex.RLock()
|
w.worldMutex.RLock()
|
||||||
defer w.worldMutex.RUnlock()
|
defer w.worldMutex.RUnlock()
|
||||||
|
|
||||||
if r, ok := w.Rovers[rover]; ok {
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
// The radar should span in range direction on each axis, plus the row/column the rover is currently on
|
// The radar should span in range direction on each axis, plus the row/column the rover is currently on
|
||||||
radarSpan := (r.Range * 2) + 1
|
radarSpan := (r.Range * 2) + 1
|
||||||
roverPos := r.Pos
|
roverPos := r.Pos
|
||||||
|
@ -314,9 +320,6 @@ func (w *World) RadarFromRover(rover string) ([]byte, error) {
|
||||||
radar[len(radar)/2] = objects.Rover
|
radar[len(radar)/2] = objects.Rover
|
||||||
|
|
||||||
return radar, nil
|
return radar, nil
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enqueue will queue the commands given
|
// Enqueue will queue the commands given
|
||||||
|
@ -362,7 +365,7 @@ func (w *World) EnqueueAllIncoming() {
|
||||||
w.Incoming = make(map[string]CommandStream)
|
w.Incoming = make(map[string]CommandStream)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute will execute any commands in the current command queue
|
// ExecuteCommandQueues will execute any commands in the current command queue
|
||||||
func (w *World) ExecuteCommandQueues() {
|
func (w *World) ExecuteCommandQueues() {
|
||||||
w.cmdMutex.Lock()
|
w.cmdMutex.Lock()
|
||||||
defer w.cmdMutex.Unlock()
|
defer w.cmdMutex.Unlock()
|
||||||
|
@ -408,16 +411,16 @@ func (w *World) ExecuteCommand(c *Command, rover string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
case CommandRepair:
|
case CommandRepair:
|
||||||
if r, err := w.GetRover(rover); err != nil {
|
r, err := w.GetRover(rover)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
}
|
||||||
// Consume an inventory item to repair
|
// Consume an inventory item to repair
|
||||||
if len(r.Inventory) > 0 {
|
if len(r.Inventory) > 0 {
|
||||||
r.Inventory = r.Inventory[:len(r.Inventory)-1]
|
r.Inventory = r.Inventory[:len(r.Inventory)-1]
|
||||||
r.Integrity = r.Integrity + 1
|
r.Integrity = r.Integrity + 1
|
||||||
w.Rovers[rover] = r
|
w.Rovers[rover] = r
|
||||||
}
|
}
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unknown command: %s", c.Command)
|
return fmt.Errorf("unknown command: %s", c.Command)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ func Abs(x int) int {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
// pmod is a mositive modulo
|
// Pmod is a mositive modulo
|
||||||
// golang's % is a "remainder" function si misbehaves for negative modulus inputs
|
// golang's % is a "remainder" function si misbehaves for negative modulus inputs
|
||||||
func Pmod(x, d int) int {
|
func Pmod(x, d int) int {
|
||||||
if x == 0 || d == 0 {
|
if x == 0 || d == 0 {
|
||||||
|
|
|
@ -1,13 +1,20 @@
|
||||||
package objects
|
package objects
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// Empty represents an non-existant object
|
||||||
Empty = byte(' ')
|
Empty = byte(' ')
|
||||||
|
|
||||||
|
// Rover represents a live rover
|
||||||
Rover = byte('R')
|
Rover = byte('R')
|
||||||
|
|
||||||
|
// SmallRock is a small stashable rock
|
||||||
SmallRock = byte('o')
|
SmallRock = byte('o')
|
||||||
|
|
||||||
|
// LargeRock is a large blocking rock
|
||||||
LargeRock = byte('O')
|
LargeRock = byte('O')
|
||||||
)
|
)
|
||||||
|
|
||||||
// Check if an object is a blocking object
|
// IsBlocking checks if an object is a blocking object
|
||||||
func IsBlocking(object byte) bool {
|
func IsBlocking(object byte) bool {
|
||||||
var blocking = [...]byte{
|
var blocking = [...]byte{
|
||||||
Rover,
|
Rover,
|
||||||
|
@ -22,7 +29,7 @@ func IsBlocking(object byte) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if an object is stashable
|
// IsStashable checks if an object is stashable
|
||||||
func IsStashable(object byte) bool {
|
func IsStashable(object byte) bool {
|
||||||
var stashable = [...]byte{
|
var stashable = [...]byte{
|
||||||
SmallRock,
|
SmallRock,
|
||||||
|
|
|
@ -31,12 +31,13 @@ func jsonPath(name string) string {
|
||||||
// Save will serialise the interface into a json file
|
// Save will serialise the interface into a json file
|
||||||
func Save(name string, data interface{}) error {
|
func Save(name string, data interface{}) error {
|
||||||
p := jsonPath(name)
|
p := jsonPath(name)
|
||||||
if b, err := json.MarshalIndent(data, "", " "); err != nil {
|
b, err := json.MarshalIndent(data, "", " ")
|
||||||
return err
|
if err != nil {
|
||||||
} else {
|
|
||||||
if err := ioutil.WriteFile(p, b, os.ModePerm); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := ioutil.WriteFile(p, b, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Saved %s\n", p)
|
log.Printf("Saved %s\n", p)
|
||||||
|
|
|
@ -60,7 +60,7 @@ func Min(v1 Vector, v2 Vector) Vector {
|
||||||
return Vector{maths.Min(v1.X, v2.X), maths.Min(v1.Y, v2.Y)}
|
return Vector{maths.Min(v1.X, v2.X), maths.Min(v1.Y, v2.Y)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Min returns the max values in both vectors
|
// Max returns the max values in both vectors
|
||||||
func Max(v1 Vector, v2 Vector) Vector {
|
func Max(v1 Vector, v2 Vector) Vector {
|
||||||
return Vector{maths.Max(v1.X, v2.X), maths.Max(v1.Y, v2.Y)}
|
return Vector{maths.Max(v1.X, v2.X), maths.Max(v1.Y, v2.Y)}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
package version
|
package version
|
||||||
|
|
||||||
|
// Version represents a version to be overrided with -ldflags
|
||||||
var Version = "undefined"
|
var Version = "undefined"
|
||||||
|
|
Loading…
Add table
Reference in a new issue