// basicnpc.txt
// 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.

begincreaturescript;

variables;

short i,sum,trgt,myx,myy,tx,ty,num,who,w;

body;

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

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),1);

	message_dialog("Clutching his bleeding side, the Necromancer falls to his knees, gasps for air rattling through his helmet.","_NO, DO YOU REALIZE WHAT YOU'VE DONE?  Everything I've worked for... ruined!  Lord have... merc...y..._");
	toggle_quest(2,0);
	award_party_xp(500,2);

	i = 6;
	while(i < 119){
		if(char_ok(i) == TRUE)
			kill_char(i,2,0);
		i = i + 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; // summoning
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if(friends_nearby(24) >= 1)
		set_state(4);
	i = 0;
	w = 0;

	myx = my_loc_x();
	myy = my_loc_y();

	set_character_pose(ME,1);
	force_instant_terrain_redraw();

	put_sparkles_on_char(ME,1,7);
	run_animation_sound(-25);

	print_str_color("Necromancer casts Dark Summoning!",4);

	pause(4);

	num = get_ran(1,2,5);

	while(i < num){
		who = get_ran(1,0,99);
		if(who <= 29) //skeleton
			sum = 234;
		if((30 <= who) && (who <= 55)) //zombie
			sum = 235;
		if((56 <= who) && (who <= 71)) //shade
			sum = 240;
		if((72 <= who) && (who <= 82)) //ghoul
			sum = 236;
		if((83 <= who) && (who <= 88)) //greater shade
			sum = 241;
		if((89 <= who) && (who <= 94)) //ghast
			sum = 237;
		if((95 <= who) && (who <= 99)) //wight
			sum = 239;

		w = 0;
		while(w < 100){ //make it possible to give up rather than trying for too long
			tx = get_ran(1,(myx - 2),(myx + 2));
			ty = get_ran(1,(myy - 2),(myy + 2));
			if(can_see_loc(tx,ty) == TRUE){
				if(char_on_loc(tx,ty) < 0){
					if(get_terrain(tx,ty) < 1)
						w = 100 + 1; //break out of the loop
					}
				}
			else
				w = w + 1;
			}

		if(w == 101){ //a valid space was found
			place_monster(tx,ty,sum,1);
			put_boom_on_space(tx,ty,7,0);
			run_animation_sound(75);
			pause(4);
			}
		i = i + 1;
		}

	set_character_pose(ME,0);
	force_instant_terrain_redraw();
	end_combat_turn();
break;

beginstate 4; // spell casting

	if(friends_nearby(24) == 0)
		set_state(3);

	if(get_health(ME) < get_max_health(ME) / 2)
		set_state(5);

	if(who_hit_me() > -1){
		trgt = who_hit_me();
		set_state(6);
		}

	if (target_ok() == FALSE)
		set_state(START_STATE);

	myx = my_loc_x();
	myy = my_loc_y();

	if(get_flag(150,15) > 0){
		trgt = get_flag(150,15);

		if(char_ok(trgt) == TRUE){

			tx = char_loc_x(trgt);
			ty = char_loc_y(trgt);

			i = get_max_health(trgt);

			set_character_pose(ME,1);
			force_instant_terrain_redraw();

			print_str_color("Necromancer casts Corpse Explosion!",3);

			put_jagged_zap(myx,myy,tx,ty,2);
			run_animation_sound(53);

			pause(2);

			put_boom_on_char(trgt,1,0);
			run_animation_sound(-152);
			kill_char(trgt,3,0);

			pause(4);

			damage_near_loc(tx,ty,i,2,0);

			set_character_pose(ME,0);
			force_instant_terrain_redraw();

			end_combat_turn();
			}
		set_flag(150,15,-1);
		}

	do_attack();

		// play_sound(52);

break;

beginstate 5; // heal

	if(friends_nearby(24) == 0)
		set_state(3);

	i = 7;

	while(i < 119){
		if(char_ok(i) == TRUE){
			if((can_see_char(i) == TRUE) && (char_attitude_to_char(ME,i) == 0)){
				trgt = i;
				i = 120;
				}
			}
		i = i + 1;
		}

	if(i == 121){
		print_str_color("Necromancer casts Death Pact!",4);

		tx = char_loc_x(trgt);
		ty = char_loc_y(trgt);

		num = get_ran(5,12,20);

		set_character_pose(ME,1);
		force_instant_terrain_redraw();

		put_sparkles_on_space(tx,ty,2,6);
		put_sparkles_on_char(ME,2,6);
		run_animation_sound(-24);

		change_char_health(ME,num);

		kill_char(trgt,3,0);

		set_character_pose(ME,0);
		force_instant_terrain_redraw();
		}
	end_combat_turn();

	set_state(3);
break;

beginstate 6; //flee, always teleport first and then change state depending on situation or use a curse

	if(enemies_nearby(2) > 0){
		w = 0;
		while(w < 100){ //make it possible to give up rather than trying for too long
			tx = get_ran(1,(myx - 3),(myx + 3));
			ty = get_ran(1,(myy - 3),(myy + 3));
			if(can_see_loc(tx,ty) == TRUE){
				if(char_on_loc(tx,ty) < 0){
					if(get_terrain(tx,ty) < 1)
						w = 100 + 1; //break out of the loop
					}
				}
			else
				w = w + 1;
			}

		if(w == 101){ //a valid space was found
			put_effect_on_space(my_loc_x(),my_loc_y(),10,5,2);
			put_effect_on_space(tx,ty,10,5,2);
			relocate_character(my_number(),tx,ty);
			force_instant_terrain_redraw();
			run_animation_sound(-163);

			deduct_ap(3);
			pause(4);
			}
		}

	if(friends_nearby(24) == 0)
		set_state(3);

	if(get_health(ME) < get_max_health(ME) / 2)
		set_state(5);

	if(char_ok(trgt) == TRUE){
		set_character_pose(ME,1);
		force_instant_terrain_redraw();

		print_str_color("Necromancer casts Decrepify!",2);

		put_effect_on_char(trgt,2,3,0);
		put_sparkles_on_char(trgt,6,6);
		run_animation_sound(52);

		set_char_status(trgt,2,-7,1,0);
		set_char_status(trgt,3,-5,1,0);
		set_char_status(trgt,21,3,1,0);

		set_character_pose(ME,0);
		force_instant_terrain_redraw();
		end_combat_turn();
		}

	set_state(4);

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;