begintownscript;

variables;

int i,j,k,r1,choice;
string light_color;

body;

beginstate INIT_STATE;
        //code called when entering
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
        //code called every turn
break;

beginstate 10;
        reset_dialog();
        add_dialog_str(0,"This pedestal is empty except for a red button. There are no markings or obvious ways of telling what the button will do if pushed.",0);
        add_dialog_choice(0,"Push the button.");
        add_dialog_choice(1,"Leave.");
        choice = run_dialog(1);
        if (choice == 1) {
                reset_dialog();
                add_dialog_str(0,"You hear a soft click from the button. The light on one of the other pedestals changes color.",0);
                run_dialog(1);
                set_flag(0,20,1 - get_flag(0,20));
        }
        if (choice == 2) {
                end();
        }
break;

beginstate 11;
        reset_dialog();
        add_dialog_str(0,"This pedestal is empty except for a blue button. There are no markings or other ways of telling what the button will do if pushed.",0);
        add_dialog_choice(0,"Push the button.");
        add_dialog_choice(1,"Leave.");
        choice = run_dialog(1);
        if (choice == 1) {
                reset_dialog();
                add_dialog_str(0,"You hear a soft click from the button. The light on one of the other pedestals changes color.",0);
                run_dialog(1);
                set_flag(0,21,1 - get_flag(0,21));
        }
        if (choice == 2) {
                end();
        }
break;

beginstate 12;
        reset_dialog();
        add_dialog_str(0,"This pedestal is empty except for a green button. There are no markings or other ways of telling what the button will do if pushed.",0);
        add_dialog_choice(0,"Push the button.");
        add_dialog_choice(1,"Leave.");
        choice = run_dialog(1);
        if (choice == 1) {
                reset_dialog();
                add_dialog_str(0,"You hear a soft click from the button. The light on one of the other pedestals changes color.",0);
                run_dialog(1);
                set_flag(0,22,1 - get_flag(0,22));
        }
        if (choice == 2) {
                end();
        }
break;

beginstate 13;
        clear_buffer();
        append_string("This pedestal contains a single light. The light is currently ");
        if ((get_flag(0,20) == 0) && (get_flag(0,21) == 0) && (get_flag(0,22) == 0)) {
                append_string("off.");
        }
        if ((get_flag(0,20) == 1) && (get_flag(0,21) == 0) && (get_flag(0,22) == 0)) {
                append_string("red.");
        }
        if ((get_flag(0,20) == 0) && (get_flag(0,21) == 1) && (get_flag(0,22) == 0)) {
                append_string("blue.");
        }
        if ((get_flag(0,20) == 0) && (get_flag(0,21) == 0) && (get_flag(0,22) == 1)) {
                append_string("green.");
        }
        if ((get_flag(0,20) == 1) && (get_flag(0,21) == 1) && (get_flag(0,22) == 0)) {
                append_string("magenta.");
        }
        if ((get_flag(0,20) == 1) && (get_flag(0,21) == 0) && (get_flag(0,22) == 1)) {
                append_string("yellow.");
        }
        if ((get_flag(0,20) == 0) && (get_flag(0,21) == 1) && (get_flag(0,22) == 1)) {
                append_string("cyan.");
        }
        if ((get_flag(0,20) == 1) && (get_flag(0,21) == 1) && (get_flag(0,22) == 1)) {
                append_string("white.");
        }
        get_buffer_text(light_color);
        reset_dialog();
        add_dialog_str(0,light_color,0);
        run_dialog(1);
break;

beginstate 14;
        reset_dialog();
        add_dialog_str(0,"There is a thick, cyan-colored magical barrier blocking this passage. You can't continue down the passage without going through the barrier.",0);
        add_dialog_choice(0,"Try to walk past the barrier.");
        add_dialog_choice(1,"Leave.");
        choice = run_dialog(1);
        if (choice == 1) {
                if ((get_flag(0,20) != 0) || (get_flag(0,21) != 1) || (get_flag(0,22) != 1)) {
                        reset_dialog();
                        add_dialog_str(0,"You approach the barrier. When you get close, a tendril of energy blasts you back. The nearby light flickers for a moment.",0);
                        run_dialog(1);
                        block_entry(1);
                }
                if ((get_flag(0,20) == 0) && (get_flag(0,21) == 1) && (get_flag(0,22) == 1)) {
                        reset_dialog();
                        add_dialog_str(0,"You approach the barrier. When you get close, it turns off for long enough to let you by.",0);
                        run_dialog(1);
                        block_entry(0);
                }
        }
        if (choice == 2) {
                block_entry(1);
        }
