SHOTbyGUN

blastdoorV2.0

Dec 31st, 2014
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. -- BlastDoor mod for PrisonArchitect by SHOT(by)GUN 1.1.2015
  2. -- Indendation looks great in Notepad++ but not in here :( sorry!
  3.  
  4. -- variables what are visible for whole script
  5. local doorSpeedMultiplier = 0.2
  6. local currentServo = "None"
  7. local doorOpenStatus = this.Open
  8. local doOpen = true
  9. local currentlyMoving = true
  10.  
  11. -- runtime variables
  12. local timer = 0
  13. local nextTimeout = 1
  14.  
  15.  
  16. function Create()
  17.    
  18.     -- this will throw an error = opens debug console! (remember to comment out on release)
  19.     -- print()
  20. end
  21.  
  22. function Update(timePassed)
  23.  
  24.     timer = timer + timePassed;
  25.     if timer > nextTimeout then
  26.         IntervalUpdate(timer)
  27.         timer = 0
  28.     end
  29.    
  30.     if currentServo == "None" then
  31.         -- no servo detected, force door to open
  32.         doOpen = true
  33.     else
  34.    
  35.         -- we have servo
  36.        
  37.         local servoIsTriggered = (tonumber(currentServo.Triggered) > 0)
  38.        
  39.         if servoIsTriggered then
  40.             doOpen = true
  41.         else
  42.             doOpen = false
  43.         end
  44.        
  45.         -- if openOnTrigger is specified to false, then reverse functionality
  46.         if not currentServo.OpenOnTrigger then
  47.             doOpen = not doOpen
  48.         end
  49.     end
  50.    
  51.     moveDoor(timePassed)
  52.  
  53. end
  54.  
  55.  
  56. function IntervalUpdate(timer)
  57.  
  58.     -- Check if we have servo
  59.     -- following will find the closest servo (if available)
  60.     local nearbyDoorServos = Object.GetNearbyObjects("Servo", 2.0)
  61.     local findClosestDistance = 9999
  62.     local findClosest = "None"
  63.     for name, distance in pairs( nearbyDoorServos ) do
  64.         if tonumber(distance) < findClosestDistance then
  65.             findClosest = name
  66.             findClosestDistance = tonumber(distance)
  67.         end
  68.     end
  69.     if findClosest == "None" then
  70.         -- could not find servo
  71.         this.Tooltip = "tooltip_blastdoor_servomissing"
  72.     else
  73.         -- we found servo
  74.         this.Tooltip = ""
  75.     end
  76.    
  77.     -- finally update currentServo
  78.     currentServo = findClosest
  79.    
  80.     -- make cling cling sound when door is moving
  81.     if currentlyMoving then
  82.         Object.Sound( "SolitaryDoor", "EndOpen" )
  83.     end
  84.  
  85. end
  86.  
  87. -- in this function we force the door to be in our wanted status
  88. function moveDoor(timePassed)
  89.  
  90.  
  91.     -- try to fix runtime error doorOpenStatus (a nil value)
  92.      if type(doorOpenStatus) ~= "number" then
  93.         doorOpenStatus = 0
  94.     end
  95.  
  96.     -- increase/decrease doorOpenStatus
  97.     if doOpen then
  98.         doorOpenStatus = doorOpenStatus + (timePassed * doorSpeedMultiplier)
  99.     else
  100.         doorOpenStatus = doorOpenStatus - (timePassed * doorSpeedMultiplier)
  101.     end
  102.    
  103.     -- do not open too much
  104.     if doorOpenStatus > 1 then
  105.         doorOpenStatus = 1
  106.     end
  107.    
  108.     -- do not close too much
  109.     if doorOpenStatus < 0 then
  110.         doorOpenStatus = 0
  111.     end
  112.    
  113.     -- funfact: getting this.Mode returns LockedOpen, LockedShut or Normal, but you cannot set the value with string, it has to be integer 0, 1 or 2
  114.    
  115.     if doorOpenStatus == 0 then
  116.         -- door is shut
  117.        
  118.         if currentlyMoving then
  119.             Object.Sound( "SolitaryDoor", "EndClose" )
  120.             currentlyMoving = false
  121.         end
  122.        
  123.     elseif doorOpenStatus == 1 then
  124.         -- door is open
  125.        
  126.         if currentlyMoving then
  127.             Object.Sound( "SolitaryDoor", "EndClose" )
  128.             currentlyMoving = false
  129.         end
  130.        
  131.     else
  132.         -- door is not open or closed
  133.        
  134.         if not currentlyMoving then
  135.             currentlyMoving = true
  136.         end
  137.        
  138.        
  139.     end
  140.    
  141.     -- finally set door "openess" to the specified value
  142.     this.Open = doorOpenStatus
  143.  
  144. end
Advertisement
Add Comment
Please, Sign In to add comment