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, henchman to General Trahison, falls over dead.","A steel key falls out of his grasp.  Seeing how it will likely come in handy, you pick it up.");
	set_flag(31,0,1);
	if(get_flag(29,0) == 0){
		message_dialog("Trahison steps back, surprised.  _Very well then, we shall meet again soon, if you get that far._","Trahison grasps a red, glass sphere and tosses it on the ground.");
		put_boom_on_char(11,2,0);
		erase_char(11);
		run_animation_sound(10);
		set_flag(29,0,1);
	}
		message_dialog("Klugmann produces a red sphere similar to the one used by Trahison.  _We will meet soon._","That having been said, he tosses the sphere onto the ground and retreats.");
		put_boom_on_char(12,2,0);
		erase_char(12);
		run_animation_sound(10);
		set_flag(30,0,2);
		change_spec_item(26,1);
		set_terrain(18,20,289);
		put_effect_on_space(18,20,8,4,0);
		force_instant_terrain_redraw();
		run_animation_sound(89);
	award_party_xp(500,40);
	award_party_xp(500,40);

		put_item_on_spot(my_loc_x(),my_loc_y(),497);

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;