2020-05-31 11:18:26 +01:00
|
|
|
package game
|
2020-05-30 23:42:01 +01:00
|
|
|
|
2020-06-02 17:44:39 +01:00
|
|
|
import (
|
2020-06-25 22:02:11 +01:00
|
|
|
"bufio"
|
2020-06-02 17:44:39 +01:00
|
|
|
"fmt"
|
2020-06-11 19:04:53 +01:00
|
|
|
"log"
|
2020-06-08 23:46:56 +01:00
|
|
|
"math"
|
2020-06-06 18:44:40 +01:00
|
|
|
"math/rand"
|
2020-06-25 22:02:11 +01:00
|
|
|
"os"
|
2020-06-06 14:44:59 +01:00
|
|
|
"sync"
|
2020-06-02 17:44:39 +01:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-06-11 20:42:59 +01:00
|
|
|
"github.com/mdiluz/rove/pkg/atlas"
|
2020-06-09 18:08:07 +01:00
|
|
|
"github.com/mdiluz/rove/pkg/bearing"
|
2020-06-26 19:45:24 +01:00
|
|
|
"github.com/mdiluz/rove/pkg/objects"
|
2020-06-09 18:08:07 +01:00
|
|
|
"github.com/mdiluz/rove/pkg/vector"
|
2020-06-02 17:44:39 +01:00
|
|
|
)
|
2020-05-31 00:06:14 +01:00
|
|
|
|
2020-05-30 23:42:01 +01:00
|
|
|
// World describes a self contained universe and everything in it
|
|
|
|
type World struct {
|
2020-06-04 21:17:43 +01:00
|
|
|
// Rovers is a id->data map of all the rovers in the game
|
2020-06-27 00:32:27 +01:00
|
|
|
Rovers map[string]Rover `json:"rovers"`
|
2020-06-06 14:44:59 +01:00
|
|
|
|
2020-06-07 13:33:44 +01:00
|
|
|
// Atlas represends the world map of chunks and tiles
|
2020-06-11 20:42:59 +01:00
|
|
|
Atlas atlas.Atlas `json:"atlas"`
|
2020-06-07 13:33:44 +01:00
|
|
|
|
2020-06-06 14:44:59 +01:00
|
|
|
// Mutex to lock around all world operations
|
|
|
|
worldMutex sync.RWMutex
|
|
|
|
|
|
|
|
// Commands is the set of currently executing command streams per rover
|
2020-06-27 00:32:27 +01:00
|
|
|
CommandQueue map[string]CommandStream `json:"commands"`
|
2020-06-06 14:44:59 +01:00
|
|
|
|
2020-06-09 20:44:25 +01:00
|
|
|
// Incoming represents the set of commands to add to the queue at the end of the current tick
|
2020-06-27 00:32:27 +01:00
|
|
|
Incoming map[string]CommandStream `json:"incoming"`
|
2020-06-09 20:44:25 +01:00
|
|
|
|
2020-06-06 14:44:59 +01:00
|
|
|
// Mutex to lock around command operations
|
|
|
|
cmdMutex sync.RWMutex
|
2020-06-25 22:02:11 +01:00
|
|
|
|
|
|
|
// Set of possible words to use for names
|
|
|
|
words []string
|
2020-05-30 23:42:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:02:11 +01:00
|
|
|
var wordsFile = os.Getenv("WORDS_FILE")
|
|
|
|
|
2020-05-30 23:42:01 +01:00
|
|
|
// NewWorld creates a new world object
|
2020-06-27 14:48:21 +01:00
|
|
|
func NewWorld(chunkSize int) *World {
|
2020-06-25 22:02:11 +01:00
|
|
|
|
|
|
|
// Try and load the words file
|
|
|
|
var lines []string
|
|
|
|
if file, err := os.Open(wordsFile); err != nil {
|
|
|
|
log.Printf("Couldn't read words file [%s], running without words: %s\n", wordsFile, err)
|
|
|
|
} else {
|
|
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
lines = append(lines, scanner.Text())
|
|
|
|
}
|
|
|
|
if scanner.Err() != nil {
|
|
|
|
log.Printf("Failure during word file scan: %s\n", scanner.Err())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:44:39 +01:00
|
|
|
return &World{
|
2020-06-27 00:32:27 +01:00
|
|
|
Rovers: make(map[string]Rover),
|
|
|
|
CommandQueue: make(map[string]CommandStream),
|
|
|
|
Incoming: make(map[string]CommandStream),
|
2020-06-27 14:48:21 +01:00
|
|
|
Atlas: atlas.NewAtlas(chunkSize),
|
2020-06-25 22:02:11 +01:00
|
|
|
words: lines,
|
2020-06-02 17:51:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// SpawnRover adds an rover to the game
|
2020-06-27 00:32:27 +01:00
|
|
|
func (w *World) SpawnRover() (string, error) {
|
2020-06-06 14:44:59 +01:00
|
|
|
w.worldMutex.Lock()
|
|
|
|
defer w.worldMutex.Unlock()
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// Initialise the rover
|
|
|
|
rover := Rover{
|
2020-06-27 01:14:17 +01:00
|
|
|
Range: 4.0,
|
|
|
|
Integrity: 10,
|
|
|
|
Name: uuid.New().String(),
|
2020-05-30 23:42:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:02:11 +01:00
|
|
|
// Assign a random name if we have words
|
|
|
|
if len(w.words) > 0 {
|
2020-06-27 00:32:27 +01:00
|
|
|
for {
|
|
|
|
// Loop until we find a unique name
|
|
|
|
name := fmt.Sprintf("%s-%s", w.words[rand.Intn(len(w.words))], w.words[rand.Intn(len(w.words))])
|
|
|
|
if _, ok := w.Rovers[name]; !ok {
|
|
|
|
rover.Name = name
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 22:02:11 +01:00
|
|
|
}
|
|
|
|
|
2020-06-06 18:45:15 +01:00
|
|
|
// Spawn in a random place near the origin
|
2020-06-26 18:22:37 +01:00
|
|
|
rover.Pos = vector.Vector{
|
2020-06-09 18:08:07 +01:00
|
|
|
X: w.Atlas.ChunkSize/2 - rand.Intn(w.Atlas.ChunkSize),
|
|
|
|
Y: w.Atlas.ChunkSize/2 - rand.Intn(w.Atlas.ChunkSize),
|
2020-06-06 18:45:15 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 22:30:03 +01:00
|
|
|
// Seach until we error (run out of world)
|
|
|
|
for {
|
2020-07-03 17:00:04 +01:00
|
|
|
_, obj := w.Atlas.QueryPosition(rover.Pos)
|
|
|
|
if !obj.IsBlocking() {
|
2020-06-27 14:48:21 +01:00
|
|
|
break
|
2020-06-07 22:30:03 +01:00
|
|
|
} else {
|
2020-06-27 14:48:21 +01:00
|
|
|
// Try and spawn to the east of the blockage
|
|
|
|
rover.Pos.Add(vector.Vector{X: 1, Y: 0})
|
2020-06-07 22:30:03 +01:00
|
|
|
}
|
2020-06-27 14:48:21 +01:00
|
|
|
|
2020-06-07 22:30:03 +01:00
|
|
|
}
|
|
|
|
|
2020-06-26 18:22:37 +01:00
|
|
|
log.Printf("Spawned rover at %+v\n", rover.Pos)
|
2020-06-08 23:46:56 +01:00
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// Append the rover to the list
|
2020-06-27 00:32:27 +01:00
|
|
|
w.Rovers[rover.Name] = rover
|
2020-05-30 23:42:01 +01:00
|
|
|
|
2020-06-27 00:32:27 +01:00
|
|
|
return rover.Name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRover gets a specific rover by name
|
|
|
|
func (w *World) GetRover(rover string) (Rover, error) {
|
|
|
|
w.worldMutex.RLock()
|
|
|
|
defer w.worldMutex.RUnlock()
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
i, ok := w.Rovers[rover]
|
|
|
|
if !ok {
|
2020-06-27 00:32:27 +01:00
|
|
|
return Rover{}, fmt.Errorf("Failed to find rover with name: %s", rover)
|
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
return i, nil
|
2020-05-30 23:42:01 +01:00
|
|
|
}
|
2020-06-02 17:44:39 +01:00
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// DestroyRover Removes an rover from the game
|
2020-06-27 00:32:27 +01:00
|
|
|
func (w *World) DestroyRover(rover string) error {
|
2020-06-06 14:44:59 +01:00
|
|
|
w.worldMutex.Lock()
|
|
|
|
defer w.worldMutex.Unlock()
|
|
|
|
|
2020-07-03 17:00:04 +01:00
|
|
|
_, ok := w.Rovers[rover]
|
2020-06-30 23:59:58 +01:00
|
|
|
if !ok {
|
2020-06-04 21:17:43 +01:00
|
|
|
return fmt.Errorf("no rover matching id")
|
2020-06-03 12:31:52 +01:00
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
|
|
|
|
delete(w.Rovers, rover)
|
2020-06-03 12:31:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-26 18:22:37 +01:00
|
|
|
// RoverPosition returns the position of the rover
|
2020-06-27 00:32:27 +01:00
|
|
|
func (w *World) RoverPosition(rover string) (vector.Vector, error) {
|
2020-06-26 18:22:37 +01:00
|
|
|
w.worldMutex.RLock()
|
|
|
|
defer w.worldMutex.RUnlock()
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
i, ok := w.Rovers[rover]
|
|
|
|
if !ok {
|
2020-06-26 18:22:37 +01:00
|
|
|
return vector.Vector{}, fmt.Errorf("no rover matching id")
|
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
return i.Pos, nil
|
2020-06-26 18:22:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRoverPosition sets the position of the rover
|
2020-06-27 00:32:27 +01:00
|
|
|
func (w *World) SetRoverPosition(rover string, pos vector.Vector) error {
|
2020-06-26 18:22:37 +01:00
|
|
|
w.worldMutex.Lock()
|
|
|
|
defer w.worldMutex.Unlock()
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
i, ok := w.Rovers[rover]
|
|
|
|
if !ok {
|
2020-06-08 23:02:09 +01:00
|
|
|
return fmt.Errorf("no rover matching id")
|
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
|
|
|
|
i.Pos = pos
|
|
|
|
w.Rovers[rover] = i
|
|
|
|
return nil
|
2020-06-08 23:02:09 +01:00
|
|
|
}
|
|
|
|
|
2020-06-26 23:37:10 +01:00
|
|
|
// RoverInventory returns the inventory of a requested rover
|
2020-07-03 17:00:04 +01:00
|
|
|
func (w *World) RoverInventory(rover string) ([]objects.Object, error) {
|
2020-06-26 23:37:10 +01:00
|
|
|
w.worldMutex.RLock()
|
|
|
|
defer w.worldMutex.RUnlock()
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
i, ok := w.Rovers[rover]
|
|
|
|
if !ok {
|
2020-06-26 23:37:10 +01:00
|
|
|
return nil, fmt.Errorf("no rover matching id")
|
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
return i.Inventory, nil
|
2020-06-26 23:37:10 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:59:00 +01:00
|
|
|
// WarpRover sets an rovers position
|
2020-06-27 00:32:27 +01:00
|
|
|
func (w *World) WarpRover(rover string, pos vector.Vector) error {
|
2020-06-06 14:44:59 +01:00
|
|
|
w.worldMutex.Lock()
|
|
|
|
defer w.worldMutex.Unlock()
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
i, ok := w.Rovers[rover]
|
|
|
|
if !ok {
|
2020-06-04 21:17:43 +01:00
|
|
|
return fmt.Errorf("no rover matching id")
|
2020-06-03 18:12:08 +01:00
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
// Nothing to do if these positions match
|
|
|
|
if i.Pos == pos {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the tile is not blocked
|
2020-07-03 17:00:04 +01:00
|
|
|
_, obj := w.Atlas.QueryPosition(pos)
|
|
|
|
if obj.IsBlocking() {
|
2020-06-30 23:59:58 +01:00
|
|
|
return fmt.Errorf("can't warp rover to occupied tile, check before warping")
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Pos = pos
|
|
|
|
w.Rovers[rover] = i
|
|
|
|
return nil
|
2020-06-03 18:12:08 +01:00
|
|
|
}
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// MoveRover attempts to move a rover in a specific direction
|
2020-06-27 00:32:27 +01:00
|
|
|
func (w *World) MoveRover(rover string, b bearing.Bearing) (vector.Vector, error) {
|
2020-06-06 14:44:59 +01:00
|
|
|
w.worldMutex.Lock()
|
|
|
|
defer w.worldMutex.Unlock()
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
i, ok := w.Rovers[rover]
|
|
|
|
if !ok {
|
|
|
|
return vector.Vector{}, fmt.Errorf("no rover matching id")
|
|
|
|
}
|
|
|
|
// Try the new move position
|
|
|
|
newPos := i.Pos.Added(b.Vector())
|
|
|
|
|
|
|
|
// Get the tile and verify it's empty
|
2020-07-03 17:00:04 +01:00
|
|
|
_, obj := w.Atlas.QueryPosition(newPos)
|
|
|
|
if !obj.IsBlocking() {
|
2020-06-30 23:59:58 +01:00
|
|
|
// Perform the move
|
|
|
|
i.Pos = newPos
|
|
|
|
w.Rovers[rover] = i
|
|
|
|
} else {
|
|
|
|
// If it is a blocking tile, reduce the rover integrity
|
|
|
|
i.Integrity = i.Integrity - 1
|
|
|
|
if i.Integrity == 0 {
|
|
|
|
// TODO: The rover needs to be left dormant with the player
|
2020-06-27 01:14:17 +01:00
|
|
|
} else {
|
2020-06-30 23:59:58 +01:00
|
|
|
w.Rovers[rover] = i
|
2020-06-07 19:03:16 +01:00
|
|
|
}
|
2020-06-02 17:44:39 +01:00
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
|
|
|
|
return i.Pos, nil
|
2020-06-02 17:44:39 +01:00
|
|
|
}
|
2020-06-03 18:12:08 +01:00
|
|
|
|
2020-06-26 18:59:12 +01:00
|
|
|
// RoverStash will stash an item at the current rovers position
|
2020-07-03 17:00:04 +01:00
|
|
|
func (w *World) RoverStash(rover string) (objects.Type, error) {
|
2020-06-26 18:59:12 +01:00
|
|
|
w.worldMutex.Lock()
|
|
|
|
defer w.worldMutex.Unlock()
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
r, ok := w.Rovers[rover]
|
|
|
|
if !ok {
|
2020-07-03 17:00:04 +01:00
|
|
|
return objects.None, fmt.Errorf("no rover matching id")
|
2020-06-26 18:59:12 +01:00
|
|
|
}
|
|
|
|
|
2020-07-03 17:00:04 +01:00
|
|
|
_, obj := w.Atlas.QueryPosition(r.Pos)
|
|
|
|
if !obj.IsStashable() {
|
|
|
|
return objects.None, nil
|
2020-06-30 23:59:58 +01:00
|
|
|
}
|
|
|
|
|
2020-07-03 17:00:04 +01:00
|
|
|
r.Inventory = append(r.Inventory, obj)
|
2020-06-30 23:59:58 +01:00
|
|
|
w.Rovers[rover] = r
|
2020-07-03 17:00:04 +01:00
|
|
|
w.Atlas.SetObject(r.Pos, objects.Object{Type: objects.None})
|
|
|
|
return obj.Type, nil
|
2020-06-26 18:59:12 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 22:14:55 +01:00
|
|
|
// RadarFromRover can be used to query what a rover can currently see
|
2020-07-03 17:00:04 +01:00
|
|
|
func (w *World) RadarFromRover(rover string) (radar []byte, objs []byte, err error) {
|
2020-06-06 14:44:59 +01:00
|
|
|
w.worldMutex.RLock()
|
|
|
|
defer w.worldMutex.RUnlock()
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
r, ok := w.Rovers[rover]
|
|
|
|
if !ok {
|
2020-07-03 17:00:04 +01:00
|
|
|
err = fmt.Errorf("no rover matching id")
|
|
|
|
return
|
2020-06-30 23:59:58 +01:00
|
|
|
}
|
2020-06-08 18:14:24 +01:00
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// The radar should span in range direction on each axis, plus the row/column the rover is currently on
|
|
|
|
radarSpan := (r.Range * 2) + 1
|
|
|
|
roverPos := r.Pos
|
2020-06-07 22:30:03 +01:00
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// Get the radar min and max values
|
|
|
|
radarMin := vector.Vector{
|
|
|
|
X: roverPos.X - r.Range,
|
|
|
|
Y: roverPos.Y - r.Range,
|
|
|
|
}
|
|
|
|
radarMax := vector.Vector{
|
|
|
|
X: roverPos.X + r.Range,
|
|
|
|
Y: roverPos.Y + r.Range,
|
|
|
|
}
|
2020-06-07 22:30:03 +01:00
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// Gather up all tiles within the range
|
2020-07-03 17:00:04 +01:00
|
|
|
radar = make([]byte, radarSpan*radarSpan)
|
|
|
|
objs = make([]byte, radarSpan*radarSpan)
|
2020-06-30 23:59:58 +01:00
|
|
|
for j := radarMin.Y; j <= radarMax.Y; j++ {
|
|
|
|
for i := radarMin.X; i <= radarMax.X; i++ {
|
|
|
|
q := vector.Vector{X: i, Y: j}
|
2020-06-27 14:48:21 +01:00
|
|
|
|
2020-07-03 17:00:04 +01:00
|
|
|
tile, obj := w.Atlas.QueryPosition(q)
|
2020-06-30 23:59:58 +01:00
|
|
|
|
|
|
|
// Get the position relative to the bottom left of the radar
|
|
|
|
relative := q.Added(radarMin.Negated())
|
|
|
|
index := relative.X + relative.Y*radarSpan
|
|
|
|
radar[index] = tile
|
2020-07-03 17:00:04 +01:00
|
|
|
objs[index] = byte(obj.Type)
|
2020-06-05 15:48:55 +01:00
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
}
|
2020-06-04 22:14:55 +01:00
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// Add all rovers to the radar
|
|
|
|
for _, r := range w.Rovers {
|
|
|
|
// If the rover is in range
|
|
|
|
dist := r.Pos.Added(roverPos.Negated())
|
|
|
|
dist = dist.Abs()
|
2020-06-25 23:51:31 +01:00
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
if dist.X <= r.Range && dist.Y <= r.Range {
|
|
|
|
relative := r.Pos.Added(radarMin.Negated())
|
|
|
|
index := relative.X + relative.Y*radarSpan
|
2020-07-03 17:00:04 +01:00
|
|
|
objs[index] = byte(objects.Rover)
|
2020-06-25 23:27:07 +01:00
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
}
|
2020-06-25 23:27:07 +01:00
|
|
|
|
2020-07-03 17:00:04 +01:00
|
|
|
return radar, objs, nil
|
2020-06-04 22:14:55 +01:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:44:59 +01:00
|
|
|
// Enqueue will queue the commands given
|
2020-06-27 00:32:27 +01:00
|
|
|
func (w *World) Enqueue(rover string, commands ...Command) error {
|
2020-06-06 14:44:59 +01:00
|
|
|
|
|
|
|
// First validate the commands
|
2020-06-03 18:12:08 +01:00
|
|
|
for _, c := range commands {
|
2020-06-06 12:45:45 +01:00
|
|
|
switch c.Command {
|
2020-06-27 01:35:09 +01:00
|
|
|
case CommandMove:
|
2020-06-09 18:09:51 +01:00
|
|
|
if _, err := bearing.FromString(c.Bearing); err != nil {
|
2020-06-09 18:33:05 +01:00
|
|
|
return fmt.Errorf("unknown bearing: %s", c.Bearing)
|
2020-06-06 12:45:45 +01:00
|
|
|
}
|
2020-06-27 01:35:09 +01:00
|
|
|
case CommandStash:
|
|
|
|
case CommandRepair:
|
2020-06-26 23:30:42 +01:00
|
|
|
// Nothing to verify
|
2020-06-06 12:45:45 +01:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown command: %s", c.Command)
|
2020-06-03 18:12:08 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 14:44:59 +01:00
|
|
|
|
|
|
|
// Lock our commands edit
|
|
|
|
w.cmdMutex.Lock()
|
|
|
|
defer w.cmdMutex.Unlock()
|
|
|
|
|
2020-06-09 20:44:25 +01:00
|
|
|
// Append the commands to the incoming set
|
2020-06-10 17:39:42 +01:00
|
|
|
if cmds, ok := w.Incoming[rover]; ok {
|
|
|
|
w.Incoming[rover] = append(cmds, commands...)
|
|
|
|
} else {
|
|
|
|
w.Incoming[rover] = commands
|
|
|
|
}
|
2020-06-06 14:44:59 +01:00
|
|
|
|
2020-06-03 18:12:08 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-06 14:44:59 +01:00
|
|
|
|
2020-06-09 20:44:25 +01:00
|
|
|
// EnqueueAllIncoming will enqueue the incoming commands
|
|
|
|
func (w *World) EnqueueAllIncoming() {
|
|
|
|
// Add any incoming commands from this tick and clear that queue
|
|
|
|
for id, incoming := range w.Incoming {
|
|
|
|
commands := w.CommandQueue[id]
|
|
|
|
commands = append(commands, incoming...)
|
|
|
|
w.CommandQueue[id] = commands
|
|
|
|
}
|
2020-06-27 00:32:27 +01:00
|
|
|
w.Incoming = make(map[string]CommandStream)
|
2020-06-09 20:44:25 +01:00
|
|
|
}
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// ExecuteCommandQueues will execute any commands in the current command queue
|
2020-06-06 15:52:03 +01:00
|
|
|
func (w *World) ExecuteCommandQueues() {
|
2020-06-06 14:44:59 +01:00
|
|
|
w.cmdMutex.Lock()
|
|
|
|
defer w.cmdMutex.Unlock()
|
|
|
|
|
2020-06-09 20:44:25 +01:00
|
|
|
// Iterate through all the current commands
|
2020-06-06 14:44:59 +01:00
|
|
|
for rover, cmds := range w.CommandQueue {
|
|
|
|
if len(cmds) != 0 {
|
|
|
|
// Extract the first command in the queue
|
|
|
|
c := cmds[0]
|
2020-06-26 22:26:27 +01:00
|
|
|
w.CommandQueue[rover] = cmds[1:]
|
2020-06-06 14:44:59 +01:00
|
|
|
|
2020-06-26 22:26:27 +01:00
|
|
|
// Execute the command
|
|
|
|
if err := w.ExecuteCommand(&c, rover); err != nil {
|
2020-06-11 19:04:53 +01:00
|
|
|
log.Println(err)
|
2020-06-26 22:26:27 +01:00
|
|
|
// TODO: Report this error somehow
|
2020-06-06 14:44:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Clean out the empty entry
|
|
|
|
delete(w.CommandQueue, rover)
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 20:44:25 +01:00
|
|
|
|
|
|
|
// Add any incoming commands from this tick and clear that queue
|
|
|
|
w.EnqueueAllIncoming()
|
2020-06-06 14:44:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteCommand will execute a single command
|
2020-06-27 00:32:27 +01:00
|
|
|
func (w *World) ExecuteCommand(c *Command, rover string) (err error) {
|
2020-06-27 02:02:18 +01:00
|
|
|
log.Printf("Executing command: %+v for %s\n", *c, rover)
|
2020-06-06 14:44:59 +01:00
|
|
|
|
|
|
|
switch c.Command {
|
2020-06-26 18:48:07 +01:00
|
|
|
case CommandMove:
|
2020-06-09 18:09:51 +01:00
|
|
|
if dir, err := bearing.FromString(c.Bearing); err != nil {
|
2020-06-26 22:26:27 +01:00
|
|
|
return err
|
2020-06-06 14:44:59 +01:00
|
|
|
} else if _, err := w.MoveRover(rover, dir); err != nil {
|
2020-06-26 22:26:27 +01:00
|
|
|
return err
|
2020-06-06 14:44:59 +01:00
|
|
|
}
|
2020-06-26 18:48:07 +01:00
|
|
|
|
2020-06-26 18:59:12 +01:00
|
|
|
case CommandStash:
|
|
|
|
if _, err := w.RoverStash(rover); err != nil {
|
2020-06-26 22:26:27 +01:00
|
|
|
return err
|
2020-06-26 18:59:12 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 01:35:09 +01:00
|
|
|
case CommandRepair:
|
2020-06-30 23:59:58 +01:00
|
|
|
r, err := w.GetRover(rover)
|
|
|
|
if err != nil {
|
2020-06-27 01:35:09 +01:00
|
|
|
return err
|
2020-06-30 23:59:58 +01:00
|
|
|
}
|
|
|
|
// Consume an inventory item to repair
|
|
|
|
if len(r.Inventory) > 0 {
|
|
|
|
r.Inventory = r.Inventory[:len(r.Inventory)-1]
|
|
|
|
r.Integrity = r.Integrity + 1
|
|
|
|
w.Rovers[rover] = r
|
2020-06-27 01:35:09 +01:00
|
|
|
}
|
2020-06-06 14:44:59 +01:00
|
|
|
default:
|
2020-06-26 22:26:27 +01:00
|
|
|
return fmt.Errorf("unknown command: %s", c.Command)
|
2020-06-06 14:44:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2020-06-06 15:52:03 +01:00
|
|
|
|
2020-06-08 23:46:56 +01:00
|
|
|
// PrintTiles simply prints the input tiles directly for debug
|
2020-06-12 22:51:18 +01:00
|
|
|
func PrintTiles(tiles []byte) {
|
2020-06-08 23:46:56 +01:00
|
|
|
num := int(math.Sqrt(float64(len(tiles))))
|
|
|
|
for j := num - 1; j >= 0; j-- {
|
|
|
|
for i := 0; i < num; i++ {
|
2020-07-03 17:00:04 +01:00
|
|
|
|
|
|
|
t := tiles[i+num*j]
|
|
|
|
if t != 0 {
|
|
|
|
fmt.Printf("%c", t)
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" ")
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:46:56 +01:00
|
|
|
}
|
|
|
|
fmt.Print("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 15:52:03 +01:00
|
|
|
// RLock read locks the world
|
|
|
|
func (w *World) RLock() {
|
|
|
|
w.worldMutex.RLock()
|
|
|
|
w.cmdMutex.RLock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock read unlocks the world
|
|
|
|
func (w *World) RUnlock() {
|
|
|
|
w.worldMutex.RUnlock()
|
|
|
|
w.cmdMutex.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock locks the world
|
|
|
|
func (w *World) Lock() {
|
|
|
|
w.worldMutex.Lock()
|
|
|
|
w.cmdMutex.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks the world
|
|
|
|
func (w *World) Unlock() {
|
|
|
|
w.worldMutex.Unlock()
|
|
|
|
w.cmdMutex.Unlock()
|
|
|
|
}
|