include <grapes.wdl>;

	var x;
	var x = media_handle;
	var keenkey = 0;
	var boobus_counter = 0;
	
	var l1fin = 0;
	var l2fin = 0;
	var l3fin = 0;
	var l4fin = 0;
	var l5fin = 0;
	var l7fin = 0;
	var l9fin = 0;
	var l10fin = 0;
	var l12fin = 0;
	var l14fin = 0;
	var l15fin = 0;
	
	sound candy = <keenscore.wav>;

function keenelevator {
   if ((event_type == event_impact) && (you == player)) {
     while (my.z < my.skill1) {
        my.z += 3*TIME;
        player.z = my.z+40;
        wait(1);
}
        wait (30);
        while (my.z > my.skill2) {
          my.z -= 3*TIME;
          wait(1);
}
}
	     wait (1);
}

action keenvator {
	my.ENABLE_IMPACT = ON;
	my.event = keenelevator;
}


function waterfall_f
{
	my.flare = on;
   while (1)
   {
		my.v -= 16*time;
		wait (1);
   }
}

ACTION waterfall_a
{
	my.flare = on;
   while (1)
   {
		my.v -= 16*time;
		wait (1);
   }
   
	my.event = waterfall_f;
}

function locked_door {
  if (EVENT_TYPE == event_trigger) {
  	if (keenkey != 1) {
  	msg_show("You need a key to open this door.", 5);
  	sleep (1);
  	return;}
   if (my._COUNTER <= 0) // if door was closed, open it
  { 
    ent_playsound(MY,open_snd,66);
    while (my._COUNTER < 120)
    {
      my.Z += 3*TIME; // raise
      my._COUNTER += 3*TIME;
      wait(1);
    }
    my._COUNTER = 120;
    keenkey = 0;
    my.event = NULL;
  }
}
}


action lock_door 
{
  my.trigger_range = 10;
  my.ENABLE_trigger = ON;  // make door sensitive for triggering
  my.event = locked_door;
}

sound key_fetch = <beamer.wav>;
var keenkey = 0;

function pickup_the_key
{
  if ((EVENT_TYPE == event_impact) && (you == player)) {
  msg_show("You got a key!",5);
  keenkey = 1;
  snd_play(key_fetch,50,0);
  wait(1); // wait one frame before removing an entity in an event function
  ent_REMOVE(MY);
}
}

action k_key 
{
  my.ENABLE_impact = ON;  // pick up by touching it.
  my.event = pickup_the_key;
}


function exit_level
{
	if ((EVENT_TYPE == event_impact) && (you == player)) {
	 media_stop(x);
    media_loop("keendreams.mid", null, 75);
	 msg_show("Congratulations!  You're now re-entering Tuberia.",5);
    wait(2); // time for message to become visible
    keenkey = 0;
    LEVEL_load("keendreams.wmb"); 
    }
}

ACTION levelexit
{
  my.ENABLE_impact = ON;  
  my.event = exit_level;

}

function killkeen {
 if ((EVENT_TYPE == event_trigger) && (you == player)) {
	player._HEALTH = 0;
	wait(1);
}
}

action instantdeath {
  my.trigger_range = 10;
  my.ENABLE_TRIGGER = ON; // make fatal objects sensitive for impact
  my.event = killkeen;
}

function health_add{
 if ((EVENT_TYPE == event_impact) && (you == player)) {
	snd_play (candy, 100, 0);
	player._HEALTH += 10;
   wait (1);
	ent_remove (my);
}
}

action candybar {
  my.ENABLE_impact = ON; // make candy bar sensitive for impact
  my.event = health_add;
  }

