// Healer v1.0.0
// healer.txt
// by Kelandon (tomwatts@berkeley.edu)
//
// Much like basicnpc, but has a small adjustment for combat. It will heal
// nearby allies while fighting.
//
// To save memory cells, the script can call a town state when the creature dies
// (much like my Special Death script).
//
// 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 - Which town state to call when the creature dies. If left at 0,
//		none.
//	Cell 2 - Dialogue node to start with if talked to. if left at 0, this
//		character doesn't talk.
//	Cell 3 - Average damage of allies before character begins to heal them. That
//		is, if this cell is set to 10, and if the character has three allies,
//		and if they are damaged 9, 13, and 11 points from their maximum health,
//		then they average 11 points of damage, so the character would heal them.
//		If they were damaged 8, 7, and 16, the character would not, because
//		they'd average 31/3, and BoA rounds down, so that would be 10.
//		Defaults to 20 if left at 0.
//	Cell 4 - How often the character will heal if allies are damaged. This is
//		the denominator of a fraction, so if this is 3, then the char will heal
//		1/3 of the time that the allies are damaged. If left at 0, defaults to
//		1.
//	Cell 5 - Minimum of damage to heal. See cell 6.
//	Cell 6 - Maximum of damage to heal. When healing, the character will heal a
//		random amount of damage between cell 5 and cell 6. If cell 6 is left at
//		0, then these two cells will be ignored: the character will heal the 
//		average damage on the allies.

begincreaturescript;

variables;

short i,j,k,avg_damage,target,last_abil_time;
short max_to_heal,min_to_heal,good_evil;

body;

beginstate INIT_STATE;
	if (get_memory_cell(3) == 0)
		set_memory_cell(3,20);
		
	if (get_memory_cell(4) == 0)
		set_memory_cell(4,1);

	if (get_memory_cell(6) > 0)
		{max_to_heal = get_memory_cell(6);
		min_to_heal = get_memory_cell(5);
		}

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

beginstate DEAD_STATE;
	// Call a town state.
	if (get_memory_cell(1) > 0)
		run_town_script(get_memory_cell(1));
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);
	
if (tick_difference(last_abil_time,get_current_tick()) > 0)
	{i = 6; // Check if allies are hurt. First, find total damage to all allies.
	while (i < 120)
		{if (char_ok(i))
			{if (can_see_loc(char_loc_x(i),char_loc_y(i)))
				{if (char_attitude_to_char(my_number(),i) == 0)
					{j = j + (get_max_health(i) - get_health(i));
					k = k + 1;
					}
				}
			}
		i = i + 1;
		}
	
	// Then divide by the number of allies. BoA will round down.
	avg_damage = j / k;
	
	// If damage to heal is not preset, heal the average damage to the allies.
	if (get_memory_cell(6) == 0)
		{min_to_heal = avg_damage;
		max_to_heal = avg_damage;
		}
	
	if (get_attitude(my_number()) < 10)
		good_evil = 0;
	else
		good_evil = 1;
	
	// Do healing.
	if ((avg_damage > get_memory_cell(3)) && (get_ran(1,1,get_memory_cell(4) == 1)))
		{print_named_str(ME,"heals allies!");
		play_sound(28);
		heal_nearby(get_ran(1,min_to_heal,max_to_heal),8,good_evil);
		last_abil_time = get_current_tick();
		deduct_ap(4);
		}
	}
		
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;