Beispiel #1
0
        public static void RunTest(Client client)
        {
            Messenger.PlayerMsg(client, client.IP.ToString(), Text.BrightRed);


            return;

            int           playerCount = 0;
            PacketHitList hitlist     = null;

            PacketHitList.MethodStart(ref hitlist);
            IMap map = client.Player.Map;

            Stopwatch watch = new Stopwatch();

            watch.Start();
            using (DatabaseConnection db = new DatabaseConnection(DatabaseID.Players)) {
                PlayerManager.RetrieveCharacterID(db, "luigiDUDE");
            }
            //Messenger.PlayerWarp(client, map.MapID, 0, 25);
            watch.Stop();
            PacketHitList.MethodEnded(ref hitlist);
            Messenger.PlayerMsg(client, "Time: " + watch.ElapsedMilliseconds + "\nPlayer Count: " + playerCount, Text.BrightGreen);
            Messenger.PlayerMsg(client, "RNG: " + Server.Math.Rand(0, 1000), Text.BrightGreen);
            Messenger.PlayerMsg(client, "Tick Count: " + Server.Core.GetTickCount().Tick, Text.BrightGreen);
            //exPlayer.Get(client).EvolutionActive = !exPlayer.Get(client).EvolutionActive;
            //Messenger.PlayerMsg(client, "Evolution active: " + exPlayer.Get(client).EvolutionActive, Text.Yellow);
            //BossBattles.StartBossBattle(client, "CliffsideRelic");


            //map.SetTile(9, 3, 3, 0, 4, 3);
            //map.SetAttribute(9, 3, Server.Enums.TileType.Scripted, 46, 0, 0, "2", "1015", "25:25");
            //Messenger.SendTile(9, 3, map);
        }
Beispiel #2
0
        public static bool VerifyKeyTile(Client client, IMap map, int x, int y, PacketHitList packetList)
        {
            PacketHitList.MethodStart(ref packetList);
            bool unlocked = false;

            Tile mapTile = map.Tile[x, y];

            if (mapTile.Type == Server.Enums.TileType.Scripted)
            {
                if (mapTile.Data1 == 58)
                {
                    int      chargeLevel = mapTile.String1.ToInt();
                    exPlayer exPlayer    = exPlayer.Get(client);

                    if ((exPlayer.ElectrolockLevel == chargeLevel && exPlayer.ElectrolockCharge >= ChargesNeededForLevel(chargeLevel) && exPlayer.ElectrolockSublevel.Count >= SublevelsNeededForLevel(chargeLevel)) ||
                        exPlayer.ElectrolockLevel > chargeLevel)
                    {
                        unlocked = true;
                        DisplayInvisibleKeyTile(client, map, x, y, packetList);
                    }
                    else
                    {
                        DisplayVisibleKeyTile(client, map, x, y, packetList);
                    }
                }
            }
            PacketHitList.MethodEnded(ref packetList);
            return(unlocked);
        }
Beispiel #3
0
        public static void VerifyMapSublevelTriggerTiles(Client client, IMap map, PacketHitList packetList)
        {
            PacketHitList.MethodStart(ref packetList);
            bool unlocked = false;

            for (int x = 0; x <= map.MaxX; x++)
            {
                for (int y = 0; y <= map.MaxY; y++)
                {
                    VerifySublevelTriggerTile(client, map, x, y, packetList);
                }
            }
            PacketHitList.MethodEnded(ref packetList);
        }
Beispiel #4
0
        public override void DeconfigurePlayer(Client client)
        {
            base.DeconfigurePlayer(client);

            client.Player.KillableAnywhere = false;
            client.Player.EndTempStatMode();

            PacketHitList packetHitList = null;

            PacketHitList.MethodStart(ref packetHitList);

            Main.RefreshCharacterSpeedLimit(client.Player.GetActiveRecruit(), client.Player.Map, packetHitList);

            PacketHitList.MethodEnded(ref packetHitList);
        }
