// Multiple Heal and Summon v1.0
// multhealsum.txt
// by Kelandon (tomwatts@berkeley.edu), based on basicnpc by Jeff Vogel
//
// Like Exodus's BasicNPC, but has multiple heals and summons. Doesn't talk.
//
// The idea is that, when damaged, this creature activates a hidden group of
// allies and may heal itself partway.
//
// In other words, if cells 3 and 4 are set to 75 and 30, respectively, then,
// when this creature is hurt below 75% of its maximum health, it will activate
// its first hidden group. If cell 6 is set to 80, then it will heal itself to
// 95% of its maximum health (regardless of what its actual health is before
// healing), since 80% of 25 is 20. This script always starts with hidden group
// 1.
//
// Then, when this creature is hurt below 30% of its maximum health, it will
// heal itself to 86% of its maximum health (an 80% heal from 30% health) and
// summon its second set of creatures, hidden group 2.
//
// If cell 5 is 0, then it stops here.
//
// 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 dead. If left at 0, none.
//	 Cell 2 - Distractibility factor. This number minus one times ten is the
//		percent chance of a creature getting distracted from its current target
//		and attacking the most recent thing that hit it. If left at 0, this
//		defaults to 4, which gives a 30% chance. (If set to 1, it has a 0%
//		chance, and if set to 11, it has a 100% chance.)
//	 Cell 3 - Percent of health at which to summon first set of creatures.
//		(I.e., if set to 40, summons at 40% health.)
//	 Cell 4 - Percent of health at which to summon second set of creatures. If
//		0, does not summon a second group.
//	 Cell 5 - Percent of health at which to summon third set of creatures. If
//		0, does not summon a third group.
//	 Cell 6 - Percent by which to heal self when summoning. If 0, does not heal.
//		(I.e., if 50 and if cell 3 is 40, this creature will heal itself to 70%
//		of its maximum health when it does its first summon, since it is at or
//		below 40% of its maximum health, which means that it is at least 60% 
//		damaged, and healing 50% of that will leave it at 70%.
//		NOTE: Creature will heal itself to the appropriate level no matter how
//		damaged it is. In the previous example, even if the creature had been
//		damaged to 10% of its maximum health before it could summon, it would
//		heal itself to 70% again when summoning.
//	 Cell 7 - Message to display when summoning. If 0, none.
//		1 - Sss-Khsss in the Black Land.
//		2 - Dalaghant in the Vahnatai Lab.
//		3 - Keeper in Third Level
//	 Cell 8 - First creature number in first group. This is used for animation
//		purposes, but for the animation to work, the groups need to have
//		consecutively numbered creatures in equally numbered groups (i.e. group
//		1 is 21-3, group 2 is 24-6, and group 3 is 27-9). If 0, no animation
//		appears on the creatures; instead this creature gets an animation.
//	 Cell 9 - How many creatures in each group. If cell 8 is 0, this is ignored.

begincreaturescript;

variables;

