Example #1
0
        public override VMPrimitiveExitCode Execute(VMStackFrame context, VMPrimitiveOperand args)
        {
            var operand = (VMSnapOperand)args;
            var avatar = context.Caller; //todo, can sometimes be an object?? see roaches object tile movement, snaps to its own routing slot
            var obj = context.StackObject;

            SLOTItem slot = null;

            switch (operand.Mode)
            {
                case VMSnapSlotScope.StackVariable:
                    slot = VMMemory.GetSlot(context, VMSlotScope.StackVariable, operand.Index);
                    break;
                case VMSnapSlotScope.BeContained:
                    return (context.StackObject.PlaceInSlot(context.Caller, 0, true, context.VM.Context)) ? VMPrimitiveExitCode.GOTO_TRUE:VMPrimitiveExitCode.GOTO_FALSE;
                case VMSnapSlotScope.InFront:
                    slot = new SLOTItem { Type = 3, Standing = 1, MinProximity = 16, Rsflags = SLOTFlags.NORTH };
                    break;
                case VMSnapSlotScope.Literal:
                    slot = VMMemory.GetSlot(context, VMSlotScope.Literal, operand.Index);
                    break;
                case VMSnapSlotScope.Global:
                    slot = VMMemory.GetSlot(context, VMSlotScope.Global, operand.Index);
                    break;
            }

            if (operand.Mode != VMSnapSlotScope.BeContained)
            {
                var parser = new VMSlotParser(slot);
                var locations = parser.FindAvaliableLocations(obj, context.VM.Context, avatar);
                if (slot.SnapTargetSlot > -1)
                {
                    if (!context.StackObject.PlaceInSlot(context.Caller, slot.SnapTargetSlot, true, context.VM.Context)) return VMPrimitiveExitCode.GOTO_FALSE;
                    if (locations.Count > 0) avatar.RadianDirection = ((slot.Rsflags & SLOTFlags.SnapToDirection) > 0) ? locations[0].RadianDirection: avatar.RadianDirection;
                }
                else
                {
                    if (locations.Count > 0)
                    {
                        if (!SetPosition(avatar, locations[0].Position,
                            ((slot.Rsflags & SLOTFlags.SnapToDirection) > 0) ? locations[0].RadianDirection : avatar.RadianDirection,
                            context.VM.Context))
                            return VMPrimitiveExitCode.GOTO_FALSE;
                    }
                    else
                    {
                        avatar.SetValue(VMStackObjectVariable.PrimitiveResultID, (parser.Blocker == null) ? (short)0 : parser.Blocker.ObjectID);
                        return VMPrimitiveExitCode.GOTO_FALSE;
                    }
                }
            }
            return VMPrimitiveExitCode.GOTO_TRUE;
        }
