Aigo 77 Posted November 16, 2024 Share Posted November 16, 2024 Hi everyone! I wanted to share a small Python script to help generate a list of the items that are currently being dropped by monsters. It's not perfect, but it works for what I needed and might be useful for others I know the code is a bit rough around the edges, and I’d really appreciate any improvements or suggestions to make it better. Even though it’s a simple tool, it can be functional as it is for now. Thank you all for the amazing work you do in the community – I truly appreciate it! How the Code Works: Spoiler Loading Data: The script starts by loading the VNUMs and names from item_names.txt. Then, it reads the contents of mob_drop_item.txt and common_drop_item.txt to find the VNUMs of droppable items. Matching VNUMs: For each VNUM from the item_names.txt, the script searches for that VNUM in the drop files. Generating Output: If a VNUM from item_names.txt appears in either of the drop files, it adds that item to a new file, items_drop_list.txt. The output file contains the VNUMs and names of all droppable items found in monsters. What You Get: The script will create a new text file called items_drop_list.txt with a list of droppable items in the format: VNUM ITEM_NAME 123 item1 456 item2 789 item3 How to Use: Drag and drop the three files (item_names.txt, mob_drop_item.txt, and common_drop_item.txt) onto the Python script. The script will process them and generate a new file called items_drop_list.txt in the same directory. CODE: Spoiler import sys import os import re # Function to read the item_names.txt file and return a dictionary with vnum as the key and name as the value def load_item_names(file_name): items = {} try: with open(file_name, 'r') as file: print(f"Opening file {file_name}...") # Skip the first line (headers) next(file) for line in file: parts = line.strip().split('\t') if len(parts) >= 2: try: vnum = parts[0] # Keep the vnum as a string initially name = parts[1] # If the vnum is a range like '110000~110099', convert it to a set of vnums if '~' in vnum: start, end = vnum.split('~') vnum_range = range(int(start), int(end) + 1) for v in vnum_range: items[v] = name else: vnum = int(vnum) # Convert vnum to an integer if valid items[vnum] = name except ValueError: print(f"Warning: Could not convert vnum '{parts[0]}' to an integer. It will be skipped.") print(f"{len(items)} items have been loaded from {file_name}.") except Exception as e: print(f"Error reading {file_name}: {e}") return items # Function to read a text file (like mob_drop_item.txt or common_drop_item.txt) and return all vnums found def load_droppable_vnums(file_name, item_names): droppable_vnums = set() try: with open(file_name, 'r') as file: print(f"Opening file {file_name}...") content = file.read() # Read the entire content as text print(f"Content of {file_name} loaded.") # Search for each vnum from item_names in the content of mob_drop_item.txt or common_drop_item.txt using regular expressions for vnum in item_names: vnum_str = str(vnum) # Convert vnum to string # Use the regular expression \b to ensure it's a whole word if re.search(rf'\b{vnum_str}\b', content): droppable_vnums.add(vnum) print(f"{len(droppable_vnums)} droppable vnums found in {file_name}.") except Exception as e: print(f"Error reading {file_name}: {e}") return droppable_vnums # Main function to combine the information and create the items_conseguibles.txt file def generate_droppable_items(item_names, mob_drop_items, common_drop_items, output_file_name): try: with open(output_file_name, 'w') as output_file: # Write the header of the output file output_file.write("VNUM\tLOCALE_NAME\n") # Use a set to avoid duplicates print("Writing to the output file...") # Combine all the droppable vnums from mob_drop_items and common_drop_items total_vnums = mob_drop_items.union(common_drop_items) for vnum in item_names: if vnum in total_vnums: output_file.write(f"{vnum}\t{item_names[vnum]}\n") print(f"The file {output_file_name} has been successfully generated.") except Exception as e: print(f"Error generating the file {output_file_name}: {e}") # Check if the script was executed with files dragged as arguments if len(sys.argv) != 4: print("Please drag the files item_names.txt, mob_drop_item.txt, and common_drop_item.txt onto the script.") input("Press Enter to exit...") # Keep the console open sys.exit(1) # Get the file paths from the arguments item_names_file = sys.argv[1] mob_drop_item_file = sys.argv[2] common_drop_item_file = sys.argv[3] # Check if the files exist if not os.path.exists(item_names_file) or not os.path.exists(mob_drop_item_file) or not os.path.exists(common_drop_item_file): print("One or more files do not exist. Please make sure you dragged the correct files.") input("Press Enter to exit...") # Keep the console open sys.exit(1) # Load the data print("Loading the data...") item_names = load_item_names(item_names_file) droppable_vnums_mob = load_droppable_vnums(mob_drop_item_file, item_names) droppable_vnums_common = load_droppable_vnums(common_drop_item_file, item_names) # Generate the file with the droppable items output_file = 'items_drop_list.txt' generate_droppable_items(item_names, droppable_vnums_mob, droppable_vnums_common, output_file) # Wait for user input so the console doesn't close immediately input("Press Enter to exit...") # Keep the console open 1 Link to comment Share on other sites More sharing options...
Recommended Posts