cleanup + games

This commit is contained in:
kepler155c@gmail.com
2019-01-17 22:01:40 -05:00
parent 6416e67130
commit ef5eab65f9
8 changed files with 422 additions and 61 deletions

View File

@@ -136,7 +136,7 @@ function turtle.level(startPt, endPt, firstPt, verbose)
turtle.set({
digPolicy = dig,
attackPolicy = 'attack',
move = 'moveAssured',
movePolicy = 'moveAssured',
})

6
games/.package Normal file
View File

@@ -0,0 +1,6 @@
{
title = 'Games',
repository = 'kepler155c/opus-apps/{{OPUS_BRANCH}}/games',
description = [[Games by various people]],
licence = 'MIT',
}

350
games/sameGame.lua Normal file
View File

@@ -0,0 +1,350 @@
--Same Game for CraftOS 1.0.0 (ShinyCube) (Advanced Computer)
-- slight modifications to run on a kiosk
local score, A, B, C, D, E
local board = {}
local selected = {}
local backup_board = {}
local backup_score
local backup_exists = false
local cnt_selected
local selected_color
local is_gameover
local best_scores = {}
local best_score_names = {}
local best_score_view = false
function init()
loadScore()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
for i = 1, 10 do
board[i] = {}
selected[i] = {}
backup_board[i] = {}
end
newGame()
eventLoop()
end
function eventLoop()
while true do
local event, button, x, y = os.pullEvent()
if event == "mouse_click" then
if best_score_view then
if y == 1 and 44 <= x and x <= 51 then
best_score_view = false
redraw()
end
else
if x >= 7 and x <= 46 and y >=8 and y <= 17 then
local j = math.floor((x-7)/2) + 1
local i = (y-8) + 1
clicked(i,j)
end
if y == 1 and 1 <= x and x <= 7 then
newGame()
end
if y == 1 and 9 <= x and x <= 16 then
undo()
redraw()
end
if y == 1 and 18 <= x and x <= 31 then
showBestScore()
end
if y == 1 and 33 <= x and x <= 42 then
redraw()
end
if y == 1 and 44 <= x and x <= 51 then
term.clear()
term.setCursorPos(1,1)
break
end
end
end
end
end
function newGame()
score = 0
A = 0
B = 0
C = 0
D = 0
E = 0
cnt_selected = 0
is_gameover = false
backup_exists = false
for i = 1, 10 do
for j = 1, 20 do
board[i][j] = math.random(5)
if(board[i][j] == 1) then A = A + 1 end
if(board[i][j] == 2) then B = B + 1 end
if(board[i][j] == 3) then C = C + 1 end
if(board[i][j] == 4) then D = D + 1 end
if(board[i][j] == 5) then E = E + 1 end
selected[i][j] = false
end
end
redraw()
end
function redraw()
redraw_term(term)
end
function redraw_term(term)
if best_score_view then
term.setCursorPos(1,1) term.write(" [ BACK ]")
else
term.setCursorPos(1,1) term.write("[ NEW ] [ UNDO ] [ HIGH SCORE ] [ SCREEN ] [ EXIT ]")
end
term.setCursorPos(16,3) term.write("Same Game for Craft OS")
term.setCursorPos(15,5) term.write("Implemented by ShinyCube")
term.setCursorPos(3,19) term.write("Score: A: B: C: D: E: ")
if best_score_view then
for i = 1, 10 do
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.setCursorPos(7,8+(i-1))
term.write(string.format("%2d. ...............................%5d",i,best_scores[i]))
term.setCursorPos(11,8+(i-1))
term.write(best_score_names[i])
end
else
for i = 1, 10 do
for j = 1, 20 do
term.setCursorPos(7+(j-1)*2,8+(i-1))
if board[i][j] == 0 then
term.blit(". ","00","ff")
elseif board[i][j] == 1 then
if selected[i][j] then
term.blit("A ","aa","00")
else
term.blit("A ","00","aa")
end
elseif board[i][j] == 2 then
if selected[i][j] then
term.blit("B ","bb","00")
else
term.blit("B ","00","bb")
end
elseif board[i][j] == 3 then
if selected[i][j] then
term.blit("C ","cc","00")
else
term.blit("C ","00","cc")
end
elseif board[i][j] == 4 then
if selected[i][j] then
term.blit("D ","dd","00")
else
term.blit("D ","00","dd")
end
elseif board[i][j] == 5 then
if selected[i][j] then
term.blit("E ","ee","00")
else
term.blit("E ","00","ee")
end
end
end
end
end
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.setCursorPos(22,7)
if is_gameover then
term.write("GAME OVER")
else
term.write(" ")
end
term.setCursorPos(9,19)
term.write(" ")
term.setCursorPos(9,19)
term.write(score)
if cnt_selected > 0 then
term.write("+" .. cnt_selected*cnt_selected-3*cnt_selected+4)
end
term.setCursorPos(23,19)
term.write(A)
term.setCursorPos(29,19)
term.write(B)
term.setCursorPos(35,19)
term.write(C)
term.setCursorPos(41,19)
term.write(D)
term.setCursorPos(47,19)
term.write(E)
end
function deselectAll()
for i = 1, 10 do
for j = 1, 20 do
selected[i][j] = false
end
end
cnt_selected = 0
end
function rec_selection(i,j)
if not selected[i][j] then
selected[i][j] = true
cnt_selected = cnt_selected + 1
if i-1 >= 1 and board[i][j] == board[i-1][j] then rec_selection(i-1,j) end
if i+1 <= 10 and board[i][j] == board[i+1][j] then rec_selection(i+1,j) end
if j-1 >= 1 and board[i][j] == board[i][j-1] then rec_selection(i,j-1) end
if j+1 <= 20 and board[i][j] == board[i][j+1] then rec_selection(i,j+1) end
end
end
function backup()
for i = 1, 10 do
for j = 1, 20 do
backup_board[i][j] = board[i][j]
backup_score = score
end
end
backup_exists = true
end
function removeSelected()
local di, dj
dj = 1
for sj = 1, 20 do
di = 10
for si = 10, 1, -1 do
if not selected[si][sj] then
board[di][dj] = board[si][sj]
di = di - 1
end
end
for di = di, 1, -1 do
board[di][dj] = 0
end
if board[10][dj] ~= 0 then dj = dj + 1 end
end
for dj = dj, 20 do
for di = 1, 10 do
board[di][dj] = 0
end
end
end
function checkGameOver()
for i = 1, 10 do
for j = 1, 20 do
if i-1>=1 and board[i][j] > 0 and board[i][j] == board[i-1][j] then return false end
if i+1<=10 and board[i][j] > 0 and board[i][j] == board[i+1][j] then return false end
if j-1>=1 and board[i][j] > 0 and board[i][j] == board[i][j-1] then return false end
if j+1<=20 and board[i][j] > 0 and board[i][j] == board[i][j+1] then return false end
end
end
return true
end
function loadScore()
local file = fs.open("same.dat","r")
if file then
for i = 1, 10 do
best_score_names[i] = file.readLine() or "NONAME"
best_scores[i] = tonumber(file.readLine()) or 0
end
file.close()
else
for i = 1, 10 do
best_score_names[i] = "NONAME"
best_scores[i] = 0
end
end
end
function saveScore()
local file = fs.open("same.dat","w")
if file then
for i = 1, 10 do
file.writeLine(best_score_names[i])
file.writeLine(best_scores[i])
end
file.flush()
end
end
function updateScore()
local rank = 1
for i = 10, 1, -1 do
if best_scores[i] < score then
best_score_names[i+1] = best_score_names[i]
best_scores[i+1] = best_scores[i]
else
rank = i + 1
break
end
end
if rank <= 10 then
best_score_names[rank] = getName(rank, score)
best_scores[rank] = score
saveScore()
best_score_view = true
redraw()
end
end
function getName(rank, score)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
print("Congratulation!")
print("You got a high score!")
print("Your score: " .. score)
print("Your rank: " .. rank)
print("Type your name. >")
local name = '...'
name = string.sub(name, 1, 30)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.clear()
return name
end
function undo()
if backup_exists then
deselectAll()
backup_exists = false
score = backup_score
for i = 1, 10 do
for j = 1, 20 do
board[i][j] = backup_board[i][j]
end
end
end
end
function showBestScore()
best_score_view = true
redraw()
end
function clicked(ci,cj)
if selected[ci][cj] then
backup()
score = score + cnt_selected*cnt_selected-3*cnt_selected+4
if selected_color == 1 then A = A - cnt_selected
elseif selected_color == 2 then B = B - cnt_selected
elseif selected_color == 3 then C = C - cnt_selected
elseif selected_color == 4 then D = D - cnt_selected
elseif selected_color == 5 then E = E - cnt_selected
end
removeSelected()
deselectAll()
if checkGameOver() then
is_gameover = true
backup_exists = false
updateScore()
redraw()
else
redraw()
end
else
if cnt_selected > 0 then
deselectAll()
redraw()
else
if board[ci][cj] > 0 then
selected_color = board[ci][cj]
rec_selection(ci,cj)
if cnt_selected == 1 then
deselectAll()
end
redraw()
end
end
end
end
init()

