-
Notifications
You must be signed in to change notification settings - Fork 18
/
hall_traps.py
415 lines (371 loc) · 15.5 KB
/
hall_traps.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import inspect
import random
import sys
import cfg
import materials
from utils import (
Vec,
enum,
iterate_cube,
get_tile_entity_tags,
weighted_choice
)
dv = [
Vec(0, -1, 0),
Vec(1, 0, 0),
Vec(0, 1, 0),
Vec(-1, 0, 0)
]
dirs = enum('N', 'E', 'S', 'W')
class Blank(object):
_name = 'blank'
_min_width = 0
_max_width = 32
_min_length = 0
_max_length = 32
def __init__(self, parent, position, size, length, direction):
self.parent = parent
self.position = position
self.size = size
self.length = length
self.direction = direction
# Set delta width and length vectors based on halleasy direction.
if direction == dirs.E:
self.dl = Vec(1, 0, 0)
self.dw = Vec(0, 0, 1)
else:
self.dl = Vec(0, 0, 1)
self.dw = Vec(1, 0, 0)
def apply_template(self, tmpl, cmds, mat, reps, pos):
'''
Apply a template.
tmpl = the actual template
mat = materials lookup
reps for each template row
pos = starting position
'''
for l in xrange(len(tmpl[0])):
for rl in xrange(reps['l'][l]):
for d in xrange(len(tmpl)):
repw = 0
for w in xrange(len(tmpl[d][l])):
for rw in xrange(reps['w'][w]):
q = pos + self.dw * repw \
+ Vec(0, -1, 0) * d
repw += 1
if tmpl[d][l][w] == 'XX':
continue
elif tmpl[d][l][w] == '~T':
if random.randint(1, 100) <= 90:
self.parent.setblock(
q,
materials.RedstoneWire,
hide=True
)
else:
self.parent.setblock(
q,
materials.TNT,
hide=True
)
continue
elif (
tmpl[d][l][w][0] == '~' and
random.randint(1, 100) <= 50
):
continue
self.parent.setblock(
q,
mat[tmpl[d][l][w]][0],
mat[tmpl[d][l][w]][1],
hide=True
)
if tmpl[d][l][w] in cmds:
self.parent.addtileentity(
get_tile_entity_tags(
eid='command_block',
Pos=q,
Command=cmds[tmpl[d][l][w]]
)
)
pos += self.dl
def mark_hallway(self, mat):
''' Handy way to mark the location of a hall trap on the map. For
debugging.'''
for p in iterate_cube(
self.position,
self.position + self.dw * (self.size - 1) + self.dl * (self.length - 1)
):
self.parent.setblock(p, mat)
def render(self):
pass
class ArrowTrap(Blank):
_name = 'arrowtrap'
_min_width = 3
_min_length = 3
_explosions = False
def render(self):
# Figure out what sort of thing will be fired
name = weighted_choice(cfg.master_projectile_traps)
data_tag = cfg.lookup_projectile_traps[name][2]
name = cfg.lookup_projectile_traps[name][0]
# Start position
pos = self.position.down(5) + self.dl - self.dw
# Materials lookup
# Default is looking East
mat = {
'RW': [materials.RedstoneWire, 0],
'*>': [materials.RedstoneRepeaterOff, 2],
'<*': [materials.RedstoneRepeaterOff, 0],
'C1': [materials.CommandBlock, 0],
'C2': [materials.CommandBlock, 0],
'[]': [materials.StoneBrick, 3],
'~P': [materials.StonePressurePlate, 0],
'~T': [materials.TNT, 0],
}
# Commands
cmds = {
'C1': '/summon {0} ~ ~3 ~1.8 {{Motion:{1},direction:{1},{2}}}'.format(
name,
'[0.0,0.2,1.0]',
data_tag),
'C2': '/summon {0} ~ ~3 ~-1.8 {{Motion:{1},direction:{1},{2}}}'.format(
name,
'[0.0,0.2,-1.0]',
data_tag)}
# If looking South, rotate some materials, and adjust the command
# blocks.
if self.direction == dirs.S:
mat['*>'][1] = 1
mat['<*'][1] = 3
cmds = {
'C1': '/summon {0} ~1.8 ~3 ~ {{Motion:{1},direction:{1},{2}}}'.format(
name,
'[1.0,0.2,0.0]',
data_tag),
'C2': '/summon {0} ~-1.8 ~3 ~ {{Motion:{1},direction:{1},{2}}}'.format(
name,
'[-1.0,0.2,0.0]',
data_tag)}
# Trap template.
# tmpl[level][dl][dw]
tmpl = [[
['C1', '<*', 'RW', '*>', 'C2'],
], [
['XX', 'XX', 'XX', 'XX', 'XX'],
], [
['XX', 'XX', '~P', 'XX', 'XX'],
], [
['XX', '[]', 'XX', '[]', 'XX'],
]]
# Make boom!
if self._explosions:
tmpl[0][0][2] = '~T'
tmpl[0][0][0] = '[]'
tmpl[0][0][4] = '[]'
# Repetitions for each template row and column.
reps = {
'w': [1, 1, self.size - 2, 1, 1],
'l': [self.length - 2],
}
self.apply_template(tmpl, cmds, mat, reps, pos)
# Vary the timing on the repeaters
for p in iterate_cube(
self.position.down(5),
self.position.down(5) + self.dw * (self.size - 1) + self.dl * (self.length - 1)
):
if (
p in self.parent.blocks and
self.parent.blocks[p].material == materials.RedstoneRepeaterOff
):
self.parent.blocks[p].data += random.choice((4, 8, 12))
class ExplodingArrowTrap(ArrowTrap):
_name = 'explodingarrowtrap'
_explosions = True
def render(self):
super(ExplodingArrowTrap, self).render()
class LavaTrap(Blank):
_name = 'lavatrap'
_min_width = 3
_max_width = 4
_min_length = 9
def render(self):
# Length of the actual trap door part.
tlength = self.length - 8
# Materials lookup
# Default is looking East
mat = {
'CC': [materials._ceiling, 0],
'SF': [materials._subfloor, 0],
'RW': [materials.RedstoneWire, 0],
'RB': [materials.BlockOfRedstone, 0],
'-*': [materials.RedstoneTorchOn, 3],
'*-': [materials.RedstoneTorchOn, 4],
'P>': [materials.StickyPiston, 3 + 8],
'-|': [materials.PistonExtension, 3 + 8],
'<P': [materials.StickyPiston, 2 + 8],
'|-': [materials.PistonExtension, 2 + 8],
'C1': [materials.CommandBlock, 0],
'C2': [materials.CommandBlock, 0],
'C3': [materials.CommandBlock, 0],
'C4': [materials.CommandBlock, 0],
'C5': [materials.CommandBlock, 0],
'C6': [materials.CommandBlock, 0],
'C7': [materials.CommandBlock, 0],
'C8': [materials.CommandBlock, 0],
'PP': [materials.StonePressurePlate, 0],
'AR': [materials.Air, 0],
'LA': [materials.Lava, 0],
'ST': [materials.Stone, 0],
}
# Commands that open and close the trap.
cmds = {'C1': '/fill ~2 ~1 ~-1 ~{} ~1 ~-1 minecraft:stone 0 replace'.format(tlength + 1),
'C2': '/fill ~2 ~1 ~1 ~{} ~1 ~1 minecraft:stone 0 replace'.format(tlength + 1),
'C3': '/fill ~-2 ~1 ~-1 ~-{} ~1 ~-1 minecraft:stone 0 replace'.format(tlength + 1),
'C4': '/fill ~-2 ~1 ~1 ~-{} ~1 ~1 minecraft:stone 0 replace'.format(tlength + 1),
'C5': '/fill ~2 ~0 ~0 ~{} ~0 ~0 minecraft:redstone_block 0 replace'.format(tlength + 1),
'C6': '/fill ~2 ~0 ~0 ~{} ~0 ~0 minecraft:redstone_block 0 replace'.format(tlength + 1),
'C7': '/fill ~-2 ~0 ~0 ~-{} ~0 ~0 minecraft:redstone_block 0 replace'.format(tlength + 1),
'C8': '/fill ~-2 ~0 ~0 ~-{} ~0 ~0 minecraft:redstone_block 0 replace'.format(tlength + 1),
}
# If looking South, rotate some materials, and adjust the command
# blocks.
if self.direction == dirs.S:
mat['-*'][1] = 1
mat['*-'][1] = 2
mat['P>'][1] = 5 + 8
mat['-|'][1] = 5 + 8
mat['<P'][1] = 4 + 8
mat['|-'][1] = 4 + 8
cmds = {'C1': '/fill ~-1 ~1 ~2 ~-1 ~1 ~{} minecraft:stone 0 replace'.format(tlength + 1),
'C2': '/fill ~1 ~1 ~2 ~1 ~1 ~{} minecraft:stone 0 replace'.format(tlength + 1),
'C3': '/fill ~-1 ~1 ~-2 ~-1 ~1 ~-{} minecraft:stone 0 replace'.format(tlength + 1),
'C4': '/fill ~1 ~1 ~-2 ~1 ~1 ~-{} minecraft:stone 0 replace'.format(tlength + 1),
'C5': '/fill ~0 ~0 ~2 ~0 ~0 ~{} minecraft:redstone_block 0 replace'.format(tlength + 1),
'C6': '/fill ~0 ~0 ~2 ~0 ~0 ~{} minecraft:redstone_block 0 replace'.format(tlength + 1),
'C7': '/fill ~0 ~0 ~-2 ~0 ~0 ~-{} minecraft:redstone_block 0 replace'.format(tlength + 1),
'C8': '/fill ~0 ~0 ~-2 ~0 ~0 ~-{} minecraft:redstone_block 0 replace'.format(tlength + 1),
}
# Trap template.
# tmpl[level][dl][dw]
tmpl = [[
['SF', 'SF', 'SF', 'SF', 'SF', 'SF'],
['SF', 'SF', 'LA', 'LA', 'SF', 'SF'],
['SF', 'SF', 'LA', 'LA', 'SF', 'SF'],
['SF', 'SF', 'LA', 'LA', 'SF', 'SF'],
['SF', 'SF', 'SF', 'SF', 'SF', 'SF'],
], [
['*-', 'C1', 'XX', 'XX', 'C2', '-*'],
['AR', 'RW', 'XX', 'XX', 'RW', 'AR'],
['P>', '-|', 'XX', 'XX', '|-', '<P'],
['AR', 'RW', 'XX', 'XX', 'RW', 'AR'],
['*-', 'C3', 'XX', 'XX', 'C4', '-*'],
], [
['C5', 'XX', 'XX', 'XX', 'XX', 'C6'],
['ST', 'XX', 'PP', 'PP', 'XX', 'ST'],
['RB', 'XX', 'XX', 'XX', 'XX', 'RB'],
['ST', 'XX', 'PP', 'PP', 'XX', 'ST'],
['C7', 'XX', 'XX', 'XX', 'XX', 'C8'],
]]
# Repetitions for each template row and column.
reps = {
'w': [1, 1, 1, 1, 1, 1],
'l': [1, 1, tlength, 1, 1],
}
# Starting position. Always the NW corner.
pos = self.position.down(5) - self.dw + self.dl * 2
# 1 width hallway
if self.size < 4:
tmpl = [[e[:3] for e in d] for d in tmpl]
self.apply_template(tmpl, cmds, mat, reps, pos)
class Portcullis(Blank):
_name = 'portcullis'
_min_width = 5
_min_length = 7
def render(self):
pos = self.position.down(4) \
- self.dw * 2 \
+ self.dl * (random.randint(0, max(0, self.length - 7)))
mat = {
'RW': [materials.RedstoneWire, 0],
'^^': [materials.RedstoneRepeaterOff, 1 + 12],
']]': [materials.WoodenButton, 3],
'[[': [materials.WoodenButton, 4],
'C1': [materials.CommandBlock, 0],
'C2': [materials.CommandBlock, 0],
'oo': [materials.Fence, 0],
'C3': [materials.CommandBlock, 0],
'C4': [materials.CommandBlock, 0],
'C5': [materials.CommandBlock, 0],
'C6': [materials.CommandBlock, 0],
'WA': [materials._wall, 0],
}
# Random gate material.
gate = random.choice((
(materials.Fence, 'fence'),
(materials.NetherBrickFence, 'nether_brick_fence'),
(materials.IronBars, 'iron_bars'),
(materials.CobblestoneWall, 'cobblestone_wall'),
))
mat['oo'][0] = gate[0]
cmds = {
'C1': '/playsound minecraft:block.piston.contract block @p ~ ~ ~ 10 1',
'C2': '/playsound minecraft:block.piston.extend block @p ~ ~ ~ 10 1',
'C3': '/fill ~-2 ~2 ~2 ~-2 ~2 ~{} minecraft:{} 0 replace'.format(self.size - 1, gate[1]),
'C4': '/fill ~-2 ~0 ~2 ~-2 ~0 ~{} minecraft:{} 0 replace'.format(self.size - 1, gate[1]),
'C5': '/fill ~-2 ~1 ~-2 ~-2 ~1 ~-{} minecraft:air 0 replace'.format(self.size - 1),
}
tmpl = [[
['XX', 'XX', 'RW', 'XX', 'XX', 'XX', 'RW', 'XX', 'XX'],
['XX', 'RW', 'RW', 'XX', 'XX', 'XX', 'RW', 'RW', 'XX'],
['XX', '^^', 'RW', 'XX', 'XX', 'XX', 'RW', '^^', 'XX'],
['C1', 'C3', 'C2', 'XX', 'XX', 'XX', 'C2', 'C5', 'C1'],
], [
['XX', 'XX', 'WA', ']]', 'XX', '[[', 'WA', 'XX', 'XX'],
['XX', 'XX', 'XX', 'oo', 'oo', 'oo', 'XX', 'XX', 'XX'],
['XX', 'XX', 'WA', ']]', 'XX', '[[', 'WA', 'XX', 'XX'],
['XX', 'C4', 'XX', 'XX', 'XX', 'XX', 'XX', 'C5', 'XX'],
], [
['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'],
['XX', 'XX', 'XX', 'oo', 'oo', 'oo', 'XX', 'XX', 'XX'],
['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'],
['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'],
], [
['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'],
['XX', 'XX', 'XX', 'oo', 'oo', 'oo', 'XX', 'XX', 'XX'],
['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'],
['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'],
], [
['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'],
['XX', 'XX', 'XX', 'oo', 'oo', 'oo', 'XX', 'XX', 'XX'],
['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'],
['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX'],
]]
reps = {
'w': [1, 1, 1, 1, self.size - 4, 1, 1, 1, 1],
'l': [1, 1, 1, 1],
}
if self.direction == dirs.S:
mat[']]'][1] = 1
mat['[['][1] = 2
mat['^^'][1] = 2 + 12
cmds[
'C3'] = '/fill ~2 ~2 ~-2 ~{} ~2 ~-2 minecraft:{} 0 replace'.format(self.size - 1, gate[1])
cmds[
'C4'] = '/fill ~2 ~0 ~-2 ~{} ~0 ~-2 minecraft:{} 0 replace'.format(self.size - 1, gate[1])
cmds[
'C5'] = '/fill ~-2 ~1 ~-2 ~-{} ~1 ~-2 minecraft:air 0 replace'.format(self.size - 1)
self.apply_template(tmpl, cmds, mat, reps, pos)
# Catalog the halls we know about.
_hall_traps = {}
# List of classes in this module.
for name, obj in inspect.getmembers(sys.modules[__name__], inspect.isclass):
# Only count the ones that are subclasses if of hall_traps.Blank
if issubclass(obj, Blank):
_hall_traps[obj._name] = obj
def new(name, parent, position, size, length, direction):
'''Return a new instance of the trap of a given name. Supply the parent
hall object.'''
if name in _hall_traps.keys():
return _hall_traps[name](parent, position, size, length, direction)
return Blank(parent, position, size, length, direction)