MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / TryMobScriptEvent

Function TryMobScriptEvent

internal/scripting/mob.go:93–137  ·  view source on GitHub ↗
(eventName string, mobInstanceId int, sourceId int, sourceType string, details map[string]any)

Source from the content-addressed store, hash-verified

91}
92
93func TryMobScriptEvent(eventName string, mobInstanceId int, sourceId int, sourceType string, details map[string]any) (bool, error) {
94
95 sMob := GetActor(0, mobInstanceId)
96 if sMob == nil {
97 return false, fmt.Errorf("mob not found")
98 }
99
100 vmw, err := getMobVM(sMob)
101 if err != nil {
102 return false, err
103 }
104
105 timestart := time.Now()
106 defer func() {
107 if eventName != "onIdle" {
108 mudlog.Debug("TryMobScriptEvent()", "eventName", eventName, "MobId", sMob.MobTypeId(), "time", time.Since(timestart))
109 }
110 }()
111 if onCommandFunc, ok := vmw.GetFunction(eventName); ok {
112
113 if details == nil {
114 details = make(map[string]any)
115 }
116
117 sRoom := GetRoom(sMob.GetRoomId())
118
119 details["sourceId"] = sourceId
120 details["sourceType"] = sourceType
121
122 res, err := runCallable(vmw, scriptRoomTimeout, onCommandFunc,
123 vmw.VM.ToValue(sMob),
124 vmw.VM.ToValue(sRoom),
125 vmw.VM.ToValue(details),
126 )
127 if err != nil {
128 return false, fmt.Errorf("%s(): %w", eventName, err)
129 }
130
131 if boolVal, ok := res.Export().(bool); ok {
132 return boolVal, nil
133 }
134 }
135
136 return false, ErrEventNotFound
137}
138
139func TryMobCommand(cmd string, rest string, mobInstanceId int, sourceId int, sourceType string) (bool, error) {
140

Callers 9

GoFunction · 0.92
SuicideFunction · 0.92
handlePlayerCombatFunction · 0.92
handleMobCombatFunction · 0.92
IdleMobsFunction · 0.92
HandleIdleMobsFunction · 0.92
GiveFunction · 0.92
ShowFunction · 0.92
AskFunction · 0.92

Calls 8

DebugFunction · 0.92
GetActorFunction · 0.85
getMobVMFunction · 0.85
GetRoomFunction · 0.85
runCallableFunction · 0.85
MobTypeIdMethod · 0.80
GetFunctionMethod · 0.80
GetRoomIdMethod · 0.45

Tested by

no test coverage detected