View File

@@ -3,7 +3,6 @@ local class = require('class')
local Config = require('config')
local Event = require('event')
local itemDB = require('itemDB')
local sync = require('sync').sync
local Util = require('util')
local device = _G.device
@@ -70,7 +69,7 @@ function Storage:init()
_G._debug('%s: %s', e, tostring(dev))
self:initStorage()
end)
Event.onInterval(15, function()
Event.onInterval(60, function()
self:showStorage()
end)
end
@@ -103,7 +102,6 @@ end
function Storage:initStorage()
local online = true
_G._debug('Initializing storage')
for k,v in pairs(self.nodes) do
if v.mtype ~= 'hidden' then
if v.adapter then
@@ -236,51 +234,50 @@ end
-- provide a consolidated list of items
function Storage:listItems(throttle)
--sync(self, function()
if not self.dirty then
return self.cache
if not self.dirty then
return self.cache
end
local timer = Timer()
local cache = { }
throttle = throttle or Util.throttle()
local t = { }
for _, adapter in self:onlineAdapters() do
if adapter.dirty then
table.insert(t, function()
adapter:listItems(throttle)
adapter.dirty = false
end)
end
end
local timer = Timer()
local cache = { }
throttle = throttle or Util.throttle()
if #t > 0 then
parallel.waitForAll(table.unpack(t))
end
local t = { }
for _, adapter in self:onlineAdapters() do
if adapter.dirty then
table.insert(t, function()
adapter:listItems(throttle)
adapter.dirty = false
end)
for _, adapter in self:onlineAdapters() do
local rcache = adapter.cache or { }
for key,v in pairs(rcache) do
local entry = cache[key]
if not entry then
entry = Util.shallowCopy(v)
entry.count = v.count
entry.key = key
cache[key] = entry
else
entry.count = entry.count + v.count
end
throttle()
end
end
itemDB:flush()
_G._debug('STORAGE: refresh ' .. #t .. ' inventories in ' .. timer())
if #t > 0 then
parallel.waitForAll(table.unpack(t))
end
self.dirty = false
self.cache = cache
for _, adapter in self:onlineAdapters() do
local rcache = adapter.cache or { }
for key,v in pairs(rcache) do
local entry = cache[key]
if not entry then
entry = Util.shallowCopy(v)
entry.count = v.count
entry.key = key
cache[key] = entry
else
entry.count = entry.count + v.count
end
throttle()
end
end
itemDB:flush()
_G._debug('STORAGE: refresh ' .. #t .. ' inventories in ' .. timer())
self.dirty = false
self.cache = cache
--end)
return self.cache
end
@@ -389,7 +386,7 @@ local function rawExport(source, target, item, qty, slot)
end)
if not s and m then
_debug(m)
_G._debug(m)
end
return total, m
@@ -448,7 +445,7 @@ local function rawInsert(source, target, slot, qty)
end
end)
if not s and m then
_debug(m)
_G._debug(m)
end
return count

View File

@@ -8,7 +8,12 @@ local InputChest = {
function InputChest:cycle(context)
for node in context.storage:filterActive('input') do
for slot, item in pairs(node.adapter.list()) do
context.storage:import(node, slot, item.count, item)
local s, m = pcall(function()
context.storage:import(node, slot, item.count, item)
end)
if not s and m then
_G._debug('INPUT error: ' .. m)
end
end
end
end

View File

@@ -28,7 +28,7 @@ local function locate()
end
local spt = GPS.getPoint() or error('GPS failure')
local chestPoint -- location of chest
local chestPoint -- location of chest
local blockTypes = { } -- blocks types requested to mine
local turtles = { } -- active turtles
local pool = { } -- all turtles
@@ -39,7 +39,7 @@ local function hijackTurtle(remoteId)
local socket, msg = Socket.connect(remoteId, 188)
if not socket then
printError(remoteId)
_G.printError(remoteId)
error(msg)
end
@@ -72,7 +72,7 @@ end
local function run(member, point)
Event.addRoutine(function()
--local _, m = pcall(function()
local _, m = pcall(function()
member.active = true
local turtle = hijackTurtle(member.id)
@@ -159,6 +159,8 @@ local function run(member, point)
until member.abort
end
emptySlots(blockTypes, Point.above(turtle.getPoint()))
if chestPoint then
dropOff()
while not turtle._goto(Point.above(spt)) do
@@ -170,9 +172,9 @@ local function run(member, point)
turtle.gotoY(spt.y)
turtle._goto(spt)
end
--end)
end)
turtles[member.id] = nil
--member.status = m
member.status = m
member.active = false
end)
end
@@ -233,7 +235,6 @@ end
function page:scan()
local gpt = GPS.getPoint()
if not gpt then
_debug('gps failed')
return
end
local rawBlocks = scanner:scan()
@@ -384,6 +385,7 @@ end)
Event.onTimeout(.5, function()
page:scan()
blocksTab.grid:setValues(page.totals)
page:sync()
end)
UI:setPage(page)

View File

@@ -20,6 +20,7 @@ local fs = _G.fs
local os = _G.os
local peripheral = _G.peripheral
local read = _G.read
local term = _G.term
local turtle = _G.turtle
UI:configure('scanningMiner', ...)

View File

@@ -170,7 +170,7 @@ local function addTrash()
local slots = turtle.getFilledSlots()
for _,slot in pairs(slots) do
trash[slot.iddmg] = true
trash[slot.key] = true
end
trash['minecraft:bucket:0'] = nil
@@ -300,11 +300,11 @@ local function normalChestUnload()
end
local slots = turtle.getFilledSlots()
for _,slot in pairs(slots) do
if not trash[slot.iddmg] and
slot.iddmg ~= 'minecraft:bucket:0' and
slot.id ~= 'minecraft:diamond_pickaxe' and
slot.id ~= 'cctweaks:toolHost' then
if slot.id ~= options.fortunePick.value then
if not trash[slot.key] and
slot.key ~= 'minecraft:bucket:0' and
slot.name ~= 'minecraft:diamond_pickaxe' and
slot.name ~= 'cctweaks:toolHost' then
if slot.name ~= options.fortunePick.value then
turtle.select(slot.index)
turtle.dropUp(64)
end
@@ -325,7 +325,7 @@ local function ejectTrash()
local cobbleSlotCount = 0
turtle.eachFilledSlot(function(slot)
if slot.iddmg == 'minecraft:cobblestone:0' then
if slot.key == 'minecraft:cobblestone:0' then
if cobbleSlotCount == 0 and slot.count > 36 then
turtle.select(slot.index)
turtle.dropDown(slot.count - 36)
@@ -333,9 +333,9 @@ local function ejectTrash()
cobbleSlotCount = cobbleSlotCount + 1
end
if trash[slot.iddmg] then
if trash[slot.key] then
-- retain 1 slot with cobble in order to indicate active mining
if slot.iddmg ~= 'minecraft:cobblestone:0' or cobbleSlotCount > 1 then
if slot.key ~= 'minecraft:cobblestone:0' or cobbleSlotCount > 1 then
turtle.select(slot.index)
turtle.dropDown(64)
end
@@ -603,7 +603,7 @@ if options.fortunePick.value then
if not turtle.getSlot('cctweaks:toolHost:0') then
error('CCTweaks tool host not found')
end
trash[s.iddmg] = nil
trash[s.key] = nil
trash['minecraft:diamond_pickaxe:0'] = nil
trash['cctweaks:toolHost:0'] = nil
end