break;

beginstate 15;
        change_outdoor_location(1,1,24,41);
        if (get_flag(0,24) == 0) {
                reset_dialog();
                add_dialog_str(0,"Finally, you emerge from the Winding Tunnel. You have walked into a high mountain valley, which is covered with snow. Above you is only a ring of mountains and clear blue sky.",0);
                add_dialog_str(1,"You have reached the center of Cherayeng Island. Someone went to a great effort to conceal this valley. Whoever they are, you'll find them by exploring it. Making your exploration fast may be vital, as the valley is very cold.",0);
                add_dialog_str(4,"Meanwhile...",0);
                set_flag(0,24,1);
                run_dialog(1);
                set_total_visibility(1);
                force_view_center(40,40);
                force_instant_terrain_redraw();
                text_bubble_on_char(33,"The humanssss");
                force_instant_terrain_redraw();
                pause(10);
                text_bubble_on_char(33,"");
                text_bubble_on_char(33,"have broken");
                force_instant_terrain_redraw();
                pause(10);
                text_bubble_on_char(33,"");
                text_bubble_on_char(33,"through our");
                force_instant_terrain_redraw();
                pause(10);
                text_bubble_on_char(33,"");
                text_bubble_on_char(33,"defencesss!");
                force_instant_terrain_redraw();
                pause(10);
                text_bubble_on_char(33,"");
                text_bubble_on_char(30,"They may sssoon");
                force_instant_terrain_redraw();
                pause(10);
                text_bubble_on_char(30,"");
                text_bubble_on_char(30,"get here.");
                force_instant_terrain_redraw();
                pause(10);
                text_bubble_on_char(30,"");
                text_bubble_on_char(27,"They won't make it.");
                force_instant_terrain_redraw();
                pause(10);
                text_bubble_on_char(27,"");
                text_bubble_on_char(27,"Esssek, take a force"); //Esseck
                force_instant_terrain_redraw();
                pause(10);
                text_bubble_on_char(27,"");
                text_bubble_on_char(27,"to destroy them."); //dessstroy
                force_instant_terrain_redraw();
                pause(10);
                text_bubble_on_char(27,"");
                text_bubble_on_char(31,"Consssider it done.");
                force_instant_terrain_redraw();
                pause(10);
                force_view_center(char_loc_x(0),char_loc_y(0));
                force_instant_terrain_redraw();
                set_total_visibility(0);
                erase_text_bubbles();
                inc_flag(0,18,1);
        }
break;

beginstate 16;
        if (get_flag(0,25) == 0) {
                reset_dialog();
                add_dialog_str(0,"This chest is held shut by a large, slightly glowing, padlock. You won't be able to break it open. On top of the chest are two grids and 32 multicolored tiles.",0);
                add_dialog_str(1,"One of the grids has 16 of the tiles in it. The other is empty. You guess that the lock has something to do with the other 16 tiles.",0);
                add_dialog_choice(0,"Play with the tiles.");
                add_dialog_choice(1,"Leave.");
                choice = run_dialog(1);
		reset_dialog();
                if (choice == 1) {
                        if (get_highest_skill(2) < 15) {
                                add_dialog_str(0,"You start playing with the tiles. None of them match anything in the original 16, so the meaning of the first grid is more obscure. Unfortunately, you can't figure out the pattern. After about 20 failed tries, you give up and leave.",0);
                                block_entry(1);
                                run_dialog(1);
                        end();
                        }
                        if (get_highest_skill(2) >= 15) {
                               add_dialog_str(0,"You start playing with the tiles. None of them match anything in the original 16, so the meaning of the first grid is more obscure. Fortunately, you have enough intelligence to figure out the pattern.",0);
                               add_dialog_str(1,"The puzzle is complicated, but you figure it out after a couple of attempts. The lock opens once you've figured out the puzzle. Inside are several glowing orbs.",0);
                               run_dialog(1);
                               set_flag(0,25,2); //line changed in 1.0b1
                               end();
                        }
                }
                if (choice == 2) {
                        block_entry(1);
                        end();
                }
        }
        if (get_flag(0,25) == 1) {
                add_dialog_str(0,"This chest is still open, and there are more orbs inside.",0);
                add_dialog_choice(0,"Take some of the orbs.");
                add_dialog_choice(1,"Leave.");
                if (choice == 1) {
                        char_give_item(1000,452);
                }
        }
break;

beginstate 17;
        change_outdoor_location(1,2,32,19);
break;