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

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/combat"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/world"
)

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

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

	// Skip optional "behind"
	target := strings.Join(args, " ")
	if args[0] == "behind" {
		if len(args) < 2 {
			sess.WriteLine("Hide behind what?")
			return
		}
		target = strings.Join(args[1:], " ")
	}

	instances := g.World.FindObjInstances(p.RoomID, target)
	if len(instances) == 0 {
		sess.WriteLine("You don't see that here.")
		return
	}

	if len(instances) > 1 && objectInstancesNeedDisambiguation(instances) {
		sess.WriteLines(
			"That's ambiguous, which one?",
			formatObjInstanceChoices(instances),
		)
		return
	}

	objDef, err := g.ObjectStore.Load(instances[0].DefID)
	if err != nil || !objDef.IsSafespot() {
		sess.WriteLine("You can't hide behind that.")
		return
	}

	cfg := objDef.Safespot
	st := g.World.GetObjState(p.RoomID, objDef.ID, instances[0].Index)
	if st == nil {
		sess.WriteLine("You can't hide behind that.")
		return
	}

	existingSS, alreadyHiding := g.safespot.Get(p.Name)

	if alreadyHiding && existingSS.Active {
		if existingSS.ObjectDefID == objDef.ID && existingSS.ObjectIndex == instances[0].Index {
			if existingSS.HideCountdown > 0 {
				sess.WriteLine("You're already moving into position there.")
			} else {
				sess.WriteLine("You're already hiding there.")
			}
			return
		}
		g.leaveSafespot(sess, p)
	}

	if g.safespotTier(p) < cfg.Tier {
		sess.WriteLine("You can't figure out how to use this as effective cover.")
		return
	}

	if cfg.MaxOccupants > 0 && len(st.SafespotOccupants) >= cfg.MaxOccupants {
		sess.WriteLines(
			"",
			fmt.Sprintf("%s already hiding there!",
				joinSafespotOccupants(st.SafespotOccupants)),
		)
		return
	}

	if st.SafespotLevel <= 0 {
		if cfg.RespawnTicks > 0 {
			sess.WriteLine(fmt.Sprintf("The %s has been destroyed and hasn't reformed yet.", objDef.Name))
			return
		}
		st.SafespotLevel = len(cfg.Levels)
		st.SafespotTicksAtLevel = 0
	}

	inCombat := combat.GetCombat(p.Name) != nil
	hideCountdown := 1
	if inCombat {
		hideCountdown = 4
	}

	g.safespot.Set(p.Name, SafespotState{
		Active:        true,
		ObjectDefID:   objDef.ID,
		ObjectIndex:   instances[0].Index,
		RoomID:        p.RoomID,
		HideCountdown: hideCountdown,
	})

	p.ActionState = &ActionState{Type: ActionHiding, TargetName: objDef.Name}

	if inCombat {
		sess.WriteLine(fmt.Sprintf("You try to get behind the %s for cover...", objDef.Name))
	}
}

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

	ss, ok := g.safespot.Get(p.Name)

	if !ok || !ss.Active {
		sess.WriteLine("You're not hiding behind anything.")
		return
	}

	if ss.HideCountdown > 0 {
		g.safespot.Delete(p.Name)
		p.ActionState = nil
		sess.WriteLine("You stop trying to hide.")
		return
	}

	g.leaveSafespot(sess, p)
}

func objectInstancesNeedDisambiguation(instances []world.ObjState) bool {
	if len(instances) < 2 {
		return false
	}
	first := instances[0].DefID
	for i := 1; i < len(instances); i++ {
		if instances[i].DefID != first {
			return true
		}
	}
	return false
}

func formatObjInstanceChoices(instances []world.ObjState) string {
	var lines []string
	for _, inst := range instances {
		lines = append(lines, fmt.Sprintf("  %d.%s", inst.Index+1, inst.Name))
	}
	return strings.Join(lines, "\n")
}

func joinSafespotOccupants(names []string) string {
	if len(names) == 1 {
		return fmt.Sprintf("%s is", names[0])
	}
	return fmt.Sprintf("%s are", strings.Join(names, ", "))
}