begintownscript;

variables;

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

body;

beginstate INIT_STATE;
        if (get_flag(1,15) == 0) {
                reset_dialog();
                add_dialog_str(0,"After a long, difficult, trek, you finally reach the core of Cherayeng Island. Someone went to great trouble to conceal this place, and they have placed guards near the entrance. You can also see four pedestals, one of which has a light on top.",0);
                run_dialog(1);
                set_flag(1,15,1);
        }
        if (get_flag(2,3) == 1) {
                reset_dialog();
                add_dialog_str(0,"You don't know what you're doing with your backs to this wall, but there is a closed gate ahead of you along with a lever. Opening this gate should be simple.",0);
                run_dialog(1);
                set_flag(2,3,2);
        }
        if ((get_flag(1,16) + get_flag(1,17) + get_flag(1,18)) == 0) {
                set_flag(1,16,1); //red
                set_flag(1,17,1); //blue
        }
        put_field_on_space(7,30,6);
        add_range_to_group(18,28,1);
        set_summon_level(1001,1);
        set_flag(1,22,0);
        set_name(16,"Nysssr"); //Nysr
        set_name(17,"Kyssst"); //Kyst
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
        //code called every turn
        if ((get_flag(1,21) == 1) && (char_ok(16) + char_ok(17)) == 0) {
                set_flag(1,21,2);
                erase_char(1001);
        }
        i = 18;
        while ((i < 28) && (get_flag(1,21) == 1)) {
                if (char_ok(i) == 0) {
                        spawn_creature(i);
                }
                i = i + 1;
        }
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) {
                add_dialog_str(0,"You hear a soft click from the button. The light on the central pedestal changes color.",0);
                set_flag(1,16,(1 - get_flag(1,16)));
                run_dialog(1);
        }
        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) {
                add_dialog_str(0,"You hear a soft click from the button. The light on the central pedestal changes color.",0);
                set_flag(1,17,(1 - get_flag(1,17)));
                run_dialog(1);
        }
        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) {
                add_dialog_str(0,"You hear a soft click from the button. The light on the central pedestal changes color.",0);
                set_flag(1,18,(1 - get_flag(1,18)));
                run_dialog(1);
        }
        if (choice == 2) {
                end();
        }
break;

