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

import (
	"fmt"
	"strings"
	"time"

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

func (g *Game) doDrink(sess *net.Session, args []string) {
	p := sess.Player.(*player.Player)

	if len(args) == 0 {
		sess.WriteLine("Drink what?")
		return
	}

	input := strings.Join(args, " ")
	matches := g.findInventoryMatches(input, p)
	if len(matches) == 0 {
		sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", input))
		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 {
		sess.WriteLine("Something went wrong.")
		return
	}

	if def.PotionEffect == "" && def.HealValue <= 0 {
		sess.WriteLine(fmt.Sprintf("You can't drink the %s.", g.itemColorize(sess, def, def.Name)))
		return
	}

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

	g.cancelRest(p.Name)

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

	g.CancelAction(p)

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

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

func (g *Game) applyPotion(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.PotionEffect == "" && def.HealValue <= 0) {
			return
		}
	}

	p.RemoveItem(itemID, 1)

	effect := strings.ToLower(def.PotionEffect)

	switch effect {
	case "battery":
		batteryRestore := def.PotionBonus
		p.Battery += float64(batteryRestore)
		if p.Battery > 100 {
			p.Battery = 100
		}
		sess.WriteLine(fmt.Sprintf("\nYou drink the %s. Your battery is restored by %d%%.",
			g.itemColorize(sess, def, def.Name), batteryRestore))

	case "heal", "":
		healAmount := def.PotionBonus
		if healAmount <= 0 {
			healAmount = def.HealValue
		}
		if healAmount <= 0 {
			healAmount = 50
		}
		before := p.HP
		maxHP := p.Level(player.Hitpoints)
		if p.HP+healAmount > maxHP {
			p.HP = maxHP
		} else {
			p.HP += healAmount
		}
		actualHeal := p.HP - before
		sess.WriteLine(fmt.Sprintf("\nYou drink the %s. You restore %d hitpoints.",
			g.itemColorize(sess, def, def.Name), actualHeal))

	case "all_combat":
		duration := engine.ToTicks(def.PotionDuration)
		buffs := []string{"attack", "strength", "defense"}
		for _, stat := range buffs {
			p.ActiveBuffs = append(p.ActiveBuffs, player.PotionBuff{
				Stat:         stat,
				BonusPercent: def.PotionBonus,
				TicksLeft:    duration,
			})
		}
		sess.WriteLine(fmt.Sprintf("\nYou drink the %s. Your combat stats are boosted by %d%% for %d ticks.",
			g.itemColorize(sess, def, def.Name), def.PotionBonus, duration))

	default:
		duration := engine.ToTicks(def.PotionDuration)
		p.ActiveBuffs = append(p.ActiveBuffs, player.PotionBuff{
			Stat:         effect,
			BonusPercent: def.PotionBonus,
			TicksLeft:    duration,
		})
		sess.WriteLine(fmt.Sprintf("\nYou drink the %s. Your %s is boosted by %d%% for %d ticks.",
			g.itemColorize(sess, def, def.Name), effect, def.PotionBonus, duration))
	}

	p.ConsumeCooldown = 3
	p.ActionState = &ActionState{Type: ActionEating}

	if g.Hub != nil {
		for _, other := range g.Hub.PlayersInRoom(p.RoomID) {
			if other != sess && other.Player != nil {
				other.WriteLine(g.colorize(other, "broadcast", fmt.Sprintf("\n%s drinks a %s.", p.Name, def.Name)))
			}
		}
	}
}