xonecas.ioDon't click! Sean Caetano Martin Sign in
space-browser / server / ai.go 167 lines5.4 kB Raw
1package main
2
3import (
4	"math"
5	"math/rand"
6	"strconv"
7)
8
9// An AI hull is a root gun with a few pieces bolted around it. The size says how
10// many pieces it carries and the tier how good they are; which piece goes on and
11// which slot it lands in is drawn per ship, so two hulls of one size still differ.
12// Every kind a player can fly turns up on a hull, because a wreck is the only place
13// a piece comes from: a star is the one exception, since being craft-only at the rim
14// is all it is.
15func (h *hub) compose(size, tier int) []piece {
16	ps := []piece{h.fitted("triangle", tier)} // the root gun is what makes it dangerous
17	for range size - 1 {
18		host := ps[rand.Intn(len(ps))]
19		free := freeSlots(ps, host)
20		if len(free) == 0 {
21			continue // this hull is boxed in, so it just carries less
22		}
23		q := h.fitted([]string{"triangle", "square", "circle"}[rand.Intn(3)], tier)
24		a := float64(free[rand.Intn(len(free))]) / slots * 2 * math.Pi
25		q.Parent, q.LX, q.LY = host.ID, host.LX+math.Cos(a)*spacing, host.LY+math.Sin(a)*spacing
26		ps = append(ps, q)
27	}
28	return ps
29}
30
31// A police hull: born on a shipyard's ring, it picks an orbit line of its own and
32// circles the yard. It is nobody's enemy until someone picks a fight with it.
33func (h *hub) spawnAI(b *body) *player {
34	a := rand.Float64() * 2 * math.Pi
35	r := b.R + standoff
36	h.nextID++
37	p := &player{ID: h.nextID, Name: "AI" + strconv.Itoa(h.nextID), AI: true, Side: sidePolice, home: b.ID,
38		X: b.X + math.Cos(a)*r, Y: b.Y + math.Sin(a)*r,
39		angle: a, ring: r + patrolLow + rand.Float64()*patrolHigh,
40		spin: patrolSpin * (0.8 + 0.5*rand.Float64())}
41	if rand.Intn(2) == 0 {
42		p.spin = -p.spin
43	}
44	p.Pieces = h.compose(aiMin+rand.Intn(aiMax-aiMin+1), 0)
45	p.FuelMax = p.capacity()
46	p.Fuel = p.FuelMax
47	return p
48}
49
50// A rogue hull: born on the patrol circle of its point, sized and built by the ring
51// it patrols, so the middle ring fields small hulls and the rim fields packs.
52func (h *hub) spawnRogue(i int) *player {
53	s := h.slots[i]
54	n, top := rogueMidPieces+rand.Intn(rogueMidVar), rogueMidTier
55	if s.ring >= rimRing-1 {
56		n, top = rogueRimPieces+rand.Intn(rogueRimVar), rogueRimTier
57	}
58	a := rand.Float64() * 2 * math.Pi
59	h.nextID++
60	p := &player{ID: h.nextID, Name: "ROGUE" + strconv.Itoa(h.nextID), AI: true, Side: sideRogue,
61		spot: i, hx: s.x, hy: s.y, angle: a,
62		ring: rogueRingLow + rand.Float64()*rogueRingVar,
63		spin: patrolSpin * (0.8 + 0.5*rand.Float64())}
64	if rand.Intn(2) == 0 {
65		p.spin = -p.spin
66	}
67	p.X, p.Y = s.x+math.Cos(a)*p.ring, s.y+math.Sin(a)*p.ring
68	p.H = a
69	p.Pieces = h.compose(n, rand.Intn(top+1))
70	p.FuelMax = p.capacity()
71	p.Fuel = p.FuelMax
72	return p
73}
74
75// A police hull circles its yard and runs down whoever hit it, and a rogue hull
76// watches the gap it was posted in. Both keep to the ground they were given.
77func (h *hub) think(p *player, dt float64) {
78	if p.Side == sideRogue {
79		h.maraud(p, dt)
80		return
81	}
82	p.angle += p.spin * dt
83	if p.Foe != 0 {
84		p.orbit, p.chase = 0, p.Foe
85		return // the chase order below flies it into gun range
86	}
87	if b := h.body(p.home); b != nil {
88		p.TX, p.TY = b.X+math.Cos(p.angle)*p.ring, b.Y+math.Sin(p.angle)*p.ring
89	}
90}
91
92// A rogue opens fire on anything that is not a rogue the moment it can shoot it, so
93// the gun range ring is the whole of its warning. It runs that ship down until one
94// of them is dead, but only that far from its own point: every hull flies at the
95// same top speed, so a chase with no leash would follow a fleeing player across the
96// galaxy forever and break the promise that deep space is not their ground.
97func (h *hub) maraud(p *player, dt float64) {
98	if o := h.player(p.Foe); o != nil && hostile(p, o) && math.Hypot(o.X-p.hx, o.Y-p.hy) < rogueLeash {
99		p.chase = p.Foe
100		return // the chase order below flies it into gun range
101	}
102	p.Foe, p.Target, p.chase = 0, 0, 0 // the prey is gone or out of reach: back to the patrol
103	if t := h.prey(p); t != nil {
104		p.Foe, p.chase = t.ID, t.ID
105		return
106	}
107	p.angle += p.spin * dt
108	p.TX, p.TY = p.hx+math.Cos(p.angle)*p.ring, p.hy+math.Sin(p.angle)*p.ring
109}
110
111// The nearest ship a rogue may shoot, which is anything that is not a rogue, and
112// only once it is close enough for its own guns: nothing is aggroed from off screen.
113func (h *hub) prey(p *player) *player {
114	var got *player
115	best := gunRange
116	for _, o := range h.players() {
117		if o == p || !hostile(p, o) {
118			continue
119		}
120		if d := math.Hypot(o.X-p.X, o.Y-p.Y); d < best {
121			best, got = d, o
122		}
123	}
124	return got
125}
126
127// A wrecked AI leaves its hull in space like anyone else. A yard births a police
128// replacement after a delay and a rogue point does the same, so neither runs dry.
129func (h *hub) bury(p *player) {
130	h.corpse(p)
131	for i, q := range h.ai {
132		if q == p {
133			h.ai = append(h.ai[:i], h.ai[i+1:]...)
134			break
135		}
136	}
137	if p.Side == sideRogue {
138		h.slots[p.spot].wait = rogueDelay
139		return
140	}
141	h.grave = append(h.grave, &grave{home: p.home, wait: aiDelay})
142}
143
144// Graves count down, then birth a new hull in the same shipyard's orbit, and a
145// silent rogue point births a new patrol on its own circle.
146func (h *hub) stepAI(dt float64) {
147	live := h.grave[:0]
148	for _, g := range h.grave {
149		if g.wait -= dt; g.wait > 0 {
150			live = append(live, g)
151			continue
152		}
153		if b := h.body(g.home); b != nil {
154			h.ai = append(h.ai, h.spawnAI(b))
155		}
156	}
157	h.grave = live
158	for i, s := range h.slots {
159		if s.wait <= 0 {
160			continue // one is alive and patrolling
161		}
162		if s.wait -= dt; s.wait > 0 {
163			continue
164		}
165		h.ai = append(h.ai, h.spawnRogue(i))
166	}
167}