Beispiel #5
0
        public static void CreateNewParty(Client leader)
        {
            PacketHitList hitlist = null;

            PacketHitList.MethodStart(ref hitlist);
            List <string> partyIDs = new List <string>();

            using (DatabaseConnection dbConnection = new DatabaseConnection(DatabaseID.Players))
            {
                partyIDs = LoadPartyIDList(dbConnection.Database);
            }

            if (leader.Player.PartyID == null)
            {
                string partyID = null;
                rwLock.EnterWriteLock();
                try
                {
                    while (true)
                    {
                        string tempID = Security.PasswordGen.Generate(10, 15);
                        if (partyIDs.Contains(tempID) == false)
                        {
                            partyID = tempID;
                            break;
                        }
                    }
                    Party party = new Party(partyID, leader);
                    leader.Player.PartyID = partyID;
                    parties.Add(partyID, party);
                }
                finally
                {
                    rwLock.ExitWriteLock();
                }
                leader.Player.AddExpKitModule(new AvailableExpKitModule(Enums.ExpKitModules.Party, true));
                PacketBuilder.AppendPartyMemberDataFor(leader, hitlist, 0);
                hitlist.AddPacket(leader, PacketBuilder.CreateChatMsg("You have created a new party!", Text.BrightGreen));
            }
            else
            {
                hitlist.AddPacket(leader, PacketBuilder.CreateChatMsg("You are already in a party!", Text.BrightRed));
            }

            PacketHitList.MethodEnded(ref hitlist);
        }
Beispiel #6
0
        public static void JoinParty(Party party, Client client)
        {
            PacketHitList hitlist = null;

            PacketHitList.MethodStart(ref hitlist);

            if (party != null)
            {
                Client leader = party.GetLeader();
                if (Combat.MoveProcessor.IsInAreaRange(1, client.Player.X, client.Player.Y, leader.Player.X, leader.Player.Y))
                {
                    if (party.AddToParty(client))
                    {
                        client.Player.PartyID = party.PartyID;
                        client.Player.AddExpKitModule(new AvailableExpKitModule(Enums.ExpKitModules.Party, true));
                        hitlist.AddPacket(client, PacketBuilder.CreateChatMsg("You have joined the party!", Text.BrightGreen));
                        //PartyMember member = party.FindMember(client.Player.CharID);
                        int slot = party.GetMemberSlot(client.Player.CharID);
                        //Messenger.SendPartyMemberData(client, member, slot);
                        foreach (Client i in party.GetOnlineMemberClients())
                        {
                            if (i.IsPlaying() && i != client)
                            {
                                //PartyMember teamMember = party.Members.FindMember(i.Player.CharID);
                                //int teamMemberSlot = party.Members.GetMemberSlot(i.Player.CharID);
                                //Messenger.SendPartyMemberData(client, teamMember, teamMemberSlot);
                                //Messenger.SendPartyMemberData(i, member, slot);
                                hitlist.AddPacket(i, PacketBuilder.CreateChatMsg(client.Player.Name + " has joined the party!", Text.BrightGreen));
                            }
                            PacketBuilder.AppendPartyMemberData(i, hitlist, slot);
                        }
                    }
                    else
                    {
                        hitlist.AddPacket(client, PacketBuilder.CreateChatMsg("You couldn't join the party!", Text.BrightRed));
                    }
                }
                else
                {
                    Messenger.PlayerMsg(client, "You need to stand next to the leader to join the party!", Text.BrightRed);
                }
            }

            PacketHitList.MethodEnded(ref hitlist);
        }
Beispiel #7
0
        public void CompleteRoom(Client client)
        {
            var playerData = Data.ExtendPlayer(client);

            if (playerData.CurrentRoom % CheckpointInterval == 0)
            {
                PacketHitList hitlist = null;

                PacketHitList.MethodStart(ref hitlist);
                Main.HealParty(hitlist, client);
                PacketHitList.MethodEnded(ref hitlist);
            }

            playerData.CurrentRoom++;
            playerData.RoomsCleared++;

            WarpToCurrentRoom(client);
        }
Beispiel #8
0
        public static void DisplayInvisibleSublevelGoalTile(Client client, IMap map, int x, int y, PacketHitList packetList)
        {
            PacketHitList.MethodStart(ref packetList);
            Tile tile = new Tile(new DataManager.Maps.Tile());

            MapCloner.CloneTile(map, x, y, tile);
            tile.Mask    = 0;
            tile.MaskSet = 7;
            tile.Type    = Enums.TileType.Walkable;
            Messenger.SendTemporaryTileTo(packetList, client, x, y, tile);

            MapCloner.CloneTile(map, x, y - 1, tile);
            tile.Fringe    = 0;
            tile.FringeSet = 7;
            tile.Type      = Enums.TileType.Walkable;
            Messenger.SendTemporaryTileTo(packetList, client, x, y - 1, tile);

            PacketHitList.MethodEnded(ref packetList);
        }
