From e36624f327fc419a34a490a343c778d91d4820e2 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sun, 26 Mar 2023 20:02:06 +0200 Subject: [PATCH] Implement solar panels --- machines.lua | 155 ++++++++++++++++++++++++- textures/industrialtest_gui_sun_bg.png | Bin 0 -> 730 bytes textures/industrialtest_gui_sun_fg.png | Bin 0 -> 746 bytes 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 textures/industrialtest_gui_sun_bg.png create mode 100644 textures/industrialtest_gui_sun_fg.png diff --git a/machines.lua b/machines.lua index ee3e3b9..77774db 100644 --- a/machines.lua +++ b/machines.lua @@ -244,7 +244,6 @@ if industrialtest.mclAvailable then definition._doc_items_create_entry=false end minetest.register_node("industrialtest:generator_active",definition) - minetest.register_craft({ type="shaped", output="industrialtest:generator", @@ -264,6 +263,160 @@ minetest.register_craft({ } }) +local function registerSolarPanelGenerator(config) + local function getFormspec(charging) + local formspec + if industrialtest.mtgAvailable then + formspec={ + "formspec_version[4]", + "size[10.8,12]", + "label[0.5,0.5;"..S(config.displayName).."]", + "list[context;charged;4.9,1.8;1,1]", + "listring[context;charged]", + (charging and "image[4.9,2.8;1,1;industrialtest_gui_sun_fg.png]" + or "image[4.9,2.8;1,1;industrialtest_gui_sun_bg.png]"), + "list[current_player;main;0.5,6.25;8,1]list[current_player;main;0.5,7.5;8,3;8]" + } + elseif industrialtest.mclAvailable then + formspec={ + "size[10.04,12]", + "label[0.25,0.25;"..S(config.displayName).."]", + "list[context;charged;4.7,1.8;1,1]", + mcl_formspec.get_itemslot_bg(4.7,1.8,1,1), + "listring[context;charged]", + (charging and "image[4.7,2.8;1,1;industrialtest_gui_sun_fg.png]" + or "image[4.7,2.8;1,1;industrialtest_gui_sun_bg.png]"), + "list[current_player;main;0.5,7;9,3;9]", + mcl_formspec.get_itemslot_bg(0.5,7,9,3), + "list[current_player;main;0.5,10.24;9,1]", + mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) + } + end + return table.concat(formspec,"") + end + definition={ + description=S(config.displayName), + tiles={ + "industrialtest_"..config.name.."_top.png", + "industrialtest_machine_block.png" + }, + drop="industrialtest:machine_block", + on_construct=function(pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + inv:set_size("charged",1) + meta:set_string("formspec",getFormspec(false)) + meta:set_float("prevAmount",0) + industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,"oooooo") + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end, + on_timer=function(pos,elapsed) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local chargedSlot=inv:get_stack("charged",1) + local shouldUpdateFormspec=false + + local amount=minetest.get_natural_light(vector.offset(pos,0,1,0))/15.0 + local charging=amount>0.5 + if charging then + industrialtest.api.addPower(meta,math.ceil(amount*config.flow*elapsed)) + end + if meta:get_int("industrialtest.powerAmount")>0 then + if chargedSlot:get_count()>0 and not industrialtest.api.isFullyCharged(chargedSlot:get_meta()) then + industrialtest.api.transferPowerToItem(meta,chargedSlot,math.ceil(config.flow*elapsed)) + inv:set_stack("charged",1,chargedSlot) + end + industrialtest.api.powerFlow(pos) + end + if amount~=meta:get_float("prevAmount") then + shouldUpdateFormspec=true + meta:set_float("prevAmount",amount) + end + + if shouldUpdateFormspec then + meta:set_string("formspec",getFormspec(charging)) + end + + return true + end, + allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local movedItemStack=inv:get_list(fromList)[1] + if toList=="charged" and not industrialtest.api.hasPowerStorage(movedItemStack:get_meta()) then + return 0 + end + return count + end, + allow_metadata_inventory_put=function(pos,listname,index,stack) + if listname=="charged" and not industrialtest.api.hasPowerStorage(stack:get_meta()) then + return 0 + end + return stack:get_count() + end, + on_metadata_inventory_put=function(pos) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end, + on_metadata_inventory_take=function(pos) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end + } + if industrialtest.mtgAvailable then + definition.groups={cracky=2} + definition.sounds=default.node_sound_metal_defaults() + definition.can_dig=function(pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + return inv:get_list("charged")[1]:get_count()==0 + end + elseif industrialtest.mclAvailable then + definition.groups={pickaxey=1} + definition.sounds=mcl_sounds.node_sound_metal_defaults() + definition._mcl_blast_resistance=1 + definition._mcl_hardness=3.9 + definition.after_dig_node=function(pos,oldnode,oldmeta) + mclAfterDigNode(pos,oldmeta,{"charged"}) + end + end + definition.groups._industrialtest_hasPowerOutput=1 + definition.groups._industrialtest_wrenchUnmountable=1 + minetest.register_node("industrialtest:"..config.name,definition) +end +registerSolarPanelGenerator({ + name="solar_panel", + displayName="Solar Panel", + capacity=industrialtest.api.lvPowerFlow*2, + flow=industrialtest.api.lvPowerFlow +}) +minetest.register_craft({ + type="shaped", + output="industrialtest:solar_panel", + recipe={ + {"industrialtest:coal_dust",industrialtest.elementKeys.glass,"industrialtest:coal_dust"}, + {industrialtest.elementKeys.glass,"industrialtest:coal_dust",industrialtest.elementKeys.glass}, + {"industrialtest:insulated_copper_cable","industrialtest:generator","industrialtest:insulated_copper_cable"} + } +}) +registerSolarPanelGenerator({ + name="lv_solar_array", + displayName="LV Solar Array", + capacity=industrialtest.api.lvPowerFlow*4, + flow=industrialtest.api.lvPowerFlow*2 +}) +registerSolarPanelGenerator({ + name="mv_solar_array", + displayName="MV Solar Array", + capacity=industrialtest.api.mvPowerFlow*2, + flow=industrialtest.api.mvPowerFlow +}) +registerSolarPanelGenerator({ + name="hv_solar_array", + displayName="HV Solar Array", + capacity=industrialtest.api.hvPowerFlow*2, + flow=industrialtest.api.hvPowerFlow +}) +-- TODO: Crafts for solar arrays + -- Item processing machines local function registerSimpleElectricItemProcessor(config) local function getFormspec(powerPercent,srcPercent) diff --git a/textures/industrialtest_gui_sun_bg.png b/textures/industrialtest_gui_sun_bg.png new file mode 100644 index 0000000000000000000000000000000000000000..d3d07d0742c92e6940aeb6f0425feb35916de641 GIT binary patch literal 730 zcmV<00ww*4P)EX>4Tx04R}tkv&MmKpe$iKcpfR5j#i`$xxjvh>AFB6^c+H)C#RSm|Xe=O&XFE z7e~Rh;NZt%)xpJCR|i)?5c~jfb#YR3krMxx6k5c1aNLh~_a1le0Dq&xR5LgZsG4P@ zlL;Z4TNOgD2q21n3?nEpQ=b#X6ge3JS*_Gq>z@3D;k>r8%ypVWNMaF75FtQD4P{hdAx^7CiitGsCp`R7$1jpgCRZ7Z z91EyIh2;3b|KNAGW?^!|O$sM~t{2<>7y&}NK(lV!-^aGyJOKjFz?IhaR~x|0C+YRJ z7CQp^wt;*07_KO2@?PS010qNS#tmY zE+YT{E+YYWr9XB6000McNliru=K~rMAsW+&X>R}k02y>eSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{006Q{L_t(I%gvHK4udcZg&)*XJ8^?th#^uYW=_VO2`NLa z!69;kOht0l4ird3LjQ*Ljg~*#de6XLi+x?NuZz8&A_E>etvIZ;O;1$WOWaEd_}FQ6 zcFsN2JFS>KPE^@3d%?UphGz*OHkVMmB?kk5F$REQz%Gl+(6D#R-3<6t%K)nE^;IbB zE#s(!5J_kl21qHsS80Zc-0a%~!p^asU7T M07*qoM6N<$f(*DXR{#J2 literal 0 HcmV?d00001 diff --git a/textures/industrialtest_gui_sun_fg.png b/textures/industrialtest_gui_sun_fg.png new file mode 100644 index 0000000000000000000000000000000000000000..8c01bd9151aa1931244133bd46291e386f6ba550 GIT binary patch literal 746 zcmVEX>4Tx04R}tkv&MmKpe$iKcpfR5j#i`$xxjvh>AFB6^c+H)C#RSm|Xe=O&XFE z7e~Rh;NZt%)xpJCR|i)?5c~jfb#YR3krMxx6k5c1aNLh~_a1le0Dq&xR5LgZsG4P@ zlL;Z4TNOgD2q21n3?nEpQ=b#X6ge3JS*_Gq>z@3D;k>r8%ypVWNMaF75FtQD4P{hdAx^7CiitGsCp`R7$1jpgCRZ7Z z91EyIh2;3b|KNAGW?^!|O$sM~t{2<>7y&}NK(lV!-^aGyJOKjFz?IhaR~x|0C+YRJ z7CQp^wteSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{006>CL_t(I%gvI_3W7isg+FZ|L2wa>M6~H4+|>j4z&=1r zAHq#Zh~Q#CFfeL!3Xv>GqAmd|KoL4r^h~o_K0Z&j1G$u$dZv%8T znFC8;1B8MrAO}t+NYA4Ilz9#8fEZXwRR9^V2M+y8%%A{YnPXrJM8ZqmN1z7m-NEe> zrk+D(j(~+uBrF8HKC$l9Nj(CXAiaw1NP_{kzo cUgh7*4KUnCmP+OmX8-^I07*qoM6N<$f|92%8vp