-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
347 lines (313 loc) · 9.43 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"fmt"
"math/rand"
"sort"
"strconv"
"strings"
"time"
)
var commandRegistry map[string]*Command
type Command struct {
name string
help string
handler func(*Connection, ...string)
mobile bool
}
var infoCommand = &Command{
name: "info",
help: "gives you some info about your current position",
handler: func(conn *Connection, args ...string) {
fmt.Fprintf(conn, "current planet: %s\n", conn.System().name)
fmt.Fprintf(conn, "bombs: %d\n", conn.bombs)
fmt.Fprintf(conn, "money: %d space duckets\n", conn.money)
},
}
var nearbyCommand = &Command{
name: "nearby",
help: "list objects nearby",
handler: func(conn *Connection, args ...string) {
system := conn.System()
neighbors, err := system.Nearby(25)
if err != nil {
log_error("unable to get neighbors: %v", err)
return
}
fmt.Fprintf(conn, "--------------------------------------------------------------------------------\n")
fmt.Fprintf(conn, "%-4s %-20s %s\n", "id", "name", "travel time")
fmt.Fprintf(conn, "--------------------------------------------------------------------------------\n")
for _, neighbor := range neighbors {
other := index[neighbor.id]
fmt.Fprintf(conn, "%-4d %-20s %v\n", other.id, other.name, system.TravelTimeTo(other))
}
fmt.Fprintf(conn, "--------------------------------------------------------------------------------\n")
},
}
var helpCommand = &Command{
name: "help",
help: "helpful things to help you",
handler: func(conn *Connection, args ...string) {
msg := `
Star Dragons is a stupid name, but it's the name that Brian suggested. It has
nothing to do with Dragons.
Anyway, Star Dragons is a game of cunning text-based, real-time strategy. You
play as some kind of space-faring entity, faring space in your inspecific
space-faring vessel. If you want a big one, it's big; if you want a small one,
it's small. If you want a pink one, it's pink, if you want a black one, it's
black. And so on, and so forth. It is the space craft of your dreams. Or
perhaps you are one of those insect-like alien races and you play as the queen.
Yeah, that's the ticket! You're the biggest baddest queen bug in space.
In Star Dragons, you issue your spacecraft (which is *not* called a Dragon)
textual commands to control it. The objective of the game is to be the first
person or alien or bug or magical space ponycorn to eradicate three enemy
species. Right now that is the only win condition.
All of the systems present in Star Dragons are named and positioned after known
exoplanet systems. When attempting to communicate from one star system to
another, it takes time for the light of your message to reach the other star
systems. Star systems that are farther away take longer to communicate with.
`
msg = strings.TrimSpace(msg)
fmt.Fprintln(conn, msg)
if len(args) == 0 {
fmt.Fprintln(conn, `use the "commands" command for a list of commands.`)
fmt.Fprintln(conn, `use "help [command-name]" to get info for a specific command.`)
return
}
for _, cmdName := range args {
cmd, ok := commandRegistry[cmdName]
if !ok {
fmt.Fprintf(conn, "no such command: %v\n", cmdName)
continue
}
fmt.Fprintf(conn, "%v: %v\n", cmdName, cmd.help)
}
},
}
var commandsCommand = &Command{
name: "commands",
help: "gives you a handy list of commands",
handler: func(conn *Connection, args ...string) {
names := make([]string, 0, len(commandRegistry))
for name, _ := range commandRegistry {
names = append(names, name)
}
sort.Strings(names)
fmt.Fprintln(conn, "--------------------------------------------------------------------------------")
for _, name := range names {
cmd := commandRegistry[name]
fmt.Fprintf(conn, "%-16s %s\n", name, cmd.help)
}
fmt.Fprintln(conn, "--------------------------------------------------------------------------------")
},
}
var scanCommand = &Command{
name: "scan",
help: "super duper scan",
handler: func(conn *Connection, args ...string) {
if !conn.CanScan() {
fmt.Fprintf(conn, "scanners are still recharging. Can scan again in %v\n", conn.NextScan())
return
}
conn.RecordScan()
system := conn.System()
log_info("scan sent from %s", system.name)
for id, _ := range index {
if id == system.id {
continue
}
delay := system.LightTimeTo(index[id])
id2 := id
After(delay, func() {
scanSystem(id2, system.id)
})
}
},
}
var broadcastCommand = &Command{
name: "broadcast",
help: "broadcast a message for all systems to hear",
handler: func(conn *Connection, args ...string) {
msg := strings.Join(args, " ")
system := conn.System()
log_info("broadcast sent from %s: %v\n", system.name, msg)
for id, _ := range index {
if id == system.id {
continue
}
delay := system.LightTimeTo(index[id])
id2 := id
After(delay, func() {
deliverMessage(id2, system.id, msg)
})
}
},
}
var gotoCommand = &Command{
name: "goto",
help: "moves to a different system, specified by either name or ID",
handler: func(conn *Connection, args ...string) {
dest_name := strings.Join(args, " ")
to, ok := nameIndex[dest_name]
if ok {
move(conn, to)
return
}
id_n, err := strconv.Atoi(dest_name)
if err != nil {
fmt.Fprintf(conn, `hmm, I don't know a system by the name "%s", try something else`, dest_name)
return
}
to, ok = index[id_n]
if !ok {
fmt.Fprintf(conn, `oh dear, there doesn't seem to be a system with id %d`, id_n)
return
}
move(conn, to)
},
}
var mineCommand = &Command{
name: "mine",
help: "mines the current system for resources",
handler: func(conn *Connection, args ...string) {
conn.StartMining()
var fn func()
fn = func() {
if !conn.IsMining() {
return
}
conn.Payout()
After(500*time.Millisecond, fn)
}
After(500*time.Millisecond, fn)
},
}
var colonizeCommand = &Command{
name: "colonize",
help: "establishes a mining colony on the current system",
handler: func(conn *Connection, arg ...string) {
system := conn.System()
var fn func()
fn = func() {
reward := int64(rand.NormFloat64()*5.0 + 100.0*system.miningRate)
if system.colonizedBy != nil {
system.colonizedBy.Deposit(reward)
fmt.Fprintf(system.colonizedBy, "mining colony on %s pays you %d space duckets. total: %d space duckets.\n", system.name, reward, system.colonizedBy.money)
}
After(5*time.Second, fn)
}
if system.colonizedBy != nil {
system.colonizedBy = conn
After(5*time.Second, fn)
return
}
if conn.money > 2000 {
conn.Withdraw(2000)
system.colonizedBy = conn
fmt.Fprintf(conn, "set up a mining colony on %s\n", conn.System().name)
After(5*time.Second, fn)
} else {
fmt.Fprintf(conn, "not enough money! it costs 2000 duckets to start a mining colony\n")
}
},
}
func move(conn *Connection, to *System) {
start := conn.System()
start.Leave(conn)
delay := start.TravelTimeTo(to)
fmt.Fprintf(conn, "moving to %s. ETA: %v\n", to.name, delay)
After(delay, func() {
to.Arrive(conn)
fmt.Fprintf(conn, "You have arrived at the %s system after a total travel time of %v.\n", to.name, delay)
})
}
var bombCommand = &Command{
name: "bomb",
help: "bombs a system, with a big space bomb",
handler: func(conn *Connection, args ...string) {
if conn.bombs < 1 {
fmt.Fprintf(conn, "no more bombs left! build more bombs!\n")
return
}
dest_name := strings.Join(args, " ")
to, ok := nameIndex[dest_name]
if ok {
bomb(conn, to)
return
}
id_n, err := strconv.Atoi(dest_name)
if err != nil {
fmt.Fprintf(conn, `hmm, I don't know a system by the name "%s", try something else\n`, dest_name)
return
}
to, ok = index[id_n]
if !ok {
fmt.Fprintf(conn, `oh dear, there doesn't seem to be a system with id %d\n`, id_n)
return
}
if !conn.CanBomb() {
fmt.Fprintf(conn, "weapons are still reloading. Can bomb again in %v\n", conn.NextBomb())
return
}
bomb(conn, to)
},
}
var mkBombCommand = &Command{
name: "mkbomb",
help: "make a bomb. Costs 500 space duckets",
handler: func(conn *Connection, args ...string) {
if conn.money < 500 {
fmt.Fprintf(conn, "not enough money! Bombs cost 500 space duckets to build, you only have %d in the bank.\n", conn.money)
return
}
conn.Withdraw(500)
conn.bombs += 1
fmt.Fprintf(conn, "built a bomb!\n")
fmt.Fprintf(conn, "bombs: %d\n", conn.bombs)
fmt.Fprintf(conn, "money: %d space duckets\n", conn.money)
},
}
func bomb(conn *Connection, to *System) {
conn.bombs -= 1
delay := conn.System().BombTimeTo(to)
fmt.Fprintf(conn, "sending bomb to %s. ETA: %v\n", to.name, delay)
After(delay, func() {
to.Bombed(conn)
})
}
func isCommand(name string) bool {
_, ok := commandRegistry[name]
return ok
}
func runCommand(conn *Connection, name string, args ...string) {
cmd, ok := commandRegistry[name]
if !ok {
fmt.Fprintf(conn, "no such command: %s\n", name)
return
}
if conn.dead {
fmt.Fprintf(conn, "you're dead.\n")
return
}
if conn.InTransit() && !cmd.mobile {
fmt.Fprintf(conn, "command %s can not be used while in transit", name)
return
}
cmd.handler(conn, args...)
}
func registerCommand(c *Command) {
commandRegistry[c.name] = c
}
func init() {
commandRegistry = make(map[string]*Command, 16)
registerCommand(bombCommand)
registerCommand(broadcastCommand)
registerCommand(colonizeCommand)
registerCommand(commandsCommand)
registerCommand(gotoCommand)
registerCommand(helpCommand)
registerCommand(infoCommand)
registerCommand(mineCommand)
registerCommand(nearbyCommand)
registerCommand(scanCommand)
registerCommand(mkBombCommand)
}