begintownscript;

variables;

int i,j,k,r1,choice,w,x,top_left,top_right,bottom_left;
short top_left_y;

body;

beginstate INIT_STATE;
        set_name(10,"Arisss"); //Aris
        set_name(11,"Esszss"); //Essz
        set_name(12,"Thessh"); //Thesh
        set_name(13,"Xolesi"); //Xolesi, Ustess
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
        //code called every turn
        if ((get_flag(1,13) > 0) && (get_flag(1,14) != ((get_current_tick() % 100) + 1))) {
                top_left = 9; //x coordinate of top left of rectangle
                top_left_y = 33; //y coordinate of top left of rectangle
                bottom_left = 39; //x coordinate of bottom left of rectangle
                top_right = 15; //y coordinate of top right of rectangle
                x = 1;
                print_str("The Chain Lightning continues to jump around.");
                set_flag(x,7,get_ran(1,top_left,top_right));
                set_flag(x,8,get_ran(1,top_left_y,bottom_left));
                set_flag(x,9,get_ran(1,top_left,top_right));
                set_flag(x,10,get_ran(1,top_left_y,bottom_left));
                set_flag(x,11,get_ran(1,top_left,top_right));
                set_flag(x,12,get_ran(1,top_left_y,bottom_left));
                put_jagged_zap(get_flag(x,1), get_flag(x,2),get_flag(x,7),get_flag(x,8),0);
                put_jagged_zap(get_flag(x,3), get_flag(x,4),get_flag(x,9),get_flag(x,10),0);
                put_jagged_zap(get_flag(x,5), get_flag(x,6),get_flag(x,11),get_flag(x,12),0);
                put_boom_on_space(get_flag(x,7),get_flag(x,8),2,0);
                put_boom_on_space(get_flag(x,9),get_flag(x,10),2,0);
                put_boom_on_space(get_flag(x,11),get_flag(x,12),2,0);
                run_animation_sound(52);
                damage_near_loc(get_flag(x,7),get_flag(x,8),(50 + (get_ran(1,0,30))),0,3);
                damage_near_loc(get_flag(x,9),get_flag(x,10),(50 + (get_ran(1,0,30))),0,3);
                damage_near_loc(get_flag(x,11),get_flag(x,12),(50 + (get_ran(1,0,30))),0,3);
                set_flag(x,1,(get_flag(x,7)));
                set_flag(x,2,(get_flag(x,8)));
                set_flag(x,3,(get_flag(x,9)));
                set_flag(x,4,(get_flag(x,10)));
                set_flag(x,5,(get_flag(x,11)));
                set_flag(x,6,(get_flag(x,12)));
                inc_flag(1,13,-1);
        }
        if (get_flag(2,3) == 2) {
                teleport_party(39,40,1);
                //move_to_new_town(7,30,43);
                set_flag(2,3,3);
        }
break;

beginstate 10;
        move_to_new_town(7,14,7);
break;

beginstate 11;
        reset_dialog();
        add_dialog_str(0,"This is a large, blue crystal pedestal, which is currently displaying the northeast coast of Cherayeng Island. Nothing appears to be happening, and there aren't any obvious ways of changing the picture. After a minute of looking, you leave.",0);
        run_dialog(1);
break;

beginstate 12;
        move_to_new_town(7,30,43);
break;

beginstate 13;
        if (get_flag(1,23) == 0) {
                reset_dialog_preset_options(7);
                choice = run_dialog(1);
                if (choice == 2) {
                        reset_dialog();
                        add_dialog_str(0,"The book describes numerous methods of manipulating cold. You aren't skilled enough to learn from most of it, but you improve your skill in Ice Lances if you aren't too skilled in it already.",0);
                        run_dialog(1);
                        while (w < 4) {
                                if ((get_spell_level(w,0,6) < 7) && (get_flag(1,23) == 0)) {
                                        change_spell_level(w,0,6,1);
                                        w = w + 1;
                                }
                        set_flag(1,23,1);
                        }
                }
        }
