-----------------------------------------------------------------------
-- the table where all the functions/methods go to
-----------------------------------------------------------------------
CrocMask = {
   quest,
   name,
   locations,
   iactors,
   triggers,
   conversations,
   cutscenes,
   checklist
}
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- init function
-----------------------------------------------------------------------
function CrocMask:init()
   local questName = "CrocMask"
   -- the quest object stores the C++ object pointer (Quest object)
   self.quest = QuestMgr:createQuest(questName)
   self.name  = questName
   
   -- the locations associated with this quest
   self.locations = {
      --"bazar",
      "nile",
      "ship_wreck",
      "desert_beach"
      --"desert_crossing"
   }

   -- the interactor objects associated with this quest
   self.iactors = {
      "item_silver_coin",
      "item_key_pirate_chest",
      "item_pirate_flag",
      "item_compass_broken",
      "item_treasure_dirty",
      "item_shirt_hawai",
      "item_stone_arm",
      "item_compass_needle",
      "item_compass_fixed",
      "item_skull",
      "item_croc_purse",
      "item_croc_teeth",

      "item_hand_treasurer",
      "animobj_ship_wreck_arm_treasurer",

      "iactor_desert_beach_to_desert_crossing",
      --"iactor_ship_wreck_key_pirate_chest",
      "iactor_ship_wreck_pirate_stuff",
      "iactor_ship_wreck_to_desert_beach",
      --"iactor_bazar_nile_stone_wet",
      --"iactor_bazar_arabian_embassy_to_bazar_tharas_room",

      "iactor_ship_wreck_treasurer_arm",
      "iactor_ship_wreck_treasurer_hand",

      "animobj_ship_wreck_pirate_chest",
      --"animobj_ship_wreck_plank_top",
      "animobj_ship_wreck_plank_bottom",
      --"animobj_bazar_ferry",

      --"iactor_ship_wreck_dead_treasurer_arm",

      "char_nubian",
      "char_granny",
      "char_crocodile",
      "char_tailor"
   }

   -- the conversations associated with this quest
   self.conversations = {
      "CrocMask_granny_start",
      "CrocMask_granny_done",
      "CrocMask_granny_give_purse",
      "CrocMask_nubian_freeride_to_bazar",
      "CrocMask_nubian_freeride_to_desert_beach",
      "CrocMask_nubian_hint",
      "CrocMask_nubian_paid",
      "CrocMask_nubian_quest",
      "CrocMask_nubian_reunion"
   }
   
   -- the cutscenes associated with this quest
   self.cutscenes = {
      "CrocMask_granny_give_purse",
      "CrocMask_nubian_paid",
      "CrocMask_nile_scene",
      "CrocMask_pimp_my_ferry",
      "CrocMask_ferry_to_desert_beach",
      "CrocMask_ferry_to_bazar",
      "CrocMask_open_pirate_chest"
   }

   -- create a proximity trigger for the crocodile
   local trig_args = ProximityTriggerArgs("myArgs")
   trig_args.entity1 = "char_assil"
   trig_args.entity2 = "char_crocodile"
   trig_args.distance = 450
   trig_args.lua_func = "CrocMask:CrocSnapsAtAssil"
   trig_args.single_shot = false
   EventTriggerMgr:createProximityEventTrigger("trig_ship_wreck_croc", trig_args)
   self.quest:addTrigger("trig_ship_wreck_croc", "ship_wreck")
   EventTriggerMgr:setEventTriggerMuted("trig_ship_wreck_croc",true)

   --self.quest:addTrigger("trig_desert_crossing_no_way", "desert_crossing")

   -- the checklist of little tasks associated with this quest
   self.checklist = {
      "CrocPurseTaken",       -- acquired the croc purse from the granny
      "SpokenWithNubian",     -- got word that a silver coin is needed
      "NubianPaid",           -- given silver coin to nubian ferryman
      "PirateChestOpened",
      "TreasurerHandTaken",
      "KeyPirateChestTaken",
      "PirateStuffTaken",
      "StoneArmTaken",
      "StoneArmWrapped",
      "CrocodileDefeated",
      "FerryPimped",
      "ScissorsGiven",
      "ScissorsSharpened",
      "NeedleGiven",
      "CompassFixed",
      "NoCompassMessage",
      "CompassHelpsMessage",
      "SkullTaken",

      "FreeRideToDesert",
      "FerryTakenToDesert",
      "FreeRideToBazar",
      "FerryTakenToBazar"
   }

   -- associate the locations with the quest
   for key, value in ipairs(self.locations) do
      self.quest:addLocation(value)
   end
   -- associate the interactors with the quest
   for key, value in ipairs(self.iactors) do
      self.quest:addInteractor(value)
   end
   -- associate the conversations with the quest
   for key, value in ipairs(self.conversations) do
      self.quest:addConversation(value)
   end
   -- associate the cutscenes with the quest
   for key, value in ipairs(self.cutscenes) do
      self.quest:addCutscene(value)
   end
   -- associate the list items with the quest
   for key, value in ipairs(self.checklist) do
      self.quest:addTaskToCheckList(value)
   end

   -- subscribe the lua functions to call
   self.quest:subscribeLocationEntered(self.name .. ":onLocationEntered")
   self.quest:subscribeLocationLeft(self.name .. ":onLocationLeft")
   self.quest:subscribeInteractorEvent(self.name .. ":onInteractorEvent")
   self.quest:subscribeInteractorEntered(self.name .. ":onMouseEnteredInteractor")
   self.quest:subscribeInteractorLeft(self.name .. ":onMouseLeftInteractor")
   self.quest:subscribeLocationFadingInFinished(self.name .. ":onLocationFadingInFinished")
   self.quest:subscribeConversationEvent(self.name .. ":onConversationEvent")
   self.quest:subscribeConversationFinished(self.name .. ":onConversationFinished")
   self.quest:subscribeCutsceneFinished(self.name .. ":onCutsceneFinished")
   self.quest:subscribeTriggerEvent(self.name .. ":onTriggerEvent")   
   self.quest:subscribeTriggerFaceEvent(self.name .. ":onTriggerFaceEvent")   
   self.quest:subscribeCheckListChanged(self.name .. ":updateFromCheckList")

   -- set the initial quest state
   self.quest:setState("Initialized")
