// t4him.txt
// version 1.0.4
// by Thralni

//<from original basicnpc>
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// 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 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4 - Amount of space between party and creature for automatic talk.
//     Set to 0 to turn this feature off

begincreaturescript;

variables;

short i,target,trgt,ability,mental_dmg,mage_taunt,char,done_attack,attack_tick;
short memory,cake; // machinery
short torture,cloud,drain,chain,blow,healthdrop; // spell and health penalty for HIM, dependant on memory and cake variables
short x,y;

body;

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

beginstate DEAD_STATE;
	message_dialog("In a rupture of anger, HIM does what we call dying, except he is mostly machine and therefor doesn't really live, meaning he is shut down. For good. They made it at HMC with the purpose of being a useful, all controlling being. They failed hideously.","Suddenly, we feel shaking earth, now that the being that all these years held this crumbling halls together has vanished, the once magnificent halls fall apart. We must get out of here before we are buried under the solid marble of this place!");

	play_sound(102);

	set_character_pose(ME,11);
	force_instant_terrain_redraw();
	pause(1);

	set_character_pose(ME,12);
	force_instant_terrain_redraw();
	pause(1);

	set_character_pose(ME,13);
	force_instant_terrain_redraw();
	pause(1);

	set_character_pose(ME,14);
	force_instant_terrain_redraw();
	pause(1);

	// Set the appropriate stuff done flag for this character being dead
	set_flag(4,1,1);
	set_flag(4,14,2); // open gates
	play_sound(99);
	flip_terrain(23,32);
	flip_terrain(24,32);
	set_terrain(my_loc_x(),my_loc_y(),422);

		while (x < 34 && y < 44) { // place antimagic fields. NOTE: x and y are one to big, since otherwise the fields are not placed on all needed spots!
			put_field_on_space(x,y,-1);

			x = x +1;
			if (x > 33) {
				x = 22;
				y = y + 1;
				}
			}

	award_party_xp(200,35);

	erase_char(ME);
break;

beginstate START_STATE;
	if (attack_tick == get_current_tick()) {
			end_combat_turn();
			}

	done_attack = 0; // reset attack

	// change health
	cake = get_flag(250,1);

	if (cake == 1) {
			healthdrop = 25;

			change_char_health(ME,(healthdrop - (2 * healthdrop)));
			print_big_str("HIM loses ",healthdrop," health points");
			}

	if (cake == 2) {
			healthdrop = 50;

			change_char_health(ME,(healthdrop - (2 * healthdrop)));
			print_big_str("HIM loses ",healthdrop," health points");
			}

	// check amount of machines destroyed
	memory = 0;
	torture = 0;
	blow = 0;
	chain = 0;

	while(memory <= get_flag(250,0)) {
			torture = torture + 5;
			blow = blow + 5;
			chain = chain + 5;

			memory = memory + 1;
			}
	
	// reduce mage_taunt every new turn
	mage_taunt = get_flag(250,2);
	if (mage_taunt > 0) {
			mage_taunt = mage_taunt - 1;
			set_flag(250,2,mage_taunt);
			}

	// If party comes close, start talking (memory cell 4 is not 0)
	if (get_flag(4,7) == 0) {
			if (get_memory_cell(4) > 0) {
					if (dist_to_party() == get_memory_cell(4)) {
							set_flag(4,7,1);
							begin_talk_mode(get_memory_cell(3));
							}
					}
			}

	// 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)) {
		set_state(3);
	}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		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 (get_health(ME) < 750) { // I'm running low on health, do the special ability!
			if (get_flag(4,8) == 0) {
					message_dialog("HIM looks genuinely worn out, but then again, it always looked worn out. Still, it manages to look even worse now.","He stares at us and trembles slightly. If it's of fear or anger you don't know, but suddenly a headache takes hold of us that forces us to the ground. It disappears soon, but it didn't make us feel any better then before.");
					set_flag(4,8,1);
					}

			// Mental torture - does great amount of mental damage
			print_str_color("HIM uses Mental Torture.",3);
			trgt = 0;
			while (trgt < 4) {
					put_effect_on_char(trgt,1,5,2);
					run_animation_sound(167);
					mental_dmg = get_ran(1,(40 - torture),(80 - torture));
					damage_char(trgt, mental_dmg,3);
					change_char_health(my_number(),-25);

					trgt = trgt + 1;
					}
			}
			
			// Mind cloud - does mental damage to mages
			if (char_with_highest_skill(11) == 0 || char_with_highest_skill(11) == 1 || char_with_highest_skill(11) == 2 || char_with_highest_skill(11) == 3) {
					if (mage_taunt == 0 && get_char_status(char_with_highest_skill(11),13) == 0 && done_attack == 0) { // are there any mages around? If so, get them! Just check if I'm ready or if it's necessary or if I haven't done any attack already
							trgt = char_with_highest_skill(11);
							if (can_see_char(trgt)) {
									print_str_color("HIM uses Mind Cloud.",3);

									put_sparkles_on_char(trgt,9,5);
									run_animation_sound(25);
									set_char_status(trgt,13,5,0,1); // dumbfound target
									set_char_status(trgt,2,-5,0,1); // weaken target
									mage_taunt = 2;
									set_flag(250,2,mage_taunt);
									deduct_ap(my_ap());

									done_attack = 1; // Don't attack again after doing this spell
									}
							}
					}

// SOME EXTRA TARGETING

				trgt = get_target(); // target is get_target, except when a character is too close: in that case, make that character the target trgt

				char = 0;

				while(char < 3 && party_size() > 1) {
				if (char_dist_to_loc(char,char_loc_x(ME),char_loc_y(ME)) < 2) {
						trgt = char;
						if (done_attack == 0) {
								attack_tick = get_current_tick();
								set_state(4);
								}
						}
				char = char + 1;
				}

				if (done_attack == 0 && can_see_char(trgt) == 1) { // no attack done? go to the spell casting state, but only if the selected target can be seen
						attack_tick = get_current_tick();
						set_state(4);
						}

				if (done_attack == 0 && can_see_char(trgt) == 0) { // no attack done? If the selected target can't be seen, select the best target available and go to the spell casting state
							if (select_target(ME,8,0)) {
									set_state(3);
									}
						set_state(4);
						}
		attack_tick = get_current_tick();
		set_state(START_STATE);
break;

beginstate 4; // What spell should be used?
	ability = get_ran(1,0,2); // give me a random number, 0 or 1 is use ability, 2 is nothing: 33% chance of doing something else
	if (ability == 0)
			set_state(5);
	if (ability == 1)
			set_state(6);
	if (ability == 2)
			set_state(7);
break;

// *** Now come the SPELLS ***

beginstate 5; // Mental blow - gives the target a huge headache
	print_str_color("HIM uses Mental Blow.",3);

	put_boom_on_char(trgt,5,0);
	run_animation_sound(46);
	mental_dmg = get_ran(1,(60 - blow),(120 - blow));
	damage_char(trgt, mental_dmg,3);
	done_attack = 1; // Don't attack again after doing this spell

	set_state(START_STATE);
break;

beginstate 6; // Brain Drain - badly affect target's mind
	print_str_color("HIM uses Brain Drain.",3);

	put_sparkles_on_char(trgt,9,5);
	run_animation_sound(65);
	set_char_status(trgt,8,1,0,1); // charm target
	set_char_status(trgt,3,-6,0,1); // slow target
	set_char_status(trgt,2,-6,0,1); // weaken target
	done_attack = 1; // Don't attack again after doing this spell

	set_state(START_STATE);
break;

beginstate 7; // Soul Chain - does both mental damage and paralyzes the target
	print_str_color("HIM uses Soul Chain",3);

	put_effect_on_char(trgt,4,5,0);
	run_animation_sound(54);
	mental_dmg = get_ran(1,(50 - chain),(60 - chain));
	damage_char(trgt, mental_dmg,3);
	set_char_status(trgt,11,1,0,1); // paralyze target
	done_attack = 1; // Don't attack again after doing this spell

	set_state(START_STATE);
break;

// *** end of special spells ***

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0 || get_flag(4,7) == 1) {
		print_str("HIM doesn't respond.");
		end();
	}
	begin_talk_mode(get_memory_cell(3));
break;