break;

beginstate 14;
        if (get_flag(1,24) == 0) {
                reset_dialog_preset_options(7);
                choice = run_dialog(1);
                if (choice == 2) {
                        reset_dialog();
                        add_dialog_str(0,"This book is full of many different kinds of puzzles, and the more you read, the more you get drawn into them, and the more effort they take to solve. Finally, after getting extremely confused, you put the book back.",0);
                        run_dialog(1);
                        set_char_status(1000,13,25,1,0);
                        set_flag(1,24,1);
                }
        }
break;

beginstate 15;
        reset_dialog();
        add_dialog_str(0,"One of the bigger books in this bookshelf is unexpectedly light. You take it off the shelf, and see a lever behind it. The book itself is hollow.",0);
        add_dialog_choice(0,"Pull the lever.");
        add_dialog_choice(1,"Leave.");
        choice = run_dialog(1);
        if (choice == 1) {
                flip_terrain(8,23); //first set of gates
                flip_terrain(9,23);
        }
break;

beginstate 16;
        if (get_flag(1,25) == 0) {
                reset_dialog_preset_options(7);
                choice = run_dialog(1);
                if (choice == 2) {
                        reset_dialog();
                        add_dialog_str(0,"This book is a long, complicated, manual on how to make and use an observation crystal. Most of the instructions in it are far too complex for you to understand, but you do learn more about the Far Sight spell by reading it.",0);
                        run_dialog(1);
                        while (w < 4) {
                                if ((get_spell_level(w,0,9) < 6) && (get_flag(1,25) == 0)) {
                                        change_spell_level(w,0,9,1);
                                        w = w + 1;
                                }
                        set_flag(1,23,1);
                        }
                }
        }
break;

beginstate 17;
        if (get_flag(1,26) == 0) {
                reset_dialog();
                add_dialog_str(0,"As you walk down this corridor, you stumble over a tripwire which was attached to a teleporter trap. In less than a second, a teleporter grabs you, and you find yourself somewhere else...",0);
                run_dialog(1);
                //play_sound(10);
                teleport_party(36,33,0);
                reset_dialog();
                add_dialog_str(0,"You arrive in the center of a room, surrounded by four drakes. One of them says _You've come a long way, far further than we expected, but you still have a long way to go._",0);
                add_dialog_str(1,"Another intones _The journey from here is ssssomething you will never reach. Your adventure will end here._ All of the drakes adopt fighting positions.",0);
                run_dialog(1);
        }
        set_flag(1,26,1);
break;

beginstate 18;
        reset_dialog_preset_options(2);
        choice = run_dialog(0);
        if (choice == 2) {
                flip_terrain(36,38);
                flip_terrain(5,31); //third set of gates
                flip_terrain(6,31);
        }
break;

beginstate 19;
        reset_dialog();
        add_dialog_str(0,"Tucked inside one of these books is a note on thick blue paper, which looks like it was written by a drake. The note reads _One of the keys to the central treasure chamber was lost in the northwestern part of this isle._",0);
        add_dialog_str(1,"_I've asked Arisss to retrieve it when an opportunity presents itself, which hasn't happened so far. I still have the other key, and there are other defenses around the treasure._",0);
        run_dialog(1);
break;

