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

import (
	"fmt"
	"strings"

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

func (g *Game) advanceClean(sess *net.Session, p *player.Player) {
	phase, _ := p.BackgroundAction.Data["phase"].(int)
	filter, _ := p.BackgroundAction.Data["filter"].(string)

	if phase == 0 {
		p.BackgroundAction.Data["phase"] = 1
		p.BackgroundAction.WaitLeft = engine.ToTicks(2)
		return
	}

	items := g.CraftIndex.ByType("clean")

	var match *cleanMatch
	for _, item := range items {
		c := item.FirstCraft()
		if filter != "" {
			if len(c.Consume) == 0 || len(c.Consume[0].Items) == 0 {
				continue
			}
			if !strings.Contains(c.Consume[0].Items[0], filter) &&
				!strings.Contains(item.ID, filter) {
				continue
			}
		}
		if p.Level(player.Pharmacy) < c.Level {
			continue
		}
		if !craftHasAllItems(c, p.HasItem) {
			continue
		}
		match = &cleanMatch{c.Consume[0].Items[0], item.ID, c.XP, c.Message}
		break
	}

	if match == nil {
		sess.WriteLine("\nYou've finished cleaning herbs.")
		g.cancelBgAction(p)
		return
	}

	for i := 0; i < 28; i++ {
		slot := p.InvSlot(i)
		if slot != nil && slot.ItemID == match.consumeID {
			slot.ItemID = match.outputID
			break
		}
	}

	if match.xp > 0 {
		g.awardSkillXP(sess, p, player.Pharmacy, match.xp)
	}
	g.AccountStore.SaveCharacter(p)

	msg := match.message
	if msg == "" {
		outDef, _ := g.ItemStore.Load(match.outputID)
		outputName := match.outputID
		if outDef != nil {
			outputName = outDef.Name
		}
		msg = fmt.Sprintf("You clean a %s.", outputName)
	}
	if p.OptionBool("xp_drops") && match.xp > 0 {
		abbr := player.SkillAbbr[player.Pharmacy]
		msg += g.colorize(sess, "xp", fmt.Sprintf(" (+%dxp %s)", match.xp, abbr))
	}
	sess.WriteLine(msg)

	hasMore := false
	for _, item := range items {
		c := item.FirstCraft()
		if filter != "" {
			if len(c.Consume) == 0 || len(c.Consume[0].Items) == 0 {
				continue
			}
			if !strings.Contains(c.Consume[0].Items[0], filter) &&
				!strings.Contains(item.ID, filter) {
				continue
			}
		}
		if p.Level(player.Pharmacy) >= c.Level && craftHasAllItems(c, p.HasItem) {
			hasMore = true
			break
		}
	}

	if !hasMore {
		sess.WriteLine("\nYou've finished cleaning herbs.")
		g.cancelBgAction(p)
		return
	}

	p.BackgroundAction.WaitLeft = engine.ToTicks(2)
}

type cleanMatch struct {
	consumeID string
	outputID  string
	xp        int
	message   string
}