Skip to content

Instantly share code, notes, and snippets.

@TwinFuture
Last active October 28, 2020 12:22
Show Gist options
  • Save TwinFuture/f3ff504dafdc4271388ef75cade010de to your computer and use it in GitHub Desktop.
Save TwinFuture/f3ff504dafdc4271388ef75cade010de to your computer and use it in GitHub Desktop.
{"slots":{"0":{"name":"slot1","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}},"-2":{"name":"system","type":{"events":[],"methods":[]}},"-3":{"name":"library","type":{"events":[],"methods":[]}}},"handlers":[{"code":"-- Hide programming board from UI\nunit.hide()\n-- Unload default Modules & Libraries from global.lua\nfunction unrequire(m)\n package.loaded[m] = nil\n _G[m] = nil\nend\nunrequire'json'\nunrequire'Navigator'\nunrequire'Helpers'\nunrequire'AxisCommand'\nunrequire'database'\nunrequire'cpml/sgui'\nunrequire'pl/init'\n-- Manually run the garbage collector \ncollectgarbage('collect')\n-- \"setpause\" controls how much memory has to be allocated before it starts a new cycle\n-- Default is 200, meaning it will run everytime the memory is doubled,\n-- 100 means it will run every allocation. we're gonna use 110\ncollectgarbage('setpause', 110)\n-- You want this above to run on every pb, before any timers.\n-- You can manually collect garbage withing your tick, bot not recommended, fix your code!\nunit.setTimer('blam', 10)","filter":{"args":[],"signature":"start()","slotKey":"-1"},"key":"0"},{"code":"-- Simple rounding function needed later.\nlocal round = function(number, decimals)\n local power = 10^decimals\n return math.floor(number * power) / power\nend\nlocal ores = {\n {\n {'Quartz',2.65,'rgba(0,255,255','Silicon',2.33,'rgba(0,255,255'},\n {'Bauxite',1.28095,'rgba(191,166,166','Aluminium',2.70,'rgba(191,166,166'},\n {'Coal',1.346,'rgba(54,69,79','Carbon',2.27,'rgba(54,69,79'},\n {'Hematite',5.04,'rgba(255,255,0','Iron',7.85,'rgba(255,255,0'}\n },\n {\n {'Malachite',4.0,'rgba(200,128,51','Copper',8.96,'rgba(200,128,51'},\n {'Limestone',2.7108,'rgba(61,255,0','Calcium',1.55,'rgba(61,255,0'},\n {'Natron',1.55,'rgba(171,92,242','Sodium',0.97,'rgba(171,92,242'},\n {'Chromite',4.54,'rgba(138,153,199','Chromium',7.19,'rgba(138,153,199'}\n },\n {\n {'Acanthite',7.2,'rgba(192,192,192','Silver',10.49,'rgba(192,192,192'},\n {'Garnierite',2.6,'rgba(80,208,80','Nickel',8.91,'rgba(80,208,80'},\n {'Pyrite',5.01,'rgba(255,255,48','Sulfur',1.82,'rgba(255,255,48'},\n {'Petalite',2.41,'rgba(204,128,255','Lithium',0.53,'rgba(204,128,255'}\n },\n {\n {'Cobaltite',6.33,'rgba(240,144,160','Cobalt',8.90,'rgba(240,144,160'},\n {'Cryolite',2.95,'rgba(144,224,80','Fluorine',1.70,'rgba(144,224,80'},\n {'Kolbeckite',2.37,'rgba(230,230,230','Scandium',2.98,'rgba(230,230,230'},\n {'Gold Nuggets',19.3,'rgba(255,209,35','Gold',19.30,'rgba(255,209,35'}\n },\n {\n {'Columbite',5.38,'rgba(155,194,201','Niobium',8.57,'rgba(155,194,201'},\n {'Illmenite',4.55,'rgba(191,194,199','Titanium',4.51,'rgba(191,194,199'},\n {'Vanadinite',6.95,'rgba(166,166,171','Vanadium',6.0,'rgba(166,166,171'},\n {'Rhodonite',3.76,'rgba(156,122,199','Manganese',7.21,'rgba(156,122,199'},\n {'Thoramine',21.30,'rgba(255,0,0','?????????'}\n }\n}\n-- Here we are gonna detect what is in the default slots, databank/core/screen.\nlocal core, bigScreen1, bigScreen2 = {};\nlocal slots = {slot1,slot2,slot3}\n\nfor i = 1, #slots do\n if slots[i].getElementClass() == 'CoreUnitStatic' then\n core = slots[i]\n end\n -- If screen1 is set, set screen2\n if slots[i].getElementClass() == 'ScreenUnit' then\n if bigScreen1 then\n bigScreen2 = slots[i]\n else\n bigScreen1 = slots[i]\n end\n end\nend\nlocal calc = function (maxHP, weight, mass)\n local hubVol = 114400 --export\n local sizes = {\n -- Mining and Inventory, Inventory Manager, Container Proficiency.\n -- Containers, min - max Hitpoints, Base Volume, Base Weight\n -- TODO: Determing what size containers the Hub is connected to.\n -- for now its hardcoded, containers connected to hub x base volume.\n -- eg: medium, 64000 * 10 containers connected to hub\n {49.00,122.00,hubVol,55.80}, --Hub\n {123.00,998.00,1000,229.09}, --xs\n {998.00,7996.00,8000,1280}, --s\n {7996.00,17316.00,64000,7421.34}, --m\n {17316.00,50316.00,128000,14842.7} --l\n }\n for z = 1, #sizes do\n if (maxHP >= sizes[z][1] and maxHP <= sizes[z][2]) then\n local containerProficiency = 30 --export\n weight = (weight - sizes[z][4]) / mass\n -- we need to times the base volume by the proficiency modifier.\n local volume = sizes[z][3] + (sizes[z][3] * containerProficiency / 100)\n local percent = weight / volume\n -- this is a skill, from inventory reducing weight of containers by 5%\n local containerOptimization = 0 --export\n -- but for future proofing we reduce weight by x%\n weight = weight - (weight * containerOptimization / 100)\n -- Convert L to KL\n if weight > 999999 then\n weight = round(weight / 1000000, 2) .. 'ML'\n elseif weight > 999 then\n weight = round(weight / 1000, 2) .. 'KL'\n else\n weight = round(weight, 2) .. 'L'\n end\n if percent < 0.5 then\n -- I like red to blue....Empty -> half full. lower upper..\n r = math.floor(255 *(1 - (percent) / 0.5) +0 * (percent) / 0.5)\n g = math.floor(0 *(1 - (percent) / 0.5) +0 * (percent) / 0.5)\n b = math.floor(0 *(1 - (percent) / 0.5) +255 * (percent) / 0.5)\n else\n -- then, blue to green, half fulle -> full.\n r = math.floor(0 *(1 - (percent - 0.5) / 0.5) +0 * (percent - 0.5) / 0.5)\n g = math.floor(0 *(1 - (percent - 0.5) / 0.5) +255 * (percent - 0.5) / 0.5)\n b = math.floor(255 *(1 - (percent - 0.5) / 0.5) +0 * (percent - 0.5) / 0.5)\n end\n percent = round(percent * 100, 1)\n return weight, percent, 'rgba('..r..','..g..','..b..',0.7)'\n end\n end\nend\nlocal outHTML = function(name, color, weight, percent, barColor)\n return [[<tr>\n <td class=\"name\" style=\"border: 1px solid ]]..color..',1); background-color:'..color..', 0.6)\">'..name..[[</td>\n <td class=\"amount\" style=\"border: 1px solid ]]..color..',1); background-color:'..color..', 0.6)\">'..weight..[[</td>\n <td class=\"percent\" style=\"border: 1px solid ]]..color..[[,1);\">\n <div class=\"bar\" style=\"width: ]]..percent..'%; background-color: '.. barColor ..'\">'..percent..[[%</div>\n </td>\n </tr>\n <tr class=\"blank\"><td></td></tr>]]\nend\nlocal htmlHead, htmlFoot, eleIds, containers = [[<html><head><style>\n .head {\n margin-top: -6px;\n font-size:50px;\n font-variant: small-caps;\n font-weight:bold\n }\n th, .head {\n background: -webkit-linear-gradient(white, #38495a);\n -webkit-background-clip: text;\n -webkit-text-fill-color: transparent;\n -webkit-text-stroke-width: 2px;\n -webkit-text-stroke-color: #fff\n }\n body {\n text-align:center\n }\n table {\n font-size:20px;\n width:48.5%;\n float:left;\n margin-left:10px;\n margin-bottom:10px\n }\n th {\n font-size:28px;\n }\n td, .bar {\n text-shadow: 1px 1px #000, 2px 2px #000;\n background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.05));\n box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.25), inset 0 1px rgba(255, 255, 255, 0.1);\n color:#fff;\n }\n .name, .amount {\n max-width:0;\n overflow: hidden;\n text-overflow: eclipse;\n white-space: nowrap;\n width:25%;\n }\n .percent {\n width:48%;\n font-family:bank;\n letter-spacing:-1px;\n }\n .blank td {\n padding:2px\n }\n</style></head><body>]], '</body></html>', core.getElementIdList(), {}\nlocal htmlBody, htmlBody2 = '', ''\n-- Grab and store all the containers into a table.\nfor i = 1, #eleIds do\n if core.getElementTypeById(eleIds[i]) == 'container'\n or core.getElementTypeById(eleIds[i]) == 'Hub Container' then\n local container = core.getElementNameById(eleIds[i])\n -- If not default container name, store.\n if not string.match(container, '%[') then\n -- Container name eg: Coal, Bauxite\n local weight, maxHP = core.getElementMassById(eleIds[i]), core.getElementMaxHitPointsById(eleIds[i])\n -- Name of containers, Size of container and Total Weight of container.\n table.insert(containers, {container, maxHP, core.getElementMassById(eleIds[i])})\n end\n end\nend\nhtmlBody = htmlBody .. '<div class=\"head\">Stars Raw Ores</div>'\nhtmlBody2 = htmlBody2 .. '<div class=\"head\">Stars Refined Ores</div>'\n-- Iterate through the list of ores so they display in order.\nfor i = 1, #ores do\n local style = ''\n if i == 5 then\n style = 'style=\"float:none;margin:0 auto;\"'\n end\n local head = '<table '.. style ..'><tbody><tr><th colspan=3>TIER '..i..'</th></tr>'\n htmlBody = htmlBody .. head\n htmlBody2 = htmlBody2 .. head\n local tiers = ores[i]\n for x = 1, #tiers do\n local oreName = tiers[x][1]\n local refinedName = tiers[x][4]\n for y = 1, #containers do\n local containerName = containers[y][1]\n -- Ores\n if containerName == oreName then\n local color = tiers[x][3]\n local weight, percent, barColor = calc(containers[y][2], containers[y][3], tiers[x][2])\n htmlBody = htmlBody .. outHTML(oreName, color, weight, percent, barColor)\n end\n -- refined\n if containerName == refinedName then\n local color = tiers[x][6]\n local weight, percent, barColor = calc(containers[y][2], containers[y][3], tiers[x][5])\n htmlBody2 = htmlBody2 .. outHTML(refinedName, color, weight, percent, barColor)\n end\n end\n end\n local foot = '</tbody></table>'\n htmlBody = htmlBody .. foot\n htmlBody2 = htmlBody2 .. foot\nend\nbigScreen1.setHTML(htmlHead .. htmlBody .. htmlFoot)\nbigScreen2.setHTML(htmlHead .. htmlBody2 .. htmlFoot)","filter":{"args":[{"value":"blam"}],"signature":"tick(timerId)","slotKey":"-1"},"key":"1"}],"methods":[],"events":[]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment