aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_trigger_utility.go
blob: 481cd40bcb14247b225f18a133c2e3ac264f8af8 (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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package game

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/item"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) triggerUtility(sess *net.Session, p *player.Player, mod *ModDef, targetArg string) {
	switch mod.ID {
	case "low_process", "high_process":
		g.triggerProcessing(sess, p, mod, targetArg)
	case "bones_to_nutrients":
		g.triggerBonesToNuts(sess, p, mod)
	case "em_grab":
		g.triggerEmGrab(sess, p, mod, targetArg)
	case "superheat":
		g.triggerSuperheat(sess, p, mod, targetArg)
	case "plank_make":
		g.triggerPlankMake(sess, p, mod, targetArg)
	}
}

func (g *Game) triggerProcessing(sess *net.Session, p *player.Player, mod *ModDef, targetArg string) {
	if g.Combat.Get(p.Name) != nil {
		sess.WriteLine("You can't do that during combat!")
		return
	}

	if targetArg == "" {
		sess.WriteLine(fmt.Sprintf("Usage: trigger %s <item>", strings.ReplaceAll(mod.ID, "_", " ")))
		return
	}

	slot, inv := g.findInventoryItem(p, targetArg)
	if slot < 0 {
		sess.WriteLine("You don't have that item.")
		return
	}

	itemDef, err := g.ItemStore.Load(inv.ItemID)
	if err != nil || itemDef.Value <= 0 {
		sess.WriteLine("That item has no value.")
		return
	}

	if p.Action != nil {
		g.cancelAction(p)
	}

	creditValue := itemDef.Value
	if mod.ID == "low_process" {
		creditValue = itemDef.Value / 2
		if creditValue < 1 {
			creditValue = 1
		}
	}

	// Refuse before consuming the item when the balance is at the cap:
	// grantCredits clamps, so processing would otherwise destroy the item's
	// value entirely.
	if creditValue > creditHeadroom(p) {
		sess.WriteLine("You can't carry any more credits.")
		return
	}

	if inv.Quantity > 1 {
		inv.Quantity--
	} else {
		p.SetInvSlot(slot, nil)
	}

	g.grantCredits(p, creditValue)

	g.triggerModReward(sess, p, mod, 1.0)

	sess.WriteLine(fmt.Sprintf("You process the %s. You receive %s credits.",
		g.itemColorize(sess, itemDef, itemDef.Name), g.colorize(sess, "currency_pickup", fmt.Sprint(creditValue))))
}

func (g *Game) triggerBonesToNuts(sess *net.Session, p *player.Player, mod *ModDef) {
	if g.Combat.Get(p.Name) != nil {
		sess.WriteLine("You can't do that during combat!")
		return
	}

	boneCount := p.CountItem("bones")
	if boneCount == 0 {
		sess.WriteLine("You don't have any bones.")
		return
	}

	if p.Action != nil {
		g.cancelAction(p)
	}

	for i := 0; i < 28; i++ {
		slot := p.InvSlot(i)
		if slot != nil && slot.ItemID == "bones" {
			slot.ItemID = "nutrient_bar"
		}
	}

	g.triggerModReward(sess, p, mod, float64(boneCount))

	sess.WriteLine(fmt.Sprintf("You convert %d bones into nutrient bars.", boneCount))
}

