// this is the reference script for all chests. 

ON INIT {
 SETNAME [description_chest]
 INVENTORY CREATE
 SET §open 0
 SET §unlock 0
 SET £key "NOKEY"
 SET §lockpickability 100
 SET £type "chest_metal"
 SETTRAP -1
 SET §trap_lockpickability 100
 SET §trapped 0
 SETPLATFORM ON
 SET §breakattempt 0
 SET §wrong_snd 0
 ACCEPT
}

ON GAME_READY {
 ANCHOR_BLOCK ON
 ACCEPT
}

ON INITEND {
 IF ( §trap_lockpickability != 100 )
 {
  SET §trapped 1
 }
 LOADANIM ACTION1 "chest_close"
 LOADANIM ACTION2 "chest_open"
 IF (£type == "chest") {
    INVENTORY SKIN "ingame_inventory_chest"
    SET_MATERIAL WOOD
    ACCEPT
  }
  IF (£type == "chest_1") {
    INVENTORY SKIN "ingame_inventory_chest"
    SET_MATERIAL WOOD
    ACCEPT
  }
  IF (£type == "chest_2") {
    INVENTORY SKIN "ingame_inventory_chest"
    SET_MATERIAL WOOD
    ACCEPT
  }
  IF (£type == "chest_metal") {
    INVENTORY SKIN "ingame_inventory_chest_metal"
    SET_MATERIAL METAL
    ACCEPT
  }
  IF (£type == "cupboard") {
   INVENTORY SKIN "ingame_inventory_chest"
   SET_MATERIAL WOOD
   LOADANIM ACTION1 "cupboard_close"
   LOADANIM ACTION2 "cupboard_open"
   USE_MESH "CUPBOARD_1\\CUPBOARD_1.teo"
   ACCEPT
  }
 ACCEPT
}
ON LOAD {
 IF (£type == "cupboard") {
  USE_MESH "CUPBOARD_1\\CUPBOARD_1.teo"
  ACCEPT
 }
 ACCEPT
}
ON INVENTORY2_OPEN {
HEROSAY -D OPEN
  IF (§open == 0) {
    IF (§unlock == 1) {
      PLAY "chestopen1"
      PLAYANIM ACTION2
      SET §open 1
      ACCEPT
    }
   SPEAK -p [player_locked] nop
   PLAY "door_locked"
   REFUSE
  }
 ACCEPT
}

ON INVENTORY2_CLOSE {
HEROSAY -D CLOSE
 PLAYANIM ACTION1
 SET §open 0
 IF (£type == "cupboard") {
  PLAY "cupboard_close"
  ACCEPT 
 }
 PLAY "chestclose"
 ACCEPT
}

ON LOCKPICKED
{
 IF ( §trapped == 2 )
 {
  IF ( §unlock == 1 )
  {
   SENDEVENT TRAP_DISARMED SELF ""
  }
  SET §unlock 1
  SET §trapped 1
 }
 ACCEPT
}

ON SPELLCAST {
 IF (§trapped == 0) ACCEPT
 IF (^DIST_PLAYER > 400) ACCEPT
 //IF (^$PARAM1 == DETECT_TRAP) {
 // SET #TMP ~^#PARAM2~
 // MUL #TMP 10
 // IF (#TMP < ^SETTRAP) ACCEPT
 // SETTRAP 1
 // ACCEPT
 //}
IF (^$PARAM1 == DISARM_TRAP) {
  SET #TMP ~^#PARAM2~
  MUL #TMP 10
  IF (#TMP < §trap_lockpickability) {
   SPEAK -p [player_not_skilled_enough] NOP
   play "magic_fizzle"
   ACCEPT
  }
  SENDEVENT TRAP_DISARMED SELF ""
 }
 ACCEPT
}

ON TRAP_DISARMED
{
 IF (§trapped == 0) ACCEPT
 TIMERpouet 1 1 HALO -f
 HALO -os 30
 PLAY "Trap_Disarmed"
 SET §trapped 0
 SETTRAP -1
 ACCEPT
}

ON COMBINE 
{
 IF (^$param1 ISCLASS "lockpicks") 
 {
  IF ( §trapped == 1 )
  {
   IF ( §unlock == 1 )
   {
    SET §trapped 2
    SET §unlock 0
    SET §lockpickability §trap_lockpickability
   }
  }
 }
//#lockpick
//---------------------------------------------------------------------------
// LOCKPICK INCLUDE START ---------------------------------------------------
//---------------------------------------------------------------------------
 IF (^$param1 ISCLASS "lockpicks") 
 {
  SET £tools ^$param1
  IF (§unlock == 1) ACCEPT
  SET_INTERACTIVITY NONE
  SENDEVENT CUSTOM ~£tools~ "INTERNO"
  PLAY "lockpicking"
  TIMERcheckdist 0 1 GOTO CHECK_PLAYER_DIST
  TIMERpick 1 3 GOTO BACK_FROM_PICKLOCK
  ACCEPT

>>CHECK_PLAYER_DIST
  IF (^DIST_PLAYER > 400) 
  {
   SENDEVENT CUSTOM ~£tools~ "INTERYES"
   TIMERcheckdist OFF
   TIMERpick OFF
   SET_INTERACTIVITY YES
  }
  ACCEPT

>>BACK_FROM_PICKLOCK
  TIMERcheckdist OFF
  SENDEVENT CUSTOM ~£tools~ "INTERYES"
  SET_INTERACTIVITY YES
  SETMAINEVENT MAIN
  IF (§lockpickability == 100) 
  {
   SPEAK -p [player_off_impossible] NOP
   ACCEPT
  }
  IF (^PLAYER_SKILL_MECANISM < §lockpickability)
  {
   SPEAK -p [player_picklock_impossible] NOP
   GOTO DAMAGE_TOOLS
  }
  SET §chance ~^PLAYER_SKILL_MECANISM~
  DEC §chance §lockpickability
  INC §chance 20
  RANDOM ~§chance~ 
  {
   SPEAK -p [player_picklock_succeed] NOP
   SET §unlock 1
   PLAY "door_unlock"
   ADDXP 150
   SENDEVENT LOCKPICKED SELF ""
   ACCEPT
  }
  SPEAK -p [player_picklock_failed] NOP
>>DAMAGE_TOOLS
  SENDEVENT LOCKPICKED SELF ""
  SENDEVENT CUSTOM ~£tools~ "DAMAGE"
  PLAY "door_locked"
  ACCEPT
 }
//---------------------------------------------------------------------------
// LOCKPICK INCLUDE END -----------------------------------------------------
//---------------------------------------------------------------------------

//#
 IF (^$PARAM1 ISIN £key ) 
 {
herosay -d "KEY USED !!!"
  IF (§unlock == 1) 
  {
   SET §unlock 0
   PLAY "door_lock"
  }
  ELSE 
  {
>>UNLOCK
   SET §unlock 1
   PLAY "door_unlock"
  }
  ACCEPT
 }
 IF (§wrong_snd == 1) ACCEPT
 SET §wrong_snd 1
 TIMERwrgsnd 1 1 SET §wrong_snd 0
 PLAY "door_wrong_key"
 ACCEPT
}

ON HIT {
 IF (^SENDER != PLAYER) ACCEPT
 IF( §breakattempt > 0 )
 {
  DEC §breakattempt 1
  HEROSAY -d "breakattempt"
  ACCEPT
 }
 SET §breakattempt 3
 HEROSAY -d "wontbreak"
 IF (§speak == 1) ACCEPT
 SET §speak 1
 SPEAK -p [player_wontbreak] SET §speak 0
 ACCEPT  
}