Beispiel #9
0
        public static bool VerifyMapKeyTiles(Client client, IMap map, PacketHitList packetList)
        {
            PacketHitList.MethodStart(ref packetList);
            bool unlocked = false;

            for (int x = 0; x <= map.MaxX; x++)
            {
                for (int y = 0; y <= map.MaxY; y++)
                {
                    bool state = VerifyKeyTile(client, map, x, y, packetList);
                    if (state)
                    {
                        unlocked = true;
                    }
                }
            }
            PacketHitList.MethodEnded(ref packetList);

            return(unlocked);
        }
Beispiel #10
0
        public static void HandleOutlawGameOver(Client client, ref PacketHitList hitList)
        {
            PacketHitList.MethodStart(ref hitList);

            if (client.Player.OutlawRole == Enums.OutlawRole.Outlaw)
            {
                var lostPoints   = client.Player.PlayerData.PendingOutlawPoints % OutlawPointInterval;
                var gainedPoints = client.Player.PlayerData.PendingOutlawPoints - lostPoints;

                client.Player.PlayerData.LockedOutlawPoints += gainedPoints;

                Messenger.PlayerMsg(client, $"You have been defeated! You gained {gainedPoints} points from this round!", Text.BrightGreen);

                Messaging.SendGlobalMessage($"{client.Player.DisplayName} has been captured!", Settings.DiscordOutlawChannel, Text.BrightGreen);
            }

            client.Player.OutlawRole = Enums.OutlawRole.None;
            client.Player.PlayerData.PendingOutlawPoints = 0;

            PacketBuilder.AppendPlayerData(client, hitList);

            PacketHitList.MethodEnded(ref hitList);
        }
Beispiel #11
0
        public static void VerifySublevelTriggerTile(Client client, IMap map, int x, int y, PacketHitList packetList)
        {
            PacketHitList.MethodStart(ref packetList);

            Tile mapTile = map.Tile[x, y];

            if (mapTile.Type == Server.Enums.TileType.Scripted)
            {
                if (mapTile.Data1 == 59)
                {
                    string   triggerID = mapTile.String1;
                    exPlayer exPlayer  = exPlayer.Get(client);
                    if (exPlayer.ElectrolockSublevelTriggersActive.Contains(triggerID) == false)
                    {
                        DisplayVisibleSublevelTriggerTile(client, map, x, y, packetList);
                    }
                    else
                    {
                        DisplayInvisibleSublevelTriggerTile(client, map, x, y, packetList);
                    }
                }
            }
            PacketHitList.MethodEnded(ref packetList);
        }
Beispiel #12
0
        public static void OnMapLoaded(Client client, IMap map, PacketHitList packetList)
        {
            PacketHitList.MethodStart(ref packetList);
            if (IsInTower(map))
            {
                // Prevent others from entering until finished. Spoiler prevention!

                if (Ranks.IsDisallowed(client, Enums.Rank.Scripter))
                {
                    packetList.HitList.Clear(); // Remove the warp packets to cancel the map load
                    Messenger.PlayerWarp(client, 1015, 25, 25);
                    if (Ranks.IsAllowed(client, Enums.Rank.Moniter))
                    {
                        Messenger.PlayerMsg(client, "Sorry, you can't go here! ~Pikachu", Text.BrightRed);
                    }
                    else
                    {
                        Messenger.PlayerMsg(client, "Sorry, you can't go here!", Text.BrightRed);
                    }
                }
                else
                {
                    if (VerifyMapKeyTiles(client, map, packetList))
                    {
                        packetList.AddPacket(client, PacketBuilder.CreateBattleMsg("An electrolock has been opened nearby!", Text.BrightGreen));
                    }
                    CheckForSublevelGoal(client, packetList);
                    VerifyMapSublevelTriggerTiles(client, map, packetList);
                }
            }
            else
            {
                ResetPlayer(client);
            }
            PacketHitList.MethodEnded(ref packetList);
        }
