Mythruna
March 29, 2024, 04:55:53 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome to the new forums. See "Announcements" for a note for new users.
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: My Scripts  (Read 8843 times)
Toboi
Newbie
*
Posts: 9


View Profile
« on: January 03, 2012, 10:20:22 AM »

Hi there!
I've written a little script for building or removing bigger things with one click.
I wrote it fast and copy&pasted a bit, so it doesn't look nice and probably it could have been done better. But I did this mainly for my fun and to practice Groovy and the (known) Mythruna-API. But if you have suggestions to improve the script, I would be happy.

The problem at the moment is that i have no environment to test it with multiple people online. I'm not sure, but I think the size-variables are global, so each player has the same.
Is this right? And if yes, how could I fix this?
Thanks, Toboi
Code:
import mythruna.*;
import mythruna.db.tree.*;
import mythruna.es.*;
import mythruna.script.*;
int size_xl = 0;
int size_yl = 0;
int size_zl = 0;
int size_xh = 0;
int size_yh = 0;
int size_zh = 0;
int type_block = 0;
int size_remove_cubel = 0;
int size_remove_cubeh = 0;
action( group:"Remove", name:"Remove cube3", type:ActionType.Block ) {

    loc = it.getBlock();
for (i in (-1..<2)) {
for (j in (-1..<2)) {
for (k in (-1..<2)) {

    setCellType(loc.x + i, loc.y + j, loc.z + k, 0);

}}
}
}
action( group:"Remove", name:"Remove cube5", type:ActionType.Block ) {

    loc = it.getBlock();
for (i in (-2..<3)) {
for (j in (-2..<3)) {
for (k in (-2..<3)) {

    setCellType(loc.x + i, loc.y + j, loc.z + k, 0);

}}
}
}


action( group:"Remove", name:"Remove cube7", type:ActionType.Block ) {

    loc = it.getBlock();
for (i in (-3..<4)) {
for (j in (-3..<4)) {
for (k in (-3..<4)) {

    setCellType(loc.x + i, loc.y + j, loc.z + k, 0);

}}
}
}
action( group:"Remove", name:"Remove cube with own size", type:ActionType.Block ) {

    loc = it.getBlock();
for (i in (-size_remove_cubel..<size_remove_cubeh)) {
for (j in (-size_remove_cubel..<size_remove_cubeh)) {
for (k in (-size_remove_cubel..<size_remove_cubeh)) {

    setCellType(loc.x + i, loc.y + j, loc.z + k, 0);

}}
}
}
action( group:"Big", name:"Water3x3x3", type:ActionType.Block ) {

loc = it.getBlock();
for (i in (-1..<2)) {
for (j in (-1..<2)) {
for (k in (-1..<2)) {
if(getCellType(loc.x + i, loc.y + j, loc.z + k) == 0){
     setCellType(loc.x + i, loc.y + j, loc.z + k, 7);
}
}
}
}
}
action( group:"Big", name:"Water5x5x5", type:ActionType.Block ) {

loc = it.getBlock();
for (i in (-2..<3)) {
for (j in (-2..<3)) {
for (k in (-2..<3)) {
if(getCellType(loc.x + i, loc.y + j, loc.z + k) == 0){
     setCellType(loc.x + i, loc.y + j, loc.z + k, 7);
}
}
}
}
}
action( group:"Big", name:"Water7x7x7", type:ActionType.Block ) {

loc = it.getBlock();
for (i in (-3..<4)) {
for (j in (-3..<4)) {
for (k in (-3..<4)) {
if(getCellType(loc.x + i, loc.y + j, loc.z + k) == 0){
     setCellType(loc.x + i, loc.y + j, loc.z + k, 7);
}
}
}
}
}
action( group:"Big", name:"Own", type:ActionType.Block ) {

loc = it.getBlock();
for (i in (-size_xl..<size_xh)) {
for (j in (-size_yl..<size_yh)) {
for (k in (-size_zl..<size_zh)) {
     setCellType(loc.x + i, loc.y + j, loc.z + k, type_block);
}
}
}
}
action( group:"Big", name:"Own if free", type:ActionType.Block ) {

loc = it.getBlock();
for (i in (-size_xl..<size_xh)) {
for (j in (-size_yl..<size_yh)) {
for (k in (-size_zl..<size_zh)) {
if(getCellType(loc.x + i, loc.y + j, loc.z + k) == 0){
     setCellType(loc.x + i, loc.y + j, loc.z + k, type_block);
}
}
}
}
}
on( [playerJoined] ) {
    type, event ->
    conn = connection;
    p = conn.getAttribute( "player" );

    println( "Adding removing tools to player:" + player );

    refs = []

    ToolActions existing = player[ToolActions.class];
 
    if( playerData != null ) {          
            refs += actions.getRef( "Remove", "Remove cube3" );
            refs += actions.getRef( "Remove", "Remove cube5" );
            refs += actions.getRef( "Remove", "Remove cube7" );
            refs += actions.getRef( "Remove", "Remove cube with own size" );
            refs += actions.getRef( "Big", "Own big block" );
            refs += actions.getRef( "Big", "Own big block if free" );
            refs += actions.getRef( "Big", "Water3x3x3" );
            refs += actions.getRef( "Big", "Water5x5x5" );
            refs += actions.getRef( "Big", "Water7x7x7" );
        
    }            
    
    println "Refs:" + refs;
    player << new ToolActions(refs, existing)
p.set( "big.size", 2 );
addShellCommand( shell, "insertsize", "Sets the size of the mass-insert block.", null ) {
          
        String[] parms = it.split( " " );
        if( it.length() == 0 || !parms.length == 4 ) {
            console.echo( "Usage: ~insertsize x y z blocktype / Build with Blocktype 'Own' to override or 'Own if free' to fill out" );
        }else{
size_xl = (parms[0].toInteger()) / 2;
size_yl = (parms[1].toInteger()) / 2;
size_zl = (parms[2].toInteger()) / 2;
size_xh = (parms[0].toInteger() + 1) / 2;
size_yh = (parms[1].toInteger() + 1) / 2;
size_zh = (parms[2].toInteger() + 1) / 2;
type_block = parms[3].toInteger();
}
    }
addShellCommand( shell, "removesize", "Sets the size of the mass-removal block.", null ) {
          
        String[] parms = it.split( " " );
        if( it.length() == 0 || !parms.length == 1 ) {
            console.echo( "Usage: ~removesize size / Remove with Blocktype 'Remove cube with own size'" );
        }else{
size_remove_cubel = (parms[0].toInteger()) / 2;
size_remove_cubeh = (parms[0].toInteger() + 1) / 2;
}
    }}

