Skip to content

Commit d407d16

Browse files
authored
Added scripting function support for modules. Added "modules.follow.GetFollowers()" and example usage. (#360)
# Description Adds support for scripting functions defined in modules. ## Changes - module/plugin support for adding scripting functions `AddScriptingFunction(funcName string, funcVal any)` - Created an example/first case use of this in guard script (checks for followers and tells them to get lost)
1 parent 539d4d7 commit d407d16

File tree

8 files changed

+96
-4
lines changed

8 files changed

+96
-4
lines changed

_datafiles/world/default/mobs/frostfang/scripts/2-guard.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,30 @@ function onIdle(mob, room) {
3838

3939
return false;
4040
}
41+
42+
43+
44+
function onPath(mob, room, eventDetails) {
45+
46+
if ( eventDetails.status == "waypoint" ) {
47+
48+
if ( UtilDiceRoll(1, 5) == 1 ) {
49+
50+
51+
if ( modules.follow ) {
52+
53+
followingActors = modules.follow.GetFollowers(mob);
54+
55+
for( var i in followingActors ) {
56+
mob.Command("sayto "+followingActors[i].ShorthandId()+" Why are you following me? Leave me be.");
57+
mob.Command("follow lose");
58+
break;
59+
}
60+
61+
}
62+
63+
64+
}
65+
}
66+
67+
}

_datafiles/world/default/mobs/frostfang/scripts/26-frostfang_citizen-locketsadness.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ function onIdle(mob, room) {
152152
mob.Command("emote sniffles a bit, holding back tears.");
153153
return true;
154154
default: // 1, 3
155+
if ( UtilDiceRoll(1, 10) == 1 ) {
156+
mob.Command("pathto 274"); // look around the bushes area
157+
return true;
158+
}
155159
return false;
156160
}
157161

internal/plugins/plugincallbacks.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
)
99

