1package main 2 3import "math" 4 5// Every ship in the world, connected players and server-run AI alike. The caller 6// holds the lock. 7func (h *hub) players() []*player { 8 players := make([]*player, 0, len(h.clients)+len(h.ai)) 9 for c := range h.clients { 10 players = append(players, c.p) 11 } 12 return append(players, h.ai...) 13} 14 15func (h *hub) body(id int) *body { 16 for _, b := range h.bodies { 17 if b.ID == id { 18 return b 19 } 20 } 21 return nil 22} 23 24func (h *hub) player(id int) *player { 25 if id == 0 { 26 return nil 27 } 28 for c := range h.clients { 29 if c.p.ID == id { 30 return c.p 31 } 32 } 33 for _, p := range h.ai { 34 if p.ID == id { 35 return p 36 } 37 } 38 return nil 39} 40 41// A dead or departed ship takes its grudges with it: nobody stays hostile with a 42// ship that is no longer there, and no guns stay pointed at a piece that is gone. 43func (h *hub) forgive(id int) { 44 for _, p := range h.players() { 45 if p.Foe == id { 46 p.Foe, p.Target = 0, 0 47 } 48 if p.chase == id { 49 p.chase = 0 50 } 51 } 52} 53 54func (h *hub) bodyAt(x, y float64) *body { 55 for _, b := range h.bodies { 56 if math.Hypot(b.X-x, b.Y-y) < b.R*hitR { 57 return b 58 } 59 } 60 return nil 61} 62 63func (h *hub) looseByID(id int) *loose { 64 for _, l := range h.loose { 65 if l.ID == id { 66 return l 67 } 68 } 69 return nil 70} 71 72// The loose piece under a point, if nobody already has it in hand. Its own outline 73// decides, exactly as it is drawn, so a click lands on the piece and never on the 74// space around it. 75func (h *hub) looseAt(x, y float64) *loose { 76 for _, l := range h.loose { 77 if l.held == 0 && pieceAt(l.Kind, l.X, l.Y, l.H, x, y) { 78 return l 79 } 80 } 81 return nil 82} 83 84func (h *hub) oreByID(id int) *ore { 85 for _, o := range h.ore { 86 if o.ID == id { 87 return o 88 } 89 } 90 return nil 91} 92 93func (h *hub) removeLoose(id int) { 94 for i, l := range h.loose { 95 if l.ID == id { 96 h.loose = append(h.loose[:i], h.loose[i+1:]...) 97 return 98 } 99 } 100} 101 102func (h *hub) removeOre(id int) { 103 for i, o := range h.ore { 104 if o.ID == id { 105 h.ore = append(h.ore[:i], h.ore[i+1:]...) 106 return 107 } 108 } 109}