// Update is called once per frame
    void Update()
    {
        if (istriggered && (Input.GetKeyDown(KeyCode.Tab) || Input.GetKeyDown(KeyCode.F)) && !isKeyDown && !gm.isEncounter)
        {
            isKeyDown = true;
            if (type == houseType.cathouse)
            {
                options.Clear();
                gm.isEncounter  = true;
                gm.eventContent = "Do you want to sleep now?";

                gm.options[0] = "yes";
                options.Add(1);
                gm.options[1] = "No";
                options.Add(0);
                gm.options[2] = "";
                options.Add(0);
                gm.options[3] = "";
                options.Add(0);
            }
            //else if (_event_num <= _event_num_max || (_event_num > _event_num_max && type == houseType.garbage_dump))
            else
            {
                try
                {
                    XmlNodeList houseEvents = houseEvent.SelectNodes("descendant::row[houseID=" + type.ToString("d") + "]");
                    foreach (XmlNode he in houseEvents)
                    {
                        bool        qualified = false;
                        XmlNodeList triggers  = eventTrigger.SelectNodes("descendant::row[eventID=" + he.SelectSingleNode("ID").InnerText + "]");
                        if (triggers.Count != 0)
                        {
                            foreach (XmlNode condition in triggers)
                            {
                                string name = condition.SelectSingleNode("triggername").InnerText;
                                int    min  = Int32.Parse(condition.SelectSingleNode("valuemin").InnerText);
                                int    max  = Int32.Parse(condition.SelectSingleNode("valuemax").InnerText);
                                qualified = isIn(name, min, max);
                                if (!qualified)
                                {
                                    break;
                                }
                            }
                        }
                        if (qualified)
                        {
                            showEvent(he);
                            break;
                        }
                        else
                        {
                            isKeyDown = false;
                        }
                    }
                }
                catch (Exception e)
                {
                    isKeyDown = false;
                }
            }
        }

        if (gm.optionRank != -1 && options.Count > 0)
        {
            int option_index = options[gm.optionRank];
            if (option_index == 0)
            {
                gm.isSleeped   = false;
                gm.isEncounter = false;
                isKeyDown      = false;
                options.Clear();
            }
            else
            {
                if (type == houseType.cathouse)
                {
                    string content    = "";
                    float  wakeupTime = (gm._staminaPoint < 50) ? 1080 : 480;
                    float  period     = wakeupTime - gm._time;
                    if (period <= 0)
                    {
                        gm.addDay();
                        period += 1440;
                    }
                    int stamina_subtract = (gm._staminaPoint < 50) ? 29 : 9;
                    gm._staminaPoint += (int)(period / 60) - stamina_subtract;
                    if (gm._satietyPoint >= 90 && gm._drinkPoint >= 90)
                    {
                        content          = "You've had a fulfilling day and you're full before you go to bed, so your body has recovered health.\nhealth: +10 ";
                        gm._healthPoint += 10;
                    }
                    else if (gm._satietyPoint >= 50 && gm._drinkPoint >= 50)
                    {
                        content          = "You have had a nice day, and you are not hungry when you fall asleep. You have recovered some health.\nhealth: +5 ";
                        gm._healthPoint += 5;
                    }
                    else if (gm._satietyPoint <= 0 || gm._drinkPoint <= 0)
                    {
                        content          = "YOU KNOW YOU WILL DIE IF YOU DON’T EAT ANYTHING. RIGHT?\nhealth:-10 ";
                        gm._healthPoint += -10;
                    }
                    else
                    {
                        content = "You have had an ok day, and you are a little hungry or thirsty before you fall asleep. ";
                    }
                    gm._satietyPoint -= 30;
                    gm._drinkPoint   -= 30;
                    gm._time          = wakeupTime;
                    gm._lasthour      = gm._punishhour;
                    gm.isEncounter    = true;
                    gm.eventContent   = content + "\nFacing the unknown next day\nsatiety - 30 drinkPoint - 30";
                    gm.isSleeped      = true;
                    options.Clear();
                    for (int i = 0; i < 4; i++)
                    {
                        gm.options[i] = (i == 0) ? "OK" : "";
                        options.Add(0);
                    }
                }
                else
                {
                    XmlNode     theOption = option.SelectSingleNode("descendant::row[ID=" + option_index + "]"), node;
                    XmlNodeList triggers = resultTrigger.SelectNodes("descendant::row[optionID=" + theOption.SelectSingleNode("ID").InnerText + "]");
                    bool        qualified = false;
                    int         min, max;

                    if (triggers.Count != 0)
                    {
                        foreach (XmlNode tri in triggers)
                        {
                            if (tri.SelectSingleNode("triggername").InnerText == "skill")
                            {
                                qualified = isSkillActive(tri.SelectSingleNode("valuemin").InnerText);
                            }
                            else
                            {
                                min       = Int32.Parse(tri.SelectSingleNode("valuemin").InnerText);
                                max       = Int32.Parse(tri.SelectSingleNode("valuemax").InnerText);
                                qualified = isIn(tri.SelectSingleNode("triggername").InnerText, min, max);
                            }
                            if (qualified)
                            {
                                string resultID = tri.SelectSingleNode("resultID").InnerText;
                                node = result.SelectSingleNode("descendant::row[ID=" + resultID + "]");
                                showResult(node);
                                break;
                            }
                        }
                    }

                    if (!qualified)
                    {
                        string[] results = theOption.SelectSingleNode("resultID").InnerText.Split(',');

                        int random = (int)(UnityEngine.Random.value * 100);
                        min = 0;
                        max = 0;
                        foreach (string index in results)
                        {
                            node = result.SelectSingleNode("descendant::row[ID=" + index + "]");
                            min  = max;
                            max += Int32.Parse(node.SelectSingleNode("weight").InnerText);

                            if (random >= min && random <= max)
                            {
                                showResult(node);
                                break;
                            }
                        }
                    }
                }
            }
            gm.optionRank = -1;
        }

        //if (_event_num > _event_num_max && type != houseType.garbage_dump)
        ////if (_event_num > _event_num_max)
        //{
        //    GetComponent<BoxCollider>().enabled = false;
        //}
        //else
        //{
        //    GetComponent<BoxCollider>().enabled = true;
        //}

        //if (gm.isDayadd) {
        //    Debug.Log("testttttttttttt");
        //    _event_num = 1;
        //    gm.isDayadd = false;
        //}
    }