[Release] Npc spawning

01/29/2010 08:59 pro4never#1
Ok so this is sorta a mini/partial release. I'm tired and I really don't feel like finishing this (did conquer 1.0 not have blue mouse or is everything just fucked up on my end?...)

Anyways I wanted to finish up a blue mouse system for people but due to my own lazyness and stupidity I can't finish it up right now. Figured I may as well release what I have done for people to use.

THIS IS FOR COFUTURE CONQUER 1.0 SOURCE!

Co Emu will be virtually IDENTICAL, lotf will require a bit of conversion (you are welcome to do it if you want)

This is a simple way for people to add/remove npcs to their server while it's running via a command or have it used for npcs you want to move. Examples of uses would be...

-Blue mouse quest (as it exists on real co with randomly moving mice)
-Find the npc quest (random locations loaded from database. When used the npc jumps to a new location, etc)
-Having npcs only show up at certain times of days for events.

Or anything else you want really. Use some imagination!

Problem with current version: maybe I'm missing something but for the life of me I can't seem to de-spawn an npc client side unless I move my character. I'm sure it's simple enough but it's 3 am and I don't care to keep working on it (I've tried a few things... but w/e, no big deal)

Under Handlers>Functions.cs
Code:
public static void SpawnNpc(int id, int type, int subtype, int map, int x, int y, int direction, int flag)
        {//blabla
            {
                {
                    Struct.NPC NPC = new Struct.NPC();
                    NPC.Direction = direction;
                    NPC.Flag = flag;
                    NPC.ID = id;
                    NPC.Map = map;
                    NPC.SubType = subtype;
                    NPC.Type = type;
                    NPC.X = Convert.ToUInt16(x);
                    NPC.Y = Convert.ToUInt16(y);
                    if (Start.Npcs.ContainsKey(NPC.ID))
                    {
                        while (Start.Npcs.ContainsKey(NPC.ID))
                            NPC.ID = Start.Rand.Next(100, 10000);
                        Console.WriteLine("Npc Id in use, selected random id: " + NPC.ID);
                    }

                    Start.Npcs.Add(NPC.ID, NPC);
                    foreach (KeyValuePair<uint, COClient> Clients in Start.Clients)
                    {
                        if (Clients.Value.Char.Map == NPC.Map && Calculation.CanSee(NPC.X, NPC.Y, Clients.Value.Char.X, Clients.Value.Char.Y))
                        {
                            Spawn.All(Clients.Value, true);
                        }
                    }

                    Console.WriteLine("Successfuly added npc to server");


                }


            }
        }
So that's the spawn code for you as I currently have it. I was going to try to add custom npc names to it but again, lazy, late at night and I honestly forget how to do it.

Same area, simple despawn void for you

Code:
 public static void DespawnNpc(int RemovalType, int ID)
        {//blabla
            switch (RemovalType)
            {
                case 1://Npc Type
                    {
                        foreach (KeyValuePair<int, Struct.NPC> LoopId in Start.Npcs)
                        {
                            if (LoopId.Value.Type == ID)
                            {
                                Start.Npcs.Remove(LoopId.Value.ID);
                                Console.WriteLine("Found type in for each loop! " + Convert.ToInt32(LoopId.Value.Type));
                                break;
                            }
                        }
                        break;
                    }
                case 2://Npc Id
                    {
                        Start.Npcs.Remove(ID);
                        Console.WriteLine("Removed Npc ID: " + Convert.ToString(ID));
                        break;
                    }
            }

            
        }
note I have 2 types. That's because for the uid of npcs I used a random value and to find them again I wanted to be able to use their npc script Id #. I included the other one for if you want to do it that way. Either one should work just fine (just go offscreen and back on and it will be gone)

Annndd simple command to use ingame to test it.

Code:
if (Splitter[0] == "/addnpc")
                    {
                        int NpcUid = Start.Rand.Next(100, 10000);
                        Handler.SpawnNpc(NpcUid, Convert.ToInt32(Splitter[1]), Convert.ToInt32(Splitter[2]), Convert.ToInt32(Splitter[3]), Convert.ToInt32(Splitter[4]), Convert.ToInt32(Splitter[5]), 2, Convert.ToInt32(Splitter[6]));
                        return 2;
                    }
Code:
if (Splitter[0] == "/removenpc")
                    {
                        Handler.DespawnNpc(1, Convert.ToInt32(Splitter[1]));
                        return 2;
                    }
Implementation of the void is just that simple for any events, just use the Handler.NpcSpawn/DespawnNpc to control what is spawned where.


Anyways, hope some ppl enjoy it seeing as I'm never going to really use it.

Note: I just noticed when I was testing the early version of it... apparently I finished random events on my 1.0 server? wtf... I was playing and then it told me I missed my pk tournament. I didn't realize I ever finished that script :S
01/29/2010 10:05 coreymills#2
nice work Pro
01/29/2010 10:10 pro4never#3
Thanks,

If I get bored later I may combine this with my random scroll system to make an ACTUAL event/quest to find an npc (srsly... so many talk to npc A, bring item B to npc C for reward D quests on here...)

Anyways, that's off topic.
01/29/2010 19:47 CompacticCo#4
Quote:
Originally Posted by pro4never View Post
Thanks,

If I get bored later I may combine this with my random scroll system to make an ACTUAL event/quest to find an npc (srsly... so many talk to npc A, bring item B to npc C for reward D quests on here...)

Anyways, that's off topic.
Nice, not going to be able to use this, but it's a great contribute.

Random question, who is that guy in your avatar?
01/29/2010 19:54 Kiyono#5
Quote:
Originally Posted by CompacticCo View Post
Nice, not going to be able to use this, but it's a great contribute.

Random question, who is that guy in your avatar?
Michael Shanks?
01/29/2010 20:08 copz1337#6
Shanks
01/29/2010 20:18 Jedex#7
Good job pro!
01/29/2010 22:27 pro4never#8
Quote:
Originally Posted by CompacticCo View Post
Nice, not going to be able to use this, but it's a great contribute.

Random question, who is that guy in your avatar?
Yah, it's Michael Shanks, an actor.


@thread, I did end up writing this for CoEmu lastnight (actually I finished writing it into a random npc quest where every time you use the npc it bounces around the game world to random locations and gives out prizes)...

I may or may not release that at some point. After all this coding though I'm almost tempted to try to write a server again though. I really do miss having one >.<