Beispiel #13
0
        public void HandoutExp(Client giver, ulong exp, PacketHitList hitlist)
        {
            PacketHitList.MethodStart(ref hitlist);
            bool  totalForAll = true;
            ulong newExp;

            if (totalForAll)
            {
                newExp = exp;
            }
            else
            {
                newExp = exp * (ulong)(Members.Count + 3) / 4 / (ulong)Members.Count;
            }


            if (!(giver.Player.GetActiveRecruit().Level == Exp.ExpManager.Exp.MaxLevels) && giver.Player.Hunted && !giver.Player.Dead)
            {
                Scripting.ScriptManager.InvokeSub("PlayerEXP", hitlist, giver, newExp);

                Combat.BattleProcessor.CheckPlayerLevelUp(hitlist, giver);
            }

            foreach (Client i in GetOnlineMemberClients())
            {
                if (i.IsPlaying() && i != giver)
                {
                    //PartyMember member = members.FindMember(i.Player.CharID);
                    //if (member != null) {
                    if (giver.Player.Map == i.Player.Map)
                    {
                        if (!(i.Player.GetActiveRecruit().Level == Exp.ExpManager.Exp.MaxLevels) && i.Player.Hunted && !i.Player.Dead)
                        {
                            int lowestBound  = 8000;
                            int chosenMiddle = 8000;
                            if (i.Player.GetActiveRecruit().Level < giver.Player.GetActiveRecruit().Level)
                            {
                                int diff = giver.Player.GetActiveRecruit().Level - i.Player.GetActiveRecruit().Level;
                                lowestBound -= diff * diff * 100 / 5;
                                int distance = Math.Max(Math.Abs(giver.Player.X - i.Player.X), Math.Abs(giver.Player.Y - i.Player.Y));

                                if (distance > 40)
                                {
                                    distance = 40;
                                }

                                if (distance < 8)
                                {
                                    distance = 8;
                                }

                                chosenMiddle = lowestBound * (distance - 8) / 40 + chosenMiddle * (40 - (distance - 8)) / 40;

                                if (chosenMiddle < 0)
                                {
                                    chosenMiddle = 0;
                                }
                            }

                            ulong moddedExp = newExp * (ulong)chosenMiddle / 8000;

                            if (moddedExp < 1)
                            {
                                moddedExp = 1;
                            }

                            Scripting.ScriptManager.InvokeSub("PlayerEXP", hitlist, i, moddedExp);

                            Combat.BattleProcessor.CheckPlayerLevelUp(hitlist, i);
                        }
                        else
                        {
                            // The party member is at the max level, so do nothing
                        }
                    }
                    //}
                }
            }

            PacketHitList.MethodEnded(ref hitlist);
        }
