using CommonAI.data;
using CommonAI.Zone;
using CommonLang;
using CommonLang.Log;
using CommonLang.Property;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using XmdsCommon.Plugin;
using XmdsCommonServer.XLS.Data;
using static XmdsCommonServer.Plugin.XmdsVirtual;
namespace XmdsCommonServer.Plugin.XmdsSkillTemplate.Skills
{
///
/// FileName: XmdsBattleSkill.cs
/// Description:
/// DateTime: 2015/6/1 17:50:47
///
public static class XmdsBattleSkill
{
//默认脚本ID.
public static readonly int DefaultSkillScriptID = 9999901;
private static TemplateManager mTemplates = null;
private static Logger log = LoggerFactory.GetLogger("XmdsBattleSkill");
//主动技能.
private static IDictionary UnitSkillMap = new SortedDictionary();
//被动技能.
private static IDictionary UnitPassiveSkillMap = new SortedDictionary();
private static IDictionary BattleFunctionEventMap = new SortedDictionary();
private static bool FinishInit = false;
//默认技能伤害机制
public static XmdsVirtual.OnCalDmageHandler mDefDamgageHandler;
//private static List UnitSkillAllTypes = new List();
static XmdsBattleSkill()
{
}
private static void LoadSkillDll()
{
if (FinishInit) { return; }
{
//扩展功能.
try
{
Type ctype = typeof(BattleFunction);
foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(ctype))
{
BattleFunction bf = ReflectionUtil.CreateInstance(stype) as BattleFunction;
BattleFunction.SetFactory(bf);
}
ctype = typeof(BattleFunctionEvent);
foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(ctype))
{
try
{
BattleFunctionEvent be = ReflectionUtil.CreateInstance(stype) as BattleFunctionEvent;
if(be.Init(0, 0))
{
BattleFunctionEventMap.Add(be.GetEventID(), be);
}
}
catch(Exception e)
{
log.Error("init battle funtion event catch: " + stype + ", " + e);
}
}
}
catch (Exception err)
{
log.Error("BattleFunctionEvent Error" + err.ToString());
//throw;
}
}
{
//主动技能.
try
{
Type ctype = typeof(UnitSkill);
foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(ctype))
{
UnitSkill sk = ReflectionUtil.CreateInstance(stype) as UnitSkill;
if (sk.SkillID < 1)
{
if(!sk.GetType().IsAbstract)
{
log.Warn("异常技能1, ID = " + sk.SkillID + ", name=" + sk.ToString());
}
continue;
}
XmdsSkillData skillData = XmdsDataMgr.GetInstance().GetXmdsSkillData(sk.SkillID);
if (skillData == null)
{
log.Warn("技能表未配置数据:" + sk.SkillID + ", 伤害走通用序列:" + sk.ToString());
continue;
}
//log.Debug("Regist skill : " + stype.FullName);
if (UnitSkillMap.ContainsKey(sk.SkillID))
{
throw new Exception("主动技能脚本ID重复:" + sk.SkillID);
}
UnitSkillMap.Add(sk.SkillID, stype);
sk.InitSkillParam(skillData);
}
}
catch (Exception err)
{
log.Error("ActiveSkillInit Error" + err.ToString());
//throw;
}
}
{
//被动技能.
try
{
Type type = typeof(UnitPassiveSkill);
foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(type))
{
UnitPassiveSkill sk = ReflectionUtil.CreateInstance(stype) as UnitPassiveSkill;
if (sk.SkillID < 1 )
{
log.Info("异常技能2, ID = " + sk.SkillID + ", name=" + sk.ToString());
continue;
}
if (XmdsDataMgr.GetInstance().GetXmdsSkillData(sk.SkillID) == null)
{
log.Error("skillData表未找到技能配置数据:" + sk.SkillID);
continue;
}
//log.Info("Regist PassiveSkill : " + stype.FullName);
if (UnitPassiveSkillMap.ContainsKey(sk.SkillID))
{
throw new Exception("被动技能脚本ID重复:" + sk.SkillID);
}
UnitPassiveSkillMap.Add(sk.SkillID, stype);
sk.InitSkillParam();
}
}
catch (Exception err)
{
log.Error("LoadPassiveSkillDll" + err.ToString());
//throw;
}
}
// 默认技能处理序列
Type defaultSkillType = null;
UnitSkillMap.TryGetValue(DefaultSkillScriptID, out defaultSkillType);
if(defaultSkillType != null)
{
GameSkill gameSkill = new GameSkill(DefaultSkillScriptID);
UnitSkill defaultSkill = ReflectionUtil.CreateInstance(defaultSkillType) as UnitSkill;
SkillTemplate skillTemplate = XmdsBattleSkill.GetSkillTemplate(DefaultSkillScriptID); ;
defaultSkill.Init(gameSkill, null, ref skillTemplate);
mDefDamgageHandler = new OnCalDmageHandler(defaultSkill.OnCallDamageProcess, gameSkill, false, 0);
}
else
{
log.Warn("默认技能伤害处理序列找不到!");
}
FinishInit = true;
}
public static void Init(TemplateManager mgr)
{
mTemplates = mgr;
//反射并初始化所有继承XmdsSkill的类.
LoadSkillDll();
}
///
/// 生成示例文件.
///
///
//public static string GenSampleSkillConfig()
//{
// bool gen_cfg = true;
// StringBuilder sb = new StringBuilder();
// UnitSkillAllTypes.Sort();
// foreach (UnitSkill sk in UnitSkillAllTypes)
// {
// Type type = sk.GetType();
// SkillTypeAttribute desca = PropertyUtil.GetAttribute(type);
// sb.AppendLine("==================================================================");
// sb.AppendLine(string.Format(">>>> {0} Catgory = {1} <<<<", desca.Category, sk.SkillID));
// foreach (string desc_line in desca.Desc.Split('\n'))
// {
// sb.AppendLine(string.Format("=== {0} ===", desc_line));
// }
// sb.AppendLine("==================================================================");
// foreach (FieldInfo fi in type.GetFields())
// {
// if (fi.IsStatic && fi.IsPublic)
// {
// FieldConfigurable cfg = PropertyUtil.GetAttribute(fi);
// object static_value = fi.GetValue(null);
// string static_text = Parser.ObjectToString(static_value);
// if (cfg != null && gen_cfg)
// {
// sb.AppendLine(cfg.Desc);
// }
// sb.AppendLine(string.Format("{0}.{1} , {2}", type.Name, fi.Name, static_text));
// }
// }
// sb.AppendLine();
// sb.AppendLine();
// }
// return sb.ToString();
//}
///
/// 加载数据配置文件.
///
///
//public static void LoadSkillConfig(string csv)
//{
// IDictionary skills = new SortedDictionary();
// foreach (UnitSkill sk in UnitSkillAllTypes)
// {
// skills.Add(sk.GetType().Name, sk);
// }
// Properties cfg = new Properties();
// cfg.ParseText(csv, ",");
// foreach (KeyValuePair line in cfg)
// {
// try
// {
// string[] kv = line.Key.Split(new char[] { '.' }, 2);
// if (kv.Length == 2)
// {
// string typeName = kv[0];
// string fieldName = kv[1];
// if (skills.ContainsKey(typeName))
// {
// object sk = skills[typeName];
// FieldInfo fi = sk.GetType().GetField(fieldName);
// FieldConfigurable fdesc = PropertyUtil.GetAttribute(fi);
// object static_value = Parser.StringToObject(line.Value, fi.FieldType);
// if (static_value != null)
// {
// fi.SetValue(null, static_value);
// if (fdesc != null) { log.Info("### " + fdesc.Desc); }
// log.Info(" --------------------- " + line.Key + " = " + line.Value);
// }
// else
// {
// log.Error("无法解析配置项 : " + line.Key + " = " + line.Value);
// }
// }
// }
// }
// catch (Exception err)
// {
// throw new Exception("无法解析配置项 : " + line.Key + " = " + line.Value + " : " + err.Message, err);
// }
// }
//}
#region 获取模板数据.
public static SkillTemplate GetSkillTemplate(int skillTemplateID)
{
SkillTemplate template = mTemplates.getSkill(skillTemplateID);
if (template != null)
{
template = template.Clone() as SkillTemplate;
}
return template;
}
public static SpellTemplate GetSpellTemplate(int spellTemplateID)
{
SpellTemplate template = mTemplates.getSpell(spellTemplateID);
if (template != null)
{
template = template.Clone() as SpellTemplate;
}
return template;
}
public static BuffTemplate GetBuffTemplate(int templateID, bool doClone = true)
{
BuffTemplate template = mTemplates.getBuff(templateID);
if (template != null && doClone)
{
template = template.Clone() as BuffTemplate;
}
return template;
}
public static UnitInfo GetUnitInfo(int templateID)
{
UnitInfo ret = mTemplates.getUnit(templateID);
return ret;
}
#endregion
#region 获取XmdsSkill.
public static UnitSkill GetUnitSkill(int id)
{
UnitSkill ret = null;
Type templateType = null;
if(UnitSkillMap.TryGetValue(id, out templateType))
{
ret = ReflectionUtil.CreateInstance(templateType) as UnitSkill;
}
return ret;
}
#endregion
#region 获取XmdsPassiveSkill.
public static UnitPassiveSkill GetPassiveSkill(int id)
{
UnitPassiveSkill ret = null;
Type templateType = null;
if (UnitPassiveSkillMap.TryGetValue(id, out templateType))
{
ret = ReflectionUtil.CreateInstance(templateType) as UnitPassiveSkill;
}
return ret;
}
#endregion
#region 获取战斗特殊事件.
//添加事件
public static void loadExtendFuntionEvent()
{
HashMap buffConfigs = XmdsDataMgr.GetInstance().GetBuffConfigDatas();
if (buffConfigs != null)
{
foreach (var buffConfig in buffConfigs.Values)
{
try
{
if (buffConfig.BaseFunID < 0) { continue; }
BattleFunctionEvent bfEvent = XmdsBattleSkill.GetBattleFunctionEvent(buffConfig.TriggerID);
if (bfEvent != null) { continue; }
if (bfEvent == null && buffConfig.BaseFunID == 0)
{
log.Warn("没有实现的触发事件:" + buffConfig.TriggerID);
continue;
}
bfEvent = XmdsBattleSkill.GetBattleFunctionEvent(buffConfig.BaseFunID);
if (bfEvent == null)
{
log.Warn("没有实现的基础触发事件:" + buffConfig.TriggerID + ", base: " + buffConfig.BaseFunID);
continue;
}
BattleFunctionEvent extendEvent = ReflectionUtil.CreateInstance(bfEvent.GetType()) as BattleFunctionEvent;
if (extendEvent == null)
{
log.Warn("创建扩展事件失败:" + buffConfig.TriggerID + ", base: " + buffConfig.BaseFunID);
continue;
}
if(extendEvent.Init(buffConfig.TriggerID, bfEvent.GetBindBuffID()))
{
BattleFunctionEventMap.Add(buffConfig.TriggerID, extendEvent);
}
}
catch (Exception e)
{
log.Error("loadExtendFuntionEvent异常:" + e);
}
}
}
}
public static BattleFunctionEvent GetBattleFunctionEvent(int id)
{
BattleFunctionEvent ret = null;
BattleFunctionEventMap.TryGetValue(id, out ret);
return ret;
}
#endregion
}
#region 技能描述字段.
//SkillTypeAttribute("狂战", "雕文技能")
[AttributeUsage(AttributeTargets.Field)]
public class FieldConfigurable : System.Attribute
{
public readonly string Desc;
public FieldConfigurable(string desc)
{
this.Desc = desc;
}
}
//【顺势】毁灭打击伤害的X%伤害.
[AttributeUsage(AttributeTargets.Class)]
public class SkillTypeAttribute : System.Attribute
{
public readonly string Desc;
public readonly string Category;
public SkillTypeAttribute(string desc, string catgory)
{
this.Desc = desc;
this.Category = catgory;
}
}
#endregion
}