# Jedi Knight Cog Script
#
# 19b_Endlevel cog
#
# Endlevel function for level 19B.
#
# Opening a door with a key causes the endlevel.
#
# [JS]
#
# (C) 1997 LucasArts Entertainment Co. All Rights Reserved
# ========================================================================================


symbols

message     startup
message     activate
message     timer
message     user0
message     shutdown

surface     switch0                       linkid=1
thing       door0                         linkid=2

thing       player                        local

flex        doorspeed=2.0

sound       wav0
sound       wav1=beep1.wav                local
sound       goalsnd=Accomplish1.wav       local
sound       locksound=df_door2-3.wav      local

int         waiting=0                     local

material    tip_mat=saberyellow0.mat      local
material    side_mat=saberyellow1.mat     local

template    tpl_wall=+ssparks_wall        local
template    tpl_blood=+ssparks_blood      local
template    tpl_saber=+ssparks_saber      local

end

# ========================================================================================

code

startup:
   player = GetLocalPlayerThing();

   // Prepare Kyle's yellow lightsaber if light path
   if(jkGetChoice() == 1)
   {
		jkSetSaberInfo(player, side_mat, tip_mat, 0.0128, 0.0128, 0.123, tpl_wall, tpl_blood, tpl_saber);
   }

   // This is the master COG, so register it.
   SetMasterCOG(GetSelfCOG());

   // Setup the goals first string in cogStrings.uni so the UI knows about it.
   SetInv(player, 99, 19500);

   // Setup the first goal as DISPLAYED
   SetGoalFlags(player, 0, 1);

   // When player starts, give him fists, and a bryar.
   SetInv(player, 1, 1.0);
   SetInv(player, 2, 1.0);
   if(GetInv(player, 11) < 50) SetInv(player, 11, 50.0);

   // Initialize weapon.
   SetFireWait(player, -1);
   SetMountWait(player, 0);
   SetCurInvWeapon(player, 0);
   SelectWeapon(player, AutoSelectWeapon(player, 1));

   return;

# ........................................................................................

activate:
   player = GetSourceRef();

   if (GetWallCel(switch0) == 1) return;

   // not the switch
   if (GetSenderId() != 1)
      {
      PlaySoundLocal(locksound, 1, 0, 0);
      return;
      }

   // no key
   if (GetInv(player, 46) == 0) {
      if (waiting) return;

      PlaySoundLocal(wav0, 1.0, 0.0, 0);
      jkPrintUNIString(-1, 1001);                  // will display the "Locked!" line
      waiting=1;
      SetTimer(5.0);
      return;
   } else {
      SetWallCel(switch0, 1);
      PlaySoundPos(wav1, SurfaceCenter(switch0), 1, -1, -1, 0);
      MoveToFrame(door0, 1, doorspeed);
      Sleep(4.0);
      jkEndLevel(1);
   }
   return;

# ........................................................................................

timer:
   waiting=0;
   return;

# ........................................................................................

user0:
   PlaySoundThing(goalsnd, player, 1.0, -1, -1, 0);
   jkPrintUNIString(player, 350);

   if(GetParam(0) == 0)
   {
      SetGoalFlags(player, 0, 2);
      SetGoalFlags(player, 1, 1);
      Return;
   }

   if(GetParam(0) == 1)
   {
      SetGoalFlags(player, 1, 2);
      SetGoalFlags(player, 2, 1);
      Return;
   }

   if(GetParam(0) == 2)
   {
      SetGoalFlags(player, 2, 2);
      SetGoalFlags(player, 3, 1);
      Return;
   }

   if(GetParam(0) == 3)
   {
      SetGoalFlags(player, 3, 2);
      Return;
   }

   Return;

# ........................................................................................

shutdown:
   // Give a force star if the player finds all the secrets
   if((GetInv(player, 71) != 0) && (GetInv(player, 70) == GetInv(player, 71)))
      ChangeInv(player, 16, 1);

   Return;

end

