// 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.

// 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 - fire explosion, 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)
//     1 - fire explosion, damages everyone within 4 spaces, does 1-8 damage 
//         times the trap's severity
//	   2 - 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.)
//     3 - unblockable damage explosion, damages everyone within 4 spaces, does 
//         1-8 damage times the trap's severity
//     4 - 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.

beginterrainscript; 

variables;
	short i_am_active = 0;
	
	short choice,r1;
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();
			}

		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_highest_skill(15) < get_mechanism_difficulty()) && ((get_memory_cell(1) == 0) || (has_special_item(get_memory_cell(1)) == 0))) {
			print_str_color("The trap goes off!",2);
			
			if (get_memory_cell(4) == 0) {
				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(char_who_activated_script(),r1,1);
				}
			if (get_memory_cell(4) == 1) {
				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(4) == 2) {
				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(char_who_activated_script(),r1,4);
				}
			if (get_memory_cell(4) == 3) {
				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(4) == 4) {
				put_boom_on_space(my_loc_x(),my_loc_y(),2,0);
				run_animation_sound(10);
				spawn_creature(get_memory_cell(5));
				}
			}
			else {
				play_sound(-136);
				print_str_color("You have disarmed the trap.",2);
				if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) 
					award_party_xp(BASE_TRAP_XP,1 + 2 * get_mechanism_difficulty());
				}
		}
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();
			}

		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_highest_skill(15) < get_mechanism_difficulty()) && ((get_memory_cell(1) == 0) || (has_special_item(get_memory_cell(1)) == 0))) {
			print_str_color("The trap goes off!",2);
			
			if (get_memory_cell(4) == 0) {
				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(char_who_activated_script(),r1,1);
				}
			if (get_memory_cell(4) == 1) {
				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(4) == 2) {
				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(char_who_activated_script(),r1,4);
				}
			if (get_memory_cell(4) == 3) {
				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(4) == 4) {
				put_boom_on_space(my_loc_x(),my_loc_y(),2,0);
				run_animation_sound(10);
				spawn_creature(get_memory_cell(5));
				}
			}
			else {
				play_sound(-136);
				print_str_color("You have disarmed the trap.",2);
				if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) 
					award_party_xp(BASE_TRAP_XP,1 + 2 * get_mechanism_difficulty());
				}
		}
break;
