Triggers = {} CollectionsUsed = { 17, 18, 19, 20, 21} FloorTextureMappings = {} CeilingTextureMappings = {} SideTextureMappings = {} Collection = nil function Triggers.init() Collection = Game.global_random(5) + 17 for poly in Polygons() do assign_texture(FloorTextureMappings, poly.floor) assign_texture(CeilingTextureMappings, poly.ceiling) for side in poly:sides() do local opposite = nil local adjacent = side.polygon if side.primary and side.type == "full" then local line = side.line if line.counterclockwise_polygon == adjacent and line.clockwise_polygon ~= nil then opposite = line.clockwise_polygon elseif line.clockwise_polygon == adjacent and line.counterclockwise_polygon ~= nil then opposite = line.counterclockwise_polygon end end if opposite == nil or opposite.floor.z > adjacent.ceiling.z or opposite.ceiling.z < adjacent.floor.z then assign_texture(SideTextureMappings, side.primary) end if side.type == "split" and side.secondary then assign_texture(SideTextureMappings, side.secondary) end end end end function assign_texture(mappings, surface) local col, tex if not surface.collection or not surface.texture_index then col = Collection tex = choose_texture(Collections[col]) else col = surface.collection.index tex = surface.texture_index end if not texture_is_reserved(surface.collection, surface.texture_index, surface.transfer_mode) then surface.collection, surface.texture_index = get_texture(mappings, col, tex) end end function get_texture(mappings, collection_index, texture_index) local hash = math.floor(collection_index * texture_index * 1000) if mappings[hash] then return mappings[hash][0], mappings[hash][1] else local col = Collection local tex = choose_texture(Collections[col]) mappings[hash] = {} mappings[hash][0] = col mappings[hash][1] = tex return col, tex end end function choose_texture(collection) local tex = Game.global_random(collection.bitmap_count-5) + 5 -- Skip switch and terminal textures -- If the chosen texture is reserved, try again while texture_is_reserved(collection, tex) do tex = Game.global_random(collection.bitmap_count-5) + 5 -- Skip switch and terminal textures end return tex end -- Liquids and doors shouldn't be changed or assigned (based on infinity shapes) function texture_is_reserved(collection, texture_index, transfer_mode) local is_reserved = false if not collection or not texture_index then is_reserved = false elseif texture_index <= 4 then is_reserved = true elseif transfer_mode == "landscape" then is_reserved = true elseif collection.index == 17 and ( texture_index == 7 or texture_index == 19 or texture_index == 29 ) then is_reserved = true elseif collection.index == 18 and ( texture_index == 6 or texture_index == 12 or texture_index == 21 ) then is_reserved = true elseif collection.index == 19 and ( texture_index == 13 or texture_index == 29 ) then is_reserved = true elseif collection.index == 20 and ( texture_index == 13 or texture_index == 23 or texture_index == 29 ) then is_reserved = true elseif collection.index == 21 and ( texture_index == 5 or texture_index == 15 or texture_index == 23 or texture_index == 27 ) then is_reserved = true end return is_reserved end