From 4e1ade8e12ba879a8312210fed0c277ca27556b9 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 8 Jun 2019 23:57:22 +0000 Subject: [PATCH] global index is gone! :party_parrot: --- bomb.go | 2 +- db.go | 2 +- game.go | 2 +- system.go | 67 +++++++++++++++++++++++++------------------------------ 4 files changed, 33 insertions(+), 40 deletions(-) diff --git a/bomb.go b/bomb.go index adc5d4c..6b44793 100644 --- a/bomb.go +++ b/bomb.go @@ -36,7 +36,7 @@ func (b *Bomb) Dead() bool { func (b *Bomb) Tick(game *Game) { b.fti -= 1 if b.fti <= 0 { - b.target.Bombed(b.profile, game.frame) + b.target.Bombed(b.profile, game) b.done = true log_info("bomb went off on %v", b.target) } diff --git a/db.go b/db.go index 5df189f..a8f383e 100644 --- a/db.go +++ b/db.go @@ -49,7 +49,7 @@ func planetsData() { planet.Store(db) } } - indexSystems() + // indexSystems() } func setupDb() { diff --git a/game.go b/game.go index 526826a..2a317c1 100644 --- a/game.go +++ b/game.go @@ -55,7 +55,7 @@ func NewGame() *Game { if err := game.Create(); err != nil { log_error("unable to create game: %v", err) } - for _, system := range index { + for _, system := range game.galaxy.systems { game.Register(system) } return game diff --git a/system.go b/system.go index fc93c1e..50d9874 100644 --- a/system.go +++ b/system.go @@ -4,15 +4,9 @@ import ( "database/sql" "fmt" "math" - "math/rand" "time" ) -var ( - index map[int]*System - nameIndex map[string]*System -) - type System struct { *Shield id int @@ -120,7 +114,7 @@ type Ray struct { dist float64 // distance in parsecs } -func (s *System) Bombed(bomber *Connection, frame int64) { +func (s *System) Bombed(bomber *Connection, game *Game) { if s.Shield != nil { if s.Shield.Hit() { s.EachConn(func(conn *Connection) { @@ -133,7 +127,7 @@ func (s *System) Bombed(bomber *Connection, frame int64) { } s.EachConn(func(conn *Connection) { - conn.Die(frame) + conn.Die(game.frame) s.Leave(conn) bomber.MadeKill(conn) }) @@ -142,21 +136,20 @@ func (s *System) Bombed(bomber *Connection, frame int64) { s.colonizedBy = nil } - for id, _ := range index { + for id, other := range game.galaxy.systems { if id == s.id { continue } - delay := s.LightTimeTo(index[id]) - id2 := id + delay := s.LightTimeTo(game.galaxy.systems[id]) + from := s + to := other time.AfterFunc(delay, func() { - bombNotice(id2, s.id) + bombNotice(to, from) }) } } -func bombNotice(to_id, from_id int) { - to := index[to_id] - from := index[from_id] +func bombNotice(to, from *System) { to.EachConn(func(conn *Connection) { conn.Printf("a bombing has been observed on %s\n", from.name) }) @@ -193,25 +186,25 @@ func dist3d(x1, y1, z1, x2, y2, z2 float64) float64 { return math.Sqrt(sq(x1-x2) + sq(y1-y2) + sq(z1-z2)) } -func indexSystems() map[int]*System { - rows, err := db.Query(`select * from planets`) - if err != nil { - log_error("unable to select all planets: %v", err) - return nil - } - defer rows.Close() - index = make(map[int]*System, 551) - nameIndex = make(map[string]*System, 551) - for rows.Next() { - p := System{} - if err := rows.Scan(&p.id, &p.name, &p.x, &p.y, &p.z, &p.planets); err != nil { - log_info("unable to scan planet row: %v", err) - continue - } - index[p.id] = &p - nameIndex[p.name] = &p - p.money = int64(rand.NormFloat64()*options.moneySigma + options.moneyMean) - // log_info("seeded system %v with %v monies", p, p.money) - } - return index -} +// func indexSystems() map[int]*System { +// rows, err := db.Query(`select * from planets`) +// if err != nil { +// log_error("unable to select all planets: %v", err) +// return nil +// } +// defer rows.Close() +// index = make(map[int]*System, 551) +// nameIndex = make(map[string]*System, 551) +// for rows.Next() { +// p := System{} +// if err := rows.Scan(&p.id, &p.name, &p.x, &p.y, &p.z, &p.planets); err != nil { +// log_info("unable to scan planet row: %v", err) +// continue +// } +// index[p.id] = &p +// nameIndex[p.name] = &p +// p.money = int64(rand.NormFloat64()*options.moneySigma + options.moneyMean) +// // log_info("seeded system %v with %v monies", p, p.money) +// } +// return index +// }