Example #2
0
        public VMPrimitiveExitCode Tick()
        {
            var avatar = (VMAvatar)Caller;
            avatar.Velocity = new Vector3(0, 0, 0);

            if (State != VMRoutingFrameState.FAILED && avatar.GetFlag(VMEntityFlags.InteractionCanceled) && avatar.GetPersonData(VMPersonDataVariable.NonInterruptable) == 0)
            {
                HardFail(VMRouteFailCode.Interrupted, null);
                return VMPrimitiveExitCode.CONTINUE;
            }

            if (WaitTime > 0)
            {
                if (Velocity > 0) Velocity--;

                if (avatar.Animations.Count < 3) StartWalkAnimation();
                avatar.Animations[0].Weight = (8 - Velocity) / 8f;
                avatar.Animations[1].Weight = (Velocity / 8f) * 0.66f;
                avatar.Animations[2].Weight = (Velocity / 8f) * 0.33f;

                WaitTime--;
                Timeout--;
                if (Timeout <= 0)
                {
                    //try again. not sure if we should reset timeout for the new route
                    SoftFail(VMRouteFailCode.NoPath, null);
                    if (State != VMRoutingFrameState.FAILED) {
                        Velocity = 0;
                        State = VMRoutingFrameState.WALKING;
                    }
                } else return VMPrimitiveExitCode.CONTINUE_NEXT_TICK;
            }

            if (RoomRouteInvalid && State != VMRoutingFrameState.BEGIN_TURN && State != VMRoutingFrameState.END_TURN)
            {
                RoomRouteInvalid = false;
                IgnoredRooms.Clear();

                WalkTo = null; //reset routing state
                if (!DoRoomRoute(CurRoute))
                {
                    if (CurRoute != null) SoftFail(VMRouteFailCode.NoRoomRoute, null);
                    else HardFail(VMRouteFailCode.NoRoomRoute, null);
                }
                else if (Rooms.Count > 0)
                {
                    State = VMRoutingFrameState.INITIAL;
                }
            }

            switch (State)
            {
                case VMRoutingFrameState.STAND_FUNC:
                    if (Thread.LastStackExitCode == VMPrimitiveExitCode.RETURN_TRUE)
                    {
                        State = VMRoutingFrameState.INITIAL;
                        if (avatar.GetPersonData(VMPersonDataVariable.Posture) == 1) avatar.SetPersonData(VMPersonDataVariable.Posture, 0);
                    }
                    else
                        SoftFail(VMRouteFailCode.CantStand, null);
                    return VMPrimitiveExitCode.CONTINUE;
                case VMRoutingFrameState.INITIAL:
                case VMRoutingFrameState.ROOM_PORTAL:
                    //check if the room portal that just finished succeeded.
                    if (State == VMRoutingFrameState.ROOM_PORTAL) {
                        if (Thread.LastStackExitCode != VMPrimitiveExitCode.RETURN_TRUE)
                        {
                            IgnoredRooms.Add(CurrentPortal);
                            State = VMRoutingFrameState.INITIAL;
                            if (!DoRoomRoute(CurRoute))
                            {
                                SoftFail(VMRouteFailCode.NoRoomRoute, null); //todo: reattempt room route with portal we tried removed.
                                return VMPrimitiveExitCode.CONTINUE;
                            }
                        }
                    }

                    if (Rooms.Count > 0)
                    { //push portal function of next portal
                        CurrentPortal = Rooms.Pop();
                        var ent = VM.GetObjectById(CurrentPortal.ObjectID);
                        State = VMRoutingFrameState.ROOM_PORTAL;
                        if (!PushEntryPoint(15, ent)) //15 is portal function
                            SoftFail(VMRouteFailCode.NoRoomRoute, null); //could not execute portal function
                        return VMPrimitiveExitCode.CONTINUE;
                    }

                    //if we're here, room route is OK. start routing to a destination.
                    if (Choices == null)
                    {
                        //perform slot parse.
                        if (Slot == null)
                        {
                            HardFail(VMRouteFailCode.Unknown, null);
                            return VMPrimitiveExitCode.CONTINUE; //this should never happen. If it does, someone has used the routing system incorrectly.
                        }

                        var parser = new VMSlotParser(Slot);

                        Choices = parser.FindAvaliableLocations(Target, VM.Context, avatar);
                        if (Choices.Count == 0)
                        {
                            HardFail(parser.FailCode, parser.Blocker);
                            return VMPrimitiveExitCode.CONTINUE;
                        }
                        else
                        {
                            CurRoute = Choices[0];
                            Choices.RemoveAt(0);
                        }
                    }

                    //do we need to sit in a seat? it should take over.
                    if (CurRoute.Chair != null)
                    {
                        if (!AttemptedChair)
                        {
                            AttemptedChair = true;
                            if (PushEntryPoint(26, CurRoute.Chair)) return VMPrimitiveExitCode.CONTINUE;
                            else
                            {
                                SoftFail(VMRouteFailCode.CantSit, null);
                                return VMPrimitiveExitCode.CONTINUE;
                            }
                        }
                        else
                        {
                            if (Thread.LastStackExitCode == VMPrimitiveExitCode.RETURN_TRUE) return VMPrimitiveExitCode.RETURN_TRUE;
                            else
                            {
                                SoftFail(VMRouteFailCode.CantSit, null);
                                return VMPrimitiveExitCode.CONTINUE;
                            }
                        }
                    }

                    //If we are sitting, and the target is not this seat we need to call the stand function on the object we are contained within.
                    if (avatar.GetPersonData(VMPersonDataVariable.Posture) == 1)
                    {
                        State = VMRoutingFrameState.STAND_FUNC;
                        //push it onto our stack, except now the portal owns our soul! when we are returned to we can evaluate the result and determine if the route failed.
                        var chair = Caller.Container;

                        if (chair == null) avatar.SetPersonData(VMPersonDataVariable.Posture, 0); //we're sitting, but are not bound to a chair. Just instantly get up..
                        else
                        {
                            if (!PushEntryPoint(27, chair)) //27 is stand. TODO: set up an enum for these
                                HardFail(VMRouteFailCode.CantStand, null);
                            return VMPrimitiveExitCode.CONTINUE;
                        }
                    }

                    //no chair, we just need to walk to the spot. Start the within-room routing.
                    if (WalkTo == null)
                    {
                        if (!AttemptWalk())
                        {
                            SoftFail(VMRouteFailCode.NoPath, null);
                            return VMPrimitiveExitCode.CONTINUE;
                        }
                    }
                    if (State != VMRoutingFrameState.TURN_ONLY) BeginWalk();

                    return VMPrimitiveExitCode.CONTINUE;
                case VMRoutingFrameState.FAILED:
                    return VMPrimitiveExitCode.RETURN_FALSE;
                case VMRoutingFrameState.TURN_ONLY:
                    return (EndWalk()) ? VMPrimitiveExitCode.RETURN_TRUE : VMPrimitiveExitCode.CONTINUE;
                case VMRoutingFrameState.SHOOED:
                    StartWalkAnimation();
                    State = VMRoutingFrameState.WALKING;
                    return VMPrimitiveExitCode.CONTINUE;
                case VMRoutingFrameState.BEGIN_TURN:
                case VMRoutingFrameState.END_TURN:
                    if (avatar.CurrentAnimationState.EndReached)
                    {
                        if (State == VMRoutingFrameState.BEGIN_TURN)
                        {
                            State = VMRoutingFrameState.WALKING;
                            WalkDirection = TargetDirection;
                            avatar.RadianDirection = (float)TargetDirection;
                            StartWalkAnimation();
                            return VMPrimitiveExitCode.CONTINUE;
                        }
                        else
                        {
                            if (!CurRoute.FaceAnywhere) avatar.RadianDirection = CurRoute.RadianDirection;

                            //reset animation, so that we're facing the correct direction afterwards.
                            avatar.Animations.Clear();
                            var animation = FSO.Content.Content.Get().AvatarAnimations.Get(avatar.WalkAnimations[3] + ".anim");
                            var state = new VMAnimationState(animation, false);
                            state.Loop = true;
                            avatar.Animations.Add(state);

                            return VMPrimitiveExitCode.RETURN_TRUE; //we are here!
                        }
                    }
                    else
                    {
                        avatar.RadianDirection += TurnTweak; //while we're turning, adjust our direction
                        return VMPrimitiveExitCode.CONTINUE_NEXT_TICK;
                    }
                case VMRoutingFrameState.WALKING:
                    if (WalkTo == null)
                    {
                        if (!AttemptWalk())
                        {
                            SoftFail(VMRouteFailCode.NoPath, null);
                        }
                        return VMPrimitiveExitCode.CONTINUE;
                    }

                    if (WalkTo.Count == 0 && MoveTotalFrames - MoveFrames <= 28 && CanPortalTurn()) //7+6+5+4...
                    {
                        //tail off
                        if (Velocity <= 0) Velocity = 1;
                        if (Velocity > 1) Velocity--;
                    }
                    else
                    {
                        //get started
                        if (Velocity < 8) Velocity++;
                    }

                    avatar.Animations[0].Weight = (8 - Velocity) / 8f;
                    avatar.Animations[1].Weight = (Velocity / 8f) * 0.66f;
                    avatar.Animations[2].Weight = (Velocity / 8f) * 0.33f;

                    MoveFrames += Velocity;
                    if (MoveFrames >= MoveTotalFrames)
                    {
                        MoveFrames = MoveTotalFrames;
                        //move us to the final spot, then attempt an advance.
                    }
                    //normal sims can move 0.05 units in a frame.

                    if (TurnFrames > 0)
                    {
                        avatar.RadianDirection = (float)(TargetDirection + DirectionUtils.Difference(TargetDirection, WalkDirection) * (TurnFrames / 10.0));
                        TurnFrames--;
                    }
                    else avatar.RadianDirection = (float)TargetDirection;

                    var diff = CurrentWaypoint - PreviousPosition;
                    diff.x = (short)((diff.x * MoveFrames) / MoveTotalFrames);
                    diff.y = (short)((diff.y * MoveFrames) / MoveTotalFrames);

                    var storedDir = avatar.RadianDirection;
                    var result = Caller.SetPosition(PreviousPosition + diff, Direction.NORTH, VM.Context);
                    avatar.RadianDirection = storedDir;
                    if (result.Status != VMPlacementError.Success && result.Status != VMPlacementError.CantBeThroughWall)
                    {
                        //route failure, either something changed or we hit an avatar
                        //on both cases try again, but if we hit an avatar then detect if they have a routing frame and stop us for a bit.
                        //we stop the first collider because in most cases they're walking across our walk direction and the problem will go away after a second once they're past.
                        //
                        //if we need to route around a stopped avatar we add them to our "avatars to consider" set.

                        bool routeAround = true;
                        VMRoutingFrame colRoute = null;

                        if (result.Object != null && result.Object is VMAvatar)
                        {
                            var colAvatar = (VMAvatar)result.Object;
                            var colTopFrame = colAvatar.Thread.Stack.LastOrDefault();

                            //we already attempted to move around this avatar... if this happens too much give up.
                            if (AvatarsToConsider.Contains(colAvatar) && --Retries <= 0)
                            {
                                SoftFail(VMRouteFailCode.NoPath, avatar);
                                return VMPrimitiveExitCode.CONTINUE;
                            }

                            if (colTopFrame != null && colTopFrame is VMRoutingFrame)
                            {
                                colRoute = (VMRoutingFrame)colTopFrame;
                                routeAround = (colRoute.WaitTime > 0);
                            }
                            if (routeAround) AvatarsToConsider.Add(colAvatar);
                        }

                        if (result.Object == null || result.Object is VMGameObject)
                        {
                            //this should not happen often. An object or other feature has blocked our path due to some change in its position.
                            //repeated occurances indicate that we are stuck in something.
                            //todo: is this safe for the robot lot?
                            if (--Retries <= 0)
                            {
                                SoftFail(VMRouteFailCode.NoPath, avatar);
                                return VMPrimitiveExitCode.CONTINUE;
                            }
                        }

                        if (routeAround)
                        {
                            var oldWalk = new LinkedList<Point>(WalkTo);
                            if (AttemptWalk()) return VMPrimitiveExitCode.CONTINUE;
                            else
                            {
                                //failed to walk around the object
                                if (result.Object is VMAvatar)
                                {
                                    WalkTo = oldWalk;
                                    //if they're a person, shoo them away.
                                    //if they're in a routing frame we can push the tree directly onto their stack
                                    //otherwise we push an interaction and just hope they move (todo: does tso do this?)

                                    //DO NOT push tree if:
                                    // - sim is already being shooed. (the parent of the top routing frame is in state "SHOOED" or shoo interaction is present)
                                    // - we are being shooed.
                                    // - sim is waiting on someone they just shooed. (presumably can move out of our way once they finish waiting)
                                    //instead just wait a small duration and try again later.

                                    //cases where we cannot continue moving increase the retry count. if this is greater than RETRY_COUNT then we fail.
                                    //not sure how the original game works.

                                    if (CanShooAvatar((VMAvatar)result.Object))
                                    {
                                        AvatarsToConsider.Remove((VMAvatar)result.Object);
                                        VMEntity callee = VM.Context.CreateObjectInstance(GOTO_GUID, new LotTilePos(result.Object.Position), Direction.NORTH).Objects[0];
                                        if (colRoute != null)
                                        {
                                            colRoute.State = VMRoutingFrameState.SHOOED;
                                            colRoute.WalkTo = null;
                                            colRoute.AvatarsToConsider.Add(avatar); //just to make sure they don't try route through us.

                                            var tree = callee.GetBHAVWithOwner(SHOO_TREE, VM.Context);
                                            result.Object.Thread.ExecuteSubRoutine(colRoute, tree.bhav, tree.owner, new VMSubRoutineOperand());

                                            WalkInterrupt(60);
                                        }
                                        else
                                        {
                                            callee.PushUserInteraction(SHOO_INTERACTION, result.Object, VM.Context);
                                            WalkInterrupt(60);
                                        }
                                    }
                                    else
                                    {
                                        WalkInterrupt(60); //wait for a little while, they're moving out of the way.
                                    }
                                    return VMPrimitiveExitCode.CONTINUE;
                                }
                                SoftFail(VMRouteFailCode.DestTileOccupied, result.Object);
                                return VMPrimitiveExitCode.CONTINUE;
                            }
                        }
                        else
                        {
                            WalkInterrupt(30);
                            return VMPrimitiveExitCode.CONTINUE;
                        }

                    }
                    Caller.VisualPosition = Vector3.Lerp(PreviousPosition.ToVector3(), CurrentWaypoint.ToVector3(), MoveFrames / (float)MoveTotalFrames);

                    var velocity = Vector3.Lerp(PreviousPosition.ToVector3(), CurrentWaypoint.ToVector3(), Velocity / (float)MoveTotalFrames) - PreviousPosition.ToVector3();
                    velocity.Z = 0;
                    avatar.Velocity = velocity;

                    if (MoveTotalFrames == MoveFrames)
                    {
                        MoveTotalFrames = 0;
                        while (MoveTotalFrames == 0)
                        {
                            var remains = AdvanceWaypoint();
                            if (!remains)
                            {
                                MoveTotalFrames = -1;
                                if (EndWalk()) return VMPrimitiveExitCode.RETURN_TRUE;
                            }
                        }
                    }
                return VMPrimitiveExitCode.CONTINUE_NEXT_TICK;
            }
            return VMPrimitiveExitCode.GOTO_FALSE; //???
        }