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

import (
	"fmt"
	"strings"

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

func (g *Game) executeJack(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	g.cancelAction(p)
	g.doJack(sess, strings.Join(args, " "))
}

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

	if combat.GetCombat(p.Name) != nil {
		sess.WriteLine("You can't do that during combat!")
		return
	}

	if target == "" {
		target = g.findTerminalInRoom(p.RoomID)
		if target == "" {
			sess.WriteLine("There's no terminal here to jack into.")
			return
		}
	}

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

	defID := instances[0].DefID
	tDef, ok := hacking.Terminals[defID]
	if !ok {
		sess.WriteLine("You can't jack into that.")
		return
	}

	hackLevel := p.Level(player.Hacking)
	if hackLevel < tDef.Level {
		sess.WriteLine(fmt.Sprintf("You need level %d Hacking to use this terminal.", tDef.Level))
		return
	}

	g.cancelAction(p)
	g.cancelBgAction(p)

	minigame := tDef.Minigame()
	display := minigame.Init(hackLevel)

	g.hackingStates[p.Name] = &hacking.Session{
		Minigame:   minigame,
		TerminalID: defID,
		Level:      hackLevel,
		ReqLevel:   tDef.Level,
		Started:    false,
	}

	sess.State = net.StateHacking

	g.broadcastAction(sess, "\n%s jacks into a terminal.", p.Name)

	sess.WriteLine(display)
	sess.Write("\nhack> ")
}

func (g *Game) findTerminalInRoom(roomID int) string {
	for _, st := range g.World.FindObjInstances(roomID, "") {
		if strings.HasPrefix(st.DefID, "terminal_") {
			return st.Name
		}
	}
	return ""
}