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

import (
	"strings"
)

func verbToSkill(verb string) string {
	switch verb {
	case "mine":
		return "mining"
	case "chop", "cut":
		return "woodcutting"
	case "fish":
		return "fishing"
	}
	return ""
}

func (g *Game) resolveDefaultTarget(roomID int, verb string) string {
	skill := verbToSkill(verb)
	if skill == "" {
		return ""
	}

	objStates := g.World.AllObjInstances(roomID)

	type candidate struct {
		defID string
		name  string
	}
	var candidates []candidate
	seen := make(map[string]bool)

	for _, st := range objStates {
		if seen[st.DefID] {
			continue
		}
		seen[st.DefID] = true

		objDef, err := g.ObjectStore.Load(st.DefID)
		if err != nil || objDef.BehaviorID == "" {
			continue
		}

		bh, err := g.BehaviorStore.Load(objDef.BehaviorID)
		if err != nil || bh.Type != "gather" {
			continue
		}

		cfg, err := g.BehaviorStore.LoadGather(objDef.BehaviorID)
		if err != nil {
			continue
		}

		if strings.EqualFold(cfg.Skill, skill) {
			candidates = append(candidates, candidate{defID: st.DefID, name: objDef.Name})
		}
	}

	if len(candidates) == 1 {
		return candidates[0].name
	}
	return ""
}

func (g *Game) resolveDefaultMob(roomID int) string {
	mobs := g.MobStore.MobsInRoom(roomID)
	if len(mobs) == 0 {
		return ""
	}

	firstDef := mobs[0].DefID
	for _, m := range mobs[1:] {
		if m.DefID != firstDef {
			return ""
		}
	}
	return mobs[0].Name
}