/
minecraft-cannon.py
executable file
·393 lines (336 loc) · 14.7 KB
/
minecraft-cannon.py
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
382
383
384
385
386
387
388
389
390
391
392
393
#www.stuffaboutcode.com
#Raspberry Pi, Minecraft Cannon
#import the minecraft.py module from the minecraft directory
import mcpi.minecraft as minecraft
#import minecraft block module
import mcpi.block as block
#import time, so delays can be used
import time
#import math, so we can use sin, cos and other maths functions
import math
#import cmd, so we can use the command line interpreter
import cmd
#Common functions
# compares vec3 objects, if they are the same returns true
def matchVec3(vec1, vec2):
if ((vec1.x == vec2.x) and (vec1.y == vec2.y) and (vec1.z == vec2.z)):
return True
else:
return False
# finds point on sphere (based on polar co-ordinates)
def findPointOnSphere(cx, cy, cz, radius, phi, theta):
#phi - angle around the pole 0<= phi <= 360
#theta - angle from plan 'up' -90 <= theta <= 90
x = cx + radius * math.cos(math.radians(theta)) * math.cos(math.radians(phi))
z = cz + radius * math.cos(math.radians(theta)) * math.sin(math.radians(phi))
y = cy + radius * math.sin(math.radians(theta))
return int(round(x,0)), int(round(y,0)), int(round(z,0))
#Class to handle minecraft drawing
class MinecraftDrawing:
def __init__(self, mc):
self.mc = mc
# draw point
def drawPoint3d(self, x, y, z, blockType, blockData=0):
self.mc.setBlock(x,y,z,blockType,blockData)
#print "x = " + str(x) + ", y = " + str(y) + ", z = " + str(z)
# draws a sphere
def drawSphere(self, vec3, radius, blockType, blockData=0):
# if the diameter is greater than 1, create a sphere, ot
if radius * 2.0 > 1.5:
#round up radius
radius = int(radius + 0.5)
for x in range(radius*-1,radius):
for y in range(radius*-1, radius):
for z in range(radius*-1,radius):
if x**2 + y**2 + z**2 < radius**2:
self.drawPoint3d(vec3.x + x, vec3.y + y, vec3.z + z, blockType, blockData)
else:
self.drawPoint3d(vec3.x, vec3.z, vec3.y, blockType, blockData)
# draws a face, when passed a collection of vertices which make up a polyhedron
def drawFace(self, vertices, blockType, blockData=0):
# get the edges of the face
edgesVertices = []
# persist first vertex
firstVertex = vertices[0]
# loop through vertices and get edges
vertexCount = 0
for vertex in vertices:
vertexCount+=1
if vertexCount > 1:
# got 2 vertices, get the points for the edge
edgesVertices = edgesVertices + self.getLine(lastVertex.x, lastVertex.y, lastVertex.z, vertex.x, vertex.y, vertex.z)
# persist the last vertex found
lastVertex = vertex
# get edge between the last and first vertices
edgesVertices = edgesVertices + self.getLine(lastVertex.x, lastVertex.y, lastVertex.z, firstVertex.x, firstVertex.y, firstVertex.z)
# sort edges vertices
def keyX( point ): return point.x
def keyY( point ): return point.y
def keyZ( point ): return point.z
edgesVertices.sort( key=keyZ )
edgesVertices.sort( key=keyY )
edgesVertices.sort( key=keyX )
# not very performant but wont have gaps between in complex models
for vertex in edgesVertices:
vertexCount+=1
# got 2 vertices, draw lines between them
if (vertexCount > 1):
self.drawLine(lastVertex.x, lastVertex.y, lastVertex.z, vertex.x, vertex.y, vertex.z, blockType, blockData)
#print "x = " + str(lastVertex.x) + ", y = " + str(lastVertex.y) + ", z = " + str(lastVertex.z) + " x2 = " + str(vertex.x) + ", y2 = " + str(vertex.y) + ", z2 = " + str(vertex.z)
# persist the last vertex found
lastVertex = vertex
# draw's all the points in a collection of vertices with a block
def drawVertices(self, vertices, blockType, blockData=0):
for vertex in vertices:
self.drawPoint3d(vertex.x, vertex.y, vertex.z, blockType, blockData)
# draw line
def drawLine(self, x1, y1, z1, x2, y2, z2, blockType, blockData=0):
self.drawVertices(self.getLine(x1, y1, z1, x2, y2, z2), blockType, blockData)
# returns points on a line
def getLine(self, x1, y1, z1, x2, y2, z2):
# return maximum of 2 values
def MAX(a,b):
if a > b: return a
else: return b
# return step
def ZSGN(a):
if a < 0: return -1
elif a > 0: return 1
elif a == 0: return 0
# list for vertices
vertices = []
# if the 2 points are the same, return single vertice
if (x1 == x2 and y1 == y2 and z1 == z2):
vertices.append(minecraft.Vec3(x1, y1, z1))
# else get all points in edge
else:
dx = x2 - x1
dy = y2 - y1
dz = z2 - z1
ax = abs(dx) << 1
ay = abs(dy) << 1
az = abs(dz) << 1
sx = ZSGN(dx)
sy = ZSGN(dy)
sz = ZSGN(dz)
x = x1
y = y1
z = z1
# x dominant
if (ax >= MAX(ay, az)):
yd = ay - (ax >> 1)
zd = az - (ax >> 1)
loop = True
while(loop):
vertices.append(minecraft.Vec3(x, y, z))
if (x == x2):
loop = False
if (yd >= 0):
y += sy
yd -= ax
if (zd >= 0):
z += sz
zd -= ax
x += sx
yd += ay
zd += az
# y dominant
elif (ay >= MAX(ax, az)):
xd = ax - (ay >> 1)
zd = az - (ay >> 1)
loop = True
while(loop):
vertices.append(minecraft.Vec3(x, y, z))
if (y == y2):
loop=False
if (xd >= 0):
x += sx
xd -= ay
if (zd >= 0):
z += sz
zd -= ay
y += sy
xd += ax
zd += az
# z dominant
elif(az >= MAX(ax, ay)):
xd = ax - (az >> 1)
yd = ay - (az >> 1)
loop = True
while(loop):
vertices.append(minecraft.Vec3(x, y, z))
if (z == z2):
loop=False
if (xd >= 0):
x += sx
xd -= az
if (yd >= 0):
y += sy
yd -= az
z += sz
xd += ax
yd += ay
return vertices
#Class for a bullet in Minecraft
class MinecraftBullet:
def __init__(self, mc, startPos, direction, angle, velocity, blastRadius):
#Value validation
if direction < 0 or direction > 359:
raise ValueError("direction must be an angle 0 - 359 degrees")
if angle < 0 or angle > 90:
raise ValueError("angle must be between 0 - 90 degrees")
if velocity < 0 or velocity > 1:
raise ValueError("velocity must be between 0 - 1 blocks per tick")
#Gravity constant - its 'similar' to earth but different so stuff flies far but also stays inside the minecraft world!
self.gravity = -0.0098
#Properties
self.mc = mc
self.startPos = startPos
self.currentPos = startPos
self.direction = direction
self.angle = angle
self.velocity = velocity
self.blastRadius = blastRadius
self.ticks = 0
#Minecraft Drawing Class
self.mcDrawing = MinecraftDrawing(mc)
#Calculate velocities in X and Z and Y based on direction and velocity
# dont ask me how this works, I did it all on a bit of paper and Im not sure myself now!
# y velocity will be re-calculated each time the bullet is updated to take gravity into account
sinYVelocity = math.sin(math.radians(angle))
self.yStartVelocity = velocity * sinYVelocity
# work out horizontal direction in minecraft world, +/- in X,Z, taking into account velocity in Y
self.xVelocity = (math.cos(math.radians(direction)) * velocity) * (1 - sinYVelocity)
self.zVelocity = (math.sin(math.radians(direction)) * velocity) * (1 - sinYVelocity)
#Draw the bullet
self.drawPos = startPos
self.draw()
def draw(self):
self.mcDrawing.drawPoint3d(self.drawPos.x, self.drawPos.y, self.drawPos.z, block.TNT)
def clear(self):
self.mcDrawing.drawPoint3d(self.drawPos.x, self.drawPos.y, self.drawPos.z, block.AIR)
def update(self):
#Update the Bullet, should be called once per 'tick'
# calculate new y velocity
self.ticks += 1
self.yVelocity = self.yStartVelocity + self.gravity * self.ticks
# find the bullets new position
newPos = minecraft.Vec3(self.currentPos.x + self.xVelocity,
self.startPos.y + (self.yStartVelocity * self.ticks + 0.5 * (self.gravity * pow(self.ticks,2))),
self.currentPos.z + self.zVelocity)
#Has the bullet moved from its last drawn position
# round the new position and compare it to the last drawn
newDrawPos = minecraft.Vec3(int(round(newPos.x, 0)),
int(round(newPos.y, 0)),
int(round(newPos.z, 0)))
movedBullet = True
if matchVec3(newDrawPos, self.drawPos) == False:
# if the bullet is moving to a block of air, move it, otherwise explode
if self.mc.getBlock(newDrawPos.x, newDrawPos.y, newDrawPos.z) == block.AIR:
# clear the last drawn bullet
self.clear()
# move the draw position
self.drawPos = minecraft.Vec3(newDrawPos.x, newDrawPos.y, newDrawPos.z)
# draw the bullet
self.draw()
else:
#exploded
self.mcDrawing.drawSphere(newDrawPos, self.blastRadius, block.AIR)
movedBullet = False
#Update the current position
self.currentPos = newPos
return movedBullet
#Class for a cannon in minecraft
class MinecraftCannon:
def __init__(self, mc, position):
#Constants
self.lenghtOfGun = 5
#Properties
self.mc = mc
self.position = position
self.angle = 30
self.direction = 0
self.baseOfGun = minecraft.Vec3(position.x, position.y + 2, position.z)
self.endOfGun = self.findEndOfGun()
# minecraft drawing class
self.mcDrawing = MinecraftDrawing(mc)
# draw gun
self.drawCannon()
self.drawGun()
def fire(self, velocity, blastRadius):
#create new bullet
bullet = MinecraftBullet(self.mc, self.endOfGun, self.direction, self.angle, velocity, blastRadius)
return bullet
def drawCannon(self):
#mcDrawing = self.mcDrawing
position = self.position
self.mc.setBlocks(position.x - 1, position.y, position.z - 1,
position.x + 1, position.y + 1, position.z + 1,
block.WOOD_PLANKS)
def clearCannon(self):
position = self.position
self.mc.setBlocks(position.x - 1, position.y, position.z - 1,
position.x + 1, position.y + 2, position.z + 1,
block.AIR)
self.clearGun()
def findEndOfGun(self):
x, y, z = findPointOnSphere(self.baseOfGun.x, self.baseOfGun.y, self.baseOfGun.z,
self.lenghtOfGun, self.direction, self.angle)
return minecraft.Vec3(x, y, z)
def drawGunInMC(self, blockType, blockData=0):
x, y, z = findPointOnSphere(self.baseOfGun.x, self.baseOfGun.y, self.baseOfGun.z,
self.lenghtOfGun-1, self.direction, self.angle)
self.mcDrawing.drawLine(self.baseOfGun.x, self.baseOfGun.y, self.baseOfGun.z,
x, y, z,
blockType, blockData)
def drawGun(self):
self.drawGunInMC(block.WOOL.id, 15)
def clearGun(self):
self.drawGunInMC(block.AIR)
def setDirection(self, direction):
#Value validation
if direction < 0 or direction > 360:
raise ValueError("direction must be an angle 0 - 360 degrees")
self.clearGun()
self.direction = direction
self.endOfGun = self.findEndOfGun()
self.drawGun()
def setAngle(self, angle):
#Value validation
if angle < 0 or angle > 90:
raise ValueError("angle must be between 0 - 90 degrees")
self.clearGun()
self.angle = angle
self.endOfGun = self.findEndOfGun()
self.drawGun()
# Class to manage the command line interface
class CannonCommands(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = "Stuffaboutcode.com Cannon >> "
self.intro = "Minecraft Cannon - www.stuffaboutcode.com"
def do_exit(self, args):
"Exit cannon [exit]"
self.cannon.clearCannon()
return -1
def do_start(self, args):
"Start cannon and create it [start]"
self.mc = minecraft.Minecraft.create()
playerPos = self.mc.player.getTilePos()
self.cannon = MinecraftCannon(self.mc, minecraft.Vec3(playerPos.x + 3, playerPos.y, playerPos.z))
def do_fire(self, args):
"Fire the cannon [fire]"
bullet = self.cannon.fire(1, 3)
while(bullet.update()):
time.sleep(0.01)
def do_rotate(self, direction):
"Rotate cannon [rotate <angle>]"
self.cannon.setDirection(int(direction))
def do_tilt(self, angle):
"Tilt cannon [tilt <angle>]"
self.cannon.setAngle(int(angle))
def do_EOF(self, line):
return True
#main program
if __name__ == "__main__":
CannonCommands().cmdloop()