« Last Edit: January 23, 2012, 01:25:31 PM by Toboi » Logged
pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« Reply #1 on: January 03, 2012, 10:48:08 AM »

Yeah, as written those sizes will be global.

I thought I had included a general player-specific place to tuck variables but it looks like you will have to use the shell environment for now (which is per player).  On a server you can also store data in the connection attributes but that is not useful for single player.

I would create a class inside your script to hold all of the sizes as it will make your life slightly easier.

Code:
class Sizes {
    int size_xl = 0;
    int size_yl = 0;
    int size_zl = 0;
    int size_xh = 0;
    int size_yh = 0;
    int size_zh = 0;
}

Inside your shell commands you can actually just set things directly, I think.  So if you had:
Code:
if( sizes == null ) {
    sizes = new Sizes();
}

At the beginning, I'm 90% sure that is tucked away in the shell environment.

From your action scripts, I think you can get to it like:
Code:
mySizes = shell.shellEnvironment.variables.sizes

I have not tested this.

Per player local variables will be handled in a more elegant way in the future... that will work the same in shell commands, action scripts, and dialog scripts, etc.
Logged
pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« Reply #2 on: January 03, 2012, 10:49:33 AM »

I'm adding a to-do list item to unify player environment variables maybe for the next release.

If you want, you can start a thread to track a scripting API wish list.
Logged
Toboi
Newbie
*
Posts: 9


View Profile
« Reply #3 on: January 23, 2012, 01:37:29 PM »

