mralasic

agent - finished

May 21st, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #after launching the program, the agent is teleported to the player
  2. agent.teleport_to_player()
  3.  
  4. #this variable will be overwritten in the function which will let us build 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.  
  30. #creating a global variable in which we will store the last direction of the agent
  31. global lastDirection
  32.  
  33. if direction==1 :
  34. currentAgentDirection=CompassDirection.SOUTH
  35.  
  36. elif direction==2:
  37. currentAgentDirection=CompassDirection.WEST
  38.  
  39. elif direction==3:
  40. currentAgentDirection=CompassDirection.EAST
  41.  
  42. elif direction==4:
  43. currentAgentDirection=CompassDirection.NORTH
  44.  
  45. #direction 5 - down, 6 - up
  46. else:
  47. currentAgentDirection=lastDirection
  48.  
  49.  
  50. #teleporting the agent, we care about his direction, setting the direction
  51. agent.teleport(agent.get_position(),currentAgentDirection)
  52. #when moving, agent will place blocks from the selected spot
  53. agent.set_assist(PLACE_ON_MOVE, True)
  54. #when moving, agent destroys blocks in his way
  55. agent.set_assist(DESTROY_OBSTACLES, True)
  56.  
  57. #the loop works until the correct amount of rails is built
  58. while currentRailAmount < railAmount:
  59. #clearing path before and after the track
  60. if agent.detect(AgentDetection.BLOCK, UP) or agent.detect(AgentDetection.BLOCK, FORWARD):
  61. agent.destroy(UP)
  62.  
  63. #if the agent detects that he is in the air and there is nothing to build on, he will place stone block
  64. elif agent.inspect(AgentInspection.BLOCK, DOWN) == 0 or agent.inspect(AgentInspection.BLOCK, DOWN) == 9:
  65. agent.set_slot(3)
  66. if railType==1:
  67. agent.set_slot(3)
  68. else:
  69. agent.set_slot(4)
  70. agent.place(DOWN)
  71. else:
  72. if railType==1:
  73. agent.set_slot(1)
  74. else:
  75. agent.set_slot(2)
  76.  
  77. #building the rail down
  78. if direction==5:
  79. agent.move(FORWARD, 1)
  80. agent.destroy(UP)
  81. agent.set_assist(PLACE_ON_MOVE, False)
  82. agent.move(DOWN, 1)
  83. agent.set_assist(PLACE_ON_MOVE, True)
  84. currentRailAmount += 1
  85. #building the rail up
  86. elif direction==6:
  87.  
  88. agent.move(FORWARD,1)
  89. agent.destroy(UP)
  90. agent.set_assist(PLACE_ON_MOVE, False)
  91. agent.move(UP,2)
  92. agent.destroy(UP)
  93. agent.move(DOWN,1)
  94. agent.set_assist(PLACE_ON_MOVE, True)
  95. currentRailAmount += 1
  96.  
  97. else:
  98. #if there are no obstacles, setting the slot to rails and moving forward.
  99. agent.move(FORWARD, 1)
  100.  
  101. currentRailAmount += 1
  102.  
  103. lastDirection = currentAgentDirection
  104. player.say("building finished!")
  105. player.on_chat("b",rollerCoasterBuilding)
  106.  
Add Comment
Please, Sign In to add comment