assofohdz / subspace-infinity

A spiritual and technical successor the infamous 2d space mmo Subspace
GNU General Public License v3.0
16 stars 1 forks source link

Server/Mods: Implement Mod Pack functionality #63

Open assofohdz opened 1 year ago

assofohdz commented 1 year ago

Implement a system that can load groovy mod packs (much like the dynamic loading of java classes that is in already):

https://github.com/pspeed42/moss/tree/master/action/src/main/java/com/simsilica/action https://github.com/pspeed42/moss/tree/master/mmod

See also: https://github.com/assofohdz/moss/blob/master/tools/blocked2/src/main/java/com/simsilica/blocked/ScriptState.java

and https://github.com/assofohdz/moss/tree/master/tools/mapper/sample/scripts

This is a lot of new stuff to take in, but would be very cool to have in.

assofohdz commented 1 year ago

base-apis.json

{
    "groupId":"com.simsilica",
    "name":"base-apis",    
    "formatVersion":"1.0",
    "author":"Simsilica, LLC",
    "version":"1.0",
    dependencies: [
        "com.simsilica:base-includes"
    ],
    initScripts: [
        "base-apis.groovy"
    ],
    config: {
    }
}

modpack json file:

{
    "groupId":"com.simsilica",
    "name":"base-includes",    
    "formatVersion":"1.0",
    "author":"Simsilica, LLC",
    "version":"1.0",
    dependencies: [        
    ],
    initScripts: [
        "base-includes.groovy"
    ],
    config: {
    }
}

base-includes.groovy:

modManager.addGlobalImport("com.simsilica.action.Option");
modManager.addGlobalImport("com.simsilica.action.PromptType");
modManager.addGlobalImport("com.simsilica.mblock.*");
...

Setting up modmanager:

modManager = new ModManager();
        modManager.loadFromResource("/mythruna/base-includes.json");
        modManager.loadFromResource("/mythruna/base-apis.json");
        modManager.loadFromResource("/mythruna/base-object-api.json");
        modManager.loadFromResource("/mythruna/base-hooks-api.json");
        modManager.loadFromResource("/mythruna/base-time-api.json");
        modManager.loadFromResource("/mythruna/base-encounter-api.json");
        modManager.loadFromResource("/mythruna/objects/standard-objects.json");
        modManager.loadFromResource("/mythruna/encounters/standard-encounters.json");
        modManager.loadFromResource("/mythruna/property/base-property.json");
        modManager.loadFromResource("/mythruna/base-accounts.json");
....
        modManager.setGlobalBinding("world", world);

...then sometime later I initialize it.

message.txt

assofohdz commented 1 year ago

Entity.metadata:

EntityId.metaClass {    
    getAt() { Class c ->
        return entityData.getComponent(delegate, c);
    }

    get() { String name ->
        // Look up the object type
        def objectType = objectTypes.getType(delegate);
        return resolveProperty(objectType, delegate, name);
    }

    isSameEntity() { Object other ->
        return resolveEntityId(other)?.id == delegate.id; 
    }

    leftShift() { EntityComponent component ->
        entityData.setComponent(delegate, component);
    }

    leftShift() { List components ->
        entityData.setComponents(delegate, components as EntityComponent[]);
    }

    remove() { Class type ->
        entityData.removeComponent(delegate, type);
    }

    contains() { Object o ->
        def other = resolveEntityId(o);
        def contained = other[ContainedIn];
        if( contained == null ) {
            // It's not contained in anything
            return false;
        }
        // Are we its root?
        if( delegate == contained.root ) {
            // We are their root so contain them by definition
            return true;
        }

        // Check the hiearchy starting here
        while( contained != null ) {
            // Are we the container?
            if( delegate == contained.container ) {
                return true;
            }
            // Try the parent
            contained = contained.container[ContainedIn];
        }                
        return false;  
    }

    getType() { ->
        return objectTypes.getType(delegate);
    }

    getContext() { ->
        return objectTypes.getContext(delegate);
    }        

    run() { ActionEnvironment env, ...vargs ->
        if( log.isDebugEnabled() ) {
            log.debug("run:" + vargs);
        }
        return objectTypes.getContext(delegate).run(env, *vargs);
    }
}