function bb_pickup {
	if ((EVENT_TYPE == event_impact) && (you == player)) {	
		wait (1);
		ent_remove (my);
		boobus_counter +=1;
		if (boobus_counter == 1) {msg_show ("You now have one Boobus Bomb.", 5); return;}
	   if (boobus_counter == 2) {msg_show ("You now have two Boobus Bombs.", 5);return;}
	   if (boobus_counter == 3) {msg_show ("You now have three Boobus Bombs.", 5);return;}
		if (boobus_counter == 4) {msg_show ("You now have four Boobus Bombs.", 5); return;}
		if (boobus_counter == 5) {msg_show ("You now have five Boobus Bombs.", 5); return;}
		if (boobus_counter == 6) {msg_show ("You now have six Boobus Bombs.", 5); return;}
		if (boobus_counter == 7) {msg_show ("You now have seven Boobus Bombs.", 5); return;}
		if (boobus_counter == 8) {msg_show ("You now have eight Boobus Bombs.", 5); return;}
		if (boobus_counter == 9) {msg_show ("You now have nine Boobus Bombs.", 5); return;}
		if (boobus_counter == 10) {msg_show ("You now have ten Boobus Bombs.", 5); return;}
	   if (boobus_counter == 11) {msg_show ("You now have eleven Boobus Bombs.", 5);return;}
		if (boobus_counter == 12) {msg_show ("You now have twelve Boobus Bombs.", 5);return;}
		if (boobus_counter == 13) {msg_show ("You now have thirteen Boobus Bombs.", 5);return;}
		if (boobus_counter == 14) {msg_show ("You now have fourteen Boobus Bombs.", 5);return;}
		if (boobus_counter == 15) {msg_show ("You now have fifteen Boobus Bombs!", 5);return;}
	}
}

action boobus_bomb {
  my.ENABLE_impact = ON; // make bombs sensitive for touching
  my.event = bb_pickup;
}

function horseradish
{
	if (l1fin == 1) {
		msg_show ("You've already been to this level!", 5);
		return;
	}
	else {
	 media_stop(x);
    media_loop("horseradish.mid", null, 75);
	 msg_show("Now entering Horseradish Hill....",5);
    wait(2); // time for message to become visible
    LEVEL_load("horseradish.wmb"); 
    l1fin = 1;
   }
}

ACTION level1
{
  my.event = horseradish;
  my.enable_scan = on;
  if (key_space ==1){ my.event = horseradish;}
}

function melonmines
{
	if (l2fin == 1) {
		msg_show ("You've already been to this level!",5);
		return;
	}
	else {
	 media_stop(x);
    media_loop("melonmines.mid", null, 75);
	 msg_show("Now entering Melon Mines...Boobus Bombs near!",5);
    wait(2); // time for message to become visible
    LEVEL_load("melonmines.wmb"); 
    l2fin = 1;
    }
}

ACTION level2
{
  my.event = melonmines;
  my.enable_scan = on; 
  if (key_space ==1){ my.event = melonmines;}
}

function bridgebottoms
{
	if (l3fin == 1) {
		msg_show ("You've already been to this level!", 5);
		return;
	}
	else {
	 media_stop(x);
    media_loop("bridgebottoms.mid", null, 75);
	 msg_show("Now entering Bridge Bottoms...",5);
    wait(2); // time for message to become visible
    LEVEL_load("bridgebottoms.wmb"); 
    l3fin = 1;
}
}


ACTION level3
{
  my.event = bridgebottoms;
  my.enable_scan = on;
  if (key_space ==1){ my.event = bridgebottoms;}
}

function rhubarbrapids
{
  if (l4fin == 1){
    msg_show ("You've already been to this level!", 5);	
    return;
}
  else {
	 media_stop(x);
    media_loop("rhubarbrapids.mid", null, 75);
	 msg_show("Now entering Rhubarb Rapids...Boobus Bombs near!",5);
    wait(2); // time for message to become visible
    LEVEL_load("rhubarbrapids.wmb"); 
    l4fin = 1;
}
}

ACTION level4
{
  my.event = rhubarbrapids;
  my.enable_scan = on; 
  if (key_space ==1){ my.event = rhubarbrapids;}
}

function parsnippass
{
	if (l5fin == 1) {
	 msg_show ("You've already been to this level!", 5);
	 return;
   }
   else {
	 media_stop(x);
    media_loop("parsnippass.mid", null, 75);
	 msg_show("Now entering Parsnip Pass...",5);
    wait(2); // time for message to become visible
    LEVEL_load("parsnippass.wmb"); 
    l5fin = 1;
}
}

ACTION level5
{
  my.event = parsnippass;
  my.enable_scan = on; 
  if (key_space ==1){ my.event = parsnippass;}
}

