Пример #1
0
        public void Execute()
        {
            try {
                bool       exec = false;
                InvokeMode mode = InvokeMode.im_ItSelf;

                switch (Action)
                {
                case EffectAction.ea_Instant:
                    // dummy
                    break;

                case EffectAction.ea_Persistent:
                    exec = (Duration == 1);
                    mode = InvokeMode.im_FinAction;
                    break;

                case EffectAction.ea_EachTurn:
                    exec = true;
                    break;

                case EffectAction.ea_RandomTurn:
                    exec = AuxUtils.Chance(10);
                    break;

                case EffectAction.ea_LastTurn:
                    exec = (Duration == 1);
                    break;
                }

                if (exec)
                {
                    EffectID eid = (EffectID)CLSID;

                    if (this is MapEffect)
                    {
                        EffectExt ext = ((MapEffect)this).Ext;
                        InvokeEffect(eid, null, Source, mode, EffectAction.ea_Instant, ext);
                    }
                    else
                    {
                        NWCreature creature   = (NWCreature)Owner;
                        int        lid        = (creature.CurrentField).LandID;
                        bool       LocusValid = (eid == EffectID.eid_Relocation) && creature.IsPlayer && creature.Effects.FindEffectByID(EffectID.eid_LocusMastery) != null && lid != GlobalVars.Land_Valhalla && lid != GlobalVars.Land_Vigrid && lid != GlobalVars.Land_Bifrost && lid != GlobalVars.Land_Nidavellir && lid != GlobalVars.Land_Niflheim && lid != GlobalVars.Land_Ocean;
                        if (LocusValid)
                        {
                            EffectExt ext = new EffectExt();
                            ext.ReqParams = new EffectParams(EffectParams.ep_Place);
                            GlobalVars.nwrWin.InitTarget(EffectID.eid_Relocation, null, mode, ext);
                        }
                        else
                        {
                            creature.UseEffect(eid, Source, mode, null);
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.Write("Effect.execute(): " + ex.Message);
            }
        }
Пример #2
0
        public void ApplyEffects(CreatureEntity target, InvokeMode invokeMode, EffectExt extData)
        {
            try {
                ItemEntry.EffectEntry[] effects = fEntry.Effects;
                if (effects == null)
                {
                    return;
                }

                int num = effects.Length;
                for (int i = 0; i < num; i++)
                {
                    ItemEntry.EffectEntry eff = fEntry.Effects[i];

                    EffectExt ext;
                    if (invokeMode < InvokeMode.im_UseBegin || invokeMode >= InvokeMode.im_ItSelf)
                    {
                        ext = extData;
                    }
                    else
                    {
                        ext = new EffectExt();
                        ext.SetParam(EffectParams.ep_ItemExt, eff.ExtData);
                    }

                    ((NWCreature)target).UseEffect(eff.EffID, this, invokeMode, ext);
                }
            } catch (Exception ex) {
                Logger.Write("Item.applyEffects(): " + ex.Message);
            }
        }
Пример #3
0
        public override void UseEffect(EffectID effectID, object source, InvokeMode invokeMode, EffectExt ext)
        {
            bool paramsValid = Effect.InitParams(effectID, this, source, invokeMode, ref ext);

            if (paramsValid)
            {
                base.UseEffect(effectID, source, invokeMode, ext);
            }
            else
            {
                GlobalVars.nwrWin.InitTarget(effectID, source, invokeMode, ext);
            }
        }
Пример #4
0
        public void Exec(NWCreature creature, EffectID effectID, InvokeMode invokeMode, EffectExt ext, string rayMsg)
        {
            GlobalVars.nwrWin.ShowText(creature, rayMsg);

            int          i      = Effect.GetMagnitude(effectID);
            EffectTarget target = EffectTarget.et_None;

            if ((ext.ReqParams.Contains(EffectParams.ep_Direction)))
            {
                target = EffectTarget.et_Direction;
            }
            else
            {
                if ((ext.ReqParams.Contains(EffectParams.ep_Creature)))
                {
                    target = EffectTarget.et_Creature;
                }
            }
            int x;
            int y;
            int x2;
            int y2;

            if (target != EffectTarget.et_Direction)
            {
                if (target != EffectTarget.et_Creature)
                {
                    return;
                }
                x = creature.PosX;
                y = creature.PosY;
                NWCreature extCr = (NWCreature)ext.GetParam(EffectParams.ep_Creature);
                x2 = extCr.PosX;
                y2 = extCr.PosY;
                int dir = Directions.GetDirByCoords(x, y, x2, y2);
                x += Directions.Data[dir].DX;
                y += Directions.Data[dir].DY;
            }
            else
            {
                int dir = (int)ext.GetParam(EffectParams.ep_Direction);
                x  = creature.PosX + Directions.Data[dir].DX;
                y  = creature.PosY + Directions.Data[dir].DY;
                x2 = x + Directions.Data[dir].DX * i;
                y2 = y + Directions.Data[dir].DY * i;
            }
            Creature = creature;
            Dir      = Directions.GetDirByCoords(x, y, x2, y2);
            EffID    = effectID;

            Field = Creature.CurrentField;

            int       eid    = (int)effectID;
            EffectRec effRec = EffectsData.dbEffects[eid];

            if (effRec.FrameCount > 0)
            {
                MapObject = new MapObject(creature.Space, Field);
                MapObject.InitByEffect(effectID);
                Field.Features.Add(MapObject);
            }
            else
            {
                MapObject = null;
            }

            TargetMeeted = AuxUtils.DoLine(x, y, x2, y2, LineProc, true);

            if (MapObject != null)
            {
                Field.Features.Remove(MapObject);
            }
        }
Пример #5
0
        public static void InvokeEffect(EffectID effectID, LocatedEntity creature, object source, InvokeMode invokeMode, EffectAction action, EffectExt ext)
        {
            if (effectID == EffectID.eid_None)
            {
                return;
            }

            try {
                if (creature != null)
                {
                    GameEvent @event = new GameEvent(GameSpace.Instance, null);
                    @event.CLSID = (int)effectID;
                    @event.SetPos(creature.PosX, creature.PosY);
                    GlobalVars.nwrWin.DoEvent(EventID.event_EffectSound, null, null, @event);
                    @event.Dispose();
                }

                ItemState state;
                if (source != null && source is Item)
                {
                    state = ((Item)source).State;
                }
                else
                {
                    state = ItemState.is_Normal;
                }

                bool valid = (ext == null || ext.Valid);
                if (valid)
                {
                    IEffectProc proc = EffectsData.dbEffectProcs[(int)effectID].Proc;
                    if (proc != null)
                    {
                        proc.Invoke(effectID, (NWCreature)creature, source, state, invokeMode, ext);
                    }
                }
            } catch (Exception ex) {
                //EffectsData.dbEffects[(int)effectID]
                string msg = "Effect.invokeEffect(): " + effectID.ToString();
                if (creature != null)
                {
                    msg = msg + "/" + ((NWCreature)creature).Entry.Sign;
                }
                if (source != null && source is Item)
                {
                    msg = msg + "/" + ((Item)source).Entry.Sign;
                }
                msg = msg + ", " + ex.Message;
                Logger.Write(msg);
            }
        }
Пример #6
0
        public static bool InitParams(EffectID effectID, LocatedEntity creature, object source, InvokeMode invokeMode, ref EffectExt refExt)
        {
            if (refExt == null)
            {
                refExt = new EffectExt();
            }

            refExt.ReqParams = EffectsData.dbEffects[(int)effectID].ReqParams;
            ICheckProc proc = EffectsData.dbEffectProcs[(int)effectID].Check;

            if (proc != null)
            {
                proc.Invoke(effectID, (NWCreature)creature, source, invokeMode, refExt);
            }

            return(refExt.Valid);
        }
Пример #7
0
        private static Task[]? InvokeEventHandlers <TEventHandler, TEventArgs>(TEventHandler?eventHandler, object?eventLock, Action <Exception, Delegate>?exceptionHandler, object sender, TEventArgs args, InvokeMode invokeMode) where TEventHandler : MulticastDelegate where TEventArgs : EventArgs
        {
            if (eventHandler is null)
            {
                return(null);
            }

            Delegate[] handlers;

            lock (eventLock ?? eventHandler)
                handlers = eventHandler.GetInvocationList();

            void invokeHandler(Delegate handler)
            {
                switch (handler)
                {
                case EventHandler simpleHandler:
                    try
                    {
                        simpleHandler.Invoke(sender, args);
                    }
                    catch (Exception ex)
                    {
                        if (exceptionHandler is null)
                        {
                            LibraryEvents.OnSuppressedException(typeof(EventHandlerExtensions), new Exception($"SafeInvoke caught exception in {typeof(TEventHandler).FullName} event handler \"{handler.GetHandlerName()}\": {ex.Message}", ex));
                        }
                        else
                        {
                            exceptionHandler(ex, simpleHandler);
                        }
                    }
                    break;

                case EventHandler <TEventArgs> typedHandler:
                    try
                    {
                        typedHandler.Invoke(sender, args);
                    }
                    catch (Exception ex)
                    {
                        if (exceptionHandler is null)
                        {
                            LibraryEvents.OnSuppressedException(typeof(EventHandlerExtensions), new Exception($"SafeInvoke caught exception in {typeof(TEventHandler).FullName} event handler \"{handler.GetHandlerName()}\": {ex.Message}", ex));
                        }
                        else
                        {
                            exceptionHandler(ex, typedHandler);
                        }
                    }
                    break;

                default:
                    try
                    {
                        handler.DynamicInvoke(sender, args);
                    }
                    catch (Exception ex)
                    {
                        if (exceptionHandler is null)
                        {
                            LibraryEvents.OnSuppressedException(typeof(EventHandlerExtensions), new Exception($"SafeInvoke caught exception in {typeof(TEventHandler).FullName} event handler \"{handler.GetHandlerName()}\": {ex.Message}", ex));
                        }
                        else
                        {
                            exceptionHandler(ex, handler);
                        }
                    }
                    break;
                }
            }

            // Safely iterate each attached handler, continuing on possible exception, so no handlers are missed
            switch (invokeMode)
            {
            case InvokeMode.InvokeAsync:
                return(handlers.Select(handler => Task.Run(() => invokeHandler(handler))).ToArray());

            case InvokeMode.InvokeParallel when handlers.Length > 1:
                Parallel.ForEach(handlers, invokeHandler);
                break;

            default:
                foreach (Delegate handler in handlers)
                {
                    invokeHandler(handler);
                }
                break;
            }

            return(null);
        }