|
|
|
package sim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/jordanorelli/astro-domu/internal/math"
|
|
|
|
"github.com/jordanorelli/astro-domu/internal/wire"
|
|
|
|
"github.com/jordanorelli/blammo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// world is the entire simulated world. A world consists of many rooms.
|
|
|
|
type world struct {
|
|
|
|
*blammo.Log
|
|
|
|
inbox chan Request
|
|
|
|
connect chan connect
|
|
|
|
|
|
|
|
done chan bool
|
|
|
|
lastEntityID int
|
|
|
|
rooms map[string]*room
|
|
|
|
players map[string]*player
|
|
|
|
}
|
|
|
|
|
|
|
|
type connect struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
login wire.Login
|
|
|
|
failed chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWorld(log *blammo.Log) *world {
|
|
|
|
bounds := math.CreateRect(10, 10)
|
|
|
|
foyer := room{
|
|
|
|
Log: log.Child("foyer"),
|
|
|
|
name: "foyer",
|
|
|
|
Rect: bounds,
|
|
|
|
tiles: make([]tile, bounds.Area()),
|
|
|
|
players: make(map[string]*player),
|
|
|
|
}
|
|
|
|
foyer.tiles[55].here = &entity{
|
|
|
|
ID: 777,
|
|
|
|
Position: math.Vec{5, 5},
|
|
|
|
Glyph: 'd',
|
|
|
|
behavior: doNothing{},
|
|
|
|
}
|
|
|
|
log.Info("created foyer with bounds: %#v having width: %d height: %d area: %d", foyer.Rect, foyer.Width, foyer.Height, foyer.Area())
|
|
|
|
return &world{
|
|
|
|
Log: log,
|
|
|
|
rooms: map[string]*room{"foyer": &foyer},
|
|
|
|
done: make(chan bool),
|
|
|
|
inbox: make(chan Request),
|
|
|
|
connect: make(chan connect),
|
|
|
|
players: make(map[string]*player),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *world) run(hz int) {
|
|
|
|
defer w.Info("simulation has exited run loop")
|
|
|
|
|
|
|
|
period := time.Second / time.Duration(hz)
|
|
|
|
w.Info("starting world with a tick rate of %dhz, frame duration of %v", hz, period)
|
|
|
|
ticker := time.NewTicker(period)
|
|
|
|
lastTick := time.Now()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c := <-w.connect:
|
|
|
|
w.register(c)
|
|
|
|
|
|
|
|
case req := <-w.inbox:
|
|
|
|
w.handleRequest(req)
|
|
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
w.tick(time.Since(lastTick))
|
|
|
|
lastTick = time.Now()
|
|
|
|
|
|
|
|
case <-w.done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *world) handleRequest(req Request) {
|
|
|
|
w.Info("read from inbox: %v", req)
|
|
|
|
|
|
|
|
if req.From == "" {
|
|
|
|
w.Error("request has no from: %v", req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p, ok := w.players[req.From]
|
|
|
|
if !ok {
|
|
|
|
w.Error("received non login request of type %T from unknown player %q", req.Wants, req.From)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.pending == nil {
|
|
|
|
p.pending = &req
|
|
|
|
} else {
|
|
|
|
p.outbox <- wire.ErrorResponse(req.Seq, "you already have a request for this frame")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *world) register(c connect) {
|
|
|
|
w.Info("register: %#v", c.login)
|
|
|
|
foyer := w.rooms["foyer"]
|
|
|
|
if len(foyer.players) >= 100 {
|
|
|
|
c.failed <- fmt.Errorf("room is full")
|
|
|
|
close(c.failed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := player{
|
|
|
|
Log: w.Log.Child("players").Child(c.login.Name),
|
|
|
|
name: c.login.Name,
|
|
|
|
outbox: make(chan wire.Response, 8),
|
|
|
|
pending: &Request{
|
|
|
|
From: c.login.Name,
|
|
|
|
Seq: 1,
|
|
|
|
Wants: &spawnPlayer{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
foyer.players[c.login.Name] = &p
|
|
|
|
w.players[c.login.Name] = &p
|
|
|
|
|
|
|
|
p.start(w.inbox, c.conn, foyer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *world) stop() error {
|
|
|
|
w.Info("stopping simulation")
|
|
|
|
w.done <- true
|
|
|
|
w.Info("simulation stopped")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *world) tick(d time.Duration) {
|
|
|
|
for _, r := range w.rooms {
|
|
|
|
r.update(d)
|
|
|
|
}
|
|
|
|
}
|