main
hkr04 2024-12-20 19:13:37 +08:00
parent 1af8749a8c
commit 7a03d40d51
3 changed files with 107 additions and 14 deletions

View File

@ -84,7 +84,7 @@ class AgentType(MyEnum):
CommandPost = 22 # 指挥部
class ModuleType(MyEnum):
HANGER = 0
HANGAR = 0
SUPPLY = 1
TRANSPORT = 2
@ -267,7 +267,7 @@ class Agent:
if module.module_type == ModuleType.SUPPLY:
self._has_supply = True
self.supply = module
elif module.module_type == ModuleType.HANGER:
elif module.module_type == ModuleType.HANGAR:
self._parked_agents = module.parked_agents
elif module.module_type == ModuleType.TRANSPORT:
self._parked_agents = module.parked_agents
@ -312,15 +312,16 @@ class Agent:
"team_id": self.team_id,
"faction_id": self.faction_id,
"move_type": self.move_type.name,
# "info_level": self.info_level,
# "stealth_level": self.stealth_level,
"info_level": self.info_level,
"stealth_level": self.stealth_level,
"defense": self.defense,
"max_endurance": self.max_endurance,
"max_fuel": self.max_fuel,
"switchable_weapons": [weapon.to_dict() for weapon in self.switchable_weapons],
"modules": [module.to_dict() for module in self.modules],
"is_key": self.is_key,
# 以下为可变属性
"pos": list(self.pos),
"pos": {"q": self.pos[0], "r": self.pos[1]},
"endurance": self.endurance,
"fuel": self.fuel,
"mobility": self.mobility,
@ -505,9 +506,9 @@ class Module(ABC):
"available_types": [agent_type.name for agent_type in self.available_types]
}
class Hanger(Module):
class Hangar(Module):
def __init__(self, available_types: List[AgentType] = None, capacity: int = 6):
super().__init__(module_type=ModuleType.HANGER,
super().__init__(module_type=ModuleType.HANGAR,
add_endurance=4,
add_ammo=-1,
add_fuel=-1,
@ -538,5 +539,4 @@ class Transport(Module):
self.parked_agents = []
def reset(self):
self.parked_agents = []
self.parked_agents = []

View File

@ -1,8 +1,9 @@
import heapq
from .agent import Agent, Weapon, Action, TileNode, Module, Supply, Hanger, Transport
from .agent import Agent, Weapon, Action, TileNode, Module, Supply, Hangar, Transport
from .agent import MoveType, TerrainType, ActionType, AgentType, ModuleType
from typing import Tuple, List, Dict, Union
import numpy as np
import json
# Define the cost matrix for agent types and terrain types
cost_matrix = [
@ -273,8 +274,8 @@ def dict_to_module(module_dict: dict) -> Module:
available_types = [AgentType.from_input(agent_type) for agent_type in module_dict.get("available_types", [])] \
if module_dict.get("available_types", []) is not None else []
capacity = module_dict.get("capacity", 0)
if module_type == ModuleType.HANGER:
return Hanger(available_types=available_types, capacity=capacity)
if module_type == ModuleType.HANGAR:
return Hangar(available_types=available_types, capacity=capacity)
elif module_type == ModuleType.SUPPLY:
return Supply(available_types=available_types, capacity=capacity)
elif module_type == ModuleType.TRANSPORT:
@ -298,4 +299,70 @@ def dict_to_agent(agent_dict: dict) -> Agent:
mobility=agent_dict["mobility"],
switchable_weapons=[dict_to_weapon(weapon_dict) for weapon_dict in agent_dict.get("switchable_weapons", [])],
modules=[dict_to_module(module_dict) for module_dict in agent_dict.get("modules", [])] if agent_dict.get("modules", []) is not None else []
)
)
def json_to_markdown(json_data):
# 解析JSON数据
if isinstance(json_data, str):
json_data = json.loads(json_data)
# 递归转换字典
def dict_to_markdown(d, indent=0):
markdown = ""
for key, value in d.items():
markdown += " " * indent + f"- {key}: "
if isinstance(value, dict):
markdown += "\n" + dict_to_markdown(value, indent + 1)
elif isinstance(value, list):
markdown += "\n" + list_to_markdown(value, indent + 1)
else:
markdown += f"{value}\n"
return markdown
# 递归转换列表
def list_to_markdown(lst, indent=0):
markdown = ""
for item in lst:
if isinstance(item, dict):
markdown += dict_to_markdown(item, indent)
elif isinstance(item, list):
markdown += list_to_markdown(item, indent)
else:
markdown += " " * indent + f"- {item}\n"
return markdown
# 判断是否是字典或列表
if isinstance(json_data, dict):
return dict_to_markdown(json_data)
elif isinstance(json_data, list):
return list_to_markdown(json_data)
else:
return str(json_data)
def increase_markdown_indent(markdown_str, levels=1, use_tabs=False):
# 根据需要使用空格或制表符来增加缩进
indent = "\t" * levels if use_tabs else " " * levels
# 将每一行加上缩进
lines = markdown_str.split("\n")
indented_lines = [indent + line for line in lines]
# 返回增加层级后的Markdown字符串
return "\n".join(indented_lines)
def agent_list_to_markdown(agent_list):
result = ""
for agent in agent_list:
result += f"### Agent {agent.agent_id}\n"
result += json_to_markdown(agent.to_dict()) + "\n"
return result
def battlefield_list_to_markdown(battlefield_list):
result = ""
for battlefield in battlefield_list:
result += f"### Battlefield {battlefield['sectorName']}\n"
result += json_to_markdown(battlefield) + "\n"
return result

View File

@ -213,6 +213,22 @@ class YesCmdrEnv(gym.Env):
return agent
raise ValueError(f"Invalid agent id: {agent_id}")
def get_agents(self, agent_ids: List[str]) -> List[Agent]:
agents = []
for agent_id in agent_ids:
agent = self.get_agent(agent_id)
# if not agent.alive:
# raise ValueError(f"Agent {agent_id} is not alive")
agents.append(agent)
return agents
def get_agents_dict(self, agent_ids: List[str]) -> Dict[str, dict]:
agents_dict = {}
for agent_id in agent_ids:
agent = self.get_agent(agent_id)
agents_dict[agent_id] = agent.to_dict()
return agents_dict
def get_node(self, pos: Tuple[int, int]) -> TileNode:
if pos not in self.map.nodes:
raise ValueError(f"Invalid position: {pos}")
@ -1286,6 +1302,16 @@ class YesCmdrEnv(gym.Env):
return obs, reward, terminated, truncated, info
def format_agents(self, agent_ids = None):
if agent_ids is None:
agent_ids = self.current_agents_dict.keys()
return json_to_markdown(self.get_agents_dict(agent_ids))
def format_battlefeild(self, battlefield_info):
dict_info = {}
dict_info["battlefield_name"] = battlefield_info["sectorName"]
dict_info["my_agents"] = self.get_agents_dict(battlefield_info["myUnitsID"])
dict_info["ally_agents"] = self.get_agents_dict(battlefield_info["allyUnitsID"])
dict_info["enemy_agents"] = self.get_agents_dict(battlefield_info["hostileUnitsID"])
return json_to_markdown(dict_info)