xonecas.ioDon't click! Sean Caetano Martin Sign in

space-browser / commits / 16e6302

feat: ai ship composition

xonecas5 weeks ago16e630223c96bed34d7910e19f868fd313622cacparent 9d4c3b86 files changed

server/ai.go+22-6
@@ -6,6 +6,27 @@ import (
66 "strconv"
77 )
88
9+// An AI hull is a root gun with a few pieces bolted around it. The size says how
10+// many pieces it carries; which piece goes on and which slot it lands in is drawn
11+// per ship, so two hulls of one size still differ. Every kind a player can fly
12+// turns up on a hull, because a wreck is the only place a piece comes from: a star
13+// is the one exception, since being craft-only at the rim is all it is.
14+func (h *hub) compose(size int) []piece {
15+ ps := []piece{h.fitted("triangle", 0)} // the root gun is what makes it dangerous
16+ for range size - 1 {
17+ host := ps[rand.Intn(len(ps))]
18+ free := freeSlots(ps, host)
19+ if len(free) == 0 {
20+ continue // this hull is boxed in, so it just carries less
21+ }
22+ q := h.fitted([]string{"triangle", "square", "circle"}[rand.Intn(3)], 0)
23+ a := float64(free[rand.Intn(len(free))]) / slots * 2 * math.Pi
24+ q.Parent, q.LX, q.LY = host.ID, host.LX+math.Cos(a)*spacing, host.LY+math.Sin(a)*spacing
25+ ps = append(ps, q)
26+ }
27+ return ps
28+}
29+
930 // A passive AI hull: born on a shipyard's ring, it picks an orbit line of its own
1031 // and circles the yard. It is nobody's enemy until someone picks a fight with it.
1132 func (h *hub) spawnAI(b *body) *player {
@@ -19,12 +40,7 @@ func (h *hub) spawnAI(b *body) *player {
1940 if rand.Intn(2) == 0 {
2041 p.spin = -p.spin
2142 }
22- gun := h.fitted("triangle", 0)
23- plate := h.fitted("square", 0)
24- plate.Parent, plate.LX = gun.ID, spacing
25- other := h.fitted("triangle", 0)
26- other.Parent, other.LY = gun.ID, spacing
27- p.Pieces = []piece{gun, plate, other}
43+ p.Pieces = h.compose(aiMin + rand.Intn(aiMax-aiMin+1))
2844 p.FuelMax = p.capacity()
2945 p.Fuel = p.FuelMax
3046 return p
server/hull.go+21-12
@@ -130,6 +130,26 @@ func (h *hub) corpse(p *player) {
130130 h.abandon(p)
131131 }
132132
133+// The slots around a piece that nothing is bolted into yet.
134+func freeSlots(ps []piece, p piece) []int {
135+ var free []int
136+ for i := range slots {
137+ a := float64(i) / slots * 2 * math.Pi
138+ x, y := p.LX+math.Cos(a)*spacing, p.LY+math.Sin(a)*spacing
139+ taken := false
140+ for _, q := range ps {
141+ if math.Hypot(q.LX-x, q.LY-y) < spacing*0.5 {
142+ taken = true
143+ break
144+ }
145+ }
146+ if !taken {
147+ free = append(free, i)
148+ }
149+ }
150+ return free
151+}
152+
133153 // Snap a dropped piece onto the free slot closest to where it was let go.
134154 func (h *hub) attach(p *player, l *loose) bool {
135155 lx, ly := hullToLocal(p, l.X, l.Y)
@@ -141,19 +161,8 @@ func (h *hub) attach(p *player, l *loose) bool {
141161 }
142162 for _, q := range near {
143163 slot, best := -1, math.MaxFloat64
144- for i := 0; i < slots; i++ {
164+ for _, i := range freeSlots(p.Pieces, q) {
145165 a := float64(i) / slots * 2 * math.Pi
146- x, y := q.LX+math.Cos(a)*spacing, q.LY+math.Sin(a)*spacing
147- taken := false
148- for _, r := range p.Pieces {
149- if math.Hypot(r.LX-x, r.LY-y) < spacing*0.5 {
150- taken = true
151- break
152- }
153- }
154- if taken {
155- continue // slot is occupied
156- }
157166 if d := math.Abs(wrap(a - math.Atan2(ly-q.LY, lx-q.LX))); d < best {
158167 slot, best = i, d
159168 }
server/intents.go+3-1
@@ -8,7 +8,9 @@ import (
88 // A click is resolved by the server: grab a piece, tear one off, engage a ship,
99 // park on a body, or fly to a point. The client never decides which happened.
1010 func (h *hub) pick(p *player, x, y float64) {
11- if l := h.freeLooseAt(x, y); l != nil {
11+ // A piece is picked up by hand, so the hull has to be at it: a click on
12+ // wreckage across the system is only a move order.
13+ if l := h.freeLooseAt(x, y); l != nil && math.Hypot(l.X-p.X, l.Y-p.Y) < grabR {
1214 p.held, l.held = l.ID, p.ID
1315 return
1416 }
server/main.go+2-6
@@ -30,7 +30,8 @@ func main() {
3030 }
3131
3232 func newHub() *hub {
33- h := &hub{clients: map[*client]bool{}, ore: []*ore{}}
33+ h := &hub{clients: map[*client]bool{}, loose: []*loose{}, ore: []*ore{}}
34+ // Empty, not nil: a nil slice marshals as null, and the client walks these.
3435 // One hand-tuned system: a star, a shipyard planet and a barren planet, both
3536 // revolving. The real galaxy generator replaces this later.
3637 h.bodies = []*body{
@@ -49,11 +50,6 @@ func newHub() *hub {
4950 h.ai = append(h.ai, h.spawnAI(b))
5051 }
5152 }
52- // Wreckage around the spawn, to build a first hull out of.
53- for i, k := range []string{"square", "circle", "star", "triangle"} {
54- h.loose = append(h.loose, h.debris(k, 0,
55- spawnX+[]float64{30, 37, -29, -35}[i], spawnY+[]float64{-20, 18, 26, -22}[i]))
56- }
5753 return h
5854 }
5955
server/stats.go+0-5
@@ -20,8 +20,3 @@ func (h *hub) fitted(kind string, tier int) piece {
2020 hp, _ := stats(kind, tier)
2121 return piece{ID: h.pieceID(), Kind: kind, HP: hp, MaxHP: hp, Tier: tier}
2222 }
23-
24-func (h *hub) debris(kind string, tier int, x, y float64) *loose {
25- hp, _ := stats(kind, tier)
26- return &loose{ID: h.pieceID(), Kind: kind, X: x, Y: y, HP: hp, MaxHP: hp, Tier: tier}
27-}
server/world.go+2-0
@@ -50,6 +50,8 @@ const (
5050
5151 aiPerYard = 2 // passive AI ships a shipyard keeps in orbit
5252 aiDelay = 25.0 // seconds after an AI death before that yard births another
53+ aiMin = 1 // ship pieces the smallest AI hull is born with
54+ aiMax = 2 // and the largest this core test system fields
5355 patrolSpin = 0.18 // radians a second an AI circles its yard on patrol
5456 patrolLow = 6.0 // the tightest patrol line sits this far beyond the standoff
5557 patrolHigh = 30.0 // and the widest this much further out again