From e8f64bc22de1338d5f2b6d67eaff0e9e67152368 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 11 Nov 2014 20:17:57 -0500 Subject: [PATCH] new scan implementation is done --- commands.go | 12 ------------ game.go | 2 +- main.go | 14 ++++++++++++-- scan.go | 30 +++++++++++++++++++++++++++++- system.go | 11 +++++++++++ 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/commands.go b/commands.go index 05b79a2..3057f0c 100644 --- a/commands.go +++ b/commands.go @@ -122,18 +122,6 @@ var scanCommand = &Command{ } currentGame.Register(NewScan(conn.System())) conn.RecordScan() - system := conn.System() - log_info("scan sent from %s", system.name) - for id, _ := range index { - if id == system.id { - continue - } - delay := system.LightTimeTo(index[id]) - id2 := id - After(delay, func() { - scanSystem(id2, system.id) - }) - } }, } diff --git a/game.go b/game.go index ac8669c..a591580 100644 --- a/game.go +++ b/game.go @@ -101,7 +101,7 @@ func (g *Game) Reset() { } func (g *Game) Run() { - ticker := time.Tick(time.Second / time.Duration(frameRate)) + ticker := time.Tick(time.Second / time.Duration(options.frameRate)) for { select { case <-ticker: diff --git a/main.go b/main.go index e9e544d..4a3d664 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "io" "log" @@ -11,13 +12,16 @@ import ( "time" ) +var options struct { + lightSpeed float64 + frameRate int +} + var ( dataPath = "/projects/exo/expl.speck" info_log *log.Logger error_log *log.Logger currentGame *Game - frameRate = 100 // frames/second - lightSpeed = 0.01 // parsecs/frame ) func log_error(template string, args ...interface{}) { @@ -79,6 +83,7 @@ func handleConnection(conn *Connection) { } func main() { + flag.Parse() dbconnect() rand.Seed(time.Now().UnixNano()) info_log = log.New(os.Stdout, "[INFO] ", 0) @@ -103,3 +108,8 @@ func main() { go handleConnection(NewConnection(conn)) } } + +func init() { + flag.Float64Var(&options.lightSpeed, "light-speed", 0.01, "speed of light in parsecs per frame") + flag.IntVar(&options.frameRate, "frame-rate", 100, "frame rate, in frames per second") +} diff --git a/scan.go b/scan.go index b23c40e..91f3608 100644 --- a/scan.go +++ b/scan.go @@ -21,6 +21,21 @@ type scanResult struct { colonizedBy *Connection } +func (r *scanResult) Empty() bool { + return r.players == nil && r.colonizedBy == nil +} + +func (r *scanResult) playerNames() []string { + if r.players == nil { + return nil + } + names := make([]string, 0, len(r.players)) + for conn := range r.players { + names = append(names, conn.PlayerName()) + } + return names +} + func NewScan(origin *System) *scan { return &scan{ origin: origin, @@ -30,7 +45,7 @@ func NewScan(origin *System) *scan { } func (s *scan) Tick(frame int64) { - s.dist += lightSpeed + s.dist += options.lightSpeed s.hits() s.echos() } @@ -55,6 +70,7 @@ func (s *scan) hits() { } func (s *scan) hitSystem(sys *System, dist float64) scanResult { + sys.NotifyInhabitants("scan detected from %s\n", s.origin.Label()) r := scanResult{ system: sys, colonizedBy: sys.colonizedBy, @@ -76,5 +92,17 @@ func (s *scan) echos() { break } log_info("echo from %v reached origin %v after %v", res.system.name, s.origin.name, time.Since(s.start)) + if res.Empty() { + continue + } + s.origin.NotifyInhabitants("results from scan of %s:\n", res.system.Label()) + s.origin.NotifyInhabitants("\tdistance: %v\n", s.origin.DistanceTo(res.system)) + inhabitants := res.playerNames() + if inhabitants != nil { + s.origin.NotifyInhabitants("\tinhabitants: %v\n", inhabitants) + } + if res.colonizedBy != nil { + s.origin.NotifyInhabitants("\tcolonized by: %v\n", res.colonizedBy.PlayerName()) + } } } diff --git a/system.go b/system.go index a217e56..3b26385 100644 --- a/system.go +++ b/system.go @@ -44,6 +44,12 @@ func (s *System) Leave(p *Connection) { p.location = nil } +func (s *System) NotifyInhabitants(template string, args ...interface{}) { + s.EachConn(func(conn *Connection) { + fmt.Fprintf(conn, template, args...) + }) +} + func (s *System) EachConn(fn func(*Connection)) { if s.players == nil { return @@ -154,6 +160,11 @@ func bombNotice(to_id, from_id int) { }) } +// for players to read. +func (s System) Label() string { + return fmt.Sprintf("%s (id: %v)", s.name, s.id) +} + func (e System) String() string { return fmt.Sprintf("", e.name, e.x, e.y, e.z, e.planets) }