Beispiel #14
0
        public void ProcessAI()
        {
            try {
                TickCount tickCount = Core.GetTickCount();

                PacketHitList packetList = null;
                PacketHitList.MethodStart(ref packetList);
                if (map.IsSaving == false)
                {
                    // Keys/Other timed tile stuff
                    for (int x = 0; x <= map.MaxX; x++)
                    {
                        for (int y = 0; y <= map.MaxY; y++)
                        {
                            if (map.Tile[x, y] != null)
                            {
                                if (tickCount.Elapsed(map.Tile[x, y].DoorTimer, 5000))
                                {
                                    if ((map.Tile[x, y].Type == Enums.TileType.Key || map.Tile[x, y].Type == Enums.TileType.Door) && map.Tile[x, y].DoorOpen == true)
                                    {
                                        map.Tile[x, y].DoorOpen = false;
                                        packetList.AddPacketToMap(map, TcpPacket.CreatePacket("mapkey", x.ToString(), y.ToString(), "0"));
                                    }
                                }
                            }
                        }
                    }

                    Scripting.ScriptManager.InvokeSub("OnMapTick", map);

                    if (map.ProcessingPaused == false)
                    {
                        int livingNpcs = 0;

                        for (int mapNpcSlot = 0; mapNpcSlot < Constants.MAX_MAP_NPCS; mapNpcSlot++)
                        {
                            int    npcNum = map.ActiveNpc[mapNpcSlot].Num;
                            MapNpc mapNpc = map.ActiveNpc[mapNpcSlot];

                            if (npcNum > 0)
                            {
                                livingNpcs++;

                                Npc npc = NpcManager.Npcs[npcNum];

                                if (npc.Behavior != Enums.NpcBehavior.FullyScriptedAI || npc.AIScript == "" || npc.AIScript.ToLower() == "none")
                                {
                                    #region Used for attacking on sight
                                    if (npc.Behavior == Enums.NpcBehavior.AttackOnSight || npc.Behavior == Enums.NpcBehavior.Guard)
                                    {
                                        foreach (Client i in map.GetClients())
                                        {
                                            if (i.Player.MapID == map.MapID && map.ActiveNpc[mapNpcSlot].Target == null && !i.Player.Dead &&
                                                i.Player.Hunted && map.Tile[i.Player.X, i.Player.Y].Type != Enums.TileType.NPCAvoid && i.Player.Hunted)
                                            {
                                                if (MovementProcessor.CanCharacterSeeCharacter(map, map.ActiveNpc[mapNpcSlot], i.Player.GetActiveRecruit()))
                                                {
                                                    if (npc.Behavior == Enums.NpcBehavior.AttackOnSight /* || i.Player.PK == true*/)
                                                    {
                                                        //if (!string.IsNullOrEmpty(npc.AttackSay)) {
                                                        //    packetList.AddPacket(i, PacketBuilder.CreateChatMsg("A " + npc.Name + " : " + npc.AttackSay, Text.Grey));
                                                        //}

                                                        map.ActiveNpc[mapNpcSlot].Target = i;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    #endregion

                                    #region Used for walking/targetting/picking up items
                                    if (npcNum > 0)
                                    {
                                        bool hasNpcWalked;

                                        if (map.ActiveNpc[mapNpcSlot].Target != null && map.ActiveNpc[mapNpcSlot].Target.IsPlaying() &&
                                            map.ActiveNpc[mapNpcSlot].Target.Player.MapID == map.MapID &&
                                            (map.Tile[map.ActiveNpc[mapNpcSlot].Target.Player.X, map.ActiveNpc[mapNpcSlot].Target.Player.Y].Type == Enums.TileType.NPCAvoid ||
                                             !map.ActiveNpc[mapNpcSlot].Target.Player.Hunted || map.ActiveNpc[mapNpcSlot].Target.Player.Dead ||
                                             !MovementProcessor.CanCharacterSeeCharacter(map, map.ActiveNpc[mapNpcSlot], map.ActiveNpc[mapNpcSlot].Target.Player.GetActiveRecruit())))
                                        {
                                            map.ActiveNpc[mapNpcSlot].Target = null;
                                        }

                                        Client target = map.ActiveNpc[mapNpcSlot].Target;


                                        if (npc.Behavior != Enums.NpcBehavior.Shopkeeper)
                                        {
                                            if (target != null)
                                            {
                                                if (target.IsPlaying() && target.Player.MapID == map.MapID)
                                                {
                                                    hasNpcWalked = false;
                                                    int dir = Math.Rand(0, 5);

                                                    hasNpcWalked = AIProcessor.MoveNpcInDirection((Enums.Direction)dir, map, target, packetList, mapNpcSlot);
                                                    if (!hasNpcWalked)
                                                    {
                                                        foreach (Enums.Direction direction in Enum.GetValues(typeof(Enums.Direction)))
                                                        {
                                                            if (direction != (Enums.Direction)dir)
                                                            {
                                                                if (AIProcessor.MoveNpcInDirection(direction, map, target, packetList, mapNpcSlot))
                                                                {
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }

                                                    if (hasNpcWalked == false)
                                                    {
                                                        if (map.ActiveNpc[mapNpcSlot].X - 1 == target.Player.X && map.ActiveNpc[mapNpcSlot].Y == target.Player.Y)
                                                        {
                                                            if (map.ActiveNpc[mapNpcSlot].Direction != Enums.Direction.Left)
                                                            {
                                                                MovementProcessor.ChangeNpcDir(packetList, map, mapNpcSlot, Enums.Direction.Left);
                                                            }
                                                            hasNpcWalked = true;
                                                        }
                                                        if (map.ActiveNpc[mapNpcSlot].X + 1 == target.Player.X && map.ActiveNpc[mapNpcSlot].Y == target.Player.Y)
                                                        {
                                                            if (map.ActiveNpc[mapNpcSlot].Direction != Enums.Direction.Right)
                                                            {
                                                                MovementProcessor.ChangeNpcDir(packetList, map, mapNpcSlot, Enums.Direction.Right);
                                                            }
                                                            hasNpcWalked = true;
                                                        }
                                                        if (map.ActiveNpc[mapNpcSlot].X == target.Player.X && map.ActiveNpc[mapNpcSlot].Y - 1 == target.Player.Y)
                                                        {
                                                            if (map.ActiveNpc[mapNpcSlot].Direction != Enums.Direction.Up)
                                                            {
                                                                MovementProcessor.ChangeNpcDir(packetList, map, mapNpcSlot, Enums.Direction.Up);
                                                            }
                                                            hasNpcWalked = true;
                                                        }
                                                        if (map.ActiveNpc[mapNpcSlot].X == target.Player.X && map.ActiveNpc[mapNpcSlot].Y + 1 == target.Player.Y)
                                                        {
                                                            if (map.ActiveNpc[mapNpcSlot].Direction != Enums.Direction.Down)
                                                            {
                                                                MovementProcessor.ChangeNpcDir(packetList, map, mapNpcSlot, Enums.Direction.Down);
                                                            }
                                                            hasNpcWalked = true;
                                                        }
                                                    }

                                                    if (hasNpcWalked == false)
                                                    {
                                                        int val = Math.Rand(0, 2);
                                                        if (val == 1)
                                                        {
                                                            val = Math.Rand(0, 4);
                                                            if (MovementProcessor.CanNpcMove(map, mapNpcSlot, (Enums.Direction)val))
                                                            {
                                                                MovementProcessor.NpcMove(packetList, map, mapNpcSlot, map.ActiveNpc[mapNpcSlot].Direction, Enums.Speed.Walking);
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    map.ActiveNpc[mapNpcSlot].Target = null;
                                                }
                                            }
                                            else
                                            {
                                                int shouldWalk = Math.Rand(0, 2);
                                                if (npc.Behavior != Enums.NpcBehavior.Friendly)
                                                {
                                                    for (int i = 0; i < Constants.MAX_MAP_ITEMS; i++)
                                                    {
                                                        if (map.ActiveItem[i].X == map.ActiveNpc[mapNpcSlot].X && map.ActiveItem[i].Y == map.ActiveNpc[mapNpcSlot].Y &&
                                                            map.ActiveItem[i].Num > -1 && map.ActiveNpc[mapNpcSlot].HeldItem == null)
                                                        {
                                                            map.ActiveNpc[mapNpcSlot].MapGetItem();
                                                            shouldWalk = 1;
                                                        }
                                                    }
                                                }
                                                else if (npc.Behavior == Enums.NpcBehavior.Friendly)
                                                {
                                                    foreach (Client i in map.GetClients())
                                                    {
                                                        if (i.Player.X >= map.ActiveNpc[mapNpcSlot].X - 1 && i.Player.X <= map.ActiveNpc[mapNpcSlot].X + 1 &&
                                                            i.Player.Y >= map.ActiveNpc[mapNpcSlot].Y - 1 && i.Player.Y <= map.ActiveNpc[mapNpcSlot].Y + 1)
                                                        {
                                                            shouldWalk = Math.Rand(0, 10);
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (shouldWalk == 0)
                                                {
                                                    shouldWalk = Math.Rand(0, 5);
                                                    if (MovementProcessor.CanNpcMove(map, mapNpcSlot, (Enums.Direction)shouldWalk))
                                                    {
                                                        MovementProcessor.NpcMove(packetList, map, mapNpcSlot, map.ActiveNpc[mapNpcSlot].Direction, Enums.Speed.Walking);
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    #endregion

                                    #region Used for attacking players

                                    if (npcNum > 0)
                                    {
                                        Client target = map.ActiveNpc[mapNpcSlot].Target;
                                        if (target != null)
                                        {
                                            if (target.IsPlaying() && target.Player.MapID == map.MapID)
                                            {
                                                // Make sure npcs dont attack more then once a second
                                                if (Core.GetTickCount().Elapsed(map.ActiveNpc[mapNpcSlot].AttackTimer, 1000))
                                                {
                                                    int usableMoveCount = 0;
                                                    for (int i = 0; i < mapNpc.Moves.Length; i++)
                                                    {
                                                        if (mapNpc.Moves[i].MoveNum > -1 && mapNpc.Moves[i].CurrentPP > 0)
                                                        {
                                                            usableMoveCount++;
                                                        }
                                                    }
                                                    if (usableMoveCount == 0)
                                                    {
                                                        if (BattleProcessor.ShouldUseMove(map, mapNpc, -1))
                                                        {
                                                            mapNpc.UseMove(-1);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        // Try to use a move, up to 50 times
                                                        for (int i = 0; i < 50; i++)
                                                        {
                                                            int moveSlotToUse = Server.Math.Rand(0, 5) - 1;
                                                            if (moveSlotToUse == -1)
                                                            {
                                                                // A standard attack
                                                                if (BattleProcessor.ShouldUseMove(map, mapNpc, moveSlotToUse))
                                                                {
                                                                    mapNpc.UseMove(moveSlotToUse);
                                                                    break;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (mapNpc.Moves[moveSlotToUse].MoveNum > -1 && mapNpc.Moves[moveSlotToUse].CurrentPP > 0)
                                                                {
                                                                    // Use a move
                                                                    if (BattleProcessor.ShouldUseMove(map, mapNpc, moveSlotToUse))
                                                                    {
                                                                        mapNpc.UseMove(moveSlotToUse);
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                map.ActiveNpc[mapNpcSlot].Target = null;
                                            }
                                        }
                                    }

                                    #endregion

                                    if (npcNum > 0 && map.ActiveNpc[mapNpcSlot].HPChanged)
                                    {
                                        map.ActiveNpc[mapNpcSlot].SendHPToMap(packetList, map, mapNpcSlot);
                                    }
                                }
                                else
                                {
                                    // Scripted AI: Reimplement?
                                    //Globals.AIScriptManager.CallAIProcessSub(NpcManager.Npcs[map.Npc[x].NpcNum].AIScript, tickCount, map.MapNum, x);
                                }
                            }
                        }
                        //try and spawn something
                        if (map.NpcSpawnWait == null)
                        {
                            map.NpcSpawnWait = Core.GetTickCount();
                        }
                        if (Core.GetTickCount().Tick > map.NpcSpawnWait.Tick)
                        {
                            if (livingNpcs < map.MaxNpcs)
                            {
                                map.SpawnNpc(true);
                            }
                            map.NpcSpawnWait = new TickCount(map.NpcSpawnWait.Tick + map.NpcSpawnTime * 1000);
                        }
                    }
                }
                //lock (MapManager.ActiveMapLockObject) {
                bool shouldRemove = false;
                if (map.PlayersOnMap.Count == 0 && tickCount.Elapsed(map.ActivationTime, AIProcessor.MapTTL))
                {
                    if (map.IsProcessingComplete())
                    {
                        shouldRemove = true;

                        for (int i = 1; i < 9; i++)
                        {
                            IMap borderingMap = MapManager.RetrieveActiveBorderingMap(map, (Enums.MapID)i);
                            if (borderingMap != null)
                            {
                                if (borderingMap.PlayersOnMap.Count > 0)
                                {
                                    shouldRemove = false;
                                }
                            }
                        }

                        if (shouldRemove)
                        {
                            MapManager.rwLock.EnterWriteLock();
                            try {
                                if (map.MapType == Enums.MapType.House)
                                {
                                    // Only save the map here when logging out - otherwise, the map is saved when the player is saved
                                    map.Save();
                                }
                                if ((map.MapType == Enums.MapType.RDungeonMap || map.MapType == Enums.MapType.Instanced))
                                {
                                    using (Database.DatabaseConnection dbConnection = new Database.DatabaseConnection(Database.DatabaseID.Data)) {
                                        if (MapManager.GetTotalPlayersOnMap(dbConnection, map.MapID) == 0)
                                        {
                                            DataManager.Maps.MapDataManager.DeleteMap(dbConnection.Database, map.MapID);
                                        }
                                        else
                                        {
                                            map.Save();
                                        }
                                    }
                                }

                                MapManager.UnsafeRemoveActiveMap(map.MapID);
                            } finally {
                                MapManager.rwLock.ExitWriteLock();
                            }
                        }
                    }
                }
                //}

                PacketHitList.MethodEnded(ref packetList);
            } catch (Exception ex) {
                Server.Exceptions.ErrorLogger.WriteToErrorLog(ex, "MapAIProcessor");
            }
        }
Beispiel #15
0
        public void SpawnNpc(MapNpcPreset npc, bool checkSight)
        {
            int  NPCNum  = 0;
            int  X       = 0;
            int  Y       = 0;
            bool Spawned = false;

            // Check for empty NPC slot
            int npcSlot = FindOpenNpcSlot();

            if (npcSlot < 0 || npcSlot >= Constants.MAX_MAP_NPCS)
            {
                return;
            }

            NPCNum = npc.NpcNum;
            if (NPCNum > 0)
            {
                ActiveNpc[npcSlot].Num    = NPCNum;
                ActiveNpc[npcSlot].Target = null;

                ActiveNpc[npcSlot].Name = NpcManager.Npcs[NPCNum].Name;
                ActiveNpc[npcSlot].Form = NpcManager.Npcs[NPCNum].Form;

                //if (NpcManager.Npcs[NPCNum].ShinyChance != 0 && Server.Math.Rand(0, NpcManager.Npcs[NPCNum].ShinyChance) == 0) ActiveNpc[npcSlot].Shiny = true;
                ActiveNpc[npcSlot].Sex = Pokedex.Pokedex.GetPokemonForm(NpcManager.Npcs[NPCNum].Species, ActiveNpc[npcSlot].Form).GenerateLegalSex();

                ActiveNpc[npcSlot].AttackTimer = new TickCount(Core.GetTickCount().Tick);
                ActiveNpc[npcSlot].PauseTimer  = new TickCount(Core.GetTickCount().Tick);

                //if (Npc[npcSlot].MinLevel == -1) {
                //Npc[npcSlot].MinLevel = NpcManager.Npcs[Npc[npcSlot].NpcNum].RecruitLevel;
                if (npc.MinLevel <= 0)
                {
                    npc.MinLevel = 1;
                    npc.MaxLevel = 1;
                }// else {
                //        Console.WriteLine("Npc found!");
                //    }
                //}
                ActiveNpc[npcSlot].Level = Server.Math.Rand(npc.MinLevel, npc.MaxLevel + 1);
                //set initial stats
                ActiveNpc[npcSlot].CalculateOriginalSprite();
                ActiveNpc[npcSlot].CalculateOriginalStats();
                ActiveNpc[npcSlot].CalculateOriginalType();
                ActiveNpc[npcSlot].CalculateOriginalAbility();
                //ActiveNpc[npcSlot].CalculateOriginalMobility();


                ActiveNpc[npcSlot].HP = ActiveNpc[npcSlot].MaxHP;

                ActiveNpc[npcSlot].Direction = (Enums.Direction)Server.Math.Rand(0, 4);

                ActiveNpc[npcSlot].GenerateMoveset();

                if (Moral == Enums.MapMoral.None)
                {
                    ActiveNpc[npcSlot].GenerateHeldItem();
                }

                if (Server.Math.Rand(0, 100) < npc.StartStatusChance)
                {
                    ActiveNpc[npcSlot].StatusAilment        = npc.StartStatus;
                    ActiveNpc[npcSlot].StatusAilmentCounter = npc.StartStatusCounter;
                }

                if (npc.SpawnX < 0 | npc.SpawnY < 0)
                {
                    // We'll try 100 times to randomly place the sprite
                    for (int i = 1; i <= 50; i++)
                    {
                        if (Tile[X, Y].Type == Enums.TileType.Walkable || Tile[X, Y].Type == Enums.TileType.Slow)
                        {
                            if (checkSight)
                            {
                                bool seen = false;
                                foreach (Client client in GetClients())
                                {
                                    if (CanCharacterSeeDestination(client.Player.GetActiveRecruit(), X, Y))
                                    {
                                        seen = true;
                                        break;
                                    }
                                }
                                if (!seen)
                                {
                                    ActiveNpc[npcSlot].X = X;
                                    ActiveNpc[npcSlot].Y = Y;
                                    Spawned = true;
                                    break;
                                }
                            }
                            else
                            {
                                ActiveNpc[npcSlot].X = X;
                                ActiveNpc[npcSlot].Y = Y;
                                Spawned = true;
                                break;
                            }
                        }
                    }

                    for (int i = 1; i <= 50; i++)
                    {
                        X = Server.Math.Rand(0, MaxX + 1);
                        Y = Server.Math.Rand(0, MaxY + 1);

                        // Check if the tile is walkable
                        if (Tile[X, Y].Type == Enums.TileType.Walkable)
                        {
                            ActiveNpc[npcSlot].X = X;
                            ActiveNpc[npcSlot].Y = Y;
                            Spawned = true;
                            break;
                        }
                    }
                }
                else
                {
                    // We no longer subtract one because Rand is ListIndex -1.
                    ActiveNpc[npcSlot].X = npc.SpawnX;
                    ActiveNpc[npcSlot].Y = npc.SpawnY;
                    Spawned = true;
                }

                // Didn't spawn, so now we'll just try to find a free tile
                if (!Spawned)
                {
                    for (Y = 0; Y <= MaxY; Y++)
                    {
                        for (X = 0; X <= MaxX; X++)
                        {
                            if (Tile[X, Y].Type == Enums.TileType.Walkable)
                            {
                                ActiveNpc[npcSlot].X = X;
                                ActiveNpc[npcSlot].Y = Y;
                                Spawned = true;
                            }
                        }
                    }
                }

                // If we suceeded in spawning then send it to everyone
                if (Spawned)
                {
                    PacketHitList hitlist = null;
                    PacketHitList.MethodStart(ref hitlist);
                    PacketBuilder.AppendNpcSpawn(MapManager.RetrieveActiveMap(MapID), hitlist, npcSlot);

                    Scripting.ScriptManager.InvokeFunction("OnNpcSpawn", MapManager.RetrieveActiveMap(MapID), npc, ActiveNpc[npcSlot], hitlist);

                    PacketHitList.MethodEnded(ref hitlist);
                }
            }
        }