Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void OnModuleActivatedItem()
        {
            NWPlayer oPC            = NWPlayer.Wrap(_.GetItemActivator());
            NWItem   oItem          = NWItem.Wrap(_.GetItemActivated());
            NWObject oTarget        = NWObject.Wrap(_.GetItemActivatedTarget());
            Location targetLocation = _.GetItemActivatedTargetLocation();

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

            if (className == string.Empty)
            {
                className = oItem.GetLocalString("ACTIVATE_JAVA_SCRIPT");
            }
            if (className == string.Empty)
            {
                className = oItem.GetLocalString("JAVA_ACTION_SCRIPT");
            }
            if (className == string.Empty)
            {
                className = oItem.GetLocalString("SCRIPT");
            }
            if (className == string.Empty)
            {
                return;
            }

            oPC.ClearAllActions();

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

            IActionItem item = App.ResolveByInterface <IActionItem>("Item." + className);

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

            string invalidTargetMessage = item.IsValidTarget(oPC, oItem, oTarget, targetLocation);

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

            if (item.MaxDistance() > 0.0f)
            {
                if (_.GetDistanceBetween(oPC.Object, oTarget.Object) > item.MaxDistance() ||
                    oPC.Area.Resref != oTarget.Area.Resref)
                {
                    oPC.SendMessage("Your target is too far away.");
                    return;
                }
            }

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

            oPC.AssignCommand(() =>
            {
                oPC.IsBusy = true;
                if (faceTarget)
                {
                    _.SetFacingPoint(oTarget.Position);
                }
                if (animationID > 0)
                {
                    _.ActionPlayAnimation(animationID, 1.0f, delay);
                }
            });

            _nwnxPlayer.StartGuiTimingBar(oPC, delay, string.Empty);
            oPC.DelayCommand(() =>
            {
                FinishActionItem(item, oPC, oItem, oTarget, targetLocation, userPosition, customData);
            }, delay);
        }