Esempio n. 1
0
def listen(s, mc):
    mcpiX = 0
    mcpiY = 0
    mcpiZ = 0
    mcpiX0 = 0
    mcpiY0 = 0
    mcpiZ0 = 0
    mcpiX1 = 0
    mcpiY1 = 0
    mcpiZ1 = 0
    blockTypeId = 1
    blockData = 0

    # Minecraft Graphics Turtle
    speed = 10
    steps = 0
    degrees = 0

    mc = minecraft.Minecraft.create()
    pos = mc.player.getPos()
    steve = turtle.MinecraftTurtle(mc, pos)

    # Minecraft Stuff
    radius = 0
    shapePoints = []
    fill = True
    mcDrawing = stuff.MinecraftDrawing(mc)

    for msg in _listen(s):
        if (msg):
            print "Received: %s" % str(msg)
            if msg[0] == 'broadcast':
                if not mc:
                    mc = minecraft.Minecraft.create()
                if msg[1] == 'hello_minecraft':
                    mc.postToChat("hello minecraft")
                elif msg[1] == 'setPos':
                    if (is_number(mcpiX) and is_number(mcpiY)
                            and is_number(mcpiZ)):
                        mc.player.setPos(mcpiX, mcpiY, mcpiZ)
                        print "setPos: %.1f %.1f %.1f" % (mcpiX, mcpiY, mcpiZ)
                elif msg[1] == 'setBlock':
                    if (is_number(mcpiX) and is_number(mcpiY)
                            and is_number(mcpiZ) and is_number(blockTypeId)
                            and is_number(blockData)):
                        mc.setBlock(mcpiX, mcpiY, mcpiZ, blockTypeId,
                                    blockData)
                        print "setBlock: %d %d %d %d %d" % (
                            mcpiX, mcpiY, mcpiZ, blockTypeId, blockData)
                # Minecraft Graphics Turtle(Start)
                elif msg[1] == 'turtle:setPos':
                    if is_number(mcpiX) and is_number(mcpiY) and is_number(
                            mcpiZ):
                        steve.setposition(mcpiX, mcpiY, mcpiZ)
                        print "turtle:setPos: %.1f %.1f %.1f" % (mcpiX, mcpiY,
                                                                 mcpiZ)
                elif msg[1] == 'turtle:forward':
                    if is_number(steps):
                        steve.forward(steps)
                        print "steve.forward: (%d)" % (steps)
                elif msg[1] == 'turtle:backward':
                    if is_number(steps):
                        steve.backward(steps)
                        print "steve.backward: (%d)" % (steps)
                elif msg[1] == 'turtle:right':
                    if is_number(degrees):
                        steve.right(degrees)
                        print "steve.right: (%d)" % (degrees)
                elif msg[1] == 'turtle:left':
                    if is_number(degrees):
                        steve.left(degrees)
                        print "steve.left: (%d)" % (degrees)
                elif msg[1] == 'turtle:up':
                    if is_number(degrees):
                        steve.up(degrees)
                        print "steve.up: (%d)" % (degrees)
                elif msg[1] == 'turtle:down':
                    if is_number(degrees):
                        steve.down(degrees)
                        print "steve.down: (%d)" % (degrees)
                elif msg[1] == 'turtle:penup':
                    steve.penup()
                    print "steve.penup"
                elif msg[1] == 'turtle:pendown':
                    steve.pendown()
                    print "steve.pendown"
                elif msg[1] == 'turtle:setheading':
                    if is_number(degrees):
                        steve.setheading(degrees)
                        print "steve.setheading: (%d)" % (degrees)
                elif msg[1] == 'turtle:setverticalheading':
                    if is_number(degrees):
                        steve.setverticalheading(degrees)
                        print "steve.setverticalheading: (%d)" % (degrees)
                # Minecraft Graphics Turtle(End)
                # Minecraft Stuff(Start)
                elif msg[1] == 'stuff:drawLine':
                    mcDrawing.drawLine(int(mcpiX1), int(mcpiY1), int(mcpiZ1),
                                       int(mcpiX), int(mcpiY), int(mcpiZ),
                                       blockTypeId, blockData)
                    print "mcDrawing.drawLine: (%d, %d, %d, %d, %d, %d, %d, %d)" % (
                        mcpiX1, mcpiY1, mcpiZ1, mcpiX, mcpiY, mcpiZ,
                        blockTypeId, blockData)
                elif msg[1] == 'stuff:drawSphere':
                    mcDrawing.drawSphere(mcpiX, mcpiY, mcpiZ, radius,
                                         blockTypeId, blockData)
                    print "mcDrawing.drawSphere: (%d, %d, %d, %d, %d, %d)" % (
                        mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                elif msg[1] == 'stuff:drawCircle':
                    mcDrawing.drawCircle(mcpiX, mcpiY, mcpiZ, radius,
                                         blockTypeId, blockData)
                    print "mcDrawing.drawCircle: (%d, %d, %d, %d, %d, %d)" % (
                        mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                elif msg[1] == 'stuff:resetShapePoints':
                    shapePoints = []
                    mcDrawing = stuff.MinecraftDrawing(mc)
                elif msg[1] == 'stuff:setShapePoints':
                    shapePoints.append(
                        minecraft.Vec3(int(mcpiX), int(mcpiY), int(mcpiZ)))
                    print "append.shapePoints:"
                    print ' '.join(str(p) for p in shapePoints)
                elif msg[1] == 'stuff:drawFace':
                    if (fill == 'True'):
                        fillFlag = True
                    elif (fill == 'False'):
                        fillFlag = False
                    mcDrawing.drawFace(shapePoints, fillFlag, blockTypeId)
                    print "mcDrawing.drawFace:"
                    print ' '.join(str(p) for p in shapePoints)
                    print(fill)
                    print(blockTypeId)
                # Minecraft Stuff(End)

                elif msg[1] == 'setBlocks':
                    if (is_number(mcpiX0) and is_number(mcpiY0)
                            and is_number(mcpiZ0) and is_number(mcpiX1)
                            and is_number(mcpiY1) and is_number(mcpiZ1)
                            and is_number(blockTypeId)
                            and is_number(blockData)):
                        mc.setBlocks(mcpiX0, mcpiY0, mcpiZ0, mcpiX1, mcpiY1,
                                     mcpiZ1, blockTypeId, blockData)
                        print "setBlocks(%d, %d, %d, %d, %d, %d, %d, %d" % (
                            mcpiX0, mcpiY0, mcpiZ0, mcpiX1, mcpiY1, mcpiZ1,
                            blockTypeId, blockData)
                elif msg[1] == 'getPos':
                    playerPos = mc.player.getPos()
                    pos = playerPos
                    s.sensorupdate({
                        'playerX': playerPos.x,
                        'playerY': playerPos.y,
                        'playerZ': playerPos.z
                    })
                elif msg[1] == 'getHeight':
                    posY = mc.getHeight(mcpiX, mcpiZ)
                    s.sensorupdate({'posY': posY})
                    mc.postToChat("posY: %d" % posY)
                elif msg[1] == 'getBlock':
                    blockFound = mc.getBlockWithData(mcpiX, mcpiY, mcpiZ)
                    s.sensorupdate({
                        'blockTypeId': blockFound.id,
                        'blockData': blockFound.data
                    })
                elif msg[1] == 'pollBlockHits':
                    blockEvents = mc.events.pollBlockHits()
                    print blockEvents
                    if blockEvents:
                        blockEvent = blockEvents[-1]
                        s.sensorupdate({
                            'blockEventX':
                            blockEvent.pos.x,
                            'blockEventY':
                            blockEvent.pos.y,
                            'blockEventZ':
                            blockEvent.pos.z,
                            'blockEventFace':
                            blockEvent.face,
                            'blockEventEntityId':
                            blockEvent.entityId
                        })
                    else:
                        s.sensorupdate({
                            'blockEventX': '',
                            'blockEventY': '',
                            'blockEventZ': '',
                            'blockEventFace': '',
                            'blockEventEntityId': ''
                        })
                elif msg[1] == 'reset':
                    mc.postToChat('reset the world')
                    mc.setBlocks(-100, 0, -100, 100, 63, 100, 0, 0)
                    mc.setBlocks(-100, -63, -100, 100, -2, 100, 1, 0)
                    mc.setBlocks(-100, -1, -100, 100, -1, 100, 2, 0)
                    mc.player.setPos(0, 0, 0)
                elif msg[1] == 'echo' or msg[1].startswith('echo '):
                    words = msg[1].split()
                    mc.postToChat(" ".join(words[1:]))
            elif msg[0] == 'sensor-update':
                mcpiX = msg[1].get('mcpiX', mcpiX)
                mcpiY = msg[1].get('mcpiY', mcpiY)
                mcpiZ = msg[1].get('mcpiZ', mcpiZ)
                mcpiX0 = msg[1].get('mcpiX0', mcpiX0)
                mcpiY0 = msg[1].get('mcpiY0', mcpiY0)
                mcpiZ0 = msg[1].get('mcpiZ0', mcpiZ0)
                mcpiX1 = msg[1].get('mcpiX1', mcpiX1)
                mcpiY1 = msg[1].get('mcpiY1', mcpiY1)
                mcpiZ1 = msg[1].get('mcpiZ1', mcpiZ1)
                blockTypeId = msg[1].get('blockTypeId', blockTypeId)
                blockData = msg[1].get('blockData', blockData)

                # Minecraft Graphics Turtle
                speed = msg[1].get('speed', speed)
                steps = msg[1].get('steps', steps)
                degrees = msg[1].get('degrees', degrees)

                # Minecraft Stuff
                radius = msg[1].get('radius', radius)
                fill = msg[1].get('fill', fill)
Esempio n. 2
0
# Minecraft Turtle Example
from mcturtle import minecraftturtle
from mcpi import minecraft
from mcpi import block

# create connection to minecraft
mc = minecraft.Minecraft.create()

# get players position
pos = mc.player.getPos()

# create minecraft turtle
steve = minecraftturtle.MinecraftTurtle(mc, pos)

steve.speed(0)
steve.penblock(block.WOOL.id, 14)
S = 50
for j in range(0, 20):
    steve.up(j * 10)
    steve.forward(S)

    steve.left(90)
    steve.down(j * 10)
    steve.forward(S)

    steve.left(90)
    steve.down(j * 10)
    steve.forward(S)

    steve.left(90)
    steve.up(j * 10)
def listen(s, mc):
  mcpiX = 0
  mcpiY = 0
  mcpiZ = 0
  mcpiX0 = 0
  mcpiY0 = 0
  mcpiZ0 = 0
  mcpiX1 = 0
  mcpiY1 = 0
  mcpiZ1 = 0
  blockTypeId = 1
  blockData = 0
  # Minecraft Graphics Turtle(Start)
  forward = 0
  backward = 0
  right = 0
  left = 0
  up = 0
  down = 0
  speed = 10
  turtleX = 0
  turtleY = 0
  turtleZ = 0
  headingangle = 0
  penBlockId = block.DIRT.id
  penBlockData = 0
  mc = minecraft.Minecraft.create()
  pos = mc.player.getPos()
  steve = turtle.MinecraftTurtle(mc,pos)
  spos = steve.position
  # Minecraft Graphics Turtle(End)
  # Minecraft Stuff(Start)
  radius = 0
  x1 = 0
  y1 = 0
  z1 = 0
  shapePoints = []
  fill = True
  mcdrawing = stuff.MinecraftDrawing(mc)
  # Minecraft Stuff(End)


  for msg in _listen(s):
    if (msg):
      print "Received: %s" % str(msg)
      if msg[0] == 'broadcast':
        if not mc:
          mc = minecraft.Minecraft.create()
        if msg[1] == 'hello_minecraft':
          mc.postToChat("hello minecraft")
        elif msg[1] == 'setPos':
          if (is_number(mc, "mcpiX", mcpiX) and is_number(mc, "mcpiY", mcpiY) and is_number(mc, "mcpiZ", mcpiZ)): 
            mc.player.setPos(mcpiX, mcpiY, mcpiZ)
            print "setPos: %.1f %.1f %.1f" % (mcpiX, mcpiY, mcpiZ)
        elif msg[1] == 'setBlock':
          if (is_number(mc, 'mcpiX', mcpiX) and is_number(mc, 'mcpiY', mcpiY) and is_number(mc, 'mcpiZ', mcpiZ) and is_number(mc, 'blockTypeId', blockTypeId) and is_number(mc, 'blockData', blockData)):
            mc.setBlock(mcpiX, mcpiY, mcpiZ, blockTypeId, blockData)
            print "setBlock: %d %d %d %d %d" % (mcpiX, mcpiY, mcpiZ, blockTypeId, blockData)
        # Minecraft Graphics Turtle(Start)
	elif msg[1] == 'initTurtle':
            steve = turtle.MinecraftTurtle(mc,spos)
	    print "steve.__init__"
        elif msg[1] == 'setPosTurtle':
          if (is_number(mc, "turtleX", turtleX) and is_number(mc, "turtleY", turtleY) and is_number(mc, "turtleZ", turtleZ)): 
	    steve.setposition(turtleX, turtleY, turtleZ)
            print "setpositionTurtle: %.1f %.1f %.1f" % (turtleX, turtleY, turtleZ)
	elif msg[1] == 'setForward':
          if is_number(mc, 'forward', forward) :
	    steve.forward(forward)          		  
	    print "steve.forward: (%d)" % (forward)
	elif msg[1] == 'setBackward':
          if is_number(mc, 'backward', backward) :
	    steve.backward(backward)          		  
	    print "steve.backward: (%d)" % (backward)
	elif msg[1] == 'setRight':
          if is_number(mc, 'right', right) :
	    steve.right(right)          		  
	    print "steve.right: (%d)" % (right)
	elif msg[1] == 'setLeft':
          if is_number(mc, 'left', left) :
	    steve.left(left)          		  
	    print "steve.left: (%d)" % (left)
	elif msg[1] == 'setUp':
          if is_number(mc, 'up', up) :
	    steve.up(up)          		  
	    print "steve.up: (%d)" % (up)
	elif msg[1] == 'setDown':
          if is_number(mc, 'down', down) :
	    steve.down(down)          		  
	    print "steve.down: (%d)" % (down)
	elif msg[1] == 'setSpeed':
          if is_number(mc, 'speed', speed) :
	    steve.speed(speed)          		  
	    print "steve.speed: (%d)" % (speed)
	elif msg[1] == 'setPenBlockId':
	    steve.penblock(penBlockId)          		  
	    print "steve.penblock: (%s)" % (penBlockId)
	elif msg[1] == 'setPenBlockData':
	    steve.penblock(penBlockId, penBlockData)          		  
	    print "steve.penblock: (%s, %d)" % (penBlockId, penBlockData)
	elif msg[1] == 'penup':
	    steve.penup()
	    print "steve.penup"
	elif msg[1] == 'pendown':
	    steve.pendown()
	    print "steve.pendown"
	elif msg[1] == 'setHeading':
          if is_number(mc, 'headingangle', headingangle) :
	    steve.setheading(headingangle)
	    print "steve.setheading: (%d)" % (headingangle)
	elif msg[1] == 'setVerticalHeading':
          if is_number(mc, 'headingangle', headingangle) :
	    steve.setverticalheading(headingangle)
	    print "steve.setverticalheading: (%d)" % (headingangle)
        # Minecraft Graphics Turtle(End)
        # Minecraft Stuff(Start)
	elif msg[1] == 'drawLine':
	    mcdrawing.drawLine(int(x1),int(y1),int(z1),int(turtleX),int(turtleY),int(turtleZ),blockTypeId, blockData)          		  
	    print "mcdrawing.drawLine: (%d, %d, %d, %d, %d, %d, %d, %d)" % (x1,y1,z1,turtleX,turtleY,turtleZ,blockTypeId, blockData)
	elif msg[1] == 'drawSphere':
	    mcdrawing.drawSphere(turtleX,turtleY,turtleZ,radius,blockTypeId, blockData)          		  
	    print "mcdrawing.drawSphere: (%d, %d, %d, %d, %d, %d)" % (turtleX,turtleY,turtleZ,radius,blockTypeId, blockData)
	elif msg[1] == 'drawCircle':
	    mcdrawing.drawCircle(turtleX,turtleY,turtleZ,radius,blockTypeId, blockData)          		  
	    print "mcdrawing.drawCircle: (%d, %d, %d, %d, %d, %d)" % (turtleX,turtleY,turtleZ,radius,blockTypeId, blockData)
	elif msg[1] == 'resetShapePoints':
	    shapePoints = []
            mcdrawing = stuff.MinecraftDrawing(mc)
	elif msg[1] == 'setShapePoints':
	    shapePoints.append(minecraft.Vec3(int(x1),int(y1),int(z1)))
	    print "append.shapePoints:"
	    print ' '.join(str(p) for p in shapePoints)
	elif msg[1] == 'drawFace':
	    if (fill == 'True'):
		    fillFlag = True
            elif (fill == 'False'):
		    fillFlag = False
	    mcdrawing.drawFace(shapePoints, fillFlag, blockTypeId)          		  
	    #print "mcdrawing.drawFace: (%d, %d, %d)" % (shapePoints,fill,blockTypeId )
	    print "mcdrawing.drawFace:"
	    print ' '.join(str(p) for p in shapePoints)
	    print(fill)
	    print(blockTypeId)
        # Minecraft Stuff(ENd)
	elif msg[1] == 'setBlocks':
          if (is_number(mc, 'mcpiX0', mcpiX0) and is_number(mc, 'mcpiY0', mcpiY0) and is_number(mc, 'mcpiZ0', mcpiZ0) and is_number(mc, 'mcpiX1', mcpiX1) and is_number(mc, 'mcpiY1', mcpiY1) and is_number(mc, 'mcpiZ1', mcpiZ1) and is_number(mc, 'blockTypeId', blockTypeId) and is_number(mc, 'blockData', blockData)):
            mc.setBlocks(mcpiX0, mcpiY0, mcpiZ0, mcpiX1, mcpiY1, mcpiZ1, blockTypeId, blockData)
            print "setBlocks(%d, %d, %d, %d, %d, %d, %d, %d" % (mcpiX0, mcpiY0, mcpiZ0, mcpiX1, mcpiY1, mcpiZ1, blockTypeId, blockData)
        elif msg[1] == 'getPos':
          playerPos = mc.player.getPos()
	  pos = playerPos
          s.sensorupdate(
           {'playerX': playerPos.x,
            'playerY': playerPos.y,
            'playerZ': playerPos.z}
          )
        elif msg[1] == 'getHeight':
          posY = mc.getHeight(mcpiX, mcpiZ)
          s.sensorupdate({'posY': posY})
          mc.postToChat("posY: %d" % posY)
        elif msg[1] == 'pollBlockHits':
          blockEvents = mc.events.pollBlockHits()
          print blockEvents
          if blockEvents:
            blockEvent = blockEvents[-1]
            s.sensorupdate(
             {'blockEventX': blockEvent.pos.x,
              'blockEventY': blockEvent.pos.y,
              'blockEventZ': blockEvent.pos.z,
              'blockEventFace': blockEvent.face,
              'blockEventEntityId': blockEvent.entityId}
            )
          else:
            s.sensorupdate(
             {'blockEventX': '',
              'blockEventY': '',
              'blockEventZ': '',
              'blockEventFace': '',
              'blockEventEntityId': ''}
            )
        elif msg[1] == 'reset':
          mc.postToChat('reset the world')
          mc.setBlocks(-100, 0, -100, 100, 63, 100, 0, 0)
          mc.setBlocks(-100, -63, -100, 100, -2, 100, 1, 0)
          mc.setBlocks(-100, -1, -100, 100, -1, 100, 2, 0)
          mc.player.setPos(0, 0, 0)
      elif msg[0] == 'sensor-update':
        mcpiX = msg[1].get('mcpiX', mcpiX)
        mcpiY = msg[1].get('mcpiY', mcpiY)
        mcpiZ = msg[1].get('mcpiZ', mcpiZ)
        mcpiX0 = msg[1].get('mcpiX0', mcpiX0)
        mcpiY0 = msg[1].get('mcpiY0', mcpiY0)
        mcpiZ0 = msg[1].get('mcpiZ0', mcpiZ0)
        mcpiX1 = msg[1].get('mcpiX1', mcpiX1)
        mcpiY1 = msg[1].get('mcpiY1', mcpiY1)
        mcpiZ1 = msg[1].get('mcpiZ1', mcpiZ1)
        blockTypeId = msg[1].get('blockTypeId', blockTypeId)
        blockData = msg[1].get('blockData', blockData)
        # Minecraft Graphics Turtle(Start)
        forward = msg[1].get('forward', forward)
        backward = msg[1].get('backward', backward)
        right = msg[1].get('right', right)
        left = msg[1].get('left', left)
        up = msg[1].get('up', up)
        down = msg[1].get('down', down)
        speed = msg[1].get('speed', speed)
        turtleX = msg[1].get('turtleX', turtleX)
        turtleY = msg[1].get('turtleY', turtleY)
        turtleZ = msg[1].get('turtleZ', turtleZ)
        headingangle = msg[1].get('headingangle', headingangle)
        penBlockId = msg[1].get('penBlockId', penBlockId)
        penBlockData = msg[1].get('penBlockData', penBlockData)
        # Minecraft Graphics Turtle(Start)
        # Minecraft Stuff(Start)
        radius = msg[1].get('radius', radius)
        fill = msg[1].get('fill', fill)
        x1 = msg[1].get('x1', x1)
        y1 = msg[1].get('y1', y1)
        z1 = msg[1].get('z1', z1)