예제 #1
0
        static int PlayerAiMove(Aggregator all, Guid playerId)
        {
            int longSleep  = 2;
            int shortSleep = 1;

            Client myClient = all.myClient;

            if (!all.playerAgents.ContainsKey(playerId))
            {
                return(longSleep);
            }

            MyAssert.Assert(myClient.myPlayerAgents.Contains(playerId));

            PlayerAgent pa         = all.playerAgents.GetValue(playerId);
            PlayerData  playerData = pa.Data;

            if (playerData == null)
            {
                return(longSleep);
            }

            if (!playerData.IsConnected)
            {
                //if (myClient.knownWorlds.ContainsKey(new Point(0, 0)))
                pa.Spawn();

                return(longSleep);
            }

            World playerWorld = myClient.worlds.TryGetWorld(playerData.WorldPosition);

            if (playerWorld == null)
            {
                return(longSleep);
            }

            if (playerData.inventory.teleport > 0 && rand.NextDouble() < .1)
            {
                Point teleportRange = Point.Zero; // internal teleport

                if (rand.NextDouble() < .1)
                {
                    teleportRange = Point.One; // external teleport
                }
                var teleportPos = (from p in Point.SymmetricRange(teleportRange)
                                   let w = myClient.worlds.TryGetWorld(p + playerWorld.Position)
                                           where w != null
                                           from t in w.GetAllTiles()
                                           where t.IsMoveable()
                                           select WorldTools.Shift(p, t.Position)).ToList();

                if (teleportPos.Any())
                {
                    Point newPos = teleportPos[rand.Next(0, teleportPos.Count)];
                    pa.Move(playerWorld.Info, newPos, ActionValidity.REMOTE);

                    return(shortSleep);
                }
            }

            PlayerMove move = null;

            for (int i = 0; i < 5; ++i)
            {
                move = PlayerRandomMove(playerWorld, playerId, all);
                if (move != null)
                {
                    break;
                }
            }

            if (move != null)
            {
                if (move.mv == ActionValidity.VALID || move.mv == ActionValidity.BOUNDARY)
                {
                    pa.Move(playerWorld.Info, move.newPos, move.mv);
                }
                else
                {
                    throw new Exception(Log.StDump(move.mv, move.newPos, "unexpected move"));
                }
            }

            return(shortSleep);
        }