begincreaturescript;

variables;

short i,target;
short last_abil_time,atarget,type,whom;

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("Klugmann, 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(),496);
	award_party_xp(500,40);
	award_party_xp(500,40);

break;

beginstate START_STATE; 
	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 (get_attitude(ME) >= 10)
		alert_char(ME);

// Time to be bad.
if(enemies_nearby(10) >= 1){
	if(tick_difference(last_abil_time,get_current_tick()) >= 1){
		atarget = 0;
		whom = get_ran(1,1,2);
		type = get_ran(1,1,3);
		while(atarget <= 120 && whom == 1){
	if(char_ok(atarget) == TRUE){
if(can_see_loc(char_loc_x(atarget),char_loc_y(atarget)) == TRUE){
if((get_attitude(ME) < 4 && get_attitude(atarget) < 4) || (get_attitude(ME) >= 10 && get_attitude(atarget) >= 10)){
	if(type == 1){
		put_effect_on_char(atarget,8,1,2);
		set_char_status(atarget,1,50,1,0);
	}
	if(type == 2){
		put_effect_on_char(atarget,9,1,2);
		set_char_status(atarget,2,50,1,0);
	}
	if(type == 3){
		put_effect_on_char(atarget,10,1,2);
		set_char_status(atarget,3,50,1,0);
	}
}
}
	}
atarget = atarget + 1;
		}
		while(atarget <= 120 && whom == 2){
	if(char_ok(atarget) == TRUE){
if(can_see_loc(char_loc_x(atarget),char_loc_y(atarget)) == TRUE){
if((get_attitude(ME) < 4 && get_attitude(atarget) >= 10) || (get_attitude(ME) >= 10 && get_attitude(atarget) < 4)){
	if(type == 1){
		put_effect_on_char(atarget,5,1,2);
		set_char_status(atarget,1,-50,1,0);
	}
	if(type == 2){
		put_effect_on_char(atarget,0,1,2);
		set_char_status(atarget,2,-50,1,0);
	}
	if(type == 3){
		put_effect_on_char(atarget,2,1,2);
		set_char_status(atarget,3,-50,1,0);
	}
}
}
	}
atarget = atarget + 1;
		}
		if(whom == 1){
			if(type == 1){
				run_animation_sound(4);
		print_str_color("Klugmann blesses allies!",2);
			}
			if(type == 2){
				run_animation_sound(28);
		print_str_color("Klugmann shields allies!",2);
			}
			if(type == 3){
				run_animation_sound(12);
		print_str_color("Klugmann hastes allies!",2);
			}
		}
		if(whom == 2){
			if(type == 1){
				run_animation_sound(46);
		print_str_color("Klugmann curses enemies!",1);
			}
			if(type == 2){
				run_animation_sound(69);
		print_str_color("Klugmann weakens enemies!",1);
			}
			if(type == 3){
				run_animation_sound(7);
		print_str_color("Klugmann slows enemies!",1);
			}
		}
		last_abil_time = get_current_tick();
	}
}
	

	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;