Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Tilerandomizer3.lua
- -- Version 3.0.0 by Emral
- -- Last updated August 22 2022
- local ls = {}
- local customCamera
- -- Replace if you want a specific seed
- ls.rng = RNG.new(42069)
- local tableinsert = table.insert
- local blocks = {}
- local blocksToBGOs = {}
- -- xOffset, yOffset, chance
- local blocksToSettings = {}
- local blocksToMap = {}
- local blockToBlockBlocks = {}
- local blockToBlockBlockMap = {}
- local blockToBlockBlockSettings = {}
- local bgoToBgoBgos = {}
- local bgoToBgoBgoMap = {}
- local bgoToBgoBgoSettings = {}
- local pseudoBGOs = {}
- local pseudoBGOIDs = {}
- local pseudoBGOIDMap = {}
- local sizableBGOMap = {}
- -- id
- -- x
- -- y
- -- block
- -- frameOffset
- local function addPseudoBGO(args)
- local psb = {}
- psb.id = args.id
- psb.block = args.block
- psb.x = args.x
- psb.y = args.y
- if pseudoBGOIDMap[args.id] == nil then
- local cfg = BGO.config[args.id]
- local psbid = {}
- psbid.width = cfg.width
- psbid.height = cfg.height
- psbid.priority = cfg.priority
- psbid.frame = 0
- psbid.frameTimer = 0
- psbid.frames = cfg.frames
- psbid.framespeed = cfg.framespeed
- tableinsert(pseudoBGOIDs, psbid)
- pseudoBGOIDMap[args.id] = psbid
- end
- psb.x = psb.x + args.blockpivot.x * psb.block.width - pseudoBGOIDMap[args.id].width * args.bgopivot.x
- psb.y = psb.y + args.blockpivot.y * psb.block.height - pseudoBGOIDMap[args.id].height * args.bgopivot.y
- psb.xOffset = psb.x - psb.block.x
- psb.yOffset = psb.y - psb.block.y
- psb.verts = {
- psb.x, psb.y,
- psb.x + pseudoBGOIDMap[args.id].width, psb.y,
- psb.x, psb.y + pseudoBGOIDMap[args.id].height,
- psb.x + pseudoBGOIDMap[args.id].width, psb.y + pseudoBGOIDMap[args.id].height,
- }
- psb.tex = {
- 0, 0,
- 1/pseudoBGOIDMap[args.id].frames, 0,
- 0, 1/pseudoBGOIDMap[args.id].frames,
- 1/pseudoBGOIDMap[args.id].frames, 1/pseudoBGOIDMap[args.id].frames,
- }
- psb.frame = args.frameOffset or ls.rng:randomInt(0, pseudoBGOIDMap[args.id].frames - 1)
- tableinsert(pseudoBGOs, psb)
- end
- -- Automatically spawns BGOs on blocks. To spawn BGOs with different pivots or chances, use this function once for each distinct chance and pivot
- -- ids: The ID or list of block IDs to spawn BGOs on
- -- list: The list of BGO IDs to spawn
- -- chance: The chance (1 is 100%, 0 is 0%) at which a BGO should be spawned
- -- 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.
- function ls.registerBlockBgoPair(ids, list, chance, pivot)
- if type(ids) == "number" then
- ids = {ids}
- end
- for k,id in ipairs(ids) do
- if not blocksToMap[id] then
- blocksToMap[id] = true
- tableinsert(blocks, id)
- blocksToSettings[id] = {}
- end
- table.insert(blocksToSettings[id], {
- targets = list,
- chance = chance or 1,
- pivot = pivot or vector(0.5, 0.5)
- })
- end
- end
- -- Transforms a block into randomized Blocks. For different pivots or chances, use this function once for each distinct chance and pivot
- -- ids: The ID or list of block IDs to apply the transformation to
- -- list: The list of Block IDs to spawn
- -- chance: The chance (1 is 100%, 0 is 0%) of transformation
- -- pivot: the pivot of the Blocks, where vector(0, 0) is top left, and vector(1, 1) is bottom right.
- function ls.registerBlockTransformation(ids, list, chance, pivot)
- if type(ids) == "number" then
- ids = {ids}
- end
- for k,id in ipairs(ids) do
- if not blockToBlockBlockMap[id] then
- blockToBlockBlockMap[id] = true
- tableinsert(blockToBlockBlocks, id)
- blockToBlockBlockSettings[id] = {}
- end
- table.insert(blockToBlockBlockSettings[id], {
- targets = list,
- chance = chance or 1,
- pivot = pivot or vector(0.5, 0.5)
- })
- end
- end
- -- 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.
- -- sizableID: ID of the sizable to transform
- -- bgoID: ID of BGO to transform the sizable into
- function ls.registerSizableAsBGO(sizableID, bgoID)
- sizableBGOMap[sizableID] = bgoID
- end
- -- Transforms a BGO into randomized BGOs. For different pivots or chances, use this function once for each distinct chance and pivot
- -- ids: The ID or list of BGO IDs to apply the transformation to
- -- list: The list of BGO IDs to spawn
- -- chance: The chance (1 is 100%, 0 is 0%) of transformation
- -- pivot: the pivot of the BGOs, where vector(0, 0) is top left, and vector(1, 1) is bottom right.
- function ls.registerBgoTransformation(ids, list, chance, pivot)
- if type(ids) == "number" then
- ids = {ids}
- end
- for k,id in ipairs(ids) do
- if not bgoToBgoBgoMap[id] then
- bgoToBgoBgoMap[id] = true
- tableinsert(bgoToBgoBgos, id)
- bgoToBgoBgoSettings[id] = {}
- end
- table.insert(bgoToBgoBgoSettings[id], {
- targets = list,
- chance = chance or 1,
- pivot = pivot or vector(0.5, 0.5)
- })
- end
- end
- function ls.onInitAPI()
- registerEvent(ls, "onStart")
- registerEvent(ls, "onTickEnd")
- registerEvent(ls, "onDraw")
- end
- function ls.onTickEnd()
- for k,v in ipairs(pseudoBGOs) do
- if v.block.isValid then
- if v.x ~= v.block.x + v.xOffset then
- local lastX = v.x
- v.x = v.block.x + v.xOffset
- local diff = v.x - lastX
- v.verts[1] = v.verts[1] + diff
- v.verts[3] = v.verts[3] + diff
- v.verts[5] = v.verts[5] + diff
- v.verts[7] = v.verts[7] + diff
- end
- if v.y ~= v.block.y + v.yOffset then
- local lastY = v.y
- v.y = v.block.y + v.yOffset
- local diff = v.y - lastY
- v.verts[2] = v.verts[2] + diff
- v.verts[4] = v.verts[4] + diff
- v.verts[6] = v.verts[6] + diff
- v.verts[8] = v.verts[8] + diff
- end
- end
- end
- end
- local bgoDrawCalls = {}
- local bgoDrawCallMap = {}
- local buffer = 0
- function ls.onStart()
- pcall(function() customCamera = require("customCamera") end)
- if customCamera then
- local function drawToCustomCamera(args)
- for k,v in ipairs(bgoDrawCalls) do
- local screenX,screenY,screenScale,screenRotation = customCamera.convertPosToScreen(args,camera.x + 400, camera.y + 300)
- local vt = {}
- local cfg = pseudoBGOIDMap[v]
- local dc = bgoDrawCallMap[v]
- local p = customCamera.convertPriority(args,cfg.priority)
- for i=1, #dc.verts, 2 do
- local vec = vector(dc.verts[i] - 400, dc.verts[i+1] - 300):rotate(screenRotation)
- vt[i] = vec.x + 400
- vt[i+1] = vec.y + 300
- end
- Graphics.glDraw{
- priority = p,
- target = args.target,
- sceneCoords = false,
- primitive = Graphics.GL_TRIANGLES,
- vertexCoords = vt,
- textureCoords = dc.tex,
- texture = Graphics.sprites.background[v].img
- }
- end
- end
- customCamera.registerSceneDraw(drawToCustomCamera)
- buffer = 200
- end
- for k,v in Block.iterateByFilterMap(sizableBGOMap) do
- local width, height = 32, 32
- for x=0, v.width - 1, width do
- for y=0, v.height - 1, height do
- local id = sizableBGOMap[v.id]
- for _, config in ipairs(bgoToBgoBgoSettings[id]) do
- if config.chance >= ls.rng:random(0, 1) then
- id = ls.rng:irandomEntry(config.targets)
- break
- end
- end
- addPseudoBGO{
- id = id,
- x = v.x + x,
- y = v.y + y,
- block = v,
- bgopivot = vector(0,0),
- blockpivot = vector(0,0)
- }
- end
- end
- -- v.width = 0
- -- v.isHidden = true
- end
- for k,v in Block.iterate(blockToBlockBlocks) do
- for _, config in ipairs(blockToBlockBlockSettings[v.id]) do
- if config.chance >= ls.rng:random(0, 1) then
- local x,y,w,h = v.x, v.y, v.width, v.height
- v:transform(ls.rng:irandomEntry(config.targets))
- v.x, v.y = x + w * config.pivot.x - v.width * config.pivot.x, y + h * config.pivot.y - v.height * config.pivot.y
- break
- end
- end
- end
- for k,v in Block.iterate(blocks) do
- for _, config in ipairs(blocksToSettings[v.id]) do
- if config.chance >= ls.rng:random(0, 1) then
- addPseudoBGO{
- id = ls.rng:irandomEntry(blocksToBGOs[v.id]),
- x = v.x + (config.xOffset or 0),
- y = v.y + (config.yOffset or 0),
- block = v,
- bgopivot = config.bgopivot or vector(0,0),
- blockpivot = config.blockpivot or vector(0,0)
- }
- break
- end
- end
- end
- for k,v in ipairs(BGO.get(bgoToBgoBgos)) do
- for _, config in ipairs(bgoToBgoBgoSettings[v.id]) do
- if config.chance >= ls.rng:random(0, 1) then
- local x,y,w,h = v.x, v.y, v.width, v.height
- v:transform(ls.rng:irandomEntry(config.targets))
- v.x, v.y = x + w * config.pivot.x - v.width * config.pivot.x, y + h * config.pivot.y - v.height * config.pivot.y
- break
- end
- end
- end
- end
- local lastFrame = 0
- local lastGroundTouching = true
- function ls.onDraw()
- for k,v in ipairs(pseudoBGOIDs) do
- v.frameTimer = v.frameTimer + 1/v.framespeed
- while v.frameTimer >= 1 do
- v.frameTimer = v.frameTimer - 1
- v.frame = (v.frame + 1) % v.frames
- end
- end
- bgoDrawCalls = {}
- bgoDrawCallMap = {}
- local x,y = camera.x, camera.y
- local t = {x, [0]=y}
- for k,v in ipairs(pseudoBGOs) do
- local config = pseudoBGOIDMap[v.id]
- if v.x + config.width >= x - buffer
- and v.x <= x + 800 + buffer
- and v.y + config.height >= y - buffer
- and v.y <= y + 600 + buffer then
- if not (v.block and ((not v.block.isValid) or v.block.isHidden)) then
- if bgoDrawCallMap[v.id] == nil then
- tableinsert(bgoDrawCalls, v.id)
- bgoDrawCallMap[v.id] = {
- verts = {},
- tex = {}
- }
- end
- local offset = config.frame/config.frames
- local hasReset = false
- local i=1
- while i <= 8 do
- tableinsert(bgoDrawCallMap[v.id].verts, v.verts[i] - t[i%2])
- tableinsert(bgoDrawCallMap[v.id].tex, v.tex[i] + offset)
- if i == 6 and not hasReset then
- i = 2
- hasReset = true
- end
- i = i + 1
- end
- end
- end
- end
- for k,v in ipairs(bgoDrawCalls) do
- local cfg = pseudoBGOIDMap[v]
- local dc = bgoDrawCallMap[v]
- Graphics.glDraw{
- priority = cfg.priority,
- sceneCoords = false,
- primitive = Graphics.GL_TRIANGLES,
- vertexCoords = dc.verts,
- textureCoords = dc.tex,
- texture = Graphics.sprites.background[v].img
- }
- end
- end
- return ls
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement