people/presets/Miner.lua

232 lines
8.1 KiB
Lua

local MODE_selling = 1 -- Sell stuff
local MODE_inventory = 2 -- Manage our inventory
local MODE_mining = 3 -- Do mining
local MODE_exitmine = 4 -- Get out of the mine
local MODE_minebuilding = 5 -- Build the mine building
local MODE_homebuilding = 6 -- Build a home
local MODE_panic = 7 -- Don't know what to do
local inventory_actions = {
-- All these have a prevaction on to prevent them raising errors.
-- We still (probably) want to try and carry on mining if we're
-- missing some materials - maybe we'll be able to get them later.
{"obtain", item="stairs:stair_cobble", num=40, prevaction={"wait", time=1}},
{"obtain", item="default:stone", num=40, prevaction={"wait", time=1}},
{"obtain", item="default:wood", num=40, prevaction={"wait", time=1}},
{"obtain", item="stairs:slab_wood", num=20, prevaction={"wait", time=1}},
{"obtain", item="default:stick", num=40, prevaction={"wait", time=1}},
{"obtain", item="default:torch", num=20, prevaction={"wait", time=1}},
{"go", name="Jeiffel Recycle Bin", successaction={"stash",
dest="default:chest",
items={{"default:dirt", keep=50},
{"default:cobble", keep=50},
"default:snow",
"default:gravel"
}
}},
{"go", name="Miner Willy's"}
}
if event.type == "program" then
mem.mode = MODE_selling
mem.cur = 1
mem.lastxmode = MODE_mining
mem.needed = {}
elseif event.type == "actfail" then
if mem.mode == MODE_inventory or mem.mode == MODE_selling or
mem.mode == MODE_minebuilding or mem.mode == MODE_homebuilding then
if type(event.failcode) == "table" and event.failcode[1] == "need" then
table.insert(mem.needed, event.failcode.item)
end
action = {"wait", time=5}
mem.mode = MODE_selling
mem.cur = 1
elseif mem.mode == MODE_mining then
mem.mode = MODE_exitmine
mem.cur = #mem.exitmine
elseif mem.mode == MODE_exitmine then
-- If something goes wrong when exiting the mine, just keep
-- trying
action = {"wait", time=5}
mem.cur = mem.cur + 1
else
mem.mode = MODE_panic
end
elseif event.type == "act" then
if mem.mode == MODE_selling then
if carrying("default:iron_lump") > 0 then
action = {"go", name="Mineral Shop", successaction={"sell", item="default:iron_lump"}}
return
end
-- TODO - need to sell excess coal here, but the sell action currently
-- doesn't allow an amount to be specified, so we'd sell it all!
mem.mode = MODE_inventory
mem.cur = 1
action = {"wait", time=1}
elseif mem.mode == MODE_inventory then
if #mem.needed > 0 then
action={"obtain", item=mem.needed[1], num=1, prevaction={"wait", time=1}}
table.remove(mem.needed, 1)
return
end
action = inventory_actions[mem.cur]
mem.cur = mem.cur + 1
if mem.cur > #inventory_actions then
if mem.lastxmode == MODE_minebuilding then
mem.mode = MODE_mining
elseif mem.lastxmode == MODE_mining then
mem.mode = MODE_homebuilding
elseif mem.lastxmode == MODE_homebuilding then
mem.mode = MODE_mining
end
mem.lastxmode = mem.mode
mem.cur = 1
end
elseif mem.mode == MODE_mining then
if mem.cur == 1 then
action = {"go", name="Miner Willy's",
successaction={"gather", items={"default:torch"}}}
mem.exitmine = {}
mem.cur = 2
elseif mem.cur == 2 then
-- Select some materials to fancy up the tunnel, if we're carrying
-- them. If not, we'll do it without them and they'll get done
-- another time.
local steps = nil
local wall_and_floor = nil
local support_c = nil
local support_s = nil
local lighting = nil
if carrying("stairs:stair_cobble") > 3 then
steps = "stairs:stair_cobble"
if carrying("default:wood") > 3 then
support_c = "default:wood"
end
if carrying("stairs:slab_wood") > 3 then
support_s = "stairs:slab_wood"
end
if carrying("default:stone") > 10 then
wall_and_floor = "default:stone"
end
end
if carrying("default:torch") > 1 then
lighting = "default:torch"
end
action = {"tunnel", distance=40, dir={x=0,y=-1,z=-1}, steps=steps,
lighting=lighting, support_ceiling=support_c,
support_side=support_s,
support_side_facein=true,
wall=wall_and_floor, floor=wall_and_floor}
action.successaction= {"tunnel", distance=8, dir={x=0,y=0,z=-1},
lighting=lighting, floor=wall_and_floor,
ceiling=wall_and_floor}
action.successaction.successaction={"go", rel={x=1,y=0,z=2}}
table.insert(mem.exitmine, {"go", name="Miner Willy's"})
mem.cur = 3
elseif mem.cur == 3 then
action = {"tunnel", distance=20, dir={x=1,y=-1,z=0}, steps="stairs:stair_cobble",
lighting="default:torch", wall="default:stone", floor="default:stone"}
action.successaction=
{"tunnel", distance=8, dir={x=1,y=0,z=0}, lighting="default:torch", successaction=
{"go", rel={x=-1,y=0,z=1}} }
table.insert(mem.exitmine, {"go", pos=vector.subtract(pos, {x=1,y=0,z=0})})
mem.cur = 4
elseif mem.cur == 4 then
mem.mode = MODE_exitmine
mem.cur = #mem.exitmine
action = {"wait", time=1}
end
elseif mem.mode == MODE_exitmine then
if mem.cur == 0 or not mem.exitmine then
mem.exitmine = nil
mem.mode = MODE_selling
mem.cur = 1
action = {"gather"}
else
action = mem.exitmine[mem.cur]
mem.cur = mem.cur - 1
end
elseif mem.mode == MODE_minebuilding then
if mem.cur == 1 then
action = {"go", name="Miner Willy's"}
mem.cur = 2
elseif mem.cur == 2 then
action = {"building", dir={x=0,y=-1,z=-1},
width=5, depth=7, height=5,
wall="default:stone",
ceiling="default:wood",
outside_floor={"default:dirt", "default:dirt_with_grass"},
door="doors:door_wood"
}
mem.cur = 3
else
mem.mode = MODE_selling
mem.cur = 1
action = {"wait", time=5}
end
elseif mem.mode == MODE_homebuilding then
local home_jctname = "people_"..name.."_home"
if mem.cur == 1 then
-- Attempt to claim our plot. If we already have it, this will fail
-- and we'll just do the wait.
action = {"claimplot", name=home_jctname, prevaction={"wait", time=1}}
mem.cur = 2
elseif mem.cur == 2 then
action = {"go", name=home_jctname, faceexit="@build"}
mem.cur = 3
elseif mem.cur == 3 then
-- We'd better be facing the right way!!
local bdir = vector.round(vector.from_speed_yaw(1, yaw))
action = {"building", dir=bdir,
width=5, depth=5, height=4,
wall="default:stone",
ceiling="default:wood",
outside_floor={"default:dirt", "default:dirt_with_grass"},
door="doors:door_wood"
}
mem.cur = 4
else
mem.mode = MODE_selling
mem.cur = 1
action = {"wait", time=5}
end
else
action = {"wait", time=600}
end
end