Exemple #1
0
        /// <summary>
        /// (Attempts to) Spawn monsters randomly around the current x y position.
        /// </summary>
        /// <param name="npcvar">The Custom NPC to use to spawn children</param>
        /// <param name="amount">The amount to spawn</param>
        /// <param name="sethealth"></param>
        /// <param name="health"></param>
        public void Multiply(CustomNPCDefinition type, int amount, bool sethealth = false, int health = 0)
        {
            //DEBUG
            TShock.Log.ConsoleInfo("DEBUG [Multiply] entry. amount={0}, sethealth={1}, health={2}", amount, sethealth, health);
            //DEBUG

            // MainNPC is gone.
            if (mainNPC == null)
            {
                return;
            }

            if (type == null)
            {
                return;
            }

            for (int i = 0; i < amount; i++)
            {
                int x = (int)mainNPC.position.X + rand.Next(-8, 9);
                int y = (int)mainNPC.position.Y + rand.Next(-8, 9);

                int npc = NPCManager.SpawnNPCAtLocation(x, y, type);
                if (npc == -1)
                {
                    //DEBUG
                    TShock.Log.ConsoleInfo("DEBUG [Multiply] spawn failed. location={0}, {1}", x, y);
                    //DEBUG
                    continue;
                }

                var spawned = NPCManager.GetCustomNPCByIndex(npc);
                if (spawned == null)
                {
                    continue;
                }

                if (sethealth)
                {
                    spawned.mainNPC.life = health;
                }

                spawned.isClone = true;
            }
        }
        /// <summary>
        /// Spawn custom npc using /csm &lt;id&gt; [amount] [&lt;x&gt; &lt;y&gt;]
        /// </summary>
        /// <param name="args"></param>
        private void CommandSpawnNPC(CommandArgs args)
        {
            //Error if too many or too few params specified
            if (args.Parameters.Count == 0 || args.Parameters.Count > 4)
            {
                args.Player.SendInfoMessage("Usage: /csm <id> [<amount>] [<x> <y>]");
                return;
            }

            //Get custom npc by id
            var cdef = NPCManager.Data.GetNPCbyID(args.Parameters[0]);

            if (cdef == null)
            {
                args.Player.SendErrorMessage("Error: The custom npc with id \"{0}\" does not exist!", args.Parameters[0]);
                return;
            }

            //Default to 1 if amount is not defined
            int amount = 1;

            //Check if amount is defined
            if (args.Parameters.Count == 2 || args.Parameters.Count == 4)
            {
                int.TryParse(args.Parameters[1], out amount);

                //Check for too many mobs
                if (amount > 200)
                {
                    args.Player.SendErrorMessage("Error: Amount needs to be lower than 200!");
                    return;
                }
            }

            //Check for X and Y
            int x;
            int y;

            //Not specified, use player's coordinates. (/csm <id> [amount])
            if (args.Parameters.Count <= 2)
            {
                x = (int)args.Player.X + rand.Next(-8, 9);
                y = (int)args.Player.Y + rand.Next(-8, 9);
            }

            //Specified, no amount (/csm <id> <x> <y>)
            else if (args.Parameters.Count == 3)
            {
                if (!int.TryParse(args.Parameters[1], out x))
                {
                    args.Player.SendErrorMessage("Error: Invalid x position defined!");
                    return;
                }
                if (!int.TryParse(args.Parameters[2], out y))
                {
                    args.Player.SendErrorMessage("Error: Invalid y position defined!");
                    return;
                }
            }

            //All arguments specified (/csm <id> <amount> <x> <y>)
            else
            {
                if (!int.TryParse(args.Parameters[2], out x))
                {
                    args.Player.SendErrorMessage("Error: Invalid x position defined!");
                    return;
                }
                if (!int.TryParse(args.Parameters[3], out y))
                {
                    args.Player.SendErrorMessage("Error: Invalid y position defined!");
                    return;
                }
            }

            //Keep track of spawns that fail.
            int failed = 0;

            //Spawn mobs
            for (int i = 0; i < amount; i++)
            {
                int j = NPCManager.SpawnNPCAtLocation(x, y, cdef);

                if (j == -1)
                {
                    failed++;
                }
                else
                {
                    cdef.currSpawnsVar++;
                }
            }

            //Inform player
            if (failed > 0)
            {
                args.Player.SendWarningMessage("Failed to spawn {0} of {1} \"{2}\"'s at ({3}, {4})", failed, amount, args.Parameters[0], x, y);
            }
            else
            {
                args.Player.SendSuccessMessage("Spawned {0} \"{1}\"'s at ({2}, {3})", amount, args.Parameters[0], x, y);
            }
        }