1package main 2 3import ( 4 "math" 5 "sync" 6 7 "github.com/gorilla/websocket" 8) 9 10const ( 11 tickRate = 20 12 tickDt = 1.0 / tickRate 13 14 standoff = 26.0 // ring radius beyond a planet's surface 15 starGap = 34.0 // a star kills on contact, so its ring sits further out 16 hitR = 1.5 // click radius of a body, as a multiple of its radius 17 spacing = 17.0 // world distance between attached piece centers 18 slots = 6 // attachment directions around a hull piece 19 // A piece you have just torn off sits one spacing out of the hull, and the outer 20 // pieces of a big hull sit two, so the hand has to reach that far to reach them. 21 handR = 2 * spacing 22 maxSpeed = 90.0 23 accel = 260.0 24 arrive = 2.2 // slowdown radius factor 25 settle = 0.5 // a ship this close to its target has arrived and stops thrusting 26 reach = 4000.0 // an order further out than this is ignored 27 28 oreGap = 15.0 // closest two ore may sit: also the spacing of the rings 29 oreRings = 4 // rings a barren planet can park ore on 30 oreRingMin = 12.0 // first ring's distance beyond the surface 31 oreBrake = 10.0 // outward deceleration, so a launch coasts out then parks 32 oreOrbitV = 12.0 // world units per second travelled along a ring 33 oreTouch = 14.0 // flying this close to ore hooks it onto the line 34 towGap = 12.0 // distance between ore on a tow line 35 oreWaitMin = 30.0 // a barren planet throws an ore every 30-60s 36 oreWaitMax = 60.0 37 oreHolds = 3 // a barren planet holds 3-6 ore, a fixed count per planet 38 39 pieceR = 11.0 // world radius of a piece: its outline, and its share of a star 40 burn = 5.0 // hit points a second a piece loses inside a star 41 dockR = 8.0 // the ship counts as docked inside this of its ring point 42 towBase = 3 // ore a hull can haul with no circle at all 43 towPer = 3 // extra ore each circle adds to the line 44 maxTier = 2 // a piece can be upgraded twice 45 yardOre = 3 // ore a shipyard takes for one service 46 yardGap = 1.0 // seconds between services, so a full line drains visibly 47 48 fuelPer = 1800.0 // world units of travel one unit of fuel buys 49 refuel = 1.0 // fuel a second a docked shipyard puts back in the tank 50 51 aiPerYard = 2 // police ships a shipyard keeps in orbit 52 aiDelay = 25.0 // seconds after a police death before that yard births another 53 aiMin = 1 // ship pieces the smallest police hull is born with 54 aiMax = 2 // and the largest a yard fields 55 patrolSpin = 0.18 // radians a second an AI circles its yard on patrol 56 patrolLow = 6.0 // the tightest patrol line sits this far beyond the standoff 57 patrolHigh = 30.0 // and the widest this much further out again 58 59 // The two factions a server hull can belong to. A connected player has no side: 60 // players are only at war with whoever declared it. 61 sidePolice = "police" 62 sideRogue = "rogue" 63 64 rogueMidN = 2 // rogue patrols in the gaps of the middle galaxy ring 65 rogueRimN = 6 // and in the gaps of the rim, so danger grows outward 66 rogueMidPieces = 2 // piece count of a middle-ring rogue hull, plus 67 rogueMidVar = 2 // this much more drawn per hull 68 rogueRimPieces = 3 // a rim rogue is bigger still 69 rogueRimVar = 3 70 rogueMidTier = 1 // best tier a middle-ring hull is built from 71 rogueRimTier = 2 // and the rim's, so going out buys quality and not only bulk 72 rogueDelay = 25.0 // seconds a dead rogue's point stays quiet 73 rogueLeash = 300.0 // how far from that point it runs a target down 74 rogueClear = 500.0 // a rogue point keeps this far from every star system 75 rogueRingLow = 80.0 // patrol circle around that point, plus 76 rogueRingVar = 120.0 // this much more per hull 77 wreckTTL = 60.0 // seconds a server hull's wreckage lies before it is gone 78 79 gunRange = 140.0 // world distance a triangle will shoot at 80 gunCool = 1.6 // seconds between salvos from one hull 81 shotSpeed = 150.0 // a round is faster than a ship, so range alone is not safety 82 shotDmg = 3.0 // hit points one round takes off a piece 83 approach = 0.8 // of gun range: an approach order closes to here, no further 84) 85 86// A hull is a graph of pieces placed relative to the ship. Piece ids are unique 87// across the whole world, so a piece can be torn off one hull and bolted to 88// another without ever changing identity. 89type piece struct { 90 ID int `json:"id"` 91 Kind string `json:"kind"` 92 LX float64 `json:"lx"` 93 LY float64 `json:"ly"` 94 Parent int `json:"parent"` 95 HP float64 `json:"hp"` 96 MaxHP float64 `json:"maxhp"` 97 Tier int `json:"tier"` 98} 99 100type loose struct { 101 ID int `json:"id"` 102 Kind string `json:"kind"` 103 X float64 `json:"x"` 104 Y float64 `json:"y"` 105 H float64 `json:"h"` 106 HP float64 `json:"hp"` 107 MaxHP float64 `json:"maxhp"` 108 Tier int `json:"tier"` 109 held int // player dragging it, 0 = free 110 ttl float64 // seconds until a server hull's wreckage is gone, 0 = it stays 111} 112 113// A shot is a real object in the world: it travels, it can be outrun sideways, 114// and it only hurts the piece it actually crosses. 115type shot struct { 116 ID int `json:"id"` 117 X float64 `json:"x"` 118 Y float64 `json:"y"` 119 H float64 `json:"h"` 120 121 owner int // player who fired it, immune to its own round 122 left float64 // world distance it can still travel 123} 124 125// An ore piece is thrown out of a barren planet and parks in one of its rings. 126// It lives in the planet's rotating frame (an angle and a radius), so it travels 127// with the planet around the star, and it is only a world point once placed. 128type ore struct { 129 ID int `json:"id"` 130 X float64 `json:"x"` 131 Y float64 `json:"y"` 132 H float64 `json:"h"` 133 Towed int `json:"towed"` // player hauling it, 0 = loose 134 135 planet int // body it orbits, 0 = drifting where it was let go 136 a float64 // slot angle on its ring, in the planet's frame 137 r float64 // current distance from the planet centre 138 vr float64 // outward speed, 0 once it is parked in its ring 139 ring float64 // radius it settles into 140} 141 142// A body is a star or a planet. Orbit 0 and speed 0 is the star, which never 143// moves; planets revolve around it, and their current place is recomputed every 144// tick. cx, cy is the centre of the system the body belongs to, so a body is a 145// world point without anything else having to know about systems. 146type body struct { 147 ID int `json:"id"` 148 Kind string `json:"kind"` 149 R float64 `json:"r"` 150 X float64 `json:"x"` 151 Y float64 `json:"y"` 152 cx float64 153 cy float64 154 orbit float64 155 a0 float64 156 speed float64 157 wait float64 // seconds until a barren planet throws its next ore 158 cap int // how many ore this barren planet keeps in orbit 159} 160 161// A body's place is a pure function of world time: planets revolve around their 162// star, and the star (orbit 0) sits still at its system's centre. 163func (b *body) place(t float64) { 164 a := b.a0 + b.speed*t 165 b.X, b.Y = b.cx+math.Cos(a)*b.orbit, b.cy+math.Sin(a)*b.orbit 166} 167 168type player struct { 169 ID int `json:"id"` 170 Name string `json:"name"` 171 Side string `json:"side"` // police or rogue for a server hull, empty for a player 172 X float64 `json:"x"` 173 Y float64 `json:"y"` 174 H float64 `json:"h"` 175 TX float64 `json:"tx"` 176 TY float64 `json:"ty"` 177 Order bool `json:"order"` // the order is still pending, so it is still to be marked 178 Pieces []piece `json:"pieces"` 179 Fuel float64 `json:"fuel"` // what is in the tank, spent by flying 180 FuelMax float64 `json:"fuelmax"` // capacity, summed from the hull every tick 181 Foe int `json:"foe"` // ship this one is chasing, 0 = nobody 182 Target int `json:"target"` // piece of that ship the guns are pointed at 183 AI bool `json:"ai"` // a server-run hull, not a connection 184 185 home int // shipyard body a police hull was born at, and comes back to 186 spot int // rogue point a rogue patrols, as an index into hub.slots 187 hx, hy float64 // centre of a rogue's patrol circle, in world units 188 angle float64 // AI patrol angle around that yard or point 189 ring float64 // AI patrol radius around it 190 spin float64 // AI patrol angular speed, negative for the other way round 191 chase int // ship this one was ordered to close on, 0 = no order 192 cool float64 // seconds until the guns can fire again 193 vx, vy float64 194 orbit int // body the ship is anchored to, 0 = none 195 ox, oy float64 // ring point at anchoring, as an offset from the system centre 196 anchored float64 // world time of anchoring 197 held int // loose piece being dragged, 0 = none 198 tow []int // ore on the tow line, in order behind the ship 199 yard float64 // seconds until a shipyard will take another load 200} 201 202type client struct { 203 ws *websocket.Conn 204 p *player 205} 206 207// A wrecked AI is gone for a while, then its shipyard births a replacement. 208type grave struct { 209 home int 210 wait float64 211} 212 213// A rogue point is a gap between two stars on a galaxy ring, and it holds exactly 214// one rogue patrol: when that hull dies the point goes quiet, and births another 215// after a delay. So a ring never runs out of patrols and never gains a second one. 216type slot struct { 217 x, y float64 218 ring float64 // the galaxy ring the point sits on: it sets how big its hull is 219 wait float64 // seconds until it births a hull, 0 while one is alive 220} 221 222type hub struct { 223 mu sync.Mutex 224 clients map[*client]bool 225 nextID int 226 bodies []*body 227 systems []*system 228 spawns []int // shipyard bodies a new run may be born in orbit of, never at the rim 229 loose []*loose 230 ore []*ore 231 shots []*shot 232 ai []*player // server-run hulls, tracked whether or not anyone is connected 233 grave []*grave 234 slots []*slot // the rogue points of the galaxy, one patrol each 235 nextPiece int 236 time float64 237} 238 239type state struct { 240 T string `json:"t"` 241 You int `json:"you"` 242 Held int `json:"held"` 243 Bodies []*body `json:"bodies"` 244 Systems []*system `json:"systems"` 245 Loose []*loose `json:"loose"` 246 Ore []*ore `json:"ore"` 247 Shots []*shot `json:"shots"` 248 Players []*player `json:"players"` 249} 250 251func (h *hub) pieceID() int { 252 h.nextPiece++ 253 return h.nextPiece 254}