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

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/combat"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) executeTrigger(sess *net.Session, args []string, rawInput string) {
	g.doTrigger(sess, strings.Join(args, " "))
}

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

	if input == "" {
		sess.WriteLine("Trigger what? Type 'mods' to see available mods.")
		return
	}

	mod, targetArg := g.parseTriggerArgs(input)
	if mod == nil {
		sess.WriteLine("Unknown mod. Type 'mods' to see available mods.")
		return
	}

	if p.Level(player.Science) < mod.Level {
		sess.WriteLine(fmt.Sprintf("You need level %d science to trigger %s.", mod.Level, mod.Name))
		return
	}

	if !g.hasJunkCost(p, mod) {
		sess.WriteLine(fmt.Sprintf("You don't have enough junk to trigger %s.", mod.Name))
		return
	}

	switch mod.Category {
	case ModCombat:
		g.triggerCombatMod(sess, p, mod, targetArg)
	case ModTransport:
		g.triggerTransport(sess, p, mod)
	case ModUtility:
		g.triggerUtility(sess, p, mod, targetArg)
	case ModEnchant:
		g.triggerEnchant(sess, p, mod, targetArg)
	}
}

func (g *Game) parseTriggerArgs(input string) (*ModDef, string) {
	lower := strings.ToLower(input)
	words := strings.Fields(lower)
	for i := len(words); i > 0; i-- {
		candidate := strings.Join(words[:i], " ")
		mod := FindMod(candidate)
		if mod != nil {
			target := strings.TrimSpace(strings.Join(words[i:], " "))
			return mod, target
		}
	}
	return nil, ""
}

func (g *Game) triggerCombatMod(sess *net.Session, p *player.Player, mod *ModDef, targetArg string) {
	if cs := combat.GetCombat(p.Name); cs != nil {
		p.AutotriggerMod = mod.ID
		sess.WriteLine(fmt.Sprintf("You switch to triggering %s.", mod.Name))
		return
	}

	if p.Action != nil {
		g.cancelAction(p)
	}

	var mobTarget string
	if targetArg == "" {
		mobTarget = g.resolveDefaultMob(p.RoomID)
		if mobTarget == "" {
			sess.WriteLine("Trigger on what?")
			return
		}
	} else {
		mobTarget = targetArg
	}

	mob := g.findMob(sess, mobTarget, p.RoomID)
	if mob == nil {
		return
	}

	if mob.HP <= 0 {
		sess.WriteLine("That is already dead.")
		return
	}

	if mob.Protected {
		sess.WriteLine(fmt.Sprintf("You can't attack %s!", mobDisplayName(mob, true)))
		return
	}

	if combat.IsMobInCombat(mob.InstanceID) {
		sess.WriteLine(fmt.Sprintf("%s is already engaged in combat!", mobDisplayName(mob, false)))
		return
	}

	p.AutotriggerMod = mod.ID
	g.startCombat(sess, p, mob)
}