aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_farm.go
blob: 19cdf12c27f9b956e46159d8908dde0d9a7cbafe (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package game

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/behavior"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) executePlant(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	g.cancelAction(p)
	if len(args) == 0 {
		sess.WriteLine("Plant what?")
	} else {
		g.doPlant(sess, strings.Join(args, " "))
	}
}

func (g *Game) executeHarvest(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	g.cancelAction(p)
	if len(args) == 0 {
		g.doHarvest(sess, "")
	} else {
		g.doHarvest(sess, strings.Join(args, " "))
	}
}

func (g *Game) executeRake(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	g.cancelAction(p)
	if len(args) == 0 {
		g.doRake(sess, "")
	} else {
		g.doRake(sess, strings.Join(args, " "))
	}
}

func (g *Game) executeWater(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	g.cancelAction(p)
	if len(args) == 0 {
		g.doWater(sess, "")
	} else {
		g.doWater(sess, strings.Join(args, " "))
	}
}

func (g *Game) executeCure(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	g.cancelAction(p)
	if len(args) == 0 {
		g.doCure(sess, "")
	} else {
		g.doCure(sess, strings.Join(args, " "))
	}
}

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

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

	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
	}

	seedID := matches[0].ID
	seedDef, err := g.ItemStore.Load(seedID)
	if err != nil || seedDef.FarmPatchType == "" {
		sess.WriteLine("You can't plant that.")
		return
	}

	farmLevel := p.Level(player.Farming)
	if seedDef.FarmLevel > 0 && farmLevel < seedDef.FarmLevel {
		sess.WriteLine(fmt.Sprintf("You need level %d farming to plant that. Your farming level is %d.", seedDef.FarmLevel, farmLevel))
		return
	}

	if !g.hasToolType(p, "spade") {
		sess.WriteLine("You need a spade to plant seeds.")
		return
	}

	patchDefID := seedDef.FarmPatchType + "_patch"
	objInstances := g.World.FindObjInstances(p.RoomID, patchDefID)
	if len(objInstances) == 0 {
		sess.WriteLine("There's no suitable patch here to plant that.")
		return
	}

	var prefix string
	for _, obj := range objInstances {
		pref := farmFlagPrefix(patchDefID, obj.Index)
		if pref == "" {
			continue
		}
		g.ensureFarmState(p, pref)
		weeds, _ := p.Flags[pref+"_weeds"].(bool)
		seedPlanted, _ := p.Flags[pref+"_seed"].(string)
		if !weeds && seedPlanted == "" {
			prefix = pref
			break
		}
	}

	if prefix == "" {
		sess.WriteLine("All patches here have something in them. Rake them first.")
		return
	}

	p.RemoveItem(seedID, 1)

	p.Action = &behavior.Action{
		Type:       "plant",
		TargetID:   seedID,
		TargetName: seedDef.Name,
		WaitLeft:   2,
		Data: &behavior.FarmData{
			SeedID: seedID,
			Prefix: prefix,
			XP:     float64(seedDef.FarmPlantXP),
		},
	}

	g.AccountStore.SaveCharacter(p)
}

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

	prefix, _, _ := g.findFarmPatch(sess, p, input, func(flags map[string]any, prefix string) bool {
		ready, _ := flags[prefix+"_ready"].(bool)
		return ready
	})
	if prefix == "" {
		if input != "" {
			sess.WriteLine("That patch isn't ready to harvest.")
		} else {
			sess.WriteLine("There's nothing ready to harvest here.")
		}
		return
	}

	g.ensureFarmState(p, prefix)
	ready, _ := p.Flags[prefix+"_ready"].(bool)
	if !ready {
		sess.WriteLine("There's nothing ready to harvest here.")
		return
	}

	if !g.hasToolType(p, "spade") {
		sess.WriteLine("You need a spade to harvest.")
		return
	}

	seedID, _ := p.Flags[prefix+"_seed"].(string)
	if seedID == "" {
		sess.WriteLine("There's nothing planted here.")
		return
	}

	seedDef, err := g.ItemStore.Load(seedID)
	if err != nil {
		sess.WriteLine("Error loading seed data.")
		return
	}

	product := seedDef.FarmProduct
	if product == "" {
		sess.WriteLine("This crop has no harvest product defined.")
		return
	}

	productDef, _ := g.ItemStore.Load(product)
	productName := product
	if productDef != nil {
		productName = productDef.Name
	}

	if p.FreeSlots() <= 0 && (productDef == nil || !productDef.Stackable || p.CountItem(product) == 0) {
		sess.WriteLine("Your inventory is full.")
		return
	}

	minYield := seedDef.FarmMinYield
	maxYield := seedDef.FarmMaxYield
	if minYield <= 0 {
		minYield = 3
	}
	if maxYield <= 0 {
		maxYield = 10
	}
	harvestXP := seedDef.FarmHarvestXP

	p.Action = &behavior.Action{
		Type:       "harvest",
		TargetID:   prefix,
		TargetName: productName,
		WaitLeft:   3,
		Data: &behavior.FarmData{
			Prefix:   prefix,
			SeedID:   seedID,
			Product:  product,
			MinYield: float64(minYield),
			MaxYield: float64(maxYield),
			XP:       float64(harvestXP),
		},
	}

	g.AccountStore.SaveCharacter(p)
}

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

	if !g.hasToolType(p, "rake") {
		sess.WriteLine("You need a rake to do that.")
		return
	}

	prefix, _, _ := g.findFarmPatch(sess, p, input, func(flags map[string]any, prefix string) bool {
		weeds, _ := flags[prefix+"_weeds"].(bool)
		dead, _ := flags[prefix+"_dead"].(bool)
		return weeds || dead
	})
	if prefix == "" {
		if input != "" {
			sess.WriteLine("That patch doesn't need raking.")
		} else {
			sess.WriteLine("There's nothing here that needs raking.")
		}
		return
	}

	g.ensureFarmState(p, prefix)
	weeds, _ := p.Flags[prefix+"_weeds"].(bool)
	dead, _ := p.Flags[prefix+"_dead"].(bool)
	if !weeds && !dead {
		sess.WriteLine("The patch doesn't need raking.")
		return
	}

	patchName := patchDisplayName(prefix)

	p.Action = &behavior.Action{
		Type:       "rake",
		TargetID:   prefix,
		TargetName: patchName,
		WaitLeft:   4,
		Data: &behavior.FarmData{
			Prefix: prefix,
		},
	}

	g.AccountStore.SaveCharacter(p)
}

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

	if !g.hasToolType(p, "watering_can") {
		sess.WriteLine("You need a watering can to do that.")
		return
	}

	prefix, _, _ := g.findFarmPatch(sess, p, input, func(flags map[string]any, prefix string) bool {
		seed, _ := flags[prefix+"_seed"].(string)
		if seed == "" {
			return false
		}
		ready, _ := flags[prefix+"_ready"].(bool)
		dead, _ := flags[prefix+"_dead"].(bool)
		watered, _ := flags[prefix+"_watered"].(bool)
		return !dead && !ready && !watered
	})
	if prefix == "" {
		if input != "" {
			sess.WriteLine("That patch doesn't need watering.")
		} else {
			sess.WriteLine("There's nothing here that needs watering.")
		}
		return
	}

	g.ensureFarmState(p, prefix)
	seedID, _ := p.Flags[prefix+"_seed"].(string)
	if seedID == "" {
		sess.WriteLine("There's nothing planted here to water.")
		return
	}
	ready, _ := p.Flags[prefix+"_ready"].(bool)
	if ready {
		sess.WriteLine("The crop is already fully grown.")
		return
	}
	watered, _ := p.Flags[prefix+"_watered"].(bool)
	if watered {
		sess.WriteLine("The patch is already watered.")
		return
	}

	patchName := patchDisplayName(prefix)

	p.Action = &behavior.Action{
		Type:       "water",
		TargetID:   prefix,
		TargetName: patchName,
		WaitLeft:   2,
		Data: &behavior.FarmData{
			Prefix: prefix,
		},
	}

	g.AccountStore.SaveCharacter(p)
}

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

	if !p.HasItem("plant_cure") {
		sess.WriteLine("You don't have any plant cure.")
		return
	}

	prefix, _, _ := g.findFarmPatch(sess, p, input, func(flags map[string]any, prefix string) bool {
		diseased, _ := flags[prefix+"_diseased"].(bool)
		return diseased
	})
	if prefix == "" {
		if input != "" {
			sess.WriteLine("That patch isn't diseased.")
		} else {
			sess.WriteLine("There's nothing here that needs curing.")
		}
		return
	}

	g.ensureFarmState(p, prefix)
	diseased, _ := p.Flags[prefix+"_diseased"].(bool)
	if !diseased {
		sess.WriteLine("The patch isn't diseased.")
		return
	}

	patchName := patchDisplayName(prefix)

	p.Action = &behavior.Action{
		Type:       "cure",
		TargetID:   prefix,
		TargetName: patchName,
		WaitLeft:   2,
		Data: &behavior.FarmData{
			Prefix: prefix,
		},
	}

	g.AccountStore.SaveCharacter(p)
}

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

	objInstances := g.World.AllObjInstances(p.RoomID)
	var farmPatches []struct {
		prefix string
		defID  string
		index  int
	}

	for _, obj := range objInstances {
		if !farmPatchDefIDs[obj.DefID] {
			continue
		}
		pref := farmFlagPrefix(obj.DefID, obj.Index)
		if pref == "" {
			continue
		}
		farmPatches = append(farmPatches, struct {
			prefix string
			defID  string
			index  int
		}{pref, obj.DefID, obj.Index})
	}

	if len(farmPatches) == 0 {
		sess.WriteLine("There are no farming patches here.")
		return
	}

	if input != "" {
		lower := strings.ToLower(input)
		var filtered []struct {
			prefix string
			defID  string
			index  int
		}
		for _, fp := range farmPatches {
			def, _ := g.ObjectStore.Load(fp.defID)
			if def != nil && strings.Contains(strings.ToLower(def.Name), lower) {
				filtered = append(filtered, fp)
			}
		}
		if len(filtered) == 0 {
			for _, fp := range farmPatches {
				if strings.Contains(fp.prefix, lower) || strings.Contains(strings.ToLower(fp.defID), lower) {
					filtered = append(filtered, fp)
				}
			}
		}
		farmPatches = filtered
	}

	for _, fp := range farmPatches {
		g.ensureFarmState(p, fp.prefix)
		def, _ := g.ObjectStore.Load(fp.defID)
		patchName := fp.defID
		if def != nil {
			patchName = def.Name
		}
		indexStr := ""
		if len(farmPatches) > 1 || fp.index > 0 {
			indexStr = fmt.Sprintf(" %d", fp.index+1)
		}

		sess.WriteLine(fmt.Sprintf("=== %s%s ===", patchName, indexStr))
		g.ShowFarmPatchDetail(sess, p, fp.prefix, fp.defID, fp.index)
	}
}