end
-----------------------------------------------------------------------

--------------------------------------------------------------------------
-- sets the quest active and updates the iactor configurations
--------------------------------------------------------------------------
function CrocMask:activate()
   _LogInfo(self.name .. ":activate", self.name)
   self.quest:setActive(true)
   self:initConfigurations()
--   self:updateFromCheckList()

   self.quest:setDone("SpokenWithNubian")
   self.quest:setDone("NubianPaid")

   -- used to equip the mask
   item_croc_mask:setDefaultInventoryAction(Interactor.Use)
   give("bar")

   animobj_crocodile_teeth_underwater:setIgnored(true)
   iactor_desert_beach_to_desert_crossing:setIgnored(true)
   -- set the secret passage to the palace to 'ignored' as Assil has to find it later
   --iactor_bazar_to_palace_backdoor:setIgnored(true)

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- sets the quest inactive and resets the iactor configurations
-----------------------------------------------------------------------
function CrocMask:deactivate()
   _LogInfo("CrocMask:deactivate", "called")
   self.quest:setActive(false)
end
-----------------------------------------------------------------------


-----------------------------------------------------------------------
-- sets the given checklist element to true after the given amount of
-- time (in seconds)
-----------------------------------------------------------------------
function CrocMask:setDoneDelayed(p_Task, p_Time)
   TimedEventMgr:createTimedEvent("CrocMask.quest:setDone(\"" .. p_Task .. "\")", 
				  p_Time)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- Encapsulates picking up an object
-----------------------------------------------------------------------
function CrocMask:pickupItem(p_Animation, p_Sentence,
				   p_BlockTime, p_TaskToSet, p_TimeToSetTask)
   if string.len(p_Sentence) > 0 then
      player:say(p_Sentence)
   end
   player:blendToPlayOnceAndBack(p_Animation)
   MouseIface:blockInput(p_BlockTime)
   self:setDoneDelayed(p_TaskToSet, p_TimeToSetTask)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when an action was performed on an interactor
