aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_eat.go
blob: 6a7497989baa3d17c52d7d2bd72a7848aedc78b2 (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
package game

import (
	"fmt"
	"time"

	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/object"
	"thehouseoficarus/internal/player"
)

func (g *Game) doEat(sess *net.Session, input string) {
	p := sess.Player.(*player.Player)

	if input == "" {
		sess.WriteLine("Eat what?")
		return
	}

	qty, itemName := parseQty(input)
	_ = qty

	matches := g.findInventoryMatches(itemName, p)
	if len(matches) == 0 {
		sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", itemName))
		return
	}

	unique := uniqueItemNames(matches)
	if len(unique) > 1 {
		g.showWhichOne(sess, matches)
		return
	}

	itemID := matches[0].ID
	def, _ := g.ItemStore.Load(itemID)

	if def == nil || def.EatMessage == "" {
		sess.WriteLine("You can't eat that.")
		return
	}

	if p.ConsumeCooldown > 0 {
		sess.WriteLine("You're still digesting your last meal.")
		return
	}

	g.cancelRest(p.Name)

	if p.Action == nil && len(p.WalkSequence) == 0 {
		g.doEatNow(sess, p, itemID, def)
		return
	}

	g.CancelAction(p)

	g.consumeQueue[p.Name] = &QueuedCommand{
		Session:   sess,
		Command:   "eat",
		Args:      itemID,
		Timestamp: time.Now(),
	}

	if !p.OptionBool("queue_silently") {
		sess.WriteLine(fmt.Sprintf("\nYou prepare to eat %s.", g.itemColorize(sess, def, def.Name)))
	}
}

func (g *Game) doEatNow(sess *net.Session, p *player.Player, itemID string, def *object.ItemDef) {
	if def == nil {
		var err error
		def, err = g.ItemStore.Load(itemID)
		if err != nil || def.EatMessage == "" {
			return
		}
	}

	p.RemoveItem(itemID, 1)

	p.HP += def.HealValue
	maxHP := p.MaxHP()
	if p.HP > maxHP {
		p.HP = maxHP
	}
	if p.HP < 0 {
		p.HP = 0
	}

	if def.HealValue != 0 {
		p.StartRegen()
	}

	p.ConsumeCooldown = 3
	g.AccountStore.SaveCharacter(p)

	p.ActionState = &ActionState{Type: ActionEating, TargetName: def.Name}

	sess.WriteLine(g.colorize(sess, "eat_food", def.EatMessage))

	if p.HP <= 0 {
		g.endCombat(sess, p, nil)
	}
}

func (g *Game) processConsumeQueue(p *player.Player, sess *net.Session) bool {
	qc, ok := g.consumeQueue[p.Name]
	if !ok {
		return false
	}
	delete(g.consumeQueue, p.Name)

	itemID := qc.Args
	def, err := g.ItemStore.Load(itemID)
	if err != nil || def.EatMessage == "" {
		return false
	}

	if p.ConsumeCooldown > 0 {
		return false
	}

	if !p.HasItem(itemID) {
		sess.WriteLine("You no longer have that to eat.")
		return false
	}

	g.doEatNow(sess, p, itemID, def)
	return true
}