-- Conveyor For Rollout Rocks, by Sirexer(Discord, Youtube) --[[description}} "This lua script, introduces conveyor support for Rollout rocks." "Currently rollout rocks do not move on conveyors, so I made this." "Concole command: cfrr_enable, stops/runs a lua script, default - On." "You can also specify in the Level header whether lua should work on your map or not." "Note: If you specify this in the level header, it will ignore the cfrr_enable command." Example: Level 01 LevelName = Test TypeOfLevel = coop LUA.cfrr_enable = true -- or Level 01 LevelName = Test TypeOfLevel = coop LUA.cfrr_enable = false --]] --[[Lua Code]] local addoninfo = { -- Addon information name = "Conveyor For Rollout Rocks", author = "Sirexer", version = 1, subversion = 2 } if not CFRR -- If lua has not been added then add it or addoninfo.version == CFRR.addoninfo.version and addoninfo.subversion > CFRR.addoninfo.subversion or addoninfo.version > CFRR.addoninfo.version -- If the lua is newer than the current lua then update it local updated = false -- Has the lua been updated? if CFRR and addoninfo.version == CFRR.addoninfo.version and addoninfo.subversion > CFRR.addoninfo.subversion or CFRR and addoninfo.version > CFRR.addoninfo.version print("\x83".."NOTICE".."\x80: ".."Replacing CFRR with a new version.") print("Uplated version: "..addoninfo.version.."."..addoninfo.subversion) print("Outdated version: "..CFRR.addoninfo.version.."."..CFRR.addoninfo.subversion) updated = true -- Lua has been updated else rawset(_G, "CFRR", {}) -- Create table if it does not exist end local cfrr = CFRR -- copy of the global table cfrr.addoninfo = addoninfo -- Add addon information to the global table cfrr.Data = {num = {}} -- Table of conveyor sectors if cfrr.enabled == nil then cfrr.enabled = true end -- Variable responsible for lua operation COM_AddCommand("cfrr_enable", function(player, arg) -- Toggle CFRR.enabled variable if arg -- The first argument in the console local str = string.lower(arg) -- String argument local int = tonumber(arg) -- Integer argument if int != nil -- Skip if Integer argument does not exist if int > 0 then cfrr.enabled = true else cfrr.enabled = false end return end if str == "off" or str == "no" or str == "disabled" or str == "disable" or str == "false" cfrr.enabled = false elseif str == "on" or str == "yes" or str == "enabled" or str == "enable" or str == "true" cfrr.enabled = true end -- With a positive value, the lua will rollout rocks will move on conveyors -- otherwise if negative, they will not move -- Note: The CFRR.enabled variable will have no effect -- if the Level header has LUA.cfrr_enable return end local var = "On" -- Gives the value as On/Off, not true/false if cfrr.enabled != true then var = "Off" end CONS_Printf(player, "\"cfrr_enable\" is \""..var.."\" default is \"On\"") -- Current value end, COM_ADMIN) -- Only admins are allowed to use the command local ShouldItWork = function(arg) -- Checks whether the script should work or not if cfrr.addoninfo.version != addoninfo.version or cfrr.addoninfo.subversion != addoninfo.subversion return false -- If a newer version of lua is loaded, it does not run end if arg == "rolloutrockmove" -- This argument checks if the rollout rocks should move local cfrr_str = mapheaderinfo[gamemap].cfrr_enable local cfrr_int = tonumber(mapheaderinfo[gamemap].cfrr_enable) if cfrr_int != nil if cfrr_int > 0 return true --[[LUA.cfrr_enable = 1]] else return false --[[LUA.cfrr_enable = 0]] end end if cfrr_str string.lower(cfrr_str) if cfrr_str == "off" or cfrr_str == "no" or cfrr_str == "disabled" or cfrr_str == "disable" or cfrr_str == "false" return false --[[LUA.cfrr_enable = Off]] elseif str == "on" or str == "yes" or str == "enabled" or str == "enable" or str == "true" return true --[[LUA.cfrr_enable = On]] end end if cfrr.enabled == false then return false end --[[cfrr_enable 0]] end return true end local IsOnGround = function(mo, z) -- Checks if Rollout rock is on the surface. local int if z == nil then return false end if not (mo.flags2 & MF2_OBJECTFLIP) -- If gravity is normal int = mo.z/FRACUNIT-z/FRACUNIT elseif (mo.flags2 & MF2_OBJECTFLIP) -- If inverted, consider the height of the object int = (mo.z+mo.height)/FRACUNIT-z/FRACUNIT end if int <= 16 and int >= -16 return true else return false end -- Note: If the conveyor sector has a slope, -- the distance between the objectand the surface cannot be exactly zero -- when the object is actually on the surface end local GetPositive = function(int) -- Function that returns a positive number if int < 0 then return -int end return int end cfrr.loadconveyors = function() -- Reset all conveyor sectors after map load if ShouldItWork() == false then return end -- Check for lua script version cfrr.Data = {num = {}} -- Reseting Data local num = 0 -- How many total conveyor sectors for sector in sectors.iterate do -- Checking all sectors if (sector.specialflags & SSF_CONVEYOR) -- Is the conveyor sector num = $ + 1 local t = { sector = sector, floor = { x = sector.floorxoffset, y = sector.flooryoffset, cmomx = 0, -- X-axis momentum floor cmomy = 0 -- Y-axis momentum floor }, ceiling = { x = sector.ceilingxoffset, y = sector.ceilingyoffset, cmomx = 0, -- X-axis momentum ceiling cmomy = 0 -- Y-axis momentum ceiling } } cfrr.Data[#sector] = t -- Inserting data into the table cfrr.Data.num[num] = cfrr.Data[#sector] -- And its order in the table end end end addHook("MapLoad", cfrr.loadconveyors) if updated == true -- Getting new data of conveyor sectors during update if gamestate == GS_LEVEL cfrr.loadconveyors() end updated = false end cfrr.updateconveyors = function() -- Updates conveyor sectors if CFRR != cfrr then rawset(_G, "CFRR", cfrr) end -- If the global table was deleted, restore it from the copy if ShouldItWork("updateconveyors") == false then return end if leveltime > 0 for i = 1, #cfrr.Data.num do -- Update all conveyor sectors local sector = cfrr.Data.num[i].sector -- Сonveyor sector local floor = cfrr.Data[#sector].floor -- Bottom(floor) of the sector floor.cmomx = -(sector.floorxoffset - floor.x) -- Getting X-axis momentum floor floor.cmomy = (sector.flooryoffset - floor.y) -- Getting Y-axis momentum floor floor.x = sector.floorxoffset -- Save the current X-position offset floor.y = sector.flooryoffset -- And Y-position offset of the floor local ceiling = cfrr.Data[#sector].ceiling -- Top(ceiling) of the sector ceiling.cmomx = -(sector.ceilingxoffset - ceiling.x) -- Getting X-axis momentum ceiling ceiling.cmomy = (sector.ceilingyoffset - ceiling.y) -- Getting Y-axis momentum ceiling ceiling.x = sector.ceilingxoffset -- Save the current X-position offset ceiling.y = sector.ceilingyoffset -- And Y-position offset of the ceiling end end end addHook("ThinkFrame", cfrr.updateconveyors) cfrr.rolloutrockmove = function(mo) -- Movement of rollout rocks on conveyors if ShouldItWork("rolloutrockmove") == false then return end -- Check for lua script version and other variables if mo.valid and leveltime > 0 -- ThinkFrame hook should run after MapLoad hook if mo.cmomx == nil then mo.cmomx = 0 end -- Adding the conveyor speed if mo.cmomy == nil then mo.cmomy = 0 end -- for the object to userdata local sss -- Data from the CFRR.Data table local sector -- Сonveyor sector local flat -- The surface must move the rollout rock local z -- Conveyor sector height local touchsector = P_MobjTouchingSectorSpecialFlag(mo, SSF_CONVEYOR) -- Sector that touched rollout rock local controlsector -- FOF Sector Manager if not (mo.flags2 & MF2_OBJECTFLIP) -- Rollout Rock Gravity is normal if mo.floorrover then controlsector = mo.floorrover.sector end if controlsector and (controlsector.specialflags & SSF_CONVEYOR) -- Is Rollout rock located above the FOF? sector = controlsector flat = "ceiling" else -- If not sector = touchsector flat = "floor" end if sector then z = P_FloorzAtPos(mo.x, mo.y, mo.z, mo.height) end -- Get the absolute height else -- Rollout Rock Gravity is flipped if mo.ceilingrover then controlsector = mo.ceilingrover.sector end if controlsector and (controlsector.specialflags & SSF_CONVEYOR) -- Is Rollout rock located below the FOF? sector = controlsector flat = "floor" else -- If not sector = touchsector flat = "ceiling" end if sector then z = P_CeilingzAtPos(mo.x, mo.y, mo.z, mo.height) end -- Get the absolute height end -- Identifies which surface to move the Rollout rock on if flat == "floor" and sector sss = cfrr.Data[#sector].floor elseif flat == "ceiling" and sector sss = cfrr.Data[#sector].ceiling end if IsOnGround(mo, z) == true and P_IsObjectOnGround(mo) -- Call function if sss -- Check the object on the other sector and the conveyor speed if mo.onconveyor != sss and mo.cmomx != sss.cmomx and mo.cmomy != sss.cmomy mo.momx = $ + mo.cmomx/4 mo.momy = $ + mo.cmomy/4 else -- Move the object without changing the speed local IsMoved = P_TryMove(mo, mo.x+sss.cmomx, mo.y+sss.cmomy, true) if IsMoved != true -- If the object can't go any farther, the object will get momentum mo.momx = $ + mo.cmomx/4 mo.momy = $ + mo.cmomy/4 end end mo.onconveyor = sss -- Current conveyor sector mo.cmomx = sss.cmomx -- Current X-axis conveyor speed mo.cmomy = sss.cmomy -- Current Y-axis conveyor speed end elseif mo.onconveyor and sss == nil -- If the object with moved from the conveyor to the normal surface or IsOnGround(mo, z) == false and sss != nil and mo.onconveyor -- If there is a conveyor below the object but not above it -- X-Momentum if GetPositive(mo.cmomx) > 0 if GetPositive(mo.momx) > GetPositive(mo.cmomx)/4 -- If the object has a speed mo.momx = mo.cmomx+mo.momx -- Then add speeds else -- If there is no speed or it is too slow mo.momx = (mo.cmomx)/2 -- Then the speed will be 2 times slower than the conveyor belt end end -- Y-Momentum if GetPositive(mo.cmomy) > 0 if GetPositive(mo.momy) > GetPositive(mo.cmomy)/4 mo.momy = mo.cmomy+mo.momy else mo.momy = (mo.cmomy)/2 end end mo.onconveyor = nil -- The object is no longer on the conveyor belt end end end addHook("MobjThinker", cfrr.rolloutrockmove, MT_ROLLOUTROCK) cfrr.netvarhook = function(net) -- Synchronization for network games if ShouldItWork("netvarhook") == false then return end cfrr.Data = net($) cfrr.enabled = net($) end addHook("NetVars", cfrr.netvarhook) else -- Lua script will not work if the version is the same as the current one or outdated print("\x83".."NOTICE".."\x80: ".."CFRR is already loaded. ") end