-- associated with this quest
-----------------------------------------------------------------------
function CrocMask:onInteractorEvent(p_Interactor, p_ActionID, p_InteractedInteractor)
   _LogInfo("CrocMask:onInteractorEvent", p_Interactor)

   local questState = self.quest:getState()
   local iactor     = GameEntityMgr:getInteractor(p_Interactor)

   if iaction(p_Interactor,p_InteractedInteractor,"item_shirt_hawai","iactor_ship_wreck_dead_treasurer_arm") then
      g_InteractionHandled = true
      player:say("ID_ARM_ROTTEN")
   end

   if iaction(p_Interactor,p_InteractedInteractor,"item_croc_teeth","item_croc_purse") or
      iaction(p_Interactor,p_InteractedInteractor,"item_croc_teeth","item_skull") or 
      iaction(p_Interactor,p_InteractedInteractor,"item_skull","item_croc_purse") then
      g_InteractionHandled = true
      player:say("ID_COMBINE_MASK")
   end

   if (p_Interactor == "animobj_ship_wreck_arm_treasurer" or
       p_Interactor == "iactor_ship_wreck_treasurer_arm") and p_ActionID == Interactor.Use then
      char_assil:blendToPlayOnceAndBack("all_pickup_low")
      MouseIface:blockInput(2)
      --animobj_ship_wreck_arm_treasurer:setDefaultAction(Interactor.LookAt)
      animobj_ship_wreck_arm_treasurer:setIgnored(true)
      iactor_ship_wreck_treasurer_arm:setIgnored(true)
      iactor_ship_wreck_treasurer_hand:setIgnored(false)
      animobj_ship_wreck_arm_treasurer:playAnimation("pull")
      item_hand_treasurer:playAnimation("pull")
      sound("pirate_arm_throw")

      -- Ankh/Ship_wreck/pankh_treasurer_dust
   end

   if (p_Interactor == "item_hand_treasurer" or p_Interactor == "iactor_ship_wreck_treasurer_hand") 
      and p_ActionID == Interactor.Pickup then
      self:pickupItem("all_pickup_low", "", 2.0, "TreasurerHandTaken", 0.8)
      item_hand_treasurer:setDefaultInventoryAction(Interactor.Use)
      iactor_ship_wreck_treasurer_hand:setIgnored(true)
   end

   if p_Interactor == "item_hand_treasurer" and p_ActionID == Interactor.Use then
      -- et voila: the key
      self.quest:setDone("KeyPirateChestTaken")
   end

   if p_Interactor == "animobj_bazar_ferry" and p_ActionID == Interactor.Use then
      if self.quest:isDone("FerryTakenToDesert") then
	 self.quest:setUndone("FerryTakenToDesert")
	 self.quest:setDone("FerryTakenToBazar")

	 api:cutscene_("CrocMask_ferry_to_bazar",0)

      elseif self.quest:isDone("FerryTakenToBazar") then
	 self.quest:setUndone("FerryTakenToBazar")
	 self.quest:setDone("FerryTakenToDesert")

	 api:cutscene_("CrocMask_ferry_to_desert_beach",0)
      end
   end

   if p_Interactor == "char_nubian" and p_ActionID == Interactor.TalkTo then
      if not self.quest:isDone("SpokenWithNubian") then
         api:conversation("CrocMask_nubian_quest")
      else
         if not self.quest:isDone("NubianPaid") then
            api:conversation("CrocMask_nubian_hint")
         elseif not self.quest:isDone("FerryPimped") then
            api:conversation("CrocMask_nubian_reunion")
         else
            if not self.quest:isDone("FerryTakenToBazar") then
               -- start the conversation to go to the bazar
--               self.quest:setUndone("FreeRideToBazar")
               api:conversation("CrocMask_nubian_freeride_to_bazar")
	       animobj_bazar_ferry:setDefaultAction(Interactor.Use)
            elseif not self.quest:isDone("FerryTakenToDesert") then
               -- start the conversation to go to the desert
