//mobsentinel.txt - Used for Solberg's sentinels. March among 2 nav points and
// back to start.

// Memory Cells:
//   Cell 0 - The class of this sentinel.
//		0 - dull bracelet
//		1 - shiny bracelet
//		2 - gold bracelet
//		3 - always hostile
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//	 Cell 3 - The nav point the character marches out to. Also goes to this
//     nav point + 1. if set to 10 or greater, doesnt move
//   Cell 4,5 - Flag that determines when it moves. if 0, goes out to nav
//     point. If 1, goes to the next nav point. If 2, back to first.
//     If 3, returns to start.
//   Cell 6 - Normally 0, otherwise, marks tat this sentinel is special in some 
//     way:
//		1 - starting section sentinel. gives intro text
//		2 - broken sentinel
//		3 - ne sentinel
//		4 - hunter event sentinel

begincreaturescript;

variables;

short i,target;
short spec_counter = 0;
short last_walk;
short angry = 0;
short r1;
short diff_adjust = 0;
short start_section;
short insp_once = 0;
short level_adj = 0;

body;

beginstate INIT_STATE;
	last_walk = get_current_tick();
	set_strategy(ME,get_strategy(ME) + 10);
	start_section = section_i_am_in();
	if (get_memory_cell(6) == 2)
		set_boss_level(ME,1);

	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);
	inc_flag(11,15,1);
	if ((gf(26,1) == 0) && (get_memory_cell(6) != 2) && (gf(41,6) == 0)) {
		if (gf(11,15) > 15) {
			change_crime_level(-1,1);
			print_str_color("The residents are annoyed that you are destroying their sentinels.",3);
			}
			else if (gf(11,15) > 7) {
				print_str_color("If you keep destroying their sentinels, people will get angry.",3);
				}
		}
break;

beginstate START_STATE; 
	if ((diff_adjust == 0) && (gf(11,15) > 18)) {
		set_boss_level(ME,1);
		change_max_health(ME,100);
		set_attack_bonus(ME,8);
		diff_adjust = 1;
		}
		
	if ((get_attitude(ME) < 10) && (can_see_char(30000)) && (dist_to_party() <= 5)) {
		if (get_memory_cell(0) == 0) {
			if ((has_spec_item(16) == 0) && (has_spec_item(17) == 0) && (has_spec_item(18) == 0)) {
				angry = 1;
				}
				else {
					// first meet dialog
					if (get_memory_cell(6) == 1) {
						if (gf(11,3) == 0) {
							sf(11,3,1);
							begin_talk_mode(2);
							}
						}
					}
			}
		if (get_memory_cell(0) == 1) {
			if ((has_spec_item(17) == 0) && (has_spec_item(18) == 0)) {
				angry = 1;
				}
			}
		if (get_memory_cell(0) == 2) {
			if (has_spec_item(18) == 0) {
				angry = 1;
				}
			}
		if (get_memory_cell(0) == 3) {
			angry = 1;
			}
		insp_once = 1;
		if (angry) {
			if (what_group_in(ME) >= 0) {
				set_attitude(30000 + what_group_in(ME),10);
				alert_char(30000 + what_group_in(ME));
				}
				else set_attitude(ME,10);
			
			print_str_color("A sentinel senses that you don't have an appropriate bracelet.",2);
			print_str_color("  It turns hostile!",2);
			if (get_memory_cell(6) == 1) {
				if (gf(11,2) == 0) {
					sf(11,2,1);
					begin_talk_mode(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_foe_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (get_foe_target(ME,12,0)) {
		if (dist_to_party() > 8)
			approach_char(ME,30000,7);
			else {
				do_attack();
				set_state(3);
				}
		}
		
	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	// se sentinel event
	if (get_memory_cell(6) == 4) {
		if (gf(41,6) > 0) {
			if (level_adj == 0) {
				level_adj = 1;
				set_level(ME,20);
				}	
			if (get_attitude(ME) >= 10) {
				set_foe_target(ME,random_group_member(0));
				set_state(3);
				}
			}
		}
		
		
	if (get_memory_cell(3) < 10) {
		if (gf(get_memory_cell(4),get_memory_cell(5)) == 0) {
			if (dist_to_nav_point(ME,get_memory_cell(3)) > 4)
				approach_nav_point(ME,get_memory_cell(3),2);
			}
		else if (gf(get_memory_cell(4),get_memory_cell(5)) == 1) {
			if (dist_to_nav_point(ME,get_memory_cell(3) + 1) > 4)
				approach_nav_point(ME,get_memory_cell(3) + 1,2);
			}
		else if (gf(get_memory_cell(4),get_memory_cell(5)) == 2) {
			if (dist_to_nav_point(ME,get_memory_cell(3)) > 4)
				approach_nav_point(ME,get_memory_cell(3),2);
			}
			else {
				return_to_start(ME,2);
				}
		}
	
	if (get_memory_cell(6) == 2) { // bahroken
		if ((dist_to_nav_point(ME,7) <= 2) && (can_see_char(30000)) && (gf(26,14) == 1) && (get_attitude(ME) < 10)) {
			spec_counter = spec_counter + 1;
			if (spec_counter == 3) {
				print_named_str(ME,"twitches slightly.");
				give_char_text_bubble(ME,"Zzzt.");
				}
			if (spec_counter == 6) {
				print_named_str(ME,"twitches.");
				give_char_text_bubble(ME,"Zzzrnk.");
				}
			if (spec_counter == 9) {
				sf(39,1,1);
				set_attitude(ME,10);
				alert_char(ME);
				begin_talk_mode(51);
				}
			}
		}
		
	// 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 (start_section != section_i_am_in()) {
		set_foe_target(ME,-1);
		return_to_start(ME,2);
		set_state(4);
		}

	if ((get_memory_cell(6) == 3) && (gf(13,3) > 0)) {
		sf(13,3,0);
		begin_talk_mode(33);
		}
		
	// switch target to a pc if we can
	if ((get_target() != get_pc_id(0)) && (get_target() != get_pc_id(1)) && (get_target() != get_pc_id(2)) && (get_target() != get_pc_id(3))) {
			r1 = get_ran(1,0,3);
		if ((char_ok(get_pc_id(r1))) && (can_see_char(get_pc_id(r1))))
			set_foe_target(ME,get_pc_id(r1));
			else {
				r1 = get_ran(1,0,3);
				if ((char_ok(get_pc_id(r1))) && (can_see_char(get_pc_id(r1))))
					set_foe_target(ME,get_pc_id(r1));
					else {
						r1 = get_ran(1,0,3);
						if ((char_ok(get_pc_id(r1))) && (can_see_char(get_pc_id(r1))))
							set_foe_target(ME,get_pc_id(r1));
						}
				}
		}
	do_attack();
break;

beginstate 4; // returning to start
	if (my_dist_from_start() < 8)
		set_state(START_STATE);

	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	
	return_to_start(ME,2);
break;

beginstate TALKING_STATE;
		print_str("Talking: The sentinels can't talk. When you get close to them,");
		print_str("  they pause and inspect you. Then they move on.");
		if (insp_once == 0) {
			print_str("  This one hasn't gotten close enough to inspect you yet.");
			}
break;