Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of a virtual room. The room is prepared for usage.
        /// </summary>
        /// <param name="roomID"></param>
        /// <param name="isPublicroom"></param>
        public virtualRoom(int roomID, bool isPublicroom)
        {
            this.roomID = roomID;
            this.isPublicroom = isPublicroom;

            string roomModel;
            DataRow dRow;
            using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
            {
                roomModel = dbClient.getString("SELECT model FROM rooms WHERE id = '" + roomID + "'");
                dRow = dbClient.getRow("SELECT door_x,door_y,door_h,door_z,heightmap,publicroom_items, swimmingpool FROM room_modeldata WHERE model = '" + roomModel + "'"); //needs caching
            }
            doorX = Convert.ToInt32(dRow["door_x"]);
            doorY = Convert.ToInt32(dRow["door_y"]);
            doorH = Convert.ToInt32(dRow["door_h"]);
            doorZ = Convert.ToByte(dRow["door_z"]);
            _Heightmap = Convert.ToString(dRow["heightmap"]);

            string[] tmpHeightmap = _Heightmap.Split(Convert.ToChar(13));
            int colX = tmpHeightmap[0].Length;
            int colY = tmpHeightmap.Length - 1;

            sqSTATE = new squareState[colX, colY];
            sqFLOORHEIGHT = new byte[colX, colY];
            sqITEMROT = new byte[colX, colY];
            sqITEMHEIGHT = new double[colX, colY];
            sqUNIT = new bool[colX, colY];
            sqSTACK = new furnitureStack[colX, colY];
            sqTRIGGER = new squareTrigger[colX, colY];

            for (int y = 0; y < colY; y++)
            {
                for (int x = 0; x < colX; x++)
                {
                    string _SQ = tmpHeightmap[y].Substring(x, 1).Trim().ToLower();
                    if (_SQ == "x")
                        sqSTATE[x, y] = squareState.Blocked;
                    else
                    {
                        sqSTATE[x, y] = squareState.Open;
                        try { sqFLOORHEIGHT[x, y] = byte.Parse(_SQ); }
                        catch { }
                    }
                }
            }

            if (isPublicroom)
            {
                string[] Items = Convert.ToString(dRow["publicroom_items"]).Split("\n".ToCharArray());
                for (int i = 0; i < Items.Length; i++)
                {
                    string[] itemData = Items[i].Split(' ');
                    int X = int.Parse(itemData[2]);
                    int Y = int.Parse(itemData[3]);
                    squareState sType = (squareState)int.Parse(itemData[6]);
                    sqSTATE[X, Y] = sType;
                    if (sType == squareState.Seat)
                    {
                        sqITEMROT[X, Y] = byte.Parse(itemData[5]);
                        sqITEMHEIGHT[X, Y] = 1.0;
                    }

                    _publicroomItems += itemData[0] + " " + itemData[1] + " " + itemData[2] + " " + itemData[3] + " " + itemData[4] + " " + itemData[5] + Convert.ToChar(13);
                }

                DataTable dTable;
                using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
                {
                    dTable = dbClient.getTable("SELECT object,x,y,goalx,goaly,stepx,stepy,roomid,state FROM room_modeldata_triggers WHERE model = '" + roomModel + "'");
                }
                foreach (DataRow dbRow in dTable.Rows)
                {
                    sqTRIGGER[Convert.ToInt32(dbRow["x"]), Convert.ToInt32(dbRow["y"])] = new squareTrigger(Convert.ToString(dbRow["object"]), Convert.ToInt32(dbRow["goalx"]), Convert.ToInt32(dbRow["goaly"]), Convert.ToInt32(dbRow["stepx"]), Convert.ToInt32(dbRow["stepy"]), (Convert.ToInt32(dbRow["state"]) == 1), Convert.ToInt32(dbRow["roomid"]));
                }
                bool result = false;
                using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
                {
                    result = dbClient.findsResult("SELECT specialcast_interval FROM room_modeldata WHERE model = '" + roomModel + "' AND specialcast_interval > 0");
                }
                if (result)
                {
                    specialCastHandler = new Thread(new ParameterizedThreadStart(handleSpecialCasts));
                    specialCastHandler.Priority = ThreadPriority.Lowest;
                    specialCastHandler.Start(roomModel);
                }
                using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
                {
                    int swimmingPool;
                    hasSwimmingPool = (int.TryParse((Convert.ToString(dRow["swimmingpool"])), out swimmingPool));
                    result = dbClient.findsResult("SELECT id FROM games_lobbies WHERE id = '" + roomID + "'");
                }

                if (result)
                {
                    using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
                    {
                        dRow = dbClient.getRow("SELECT type,rank FROM games_lobbies WHERE id = '" + roomID + "'");
                    }
                    this.Lobby = new gameLobby(this, (Convert.ToString(dRow["type"]) == "bb"), Convert.ToString(dRow["rank"]));
                }
            }
            else
            {
                floorItemManager = new FloorItemManager(this);
                wallItemManager = new WallItemManager(this);

                DataTable dTable;
                using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
                {
                    dTable = dbClient.getTable("SELECT furniture.id, furniture.tid, furniture.x, furniture.y, furniture.z, furniture.h, furniture.var, furniture.wallpos FROM rooms, furniture WHERE furniture.roomid = rooms.id AND rooms.id = '" + roomID + "' ORDER BY rooms.id ASC");
                }
                foreach (DataRow dbRow in dTable.Rows)
                    if (Convert.ToString(dbRow["wallpos"]) == "") // Flooritem
                        floorItemManager.addItem(Convert.ToInt32(dbRow["id"]), Convert.ToInt32(dbRow["tid"]), Convert.ToInt32(dbRow["x"]), Convert.ToInt32(dbRow["y"]), Convert.ToInt32(dbRow["z"]), Convert.ToDouble(dbRow["h"]), Convert.ToString(dbRow["var"]));
                    else
                        wallItemManager.addItem(Convert.ToInt32(dbRow["id"]), Convert.ToInt32(dbRow["tid"]), Convert.ToString(dbRow["wallpos"]), Convert.ToString(dbRow["var"]), false);
            }
            _Users = new Hashtable();
            _Bots = new Hashtable();
            loadBots();

            _activeGroups = new HashSet<int>();
            _statusUpdates = new StringBuilder();
            _statusHandler = new Thread(new ThreadStart(cycleStatuses));
            _statusHandler.Start();

            sqSTATE[doorX, doorY] = 0; // Door always walkable
        }
        /// <summary>
        /// Initializes a new instance of a virtual room. The room is prepared for usage.
        /// </summary>
        /// <param name="roomID"></param>
        /// <param name="isPublicroom"></param>
        public virtualRoom(int roomID, bool isPublicroom)
        {
            this.roomID = roomID;
            this.isPublicroom = isPublicroom;

            string roomModel = DB.runRead("SELECT model FROM rooms WHERE id = '" + roomID + "'");
            doorX = DB.runRead("SELECT door_x FROM room_modeldata WHERE model = '" + roomModel + "'", null);
            doorY = DB.runRead("SELECT door_y FROM room_modeldata WHERE model = '" + roomModel + "'", null);
            doorH = DB.runRead("SELECT door_h FROM room_modeldata WHERE model = '" + roomModel + "'", null);
            doorZ = byte.Parse(DB.runRead("SELECT door_z FROM room_modeldata WHERE model = '" + roomModel + "'"));
            _Heightmap = DB.runRead("SELECT heightmap FROM room_modeldata WHERE model = '" + roomModel + "'");

            string[] tmpHeightmap = _Heightmap.Split(Convert.ToChar(13));
            int colX = tmpHeightmap[0].Length;
            int colY = tmpHeightmap.Length - 1;

            sqSTATE = new squareState[colX, colY];
            sqFLOORHEIGHT = new byte[colX, colY];
            sqITEMROT = new byte[colX, colY];
            sqITEMHEIGHT = new double[colX, colY];
            sqUNIT = new bool[colX, colY];
            sqSTACK = new furnitureStack[colX, colY];
            sqTRIGGER = new squareTrigger[colX,colY];

            for (int y = 0; y < colY; y++)
            {
                for (int x = 0; x < colX; x++)
                {
                    string _SQ = tmpHeightmap[y].Substring(x, 1).Trim().ToLower();
                    if (_SQ == "x")
                        sqSTATE[x, y] = squareState.Blocked;
                    else
                    {
                        sqSTATE[x, y] = squareState.Open;
                        try { sqFLOORHEIGHT[x, y] = byte.Parse(_SQ); }
                        catch { }
                    }
                }
            }

            if (isPublicroom)
            {
                string[] Items = DB.runRead("SELECT publicroom_items FROM room_modeldata WHERE model = '" + roomModel + "'").Split("\n".ToCharArray());
                for (int i = 0; i < Items.Length; i++)
                {
                    string[] itemData = Items[i].Split(' ');
                    int X = int.Parse(itemData[2]);
                    int Y = int.Parse(itemData[3]);
                    squareState sType = (squareState)int.Parse(itemData[6]);
                    sqSTATE[X, Y] = sType;
                    if (sType == squareState.Seat)
                    {
                        sqITEMROT[X, Y] = byte.Parse(itemData[5]);
                        sqITEMHEIGHT[X, Y] = 1.0;
                    }

                    _publicroomItems += itemData[0] + " " + itemData[1] + " " + itemData[2] + " " + itemData[3] + " " + itemData[4] + " " + itemData[5] + Convert.ToChar(13);
                }

                int[] triggerIDs = DB.runReadColumn("SELECT id FROM room_modeldata_triggers WHERE model = '" + roomModel + "'",0,null);
                if (triggerIDs.Length > 0)
                {
                    for (int i = 0; i < triggerIDs.Length; i++)
                    {
                        string Object = DB.runRead("SELECT object FROM room_modeldata_triggers WHERE id = '" + triggerIDs[i] + "'");
                        int[] Nums = DB.runReadRow("SELECT x,y,goalx,goaly,stepx,stepy,roomid,state FROM room_modeldata_triggers WHERE id = '" + triggerIDs[i] + "'",null);
                        sqTRIGGER[Nums[0], Nums[1]] = new squareTrigger(Object, Nums[2], Nums[3],Nums[4],Nums[5],(Nums[7] == 1),Nums[6]);
                    }
                }

                if(DB.checkExists("SELECT specialcast_interval FROM room_modeldata WHERE model = '" + roomModel + "' AND specialcast_interval > 0"))
                {
                    specialCastHandler = new Thread(new ParameterizedThreadStart(handleSpecialCasts));
                    specialCastHandler.Priority = ThreadPriority.Lowest;
                    specialCastHandler.Start(roomModel);
                }

                hasSwimmingPool = DB.checkExists("SELECT swimmingpool FROM room_modeldata WHERE model = '" + roomModel + "' AND swimmingpool = '1'");
            }
            else
            {
                int[] itemIDs = DB.runReadColumn("SELECT id FROM furniture WHERE roomid = '" + roomID + "' ORDER BY h ASC",0,null);
                floorItemManager = new FloorItemManager(this);
                wallItemManager = new WallItemManager(this);

                if(itemIDs.Length > 0)
                {
                    int[] itemTIDs = DB.runReadColumn("SELECT tid FROM furniture WHERE roomid = '" + roomID + "' ORDER BY h ASC",0,null);
                    int[] itemXs = DB.runReadColumn("SELECT x FROM furniture WHERE roomid = '" + roomID + "' ORDER BY h ASC",0,null);
                    int[] itemYs = DB.runReadColumn("SELECT y FROM furniture WHERE roomid = '" + roomID + "' ORDER BY h ASC",0,null);
                    int[] itemZs = DB.runReadColumn("SELECT z FROM furniture WHERE roomid = '" + roomID + "' ORDER BY h ASC",0,null);
                    string[] itemHs = DB.runReadColumn("SELECT h FROM furniture WHERE roomid = '" + roomID + "' ORDER BY h ASC",0);
                    string[] itemVars = DB.runReadColumn("SELECT var FROM furniture WHERE roomid = '" + roomID + "' ORDER BY h ASC",0);
                    string[] itemWallPos = DB.runReadColumn("SELECT wallpos FROM furniture WHERE roomid = '" + roomID + "' ORDER BY h ASC",0);

                    for(int i = 0; i < itemIDs.Length; i++)
                    {
                        if(itemWallPos[i] == "") // Flooritem
                            floorItemManager.addItem(itemIDs[i], itemTIDs[i], itemXs[i], itemYs[i], itemZs[i], double.Parse(itemHs[i]), itemVars[i]);
                        else // Wallitem
                            wallItemManager.addItem(itemIDs[i], itemTIDs[i], itemWallPos[i], itemVars[i], false);
                    }
                }
            }
            _Users = new Hashtable();
            _Bots = new Hashtable();
            loadBots();

            _activeGroups = new HashSet<int>();
            _statusUpdates = new StringBuilder();
            _statusHandler = new Thread(new ThreadStart(cycleStatuses));
            _statusHandler.Start();

            sqSTATE[doorX, doorY] = 0; // Door always walkable
        }