Advertisement
Wojbie

Cwaila

Jan 15th, 2020 (edited)
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.51 KB | None | 0 0
  1. --------------------------------------------------------------------
  2. -- Wojbies Program 1.2 - "Cwaila" - Canvas, what am i looking at? --
  3. --------------------------------------------------------------------
  4. --   Copyright (c) 2017-2021 Wojbie (wojbie@wojbie.net)
  5. --   Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
  6. --   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. --   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8. --   3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9. --   4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. --   5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  11. --   NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. YOU ACKNOWLEDGE THAT THIS SOFTWARE IS NOT DESIGNED, LICENSED OR INTENDED FOR USE IN THE DESIGN, CONSTRUCTION, OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY.
  12.  
  13. --# Config variables
  14.  
  15. local tArgs = {...}
  16. local scale = tonumber(tArgs[1]) or 1 --Size of Cwaila Box
  17.  
  18. --# Initializing
  19. local range = 8
  20. local function save(name, data, binary) local file = fs.open(tostring(name), binary and "wb" or "w") if not file then return false end file.write(data) file.close() return true end
  21. local function getHttp(source, post, header, binary) if not http.checkURL(source) then return false end local file = post and http.post(source, post, header, binary) or http.get(source, header, binary) if not file then return false end local data = file.readAll() file.close() if data then return data end end
  22. local function saveHttp(name, source, post, header, binary) if binary == nil then binary = true end local file = getHttp(source, post, header, binary) if not file then return false end return save(name, file, binary) end  
  23.  
  24. local modules = peripheral.wrap("back")
  25. if not fs.exists("/w/positioning.lua") then print("Downloading module /w/positioning") assert(saveHttp("/w/positioning.lua", "https://pastebin.com/raw/0Jnk4Uv9"), "Download failed") end
  26. local position = require("/w/positioning")
  27.  
  28. assert(modules, "Needs neural interface")
  29. assert(modules.hasModule("plethora:scanner"), "Needs block scanner")
  30. assert(modules.hasModule("plethora:sensor"), "Needs entity sensor")
  31. assert(modules.hasModule("plethora:introspection"), "Needs introspection module")
  32. assert(modules.hasModule("plethora:glasses"), "Needs overlay glasses")
  33.  
  34. --# Canvas operations
  35. local canvas2d = modules.canvas().addGroup({0, 0})
  36. local sizeX = modules.canvas().getSize()
  37.  
  38. local textSizeX = { --Size of character - for \167 i eat next character. For newlines i reset size counter and go one lower.
  39.     ["\0"] = 1, ["\9"] = false, ["\10"] = false, ["\13"] = false, ["\32"] = 4,
  40.     ["!"] = 2, ['"'] = 5, ["'"] = 3, ['('] = 5, [')'] = 5, ['*'] = 5, [","] = 2, ["."] = 2, [":"] = 2, [";"] = 2, ['<'] = 5, ['>'] = 5,
  41.     ['@'] = 7, ["I"] = 4, ['['] = 4, [']'] = 4, ["`"] = 3, ["f"] = 5, ["i"] = 2, ["k"] = 5, ["l"] = 3, ["t"] = 4, ['{'] = 5, ["|"] = 2, ['}'] = 5, ['~'] = 7,
  42.     ["\160"] = 4, ["\161"] = 2, ["\162"] = 4, ["\164"] = 4, ["\165"] = 4, ["\166"] = 1, ["\167"] = false, ["\168"] = 3, ["\169"] = 5, ["\174"] = 7, ["\175"] = 4, ["\176"] = 7, ["\177"] = 7, ["\179"] = 3, ["\180"] = 2, ["\181"] = 3, ["\182"] = 4, ["\184"] = 2, ["\185"] = 2, ["\190"] = 4, ["\195"] = 4, ["\204"] = 3, ["\205"] = 4, ["\206"] = 3, ["\207"] = 3, ["\208"] = 4, ["\210"] = 4, ["\215"] = 4, ["\217"] = 4, ["\219"] = 4, ["\221"] = 4, ["\222"] = 4, ["\236"] = 3, ["\237"] = 3, ["\239"] = 4, ["\240"] = 4, ["\247"] = 7, ["\253"] = 4, ["\254"] = 3,
  43. }
  44. for i = 1, 8 do textSizeX[string.char(i)] = 9 end
  45. for i = 11, 12 do textSizeX[string.char(i)] = 9 end
  46. for i = 14, 31 do textSizeX[string.char(i)] = 9 end
  47. for i = 127, 159 do textSizeX[string.char(i)] = 9 end
  48. textSizeX["\173"] = 9
  49.  
  50. local function canvasTextOffset(text, scale)
  51.     local outX, outY, maxX = 0, 9, 0
  52.     text:gsub("()(.)", function(p, c)
  53.         if textSizeX[c] == false then
  54.             if c == "\10" or c == "\13" then
  55.                 outY = outY + 9
  56.                 maxX = math.max(maxX, outX)
  57.                 outX = 0
  58.             elseif c == "\9" then
  59.                 outX = math.ceil(outX / 16) * 16
  60.             elseif c == "\167" then
  61.                 local n = text:sub(p + 1, p + 1)
  62.                 if textSizeX[n] then --cancel next character
  63.                     outX = outX - textSizeX[n]
  64.                 else
  65.                     outX = outX - 6
  66.                 end
  67.             end
  68.         elseif textSizeX[c] then
  69.             outX = outX + textSizeX[c]
  70.         else
  71.             outX = outX + 6
  72.         end
  73.     end)
  74.     return outX * scale, outY * scale, math.max(maxX, outX) * scale
  75. end
  76.  
  77. local function cwailaBoxItem(block, scale)
  78.     scale = scale or 1
  79.     local text = block.displayName .. "\n" .. string.match(block.name, "^([^:]+)"):gsub("^.", string.upper)
  80.     local _, offy, offx = canvasTextOffset(text, scale)
  81.     local center = (sizeX - offx) / 2
  82.     local thumbnail = 20 * scale
  83.     canvas2d.addRectangle(center - thumbnail - 5, 10 - 5, offx + thumbnail + 10, offy + 10, 0x00000050 )
  84.     canvas2d.addLines({center - thumbnail - 5, 10 - 5}, {center + offx + 5, 10 - 5}, {center + offx + 5, 10 + offy + 5}, {center - thumbnail - 5, 10 + offy + 5}, 0x490BB950 , 5 * scale )
  85.     canvas2d.addText({center, 10}, block.displayName, 0xFFFFFFFF, scale).setShadow(true)
  86.     canvas2d.addText({center, 10 + 9 * scale}, string.match(block.name, "^([^:]+)"):gsub("^.", string.upper), 0x5349CBFF, scale).setShadow(true)
  87.     pcall(canvas2d.addItem, {center - thumbnail, 10}, block.name, block.metadata, scale)
  88. end
  89.  
  90. local function cwailaBoxEntity(entity, scale)
  91.     scale = scale or 1
  92.     local text = entity.displayName .. (entity.health and "\n" .. string.format("%d / %d", entity.health, entity.maxHealth) or "")
  93.     local _, offy, offx = canvasTextOffset(text, scale)
  94.     local center = (sizeX - offx) / 2
  95.     canvas2d.addRectangle(center - 5, 10 - 5, offx + 10, offy + 10, 0x00000090 )
  96.     canvas2d.addLines({center - 5, 10 - 5}, {center + offx + 5, 10 - 5}, {center + offx + 5, 10 + offy + 5}, {center - 5, 10 + offy + 5}, 0x490BB950 , 5 * scale )
  97.     canvas2d.addText({center, 10}, entity.displayName, 0xFFFFFFFF, scale).setShadow(true)
  98.     if entity.health then canvas2d.addText({center, 10 + 9 * scale}, string.format("%d / %d", entity.health, entity.maxHealth), 0xFE130FFF, scale).setShadow(true) end
  99. end
  100.  
  101. --# Main ray casting code
  102. local function point()
  103.     local last
  104.     while true do
  105.         local scanned_blocks = modules.scan()
  106.         local ent = modules.sense()
  107.         local meta = modules.getMetaOwner()
  108.         local entities = {}
  109.         for _, k in pairs(ent) do
  110.             if k.id ~= meta.id then
  111.                 entities[position(k)] = k
  112.             end
  113.         end
  114.         for current in position.iterateBlocks(position.pitchYawVector(meta.pitch, meta.yaw), position(meta.withinBlock)) do
  115.             local pos = current:floor()
  116.             local num = 17 ^ 2 * (pos.x + 8) + 17 * (pos.y + 8) + (pos.z + 8) + 1 or 0
  117.             local block = scanned_blocks[num]
  118.             local entHit
  119.             for i, k in pairs(entities) do
  120.                 if current:distance(i) < 1 then entHit = k break end
  121.             end
  122.             if math.abs(pos.x) > range or math.abs(pos.y) > range or math.abs(pos.z) > range then
  123.                 if last then canvas2d.clear() last = nil end
  124.                 break
  125.             elseif entHit then
  126.                 if last == entHit.id then break end
  127.                 last = entHit.id
  128.                 local ok, meta = pcall(modules.getMetaByID, last)
  129.                 if not ok or not meta then last = nil break end
  130.                 canvas2d.clear()
  131.                 cwailaBoxEntity(meta, scale)
  132.                 break
  133.             elseif block and block.name ~= "minecraft:air" then
  134.                 if last == num then break end
  135.                 last = num
  136.                 local ok, meta = pcall(modules.getBlockMeta, pos.x, pos.y, pos.z)
  137.                 if not ok or not meta or meta.name == "minecraft:air" then last = nil break end
  138.                 canvas2d.clear()
  139.                 cwailaBoxItem(meta, scale)
  140.                 break
  141.             end
  142.         end
  143.         sleep(0.5)
  144.     end
  145. end
  146. print("Active")
  147. print(pcall(point))
  148. canvas2d.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement