KBM-Quine

doorUtils pre-release v2

Dec 17th, 2020 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.18 KB | None | 0 0
  1. -------------------------------------------
  2. --[[  doorUtils.lua v1.0 by KBM-Quine  ]]--
  3. --[[     with some code help from:     ]]--
  4. --[[         Enjl and MrDoubleA        ]]--
  5. -------------------------------------------
  6. local effectconfig = require("base/game/effectconfig")
  7. local doorUtils = {}
  8.  
  9. doorUtils.defaults = {
  10.     usePluralMacguffins = true,
  11.     macguffin = "star",
  12.     macguffinPlural = "stars",
  13.     macguffinMessage = "You need [number] [macguffin] to enter.",
  14.     macguffinMessagePlural = "You need [number] [macguffinplural] to enter.",
  15.     macguffinIcon = nil,
  16.     centerMacguffinOnBackground = false,
  17.     macguffinAlignX = 0.5,
  18.     macguffinAlignY = 1,
  19.     macguffinOffsetX = 0,
  20.     macguffinOffsetY = 0,
  21.     lockIcon = nil,
  22.     centerLockOnBackground = false,
  23.     lockAlignX = 0,
  24.     lockAlignY = 0,
  25.     lockOffsetX = 0,
  26.     lockOffsetY = 0,
  27.     effectOffsetX = 0,
  28.     effectOffsetY = 0,
  29.     autoResizeWarp = false,
  30.     autoRegisterBaseDoors = true,
  31. }
  32.  
  33. if doorUtils.defaults.autoRegisterBaseDoors then
  34.     BGO.config[87].door = BGO.config[87].door or true
  35.     BGO.config[88].door = BGO.config[88].door or true
  36.     BGO.config[107].door = BGO.config[107].door or true
  37.     BGO.config[141].door = BGO.config[141].door or true
  38. end
  39.  
  40. function effectconfig.onTick.TICK_DOORUTILS(v)
  41.     v.timer = 2
  42.     v.frSpeed = v.frSpeed or v.framespeed
  43.     v.framespeed = 0
  44.     v.doorTimer = v.doorTimer or v.lifetime
  45.     v.doorTimer = v.doorTimer - 1
  46.     v.animDir = v.animDir or 1
  47.     if v.animDir == 1 then
  48.         if v.doorTimer%v.frSpeed == 0 and v.doorTimer > math.floor(v.lifetime*0.667) and v.animationFrame ~= v.frames - 1 then
  49.             v.animationFrame = v.animationFrame + 1
  50.         elseif v.animationFrame == v.frames - 1 then
  51.             v.animationFrame = v.frames - 1
  52.         end
  53.         if v.doorTimer < math.floor(v.lifetime*0.667) then
  54.             v.animDir = 0
  55.         end
  56.     end
  57.     if v.animDir == 0 then
  58.         v.animationFrame = v.frames - 1
  59.         if v.doorTimer < math.ceil(v.lifetime*0.333) then
  60.             v.animationFrame = v.frames - 2
  61.             v.animDir = -1
  62.         end
  63.     end
  64.     if v.animDir == -1 then
  65.         if v.doorTimer%v.frSpeed == 0 then
  66.             v.animationFrame = v.animationFrame - 1
  67.         end
  68.     end
  69.     if v.animationFrame == -1 then
  70.         v.timer = 0
  71.     end
  72. end
  73.  
  74. function doorUtils.onInitAPI()
  75.     registerEvent (doorUtils, "onStart", "onStart", true)
  76.     registerEvent (doorUtils, "onDraw", "onDraw", true)
  77.     registerEvent (doorUtils, "onMessageBox", "onMessageBox", true)
  78.    
  79. end
  80.  
  81. function doorUtils.onStart()
  82.     if not isOverworld then
  83.         for  _,v in ipairs(BGO.get())  do
  84.             for  _,wEnt in ipairs(Warp.getIntersectingEntrance(v.x, v.y, v.x + v.width + 8, v.y + v.height + 8))  do
  85.                 for  _,wExi in ipairs(Warp.getIntersectingExit(v.x, v.y, v.x + v.width + 8, v.y + v.height + 8))  do
  86.                     if wEnt ~= nil and BGO.config[v.id].door and v.width > 32 then
  87.                         if BGO.config[v.id].autoResizeWarp or doorUtils.defaults.autoResizeWarp then
  88.                             wEnt.entranceX = v.x + 16
  89.                             wEnt.entranceWidth = v.width - 32
  90.                         end
  91.                     end
  92.                     if wExi ~= nil and BGO.config[v.id].door and v.width > 32 then
  93.                         if BGO.config[v.id].autoResizeWarp or doorUtils.defaults.autoResizeWarp then
  94.                             wExi.exitX = v.x + 16
  95.                             wExi.exitWidth = v.width - 32
  96.                         end
  97.                     end
  98.                 end
  99.             end
  100.         end
  101.         for  _,v in ipairs(BGO.get({98, 160}))  do
  102.             if v.id == 160 then
  103.                 for  _,b in ipairs(BGO.getIntersecting(v.x, v.y, v.x + v.width + 8, v.y + v.height + 8))  do
  104.                     for  _,w in ipairs(Warp.getIntersectingEntrance(v.x, v.y, v.x + v.width + 8, v.y + v.height + 8))  do
  105.                         if b ~= nil and BGO.config[b.id].door then
  106.                             v.data.macguffinIconIndex = v.data.macguffinIconIndex or w.idx
  107.                             if b.id ~= 160 then
  108.                                 if BGO.config[b.id].macguffinIcon ~= nil or doorUtils.defaults.macguffinIcon ~= nil then
  109.                                     v.id = BGO.config[b.id].macguffinIcon or doorUtils.defaults.macguffinIcon
  110.                                     v.data.IconWidth =  BGO.config[BGO.config[b.id].macguffinIcon].width or BGO.config[doorUtils.defaults.macguffinIcon].width
  111.                                     v.data.IconHeight = BGO.config[BGO.config[b.id].macguffinIcon].height or BGO.config[doorUtils.defaults.macguffinIcon].height
  112.                                 end
  113.                                 if BGO.config[b.id].centerMacguffinOnBackground or doorUtils.defaults.centerMacguffinOnBackground then
  114.                                     v.x = (b.x + b.width*0.5) - (v.data.IconWidth or v.width)*(BGO.config[b.id].macguffinAlignX or doorUtils.defaults.macguffinAlignX) + (BGO.config[b.id].macguffinOffsetX or doorUtils.defaults.macguffinOffsetX)
  115.                                     v.y = (b.y + b.height*0.5) - (v.data.IconHeight or v.height)*(BGO.config[b.id].macguffinAlignY or doorUtils.defaults.macguffinAlignY) + (BGO.config[b.id].macguffinOffsetY or doorUtils.defaults.macguffinOffsetY)
  116.                                 else
  117.                                     v.x = (w.entranceX + w.entranceWidth*0.5) - (v.data.IconWidth or v.width)*(BGO.config[b.id].macguffinAlignX or doorUtils.defaults.macguffinAlignX) + (BGO.config[b.id].macguffinOffsetX or doorUtils.defaults.macguffinOffsetX)
  118.                                     v.y = w.entranceY - (v.data.IconHeight or v.height)*(BGO.config[b.id].macguffinAlignY or doorUtils.defaults.macguffinAlignY) + (BGO.config[b.id].macguffinOffsetY or doorUtils.defaults.macguffinOffsetY)
  119.                                 end
  120.                             end
  121.                         end
  122.                     end
  123.                 end
  124.             end
  125.             if v.id == 98 then
  126.                 for  _,b in ipairs(BGO.getIntersecting(v.x, v.y, v.x + v.width + 8, v.y + v.height + 8))  do
  127.                     for  _,w in ipairs(Warp.getIntersectingEntrance(v.x, v.y, v.x + v.width + 8, v.y + v.height + 8))  do
  128.                         if b ~= nil and BGO.config[b.id].door then
  129.                             v.data.lockIconIndex = v.data.lockIconIndex or w.idx
  130.                             if b.id ~= 98 then
  131.                                 if BGO.config[b.id].lockIcon ~= nil or doorUtils.defaults.lockIcon ~= nil then
  132.                                     v.id = BGO.config[b.id].lockIcon or doorUtils.defaults.lockIcon
  133.                                     v.data.IconWidth = BGO.config[BGO.config[b.id].lockIcon].width or BGO.config[doorUtils.defaults.lockIcon].width
  134.                                     v.data.IconHeight = BGO.config[BGO.config[b.id].lockIcon].height or BGO.config[doorUtils.defaults.lockIcon].height
  135.                                 end
  136.                                 if BGO.config[b.id].centerlockonbackground or doorUtils.defaults.centerLockOnBackground then
  137.                                     v.x = (b.x + b.width*0.5) - (v.data.IconWidth or v.width)*(BGO.config[b.id].lockAlignX or doorUtils.defaults.lockAlignX) + (BGO.config[b.id].lockOffsetX or doorUtils.defaults.lockOffsetX)
  138.                                     v.y = (b.y + b.height*0.5) - (v.data.IconHeight or v.height)*(BGO.config[b.id].lockAlignY or doorUtils.defaults.lockAlignY) + (BGO.config[b.id].lockOffsetY or doorUtils.defaults.lockOffsetY)
  139.                                 else
  140.                                     v.x = w.entranceX + w.entranceWidth*(BGO.config[b.id].lockAlignX or doorUtils.defaults.lockAlignX) + (BGO.config[b.id].lockOffsetX or doorUtils.defaults.lockOffsetX)
  141.                                     v.y = w.entranceY + w.entranceHeight*(BGO.config[b.id].lockAlignY or doorUtils.defaults.lockAlignY) + (BGO.config[b.id].lockOffsetY or doorUtils.defaults.lockOffsetX)
  142.                                 end
  143.                             end
  144.                         end
  145.                     end
  146.                 end
  147.             end
  148.         end
  149.     end
  150. end
  151.  
  152. function doorUtils.resolveStrings(message, number, BGOID)
  153.     local singular = BGO.config[BGOID].macguffin or doorUtils.defaults.macguffin
  154.     local plural = BGO.config[BGOID].macguffinPlural or doorUtils.defaults.macguffinPlural
  155.     message = string.gsub(message, "%[macguffin%]", singular)
  156.     message = string.gsub(message, "%[macguffinplural%]", plural)
  157.     message = string.gsub(message, "%[number%]", number)
  158.     return message
  159. end
  160.  
  161. function doorUtils.onDraw()
  162.     if not isOverworld then
  163.         for  _,v in ipairs(BGO.get())  do
  164.             if v.data.macguffinIconIndex ~= nil then
  165.                 if Warp(v.data.macguffinIconIndex).starsRequired <= mem(0x00B251E0, FIELD_WORD) and not v.isHidden then
  166.                     v.isHidden = true
  167.                     v.layer = ""
  168.                 end
  169.                 if Warp(v.data.macguffinIconIndex).starsRequired > mem(0x00B251E0, FIELD_WORD) and v.isHidden then -- allow relocking
  170.                     v.isHidden = false
  171.                     v.layer = Warp(v.data.macguffinIconIndex).layer
  172.                 end
  173.             end
  174.             if v.data.lockIconIndex ~= nil then
  175.                 if not Warp(v.data.lockIconIndex).locked and not v.isHidden then
  176.                     v.isHidden = true
  177.                     v.layer = ""
  178.                 end
  179.                 if Warp(v.data.lockIconIndex).locked and v.isHidden then -- allow relocking
  180.                     v.isHidden = false
  181.                     v.layer = Warp(v.data.lockIconIndex).layer
  182.                 end
  183.             end
  184.         end
  185.         for _, p in ipairs(Player.get()) do
  186.             if p.ForcedAnimationState == 7 and p.ForcedAnimationTimer == 0 then
  187.                 local warpID = 0
  188.                 for  _,w in ipairs(Warp.getIntersectingEntrance(p.x, p.y, p.x + p.width, p.y + p.height))  do
  189.                     for  _,b in ipairs(BGO.getIntersecting(w.entranceX, w.entranceY, w.entranceX + w.entranceWidth, w.entranceY + w.entranceHeight))  do
  190.                         if b.data.macguffinIconIndex == nil and b.data.lockIconIndex == nil and BGO.config[b.id].door then
  191.                             warpID = w.idx
  192.                             local effect = BGO.config[b.id].effect
  193.                             local spawned = false
  194.                             if effect ~= nil then
  195.                                 Effect.spawn(effect, b.x + (BGO.config[b.id].effectOffsetX or doorUtils.defaults.effectOffsetX), b.y + (BGO.config[b.id].effectOffsetY or doorUtils.defaults.effectOffsetY))
  196.                                 spawned = true
  197.                             end
  198.                            
  199.                             for  _,e in ipairs(Effect.getIntersecting(w.entranceX, w.entranceY, w.entranceX + w.entranceWidth, w.entranceY + w.entranceHeight))  do
  200.                                 if e.id == 54 or e.id == 55 or e.id == 59 or e.id == 103 then
  201.                                     if spawned then
  202.                                         e.timer = 0
  203.                                     end
  204.                                 end
  205.                                 if effect == nil then
  206.                                     if e.id == 54 or e.id == 55 or e.id == 59 or e.id == 103 then
  207.                                         if spawned then
  208.                                             e.timer = 0
  209.                                         end
  210.                                         e.x = b.x + (BGO.config[b.id].effectOffsetX or doorUtils.defaults.effectOffsetX)
  211.                                         e.y = b.y + (BGO.config[b.id].effectOffsetY or doorUtils.defaults.effectOffsetY)
  212.                                         e.width = Graphics.sprites.effect[e.id].img.width -- make the ghost house door effect resizable
  213.                                     end
  214.                                 end
  215.                             end
  216.                         end
  217.                     end
  218.                     for  _,b in ipairs(BGO.getIntersecting(w.exitX, w.exitY, w.exitX + w.exitWidth, w.exitY + w.exitHeight))  do -- spawn an effect at the exit, to mimic vanilla behaviour.
  219.                         if b.data.macguffinIconIndex == nil and b.data.lockIconIndex == nil and BGO.config[b.id].door then
  220.                             local effect = BGO.config[b.id].effect
  221.                             local spawned = false
  222.                             if effect ~= nil then
  223.                                 Effect.spawn(effect, b.x + (BGO.config[b.id].effectOffsetX or doorUtils.defaults.effectOffsetX), b.y + (BGO.config[b.id].effectOffsetY or doorUtils.defaults.effectOffsetY))
  224.                                 spawned = true
  225.                             end
  226.                            
  227.                            
  228.                             for  _,e in ipairs(Effect.getIntersecting(w.exitX, w.exitY, w.exitX + w.exitWidth, w.exitY + w.exitHeight))  do
  229.                                 if e.id == 54 or e.id == 55 or e.id == 59 or e.id == 103 then
  230.                                     if spawned then
  231.                                         e.timer = 0
  232.                                     end
  233.                                 end
  234.                                 if effect == nil then
  235.                                     if e.id == 54 or e.id == 55 or e.id == 59 or e.id == 103 then
  236.                                         if spawned then
  237.                                             e.timer = 0
  238.                                         end
  239.                                         e.x = b.x + (BGO.config[b.id].effectOffsetX or doorUtils.defaults.effectOffsetX)
  240.                                         e.y = b.y + (BGO.config[b.id].effectOffsetY or doorUtils.defaults.effectOffsetY)
  241.                                         e.width = Graphics.sprites.effect[e.id].img.width -- make the ghost house door effect resizable
  242.                                     end
  243.                                
  244.                                 end
  245.                             end
  246.                         end
  247.                     end
  248.                 end
  249.             end
  250.         end
  251.     end
  252. end
  253.  
  254. function doorUtils.onMessageBox(event, message)
  255.     if not isOverworld then
  256.         if string.find(message, "You need %d+ stars? to enter%.") then
  257.             local BGOID
  258.             local newMessage
  259.             local number = string.match(message, "%d+")
  260.             local usePlural
  261.             local plural
  262.             local singular
  263.             for _, p in ipairs(Player.get()) do
  264.                 for  _,w in ipairs(Warp.getIntersectingEntrance(p.x, p.y, p.x + p.width, p.y + p.height))  do
  265.                     for  _,b in ipairs(BGO.getIntersecting(w.entranceX, w.entranceY, w.entranceX + w.entranceWidth, w.entranceY + w.entranceHeight))  do
  266.                         if w.starsRequired > 0 then
  267.                             if b.data.macguffinIconIndex == nil and BGO.config[b.id].door then
  268.                                 BGOID = b.id
  269.                             end
  270.                         end
  271.                     end
  272.                 end
  273.             end
  274.             if BGO.config[BGOID].usePluralMacguffins ~= nil then
  275.                 usePlural = BGO.config[BGOID].usePluralMacguffins
  276.             else
  277.                 usePlural = doorUtils.defaults.usePluralMacguffins
  278.             end
  279.             if tonumber(number) > 1 and usePlural then
  280.                 newMessage = doorUtils.resolveStrings(BGO.config[BGOID].macguffinMessagePlural or doorUtils.defaults.macguffinMessagePlural, number, BGOID)
  281.             else
  282.                 newMessage = doorUtils.resolveStrings(BGO.config[BGOID].macguffinMessage or doorUtils.defaults.macguffinMessage, number, BGOID)
  283.             end
  284.             if not string.find(newMessage, "You need %d+ stars? to enter%.") then
  285.                 event.cancelled = true
  286.             else
  287.                 newMessage = nil
  288.             end
  289.             if newMessage ~= nil then
  290.                 Text.showMessageBox(newMessage)
  291.             end
  292.         end
  293.     end
  294. end
  295.  
  296. return doorUtils
  297.  
  298. --Someone told me someone's there. I lost myself, you're always moving.
Add Comment
Please, Sign In to add comment