func (g *Game) ShowFarmPatchDetail(sess *net.Session, p *player.Player, prefix, defID string, index int) {
	weeds, _ := p.Flags[prefix+"_weeds"].(bool)
	if weeds {
		sess.WriteLine("  Status: Weeds")
		sess.WriteLine("  (Rake to clear before planting)")
		return
	}

	seedID, _ := p.Flags[prefix+"_seed"].(string)
	if seedID == "" {
		sess.WriteLine("  Status: Empty")
		sess.WriteLine("  (Ready to plant)")
		return
	}

	seedName := seedID
	if def, err := g.ItemStore.Load(seedID); err == nil {
		seedName = def.Name
	}

	dead, _ := p.Flags[prefix+"_dead"].(bool)
	if dead {
		sess.WriteLine(fmt.Sprintf("  Status: Dead"))
		sess.WriteLine(fmt.Sprintf("  Planted: %s", seedName))
		sess.WriteLine("  (Rake to clear)")
		return
	}

	diseased, _ := p.Flags[prefix+"_diseased"].(bool)
	if diseased {
		sess.WriteLine(fmt.Sprintf("  Status: Diseased!"))
		sess.WriteLine(fmt.Sprintf("  Planted: %s", seedName))
		sess.WriteLine("  (Use plant cure to save it)")
		return
	}

	ready, _ := p.Flags[prefix+"_ready"].(bool)
	if ready {
		sess.WriteLine(fmt.Sprintf("  Status: Ready to harvest!"))
		sess.WriteLine(fmt.Sprintf("  Planted: %s", seedName))
		return
	}

	stage := intFromFlag(p.Flags, prefix+"_stage")
	maxStages := 4
	if def, err := g.ItemStore.Load(seedID); err == nil && def.FarmStages > 0 {
		maxStages = def.FarmStages
	}
	watered, _ := p.Flags[prefix+"_watered"].(bool)

	sess.WriteLine(fmt.Sprintf("  Status: Growing (stage %d/%d)", stage, maxStages))
	sess.WriteLine(fmt.Sprintf("  Planted: %s", seedName))
	sess.WriteLine(fmt.Sprintf("  Watered: %s", boolToYes(watered)))
	sess.WriteLine(fmt.Sprintf("  Diseased: %s", boolToYes(diseased)))
}

