1package main 2 3import ( 4 "math" 5 "math/rand" 6) 7 8// A click is resolved by the server: grab a piece, tear one off, engage a ship, 9// park on a body, or fly to a point. The client never decides which happened. 10func (h *hub) pick(p *player, x, y float64) { 11 // A piece is picked up by hand: the click lands on its outline, and the piece is 12 // within arm's reach of the hull. Nothing else is grabbable, so a click on 13 // wreckage across the system is only a move order. 14 if l := h.looseAt(x, y); l != nil && math.Hypot(l.X-p.X, l.Y-p.Y) < handR { 15 p.held, l.held = l.ID, p.ID 16 return 17 } 18 if id := hullPieceAt(p, x, y); id != 0 { 19 if off := h.detach(p, id); len(off) > 0 { 20 h.loose = append(h.loose, off...) 21 if l := h.looseByID(id); l != nil { 22 p.held, l.held = id, p.ID 23 } 24 return 25 } 26 } 27 for _, o := range h.players() { 28 if o == p { 29 continue 30 } 31 if id := hullPieceAt(o, x, y); id != 0 { 32 h.engage(p, o, id) 33 return 34 } 35 } 36 if b := h.bodyAt(x, y); b != nil { 37 h.anchor(p, b) 38 return 39 } 40 p.chase = 0 // any other order takes the ship off its approach 41 p.orbit = 0 42 p.TX, p.TY = x, y 43} 44 45// Clicking a ship's piece is a declaration, not a shot: both hulls become hostile, 46// the attacker closes to gun range, and its guns are pointed at the piece that was 47// clicked. Nobody is hostile until someone says so. A third hull keeps whatever 48// fight it was already in, so declaring on one enemy never calls off another. 49func (h *hub) engage(p, o *player, piece int) { 50 p.Foe, o.Foe, p.chase, p.orbit = o.ID, p.ID, o.ID, 0 51 p.Target = piece 52} 53 54func (h *hub) drag(p *player, x, y float64) { 55 l := h.looseByID(p.held) 56 if l == nil || l.held != p.ID { 57 return 58 } 59 l.X, l.Y = x, y 60} 61 62func (h *hub) drop(p *player, x, y float64) { 63 l := h.looseByID(p.held) 64 if l == nil { 65 return 66 } 67 p.held, l.held = 0, 0 68 l.X, l.Y = x, y 69 h.attach(p, l) 70} 71 72// Flying over ore hooks it onto the line: no clicks and nothing to decide. The 73// line only takes what the hull can haul, so a full ship flies past the rest. 74func (h *hub) haul(p *player) { 75 if p.AI { 76 return // an AI carries nothing: ore only ever moves between players 77 } 78 for _, o := range h.ore { 79 if len(p.tow) >= p.towLimit() { 80 return 81 } 82 if o.Towed != 0 || math.Hypot(p.X-o.X, p.Y-o.Y) > oreTouch { 83 continue 84 } 85 o.Towed, o.planet = p.ID, 0 // off the ring and onto the line 86 p.tow = append(p.tow, o.ID) 87 } 88} 89 90func (p *player) release(o *ore) { 91 o.Towed = 0 92 for i, id := range p.tow { 93 if id == o.ID { 94 p.tow = append(p.tow[:i], p.tow[i+1:]...) 95 return 96 } 97 } 98} 99 100func (p *player) towLimit() int { 101 n := towBase 102 for _, q := range p.Pieces { 103 if q.Kind == "circle" { 104 n += towPer 105 } 106 } 107 return n 108} 109 110// Docked means the ship has actually settled on its ring point, not that it is 111// still crossing the system toward it: a yard only serves a ship that stopped. 112func (p *player) docked() bool { 113 return math.Hypot(p.X-p.TX, p.Y-p.TY) < dockR 114} 115 116// Fuel capacity is summed from the hull: every piece carries a little and the 117// star carries most of it, so range is a decision about what you bolt on. 118func (p *player) capacity() float64 { 119 f := 0.0 120 for _, q := range p.Pieces { 121 _, u := stats(q.Kind, q.Tier) 122 f += u 123 } 124 return f 125} 126 127// Fuel is spent by the distance flown, so a stopped ship spends nothing, and a 128// ship settled in orbit is carried by the body for free. A docked shipyard fills 129// the tank back up for free. 130func (h *hub) drive(p *player, dt float64) { 131 if p.AI { 132 return // an AI never leaves its system, so range is not a thing it plays 133 } 134 if p.orbit != 0 && p.docked() { 135 if b := h.body(p.orbit); b != nil && b.Kind == "shipyard" { 136 p.Fuel = math.Min(p.FuelMax, p.Fuel+refuel*dt) 137 } 138 return 139 } 140 p.Fuel = math.Max(0, p.Fuel-math.Hypot(p.vx, p.vy)*dt/fuelPer) 141} 142 143// A shipyard services one load of ore at a time: three ore patch the whole hull 144// up, and once nothing is damaged the load buys a tier on a random piece that can 145// still take one. A whole, fully upgraded ship has nothing to buy, so it keeps 146// its ore and the yard does not fire. 147func (h *hub) service(p *player) bool { 148 hurt := false 149 for i, q := range p.Pieces { 150 if q.HP < q.MaxHP { 151 hurt = true 152 p.Pieces[i].HP = q.MaxHP 153 } 154 } 155 if hurt { 156 return true // repair first: a wreck is worth nothing upgraded 157 } 158 var open []int 159 for i, q := range p.Pieces { 160 if q.Tier < maxTier { 161 open = append(open, i) 162 } 163 } 164 if len(open) == 0 { 165 return false 166 } 167 q := &p.Pieces[open[rand.Intn(len(open))]] 168 q.Tier++ 169 q.MaxHP, _ = stats(q.Kind, q.Tier) 170 q.HP = q.MaxHP // a yard upgrade comes out whole 171 return true 172} 173 174// Ore handed over leaves the world with the line: the yard keeps it. 175func (h *hub) spend(p *player) { 176 for i := 0; i < yardOre && len(p.tow) > 0; i++ { 177 o := h.oreByID(p.tow[0]) 178 if o == nil { 179 p.tow = p.tow[1:] // an ore that is already gone can never stall the yard 180 continue 181 } 182 p.release(o) 183 h.removeOre(o.ID) 184 } 185} 186 187// Whatever the ship was dragging stays where it was: a dead or departed hull takes 188// nothing with it. 189func (h *hub) abandon(p *player) { 190 for _, id := range p.tow { 191 if o := h.oreByID(id); o != nil { 192 o.Towed = 0 // the line goes slack where the ship stopped 193 } 194 } 195 if l := h.looseByID(p.held); l != nil { 196 l.held = 0 197 } 198} 199 200// A ship in orbit of a shipyard keeps handing over loads as long as it has 201// enough ore on the line, one load a second so the yard can be watched working. 202func (h *hub) yard(p *player, dt float64) { 203 p.yard = math.Max(0, p.yard-dt) 204 if p.yard > 0 || len(p.tow) < yardOre || !h.service(p) { 205 return 206 } 207 h.spend(p) 208 p.yard = yardGap 209}