[Plugin] MyScript - Use JavaScript to script wool-screens (with video)

Discussion in 'Plugin Development' started by LennardF1989, Jul 24, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    LennardF1989

    In my spare time I have been working on implementing JavaScript into a plugin called MyScript, so it is much easier to script specific redstone circuits (including games, if you wish).

    At this point I am far enough to show off my first example: Conways Game of Life.

    The Game of Life is pretty simple, take a 2 dimensional grid, make some cells live and apply a few simple rules each loop (called a generation):
    1. If a dead cell has exactly 3 neighbours, it becomes live
    2. If a live cell has less than 2 or more than 3 neigbours, it dies
    3. Otherwise, the cell stays alive
    When you start with the following pattern (called a "Gosper Glider Gun"):
    Glider Gun.png

    You'll get the following result:


    Here's the script I used:
    Code:javascript
    1. //Get the LCD
    2. var lcd = screen("LCD");
    3. var width = lcd.width();
    4. var height = lcd.height();
    5.  
    6. //Prepare the neighbours array
    7. var neighbours = new Array(9);
    8. var count;
    9. var x;
    10. var y;
    11. var i;
    12.  
    13. //Create a glider gun
    14. lcd.backBuffer(2,9,15);
    15. //... omitted
    16. lcd.backBuffer(37,8,15);
    17. lcd.flush();
    18. wait(1000);
    19.  
    20. //Update generation
    21. while(true) {
    22. for(x = 0; x < width; x++) {
    23. for(y = 0; y < height; y++) {
    24. //First get all neighbours
    25. neighbours[0] = lcd.frontBuffer(x - 1, y - 1);
    26. neighbours[1] = lcd.frontBuffer(x, y - 1);
    27. neighbours[2] = lcd.frontBuffer(x + 1, y - 1);
    28. neighbours[3] = lcd.frontBuffer(x - 1, y);
    29. neighbours[4] = lcd.frontBuffer(x, y);
    30. neighbours[5] = lcd.frontBuffer(x + 1, y);
    31. neighbours[6] = lcd.frontBuffer(x - 1, y + 1);
    32. neighbours[7] = lcd.frontBuffer(x, y + 1);
    33. neighbours[8] = lcd.frontBuffer(x + 1, y + 1);
    34.  
    35. //Reset count
    36. count = 0;
    37.  
    38. //Count live neigbours
    39. for(i = 0; i < 9; i++) {
    40. if(i != 4 && neighbours == 15) {
    41. count++;
    42. }
    43. }
    44.  
    45. //Apply the rules
    46. //If a dead cell has exactly 3 neighbours, it becomes live
    47. //If a live cell has less than 2 or more than 3 neigbours, it dies
    48. //Otherwise, the cell stays alive
    49. if(neighbours[4] == 0 && count == 3) {
    50. lcd.backBuffer(x, y, 15);
    51. }
    52. else if(neighbours[4] == 15 && (count < 2 || count > 3)) {
    53. lcd.backBuffer(x, y, 0);
    54. }
    55. }
    56. }
    57.  
    58. //Flush the result to the frontbuffer
    59. lcd.flush();
    60. wait(1000);
    61. }
    The functions "screen" and "wait" are native functions I defined in the plugin. The screen (which I gave the name "LCD" and is 40x20 in size) is defined using a Sign:
    2011-07-24_23.19.05.png

    I protected the code against endless loops and you are forced to wait at least 1 second between each flush call. You also cannot access any underlying Java-code unless I allow a the script to. Every script runs in his own thread and can be killed at any moment. I ran this for about 25 minutes and I didn't experience any problems, so I'm very happy. Though I did notice a bug, which is possibly inside Bukkit: at some point, blocks stop updating, as if Minecraft gives up updating because they get updated too often. Is this a known bug or intented feature?

    Hope you enjoyed my little preview,
    Lennard
     
    zhuowei likes this.
  2. Offline

    Acrobot

    Nice one!
     
Thread Status:
Not open for further replies.

Share This Page