begincreaturescript;

variables;

short i,target,blades,dtarget,misses,last_abil_time;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	if (get_attitude(ME) >= 10)
		alert_char(ME);
	break;

beginstate DEAD_STATE;

	message_dialog("Todmacher, Trahison's cohort and ally, falls over dead.","Two down, one to go.");
	inc_flag(33,0,1);
	if(get_flag(33,0) >= 29){
		message_dialog("Finally!  Trahison and all of his men are defeated.  Fortress Grenze's rebellion is dead.","Now, to leave.");
		set_flag(33,0,30);
		toggle_quest(4,0);
	}

		put_item_on_spot(my_loc_x(),my_loc_y(),497);
	award_party_xp(500,40);
	award_party_xp(500,40);

break;

beginstate START_STATE; 

if(enemies_nearby(10) >= 1){
	if(tick_difference(last_abil_time,get_current_tick()) >= 1){
		blades = 0;
		misses = 0;
		while(blades <= 5){
			dtarget = get_ran(1,0,120);
			if(char_ok(dtarget) == TRUE){
				if(can_see_loc(char_loc_x(dtarget),char_loc_y(dtarget)) == TRUE && get_attitude(dtarget) != get_attitude(ME)){
					put_straight_zap(my_loc_x(),my_loc_y(),char_loc_x(dtarget),char_loc_y(dtarget),3);
					set_character_pose(ME,1);
					force_instant_terrain_redraw();
					run_animation_sound(69);
					pause(2);
					if(dtarget >= 6){
						kill_char(dtarget,3,0);
					}
					else{
						damage_char(dtarget,50,0);
					}
					blades = blades + 1;
					set_character_pose(ME,2);
					force_instant_terrain_redraw();
					pause(2);
				}
			}
			else{
				misses = misses + 1;
				if (misses == 30){
					misses = 0;
					blades = blades + 1;
				}
			}
		}
		last_abil_time = get_current_tick();
	}
}

	if (get_attitude(ME) >= 10)
		alert_char(ME);

	// 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) {
		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(enemies_nearby(10) >= 1){
	if(tick_difference(last_abil_time,get_current_tick()) >= 1){
		misses = 0;
		blades = 0;
		while(blades <= 5){
			dtarget = get_ran(1,0,120);
			if(char_ok(dtarget) == TRUE){
				if(can_see_loc(char_loc_x(dtarget),char_loc_y(dtarget)) == TRUE && get_attitude(dtarget) != get_attitude(ME)){
					put_straight_zap(my_loc_x(),my_loc_y(),char_loc_x(dtarget),char_loc_y(dtarget),3);
					set_character_pose(ME,1);
					force_instant_terrain_redraw();
					run_animation_sound(69);
					pause(2);
					if(dtarget >= 6){
						kill_char(dtarget,3,0);
					}
					else{
	damage_char(dtarget,200,0);
	set_char_status(dtarget,4,(get_char_status(dtarget,4) * -1),1,0);
					}
					blades = blades + 1;
					set_character_pose(ME,2);
					force_instant_terrain_redraw();
					pause(2);
				}
			}
			else{
				misses = misses + 1;
				if (misses == 30){
					misses = 0;
					blades = blades + 1;
				}
			}
		}
		last_abil_time = get_current_tick();
	}
}

	if (get_attitude(ME) >= 10)
		alert_char(ME);

	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;