C# Help

04/03/2010 02:09 scottdavey#1
The Error:
[Only registered and activated users can see links. Click Here To Register...]

Load NPCS
Code:
public static void LoadNPCs()
        {
            try
            {
                System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(System.Windows.Forms.Application.StartupPath + @"\NPCS\\");
                int count;
                count = d.GetFiles().Length;
                Console.WriteLine("File Count: " + count);
                if (count >= 1)
                {
                    NPCs = new uint[count][];
                    for (int i = 0; i < count; i++)
                    {
                        string filepath = System.Windows.Forms.Application.StartupPath + @"\NPCS\\" + i + ".ini";
                        if (System.IO.File.Exists(filepath))
                        {
                            Ini NPC = new Ini(filepath);
                            uint npctype = uint.Parse(NPC.ReadValue("Data", "Type"));
                            uint npcflags = uint.Parse(NPC.ReadValue("Data", "Flags"));
                            uint npcdirection = uint.Parse(NPC.ReadValue("Data", "Flags"));
                            uint npcx = uint.Parse(NPC.ReadValue("Data", "X"));
                            uint npcy = uint.Parse(NPC.ReadValue("Data", "Y"));
                            uint npcmap = uint.Parse(NPC.ReadValue("Data", "Map"));
                            uint npcsobtype = uint.Parse(NPC.ReadValue("Data", "SobType"));
                            Console.WriteLine("NPC ID: " + i + " loaded - " + npctype + "," + npcflags + "," + npcdirection + "," + npcx + "," + npcy + "," + npcmap + "," + npcsobtype + ".");
                            NPCs[i] = new uint[8] { (uint)i, npctype, npcflags, npcdirection, npcx, npcy, npcmap, npcsobtype };
                            
                        }
                    }
                }
                General.WriteLine("Loaded " + count + " NPCs.");
            }
            catch (Exception Exc) { General.WriteLine(Convert.ToString(Exc)); }
            //Console.WriteLine("NOTE TO SELF: CONVERT LOADNPCS() to INI");
        }
SpawnAllNpcs
Code:
public static void SpawnAllNPCs()
        {
            try
            {
                foreach (uint[] NPC in DataBase.NPCs)
                {
                    SingleNPC npc = new SingleNPC(Convert.ToUInt32(NPC[0]), Convert.ToUInt32(NPC[1]), Convert.ToByte(NPC[2]), Convert.ToByte(NPC[3]), Convert.ToInt16(NPC[4]), Convert.ToInt16(NPC[5]), Convert.ToInt16(NPC[6]), Convert.ToByte(NPC[7]));
                    AllNPCs.Add(npc.UID, npc);
                }
                DataBase.NPCs = null;

                SingleNPC npcc = new SingleNPC(614, 1450, 2, 0, (short)DataBase.GC1X, (short)DataBase.GC1Y, (short)DataBase.GC1Map, 0);
                AllNPCs.Add(614, npcc);

                npcc = new SingleNPC(615, 1460, 2, 0, (short)DataBase.GC2X, (short)DataBase.GC2Y, (short)DataBase.GC2Map, 0);
                AllNPCs.Add(615, npcc);

                npcc = new SingleNPC(616, 1470, 2, 0, (short)DataBase.GC3X, (short)DataBase.GC3Y, (short)DataBase.GC3Map, 0);
                AllNPCs.Add(616, npcc);

                npcc = new SingleNPC(617, 1480, 2, 0, (short)DataBase.GC4X, (short)DataBase.GC4Y, (short)DataBase.GC4Map, 0);
                AllNPCs.Add(617, npcc);                

            }
            catch (Exception Exc) { General.WriteLine(Convert.ToString(Exc)); }
        }
    }
I'm working on converting LOTF to ini and i've just converted accounts which works fine although i'm getting the error in the screenshot and the NPC does not load ingame.

I'm just doing this as a side project for when i'm bored, not planning on using it.

Any ideas?
04/03/2010 02:14 spare2#2
What's line 19?
You are probably calling an empty array.
04/03/2010 02:16 PeTe Ninja#3
Here this loads over 500 NPCs and it 5017 LOTF... Taken from Hybrids Source and fixed a bit to make into LOTF...