1010
type PluginCallbacks struct {
11-
userCommands map[string]usercommands.CommandAccess
12-
mobCommands map[string]mobcommands.CommandAccess
11+
userCommands map[string]usercommands.CommandAccess
12+
mobCommands map[string]mobcommands.CommandAccess
13+
scriptCommands map[string]map[string]any
1314

1415
iacHandler func(uint64, []byte) bool
1516
onLoad func()
@@ -19,8 +20,9 @@ type PluginCallbacks struct {
1920

2021
func newPluginCallbacks() PluginCallbacks {
2122
return PluginCallbacks{
22-
userCommands: map[string]usercommands.CommandAccess{},
23-
mobCommands: map[string]mobcommands.CommandAccess{},
23+
userCommands: map[string]usercommands.CommandAccess{},
24+
mobCommands: map[string]mobcommands.CommandAccess{},
25+
scriptCommands: map[string]map[string]any{},
2426
}
2527
}
2628

internal/plugins/plugins.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/GoMudEngine/GoMud/internal/configs"
1616
"github.com/GoMudEngine/GoMud/internal/mobcommands"
1717
"github.com/GoMudEngine/GoMud/internal/mudlog"
18+
"github.com/GoMudEngine/GoMud/internal/scripting"
1819
"github.com/GoMudEngine/GoMud/internal/usercommands"
1920
"github.com/GoMudEngine/GoMud/internal/util"
2021
"gopkg.in/yaml.v2"
@@ -228,6 +229,16 @@ func (p *Plugin) ExportFunction(stringId string, f any) {
228229
p.exportedFunctions[stringId] = f
229230
}
230231

232+
// Registers a UserCommand and callback
233+
func (p *Plugin) AddScriptingFunction(funcName string, scriptFunc any) {
234+
235+
if _, ok := p.Callbacks.scriptCommands[p.name]; !ok {
236+
p.Callbacks.scriptCommands[p.name] = map[string]any{}
237+
}
238+
239+
p.Callbacks.scriptCommands[p.name][funcName] = scriptFunc
240+
}
241+
231242
// Registers a UserCommand and callback
232243
func (p *Plugin) AddUserCommand(command string, handlerFunc usercommands.UserCommand, allowWhenDowned bool, isAdminOnly bool) {
233244

@@ -421,6 +432,12 @@ func Load(dataFilesPath string) {
421432
mobcommands.RegisterCommand(cmd, info.Func, info.AllowedWhenDowned)
422433
}
423434

435+
for nameSpace, funcMap := range p.Callbacks.scriptCommands {
436+
for cmd, funcRef := range funcMap {
437+
scripting.AddModlueFunction(nameSpace, cmd, funcRef)
438+
}
439+
}
440+
424441
// Check for config.yaml override and set missing values accordingly
425442
OLPath := util.FilePath(`data-overlays`, `/`, `config.yaml`)
426443
if b, err := p.files.ReadFile(OLPath); err == nil {

internal/scripting/module_func.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package scripting
2+
3+
import (
4+
"github.com/dop251/goja"
5+
)
6+
7+
var (
8+
moduleFunctions = map[string]map[string]any{}
9+
)
10+
11+
func AddModlueFunction(namespace string, name string, funcRef any) {
12+
if _, ok := moduleFunctions[namespace]; !ok {
13+
moduleFunctions[namespace] = map[string]any{}
14+
}
15+
moduleFunctions[namespace][name] = funcRef
16+
}
17+
18+
func setModuleFunctions(vm *goja.Runtime) {
19+
vm.Set("modules", moduleFunctions)
20+
}

internal/scripting/scripting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func setAllScriptingFunctions(vm *goja.Runtime) {
100100
setSpellFunctions(vm)
101101
setItemFunctions(vm)
102102
setUtilFunctions(vm)
103+
setModuleFunctions(vm)
103104
}
104105

105106
func PruneVMs(forceClear ...bool) {

modules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Extract any modules into this folder.
2424
* Add/Over-write existing template files (See `modules/auctions`)
2525
* Add/Over-write help files (See `modules/auctions`)
2626
* Add/Over-write user or mob commands (See `modules/auctions`)
27+
* Add functions for scripting (See `modules/follow`)
2728
* Save/Load their own data (See `modules/leaderboards`)
2829
* Track their own config values (See `modules/leaderboards`)
2930
* Modify help menu items, command aliases, help aliases (See `modules/leaderboards`)

modules/follow/follow.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/GoMudEngine/GoMud/internal/parties"
1111
"github.com/GoMudEngine/GoMud/internal/plugins"
1212
"github.com/GoMudEngine/GoMud/internal/rooms"
13+
"github.com/GoMudEngine/GoMud/internal/scripting"
1314
"github.com/GoMudEngine/GoMud/internal/users"
1415
"github.com/GoMudEngine/GoMud/internal/util"
1516
)
@@ -55,6 +56,13 @@ func init() {
5556
f.plug.AddUserCommand(`follow`, f.followUserCommand, true, false)
5657
f.plug.AddMobCommand(`follow`, f.followMobCommand, true)
5758

59+
//
60+
// Register any scripting functions
61+
//
62+
// Will be available in scripts as:
63+
// module.follow.GetFollowers()
64+
f.plug.AddScriptingFunction("GetFollowers", f.Scripting_GetFollowers)
65+
5866
events.RegisterListener(events.RoomChange{}, f.roomChangeHandler)
5967
events.RegisterListener(events.PlayerDespawn{}, f.playerDespawnHandler)
6068
events.RegisterListener(events.MobIdle{}, f.idleMobHandler, events.First)
@@ -79,6 +87,18 @@ type FollowModule struct {
7987
followers map[followId]followId // key => who's following someone. value => who's being followed
8088
}
8189

90+
// Intended to be invoked by a script.
91+
func (f *FollowModule) Scripting_GetFollowers(targetActor scripting.ScriptActor) []*scripting.ScriptActor {
92+
93+
results := []*scripting.ScriptActor{}
94+
95+
for _, f := range f.getFollowers(followId{mobInstanceId: targetActor.InstanceId()}) {
96+
results = append(results, scripting.GetActor(f.userId, f.mobInstanceId))
97+
}
98+
99+
return results
100+
}
101+
82102
// Get all followeres attached to a target
83103
func (f *FollowModule) isFollowing(followCheck followId) bool {
84104
_, ok := f.followers[followCheck]

0 commit comments

Comments
 (0)