RPG多人回合制战斗框架设计《六:战斗AI》

尉迟雅昶
2023-12-01

RPG战斗框架包含以下四部分的内容:

一、基本战斗流程

二、战斗角色设计

三、战斗伤害计算

四、技能设计

五、BUFF设计

六、战斗AI

RPG战斗的ai主要用在战斗NPC上

以战斗来看,应该有攻击意向,攻击方式,注重攻击或者防御,如何应对受击,对不同的情况有特定应对方式、如潜行/突袭,追击意向,技能释放意向等等。

其实电脑怪物要智能些,符合策划需求,像玩家一样,会特定时候放技能,血少时候逃跑等

战斗角色要存在ai的功能
class CNpcWarrior(CWarrior):
    def __init__(self, ai_func):
        self.m_ai_func = ai_func

AI = 条件 + 行为

一个怪物执行多个AI即可以完成想要的只能效果

比如:

AI1 = 血少时候 + 逃跑

AI2 = 最后一回合时候 + 释放技能

AISet = AI1 + AI2

怪物即可用AISet去实现

条件:

class CLimit(object):
	m_id = 101
	m_name = "血量低于指定值"

	def is_limited(self, warobj, wobj, hp):
		if wobj.get_hp() < hp:
			return True
		return False


class CLimit(object):
	m_id = 102
	m_name = "非某回合"

	def is_limited(self, warobj, wobj, bout):
		if warobj.m_bout != bout:
			return True

	return False

行为:

class CAction(object):
	m_id = 101
	m_name = "逃跑"

	def do_action(self, warobj, wobj, *args):
		wobj.escape()

AI父类:

 

# -*- coding: utf-8 -*-

import ai.limit
import ai.action


class CAIBase(object):
	m_id = 0
	m_name = ""
	m_opt = ""
	m_limits = []
	m_actions = []

	def __init__(self):
		lst = []
		for id in self.m_limits:
			ob = ai.limit.load_limit(id)
			lst.append(ob)
		self.m_limit_objects = lst

	def __call__(self, *args):
		limit_args, action_args, warobj, wobj = args

		limit_exp = self.m_opt
		for i, id in enumerate(self.m_limits):
			limit_exp = limit_exp.replace("%d" % id, "%s" % (
					"self.m_limit_objects[%d].IsLimited(warobj, wobj, *limit_args[%d])" % (i, i)), 1)

		if eval(limit_exp):
			for i, id in enumerate(self.m_actions):
				func = ai.action.load_action(id)
				func(warobj, wobj, *action_args[i])

ai 字类:

class CAI(CAIBase):
	m_id = 1001
	m_name = "血少非某回合逃跑"
	m_opt = "101 and not 102"
	m_limits = [101, 102]
	m_actions = [101]

多个ai集合
 


class CAISetBase(object):
	m_sid = 0
	m_ai_list = []

	def __init__(self):
		lst = []
		aiidlst = []
		for aid, args in self.m_ai_list:
			func = ai.load_ai_func(aid, *args)
			lst.append(func)
			aiidlst.append(aid)
		self.m_ai_objects = lst
		self.m_ai_list = aiidlst

	def __call__(self, warobj, wobj):
		for func in self.m_ai_objects:
			func(warobj, wobj)
# -*- coding: utf-8 -*-


class CAISet(CAISetBase):
    m_SID = 1002
    m_AIList = [
        (1001, (((100, 5),), ((None,),))),
        (1001, (((100, 5),), ((None,),))),
    ]

 类似资料: