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, 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("Todmacher 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(13,2,0);
		erase_char(13);
		run_animation_sound(10);
		set_flag(30,0,1);
		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(),496);

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;