Rhino JS Scriptable block

    Good idea?

    • Yes

      Votes: 1 100.0%
    • No

      Votes: 0 0.0%
    • Maybe

      Votes: 0 0.0%

    • Total voters
      1
    Joined
    Feb 18, 2015
    Messages
    9
    Reaction score
    3
    A few ideas for the implementation of a scriptable block:

    I know LUA is already supported by the engine, however i would like to propose what i honestly
    think is a better and more viable alternative.

    Javascript, more specificly Rhino.
    The advantages are higher performance, better events, a more consistent syntax and less memory usage.

    In-game implementation:

    One script block.
    I/O blocks.

    The script block has a editor, much like the display-text editor, and it saves and executes the script.
    For IO, the idea is that you have the ability to label activation-blocks, and then link them to the script block.
    Then you can simply access them as boolean variables inside the script.


    Engine implementation:

    The reason i specified performance as one of the advantages, is because rhino compiles javascript into
    java bytecode, which is then run on the JVM. This is basicly as fast as the engine code itself.
    Rhino also supports sandboxing, so you can limit to a few certain functions that the script is allowed to call.
    A seperate thread to run the scripts would also be desireable.

    For lag prevention, i suggest a per-player limit of number of instructions every tick.
    For example every player gets a maximum of 5000 bytecode instructions, this prevents people from making laggy scripts. (also prevents infinite loops)
    (Have one script block that runs at 2500ops, then you have used half of your quota)

    This can be implemented fairly easily.

    context.setInstructionObserverThreshold( numberOfInstructions );


    In Rhino you can also implement Java Interfaces and abstract classes which reside in the engine, for example:

    Java:
    public interface ChatListener {

    public void onChat(Player player, String text);​
    }


    JS:
    var chatListener = new ChatListener(){

    onChat: function(player, text){​

    if(player !== owner()){
    return;​
    }​

    if(text == "!hangar"){
    hangarBay = true;​
    }​
    }​
    };

    Other ideas for interfaces:
    DamageListener, ActivationListener, StorageListener.

    Function ideas:
    aimTurret(x,y)
    Obj = Ranger(pos,dir,distance) // returns {distance: number, hitEntity: entity, hit: boolean}
    applyThrust(dir)
    applyAngThrust(ang)

    A example Rhino implementation i did for a webserver: https://github.com/LeifRoss/Server_System_C/blob/master/src/gc/server/util/ScriptEnvironment.java
    (Can obviously be done alot better, but i was very short on time)


    Rhino: https://developer.mozilla.org/en-US/docs/Rhino_documentation