// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.

begincreaturescript;

variables;

short i,target;
short last_abil_time,my_attack;
short r1,top_left,top_right,bottom_left,x;
short choice,my_target,num_targets;
short top_left_y;
string temp_string;

body;

beginstate INIT_STATE;
        change_spell_level(6,0,0,8);
        change_spell_level(6,0,1,8);
        change_spell_level(6,0,2,8);
        change_spell_level(6,0,3,8);
        change_spell_level(6,0,4,8);
        change_spell_level(6,0,5,7);
        change_spell_level(6,0,6,7);
        change_spell_level(6,0,7,7);
        change_spell_level(6,0,8,7);
        change_spell_level(6,0,9,7);
        change_spell_level(6,0,10,6);
        change_spell_level(6,0,11,6);
        change_spell_level(6,0,12,6);
        change_spell_level(6,0,13,6);
        change_spell_level(6,0,14,6);
        change_spell_level(6,0,15,5);
        change_spell_level(6,0,16,5);
        change_spell_level(6,0,17,5);
        change_spell_level(6,0,18,5);
        change_spell_level(6,0,19,5);
break;

beginstate DEAD_STATE;
        reset_dialog();
        add_dialog_str(0,"After a long fight, you deal Sulfras a crippling blow! The blow wasn't powerful enough to kill her immediately, and she is able to get off one more spell before dying. This spell collapses the entire structure onto your heads, killing everyone inside it.",0);
        add_dialog_str(1,"Eventually, the remaining members of the expedition decide you're never coming back, and that their best chance is to get off the island with the lifeboats. In that, they succeed.",0);
        run_dialog(1);
        kill_char(1000,3,0);
break;

beginstate START_STATE; 
        // if I have a target for some reason, go attack it
        if (target_ok()) {
                if (dist_to_char(get_target()) <= 16) {
                        set_state(3);
                        set_mobility(ME,1);
                }
                        else set_target(ME,-1);
        }
        
        // Look for a target, attack it if visible
        if (select_target(ME,8,0)) {
                //do_attack();
                set_state(3);
                }
                
        // Have I been hit? Strike back!
        if (who_hit_me() >= 0) {
                set_target(ME,who_hit_me());
                //do_attack();
                set_state(3);
                }
                
        // Otherwise, just peacefully move around. Go back to start, if I'm too far
        // from where I started.
        if (my_dist_from_start() >= 6 || my_dist_from_start() > 0) {
                if (get_ran(1,1,100) < 40) 
                        return_to_start(ME,1);
                }
                else if (get_memory_cell(0) == 0) {
                        fidget(ME,25);
                        }

        // if we're in combat and the above didn't give me anything to do, just
        // stop now. Otherwise, game will keep running script, and that eats up CPU time.
        if (am_i_doing_action() == FALSE)
                end_combat_turn();
break;

