// View plain text
//abilswitch
//This script will behave like basicnpc, except that if directed to, it 
//will regularly switch its special ability among a chosen set. 
// - IT SHOULD NOT BE GIVEN MORE THAN TWELVE ABILITIES TO CYCLE THROUGH - 
//Use of abilitites 0-9 is specified in memory cell 4. Where n is the 
//number of the ability, add 2^n to cell 4 to add it to the list. 
//For abilities 10-19, add 2^(n-10) to cell 5, etc. 
//Examples: Use all breath weapons: 0 896 0 32
//	    Fire all beams: 652 58 0 5
//	    Radiate all fields: 0 0 448 896
//	    Use all melee effects: 338 5 15 64
//	    Throw all types of projectiles: 32 64 0 16

// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4 - which of abilities 0-9 to use
//   Cell 5 - which of abilities 10-19 to use
//   Cell 6 - which of abilities 20-29 to use
//   Cell 7 - which of abilities 30-39 to use

begincreaturescript;

variables;

short c, d, i, target;
short last_abil_time,num_abils;
short r1;
short a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11;
short current_abil;

body;

beginstate INIT_STATE;
	last_abil_time = get_current_tick();
	num_abils = 0;
	if(((get_memory_cell(4)) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(4) / 2) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(4) / 4) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(4) / 8) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(4) / 16) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(4) / 32) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(4) / 64) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(4) / 128) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(4) / 256) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(4) / 512) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5)) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5) / 2) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5) / 4) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5) / 8) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5) / 16) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5) / 32) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5) / 64) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5) / 128) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5) / 256) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(5) / 512) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6)) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6) / 2) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6) / 4) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6) / 8) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6) / 16) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6) / 32) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6) / 64) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6) / 128) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6) / 256) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(6) / 512) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7)) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7) / 2) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7) / 4) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7) / 8) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7) / 16) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7) / 32) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7) / 64) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7) / 128) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7) / 256) % 2) == 1)
		num_abils = num_abils + 1;
	if(((get_memory_cell(7) / 512) % 2) == 1)
		num_abils = num_abils + 1;
	if(num_abils > 12){
		print_str_color("abilswitch error: Too many abilities; setting to 0.",1);
		num_abils = 0;
	}
	i = 0;
	target = 0;
	d = 1;
	while(i < 40){
		d = d * 2;
		c = (i / 10) + 4;
		if(i == 10 || i==20 || i==30)
			d = 1;
		if(((get_memory_cell(c) / d) % 2) == 1){
			if(target == 0)
				a0 = i;
			else if(target == 1)
				a1 = i;
			else if(target == 2)
				a2 = i;	
			else if(target == 3)
				a3 = i;
			else if(target == 4)
				a4 = i;
			else if(target == 5)
				a5 = i;
			else if(target == 6)
				a6 = i;
			else if(target == 7)
				a7 = i;
			else if(target == 8)
				a8 = i;
			else if(target == 9)
				a9 = i;
			else if(target == 10)
				a10 = i;
			else if(target == 11)
				a11 = i;
			target = target + 1;
		}
		i = i + 1;
	}
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	current_abil = 0;
	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_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) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0))) {
		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 (target_ok() == FALSE)
		set_state(START_STATE);
	if ((tick_difference(last_abil_time,get_current_tick()) >= 1) &&
	  ((get_memory_cell(4) > 0) || (get_memory_cell(5) > 0) || (get_memory_cell(6) > 0) 
	  || (get_memory_cell(7) > 0)) && (can_see_char(1000))) {
	  	r1 = get_ran(1,0,num_abils - 1);
	  	if(r1 == 0){
			set_special_ability(ME,a0);
			current_abil = a0;
		}
		else if(r1 == 1){
			set_special_ability(ME,a1);
			current_abil = a1;
		}
		else if(r1 == 2){
			set_special_ability(ME,a2);
			current_abil = a2;
		}
		else if(r1 == 3){
			set_special_ability(ME,a3);
			current_abil = a3;
		}
		else if(r1 == 4){
			set_special_ability(ME,a4);
			current_abil = a4;
		}
		else if(r1 == 5){
			set_special_ability(ME,a5);
			current_abil = a5;
		}
		else if(r1 == 6){
			set_special_ability(ME,a6);
			current_abil = a6;
		}
		else if(r1 == 7){
			set_special_ability(ME,a7);
			current_abil = a7;
		}
		else if(r1 == 8){
			set_special_ability(ME,a8);
			current_abil = a8;
		}
		else if(r1 == 9){
			set_special_ability(ME,a9);
			current_abil = a9;
		}
		else if(r1 == 10){
			set_special_ability(ME,a10);
			current_abil = a10;
		}
		else if(r1 == 11){
			set_special_ability(ME,a11);
			current_abil = a11;
		}
		last_abil_time = get_current_tick();
	}
	if(current_abil > 0)
		do_attack_tactic(1);
	else
		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