// trap.txt - This is the basic script that powers all traps in boxes, chests, 
// etc., or traps places in open spaces and corridors. If you are working on a 
// scenario that contains traps, DO NOT REMOVE THIS SCRIPT.
                                        
// When you want to put a trap on a section of floor or container, you need to
// use the create terrain script button. Then you need to set the memory cells
// for the sort of trap.

// Alterations by Ishad Nha
// I wanted a script that could be used in porting scenarios from Exile to Avernum,
// so the first thirteen trap numbers are simply those found in the BoE Editor Help.

// Memory Cells -                         
//   0 - Trap difficulty. This is the tool use skill needed to disarm the trap.
//         Lockpicks don't help. If this is set to a really high number (say, 200),
//         it won't be disarmable.                
//   1 - Key needed. If left at 0, no special item helps bypass the trap. 
//        Otherwise, if that party has this special item, the trap disarms 
//        automatically.                        
//   2,3 - Coordinates for a stuff done flag. If these are 0 and 0, ignored. 
//        Otherwise, the stuff done flag is set to 1 when the trap is disarmed. If 
//        the flag is non-zero, than when the party enters this zone, trap is still 
//        disarmed.                        
//   4 - Trap type. What sort of damage does the trap do? The amount of damage 
//         is determined by the severity, which is set in memory cell 5.
//                0        randomly selected from the list below
//                1        unblockable damage explosion, damages person who activated trap, does 
//                        1-8 damage times the trap's severity (Unblockable damage kills a character 
//                                if it takes it below 0 health.)
//                2        poison 1 character
//                3        poison party        
//                4        damage party, an unblockable damage explosion damages everyone within 
//                                4 spaces, does 1-8 damage times the trap's severity
//                5        paralyze character
//                6        no trap after all
//                7        drain experience
//                8        make town hostile
//                9        fire damages person who activated trap, does 1-8 damage times the trap's
//                                severity (Note fire explosions have very little chance of killing the victim,
//                                as it will usually only knock the victim down to 0 health)
//                10        dumbfound 1 character
//                11        disease 1 character
//                12        disease party        
//                13        fire explosion, damages everyone within 4 spaces, does 1-8 damage times 
//                                the trap's severity
//                14        Spawns a creature. Severity is number of creature to spawn.
//   5 - Trap severity. How much effect the trap has. If left at 0, defaults to 
//          a severity of 10.                
//   6 - Run town script (#).  This is something I have added myself, this enables a
//         node in the townscript to be run when the party successfully completes the 
//         disarming of the trap.                
//   7 - Run town script (#).  Like part (6) above, only this occurs when disarming 
//         fails and the trap is triggered.        
                                        
beginterrainscript;                         
                                        
variables;                                
        short i_am_active = 0;        
        short choice,r1,u,w;                
body;                                        
                                        
beginstate INIT_STATE;                
        if (get_memory_cell(5) == 0)        
                set_memory_cell(5,10);
                                        
        if (get_memory_cell(0) > 0) {        
                i_am_active = 1;        
                set_mechanism_difficulty(get_memory_cell(0));
                if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
                        if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
                                i_am_active = 0;
                        }                
                }                        
        break;                                
                                        
beginstate START_STATE;                
break;                                        
                                        
beginstate SEARCH_STATE;        
        if (i_am_active == 1) {        
                reset_dialog();        
                if (get_highest_skill(15) < get_memory_cell(0) - 6) 
                        add_dialog_str(0,"You think that you have detected a trap. Your character with the highest Tool Use skill can attempt to disarm it. However, the trap seems impossibly difficult.",0);
                        else if (get_highest_skill(15) < get_memory_cell(0)) 
                                add_dialog_str(0,"You think that you have detected a trap. Your character with the highest Tool Use skill can attempt to disarm it. However, the trap seems very difficult.",0);
                                else add_dialog_str(0,"You think that you have detected a trap. Your character with the highest Tool Use skill can attempt to disarm it. The trap doesn't seem difficult.",0);
                                        
                add_dialog_choice(0,"Leave it alone.");
                add_dialog_choice(1,"Try to disarm the trap.");
                choice = run_dialog(0);
                if (choice == 1) {        
                        block_entry(1);
                        end();                
                        }                
                if (choice == 2)         
                set_state_continue(8);
                }                        
                break;                        
                                        
