def __init__(self, world, seed): super(TerrainGeneratorSimple, self).__init__(seed) self.world = world self.seed = G.SEED #seed self.rand = random.Random(seed) perm = range(255) self.rand.shuffle(perm) self.noise = SimplexNoise(permutation_table=perm).noise2 #self.noise = PerlinNoise(seed).noise self.PERSISTENCE = 2.1379201 #AKA lacunarity self.H = 0.836281 self.biome_generator = BiomeGenerator(seed) #Fun things to adjust self.OCTAVES = 9 #Higher linearly increases calc time; increases apparent 'randomness' self.height_range = 32 #If you raise this, you should shrink zoom_level equally self.height_base = 32 #The lowest point the perlin terrain will generate (below is "underground") self.island_shore = 38 #below this is sand, above is grass .. island only self.water_level = 36 # have water 2 block higher than base, allowing for some rivers... self.zoom_level = 0.002 #Smaller will create gentler, softer transitions. Larger is more mountainy # ores avaliable on the lowest level, closet to bedrock self.lowlevel_ores = ((B.stone_block, ) * 75 + (B.diamondore_block, ) * 2 + (B.sapphireore_block, ) * 2) # ores in the 'mid-level' .. also, the common ore blocks self.midlevel_ores = ((B.stone_block, ) * 80 + (B.rubyore_block, ) * 2 + (B.coalore_block, ) * 4 + (B.gravel_block, ) * 5 + (B.ironore_block, ) * 5 + (B.lapisore_block, ) * 2) # ores closest to the top level dirt and ground self.highlevel_ores = ((B.stone_block, ) * 85 + (B.gravel_block, ) * 5 + (B.coalore_block, ) * 3 + (B.quartz_block, ) * 5) self.underwater_blocks = ((B.sand_block, ) * 70 + (B.gravel_block, ) * 20 + (B.clay_block, ) * 10) #self.world_type_trees = (N.oak_tree, N.birch_tree, N.water_melon, N.pumpkin, N.y_flowers, N.potato, N.carrot, N.rose) self.world_type_trees = (N.oak_tree, N.birch_tree, N.jungle_tree) self.world_type_plants = (N.pumpkin, N.potato, N.carrot, N.water_melon) self.world_type_grass = (N.y_flowers, N.tall_grass, N.rose, N.tall_grass0, N.tall_grass1, N.cactus, N.tall_grass2, N.tall_cactus, N.tall_grass3, N.tall_grass4, N.tall_grass5, N.tall_grass6, N.tall_grass7, N.dead_bush, N.desert_grass) #This is a list of blocks that may leak over from adjacent sectors and whose presence doesn't mean the sector is generated self.autogenerated_blocks = N.VEGETATION_BLOCKS self.nether = (B.nether_block, B.soulsand_block, B.netherore_block, B.air_block) #self.nether = ((B.nether_block,) * 80 + (B.soulsand_block,) * 15 + (B.netherore_block,) * 5 + (B.air_block,) * 10) self.weights = [ self.PERSISTENCE**(-self.H * n) for n in xrange(self.OCTAVES) ]
def show_map(self): print("map called...") # taken from Nebual's biome_explorer, this is ment to be a full screen map that uses mini tiles to make a full 2d map. with open(os.path.join(G.game_dir, "world", "seed"), "rb") as f: SEED = f.read() b = BiomeGenerator(SEED) x, y, z = self.player.position curx = x cury = y xsize = 79 ysize = 28 pbatch = pyglet.graphics.Batch() pgroup = pyglet.graphics.OrderedGroup(1) DESERT, PLAINS, MOUNTAINS, SNOW, FOREST = range(5) letters = ["D", "P", "M", "S", "F"] # temp background pic... image = load_image('resources', 'textures', 'main_menu_background.png') #map_frame = image_sprite(image, pbatch, 0, y=G.WINDOW_WIDTH, height=G.WINDOW_HEIGHT) #sprite = pyglet.sprite.Sprite(image) #sprite.image(image) #sprite.visible = True # map_frame.draw() # map_frame.visible = True for y in range(int(cury), int(cury + ysize)): for x in range(int(curx), int(curx + xsize)): #string += letters[b.get_biome_type(x,y)] tmap = letters[b.get_biome_type(x, y)] tile_map = load_image('resources', 'textures', tmap + '.png') tile_map.anchor_x = x * 8 tile_map.anchor_Y = y * 8 sprite = pyglet.sprite.Sprite(tile_map, x=x * 8, y=y * 8, batch=pbatch) game_map = image_sprite(tile_map, pbatch, pgroup, x * 8, y * 8, 8, 8) game_map = pyglet.sprite.Sprite(image, x=G.WINDOW_WIDTH, y=G.WINDOW_HEIGHT, batch=pbatch, group=pgroup) game_map = pyglet.sprite.Sprite(tile_map, x=x * 8, y=y * 8, batch=pbatch, group=pgroup) tile_map.blit(x * 8, y * 8) #tile_map.draw() #map.append(tmap) game_map.draw() pbatch.draw()
def __init__(self, seed): super(TerrainGenerator, self).__init__(seed) self.base_gen = PerlinNoise(seed) self.base_gen.octave = 8 self.ocean_gen = PerlinNoise(seed + 11) self.ocean_gen.octave = 8 self.river_gen = PerlinNoise(seed + 31) self.river_gen.octave = 8 self.mount_gen = PerlinNoise(seed + 41) self.hill_gen = PerlinNoise(seed + 71) self.cave_gen = PerlinNoise(seed + 141) self.biome_gen = BiomeGenerator(seed)
def set_seed(self, seed): self.base_gen = PerlinNoise(seed) self.base_gen.octave = 8 self.ocean_gen = PerlinNoise(seed + 11) self.ocean_gen.octave = 8 self.river_gen = PerlinNoise(seed + 31) self.river_gen.octave = 8 self.mount_gen = PerlinNoise(seed + 41) self.hill_gen = PerlinNoise(seed + 71) self.cave_gen = PerlinNoise(seed + 141) self.biome_gen = BiomeGenerator(seed) self.seed = seed
def dequeue_packet(self): with self.lock: packetid, packet = self.world.sector_packets.popleft() if packetid == 1: # Entire Sector blocks, sectors = self.world, self.world.sectors secpos = struct.unpack("iii", packet[:12]) sector = sectors[secpos] cx, cy, cz = sector_to_blockpos(secpos) fpos = 12 exposed_pos = fpos + 1024 for x in range(cx, cx + 8): for y in range(cy, cy + 8): for z in range(cz, cz + 8): read = packet[fpos:fpos + 2] fpos += 2 unpacked = structuchar2.unpack(read) if read != null2 and unpacked in BLOCKS_DIR: position = x, y, z try: blocks[position] = BLOCKS_DIR[unpacked] if blocks[position].sub_id_as_metadata: blocks[position] = type( BLOCKS_DIR[unpacked])() blocks[position].set_metadata(0) except KeyError: main_blk = BLOCKS_DIR[(unpacked[0], 0)] if main_blk.sub_id_as_metadata: # sub id is metadata blocks[position] = type(main_blk)() blocks[position].set_metadata(unpacked[-1]) sector.append(position) if packet[exposed_pos:exposed_pos + 1] == b"1": blocks.show_block(position) exposed_pos += 1 if secpos in self.world.sector_queue: del self.world.sector_queue[ secpos] #Delete any hide sector orders elif packetid == 2: # Blank Sector self.world.sectors[struct.unpack("iii", packet)] = [] elif packetid == 3: # Add Block self.world._add_block(struct.unpack("iii", packet[:12]), BLOCKS_DIR[struct.unpack("BB", packet[12:])]) elif packetid == 4: # Remove Block self.world._remove_block(struct.unpack("iii", packet)) elif packetid == 5: # Chat Print self.controller.write_line(packet[:-4].decode('utf-8'), color=struct.unpack( "BBBB", packet[-4:])) if not self.controller.text_input.visible: self.controller.chat_box.visible = True pyglet.clock.unschedule(self.controller.hide_chat_box) pyglet.clock.schedule_once(self.controller.hide_chat_box, G.CHAT_FADE_TIME) elif packetid == 6: # Inventory player = self.controller.player caret = 0 for inventory in (player.quick_slots.slots, player.inventory.slots, player.armor.slots): for i in range(len(inventory)): id_main, id_sub, amount = struct.unpack( "HBB", packet[caret:caret + 4]) caret += 4 if id_main == 0: continue durability = -1 if id_main >= G.ITEM_ID_MIN and ( id_main, id_sub) not in G.ITEMS_DIR: #The subid must be durability durability = id_sub * G.ITEMS_DIR[ (id_main, 0)].durability // 255 id_sub = 0 inventory[i] = ItemStack(type=BlockID(id_main, id_sub), amount=amount, durability=durability) self.controller.item_list.update_items() self.controller.inventory_list.update_items() elif packetid == 7: # New player connected plyid, name = struct.unpack( "H", packet[:2])[0], packet[2:].decode('utf-8') if plyid not in self.controller.player_ids: self.controller.player_ids[plyid] = Player(username=name, local_player=False) elif name == '\0': del self.controller.player_ids[plyid] elif packetid == 8: # Player Movement ply = self.controller.player_ids[struct.unpack("H", packet[:2])[0]] ply.momentum = struct.unpack("fff", packet[2:14]) ply.position = struct.unpack("ddd", packet[14:]) elif packetid == 9: # Player Jump self.controller.player_ids[struct.unpack("H", packet)[0]].dy = 0.016 elif packetid == 10: # Update Tile Entity self.world[struct.unpack("iii", packet[:12])].update_tile_entity( packet[12:]) elif packetid == 255: # Spawn Position self.controller.player.position = struct.unpack("iii", packet[:12]) packet = packet[12:] packet, seed = extract_string_packet(packet) self.world.biome_generator = BiomeGenerator(seed) #Now that we know where the player should be, we can enable .update again self.controller.update = self.controller.update_disabled else: warn( "Received unknown packetid %s, there's probably a version mismatch between client and server!" % packetid)
def dequeue_packet(self): with self.lock: packetid, packet = self.world.sector_packets.popleft() # print(f"CLIENTPACKET_ID: ", packetid) if packetid != 1 else None # print(f"CLIENTPACKET_PAK:", packet) if packetid != 1 else None if packetid == 1: # Entire Sector blocks, sectors = self.world, self.world.sectors # secpos = struct.unpack("iii", packet[:12]) secpos = packet["sector"] # print(packet) sector = sectors[secpos] cx, cy, cz = sector_to_blockpos(secpos) fpos = 0 # 12 exposed_pos = 0 # fpos + 1024 for x in range(cx, cx + 8): for y in range(cy, cy + 8): for z in range(cz, cz + 8): sector_data = packet["sectorData"][ fpos:fpos + 2] # .encode("ascii") # print(read) fpos += 2 unpacked = structuchar2.unpack(sector_data) # unpacked = read # print("UNPACKED:", unpacked) if sector_data != null2 and unpacked in BLOCKS_DIR: position = x, y, z try: blocks[position] = BLOCKS_DIR[unpacked] if blocks[position].sub_id_as_metadata: blocks[position] = type( BLOCKS_DIR[unpacked])() blocks[position].set_metadata(0) except KeyError: main_blk = BLOCKS_DIR[(unpacked[0], 0)] if main_blk.sub_id_as_metadata: # sub id is metadata blocks[position] = type(main_blk)() blocks[position].set_metadata(unpacked[-1]) sector.append(position) if packet["exposedSector"][exposed_pos] is "1": blocks.show_block(position) exposed_pos += 1 if secpos in self.world.sector_queue: del self.world.sector_queue[ secpos] # Delete any hide sector orders elif packetid == 2: # Blank Sector self.world.sectors[packet["sector"]] = [] elif packetid == 3: # Add Block self.world._add_block(packet["position"], BLOCKS_DIR[packet["block"]]) elif packetid == 4: # Remove Block self.world._remove_block(packet["position"]) elif packetid == 5: # Chat Print self.controller.write_line(packet["message"].decode('utf-8'), color=packet["color"]) if not self.controller.text_input.visible: self.controller.chat_box.visible = True pyglet.clock.unschedule(self.controller.hide_chat_box) pyglet.clock.schedule_once(self.controller.hide_chat_box, G.CHAT_FADE_TIME) elif packetid == 6: # Inventory player = self.controller.player caret = 0 print("CLIENT_PACKET06:", packet) for i in range(len(player.quick_slots.slots)): id_main, id_sub, amount = packet["items"]["quickSlots"][i] # print(id_main, id_sub, amount) # caret += 4 if id_main == 0: continue durability = -1 if id_main >= G.ITEM_ID_MIN and (id_main, id_sub) not in G.ITEMS_DIR: # The subid must be durability durability = id_sub * G.ITEMS_DIR[BlockID( id_main, 0)].durability / 255 id_sub = 0 player.quick_slots.slots[i] = ItemStack(type=BlockID( id_main, id_sub), amount=amount, durability=durability) for i in range(len(player.inventory.slots)): id_main, id_sub, amount = packet["items"]["inventory"][i] # print(id_main, id_sub, amount) # caret += 4 if id_main == 0: continue durability = -1 if id_main >= G.ITEM_ID_MIN and (id_main, id_sub) not in G.ITEMS_DIR: # The subid must be durability durability = id_sub * G.ITEMS_DIR[BlockID( id_main, 0)].durability / 255 id_sub = 0 player.quick_slots.slots[i] = ItemStack(type=BlockID( id_main, id_sub), amount=amount, durability=durability) for i in range(len(player.armor.slots)): id_main, id_sub, amount = packet["items"]["armor"][i] # print(id_main, id_sub, amount) # caret += 4 if id_main == 0: continue durability = -1 if id_main >= G.ITEM_ID_MIN and (id_main, id_sub) not in G.ITEMS_DIR: # The subid must be durability durability = id_sub * G.ITEMS_DIR[BlockID( id_main, 0)].durability / 255 id_sub = 0 player.quick_slots.slots[i] = ItemStack(type=BlockID( id_main, id_sub), amount=amount, durability=durability) # for inventory in (player.quick_slots.slots, player.inventory.slots, player.armor.slots): # # for item in (player.quick_slots.slots + player.inventory.slots + player.armor.slots) # for i in range(len(inventory)): # # print(packet) # # # print("PACKET_0x06:", packet.decode()) # # # packet = eval(packet.decode()[:-1]) if packet.decode().startswith( # # "b'") and packet.decode().endswith("'" + chr(packet[-1])) else packet # id_main, id_sub, amount = packet[] # # # print(id_main, id_sub, amount) # caret += 4 # if id_main == 0: continue # durability = -1 # if id_main >= G.ITEM_ID_MIN and (id_main, id_sub) not in G.ITEMS_DIR: # # The subid must be durability # durability = id_sub * G.ITEMS_DIR[BlockID(id_main, 0)].durability / 255 # id_sub = 0 # inventory[i] = ItemStack(type=BlockID(id_main, id_sub), amount=amount, durability=durability) self.controller.item_list.update_items() self.controller.inventory_list.update_items() elif packetid == 7: # New player connected # plyid, name = struct.unpack("H", packet[:2])[0], packet[2:].decode('utf-8') plyid = packet["playerID"] name = packet["username"] if plyid not in self.controller.player_ids: self.controller.player_ids[plyid] = Player(username=name, local_player=False) elif name == '\0': del self.controller.player_ids[plyid] elif packetid == 8: # Player Movement ply = self.controller.player_ids[ packet["playerID"]] # struct.unpack("H", packet[:2])[0]] ply.momentum = packet[ "momentum"] # struct.unpack("fff", packet[2:14]) ply.position = packet[ "position"] # struct.unpack("ddd", packet[14:]) elif packetid == 9: # Player Jump self.controller.player_ids[packet["playerID"]].dy = 0.016 elif packetid == 10: # Update Tile Entity self.world[packet["position"]].update_tile_entity(packet["value"]) elif packetid == 255: # Spawn Position self.controller.player.position = packet[ "position"] # struct.unpack("iii", packet[:12]) # print(packet) # packet = packet[12:] # TODO: Get the packet data from dictionary. PACKETID: 255 | CLIENT # packet, seed = extract_string_packet(packet) seed = packet["seed"] self.world.biome_generator = BiomeGenerator(seed) # Now that we know where the player should be, we can enable .update again self.controller.update = self.controller.update_disabled else: warn( "Received unknown packetid %s, there's probably a version mismatch between client and server!" % packetid)