beginstate 20;
        reset_dialog();
        if ((has_item(480) == 0) && (has_item(481) == 0) && (has_item(482) == 0)) {
                add_dialog_str(0,"This pedestal has a cyan button on it, and a depression for a single stone. You go through your inventory, but you don't have anything which fits on the pedestal.",0);
        }
        if ((has_item(480) == 1) || (has_item(481) == 1) || (has_item(482) == 1)) {
                set_flag(1,28,1);
                add_dialog_str(0,"This pedestal has a cyan button on it, and a depression for a single stone. It looks like the stones you found nearby will fit into the depression.",0);
                add_dialog_choice(0,"Put one of the stones into the depression and push the button.");
                add_dialog_choice(1,"Leave.");
        }
        choice = run_dialog(1);
        if ((choice == 1) && (get_flag(1,28) > 0)) {
                if ((has_item(480) + has_item(481) + has_item(482)) > 1) {
                        reset_dialog();
                        add_dialog_str(0,"All of the stones you are carrying change color briefly, and then return to their original color. Nothing else happens. Maybe you need to be carrying stones of only one color to be able to use this button properly.",0);
                        run_dialog(1);
                        end();
                }
                set_state_continue(23);
        }
break;

beginstate 21;
        reset_dialog();
        if ((has_item(480) == 0) && (has_item(481) == 0) && (has_item(482) == 0)) {
                add_dialog_str(0,"This pedestal has a yellow button on it, and a depression for a single stone. You go through your inventory, but you don't have anything which fits on the pedestal.",0);
        }
        if ((has_item(480) == 1) || (has_item(481) == 1) || (has_item(482) == 1)) {
                set_flag(1,28,2);
                add_dialog_str(0,"This pedestal has a yellow button on it, and a depression for a single stone. It looks like the stones you found nearby will fit into the depression.",0);
                add_dialog_choice(0,"Put one of the stones into the depression and push the button.");
                add_dialog_choice(1,"Leave.");
        }
        choice = run_dialog(1);
        if ((choice == 1) && (get_flag(1,28) > 0)) {
                if ((has_item(480) + has_item(481) + has_item(482)) > 1) {
                        reset_dialog();
                        add_dialog_str(0,"All of the stones you are carrying change color briefly, and then return to their original color. Nothing else happens. Maybe you need to be carrying stones of only one color to be able to use this button properly.",0);
                        run_dialog(1);
                        end();
                }
                set_state_continue(23);
        }
break;

beginstate 22;
        reset_dialog();
        if ((has_item(480) == 0) && (has_item(481) == 0) && (has_item(482) == 0)) {
                add_dialog_str(0,"This pedestal has a magenta button on it, and a depression for a single stone. You go through your inventory, but you don't have anything which fits on the pedestal.",0);
        }
        if ((has_item(480) == 1) || (has_item(481) == 1) || (has_item(482) == 1)) {
                set_flag(1,28,3);
                add_dialog_str(0,"This pedestal has a magenta button on it, and a depression for a single stone. It looks like the stones you found nearby will fit into the depression.",0);
                add_dialog_choice(0,"Put one of the stones into the depression and push the button.");
                add_dialog_choice(1,"Leave.");
        }
        choice = run_dialog(1);
        if ((choice == 1) && (get_flag(1,28) > 0)) {
                if ((has_item(480) + has_item(481) + has_item(482)) > 1) {
                        reset_dialog();
                        add_dialog_str(0,"All of the stones you are carrying change color briefly, and then return to their original color. Nothing else happens. Maybe you need to be carrying stones of only one color to be able to use this button properly.",0);
                        run_dialog(1);
                        end();
                }
                set_state_continue(23);
        }
break;

