Пример #1
0
        private void OnFinishActionItem(OnFinishActionItem data)
        {
            using (new Profiler(nameof(FinishActionItem)))
            {
                IActionItem actionItem = ItemService.GetActionItemHandler(data.ClassName);
                data.Player.IsBusy = false;

                Vector userPosition = data.Player.Position;
                if (userPosition.X != data.UserPosition.X ||
                    userPosition.Y != data.UserPosition.Y ||
                    userPosition.Z != data.UserPosition.Z)
                {
                    data.Player.SendMessage("You move and interrupt your action.");
                    return;
                }

                float maxDistance = actionItem.MaxDistance(data.Player, data.Item, data.Target, data.TargetLocation);
                if (maxDistance > 0.0f)
                {
                    NWObject owner = GetItemPossessor(data.Target);

                    if (data.Target.IsValid && owner.IsValid)
                    {
                        // We are okay - we have targeted an item in our inventory (we can't target someone
                        // else's inventory, so no need to actually check distance).
                    }
                    else if (data.Target.Object == _.OBJECT_SELF)
                    {
                        // Also okay.
                    }
                    else if (data.Target.IsValid &&
                             (_.GetDistanceBetween(data.Player, data.Target) > maxDistance ||
                              data.Player.Area.Resref != data.Target.Area.Resref))
                    {
                        data.Player.SendMessage("Your target is too far away.");
                        return;
                    }
                    else if (!data.Target.IsValid &&
                             (_.GetDistanceBetweenLocations(data.Player.Location, data.TargetLocation) > maxDistance ||
                              data.Player.Area.Resref != ((NWArea)_.GetAreaFromLocation(data.TargetLocation)).Resref))
                    {
                        data.Player.SendMessage("That location is too far away.");
                        return;
                    }
                }

                if (!data.Target.IsValid && !actionItem.AllowLocationTarget())
                {
                    data.Player.SendMessage("Unable to locate target.");
                    return;
                }

                string invalidTargetMessage = actionItem.IsValidTarget(data.Player, data.Item, data.Target, data.TargetLocation);
                if (!string.IsNullOrWhiteSpace(invalidTargetMessage))
                {
                    data.Player.SendMessage(invalidTargetMessage);
                    return;
                }

                actionItem.ApplyEffects(data.Player, data.Item, data.Target, data.TargetLocation, data.CustomData);

                if (actionItem.ReducesItemCharge(data.Player, data.Item, data.Target, data.TargetLocation, data.CustomData))
                {
                    if (data.Item.Charges > 0)
                    {
                        data.Item.ReduceCharges();
                    }

                    if (data.Item.Charges <= 0)
                    {
                        data.Item.Destroy();
                    }
                }
            }
        }
Пример #2
0
        private void OnFinishActionItem(OnFinishActionItem data)
        {
            using (new Profiler(nameof(FinishActionItem)))
            {
                IActionItem actionItem = ItemService.GetActionItemHandler(data.ClassName);
                data.Player.IsBusy = false;

                Vector userPosition = data.Player.Position;
                if (userPosition.m_X != data.UserPosition.m_X ||
                    userPosition.m_Y != data.UserPosition.m_Y ||
                    userPosition.m_Z != data.UserPosition.m_Z)
                {
                    data.Player.SendMessage("You move and interrupt your action.");
                    return;
                }

                float maxDistance = actionItem.MaxDistance(data.Player, data.Item, data.Target, data.TargetLocation);
                if (maxDistance > 0.0f)
                {
                    if (data.Target.IsValid &&
                        (_.GetDistanceBetween(data.Player, data.Target) > maxDistance ||
                         data.Player.Area.Resref != data.Target.Area.Resref))
                    {
                        data.Player.SendMessage("Your target is too far away.");
                        return;
                    }
                    else if (!data.Target.IsValid &&
                             (_.GetDistanceBetweenLocations(data.Player.Location, data.TargetLocation) > maxDistance ||
                              data.Player.Area.Resref != ((NWArea)_.GetAreaFromLocation(data.TargetLocation)).Resref))
                    {
                        data.Player.SendMessage("That location is too far away.");
                        return;
                    }
                }

                if (!data.Target.IsValid && !actionItem.AllowLocationTarget())
                {
                    data.Player.SendMessage("Unable to locate target.");
                    return;
                }

                string invalidTargetMessage = actionItem.IsValidTarget(data.Player, data.Item, data.Target, data.TargetLocation);
                if (!string.IsNullOrWhiteSpace(invalidTargetMessage))
                {
                    data.Player.SendMessage(invalidTargetMessage);
                    return;
                }

                actionItem.ApplyEffects(data.Player, data.Item, data.Target, data.TargetLocation, data.CustomData);

                if (actionItem.ReducesItemCharge(data.Player, data.Item, data.Target, data.TargetLocation, data.CustomData))
                {
                    if (data.Item.Charges > 0)
                    {
                        data.Item.ReduceCharges();
                    }
                    else
                    {
                        data.Item.Destroy();
                    }
                }
            }
        }
