new scan implementation is done

slack
Jordan Orelli 10 years ago
parent 296942ea81
commit e8f64bc22d

@ -122,18 +122,6 @@ var scanCommand = &Command{
} }
currentGame.Register(NewScan(conn.System())) currentGame.Register(NewScan(conn.System()))
conn.RecordScan() 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)
})
}
}, },
} }

@ -101,7 +101,7 @@ func (g *Game) Reset() {
} }
func (g *Game) Run() { func (g *Game) Run() {
ticker := time.Tick(time.Second / time.Duration(frameRate)) ticker := time.Tick(time.Second / time.Duration(options.frameRate))
for { for {
select { select {
case <-ticker: case <-ticker:

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -11,13 +12,16 @@ import (
"time" "time"
) )
var options struct {
lightSpeed float64
frameRate int
}
var ( var (
dataPath = "/projects/exo/expl.speck" dataPath = "/projects/exo/expl.speck"
info_log *log.Logger info_log *log.Logger
error_log *log.Logger error_log *log.Logger
currentGame *Game currentGame *Game
frameRate = 100 // frames/second
lightSpeed = 0.01 // parsecs/frame
) )
func log_error(template string, args ...interface{}) { func log_error(template string, args ...interface{}) {
@ -79,6 +83,7 @@ func handleConnection(conn *Connection) {
} }
func main() { func main() {
flag.Parse()
dbconnect() dbconnect()
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
info_log = log.New(os.Stdout, "[INFO] ", 0) info_log = log.New(os.Stdout, "[INFO] ", 0)
@ -103,3 +108,8 @@ func main() {
go handleConnection(NewConnection(conn)) 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")
}

@ -21,6 +21,21 @@ type scanResult struct {
colonizedBy *Connection 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 { func NewScan(origin *System) *scan {
return &scan{ return &scan{
origin: origin, origin: origin,
@ -30,7 +45,7 @@ func NewScan(origin *System) *scan {
} }
func (s *scan) Tick(frame int64) { func (s *scan) Tick(frame int64) {
s.dist += lightSpeed s.dist += options.lightSpeed
s.hits() s.hits()
s.echos() s.echos()
} }
@ -55,6 +70,7 @@ func (s *scan) hits() {
} }
func (s *scan) hitSystem(sys *System, dist float64) scanResult { func (s *scan) hitSystem(sys *System, dist float64) scanResult {
sys.NotifyInhabitants("scan detected from %s\n", s.origin.Label())
r := scanResult{ r := scanResult{
system: sys, system: sys,
colonizedBy: sys.colonizedBy, colonizedBy: sys.colonizedBy,
@ -76,5 +92,17 @@ func (s *scan) echos() {
break break
} }
log_info("echo from %v reached origin %v after %v", res.system.name, s.origin.name, time.Since(s.start)) 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())
}
} }
} }

@ -44,6 +44,12 @@ func (s *System) Leave(p *Connection) {
p.location = nil 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)) { func (s *System) EachConn(fn func(*Connection)) {
if s.players == nil { if s.players == nil {
return 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 { func (e System) String() string {
return fmt.Sprintf("<name: %s x: %v y: %v z: %v planets: %v>", e.name, e.x, e.y, e.z, e.planets) return fmt.Sprintf("<name: %s x: %v y: %v z: %v planets: %v>", e.name, e.x, e.y, e.z, e.planets)
} }

Loading…
Cancel
Save