1. function CreateRandomPortal(user,cell,ent)
  2. if not InfiniDungeon.HandleHook("OnCreateRandomPortal",user,cell,ent) then return end
  3.  
  4. local name = ent:GetName()
  5. if not name then return end
  6.  
  7. local room = GetRoom(ent)
  8.  
  9. if table.getn(ID_DOORS) < 1 then
  10. PopulateDoors()
  11. end
  12.  
  13. local theme = GetTheme(ent)
  14.  
  15. -- Before we do ANY selecting, we need to check if this door is a THEME-CHANGER
  16. local contr = GetDoorController(ent)
  17. if (contr) then
  18. if contr.oldtheme then -- This door is, indeed, a theme-changer. Set the new theme to whatever theme it changes to.
  19. local changetheme=true
  20. if contr.themelogic_name then -- This theme is controlled by logic; let's abide by that logic!
  21. local logic = contr.themelogic_name
  22. changetheme=false
  23.  
  24. --[[if ID_LOGIC_COUNTERS[logic] then
  25. if contr.themelogic_operator=="ltet" then -- "Less Than Equal To"
  26. if ID_LOGIC_COUNTERS[logic] <= tonumber(contr.themelogic_value) then changetheme=true end
  27. end
  28. if contr.themelogic_operator=="gtet" then -- "Greater Than Equal To"
  29. if ID_LOGIC_COUNTERS[logic] >= tonumber(contr.themelogic_value) then changetheme=true end
  30. end
  31. end]]
  32.  
  33. if not user.LogicCounters then user.LogicCounters = {} end
  34. if not user.LogicCounters[logic] then user.LogicCounters[logic] = 0 end
  35. --if user.LogicCounters[logic] then
  36. if contr.themelogic_operator=="ltet" then -- "Less Than Equal To"
  37. if user.LogicCounters[logic] <= tonumber(contr.themelogic_value) then changetheme=true end
  38. end
  39. if contr.themelogic_operator=="gtet" then -- "Greater Than Equal To"
  40. if user.LogicCounters[logic] >= tonumber(contr.themelogic_value) then changetheme=true end
  41. end
  42. --end
  43. end
  44.  
  45. if changetheme then
  46. theme = contr.newtheme -- In theory, this should be all we need to do. How this system is set up should allow seamless transition between themes.
  47. end
  48. end
  49. end
  50.  
  51. local door = ID_THEMEDOORS[theme][math.random(#ID_THEMEDOORS[theme])]
  52. local room2 = string.sub(door:GetName(),2,3)
  53.  
  54. if (room==room2) or (not CanBeLinked(door,cell,ent)) then -- Same room; try to pick a different door!
  55. for j=1,6 do
  56. door = ID_THEMEDOORS[theme][math.random(#ID_THEMEDOORS[theme])]
  57. room2 = GetRoom(door)
  58.  
  59. if (room~=room2) and (CanBeLinked(door,cell,ent)) then
  60. break
  61. end
  62. end
  63. end
  64.  
  65. room = string.gsub(room,"d","")
  66. room2 = string.gsub(room2,"d","")
  67.  
  68. print("ROOM1:"..room,"ROOM2: "..room2)
  69. if room==room2 then
  70. return
  71. end
  72.  
  73. if (not CanBeLinked(door,cell,ent)) then
  74. return
  75. end
  76.  
  77. -- If door A has a forced link, link to its forced counterpart
  78. if HasForcedLink(ent) then
  79. door = GetForcedLink(ent)
  80. end
  81.  
  82. -- If door B has a forced link, link to its forced counterpart
  83. if HasForcedLink(door) then
  84. ent = GetForcedLink(door)
  85. end
  86.  
  87. if ID_NEXTROOM then
  88. local d = DoorsInRoom(ID_NEXTROOM)
  89.  
  90. door = d[math.random(1,#d)]
  91.  
  92. ID_NEXTROOM = nil
  93. end
  94.  
  95. LinkInCell(cell,ent,door)
  96.  
  97. InfiniDungeon.HandleHook("RandomPortalCreated",cell,ent,door)
  98. end