Пример #3
0
        private static void OnModuleActivatedItem()
        {
            NWPlayer user           = (_.GetItemActivator());
            NWItem   oItem          = (_.GetItemActivated());
            NWObject target         = (_.GetItemActivatedTarget());
            Location targetLocation = _.GetItemActivatedTargetLocation();

            string className = oItem.GetLocalString("SCRIPT");

            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("ACTIVATE_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("ACTION_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("SCRIPT");
            }
            // Legacy events follow. We can't remove these because of backwards compatibility issues with existing items.
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("JAVA_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("ACTIVATE_JAVA_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("JAVA_ACTION_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                return;
            }

            user.ClearAllActions();

            if (user.IsBusy)
            {
                user.SendMessage("You are busy.");
                return;
            }

            // Remove "Item." prefix if it exists.
            if (className.StartsWith("Item."))
            {
                className = className.Substring(5);
            }
            IActionItem item = GetActionItemHandler(className);

            string invalidTargetMessage = item.IsValidTarget(user, oItem, target, targetLocation);

            if (!string.IsNullOrWhiteSpace(invalidTargetMessage))
            {
                user.SendMessage(invalidTargetMessage);
                return;
            }

            float maxDistance = item.MaxDistance(user, oItem, target, targetLocation);

            if (maxDistance > 0.0f)
            {
                if (target.IsValid &&
                    (_.GetDistanceBetween(user.Object, target.Object) > maxDistance ||
                     user.Area.Resref != target.Area.Resref))
                {
                    user.SendMessage("Your target is too far away.");
                    return;
                }
                else if (!target.IsValid &&
                         (_.GetDistanceBetweenLocations(user.Location, targetLocation) > maxDistance ||
                          user.Area.Resref != ((NWArea)_.GetAreaFromLocation(targetLocation)).Resref))
                {
                    user.SendMessage("That location is too far away.");
                    return;
                }
            }

            CustomData customData   = item.StartUseItem(user, oItem, target, targetLocation);
            float      delay        = item.Seconds(user, oItem, target, targetLocation, customData);
            int        animationID  = item.AnimationID();
            bool       faceTarget   = item.FaceTarget();
            Vector     userPosition = user.Position;

            user.AssignCommand(() =>
            {
                user.IsBusy = true;
                if (faceTarget)
                {
                    _.SetFacingPoint(!target.IsValid ? _.GetPositionFromLocation(targetLocation) : target.Position);
                }
                if (animationID > 0)
                {
                    _.ActionPlayAnimation(animationID, 1.0f, delay);
                }
            });

            NWNXPlayer.StartGuiTimingBar(user, delay, string.Empty);

            var @event = new OnFinishActionItem(className, user, oItem, target, targetLocation, userPosition, customData);

            user.DelayEvent(delay, @event);
        }
Пример #4
0
        private static void OnItemUsed()
        {
            NWPlayer user            = _.OBJECT_SELF;
            NWItem   oItem           = _.StringToObject(NWNXEvents.GetEventData("ITEM_OBJECT_ID"));
            NWObject target          = _.StringToObject(NWNXEvents.GetEventData("TARGET_OBJECT_ID"));
            var      targetPositionX = (float)Convert.ToDouble(NWNXEvents.GetEventData("TARGET_POSITION_X"));
            var      targetPositionY = (float)Convert.ToDouble(NWNXEvents.GetEventData("TARGET_POSITION_Y"));
            var      targetPositionZ = (float)Convert.ToDouble(NWNXEvents.GetEventData("TARGET_POSITION_Z"));
            var      targetPosition  = Vector3(targetPositionX, targetPositionY, targetPositionZ);
            Location targetLocation  = Location(user.Area, targetPosition, 0.0f);

            string className = oItem.GetLocalString("SCRIPT");

            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("ACTIVATE_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("ACTION_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("SCRIPT");
            }
            // Legacy events follow. We can't remove these because of backwards compatibility issues with existing items.
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("JAVA_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("ACTIVATE_JAVA_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                className = oItem.GetLocalString("JAVA_ACTION_SCRIPT");
            }
            if (string.IsNullOrWhiteSpace(className))
            {
                return;
            }

            // Bypass the NWN "item use" animation.
            NWNXEvents.SkipEvent();

            user.ClearAllActions();

            if (user.IsBusy)
            {
                user.SendMessage("You are busy.");
                return;
            }

            // Remove "Item." prefix if it exists.
            if (className.StartsWith("Item."))
            {
                className = className.Substring(5);
            }
            IActionItem item = GetActionItemHandler(className);

            string invalidTargetMessage = item.IsValidTarget(user, oItem, target, targetLocation);

            if (!string.IsNullOrWhiteSpace(invalidTargetMessage))
            {
                user.SendMessage(invalidTargetMessage);
                return;
            }

            // NOTE - these checks are duplicated in FinishActionItem.  Keep both in sync.
            float maxDistance = item.MaxDistance(user, oItem, target, targetLocation);

            if (maxDistance > 0.0f)
            {
                NWObject owner = GetItemPossessor(target);

                if (target.IsValid && owner.IsValid)
                {
                    // We are okay - we have targeted an item in our inventory (we can't target someone
                    // else's inventory, so no need to actually check distance).
                }
                else if (target.Object == _.OBJECT_SELF)
                {
                    // Also okay.
                }
                else if (target.IsValid &&
                         (GetDistanceBetween(user.Object, target.Object) > maxDistance ||
                          user.Area.Resref != target.Area.Resref))
                {
                    user.SendMessage("Your target is too far away.");
                    return;
                }
                else if (!target.IsValid &&
                         (GetDistanceBetweenLocations(user.Location, targetLocation) > maxDistance ||
                          user.Area.Resref != ((NWArea)GetAreaFromLocation(targetLocation)).Resref))
                {
                    user.SendMessage("That location is too far away.");
                    return;
                }
            }

            CustomData customData   = item.StartUseItem(user, oItem, target, targetLocation);
            float      delay        = item.Seconds(user, oItem, target, targetLocation, customData);
            var        animationID  = item.AnimationID();
            bool       faceTarget   = item.FaceTarget();
            Vector3    userPosition = user.Position;

            user.AssignCommand(() =>
            {
                user.IsBusy = true;
                if (faceTarget)
                {
                    SetFacingPoint(!target.IsValid ? GetPositionFromLocation(targetLocation) : target.Position);
                }
                if (animationID > 0)
                {
                    ActionPlayAnimation(animationID, 1.0f, delay);
                }
            });

            if (delay > 0.0f)
            {
                NWNXPlayer.StartGuiTimingBar(user, delay, string.Empty);
            }

            var @event = new OnFinishActionItem(className, user, oItem, target, targetLocation, userPosition, customData);

            user.DelayEvent(delay, @event);
        }