/// <summary>
 /// Constructor for the jtbvGameState class
 /// </summary>
 public jtbvGameState(jtbvTTYReader newTTY)
 {
     TTY = newTTY;
     player = new jtbvPlayerState();
     Mode = jtbvMode.UNKNOWN;
     FullRedrawRequested = true;
 }
        /// <summary>
        /// Process a list of incoming messages and try to make sense of them.
        /// </summary>
        /// <param name="incomingMessages">A list of messages (including map and list data) that were received during the last turn.</param>
        public void ProcessMessages(ArrayList incomingMessages)
        {
            Match m;

            bool statusUpdated = false;

            //			this.moreRequested = false;

            if (incomingMessages.Count > 0)
                jtbvLog.WriteFile("RecvdMsgs.log", "New batch of messages...");

            foreach (jtbvReceivedMessage msg in incomingMessages)
            {
                jtbvLog.WriteFile("RecvdMsgs.log", msg.ToString());

                switch (Mode)
                {
                    case jtbvMode.GAME:

                        if (msg.CursorLoc.Y >= 22) // If it's one of the status lines
                        {
                            statusUpdated = true;
                        }
                        else if (msg.CursorLoc.Y >= 2) // If it's in the map
                        {
                            this.CurrDlvl.UseMsgData(msg, this);
                        }
                        else // If it's in the first two notification lines
                        {
                            this.LastNotice = msg.Text;

                            if (Regex.Match(msg.Text, @"--More--$").Success)
                            {
                                this.moreRequested = true;
                            }

                            if (msg.Text == @"Pick an object.")
                            {

                            }
                            else if ((m = Regex.Match(msg.Text, @"(.+)\? \[(ynq?)\] \(n\) $")).Success)
                            {
                                this.promptVerb = m.Groups[1].Value;
                                this.promptOptions = m.Groups[2].Value;

                                this.Mode = jtbvMode.YN_PROMPT;
                            }
                            else if ((m = Regex.Match(msg.Text, @"^What do you want to (.+)\? \[(.+) or \?\*\] $")).Success)
                            {
                                this.promptVerb = m.Groups[1].Value;
                                this.promptOptions = m.Groups[2].Value;

                                this.Mode = jtbvMode.PROMPT;
                            }
                            else if ((Regex.Match(msg.Text, @"^That door is closed.").Success) ||
                                     (Regex.Match(msg.Text, @"^Ouch!  You bump into a door.").Success))
                            {
                                if (this.CurrDlvl[this.LastCmdTarget].Tile != jtbvTile.LOCKED_DOOR)
                                    this.CurrDlvl[this.LastCmdTarget].Tile = jtbvTile.CLOSED_DOOR;
            //								this.CurrDlvl[target].Tile = jtbvTile.CLOSED_DOOR;
                            }
                            else if(Regex.Match(msg.Text, @"^The door opens.").Success)
                            {
                                this.CurrDlvl[this.LastCmdTarget].Tile = jtbvTile.OPEN_DOOR;
                            }
                            else if(Regex.Match(msg.Text, @"^This door is locked.").Success)
                            {
                                Console.WriteLine(this.LastCmdTarget.ToString() + " is of type " + this.CurrDlvl[this.LastCmdTarget].Tile.ToString() + ", setting to type locked door.");
                                this.CurrDlvl[this.LastCmdTarget].Tile = jtbvTile.LOCKED_DOOR;
                                Console.WriteLine("Type is now " + this.CurrDlvl[this.LastCmdTarget].Tile.ToString());
                                //CommandStack.Push("o" + TargetDirection);
                            }
                            else if ((Regex.Match(msg.Text, @"^As you kick the door, it crashes open!").Success) || (Regex.Match(msg.Text, @"^As you kick the door, it shatters to pieces!").Success))
                            {
                                this.CurrDlvl[this.LastCmdTarget].Tile = jtbvTile.BROKEN_DOOR;
                                //CommandStack.Push(((char) 0x04).ToString() + TastDirection);
                            }
                            else if ((Regex.Match(msg.Text, @"^There's not enough room to kick down here").Success) ||
                                     (Regex.Match(msg.Text, @"^You fall into a pit!").Success) &&
                                     (Regex.Match(msg.Text, @"^You are still in a pit.").Success))
                            {
                                this.Player.InPit = true;
                            }
                            else if (Regex.Match(msg.Text, @"^You crawl to the edge of the pit\.").Success)
                            {
                                this.Player.InPit = false;
                            }
                            else if (Regex.Match(msg.Text, @"^Your right leg is in no shape for kicking\.").Success)
                            {
                                this.Player.LegInjured = true;
                            }
                            else if (Regex.Match(msg.Text, @"^Your leg feels somewhat better").Success)
                            {
                                this.Player.LegInjured = false;
                            }
                            else if (Regex.Match(msg.Text, @"^You are too small to do any kicking\.").Success)
                            {
                                this.Player.TooSmallToKick = true;
                            }
                            else if (Regex.Match(msg.Text, @"^You return to .+ form\.").Success)
                            {
                                this.Player.TooSmallToKick = false;
                            }
                            else if (Regex.Match(msg.Text, @"^You have no free hand to write with!").Success)
                            {
                                this.Player.NoFreeHand = true;
                            }
                            else if (msg.Text == @"You read:")
                            {
                                Console.WriteLine(@"Reading a long message...");
                                Mode = jtbvMode.WINDOW_LIST;
                            }
                            else if (msg.Text == @" Things that are here:")
                            {
                                Console.WriteLine(@"Found a list of things...");
                                this.CurrDlvl[this.Player.Loc].Items.Clear();
                                Mode = jtbvMode.WINDOW_LIST;
                            }
                            else if ((m = Regex.Match(msg.Text, @"^Call a .+: $")).Success)
                            {
                                this.promptText = msg.Text;
                                Mode = jtbvMode.TEXT_PROMPT;
                            }
                            else if (msg.Text == "\"Hello stranger, who are you?\" - ")
                            {
                                this.promptText = msg.Text;
                                Mode = jtbvMode.TEXT_PROMPT;
                            }
                            else if ((msg.Text == @"--More--") ||
                                     (msg.Text == @"Restoring save file...--More--"))
                            {
                                this.moreRequested = true;
                            }
                            else if (msg.Text == @"Do you want your possessions identified? [ynq] (n) ")
                            {
                                this.player.Dead = true;
                            }
                            else if ((msg.Text.StartsWith(@"^You begin praying to ")))
                            {
                                this.player.LastPrayerTurn = this.CurrentTurn;
                            }
                            else if (Regex.Match(msg.Text, @"^You are beginning to feel hungry.").Success)
                            {
                                this.player.Hunger = jtbvHungerStatus.HUNGRY;
                            }
                            else if ((Regex.Match(msg.Text, @"^You are beginning to feel weak.").Success) ||
                                     (Regex.Match(msg.Text, @"needs food, badly!").Success))
                            {
                                this.player.Hunger = jtbvHungerStatus.WEAK;
                            }
                            else if (Regex.Match(msg.Text, @"^You faint from lack of food.").Success)
                            {
                                this.player.Hunger = jtbvHungerStatus.FAINTING;
                            }
                            else if (Regex.Match(msg.Text, @"^Your stomach feels content.").Success)
                            {
                                this.player.Hunger = jtbvHungerStatus.NEUTRAL;
                            }
                            else if ((m = Regex.Match(msg.Text, @"^Really attack .*\? \[yn\] \(n\) $")).Success)
                            {
                                this.moreRequested = true;
                            }
                            else if (msg.Text == @"There is a staircase down here.")
                            {
                                this.CurrDlvl[player.Loc].Tile = jtbvTile.STAIRCASE_DOWN;
                            }
                            else if ((msg.Text == @"You try to move the boulder, but in vain.")
                                     || (msg.Text == @"Perhaps that's why you cannot move it."))
                            {
                                this.CurrDlvl[this.LastCmdTarget].Tile = jtbvTile.WALL;
                            }
                            else if (msg.Text == @"From the murky depths, a hand reaches up to bless the sword.")
                            {
                                this.ExcalCreated = true;
                            }
                            else if (msg.Text.EndsWith(@"fountain dries up!"))
                            {
                                jtbvLog.WriteFile("MovementErrs.log", "Location3");

                                this.CurrDlvl[this.Player.Loc].Tile = jtbvTile.FLOOR;
                            }
                            else if (msg.Text == @"However, you can squeeze yourself into a small opening.")
                            {
                                this.CurrDlvl[this.LastCmdTarget].Tile = jtbvTile.UNKNOWN_PASSABLE;
                            }
                            else if ((m = Regex.Match(msg.Text, @"^(.)       .+ \((.+)\)")).Success)
                            {
                                char sym = m.Groups[1].Value[0];
                                string desc = m.Groups[2].Value;

                                this.CurrDlvl.UseQueryResult(sym, desc);
                            }
                            else if ((m = Regex.Match(msg.Text, @"^(I)       a remembered, unseen, creature")).Success)
                            {
                                char sym = m.Groups[1].Value[0];
                                string desc = "";

                                this.CurrDlvl.UseQueryResult(sym, desc);
                            }
                            else if (msg.Text == "Unknown command ' '.")
                            {
                                this.moreRequested = false;
                            }
            //							else if ((m = Regex.Match(msg.Text, "^Really attack .*\\? \\[yn\\] \\(n\\) $")).Success)
                            else
                            {
                                //Don't understand this message
                                jtbvLog.WriteFile("ConfusingMessages.log", "Didn't understand message '" + msg.Text + "'");
                                this.moreRequested = true;
                            }
                        }
                        break;
                    case jtbvMode.WINDOW_LIST:
                        Console.WriteLine("Window list mode");
                        Console.WriteLine("'" + msg.Text + "'");
                        if (msg.Text.EndsWith("--More--"))
                            Mode = jtbvMode.GAME;

                        this.moreRequested = true;

                        break;
                    case jtbvMode.TEXT_PROMPT:
                        this.Mode = jtbvMode.GAME;
                        break;
                    case jtbvMode.PROMPT:
                        this.Mode = jtbvMode.GAME;
                        break;
                    case jtbvMode.YN_PROMPT:
                        this.Mode = jtbvMode.GAME;
                        break;
                    case jtbvMode.DGAMELAUNCH:
                        if (msg.Text == " Not logged in.")
                            this.LoggedIn = false;
                        else if (msg.Text.StartsWith(" Logged in as: "))
                            this.LoggedIn = true;
                        else if (msg.Text == " There are some stale NetHack processes, will recover in 10  seconds.")
                        {
                            Console.WriteLine("Already logged in -- waiting 10 seconds");
                            System.Threading.Thread.Sleep(10000);
                            this.moreRequested = true;
                            Mode = jtbvMode.GAME;
                        }
                        else if (msg.Text == "         See license for details.")
                        {
                            this.moreRequested = true;
                            Mode = jtbvMode.GAME;
                        }
                        break;
                    case jtbvMode.UNKNOWN:
                    default:
                        if 		(msg.Text == " ## dgamelaunch - network console game launcher")
                        {
                            Mode = jtbvMode.DGAMELAUNCH;
                        }
                        break;
                }
            }
            if (statusUpdated)
            {
                ProcessStatusLines();
            }
        }