Skip to content

Commit 909a857

Browse files
committed
More branch mining improvements
1 parent 708c93b commit 909a857

File tree

3 files changed

+156
-120
lines changed

3 files changed

+156
-120
lines changed

docs/clientcommands.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,24 @@ declare class ControllablePlayer extends Player {
107107
/**
108108
* Teleports the player a limited distance. This function cannot teleport the player more than 0.5 blocks,
109109
* and is meant for alignment rather than movement. Use properties like {@link pressingForward} and
110-
* {@link sprinting} for movement.
110+
* {@link sprinting} for movement. Returns whether successful (the player was close enough to the
111+
* given position).
111112
* @param x The x-position to snap the player to
112113
* @param y The y-position to snap the player to
113114
* @param z The z-position to snap the player to
114115
* @param sync Whether to sync the position with the server immediately after the teleport, rather than
115116
* at the start of the next tick. If absent, defaults to false
116117
*/
117-
snapTo(x: number, y: number, z: number, sync?: boolean): void;
118+
snapTo(x: number, y: number, z: number, sync?: boolean): boolean;
119+
120+
/**
121+
* Moves the player in a straight line to the specified target position. Blocks until it reaches there.
122+
* Returns whether successful
123+
* @param x The x-position of the target
124+
* @param z The z-position of the target
125+
* @param smart Default true, whether to jump up single block gaps
126+
*/
127+
moveTo(x: number, z: number, smart?: boolean): boolean;
118128

119129
/**
120130
* The yaw of the player in degrees. 0 degrees is to the south, increasing clockwise
@@ -214,7 +224,8 @@ declare class ControllablePlayer extends Player {
214224
rightClick(): boolean;
215225
/**
216226
* Right clicks a block. Will click on the closest part of the block at the given position.
217-
* This function also modifies the player rotation to look at where they clicked.
227+
* This function also modifies the player rotation to look at where they clicked, if they are
228+
* not already hovering over the block.
218229
* @param x The x-position of the block to right click
219230
* @param y The y-position of the block to right click
220231
* @param z The z-position of the block to right click
@@ -232,7 +243,8 @@ declare class ControllablePlayer extends Player {
232243

233244
/**
234245
* Left clicks on a block. Will click on the closest part of the block at the given position.
235-
* This function also modifies the player rotation to look at where they clicked.
246+
* This function also modifies the player rotation to look at where they clicked, if they are
247+
* not already hovering over the block.
236248
*
237249
* Warning: left-clicking many blocks is a continuous mining action. This function on its own
238250
* will not work for this! For an easy way to mine blocks, see {@link longMineBlock}

docs/example_scripts/branch_mine.js

Lines changed: 57 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ var canWalkOn = function(block) {
4545
return true;
4646
};
4747

48+
var isFallingBlock = function(block) {
49+
return block === "gravel" || block === "sand" || block === "red_sand";
50+
};
51+
4852
var getTool = function(block) {
4953
if (block === "dirt" || block === "gravel" || block === "grass_block")
5054
return "shovel";
@@ -56,24 +60,12 @@ var getTool = function(block) {
5660
};
5761

5862
var centerPlayer = function() {
59-
var centerX = Math.floor(player.x) + 0.5;
60-
var centerZ = Math.floor(player.z) + 0.5;
61-
var isCentered = function() {
62-
return Math.abs(player.x - centerX) < 0.2 && Math.abs(player.z - centerZ) < 0.2;
63-
};
64-
if (isCentered())
65-
return;
66-
67-
player.lookAt(centerX, player.y + player.eyeHeight, centerZ);
68-
player.blockInput();
69-
player.pressingForward = true;
70-
while (!isCentered())
71-
tick();
7263
player.pressingForward = false;
73-
while (player.motionX * player.motionX + player.motionY * player.motionY + player.motionZ * player.motionZ < 0.0001)
64+
while (player.motionX * player.motionX + player.motionZ * player.motionZ > 0.001)
7465
tick();
75-
player.unblockInput();
76-
player.snapTo(centerX, player.y, centerZ);
66+
if (Math.abs(Math.floor(player.x) + 0.5 - player.x) >= 0.2 || Math.abs(Math.floor(player.z) + 0.5 - player.z) >= 0.2)
67+
return player.moveTo(Math.floor(player.x) + 0.5, Math.floor(player.z) + 0.5);
68+
return true;
7769
};
7870

7971
var pickUpItems = function() {
@@ -163,7 +155,27 @@ var mineBlock = function(x, y, z) {
163155
}
164156
if (!picked)
165157
return false;
166-
return player.longMineBlock(x, y, z);
158+
159+
var oldBlock = world.getBlock(x, y, z);
160+
if (oldBlock === "air") return true;
161+
var failCount = 0;
162+
do {
163+
if (!player.longMineBlock(x, y, z))
164+
failCount++;
165+
if (failCount > 5)
166+
return false;
167+
} while (world.getBlock(x, y, z) === oldBlock);
168+
169+
var blockAbove = world.getBlock(x, y + 1, z);
170+
171+
if (isFallingBlock(blockAbove)) {
172+
while (world.getBlock(x, y, z) !== blockAbove)
173+
tick();
174+
if (!mineBlock(x, y, z))
175+
return false;
176+
}
177+
178+
return true;
167179
};
168180

169181
var makeBridge = function(x, y, z, dx, dz) {
@@ -251,31 +263,10 @@ var mineNearbyOre = function(x, y, z) {
251263
centerPlayer();
252264

253265
// do the step up
254-
player.lookAt(player.x + dx, player.y + player.eyeHeight, player.z + dz);
255-
player.blockInput();
256-
player.pressingForward = true;
257-
for (var i = 0; i < 10; i++)
258-
tick();
259-
player.jumping = true;
260-
tick();
261-
player.jumping = false;
262-
while (Math.floor(player.x) === x && Math.floor(player.z) === z)
263-
tick();
264-
player.pressingForward = false;
265-
player.unblockInput();
266-
267-
mineNearbyOre(x + dx, y + 1, z + dz);
268-
266+
if (!player.moveTo(x + dx + 0.5, z + dz + 0.5)) return false;
267+
if (!mineNearbyOre(x + dx, y + 1, z + dz)) return false;
269268
centerPlayer();
270-
271-
player.lookAt(player.x - dx, player.y + player.eyeHeight, player.z - dz);
272-
player.blockInput();
273-
player.pressingForward = true;
274-
var currentY = player.y;
275-
while (player.y === currentY)
276-
tick();
277-
player.pressingForward = false;
278-
player.unblockInput();
269+
if (!player.moveTo(x + 0.5, z + 0.5)) return false;
279270
}
280271

281272
// mine blocks around feet level
@@ -294,109 +285,55 @@ var mineNearbyOre = function(x, y, z) {
294285
}
295286

296287
centerPlayer();
297-
298-
player.lookAt(player.x + dx, player.y + player.eyeHeight, player.z + dz);
299-
player.blockInput();
300-
player.pressingForward = true;
301-
while (Math.floor(player.x) === x && Math.floor(player.z) === z)
302-
tick();
303-
player.pressingForward = false;
304-
player.unblockInput();
305-
306-
mineNearbyOre(x + dx, y, z + dz);
307-
288+
if (!player.moveTo(x + dx + 0.5, z + dz + 0.5)) return false;
289+
if (!mineNearbyOre(x + dx, y, z + dz)) return false;
308290
centerPlayer();
309-
310-
player.lookAt(player.x - dx, player.y + player.eyeHeight, player.z - dz);
311-
player.blockInput();
312-
player.pressingForward = true;
313-
while (Math.floor(player.x) !== x || Math.floor(player.z) !== z)
314-
tick();
315-
player.pressingForward = false;
316-
player.unblockInput();
291+
if (!player.moveTo(x + 0.5, z + 0.5)) return false;
317292

318293
// mine block below feet level
319294
if (isWantedBlock(world.getBlock(x + dx, y - 1, z + dz))) {
320295
if (!mineBlock(x + dx, y - 1, z + dz))
321296
return false;
322297
if (!canWalkThrough(world.getBlock(x + dx, y - 2, z + dz))) {
323298
// collect the block
324-
player.lookAt(player.x + dx, player.y + player.eyeHeight, player.z + dz);
325-
player.blockInput();
326-
player.pressingForward = true;
327-
while (Math.floor(player.x) === x && Math.floor(player.z) === z)
328-
tick();
329-
player.pressingForward = false;
330-
player.unblockInput();
331-
299+
if (!player.moveTo(x + dx + 0.5, z + dz + 0.5)) return false;
332300
if (!pickUpItems())
333301
return false;
334-
335-
mineNearbyOre(x + dx, y, z + dz);
336-
302+
if (!mineNearbyOre(x + dx, y, z + dz)) return false;
337303
centerPlayer();
338-
339-
player.lookAt(player.x - dx, player.y + player.eyeHeight, player.z - dz);
340-
player.blockInput();
341-
player.pressingForward = true;
342-
for (var i = 0; i < 10; i++)
343-
tick();
344-
player.jumping = true;
345-
tick();
346-
player.jumping = false;
347-
while (Math.floor(player.x) !== x || Math.floor(player.z) !== z)
348-
tick();
349-
player.pressingForward = false;
350-
player.unblockInput();
304+
if (!player.moveTo(x + 0.5, z + 0.5)) return false;
351305
}
352306
}
353307
}
354308
}
355309
}
356310

311+
// keep mining for blocks possible exposed by the mining operation
357312
for (var dy = 1; dy >= 0; dy--) {
358313
for (var i = 0; i < 4; i++) {
359314
var dx = cardinals4[i][0], dz = cardinals4[i][1];
360315
if (canWalkThrough(world.getBlock(x + dx, y + dy, z + dz))) {
361316
for (var j = 0; j < 6; j++) {
362317
var ddx = cardinals6[j][0], ddy = cardinals6[j][1], ddz = cardinals6[j][2];
318+
if (ddy === -1 && dy === 0) continue; // mineNearbyOre doesn't mine straight down
363319
if (isWantedBlock(world.getBlock(x + dx + ddx, y + dy + ddy, z + dz + ddz))) {
364320
if (!clearWay(x, y, z, dx, dz))
365321
return false;
366322

367323
centerPlayer();
368-
369-
player.lookAt(player.x + dx, player.y + player.eyeHeight, player.z + dz);
370-
player.blockInput();
371-
player.pressingForward = true;
372-
while (Math.floor(player.x) === x && Math.floor(player.z) === z)
373-
tick();
374-
player.pressingForward = false;
375-
player.unblockInput();
376-
377-
mineNearbyOre(x + dx, y, z + dz);
378-
324+
if (!player.moveTo(x + dx + 0.5, z + dz + 0.5)) return false;
325+
if (!mineNearbyOre(x + dx, y, z + dz)) return false;
379326
centerPlayer();
380-
381-
player.lookAt(player.x - dx, player.y + player.eyeHeight, player.z - dz);
382-
player.blockInput();
383-
player.pressingForward = true;
384-
while (Math.floor(player.x) !== x || Math.floor(player.z) !== z)
385-
tick();
386-
player.pressingForward = false;
387-
player.unblockInput();
327+
if (!player.moveTo(x + 0.5, z + 0.5)) return false;
388328
}
389329
}
390330
}
391331
}
392332
}
333+
return true;
393334
};
394335

395-
var makeTunnel = function(dx, dz) {
396-
var x = Math.floor(player.x);
397-
var y = Math.floor(player.y);
398-
var z = Math.floor(player.z);
399-
336+
var makeTunnel = function(x, y, z, dx, dz) {
400337
centerPlayer();
401338

402339
if (!clearWay(x, y, z, dx, dz))
@@ -432,11 +369,21 @@ var makeTunnel = function(dx, dz) {
432369
print("Couldn't place torch");
433370
}
434371

435-
mineNearbyOre(x + dx, y, z + dz);
372+
if (!mineNearbyOre(x + dx, y, z + dz)) return false;
436373

437374
return true;
438375
};
439376

440-
while (makeTunnel(1, 0));
377+
var x = Math.floor(player.x);
378+
var y = Math.floor(player.y);
379+
var z = Math.floor(player.z);
380+
var dx = 1, dz = 0;
381+
382+
while (true) {
383+
if (!makeTunnel(x, y, z, dx, dz))
384+
break;
385+
x += dx;
386+
z += dz;
387+
}
441388

442389
print("Finished making tunnel");

0 commit comments

Comments
 (0)