Advertisement
Enjl

tilerandomizer3

Aug 22nd, 2022 (edited)
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.18 KB | None | 0 0
  1. -- Tilerandomizer3.lua
  2. -- Version 3.0.0 by Emral
  3. -- Last updated August 22 2022
  4.  
  5. local ls = {}
  6.  
  7. local customCamera
  8.  
  9. -- Replace if you want a specific seed
  10. ls.rng = RNG.new(42069)
  11.  
  12. local tableinsert = table.insert
  13.  
  14. local blocks = {}
  15. local blocksToBGOs = {}
  16. -- xOffset, yOffset, chance
  17. local blocksToSettings = {}
  18. local blocksToMap = {}
  19.  
  20. local blockToBlockBlocks = {}
  21. local blockToBlockBlockMap = {}
  22. local blockToBlockBlockSettings = {}
  23.  
  24. local bgoToBgoBgos = {}
  25. local bgoToBgoBgoMap = {}
  26. local bgoToBgoBgoSettings = {}
  27.  
  28. local pseudoBGOs = {}
  29. local pseudoBGOIDs = {}
  30. local pseudoBGOIDMap = {}
  31.  
  32. local sizableBGOMap = {}
  33.  
  34. -- id
  35. -- x
  36. -- y
  37. -- block
  38. -- frameOffset
  39. local function addPseudoBGO(args)
  40.     local psb = {}
  41.     psb.id = args.id
  42.     psb.block = args.block
  43.     psb.x = args.x
  44.     psb.y = args.y
  45.  
  46.     if pseudoBGOIDMap[args.id] == nil then
  47.         local cfg = BGO.config[args.id]
  48.         local psbid = {}
  49.         psbid.width = cfg.width
  50.         psbid.height = cfg.height
  51.         psbid.priority = cfg.priority
  52.         psbid.frame = 0
  53.         psbid.frameTimer = 0
  54.         psbid.frames = cfg.frames
  55.         psbid.framespeed = cfg.framespeed
  56.         tableinsert(pseudoBGOIDs, psbid)
  57.         pseudoBGOIDMap[args.id] = psbid
  58.     end
  59.  
  60.     psb.x = psb.x + args.blockpivot.x * psb.block.width - pseudoBGOIDMap[args.id].width * args.bgopivot.x
  61.     psb.y = psb.y + args.blockpivot.y * psb.block.height - pseudoBGOIDMap[args.id].height * args.bgopivot.y
  62.  
  63.     psb.xOffset = psb.x - psb.block.x
  64.     psb.yOffset = psb.y - psb.block.y
  65.     psb.verts = {
  66.         psb.x, psb.y,
  67.         psb.x + pseudoBGOIDMap[args.id].width, psb.y,
  68.         psb.x, psb.y + pseudoBGOIDMap[args.id].height,
  69.         psb.x + pseudoBGOIDMap[args.id].width, psb.y + pseudoBGOIDMap[args.id].height,
  70.     }
  71.     psb.tex = {
  72.         0, 0,
  73.         1/pseudoBGOIDMap[args.id].frames, 0,
  74.         0, 1/pseudoBGOIDMap[args.id].frames,
  75.         1/pseudoBGOIDMap[args.id].frames, 1/pseudoBGOIDMap[args.id].frames,
  76.     }
  77.  
  78.     psb.frame = args.frameOffset or ls.rng:randomInt(0, pseudoBGOIDMap[args.id].frames - 1)
  79.  
  80.     tableinsert(pseudoBGOs, psb)
  81. end
  82.  
  83. -- Automatically spawns BGOs on blocks. To spawn BGOs with different pivots or chances, use this function once for each distinct chance and pivot
  84. -- ids: The ID or list of block IDs to spawn BGOs on
  85. -- list: The list of BGO IDs to spawn
  86. -- chance: The chance (1 is 100%, 0 is 0%) at which a BGO should be spawned
  87. -- pivot: the pivot of the BGOs, where vector(0, 0) is top left, and vector(1, 1) is bottom right. For example, vector(0.5, 0) is good for spawning trees on top of blocks.
  88. function ls.registerBlockBgoPair(ids, list, chance, pivot)
  89.     if type(ids) == "number" then
  90.         ids = {ids}
  91.     end
  92.     for k,id in ipairs(ids) do
  93.         if not blocksToMap[id] then
  94.             blocksToMap[id] = true
  95.             tableinsert(blocks, id)
  96.  
  97.             blocksToSettings[id] = {}
  98.         end
  99.  
  100.         table.insert(blocksToSettings[id], {
  101.             targets = list,
  102.             chance = chance or 1,
  103.             pivot = pivot or vector(0.5, 0.5)
  104.         })
  105.     end
  106. end
  107.  
  108. -- Transforms a block into randomized Blocks. For different pivots or chances, use this function once for each distinct chance and pivot
  109. -- ids: The ID or list of block IDs to apply the transformation to
  110. -- list: The list of Block IDs to spawn
  111. -- chance: The chance (1 is 100%, 0 is 0%) of transformation
  112. -- pivot: the pivot of the Blocks, where vector(0, 0) is top left, and vector(1, 1) is bottom right.
  113. function ls.registerBlockTransformation(ids, list, chance, pivot)
  114.     if type(ids) == "number" then
  115.         ids = {ids}
  116.     end
  117.     for k,id in ipairs(ids) do
  118.         if not blockToBlockBlockMap[id] then
  119.             blockToBlockBlockMap[id] = true
  120.             tableinsert(blockToBlockBlocks, id)
  121.  
  122.             blockToBlockBlockSettings[id] = {}
  123.         end
  124.  
  125.         table.insert(blockToBlockBlockSettings[id], {
  126.             targets = list,
  127.             chance = chance or 1,
  128.             pivot = pivot or vector(0.5, 0.5)
  129.         })
  130.     end
  131. end
  132.  
  133. -- Transforms a sizable into BGOs of a given ID. Currently only compatible with 32x32 tiling. This transformation happens BEFORE the BGOTransformation step, meaning that the BGO the sizable transforms into can afterwards be randomized.
  134. -- sizableID: ID of the sizable to transform
  135. -- bgoID: ID of BGO to transform the sizable into
  136. function ls.registerSizableAsBGO(sizableID, bgoID)
  137.     sizableBGOMap[sizableID] = bgoID
  138. end
  139.  
  140. -- Transforms a BGO into randomized BGOs. For different pivots or chances, use this function once for each distinct chance and pivot
  141. -- ids: The ID or list of BGO IDs to apply the transformation to
  142. -- list: The list of BGO IDs to spawn
  143. -- chance: The chance (1 is 100%, 0 is 0%) of transformation
  144. -- pivot: the pivot of the BGOs, where vector(0, 0) is top left, and vector(1, 1) is bottom right.
  145. function ls.registerBgoTransformation(ids, list, chance, pivot)
  146.     if type(ids) == "number" then
  147.         ids = {ids}
  148.     end
  149.     for k,id in ipairs(ids) do
  150.         if not bgoToBgoBgoMap[id] then
  151.             bgoToBgoBgoMap[id] = true
  152.             tableinsert(bgoToBgoBgos, id)
  153.    
  154.             bgoToBgoBgoSettings[id] = {}
  155.         end
  156.    
  157.         table.insert(bgoToBgoBgoSettings[id], {
  158.             targets = list,
  159.             chance = chance or 1,
  160.             pivot = pivot or vector(0.5, 0.5)
  161.         })
  162.     end
  163. end
  164.  
  165. function ls.onInitAPI()
  166.     registerEvent(ls, "onStart")
  167.     registerEvent(ls, "onTickEnd")
  168.     registerEvent(ls, "onDraw")
  169. end
  170.  
  171. function ls.onTickEnd()
  172.     for k,v in ipairs(pseudoBGOs) do
  173.         if v.block.isValid then
  174.             if v.x ~= v.block.x + v.xOffset then
  175.                 local lastX = v.x
  176.                 v.x = v.block.x + v.xOffset
  177.                 local diff = v.x - lastX
  178.                 v.verts[1] = v.verts[1] + diff
  179.                 v.verts[3] = v.verts[3] + diff
  180.                 v.verts[5] = v.verts[5] + diff
  181.                 v.verts[7] = v.verts[7] + diff
  182.             end
  183.             if v.y ~= v.block.y + v.yOffset then
  184.                 local lastY = v.y
  185.                 v.y = v.block.y + v.yOffset
  186.                 local diff = v.y - lastY
  187.                 v.verts[2] = v.verts[2] + diff
  188.                 v.verts[4] = v.verts[4] + diff
  189.                 v.verts[6] = v.verts[6] + diff
  190.                 v.verts[8] = v.verts[8] + diff
  191.             end
  192.         end
  193.     end
  194. end
  195.  
  196. local bgoDrawCalls = {}
  197. local bgoDrawCallMap = {}
  198.  
  199. local buffer = 0
  200.  
  201. function ls.onStart()
  202.     pcall(function() customCamera = require("customCamera") end)
  203.  
  204.     if customCamera then
  205.         local function drawToCustomCamera(args)
  206.            
  207.             for k,v in ipairs(bgoDrawCalls) do
  208.                 local screenX,screenY,screenScale,screenRotation = customCamera.convertPosToScreen(args,camera.x + 400, camera.y + 300)
  209.  
  210.                 local vt = {}
  211.  
  212.  
  213.                 local cfg = pseudoBGOIDMap[v]
  214.                 local dc = bgoDrawCallMap[v]
  215.                 local p = customCamera.convertPriority(args,cfg.priority)
  216.  
  217.                 for i=1, #dc.verts, 2 do
  218.                     local vec = vector(dc.verts[i] - 400, dc.verts[i+1] - 300):rotate(screenRotation)
  219.                     vt[i] = vec.x + 400
  220.                     vt[i+1] = vec.y + 300
  221.                 end
  222.                 Graphics.glDraw{
  223.                     priority = p,
  224.                     target = args.target,
  225.                     sceneCoords = false,
  226.                     primitive = Graphics.GL_TRIANGLES,
  227.                     vertexCoords = vt,
  228.                     textureCoords = dc.tex,
  229.                     texture = Graphics.sprites.background[v].img
  230.                 }
  231.             end
  232.         end
  233.  
  234.         customCamera.registerSceneDraw(drawToCustomCamera)
  235.  
  236.         buffer = 200
  237.     end
  238.  
  239.     for k,v in Block.iterateByFilterMap(sizableBGOMap) do
  240.         local width, height = 32, 32
  241.         for x=0, v.width - 1, width do
  242.             for y=0, v.height - 1, height do
  243.                 local id = sizableBGOMap[v.id]
  244.                 for _, config in ipairs(bgoToBgoBgoSettings[id]) do
  245.                     if config.chance >= ls.rng:random(0, 1) then
  246.                         id = ls.rng:irandomEntry(config.targets)
  247.                         break
  248.                     end
  249.                 end
  250.  
  251.                 addPseudoBGO{
  252.                     id = id,
  253.                     x = v.x + x,
  254.                     y = v.y + y,
  255.                     block = v,
  256.                     bgopivot = vector(0,0),
  257.                     blockpivot = vector(0,0)
  258.                 }
  259.             end
  260.         end
  261.         -- v.width = 0
  262.         -- v.isHidden = true
  263.     end
  264.  
  265.     for k,v in Block.iterate(blockToBlockBlocks) do
  266.         for _, config in ipairs(blockToBlockBlockSettings[v.id]) do
  267.             if config.chance >= ls.rng:random(0, 1) then
  268.                 local x,y,w,h = v.x, v.y, v.width, v.height
  269.                 v:transform(ls.rng:irandomEntry(config.targets))
  270.                 v.x, v.y = x + w * config.pivot.x - v.width * config.pivot.x, y + h * config.pivot.y - v.height * config.pivot.y
  271.                 break
  272.             end
  273.         end
  274.     end
  275.  
  276.     for k,v in Block.iterate(blocks) do
  277.         for _, config in ipairs(blocksToSettings[v.id]) do
  278.             if config.chance >= ls.rng:random(0, 1) then
  279.                 addPseudoBGO{
  280.                     id = ls.rng:irandomEntry(blocksToBGOs[v.id]),
  281.                     x = v.x + (config.xOffset or 0),
  282.                     y = v.y + (config.yOffset or 0),
  283.                     block = v,
  284.                     bgopivot = config.bgopivot or vector(0,0),
  285.                     blockpivot = config.blockpivot or vector(0,0)
  286.                 }
  287.                 break
  288.             end
  289.         end
  290.     end
  291.  
  292.     for k,v in ipairs(BGO.get(bgoToBgoBgos)) do
  293.         for _, config in ipairs(bgoToBgoBgoSettings[v.id]) do
  294.             if config.chance >= ls.rng:random(0, 1) then
  295.                 local x,y,w,h = v.x, v.y, v.width, v.height
  296.                 v:transform(ls.rng:irandomEntry(config.targets))
  297.                 v.x, v.y = x + w * config.pivot.x - v.width * config.pivot.x, y + h * config.pivot.y - v.height * config.pivot.y
  298.                 break
  299.             end
  300.         end
  301.     end
  302. end
  303.  
  304. local lastFrame = 0
  305. local lastGroundTouching = true
  306.  
  307. function ls.onDraw()
  308.     for k,v in ipairs(pseudoBGOIDs) do
  309.         v.frameTimer = v.frameTimer + 1/v.framespeed
  310.         while v.frameTimer >= 1 do
  311.             v.frameTimer = v.frameTimer - 1
  312.             v.frame = (v.frame + 1) % v.frames
  313.         end
  314.     end
  315.  
  316.     bgoDrawCalls = {}
  317.     bgoDrawCallMap = {}
  318.     local x,y = camera.x, camera.y
  319.     local t = {x, [0]=y}
  320.    
  321.     for k,v in ipairs(pseudoBGOs) do
  322.         local config = pseudoBGOIDMap[v.id]
  323.         if v.x + config.width >= x - buffer
  324.         and v.x <= x + 800 + buffer
  325.         and v.y + config.height >= y - buffer
  326.         and v.y <= y + 600 + buffer then
  327.             if not (v.block and ((not v.block.isValid) or v.block.isHidden)) then
  328.                 if bgoDrawCallMap[v.id] == nil then
  329.                     tableinsert(bgoDrawCalls, v.id)
  330.                     bgoDrawCallMap[v.id] = {
  331.                         verts = {},
  332.                         tex = {}
  333.                     }
  334.                 end
  335.  
  336.                 local offset = config.frame/config.frames
  337.                 local hasReset = false
  338.                 local i=1
  339.                 while i <= 8 do
  340.                     tableinsert(bgoDrawCallMap[v.id].verts, v.verts[i] - t[i%2])
  341.                     tableinsert(bgoDrawCallMap[v.id].tex, v.tex[i] + offset)
  342.                     if i == 6 and not hasReset then
  343.                         i = 2
  344.                         hasReset = true
  345.                     end
  346.                     i = i + 1
  347.                 end
  348.             end
  349.         end
  350.     end
  351.     for k,v in ipairs(bgoDrawCalls) do
  352.         local cfg = pseudoBGOIDMap[v]
  353.         local dc = bgoDrawCallMap[v]
  354.         Graphics.glDraw{
  355.             priority = cfg.priority,
  356.             sceneCoords = false,
  357.             primitive = Graphics.GL_TRIANGLES,
  358.             vertexCoords = dc.verts,
  359.             textureCoords = dc.tex,
  360.             texture = Graphics.sprites.background[v].img
  361.         }
  362.     end
  363. end
  364.  
  365. return ls
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement