//mobserver.txt - Like basic monst, but this character is a servitor. Does 
// menial tasks of the given type. Doesn't fight back if attacked.
// 
// Memory Cells:
//   Cell 0 - Dialogue node when spoken to
//   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.
//   Cell 4 - Servant type
//		0 - immobile
//		1 - fetches wood. goes to nav point, makes a stick. picks it up. 
//			returns.
//		2 - mines rocks out of wall, takes them to nav point, drops on floor
//		3 - goes to nav point, picks up a rock, returns it to start
//		4 - goes to brick pile ot get bricks and then takes them to a building

begincreaturescript;

variables;

short i,target;
short server_step = 0;
short last_walk;

body;

beginstate INIT_STATE;
	set_summon_level(ME,1);
	
	last_walk = get_current_tick();

	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);
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_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);
		}

	// fetch wood
	if (get_memory_cell(4) == 1) { 
		if (server_step == 0) { // wait to start
			if (tick_difference(last_walk,get_current_tick()) > 6) {
				approach_nav_point(ME,get_memory_cell(3),1);
				last_walk = get_current_tick();
				server_step = 1;
				end_combat_turn();
				}
			end();
			}
		if (server_step == 1) { // go to wood
			if (approach_nav_point(ME,get_memory_cell(3),1)) {
				server_step = 2;
				end_combat_turn();
				end();
				}
			}
		if (server_step == 2) { // start chopping wood
			if (approach_nav_point(ME,get_memory_cell(3),1)) {
				if (can_see_char(30000))
					print_named_str(ME,"begins to chop some wood.");
				server_step = 3;
				last_walk = get_current_tick();
				end_combat_turn();
				end();
				}
			}
		if (server_step == 3) { // chopping
			if (take_item_on_floor_here(60,my_loc_x(),my_loc_y())) {
				server_step = 4;
				if (can_see_char(30000))
					print_named_str(ME,"picks up a piece of wood.");
				end();
				}
				else if (tick_difference(last_walk,get_current_tick()) > 6) {
					if (can_see_char(30000))
						print_named_str(ME,"tears loose a piece of wood.");
					last_walk = get_current_tick();
					put_item_on_spot(60,my_loc_x(),my_loc_y());
					end();
					}
					else if (get_ran(1,0,100) < 20)
						give_char_text_bubble(ME,"Chop! Chop!");
			end_combat_turn();
			}
		if (server_step == 4) { // back to start
			if (return_to_start(ME,1)) {
				if (can_see_char(30000))
					print_named_str(ME,"puts the wood into the furnace.");
				last_walk = get_current_tick();
				server_step = 0;
				end_combat_turn();
				end();
				}
			}
		}

	// mine rocks
	if (get_memory_cell(4) == 2) { 
		if (server_step == 0) { // mine rocks
			if (tick_difference(last_walk,get_current_tick()) > 8) {
				if (can_see_char(30000))
					print_named_str(ME,"finishes gathering some rocks.");
				approach_nav_point(ME,get_memory_cell(3),1);
				last_walk = get_current_tick();
				server_step = 1;
				}
				else if (get_ran(1,0,100) < 20)
					give_char_text_bubble(ME,"Whack! Whack!");
			end_combat_turn();
			end();
			}
		if (server_step == 1) { // go to drop off pt
			if (approach_nav_point(ME,get_memory_cell(3),1)) {
				server_step = 2;
				end_combat_turn();
				end();
				}
			}
		if (server_step == 2) { // drop rock
			if (can_see_char(30000))
				print_named_str(ME,"drops off some rocks.");
			server_step = 3;
			put_item_on_spot(190,my_loc_x(),my_loc_y());
			last_walk = get_current_tick();
			end_combat_turn();
			end();
			}
		if (server_step == 3) { // back to start
			if (return_to_start(ME,1)) {
				if (can_see_char(30000))
					print_named_str(ME,"resumes digging.");
				last_walk = get_current_tick();
				server_step = 0;
				end_combat_turn();
				}
			end();
			}
		}
	
	// collect rocks
	if (get_memory_cell(4) == 3) { 
		end_combat_turn();
		if (server_step == 0) { // wait to start
			if (tick_difference(last_walk,get_current_tick()) > 6) {
				approach_nav_point(ME,get_memory_cell(3),1);
				last_walk = get_current_tick();
				server_step = 1;
				end_combat_turn();
				}
			end();
			}
		if (server_step == 1) { // go to wood
			if (approach_nav_point(ME,get_memory_cell(3),1)) {
				last_walk = get_current_tick();
				server_step = 2;
				end_combat_turn();
				end();
				}
			}
		if (server_step == 2) { // chopping
			if (take_item_on_floor_here(190,my_loc_x(),my_loc_y())) {
				server_step = 3;
				if (can_see_char(30000))
					print_named_str(ME,"picks up a load of rocks.");
				}
				else if (tick_difference(last_walk,get_current_tick()) > 6) {
					server_step = 3;
					}
			end_combat_turn();
			end();
			}
		if (server_step == 3) { // back to start
			if (return_to_start(ME,1)) {
				if (can_see_char(30000))
					print_named_str(ME,"drops the rocks on the pile.");
				last_walk = get_current_tick();
				server_step = 0;
				end_combat_turn();
				end();
				}
			}
		}

	// make wall
	if (get_memory_cell(4) == 4) { 
		if (server_step == 0) { // mine rocks
			if (tick_difference(last_walk,get_current_tick()) > 8) {
				if (can_see_char(30000))
					print_named_str(ME,"finishes gathering up bricks.");
				approach_nav_point(ME,get_memory_cell(3),1);
				last_walk = get_current_tick();
				server_step = 1;
				}
			end_combat_turn();
			end();
			}
		if (server_step == 1) { // go to drop off pt
			if (approach_nav_point(ME,get_memory_cell(3),1)) {
				server_step = 2;
				end_combat_turn();
				end();
				}
			}
		if (server_step == 2) { // drop rock
			if (can_see_char(30000))
				print_named_str(ME,"starts building a wall.");
			server_step = 3;
			last_walk = get_current_tick();
			end_combat_turn();
			end();
			}
		if (server_step == 3) { // back to start
			if (return_to_start(ME,1)) {
				if (can_see_char(30000))
					print_named_str(ME,"goes to get more bricks.");
				last_walk = get_current_tick();
				server_step = 0;
				end_combat_turn();
				}
			end();
			}
		}

	// 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(0) == 0) {
		print_str("Talking: Stone servants have a variety of skills and abilities. They");
		print_str("  also all have one thing in common: They don't talk.");
		}
		else begin_talk_mode(get_memory_cell(0));
break;