// Bug Zapper v1.0.0
// bugzapper.txt
// by Kelandon (tomwatts@berkeley.edu)
// This really ought to be a terrain script, but I needed the targeting calls,
// which require this to be a creature script. Give this script to an invisible
// monster on a blocked space, ideally.
//
// The idea is that this space will zap hostile monsters, doing some damage. The 
// text is set to an altar zapping an insect, which you can modify if 
// necessary. It also only zaps while the party is present.
//
// Memory Cells:
//	Cell 0 - What color zap when zapping. Color possibilities:
//		0 - fire ray
//		1 - ice ray
//		2 - death ray
//		3 - green ray
//		4 - white ray
//		5 - black ray
//		6 - blue ray
//	Cell 1 - What sound to run when zapping. If left at 0, this defaults to 
//		sound 102 (a large magic zap).
//	Cell 2 - How to get targets. If 0, uses a regular select_target. Right now, 
//		there are no other methods, but in theory, you can add as many target 
//		acquisition states as you like. This is an egg; I intended to make this 
//		more useful, but never managed it.
//	Cell 3 - How often the creature reacquires targets. (So if this is 1, the
// 		script gets new targets every turn.) If left at 0, the script only gets
//		new targets when the old target dies.

begincreaturescript;

variables;

short i,target,sound,targeting_state;
short j = 0;

body;

beginstate INIT_STATE;
	set_mobility(ME,0);
	
	if (get_memory_cell(1) > 0)
		sound = get_memory_cell(1);
	else 
		sound = 102;
	if (get_memory_cell(2) == 0)
		targeting_state = 4;
	if (get_memory_cell(3) > 0)
		j = 1;
break;

beginstate DEAD_STATE;
break;

beginstate START_STATE; 
	// If the party is not in sight, end immediately.
	if (can_see_char(1000) == 0)
		end_combat_turn();
	
	// Look for a target; if found, send into combat
	if (select_target(ME,12,0) == 0)
		{end_combat_turn();
		end();
		}
	
	set_state(3);
break;

beginstate 3; // zapping
	// If the party is not in sight, end immediately.
	if (can_see_char(1000) == 0)
		{end_combat_turn();
		end();
		}

if ((target_ok() == 0) || (j > get_memory_cell(3)))
	set_state(targeting_state);

	put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(get_target()),char_loc_y(get_target()),get_memory_cell(0));
	put_boom_on_char(get_target(),6,0);
	damage_char(get_target(),25,3);
	print_str("The altar zaps an insect!");
	run_animation_sound(sound);
	if (get_memory_cell(3) > 0)
		j = j + 1;
	end_combat_turn();
break;

beginstate 4; // target acquisition
	if (get_memory_cell(3) > 0)
		j = 1;
	if (select_target(ME,12,0))
		set_state(3);
	else
		set_state(START_STATE);
break;

beginstate TALKING_STATE;
break;