diff --git a/machines/tool_workshop.lua b/machines/tool_workshop.lua
index 57e9e85..f55c1c6 100644
--- a/machines/tool_workshop.lua
+++ b/machines/tool_workshop.lua
@@ -15,9 +15,61 @@
-- along with this program. If not, see .
local S=minetest.get_translator("industrialtest")
-local toolWorkshop={}
+industrialtest.ToolWorkshop=table.copy(industrialtest.ActivatedElectricMachine)
+industrialtest.internal.unpackTableInto(industrialtest.ToolWorkshop,{
+ name="industrialtest:tool_workshop",
+ description=S("Tool Workshop"),
+ tiles={
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png^industrialtest_tool_workshop_front.png",
+ },
+ sounds="metal",
+ requiresWrench=true,
+ facedir=true,
+ storageLists={
+ "src",
+ "upgrades",
+ "powerStorage"
+ },
+ powerLists={
+ {
+ list="powerStorage",
+ direction="i"
+ }
+ },
+ active={
+ tiles={
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png",
+ "industrialtest_advanced_machine_block.png^industrialtest_tool_workshop_front_active.png",
+ }
+ },
+ capacity=15000,
+ flow=industrialtest.api.hvPowerFlow,
+ hasPowerInput=true,
+ ioConfig="iiiiii",
+ _opPower=10000,
+ _efficiency=200
+})
-toolWorkshop.getFormspec=function(pos)
+function industrialtest.ToolWorkshop.onConstruct(self,pos)
+ local meta=minetest.get_meta(pos)
+ local inv=meta:get_inventory()
+ inv:set_size("powerStorage",1)
+ inv:set_size("src",1)
+ inv:set_size("upgrades",4)
+ industrialtest.ActivatedElectricMachine.onConstruct(self,pos)
+end
+
+function industrialtest.ToolWorkshop.getFormspec(self,pos)
+ local parentFormspec=industrialtest.ActivatedElectricMachine.getFormspec(self,pos)
local meta=minetest.get_meta(pos)
local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100
local formspec={
@@ -31,79 +83,69 @@ toolWorkshop.getFormspec=function(pos)
industrialtest.internal.getItemSlotBg(9,0.9,1,4),
"listring[context;src]"
}
- return table.concat(formspec,"")
+ return parentFormspec..table.concat(formspec,"")
end
-toolWorkshop.onConstruct=function(pos,meta,inv)
- inv:set_size("powerStorage",1)
- inv:set_size("src",1)
- inv:set_size("upgrades",4)
-end
-
-toolWorkshop.onTimer=function(pos,elapsed,meta,inv)
- local powerStorageSlot=inv:get_stack("powerStorage",1)
- local toolSlot=inv:get_stack("src",1)
- local requiredPower=industrialtest.api.getMachineSpeed(meta)*10000
- local shouldRerunTimer=false
- local shouldUpdateFormspec=false
-
- if powerStorageSlot:get_count()>0 then
- local stackMeta=powerStorageSlot:get_meta()
- if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then
- shouldUpdateFormspec=true
- shouldRerunTimer=true
- industrialtest.api.updateItemPowerText(powerStorageSlot)
- inv:set_stack("powerStorage",1,powerStorageSlot)
+function industrialtest.ToolWorkshop.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
+ if toList=="src" then
+ local meta=minetest.get_meta(pos)
+ local inv=meta:get_inventory()
+ local movedItemStack=inv:get_stack(fromList,fromIndex)
+ if not self.isTool(movedItemStack) then
+ return 0
end
end
-
- if toolSlot:get_count()>0 and toolSlot:get_wear()>0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then
- minetest.swap_node(pos,{
- name="industrialtest:tool_workshop_active",
- param2=minetest.get_node(pos).param2
- })
- minetest.get_node_timer(pos):start(industrialtest.updateDelay)
- end
-
- return shouldRerunTimer,shouldUpdateFormspec
+ return industrialtest.ActivatedElectricMachine.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
end
-toolWorkshop.activeOnTimer=function(pos,elapsed,meta,inv)
- local powerStorageSlot=inv:get_stack("powerStorage",1)
+function industrialtest.ToolWorkshop.allowMetadataInventoryPut(self,pos,listname,index,stack,player)
+ if listname=="tool" and not self.isTool(stack) then
+ return 0
+ end
+ return industrialtest.ActivatedElectricMachine.allowMetadataInventoryPut(self,pos,listname,index,stack,player)
+end
+
+function industrialtest.ToolWorkshop.onMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
+ self:triggerIfNeeded(pos)
+ industrialtest.ActivatedElectricMachine.onMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
+end
+
+function industrialtest.ToolWorkshop.onMetadataInventoryPut(self,pos,listname,index,stack)
+ self:triggerIfNeeded(pos)
+ industrialtest.ActivatedElectricMachine.onMetadataInventoryPut(self,pos,listname,index,stack)
+end
+
+function industrialtest.ToolWorkshop.shouldActivate(self,pos)
+ local meta=minetest.get_meta(pos)
+
+ local requiredPower=industrialtest.api.getMachineSpeed(meta)*self._opPower
+ if meta:get_int("industrialtest.powerAmount")0
+end
+
+function industrialtest.ToolWorkshop.shouldDeactivate(self,pos)
+ return not self:shouldActivate(pos)
+end
+
+function industrialtest.ToolWorkshop.activeUpdate(self,pos,elapsed,meta,inv)
local toolSlot=inv:get_stack("src",1)
local speed=industrialtest.api.getMachineSpeed(meta)
- local requiredPower=speed*10000
- local shouldRerunTimer=false
- local shouldUpdateFormspec=false
- if powerStorageSlot:get_count()>0 then
- local stackMeta=powerStorageSlot:get_meta()
- if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then
- shouldUpdateFormspec=true
- shouldRerunTimer=true
- industrialtest.api.updateItemPowerText(powerStorageSlot)
- inv:set_stack("powerStorage",1,powerStorageSlot)
- end
- end
+ local requiredPower=industrialtest.api.getMachineSpeed(meta)*self._opPower
+ local removed=math.min(toolSlot:get_wear(),speed*self._efficiency)
+ toolSlot:set_wear(toolSlot:get_wear()-removed)
+ inv:set_stack("src",1,toolSlot)
+ industrialtest.api.addPower(meta,-requiredPower)
- if toolSlot:get_count()>0 and toolSlot:get_wear()>0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then
- local removed=math.min(toolSlot:get_wear(),speed*200)
- toolSlot:set_wear(toolSlot:get_wear()-removed)
- inv:set_stack("src",1,toolSlot)
- industrialtest.api.addPower(meta,-requiredPower)
- shouldRerunTimer=true
- shouldUpdateFormspec=true
- else
- minetest.swap_node(pos,{
- name="industrialtest:tool_workshop",
- param2=minetest.get_node(pos).param2
- })
- end
-
- return shouldRerunTimer,shouldUpdateFormspec
+ return true
end
-local function isTool(stack)
+function industrialtest.ToolWorkshop.isTool(stack)
local def=minetest.registered_tools[stack:get_name()]
if not def or industrialtest.api.hasPowerStorage(stack:get_meta())then
return false
@@ -111,76 +153,8 @@ local function isTool(stack)
return def.groups and (def.groups.pickaxe or def.groups.sword or def.groups.hoe or def.groups.tool or def.groups.weapon or def.groups.shovel or def.groups.axe)
end
-toolWorkshop.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toIndex,count)
- if toList=="src" then
- local meta=minetest.get_meta(pos)
- local inv=meta:get_inventory()
- local movedItemStack=inv:get_stack(fromList,fromIndex)
- if not isTool(movedItemStack) then
- return 0
- end
- end
- return count
-end
+industrialtest.ToolWorkshop:register()
-toolWorkshop.allowMetadataInventoryPut=function(pos,listname,index,stack)
- if listname=="tool" and not isTool(stack) then
- return 0
- end
- return stack:get_count()
-end
-
-toolWorkshop.metadataChange=function(pos)
- minetest.get_node_timer(pos):start(industrialtest.updateDelay)
-end
-
-industrialtest.internal.registerMachine({
- name="tool_workshop",
- displayName=S("Tool Workshop"),
- getFormspec=toolWorkshop.getFormspec,
- capacity=20000,
- flow=industrialtest.api.hvPowerFlow,
- ioConfig="iiiiii",
- requiresWrench=true,
- registerActiveVariant=true,
- powerSlots={"powerStorage"},
- storageSlots={"src"},
- sounds="metal",
- groups={
- _industrialtest_hasPowerInput=1
- },
- customKeys={
- tiles={
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png^industrialtest_tool_workshop_front.png",
- "industrialtest_advanced_machine_block.png"
- },
- paramtype2="facedir",
- legacy_facedir_simple=true
- },
- activeCustomKeys={
- tiles={
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png",
- "industrialtest_advanced_machine_block.png^industrialtest_tool_workshop_front_active.png",
- "industrialtest_advanced_machine_block.png"
- },
- },
- onConstruct=toolWorkshop.onConstruct,
- onTimer=toolWorkshop.onTimer,
- activeOnTimer=toolWorkshop.activeOnTimer,
- allowMetadataInventoryMove=toolWorkshop.allowMetadataInventoryMove,
- allowMetadataInventoryPut=toolWorkshop.allowMetadataInventoryPut,
- onMetadataInventoryPut=toolWorkshop.metadataChange,
- onMetadataInventoryMove=toolWorkshop.metadataChange
-})
minetest.register_craft({
type="shaped",
output="industrialtest:tool_workshop",