Code:
public static void LoadNPCs()
        {
            if (Directory.Exists(@"C:\DataBase\\cq_npc\\"))
            {
                int npcs = 0;

                foreach (string file in Directory.GetFiles((@"C:\DataBase\\cq_npc\\")))
                {
                    IniFile2 cq_npc = new IniFile2(file);
                    uint UID = cq_npc.ReadUInt32("cq_npc", "id", 0);
                    short X = (short)cq_npc.ReadUInt16("cq_npc", "cellx", 0);
                    short Y = (short)cq_npc.ReadUInt16("cq_npc", "celly", 0);
                    short Map = (short)cq_npc.ReadUInt16("cq_npc", "mapid", 0);
                    ushort Type = cq_npc.ReadUInt16("cq_npc", "lookface", 0);
                    byte Flags = (byte)(ConquerAngle)cq_npc.ReadUInt16("cq_npc", "type", 0);
                    byte Sob = (byte)cq_npc.ReadUInt16("cq_npc", "sort", 0);

                    SingleNPC npc = new SingleNPC(UID, Type, Flags, 2, X, Y, Map, Sob);
                    npcs++;
                    NPCs.Add(UID, npc);
                }

                Console.WriteLine("Loaded " + npcs + " NPCs.");
            }
            else { Console.WriteLine("CQ_NPC IS MISSING. WARNING: CAN NOT LOAD NPCS!"); }
        }
Make sure you define the "NPCs.Add" My NPCs hashtable was in same place where I loaded them... also make sure you define the location of your npcs.. I've uploaded the DataBase/cq_npc file folder..so you can put it in C:/


Forgot to give you IniFile2.. here you go :)

Code:
public class IniFile2
    {
        public string FileName;

        public IniFile2()
        {
        }

        public IniFile2(string _FileName)
        {
            this.FileName = _FileName;
        }

        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int GetPrivateProfileStringA(string Section, string Key, string _Default, StringBuilder Buffer, int BufferSize, string FileName);
        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int WritePrivateProfileStringA(string Section, string Key, string Arg, string FileName);

        public byte ReadByte(string Section, string Key, byte _Default)
        {
            byte buf = _Default;
            byte.TryParse(this.ReadString(Section, Key, _Default.ToString(), 6), out buf);
            return buf;
        }

        public short ReadInt16(string Section, string Key, short _Default)
        {
            short buf = _Default;
            short.TryParse(this.ReadString(Section, Key, _Default.ToString(), 9), out buf);
            return buf;
        }

        public int ReadInt32(string Section, string Key, int _Default)
        {
            int buf = _Default;
            int.TryParse(this.ReadString(Section, Key, _Default.ToString(), 15), out buf);
            return buf;
        }

        public sbyte ReadSByte(string Section, string Key, byte _Default)
        {
            sbyte buf = (sbyte)_Default;
            sbyte.TryParse(this.ReadString(Section, Key, _Default.ToString(), 6), out buf);
            return buf;
        }

        public string ReadString(string Section, string Key, string _Default)
        {
            return this.ReadString(Section, Key, _Default, 255);
        }

        public string ReadString(string Section, string Key, string _Default, int BufSize)
        {
            StringBuilder Buffer = new StringBuilder(BufSize);
            GetPrivateProfileStringA(Section, Key, _Default, Buffer, BufSize, this.FileName);
            return Buffer.ToString();
        }

        public ushort ReadUInt16(string Section, string Key, ushort _Default)
        {
            ushort buf = _Default;
            ushort.TryParse(this.ReadString(Section, Key, _Default.ToString(), 9), out buf);
            return buf;
        }

        public uint ReadUInt32(string Section, string Key, uint _Default)
        {
            uint buf = _Default;
            uint.TryParse(this.ReadString(Section, Key, _Default.ToString(), 15), out buf);
            return buf;
        }

        public void Write(string Section, string Key, object Value)
        {
            WritePrivateProfileStringA(Section, Key, Value.ToString(), this.FileName);
        }

        public void Write(string Section, string Key, string Value)
        {
            WritePrivateProfileStringA(Section, Key, Value, this.FileName);
        }

    }
and here is conquer angle..

Code:
public enum ConquerAngle : byte
        {
            SouthWest = 0,
            West = 1,
            NorthWest = 2,
            North = 3,
            NorthEast = 4,
            East = 5,
            SouthEast = 6,
            South = 7
        }
04/03/2010 02:22 scottdavey#4
Quote:
Originally Posted by PeTe Ninja View Post
Yo..scott i have like over 500 npcs loaded from INI and spawned... do you want it? im gonna edit this post anyway to post it here...
That would be great man, i'm a good pawn coder which is similiar to c so i'm kinda coding without much knowledge.

