SHOW:
|
|
- or go back to the newest paste.
| 1 | -- api used for 2d pathfinding | |
| 2 | ||
| 3 | ||
| 4 | -- defining variables of coordinates and direction | |
| 5 | -- Initialize parameters for the location and direction | |
| 6 | x_live_coord = 1 | |
| 7 | y_live_coord = 1 | |
| 8 | live_facing_direction = 0 | |
| 9 | ||
| 10 | ||
| 11 | -- Basic moveset functions turning and forward | |
| 12 | ||
| 13 | function rec_turn_right() | |
| 14 | -- action | |
| 15 | turtle.turnRight() | |
| 16 | ||
| 17 | -- recording | |
| 18 | live_facing_direction = live_facing_direction + 1 | |
| 19 | if (live_facing_direction == 4) then | |
| 20 | live_facing_direction = 0 | |
| 21 | end | |
| 22 | end | |
| 23 | ||
| 24 | function rec_turn_left() | |
| 25 | -- action | |
| 26 | turtle.turnLeft() | |
| 27 | ||
| 28 | -- recording | |
| 29 | live_facing_direction = live_facing_direction - 1 | |
| 30 | if (live_facing_direction == -1) then | |
| 31 | live_facing_direction = 3 | |
| 32 | end | |
| 33 | ||
| 34 | end | |
| 35 | ||
| 36 | function rec_forward_firm() | |
| 37 | -- action | |
| 38 | repeat | |
| 39 | turtle.dig() | |
| 40 | until turtle.forward() | |
| 41 | ||
| 42 | --recording | |
| 43 | if (live_facing_direction == 0) then | |
| 44 | y_live_coord = y_live_coord + 1 | |
| 45 | elseif (live_facing_direction == 1) then | |
| 46 | x_live_coord = x_live_coord + 1 | |
| 47 | elseif (live_facing_direction == 2) then | |
| 48 | y_live_coord = y_live_coord - 1 | |
| 49 | elseif (live_facing_direction == 3) then | |
| 50 | x_live_coord = x_live_coord - 1 | |
| 51 | end | |
| 52 | print(x_live_coord, y_live_coord) | |
| 53 | refuel_with("minecraft:coal")
| |
| 54 | ||
| 55 | end | |
| 56 | ||
| 57 | ||
| 58 | function up_firm() | |
| 59 | repeat | |
| 60 | turtle.digUp() | |
| 61 | until turtle.up() | |
| 62 | end | |
| 63 | ||
| 64 | function down_firm() | |
| 65 | repeat | |
| 66 | turtle.digDown() | |
| 67 | until turtle.down() | |
| 68 | end | |
| 69 | ||
| 70 | function turtle_face_turn(face) | |
| 71 | if (face - live_facing_direction == 1 or face - live_facing_direction == -3) then | |
| 72 | rec_turn_right() | |
| 73 | elseif(face - live_facing_direction == -1 or face - live_facing_direction == 3) then | |
| 74 | rec_turn_left() | |
| 75 | - | else |
| 75 | + | elseif(face == live_facing_direction) then |
| 76 | print("not turning")
| |
| 77 | else | |
| 78 | rec_turn_left() | |
| 79 | rec_turn_left() | |
| 80 | end | |
| 81 | end | |
| 82 | ||
| 83 | ||
| 84 | -- the GOTO function, give one coordinate and move to the function | |
| 85 | -- first align the facing direction to 0 | |
| 86 | -- get to the coordinate and return facing direction to 0 | |
| 87 | function turtle_goto(a,b) | |
| 88 | -- calculation | |
| 89 | delta_x = a - x_live_coord | |
| 90 | delta_y = b - y_live_coord | |
| 91 | ||
| 92 | -- action section below | |
| 93 | -- facing the correct direction for x movement | |
| 94 | if (delta_x > 0) then | |
| 95 | turtle_face_turn(1) | |
| 96 | elseif (delta_x < 0) then | |
| 97 | turtle_face_turn(3) | |
| 98 | delta_x = - delta_x | |
| 99 | end | |
| 100 | ||
| 101 | ||
| 102 | -- x movement | |
| 103 | for x_movement = 0,delta_x - 1,1 | |
| 104 | do | |
| 105 | rec_forward_firm() | |
| 106 | end | |
| 107 | ||
| 108 | ||
| 109 | -- facing the correct direction for y movement | |
| 110 | if (delta_y > 0) then | |
| 111 | turtle_face_turn(0) | |
| 112 | elseif (delta_y < 0) then | |
| 113 | turtle_face_turn(2) | |
| 114 | delta_y = - delta_y | |
| 115 | end | |
| 116 | -- y movement | |
| 117 | for y_movement = 0,delta_y - 1,1 | |
| 118 | do | |
| 119 | rec_forward_firm() | |
| 120 | end | |
| 121 | ||
| 122 | ||
| 123 | -- returning to the 0 facing direction | |
| 124 | ||
| 125 | end | |
| 126 | ||
| 127 | -- the pathfinding function, will try to visit all points in an array | |
| 128 | ||
| 129 | -- some utilities | |
| 130 | ||
| 131 | function pathfind_and_action() | |
| 132 | for loop_num=1, array_length, 1 | |
| 133 | do | |
| 134 | print("NEXT!!")
| |
| 135 | near_init_term_num = 1 | |
| 136 | while visited[near_init_term_num] == 1 do | |
| 137 | near_init_term_num = near_init_term_num + 1 | |
| 138 | end | |
| 139 | ||
| 140 | nearest_x = x_destinations[near_init_term_num] | |
| 141 | nearest_y = y_destinations[near_init_term_num] | |
| 142 | min_distance = math.abs(nearest_x - x_live_coord) + math.abs(nearest_y - y_live_coord) | |
| 143 | ||
| 144 | last_item_num = near_init_term_num | |
| 145 | for item_num = 1, array_length, 1 | |
| 146 | do | |
| 147 | if visited[item_num] == 0 then | |
| 148 | if (math.abs(x_destinations[item_num] - x_live_coord) + math.abs(y_destinations[item_num] - y_live_coord)) < min_distance then | |
| 149 | nearest_x = x_destinations[item_num] | |
| 150 | nearest_y = y_destinations[item_num] | |
| 151 | min_distance = math.abs(nearest_x - x_live_coord) + math.abs(nearest_y - y_live_coord) | |
| 152 | last_item_num = item_num | |
| 153 | end | |
| 154 | end | |
| 155 | end | |
| 156 | visited[last_item_num] = 1 | |
| 157 | print("going to ", nearest_x, nearest_y)
| |
| 158 | turtle_goto(nearest_x,nearest_y) | |
| 159 | ||
| 160 | turtle_action() | |
| 161 | end | |
| 162 | ||
| 163 | end | |
| 164 | ||
| 165 | ||
| 166 | ||
| 167 | -- action loop section | |
| 168 | ||
| 169 | function place_item(item_name) | |
| 170 | item_tot = 0 | |
| 171 | for cell_num = 1,16 do | |
| 172 | turtle.select(cell_num) | |
| 173 | if turtle.getItemDetail() then | |
| 174 | item_detail = turtle.getItemDetail() | |
| 175 | if (item_detail.name == item_name) then | |
| 176 | turtle.placeDown() | |
| 177 | item_tot = item_tot + item_detail.count | |
| 178 | end | |
| 179 | end | |
| 180 | end | |
| 181 | print("item_left: %d", item_tot)
| |
| 182 | end | |
| 183 | ||
| 184 | function refuel_with(item_name) | |
| 185 | for cell_num = 1,16 do | |
| 186 | turtle.select(cell_num) | |
| 187 | if turtle.getItemDetail() then | |
| 188 | item_detail = turtle.getItemDetail() | |
| 189 | if (item_detail.name == item_name) then | |
| 190 | turtle.refuel(1) | |
| 191 | end | |
| 192 | end | |
| 193 | end | |
| 194 | end | |
| 195 | ||
| 196 | ||
| 197 | ||
| 198 | -- example code of coordinates given | |
| 199 | - | x_destinations = {}
|
| 199 | + | |
| 200 | - | y_destinations = {}
|
| 200 | + | |
| 201 | - | visited = {}
|
| 201 | + | |
| 202 | - | array_length = 0 |
| 202 | + | |
| 203 | print('nothing')
| |
| 204 | success, data = turtle.inspectDown() | |
| 205 | if success then | |
| 206 | if (data.state.age == 7) then | |
| 207 | turtle.digDown() | |
| 208 | place_item("minecraft:potato")
| |
| 209 | end | |
| 210 | end | |
| 211 | end | |
| 212 | ||
| 213 | ||
| 214 | function plane_execution(x_destinations_input, y_destinations_input, turtle_action_input) | |
| 215 | x_destinations = x_destinations_input | |
| 216 | y_destinations = y_destinations_input | |
| 217 | turtle_action = turtle_action_input | |
| 218 | for x_i in x_destinations_input do | |
| 219 | visited[#visited + 1] = 0 | |
| 220 | end | |
| 221 | array_length = #visited | |
| 222 | turtle_action = turtle_action_input | |
| 223 | pathfind_and_action() | |
| 224 | end | |
| 225 | ||
| 226 | function squarefill(a,b,s) | |
| 227 | array_length = a*b | |
| 228 | current_length=1 | |
| 229 | for xfill=1,a,s | |
| 230 | do | |
| 231 | for yfill=1,b,s | |
| 232 | do | |
| 233 | x_destinations[current_length]=xfill | |
| 234 | y_destinations[current_length]=yfill | |
| 235 | visited[current_length]=0 | |
| 236 | current_length=current_length + 1 | |
| 237 | end | |
| 238 | end | |
| 239 | end | |
| 240 | ||
| 241 | ||
| 242 | ||
| 243 | while true do | |
| 244 | - | --squarefill(3,5,1) |
| 244 | + | x_destinations = {}
|
| 245 | - | --pathfind_and_action() |
| 245 | + | y_destinations = {}
|
| 246 | - | turtle_goto(1,6) |
| 246 | + | visited = {}
|
| 247 | - | turtle_face_turn(0) |
| 247 | + | array_length = 0 |
| 248 | squarefill(5,9,1) | |
| 249 | pathfind_and_action() | |
| 250 | turtle_goto(1,1) | |
| 251 | turtle_face_turn(0) | |
| 252 | sleep(500) | |
| 253 | end | |
| 254 | ||
| 255 |