func boolToYes(b bool) string {
	if b {
		return "Yes"
	}
	return "No"
}

func (g *Game) findFarmPatch(sess *net.Session, p *player.Player, input string, matcher func(map[string]any, string) bool) (prefix string, defID string, index int) {
	objInstances := g.World.AllObjInstances(p.RoomID)
	if len(objInstances) == 0 {
		return "", "", -1
	}

	type patchInfo struct {
		prefix string
		defID  string
		index  int
		name   string
	}
	var patches []patchInfo

	for _, obj := range objInstances {
		if !farmPatchDefIDs[obj.DefID] {
			continue
		}
		g.ensureFarmState(p, farmFlagPrefix(obj.DefID, obj.Index))
	}

	for _, obj := range objInstances {
		if !farmPatchDefIDs[obj.DefID] {
			continue
		}
		pref := farmFlagPrefix(obj.DefID, obj.Index)
		if pref == "" {
			continue
		}
		if !matcher(p.Flags, pref) {
			continue
		}
		var name string
		if def, err := g.ObjectStore.Load(obj.DefID); err == nil {
			name = def.Name
		} else {
			name = obj.DefID
		}
		patches = append(patches, patchInfo{prefix: pref, defID: obj.DefID, index: obj.Index, name: name})
	}

	if len(patches) == 0 {
		return "", "", -1
	}

	if input == "" {
		return patches[0].prefix, patches[0].defID, patches[0].index
	}

	lower := strings.ToLower(input)
	for _, pi := range patches {
		if strings.Contains(lower, strings.ToLower(pi.name)) {
			return pi.prefix, pi.defID, pi.index
		}
	}
	for _, pi := range patches {
		if strings.Contains(lower, pi.prefix) {
			return pi.prefix, pi.defID, pi.index
		}
	}

	return "", "", -1
}