Skip to content

Instantly share code, notes, and snippets.

@walterhiggins
Last active November 21, 2015 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterhiggins/fc0193baeaa8f5fddacd to your computer and use it in GitHub Desktop.
Save walterhiggins/fc0193baeaa8f5fddacd to your computer and use it in GitHub Desktop.

Some notes on the ScriptCraft Workshop for CoderDojo@IBM

There were some very good ideas for improvements and additions. One attendee wondered if it was possible to populate buildings as they were built. I thought I'd already written a spawn module to do this but it turns out I hadn't. I've just added a spawn module to version 3.1.11 which is out today (21st Nov 2015). Get it here http://scriptcraftjs.org/download/latest/

The Drone now also has a spawn method so you can for example unleash a horde of zombies by combining the spawn(), movement and times() functions like this...

/js spawn('ZOMBIE').fwd().times(10)

... unleashes a line of 10 zombies all standing one behind the other.

To see a full list of all possible entity types that can be spawned, you'll need to to the following (I should make this easier in a subsequent release)...

/js var ents = require('entities');
/js ents. 

.. then press the TAB key to see all possible completions.

Happy Spawning.

Another question that came up...

How do I change all blocks of one type to another within a given radius?

This one is tricky. Let's assume that rather than a radius we limit the extent to a cube (rather than a sphere). Let's assume we need a function which will take the following parameters:

  • Starting location
  • Extent along each axis (x, y, z)
  • original block type
  • new block type

We'll need to check each block's type along 3 dimensions so we'll need 3 nested for loops. The function might look like this...

// scriptcraft/plugins/xform-blocks.js
function xformBlocks( location, extent, oldBlockType, newBlockType){
  var startX = location.x - extent;
  var endX = location.x + extent;
  var startY = location.y - extent;
  var endY = location.y + extent;
  var startZ = location.z - extent;
  var endZ = location.z + extent;
  var x, y, z, block, world;
  world = location.world;
  for (x = startX; x < endX; x++){
    for (y = startY; y < endY; y++){
      for (z = startZ; z < endZ; z++){
        block = world.getBlockAt(x, y, z);
        if (block.typeId == oldBlockType){
          block.typeId = newBlockType;
          block.update();
        }
      }
    }
  }
}
exports.xformBlocks = xformBlocks;

To use this function at the in-game prompt you'd need to add the above file to the plugins directory and use it like this...

/js xformBlocks( self.location, 5, blocks.grass, blocks.gold )

Which would transform all grass blocks in a 10x10x10 cube centered on the player, into gold blocks. I haven't verified this code will work yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment