aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_verbs.go
blob: 610297801ac3d7b587cb0c3463e096e0b1070d74 (plain)
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
package game

import (
	"strings"

	"thehouseoficarus/internal/behavior"
	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/world"
)

var alwaysAvailable = []string{
	"look",
	"say",
	"global",
	"who",
	"exits",
	"map",
	"walk",
	"score", "stats",
	"inventory",
	"equip",
	"remove",
	"style",
	"tech",
	"option",
	"color", "colortable",
	"prompt",
	"alias", "unalias",
	"help",
	"queued",
	"stop",
	"aps",
	"autotrigger",
	"trigger",
	"sneak",
	"mods",
	"identify",
	"eat", "drink",
	"fletch",
	"clean",
	"burn", "stoke",
	"search",
	"get",
	"drop",
}

// roomCommandSections holds the interactable commands advertised for the
// player's current room, before rendering. It is shared by the verbs display
// and by prefix resolution, so an abbreviation can only beat a same-prefix
// rival when the player can actually use it here.
type roomCommandSections struct {
	objects []string
	mobs    []string
	exits   []string
	ground  []string
}

// collectRoomCommands builds the room's object, mob, exit and ground command
// listings without writing anything to the session.
func (g *Game) collectRoomCommands(sess *net.Session) roomCommandSections {
	p := sess.Player
	var rc roomCommandSections
	seen := make(map[string]bool)

	for _, st := range g.World.AllObjInstances(p.RoomID) {
		def, _, err := g.resolveObjectDef(p.RoomID, st.DefID)
		if err != nil || (def.Hidden && !p.GodMode) {
			continue
		}
		name := strings.ToLower(def.Name)

		bt := def.BehaviorType()

		if bt == "gather" && def.Gather != nil {
			for verb, skill := range verbSkill {
				if strings.EqualFold(def.Gather.Skill, skill) {
					addUnique(&rc.objects, &seen, verb+" "+name)
				}
			}
		}

		if bt == "talk" {
			addUnique(&rc.objects, &seen, "talk "+name)
		}

		for _, ui := range def.OnUse {
			if ui.ItemID == "" && (ui.Condition == nil || g.checkCondition(sess, ui.Condition)) {
				addUnique(&rc.objects, &seen, "use "+name)
				break
			}
		}

		recipes := g.stationRecipes(p, def.ID)
		if len(recipes) > 0 {
			addUnique(&rc.objects, &seen, "use "+name)
			for _, r := range recipes {
				c := r.FirstCraft()
				key := c.Subtype
				if key == "" {
					key = c.Type
				}
				if info, ok := productionTypes[key]; ok {
					if info.ActionType != behavior.TypeCombine && info.ActionType != behavior.TypeFletch && info.ActionType != behavior.TypeMix {
						addUnique(&rc.objects, &seen, string(info.ActionType))
					}
				}
				for _, c := range r.FirstCraft().Ingredients {
					for _, itemID := range c.Items {
						if itemDef, err := g.ItemStore.Load(itemID); err == nil {
							addUnique(&rc.objects, &seen, "use "+itemDef.Name+" on "+name)
						}
					}
				}
			}
		}

		for _, ui := range def.OnUse {
			if ui.ItemID != "" && (ui.Condition == nil || g.checkCondition(sess, ui.Condition)) {
				if itemDef, err := g.ItemStore.Load(ui.ItemID); err == nil {
					addUnique(&rc.objects, &seen, "use "+itemDef.Name+" on "+name)
				}
			}
		}

		if def.Steal != nil && len(def.Steal.Drops) > 0 {
			addUnique(&rc.objects, &seen, "steal "+name)
		}

		addUnique(&rc.objects, &seen, "look "+name)
	}

	for _, m := range g.MobStore.MobsInRoom(p.RoomID) {
		mName := strings.ToLower(m.Name)

		if !m.Protected {
			addUnique(&rc.mobs, &seen, "attack "+mName)
		}
		if m.IsTalkable() {
			addUnique(&rc.mobs, &seen, "talk "+mName)
		}
		if len(m.StealDrops) > 0 {
			addUnique(&rc.mobs, &seen, "steal "+mName)
		}
		addUnique(&rc.mobs, &seen, "look "+mName)
	}

	if room, err := g.World.LoadRoom(p.RoomID); err == nil {
		var exitDirs []string
		for _, dir := range world.ExitOrder {
			ed, ok := room.Exits[dir]
			if !ok {
				continue
			}
			if !g.exitDisplayState(sess, p.RoomID, dir, ed).Visible {
				continue
			}
			exitDirs = append(exitDirs, string(dir))
		}
		if len(exitDirs) > 0 {
			rc.exits = append(rc.exits, strings.Join(exitDirs, ", "))
		}
	}

	ground := g.World.GroundItems(p.RoomID)
	if len(ground) > 0 {
		for itemID := range ground {
			if def, err := g.ItemStore.Load(itemID); err == nil {
				addUnique(&rc.ground, &seen, "get "+def.Name)
			}
		}
		addUnique(&rc.ground, &seen, "get all")
	}

	if g.CourseStore != nil {
		if info := g.CourseStore.GetObstacle(p.RoomID); info != nil {
			addUnique(&rc.ground, &seen, info.Verb)
		}
	}

	if g.canUseBank(p) {
		addUnique(&rc.objects, &seen, "bank")
	}

	if g.findTerminalInRoom(p.RoomID) != "" {
		addUnique(&rc.objects, &seen, "jack")
	}

	return rc
}

func (g *Game) executeVerbs(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	if sess.CryptoActive && g.atCryptoCounter(p) {
		g.showCasinoHints(sess, g.cryptoCommands(sess))
		return
	}
	if sess.PredictionActive && g.atPredictionCounter(p) {
		g.showCasinoHints(sess, g.predictionCommands(sess))
		return
	}
	// A seated casino player has no room verbs: the only valid commands are
	// the game's live hints, so what/verbs shows those instead of exits and
	// objects that cannot be used mid-hand.
	if g.Casino != nil {
		if table, playing := g.Casino.Participation(p.Name); playing {
			if table != nil {
				g.showCasinoHints(sess, table.Hints(p.Name))
			} else if machine, ok := g.Casino.MachineFor(p.Name); ok {
				g.showCasinoHints(sess, machine.Hints())
			}
			return
		}
	}
	showAll := len(args) == 1 && strings.ToLower(args[0]) == "all"

	rc := g.collectRoomCommands(sess)

	hasRoomCmds := len(rc.objects) > 0 || len(rc.mobs) > 0 || len(rc.exits) > 0 || len(rc.ground) > 0

	if hasRoomCmds {
		if len(rc.objects) > 0 {
			sess.WriteLine("Objects:")
			writeCmdLines(sess, rc.objects)
		}
		if len(rc.mobs) > 0 {
			sess.WriteLine("Mobs:")
			writeCmdLines(sess, rc.mobs)
		}
		if len(rc.exits) > 0 {
			sess.WriteLine("Exits:")
			writeCmdLines(sess, rc.exits)
		}
		if len(rc.ground) > 0 {
			sess.WriteLine("Ground:")
			writeCmdLines(sess, rc.ground)
		}
	} else {
		sess.WriteLine("There is nothing notable to interact with here.")
	}

	if showAll {
		sess.WriteLine("")
		sess.WriteLine("Always available:")
		writeCmdLineStrings(sess, alwaysAvailable)
	} else {
		sess.WriteLine("")
		sess.WriteLine(`Type "verbs all" to see all available commands.`)
	}
}

// showCasinoHints prints the live command hints for a seated player. It is
// the casino branch of the what/verbs command, so a casino never lists room
// exits that are not valid options while seated.
func (g *Game) showCasinoHints(sess *net.Session, commands []casino.MenuCommand) {
	if len(commands) == 0 {
		sess.WriteLine(g.colorize(sess, "casino_action", "No commands are available right now."))
		return
	}
	sess.WriteLine(g.casinoMenuCommands(sess, commands))
}

func addUnique(list *[]string, seen *map[string]bool, cmd string) {
	if (*seen)[cmd] {
		return
	}
	(*seen)[cmd] = true
	*list = append(*list, cmd)
}

func writeCmdLines(sess *net.Session, cmds []string) {
	wrapCmdLines(sess, cmds, "  ")
}

func writeCmdLineStrings(sess *net.Session, cmds []string) {
	wrapCmdLines(sess, cmds, "  ")
}

func wrapCmdLines(sess *net.Session, cmds []string, prefix string) {
	width := 70
	if sess.Player != nil {
		if w := sess.Player.OptionInt("wrap_width"); w > 0 {
			width = w
		}
	}
	var current string
	for _, cmd := range cmds {
		if current == "" {
			current = prefix + cmd
		} else if len(current)+len(cmd)+2 <= width {
			current += ", " + cmd
		} else {
			sess.WriteLine(current)
			current = prefix + cmd
		}
	}
	if current != "" {
		sess.WriteLine(current)
	}
}