Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions _datafiles/world/default/mobs/frostfang/scripts/2-guard.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,30 @@ function onIdle(mob, room) {

return false;
}



function onPath(mob, room, eventDetails) {

if ( eventDetails.status == "waypoint" ) {

if ( UtilDiceRoll(1, 5) == 1 ) {


if ( modules.follow ) {

followingActors = modules.follow.GetFollowers(mob);

for( var i in followingActors ) {
mob.Command("sayto "+followingActors[i].ShorthandId()+" Why are you following me? Leave me be.");
mob.Command("follow lose");
break;
}

}


}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ function onIdle(mob, room) {
mob.Command("emote sniffles a bit, holding back tears.");
return true;
default: // 1, 3
if ( UtilDiceRoll(1, 10) == 1 ) {
mob.Command("pathto 274"); // look around the bushes area
return true;
}
return false;
}

Expand Down
10 changes: 6 additions & 4 deletions internal/plugins/plugincallbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

type PluginCallbacks struct {
userCommands map[string]usercommands.CommandAccess
mobCommands map[string]mobcommands.CommandAccess
userCommands map[string]usercommands.CommandAccess
mobCommands map[string]mobcommands.CommandAccess
scriptCommands map[string]map[string]any

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

func newPluginCallbacks() PluginCallbacks {
return PluginCallbacks{
userCommands: map[string]usercommands.CommandAccess{},
mobCommands: map[string]mobcommands.CommandAccess{},
userCommands: map[string]usercommands.CommandAccess{},
mobCommands: map[string]mobcommands.CommandAccess{},
scriptCommands: map[string]map[string]any{},
}
}

Expand Down
17 changes: 17 additions & 0 deletions internal/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/GoMudEngine/GoMud/internal/configs"
"github.com/GoMudEngine/GoMud/internal/mobcommands"
"github.com/GoMudEngine/GoMud/internal/mudlog"
"github.com/GoMudEngine/GoMud/internal/scripting"
"github.com/GoMudEngine/GoMud/internal/usercommands"
"github.com/GoMudEngine/GoMud/internal/util"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -228,6 +229,16 @@ func (p *Plugin) ExportFunction(stringId string, f any) {
p.exportedFunctions[stringId] = f
}

// Registers a UserCommand and callback
func (p *Plugin) AddScriptingFunction(funcName string, scriptFunc any) {

if _, ok := p.Callbacks.scriptCommands[p.name]; !ok {
p.Callbacks.scriptCommands[p.name] = map[string]any{}
}

p.Callbacks.scriptCommands[p.name][funcName] = scriptFunc
}

// Registers a UserCommand and callback
func (p *Plugin) AddUserCommand(command string, handlerFunc usercommands.UserCommand, allowWhenDowned bool, isAdminOnly bool) {

Expand Down Expand Up @@ -421,6 +432,12 @@ func Load(dataFilesPath string) {
mobcommands.RegisterCommand(cmd, info.Func, info.AllowedWhenDowned)
}

for nameSpace, funcMap := range p.Callbacks.scriptCommands {
for cmd, funcRef := range funcMap {
scripting.AddModlueFunction(nameSpace, cmd, funcRef)
}
}

// Check for config.yaml override and set missing values accordingly
OLPath := util.FilePath(`data-overlays`, `/`, `config.yaml`)
if b, err := p.files.ReadFile(OLPath); err == nil {
Expand Down
20 changes: 20 additions & 0 deletions internal/scripting/module_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package scripting

import (
"github.com/dop251/goja"
)

var (
moduleFunctions = map[string]map[string]any{}
)

func AddModlueFunction(namespace string, name string, funcRef any) {
if _, ok := moduleFunctions[namespace]; !ok {
moduleFunctions[namespace] = map[string]any{}
}
moduleFunctions[namespace][name] = funcRef
}

func setModuleFunctions(vm *goja.Runtime) {
vm.Set("modules", moduleFunctions)
}
1 change: 1 addition & 0 deletions internal/scripting/scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func setAllScriptingFunctions(vm *goja.Runtime) {
setSpellFunctions(vm)
setItemFunctions(vm)
setUtilFunctions(vm)
setModuleFunctions(vm)
}

func PruneVMs(forceClear ...bool) {
Expand Down
1 change: 1 addition & 0 deletions modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Extract any modules into this folder.
* Add/Over-write existing template files (See `modules/auctions`)
* Add/Over-write help files (See `modules/auctions`)
* Add/Over-write user or mob commands (See `modules/auctions`)
* Add functions for scripting (See `modules/follow`)
* Save/Load their own data (See `modules/leaderboards`)
* Track their own config values (See `modules/leaderboards`)
* Modify help menu items, command aliases, help aliases (See `modules/leaderboards`)
Expand Down
20 changes: 20 additions & 0 deletions modules/follow/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/GoMudEngine/GoMud/internal/parties"
"github.com/GoMudEngine/GoMud/internal/plugins"
"github.com/GoMudEngine/GoMud/internal/rooms"
"github.com/GoMudEngine/GoMud/internal/scripting"
"github.com/GoMudEngine/GoMud/internal/users"
"github.com/GoMudEngine/GoMud/internal/util"
)
Expand Down Expand Up @@ -55,6 +56,13 @@ func init() {
f.plug.AddUserCommand(`follow`, f.followUserCommand, true, false)
f.plug.AddMobCommand(`follow`, f.followMobCommand, true)

//
// Register any scripting functions
//
// Will be available in scripts as:
// module.follow.GetFollowers()
f.plug.AddScriptingFunction("GetFollowers", f.Scripting_GetFollowers)

events.RegisterListener(events.RoomChange{}, f.roomChangeHandler)
events.RegisterListener(events.PlayerDespawn{}, f.playerDespawnHandler)
events.RegisterListener(events.MobIdle{}, f.idleMobHandler, events.First)
Expand All @@ -79,6 +87,18 @@ type FollowModule struct {
followers map[followId]followId // key => who's following someone. value => who's being followed
}

// Intended to be invoked by a script.
func (f *FollowModule) Scripting_GetFollowers(targetActor scripting.ScriptActor) []*scripting.ScriptActor {

results := []*scripting.ScriptActor{}

for _, f := range f.getFollowers(followId{mobInstanceId: targetActor.InstanceId()}) {
results = append(results, scripting.GetActor(f.userId, f.mobInstanceId))
}

return results
}

// Get all followeres attached to a target
func (f *FollowModule) isFollowing(followCheck followId) bool {
_, ok := f.followers[followCheck]
Expand Down
Loading