using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace SkySide3gby_NetRlb
{
                               

    public partial class Form1 : Form
    {
        Byte[] rlbByte = new Byte[1048576 + 1];//100M
        Byte[,] resByte = new Byte[116, 1048576];//10M
        int[] resAddress = new int[116];//0-4000h
        Byte[] tmpBmpByte = new Byte[1048576 + 1];//10M
        Byte[,] resPicByte = new Byte[201, 50000];//2000000];
        int[] resPicByteLen = new int[201];
        int tmpBmpByteLen = 0;
        RCHEAD[] reshead = new RCHEAD[116];
        int[] reswidth = new int[116];
        int[] resheight = new int[116];

        

        HERO[,] hero = new HERO[4,200];
        CITY[,] city = new CITY[4,38];
        GOOD[,] good = new GOOD[4,33];
        string[,] heroname = new string[4,200];
        string[] cityname = new string[38];
        string[] goodname = new string[33];

        Byte[] savByte = new Byte[3245 + 2708 + 1];
        int savByteLen = 3245 + 2708;
        HERO[] savhero = new HERO[200];
        CITY[] savcity = new CITY[38];
        GOOD[] savgood = new GOOD[33];
        string[] savheroname = new string[200];
        string[] savcityname = new string[38];
        string[] savgoodname = new string[33];
        int savPeriod = 0;
        string g_filename = "";
        string g_filename1 = "";

        Byte[,] g_HeroQueue = new Byte[4,200];
        Byte[,] g_GoodsQueue = new Byte[4,33];

        //int[] respicAddress = new int[300];//用于图片在res中相对的起始位置
        int resmax = 0;
        int resselected = 0;
        int respicmax = 0;
        int respicselected = 0;
        bool saved = false;
        int rlbByteLen = 0;
        //bool edited = false;
        bool isRlb = true;
        int g_idHero = 0;
        int g_idGood = 0;
        int g_idCity = 0;
        int g_idSavHero = 0;
        int g_idSavCity = 0;
        bool notSavedHero = false;
        bool notSavedGood = false;
        bool notSavedCity = false;
        //bool intialed = false
        bool noRlb = true;

        bool isLib = false;
        bool isSsv = false;

        bool cannotSave = false;


        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 116; i++)
            {
                reshead[i] = new RCHEAD();
                reshead[i].ItmCnt = 0;
                reshead[i].ItmLen = 0;
                reshead[i].Reserved = 0;
                reshead[i].ResId = 0;
                reshead[i].ResKey = 0;
                reshead[i].ResLen = 0;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if(listBox1.SelectedIndex>0)label5.Text = resAddress[listBox1.SelectedIndex]+"";
            resselected = listBox1.SelectedIndex;


            //res 
            //61		/* 武将资源 */
//62		/* 武将名称一 */
//70		/* 武将名称二 */
//71		/* 武将名称三 */
//72		/* 武将名称四 */
//57		/* 城市资源 */
//58		/* 城市名称 */
//73		/* 道具名称 */
//66		/* 道具资源 */
//GENERAL_CON		63		/* 武将出现条件 */
//GOODS_CON		67		/* 道具出现条件 */

            int[] canUseResId = { 61, 62, 70, 71, 72, 57, 58, 66, 73,63,67};



            for (int i = 0; i < canUseResId.Length; i++)
            {
                if ((resselected + 1) == canUseResId[i])
                {
                    if (i == 0)//武将资源
                    {
                        InitHero(resselected);
                    }
                    else if (i > 0 && i < 5)//武将名称
                    {
                        InitHeroName(resselected);
                    }
                    else if (i == 5)//城池资源
                    {
                        InitCity(resselected);
                    }
                    else if (i == 6)//城池名称
                    {
                        InitCityName(resselected);
                    }
                    else if (i == 7)//道具资源
                    {
                        InitGood(resselected);
                    }
                    else if (i == 8)//道具名称
                    {
                        InitGoodName(resselected);
                    }


                    /*
                    byte[] b1 = new byte[4];
                    b1[0] = resByte[resselected, 4];
                    b1[1] = 0;// resByte[resselected, 5];
                    b1[2] = 0;
                    b1[3] = 0;
                    int piccnt = BitConverter.ToInt32(b1, 0);
                    if (resByte[resselected, 5] > 0) piccnt *= 2;
                    respicmax = piccnt;
                    if (piccnt > 200) piccnt = 200;
                    for (int l = 0; l < piccnt; l++)
                    {
                        
                        if (isRlb)
                        {
                            //InitResPicColor(resselected, l, 0);
                            InitResPicJPG(resselected, l, 0);
                        }
                        else
                        {
                            InitResPic(resselected, l, 0);
                        }
                    }*/



                    
                }
            }
            
            
        }

        private void InitGoodName(int resselected)
        {
            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];
            
                int st = 0;
                for (int i = 0; i < 33; i++)
                {
                    byte[] byteArray = new byte[10];
                    byte reskey = 0xc3;
                    byteArray[0] = (byte)(resByte[resselected, idx + st] - reskey);
                    byteArray[1] = (byte)(resByte[resselected, idx + st + 1] - reskey);
                    byteArray[2] = (byte)(resByte[resselected, idx + st + 2] - reskey);
                    byteArray[3] = (byte)(resByte[resselected, idx + st + 3] - reskey);
                    byteArray[4] = (byte)(resByte[resselected, idx + st + 4] - reskey);
                    byteArray[5] = (byte)(resByte[resselected, idx + st + 5] - reskey);
                    byteArray[6] = (byte)(resByte[resselected, idx + st + 6] - reskey);
                    byteArray[7] = (byte)(resByte[resselected, idx + st + 7] - reskey);
                    byteArray[8] = (byte)(resByte[resselected, idx + st + 8] - reskey);
                    byteArray[9] = (byte)(resByte[resselected, idx + st + 9] - reskey);

                    goodname[i] = System.Text.Encoding.Default.GetString(byteArray);
                    //hero[j, i].Name = resByte[resselected, idx];

                    st += 10;

                }
            
        }

        private void InitCityName(int resselected)
        {
            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];
            
                int st = 0;
                for (int i = 0; i < 38; i++)
                {
                    byte[] byteArray = new byte[10];
                    byte reskey = 0xc0;
                    byteArray[0] = (byte)(resByte[resselected, idx + st] - reskey);
                    byteArray[1] = (byte)(resByte[resselected, idx + st + 1] - reskey);
                    byteArray[2] = (byte)(resByte[resselected, idx + st + 2] - reskey);
                    byteArray[3] = (byte)(resByte[resselected, idx + st + 3] - reskey);
                    byteArray[4] = (byte)(resByte[resselected, idx + st + 4] - reskey);
                    byteArray[5] = (byte)(resByte[resselected, idx + st + 5] - reskey);
                    byteArray[6] = (byte)(resByte[resselected, idx + st + 6] - reskey);
                    byteArray[7] = (byte)(resByte[resselected, idx + st + 7] - reskey);
                    byteArray[8] = (byte)(resByte[resselected, idx + st + 8] - reskey);
                    byteArray[9] = (byte)(resByte[resselected, idx + st + 9] - reskey);

                    cityname[i] = System.Text.Encoding.Default.GetString(byteArray);
                    //hero[j, i].Name = resByte[resselected, idx];

                    st += 10;

                }
            
        }

        private void InitHeroName(int resselected)
        {
            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];
           
                int st = 0;
                for (int i = 0; i < 200; i++)
                {
                    byte[] byteArray = new byte[8];
                    byte reskey = 0xc3;
                    byteArray[0] = (byte)(resByte[resselected, idx + st] - reskey);
                    byteArray[1] = (byte)(resByte[resselected, idx + st + 1]- reskey);
                    byteArray[2] = (byte)(resByte[resselected, idx + st + 2]- reskey);
                    byteArray[3] = (byte)(resByte[resselected, idx + st + 3]- reskey);
                    byteArray[4] = (byte)(resByte[resselected, idx + st + 4]- reskey);
                    byteArray[5] = (byte)(resByte[resselected, idx + st + 5]- reskey);
                    byteArray[6] = (byte)(resByte[resselected, idx + st + 6]- reskey);
                    byteArray[7] = (byte)(resByte[resselected, idx + st + 7] - reskey);

                    if ((resselected + 1) == 62)
                    {
                        heroname[0, i] = System.Text.Encoding.Default.GetString(byteArray);
                    }
                    else if ((resselected +1) == 70)
                    {
                        heroname[1, i] = System.Text.Encoding.Default.GetString(byteArray);
                    }
                    else if ((resselected +1) == 71)
                    {
                        heroname[2, i] = System.Text.Encoding.Default.GetString(byteArray);
                    }
                    else if ((resselected + 1) == 72)
                    {
                        heroname[3, i] = System.Text.Encoding.Default.GetString(byteArray);
                    }
                    //hero[j, i].Name = resByte[resselected, idx];

                    st += 8;

                }
        }

        private void InitGood(int resselected)
        {
            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];
            //for (int j = 0; j < 4; j++)
            {
                idx = 0;
                for (int i = 0; i < 33; i++)
                {
                    good[0, i] = new GOOD();
                    good[0, i].idx = resByte[resselected, idx];
                    good[0, i].useflag = resByte[resselected, idx + 1];

                    //public byte[] name = new byte[10];		/* 道具名字 */


                    //public byte[] inf = new byte[50];		/* 道具说明 */

                    good[0, i].at = resByte[resselected, idx + 62];
                    good[0, i].iq = resByte[resselected, idx + 63];
                    good[0, i].move = resByte[resselected, idx + 64];
                    good[0, i].arm = resByte[resselected, idx + 65];


                    idx += 66;

                }
            }
        }

        private void InitGoodCon(int resselected)
        {
            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];
            for (int j = 0; j < 4; j++)
            {
                for (int i = 0; i < 33; i++)
                {
                    good[j, i].AppearYear = resByte[resselected, idx + 0];
                    good[j, i].SearchId = resByte[resselected, idx + 1];
                    good[j, i].AppearCityId = resByte[resselected, idx + 2];

                    idx += 3;

                }
            }
        }

        private void InitCity(int resselected)
        {
            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];
            for (int j = 0; j < 4; j++)
            {
                for (int i = 0; i < 38; i++)
                {
                    city[j, i] = new CITY();
                    city[j,i].Id = resByte[resselected, idx];
                    city[j, i].Belong = resByte[resselected, idx + 1];
                    city[j, i].SatrapId = resByte[resselected, idx + 2];

                    byte[] b1 = new byte[2];
                    b1[0] = resByte[resselected, idx + 3];
                    b1[1] = resByte[resselected, idx + 4];
                    city[j, i].FarmingLimit = BitConverter.ToUInt16(b1, 0);

                    byte[] b2 = new byte[2];
                    b2[0] = resByte[resselected, idx + 5];
                    b2[1] = resByte[resselected, idx + 6];
                    city[j, i].Farming = BitConverter.ToUInt16(b2, 0);

                    byte[] b3 = new byte[2];
                    b3[0] = resByte[resselected, idx + 7];
                    b3[1] = resByte[resselected, idx + 8];
                    city[j, i].CommerceLimit = BitConverter.ToUInt16(b3, 0);

                    byte[] b4 = new byte[2];
                    b4[0] = resByte[resselected, idx + 9];
                    b4[1] = resByte[resselected, idx + 10];
                    city[j, i].Commerce = BitConverter.ToUInt16(b4, 0);

                    city[j, i].PeopleDevotion = resByte[resselected, idx + 11];
                    city[j, i].AvoidCalamity = resByte[resselected, idx + 12];

                    byte[] b5 = new byte[4];
                    b5[0] = resByte[resselected, idx + 13];
                    b5[1] = resByte[resselected, idx + 14];
                    b5[2] = resByte[resselected, idx + 15];
                    b5[3] = resByte[resselected, idx + 16];
                    city[j, i].PopulationLimit = BitConverter.ToUInt32(b5, 0);

                    byte[] b6 = new byte[4];
                    b6[0] = resByte[resselected, idx + 17];
                    b6[1] = resByte[resselected, idx + 18];
                    b6[2] = resByte[resselected, idx + 19];
                    b6[3] = resByte[resselected, idx + 20];
                    city[j, i].Population = BitConverter.ToUInt32(b6, 0);

                    byte[] b7 = new byte[2];
                    b7[0] = resByte[resselected, idx + 21];
                    b7[1] = resByte[resselected, idx + 22];
                    city[j, i].Money = BitConverter.ToUInt16(b7, 0);

                    byte[] b8 = new byte[2];
                    b8[0] = resByte[resselected, idx + 23];
                    b8[1] = resByte[resselected, idx + 24];
                    city[j, i].Food = BitConverter.ToUInt16(b8, 0);

                    byte[] b9 = new byte[2];
                    b9[0] = resByte[resselected, idx + 25];
                    b9[1] = resByte[resselected, idx + 26];
                    city[j, i].MothballArms = BitConverter.ToUInt16(b9, 0);

                    city[j, i].PersonQueue = resByte[resselected, idx + 27];
                    city[j, i].Persons = resByte[resselected, idx + 28];
                    city[j, i].ToolQueue = resByte[resselected, idx + 29];
                    city[j, i].Tools = resByte[resselected, idx + 30];


                    idx += 31;

                }
                idx += 2;
            }
        }

        private void UpdateHero(int resselected)
        {


            int idx = 0;

            for (int j = 0; j < 4; j++)
            {

                for (int i = 0; i < 200; i++)
                {
                    resByte[resselected, idx] = hero[j, i].Id;
                    resByte[resselected, idx + 1] = hero[j, i].Belong;
                    resByte[resselected, idx + 2] = hero[j, i].Level;
                    resByte[resselected, idx + 3] = hero[j, i].Force;
                    resByte[resselected, idx + 4] = hero[j, i].IQ;
                    resByte[resselected, idx + 5] = hero[j, i].Devotion;
                    resByte[resselected, idx + 6] = hero[j, i].Character;
                    resByte[resselected, idx + 7] = hero[j, i].Experience;
                    resByte[resselected, idx + 8] = hero[j, i].Thew;
                    resByte[resselected, idx + 9] = hero[j, i].ArmsType;

                    resByte[resselected, idx + 10] = (byte)((hero[j, i].Arms ) & 0xFF);
                    resByte[resselected, idx + 11] = (byte)((hero[j, i].Arms >> 8) & 0xFF);
                    
                    resByte[resselected, idx + 12] = hero[j, i].Equip[0];
                    resByte[resselected, idx + 13] = hero[j, i].Equip[1];
                    resByte[resselected, idx + 14] = hero[j, i].Age;

                    idx += 15;

                }
            }
        }

        private void UpdateHeroSav()
        {


            int idx = 0;

            int startPos = 12;

                for (int i = 0; i < 200; i++)
                {
                    savByte[startPos] = savhero[i].Id;
                    savByte[startPos + 1] = savhero[i].Belong;
                    savByte[startPos + 2] = savhero[i].Level;
                    savByte[startPos + 3] = savhero[i].Force;
                    savByte[startPos + 4] = savhero[i].IQ;
                    savByte[startPos + 5] = savhero[i].Devotion;
                    savByte[startPos + 6] = savhero[i].Character;
                    savByte[startPos + 7] = savhero[i].Experience;
                    savByte[startPos + 8] = savhero[i].Thew;
                    savByte[startPos + 9] = savhero[i].ArmsType;

                    savByte[startPos + 10] = (byte)((savhero[i].Arms) & 0xFF);
                    savByte[startPos + 11] = (byte)((savhero[i].Arms >> 8) & 0xFF);

                    savByte[startPos + 12] = savhero[i].Equip[0];
                    savByte[startPos + 13] = savhero[i].Equip[1];
                    savByte[startPos + 14] = savhero[i].Age;

                    startPos += 15;

                }
            
        }

        private void UpdateGood(int resselected)
        {
            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];

            for (int i = 0; i < 33; i++)
            {
                 resByte[resselected, idx] = good[0, i].idx;
                 resByte[resselected, idx + 1] = good[0, i].useflag;

                //public byte[] name = new byte[10];		/* 道具名字 */


                //public byte[] inf = new byte[50];		/* 道具说明 */

                 resByte[resselected, idx + 62] = good[0, i].at;
                 resByte[resselected, idx + 63] = good[0, i].iq;
                 resByte[resselected, idx + 64] = good[0, i].move;
                 resByte[resselected, idx + 65] = good[0, i].arm;


                idx += 66;

            }
        }

        private void UpdateCity(int resselected)
        {

            int idx = 0;

            for (int j = 0; j < 4; j++)
            {
                for (int i = 0; i < 38; i++)
                {
                    resByte[resselected, idx] = city[j, i].Id;
                    resByte[resselected, idx + 1] = city[j, i].Belong;
                    resByte[resselected, idx + 2] = city[j, i].SatrapId;

                    resByte[resselected, idx + 3] = (byte)((city[j, i].FarmingLimit ) & 0xFF);
                    resByte[resselected, idx + 4] = (byte)((city[j, i].FarmingLimit >> 8) & 0xFF);
                    
                    resByte[resselected, idx + 5] = (byte)((city[j, i].Farming ) & 0xFF);
                    resByte[resselected, idx + 6] = (byte)((city[j, i].Farming >> 8) & 0xFF);
                    
                    resByte[resselected, idx + 7] = (byte)((city[j, i].CommerceLimit) & 0xFF);
                    resByte[resselected, idx + 8] = (byte)((city[j, i].CommerceLimit >> 8) & 0xFF);
                    

                    resByte[resselected, idx + 9] = (byte)((city[j, i].Commerce) & 0xFF);
                    resByte[resselected, idx + 10] = (byte)((city[j, i].Commerce >> 8) & 0xFF);
                    

                    resByte[resselected, idx + 11] = city[j, i].PeopleDevotion;
                    resByte[resselected, idx + 12] = city[j, i].AvoidCalamity;

                    resByte[resselected, idx + 13] = (byte)((city[j, i].PopulationLimit) & 0xFF);
                    resByte[resselected, idx + 14] = (byte)((city[j, i].PopulationLimit >> 8) & 0xFF);
                    resByte[resselected, idx + 15] = (byte)((city[j, i].PopulationLimit >> 16) & 0xFF);
                    resByte[resselected, idx + 16] = (byte)((city[j, i].PopulationLimit >> 24) & 0xFF);
                    

                    resByte[resselected, idx + 17] = (byte)((city[j, i].Population ) & 0xFF);
                    resByte[resselected, idx + 18] = (byte)((city[j, i].Population >> 8) & 0xFF);
                    resByte[resselected, idx + 19] = (byte)((city[j, i].Population >> 16) & 0xFF);
                    resByte[resselected, idx + 20] = (byte)((city[j, i].Population >> 24) & 0xFF);
                    
                    resByte[resselected, idx + 21] = (byte)((city[j, i].Money) & 0xFF);
                    resByte[resselected, idx + 22] = (byte)((city[j, i].Money >> 8) & 0xFF);
                    
                    resByte[resselected, idx + 23] = (byte)((city[j, i].Food) & 0xFF);
                    resByte[resselected, idx + 24] = (byte)((city[j, i].Food >> 8) & 0xFF);
                    
                    resByte[resselected, idx + 25] = (byte)((city[j, i].MothballArms) & 0xFF);
                    resByte[resselected, idx + 26] = (byte)((city[j, i].MothballArms >> 8) & 0xFF);
                    
                    //resByte[resselected, idx + 27] = city[j, i].PersonQueue;
                    //resByte[resselected, idx + 28] = city[j, i].Persons;
                    //resByte[resselected, idx + 29] = city[j, i].ToolQueue;
                    //resByte[resselected, idx + 30] = city[j, i].Tools;


                    idx += 31;

                }
                idx += 2;
            }
        }

        private void UpdateCityHeroInfo(int resselected)
        {

            int idx = 0;

            for (int j = 0; j < 4; j++)
            {
                for (int i = 0; i < 38; i++)
                {
                    
                    resByte[resselected, idx + 27] = city[j, i].PersonQueue;
                    resByte[resselected, idx + 28] = city[j, i].Persons;


                    idx += 31;

                }
                idx += 2;
            }
        }

        private void UpdateCityGoodInfo(int resselected)
        {

            int idx = 0;

            for (int j = 0; j < 4; j++)
            {
                for (int i = 0; i < 38; i++)
                {

                    resByte[resselected, idx + 29] = city[j, i].ToolQueue;
                    resByte[resselected, idx + 30] = city[j, i].Tools;


                    idx += 31;

                }
                idx += 2;
            }
        }

        private void UpdateCitySav()
        {

            int idx = 0;
            int startPos = 3245 + 1530;

            
                for (int i = 0; i < 38; i++)
                {
                    savByte[startPos] = savcity[i].Id;
                    savByte[startPos + 1] = savcity[i].Belong;
                    savByte[startPos + 2] = savcity[i].SatrapId;

                    savByte[startPos + 3] = (byte)((savcity[i].FarmingLimit) & 0xFF);
                    savByte[startPos + 4] = (byte)((savcity[i].FarmingLimit >> 8) & 0xFF);
                    
                    savByte[startPos + 5] = (byte)((savcity[i].Farming) & 0xFF);
                    savByte[startPos + 6] = (byte)((savcity[i].Farming >> 8) & 0xFF);
                    
                    savByte[startPos + 7] = (byte)((savcity[i].CommerceLimit) & 0xFF);
                    savByte[startPos + 8] = (byte)((savcity[i].CommerceLimit >> 8) & 0xFF);
                    

                    savByte[startPos + 9] = (byte)((savcity[i].Commerce) & 0xFF);
                    savByte[startPos + 10] = (byte)((savcity[i].Commerce >> 8) & 0xFF);
                    

                    savByte[startPos + 11] = savcity[i].PeopleDevotion;
                    savByte[startPos + 12] = savcity[i].AvoidCalamity;

                    savByte[startPos + 13] = (byte)((savcity[i].PopulationLimit) & 0xFF);
                    savByte[startPos + 14] = (byte)((savcity[i].PopulationLimit >> 8) & 0xFF);
                    savByte[startPos + 15] = (byte)((savcity[i].PopulationLimit >> 16) & 0xFF);
                    savByte[startPos + 16] = (byte)((savcity[i].PopulationLimit >> 24) & 0xFF);
                    

                    savByte[startPos + 17] = (byte)((savcity[i].Population) & 0xFF);
                    savByte[startPos + 18] = (byte)((savcity[i].Population >> 8) & 0xFF);
                    savByte[startPos + 19] = (byte)((savcity[i].Population >> 16) & 0xFF);
                    savByte[startPos + 20] = (byte)((savcity[i].Population >> 24) & 0xFF);
                    
                    savByte[startPos + 21] = (byte)((savcity[i].Money) & 0xFF);
                    savByte[startPos + 22] = (byte)((savcity[i].Money >> 8) & 0xFF);
                    
                    savByte[startPos + 23] = (byte)((savcity[i].Food) & 0xFF);
                    savByte[startPos + 24] = (byte)((savcity[i].Food >> 8) & 0xFF);
                    
                    savByte[startPos + 25] = (byte)((savcity[i].MothballArms) & 0xFF);
                    savByte[startPos + 26] = (byte)((savcity[i].MothballArms >> 8) & 0xFF);
                    
                    savByte[startPos + 27] = savcity[i].PersonQueue;
                    savByte[startPos + 28] = savcity[i].Persons;
                    savByte[startPos + 29] = savcity[i].ToolQueue;
                    savByte[startPos + 30] = savcity[i].Tools;


                    startPos += 31;

                }
            
        }

        private void UpdateHeroCon(int resselected)
        {


            int idx = 0;

            for (int j = 0; j < 4; j++)
            {

                for (int i = 0; i < 200; i++)
                {
                    resByte[resselected, idx + 0] = hero[j, i].BornYear;
                    resByte[resselected, idx + 1] = hero[j, i].SearchId;
                    resByte[resselected, idx + 2] = hero[j, i].AppearCityId;
                    
                    idx += 3;

                }
            }
        }

        private void UpdateGoodCon(int resselected)
        {
            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];
            for (int j = 0; j < 4; j++)
            {
                for (int i = 0; i < 33; i++)
                {
                    resByte[resselected, idx + 0] = good[j, i].AppearYear;
                    resByte[resselected, idx + 1] = good[j, i].SearchId;
                    resByte[resselected, idx + 2] = good[j, i].AppearCityId;
                    
                    idx += 3;

                }
            }
        }


        private void InitHero(int resselected)
        {

            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];
            for (int j = 0; j < 4; j++)
            {

                for (int i = 0; i < 200; i++)
                {
                    hero[j, i] = new HERO();
                    hero[j, i].Id = resByte[resselected, idx];
                    hero[j, i].Belong = resByte[resselected, idx + 1];
                    hero[j, i].Level = resByte[resselected, idx + 2];
                    hero[j, i].Force = resByte[resselected, idx + 3];
                    hero[j, i].IQ = resByte[resselected, idx + 4];
                    hero[j, i].Devotion = resByte[resselected, idx + 5];
                    hero[j, i].Character = resByte[resselected, idx + 6];
                    hero[j, i].Experience = resByte[resselected, idx + 7];
                    hero[j, i].Thew = resByte[resselected, idx + 8];
                    hero[j, i].ArmsType = resByte[resselected, idx + 9];

                    byte[] b1 = new byte[2];
                    b1[0] = resByte[resselected, idx + 10];
                    b1[1] = resByte[resselected, idx + 11];
                    hero[j, i].Arms = BitConverter.ToUInt16(b1, 0);

                    hero[j, i].Equip[0] = resByte[resselected, idx + 12];
                    hero[j, i].Equip[1] = resByte[resselected, idx + 13];
                    hero[j, i].Age = resByte[resselected, idx + 14];

                    idx += 15;

                }
            }
        }

        private void InitHeroCon(int resselected)
        {

            //reshead[resselected].
            int idx = 0;
            //HERO [] hero= new HERO[200];
            for (int j = 0; j < 4; j++)
            {

                for (int i = 0; i < 200; i++)
                {
                    hero[j, i].BornYear = resByte[resselected, idx + 0];
                    hero[j, i].SearchId = resByte[resselected, idx + 1];
                    hero[j, i].AppearCityId = resByte[resselected, idx + 2];
                    
                    idx += 3;

                }
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void 资源ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void MySave(string FileName)
        {
            for (int k1 = 0; k1 < 16384; k1++)
            {
                rlbByte[k1] = 0;
            }
            //resAddress - >rlbByte
            /*for(int i = 0 ; i < 108 ; i ++)
            {
                byte[] b2 = BitConverter.GetBytes(resAddress[i]);
                for (int q = 0; q < b2.Length; q++)
                {
                     if (q >= 4) break;
                     rlbByte[i*4+q] = b2[q];
                }
            }*/

            int pos = 16384;//4000h开始
            //resByte->rlbByte
            for (int j = 0; j < 116; j++)
            {
                //pos = resAddress[j];
                resAddress[j] = pos;
                byte[] b0 = BitConverter.GetBytes(resAddress[j]);
                for (int q = 0; q < b0.Length; q++)
                {
                    if (q >= 4) break;
                    rlbByte[j * 4 + q] = b0[q];
                }

                byte[] b1 = BitConverter.GetBytes(reshead[j].ResLen);
                for (int q = 0; q < b1.Length; q++)
                {
                    if (q >= 4) break;
                    rlbByte[pos + q] = b1[q];
                }
                pos += 4;
                byte[] b2 = BitConverter.GetBytes(reshead[j].ResId);
                for (int q = 0; q < b2.Length; q++)
                {
                    if (q >= 2) break;
                    rlbByte[pos + q] = b2[q];
                }
                pos += 2;
                byte[] b3 = BitConverter.GetBytes(reshead[j].ItmCnt);
                for (int q = 0; q < b3.Length; q++)
                {
                    if (q >= 2) break;
                    rlbByte[pos + q] = b3[q];
                }
                pos += 2;
                byte[] b4 = BitConverter.GetBytes(reshead[j].ItmLen);
                for (int q = 0; q < b4.Length; q++)
                {
                    if (q >= 2) break;
                    rlbByte[pos + q] = b4[q];
                }
                pos += 2;
                byte[] b5 = BitConverter.GetBytes(reshead[j].ResKey);
                for (int q = 0; q < b5.Length; q++)
                {
                    if (q >= 2) break;
                    rlbByte[pos + q] = b5[q];
                }

                pos += 2;

                for (int k = 0; k < reshead[j].ResLen; k++)
                {
                    rlbByte[pos + k] = resByte[j, k];
                }
                pos += reshead[j].ResLen;
            }

            rlbByteLen = pos;




            FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            BinaryWriter bw = new BinaryWriter(fs);
            for (int i = 0; i < rlbByteLen; i++)
            {
                bw.Write(rlbByte[i]);
            }

            bw.Close();
            fs.Close();
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            notSavedHero = false;
            if (tbxHeroName.Text != "" && cbxHeroBelong.SelectedIndex >= 0 && (!cannotSave) ) SaveHeroData();
            UpdateHero(60);
            UpdateHeroCon(62);
            int period = cbxHeroPeriod.SelectedIndex;
            if (period == 0) UpdateHeroName(61);
            if (period == 1) UpdateHeroName(69);
            if (period == 2) UpdateHeroName(70);
            if (period == 3) UpdateHeroName(71);
            UpdateHeroAtWhere(64);
            MessageBox.Show("保存成功");
        }

        private void UpdateHeroAtWhere(int resselected)
        {
            int idx = 0;

            for (int ji = 0; ji < 4; ji++)
            {
                for (int jk = 0; jk < 200; jk++)
                {
                    g_HeroQueue[ji,jk] = 0;
                }
            }


            for (int jj = 0; jj < 4; jj++)
            {
                idx = 0;
                for (int ii = 0; ii < 38; ii++)//城池 ii = 0为空闲
                {
                    city[jj, ii].Persons = 0;
                    city[jj, ii].PersonQueue = (byte)idx;
                    for (int kk = 0; kk < 200; kk++)//武将
                    {
                        if (hero[jj, kk].AppearCityId == (ii + 1))
                        {
                            city[jj, ii].Persons++;
                            g_HeroQueue[jj,idx] = (byte)kk;
                            idx++;
                        }

                    }
                }
            }

            UpdateCityHeroInfo(56);

            for (int yu = 0; yu < 4; yu++)
            {
                for (idx = 0; idx < 200; idx++)
                {
                    resByte[resselected, yu*200+idx] = g_HeroQueue[yu,idx];

                }
            }
        }

        private void UpdateHeroName(int resselected)
        {
            int idx = 0;
            int ridx = 0;

            if (resselected == 61) ridx = 0;
            if (resselected == 69) ridx = 1;
            if (resselected == 70) ridx = 2;
            if (resselected == 71) ridx = 3;

            byte reskey = 0xc3;
            
                for (int i = 0; i < 200; i++)
                {
                    byte[] b0 = System.Text.Encoding.Default.GetBytes(heroname[ridx, i]);

                    int len = b0.Length>8?8:b0.Length;
                    for (int k = 0; k < 8; k++)
                    {
                        resByte[resselected, idx + k] = reskey;
                        if (k < len)
                        {
                            resByte[resselected, idx + k] = (byte)(b0[k] + reskey);
                        }
                    }

                    idx += 8;

                }
            
        }

        
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            resmax = 0;
            resselected = -1;
            respicmax = 0;
            respicselected = -1;
            saved = false;
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void MyOpen(string filename)
        {
            FileStream fs;
            fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            BinaryReader br = new BinaryReader(fs);
            for (int i = 0; i < fs.Length; i++)
            {
                rlbByte[i] = br.ReadByte();
            }
            rlbByteLen = (int)fs.Length;

            br.Close();
            fs.Close();

            listBox1.Items.Clear();
            InitRlb();

        }

        private void InitRlb()
        {
            //head
            for (int i = 0; i < 116; i++)
            {
                byte[] tb = new byte[4];
                tb[0] = rlbByte[i * 4];
                tb[1] = rlbByte[i * 4 + 1];
                tb[2] = rlbByte[i * 4 + 2];
                tb[3] = rlbByte[i * 4 + 3];
                resAddress[i] = BitConverter.ToInt32(tb, 0);
            }
            //资源
            for (int j = 0; j < 116; j++)
            {
                int adr = resAddress[j];
                if (adr < 0)
                {
                    listBox1.Items.Add(reshead[j].ResId + "-NONE");
                    continue;
                }
                byte[] b1 = new byte[4];
                b1[0] = rlbByte[adr];
                b1[1] = rlbByte[adr + 1];
                b1[2] = rlbByte[adr + 2];
                b1[3] = rlbByte[adr + 3];
                reshead[j].ResLen = BitConverter.ToInt32(b1, 0);
                adr += 4;
                byte[] b2 = new byte[4];
                b2[0] = rlbByte[adr];
                b2[1] = rlbByte[adr + 1];
                b2[2] = 0;
                b2[3] = 0;
                reshead[j].ResId = BitConverter.ToInt32(b2, 0);
                adr += 2;
                byte[] b3 = new byte[4];
                b3[0] = rlbByte[adr];
                b3[1] = rlbByte[adr + 1];
                b3[2] = 0;
                b3[3] = 0;
                reshead[j].ItmCnt = BitConverter.ToInt32(b3, 0);
                adr += 2;
                byte[] b4 = new byte[4];
                b4[0] = rlbByte[adr];
                b4[1] = rlbByte[adr + 1];
                b4[2] = 0;
                b4[3] = 0;
                reshead[j].ItmLen = BitConverter.ToInt32(b4, 0);
                adr += 2;
                byte[] b5 = new byte[4];
                b5[0] = rlbByte[adr];
                b5[1] = rlbByte[adr + 1];
                b5[2] = 0;
                b5[3] = 0;
                reshead[j].ResKey = BitConverter.ToInt32(b5, 0);
                adr += 2;
                for (int k = adr; k < reshead[j].ResLen + adr; k++)
                {
                    resByte[j, k - adr] = rlbByte[k];
                }

                

                listBox1.Items.Add(reshead[j].ResId +"");

                

            }


            int[] canUseResId = { 61, 62, 70, 71, 72, 57, 58, 66, 73, 63, 67 };

            InitHero(60);
            InitHeroName(61);
            InitHeroName(69);
            InitHeroName(70);
            InitHeroName(71);
            InitHeroCon(62);
            InitCity(56);
            InitCityName(57);
            InitGood(65);
            InitGoodName(72);
            InitGoodCon(66);
            InitHeroAtWhere(64);
            InitGoodAtWhere(67);
            
            
        }

        private void InitGoodAtWhere(int resselected)
        {
            int idx = 0;

            for (int jj = 0; jj < 4; jj++)
            {
                for (idx = 0; idx < 33; idx++)
                {
                    g_GoodsQueue[jj,idx] = resByte[resselected, jj*33 + idx];

                }
            }


            for (int jj = 0; jj < 4; jj++)
            {
                idx = 0;
                for (int ii = 0; ii < 38; ii++)//城池
                {
                    int i, j;

                    j = city[jj,ii].ToolQueue;

                    for (i = 0; i < city[jj, ii].Tools; i++,j++)
                    {
                        good[jj, g_GoodsQueue[jj,j]].AppearCityId = Convert.ToByte(ii+1);
                        
                    }
                    
                }
            }
        }

        private void InitHeroAtWhere(int resselected)
        {
            int idx = 0;

            for (int jj = 0; jj < 4; jj++)
            {
                for (idx = 0; idx < 200; idx++)
                {
                    g_HeroQueue[jj, idx] = resByte[resselected, jj * 200 + idx];

                }
            }


            for (int jj = 0; jj < 4; jj++)
            {
                idx = 0;
                for (int ii = 0; ii < 38; ii++)//城池
                {
                    int i, j;

                    j = city[jj, ii].PersonQueue;

                    for (i = 0; i < city[jj, ii].Persons; i++, j++)
                    {
                        hero[jj, g_HeroQueue[jj, j]].AppearCityId = Convert.ToByte(ii+1);

                    }

                }
            }
        }

        private void 添加占位符ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

        }

        

        

        private void 占位符ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add((resmax + 1) + "");
            reshead[resmax].ItmCnt = 0;
            reshead[resmax].ResLen = 12;//头部12
            reshead[resmax].ResId = resmax;
            resmax++;
        }

        private void 资源项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //listBox1.Items.Add
            listBox1.Items.Add((resmax + 1) + "");
            reshead[resmax].ItmCnt = 0;
            reshead[resmax].ResLen = 12;//头部12
            reshead[resmax].ResId = resmax;
            resmax++;
            
        }

        

        private void ChangePicByteJPG(int sid,int spicidx)
        {
            FileStream fs;
            fs = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read);

            BinaryReader br = new BinaryReader(fs);

            for (int i = 0; i < fs.Length; i++)
            {
                tmpBmpByte[i] = br.ReadByte();
            }
            tmpBmpByteLen = (int)fs.Length;

            br.Close();
            fs.Close();

            byte[] byteArray = BitConverter.GetBytes(tmpBmpByteLen);
            for (int i = 0; i < 4; i++)
            {
                resPicByte[spicidx, i] = byteArray[i];
            }

            for (int i = 4; i < tmpBmpByteLen + 4; i++)
            {
                resPicByte[spicidx,  i] = tmpBmpByte[i - 4];
                if (tmpBmpByte[i - 4] == 0xff && tmpBmpByte[i - 3] == 0xc0  )
                {
                    //FF CO + 5
                    //width
                    resByte[sid, 1] = tmpBmpByte[i - 4 + 7];
                    resByte[sid, 0] = tmpBmpByte[i - 4 + 8];

                    //height
                    resByte[sid, 3] = tmpBmpByte[i - 4 + 5];
                    resByte[sid, 2] = tmpBmpByte[i - 4 + 6];

                }
            }
            //resPicByteLen[spicidx] = tmpBmpByteLen + 6 - 54;

            

            

            byte[] b1 = new byte[4];
            b1[0] = resByte[sid ,4];
            b1[1] = 0;
            b1[2] = 0;
            b1[3] = 0;
            int piccount = BitConverter.ToInt32(b1, 0);
            if (resByte[sid, 5] > 0) piccount *= 2;



            resPicByteLen[spicidx] = tmpBmpByteLen+4;

            reshead[sid].ResLen = 0;

            int pos = 6;
            for (int i = 0; i < piccount; i++)
            {
                for (int j = 0; j < resPicByteLen[i]; j++)
                {
                    resByte[sid, pos] = resPicByte[i, j];
                    pos++;
                }
                reshead[sid].ResLen += resPicByteLen[i];
            }
            reshead[sid].ResLen += 6;
            //edited = true;
        }

        private void ChangePicByte(int sid, int spicidx)
        {
            FileStream fs;
            fs = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read);

            BinaryReader br = new BinaryReader(fs);

            for (int i = 0; i < fs.Length; i++)
            {
                tmpBmpByte[i] = br.ReadByte();
            }
            tmpBmpByteLen = (int)fs.Length;

            br.Close();
            fs.Close();


            //width
            resByte[sid, 0] = tmpBmpByte[18];
            resByte[sid, 1] = tmpBmpByte[19];

            //height
            resByte[sid, 2] = tmpBmpByte[22];
            resByte[sid, 3] = tmpBmpByte[23];

            byte[] b0 = new byte[4];
            b0[0] = tmpBmpByte[18];
            b0[1] = tmpBmpByte[19];
            b0[2] = 0;
            b0[3] = 0;
            int width = BitConverter.ToInt32(b0, 0);

            byte[] b2 = new byte[4];
            b2[0] = tmpBmpByte[22];
            b2[1] = tmpBmpByte[23];
            b2[2] = 0;
            b2[3] = 0;
            int height = BitConverter.ToInt32(b2, 0);

            byte[] b1 = new byte[4];
            b1[0] = resByte[sid, 4];
            b1[1] = 0;
            b1[2] = 0;
            b1[3] = 0;
            int piccount = BitConverter.ToInt32(b1, 0);
            if (resByte[sid, 5] > 0) piccount *= 2;
            
            int width1 = width + 8 - width % 8;
            int tmp = 0;
            if ((width1 - width) % 8 == 0)
            {
                tmp = 1;
            }


            width = (width + 8 - width % 8) / 8;


            int width2 = 0;
            if ((width - tmp) % 4 != 0)
            {
                width2 = (width - tmp) + 4 - (width - tmp) % 4;
            }
            else
            {
                width2 = width - tmp;
            }

            int byteCount = height * (width2 -1);
            byte[] buffer1 = new byte[16384];

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < (width - tmp); j++)
                {
                    resPicByte[spicidx, (height - i - 1) * (width - tmp) + j] = (byte)(255 -(int)tmpBmpByte[i * width2 + j + 62]);
                }
            }

            
            resPicByteLen[spicidx] = byteCount;
            //resPicByteLen[spicidx] = tmpBmpByteLen + 6 - 54;

            reshead[sid].ResLen = 0;

            int pos = 6;
            for (int i = 0; i < piccount; i++)
            {
                for (int j = 0; j < resPicByteLen[i]; j++)
                {
                    resByte[sid, pos] = resPicByte[i, j];
                    pos++;
                }
                reshead[sid].ResLen += resPicByteLen[i];
            }
            reshead[sid].ResLen += 6;
            //edited = true;
        }

        private void ChangeSrsPicByteJPG(int sid, int spicidx)
        {
            
            byte[] byteArray = BitConverter.GetBytes(tmpBmpByteLen);
            for (int i = 0; i < 4; i++)
            {
                resPicByte[spicidx, i] = byteArray[i];
            }

            for (int i = 4; i < tmpBmpByteLen + 4; i++)
            {
                resPicByte[spicidx, i] = tmpBmpByte[i - 4];
                
            }

            //resPicByteLen[spicidx] = tmpBmpByteLen + 6 - 54;


            int startpos = 6;
            byte[] b1 = new byte[4];
            b1[0] = resByte[resselected, 2];
            b1[1] = 0;
            b1[2] = 0;
            b1[3] = 0;
            int srscount = BitConverter.ToInt32(b1, 0);

            byte[] b3 = new byte[4];
            b3[0] = resByte[resselected, 3];
            b3[1] = 0;
            b3[2] = 0;
            b3[3] = 0;
            int piccount = BitConverter.ToInt32(b3, 0);

            startpos = startpos + 5 * srscount;

            reshead[sid].ResLen = startpos;

            



            int pos = startpos;
            for (int i = 0; i < piccount; i++)
            {
                if (i == spicidx)
                {
                    //可用 FF CO + 3 获取长高
                    for (int ii = 4; ii < tmpBmpByteLen + 4; ii++)
                    {
                        resPicByte[spicidx, ii] = tmpBmpByte[ii - 4];
                        if (tmpBmpByte[ii - 4] == 0xff && tmpBmpByte[ii - 3] == 0xc0)
                        {
                            //FF CO + 5
                            //width
                            resByte[sid, pos + 1] = tmpBmpByte[ii - 4 + 7];
                            resByte[sid, pos + 0] = tmpBmpByte[ii - 4 + 8];

                            //height
                            resByte[sid, pos + 3] = tmpBmpByte[ii - 4 + 5];
                            resByte[sid, pos + 2] = tmpBmpByte[ii - 4 + 6];

                        }
                    }
                    //width
                    //resByte[sid, pos + 0] = 0;
                    //resByte[sid, pos + 1] = 0;

                    //height
                    //resByte[sid, pos + 2] = 0;
                    //resByte[sid, pos + 3] = 0;



                    resPicByteLen[spicidx] = tmpBmpByteLen + 4;

                }
                pos += 6;
                for (int j = 0; j < resPicByteLen[i]; j++)
                {
                    if (j >= 2000000) break;
                    resByte[sid, pos] = resPicByte[i, j];
                    pos++;
                }
                reshead[sid].ResLen += resPicByteLen[i];
                reshead[sid].ResLen += 6;
            }
            //edited = true;
        }

        private void ChangeSrsPicByteJPGMask(int sid, int spicidx,int fileLen1,int fileLen2)
        {
            int startpos1 = 0;
            int picpos1 = 4;
            int startpos2 = fileLen1 + 4;
            int picpos2 = fileLen1 + 8;

            byte[] byteArray = BitConverter.GetBytes(fileLen1);
            for (int i = 0; i < 4; i++)
            {
                resPicByte[spicidx, i] = byteArray[i];
            }

            for (int i = 4; i < fileLen1 + 4; i++)
            {
                resPicByte[spicidx, i] = tmpBmpByte[i - 4];

            }

            byteArray = BitConverter.GetBytes(fileLen2);
            for (int i = 0; i < 4; i++)
            {
                resPicByte[spicidx, i + startpos2] = byteArray[i];
            }

            for (int i = 4; i < fileLen2 + 4; i++)
            {
                resPicByte[spicidx, i + startpos2] = tmpBmpByte[i + fileLen1 - 4];

            }

            //resPicByteLen[spicidx] = tmpBmpByteLen + 6 - 54;


            int startpos = 6;
            byte[] b1 = new byte[4];
            b1[0] = resByte[resselected, 2];
            b1[1] = 0;
            b1[2] = 0;
            b1[3] = 0;
            int srscount = BitConverter.ToInt32(b1, 0);

            byte[] b3 = new byte[4];
            b3[0] = resByte[resselected, 3];
            b3[1] = 0;
            b3[2] = 0;
            b3[3] = 0;
            int piccount = BitConverter.ToInt32(b3, 0);

            startpos = startpos + 5 * srscount;

            reshead[sid].ResLen = startpos;





            int pos = startpos;
            for (int i = 0; i < piccount; i++)
            {
                if (i == spicidx)
                {
                    //可用 FF CO + 3 获取长高
                    for (int ii = 4; ii < fileLen1 + 4; ii++)
                    {
                        resPicByte[spicidx, ii] = tmpBmpByte[ii - 4];
                        if (tmpBmpByte[ii - 4] == 0xff && tmpBmpByte[ii - 3] == 0xc0)
                        {
                            //FF CO + 5
                            //width
                            resByte[sid, pos + 1] = tmpBmpByte[ii - 4 + 7];
                            resByte[sid, pos + 0] = tmpBmpByte[ii - 4 + 8];

                            //height
                            resByte[sid, pos + 3] = tmpBmpByte[ii - 4 + 5];
                            resByte[sid, pos + 2] = tmpBmpByte[ii - 4 + 6];

                            //mask
                            resByte[sid, pos + 5] = 1;
                        }
                    }

                    for (int ii = startpos2 + 4; ii < fileLen1 + fileLen2 + 4; ii++)
                    {
                        resPicByte[spicidx, ii] = tmpBmpByte[ii - 4 - 4];
                        
                    }
                    //width
                    //resByte[sid, pos + 0] = 0;
                    //resByte[sid, pos + 1] = 0;

                    //height
                    //resByte[sid, pos + 2] = 0;
                    //resByte[sid, pos + 3] = 0;



                    resPicByteLen[spicidx] = fileLen1 + fileLen2 + 8;

                }
                pos += 6;
                for (int j = 0; j < resPicByteLen[i]; j++)
                {
                    if (j >= 2000000) break;
                    resByte[sid, pos] = resPicByte[i, j];
                    pos++;
                }
                reshead[sid].ResLen += resPicByteLen[i];
                reshead[sid].ResLen += 6;
            }
            //edited = true;
        }

        private void ChangeSrsPicByte(int sid, int spicidx)
        {

            for (int i = 0; i < tmpBmpByteLen + 6 - 54; i++)
            {
                resPicByte[spicidx, i] = tmpBmpByte[i + 54];
            }
            //resPicByteLen[spicidx] = tmpBmpByteLen + 6 - 54;


            int startpos = 6;
            byte[] b1 = new byte[4];
            b1[0] = resByte[resselected, 2];
            b1[1] = 0;
            b1[2] = 0;
            b1[3] = 0;
            int srscount = BitConverter.ToInt32(b1, 0);

            byte[] b3 = new byte[4];
            b3[0] = resByte[resselected, 3];
            b3[1] = 0;
            b3[2] = 0;
            b3[3] = 0;
            int piccount = BitConverter.ToInt32(b3, 0);

            startpos = startpos + 5 * srscount;

            reshead[sid].ResLen = startpos;





            int pos = startpos;
            for (int i = 0; i < piccount; i++)
            {
                if (i == spicidx)
                {
                    //width
                    resByte[sid, pos + 0] = tmpBmpByte[18];
                    resByte[sid, pos + 1] = tmpBmpByte[19];

                    //height
                    resByte[sid, pos + 2] = tmpBmpByte[22];
                    resByte[sid, pos + 3] = tmpBmpByte[23];

                    byte[] b0 = new byte[4];
                    b0[0] = tmpBmpByte[18];
                    b0[1] = tmpBmpByte[19];
                    b0[2] = 0;
                    b0[3] = 0;
                    int width = BitConverter.ToInt32(b0, 0);

                    byte[] b2 = new byte[4];
                    b2[0] = tmpBmpByte[22];
                    b2[1] = tmpBmpByte[23];
                    b2[2] = 0;
                    b2[3] = 0;
                    int height = BitConverter.ToInt32(b2, 0);


                    int width2 = (width * 24 + 31) / 32 * 4;//每行字节数
                    int byteCount = height * width2;

                    resPicByteLen[spicidx] = byteCount;// +6;

                }
                pos += 6;
                for (int j = 0; j < resPicByteLen[i]; j++)
                {
                    if (j >= 2000000) break;
                    resByte[sid, pos] = resPicByte[i, j];
                    pos++;
                }
                reshead[sid].ResLen += resPicByteLen[i];
            }

            //edited = true;
        }

        

        

        private void 批量添加普通图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {

                string[] filenames = openFileDialog3.FileNames;
                int residt = 0;
                foreach (string filename in filenames)
                {
                    //File fileOpen = new File(fName);
                    FileStream fs;
                    fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                    BinaryReader br = new BinaryReader(fs);

                    for (int i = 0; i < fs.Length; i++)
                    {
                        tmpBmpByte[i] = br.ReadByte();
                    }
                    tmpBmpByteLen = (int)fs.Length;

                    br.Close();
                    fs.Close();

                    respicselected = residt;
                    residt ++;
                    //listBox2.Items.Add(respicselected + "");
                    //respicmax++;

                    //ChangePicByte(resselected, respicselected);
                    ChangePicByteJPG(resselected, respicselected);

                    //int curpos = reshead[resselected].ResLen;
                    //reshead[resmax].ItmCnt++;
                    //listBox2.Items.Add((resmax + 1) + "");
                    //resmax++;
                }


            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            批量添加普通图片ToolStripMenuItem_Click(sender, e);
        }

        private void rLBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog2.FileName;
                //File fileOpen = new File(fName);
                MyOpen(filename);
                isRlb = true;
            }
        }

        private void lIBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void 图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 批量添加特效图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {
                string[] filenames = openFileDialog3.FileNames;
                int residt = 0;
                foreach (string filename in filenames)
                {
                    //string filename = openFileDialog1.FileName;
                    //File fileOpen = new File(fName);
                    FileStream fs;
                    fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                    BinaryReader br = new BinaryReader(fs);

                    for (int i = 0; i < fs.Length; i++)
                    {
                        tmpBmpByte[i] = br.ReadByte();
                    }
                    tmpBmpByteLen = (int)fs.Length;

                    br.Close();
                    fs.Close();

                    respicselected = residt;
                    residt++;
                    //listBox2.Items.Add(respicselected + "");
                    //respicmax++;

                    //ChangeSrsPicByte(resselected, respicselected);
                    ChangeSrsPicByteJPG(resselected, respicselected);

                    //int curpos = reshead[resselected].ResLen;
                    //reshead[resmax].ItmCnt++;
                    //listBox2.Items.Add((resmax + 1) + "");
                    //resmax++;
                }


            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            批量添加特效图片ToolStripMenuItem_Click(sender, e);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitPanel(3);
            InitPanel(2);
            InitPanel(1);
            InitPanel(0);
            
            
        }

        private void InitPanel(int type)
        {
            cannotSave = true;
            if (type == 0)
            {

                if(cbxHeroPeriod.Items.Count>0)cbxHeroPeriod.SelectedIndex = 0;
                cbxHeroId.Value = 0;
                g_idHero = 0;
            }
            if (type == 1)
            {
                if (cbxCityPeriod.Items.Count > 0) cbxCityPeriod.SelectedIndex = 0;
                cbxCityId.Value = 0;
                g_idCity = 0;
            }
            if (type == 2)
            {
                if (cbxGoodPeriod.Items.Count > 0) cbxGoodPeriod.SelectedIndex = 0;
                cbxGoodId.Value = 0;
                g_idGood = 0;
            }
            if (type == 3)
            {
                if (cbxHeadPeriod.Items.Count > 0) cbxHeadPeriod.SelectedIndex = 0;
                
            }
            cannotSave = false;
        }



        

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (noRlb == true)
            {
                return;
            }
            /*if ( notSavedHero == true)
            {
                if (MessageBox.Show("是否不保存当前时期的数据修改就改变时期？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {

                }
                else
                {
                    return;
                }
            }*/
            notSavedHero = true;

            cannotSave = true;
            cbxHeroId.Value = 0;
            SetHeroInfo();
            SetHeroData();
            tbxHeroName.Text = heroname[cbxHeroPeriod.SelectedIndex, (int)cbxHeroId.Value];
            g_idHero = 0;

            cannotSave = false;
        }

        private void SetHeroInfo()
        {
            int period = cbxHeroPeriod.SelectedIndex;
            //cbxHeroId.Items.Clear();
            cbxHeroBelong.Items.Clear();
            cbxHeroSearchId.Items.Clear();
            cbxHeroAppearCityId.Items.Clear();
            cbxHeroEquip1.Items.Clear();
            cbxHeroEquip2.Items.Clear();

            cbxHeroSearchId.Items.Add("无");
            cbxHeroBelong.Items.Add("无");
            cbxHeroAppearCityId.Items.Add("无");
            cbxHeroEquip1.Items.Add("无");
            cbxHeroEquip2.Items.Add("无");

            for (int i = 0; i < 200; i++)
            {
                //cbxHeroId.Items.Add(i+1 +"");
                cbxHeroBelong.Items.Add(heroname[cbxHeroPeriod.SelectedIndex, i]);
                cbxHeroSearchId.Items.Add(heroname[cbxHeroPeriod.SelectedIndex, i]);
                
            }

            for (int i = 0; i < 38; i++)
            {
                cbxHeroAppearCityId.Items.Add(cityname[i]);
            }

            for (int i = 0; i < 33; i++)
            {
                cbxHeroEquip1.Items.Add(goodname[i]);
                cbxHeroEquip2.Items.Add(goodname[i]);
            }

        }

       

        private void SaveHeroData()
        {
            int pid = cbxHeroPeriod.SelectedIndex;
            int hid = g_idHero;
            heroname[pid, hid] = tbxHeroName.Text;
            //cbxHeroBelong.Text = heroname[pid, hero[pid, hid].Belong - 1];
            //int t_pos = cbxHeroBelong.SelectedIndex < 0 ? 0 : cbxHeroBelong.SelectedIndex;
            hero[pid, hid].Belong = (byte)(cbxHeroBelong.SelectedIndex + 0);
            hero[pid, hid].Level = Convert.ToByte(tbxHeroDegree.Text);
            hero[pid, hid].Force = Convert.ToByte(tbxHeroForce.Text);
            hero[pid, hid].IQ = Convert.ToByte(tbxHeroIQ.Text);
            hero[pid, hid].Devotion = Convert.ToByte(tbxHeroDevotion.Text);
            hero[pid, hid].Character = (byte)(cbxHeroCharacter.SelectedIndex  ) ;
            hero[pid, hid].ArmsType = (byte)(cbxHeroArmsType.SelectedIndex  ) ;
            hero[pid, hid].Age = Convert.ToByte(tbxHeroAge.Text);
            hero[pid, hid].Equip[0] = (byte)(cbxHeroEquip1.SelectedIndex + 0);
            hero[pid, hid].Equip[1] = (byte)(cbxHeroEquip2.SelectedIndex + 0);

            hero[pid, hid].BornYear = Convert.ToByte(tbxHeroBornYear.Text);
            //int t_pos2 = cbxHeroSearchId.SelectedIndex < 0 ? 0 : cbxHeroSearchId.SelectedIndex;
            hero[pid, hid].SearchId = (byte)(cbxHeroSearchId.SelectedIndex + 0);
            //int t_pos1 = cbxHeroAppearCityId.SelectedIndex < 0 ? 0 : cbxHeroAppearCityId.SelectedIndex;
            hero[pid, hid].AppearCityId = (byte)(cbxHeroAppearCityId.SelectedIndex + 0);
        }

        private void SetHeroData()
        {
            int pid = cbxHeroPeriod.SelectedIndex;
            int hid = (int)cbxHeroId.Value;//.SelectedIndex;
            //cbxHeroBelong.Text = heroname[pid, hero[pid, hid].Belong - 1];
            cbxHeroBelong.SelectedIndex = hero[pid, hid].Belong - 0;
            tbxHeroDegree.Text = hero[pid, hid].Level + "";
            tbxHeroForce.Text = hero[pid, hid].Force + "";
            tbxHeroIQ.Text = hero[pid, hid].IQ + "";
            tbxHeroDevotion.Text = hero[pid, hid].Devotion + "";
            cbxHeroCharacter.SelectedIndex = hero[pid, hid].Character;
            cbxHeroArmsType.SelectedIndex = hero[pid, hid].ArmsType;
            tbxHeroAge.Text = hero[pid, hid].Age + "";
            cbxHeroEquip1.SelectedIndex = hero[pid, hid].Equip[0] - 0;
            cbxHeroEquip2.SelectedIndex = hero[pid, hid].Equip[1] - 0;
            tbxHeroBornYear.Text = hero[pid, hid].BornYear + "";
            cbxHeroSearchId.SelectedIndex = hero[pid, hid].SearchId - 0;
            cbxHeroAppearCityId.SelectedIndex = hero[pid, hid].AppearCityId - 0;
        }

        private void SaveSavHeroData()
        {
            int hid = g_idSavHero;
            savheroname[hid] = tbxSaveHeroName.Text;
            //cbxHeroBelong.Text = heroname[pid, hero[pid, hid].Belong - 1];
            savhero[hid].Belong = (byte)(cbxSaveHeroBelong.SelectedIndex + 0);
            savhero[hid].Level = Convert.ToByte(tbxSaveHeroDegree.Text);
            savhero[hid].Force = Convert.ToByte(tbxSaveHeroForce.Text);
            savhero[hid].IQ = Convert.ToByte(tbxSaveHeroIQ.Text);
            savhero[hid].Devotion = Convert.ToByte(tbxSaveHeroDevotion.Text);
            savhero[hid].Character = (byte)(cbxSaveHeroCharacter.SelectedIndex);
            savhero[hid].ArmsType = (byte)(cbxSaveHeroArmsType.SelectedIndex);
            savhero[hid].Age = Convert.ToByte(tbxSaveHeroAge.Text);
            //savhero[hid].AppearCityId = (byte)(cbxSaveHeroAppearCityId.SelectedIndex + 1);
            //savhero[hid].SearchId = (byte)(cbxSaveHeroSearchId.SelectedIndex + 1);
        }

        private void SetSavHeroData()
        {
            int hid = (int)cbxSaveHeroId.Value;
            //cbxHeroBelong.Text = heroname[pid, hero[pid, hid].Belong - 1];
            cbxSaveHeroBelong.SelectedIndex = savhero[hid].Belong - 0;
            tbxSaveHeroDegree.Text = savhero[hid].Level + "";
            tbxSaveHeroForce.Text = savhero[hid].Force + "";
            tbxSaveHeroIQ.Text = savhero[hid].IQ + "";
            tbxSaveHeroDevotion.Text = savhero[hid].Devotion + "";
            cbxSaveHeroCharacter.SelectedIndex = savhero[hid].Character;
            cbxSaveHeroArmsType.SelectedIndex = savhero[hid].ArmsType;
            tbxSaveHeroAge.Text = savhero[hid].Age + "";
            //cbxSaveHeroAppearCityId.SelectedIndex = savhero[hid].AppearCityId - 1;
            //cbxSaveHeroSearchId.SelectedIndex = savhero[hid].SearchId - 1;
        }

        private void InitResPic(int resselected, int no, int type)//type=1 srs
        {
            if (type == 0)
            {
                byte[] b1 = new byte[4];
                b1[0] = resByte[resselected, 0];
                b1[1] = resByte[resselected, 1];
                b1[2] = 0;
                b1[3] = 0;
                int width = BitConverter.ToInt32(b1, 0);

                byte[] b2 = new byte[4];
                b2[0] = resByte[resselected, 2];
                b2[1] = resByte[resselected, 3];
                b2[2] = 0;
                b2[3] = 0;
                int height = BitConverter.ToInt32(b2, 0);

                int width1 = width + 8 - width % 8;
                int tmp = 0;
                if ((width1 - width) % 8 == 0) tmp = 1;

                int byteCount = ((width1 / 8 - tmp) * height);

                for (int i = 0; i < byteCount; i++)
                {
                    resPicByte[no, i] = resByte[resselected, i + 6 + no * byteCount];
                }
                resPicByteLen[no] = byteCount;
            }

        }

        private void InitResPicJPG(int resselected, int no, int type)//type=1 srs
        {
            if (type == 0)
            {
                byte[] b1 = new byte[4];
                b1[0] = resByte[resselected, 4];
                b1[1] = 0;
                b1[2] = 0;
                b1[3] = 0;
                int picCount = BitConverter.ToInt32(b1, 0);

                int picLen = 0;
                int cur = 6;

                for (int i = 0; i < no; i++)//计算之前的偏移量
                {
                    byte[] b8 = new byte[4];
                    b8[0] = resByte[resselected, cur];
                    b8[1] = resByte[resselected, cur + 1];
                    b8[2] = resByte[resselected, cur + 2];
                    b8[3] = resByte[resselected, cur + 3];

                    picLen = BitConverter.ToInt32(b8, 0) + 4;//图片大小
                    if (picLen > 2000000 || picLen < 0) picLen = 0;
                    cur += picLen;
                }

                byte[] b18 = new byte[4];
                b18[0] = resByte[resselected, cur];
                b18[1] = resByte[resselected, cur + 1];
                b18[2] = resByte[resselected, cur + 2];
                b18[3] = resByte[resselected, cur + 3];

                picLen = BitConverter.ToInt32(b18, 0) + 4;//图片大小
                if (picLen > 2000000 || picLen < 0) picLen = 0;

                for (int i = 0; i < picLen; i++)
                {
                    resPicByte[no, i] = resByte[resselected, i + cur];
                }
                resPicByteLen[no] = picLen;

            }

        }

        private void SaveHead(int index, int picno)
        {
            MyBitMap myBitMap = new MyBitMap();
            int picLen = resPicByteLen[picno] + 6;//reshead[index].ResLen;
            for (int i = 6; i < picLen; i++)
            {
                int tmp = 255 - Convert.ToInt32(resPicByte[picno, i - 6]);//resByte[index,i]);
                myBitMap.biImage[i - 6] = Convert.ToByte(tmp);
            }


            myBitMap.bfSize = BitConverter.GetBytes(62 + picLen - 6);
            myBitMap.biSizeImage = BitConverter.GetBytes(picLen - 6);
            myBitMap.biWidth = BitConverter.GetBytes(Convert.ToInt32(resByte[index, 0]));
            myBitMap.biHeight = BitConverter.GetBytes(Convert.ToInt32(resByte[index, 2]));

            Image im = myBitMap.BytesToImage(picno);
            if (im == null) return;

            if (saveFileDialog2.ShowDialog() == DialogResult.OK)
            {
                string FileName = saveFileDialog2.FileName;

                im.Save(FileName,System.Drawing.Imaging.ImageFormat.Bmp);
                MessageBox.Show("保存成功");
            }

            

            
        }

        private void SaveHeadAuto(int index, int picno,string path)
        {
            MyBitMap myBitMap = new MyBitMap();
            int picLen = resPicByteLen[picno] + 6;//reshead[index].ResLen;
            for (int i = 6; i < picLen; i++)
            {
                int tmp = 255 - Convert.ToInt32(resPicByte[picno, i - 6]);//resByte[index,i]);
                myBitMap.biImage[i - 6] = Convert.ToByte(tmp);
            }


            myBitMap.bfSize = BitConverter.GetBytes(62 + picLen - 6);
            myBitMap.biSizeImage = BitConverter.GetBytes(picLen - 6);
            myBitMap.biWidth = BitConverter.GetBytes(Convert.ToInt32(resByte[index, 0]));
            myBitMap.biHeight = BitConverter.GetBytes(Convert.ToInt32(resByte[index, 2]));

            Image im = myBitMap.BytesToImage(picno);
            if (im == null) return;

            string FileName = path + @"\" + (index - 47) + "_" + picno + ".bmp";

            im.Save(FileName, System.Drawing.Imaging.ImageFormat.Bmp);




        }



        private void SaveHeadJPG(int index, int picno)
        {
            //MyBitMap myBitMap = new MyBitMap();
            byte[] b1 = new byte[4];
            b1[0] = resPicByte[picno, 0];
            b1[1] = resPicByte[picno, 1];
            b1[2] = resPicByte[picno, 2];
            b1[3] = resPicByte[picno, 3];
            int picLen = BitConverter.ToInt32(b1, 0);
            if (picLen > 10485760) picLen = 0;
            Byte[] buffer = new Byte[10485760];//19200];
            for (int i = 4; i < picLen + 4; i++)
            {
                buffer[i - 4] = resPicByte[picno, i];
            }

            try
            {
                MemoryStream ms = new MemoryStream(buffer);
                //FileStream fs = File.Create("D:\\1.qq");
                //fs.Write(buffer, 0, buffer.Length);


                Image im = System.Drawing.Image.FromStream(ms);

                ms.Close();



                Graphics g = this.pbxHead.CreateGraphics();
                g.Clear(Color.White);
                g.DrawImage(im, 0, 0);
                g.Dispose();
            }
            catch { }
        }
        

        private void ShowInBox(int index, int picno)
        {
            MyBitMap myBitMap = new MyBitMap();
            int picLen = resPicByteLen[picno] + 6;//reshead[index].ResLen;
            for (int i = 6; i < picLen; i++)
            {
                int tmp = 255 - Convert.ToInt32(resPicByte[picno, i - 6]);//resByte[index,i]);
                myBitMap.biImage[i - 6] = Convert.ToByte(tmp);
            }


            myBitMap.bfSize = BitConverter.GetBytes(62 + picLen - 6);
            myBitMap.biSizeImage = BitConverter.GetBytes(picLen - 6);
            myBitMap.biWidth = BitConverter.GetBytes(Convert.ToInt32(resByte[index, 0]));
            myBitMap.biHeight = BitConverter.GetBytes(Convert.ToInt32(resByte[index, 2]));

            Image im = myBitMap.BytesToImage(picno);
            if (im == null) return;
            Graphics g = this.pbxHead.CreateGraphics();
            g.Clear(Color.White);
            Rectangle destRect = new Rectangle(0, 0, pbxHead.Width, pbxHead.Height);
            g.DrawImage(im, destRect);
            
            g.Dispose();
        }

        

        private void ShowInBoxJPG(int index, int picno)
        {
            //MyBitMap myBitMap = new MyBitMap();
            byte[] b1 = new byte[4];
            b1[0] = resPicByte[picno, 0];
            b1[1] = resPicByte[picno, 1];
            b1[2] = resPicByte[picno, 2];
            b1[3] = resPicByte[picno, 3];
            int picLen = BitConverter.ToInt32(b1, 0);
            if (picLen > 10485760) picLen = 0;
            Byte[] buffer = new Byte[10485760];//19200];
            for (int i = 4; i < picLen + 4; i++)
            {
                buffer[i - 4] = resPicByte[picno, i];
            }

            try
            {
                MemoryStream ms = new MemoryStream(buffer);
                //FileStream fs = File.Create("D:\\1.qq");
                //fs.Write(buffer, 0, buffer.Length);


                Image im = System.Drawing.Image.FromStream(ms);

                ms.Close();



                Graphics g = this.pbxHead.CreateGraphics();
                g.Clear(Color.White);
                Rectangle destRect = new Rectangle(0, 0, pbxHead.Width, pbxHead.Height);
                g.DrawImage(im, destRect);
                g.Dispose();
            }
            catch { }
        }

        private void cbxCityPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (noRlb == true)
            {
                return;
            }
            cannotSave = true;

            cbxCityId.Value = 0;

            
            SetCityInfo();
            SetCityData();
            tbxCityName.Text = cityname[(int)cbxCityId.Value];
            g_idCity = 0;

            cannotSave = false;
        }

        private void SetCityInfo()
        {
            int period = cbxCityPeriod.SelectedIndex;
            cbxCityBelong.Items.Clear();

            cbxCityBelong.Items.Add("无");
            for (int i = 0; i < 200; i++)
            {
                cbxCityBelong.Items.Add(heroname[cbxCityPeriod.SelectedIndex, i]);
            }
        }

       
        private void SaveCityData()
        {
            int pid = cbxCityPeriod.SelectedIndex;
            int hid = g_idCity;
            cityname[hid] = tbxCityName.Text;
            city[pid, hid].Belong = (byte)(cbxCityBelong.SelectedIndex +0);
            city[pid, hid].PeopleDevotion = Convert.ToByte(tbxCityPeopleDevotion.Text);
            city[pid, hid].FarmingLimit = Convert.ToUInt16(tbxCityFarmingLimit.Text);
            city[pid, hid].Farming = Convert.ToUInt16(tbxCityFarming.Text);
            city[pid, hid].CommerceLimit = Convert.ToUInt16(tbxCityCommerceLimit.Text);
            city[pid, hid].Commerce = Convert.ToUInt16(tbxCityCommerce.Text);
            city[pid, hid].PopulationLimit = Convert.ToUInt32(tbxCityPopulationLimit.Text);
            city[pid, hid].Population = Convert.ToUInt32(tbxCityPopulation.Text);
            city[pid, hid].AvoidCalamity = Convert.ToByte(tbxCityAvoidCalamity.Text);
            city[pid, hid].Money = Convert.ToUInt16(tbxCityMoney.Text);
            city[pid, hid].Food = Convert.ToUInt16(tbxCityFood.Text);
            city[pid, hid].MothballArms = Convert.ToUInt16(tbxCityMothballArms.Text);
        }

        private void SetCityData()
        {
            int pid = cbxCityPeriod.SelectedIndex;
            int hid = (int)cbxCityId.Value ;
            //cbxHeroBelong.Text = heroname[pid, hero[pid, hid].Belong - 1];
            cbxCityBelong.SelectedIndex = city[pid, hid].Belong - 0;
            tbxCityPeopleDevotion.Text = city[pid, hid].PeopleDevotion + "";
            tbxCityFarmingLimit.Text = city[pid, hid].FarmingLimit + "";
            tbxCityFarming.Text = city[pid, hid].Farming + "";
            tbxCityCommerceLimit.Text = city[pid, hid].CommerceLimit + "";
            tbxCityCommerce.Text = city[pid, hid].Commerce + "";
            tbxCityPopulationLimit.Text = city[pid, hid].PopulationLimit + "";
            tbxCityPopulation.Text = city[pid, hid].Population + "";
            tbxCityAvoidCalamity.Text = city[pid, hid].AvoidCalamity + "";
            tbxCityMoney.Text = city[pid, hid].Money + "";
            tbxCityFood.Text = city[pid, hid].Food + "";
            tbxCityMothballArms.Text = city[pid, hid].MothballArms + "";
           
        }

        private void SaveSavCityData()
        {
            int hid = g_idSavCity;
            savcityname[hid] = tbxSaveCityName.Text;
            savcity[hid].Belong = (byte)(cbxSaveCityBelong.SelectedIndex + 0);
            savcity[hid].PeopleDevotion = Convert.ToByte(cbxSaveCityPeopleDevotion.Text);
            savcity[hid].FarmingLimit = Convert.ToUInt16(tbxSaveCityFarmingLimit.Text);
            savcity[hid].Farming = Convert.ToUInt16(tbxSaveCityFarming.Text);
            savcity[hid].CommerceLimit = Convert.ToUInt16(tbxSaveCityCommerceLimit.Text);
            savcity[hid].Commerce = Convert.ToUInt16(tbxSaveCityCommerce.Text);
            savcity[hid].PopulationLimit = Convert.ToUInt32(tbxSaveCityPopulationLimit.Text);
            savcity[hid].Population = Convert.ToUInt32(tbxSaveCityPopulation.Text);
            savcity[hid].AvoidCalamity = Convert.ToByte(tbxSaveCityAvoidCalamity.Text);
            savcity[hid].Money = Convert.ToUInt16(tbxSaveCityMoney.Text);
            savcity[hid].Food = Convert.ToUInt16(tbxSaveCityFood.Text);
            savcity[hid].MothballArms = Convert.ToUInt16(tbxSaveCityMothballArms.Text);
        }

        private void SetSavCityData()
        {
            int hid = (int)cbxSaveCityId.Value;// SelectedIndex;
            //cbxHeroBelong.Text = heroname[pid, hero[pid, hid].Belong - 1];
            cbxSaveCityBelong.SelectedIndex = savcity[hid].Belong;
            cbxSaveCityPeopleDevotion.Text = savcity[hid].PeopleDevotion + "";
            tbxSaveCityFarmingLimit.Text = savcity[hid].FarmingLimit + "";
            tbxSaveCityFarming.Text = savcity[hid].Farming + "";
            tbxSaveCityCommerceLimit.Text = savcity[hid].CommerceLimit + "";
            tbxSaveCityCommerce.Text = savcity[hid].Commerce + "";
            tbxSaveCityPopulationLimit.Text = savcity[hid].PopulationLimit + "";
            tbxSaveCityPopulation.Text = savcity[hid].Population + "";
            tbxSaveCityAvoidCalamity.Text = savcity[hid].AvoidCalamity + "";
            tbxSaveCityMoney.Text = savcity[hid].Money + "";
            tbxSaveCityFood.Text = savcity[hid].Food + "";
            tbxSaveCityMothballArms.Text = savcity[hid].MothballArms + "";

        }

        private void cbxGoodPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (noRlb == true)
            {
                return;
            }
            cannotSave = true;

            cbxGoodId.Value = 0;
            SetGoodInfo();
            SetGoodData();
            tbxGoodName.Text = goodname[(int)cbxGoodId.Value];
            g_idGood = 0;

            cannotSave = false;
        }

        private void SetGoodInfo()
        {
            int period = cbxGoodPeriod.SelectedIndex;
            //cbxGoodId.Items.Clear();
            cbxGoodAppearCityId.Items.Clear();
            cbxGoodSearchId.Items.Clear();
            for (int i = 0; i < 33; i++)
            {
                //cbxGoodId.Items.Add(i + 1 + "");
            }

            cbxGoodAppearCityId.Items.Add("无");
            for (int i = 0; i < 38; i++)
            {
                cbxGoodAppearCityId.Items.Add(cityname[i]);
            }
            cbxGoodSearchId.Items.Add("无");
            for (int i = 0; i < 200; i++)
            {
                cbxGoodSearchId.Items.Add(heroname[cbxGoodPeriod.SelectedIndex, i]);
            }
            
        }

        
        private void SaveGoodData()
        {
            int pid = cbxGoodPeriod.SelectedIndex;
            int hid = g_idGood;
            goodname[hid] = tbxGoodName.Text;
            good[0, hid].useflag = (byte)(cbxGoodUseFlag.SelectedIndex);
            good[0, hid].at = Convert.ToByte(tbxGoodAt.Text);
            good[0, hid].iq = Convert.ToByte(tbxGoodIQ.Text);
            good[0, hid].move = Convert.ToByte(tbxGoodMove.Text);
            good[0, hid].arm = (byte)(cbxGoodArm.SelectedIndex);

            good[pid, hid].AppearYear = Convert.ToByte(tbxGoodAppearYear.Text);
            good[pid, hid].SearchId = (byte)(cbxGoodSearchId.SelectedIndex + 0);
            good[pid, hid].AppearCityId = (byte)(cbxGoodAppearCityId.SelectedIndex + 0);
        }

        private void SetGoodData()
        {
            int pid = cbxGoodPeriod.SelectedIndex;
            int hid = (int)cbxGoodId.Value;// SelectedIndex;
            //cbxHeroBelong.Text = heroname[pid, hero[pid, hid].Belong - 1];
            cbxGoodUseFlag.SelectedIndex = good[0, hid].useflag;
            tbxGoodAt.Text = good[0, hid].at + "";
            tbxGoodIQ.Text = good[0, hid].iq + "";
            tbxGoodMove.Text = good[0, hid].move + "";
            cbxGoodArm.SelectedIndex = good[0, hid].arm;

            tbxGoodAppearYear.Text = good[pid, hid].AppearYear + "";
            cbxGoodSearchId.SelectedIndex = good[pid, hid].SearchId - 0;
            cbxGoodAppearCityId.SelectedIndex = good[pid, hid].AppearCityId - 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (tbxCityName.Text != "" && cbxCityBelong.SelectedIndex >= 0 && (!cannotSave)) SaveCityData();
            UpdateCity(56);
            UpdateCityName(57);
            MessageBox.Show("保存成功");
        }

        private void UpdateCityName(int resselected)
        {
            int idx = 0;

            byte reskey = 0xc0;

            for (int i = 0; i < 38; i++)
            {
                byte[] b0 = System.Text.Encoding.Default.GetBytes(cityname[i]);

                int len = b0.Length > 10 ? 10 : b0.Length;
                for (int k = 0; k < 10; k++)
                {
                    resByte[resselected, idx + k] = 0xc0;
                    if (k < len)
                    {
                        resByte[resselected, idx + k] = (byte)(b0[k] + reskey);
                    }
                }

                idx += 10;

            }

        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            if (tbxGoodName.Text != "" && cbxGoodAppearCityId.SelectedIndex >= 0 && (!cannotSave)) SaveGoodData();
            UpdateGood(65);
            UpdateGoodCon(66);
            UpdateGoodName(72);
            UpdateGoodAtWhere(67);
            MessageBox.Show("保存成功");
        }

        private void UpdateGoodAtWhere(int resselected)
        {
            int idx = 0;

            for (int ji = 0; ji < 4; ji++)
            {
                for (int jk = 0; jk < 33; jk++)
                {
                    g_GoodsQueue[ji,jk] = 0;
                }
            }


            for (int jj = 0; jj < 4; jj++)
            {
                idx = 0;
                for (int ii = 0; ii < 38; ii++)//城池
                {
                    city[jj, ii].Tools = 0;
                    city[jj, ii].ToolQueue = (byte)idx;

                    for (int kk = 0; kk < 33; kk++)//道具
                    {
                        if (good[jj, kk].AppearCityId == (ii + 1))
                        {
                            city[jj, ii].Tools++;
                            g_GoodsQueue[jj,idx] = (byte)kk;
                            idx++;
                        }
                        

                    }

                }
            }

            UpdateCityGoodInfo(56);

            for (int yu = 0; yu < 4; yu++)
            {
                for (idx = 0; idx < 33; idx++)
                {
                    resByte[resselected, yu*33 + idx] = g_GoodsQueue[yu,idx];

                }
            }

        }

        private void UpdateGoodName(int resselected)
        {
            int idx = 0;


            byte reskey = 0xc3;

            for (int i = 0; i < 33; i++)
            {
                byte[] b0 = System.Text.Encoding.Default.GetBytes(goodname[i]);

                int len = b0.Length > 10 ? 10 : b0.Length;
                for (int k = 0; k < 10; k++)
                {
                    resByte[resselected, idx + k] = 0xc3;
                    if (k < len)
                    {
                        resByte[resselected, idx + k] = (byte)(b0[k] + reskey);
                    }
                }

                idx += 10;

            }

        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitPanel(tabControl1.SelectedIndex);
        }

        private void 关于天际边三国霸业修改程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                OpenLib(sender, e);

            }
            else
            {
                return;
            }

            cannotSave = true;
            
            cbxHeroId.Value = 0;
            SetHeroInfo();
            SetHeroData();
            tbxHeroName.Text = heroname[cbxHeroPeriod.SelectedIndex, (int)cbxHeroId.Value];
            g_idHero = 0;

            cbxCityId.Value = 0;
            SetCityInfo();
            SetCityData();
            tbxCityName.Text = cityname[(int)cbxCityId.Value];
            g_idCity = 0;

            cbxGoodId.Value = 0;
            SetGoodInfo();
            SetGoodData();
            tbxGoodName.Text = goodname[(int)cbxGoodId.Value];
            g_idGood = 0;

            cannotSave = false;

            cbxHeadPeriod.SelectedIndex = 0;
            cbxHeadPeriod_SelectedIndexChanged(sender, e);

           

        }

        private void OpenSsv(int idx)
        {
            if (tbxSavPos.Text == "") return;
            isLib = false;
            isSsv = true;
            string filename = tbxSavPos.Text;
            string filename1 = tbxSavPos.Text;
            if (idx == 0)
            {
                filename += @"\sango0.sav";
                filename1 += @"\sango1.sav";
            }
            else if (idx == 1)
            {
                filename += @"\sango2.sav";
                filename1 += @"\sango3.sav";
            }
            else if (idx == 2)
            {
                filename += @"\sango4.sav";
                filename1 += @"\sango5.sav";
            }
            g_filename = filename;
            g_filename1 = filename1;
            MyOpenSav(filename, filename1);
            isRlb = false;
            noRlb = false;
            
        }

        private void MyOpenSav(string filename, string filename1)
        {
            FileStream fs;
            fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            BinaryReader br = new BinaryReader(fs);
            for (int i = 0; i < fs.Length; i++)
            {
                savByte[i] = br.ReadByte();
            }
            savByteLen = (int)fs.Length;

            br.Close();
            fs.Close();

            FileStream fs1;
            fs1 = new FileStream(filename1, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            BinaryReader br1 = new BinaryReader(fs1);
            for (int i = savByteLen; i < savByteLen + fs1.Length; i++)
            {
                savByte[i] = br1.ReadByte();
            }
            savByteLen += (int)fs1.Length;

            br1.Close();
            fs1.Close();

            savPeriod = savByte[0] - 1;

            int startPos = 12;

            for (int i = 0; i < 200; i++)
            {
                savhero[i] = new HERO();
                savhero[i].Id = savByte[startPos];
                savhero[i].Belong = savByte[startPos + 1];
                savhero[i].Level = savByte[startPos + 2];
                savhero[i].Force = savByte[startPos + 3];
                savhero[i].IQ = savByte[startPos + 4];
                savhero[i].Devotion = savByte[startPos + 5];
                savhero[i].Character = savByte[startPos + 6];
                savhero[i].Experience = savByte[startPos + 7];
                savhero[i].Thew = savByte[startPos + 8];
                savhero[i].ArmsType = savByte[startPos + 9];

                byte[] b1 = new byte[2];
                b1[0] = savByte[startPos + 10];
                b1[1] = savByte[startPos + 11];
                savhero[i].Arms = BitConverter.ToUInt16(b1, 0);

                savhero[i].Equip[0] = savByte[startPos + 12];
                savhero[i].Equip[1] = savByte[startPos + 13];
                savhero[i].Age = savByte[startPos + 14];

                startPos += 15;
            }

            startPos = 3245 + 1530;

            for (int i = 0; i < 38; i++)
            {
                savcity[i] = new CITY();
                savcity[i].Id = savByte[startPos];
                savcity[i].Belong = savByte[startPos + 1];
                savcity[i].SatrapId = savByte[startPos + 2];

                byte[] b1 = new byte[2];
                b1[0] = savByte[startPos + 3];
                b1[1] = savByte[startPos + 4];
                savcity[i].FarmingLimit = BitConverter.ToUInt16(b1, 0);

                byte[] b2 = new byte[2];
                b2[0] = savByte[startPos + 5];
                b2[1] = savByte[startPos + 6];
                savcity[i].Farming = BitConverter.ToUInt16(b2, 0);

                byte[] b3 = new byte[2];
                b3[0] = savByte[startPos + 7];
                b3[1] = savByte[startPos + 8];
                savcity[i].CommerceLimit = BitConverter.ToUInt16(b3, 0);

                byte[] b4 = new byte[2];
                b4[0] = savByte[startPos + 9];
                b4[1] = savByte[startPos + 10];
                savcity[i].Commerce = BitConverter.ToUInt16(b4, 0);

                savcity[i].PeopleDevotion = savByte[startPos + 11];
                savcity[i].AvoidCalamity = savByte[startPos + 12];

                byte[] b5 = new byte[4];
                b5[0] = savByte[startPos + 13];
                b5[1] = savByte[startPos + 14];
                b5[2] = savByte[startPos + 15];
                b5[3] = savByte[startPos + 16];
                savcity[i].PopulationLimit = BitConverter.ToUInt32(b5, 0);

                byte[] b6 = new byte[4];
                b6[0] = savByte[startPos + 17];
                b6[1] = savByte[startPos + 18];
                b6[2] = savByte[startPos + 19];
                b6[3] = savByte[startPos + 20];
                savcity[i].Population = BitConverter.ToUInt32(b6, 0);

                byte[] b7 = new byte[2];
                b7[0] = savByte[startPos + 21];
                b7[1] = savByte[startPos + 22];
                savcity[i].Money = BitConverter.ToUInt16(b7, 0);

                byte[] b8 = new byte[2];
                b8[0] = savByte[startPos + 23];
                b8[1] = savByte[startPos + 24];
                savcity[i].Food = BitConverter.ToUInt16(b8, 0);

                byte[] b9 = new byte[2];
                b9[0] = savByte[startPos + 25];
                b9[1] = savByte[startPos + 26];
                savcity[i].MothballArms = BitConverter.ToUInt16(b9, 0);

                savcity[i].PersonQueue = savByte[startPos + 27];
                savcity[i].Persons = savByte[startPos + 28];
                savcity[i].ToolQueue = savByte[startPos + 29];
                savcity[i].Tools = savByte[startPos + 30];


                startPos += 31;

            }

            /*gam_fwrite((U8 *)&g_PIdx,1,1,fp);
	gam_fwrite((U8 *)&g_PlayerKing,1,1,fp);
	gam_fwrite((U8 *)&g_YearDate,2,1,fp);
	gam_fwrite((U8 *)&g_LookEnemy,1,1,fp);
	gam_fwrite((U8 *)&g_LookMovie,1,1,fp);
	gam_fwrite((U8 *)&g_MoveSpeed,1,1,fp);
	gam_fwrite((U8 *)&g_MonthDate,1,1,fp);
	gam_fwrite((U8 *)&g_CityPos,sizeof(CitySetType),1,fp);//4
	gam_fwrite((U8 *)g_Persons,sizeof(PersonType),PERSON_MAX,fp);//15*200
	gam_fwrite((U8 *)g_PersonsQueue,1,PERSON_MAX,fp);//200
	gam_fwrite((U8 *)g_GoodsQueue,1,GOODS_MAX,fp);*///33

            /*gam_fwrite((U8 *)FIGHTERS_IDX,1,FIGHT_ORDER_MAX,fp);//30
	gam_fwrite((U8 *)FIGHTERS,10,FIGHT_ORDER_MAX,fp);//30*10
	gam_fwrite((U8 *)ORDERQUEUE,sizeof(OrderType),ORDER_MAX,fp);//12*100
	gam_fwrite((U8 *)g_Cities,sizeof(CityType),CITY_MAX,fp);*///31*38
            //InitRlb();

        }

        private void OpenLib(object sender, EventArgs e)
        {
            isLib = true;
            isSsv = false;
            string filename = openFileDialog2.FileName;
            //File fileOpen = new File(fName);
            MyOpen(filename);
            isRlb = false;
            noRlb = false;
            textBox1.Text = filename;
            tabControl1.Enabled = true;
            comboBox1_SelectedIndexChanged(sender, e);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确认已经保存了各时期的数据", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string FileName = saveFileDialog1.FileName;

                    MySave(FileName);
                    MessageBox.Show("保存成功");
                }
            }
            

            

            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先载入LIB文件用于读取姓名等信息", "提示");
                return;

            }
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                
                if (textBox1.Text!="" && File.Exists(folderBrowserDialog1.SelectedPath + @"\sango0.sav") || File.Exists(folderBrowserDialog1.SelectedPath + @"\sango2.sav") || File.Exists(folderBrowserDialog1.SelectedPath + @"\sango4.sav"))
                {
                    string FilePath = folderBrowserDialog1.SelectedPath;
                    tbxSavPos.Text = FilePath;
                    cbxSaveId.Enabled = true;
                    tbcSav.Enabled = true;
                    cbxSaveId.SelectedIndex = 0;
                    OpenSsv(0);

                    cbxSaveHeroId.Value = 0;
                    SetHeroInfo();
                    SetSavHeroData();
                    tbxSaveHeroName.Text = heroname[savPeriod, (int)cbxSaveHeroId.Value];
                    g_idSavHero = 0;

                    cbxSaveCityId.Value = 0;
                    SetCityInfo();
                    SetSavCityData();
                    tbxSaveCityName.Text = cityname[(int)cbxSaveCityId.Value];
                    g_idSavCity = 0;


                }
                else if (textBox1.Text =="" )
                {
                    MessageBox.Show("请先载入LIB库");
                }
                else
                {
                    MessageBox.Show("存档位置错误");
                }
            }  
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MySaveSav(g_filename, g_filename1);
            MessageBox.Show("保存成功");
        }

        private void MySaveSav(string FileName, string FileName1)
        {

            //int savByteLen = 3245 + 2708;
            int startPos = 0;

            FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            BinaryWriter bw = new BinaryWriter(fs);
            for (int i = 0; i < 3245; i++)
            {
                bw.Write(savByte[i]);
            }

            bw.Close();
            fs.Close();

            startPos = 3245;
            FileStream fs1 = new FileStream(FileName1, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            BinaryWriter bw1 = new BinaryWriter(fs1);
            for (int i = startPos; i < startPos + 2708 ; i++)
            {
                bw1.Write(savByte[i]);
            }

            bw1.Close();
            fs1.Close();



        }

        private void cbxSaveId_SelectedIndexChanged(object sender, EventArgs e)
        {
            OpenSsv(cbxSaveId.SelectedIndex);
            

            //cbxSaveHeroId.Items.Clear();
            cbxSaveHeroBelong.Items.Clear();
            cbxSaveCityBelong.Items.Clear();
            //cbxSaveHeroSearchId.Items.Clear();
            //cbxSaveHeroAppearCityId.Items.Clear();

            cbxSaveHeroBelong.Items.Add("无");
            cbxSaveCityBelong.Items.Add("无");

            for (int i = 0; i < 200; i++)
            {
                //cbxSaveHeroId.Items.Add(i + 1 + "");
                cbxSaveHeroBelong.Items.Add(heroname[savPeriod, i]);
                cbxSaveCityBelong.Items.Add(heroname[savPeriod, i]);
                //cbxSaveHeroSearchId.Items.Add(heroname[savPeriod, i]);

            }

            for (int i = 0; i < 38; i++)
            {
                //cbxSaveHeroAppearCityId.Items.Add(cityname[i]);
                //cbxSaveCityBelong.Items.Add(cityname[ i]);
            }

            cannotSave = true;

            cbxSaveHeroId.Value = 0;
            cbxSaveCityId.Value = 0;

            cbxSaveHeroId_ValueChanged(sender, e);
            cbxSaveCityId_ValueChanged(sender, e);

            cannotSave = false;
        }

        
        
        private void cbxHeroId_ValueChanged(object sender, EventArgs e)
        {
            if (tbxHeroName.Text != "" && cbxHeroBelong.SelectedIndex >= 0 && (!cannotSave)) SaveHeroData();

            tbxHeroName.Text = heroname[cbxHeroPeriod.SelectedIndex, (int)cbxHeroId.Value];

            SetHeroData();
            g_idHero = (int)cbxHeroId.Value;//.SelectedIndex;
        }

        private void cbxCityId_ValueChanged(object sender, EventArgs e)
        {
            if (tbxCityName.Text != "" && cbxCityBelong.SelectedIndex >= 0 && (!cannotSave)) SaveCityData();

            tbxCityName.Text = cityname[(int)cbxCityId.Value];

            SetCityData();
            g_idCity = (int)cbxCityId.Value;
        }

        private void cbxGoodId_ValueChanged(object sender, EventArgs e)
        {
            if (tbxGoodName.Text != "" && cbxGoodAppearCityId.SelectedIndex >= 0 && (!cannotSave)) SaveGoodData();
            tbxGoodName.Text = goodname[(int)cbxGoodId.Value];
            SetGoodData();
            g_idGood = (int)cbxGoodId.Value;// SelectedIndex;
        }

        private void cbxSaveHeroId_ValueChanged(object sender, EventArgs e)
        {
            if (tbxSaveHeroName.Text != "" && cbxSaveHeroBelong.SelectedIndex >= 0 && (!cannotSave)) SaveSavHeroData();

            tbxSaveHeroName.Text = heroname[savPeriod, (int)cbxSaveHeroId.Value];

            SetSavHeroData();
            g_idSavHero = (int)cbxSaveHeroId.Value;
        }

        private void cbxSaveCityId_ValueChanged(object sender, EventArgs e)
        {
            if (tbxSaveCityName.Text != "" && cbxSaveCityBelong.SelectedIndex >= 0 && (!cannotSave)) SaveSavCityData();

            tbxSaveCityName.Text = cityname[(int)cbxSaveCityId.Value];

            SetSavCityData();
            g_idSavCity = (int)cbxSaveCityId.Value;//20190302
        }

        private void lIB库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button5_Click_1(sender, e);
            tbcMain.SelectedIndex = 0;
        }

        private void 存档ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button7_Click_1(sender, e);
            tbcMain.SelectedIndex = 1;
        }

        private void lIB库ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            button6_Click(sender, e);
        }

        private void 存档ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            button2_Click(sender, e);
        }

        private void lIB库ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            button6_Click(sender, e);
        }

        private void 存档ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            button2_Click(sender, e);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (tbxSaveHeroName.Text != "" && cbxSaveHeroBelong.SelectedIndex >= 0 && (!cannotSave)) SaveSavHeroData();
            UpdateHeroSav();
            MessageBox.Show("保存成功");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (tbxSaveCityName.Text != "" && cbxSaveCityBelong.SelectedIndex >= 0 && (!cannotSave)) SaveSavCityData();
            UpdateCitySav();
            MessageBox.Show("保存成功");
        }

        private void 修改教程ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            System.Diagnostics.Process.Start("http://3gby.skysidestudio.com/newslist_5_1.html");
        }

        private void 修改限制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("本修改程序不支持新数据的增加，只允许数据修改，因此支持的数据范围为武将0-199 城池0-37 道具0-32，因此凡是超出这数组的修改均不会被程序支持");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (openFileDialog4.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog4.FileName;
                
                textBox2.Text = filename;
                pbxHead.Image = Image.FromFile(filename);

            }
            else
            {
                return;
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            //getHeadPic(cbxHeadPeriod.SelectedIndex,cbxHeadId.Value);

            int resid = cbxHeadPeriod.SelectedIndex + 47;//20190412 48-》47
            int picid = (int)cbxHeadId.Value;
            //ChangePicByte(resid, picid);
            InitResPic(resid, picid, 0);
            SaveHead(resid, picid);

            
        }

        private void button12_Click(object sender, EventArgs e)
        {
            int resid = cbxHeadPeriod.SelectedIndex + 48;
            int picid = (int)cbxHeadId.Value;
            ChangePicByte(resid, picid);
            //InitResPic(resid, picid, 0);
            MessageBox.Show("保存成功");
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            radioButton2.Checked = false;
            MessageBox.Show("暂不支持");
            radioButton1.Checked = true;
            return;
        }

        private void cbxHeadId_ValueChanged(object sender, EventArgs e)
        {
            int resid = cbxHeadPeriod.SelectedIndex +47;
            int picid = (int)cbxHeadId.Value;
            //ChangePicByte(resid, picid);
            
            ShowInBox(resid, picid);
        }

        private void cbxHeadPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbxHeadId.Value = 0;
            int resid = cbxHeadPeriod.SelectedIndex + 47;
            for (int ipicid = 0; ipicid < 200; ipicid++)
            {
                InitResPic(resid, ipicid, 0);
            }
            
        }

        private void button13_Click(object sender, EventArgs e)
        {
            int resid = cbxHeadPeriod.SelectedIndex + 47;//20190412 48-》47
            //int picid = (int)cbxHeadId.Value;
            //ChangePicByte(resid, picid);
            if (folderBrowserDialog2.ShowDialog() == DialogResult.OK)
            {

                string path = folderBrowserDialog2.SelectedPath;
                for (int i = 0; i < 200; i++)
                {
                    InitResPic(resid, i, 0);
                    SaveHeadAuto(resid, i, path);
                }
                MessageBox.Show("导出成功");
            }
        }

    }

    public class RCHEAD
    {
        public int ResLen;			/* 资源串总长度 */
        public int ResId;			/* 资源串id */
        public int ItmCnt;			/* 资源项个数 */
        public int ItmLen;			/* 资源项长度(0表示资源项不等长，后面会有资源项索引序列) */
        public int ResKey;			/* 资源串加密钥匙(加密算法统一使用+ResKey) */
        public int Reserved;		/* 保留 */
    };				/* 资源串头定义 */

    public class RIDX
    {

        public int offset;			/* 资源项相对资源起始地址的偏移地址 */
        public int rlen;			/* 资源项的数据长度 */
    }   

    public class PictureHead
    {
        public int wid;		/*图片宽*/
        public int hig;		/*图片高*/
        public int count;		/*图片数*/
        public int mask;		/*高4为bpp；(mask & 0x01)为1时为透明图；(mask & 0x02)为0时为横向图，为1时为纵向图*/
    };


    public class HERO
    {
        public byte Id;				/*编号*/
        public byte Belong;			/*归属*/
        public byte Level;			/*等级*/
        public byte Force;			/*武力*/
        public byte IQ;				/*智力*/
        public byte Devotion;			/*忠诚*/
        public byte Character;			/*性格*/
        public byte Experience;			/*经验*/
        public byte Thew;			/*体力*/
        public byte ArmsType;			/*兵种*/
        public ushort Arms;			/*兵力*/
        public byte[] Equip = new byte[2];			/*装备*/
        public byte Age;				/*年龄*/

        public string name;

        //63
        public byte BornYear;	 //出生年
        public byte SearchId;
        public byte AppearCityId;   //出生城池
    };

    public class CITY
    {
        public byte Id;
        public byte Belong;			/*归属*/
        public byte SatrapId;			/*太守编号*/
        public ushort FarmingLimit;		/*农业上限*/
        public ushort Farming;			/*农业开发度*/
        public ushort CommerceLimit;		/*商业上限*/
        public ushort Commerce;			/*商业开发度*/
        public byte PeopleDevotion;		/*民忠*/
        public byte AvoidCalamity;		/*防灾*/
        public uint PopulationLimit;		/*人口上限*/
        public uint Population;			/*人口*/
        public ushort Money;			/*金钱*/
        public ushort Food;			/*粮食*/
        public ushort MothballArms;		/*后备兵力*/
        public byte PersonQueue;			/*人才队列*/
        public byte Persons;			/*人才数*/
        public byte ToolQueue;			/*道具队列*/
        public byte Tools;			/*道具数*/

        public string name;
    };

    public class GOOD
    {
        public byte idx;			/* 道具序号 */
        public byte useflag;		/* 使用标志：是被使用还是被装备*/
        //public byte[] name = new byte[10];		/* 道具名字 */
        //public byte[] inf = new byte[50];		/* 道具说明 */
        public byte at;			/* 对武力的加层 */
        public byte iq;			/* 对智力的加层 */
        public byte move;			/* 对移动力的加层 */
        public byte arm;			/* 对兵种的改变 */

        public string name;

        //67
        public byte AppearYear;
        public byte SearchId;
        public byte AppearCityId;
    };



    public class MyBitMap//黑白：文件头(14字节)+信息头(40字节)+2个调色板(共8字节)+Height(图像高度)*(Width+8-Width%8)/8
    {

        //BMP文件头14
        public Byte[] bfType = { 0x42, 0x4d };
        public Byte[] bfSize = new Byte[4];
        public Byte[] bfReserved1 = { 0, 0 };
        public Byte[] bfReserved2 = { 0, 0 };
        public Byte[] bfOffBits = { 0x3e, 0, 0, 0 };

        //信息头40
        public Byte[] biSize = { 0x28, 0, 0, 0 }; // 本结构所占用字节数
        public Byte[] biWidth = new Byte[4]; // 位图的宽度，以像素为单位
        public Byte[] biHeight = new Byte[4]; // 位图的高度，以像素为单位
        public Byte[] biPlanes = { 1, 0 }; // 目标设备的级别，必须为1
        public Byte[] biBitCount = { 1, 0 };// 每个像素所需的位数，必须是1(双色),4(16色)，8(256色)或24(真彩色)之一
        public Byte[] biCompression = { 0, 0, 0, 0 }; // 位图压缩类型，必须是 0(不压缩)
        public Byte[] biSizeImage = new Byte[4]; // 位图的大小，以字节为单位.必须为4倍数
        public Byte[] biXPelsPerMeter = { 0xc4, 0x0e, 0, 0 }; // 位图水平分辨率，每米像素数
        public Byte[] biYPelsPerMeter = { 0xc4, 0x0e, 0, 0 }; // 位图垂直分辨率，每米像素数
        public Byte[] biClrUsed = { 0, 0, 0, 0 };// 位图实际使用的颜色表中的颜色数
        public Byte[] biClrImportant = { 0, 0, 0, 0 };// 位图显示过程中重要的颜色数

        //调色板8
        public Byte[] bmiColors = { 0, 0, 0, 0, 0xff, 0xff, 0xff, 0 };

        //位图数据 0-黑 1-白 Height(图像高度)*(Width+8-Width%8)/8
        public Byte[] biImage = new Byte[10485760];//19200];

        public Image BytesToImage()
        {
            return BytesToImage(0);
        }

        public Image BytesToImage(int no)
        {
            int count = BitConverter.ToInt32(bfSize, 0);
            int width = BitConverter.ToInt32(biWidth, 0);
            int height = BitConverter.ToInt32(biHeight, 0);
            byte[] buffer = new byte[16384];


            buffer[0] = 0x42;
            buffer[1] = 0x4d;
            buffer[2] = bfSize[0];
            buffer[3] = bfSize[1];
            buffer[4] = bfSize[2];
            buffer[5] = bfSize[3];
            buffer[6] = 0;
            buffer[7] = 0;
            buffer[8] = 0;
            buffer[9] = 0;
            buffer[10] = 0x3e;
            buffer[11] = 0;
            buffer[12] = 0;
            buffer[13] = 0;
            buffer[14] = 0x28;
            buffer[15] = 0;
            buffer[16] = 0;
            buffer[17] = 0;
            buffer[18] = biWidth[0];
            buffer[19] = biWidth[1];
            buffer[20] = biWidth[2];
            buffer[21] = biWidth[3];
            buffer[22] = biHeight[0];
            buffer[23] = biHeight[1];
            buffer[24] = biHeight[2];
            buffer[25] = biHeight[3];
            buffer[26] = 1;
            buffer[27] = 0;
            buffer[28] = 1;
            buffer[29] = 0;
            buffer[30] = 0;
            buffer[31] = 0;
            buffer[32] = 0;
            buffer[33] = 0;
            buffer[34] = biSizeImage[0];
            buffer[35] = biSizeImage[1];
            buffer[36] = biSizeImage[2];
            buffer[37] = biSizeImage[3];
            buffer[38] = 0xc4;
            buffer[39] = 0x0e;
            buffer[40] = 0;
            buffer[41] = 0;
            buffer[42] = 0xc4;
            buffer[43] = 0x0e;
            buffer[44] = 0;
            buffer[45] = 0;
            buffer[46] = 0;
            buffer[47] = 0;
            buffer[48] = 0;
            buffer[49] = 0;
            buffer[50] = 0;
            buffer[51] = 0;
            buffer[52] = 0;
            buffer[53] = 0;
            buffer[54] = 0;
            buffer[55] = 0;
            buffer[56] = 0;
            buffer[57] = 0;
            buffer[58] = 0xff;
            buffer[59] = 0xff;
            buffer[60] = 0xff;
            buffer[61] = 0;

            //
            byte[] buffer1 = new byte[16384];

            int width1 = width + 8 - width % 8;
            int tmp = 0;
            if ((width1 - width) % 8 == 0)
            {
                tmp = 1;
            }


            width = (width + 8 - width % 8) / 8;


            int width2 = 0;
            if ((width - tmp) % 4 != 0)
            {
                width2 = (width - tmp) + 4 - (width - tmp) % 4;
            }
            else
            {
                width2 = width - tmp;
            }

            //width = (width + 8 - width % 8) / 8;

            //if (no == 0)
            {
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        buffer1[(height - i - 1) * width2 + j] = biImage[i * (width - tmp) + j];
                    }
                }

            
                
            }
            /*else
            {
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        buffer1[(height - i - 1) * width2 + j] = biImage[i * (width - tmp) + j + no * ((width1 / 8 - tmp) * height)];
                    }
                }
            }*/

            for (int i = 62; i < (height - 1) * width2 + width2 + 62; i++)
            {
                //buffer[count - i + 61 ] = biImage[i - 62];
                buffer[i] = buffer1[i - 62];
            }


            MemoryStream ms = new MemoryStream(buffer);
            /*FileStream fs = File.Create("C:\\1.qq");
            fs.Write(buffer, 0, buffer.Length);*/


            Image image = System.Drawing.Image.FromStream(ms);

            //ms.Close();
            //fs.Close();
            return image;
        }

        public Image BytesToImageColor(int no)
        {
            int count = BitConverter.ToInt32(bfSize, 0);
            int width = BitConverter.ToInt32(biWidth, 0);
            int height = BitConverter.ToInt32(biHeight, 0);

            int width2 = (width * 24 + 31) / 32 * 4;//每行字节数
            int byteCount = height * width2;

            byte[] buffer = new byte[byteCount + 54];


            buffer[0] = 0x42;
            buffer[1] = 0x4d;
            buffer[2] = bfSize[0];
            buffer[3] = bfSize[1];
            buffer[4] = bfSize[2];
            buffer[5] = bfSize[3];
            buffer[6] = 0;
            buffer[7] = 0;
            buffer[8] = 0;
            buffer[9] = 0;
            buffer[10] = 0x36;
            buffer[11] = 0;
            buffer[12] = 0;
            buffer[13] = 0;
            buffer[14] = 0x28;
            buffer[15] = 0;
            buffer[16] = 0;
            buffer[17] = 0;
            buffer[18] = biWidth[0];
            buffer[19] = biWidth[1];
            buffer[20] = biWidth[2];
            buffer[21] = biWidth[3];
            buffer[22] = biHeight[0];
            buffer[23] = biHeight[1];
            buffer[24] = biHeight[2];
            buffer[25] = biHeight[3];
            buffer[26] = 1;
            buffer[27] = 0;
            buffer[28] = 24;
            buffer[29] = 0;
            buffer[30] = 0;
            buffer[31] = 0;
            buffer[32] = 0;
            buffer[33] = 0;
            buffer[34] = biSizeImage[0];
            buffer[35] = biSizeImage[1];
            buffer[36] = biSizeImage[2];
            buffer[37] = biSizeImage[3];
            buffer[38] = 0xc4;
            buffer[39] = 0x0e;
            buffer[40] = 0;
            buffer[41] = 0;
            buffer[42] = 0xc4;
            buffer[43] = 0x0e;
            buffer[44] = 0;
            buffer[45] = 0;
            buffer[46] = 0;
            buffer[47] = 0;
            buffer[48] = 0;
            buffer[49] = 0;
            buffer[50] = 0;
            buffer[51] = 0;
            buffer[52] = 0;
            buffer[53] = 0;
            

            //
            byte[] buffer1 = new byte[byteCount];

            int width1 = width ;
            


            width = width * 3;



            no = 0;
            if (no == 0)
            {
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        buffer1[i * width2 + j] = biImage[i * width + j];
                    }
                }



            }
            else
            {
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        buffer1[i * width2 + j] = biImage[i * width + j + no * width * height];
                    }
                }
            }

            for (int i = 54; i < byteCount+54; i++)
            {
                //buffer[count - i + 61 ] = biImage[i - 62];
                buffer[i] = buffer1[i - 54];
            }

            try
            {

                MemoryStream ms = new MemoryStream(buffer);
                //FileStream fs = File.Create("D:\\1.qq");
                //fs.Write(buffer, 0, buffer.Length);


                Image image = System.Drawing.Image.FromStream(ms);

                ms.Close();
                //fs.Close();
                return image;
            }
            catch { }
            return null;
        }


    }
}