beginstate 3; // attacking
        if ((get_flag(2,3) == 0) && ((get_health(ME)) < (get_max_health(ME) / 2))) {
                reset_dialog();
                add_dialog_str(0,"You have succeeded in severly wounding Sulfras! She realizes that you could be a serious threat, and is now willing to give talking a try _You said that you wanted to find a way off this island?_.",0);
                add_dialog_choice(0,"Yes, we are trying to find a way off this island.");
                choice = run_dialog(1);
                if (choice == 2) {
                        set_flag(2,3,1);
                        end();
                }
                reset_dialog();
                add_dialog_str(0,"She thinks carefully for a moment. _I have an offer I can make with that. As you may know by now, there is a huge nest of basilisks on the western shore blocking access to a boat._",0);
                add_dialog_str(1,"She continues talking. _I can give you an item which would help you pass them, but in return, you must allow me to wipe some of your memories. However, if you don't accept this offer, I'll continue fighting you.",0);
                add_dialog_choice(0,"We accept your offer.");
                add_dialog_choice(1,"We reject your offer.");
                choice = run_dialog(1);
                if (choice == 2) {
                        set_flag(2,3,1);
                        end();
                }
                reset_dialog();
                add_dialog_str(0,"She looks relieved. _Very well, then._ She walks off for a moment and then returns with a brown orb. _When used, this orb will give you the appearance of a basilisk, allowing you to get by them undetected. And now, for your end of the bargain._",0);
                add_dialog_str(1,"She casts a spell, and you pass out. When you wake up, you aren't where you were.",0);
                run_dialog(1);
                set_attitude(ME,4);
                set_flag(2,3,2);
                change_spec_item(2,1);
                end();
        }

        if (target_ok() == FALSE)
                set_state(START_STATE);
        my_attack = (get_ran(1,1,8));
        if ((my_attack == 1) && (get_target() >= 0) && (dist_to_char(get_target()) < 8) && (get_energy(ME) >= 25)) {
                print_named_str(ME,"casts Coldwave!");
                put_straight_zap(char_loc_x(ME), char_loc_y(ME),char_loc_x(get_target()),char_loc_y(get_target()),6);
                put_boom_on_char(get_target(),2,0);
                run_animation_sound(52);
                damage_near_loc(char_loc_x(get_target()),char_loc_y(get_target()), 80 + (get_ran(1,0,80)),3,5);
                // What I wanted to do is like damage_nearby, but from the target's coordinates, not the caster's.
                change_char_energy(ME,-25);
                deduct_ap(5);
        }
        if ((my_attack == 2) && (get_target() >= 0) && (dist_to_char(get_target())) < 6 && (get_energy(ME) >= 40)) {
                print_named_str(ME,"casts Weaken Foe!");
                change_char_health(get_target(),get_max_health(get_target()) / (0 - 10));
                change_char_energy(get_target(),get_energy(get_target()) / (0 - 10));
                play_sound(65);
                change_char_energy(ME,-40);
                deduct_ap(5);
        }
        if ((my_attack == 3) && (get_target() >= 0) && (get_energy(ME) >= 70) && (get_flag(1,13) == 0)) {
                top_left = 9; //x coordinate of top left of rectangle
                top_left_y = 33; //y coordinate of top left of rectangle
                bottom_left = 39; //y coordinate of bottom left of rectangle
                top_right = 15; //x coordinate of top right of rectangle
                x = 1;
                print_named_str(ME,"casts Chain Lightning!");
                set_flag(1,13,10);
                set_flag(1,14,get_current_tick() % 100)
                set_flag(x,1,char_loc_x(ME));
                set_flag(x,2,char_loc_y(ME));
                set_flag(x,3,char_loc_x(ME));
                set_flag(x,4,char_loc_y(ME));
                set_flag(x,5,char_loc_x(ME));
                set_flag(x,6,char_loc_y(ME));
                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));
                clear_buffer();
                append_string("(");
                append_number(get_flag(x,7));
                append_string(",");
                append_number(get_flag(x,8));
                append_string(") (");
                append_number(get_flag(x,9));
                append_string(",");
                append_number(get_flag(x,10));
                append_string(") (");
                append_number(get_flag(x,11));
                append_string(",");
                append_number(get_flag(x,12));
                append_string(")");
                get_buffer_text(temp_string);
                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);
                change_char_energy(ME, -70); //-100
                deduct_ap(5);
                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)));
        }

        if ((my_attack == 4) && (get_target() >= 0) && (dist_to_char(get_target()) < 7) && (get_energy(ME) >= 50) && (get_flag(0,14) != 3)) {
                print_named_str(ME,"casts Ball Lightning!");
                put_straight_zap(char_loc_x(ME), char_loc_y(ME),char_loc_x(get_target()),char_loc_y(get_target()),0);
                put_boom_on_char(get_target(),1,0);
                run_animation_sound(102);
                if (get_flag(0,14) < 2) {
                        damage_char(get_target(),500 + get_ran(1,0,300),3);
                }
                else {
                        reset_dialog();
                        add_dialog_str(0,"Sulfras attempts to shoot a ball of lightning at you, but Cerund has taught you how to protect yourself from this attack. You deflect the ball before it can damage you. Sulfras looks alarmed, but resolves to continue fighting you.",0);
                        run_dialog(1);
                        set_flag(0,14,3);
                }
                change_char_energy(ME, -50); //100
                deduct_ap(5);
        }
        if ((my_attack == 5) && (get_target() >= 0) && (dist_to_char(get_target())) < 6 && (get_energy(ME) >= 40)) {
                print_named_str(ME,"casts Chaos!");
                num_targets = 3;
                while (num_targets > 0) {
                        my_target = random_party_member();
                        put_jagged_zap(char_loc_x(ME), char_loc_y(ME),char_loc_x(my_target),char_loc_y(my_target),5);
                        put_boom_on_char(my_target,get_ran(1,0,7),0);
                        damage_char(my_target,40 + get_ran(1,0,20),get_ran(1,0,6));
                        run_animation_sound(62);
                        num_targets = num_targets - 1;
                }
                change_char_energy(ME,-40);
                deduct_ap(5);
        }
        do_attack();
break;

beginstate TALKING_STATE;
        print_str("Talking: It doesn't respond.");
        end();
break;