Thanks bud!

And for the other guy,

line 19
Code:
SingleNPC npc = new SingleNPC(Convert.ToUInt32(NPC[0]), Convert.ToUInt32(NPC[1]), Convert.ToByte(NPC[2]), Convert.ToByte(NPC[3]), Convert.ToInt16(NPC[4]), Convert.ToInt16(NPC[5]), Convert.ToInt16(NPC[6]), Convert.ToByte(NPC[7]));
04/03/2010 02:25 PeTe Ninja#5
Quote:
Originally Posted by scottdavey View Post
That would be great man, i'm a good pawn coder which is similiar to c so i'm kinda coding without much knowledge.

Thanks bud!

And for the other guy,

line 19
Code:
SingleNPC npc = new SingleNPC(Convert.ToUInt32(NPC[0]), Convert.ToUInt32(NPC[1]), Convert.ToByte(NPC[2]), Convert.ToByte(NPC[3]), Convert.ToInt16(NPC[4]), Convert.ToInt16(NPC[5]), Convert.ToInt16(NPC[6]), Convert.ToByte(NPC[7]));

Just updated my previous post, and yeah I've tested it before and it works great... :)

Me and my friend a long time ago used the current way LOTF loads npcs ( through mysql ) and inserted over 500NPCs ourself..I've lost it a few times and had to do it again by creating queries.. and then I did lose it again and finally just was like..Why don't I load them from INI? ha so Now I did...
04/03/2010 02:38 scottdavey#6
Quote:
Originally Posted by PeTe Ninja View Post
Just updated my previous post, and yeah I've tested it before and it works great... :)

Me and my friend a long time ago used the current way LOTF loads npcs ( through mysql ) and inserted over 500NPCs ourself..I've lost it a few times and had to do it again by creating queries.. and then I did lose it again and finally just was like..Why don't I load them from INI? ha so Now I did...
Errors when i added the class:

[Only registered and activated users can see links. Click Here To Register...]
04/03/2010 03:00 PeTe Ninja#7
Quote:
Originally Posted by scottdavey View Post
Errors when i added the class:

[Only registered and activated users can see links. Click Here To Register...]
System.RunTime.Interopservices << something of that.. define it by the other system thingies..
04/03/2010 03:12 scottdavey#8
Quote:
Originally Posted by PeTe Ninja View Post
System.RunTime.Interopservices << something of that.. define it by the other system thingies..
Ok got it thanks, how do i define the .add?
04/03/2010 03:32 PeTe Ninja#9
Quote:
Originally Posted by scottdavey View Post
Ok got it thanks, how do i define the .add?
.add?

Code:
using System.RunTime.InteropServices;
by the other wones..
04/03/2010 03:36 scottdavey#10
Quote:
Originally Posted by PeTe Ninja View Post
.add?

Code:
using System.RunTime.InteropServices;
by the other wones..
I mean:

NPCs.Add(UID, npc);

I'm new to C#, i have all the other errors fixed.
04/03/2010 03:42 PeTe Ninja#11
oh well i have a hastable in my database.cs

public Hashtable NPCs = new Hashtable();

you might have it in like World so do like

World.NPCs ..
04/03/2010 04:17 scottdavey#12
Quote:
Originally Posted by PeTe Ninja View Post
oh well i have a hastable in my database.cs

public Hashtable NPCs = new Hashtable();

you might have it in like World so do like

World.NPCs ..
Couldn't get it working, oh well i'll try and fix my own code.

[Only registered and activated users can see links. Click Here To Register...]

Got your code working although the npcs aren't spawned ingame, any ideas?

Can you spawn your spawnallnpcs, as from the error i assume thats the problem.
04/06/2010 07:07 PeTe Ninja#13
yeah i spawned them all.. all 565 if im not mistaken npcs..
04/06/2010 17:35 scottdavey#14
Quote:
Originally Posted by PeTe Ninja View Post
yeah i spawned them all.. all 565 if im not mistaken npcs..
For some reason they won't spawn ingame for me?

I have used your code, perhaps i'm missing something or your spawnallnpcs function is different to mine?
04/06/2010 19:18 PeTe Ninja#15
Quote:
Originally Posted by scottdavey View Post
For some reason they won't spawn ingame for me?

I have used your code, perhaps i'm missing something or your spawnallnpcs function is different to mine?
in a bit pm me ur tv info ill be back in right now ><