Exemplo n.º 1
0
        public AIAttackModule AddPriorityAttackModule(AIAttackModule mod, bool atStart = true)
        {
            if (atStart)
            {
                attackModules.Insert(0, mod);
            }
            else
            {
                attackModules.Add(mod);
            }

            return(mod);
        }
Exemplo n.º 2
0
        protected MonsterAI(Character o) : base(o)
        {
            attackModules         = new List <AIAttackModule>();
            aggro                 = new Dictionary <Character, int>();
            currentPrioridyModule = null;

            attackStartedTime = 0;

            useTimers = false;

            IsAggressive    = GetTemplate().IsAggressive;
            AggressionRange = GetTemplate().AggressionRange;

            CreateModules();
        }
Exemplo n.º 3
0
        public bool LaunchAttackModule(Character target, float distSqr, float hpPercentage)
        {
            try
            {
                if (currentPrioridyModule != null)
                {
                    if (currentPrioridyModuleTime + currentPrioridyModule.keepActiveFor >= Time.time)
                    {
                        if (currentPrioridyModule.ForceLaunch(target, distSqr, hpPercentage))
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        currentPrioridyModule = null;
                    }
                }

                foreach (AIAttackModule module in attackModules)
                {
                    if (module.Launch(target, distSqr, hpPercentage, attackStartedTime))
                    {
                        if (module.keepActiveFor > 0)
                        {
                            currentPrioridyModule     = module;
                            currentPrioridyModuleTime = Time.time;
                        }

                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError("error while doing ai module: " + e.StackTrace);
            }

            return(false);
        }
Exemplo n.º 4
0
        public override MonsterAI CreateAI(Character ch)
        {
            if (AiType != null)
            {
                MonsterAI       ai = null;
                MonsterTemplate t;
                List <Type>     types = Utils.GetTypesInNamespace("Assets.scripts.AI", true, typeof(MonsterAI));

                foreach (Type type in types)
                {
                    if (type.Name.Equals(AiType, StringComparison.InvariantCultureIgnoreCase))
                    {
                        ai = Activator.CreateInstance(type, ch) as MonsterAI;
                        break;
                    }
                }

                if (ai == null)
                {
                    throw new NullReferenceException("CustomAIType " + AiType + " doesnt exist!");
                }

                foreach (AddModuleInfo info in NewModules)
                {
                    int    id        = info.id;
                    string module    = info.module + "Module";
                    bool   highPrior = info.atHighPriority;

                    List <Type>    moduleTypes = Utils.GetTypesInNamespace("Assets.scripts.AI.Modules", true, typeof(AIAttackModule));
                    AIAttackModule newModule   = null;

                    foreach (Type type in moduleTypes)
                    {
                        if (type.Name.Equals(module, StringComparison.InvariantCultureIgnoreCase))
                        {
                            newModule = Activator.CreateInstance(type, ai) as AIAttackModule;
                            break;
                        }
                    }

                    if (newModule != null)
                    {
                        newModule.id = id;

                        Debug.Log("added new module " + newModule.id + " name " + module);

                        ai.AddPriorityAttackModule(newModule, highPrior);
                    }
                    else
                    {
                        Debug.LogError("unknown module: " + module);
                    }
                }

                FieldInfo field;
                foreach (AiParamInfo info in AiParams)
                {
                    int    id     = info.id;
                    string module = info.module + "Module";
                    string key    = info.param;
                    string value  = info.value;

                    AIAttackModule moduleClass;

                    // find by ID
                    if (id > 0)
                    {
                        moduleClass = ai.GetAttackModule(id);
                    }
                    else                     // find by module name
                    {
                        moduleClass = ai.GetAttackModule(module);
                    }

                    field = moduleClass.GetType().GetField(key, BindingFlags.Public | BindingFlags.Instance);
                    if (field != null)
                    {
                        if (field.FieldType == typeof(int))
                        {
                            field.SetValue(moduleClass, Int32.Parse(value));
                        }
                        else if (field.FieldType == typeof(float))
                        {
                            field.SetValue(moduleClass, float.Parse(value));
                        }
                        else if (field.FieldType == typeof(double))
                        {
                            field.SetValue(moduleClass, Double.Parse(value));
                        }
                        else if (field.FieldType == typeof(bool))
                        {
                            field.SetValue(moduleClass, Boolean.Parse(value));
                        }
                        else if (field.FieldType == typeof(string))
                        {
                            field.SetValue(moduleClass, value);
                        }
                        else
                        {
                            throw new ArgumentException("invalid custom AI field " + key + " - has unknown type (not int, float, double or string)");
                        }
                    }
                    else
                    {
                        Debug.LogError("Cant write to property " + key + " in " + moduleClass.GetType().Name + " (property is null)");
                    }
                }

                Debug.Log("Created custom " + ai.GetType().Name + " AI for " + ch.Name);
                name_turret = ch.Name;
                return(ai);
            }
            else if (OldTemplate != null)
            {
                return(OldTemplate.CreateAI(ch));
            }

            throw new NullReferenceException("null AI (not set for some reason");
        }
Exemplo n.º 5
0
 /// <summary>
 /// modules added earlier have higher priority !!!
 /// </summary>
 /// <returns></returns>
 public AIAttackModule AddAttackModule(AIAttackModule mod)
 {
     attackModules.Add(mod);
     return(mod);
 }