1package main 2 3import ( 4 "math" 5 "math/rand" 6 "sort" 7) 8 9// A ring point at angle a, as an offset from the centre of the body's system. The 10// body's own place is folded in, so a ship parked there turns with the body around 11// the whole star rather than around itself, and systems can sit anywhere. 12func ring(b *body, a, r float64) (float64, float64) { 13 return b.X - b.cx + math.Cos(a)*r, b.Y - b.cy + math.Sin(a)*r 14} 15 16// Parking on a body bolts the target to that body's frame: the ring point turns 17// with the planet around its star, so the ship is carried along. 18func (h *hub) anchor(p *player, b *body) { 19 gap := standoff 20 if b.Kind == "star" { 21 gap = starGap 22 } 23 a := math.Atan2(p.Y-b.Y, p.X-b.X) // enter the ring on the side we already are on 24 p.orbit, p.anchored = b.ID, h.time 25 p.chase = 0 // settling into orbit breaks off any approach order 26 p.ox, p.oy = ring(b, a, b.R+gap) 27} 28 29// A new run is born on the ring of one of the inner yards, already carried by it: 30// a ship starts in orbit, not in empty space. 31func (h *hub) deploy(p *player) { 32 if len(h.spawns) == 0 { 33 return // the generator always yards the core, so there is always somewhere to start 34 } 35 b := h.body(h.spawns[rand.Intn(len(h.spawns))]) 36 p.ox, p.oy = ring(b, rand.Float64()*2*math.Pi, b.R+standoff) 37 p.X, p.Y = b.cx+p.ox, b.cy+p.oy 38 p.TX, p.TY = p.X, p.Y 39 p.orbit, p.anchored = b.ID, h.time 40} 41 42// Torn-off pieces keep the ids they had, so a piece never becomes a new thing by 43// being ripped off a hull. 44func (h *hub) detach(p *player, id int) []*loose { 45 if len(p.Pieces) < 2 || p.Pieces[0].ID == id { 46 return nil 47 } 48 return h.sever(p, map[int]bool{id: true}) 49} 50 51// Anything no longer connected to the root piece drifts off, carrying the damage 52// it had taken with it. 53func (h *hub) sever(p *player, gone map[int]bool) []*loose { 54 for grew := true; grew; { 55 grew = false 56 for _, q := range p.Pieces { 57 if !gone[q.ID] && gone[q.Parent] { 58 gone[q.ID], grew = true, true 59 } 60 } 61 } 62 var off []*loose 63 keep := p.Pieces[:0] 64 for _, q := range p.Pieces { 65 if !gone[q.ID] { 66 keep = append(keep, q) 67 continue 68 } 69 off = append(off, h.wreck(p, q)) 70 } 71 p.Pieces = keep 72 return off 73} 74 75// A piece torn off a hull keeps its identity, its damage and its tier, and lands in 76// space where the hull was holding it. A server hull's wreckage carries a timer: it 77// is litter from a fight nothing was watching, not somebody's run. 78func (h *hub) wreck(p *player, q piece) *loose { 79 x, y := hullToWorld(p, q.LX, q.LY) 80 l := &loose{ID: q.ID, Kind: q.Kind, X: x, Y: y, H: p.H, HP: q.HP, MaxHP: q.MaxHP, Tier: q.Tier} 81 if p.AI { 82 l.ttl = wreckTTL 83 } 84 return l 85} 86 87// Wreckage with a timer is taken out of the world when it runs down, unless a hand 88// is on it: a piece being dragged is not litter yet. Nothing a player left behind 89// has a timer, because that is where a run ended and what it was flying. 90func (h *hub) stepLoose(dt float64) { 91 live := h.loose[:0] 92 for _, l := range h.loose { 93 if l.ttl > 0 && l.held == 0 { 94 if l.ttl -= dt; l.ttl <= 0 { 95 continue 96 } 97 } 98 live = append(live, l) 99 } 100 h.loose = live 101} 102 103// A star is not instant death: its heat eats whatever of the hull actually enters 104// it, so brushing the limb costs armour and lingering costs the run. A piece is hit 105// by its own outline, not the ship's centre, so a big rig can lose an outer piece 106// while the root stays clear. 107func (h *hub) burn(p *player, dt float64) { 108 var dead []int 109 for i := range p.Pieces { 110 q := &p.Pieces[i] 111 x, y := hullToWorld(p, q.LX, q.LY) 112 if !h.hot(x, y) { 113 continue 114 } 115 q.HP = math.Max(0, q.HP-burn*dt) 116 if q.HP == 0 { 117 dead = append(dead, q.ID) 118 } 119 } 120 for _, id := range dead { 121 h.destroy(p, id) // the dead piece and whatever only it held on 122 } 123} 124 125// A point is hot once it sits past a star's surface, with a piece's own girth 126// taken into account, so only real overlap burns. 127func (h *hub) hot(x, y float64) bool { 128 for _, b := range h.bodies { 129 if b.Kind == "star" && math.Hypot(b.X-x, b.Y-y) < b.R+pieceR { 130 return true 131 } 132 } 133 return false 134} 135 136// A piece at zero hit points is destroyed for good: the hull loses it, and 137// whatever it was the only link to breaks off as wreckage. 138func (h *hub) destroy(p *player, id int) { 139 if len(p.Pieces) < 2 || p.Pieces[0].ID == id { 140 return // the root piece is the run itself 141 } 142 for _, l := range h.sever(p, map[int]bool{id: true}) { 143 if l.ID != id { 144 h.loose = append(h.loose, l) // the dead piece is gone, not salvage 145 } 146 } 147} 148 149// Losing the root piece ends that life. Everything still bolted to the hull is left 150// where the ship died as loose wreckage, the tow line goes slack, and the callsign 151// comes back in the core yard's orbit in a brand new ship under a new player id, so 152// the client snaps there instead of sliding out of its own corpse. 153func (h *hub) respawn(p *player) *player { 154 h.corpse(p) 155 h.nextID++ 156 q := &player{ID: h.nextID, Name: p.Name} // the callsign outlives the ship 157 q.Pieces = []piece{h.fitted("triangle", 0)} // every life starts from one triangle 158 q.FuelMax = q.capacity() 159 q.Fuel = q.FuelMax 160 h.deploy(q) 161 return q 162} 163 164// A dead hull leaves everything but its root piece where it died, its tow line 165// goes slack, and the run that just ended is nobody's enemy any more. 166func (h *hub) corpse(p *player) { 167 h.forgive(p.ID) 168 for _, q := range p.Pieces[1:] { // the dead root piece is gone, not salvage 169 h.loose = append(h.loose, h.wreck(p, q)) 170 } 171 h.abandon(p) 172} 173 174// The slots around a piece that nothing is bolted into yet. 175func freeSlots(ps []piece, p piece) []int { 176 var free []int 177 for i := range slots { 178 a := float64(i) / slots * 2 * math.Pi 179 x, y := p.LX+math.Cos(a)*spacing, p.LY+math.Sin(a)*spacing 180 taken := false 181 for _, q := range ps { 182 if math.Hypot(q.LX-x, q.LY-y) < spacing*0.5 { 183 taken = true 184 break 185 } 186 } 187 if !taken { 188 free = append(free, i) 189 } 190 } 191 return free 192} 193 194// Snap a dropped piece onto the free slot closest to where it was let go. 195func (h *hub) attach(p *player, l *loose) bool { 196 lx, ly := hullToLocal(p, l.X, l.Y) 197 dist := func(q piece) float64 { return math.Hypot(q.LX-lx, q.LY-ly) } 198 near := append([]piece(nil), p.Pieces...) 199 sort.Slice(near, func(i, j int) bool { return dist(near[i]) < dist(near[j]) }) 200 if dist(near[0]) > spacing*1.5 { 201 return false // not dropped on the hull 202 } 203 for _, q := range near { 204 slot, best := -1, math.MaxFloat64 205 for _, i := range freeSlots(p.Pieces, q) { 206 a := float64(i) / slots * 2 * math.Pi 207 if d := math.Abs(wrap(a - math.Atan2(ly-q.LY, lx-q.LX))); d < best { 208 slot, best = i, d 209 } 210 } 211 if slot < 0 { 212 continue // piece is surrounded, try the next one out 213 } 214 a := float64(slot) / slots * 2 * math.Pi 215 p.Pieces = append(p.Pieces, piece{ 216 ID: l.ID, Kind: l.Kind, Parent: q.ID, 217 LX: q.LX + math.Cos(a)*spacing, 218 LY: q.LY + math.Sin(a)*spacing, 219 HP: l.HP, MaxHP: l.MaxHP, Tier: l.Tier, 220 }) 221 h.removeLoose(l.ID) 222 return true 223 } 224 return false 225}