beginstate STEP_INTO_SPOT_STATE;
        if (i_am_active == 1) {        
                reset_dialog();        
                if (get_highest_skill(15) < get_memory_cell(0) - 6) 
                        add_dialog_str(0,"You think that you have detected a trap in the area ahead. Your character with the highest Tool Use skill can attempt to disarm it. However, the trap seems impossibly difficult.",0);
                        else if (get_highest_skill(15) < get_memory_cell(0)) 
                                add_dialog_str(0,"You think that you have detected a trap in the area ahead. Your character with the highest Tool Use skill can attempt to disarm it. However, the trap seems very difficult.",0);
                                else add_dialog_str(0,"You think that you have detected a trap in the area ahead. Your character with the highest Tool Use skill can attempt to disarm it. The trap doesn't seem difficult.",0);
                                        
                add_dialog_choice(0,"Leave it alone.");
                add_dialog_choice(1,"Try to disarm the trap.");
                choice = run_dialog(0);
                if (choice == 1) {        
                        block_entry(1);
                        end();                
                        }                
                if (choice == 2)         
                set_state_continue(8);
        }                                
        break;                                
        beginstate 8;                        
                if ((get_highest_skill(15) < get_mechanism_difficulty()) && ((get_memory_cell(1) == 0) || (has_special_item(get_memory_cell(1)) == 0))) 
                        set_state_continue(9);
                else set_state_continue(40);
                break;                        
                                        
                        beginstate 9;        
                        print_str_color("The trap goes off!",1);
                                w = char_who_activated_script();
                        set_state_continue(get_memory_cell(4) + 10);
                        break;                
                        beginstate 10;
                        u = get_ran(1,1,14);
                        set_state_continue(u + 10);
                        break;                
                        beginstate 11;
                                put_boom_on_space(my_loc_x(),my_loc_y(),0,0);
                                run_animation_sound(-51);
                                r1 = get_ran(get_memory_cell(5),1,8);
                                damage_char(w,r1,4);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                                        
                        beginstate 12;
                                set_char_status(w,0,get_memory_cell(5),1,1);
                                put_effect_on_char(w,13,8,2);
                                run_animation_sound(-17);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                        beginstate 13;
                                set_party_status(0,get_memory_cell(5),1);
                                put_effect_on_char(w,13,8,2);
                                run_animation_sound(-17);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                        beginstate 14;
                                put_boom_on_space(my_loc_x(),my_loc_y(),0,0);
                                run_animation_sound(-51);
                                r1 = get_ran(get_memory_cell(5),1,8);
                                damage_nearby(r1,4,4,2);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                                        
                        beginstate 15;
                                set_char_status(w,11,get_memory_cell(5),1,1);
                                play_sound(-17);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                        beginstate 16;
                                play_sound(-57);
                                print_str_color("There was no trap after all!",2);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                                        
                        beginstate 17;
                                change_char_xp(w,-1 * get_memory_cell(5));
                                play_sound(-94);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                        beginstate 18;
                                make_town_hostile();
                                play_sound(-18);
                                print_str_color("That might have been a bad idea!",1);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                                        
                        beginstate 19;
                                put_boom_on_space(my_loc_x(),my_loc_y(),0,0);
                                run_animation_sound(-51);
                                r1 = get_ran(get_memory_cell(5),1,8);
                                damage_char(w,r1,1);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                                        
                        beginstate 20;
                                set_char_status(w,13,get_memory_cell(5),1,1);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                                        
                        beginstate 21;
                                set_char_status(w,0,get_memory_cell(5),1,1);
                                put_effect_on_char(w,12,8,2);
                                run_animation_sound(-66);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                        beginstate 22;
                                set_party_status(7,get_memory_cell(5),1);
                                put_effect_on_char(w,12,8,2);
                                run_animation_sound(-66);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                        beginstate 23;
                                put_boom_on_space(my_loc_x(),my_loc_y(),0,0);
                                run_animation_sound(-51);
                                r1 = get_ran(get_memory_cell(5),1,8);
                                damage_nearby(r1,4,1,2);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                        beginstate 24;
                                put_boom_on_space(my_loc_x(),my_loc_y(),2,0);
                                run_animation_sound(-10);
                                place_monster(my_loc_x(),my_loc_y(),get_memory_cell(5),1);
                                if (get_memory_cell(7) > 9)
                                        run_town_script(get_memory_cell(7));
                        break;                
                                        
                        beginstate 40;
                                play_sound(-136);
                                print_str_color("You have disarmed the trap.",2);
                                i_am_active = 0;
                                if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) 
                                set_flag(get_memory_cell(2),get_memory_cell(3),1);
                                if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) 
                                        award_party_xp(BASE_TRAP_XP,1 + 2 * get_mechanism_difficulty());
                                if (get_memory_cell(6) > 9)
                                        run_town_script(get_memory_cell(6));
                        break;