actionunlockcontainerdoor.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. class ActionUnlockShippingContainer: ActionUnlockDoors
  2. {
  3. //custom condition, wrong key unlock attempt is allowed
  4. override bool ActionCondition( PlayerBase player, ActionTarget target, ItemBase item )
  5. {
  6. ContainerLockedBase shipCont;
  7. if (Class.CastTo(shipCont, target.GetObject()))
  8. {
  9. int doorIndex = TranslateLockSelectionIntoDoorIdx(target);
  10. if (doorIndex != -1)
  11. return shipCont.IsDoorLocked(doorIndex);
  12. }
  13. return false;
  14. }
  15. override void OnFinishProgressServer( ActionData action_data )
  16. {
  17. ShippingContainerKeys_ColorBase key = ShippingContainerKeys_ColorBase.Cast(action_data.m_MainItem);
  18. ContainerLockedBase shipCont = ContainerLockedBase.Cast(action_data.m_Target.GetObject());
  19. if (shipCont && key && ((shipCont.GetLockCompatibilityType(shipCont.GetDoorIndex(action_data.m_Target.GetComponentIndex())) & key.GetKeyCompatibilityType()) == 0))
  20. {
  21. key.DestroyKeyServer();
  22. }
  23. else
  24. {
  25. UnlockDoor(action_data.m_Target);
  26. MiscGameplayFunctions.DealAbsoluteDmg(action_data.m_MainItem, APPLIED_DMG);
  27. }
  28. }
  29. override protected void UnlockDoor(ActionTarget target)
  30. {
  31. Building building;
  32. if (Class.CastTo(building, target.GetObject()))
  33. {
  34. int doorIndex = TranslateLockSelectionIntoDoorIdx(target);
  35. if (doorIndex != -1)
  36. {
  37. building.UnlockDoor(doorIndex,false);
  38. }
  39. }
  40. }
  41. //! Returns door idx
  42. protected int TranslateLockSelectionIntoDoorIdx(ActionTarget target)
  43. {
  44. //side1_lock
  45. ContainerLockedBase shipCont;
  46. if (Class.CastTo(shipCont, target.GetObject()))
  47. {
  48. string selectionName = shipCont.GetActionComponentName( target.GetComponentIndex() );
  49. if (selectionName.Contains("_lock"))
  50. return (selectionName.Substring(4,1).ToInt() - 1);
  51. }
  52. return -1;
  53. }
  54. };