289 lines
8.3 KiB
Python
289 lines
8.3 KiB
Python
"""
|
|
群体间交互示例
|
|
演示不同群体之间的交互行为
|
|
"""
|
|
|
|
from panda3d.core import Vec3
|
|
|
|
def create_cooperative_swarms(swarm_manager):
|
|
"""
|
|
创建合作群体
|
|
两个群体相互合作,共同完成任务
|
|
"""
|
|
print("创建合作群体...")
|
|
|
|
# 创建两个不同类型的群体
|
|
swarm_configs = [
|
|
{
|
|
'type': '鸟类',
|
|
'count': 30,
|
|
'position_offset': Vec3(-20, -20, 10)
|
|
},
|
|
{
|
|
'type': '昆虫',
|
|
'count': 50,
|
|
'position_offset': Vec3(20, 20, 5)
|
|
}
|
|
]
|
|
|
|
swarm_manager.create_multiple_swarms(swarm_configs)
|
|
|
|
# 添加合作交互关系
|
|
if len(swarm_manager.swarms) >= 2:
|
|
swarm_manager.interaction_manager.add_interaction(
|
|
swarm_manager.swarms[0],
|
|
swarm_manager.swarms[1],
|
|
'cooperative',
|
|
0.8
|
|
)
|
|
|
|
print("合作群体创建完成")
|
|
|
|
def create_competitive_swarms(swarm_manager):
|
|
"""
|
|
创建竞争群体
|
|
两个群体争夺同一资源
|
|
"""
|
|
print("创建竞争群体...")
|
|
|
|
# 创建两个相同类型的群体
|
|
swarm_configs = [
|
|
{
|
|
'type': '鱼类',
|
|
'count': 25,
|
|
'position_offset': Vec3(-15, 0, 8)
|
|
},
|
|
{
|
|
'type': '鱼类',
|
|
'count': 25,
|
|
'position_offset': Vec3(15, 0, 8)
|
|
}
|
|
]
|
|
|
|
swarm_manager.create_multiple_swarms(swarm_configs)
|
|
|
|
# 添加竞争交互关系
|
|
if len(swarm_manager.swarms) >= 2:
|
|
swarm_manager.interaction_manager.add_interaction(
|
|
swarm_manager.swarms[0],
|
|
swarm_manager.swarms[1],
|
|
'competitive',
|
|
1.0
|
|
)
|
|
|
|
# 在中心添加一个资源点
|
|
resource_position = Vec3(0, 0, 10)
|
|
swarm_manager.create_obstacle(resource_position, 3.0)
|
|
|
|
print("竞争群体创建完成")
|
|
|
|
def create_predator_prey_swarms(swarm_manager):
|
|
"""
|
|
创建捕食者-猎物群体
|
|
一个群体捕食另一个群体
|
|
"""
|
|
print("创建捕食者-猎物群体...")
|
|
|
|
# 创建捕食者和猎物群体
|
|
swarm_configs = [
|
|
{
|
|
'type': '鸟类', # 捕食者
|
|
'count': 10,
|
|
'position_offset': Vec3(-25, -25, 15)
|
|
},
|
|
{
|
|
'type': '昆虫', # 猎物
|
|
'count': 80,
|
|
'position_offset': Vec3(0, 0, 5)
|
|
}
|
|
]
|
|
|
|
swarm_manager.create_multiple_swarms(swarm_configs)
|
|
|
|
# 添加捕食者-猎物交互关系
|
|
if len(swarm_manager.swarms) >= 2:
|
|
swarm_manager.interaction_manager.add_interaction(
|
|
swarm_manager.swarms[0], # 捕食者
|
|
swarm_manager.swarms[1], # 猎物
|
|
'predator_prey',
|
|
1.2
|
|
)
|
|
|
|
print("捕食者-猎物群体创建完成")
|
|
|
|
def create_complex_ecosystem(swarm_manager):
|
|
"""
|
|
创建复杂生态系统
|
|
包含多个群体和多种交互关系
|
|
"""
|
|
print("创建复杂生态系统...")
|
|
|
|
# 创建多个群体
|
|
swarm_configs = [
|
|
{
|
|
'type': '鸟类', # 猎食者
|
|
'count': 15,
|
|
'position_offset': Vec3(-30, -30, 20)
|
|
},
|
|
{
|
|
'type': '鱼类', # 中级消费者
|
|
'count': 40,
|
|
'position_offset': Vec3(0, -20, 10)
|
|
},
|
|
{
|
|
'type': '昆虫', # 初级消费者
|
|
'count': 100,
|
|
'position_offset': Vec3(20, 10, 5)
|
|
},
|
|
{
|
|
'type': '鸟类', # 分解者协作者
|
|
'count': 20,
|
|
'position_offset': Vec3(30, 30, 15)
|
|
}
|
|
]
|
|
|
|
swarm_manager.create_multiple_swarms(swarm_configs)
|
|
|
|
# 添加复杂的交互关系
|
|
if len(swarm_manager.swarms) >= 4:
|
|
# 鸟类捕食鱼类
|
|
swarm_manager.interaction_manager.add_interaction(
|
|
swarm_manager.swarms[0],
|
|
swarm_manager.swarms[1],
|
|
'predator_prey',
|
|
1.0
|
|
)
|
|
|
|
# 鱼类捕食昆虫
|
|
swarm_manager.interaction_manager.add_interaction(
|
|
swarm_manager.swarms[1],
|
|
swarm_manager.swarms[2],
|
|
'predator_prey',
|
|
0.8
|
|
)
|
|
|
|
# 鸟类与分解者协作者合作
|
|
swarm_manager.interaction_manager.add_interaction(
|
|
swarm_manager.swarms[0],
|
|
swarm_manager.swarms[3],
|
|
'cooperative',
|
|
0.6
|
|
)
|
|
|
|
# 昆虫与分解者协作者竞争资源
|
|
swarm_manager.interaction_manager.add_interaction(
|
|
swarm_manager.swarms[2],
|
|
swarm_manager.swarms[3],
|
|
'competitive',
|
|
0.7
|
|
)
|
|
|
|
# 添加环境元素
|
|
# 食物源
|
|
swarm_manager.create_obstacle(Vec3(10, 0, 8), 4.0)
|
|
swarm_manager.create_obstacle(Vec3(-10, 20, 6), 3.0)
|
|
|
|
# 障碍物
|
|
swarm_manager.create_obstacle(Vec3(-15, -10, 12), 5.0)
|
|
swarm_manager.create_obstacle(Vec3(25, -15, 10), 6.0)
|
|
|
|
print("复杂生态系统创建完成")
|
|
|
|
def create_dynamic_interactions(swarm_manager):
|
|
"""
|
|
创建动态交互场景
|
|
交互关系随时间变化
|
|
"""
|
|
print("创建动态交互场景...")
|
|
|
|
# 创建两个群体
|
|
swarm_configs = [
|
|
{
|
|
'type': '鸟类',
|
|
'count': 30,
|
|
'position_offset': Vec3(-20, 0, 15)
|
|
},
|
|
{
|
|
'type': '鱼类',
|
|
'count': 30,
|
|
'position_offset': Vec3(20, 0, 10)
|
|
}
|
|
]
|
|
|
|
swarm_manager.create_multiple_swarms(swarm_configs)
|
|
|
|
# 初始为合作关系
|
|
if len(swarm_manager.swarms) >= 2:
|
|
swarm_manager.interaction_manager.add_interaction(
|
|
swarm_manager.swarms[0],
|
|
swarm_manager.swarms[1],
|
|
'cooperative',
|
|
0.8
|
|
)
|
|
|
|
# 设置动态交互变化
|
|
swarm_manager.dynamic_interactions = {
|
|
'time_points': [0, 30, 60, 90], # 时间点(秒)
|
|
'interactions': [
|
|
('cooperative', 0.8),
|
|
('competitive', 1.0),
|
|
('predator_prey', 1.2), # 第一个群体捕食第二个群体
|
|
('cooperative', 0.6)
|
|
]
|
|
}
|
|
|
|
print("动态交互场景创建完成")
|
|
|
|
def update_dynamic_interactions(swarm_manager, elapsed_time):
|
|
"""
|
|
更新动态交互关系
|
|
"""
|
|
if hasattr(swarm_manager, 'dynamic_interactions'):
|
|
time_points = swarm_manager.dynamic_interactions['time_points']
|
|
interactions = swarm_manager.dynamic_interactions['interactions']
|
|
|
|
# 找到当前时间点
|
|
current_index = 0
|
|
for i, time_point in enumerate(time_points):
|
|
if elapsed_time >= time_point:
|
|
current_index = i
|
|
else:
|
|
break
|
|
|
|
if current_index < len(interactions):
|
|
interaction_type, strength = interactions[current_index]
|
|
|
|
# 更新交互关系
|
|
if len(swarm_manager.swarms) >= 2:
|
|
# 清除现有交互
|
|
swarm_manager.interaction_manager.clear_interactions()
|
|
|
|
# 添加新交互
|
|
swarm_manager.interaction_manager.add_interaction(
|
|
swarm_manager.swarms[0],
|
|
swarm_manager.swarms[1],
|
|
interaction_type,
|
|
strength
|
|
)
|
|
|
|
print(f"交互关系更新为: {interaction_type} (强度: {strength})")
|
|
|
|
def run_interaction_example(swarm_manager):
|
|
"""
|
|
运行交互示例
|
|
"""
|
|
print("=== 群体间交互示例 ===")
|
|
|
|
# 创建复杂生态系统
|
|
create_complex_ecosystem(swarm_manager)
|
|
|
|
# 启用相关配置
|
|
swarm_manager.config.set("interaction_weight", 1.0)
|
|
swarm_manager.config.set("cohesion_weight", 1.0)
|
|
swarm_manager.config.set("separation_weight", 1.5)
|
|
swarm_manager.config.set("alignment_weight", 1.0)
|
|
|
|
print("交互示例设置完成")
|
|
print("观察不同群体之间的复杂交互行为")
|
|
|
|
return update_dynamic_interactions |