This repository was archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuild_platform.js
More file actions
258 lines (235 loc) · 9.81 KB
/
build_platform.js
File metadata and controls
258 lines (235 loc) · 9.81 KB
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
var GROUP_NAME = "rsf";
var PLATFORM_Y = 40;
var TEMPLATE_Y = 20;
var TEMPLATE_Z = -3367;
var PLATFORM_X_MIN = 3227;
var PLATFORM_X_MAX = 3305;
var PLATFORM_Z_MIN = -3442;
var PLATFORM_Z_MAX = -3364;
var STONE_BLOCK = "minecraft:stone";
var SLAB_BLOCK = "minecraft:smooth_stone_slab";
var craftingTable;
var directionNames = ["north", "east", "south", "west"];
var dx = [0, 1, 0, -1];
var dz = [-1, 0, 1, 0];
var opposite = function(dir) { return (dir + 2) % 4; };
var isChestContainer = function(type) { return type === "generic_9x3" || type === "generic_9x6"; };
var openContainer = function(x, y, z, type) {
if (typeof(type) === "string") {
var typeName = type;
type = function(it) { return it === typeName };
}
player.lookAt(x, y, z);
player.syncRotation();
if (!player.rightClick(x, y, z))
throw new Error("Could not right click on container at " + x + ", " + y + ", " + z);
var timeout = 0;
while (player.currentContainer === null || !type(player.currentContainer.type)) {
tick();
timeout++;
if (timeout > 100)
throw new Error("Failed to open container at " + x + ", " + y + ", " + z);
}
anticheatDelay();
};
var anticheatDelay = function() {
for (var i = 0; i < 20; i++)
tick();
};
var anticheatMediumDelay = function() {
for (var i = 0; i < 4; i++)
tick();
};
var anticheatLessDelay = function() {
tick();
};
var findReplenishArea = function() {
var found = false;
var minDistanceSq = 1000000;
for (var xDelta = -7; xDelta <= 7; xDelta++) {
for (var zDelta = -7; zDelta <= 7; zDelta++) {
for (var yDelta = -7; yDelta <= 7; yDelta++) {
var distanceSq = xDelta * xDelta + yDelta * yDelta + zDelta * zDelta;
if (distanceSq >= minDistanceSq)
continue;
var x = player.x + xDelta;
var y = player.y + player.eyeHeight + yDelta;
var z = player.z + zDelta;
if (world.getBlock(x, y, z) === "crafting_table") {
craftingTable = [x, y, z];
minDistanceSq = distanceSq;
found = true;
}
}
}
}
if (!found)
throw new Error("Could not find replenish area");
return craftingTable;
};
var gatherStone = function(stoneNeeded) {
for (var xDelta = -5; xDelta <= 5; xDelta++) {
for (var zDelta = -5; zDelta <= 5; zDelta++) {
for (var yDelta = -5; yDelta <= 5; yDelta++) {
var x = player.x + xDelta;
var y = player.y + player.eyeHeight + yDelta;
var z = player.z + zDelta;
if (world.getBlock(x, y, z) === "chest" || world.getBlock(x, y, z) === "trapped_chest") {
try {
openContainer(x, y, z, isChestContainer);
var chestItems = player.currentContainer.items;
for (var i = 0; i < chestItems.length; i++) {
if (chestItems[i].id === STONE_BLOCK) {
stoneNeeded -= chestItems[i].Count;
player.currentContainer.click(i, {type: "quick_move"});
anticheatLessDelay();
if (stoneNeeded <= 0)
return;
}
}
player.closeContainer();
anticheatDelay();
} catch (e) {
if (!(e instanceof Error))
throw e;
}
}
}
}
}
throw new Error("Not enough stone");
};
var ensureResources = function() {
// Check if we already have the resources
var foundStone = 0, foundSlabs = 0;
var items = player.inventory.items;
for (var slot = 0; slot < 36; slot++) {
if (items[slot].id === STONE_BLOCK)
foundStone += items[slot].Count;
else if (items[slot].id === SLAB_BLOCK)
foundSlabs += items[slot].Count;
}
if (foundStone !== 0 && foundSlabs !== 0)
return;
var slabsNeeded = Math.max(0, 64 * 10 - foundSlabs);
slabsNeeded = Math.ceil(slabsNeeded / 6) * 6;
var stoneNeeded = 64 * 10 - foundStone;
stoneNeeded += slabsNeeded / 2;
// Travel near to the replenish area (blocks may not be visible until nearby)
var xDistanceToReplenishArea = replenishArea[0] + 0.5 - player.x;
var zDistanceToReplenishArea = replenishArea[2] + 0.5 - player.z;
var hDistanceToReplenishArea = Math.sqrt(xDistanceToReplenishArea * xDistanceToReplenishArea + zDistanceToReplenishArea * zDistanceToReplenishArea);
var targetX = replenishArea[0] + 0.5 - xDistanceToReplenishArea * 2 / hDistanceToReplenishArea;
var targetZ = replenishArea[2] + 0.5 - zDistanceToReplenishArea * 2 / hDistanceToReplenishArea;
if (!player.pathTo(targetX, player.y, targetZ))
throw new Error("Could not move to replenish area");
// Re-find the replenish area in case it has moved
findReplenishArea();
if (stoneNeeded > 0) {
gatherStone(stoneNeeded);
}
// Craft slabs
if (slabsNeeded > 0) {
openContainer(craftingTable[0], craftingTable[1], craftingTable[2], "crafting");
var recipeCount = slabsNeeded / 6;
while (recipeCount > 0) {
for (var slot = 1; slot <= 3; slot++) {
var itemsNeeded = Math.min(64, recipeCount);
items = player.inventory.items;
for (var i = 0; i < 36; i++) {
if (items[i].id === STONE_BLOCK) {
var count = items[i].Count;
player.inventory.click(i); // pickup the stone
anticheatMediumDelay();
if (count <= itemsNeeded || itemsNeeded === 64) {
// drop down all the stone
itemsNeeded -= count;
player.currentContainer.click(slot);
anticheatMediumDelay();
} else {
// drop down as many stone as needed then put the rest back
for (var j = 0; j < itemsNeeded; j++) {
player.currentContainer.click(slot, {rightClick: true});
anticheatMediumDelay();
}
player.inventory.click(i);
anticheatMediumDelay();
itemsNeeded = 0;
}
if (itemsNeeded <= 0)
break;
}
}
}
var timeout = 0;
while (player.currentContainer.items[0].id !== SLAB_BLOCK) {
tick();
timeout++;
if (timeout > 100)
throw new Error("Failed to craft slabs");
}
player.currentContainer.click(0, {type: "quick_move"});
anticheatMediumDelay();
recipeCount -= 64;
}
player.closeContainer();
anticheatDelay();
}
if (foundStone === 0) {
if (!player.pick(STONE_BLOCK))
throw new Error("Someone is tampering with the bot");
anticheatDelay();
chat("/ctf " + GROUP_NAME);
}
};
var placePlatform = function() {
for (var x = PLATFORM_X_MAX; x >= PLATFORM_X_MIN; x--) {
var countDown = x % 2 === PLATFORM_X_MAX % 2;
var standingX = x;
if (!countDown)
standingX--;
for (var z = countDown ? PLATFORM_Z_MAX : PLATFORM_Z_MIN; countDown ? z >= PLATFORM_Z_MIN : z <= PLATFORM_Z_MAX; z += countDown ? -1 : 1) {
ensureResources();
if (world.getBlock(x, PLATFORM_Y, z).endsWith("_slab"))
continue;
if (!world.getBlockState(x, TEMPLATE_Y, z).solid || world.getBlock(x, TEMPLATE_Y, z).endsWith("sign"))
continue;
if (!player.pathTo(standingX + 0.5, player.y, z + 0.5))
throw new Error("Movement to " + standingX + ", " + z + " failed");
player.pick(SLAB_BLOCK);
var placementSide = -1;
for (var dir = 0; dir < 4; dir++) {
if (world.getBlockState(x + dx[dir], PLATFORM_Y, z + dz[dir]).solid) {
placementSide = dir;
break;
}
}
if (placementSide === -1)
throw new Error("Nothing to place the slab against at " + x + ", " + z);
var closestPoint = world.getClosestVisiblePoint(x + dx[placementSide], PLATFORM_Y, z + dz[placementSide], directionNames[opposite(dir)]);
if (!closestPoint) {
if (!player.moveTo(standingX + 0.5, z + 0.5))
throw new Error("Movement to " + standingX + ", " + z + " failed");
closestPoint = world.getClosestVisiblePoint(x + dx[placementSide], PLATFORM_Y, z + dz[placementSide], directionNames[opposite(dir)]);
if (!closestPoint)
throw new Error("Slab not in view");
}
var clickX = closestPoint.x;
var clickY = closestPoint.y;
var clickZ = closestPoint.z;
if (clickY - Math.floor(clickY) < 0.6)
clickY += 0.1;
anticheatMediumDelay();
player.lookAt(clickX, clickY, clickZ);
anticheatMediumDelay();
player.rightClick(x + dx[placementSide], PLATFORM_Y, z + dz[placementSide], directionNames[opposite(dir)]);
}
}
};
replenishArea = findReplenishArea();
chat("/cto");
if (player.pick(STONE_BLOCK)) {
anticheatDelay();
chat("/ctf " + GROUP_NAME);
}
placePlatform();