--               self.quest:setUndone("FreeRideToDesert")
               api:conversation("CrocMask_nubian_freeride_to_desert_beach")
            end
         end
      end

   elseif p_Interactor == "iactor_ship_wreck_key_pirate_chest" and p_ActionID == Interactor.Use then
      --iactor_ship_wreck_key_pirate_chest:setDefaultAction(Interactor.Pickup)
      --iactor_ship_wreck_hand_closed:setVisible(false)
      --iactor_ship_wreck_hand_opened:setVisible(true)

   elseif p_Interactor == "iactor_ship_wreck_key_pirate_chest" and p_ActionID == Interactor.Pickup then
      self:pickupItem("all_pickup_low", "", 2.0, "KeyPirateChestTaken", 0.8)

   --elseif p_Interactor == "animobj_ship_wreck_plank_top" and p_ActionID == Interactor.Pickup then
   --   animobj_ship_wreck_plank_top:setIgnored(true)
   --   animobj_ship_wreck_plank_bottom:setIgnored(false)
   --   api:animation("animobj_ship_wreck_plank_top", "animobj_ship_wreck_plank_top/planks_move")

   --elseif p_Interactor == "animobj_ship_wreck_plank_bottom" and p_ActionID == Interactor.Pickup then
   --   animobj_ship_wreck_plank_bottom:setIgnored(true)
   --   item_stone_arm:setIgnored(false)
   --   api:animation("animobj_ship_wreck_plank_bottom", "animobj_ship_wreck_plank_bottom/planks_move")
      
   elseif p_Interactor == "animobj_ship_wreck_plank_bottom" and p_ActionID == Interactor.Pickup then
      CharacterControllerMgr:say("char_assil", "ID_CANNOT_MOVE_IT")

   elseif p_Interactor == "animobj_ship_wreck_plank_bottom" and 
      p_InteractedInteractor == "item_bar" then

      g_InteractionHandled = true

      sound("plank_remove")
      char_assil:blendToPlayOnceAndBack("all_pull_lever")
      MouseIface:blockInput(3)
      animobj_ship_wreck_plank_bottom:setIgnored(true)
      item_stone_arm:setIgnored(false)

      api:callDelayed("api:animation(\"animobj_ship_wreck_plank_bottom\",\"animobj_ship_wreck_plank_bottom/planks_move\")",1.0)
      --api:animation("animobj_ship_wreck_plank_bottom", "animobj_ship_wreck_plank_bottom/planks_move")
      Inventory:removeItemSafely("item_bar")

      -- Ankh/Ship_wreck/pankh_planks_dust

   elseif p_Interactor == "item_stone_arm" and p_ActionID == Interactor.Pickup then
      self:pickupItem("all_pickup_low", "", 2.0, "StoneArmTaken", 0.8)
      sound("stone_arm_pickup")
   elseif (p_Interactor == "item_stone_arm" and p_InteractedInteractor == "item_shirt_hawai") or
          (p_Interactor == "item_shirt_hawai" and p_InteractedInteractor == "item_stone_arm") then
	  g_InteractionHandled = true
	  if not self.quest:isDone("StoneArmTaken") then
	     self.quest:setDone("StoneArmTaken")
	  end
      self.quest:setDone("StoneArmWrapped")

   elseif p_Interactor == "char_nubian" and p_InteractedInteractor == "item_silver_coin" then
      g_InteractionHandled = true
      -- already paid?
      if self.quest:isDone("NubianPaid") then
	 -- nothing to do
      elseif not self.quest:isDone("SpokenWithNubian") or
         not EnterPalace.quest:isDone("GuardsFearRevealed") then
         api:conversation("CrocMask_assil_keep_coin")
      else
         self.quest:setDone("NubianPaid")
         Inventory:removeItemSafely("item_silver_coin")
         api:conversation("CrocMask_nubian_paid")
      end

   elseif iaction(p_Interactor, p_InteractedInteractor, "item_stone_arm", "item_pirate_flag") then
      player:say("ID_FLAG_TOO_PRECIOUS")
      g_InteractionHandled = true

   elseif p_Interactor == "animobj_ship_wreck_pirate_chest" and p_InteractedInteractor == "item_key_pirate_chest" then
      g_InteractionHandled = true
      -- cutscene with anims
      api:cutscene("CrocMask_open_pirate_chest")
      self.quest:setDone("PirateChestOpened")
      --CharacterControllerMgr:say("char_assil", "Cool, I opened the chest!")
      --iactor:playAnimation("open")
      -- we do not need it anymore
      Inventory:removeItemSafely("item_key_pirate_chest")

   elseif p_Interactor == "char_crocodile" and p_InteractedInteractor == "item_stone_arm" then
      g_InteractionHandled = true
      --EventTriggerMgr:setEventTriggerMuted("trig_ship_wreck_croc",true)
      -- to be defined how to handle this
      player:say("CFG_ITEM_STONE_ARM_LOOKAT")

   elseif p_Interactor == "char_crocodile" and p_InteractedInteractor == "item_stone_arm_shirt" then
      g_InteractionHandled = true
      EventTriggerMgr:setEventTriggerMuted("trig_ship_wreck_croc",true)
      api:cutscene("CrocMask_crocodile_fight")
      self.quest:setUndone("StoneArmWrapped")
      Inventory:removeItem("item_stone_arm_shirt")

      -- pickup the pirate stuff within the (now opened) pirate chest
   elseif p_Interactor == "iactor_ship_wreck_pirate_stuff" and p_ActionID == Interactor.Pickup then
      self:pickupItem("all_pickup_midlow", "", 2.0, "PirateStuffTaken", 0.8)
      
      sound("pirate_stuff_pickup")

      -- pimp my ferry
   elseif p_InteractedInteractor == "item_pirate_flag" then
      if p_Interactor == "char_nubian" or p_Interactor == "iactor_ferry" then
         g_InteractionHandled = true
         -- cutscene: nubian applies flag
         api:cutscene("CrocMask_pimp_my_ferry")
         self.quest:setDone("FerryPimped")
         Inventory:removeItem("item_pirate_flag")
      end
   
   -- desert crossing
   elseif p_Interactor == "item_skull" and p_ActionID == Interactor.Pickup then
      self:pickupItem("all_pickup_low", "", 2.0, "SkullTaken", 0.8)
      sound("skull_pickup")
   -- talk to tailor
   elseif p_Interactor == "char_tailor" and p_ActionID == Interactor.TalkTo then
      if not self.quest:isDone("NeedleGiven") then
         if not self.quest:isDone("ScissorsGiven") then
            api:conversation("CrocMask_tailor_quest")
         else
            api:conversation("CrocMask_tailor_hint")
         end
      else
         api:conversation("CrocMask_tailor_done")
      end

   elseif p_Interactor == "iactor_bazar_nile_stone_wet" and p_InteractedInteractor == "item_scissors_blunt" then
      g_InteractionHandled = true
      if not self.quest:isDone("ScissorsSharpened") then
         self:pickupItem("all_combine_low", "ID_CROCMASK_SCISSORS_SHARP", 2.0, "ScissorsSharpened", 1.3)
         sound("scissors_sharpen")
      else
         CharacterControllerMgr:say("char_assil", "ID_CROCMASK_SCISSORS_SHARP")
      end

   elseif (p_Interactor == "char_tailor" and p_InteractedInteractor == "item_scissors_sharp") or
          (p_Interactor == "char_tailor" and p_InteractedInteractor == "item_scissors_blunt") then
      g_InteractionHandled = true
      api:conversation("CrocMask_tailor_hint")

   -- fix the compass
   elseif (p_Interactor == "item_compass_broken" and p_InteractedInteractor == "item_compass_needle") or
          (p_Interactor == "item_compass_needle" and p_InteractedInteractor == "item_compass_broken") then
      g_InteractionHandled = true
      self.quest:setDone("CompassFixed")
      sound("compass_fix")
      CharacterControllerMgr:say("char_assil", "ID_CROCMASK_COMPASS_FIXED")
  
   -- talk to granny
   elseif p_Interactor == "char_granny" and p_ActionID == Interactor.TalkTo then
      if not self.quest:isDone("CrocPurseTaken") then
         api:conversation("CrocMask_granny_start")
      else
         api:conversation("CrocMask_granny_done")
      end

   -- try to steal the purse
   elseif p_Interactor == "item_croc_purse" and p_ActionID == Interactor.Pickup then
      --api:cutscene("CrocMask_granny_give_purse")
      api:conversation("CrocMask_granny_give_purse")
   end