I've written another script.
(I didn't want to make a new thread, so i just changed topic   Wink)It allows you to easily teleport to defined points and to define these points.
It has no privilege management in it, so anyone could add points. With a big server and many points it would be probably useful to manage the points a bit better, the way it is now it can just show you all points.
Usage:
~addTP New Point - adds a Point with the name "New Point"
~teleports - listens all existing points
~teleport 1 - teleports you to the point with this number.

The points are saved in an extern file, named "teleport.data" in the following format:
id,name,x,y,z

If you decide to edit this file(to delete points for example, that's not (yet?) possible in game), you should pay attention that the id of the last entry is the highest id. The ID's are only in this way managed, that the next ID is the last ID + 1. So if the last id is 2 but you already have a entry with ID 3, and you add a Point, there will be two entrys with ID 3. And this could make it quite difficult to teleport to the second entry with this ID^^

So, here is the script:
Code:
import com.jme3.math.*;

import mythruna.Coordinates;
import mythruna.db.*;
class port {
int x = 0;
int y = 0;
int z = 0;
int id = 0;
String name = "";
}
int nxtId = 3
ports = []


f = new File('teleport.data')
if(f.exists()){
f.eachLine { line ->
  data = line.split(',')
  newPort = new port();
  newPort.id = data[0].toInteger();
  newPort.name = data[1]
  newPort.x = data[2].toInteger();
  newPort.y = data[3].toInteger();
  newPort.z = data[4].toInteger();
  ports.add([id:data[0], name:data[1], x:data[2], y:data[3], z:data[4]])
  nxtId = data[0].toInteger() + 1
}    
}else{
  ports = [
            [id:'1', name:'Spawn',x:'512', y:'512', z:'80']
  ]    
  ports.each {  
    def row = [it.id, it.name, it.x, it.y, it.z]
    f.append(row.join(','))  
    f.append('\n')    
}
}

  
println("Existing Teleport-ports:")
ports.each {  
    def row = [it.id, it.name, it.x, it.y, it.z]
    println(row.join(','))  
}
on( [playerConnected] ) {
    type, event ->
        
    conn = connection;
    p = conn.getAttribute( "player" );

    addShellCommand( shell, "addTP", "Adds a Teleport-Point", null ) {
        String[] parms = it.split( " " );
        if( parms.length > 0 ) {
        pos = getLocation( conn );
            ports.add([id:nxtId, name:it, x:pos.x.toInteger(), y:pos.y.toInteger(), z:pos.z.toInteger()])
            def row = [nxtId, it, pos.x.toInteger(), pos.y.toInteger(), pos.z.toInteger()]
            f.append('\n' + row.join(','))
            console.echo("Added Port. ID: " + nxtId)
            nxtId++
        }else{
            console.echo("Wrong number of params. Usage: ~addTP [name of port]")
        }    
    }
    
    
    addShellCommand( shell, "teleport", "Teleports to a predefined Point(list all points with ~teleports)", null ) {
        String[] parms = it.split( " " );
        if( parms.length == 1 ) {
        

        int num = parms[0].toInteger() - 1
            warp(conn, ports[num].x.toInteger(), ports[num].y.toInteger(),ports[num].z.toInteger() + 1)
            console.echo("Teleporting to: " + ports[num].name)  
        }else{
            console.echo("Wrong number of params. Usage: ~teleport [port(number)]")
        }    
    }
    addShellCommand( shell, "teleports", "Shows all Teleport-Points", null ) {
            ports.each {  
               def row = [it.id, it.name, it.x, it.y, it.z]
               console.echo(row.join(','))    
            }
            
    }
}

//Edit:
I copied the imports from other scripts. Which ones do I actually need?
« Last Edit: January 23, 2012, 01:39:15 PM by Toboi » Logged
pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« Reply #4 on: January 23, 2012, 01:51:31 PM »

Neat.

Doesn't look like you need either of the imports.

After the next release (whenever that is), I will circle back and show you how you can manage teleport locations through the entity system... then you won't need an external file and could even keep the teleports per player if desired.
Logged
Moonkey
Hero Member
*****
Posts: 1587

This is probably a picture.


View Profile
« Reply #5 on: February 28, 2012, 09:46:51 AM »

I've noticed something wierd. I didn't think it was true but...

name="Golden CraftingBench"
id=203;
texturefile="ModBlocks.png";
textureindextop=4;
textureindexeast=3;
textureindexwest=3;
textureindexsouth=3;
textureindexnorth=3;
texturebottomindex=5;
material="rock";
stepsound="stone";
hardness=1.5;
resistance=5;
rightclicked="customstuff.openGui(world, origin, 'defaultcraft4x4', player);";


I actually do real coding lol. Minecrafts crafting benches are 3x3 right? I use a mod that makes mods the easy way for minecraft. And it has gui's for 4x4 crafting 5x5, 6x6, and 7x7. (46 squares to place on 7x7. What would you need to craft that size o.o). Strange. I'm now interested in scripting mythruna.
Logged

Mythruna: Don't you dare read any posts I made before 2014.
pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« Reply #6 on: February 28, 2012, 10:20:52 AM »

Big scripting update coming soon.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.20 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!