1package main 2 3import ( 4 "encoding/json" 5 "math" 6 "time" 7 8 "github.com/gorilla/websocket" 9) 10 11// The rig flies as one body: steer toward the target, never past it. A target that 12// moves carries its own velocity, and the ship flies that plus its closing speed, so 13// the slowdown ramp measures the gap that is left and not a point that runs away. 14func (p *player) steer(dt, tvx, tvy float64) { 15 dx, dy := p.TX-p.X, p.TY-p.Y 16 dist := math.Hypot(dx, dy) 17 wantX, wantY := tvx, tvy 18 if dist >= settle { 19 s := math.Min(maxSpeed, dist*arrive) 20 wantX, wantY = tvx+dx/dist*s, tvy+dy/dist*s 21 } 22 dvx, dvy := wantX-p.vx, wantY-p.vy 23 if dl, max := math.Hypot(dvx, dvy), accel*dt; dl > max { 24 p.vx += dvx / dl * max 25 p.vy += dvy / dl * max 26 } else { 27 p.vx, p.vy = wantX, wantY 28 } 29 p.X += p.vx * dt 30 p.Y += p.vy * dt 31 if math.Hypot(p.vx, p.vy) > 4 { 32 p.H = math.Atan2(p.vy, p.vx) 33 } 34} 35 36func (h *hub) step(dt float64) { 37 h.time += dt 38 for _, b := range h.bodies { 39 b.place(h.time) 40 if b.Kind != "barren" { 41 continue 42 } 43 if b.wait -= dt; b.wait <= 0 { 44 b.wait = oreWait() 45 h.spawnOre(b) 46 } 47 } 48 for _, o := range h.ore { 49 h.stepOre(o, dt) 50 } 51 for _, p := range h.players() { // every hull, connected or not 52 if p.AI { 53 h.think(p, dt) // patrol a yard, or run down whoever it is fighting 54 } 55 var tvx, tvy float64 56 if b := h.body(p.orbit); b != nil { 57 // The ring point turns with the body around its star, so the ship is 58 // carried with it even in another system. Keeping up with it is the 59 // point's own velocity, which grows with the ring: chasing only where it 60 // was leaves the ship parked short of a fast body and never docked. 61 d := b.speed * (h.time - p.anchored) 62 cs, sn := math.Cos(d), math.Sin(d) 63 p.TX = b.cx + p.ox*cs - p.oy*sn 64 p.TY = b.cy + p.ox*sn + p.oy*cs 65 tvx, tvy = b.speed*-(p.TY-b.cy), b.speed*(p.TX-b.cx) 66 } 67 if o := h.player(p.chase); o != nil { 68 tvx, tvy = 0, 0 // the hull being closed on is aimed at, not led 69 if math.Hypot(o.X-p.X, o.Y-p.Y) > gunRange*approach { 70 p.TX, p.TY = o.X, o.Y // close in until the guns can reach 71 } else { 72 p.TX, p.TY = p.X, p.Y // in range: settle and keep shooting 73 } 74 } 75 p.steer(dt, tvx, tvy) 76 // An order is spent once the ship reaches it, and a spent order is nothing to 77 // mark: a point when the ship settles on it, an orbit when it is docked on the 78 // ring. A flown point stops being a target at all; an orbit has to keep its 79 // turning ring point, or the ship would be left standing still. 80 r := settle 81 if p.orbit != 0 { 82 r = dockR 83 } 84 p.Order = math.Hypot(p.TX-p.X, p.TY-p.Y) >= r 85 if !p.Order && p.orbit == 0 { 86 p.TX, p.TY = p.X, p.Y 87 } 88 h.burn(p, dt) 89 h.haul(p) 90 h.guns(p, dt) 91 p.FuelMax = p.capacity() 92 h.drive(p, dt) 93 if b := h.body(p.orbit); b != nil && b.Kind == "shipyard" && p.docked() { 94 h.yard(p, dt) 95 } 96 for i, id := range p.tow { 97 if o := h.oreByID(id); o != nil { 98 h.onLine(o, p, float64(i+1)) 99 } 100 } 101 if p.Pieces[0].HP == 0 || p.Fuel == 0 { 102 h.die(p) // the root piece is the run itself, and so is the tank 103 } 104 } 105 h.stepShots(dt) 106 h.stepAI(dt) 107 h.stepLoose(dt) 108} 109 110// A root piece at zero, or a dry tank, ends that hull. A player is replaced at 111// once under a new id, so the client snaps there; an AI leaves a wreck and comes 112// back to its own shipyard after a delay. 113func (h *hub) die(p *player) { 114 if p.AI { 115 h.bury(p) 116 return 117 } 118 for c := range h.clients { 119 if c.p == p { 120 c.p = h.respawn(p) 121 return 122 } 123 } 124} 125 126func (h *hub) tick() { 127 for range time.Tick(time.Second / tickRate) { 128 h.mu.Lock() 129 h.step(tickDt) 130 players := h.players() 131 // Marshal under the lock: the state is shared, not copied per client. 132 out := make(map[*client][]byte, len(h.clients)) 133 for c := range h.clients { 134 b, err := json.Marshal(state{ 135 T: "state", You: c.p.ID, Held: c.p.held, 136 Bodies: h.bodies, Systems: h.systems, Loose: h.loose, Ore: h.ore, Shots: h.shots, Players: players, 137 }) 138 if err == nil { 139 out[c] = b 140 } 141 } 142 h.mu.Unlock() 143 144 for c, b := range out { 145 c.ws.SetWriteDeadline(time.Now().Add(2 * time.Second)) 146 c.ws.WriteMessage(websocket.TextMessage, b) 147 } 148 } 149}