-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
381 lines (335 loc) · 13.3 KB
/
main.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
enum RcbVerticalDirection {
//% block="up" blockId="rollerCoasterBuilderUp"
Up,
//% block="down" blockId="rollerCoasterBuilderDown"
Down
}
enum RcbPowerLevel {
//% block="full" blockId="rollerCoasterBuilderFullPower"
Full,
//% block="normal" blockId="rollerCoasterBuilderNormalPower"
Normal,
//% block="no" blockId="rollerCoasterBuilderNoPower"
No
}
//% color="#9C5F9B" block="Roller Coaster" icon="\uf3ff"
namespace rollerCoasterBuilder {
let railBase = PLANKS_OAK
let powerInterval = 5 // Keep between 1 and 8, else minecarts may stop between power
// Can be disabled for perf.
let waterProtection = true
let lavaProtection = true
//% block="add single rail to track"
//% blockId="rcbAddRail" weight=65
export function addRail() {
placeRailInternal(builder.position(), railBase, RAIL)
}
//% block="add single powered rail to track"
//% blockId="rcbAddPoweredRail" weight=70
export function addPoweredRail() {
placeRailInternal(builder.position(), REDSTONE_BLOCK, POWERED_RAIL)
}
// Intentionally not exposed, as it's a bit confusing...
function addUnpoweredPoweredRail() {
placeRailInternal(builder.position(), railBase, POWERED_RAIL)
}
function placeAirAbove(position: Position, start: number, dist: number) {
for (let i = 0; i <= dist - 1; i++) {
// Check for air first or we get a bunch of "cannot place block" errors.
const pos = position.move(CardinalDirection.Up, i + start)
if (!blocks.testForBlock(AIR, pos)) {
blocks.place(AIR, pos)
}
}
}
function replaceWaterAndLava(cornerOne: Position, cornerTwo: Position) {
if (waterProtection) {
blocks.replace(GLASS, 9, cornerOne, cornerTwo) // 9 == also water?
blocks.replace(GLASS, WATER, cornerOne, cornerTwo)
}
if (lavaProtection) {
blocks.replace(GLASS, 11, cornerOne, cornerTwo) // 11 == also lava?
blocks.replace(GLASS, LAVA, cornerOne, cornerTwo)
}
}
function placeRailInternal(position: Position, baseBlock: number, railBlock: number) {
blocks.place(baseBlock, position)
if (waterProtection || lavaProtection) {
const southWestDownCorner = position.move(CardinalDirection.South, 1).move(CardinalDirection.West, 1).move(CardinalDirection.Up, 1)
const northEastUpCorner = position.move(CardinalDirection.North, 1).move(CardinalDirection.East, 1).move(CardinalDirection.Up, 4)
replaceWaterAndLava(southWestDownCorner, northEastUpCorner)
}
// Need air blocks so player can fit if the track tunnels (or intersects with something).
placeAirAbove(position, 1, 3)
blocks.place(railBlock, position.move(CardinalDirection.Up, 1))
}
function getButtonAuxForDirection(direction: CompassDirection) {
switch (direction) {
case CompassDirection.North:
return 5;
case CompassDirection.East:
return 3;
case CompassDirection.South:
return 4;
case CompassDirection.West:
return 2;
default:
return 0;
}
}
//% block="begin track at $position heading $direction"
//% position.shadow=minecraftCreatePosition
//% direction.defl=CompassDirection.North
//% powerLevel.defl=RcBldPowerLevel.Normal
//% blockId="rcbBeginTrack" weight=100
export function placeTrackStart(position: Position, direction: CompassDirection) {
// Block presets
let btnBkgBlock = PINK_CONCRETE
let nonBtnBkgBlock = BLOCK_OF_QUARTZ
let rampBlock = QUARTZ_SLAB
let btn = WARPED_BUTTON
let btnAux = getButtonAuxForDirection(direction)
builder.teleportTo(position)
builder.face(direction)
// Rails
addUnpoweredPoweredRail()
builder.move(FORWARD, 1)
addUnpoweredPoweredRail()
builder.move(FORWARD, 1)
addRail()
// Ramp
builder.move(RIGHT, 1)
builder.place(rampBlock)
placeAirAbove(builder.position(), 1, 3)
builder.move(BACK, 1)
builder.place(rampBlock)
placeAirAbove(builder.position(), 1, 3)
builder.move(BACK, 1)
builder.place(rampBlock)
placeAirAbove(builder.position(), 1, 3)
// Non-Button Background
builder.move(BACK, 1)
builder.mark()
builder.move(LEFT, 1)
builder.raiseWall(nonBtnBkgBlock, 4)
// Btn Background
builder.move(LEFT, 1)
builder.mark()
builder.move(LEFT, 1)
builder.raiseWall(btnBkgBlock, 4)
builder.mark()
builder.move(FORWARD, 3)
builder.raiseWall(btnBkgBlock, 4)
builder.move(RIGHT, 1)
builder.place(btnBkgBlock)
placeAirAbove(builder.position(), 1, 3)
builder.move(BACK, 1)
builder.place(btnBkgBlock)
placeAirAbove(builder.position(), 1, 3)
builder.move(BACK, 1)
builder.place(btnBkgBlock)
placeAirAbove(builder.position(), 1, 3)
// Redstone
builder.move(UP, 1)
builder.place(REDSTONE_WIRE)
// Button
builder.move(UP, 1)
player.say(btn + ":" + btnAux)
builder.place(blocks.blockWithData(btn, btnAux))
// Minecart
// builder.shift(0, -1, -1)
// builder.place(MINECART)
mobs.give(mobs.target(LOCAL_PLAYER), MINECART, 1)
// Set builder location for next piece of track
builder.shift(3, -2, -1)
}
//% block="place track end"
//% position.shadow=minecraftCreatePosition
//% direction.defl=CompassDirection.North
//% powerLevel.defl=RcBldPowerLevel.Normal
//% blockId="rcbPlaceEndTrack" weight=99
export function placeTrackEnd() {
addRail()
builder.move(FORWARD, 1)
builder.place(railBase)
builder.move(UP, 1)
builder.place(railBase)
builder.shift(1, -1, 0)
}
//% block="add straight line of length $length || with $powerLevel power"
//% length.defl=10 length.min=1
//% powerLevel.defl=RcBldPowerLevel.Normal
//% blockId="rcbAddStraightLine" weight=95
export function addStraightLine(length: number, powerLevel: RcbPowerLevel = RcbPowerLevel.Normal) {
for (let index = 0; index < length; index++) {
if (powerLevel != RcbPowerLevel.No && index % powerInterval == 0) {
addPoweredRail()
} else {
if (powerLevel == RcbPowerLevel.Full) {
addUnpoweredPoweredRail();
} else {
addRail()
}
}
builder.move(FORWARD, 1)
}
}
//% block="add ramp $direction $distance blocks || changing 1 block vertically every $horizSpace blocks forward"
//% distance.defl=10
//% horizSpace.defl=1
//% horizSpace.min=1
//% blockId="rcbAddRamp" weight=90
export function addRamp(direction: RcbVerticalDirection, distance: number, horizSpace: number = 1) {
if (direction == RcbVerticalDirection.Up) {
rampUp(distance, horizSpace);
}
else {
rampDown(distance, horizSpace);
}
}
function rampUp(height: number, horizSpace: number) {
let unpoweredBlocksPlaced = 8; // Set to 8 so first block is powered.
for (let currentHeight = 0; currentHeight <= height; currentHeight++) {
for (let currentHoriz = 0; currentHoriz < horizSpace; currentHoriz++) {
if (unpoweredBlocksPlaced >= 8) {
rollerCoasterBuilder.addPoweredRail()
unpoweredBlocksPlaced = 0
} else {
addUnpoweredPoweredRail()
unpoweredBlocksPlaced++
}
builder.move(FORWARD, 1)
}
builder.move(UP, 1);
}
builder.move(DOWN, 1)
}
function rampDown(descentDistance: number, horizSpace: number) {
for (let currentDescent = 0; currentDescent <= descentDistance; currentDescent++) {
for (let currentHoriz = 0; currentHoriz < horizSpace; currentHoriz++) {
// Place powered at start only if needed, then every powerInterval blocks.
// Only needed on first descent level since the rest have the downhill to speed up.
let powerAtStart = currentDescent == 0 && horizSpace >= powerInterval;
if ((currentHoriz + (powerAtStart ? 0 : 1)) % powerInterval == 0) {
rollerCoasterBuilder.addPoweredRail()
}
else {
rollerCoasterBuilder.addRail()
}
builder.move(FORWARD, 1)
}
builder.move(DOWN, 1)
}
// Undo the final down movement, since we didn't actually place a block.
builder.move(UP, 1)
}
//% block="add $direction turn"
//% blockId="rcbAddTurn" weight=85
export function addTurn(direction: TurnDirection) {
rollerCoasterBuilder.addRail();
builder.move(FORWARD, 1);
rollerCoasterBuilder.addRail();
builder.turn(direction);
builder.move(FORWARD, 1);
rollerCoasterBuilder.addRail();
builder.move(FORWARD, 1);
}
//% block="add spiral going $verticalDirection turning $turnDirection with width $width and height $height"
//% width.min=3 width.defl=3
//% height.min=1 height.defl=10
//% blockId="rcbAddSpiral" weight=80
export function addSpiral(verticalDirection: RcbVerticalDirection, turnDirection: TurnDirection, height: number = 10, width: number = 3) {
let totalHeightDiff = 0
while (totalHeightDiff < height) {
let heightChange = verticalDirection == RcbVerticalDirection.Up && totalHeightDiff == 0 ? width - 1 : width - 2
if (totalHeightDiff + heightChange > height) {
heightChange = height - totalHeightDiff
}
if (heightChange == 0) return; // Error
rollerCoasterBuilder.addRamp(verticalDirection, heightChange, 1)
totalHeightDiff += heightChange
if (verticalDirection == RcbVerticalDirection.Up) {
// Unpower the final rail in the ramp, so it can turn
builder.move(BACK, 1)
rollerCoasterBuilder.addRail()
}
// Turn (unless we're done, in which case allow track to continue straight)
if (totalHeightDiff != height) {
builder.turn(turnDirection)
}
if (verticalDirection == RcbVerticalDirection.Up) {
builder.move(FORWARD, 1)
}
}
}
//% block="add free fall of height $height"
//% height.min=4 height.max=384 height.defl=10
//% blockId="rcbAddFreeFall" weight=75
export function addFreeFall(height: number) {
// Clear out free-fall area
let startPos = builder.position()
let cornerOne = undefined
let cornerTwo = undefined
builder.move(UP, 2)
builder.mark()
if (waterProtection || lavaProtection) {
// This is icky, but I don't know of a better way to get it relative to facing direction.
builder.shift(-1, 1, 1)
cornerOne = builder.position()
builder.shift(1, -1, -1)
}
builder.shift(2, -height - 2, 0)
if (waterProtection || lavaProtection) {
builder.shift(1, -1, -1)
cornerTwo = builder.position()
builder.shift(-1, 1, 1)
}
replaceWaterAndLava(cornerOne, cornerTwo)
builder.fill(AIR)
builder.teleportTo(startPos)
// Create wall to stop cart from moving forwards once it's off the track
addRail()
builder.move(FORWARD, 2)
builder.mark()
builder.move(UP, 2)
builder.fill(railBase, FillOperation.Keep)
// We need a bit of a ramp at the bottom to get moving again.
builder.move(BACK, 2)
builder.move(DOWN, height)
addUnpoweredPoweredRail()
builder.move(FORWARD, 1)
builder.move(DOWN, 1)
addPoweredRail()
builder.move(FORWARD, 1)
builder.move(DOWN, 1)
addUnpoweredPoweredRail()
}
//% group="Customization"
//% block="set base block to $blockType"
//% blockType.shadow=minecraftBlock
//% blockId="rcbSetBaseBlock" weight=20
export function setRollerCoasterBaseBlock(blockType: number) {
railBase = blockType
}
//% group="Customization"
//% block="set normal power interval to $interval"
//% interval.defl=5 interval.min=1 interval.max=8
//% blockId="rcbSetPowerInterval" weight=19
export function setNormalPowerInterval(interval: number = 5) {
powerInterval = interval
}
//% group="Customization"
//% block="set water protection to $value"
//% value.defl=true
//% blockId="rcbSetWaterProtection" weight=18
export function setWaterProtection(value: boolean) {
waterProtection = value
}
//% group="Customization"
//% block="set lava protection to $value"
//% value.defl=true
//% blockId="rcbSetLavaProtection" weight=17
export function setLavaProtection(value: boolean) {
lavaProtection = value
}
}