function spudcity
{
	if (l7fin == 1){
	 msg_show ("You've already been to this level!", 5);
	 return;
   }
   else	{
	 media_stop(x);
    media_loop("spudcity.mid", null, 75);
	 msg_show("Now entering Spud City...Boobus Bombs near!",5);
    wait(2); // time for message to become visible
    LEVEL_load("spudcity.wmb"); 
    l7fin = 1;
   }
}

ACTION level7
{
  my.event = spudcity;
  my.enable_scan = on; 
  if (key_space ==1){ my.event = spudcity;}
}

function appleacres
{
  if (l9fin == 1){
  	msg_show ("You've already been to this level!", 5);
  	return;
  }
  else {
	 media_stop(x);
    media_loop("appleacres.mid", null, 75);
	 msg_show("Now entering Apple Acres...Boobus Bombs near!",5);
    wait(2); // time for message to become visible
    LEVEL_load("appleacres.wmb"); 
    l9fin = 1;
   }

}

ACTION level9
{
  my.event = appleacres;
  my.enable_scan = on; 
  if (key_space ==1){ my.event = appleacres;}
}

function grapegrove
{
	if (l10fin == 1) {
		msg_show ("You've already been to this level!", 5);
	}
	else {
	 media_stop(x);
    media_loop("grapegrove.mid", null, 75);
	 msg_show("Now entering Grape Grove...Boobus Bombs near!",5);
    wait(2); // time for message to become visible
    LEVEL_load("grapegrove.wmb"); 
    l10fin = 1;
}
}

ACTION level10
{
  my.event = grapegrove;
  my.enable_scan = on;
  if (key_space ==1){ my.event = grapegrove;}
}

function brusselsprout
{
	if (l12fin == 1) {
		msg_show ("You've already been to this level!", 5);
	}
	else {
	 media_stop(x);
    media_loop("squashswamp.mid", null, 75);
	 msg_show("Now entering Brussel Sprout Bay...",5);
    wait(2); // time for message to become visible
    LEVEL_load("brusselsprout.wmb"); 
    l12fin = 1;
}
}

ACTION level12
{
  my.event = brusselsprout;
  my.enable_scan = on;
  if (key_space ==1){ my.event = brusselsprout;}
}

function squashswamp
{
	if (l14fin == 1) {
		msg_show ("You've already been to this level!", 5);
	}
	else {
	 media_stop(x);
    media_loop("brusselsprout.mid", null, 75);
	 msg_show("Now entering Squash Swamp...Boobus Bombs near!",5);
    wait(2); // time for message to become visible
    LEVEL_load("squashswamp.wmb"); 
    l14fin = 1;
}
}

ACTION level14
{
  my.event = squashswamp;
  my.enable_scan = on;
  if (key_space ==1){ my.event = squashswamp;}
}

function castletuberia
{
	if (boobus_counter < 15) {
		msg_show ("You can't enter Boobus Tuber's castle until you have 15 Boobus bombs to attack him.", 5);
		return;
	}
	 media_stop(x);
    media_loop("castletuberia.mid", null, 75);
	 msg_show("Now entering Castle Tuberia...",5);
    wait(2); // time for message to become visible
    LEVEL_load("castletuberia.wmb"); 
}

ACTION level15
{
  my.event = castletuberia;
  my.enable_scan = on;
  if (key_space ==1){ my.event = castletuberia;}
}

function boobuschamber{
  if (event_type == event_impact){
	 media_stop(x);
    media_loop("boobuschamber.mid", null, 75);
	 msg_show("Now entering Boobus' Chamber!!  Advisory: SAVE THE GAME NOW!",5);
    wait(2); // time for message to become visible
    LEVEL_load("boobuschamber.wmb"); 
}
}

action level16 { 
  my.enable_impact = on;
  my.event = boobuschamber;
  
 }


var tongue_value = 0;

action the_tongue {
	tongue_value = 0;
	while (1){
	while (tongue_value <= 550){
		my.y += 3*TIME;
		tongue_value += 3*TIME;
		wait (1);
	}
	sleep (4);
	while (tongue_value >= 0){
		my.y -= 3*TIME;
		tongue_value -= 3*TIME;
		wait (1);
	}
	sleep (4);
	tongue_value = 0;
}
}