beginstate 13;
        clear_buffer();
        append_string("This pedestal contains a single light. The light is currently ");
        if ((get_flag(1,16) == 0) && (get_flag(1,17) == 0) && (get_flag(1,18) == 0)) {
                append_string("off.");
        }
        if ((get_flag(1,16) == 1) && (get_flag(1,17) == 0) && (get_flag(1,18) == 0)) {
                append_string("red.");
        }
        if ((get_flag(1,16) == 0) && (get_flag(1,17) == 1) && (get_flag(1,18) == 0)) {
                append_string("blue.");
        }
        if ((get_flag(1,16) == 0) && (get_flag(1,17) == 0) && (get_flag(1,18) == 1)) {
                append_string("green.");
        }
        if ((get_flag(1,16) == 1) && (get_flag(1,17) == 1) && (get_flag(1,18) == 0)) {
                append_string("magenta.");
        }
        if ((get_flag(1,16) == 1) && (get_flag(1,17) == 0) && (get_flag(1,18) == 1)) {
                append_string("yellow.");
        }
        if ((get_flag(1,16) == 0) && (get_flag(1,17) == 1) && (get_flag(1,18) == 1)) {
                append_string("cyan.");
        }
        if ((get_flag(1,16) == 1) && (get_flag(1,17) == 1) && (get_flag(1,18) == 1)) {
                append_string("white.");
        }
        append_string(" There is also a button on the pedestal.");
        get_buffer_text(light_color);
        reset_dialog();
        add_dialog_str(0,light_color,0);
        add_dialog_choice(0,"Push the button.");
        add_dialog_choice(1,"Leave.");
        choice = run_dialog(1);
        if (choice == 1) {
                add_dialog_str(0,"You hear a soft click from the button, immediately followed by grinding noises from some of the portculli.",0);
                play_sound(99);
                set_terrain(20,39,27); //red
                set_terrain(20,38,27); //red
                set_terrain(20,34,27); //yellow
                set_terrain(20,33,27); //yellow
                set_terrain(23,32,26); //green
                set_terrain(24,32,26); //green
                set_terrain(25,32,26); //green
                set_terrain(28,33,29); //cyan
                set_terrain(28,34,29); //cyan
                set_terrain(28,38,29); //blue
                set_terrain(28,39,29); //blue
                set_terrain(25,40,28); //purple
                set_terrain(24,40,28); //purple
                set_terrain(23,40,28); //purple
                if ((get_flag(1,16) == 1) && (get_flag(1,17) == 0) && (get_flag(1,18) == 0)) {
                        set_terrain(20,39,31); //red
                        set_terrain(20,38,31); //red
                }
                if ((get_flag(1,16) == 0) && (get_flag(1,17) == 1) && (get_flag(1,18) == 0)) {
                        set_terrain(28,38,33); //blue
                        set_terrain(28,39,33); //blue
                }
                if ((get_flag(1,16) == 0) && (get_flag(1,17) == 0) && (get_flag(1,18) == 1)) {
                        set_terrain(23,32,30); //green
                        set_terrain(24,32,30); //green
                        set_terrain(25,32,30); //green
                }
                if ((get_flag(1,16) == 1) && (get_flag(1,17) == 1) && (get_flag(1,18) == 0)) {
                        set_terrain(25,40,32); //purple
                        set_terrain(24,40,32); //purple
                        set_terrain(23,40,32); //purple
                }
                if ((get_flag(1,16) == 1) && (get_flag(1,17) == 0) && (get_flag(1,18) == 1)) {
                        set_terrain(20,34,31); //yellow
                        set_terrain(20,33,31); //yellow
                }
                if ((get_flag(1,16) == 0) && (get_flag(1,17) == 1) && (get_flag(1,18) == 1)) {
                        set_terrain(28,33,33); //cyan
                        set_terrain(28,34,33); //cyan
                }
        }
        if (choice == 2) {
                end();
        }
break;

beginstate 14;
        reset_dialog();
        add_dialog_str(0,"You find a book here titled _Color Theory_. There is no author listed, and you don't think the book will be very interesting. Still, you could read it if you wanted.",0);
        add_dialog_choice(0,"Read the book.");
        add_dialog_choice(1,"Leave.");
        choice = run_dialog(1);
        if (choice == 1) {
                reset_dialog();
                add_dialog_str(0,"You open the first page and start reading. _Color Theory._",0);
                add_dialog_str(1,"We have worked for a while trying to come up with a reliable system for recording colors so that they can be accurately reproduced. Finally, we've been able to split any color we've seen into red, green, and blue components.",0);
                add_dialog_str(2,"By mixing various amounts of these three colors, we can almost perfectly reproduce any color we know of. Of interest is that we can produce magenta by mixing red and blue, while yellow is produced by combining red and green.",0);
                add_dialog_str(3,"If we add blue and green together, cyan is produced. Varying the amounts of these colors allows us to reproduce millions of different shades.",0);
                add_dialog_str(5,"The rest of the book details the precise amounts necessary to make each shade. You close the book without reading much further. At least this explains the mechanics behind some of the puzzles you've seen so far.",0);
                run_dialog(1);
        }
break;

beginstate 15;
        if (get_flag(1,19) < 1) {
                reset_dialog();
                add_dialog_str(0,"You find a room containing a small island model. It doesn't take you long to figure out what this is; a representation of Cherayeng Island. You've already seen a lot of the island, but there may be something valuable to be learned from the model.",0);
                add_dialog_str(1,"Unfortunately, the two eyebeasts in the room don't intend to give you a chance to look at the model's details.",0);
                run_dialog(1);
                set_flag(1,19,1);
        }
break;

beginstate 16;
        if (get_flag(1,20) == 0) {
                reset_dialog();
                add_dialog_str(0,"On the western edge of the model, you find a small representation of a boat. If the model is kept up to date, there should be a boat on the western edge of the island, which you may be able to use to escape.",0);
                add_dialog_str(1,"The scale is too small for you to tell if there's been any damage to the boat, however.",0);
                run_dialog(1);
                set_flag(1,20,1);
        }
