Files
echoes-of-the-ash/scripts/export_to_json.py
2026-02-05 16:09:34 +01:00

166 lines
5.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Extract current game data to JSON files
This script reads the current Python-based game data and exports it to JSON format
"""
import sys
import json
from pathlib import Path
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent))
from data.world_loader import game_world, export_map_data
from data.npcs import NPCS, LOCATION_DANGER, LOCATION_SPAWNS
from data.items import ITEMS
def export_to_json():
"""Export all game data to JSON files"""
gamedata_dir = Path(__file__).parent / 'gamedata'
gamedata_dir.mkdir(exist_ok=True)
print("🔄 Exporting game data to JSON...")
# 1. Export locations and world data
print(" 📍 Exporting locations...")
locations_data = {
"locations": [],
"connections": []
}
for location in game_world.locations.values():
loc_data = {
"id": location.id,
"name": location.name,
"description": location.description,
"x": location.x,
"y": location.y,
"image_path": location.image_path,
"interactables": {}
}
# Add interactables
for instance_id, interactable in location.interactables.items():
inter_data = {
"id": interactable.id,
"name": interactable.name,
"image_path": interactable.image_path,
"actions": {}
}
# Add actions
for action_id, action in interactable.actions.items():
action_data = {
"id": action.id,
"label": action.label,
"stamina_cost": action.stamina_cost,
"outcomes": {}
}
# Add outcomes
for outcome_name, outcome in action.outcomes.items():
outcome_data = {
"text": outcome.text,
"items_reward": outcome.items_reward,
"damage_taken": outcome.damage_taken
}
action_data["outcomes"][outcome_name] = outcome_data
inter_data["actions"][action_id] = action_data
loc_data["interactables"][instance_id] = inter_data
locations_data["locations"].append(loc_data)
# Add connections with distance-based stamina cost
for direction, dest_id in location.exits.items():
dest_loc = game_world.get_location(dest_id)
if dest_loc:
from data.travel_helpers import calculate_base_stamina_cost
stamina_cost = calculate_base_stamina_cost(location, dest_loc)
else:
stamina_cost = 5 # Fallback
locations_data["connections"].append({
"from": location.id,
"to": dest_id,
"direction": direction,
"stamina_cost": stamina_cost
})
with open(gamedata_dir / 'locations.json', 'w') as f:
json.dump(locations_data, f, indent=2)
print(f" ✅ Exported {len(locations_data['locations'])} locations and {len(locations_data['connections'])} connections")
# 2. Export NPCs and spawns
print(" 👹 Exporting NPCs...")
npcs_data = {
"npcs": {},
"danger_levels": {},
"spawn_tables": {}
}
for npc_id, npc in NPCS.items():
# Convert loot tables to serializable format
loot_table = [
{"item_id": loot.item_id, "quantity_min": loot.quantity_min, "quantity_max": loot.quantity_max, "drop_chance": loot.drop_chance}
for loot in npc.loot_table
]
corpse_loot = [
{"item_id": loot.item_id, "quantity_min": loot.quantity_min, "quantity_max": loot.quantity_max, "required_tool": loot.required_tool}
for loot in npc.corpse_loot
]
npcs_data["npcs"][npc_id] = {
"npc_id": npc.npc_id,
"name": npc.name,
"description": npc.description,
"emoji": npc.emoji,
"hp_min": npc.hp_min,
"hp_max": npc.hp_max,
"damage_min": npc.damage_min,
"damage_max": npc.damage_max,
"defense": npc.defense,
"xp_reward": npc.xp_reward,
"loot_table": loot_table,
"corpse_loot": corpse_loot,
"flee_chance": npc.flee_chance,
"status_inflict_chance": npc.status_inflict_chance,
"image_url": npc.image_url,
"death_message": npc.death_message
}
for location_id, (danger, encounter_rate, wandering_chance) in LOCATION_DANGER.items():
npcs_data["danger_levels"][location_id] = {
"danger_level": danger,
"encounter_rate": encounter_rate,
"wandering_chance": wandering_chance
}
for location_id, spawns in LOCATION_SPAWNS.items():
npcs_data["spawn_tables"][location_id] = [
{"npc_id": npc_id, "weight": weight}
for npc_id, weight in spawns
]
with open(gamedata_dir / 'npcs.json', 'w') as f:
json.dump(npcs_data, f, indent=2)
print(f" ✅ Exported {len(npcs_data['npcs'])} NPCs, {len(npcs_data['danger_levels'])} danger configs, {len(npcs_data['spawn_tables'])} spawn tables")
# 3. Export items
print(" 🎒 Exporting items...")
items_data = {
"items": ITEMS # ITEMS is already a dict with the right structure
}
with open(gamedata_dir / 'items.json', 'w') as f:
json.dump(items_data, f, indent=2)
print(f" ✅ Exported {len(items_data['items'])} items")
print("\n✅ Export complete! JSON files created in gamedata/")
print(f" 📁 {gamedata_dir.absolute()}")
if __name__ == '__main__':
export_to_json()