Skip to content

Commit 34b77d7

Browse files
committed
adding pathto command and basic path following for mobs
1 parent ffcc627 commit 34b77d7

File tree

5 files changed

+156
-2
lines changed

5 files changed

+156
-2
lines changed

internal/hooks/NewRound_IdleMobs.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hooks
33

44
import (
55
"fmt"
6+
"strconv"
67
"time"
78

89
"github.com/GoMudEngine/GoMud/internal/configs"
@@ -78,6 +79,54 @@ func IdleMobs(e events.Event) events.ListenerReturn {
7879
continue
7980
}
8081

82+
// Check whether they are currently in the middle of a path, or have one waiting to start.
83+
if currentStep := mob.Path.Current(); currentStep != nil || mob.Path.Len() > 0 {
84+
85+
if currentStep == nil {
86+
//mob.Command(`say I'm beginning a new path.`)
87+
} else {
88+
89+
// If their currentStep isnt' actually the room they are in
90+
// They've somehow been moved. Reclaculate a new path.
91+
if currentStep.RoomId() != mob.Character.RoomId {
92+
//mob.Command(`say I seem to have wandered off my path.`)
93+
94+
reDoWaypoints := mob.Path.Waypoints()
95+
if len(reDoWaypoints) > 0 {
96+
newCommand := `pathto`
97+
for _, wpInt := range reDoWaypoints {
98+
newCommand += ` ` + strconv.Itoa(wpInt)
99+
}
100+
mob.Command(newCommand)
101+
continue
102+
}
103+
104+
mob.Command(`pathto home`)
105+
continue
106+
}
107+
108+
if currentStep.Waypoint() {
109+
//mob.Command(`say I've reached a waypoint.`)
110+
}
111+
}
112+
113+
if nextStep := mob.Path.Next(); nextStep != nil {
114+
115+
if room := rooms.LoadRoom(mob.Character.RoomId); room != nil {
116+
if exitInfo, ok := room.Exits[nextStep.ExitName()]; ok {
117+
if exitInfo.RoomId == nextStep.RoomId() {
118+
mob.Command(nextStep.ExitName())
119+
continue
120+
}
121+
}
122+
}
123+
124+
}
125+
126+
//mob.Command(`say I'm.... done.`)
127+
mob.Path.Clear()
128+
}
129+
81130
if mob.InConversation() {
82131
mob.Converse()
83132
continue

internal/mobcommands/mobcommands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
"lookforaid": {LookForAid, false},
4949
"lookfortrouble": {LookForTrouble, false},
5050
"noop": {Noop, true},
51+
"pathto": {Pathto, false},
5152
"portal": {Portal, false},
5253
"put": {Put, false},
5354
"remove": {Remove, false},

internal/mobcommands/pathto.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mobcommands
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
7+
"github.com/GoMudEngine/GoMud/internal/mapper"
8+
"github.com/GoMudEngine/GoMud/internal/mobs"
9+
"github.com/GoMudEngine/GoMud/internal/rooms"
10+
"github.com/GoMudEngine/GoMud/internal/util"
11+
)
12+
13+
func Pathto(rest string, mob *mobs.Mob, room *rooms.Room) (bool, error) {
14+
15+
toRoomIds := []int{}
16+
17+
for _, roomIdStr := range util.SplitButRespectQuotes(strings.ToLower(rest)) {
18+
19+
if roomIdStr == `home` {
20+
toRoomIds = append(toRoomIds, mob.HomeRoomId)
21+
continue
22+
}
23+
24+
if roomIdInt, err := strconv.Atoi(roomIdStr); err == nil {
25+
toRoomIds = append(toRoomIds, roomIdInt)
26+
}
27+
}
28+
29+
if len(toRoomIds) == 0 {
30+
return false, nil
31+
}
32+
33+
path, err := mapper.GetPath(mob.Character.RoomId, toRoomIds...)
34+
if err != nil {
35+
return false, err
36+
}
37+
38+
newPath := []mobs.PathRoom{}
39+
40+
// Copy everything over
41+
for _, p := range path {
42+
newPath = append(newPath, p)
43+
}
44+
45+
mob.Path.SetPath(newPath)
46+
47+
return true, nil
48+
}

internal/mobs/mobs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ type Mob struct {
7676
QuestFlags []string `yaml:"questflags,omitempty,flow"` // What quest flags are set on this mob?
7777
BuffIds []int `yaml:"buffids,omitempty"` // Buff Id's this mob always has upon spawn
7878
tempDataStore map[string]any
79-
conversationId int // Identifier of conversation currently involved in.
80-
hasConverseFile bool // whether they have a converse file to look for conversations in
79+
conversationId int // Identifier of conversation currently involved in.
80+
hasConverseFile bool // whether they have a converse file to look for conversations in
81+
Path PathQueue `yaml:"-"` // a pre-calculated path the mob is following.
8182
}
8283

8384
func MobInstanceExists(instanceId int) bool {

internal/mobs/mobs_path.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package mobs
2+
3+
type PathRoom interface {
4+
ExitName() string
5+
RoomId() int
6+
Waypoint() bool
7+
}
8+
9+
type PathQueue struct {
10+
roomQueue []PathRoom
11+
currentRoom PathRoom
12+
}
13+
14+
func (p PathQueue) Len() int {
15+
return len(p.roomQueue)
16+
}
17+
18+
func (p *PathQueue) Clear() {
19+
p.roomQueue = []PathRoom{}
20+
p.currentRoom = nil
21+
}
22+
23+
func (p PathQueue) Current() PathRoom {
24+
return p.currentRoom
25+
}
26+
27+
func (p *PathQueue) Next() PathRoom {
28+
if len(p.roomQueue) == 0 {
29+
return nil
30+
}
31+
p.currentRoom = p.roomQueue[0]
32+
p.roomQueue = p.roomQueue[1:]
33+
return p.currentRoom
34+
}
35+
36+
// returns a list of remaining waypoint roomIds
37+
func (p *PathQueue) Waypoints() []int {
38+
wpList := []int{}
39+
if p.currentRoom != nil && p.currentRoom.Waypoint() {
40+
wpList = append(wpList, p.currentRoom.RoomId())
41+
}
42+
43+
for _, r := range p.roomQueue {
44+
if r.Waypoint() {
45+
wpList = append(wpList, r.RoomId())
46+
}
47+
}
48+
49+
return wpList
50+
}
51+
52+
func (p *PathQueue) SetPath(path []PathRoom) {
53+
p.roomQueue = path
54+
p.currentRoom = nil
55+
}

0 commit comments

Comments
 (0)