fisher using state machine
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
local machine = require('neural.state')
|
||||
|
||||
local device = _G.device
|
||||
local os = _G.os
|
||||
|
||||
@@ -19,51 +21,98 @@ local depth = -3
|
||||
local icon
|
||||
local scales = { .2, .4, .6, .8, 1, .8, .6, .4 }
|
||||
local scale = 0
|
||||
local w, h
|
||||
|
||||
if canvas then
|
||||
local w, h = canvas.getSize()
|
||||
w, h = canvas.getSize()
|
||||
icon = canvas.addItem({ w - 20, h - 20 }, 'minecraft:fishing_rod' )
|
||||
end
|
||||
|
||||
local fsm = machine.create({
|
||||
events = {
|
||||
{ name = 'startup', from = 'none', to = 'wait' },
|
||||
{ name = 'rod', from = 'wait', to = 'idle' },
|
||||
{ name = 'norod', from = 'idle', to = 'wait' },
|
||||
{ name = 'norod', from = 'fishing', to = 'wait' },
|
||||
{ name = 'cast', from = 'idle', to = 'fishing' },
|
||||
{ name = 'reel', from = 'fishing', to = 'idle' },
|
||||
},
|
||||
|
||||
callbacks = {
|
||||
-- events
|
||||
oncast = function()
|
||||
kinetic.use(.2)
|
||||
os.sleep(.5)
|
||||
local meta = sensor.getMetaByName('unknown')
|
||||
depth = meta and meta.y - .5 or depth
|
||||
return true
|
||||
end,
|
||||
|
||||
onreel = function()
|
||||
kinetic.use(.3)
|
||||
os.sleep(.5)
|
||||
return true
|
||||
end,
|
||||
|
||||
-- state changes
|
||||
onenterwait = function()
|
||||
print('waitng for fishing rod to be selected')
|
||||
if icon then
|
||||
icon.remove()
|
||||
icon = canvas.addItem({ w - 20, h - 20 }, 'minecraft:fishing_rod' )
|
||||
end
|
||||
end,
|
||||
|
||||
onleavewait = function()
|
||||
print('fishing...')
|
||||
end,
|
||||
|
||||
onenterfishing = function()
|
||||
if icon then
|
||||
icon.remove()
|
||||
scale = 0
|
||||
icon = canvas.addItem({ w - 20, h - 20 }, 'minecraft:fish', math.random(0, 3) )
|
||||
end
|
||||
end,
|
||||
}
|
||||
})
|
||||
|
||||
local function isHoldingRod()
|
||||
local owner = sensor.getMetaOwner()
|
||||
local held = owner.heldItem and owner.heldItem.getMetadata()
|
||||
return held and held.rawName == 'item.fishingRod'
|
||||
end
|
||||
|
||||
local function fish()
|
||||
fsm:startup()
|
||||
while true do
|
||||
local meta = sensor.getMetaByName('unknown')
|
||||
if not meta then
|
||||
local owner = sensor.getMetaOwner()
|
||||
local held = owner.heldItem and owner.heldItem.getMetadata()
|
||||
if held and held.rawName == 'item.fishingRod' then
|
||||
if icon then
|
||||
icon.setItem('minecraft:fish', math.random(0, 3))
|
||||
end
|
||||
kinetic.use(.2)
|
||||
print('casting')
|
||||
os.sleep(.5)
|
||||
meta = sensor.getMetaByName('unknown')
|
||||
depth = meta and meta.y - .5 or depth
|
||||
else
|
||||
if icon then
|
||||
icon.setItem('minecraft:fishing_rod')
|
||||
icon.setScale(1)
|
||||
end
|
||||
print('waiting for fishing rod to be selected')
|
||||
end
|
||||
os.sleep(1)
|
||||
else
|
||||
if meta.y < depth then
|
||||
kinetic.use(.3)
|
||||
print('reeled in')
|
||||
end
|
||||
if icon then
|
||||
scale = scale + 1
|
||||
icon.setScale(scales[(scale % #scales) + 1])
|
||||
if isHoldingRod() then
|
||||
fsm:rod()
|
||||
if not meta then
|
||||
fsm:cast()
|
||||
elseif meta and meta.y < depth then
|
||||
fsm:reel()
|
||||
end
|
||||
os.sleep(.1)
|
||||
else
|
||||
fsm:norod()
|
||||
os.sleep(1)
|
||||
end
|
||||
|
||||
if icon and fsm.current == 'fishing' then
|
||||
scale = scale + 1
|
||||
icon.setScale(scales[(scale % #scales) + 1])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
pcall(fish)
|
||||
local s, m = pcall(fish)
|
||||
|
||||
if icon then
|
||||
icon.remove()
|
||||
end
|
||||
end
|
||||
|
||||
if not s and m then
|
||||
error(m)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user