mralasic

agent

May 21st, 2022 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1. #after launching the program, the agent is teleported to the player
  2. agent.teleport_to_player()
  3.  
  4. #this varialbe will be overwritten in the function which will let us build the rails up and down in the correct direction
  5. lastDirection = CompassDirection.SOUTH
  6.  
  7. '''
  8. Project specifications:
  9.    
  10. slot 1 - rails
  11. slot 2 - powered rails
  12. slot 3 - stone
  13. slot 4 - redstone
  14. '''
  15.  
  16. def refillBlocks():
  17.     #adding 64 rails in slot 1 to the agent
  18.     agent.set_item(RAIL, 64, 1)
  19.     agent.set_item(POWERED_RAIL, 64, 2)
  20.     agent.set_item(STONE, 64, 3)
  21.     agent.set_item(REDSTONE_BLOCK, 64, 4)
  22.  
  23.  
  24. def rollerCoasterBuilding(railAmount, direction, railType):
  25.     #the varialbe storing the current number of built rails, when calling the function we can decide how many rails we want to build
  26.     currentRailAmount = 0
  27.     #function refilling agent's slots with proper blocks so he can keep building
  28.     refillBlocks()
  29.     # creating a global variable in which we will store the last direction of agent
  30.     global lastDirection
  31.     #defining the direction of agent
  32.     if direction == 1:
  33.         currentAgentDirection = CompassDirection.SOUTH
  34.     elif direction == 2:
  35.         currentAgentDirection = CompassDirection.NORTH
  36.     elif direction == 3:
  37.         currentAgentDirection = CompassDirection.EAST
  38.     elif direction == 4:
  39.         currentAgentDirection = CompassDirection.WEST
  40.     #direction 5 - down, 6 - up
  41.     else:
  42.         currentAgentDirection = lastDirection
  43.     #teleporting the agent, we care about his direction which we will modify later
  44.     agent.teleport(agent.get_position(),currentAgentDirection)
  45.     #when moving, agent will place blocks from the selected spot
  46.     agent.set_assist(PLACE_ON_MOVE, True)
  47.     #when moving, agent destroys blocks in his way
  48.     agent.set_assist(DESTROY_OBSTACLES, True)
  49.    
  50.     #the loop works until the correct amount of rails is built
  51.     while currentRailAmount < railAmount:
  52.         #clearing path before and after the track
  53.         if agent.detect(AgentDetection.BLOCK, UP) or agent.detect(AgentDetection.BLOCK, FORWARD):
  54.             agent.destroy(UP)
  55.  
  56.         #if the agent detects that he is in the air and there is nothing to build on, he will place stone block
  57.         elif agent.inspect(AgentInspection.BLOCK, DOWN) == 0 or agent.inspect(AgentInspection.BLOCK, DOWN) == 9:
  58.             if railType == 1:
  59.                 agent.set_slot(3)
  60.             else:
  61.                 agent.set_slot(4)
  62.             agent.place(DOWN)
  63.         else:
  64.             #if there are no obstacles, setting the slot to rails and moving forward.
  65.             if railType == 1:
  66.                 agent.set_slot(1)
  67.             else:
  68.                 agent.set_slot(2)
  69.             #  building the rail down  
  70.             if direction == 5:
  71.                 agent.move(FORWARD, 1)
  72.                 agent.destroy(UP)
  73.                 agent.set_assist(PLACE_ON_MOVE, False)
  74.                 agent.move(DOWN, 1)
  75.                 agent.set_assist(PLACE_ON_MOVE, True)
  76.                 currentRailAmount += 1
  77.             # building the rail up
  78.             elif direction == 6:
  79.                 agent.move(FORWARD, 1)
  80.                 agent.destroy(UP)
  81.                 agent.set_assist(PLACE_ON_MOVE, False)
  82.                 agent.move(UP,2)
  83.                 agent.destroy(UP)
  84.                 agent.move(DOWN,1)
  85.                 agent.set_assist(PLACE_ON_MOVE, True)
  86.                 currentRailAmount += 1
  87.             else:
  88.                 agent.move(FORWARD, 1)
  89.                 currentRailAmount += 1
  90.  
  91.     player.say("building finished!")
  92. player.on_chat("b",rollerCoasterBuilding)
  93.  
Add Comment
Please, Sign In to add comment