// View plain text
//chaos.txt
//
//When activated this script sets off random explosions around the party
//as if the whole town was blowing up or something. The explosions will
//destroy the terrain that was hit, unless it is in the range 38-45 or 
//is type 1. This way you can put in walls that will not be destroyed. 
//It then damages any character on that location.
//The active mode can also be triggered by sending the script a message value 111 

// Memory Cells 
//   0,1 - The coordinates of a flag to control the script. The script will 
//		set off explosions only when the flag is 1. If the script is
//		activated by a message, it will set this flag to 1.
//   2 - The type of damage to do; defaults to 1, fire damage. 
//   3 - The amount of damage to do to hostile characters; defaults to 10.
//	***Set this value to 138 if you want no damage to be done***
//   4 - The amount of damage to do to friedly characters; defaults to 10.
//	***Set this value to 138 if you want no damage to be done***

beginterrainscript; 

variables;
	short t, target, x, y, n;
body;

beginstate INIT_STATE;
	set_script_mode(3);
	if(get_memory_cell(2) == 0)
		set_memory_cell(2,1);
	
break;

beginstate START_STATE;
	if((my_current_message() == 111) || (get_flag(get_memory_cell(0),get_memory_cell(1)) == 1)){
		set_flag(get_memory_cell(0),get_memory_cell(1),1);
		set_state(3);
	}

break;

beginstate SEARCH_STATE;
break;

beginstate STEP_INTO_SPOT_STATE;
	set_ter_script_mode(37,3);
	i_am_active = 1;
	set_state(3);
break;

beginstate 3;
	if(get_flag(get_memory_cell(0),get_memory_cell(1)) == 0)
		set_state(START_STATE);
	if((get_ran(1,1,4) == 1) && (n != get_current_tick())){
		target = random_party_member();
		x = (char_loc_x(target) + get_ran(1,1,10)) - 5;
		y = (char_loc_y(target) + get_ran(1,1,10)) - 5;
		if(party_can_see_loc(x,y)){
			put_boom_on_space(x,y,1,0);
			run_animation_sound(152);
		}
		if((get_terrain(x,y) > 37) && (get_terrain(x,y) < 46) && (get_terrain(x,y) != 1))
			set_terrain(x,y,156);
		if(char_on_loc(x,y) != -1){
			target=char_on_loc(x,y);
			if((get_attitude(target) > 4) && (get_memory_cell(3) != 138))
				damage_char(target,get_memory_cell(3),get_memory_cell(2));
			else if(get_memory_cell(4) != 138)
				damage_char(target,get_memory_cell(4),get_memory_cell(2));
		}
	}
	else if(n == get_current_tick())
		end();
	n = get_current_tick();
break