-
Notifications
You must be signed in to change notification settings - Fork 7
/
extension.ts
338 lines (317 loc) · 10.6 KB
/
extension.ts
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* Writing data for hacking stem experiments
*/
//% weight=94 color=#EC7505 icon="\uf1b3"
namespace hourOfCode {
// the agentDestroy and hazardsRemain functions are supposed to
// look indistinguishable from lesson to lesson, since only one
// set will ever appear at a time. their display names should
// not specifically include which lesson they are a part of
let targetsL4 = 5
let targetsL5 = 10
let targetsL6 = 49
let counterL4 = 9
let counterL5 = 30
let counterL6 = 60
let shortHazards = [31, FERN, TALLGRASS] // single-block plants and old base id
let tallHazards = [175, PEONY, ROSE_BUSH, DOUBLE_TALLGRASS, LARGE_FERN, LILAC, SUNFLOWER] // double plants and old base id
let airBlock = Block.Air
let brokeNonHazard = false
let taskIsComplete = false
let monitorCount = 18
// hidden from user, used by other functions
//% block
function completeTask() {
if (!taskIsComplete) {
mobs.execute(
mobs.target(TargetSelectorKind.NearestPlayer),
positions.create(0, 0, 0),
"scoreboard players set @s success 1"
)
taskIsComplete = true
}
}
/**
* Opens a gate
*/
//% block="open gate"
//% weight=90
export function openDoor() {
completeTask()
}
/**
* Detects if there is a dry fern next to the agent in the specified direction
* @param dir the direction to detect the dry fern
*/
//% block="agent detect dry fern %dir"
//% weight=80
export function agentDetectDryFern(dir: SixDirection) {
return shortHazards.indexOf(agent.inspect(AgentInspection.Block, dir)) != -1
}
/**
* Detects if there is dry grass next to the agent in the specified direction
* @param dir the direction to detect the dry grass
*/
//% block="agent detect dry grass %dir"
//% weight=80
export function agentDetectDryGrass(dir: SixDirection) {
return tallHazards.indexOf(agent.inspect(AgentInspection.Block, dir)) != -1
}
/**
* Detects if there is dry brush next to the agent in the specified direction
* @param dir the direction to detect the dry brush
*/
//% block="agent detect dry brush %dir"
//% weight=80
export function agentDetectDryBrush(dir: SixDirection) {
let currentTarget = agent.inspect(AgentInspection.Block, dir)
return tallHazards.indexOf(currentTarget) != -1 || shortHazards.indexOf(currentTarget) != -1
}
/**
* Check for any hazards in a direction
* @param dir the direction to check for hazards
*/
//% block="agent analyze %dir"
//% weight=70
export function agentAnalyze(dir: SixDirection) {
let targetBlock = agent.inspect(AgentInspection.Block, dir)
if (shortHazards.indexOf(targetBlock) != -1 || tallHazards.indexOf(targetBlock) != -1) {
mobs.execute(
mobs.target(TargetSelectorKind.NearestPlayer),
positions.create(0, 0, 0),
"playsound random.levelup @p"
)
completeTask()
}
}
/**
* Checks that there are fire hazards
*/
//% block="hazards remain"
//% weight=45
export function hazardsRemainL4() {
let timeout = 10
if (agent.inspect(AgentInspection.Block, SixDirection.Forward) == airBlock) {
counterL4--
if (targetsL4 <= 0) {
completeTask()
return false
} else if (counterL4 < 0) {
return false
} else if (brokeNonHazard) {
return false
}
// Keep testing until the next hazard appears (air means it has not)
while (agent.inspect(AgentInspection.Block, SixDirection.Forward) == airBlock) {
// If a hazard has not appeared, wait 6 ticks before testing again
for (let index = 0; index < 6; index++) {
// We use testForBlock because it takes one tick (this is more reliable than waiting for X milliseconds because it's not platform dependent)
blocks.testForBlock(GRASS, world(-60, 71, -90)); // we're ignoring the return value since we just use this to pass time
}
if (timeout-- <= 0) {
return false
}
}
}
// If we reached this point, a hazard has appeared! return true here so the student's code inside the while(hazardsRemain) can continue
return true
}
/**
* Commands the agent to destroy a block in the given direction
* @param dir the direction to destroy a block at
*/
//% block="agent destroy %dir"
//% weight=40
export function agentDestroyL4(dir: SixDirection) {
let targetBlock4 = agent.inspect(AgentInspection.Block, dir)
if (shortHazards.indexOf(targetBlock4) != -1 || tallHazards.indexOf(targetBlock4) != -1) {
targetsL4--
} else if (targetBlock4 != airBlock) {
brokeNonHazard = true
}
agent.destroy(dir)
}
/**
* Checks that there are fire hazards
*/
//% block="hazards remain"
//% weight=55
export function hazardsRemainL5() {
counterL5--
if (brokeNonHazard) {
return false
} else if (counterL5 < 0) {
return false
} else if (targetsL5 <= 0) {
completeTask()
return false
} else {
return true
}
}
/**
* Commands the agent to destroy a block in the given direction
* @param dir the direction to destroy a block at
*/
//% block="agent destroy %dir"
//% weight=50
export function agentDestroyL5(dir: SixDirection) {
let targetZ5 = 70
let agentZ5 = agent.getPosition().getValue(Axis.Z)
let targetBlock5 = agent.inspect(AgentInspection.Block, dir)
if (shortHazards.indexOf(targetBlock5) != -1 || tallHazards.indexOf(targetBlock5) != -1 ) {
if (Math.abs(agentZ5 - targetZ5) <= 1) {
targetsL5--
} else {
brokeNonHazard = true
}
} else if (targetBlock5 != airBlock) {
brokeNonHazard = true
}
agent.destroy(dir)
}
/**
* Checks that there are fire hazards
*/
//% block="hazards remain"
//% weight=65
export function hazardsRemainL6() {
counterL6--
if (brokeNonHazard) {
return false
} else if (counterL6 < 0) {
return false
} else if (targetsL6 <= 0) {
completeTask()
return false
} else {
return true
}
}
/**
* Commands the agent to destroy a block in the given direction
* @param dir the direction to destroy a block at
*/
//% block="agent destroy %dir"
//% weight=60
export function agentDestroyL6(dir: SixDirection) {
let targetX6 = -8
let targetZ6 = 77
let radius = 5
let agentX6 = agent.getPosition().getValue(Axis.X)
let agentZ6 = agent.getPosition().getValue(Axis.Z)
let targetBlock6 = agent.inspect(AgentInspection.Block, dir)
if (shortHazards.indexOf(targetBlock6) != -1 || tallHazards.indexOf(targetBlock6) != -1) {
if (Math.abs(agentX6 - targetX6) <= radius && Math.abs(agentZ6 - targetZ6) <= radius) {
targetsL6--
} else {
brokeNonHazard = true
}
} else if (targetBlock6 != airBlock) {
brokeNonHazard = true
}
agent.destroy(dir)
}
/**
* Agent watches the monitor for hazards
*/
//% block="agent look for hazards"
//% weight=75
export function agentLookForHazards() {
monitorCount--
loops.pause(500)
if (taskIsComplete) {
return false
} else if (monitorCount < 0) {
return false
} else {
return true
}
}
/**
* Sees that there is a hazard on the monitor
*/
//% block="hazard found"
//% weight=74
export function agentSeeHazard() {
let hiddenTarget = positions.createWorld(-63, 68, -78)
let successBlock = Block.GoldBlock
return blocks.testForBlock(successBlock, hiddenTarget)
}
/**
* Warns the team of a high-risk area
*/
//% block="alert team"
//% weight = 70
export function alertTeam() {
agent.attack(SixDirection.Up)
if (agentSeeHazard()) {
completeTask()
} else {
monitorCount = -1
}
}
/**
* Commands the agent to place a redstone torch in the specified direction
*/
//% block="agent place redstone torch %dir"
//% weight = 97
export function agentPlaceRedstoneTorch(dir: SixDirection) {
let MIN_SLOT = 1
let MAX_SLOT = 27
let randomSlot = Math.randomRange(MIN_SLOT, MAX_SLOT)
agent.setItem(REDSTONE_TORCH, 1, randomSlot)
agent.setSlot(randomSlot)
agent.place(dir)
agent.setItem(AIR, 1, randomSlot)
}
/**
* Commands the agent to place an acacia plank in the specified direction
*/
//% block="agent place acacia %dir"
//% weight = 98
export function agentPlaceAcacia(dir: SixDirection) {
let MIN_SLOT = 1
let MAX_SLOT = 27
let randomSlot = Math.randomRange(MIN_SLOT, MAX_SLOT)
agent.setItem(PLANKS_ACACIA, 1, randomSlot)
agent.setSlot(randomSlot)
agent.place(dir)
agent.setItem(AIR, 1, randomSlot)
}
/**
* Commands the agent to plant carrots downwards
*/
//% block="agent plant carrots"
//% weight = 99
export function agentPlantCarrots() {
let MIN_SLOT = 1
let MAX_SLOT = 27
let randomSlot = Math.randomRange(MIN_SLOT, MAX_SLOT)
agent.setItem(CARROTS, 1, randomSlot)
agent.setSlot(randomSlot)
agent.place(DOWN)
agent.setItem(AIR, 1, randomSlot)
}
/**
* Checks whether the agent has reached the goal
*/
//% block="goal not reached"
//% weight = 96
export function goalNotReached() {
return agent.inspect(AgentInspection.Block, DOWN) != GOLD_BLOCK
}
/**
* Commands the agent to collect all nearby blocks and items
*/
//% block="collect all"
//% weight = 95
export function collectAndCount() {
let SLOT_NUMBER = 1
let TOTAL_PIECES = 29
agent.collectAll()
if (agent.getItemCount(SLOT_NUMBER) >= TOTAL_PIECES) {
blocks.place(REDSTONE_TORCH, world(-143, 31, 59))
}
}
}