global index is gone! :party_parrot:

master
Jordan Orelli 6 years ago
parent 209635083d
commit 4e1ade8e12

@ -36,7 +36,7 @@ func (b *Bomb) Dead() bool {
func (b *Bomb) Tick(game *Game) { func (b *Bomb) Tick(game *Game) {
b.fti -= 1 b.fti -= 1
if b.fti <= 0 { if b.fti <= 0 {
b.target.Bombed(b.profile, game.frame) b.target.Bombed(b.profile, game)
b.done = true b.done = true
log_info("bomb went off on %v", b.target) log_info("bomb went off on %v", b.target)
} }

@ -49,7 +49,7 @@ func planetsData() {
planet.Store(db) planet.Store(db)
} }
} }
indexSystems() // indexSystems()
} }
func setupDb() { func setupDb() {

@ -55,7 +55,7 @@ func NewGame() *Game {
if err := game.Create(); err != nil { if err := game.Create(); err != nil {
log_error("unable to create game: %v", err) log_error("unable to create game: %v", err)
} }
for _, system := range index { for _, system := range game.galaxy.systems {
game.Register(system) game.Register(system)
} }
return game return game

@ -4,15 +4,9 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"math" "math"
"math/rand"
"time" "time"
) )
var (
index map[int]*System
nameIndex map[string]*System
)
type System struct { type System struct {
*Shield *Shield
id int id int
@ -120,7 +114,7 @@ type Ray struct {
dist float64 // distance in parsecs 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 != nil {
if s.Shield.Hit() { if s.Shield.Hit() {
s.EachConn(func(conn *Connection) { s.EachConn(func(conn *Connection) {
@ -133,7 +127,7 @@ func (s *System) Bombed(bomber *Connection, frame int64) {
} }
s.EachConn(func(conn *Connection) { s.EachConn(func(conn *Connection) {
conn.Die(frame) conn.Die(game.frame)
s.Leave(conn) s.Leave(conn)
bomber.MadeKill(conn) bomber.MadeKill(conn)
}) })
@ -142,21 +136,20 @@ func (s *System) Bombed(bomber *Connection, frame int64) {
s.colonizedBy = nil s.colonizedBy = nil
} }
for id, _ := range index { for id, other := range game.galaxy.systems {
if id == s.id { if id == s.id {
continue continue
} }
delay := s.LightTimeTo(index[id]) delay := s.LightTimeTo(game.galaxy.systems[id])
id2 := id from := s
to := other
time.AfterFunc(delay, func() { time.AfterFunc(delay, func() {
bombNotice(id2, s.id) bombNotice(to, from)
}) })
} }
} }
func bombNotice(to_id, from_id int) { func bombNotice(to, from *System) {
to := index[to_id]
from := index[from_id]
to.EachConn(func(conn *Connection) { to.EachConn(func(conn *Connection) {
conn.Printf("a bombing has been observed on %s\n", from.name) 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)) return math.Sqrt(sq(x1-x2) + sq(y1-y2) + sq(z1-z2))
} }
func indexSystems() map[int]*System { // func indexSystems() map[int]*System {
rows, err := db.Query(`select * from planets`) // rows, err := db.Query(`select * from planets`)
if err != nil { // if err != nil {
log_error("unable to select all planets: %v", err) // log_error("unable to select all planets: %v", err)
return nil // return nil
} // }
defer rows.Close() // defer rows.Close()
index = make(map[int]*System, 551) // index = make(map[int]*System, 551)
nameIndex = make(map[string]*System, 551) // nameIndex = make(map[string]*System, 551)
for rows.Next() { // for rows.Next() {
p := System{} // p := System{}
if err := rows.Scan(&p.id, &p.name, &p.x, &p.y, &p.z, &p.planets); err != nil { // 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) // log_info("unable to scan planet row: %v", err)
continue // continue
} // }
index[p.id] = &p // index[p.id] = &p
nameIndex[p.name] = &p // nameIndex[p.name] = &p
p.money = int64(rand.NormFloat64()*options.moneySigma + options.moneyMean) // p.money = int64(rand.NormFloat64()*options.moneySigma + options.moneyMean)
// log_info("seeded system %v with %v monies", p, p.money) // // log_info("seeded system %v with %v monies", p, p.money)
} // }
return index // return index
} // }

Loading…
Cancel
Save