Exemple #1
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);
                }
            }
        }