var the_lever = 0;
var boobus_dead = 0;

function big_lever {
	/*
 if (boobus_dead != 1){
 	 msg_show ("Deal with Boobus Tuber first!", 5);
 	 return;
}
*/
	while (the_lever < 70) {
	   my.roll -= 3*TIME;
	   the_lever += 3* TIME;
	   wait (1);
	}
	 media_stop(x);
    media_loop("keenend.mid", null, 75);
    LEVEL_load("keenending.wmb"); 
}

action red_lever {
	my.enable_scan = ON;
	my.event = big_lever;
}

function ending_the_game () {
	if (EVENT_TYPE == event_IMPACT) {
		EXIT; 
	}
}

action end_keen {
	my.ENABLE_impact = ON;
	my.event = ending_the_game;
}


//Enemy AI functions and actions

function mystate_walk () {
while (1) {
 	 if (my._health <= 0) {mystate_die(); return;}
 	 if (my._signal == 1) {mystate_attack(); return;}
/*	ent_cycle ("walk", my._animdist);
 	if (my._animdist < 100) {
 		my._animdist += 5*TIME;
   }
   else {
   	my._animdist = 0;
   }
*/
   if (my._force < 300) {
   	my._force += 3*TIME;
   }
   else {
   	if (my._armor < 90) {
         my._armor += 3 * TIME;
         my.pan += 5 * TIME;
      }
      else {
      	my._armor = 0;
      	my._force = 0;   
         sleep (2);
         mystate_scan ();
      }
   }
	wait (1);
 }
}

function mystate_scan () {
	temp.PAN = 180;
   temp.TILT = 180;
   temp.Z = 500;
   indicator = _WATCH;
   SCAN_ENTITY(my.x,temp);
   if (my._SIGNAL == 1) {mystate_attack(); return; }
}

function mystate_die ()  {
  my._ANIMDIST = 0; // reset entities' animation time to zero
  while (my._ANIMDIST < 100)
   {
    ent_FRAME ("death",my._ANIMDIST); // set the frame from the percentage
    my._ANIMDIST += 5.0 * TIME; // calculate next percentage for death in ~1.25 seconds
    wait(1);
   }
  my.enable_scan = OFF; // become insensible
  my.enable_shoot = OFF;
  ent_remove (me);
}

function mystate_fight () {
  if (((EVENT_TYPE == EVENT_SCAN) && (indicator == _EXPLODE)))
  || ((EVENT_TYPE == EVENT_SHOOT) && (indicator == _GUNFIRE))
  {
  	  my._SIGNAL = 1; 
  	  my._health = 0;
  }
    if ((EVENT_TYPE == EVENT_DETECT) && (YOU == player))
  {
    my._SIGNAL = 1;
  }
}

function mystate_attack ()
{
  my._ANIMDIST = 0;
  while (1)
  {
    if (my._HEALTH <= 0) { mystate_die(); return; }
 
    temp.X = player.X - my.X;
    temp.Y = player.Y - my.Y;
    temp.Z = player.Z - my.Z;
    vec_to_angle(my_angle,temp);
    force = 4; // set rotation speed
    actor_turn(); // rotate towards my_angle
    wait (1);
    
    ent_CYCLE("attack", my._ANIMDIST);
    my._ANIMDIST += 5 * TIME;
    if (my._ANIMDIST > 100)
    {
      my._ANIMDIST -= 100;
      // fire at player at end of animation cycle
      shot_speed.X = 100;
      shot_speed.Y = 0;
      shot_speed.Z = 0;
      my_angle.PAN = my.PAN; // TILT is already set from vec_to_angle
      vec_rotate(shot_speed,my_angle);
      // now shot_speed points ahead, towards the player
      damage = 50;
      fire_mode = 16; // rocket.mdl
      ent_CREATE("rocket.mdl",my.x,bullet_shot);
    }
    wait(1);
  }


 }
 }
 
 