end
-----------------------------------------------------------------------
-- cam_bazar_granny
-----------------------------------------------------------------------
-- lua slot called when the mouse entered an iactor associated 
-- with this quest
-----------------------------------------------------------------------
function CrocMask:onMouseEnteredInteractor(p_Interactor)
   _LogInfo("CrocMask:onMouseEnteredInteractor", p_Interactor)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when the mouse left an iactor associated 
-- with this quest
-----------------------------------------------------------------------
function CrocMask:onMouseLeftInteractor(p_Interactor)
   _LogInfo("CrocMask:onMouseLeftInteractor", p_Interactor)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when the player entered a location that is associated
-- with this quest
-----------------------------------------------------------------------
function CrocMask:onLocationEntered(p_Location)
   _LogInfo("CrocMask:onLocationEntered", p_Location)

   if p_Location == "nile" then
--      api:cutscene_("CrocMask_nile_scene",0)
   end

   if p_Location == "bazar" then
--      animobj_bazar_ferry:setConfiguration("cfg_animobj_bazar_ferry")
--      char_nubian:setConfiguration("CrocMask_char_nubian_bazar")
      if self.quest:isDone("FerryPimped") then
	 animobj_bazar_ferry:setDefaultAction(Interactor.Use)
      end
   end

   if p_Location == "desert_beach" then
      if self.quest:isDone("FerryPimped") then
	 animobj_bazar_ferry:setDefaultAction(Interactor.Use)
      end
   end

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when the player left a location that is associated
-- with this quest
-----------------------------------------------------------------------
function CrocMask:onLocationLeft(p_Location)
   _LogInfo("CrocMask:onLocationLeft", p_Location)

   if p_Location == "desert_crossing" then
      self.quest:setUndone("NoCompassMessage")
   end

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called whenever a location associated with this quest
-- has finished fading in
-----------------------------------------------------------------------
function CrocMask:onLocationFadingInFinished(p_Location)
   _LogInfo("CrocMask:onLocationFadingInFinished", p_Location)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when a cutscene associated with this quest was
