// Town 44 Legare v1.0
// t44legare.txt
// by Kelandon (tomwatts@berkeley.edu), based on basicnpc by Jeff Vogel
//
// Like Status Nearby (Creature), but has a few extra abilities. Also is more
// specific.
//
// 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 - Coordinates of the SDF that causes fun effects.
//	 Cell 3 - How many turns between causing status. If 0, causes status every
//		turn.
//   Cell 4 - How much of the status.
//   Cell 5 - Radius of status effect.

begincreaturescript;

variables;

short i,target,last_abil_time,blast1 = 0,blast2 = 0;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	
	last_abil_time = get_current_tick();
break;

beginstate DEAD_STATE;
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);
			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) && (get_memory_cell(0) > 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 (target_ok() == FALSE)
		set_state(START_STATE);
	
	i = 0;
	
	// Abilities.
	if (tick_difference(last_abil_time,get_current_tick()) > get_memory_cell(3))
		i = 4; // divine aid those nearby
	if ((get_flag(get_memory_cell(1),get_memory_cell(2)) == 2) && (blast2 == 0))
		i = 3; // big damage
	if ((get_flag(get_memory_cell(1),get_memory_cell(2)) == 1) && (blast1 == 0))
		i = 2; // summons
	if (get_health(my_number()) < (get_max_health(my_number()) * 3) / 4)
		i = 1; // healing
		
	if (i == 1) {
		heal_nearby(get_ran(1,get_max_health(my_number())) / 2,get_max_health(my_number())),6,0);
		set_character_pose(my_number(),1);
		run_animation_sound(28);
		set_character_pose(my_number(),2);
		force_instant_terrain_redraw();
		print_str_color("Legare heals those nearby!",2);
		}
	
	if (i == 2) {
		activate_hidden_group(3);
		set_character_pose(my_number(),1);
		run_animation_sound(24);
		set_character_pose(my_number(),2);
		force_instant_terrain_redraw();
		print_str_color("Legare summons many helpers!",2);
		blast1 = 1;
		}
	
	if (i == 3) {
		put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(8),char_loc_y(8),2);
		put_boom_on_char(8,6,0);
		set_character_pose(my_number(),1);
		damage_char(8,get_ran(1,get_max_health(8) / 4,get_max_health(8) / 3),4);
		run_animation_sound(54);
		set_character_pose(my_number(),2);
		force_instant_terrain_redraw();
		print_str_color("Legare blasts the enemy chief!",2);
		message_dialog("Legare's massive lightning bolt tears through the slith chief's impenetrable shield! Now the chief is as vulnerable as anybody else!","You sense that if you can only slay this chief, you will be able to finish this combat.");
		blast2 = 1;
		set_terrain(1,7,0);
		set_terrain(2,7,0);
		set_terrain(3,7,0);
		set_terrain(1,3,0);
		set_terrain(2,3,0);
		set_terrain(3,3,0);
		set_char_status(8,4,0 - get_char_status(8,4),1,0);
		}

	if (i == 4) {
		status_nearby(get_memory_cell(4),get_memory_cell(5),16,0,40);
		set_character_pose(my_number(),1);
		run_animation_sound(24);
		set_character_pose(my_number(),2);
		force_instant_terrain_redraw();
		print_str_color("Legare invokes divine aid!",2);
		last_abil_time = get_current_tick();
		}
		
	do_attack();
break;

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