任务物品获得难度补全
很多PVF文件,在写获得任务物品时,总是丢掉最高难度。一个个的修改又太麻烦了,所以: import os def process_quest_file(file_path): “”” 处理单个任务文件,修改[monster reward item]区域的内容 返回: (status, message) status: `processed`表示已处理, `skipped`表示跳过, `error`表示错误 “”” try: with open(file_path, `r`, encoding=`utf-8`) as f: content = f.read() except Exception as e: return (`error`, f`读取文件失败: {str(e)}`) lines = content.split(`n`) # 找到monster reward item区域 start_idx = -1 end_idx = -1 for i, line in enumerate(lines): if line.strip() == `[monster reward item]`: start_idx = i elif line.strip() == `[/monster reward item]`: end_idx = i break # 如果没有找到该区域,跳过 if start_idx == -1 or end_idx == -1: return (`skipped`, `无[monster reward item]字段`) # 第一遍遍历:检查是否有难度为-1的行 for i in range(start_idx + 1, end_idx): line = lines[i].strip() if not line: continue parts = line.split(`t`) parts = [p for p in parts if p] if len(parts) == 7: difficulty = parts[2] if difficulty == `-1`: return (`skipped`, `难度为-1,无需处理`) # 第二遍遍历:提取所有数据,并按(列1,列2,列4,列5,列6,列7)分组 grouped_data = {} for i in range(start_idx + 1, end_idx): line = lines[i].strip() if not line: continue parts = line.split(`t`) parts = [p for p in parts if p] if len(parts) == 7: col1, col2, col3, col4, col5, col6, col7 = parts key = (col1, col2, col4, col5, col6, col7) difficulty = int(col3) if key not in grouped_data: grouped_data[key] = {} grouped_data[key][difficulty] = (col1, col2, col3, col4, col5, col6, col7) if not grouped_data: return (`skipped`, `无有效数据`) # 找到所有难度的最小值 all_difficulties = set() for diff_data in grouped_data.values(): all_difficulties.update(diff_data.keys()) min_difficulty = min(all_difficulties) MAX_DIFFICULTY = 4 # 获取所有的唯一基础组合(按列1,列2,列4分组) unique_combinations_by_base = {} for key in grouped_data.keys(): col1, col2, col4, col5, col6, col7 = key base_key = (col1, col2, col4) if base_key not in unique_combinations_by_base: unique_combinations_by_base[base_key] = set() unique_combinations_by_base[base_key].add((col5, col6, col7)) # 生成新的reward行 new_reward_lines = [] for difficulty in range(min_difficulty, MAX_DIFFICULTY + 1): for base_key in unique_combinations_by_base.keys(): col1, col2, col4 = base_key values = unique_combinations_by_base[base_key] # 查找当前难度的值 best_value = None for col5, col6, col7 in values: key = (col1, col2, col4, col5, col6, col7) if key in grouped_data and difficulty in grouped_data[key]: best_value = (col5, col6, col7) break # 如果没有找到,查找上一难度的值(就近原则) if best_value is None: for prev_diff in range(difficulty – 1, min_difficulty – 1, -1): for col5, col6, col7 in values: key = (col1, col2, col4, col5, col6, col7) if key in grouped_data and prev_diff in grouped_data[key]: best_value = (col5, col6, col7) break if best_value: break # 使用最小难度的值作为默认值 if best_value is None: for col5, col6, col7 in values: key = (col1, col2, col4, col5, col6, col7) if key in grouped_data: best_value = (col5, col6, col7) break if best_value: col5, col6, col7 = best_value line_str = f`t{col1}t{col2}t{difficulty}t{col4}t{col5}t{col6}t{col7}` new_reward_lines.append(line_str) # 重新构建文件内容 new_lines = lines[:start_idx + 1] new_lines.extend(new_reward_lines) new_lines.append(`[/monster reward item]`) new_lines.extend(lines[end_idx + 1:]) # 保存文件 new_content = `n`.join(new_lines) with open(file_path, `w`, encoding=`utf-8`) as f: f.write(new_content) return (`processed`, `处理成功`) def process_all_quest_files(root_folder): “”” 遍历root_folder及其子文件夹,处理所有包含[monster reward item]的文件 “”” processed_count = 0 skipped_count = 0 error_count = 0 print(f”开始处理文件夹: {root_folder}”) print(“-” * 60) for dirpath, dirnames, filenames in os.walk(root_folder): for filename in filenames: file_path = os.path.join(dirpath, filename) # 先检查文件是否包含[monster reward item] try: with open(file_path, `r`, encoding=`utf-8`) as f: content = f.read() if `[monster reward item]` not in content: skipped_count += 1 continue except: skipped_count += 1 continue # 处理文件 status, message = process_quest_file(file_path) if status == `processed`: processed_count += 1 print(f”[已处理] {file_path}”) elif status == `skipped`: skipped_count += 1 print(f”[跳过] {file_path} – {message}”) else: error_count += 1 print(f”[错误] {file_path} – {message}”) print(“-” * 60) print(f”处理完成!”) print(f”已处理文件: {processed_count}”) print(f”跳过文件: {skipped_count}”) print(f”错误文件: {error_count}”) # 主程序入口 if __name__ == “__main__”: root_folder = r”D:gamednf70scriptn_quest” process_all_quest_files(root_folder) 先判断qst文件中的难度值。如果只有-1,则直接跳过。 找到难度的最小值,比如这里是2,然后补全到4。 实际上,在更高难度的填充中,获得概率也应该适当增加一点点,最高不超过100为止。

作者:贴吧用户_aK7tA9K

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。