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

import (
	"thehouseoficarus/internal/behavior"
	"thehouseoficarus/internal/engine"
	"thehouseoficarus/internal/item"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) startCombine(sess *net.Session, p *player.Player, item *item.ItemDef, craft *item.CraftDef, count int) {
	g.cancelAction(p)
	g.cancelBgAction(p)

	if !craftHasAllItemsQty(craft, p.CountItem) {
		sess.WriteLine("You don't have the required materials.")
		g.reprompt(sess)
		return
	}

	outDef, _ := g.ItemStore.Load(item.ID)
	outputName := item.ID
	if outDef != nil {
		outputName = outDef.Name
	}

	startMsg := g.resolveCraftMessage(sess, craft.StartMessage, "You start combining %i1.", craft, item.ID, nil, p.HasItem)
	endMsg := g.resolveCraftMessage(sess, craft.EndMessage, "You've finished combining.", craft, item.ID, nil, p.HasItem)

	p.Action = &behavior.Action{
		Type:       behavior.TypeCombine,
		TargetID:   item.ID,
		TargetName: outputName,
		Data: &behavior.CombineData{
			ItemID:        item.ID,
			Phase:         0,
			StepIndex:     0,
			Remaining:     count,
			StartMessage:  startMsg,
			EndMessage:    endMsg,
			TicksPerCycle: craft.TicksPerCycle,
		},
		WaitLeft: engine.ToTicks(1),
	}

	g.broadcastAction(sess, "%s starts combining.", p.Name)
}

func (g *Game) advanceCombine(sess *net.Session, p *player.Player) {
	d, ok := p.Action.Data.(*behavior.CombineData)
	if !ok {
		g.cancelAction(p)
		return
	}

	item := g.loadCraftItem(d.ItemID)
	if item == nil {
		g.cancelAction(p)
		return
	}
	craft := item.FirstCraft()

	switch d.Phase {
	case 0:
		if d.StartMessage != "" {
			sess.WriteLine(d.StartMessage)
		}
		d.Phase = 1
		if len(craft.Steps) > 0 {
			p.Action.WaitLeft = engine.ToTicks(craft.Steps[0].Delay)
		} else {
			firstWait := craft.TicksPerCycle
			if firstWait <= 0 {
				firstWait = 4
			}
			p.Action.WaitLeft = engine.ToTicks(firstWait)
		}
		return

	case 1:
		if len(craft.Steps) > 0 && d.StepIndex < len(craft.Steps) {
			step := craft.Steps[d.StepIndex]
			sess.WriteLine(g.resolveCraftVar(sess, step.Message, craft, item.ID, nil, p.HasItem))
			d.StepIndex++
			if d.StepIndex < len(craft.Steps) {
				p.Action.WaitLeft = engine.ToTicks(craft.Steps[d.StepIndex].Delay)
				return
			}
		}
		d.Phase = 2
		p.Action.WaitLeft = engine.ToTicks(1)
		return

	case 2:
		if !craftHasAllItemsQty(craft, p.CountItem) {
			if d.EndMessage != "" {
				sess.WriteLine(d.EndMessage)
			}
			g.cancelAction(p)
			return
		}

		// Byproducts must be collected BEFORE consumption: the resolver maps
		// each ingredient entry to the alternative actually being consumed
		// (bucket vs jug of water), which requires the items still present.
		byproducts := g.collectByproducts(p, item)

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

		outputQty := craft.OutputQty
		if outputQty <= 0 {
			outputQty = 1
		}

		outDef, _ := g.ItemStore.Load(item.ID)
		placed := false
		if outDef != nil && outDef.Stackable {
			for i := 0; i < 28; i++ {
				slot := p.InvSlot(i)
				if slot != nil && slot.ItemID == item.ID {
					slot.Quantity += outputQty
					placed = true
					break
				}
			}
		}
		if !placed {
			freeSlot := p.FirstFreeSlot()
			if freeSlot == -1 {
				sess.WriteLine("Your inventory is too full!")
				g.cancelAction(p)
				return
			}
			p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: item.ID, Quantity: outputQty})
		}

		// Return the empty containers (and any other byproducts) alongside
		// the output; without this, combine crafts silently discarded them.
		for _, bp := range byproducts {
			if slot := p.FirstFreeSlot(); slot >= 0 {
				p.SetInvSlot(slot, &player.InventorySlot{ItemID: bp, Quantity: 1})
			}
		}

		g.saveCharacter(p)

		if d.Remaining > 0 {
			d.Remaining--
			if d.Remaining <= 0 {
				if d.EndMessage != "" {
					sess.WriteLine(d.EndMessage)
				}
				g.cancelAction(p)
				return
			}
		}

		if !craftHasAllItemsQty(craft, p.CountItem) {
			if d.EndMessage != "" {
				sess.WriteLine(d.EndMessage)
			}
			g.cancelAction(p)
			return
		}

		d.Phase = 0
		d.StepIndex = 0
		p.Action.WaitLeft = engine.ToTicks(1)
	}
}