-- finished
-----------------------------------------------------------------------
function CrocMask:onCutsceneFinished(p_Cutscene)
   _LogInfo("CrocMask:onCutsceneFinished", p_Cutscene)

   if p_Cutscene == "CrocMask_nile_scene" then
      CrocMask:startShipWreckSequence()
      
      -- remove fruits and laxative (we don't need it anymore)
      Inventory:removeItemSafely("item_fruit_a")
      Inventory:removeItemSafely("item_fruit_b")
      Inventory:removeItemSafely("item_fruit_c")
      Inventory:removeItemSafely("item_laxative")

      item_fruit_a:setDefaultAction(Interactor.LookAt)
      item_fruit_b:setDefaultAction(Interactor.LookAt)
      item_fruit_c:setDefaultAction(Interactor.LookAt)
      item_laxative:setDefaultAction(Interactor.LookAt)
      --iactor_bazar_banana_hut_plate_lemons:setDefaultAction(Interactor.LookAt)
   end

   if p_Cutscene == "CrocMask_granny_give_purse" then
      Global:continueGrannyWalkLoop()
      CameraMgr:activateCamera("cam_bazar_a")
   end

   if p_Cutscene == "CrocMask_ferry_to_bazar" then
      if self.quest:isDone("FerryPimped") then
	 animobj_bazar_ferry:setDefaultAction(Interactor.Use)
      end
   end

   if p_Cutscene == "CrocMask_ferry_to_desert_beach" then
      if self.quest:isDone("FerryPimped") then
	 animobj_bazar_ferry:setDefaultAction(Interactor.Use)
      end
   end

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when a conversation associated with this quest
-- was finished
-----------------------------------------------------------------------
function CrocMask:onConversationFinished(p_Conversation)
   _LogInfo("CrocMask:onConversationFinished", p_Conversation)

   if p_Conversation == "CrocMask_nubian_paid" then
      api:cutscene_("CrocMask_nile_scene",0)
      self.quest:setDone("FerryTakenToDesert")
   end

   if p_Conversation == "CrocMask_granny_give_purse" then
      Global:continueGrannyWalkLoop()
      CameraMgr:activateCamera("cam_bazar_a")
   end

   if p_Conversation == "CrocMask_nubian_freeride_to_desert_beach" then
      if self.quest:isDone("FreeRideToDesert") then
         self.quest:setUndone("FerryTakenToBazar")

	 --api:cutscene_("CrocMask_ferry_to_desert_beach",0)
	 -- thx for playing
	 Ankh:showExtroImage()

         self.quest:setDone("FerryTakenToDesert")
      end
   end

   if p_Conversation == "CrocMask_nubian_freeride_to_bazar" then
      if self.quest:isDone("FreeRideToBazar") then
         self.quest:setDone("FerryTakenToBazar")

	 --api:cutscene_("CrocMask_ferry_to_bazar",0)
	 --api:cutscene_("CrocMask_ferry_to_desert_beach",0)
	 -- thx for playing
	 Demo:showExtroImage()

         self.quest:setUndone("FerryTakenToDesert")
      end
   end

   if p_Conversation == "CrocMask_granny_start" or
      p_Conversation == "CrocMask_granny_done" then
      Global:continueGrannyWalkLoop()
      CameraMgr:activateCamera("cam_bazar_a")
   end

end
----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when a conversation associated with this quest
-- has fired a special event
-----------------------------------------------------------------------
function CrocMask:onConversationEvent(p_Conversation, p_Event)
   _LogInfo("CrocMask:onConversationEvent", p_Conversation .. ":" .. p_Event)

   if p_Conversation == "CrocMask_tailor_hint" and
      p_Event == "CrocMask_needle_given" then
      self.quest:setDone("NeedleGiven")
      sound("needle_receive")
      char_tailor:setConfiguration("cfg_char_tailor")
   end

   if p_Event == "EventBeamAssilGranny" then
      self:beamAssilGranny()
   end

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
--
-----------------------------------------------------------------------
function CrocMask:beamAssilGranny()
   --Global:stopBazarWalkLoops()
   Global:stopGrannyWalkLoop()
   char_assil:setPosition(101.284, 154.704, -1155.67)
   char_assil:setOrientation(Degree(0),Degree(285),Degree(0))
   --char_granny:setPosition(-41.7212, 146.399, -1099.62)
   char_granny:setPosition(-3,145,-1100)
   char_granny:setOrientation(Degree(0),Degree(100),Degree(0))
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
--
-----------------------------------------------------------------------
function CrocMask:startShipWreckSequence()
   -- unmute trigger so Assil runs away from crocodile
   EventTriggerMgr:setEventTriggerMuted("trig_ship_wreck_croc",false)
   CrocMask:setUnderwaterSpeed()
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
--
-----------------------------------------------------------------------
function CrocMask:endShipWreckSequence()
   CharacterControllerMgr:setWalkAnimation("char_assil", "all_walk")
   CharacterControllerMgr:setWalkSpeed("char_assil", 126)
   CharacterControllerMgr:setRunAnimation("char_assil", "all_run")
   CharacterControllerMgr:setRunSpeed("char_assil", 400)
   api:detachFromBoneSafely("char_assil","animobj_nile_crystal_bowl")
   LocationMgr:activateLocation("desert_beach")
   CameraMgr:activateCamera("cam_desert_beach")
   char_assil:setPosition(1300, -110, 1150)
   char_assil:setYaw(Degree(-145))
   self.quest:setDone("CrocodileDefeated")
   Inventory:addItemSafely("item_croc_teeth")
   char_nubian:setConfiguration("CrocMask_char_nubian_desert_beach")
   animobj_bazar_ferry:setConfiguration("cfg_animobj_bazar_ferry_desert_beach")
--   char_tailor:setConfiguration("CrocMask_PirateStuffTaken_char_tailor")
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when a trigger associated with this quest
-- has fired its event enhanced with the face the trigger was entered
-----------------------------------------------------------------------
function CrocMask:onTriggerFaceEvent(p_Trigger,p_FrontFace)
   _LogInfo("CrocMask:onTriggerFaceEvent", p_Trigger)

   if p_Trigger == "trig_desert_crossing_no_way" then
      if p_FrontFace then
         if not self.quest:isDone("CompassFixed") then
            if not self.quest:isDone("NoCompassMessage") then
               self.quest:setDone("NoCompassMessage")
               CharacterControllerMgr:say("char_assil", "ID_NO_COMPASS")
            end
         else
            if not self.quest:isDone("CompassHelpsMessage") then
               self.quest:setDone("CompassHelpsMessage")
               CharacterControllerMgr:say("char_assil", "ID_COMPASS_HELPS")
            end
         end
      end
   end

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when a trigger associated with this quest
-- has fired its event
-----------------------------------------------------------------------
function CrocMask:onTriggerEvent(p_Trigger)
   _LogInfo("CrocMask:onTriggerEvent", p_Trigger)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- sets the interactor configurations for the iactors associated with
-- this quest according to the quest's current state
-----------------------------------------------------------------------
function CrocMask:initConfigurations()
   _LogInfo("CrocMask:initConfigurations", "")
   local questState = self.quest:getState()
   -- default case, quest just initialized
   if questState == "Initialized" then
      for key, value in ipairs(self.iactors) do
	      local configName = self.name .. "_" .. value
	      _LogInfo("setting config: '" .. value .. "' to '" .. configName .. "'","")
	      if InteractorConfigurationMgr:isConfigurationPresent(configName) then
	         local iactor = GameEntityMgr:getInteractor(value)
	         iactor:setConfiguration(configName, true)
	      else
	         _LogWarning("lua", "No such config: " .. configName)
	      end
      end
   else -- any other state
      for key, value in ipairs(self.iactors) do
	      local iactor = GameEntityMgr:getInteractor(value)
	      -- config name defaults to "QuestName_State_objname"
	      local configName = self.name .. "_" .. questState .. "_" .. value
	      -- is there a special configuration available for this state?
	      --_LogInfo("lua", "checking for config to exist: " .. configName)
	      if (InteractorConfigurationMgr:isConfigurationPresent(configName) ~= true) then
	         -- no, use the default for this quest
	         configName = self.name .. "_" .. value
	      end
	      if (InteractorConfigurationMgr:isConfigurationPresent(configName)) then
	         --_LogInfo("setting config: '" .. value .. "' to '" .. configName .. "'","")
	         iactor:setConfiguration(configName, true)
	      end
      end
   end
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- updates the configs for the associated interactors based
-- on the little tasks already completed within this quest
-----------------------------------------------------------------------
function CrocMask:updateFromCheckList(p_Task, p_Done)
   _LogInfo("CrocMask:updateFromCheckList", "")

   if p_Task == "FerryTakenToDesert" then
      if p_Done then
         --LocationMgr:changeCameraAndOrLocation("desert_beach","cam_desert_beach",PinaVector3(677,15, 1457),
         --                                      Rotate(Degree(0),Degree(180),Degree(0)),"default")
         --animobj_bazar_ferry:setConfiguration("cfg_animobj_bazar_ferry_desert_beach")
         --char_nubian:setConfiguration("CrocMask_char_nubian_desert_beach")
	 --api:cutscene_("CrocMask_ferry_to_desert_beach",0)
      end
   end

   if p_Task == "FerryTakenToBazar" then
      if p_Done then
         --LocationMgr:changeCameraAndOrLocation("bazar","cam_bazar_nile",PinaVector3(13726, -1719, 592),
         --             Rotate(Degree(0),Degree(0),Degree(0)),"nile")
         --animobj_bazar_ferry:setConfiguration("cfg_animobj_bazar_ferry")
         --char_nubian:setConfiguration("CrocMask_char_nubian_bazar")
	 --api:cutscene_("CrocMask_ferry_to_bazar",0)
      end
   end

   if p_Task == "TreasurerHandTaken" then
      if p_Done then
	 Inventory:addItemSafely("item_hand_treasurer")
	 --iactor_ship_wreck_dead_treasurer_arm:setIgnored(true)
	 iactor_ship_wreck_dead_treasurer:setIgnored(true)
	 animobj_ship_wreck_arm_treasurer:setIgnored(true)
      else
	 Inventory:removeItemSafely("item_hand_treasurer")
	 --iactor_ship_wreck_dead_treasurer_arm:setIgnored(false)
	 iactor_ship_wreck_dead_treasurer:setIgnored(false)
	 animobj_ship_wreck_arm_treasurer:setIgnored(false)
      end
   end

   if p_Task == "KeyPirateChestTaken" then
      if p_Done then
         Inventory:addItemSafely("item_key_pirate_chest")
         --iactor_ship_wreck_key_pirate_chest:setIgnored(true)
	      Inventory:removeItemSafely("item_hand_treasurer")
	      --sound("pirate_hand_open")
	      sound("key_pickup")
      else
         Inventory:removeItemSafely("item_key_pirate_chest")
         --iactor_ship_wreck_key_pirate_chest:setIgnored(false)
      end
   end

   if p_Task == "StoneArmTaken" then
      if p_Done then
         Inventory:addItemSafely("item_stone_arm")
      else
         Inventory:removeItemSafely("item_stone_arm")
      end
   end

   if p_Task == "PirateChestOpened" then
      if p_Done then
         iactor_ship_wreck_pirate_stuff:setIgnored(false)
         Inventory:removeItemSafely("item_key_pirate_chest")
      else
         iactor_ship_wreck_pirate_stuff:setIgnored(true)
         Inventory:addItemSafely("item_key_pirate_chest")
      end
   end

   if p_Task == "PirateStuffTaken" then
      if p_Done then
         Inventory:addItemSafely("item_pirate_flag")
         Inventory:addItemSafely("item_compass_broken")
         Inventory:addItemSafely("item_treasure_dirty")
         Inventory:addItemSafely("item_shirt_hawai")
         iactor_ship_wreck_pirate_stuff:setIgnored(true)
      end
   end

   if p_Task == "StoneArmWrapped" then
      if p_Done then
         Inventory:removeItemSafely("item_shirt_hawai")
         Inventory:removeItemSafely("item_stone_arm")
         Inventory:addItemSafely("item_stone_arm_shirt")
      end
   end

   if p_Task == "SkullTaken" then
      if p_Done then
         Inventory:addItemSafely("item_skull")
      else
         Inventory:removeItemSafely("item_skull")
      end
   end

   if p_Task == "ScissorsGiven" then
      if p_Done then
         Inventory:addItemSafely("item_scissors_blunt")
         -- ToDo: sharpen scissors
         Ankh:addToDoEntry("ID_TODO_04")
      else
         Inventory:removeItemSafely("item_scissors_blunt")
      end
   end

   if p_Task == "ScissorsSharpened" then
      if p_Done then
         Inventory:removeItemSafely("item_scissors_blunt")
         Inventory:addItemSafely("item_scissors_sharp")
      else
         Inventory:removeItemSafely("item_scissors_sharp")
         Inventory:addItemSafely("item_scissors_blunt")
      end
   end

   if p_Task == "NeedleGiven" then
      if p_Done then
         Inventory:removeItemSafely("item_scissors_sharp")
         Inventory:addItemSafely("item_compass_needle")
         -- ToDo remove: sharpen scissors
         Ankh:removeToDoEntry("ID_TODO_04")
      else
         Inventory:removeItemSafely("item_compass_needle")
         Inventory:addItemSafely("item_scissors_sharp")
      end
   end

   if p_Task == "CompassFixed" then
      if p_Done then
         Inventory:addItemSafely("item_compass_fixed")
         Inventory:removeItemSafely("item_compass_broken")
         Inventory:removeItemSafely("item_compass_needle")
         iactor_desert_beach_to_desert_crossing:setConfiguration("CrocMask_CompassFixed_iactor_desert_beach_to_desert_crossing")
         iactor_desert_crossing_to_desert_rock:setIgnored(false)
         iactor_desert_crossing_to_desert_plain:setIgnored(false)
      else
         Inventory:removeItemSafely("item_compass_fixed")
         Inventory:addItemSafely("item_compass_broken")
         Inventory:addItemSafely("item_compass_needle")
         iactor_desert_beach_to_desert_crossing:setConfiguration("cfg_iactor_desert_beach_to_desert_crossing")
         iactor_desert_crossing_to_desert_rock:setIgnored(true)
         iactor_desert_crossing_to_desert_plain:setIgnored(true)
      end
   end

   if p_Task == "CrocPurseTaken" then
      if p_Done then
         api:detachFromBoneSafely("char_granny","item_croc_purse")
         Inventory:addItemSafely("item_croc_purse")
      else
         Inventory:removeItemSafely("item_croc_purse")
         item_croc_purse:setConfiguration("cfg_item_croc_purse")
         api:attachToBoneSafely("char_granny","item_croc_purse","RigLArmPalm")
      end
   end

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- sets the speed when assil is under water
-----------------------------------------------------------------------
function CrocMask:setUnderwaterSpeed()

   CharacterControllerMgr:setWalkAnimation("char_assil", "assil_walk_underwater")
   CharacterControllerMgr:setWalkSpeed("char_assil", 60)
   CharacterControllerMgr:setRunAnimation("char_assil", "assil_walk_underwater")
   CharacterControllerMgr:setRunSpeed("char_assil", 60)

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- if Assil comes to close to the croc he runs away
-----------------------------------------------------------------------
function CrocMask:CrocSnapsAtAssil()

   char_crocodile:blendToPlayOnceAndBack("crocodile_snap")
   char_assil:setYaw(Degree(0))
   --CharacterControllerMgr:setRunSpeed("char_assil", 3300)
   CharacterControllerMgr:run("char_assil", PinaVector3(0,294,424))
   --TimedEventMgr:createTimedEvent("CrocMask:setUnderwaterSpeed()", 1.5)
   MouseIface:blockInput(2.5)
   sound("croc_roar")
end
-----------------------------------------------------------------------

