[furnace.lua|workbench.lua] Code splitted from blocks.lua

This commit is contained in:
Quentin Bazin 2019-01-08 02:39:07 +01:00
parent 66da21e20e
commit a17df4205f
5 changed files with 142 additions and 138 deletions

1
TODO
View File

@ -47,6 +47,7 @@ TODO
# World
• TODO: Block metadata
• TODO: Day/night cycle
# Chunk generation

View File

@ -96,144 +96,8 @@ mod:block {
texture = 218,
}
mod:block {
id = "workbench",
name = "Workbench",
texture = 77,
on_block_activated = function(pos, player, world)
local gui = LuaGUI.new()
-- FIXME: Replace this by gui:set_size() and gui:set_centered()
local gui_pos = {
x = SCREEN_WIDTH / GUI_SCALE / 2.0 - 176 / 2.0,
y = SCREEN_HEIGHT / GUI_SCALE / 2.0 - 166 / 2.0
}
gui:button {
name = "btn_hello",
pos = {x = 0, y = 0},
text = "Test button",
on_click = function(self)
print("Test button pressed")
end,
}
gui:inventory {
name = "inv_main",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 83},
player = "player",
inventory = "main",
size = {x = 9, y = 3},
offset = 9,
count = 9 * 3,
}
gui:inventory {
name = "inv_hotbar",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 141},
player = "player",
inventory = "main",
size = {x = 9, y = 1},
offset = 0,
count = 9,
}
gui:crafting {
name = "inv_crafting",
pos = {x = gui_pos.x, y = gui_pos.y},
block = {x = pos.x, y = pos.y, z = pos.z},
offset = 0,
}
gui:image {
name = "img_background",
pos = gui_pos,
texture = "texture-workbench",
clip = {x = 0, y = 0, width = 176, height = 166},
}
gui:show()
end,
}
mod:block {
id = "furnace",
name = "Furnace",
texture = 164,
on_block_activated = function(pos, player, world)
-- FIXME: Replace this by gui:set_size() and gui:set_centered()
local gui_pos = {
x = SCREEN_WIDTH / GUI_SCALE / 2.0 - 176 / 2.0,
y = SCREEN_HEIGHT / GUI_SCALE / 2.0 - 166 / 2.0
}
local gui = LuaGUI.new()
gui:furnace {
name = "furnace",
pos = gui_pos,
block = {x = pos.x, y = pos.y, z = pos.z}
}
gui:show()
end,
on_tick = function(pos, player, chunk, world)
local data = world:get_block_data(pos.x, pos.y, pos.z)
if not data then return end
local input_stack = data.inventory:get_stack(0, 0)
local output_stack = data.inventory:get_stack(1, 0)
local fuel_stack = data.inventory:get_stack(2, 0)
local ticks_remaining = bit.band(data.data, 0xfff)
local current_burn_time = bit.band(bit.brshift(data.data, 12), 0xfff)
local item_progress = bit.band(bit.brshift(data.data, 24), 0xff)
-- print(tostring(ticks_remaining).." "..tostring(current_burn_time).." "..tostring(item_progress))
local recipe = openminer:registry():get_recipe(data.inventory)
if recipe and recipe:type() ~= "smelt" then
recipe = nil
end
if ticks_remaining == 0 and recipe and fuel_stack:amount() > 0 and
(output_stack:item():id() == 0 or output_stack:amount() == 0
or output_stack:item():id() == recipe:result():item():id()) then
ticks_remaining = fuel_stack:item():burn_time()
current_burn_time = fuel_stack:item():burn_time()
world:set_data(pos.x, pos.y, pos.z, 1)
elseif ticks_remaining > 0 then
ticks_remaining = ticks_remaining - 1
if recipe and (output_stack:item():id() == 0 or output_stack:amount() == 0
or output_stack:item():id() == recipe:result():item():id()) then
item_progress = item_progress + 1
else
item_progress = 0
end
elseif ticks_remaining == 0 then
current_burn_time = 0
world:set_data(pos.x, pos.y, pos.z, 0)
end
if item_progress >= 200 and recipe then
item_progress = 0;
data.inventory:set_stack(0, 0, (input_stack:amount() - 1 > 0) and input_stack:item():name() or "", input_stack:amount() - 1)
data.inventory:set_stack(1, 0, recipe:result():item():name(), output_stack:amount() + recipe:result():amount())
end
local a = bit.blshift(bit.band(current_burn_time, 0xfff), 12)
local b = bit.blshift(bit.band(item_progress, 0xff), 24)
data.data = bit.band(ticks_remaining, 0xfff) + a + b
end,
}
dofile("mods/workbench.lua")
dofile("mods/furnace.lua")
mod:block {
id = "ore_iron",

71
mods/furnace.lua Normal file
View File

@ -0,0 +1,71 @@
mod:block {
id = "furnace",
name = "Furnace",
texture = 164,
on_block_activated = function(pos, player, world)
-- FIXME: Replace this by gui:set_size() and gui:set_centered()
local gui_pos = {
x = SCREEN_WIDTH / GUI_SCALE / 2.0 - 176 / 2.0,
y = SCREEN_HEIGHT / GUI_SCALE / 2.0 - 166 / 2.0
}
local gui = LuaGUI.new()
gui:furnace {
name = "furnace",
pos = gui_pos,
block = {x = pos.x, y = pos.y, z = pos.z}
}
gui:show()
end,
on_tick = function(pos, player, chunk, world)
local data = world:get_block_data(pos.x, pos.y, pos.z)
if not data then return end
local input_stack = data.inventory:get_stack(0, 0)
local output_stack = data.inventory:get_stack(1, 0)
local fuel_stack = data.inventory:get_stack(2, 0)
local ticks_remaining = bit.band(data.data, 0xfff)
local current_burn_time = bit.band(bit.brshift(data.data, 12), 0xfff)
local item_progress = bit.band(bit.brshift(data.data, 24), 0xff)
-- print(tostring(ticks_remaining).." "..tostring(current_burn_time).." "..tostring(item_progress))
local recipe = openminer:registry():get_recipe(data.inventory)
if recipe and recipe:type() ~= "smelt" then
recipe = nil
end
if ticks_remaining == 0 and recipe and fuel_stack:amount() > 0 and
(output_stack:item():id() == 0 or output_stack:amount() == 0
or output_stack:item():id() == recipe:result():item():id()) then
ticks_remaining = fuel_stack:item():burn_time()
current_burn_time = fuel_stack:item():burn_time()
world:set_data(pos.x, pos.y, pos.z, 1)
elseif ticks_remaining > 0 then
ticks_remaining = ticks_remaining - 1
if recipe and (output_stack:item():id() == 0 or output_stack:amount() == 0
or output_stack:item():id() == recipe:result():item():id()) then
item_progress = item_progress + 1
else
item_progress = 0
end
elseif ticks_remaining == 0 then
current_burn_time = 0
world:set_data(pos.x, pos.y, pos.z, 0)
end
if item_progress >= 200 and recipe then
item_progress = 0;
data.inventory:set_stack(0, 0, (input_stack:amount() - 1 > 0) and input_stack:item():name() or "", input_stack:amount() - 1)
data.inventory:set_stack(1, 0, recipe:result():item():name(), output_stack:amount() + recipe:result():amount())
end
local a = bit.blshift(bit.band(current_burn_time, 0xfff), 12)
local b = bit.blshift(bit.band(item_progress, 0xff), 24)
data.data = bit.band(ticks_remaining, 0xfff) + a + b
end,
}

View File

@ -9,6 +9,8 @@ mod = LuaMod.new("default")
dofile("mods/bit.lua") -- FIXME
dofile("mods/blocks.lua")
dofile("mods/workbench.lua")
dofile("mods/furnace.lua")
dofile("mods/items.lua")
dofile("mods/recipes.lua")

66
mods/workbench.lua Normal file
View File

@ -0,0 +1,66 @@
mod:block {
id = "workbench",
name = "Workbench",
texture = 77,
on_block_activated = function(pos, player, world)
local gui = LuaGUI.new()
-- FIXME: Replace this by gui:set_size() and gui:set_centered()
local gui_pos = {
x = SCREEN_WIDTH / GUI_SCALE / 2.0 - 176 / 2.0,
y = SCREEN_HEIGHT / GUI_SCALE / 2.0 - 166 / 2.0
}
gui:button {
name = "btn_test",
pos = {x = 0, y = 0},
text = "Test button",
on_click = function(self)
print("Test button pressed")
end,
}
gui:inventory {
name = "inv_main",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 83},
player = "player",
inventory = "main",
size = {x = 9, y = 3},
offset = 9,
count = 9 * 3,
}
gui:inventory {
name = "inv_hotbar",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 141},
player = "player",
inventory = "main",
size = {x = 9, y = 1},
offset = 0,
count = 9,
}
gui:crafting {
name = "inv_crafting",
pos = {x = gui_pos.x, y = gui_pos.y},
block = {x = pos.x, y = pos.y, z = pos.z},
offset = 0,
}
gui:image {
name = "img_background",
pos = gui_pos,
texture = "texture-workbench",
clip = {x = 0, y = 0, width = 176, height = 166},
}
gui:show()
end,
}