func (g *Game) triggerEmGrab(sess *net.Session, p *player.Player, mod *ModDef, targetArg string) {
	if targetArg == "" {
		sess.WriteLine("Grab what? Usage: trigger em grab <item>")
		return
	}

	// Reservation-aware: only items this player may take are considered, so
	// em grab cannot magnetize kill loot reserved for another player.
	groundItems := g.World.AccessibleGround(p.RoomID, p.Name)
	var matchedID string
	for itemID := range groundItems {
		def, _ := g.ItemStore.Load(itemID)
		if def != nil && def.MatchesName(targetArg) {
			matchedID = itemID
			break
		}
		if strings.HasPrefix(itemID, strings.ToLower(targetArg)) {
			matchedID = itemID
			break
		}
	}

	if matchedID == "" {
		sess.WriteLine("You don't see that here.")
		return
	}

	if p.Action != nil {
		g.cancelAction(p)
	}

	qty := groundItems[matchedID]
	def, _ := g.ItemStore.Load(matchedID)
	name := matchedID
	if def != nil {
		name = def.Name
	}

	// Credits are currency, not an inventory item: collect them the same way
	// the get command does, respecting the balance cap so the excess stays on
	// the ground instead of being removed and clamped away.
	if matchedID == "credits" {
		room := creditHeadroom(p)
		if room <= 0 {
			sess.WriteLine(g.colorize(sess, "currency_pickup", "Your purse can't hold any more credits."))
			return
		}
		taken := g.World.RemoveAvailableItem(p.RoomID, matchedID, room, p.Name)
		if taken <= 0 {
			return
		}
		g.grantCredits(p, taken)
		g.saveCharacter(p)
		g.triggerModReward(sess, p, mod, 1.0)
		sess.WriteLine(fmt.Sprintf("You magnetically pull %d credits toward you.", taken))
		return
	}

	if def != nil && def.Stackable {
		taken := g.World.RemoveAvailableItem(p.RoomID, matchedID, qty, p.Name)
		if taken <= 0 {
			sess.WriteLine("You don't see that here.")
			return
		}
		if !placeCraftOutput(p, matchedID, def, taken) {
			g.World.AddGroundItem(p.RoomID, matchedID, taken)
			sess.WriteLine("Your inventory is full.")
			return
		}
	} else {
		// Non-stackable items take one slot each; leave any remainder on the
		// ground instead of cramming N items into a single slot. Remove from
		// the ground first (reservation-aware) so a placement failure cannot
		// duplicate stock.
		taken, ok := g.World.RemoveReservedItem(p.RoomID, matchedID, qty, p.Name)
		if !ok || taken == 0 {
			sess.WriteLine("Your inventory is full.")
			return
		}
		placed := 0
		for placed < taken {
			freeSlot := p.FirstFreeSlot()
			if freeSlot < 0 {
				break
			}
			p.SetInvSlot(freeSlot, g.newInventorySlot(matchedID, 1))
			placed++
		}
		if placed < taken {
			g.World.AddGroundItem(p.RoomID, matchedID, taken-placed)
			sess.WriteLine(fmt.Sprintf("Your inventory fills up; %d %s remain on the ground.", taken-placed, name))
		}
	}

	g.triggerModReward(sess, p, mod, 1.0)
	sess.WriteLine(fmt.Sprintf("You magnetically pull the %s toward you.", g.itemColorize(sess, def, name)))
}

func (g *Game) triggerSuperheat(sess *net.Session, p *player.Player, mod *ModDef, targetArg string) {
	if g.Combat.Get(p.Name) != nil {
		sess.WriteLine("You can't do that during combat!")
		return
	}

	if targetArg == "" {
		sess.WriteLine("Superheat what? Usage: trigger superheat <ore>")
		return
	}

	slot, inv := g.findInventoryItem(p, targetArg)
	if slot < 0 {
		sess.WriteLine("You don't have that item.")
		return
	}

	var match *item.ItemDef
	for _, item := range g.CraftIndex.BySubtype("smelting") {
		if craftMatchesEntry(item.FirstCraft(), inv.ItemID) {
			match = item
			break
		}
	}
	if match == nil {
		sess.WriteLine("You can't superheat that.")
		return
	}
	c := match.FirstCraft()

	if c.Level > 0 && p.Level(player.SkillName(c.Type)) < c.Level {
		sess.WriteLine(fmt.Sprintf("You need level %d %s to smelt that.", c.Level, c.Type))
		return
	}

	// Availability resolves each entry the way consumeIngredients will: ONE
	// listed alternative must cover the entry's full quantity. The previous
	// hand-rolled loop required EVERY listed alternative, e.g. both a
	// bucket AND a jug of water to superheat clay, and could not name the
	// missing choice.
	for i := range c.Ingredients {
		id, qty := craftIngredientSource(&c.Ingredients[i], p.CountItem)
		if id != "" {
			continue
		}
		names := make([]string, 0, len(c.Ingredients[i].Items))
		for _, itemID := range c.Ingredients[i].Items {
			def, _ := g.ItemStore.Load(itemID)
			name := itemID
			if def != nil {
				name = def.Name
			}
			names = append(names, fmt.Sprintf("%d %s", qty, name))
		}
		sess.WriteLine(fmt.Sprintf("You need one of: %s.", strings.Join(names, " or ")))
		return
	}

	if p.Action != nil {
		g.cancelAction(p)
	}

	// Verify the output can be placed BEFORE consuming: a full inventory
	// must never destroy ingredients without producing the bar.
	if !craftOutputFits(p, match.ID, match) {
		sess.WriteLine("Your inventory is too full!")
		return
	}

	consumeIngredients(c, p.CountItem, p.RemoveItem)

	outputQty := c.OutputQty
	if outputQty <= 0 {
		outputQty = 1
	}
	placed := false
	for i := 0; i < 28; i++ {
		slot := p.InvSlot(i)
		if slot != nil && slot.ItemID == match.ID {
			slot.Quantity += outputQty
			placed = true
			break
		}
	}
	if !placed {
		freeSlot := p.FirstFreeSlot()
		if freeSlot < 0 {
			sess.WriteLine("Your inventory is too full!")
			return
		}
		p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: match.ID, Quantity: outputQty})
	}

	var extraGains []xpGain
	if c.XP > 0 {
		extraGains = []xpGain{{c.EffectiveSkill(), c.XP}}
	}
	g.triggerModReward(sess, p, mod, 1.0, extraGains...)

	outputName := match.Name
	inputDef, _ := g.ItemStore.Load(inv.ItemID)
	inputName := inv.ItemID
	if inputDef != nil {
		inputName = inputDef.Name
	}

	sess.WriteLine(fmt.Sprintf("You superheat the %s and produce a %s.", g.itemColorize(sess, inputDef, inputName), g.itemColorize(sess, match, outputName)))
}

func (g *Game) triggerPlankMake(sess *net.Session, p *player.Player, mod *ModDef, targetArg string) {
	if g.Combat.Get(p.Name) != nil {
		sess.WriteLine("You can't do that during combat!")
		return
	}

	logTypes := map[string]struct {
		plankID string
		name    string
		cost    int
	}{
		"logs":          {"planks", "planks", 3},
		"oak_logs":      {"oak_planks", "oak planks", 7},
		"teak_logs":     {"teak_planks", "teak planks", 14},
		"mahogany_logs": {"mahogany_planks", "mahogany planks", 28},
	}

	if targetArg != "" {
		slot, inv := g.findInventoryItem(p, targetArg)
		if slot < 0 {
			sess.WriteLine("You don't have that item.")
			return
		}
		info, ok := logTypes[inv.ItemID]
		if !ok {
			sess.WriteLine("You can't turn that into planks.")
			return
		}

		if p.Credits < info.cost {
			sess.WriteLine(fmt.Sprintf("You need %d credits for Plank Make (70%% of sawmill cost).", info.cost))
			return
		}

		plankDef, _ := g.ItemStore.Load(info.plankID)
		// Check placement BEFORE consuming the log and credits: a full
		// inventory must never delete materials without producing planks.
		if !craftOutputFits(p, info.plankID, plankDef) {
			sess.WriteLine("Your inventory is full.")
			return
		}

		if p.Action != nil {
			g.cancelAction(p)
		}

		logID := inv.ItemID
		p.RemoveItem(logID, 1)
		p.Credits -= info.cost

		if !placeCraftOutput(p, info.plankID, plankDef, 1) {
			// Defensive: craftOutputFits guaranteed placement. Refund the
			// log (to the ground, so it can never be lost) and the credits.
			p.Credits += info.cost
			g.World.AddGroundItem(p.RoomID, logID, 1)
			sess.WriteLine("Your inventory is full.")
			return
		}

		g.triggerModReward(sess, p, mod, 1.0, xpGain{Skill: string(player.Construction), XP: 10})

		sess.WriteLine(fmt.Sprintf("You convert the log into %s.", info.name))
		return
	}

	for logID, info := range logTypes {
		qty := p.CountItem(logID)
		if qty == 0 {
			continue
		}

		totalCost := info.cost * qty
		if p.Credits < totalCost {
			continue
		}

		if p.Action != nil {
			g.cancelAction(p)
		}

		p.RemoveItem(logID, qty)
		p.Credits -= totalCost

		processed := 0
		for i := 0; i < qty; i++ {
			freeSlot := p.FirstFreeSlot()
			if freeSlot == -1 {
				g.World.AddGroundItem(p.RoomID, info.plankID, qty-i)
				break
			}
			p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: info.plankID, Quantity: 1})
			processed++
		}

		g.triggerModReward(sess, p, mod, float64(qty), xpGain{Skill: string(player.Construction), XP: 10 * qty})

		sess.WriteLine(fmt.Sprintf("You convert %d logs into %s for %d credits.", processed, info.name, totalCost))
		if processed < qty {
			sess.WriteLine(fmt.Sprintf("Your inventory is full. The remaining %d planks fall to the ground.", qty-processed))
		}
		return
	}

	sess.WriteLine("You don't have any logs to convert.")
}