-
Notifications
You must be signed in to change notification settings - Fork 0
/
wateringBot.ms
247 lines (209 loc) · 4.8 KB
/
wateringBot.ms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Watering Bot
import "pathfinding"
directions = {"North": 0, "East": 1, "South": 2, "West": 3}
endPositionX = 69
endPositionY = 28
waterSourceX = 69
waterSourceY = 28
directionToWater = directions["East"]
me.refill = function
origin = new Point
origin.x = me.position.x
origin.y = me.position.y
facing = me.facing
goal = new Point
goal.x = waterSourceX
goal.y = waterSourceY
path = findPath(origin, goal)
for point in path
me.moveTo(point.x, point.y)
end for
me.turn(directionToWater)
me.select "Watering Can"
me.useTool
if path.len > 0 then
for i in range(-1, (path.len * -1))
point = path[i]
me.moveTo(point.x, point.y)
end for
end if
me.turn(facing)
end function
me.checkEmptyCan = function
if me.inventory[me.currentToolIndex].type != "WateringCan" then
me.select "Watering Can"
end if
if me.inventory[me.currentToolIndex].waterLeft < 1 then
me.refill
end if
end function
me.water = function
me.checkEmptyCan
me.select "Watering Can"
me.useTool
end function
isDryCropAtTile = function(tile)
if tile == null then return false
bool = tile.hasIndex("crop")
bool = bool and tile.hasIndex("dry")
if bool then
bool = bool and tile.crop
bool = bool and tile.dry
end if
return bool
end function
isDryCropAhead = function
tile = me.ahead
return isDryCropAtTile(tile)
end function
me.turn = function(direction)
if direction < 0 or direction > 3 then
return
end if
if me.facing == direction then
return
else if (me.facing - direction) % 2 == 0 then
me.right
me.right
else if (me.facing - 1) == direction or
(me.facing) == (direction - 3) then
me.left
else
me.right
end if
end function
isAheadOccupied = function
if me.ahead == null then return false
return me.ahead.type == "Character"
end function
me.moveForward = function
if isAheadOccupied then
print "Waiting for obstacle to move"
wait 1
else
me.forward
end if
end function
me.moveTo = function(x, y)
if me.position.y > y then
me.turn(directions["North"])
else if me.position.y < y then
me.turn(directions["South"])
end if
while me.position.y != y
me.moveForward
end while
if me.position.x < x then
me.turn(directions["East"])
else if me.position.x > x then
me.turn(directions["West"])
end if
while me.position.x != x
me.moveForward
end while
end function
me.moveToAndWater = function(x, y)
if me.position.y > y then
me.turn(directions["North"])
else if me.position.y < y then
me.turn(directions["South"])
end if
if isDryCropAhead then
me.water
end if
while me.position.y != y
me.moveForward
end while
if me.position.x < x then
me.turn(directions["East"])
else if me.position.x > x then
me.turn(directions["West"])
end if
if isDryCropAhead then
me.water
end if
while me.position.x != x
me.moveForward
end while
end function
moveThroughPath = function(path)
if not path isa list then
return
end if
for point in path
if not point isa Point then
print "Path element wasnt a Point"
return
end if
me.moveToAndWater(point.x, point.y)
end for
end function
distance = function(point1, point2)
if not point1 isa Point then
print "Argument must be Point(Distance)"
return -1
end if
if not point1 isa Point then
print "Argument must be Point(Distance)"
return -1
end if
return abs(point1.x - point2.x) +
abs(point1.y - point2.y)
end function
waterField = function
//startup combined with this script
//might create a little freeze so
//script will wait 2 seconds before starting
wait 2
previousPoint = new Point
previousPoint.x = me.position.x
previousPoint.y = me.position.y
xStart = farm.width
xEnd = 0
isFieldInRow = false
for y in range (0, farm.height)
if isFieldInRow then
swap = xStart
xStart = xEnd
xEnd = swap
end if
isFieldInRow = false
for x in range (xStart, xEnd)
tile = farm.tile(x, y)
if not isDryCropAtTile(tile) then
continue
end if
isFieldInRow = true
goal = new Point
goal.x = x
goal.y = y
if distance(goal, previousPoint) == 1 then
me.moveToAndWater(x, y)
else
path = findPath(previousPoint, goal)
moveThroughPath(path)
end if
previousPoint = new Point
previousPoint = goal
yield
end for
yield
end for
origin = new Point
origin.x = me.position.x
origin.y = me.position.y
goal = new Point
goal.x = endPositionX
goal.y = endPositionY
path = findPath(origin, goal)
moveThroughPath(path)
me.refill
end function
text.clear
main = function
if me.isBot != 0 then
print "Bot is starting"
waterField
end if
end function
return main