WorldSimulation / plugins /BattlePlugin.py
yingqianjiang-lingoace
update
0a0d866
raw
history blame
2.95 kB
import random
class BattlePlugin:
def __init__(self, probability, min_battle_age=10, max_battle_round=100):
self.probability = probability
self.min_battle_age = min_battle_age # 最小参战年龄
self.max_battle_round = max_battle_round # 最大战斗回合数
def simulate_battle(self, character, opponent):
battle_round = 0
character.before_battle()
opponent.before_battle()
while character.check_is_alive() and opponent.check_is_alive() and battle_round < self.max_battle_round:
battle_round += 1
# 根据攻击速度确定行动顺序
if character.combat_power["attack_speed"] >= opponent.combat_power["attack_speed"]:
character.attack(opponent)
if opponent.check_is_alive():
opponent.attack(character)
else:
opponent.attack(character)
if character.check_is_alive():
character.attack(opponent)
# 胜利或平手判断
if character.check_is_alive() and opponent.check_is_alive():
# print(f"{character.name}和{opponent.name}打成平手!")
return None
elif character.check_is_alive():
print(f"{character.name}战胜了{opponent.name}!")
return (character, opponent)
elif opponent.check_is_alive():
print(f"{opponent.name}战胜了{character.name}!")
return (opponent, character)
def perform_battles(self, characters, character_die):
# 按照年龄筛选出参战人员,且成仙者不参与战斗
eligible_characters = [character for character in characters if character.apparent_age > self.min_battle_age and character.check_is_alive() and not character.is_immortal]
for _ in range(int(len(eligible_characters) * self.probability)):
character = random.choice(eligible_characters)
opponent = random.choice(eligible_characters)
if character != opponent and character.clan != opponent.clan:
result = self.simulate_battle(character, opponent)
if result is not None:
(winner, loser) = result
winner.cultivate(100)
winner.history.append(f"{winner.real_age}岁({winner.view_rank()}),战胜了{loser.name}{loser.view_rank()})")
loser.history.append(f"{loser.real_age}岁({loser.view_rank()}),被{winner.name}{winner.view_rank()})打败了")
if not loser.is_alive:
print(f"{loser.name}因失血过多死亡了!")
character_die(loser)
return
def set_battle_rate(self, probability):
self.probability = probability
# 统一插件接口
def execute(self, *args, **kwargs):
self.perform_battles(*args, **kwargs)