action tater_trooper {

  my._WALKFRAMES = 1;
  my._ENTFORCE = 1.5;
  my._HEALTH = 10;
  my._SIGNAL = 0; // player not yet detected

  my.enable_scan = ON; // sensible of explosions
  my.enable_shoot = ON; // sensible of gunshots
  my.enable_detect = ON; // sensible of the player
  my.event = mystate_fight; // handle hits & detection

  mystate_walk (); //March around.
}


ACTION keen_ai_one
{
	// set up and call helper functions
	// Health-
	if (MY._HEALTH <= 0) { MY._HEALTH = 50; }	// default 50 health

	// Armor-
	if (MY._ARMOR <= 0) { MY._ARMOR = 0; }	// default 0 armor

 	// Force-
	if (MY._FORCE == 0) { MY._FORCE = 1.5; } 	// default force

 	// Alertness-
	temp = int(MY._ALERTNESS);
	if(temp == 0) { MY._I_ALERTNESS = 1000; }	// default alertness range
	else { MY._I_ALERTNESS = int(MY._ALERTNESS); }
	temp = frc(MY._ALERTNESS) * 1000;
	if(temp == 0) { MY._I_ALERTNESS += 0.180; }	// deault alertness cone
	else { MY._I_ALERTNESS += frc(MY._ALERTNESS); }


	// Cowardice-
	if(MY._COWARDICE == 0) { MY._I_COWARDICE = 30; }	// default of 30
	else { MY._I_COWARDICE = MY._COWARDICE; }

	// Firemode-
	if (MY._FIREMODE == 0)
   {MY._FIREMODE = 16.10;} 
	//MY._FIREMODE = DAMAGE_SHOOT+FIRE_PARTICLE+HIT_FLASH+0.10; }	// use default

	// Accuracy-
	if (MY._ACCURACY > 0)
	{
		MY._I_ACCURACY = MY._ACCURACY; // positive value given, use specified accuracy
	}
	else
	{
		if (MY._ACCURACY < 0)
		{ MY._I_ACCURACY = 0; }	// negative value given, use perfect accuracy
		else // MY._ACCURACY == 0
		{ MY._I_ACCURACY = 5; }	// no value given, use default accuracy
	}



	// Hitmode-
	//--MY._HITMODE = MY._HITMODE;	// zero is the default, no extra checking needed

	// Muzzle Vertex-
	MY._MUZZLE_VERT = int(MY._MUZZLEATTACH);


	if((int(MY._WEAPONRANGE)) <= 0) { MY._WEAPONRANGE = 1500; } // default to 1500 quants for weapon range
	if((frc(MY._WEAPONRANGE)) == 0) { MY._WEAPONRANGE += 0.32; } // default to 32ticks for reload speed

	// uses same advanced animation packing as player (dont let the 'player' prefix fool you)
	player_anim_pack();

	// Other setup
	MY._WALKSOUND = _SOUND_ROBOT;

	anim_init();	// setup animation a bit
	drop_shadow(); // attach shadow to robot
	MY._MOVEMODE = _MODE_WALKING; // standing/walking/running
	actor_adv_anim2();	// use new animation style (with blending)

// create a test box that shows AI's state
IFDEF SHOWAIBOXES;
	ent_create("cube.mdl",my.x,_actor_ai_testbox);
ENDIF;

	actor_ai_loop_one();	// call AI loop
}


action credits {
   while (1) {
   	my.z += 3 * TIME;
   	wait (1);
   }
}

function melon_shoot () {
 while (1) {
    ent_CYCLE("attack", my._ANIMDIST);
    my._ANIMDIST += 5 * TIME;
    if (my._ANIMDIST > 100)
    {
      my._ANIMDIST -= 100;
      // fire at player at end of animation cycle
      shot_speed.X = 0;
      shot_speed.Y = 0;
      shot_speed.Z = -100;
      my_angle.PAN = my.PAN; // TILT is already set from vec_to_angle
      vec_rotate(shot_speed,my_angle);
      // now shot_speed points ahead, towards the player
      damage = 50;
      fire_mode = 16; // rocket.mdl
      ent_CREATE("rocket.mdl",my.x,bullet_shot);
    }
    wait(1);
   }
}

action melon () {
	my.event = melon_shoot();
}