// brigand.txt version 1.0.1
// niemandcw@gmail.com
//Brigands with this script should be initially neutral. They can then fidget
// lkie basicnpc, or be set to decide to take walks to various waypoints 
//around the towns, as though wandering off to chat with buddies or something. 
//When not hostile, brigands will scan for any signs that the party has done 
//something traitorous, such as bodies of slain brigands, or other brigands 
//who are already hostile to the party. If a brigand sees or hears something 
//suspicious, it turns hostile to the party and begins shouting (broadcasting 
// amessage 50) to any other nearby brigands. When a brigand dies, it drops
// a corresponding corpse item, that will tip off any brigands who see it, but can 
//be carried away and hidden by the party. If a brigand catches the party carrying
// a body, it will immediately turn hostile and attack. 
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - Just fidget in the same area.
//     1 - Wander randomly, including sometimes walking to way points
//     2 - Stands still until a target appears.
//     3 - 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 x,y;
short going_somewhere, dest;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 3)
		set_mobility(ME,0);
	dest = -1;
	going_somewhere = 0;
	if (get_memory_cell(0) == 0)
		set_script_mode(1);
	if (get_memory_cell(0) == 1)
		set_script_mode(3);
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);
	dest = 449;
	if(creature_type(ME) == 233)
		dest = 445;
	else if(creature_type(ME) == 234)
		dest = 446;
	else if(creature_type(ME) == 235)
		dest = 449;
	else if(creature_type(ME) == 236)
		dest = 447;
	else if(creature_type(ME) == 237)
		dest = 448;
	drop_item(dest);
		
break;

beginstate START_STATE; 
	if(get_attitude(ME) != 10){
		i = my_current_message(); //listen for alarms
		if(i==50){
			set_attitude(ME,10);
			stop_moving(ME);
			if(can_see_char(1000))
				print_str_color("The brigand hears the noise and rushes to help his comrade.",3);
		}
		//look for bodies nearby
		x = my_loc_x() - 7;
		while(x < (my_loc_x() + 8)){
			y = my_loc_y() - 7;
			while(y < (my_loc_y() + 7)){
				if(can_see_loc(x,y) && item_of_class_on_spot(x,y,1)){
					if(can_see_char(1000)){
						print_str_color("The brigand sees the body on the floor, looks at",3);
						print_str_color(" you, then draws his weapon and attacks.",3);
					}
					set_attitude(ME,10);
					stop_moving(ME);
					x = 1000; //break out of the loop
				}
				y = y + 1;
			}
			x = x + 1;
		}
		//make sure the party isn't carying any bodies
		if(can_see_char(1000) && has_item_of_class(1,0)){
			print_str_color("The brigand sees the body you're carrying and charges",3);
			print_str_color(" at you with a shout.",3);
			set_attitude(ME,10);
			stop_moving(ME);
		}
	}
	if(get_attitude(ME) == 10){
		broadcast_char_message(10,50,0);
		if(what_group_in(ME) > -1)
			set_attitude(what_group_in(ME) + 1000,10);
		// 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. 
	if((get_memory_cell(0)==1) && (going_somewhere==0) && (get_ran(1,0,100) < 5)){ //maybe decide to take a walk
		dest = get_ran(1,0,6);
		going_somewhere = 1;
	}
	if(going_somewhere==1){ //continue walking
		if(approach_waypoint(ME,dest,2))
			going_somewhere = 0;
		if(get_ran(1,0,100) < 10) //maybe stop where I am
			going_somewhere = 0;
	}
	else if (get_memory_cell(0) < 2) { //otherwise just hang around
		if(get_memory_cell(0)==0){
			if (my_dist_from_start() >= 6 && get_ran(1,1,100) < 40) 
					return_to_start(ME,1);
			else
				fidget(ME,25);
		}
		else{
			if (my_dist_from_start() >= 8 && get_ran(1,1,100) < 8) 
					return_to_start(ME,1);
			else
				fidget(ME,10);
		}
	}

	// 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
	broadcast_char_message(10,50,0);
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

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