def build_gradient_line(protocol, colors, points): sp = cbc.ServerPlayer() block_action = BlockAction() block_action.player_id = sp.player_id block_action.value = BUILD_BLOCK set_color = SetColor() set_color.player_id = sp.player_id color_range = list(zip(*colors)) lp = len(points) - 1 map_ = protocol.map for i in range(len(points)): pct = 1 - (i + 0.0) / lp, (i + 0.0) / lp if lp else (1, 0) color = tuple( int(round(sum(c * p for c, p in zip(crng, pct)))) for crng in color_range) map_.set_point(*points[i], color=color) set_color.value = make_color(*color) protocol.send_contained(set_color, save=True) block_action.x, block_action.y, block_action.z = points[i] protocol.send_contained(block_action, save=True)
def clear_solid_generator(protocol, x1, y1, z1, x2, y2, z2, god=False, destroy=True): block_action = BlockAction() block_action.value = DESTROY_BLOCK splayer = cbc.ServerPlayer() block_action.player_id = splayer.player_id map = protocol.map check_protected = hasattr(protocol, 'protected') x1, x2 = sorted((x1, x2)) y1, y2 = sorted((y1, y2)) z1, z2 = sorted((z1, z2)) clear = map.destroy_point if destroy else map.remove_point get_solid = map.get_solid for x, y, z in product(xrange(x1, x2 + 1), xrange(y1, y2 + 1), xrange(z1, z2 + 1)): packets = 0 if get_solid(x, y, z) and ( god or not (check_protected and protocol.is_protected(x, y, z) ) #not protected and not (protocol.god_blocks is not None and (x, y, z) in protocol.god_blocks)): #not a god block block_action.x = x block_action.y = y block_action.z = z protocol.send_contained(block_action, save=True) clear(x, y, z) packets = 1 yield packets, 0
def quickbuild_generator(self, origin, structure, default_color): map = self.protocol.map protocol = self.protocol splayer = cbc.ServerPlayer() block_action = BlockAction() block_action.value = BUILD_BLOCK block_action.player_id = splayer.player_id set_color = SetColor() set_color.value = make_color(*default_color) set_color.player_id = splayer.player_id pcolor = default_color protocol.send_contained(set_color, save=True) if not isinstance(structure, dict): structure = dict(structure) for xyz, color in structure.iteritems(): x, y, z = [a + b for a, b in zip(xyz, origin)] if (x < 0 or x >= 512 or y < 0 or y >= 512 or z < 0 or z >= 62): continue if map.get_solid(x, y, z): continue color = color or default_color if color != pcolor: set_color.value = make_color(*color) protocol.send_contained(set_color, save=True) pcolor = color yield 1, 0 self.on_block_build(x, y, z) block_action.x, block_action.y, block_action.z = x, y, z protocol.send_contained(block_action, save=True) map.set_point(x, y, z, pcolor) yield 1, 0
def build_filled_generator(protocol, x1, y1, z1, x2, y2, z2, color, god=False, god_build=False): # create a player instance, freed when the generator is done # other scripts that also use ServerPlayer won't get the same id! # this won't be necessary in 1.0 splayer = cbc.ServerPlayer() line = BlockLine() line.player_id = splayer.player_id set_color = SetColor() set_color.value = make_color(*color) set_color.player_id = splayer.player_id protocol.send_contained(set_color, save=True) packets = 1 check_protected = hasattr(protocol, 'protected') if god_build and protocol.god_blocks is None: protocol.god_blocks = set() map = protocol.map ranges = [ xrange(min(x1, x2), max(x1, x2) + 1), xrange(min(y1, y2), max(y1, y2) + 1), xrange(min(z1, z2), max(z1, z2) + 1) ] order = zip(*sorted(zip([len(x) for x in ranges], [0, 1, 2])))[1] # set the first block position prod = ordered_product(ranges, order) line.x1, line.y1, line.z1 = prod.next() line.x2 = line.x1 line.y2 = line.y1 line.z2 = line.z1 map.set_point(line.x1, line.y1, line.z1, color) for x, y, z in prod: packets = 0 if not god and check_protected and protocol.is_protected(x, y, z): continue if god_build: protocol.god_blocks.add((x, y, z)) changed = (line.x1 != x or line.x2 != x) + ( line.y1 != y or line.y2 != y) + (line.z1 != z or line.z2 != z) dist = abs(line.x1 - x) + abs(line.y1 - y) + abs(line.z1 - z) if changed > 1 or dist >= MAX_LINE_BLOCKS: protocol.send_contained(line, save=True) packets += 2 line.x1 = x line.y1 = y line.z1 = z line.x2 = x line.y2 = y line.z2 = z map.set_point(x, y, z, color) yield packets, 0 protocol.send_contained(line, save=True) yield 1, 0