class BlocksTests(TestCase): def setUp(self): self.blocks = Blocks() def test_get_set(self): self.blocks.set(box) self.assertEqual(self.blocks.get(), box) def test_get_set_with_moving(self): self.blocks.set(box_drop_after) self.assertEqual(self.blocks.get(), box_drop_after) def test_insert_new(self): self.blocks.set(box) self.blocks.insert_block("AE") self.assertEqual(self.blocks.get(), box_new_block) def test_insert_diamond(self): self.blocks.set(box) self.blocks.insert_diamond(1) self.assertEqual(self.blocks.get(), box_new_diamond) def test_drop(self): self.blocks.set(box_drop_before) self.blocks.drop() self.assertEqual(self.blocks.get(), box_drop_after) def test_drop_partial(self): self.blocks.set(box_drop_independent_before) self.blocks.drop() self.assertEqual(self.blocks.get(), box_drop_independent_after) def test_drop_result(self): self.blocks.set(box_drop_before) r = self.blocks.drop() self.assertEqual(r,[(3,0,'a',DOWN),(4,0,'e',DOWN)]) def test_settle(self): self.blocks.set(box_settle_before) self.blocks.drop() self.assertEqual(self.blocks.get(), box_settle_after) def test_leftshift(self): self.blocks.set(box_shift_before) result = self.blocks.left_shift() self.assertEqual(self.blocks.get(), box_leftshift) self.assertTrue(result) def test_rightshift(self): self.blocks.set(box_shift_before) result = self.blocks.right_shift() self.assertEqual(self.blocks.get(), box_rightshift) self.assertTrue(result) def test_leftshift_return(self): self.blocks.set(box_shift_before) result = self.blocks.left_shift() self.assertEqual(result,[(3,1,'a',LEFT),(4,1,'e',LEFT)]) def test_rightshift_return(self): self.blocks.set(box_shift_before) result = self.blocks.right_shift() self.assertEqual(result,[(3,1,'a',RIGHT),(4,1,'e',RIGHT)]) def test_cannot_shift_without_moving(self): self.blocks.set(box) result = self.blocks.left_shift() self.assertFalse(result) self.assertEqual(self.blocks.get(), box) result = self.blocks.right_shift() self.assertFalse(result) self.assertEqual(self.blocks.get(), box) def test_cannot_leftshift(self): self.blocks.set(box_cannot_leftshift) result = self.blocks.left_shift() self.assertEqual(self.blocks.get(), box_cannot_leftshift) self.assertFalse(result) def test_cannot_rightshift(self): self.blocks.set(box_cannot_rightshift1) result = self.blocks.right_shift() self.assertEqual(self.blocks.get(), box_cannot_rightshift1) self.assertFalse(result) self.blocks.set(box_cannot_rightshift2) result = self.blocks.right_shift() self.assertFalse(result) self.assertEqual(self.blocks.get(), box_cannot_rightshift2) def test_rotate(self): self.blocks.set(box_rotate_1) for after in [box_rotate_2, box_rotate_3, box_rotate_4]: self.blocks.rotate() self.assertEqual(self.blocks.get(), after) def test_rotate_return(self): self.blocks.set(box_rotate_1) result = self.blocks.rotate() self.assertEqual(result, [(4,1,'e',DOWNLEFT)]) def test_cannot_rotate(self): self.blocks.set(box_cannot_rotate) self.blocks.rotate() self.assertEqual(self.blocks.get(), box_cannot_rotate) def test_remove_single(self): self.blocks.set(box_remove_single_before) self.blocks.remove_multiplets() self.assertEqual(self.blocks.get(), box_remove_single_after) def test_remove_multi(self): self.blocks.set(box_remove_multi_before) self.blocks.remove_multiplets() self.assertEqual(self.blocks.get(), box_remove_multi_after) def test_drop_bricks(self): self.blocks.set(box_drop_bricks_before) result = self.blocks.drop_bricks() expected = [(2,2,'a',DOWN*2),(4,3,'b',DOWN*1), (5,1,'e',DOWN*4),(5,0,'e',DOWN*4), (6,4,'c',DOWN*1),(6,3,'d',DOWN*1)] self.assertEqual(self.blocks.get(), box_drop_bricks_after) for r,e in zip(result,expected): self.assertEqual(r[0],e[0]) self.assertEqual(r[1],e[1]) self.assertEqual(r[2],e[2]) self.assertEqual(sum(r[3]==e[3]),2) def test_remove_and_drop(self): self.blocks.set(box_drop_remove_before) self.blocks.remove_multiplets() self.blocks.drop_bricks() self.assertEqual(self.blocks.get(), box_drop_remove_after) def test_remove_consecutive(self): self.blocks.set(box_remove_consecutive_before) self.blocks.remove_consecutive() self.assertEqual(self.blocks.get(), box_remove_consecutive_after) def test_box_overflow(self): self.blocks.set(box_game_over) self.assertTrue(self.blocks.box_overflow()) self.blocks.set(box_not_game_over) self.assertFalse(self.blocks.box_overflow()) def test_get_stack_size(self): """returns height of the pile""" self.blocks.set(box) self.assertEqual(self.blocks.get_stack_size(),5) self.blocks.set(box_drop_after) self.assertEqual(self.blocks.get_stack_size(),5) self.blocks.set(box_remove_multi_after) self.assertEqual(self.blocks.get_stack_size(),3)
def schem2bp(fileName, opt_expand = False): # Load NBT nbtfile = nbt.NBTFile(fileName, 'rb') # Compute values height = nbtfile['Height'].value length = nbtfile['Length'].value width = nbtfile['Width'].value b = Blocks(width, height, length) b.datas = nbtfile['Data'].value b.ids = nbtfile['Blocks'].value # Expand if opt_expand: expand(b, 2) # The ship core will be the bedrock block (id 7) b.set_orig_on_blockid(7) pos = b.index_to_pos(len(b.datas)-1) print "Last block : x=%2d, y=%2d, z=%2d" % pos datas = {} blocks_counter = {} for i in xrange(0, len(b.datas)): pos = b.index_to_pos(i) id, data = b.get(pos) if id: chunk_pos = ( pos[0] / 16 * 16, pos[1] / 16 * 16, pos[2] / 16 * 16 ) data_pos = ( (chunk_pos[0] + 128) / 255 , (chunk_pos[1] + 128) / 255 , (chunk_pos[2] + 128) / 255 ) if not data_pos in datas: print "New data (%2d, %2d, %2d)" % data_pos datas[data_pos] = {} if not chunk_pos in datas[data_pos]: print "New chunk (%2d, %2d, %2d)" % chunk_pos datas[data_pos][chunk_pos] = {} x_off = chunk_pos[0] y_off = chunk_pos[1] z_off = chunk_pos[2] id, hp, active, orient = resolver.minecraft_to_starmade( id, data ) block_pos = (pos[0] - x_off, pos[1] - y_off, pos[2] - z_off) datas[data_pos][chunk_pos][block_pos] = { 'id': id, 'hp': hp, 'active': active, 'orient': orient } if not id in blocks_counter: blocks_counter[id]=1 else: blocks_counter[id]+=1 # Create blueprint basename = os.path.basename(fileName) bpname = os.path.splitext(basename)[0] bp = { 'datas': {}, 'header': { 'blocks': blocks_counter, 'bounds_a': ( b.orig[0] - 1.0, b.orig[1] - 1.0, b.orig[2] - 1.0), 'bounds_b': ( b.orig[0] + b.width + 0.0, b.orig[1] + b.height + 0.0, b.orig[2] + b.length + 0.0), 'int_a': 0, 'int_b': 0}, 'logic': {'controllers': [], 'int_a': 0}, 'meta': {'byte_a': 1, 'int_a': 0} } for data in datas: data_filename = "ENTITY_SHIP_%s.%d.%d.%d.smd2" % ((bpname, ) + data) bp_d = bp['datas'][data_filename] = { 'chunk_index': {}, 'chunk_timestamps': {}, 'chunks': [], 'filelen': 0, 'int_a': 0, 'pos': data } ci = 0 for chunk in datas[data]: pos_index = blueprint.indexed_pos( chunk, data ) bp_d['chunk_index'][pos_index] = { 'id' : ci, 'len' : 0 } bp_d['chunk_timestamps'][pos_index] = int(time.time()* 1000) bp_d['chunks'].append({ 'blocks': datas[data][chunk], 'pos': chunk, 'timestamp': int(time.time()* 1000), 'type': 1 }) ci += 1 blueprint.writeBlueprint(bpname, bp)