break;

beginstate 17;
        if (get_flag(1,21) == 0) {
                reset_dialog();
                add_dialog_str(0,"You walk into a seemingly empty part of the caverns. Suddenly, out of nowhere, something says _Sssstop!_ Turning towards the source of the sound, you see two drakes and several other beasts.",0);
                add_dialog_str(1,"One of the drakes says _We congratulate you on getting passsst Esssek. Nobody hassss been able to get that far before. It sssshall make little difference, however. We will dessstroy you.",0);
                run_dialog(1);
                set_flag(1,21,1);
                activate_hidden_group(1);
                set_name(16,"Nysssr"); //Nysr
                set_name(17,"Kyssst"); //Kyst
        }
break;

beginstate 18;
        reset_dialog_preset_options(2);
        choice = run_dialog(0);
        if (choice == 2) {
                flip_terrain(34,5);
                flip_terrain(21,22);
                flip_terrain(21,23);
        }
break;

beginstate 19;
        reset_dialog_preset_options(3);
        choice = run_dialog(0);
        if (choice == 2) {
                teleport_party(40,9,0);
        }
break;

beginstate 20;
        flip_terrain(38,11); //rune 1 changes light 1
        flip_terrain(38,15); //...light 5...
        flip_terrain(38,16); //and light 6
break;

beginstate 21;
        flip_terrain(38,11); //rune 2
        flip_terrain(38,14); //light 4
        flip_terrain(38,16); //
break;

beginstate 22;
        flip_terrain(38,11); //rune 3
        flip_terrain(38,12); //light 2
        flip_terrain(38,13); //light 3
break;

beginstate 23;
        flip_terrain(38,11); //rune 4
        flip_terrain(38,12);
        flip_terrain(38,16);
break;

beginstate 24;
        flip_terrain(38,16); //concealed rune changes light 6
break;

beginstate 25;
        reset_dialog();
        if ((get_terrain(38,11) + get_terrain(38,12) + get_terrain(38,13) + get_terrain(38,14) + get_terrain(38,15) + get_terrain(38,16)) != 2670) {
                add_dialog_str(0,"There is a portal here, which was designed to teleport people somewhere else. When you get close, you realize that this one doesn't have enough energy to teleport you safely. You walk away from it.",0);
                run_dialog(1);
                end();
        }
        reset_dialog_preset_options(3);
        choice = run_dialog(0);
        if (choice == 2) {
                teleport_party(23,17,0);
        }
break;

beginstate 26;
        if ((is_object_on_space(6,10,2) == 0) && (get_flag(1,22) == 0)) {
                print_str("With a popping noise, a crate appears down the corridor.");
                put_object_on_space(6,10,2);
                set_flag(1,22,1);
        }
break;

beginstate 27;
        reset_dialog_preset_options(2);
        choice = run_dialog(0);
        if (choice == 2) {
                flip_terrain(7,20);
                flip_terrain(7,21);
                flip_terrain(24,13);
                flip_terrain(25,13);
        }
break;

beginstate 28;
        if ((is_object_on_space(23,5,2) + is_object_on_space(25,6,2) + is_object_on_space(25,8,2) + is_object_on_space(23,9,2) + is_object_on_space(21,6,2) + is_object_on_space(21,8,2) == 6) || (get_flag(2,1) == 1)) {
                set_flag(2,1,1);
                end();
        }
        reset_dialog();
        add_dialog_str(0,"You try to walk toward the staircase, but yet again, a magical field stops you. Nearby, you hear a reptilian voice intone _Put the cratessss on the runesssss to passssss._",0);
        run_dialog(1);
        block_entry(1);
break;

beginstate 29;
        move_to_new_town(8,7,8);
break;

beginstate 30;
        move_to_new_town(8,30,43);
break;

beginstate 31;
        reset_dialog_preset_options(2);
        choice = run_dialog(0);
        if (choice == 2) {
                flip_terrain(29,44);
                flip_terrain(27,42);
                flip_terrain(27,43);
                flip_terrain(25,40); //purple
                flip_terrain(24,40); //purple
                flip_terrain(23,40); //purple
        }
break;