short i,j,k,distract,summon,choice;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	
	summon = 0;
	
	distract = get_memory_cell(2);
	if (distract == 0)
		distract = 4;
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);
	
	// Distractibility.
	if (who_hit_me() >= 0)
		if (get_ran(1,1,10) < distract)
			set_target(my_number(),who_hit_me());
	
	// First summon.
	if (get_health(my_number()) <= (get_max_health(my_number()) / 100) * get_memory_cell(3))
		if (summon == 0) {
			// Message, if necessary.
			if (get_memory_cell(7) == 1)
				message_dialog("Stumbling backwards, the leader of the devil-worshipping sliths hisses violently, _Wrath of the Thkhi upon you!_ He utters words of magic, and his wounds decrease. A few more words, and he has several ghostly accomplices.","");
			if (get_memory_cell(7) == 2) {
				play_sound(48);
				message_dialog("Dalaghant, warm now to the battle, roars terribly, turning the heads of all creatures in the Sacrifice Fields. They had been wandering mindlessly, paying no attention to your combat, but now they turn angrily to you.","He also casts a mild healing spell and summons two more undead vahnatai to his side.");
				make_town_hostile();
				set_attitude(6,10);
				set_attitude(7,10);
				set_attitude(8,10);
				set_attitude(9,10);
				set_attitude(10,10);
				set_attitude(11,10);
				set_attitude(12,10);
				set_attitude(13,10);
				set_attitude(14,10);
				set_attitude(21,10);
				set_attitude(22,10);
				set_attitude(24,10);
				}
			if (get_memory_cell(7) == 3) {
				play_sound(111);
				message_dialog("The vahnatai keeper shrieks in anger, waving his arms in mystic circles and chanting a spidery language. Two demons suddenly appear on the flashing runes!","");
				}
				
			// Do the summon.
			activate_hidden_group(1);
			
			// Healing; i is the amount to heal by.
			if (get_memory_cell(6) > 0) {
				i = (((get_max_health(my_number()) / 100) * (100 - get_memory_cell(3))) / 100) * get_memory_cell(6);
				change_char_health(my_number(),i);
				}
				
			// Animation.
			if (get_memory_cell(8) > 0) {
				j = get_memory_cell(8);
				while (j <= get_memory_cell(8) + (get_memory_cell(9) - 1)) {
					put_boom_on_char(j,2,0);
					j = j + 1;
					}
				set_character_pose(my_number(),1);
				}
			else
				put_boom_on_char(my_number(),3,0);
			run_animation_sound(10);
			
			// Finish.
			set_character_pose(my_number(),2);
			summon = 1;
			}
			
	// Second summon.
	if (get_memory_cell(4) > 0)
		if (get_health(my_number()) <= (get_max_health(my_number()) / 100) * get_memory_cell(4))
			if (summon == 1) {
				// Message, if necessary.
				if (get_memory_cell(7) == 1)
					message_dialog("Blood covering his scaly arms, the slith chief withdraws again for a moment. He speaks a few more words of magic, summoning additional translucent combatants and healing himself.","_Fear the power of Sss-Khsss!_ he growls.");
				if (get_memory_cell(7) == 2) {
					reset_dialog();
					add_dialog_str(0,"Reeling from your unexpectedly strong blows, the demon king shouts, enraged, _I offered you the chance to serve the true God, to live his servant forever, to worship alongside me, yet your spurn my hospitality!_",0);
					add_dialog_str(1,"He growls. _If not to join me, why have you come into my home?_",0);
					add_dialog_str(2,"What do you say?",0);
					if (party_size() == 1) {
						add_dialog_choice(0,"I'm looking for a way out of this hellish Lava Ocean.");
						add_dialog_choice(1,"Why should I tell you, creature from the abyss?");
						}
					else {
						add_dialog_choice(0,"We're looking for a way out of this hellish Lava Ocean.");
						add_dialog_choice(1,"Why should we tell you, creature from the abyss?");
						}
					add_dialog_choice(2,"Just exploring. You know, the stuff that adventurers normally do.");
					choice = run_dialog(1);
					if (choice == 1)
						message_dialog("_A way out?_ roars the demon king. _Yes, there is a way through here, but I will make sure that you never use it!_ He utters words of magic, healing himself, and a few more to summon several fiery abominations.","");
					if (choice == 2)
						message_dialog("_You do not need to!_ roars the demon king. _I already know. I have watched you from the first. You seek a passage out of the Lava Ocean._","He continues, _There is such a way through here, but I will make sure that you never use it!_ He utters words of magic, healing himself, and a few more to summon several fiery abominations.");
					if (choice == 3)
						message_dialog("_You lie!_ roars the demon king. _I already know. I have watched you from the first. You seek a passage out of the Lava Ocean._","He continues, _There is such a way through here, but I will make sure that you never use it!_ He utters words of magic, healing himself, and a few more to summon two fiery abominations.");
					}
			if (get_memory_cell(7) == 3) 
				message_dialog("The vahnatai keeper staggers backwards, significantly wounded now. He breathes out some smoke suddenly, and two more monsters appear. His wounds also shrink.","");
				
				// Do the summon.
				activate_hidden_group(2);
				
				// Healing; i is the amount to heal by.
				if (get_memory_cell(6) > 0) {
					i = (((get_max_health(my_number()) / 100) * (100 - get_memory_cell(4))) / 100) * get_memory_cell(6);
					change_char_health(my_number(),i);
					}
					
				// Animation.
				if (get_memory_cell(8) > 0) {
					j = get_memory_cell(8) + get_memory_cell(9);
					while (j <= get_memory_cell(8) + (get_memory_cell(9) - 1) + get_memory_cell(9)) {
						put_boom_on_char(j,2,0);
						j = j + 1;
						}
					set_character_pose(my_number(),1);
					}
				else
					put_boom_on_char(my_number(),3,0);
				run_animation_sound(10);
				
				// Finish.
				set_character_pose(my_number(),2);
				summon = 2;
				}
	
	// Third summon.
	if (get_memory_cell(5) > 0)
		if (get_health(my_number()) <= (get_max_health(my_number()) / 100) * get_memory_cell(5))
			if (summon == 2) {
				// Message, if necessary.
				if (get_memory_cell(7) == 1)
					message_dialog("Bleeding badly now, the leader of these barbaric sliths hisses, _Footsoldiers of the ethereal realms, I summon your aid!_ He whispers words of magic, and frothing demons appear while he heals himself.","");
				
				// Do the summon.
				activate_hidden_group(3);
				
				// Healing; i is the amount to heal by.
				if (get_memory_cell(6) > 0) {
					i = (((get_max_health(my_number()) / 100) * (100 - get_memory_cell(5))) / 100) * get_memory_cell(6);
					change_char_health(my_number(),i);
					}
					
				// Animation.
				if (get_memory_cell(8) > 0) {
					j = get_memory_cell(8) + (2 * get_memory_cell(9));
					while (j <= get_memory_cell(8) + (get_memory_cell(9) - 1) + (2 * get_memory_cell(9))) {
						put_boom_on_char(j,2,0);
						j = j + 1;
						}
					set_character_pose(my_number(),1);
					}
				else
					put_boom_on_char(my_number(),3,0);
				run_animation_sound(10);
				
				// Finish.
				set_character_pose(my_number(),2);
				summon = 3;
				}
	
	do_attack();
break;

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