Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------------------------
- -- Wojbies Program 1.2 - "Cwaila" - Canvas, what am i looking at? --
- --------------------------------------------------------------------
- -- Copyright (c) 2017-2021 Wojbie (wojbie@wojbie.net)
- -- 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:
- -- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- -- 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.
- -- 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.
- -- 4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
- -- 5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
- -- 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.
- --# Config variables
- local tArgs = {...}
- local scale = tonumber(tArgs[1]) or 1 --Size of Cwaila Box
- --# Initializing
- local range = 8
- 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
- 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
- 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
- local modules = peripheral.wrap("back")
- 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
- local position = require("/w/positioning")
- assert(modules, "Needs neural interface")
- assert(modules.hasModule("plethora:scanner"), "Needs block scanner")
- assert(modules.hasModule("plethora:sensor"), "Needs entity sensor")
- assert(modules.hasModule("plethora:introspection"), "Needs introspection module")
- assert(modules.hasModule("plethora:glasses"), "Needs overlay glasses")
- --# Canvas operations
- local canvas2d = modules.canvas().addGroup({0, 0})
- local sizeX = modules.canvas().getSize()
- local textSizeX = { --Size of character - for \167 i eat next character. For newlines i reset size counter and go one lower.
- ["\0"] = 1, ["\9"] = false, ["\10"] = false, ["\13"] = false, ["\32"] = 4,
- ["!"] = 2, ['"'] = 5, ["'"] = 3, ['('] = 5, [')'] = 5, ['*'] = 5, [","] = 2, ["."] = 2, [":"] = 2, [";"] = 2, ['<'] = 5, ['>'] = 5,
- ['@'] = 7, ["I"] = 4, ['['] = 4, [']'] = 4, ["`"] = 3, ["f"] = 5, ["i"] = 2, ["k"] = 5, ["l"] = 3, ["t"] = 4, ['{'] = 5, ["|"] = 2, ['}'] = 5, ['~'] = 7,
- ["\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,
- }
- for i = 1, 8 do textSizeX[string.char(i)] = 9 end
- for i = 11, 12 do textSizeX[string.char(i)] = 9 end
- for i = 14, 31 do textSizeX[string.char(i)] = 9 end
- for i = 127, 159 do textSizeX[string.char(i)] = 9 end
- textSizeX["\173"] = 9
- local function canvasTextOffset(text, scale)
- local outX, outY, maxX = 0, 9, 0
- text:gsub("()(.)", function(p, c)
- if textSizeX[c] == false then
- if c == "\10" or c == "\13" then
- outY = outY + 9
- maxX = math.max(maxX, outX)
- outX = 0
- elseif c == "\9" then
- outX = math.ceil(outX / 16) * 16
- elseif c == "\167" then
- local n = text:sub(p + 1, p + 1)
- if textSizeX[n] then --cancel next character
- outX = outX - textSizeX[n]
- else
- outX = outX - 6
- end
- end
- elseif textSizeX[c] then
- outX = outX + textSizeX[c]
- else
- outX = outX + 6
- end
- end)
- return outX * scale, outY * scale, math.max(maxX, outX) * scale
- end
- local function cwailaBoxItem(block, scale)
- scale = scale or 1
- local text = block.displayName .. "\n" .. string.match(block.name, "^([^:]+)"):gsub("^.", string.upper)
- local _, offy, offx = canvasTextOffset(text, scale)
- local center = (sizeX - offx) / 2
- local thumbnail = 20 * scale
- canvas2d.addRectangle(center - thumbnail - 5, 10 - 5, offx + thumbnail + 10, offy + 10, 0x00000050 )
- 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 )
- canvas2d.addText({center, 10}, block.displayName, 0xFFFFFFFF, scale).setShadow(true)
- canvas2d.addText({center, 10 + 9 * scale}, string.match(block.name, "^([^:]+)"):gsub("^.", string.upper), 0x5349CBFF, scale).setShadow(true)
- pcall(canvas2d.addItem, {center - thumbnail, 10}, block.name, block.metadata, scale)
- end
- local function cwailaBoxEntity(entity, scale)
- scale = scale or 1
- local text = entity.displayName .. (entity.health and "\n" .. string.format("%d / %d", entity.health, entity.maxHealth) or "")
- local _, offy, offx = canvasTextOffset(text, scale)
- local center = (sizeX - offx) / 2
- canvas2d.addRectangle(center - 5, 10 - 5, offx + 10, offy + 10, 0x00000090 )
- 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 )
- canvas2d.addText({center, 10}, entity.displayName, 0xFFFFFFFF, scale).setShadow(true)
- if entity.health then canvas2d.addText({center, 10 + 9 * scale}, string.format("%d / %d", entity.health, entity.maxHealth), 0xFE130FFF, scale).setShadow(true) end
- end
- --# Main ray casting code
- local function point()
- local last
- while true do
- local scanned_blocks = modules.scan()
- local ent = modules.sense()
- local meta = modules.getMetaOwner()
- local entities = {}
- for _, k in pairs(ent) do
- if k.id ~= meta.id then
- entities[position(k)] = k
- end
- end
- for current in position.iterateBlocks(position.pitchYawVector(meta.pitch, meta.yaw), position(meta.withinBlock)) do
- local pos = current:floor()
- local num = 17 ^ 2 * (pos.x + 8) + 17 * (pos.y + 8) + (pos.z + 8) + 1 or 0
- local block = scanned_blocks[num]
- local entHit
- for i, k in pairs(entities) do
- if current:distance(i) < 1 then entHit = k break end
- end
- if math.abs(pos.x) > range or math.abs(pos.y) > range or math.abs(pos.z) > range then
- if last then canvas2d.clear() last = nil end
- break
- elseif entHit then
- if last == entHit.id then break end
- last = entHit.id
- local ok, meta = pcall(modules.getMetaByID, last)
- if not ok or not meta then last = nil break end
- canvas2d.clear()
- cwailaBoxEntity(meta, scale)
- break
- elseif block and block.name ~= "minecraft:air" then
- if last == num then break end
- last = num
- local ok, meta = pcall(modules.getBlockMeta, pos.x, pos.y, pos.z)
- if not ok or not meta or meta.name == "minecraft:air" then last = nil break end
- canvas2d.clear()
- cwailaBoxItem(meta, scale)
- break
- end
- end
- sleep(0.5)
- end
- end
- print("Active")
- print(pcall(point))
- canvas2d.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement