// 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,target;
short h;

body;

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

beginstate DEAD_STATE;
	inc_flag(2,8,1);
	if (get_flag(2,8) >= 6) {
		reset_dialog();
		add_dialog_str(1,"You flay this Hunan cur to within an inch of his life. He is whimpering and bleeding from many wounds. You decide that, as he appears to be the last, you might as well interrogate him.",0);
		add_dialog_str(2,"The Hunan speak a variant of the language that the Megiddo people speak. It turns out that communication barriers between your two peoples are mostly cultural; with enough kicking and threats, the Hunan breaks down and starts speaking.",0);
		add_dialog_str(3,"_No killing!_ he screams. _We are right!_",0);
		add_dialog_choice(0,"No killing? Of course not! We just want to torture you. Let's start with why you're here.");
		add_dialog_choice(1,"Squeal, you rat!");
		i = 0;
		i = run_dialog(1);
	
		reset_dialog();
		add_dialog_str(1,"The little savage is crying. _We invade because you invade! The chief says you Meggid will come, with big arrows and big spears. So we come first, to stop you._ He whimpers. _We are right._",0);
		add_dialog_str(2,"You prod a little more, asking about this threatened invasion and the Hunan chief. The Hunan have trouble talking coherently in the best of times, and these circumstances do no good.",0);
		add_dialog_str(3,"_Lu Liath!_ the Hunan howls. You almost feel pity; he must be having trouble speaking, what with all the blood that's gushed out of his lungs. _Lu Liath, the biggest chief, he says your shaman has warned that you will hurt us!_",0);
		add_dialog_choice(0,"Our shaman? We don't have shaman.");
		add_dialog_choice(1,"So your pathetic plan was to invade Megiddo, with just you seven fools?");
		i = 0;
		h = run_dialog(1);
		
		reset_dialog();
		if (h == 1) {
			reset_dialog();
			add_dialog_str(1,"The creature just coughs and moans.",0);
			add_dialog_choice(0,"So your pathetic plan was to invade Megiddo, with just you seven fools?");
			i = run_dialog(1);
			h = 2;
			set_flag(2,10,250);
			}
		if (h == 2) {
			reset_dialog();
			add_dialog_str(1,"_Lu Liath, he will bring more!_ the raider cries out. _We are lookers; we look at how to get to your home. Then we will come, all of us._ He is shaking uncontrollably now. He'll probably die soon.",0);
			add_dialog_choice(0,"We should warn the castle of this coming attack. Maybe we'll get a big reward!");
			i = run_dialog(1);
			}
		
		reset_dialog();
		add_dialog_str(1,"The Hunan nods weakly. _You go. I stay here._ His breathing becomes more rapid, his voice weaker. _I... I stay in the dirt. I rest here..._ His voice trails away. You check; he's not breathing anymore.",0);
		add_dialog_str(2,"It would be best if you returned to Megiddo tower to alert the King that the Hunan are planning an attack. You'll probably get a lot of money for this, and medals all around.",0);
		if (get_flag(0,16) == 250) {
			add_dialog_str(3,"Deadeye walks over to the dead body and gives it a brutal kick. _Good riddance,_ he mutters. _I don't know about you, but I'm heading back to the castle right now. We must protect Megiddo._ He hurries into the forest.",0);
			remove_char_from_party(100);
			}
		set_flag(0,15,25);
		add_dialog_choice(0,"It's time to go.");
		i = run_dialog(1);
		set_flag(2,8,250);
		end();

		//run_town_script(80);
		//begin_talk_mode(get_memory_cell(3));
		}
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);
	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;