beginstate 23;
        if ((get_flag(1,28) == 1) && (has_item(480) == 1)) {
                print_str("The blue stone becomes green.");
                take_item(480);
                reward_give(482);
                set_flag(1,28,0);
                end();
        }
        if ((get_flag(1,28) == 1) && (has_item(481) == 1)) {
                print_str("The red stone disappears.");
                take_item(481);
                set_flag(1,28,0);
                end();
        }
        if ((get_flag(1,28) == 1) && (has_item(482) == 1)) {
                print_str("The green stone becomes blue.");
                take_item(482);
                reward_give(480);
                set_flag(1,28,0);
                end();
        }
        if ((get_flag(1,28) == 2) && (has_item(480) == 1)) {
                print_str("The blue stone disappears.");
                take_item(480);
                set_flag(1,28,0);
                end();
        }
        if ((get_flag(1,28) == 2) && (has_item(481) == 1)) {
                print_str("The red stone becomes green.");
                take_item(481);
                reward_give(482);
                set_flag(1,28,0);
                end();
        }
        if ((get_flag(1,28) == 2) && (has_item(482) == 1)) {
                print_str("The green stone becomes red.");
                take_item(482);
                reward_give(481);
                set_flag(1,28,0);
                end();
        }
        if ((get_flag(1,28) == 3) && (has_item(480) == 1)) {
                print_str("The blue stone becomes red.");
                take_item(480);
                reward_give(481);
                set_flag(1,28,0);
                end();
        }
        if ((get_flag(1,28) == 3) && (has_item(481) == 1)) {
                print_str("The red stone becomes blue.");
                take_item(481);
                reward_give(480);
                set_flag(1,28,0);
                end();
        }
        if ((get_flag(1,28) == 3) && (has_item(482) == 1)) {
                print_str("The green stone disappears.");
                take_item(482);
                set_flag(1,28,0);
                end();
        }
break;

beginstate 24;
        if ((get_flag(1,29) == 0) && ((has_item(480) + has_item(481) + has_item(482)) < 3)) {
                reset_dialog();
                add_dialog_str(0,"There is another locked gate in your way. This one has three depressions on the ground, which are colored red, green, and blue respectively. You don't have anything which is the right size and color to fill all three sockets.",0);
                run_dialog(1);
                end();
        }
        if ((get_flag(1,29) == 0) && ((has_item(480) + has_item(481) + has_item(482)) == 3)) {
                reset_dialog();
                add_dialog_str(0,"There is another locked gate in your way. This one has three depressions on the ground, which are colored red, green, and blue respectively. You are carrying stones of the correct colors to fill the sockets.",0);
                add_dialog_choice(0,"Put the stones into the depressions.");
                add_dialog_choice(1,"Leave.");
                choice = run_dialog(1);
                if (choice == 1) {
                        reset_dialog();
                        add_dialog_str(0,"You insert each of the stones into the depression with the same color, and they fuse themselves when fully inserted. Once the last stone has been inserted, the nearby gate opens with a grinding noise.",0);
                        set_flag(1,29,1);
                        flip_terrain(21,32);
                        flip_terrain(21,33);
                        flip_terrain(21,34);
                        take_item(480);
                        take_item(481);
                        take_item(482);
                        end();
                }
        }
        if ((get_flag(1,29) > 0) && (get_terrain(21,32) == 29)) {
                flip_terrain(21,32);
                flip_terrain(21,33);
                flip_terrain(21,34);
        }
break;

beginstate 25;
        if (get_flag(2,0) == 0) {
                reset_dialog();
                add_dialog_str(0,"At long last, you arrive at the innermost part of Cherayeng Island. It is a small, lava-filled cavern. You look briefly around for signs of life, which aren't too difficult to spot.",0);
                add_dialog_str(1,"A large dragon is sitting in the center of this cavern. The dragon looks extremely angry. She shoots a lance of fire at you, which you narrowly avoid.",0);
                add_dialog_str(2,"_I am Sulfras, and I have gone to great effort to hide from the Empire. Now you find me, even here! You will all die, and slowly!_",0);
                add_dialog_str(3,"You try to explain that you are just trying to escape the island, but she will have none of it. _The Empire has tricked me before, and they won't ever do so again. I don't believe your lies._",0);
                set_flag(2,0,1);
                run_dialog(1);
                activate_hidden_group(1);
                end();
        }
break;

beginstate 26;
        reset_dialog_preset_options(2);
        choice = run_dialog(0);
        if (choice == 2) {
                flip_terrain(9,27);
                flip_terrain(7,27); //second set of gates
                flip_terrain(7,28);
        }
break;