diff --git a/basis/node_states.lua b/basis/node_states.lua index 438c530..2a2712e 100644 --- a/basis/node_states.lua +++ b/basis/node_states.lua @@ -42,11 +42,11 @@ Node states: +---------+ +----------+ +---------+ Node metadata: - "tubelib_number" - string with tubelib number, like "123" - "tubelib_state" - node state, like "RUNNING" - "tubelib_item_meter" - node item/runtime counter - "tubelib_countdown" - countdown to stadby mode - "tubelib_aging" - aging counter + "techage_number" - string with tubelib number, like "123" + "techage_state" - node state, like "RUNNING" + "techage_item_meter" - node item/runtime counter + "techage_countdown" - countdown to stadby mode + "techage_aging" - aging counter ]]-- -- for lazy programmers @@ -63,17 +63,17 @@ techage.STOPPED = 1 -- not operational/turned off techage.RUNNING = 2 -- in normal operation/turned on techage.STANDBY = 3 -- nothing to do (e.g. no input items), or blocked anyhow (output jammed), -- or node (world) not loaded -techage.FAULT = 4 -- any fault state (e.g. no fuel), which can be fixed by the player +techage.FAULT = 4 -- any fault state (e.g. no power), which can be fixed by the player techage.BLOCKED = 5 -- a pushing node is blocked due to a full destination inventory techage.DEFECT = 6 -- a defect (broken), which has to be repaired by the player techage.StatesImg = { - "tubelib_inv_button_off.png", - "tubelib_inv_button_on.png", - "tubelib_inv_button_standby.png", - "tubelib_inv_button_error.png", - "tubelib_inv_button_warning.png", - "tubelib_inv_button_off.png", + "techage_inv_button_off.png", + "techage_inv_button_on.png", + "techage_inv_button_standby.png", + "techage_inv_button_error.png", + "techage_inv_button_warning.png", + "techage_inv_button_off.png", } -- Return state button image for the node inventory @@ -81,7 +81,7 @@ function techage.state_button(state) if state and state < 7 and state > 0 then return techage.StatesImg[state] end - return "tubelib_inv_button_off.png" + return "techage_inv_button_off.png" end -- State string based on button states @@ -106,7 +106,7 @@ local AGING_FACTOR = 4 -- defect random factor techage.NodeStates = {} local NodeStates = techage.NodeStates -local function start_condition_fullfilled(pos, meta) +local function can_start(pos, mem) return true end @@ -121,9 +121,9 @@ function NodeStates:new(attr) node_name_active = attr.node_name_active, node_name_defect = attr.node_name_defect, infotext_name = attr.infotext_name, - start_condition_fullfilled = attr.start_condition_fullfilled or start_condition_fullfilled, - on_start = attr.on_start, - on_stop = attr.on_stop, + can_start = attr.can_start or can_start, + start_node = attr.start_node, + stop_node = attr.stop_node, formspec_func = attr.formspec_func, } if attr.aging_factor then @@ -135,42 +135,41 @@ function NodeStates:new(attr) return o end -function NodeStates:node_init(pos, number) - local meta = M(pos) - meta:set_int("tubelib_state", STOPPED) - meta:set_string("tubelib_number", number) +function NodeStates:node_init(pos, mem, number) + mem.techage_state = STOPPED + mem.techage_number = number if self.infotext_name then - meta:set_string("infotext", self.infotext_name.." "..number..": stopped") + M(pos):set_string("infotext", self.infotext_name.." "..number..": stopped") end if self.has_item_meter then - meta:set_int("tubelib_item_meter", 0) + mem.techage_item_meter = 0 end if self.aging_level1 then - meta:set_int("tubelib_aging", 0) + mem.techage_aging = 0 end if self.formspec_func then - meta:set_string("formspec", self.formspec_func(self, pos, meta)) + M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) end end -function NodeStates:stop(pos, meta) - local state = meta:get_int("tubelib_state") +function NodeStates:stop(pos, mem) + local state = mem.techage_state if state ~= DEFECT then - if self.on_stop then - self.on_stop(pos, meta, state) + if self.stop_node then + self.stop_node(pos, mem, state) end - meta:set_int("tubelib_state", STOPPED) + mem.techage_state = STOPPED if self.node_name_passive then local node = minetest.get_node(pos) node.name = self.node_name_passive minetest.swap_node(pos, node) end if self.infotext_name then - local number = meta:get_string("tubelib_number") - meta:set_string("infotext", self.infotext_name.." "..number..": stopped") + local number = mem.techage_number + M(pos):set_string("infotext", self.infotext_name.." "..number..": stopped") end if self.formspec_func then - meta:set_string("formspec", self.formspec_func(self, pos, meta)) + M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) end minetest.get_node_timer(pos):stop() return true @@ -178,17 +177,18 @@ function NodeStates:stop(pos, meta) return false end -function NodeStates:start(pos, meta, called_from_on_timer) - local state = meta:get_int("tubelib_state") +function NodeStates:start(pos, mem, called_from_on_timer) + local state = mem.techage_state if state == STOPPED or state == STANDBY or state == BLOCKED then - if not self.start_condition_fullfilled(pos, meta) then + if not self.can_start(pos, mem, state) then + self:fault(pos, mem) return false end - if self.on_start then - self.on_start(pos, meta, state) + if self.start_node then + self.start_node(pos, mem, state) end - meta:set_int("tubelib_state", RUNNING) - meta:set_int("tubelib_countdown", 4) + mem.techage_state = RUNNING + mem.techage_countdown = 4 if called_from_on_timer then -- timer has to be stopped once to be able to be restarted self.stop_timer = true @@ -199,11 +199,11 @@ function NodeStates:start(pos, meta, called_from_on_timer) minetest.swap_node(pos, node) end if self.infotext_name then - local number = meta:get_string("tubelib_number") - meta:set_string("infotext", self.infotext_name.." "..number..": running") + local number = mem.techage_number + M(pos):set_string("infotext", self.infotext_name.." "..number..": running") end if self.formspec_func then - meta:set_string("formspec", self.formspec_func(self, pos, meta)) + M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) end minetest.get_node_timer(pos):start(self.cycle_time) return true @@ -211,9 +211,9 @@ function NodeStates:start(pos, meta, called_from_on_timer) return false end -function NodeStates:standby(pos, meta) - if meta:get_int("tubelib_state") == RUNNING then - meta:set_int("tubelib_state", STANDBY) +function NodeStates:standby(pos, mem) + if mem.techage_state == RUNNING then + mem.techage_state = STANDBY -- timer has to be stopped once to be able to be restarted self.stop_timer = true if self.node_name_passive then @@ -222,11 +222,11 @@ function NodeStates:standby(pos, meta) minetest.swap_node(pos, node) end if self.infotext_name then - local number = meta:get_string("tubelib_number") - meta:set_string("infotext", self.infotext_name.." "..number..": standby") + local number = mem.techage_number + M(pos):set_string("infotext", self.infotext_name.." "..number..": standby") end if self.formspec_func then - meta:set_string("formspec", self.formspec_func(self, pos, meta)) + M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) end minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks) return true @@ -235,9 +235,9 @@ function NodeStates:standby(pos, meta) end -- special case of standby for pushing nodes -function NodeStates:blocked(pos, meta) - if meta:get_int("tubelib_state") == RUNNING then - meta:set_int("tubelib_state", BLOCKED) +function NodeStates:blocked(pos, mem) + if mem.techage_state == RUNNING then + mem.techage_state = BLOCKED -- timer has to be stopped once to be able to be restarted self.stop_timer = true if self.node_name_passive then @@ -246,11 +246,11 @@ function NodeStates:blocked(pos, meta) minetest.swap_node(pos, node) end if self.infotext_name then - local number = meta:get_string("tubelib_number") - meta:set_string("infotext", self.infotext_name.." "..number..": blocked") + local number = mem.techage_number + M(pos):set_string("infotext", self.infotext_name.." "..number..": blocked") end if self.formspec_func then - meta:set_string("formspec", self.formspec_func(self, pos, meta)) + M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) end minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks) return true @@ -258,20 +258,20 @@ function NodeStates:blocked(pos, meta) return false end -function NodeStates:fault(pos, meta) - if meta:get_int("tubelib_state") == RUNNING then - meta:set_int("tubelib_state", FAULT) +function NodeStates:fault(pos, mem) + if mem.techage_state == RUNNING then + mem.techage_state = FAULT if self.node_name_passive then local node = minetest.get_node(pos) node.name = self.node_name_passive minetest.swap_node(pos, node) end if self.infotext_name then - local number = meta:get_string("tubelib_number") - meta:set_string("infotext", self.infotext_name.." "..number..": fault") + local number = mem.techage_number + M(pos):set_string("infotext", self.infotext_name.." "..number..": fault") end if self.formspec_func then - meta:set_string("formspec", self.formspec_func(self, pos, meta)) + M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) end minetest.get_node_timer(pos):stop() return true @@ -279,34 +279,34 @@ function NodeStates:fault(pos, meta) return false end -function NodeStates:defect(pos, meta) - meta:set_int("tubelib_state", DEFECT) +function NodeStates:defect(pos, mem) + mem.techage_state = DEFECT if self.node_name_defect then local node = minetest.get_node(pos) node.name = self.node_name_defect minetest.swap_node(pos, node) end if self.infotext_name then - local number = meta:get_string("tubelib_number") - meta:set_string("infotext", self.infotext_name.." "..number..": defect") + local number = mem.techage_number + M(pos):set_string("infotext", self.infotext_name.." "..number..": defect") end if self.formspec_func then - meta:set_string("formspec", self.formspec_func(self, pos, meta)) + M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) end minetest.get_node_timer(pos):stop() return true end -function NodeStates:get_state(meta) - return meta:get_int("tubelib_state") +function NodeStates:get_state(mem) + return mem.techage_state end -function NodeStates:get_state_string(meta) - return techage.StateStrings[meta:get_int("tubelib_state")] +function NodeStates:get_state_string(mem) + return techage.StateStrings[mem.techage_state] end -function NodeStates:is_active(meta) - local state = meta:get_int("tubelib_state") +function NodeStates:is_active(mem) + local state = mem.techage_state if self.stop_timer == true then self.stop_timer = false return false @@ -316,116 +316,117 @@ end -- To be called if node is idle. -- If countdown reaches zero, the node is set to STANDBY. -function NodeStates:idle(pos, meta) - local countdown = meta:get_int("tubelib_countdown") - 1 - meta:set_int("tubelib_countdown", countdown) +function NodeStates:idle(pos, mem) + local countdown = mem.techage_countdown - 1 + mem.techage_countdown = countdown if countdown < 0 then - self:standby(pos, meta) + self:standby(pos, mem) end end -- To be called after successful node action to raise the timer -- and keep the node in state RUNNING -function NodeStates:keep_running(pos, meta, val, num_items) +function NodeStates:keep_running(pos, mem, val, num_items) num_items = num_items or 1 -- set to RUNNING if not already done - self:start(pos, meta, true) - meta:set_int("tubelib_countdown", val) - meta:set_int("tubelib_item_meter", meta:get_int("tubelib_item_meter") + (num_items or 1)) + self:start(pos, mem, true) + mem.techage_countdown = val + mem.techage_item_meter = mem.techage_item_meter + (num_items or 1) if self.aging_level1 then - local cnt = meta:get_int("tubelib_aging") + num_items - meta:set_int("tubelib_aging", cnt) + local cnt = mem.techage_aging + num_items + mem.techage_aging = cnt if (cnt > (self.aging_level1) and math.random(self.aging_level2/num_items) == 1) or cnt >= 999999 then - self:defect(pos, meta) + self:defect(pos, mem) end end end -- Start/stop node based on button events. -- if function returns false, no button was pressed -function NodeStates:state_button_event(pos, fields) +function NodeStates:state_button_event(pos, mem, fields) if fields.state_button ~= nil then - local state = self:get_state(M(pos)) + local state = mem.techage_state + print("on_receive_fields", state) if state == STOPPED or state == STANDBY or state == BLOCKED then - self:start(pos, M(pos)) + self:start(pos, mem) elseif state == RUNNING or state == FAULT then - self:stop(pos, M(pos)) + self:stop(pos, mem) end return true end return false end -function NodeStates:get_state_button_image(meta) - local state = meta:get_int("tubelib_state") +function NodeStates:get_state_button_image(mem) + local state = mem.techage_state return techage.state_button(state) end -- command interface function NodeStates:on_receive_message(pos, topic, payload) if topic == "on" then - self:start(pos, M(pos)) + self:start(pos, tubelib2.get_mem(pos)) return true elseif topic == "off" then - self:stop(pos, M(pos)) + self:stop(pos, tubelib2.get_mem(pos)) return true elseif topic == "state" then local node = minetest.get_node(pos) if node.name == "ignore" then -- unloaded node? return "blocked" end - return self:get_state_string(M(pos)) + return self:get_state_string(tubelib2.get_mem(pos)) elseif self.has_item_meter and topic == "counter" then - return M(pos):get_int("tubelib_item_meter") + return mem.techage_item_meter elseif self.has_item_meter and topic == "clear_counter" then - M(pos):set_int("tubelib_item_meter", 0) + mem.techage_item_meter = 0 return true elseif self.aging_level1 and topic == "aging" then - return M(pos):get_int("tubelib_aging") + return mem.techage_aging end end -- repair corrupt node data and/or migrate node to state2 function NodeStates:on_node_load(pos, not_start_timer) - local meta = minetest.get_meta(pos) + local mem = tubelib2.get_mem(pos) -- legacy node number/state/counter? - local number = meta:get_string("number") + local number = mem.number if number ~= "" and number ~= nil then - meta:set_string("tubelib_number", number) - meta:set_int("tubelib_state", techage.state(meta:get_int("running"))) + mem.techage_number = number + mem.techage_state = techage.state(mem.running) if self.has_item_meter then - meta:set_int("tubelib_item_meter", meta:get_int("counter")) + mem.techage_item_meter = mem.counter end if self.aging_level1 then - meta:set_int("tubelib_aging", 0) + mem.techage_aging = 0 end - meta:set_string("number", nil) - meta:set_int("running", 0) - meta:set_int("counter", 0) + mem.number = nil + mem.running = 0 + mem.counter = 0 end -- node number corrupt? - number = meta:get_string("tubelib_number") + number = mem.techage_number if number == "" then number = techage.get_new_number(pos, self.node_name_passive) - meta:set_string("tubelib_number", number) + mem.techage_number = number else local info = techage.get_node_info(number) if not info or info.pos ~= pos then number = techage.get_new_number(pos, self.node_name_passive) - meta:set_string("tubelib_number", number) + mem.techage_number = number end end -- state corrupt? - local state = meta:get_int("tubelib_state") + local state = mem.techage_state if state == 0 then if minetest.get_node_timer(pos):is_started() then - meta:set_int("tubelib_state", RUNNING) + mem.techage_state = RUNNING else - meta:set_int("tubelib_state", STOPPED) + mem.techage_state = STOPPED end elseif state == RUNNING and not not_start_timer then minetest.get_node_timer(pos):start(self.cycle_time) @@ -436,29 +437,29 @@ function NodeStates:on_node_load(pos, not_start_timer) end if self.formspec_func then - meta:set_string("formspec", self.formspec_func(self, pos, meta)) + M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) end end -- Repair of defect (feature!) nodes function NodeStates:on_node_repair(pos) - local meta = M(pos) - if meta:get_int("tubelib_state") == DEFECT then - meta:set_int("tubelib_state", STOPPED) + local mem = tubelib2.get_mem(pos) + if mem.techage_state == DEFECT then + mem.techage_state = STOPPED if self.node_name_passive then local node = minetest.get_node(pos) node.name = self.node_name_passive minetest.swap_node(pos, node) end if self.aging_level1 then - meta:set_int("tubelib_aging", 0) + mem.techage_aging = 0 end if self.infotext_name then - local number = meta:get_string("tubelib_number") - meta:set_string("infotext", self.infotext_name.." "..number..": stopped") + local number = mem.techage_number + M(pos):set_string("infotext", self.infotext_name.." "..number..": stopped") end if self.formspec_func then - meta:set_string("formspec", self.formspec_func(self, pos, meta)) + M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) end return true end @@ -467,8 +468,9 @@ end -- Return working or defect machine, depending on machine lifetime function NodeStates:after_dig_node(pos, oldnode, oldmetadata, digger) + local mem = tubelib2.get_mem(pos) local inv = minetest.get_inventory({type="player", name=digger:get_player_name()}) - local cnt = oldmetadata.fields.tubelib_aging and tonumber(oldmetadata.fields.tubelib_aging) or 0 + local cnt = mem.techage_aging or 0 local is_defect = cnt > self.aging_level1 and math.random(self.aging_level2 / cnt) == 1 if self.node_name_defect and is_defect then inv:add_item("main", ItemStack(self.node_name_defect)) diff --git a/basis/power.lua b/basis/power.lua index 576277a..691d53b 100644 --- a/basis/power.lua +++ b/basis/power.lua @@ -121,30 +121,36 @@ end -- To be called delayed from any node, after any change. -- The result is stored in mem.power_result -local function calc_power_consumption(pos, dir) +local function check_power_consumption(pos, dir) local mem = tubelib2.get_mem(pos) Route = {} local sum = power_consumption(pos, dir) Route = {} turn_on(pos, nil, sum > 0) - +end + +-- +-- Generator with one power output side +-- +function techage.calc_power_consumption(pos, mem, max_power) + mem.power_produce = max_power + Route = {} + local sum = power_consumption(pos, mem.power_dir) mem.power_result = sum return sum end --- --- Generator with on power output side --- -function techage.generator_on(pos, max_power) - local mem = tubelib2.get_mem(pos) - mem.power_produce = max_power - return calc_power_consumption(pos, mem.power_dir) +function techage.generator_on(pos, mem) + if mem.power_result > 0 then + Route = {} + turn_on(pos, nil, true) + end end -function techage.generator_off(pos) - local mem = tubelib2.get_mem(pos) +function techage.generator_off(pos, mem) mem.power_produce = 0 - return calc_power_consumption(pos, mem.power_dir) + Route = {} + turn_on(pos, nil, false) end function techage.generator_power_consumption(pos, dir) @@ -174,12 +180,12 @@ function techage.generator_after_tube_update(node, pos, out_dir, peer_pos, peer_ -- Generator accept one dir only mem.connections = {[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}} end - minetest.after(0.2, calc_power_consumption, pos) + minetest.after(0.2, check_power_consumption, pos) end end function techage.generator_on_destruct(pos) - techage.generator_off(pos) + techage.generator_off(pos, tubelib2.get_mem(pos)) end function techage.generator_after_dig_node(pos, oldnode) @@ -188,6 +194,7 @@ function techage.generator_after_dig_node(pos, oldnode) end function techage.generator_formspec_level(mem) + print("generator_formspec_level", mem.power_result, mem.power_produce) local percent = ((mem.power_result or 0) * 100) / (mem.power_produce or 1) return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png]" end @@ -214,7 +221,7 @@ function techage.distributor_after_tube_update(node, pos, out_dir, peer_pos, pee else mem.connections[out_dir] = {pos = peer_pos, in_dir = peer_in_dir} end - minetest.after(0.2, calc_power_consumption, pos) + minetest.after(0.2, check_power_consumption, pos) end function techage.distributor_after_dig_node(pos, oldnode) @@ -254,7 +261,7 @@ function techage.consumer_after_tube_update(node, pos, out_dir, peer_pos, peer_i mem.connections = {[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}} end end - minetest.after(0.2, calc_power_consumption, pos) + minetest.after(0.2, check_power_consumption, pos) end function techage.consumer_after_dig_node(pos, oldnode) diff --git a/electric/generator.lua b/electric/generator.lua new file mode 100644 index 0000000..4854bab --- /dev/null +++ b/electric/generator.lua @@ -0,0 +1,132 @@ +-- for lazy programmers +local S = function(pos) if pos then return minetest.pos_to_string(pos) end end +local P = minetest.string_to_pos +local M = minetest.get_meta + +-- Load support for intllib. +local MP = minetest.get_modpath("tubelib2") +local I,_ = dofile(MP.."/intllib.lua") + +local STANDBY_TICKS = 4 +local COUNTDOWN_TICKS = 4 +local CYCLE_TIME = 2 + +local function formspec(self, pos, mem) + return "size[8,7]".. + default.gui_bg.. + default.gui_bg_img.. + default.gui_slots.. + "image[2,0.5;1,2;"..techage.generator_formspec_level(mem).. + "image_button[3,1.2;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. + "button[5.5,1.2;1.8,1;update;"..I("Update").."]".. + "list[current_player;main;0,3;8,4;]".. + "listring[current_name;water]".. + "listring[current_player;main]".. + default.get_hotbar_bg(0, 3) +end + +local function can_start(pos, mem, state) + local sum = techage.calc_power_consumption(pos, mem, 8) + if sum > 0 then + M(pos):set_string("infotext", "On:"..sum.." / "..8) + return true + end + return false +end + +local function start_node(pos, mem, state) + techage.generator_on(pos, mem) +end + +local function stop_node(pos, mem, state) + techage.generator_off(pos, mem) + M(pos):set_string("infotext", "Off") +end + + +local State = techage.NodeStates:new({ + node_name_passive = "techage:power", + cycle_time = CYCLE_TIME, + standby_ticks = STANDBY_TICKS, + has_item_meter = true, + aging_factor = 10, + formspec_func = formspec, + can_start = can_start, + start_node = start_node, + stop_node = stop_node, +}) + + +local function distibuting(pos, mem) + local sum = techage.calc_power_consumption(pos, mem, 8) + if sum > 0 then + State:keep_running(pos, mem, COUNTDOWN_TICKS) + else + State:fault(pos, mem) + end + M(pos):set_string("infotext", "On:"..sum.." / "..8) +end + +local function node_timer(pos, elapsed) + print("node_timer") + local mem = tubelib2.get_mem(pos) + distibuting(pos, mem) + return State:is_active(mem) +end + +local function on_receive_fields(pos, formname, fields, player) + if minetest.is_protected(pos, player:get_player_name()) then + return + end + local mem = tubelib2.get_mem(pos) + State:state_button_event(pos, mem, fields) + + if fields.update then + M(pos):set_string("formspec", formspec(State, pos, mem)) + end +end + +local function on_rightclick(pos) + local mem = tubelib2.get_mem(pos) + M(pos):set_string("formspec", formspec(State, pos, mem)) +end + +minetest.register_node("techage:power", { + description = "TechAge Power", + tiles = { + -- up, down, right, left, back, front + 'techage_electric_button.png^techage_electric_power.png', + 'techage_electric_button.png^techage_electric_power.png', + 'techage_electric_button.png^techage_electric_power.png^techage_electric_plug.png', + 'techage_electric_button.png^techage_electric_power.png', + 'techage_electric_button.png^techage_electric_power.png', + 'techage_electric_button.png^techage_electric_power.png', + }, + paramtype2 = "facedir", + groups = {cracky=2, crumbly=2, choppy=2}, + on_rotate = screwdriver.disallow, + is_ground_content = false, + + techage = { + power_consumption = techage.generator_power_consumption, + power_network = techage.ElectricCable, + power_consume = 0, + }, + + after_place_node = function(pos, placer) + local mem = techage.generator_after_place_node(pos) + State:node_init(pos, mem, "") + end, + + after_dig_node = function(pos, oldnode, oldmetadata, digger) + State:after_dig_node(pos, oldnode, oldmetadata, digger) + techage.generator_after_dig_node(pos, oldnode) + end, + + after_tube_update = techage.generator_after_tube_update, + on_destruct = techage.generator_on_destruct, + on_timer = node_timer, + on_receive_fields = on_receive_fields, + on_rightclick = on_rightclick, +}) + diff --git a/electric/test.lua b/electric/test.lua index 0ffcaac..f48415f 100644 --- a/electric/test.lua +++ b/electric/test.lua @@ -97,47 +97,3 @@ minetest.register_node("techage:lamp_on", { sounds = default.node_sound_wood_defaults(), }) - -minetest.register_node("techage:power", { - description = "TechAge Power", - tiles = { - -- up, down, right, left, back, front - 'techage_electric_button.png^techage_electric_power.png', - 'techage_electric_button.png^techage_electric_power.png', - 'techage_electric_button.png^techage_electric_power.png^techage_electric_plug.png', - 'techage_electric_button.png^techage_electric_power.png', - 'techage_electric_button.png^techage_electric_power.png', - 'techage_electric_button.png^techage_electric_power.png', - }, - paramtype2 = "facedir", - groups = {cracky=2, crumbly=2, choppy=2}, - on_rotate = screwdriver.disallow, - is_ground_content = false, - - techage = { - power_consumption = techage.generator_power_consumption, - power_network = Cable, - power_consume = 0, - }, - - after_place_node = techage.generator_after_place_node, - after_tube_update = techage.generator_after_tube_update, - on_destruct = techage.generator_on_destruct, - after_dig_node = techage.generator_after_dig_node, - - on_rightclick = function(pos, node, clicker) - local mem = tubelib2.get_mem(pos) - mem.running = mem.running or false - print("on_rightclick", mem.running) - if mem.running then - mem.running = false - local sum = techage.generator_off(pos) - M(pos):set_string("infotext", sum.." / "..8) - else - mem.running = true - local sum = techage.generator_on(pos, 8) - M(pos):set_string("infotext", sum.." / "..8) - end - end, -}) - diff --git a/init.lua b/init.lua index 4cd4135..729b8c8 100644 --- a/init.lua +++ b/init.lua @@ -14,20 +14,23 @@ local MP = minetest.get_modpath("techage") dofile(MP.."/basis/intllib.lua") dofile(MP.."/basis/power.lua") -- power distribution +dofile(MP.."/basis/node_states.lua") dofile(MP.."/basis/trowel.lua") -- hidden networks dofile(MP.."/basis/junction.lua") -- network junction box -- Steam Engine -dofile(MP.."/steam_engine/drive_axle.lua") -dofile(MP.."/steam_engine/steam_pipe.lua") -dofile(MP.."/steam_engine/firebox.lua") -dofile(MP.."/steam_engine/boiler.lua") -dofile(MP.."/steam_engine/cylinder.lua") -dofile(MP.."/steam_engine/flywheel.lua") -dofile(MP.."/steam_engine/gearbox.lua") +--dofile(MP.."/steam_engine/drive_axle.lua") +--dofile(MP.."/steam_engine/steam_pipe.lua") +--dofile(MP.."/steam_engine/firebox.lua") +--dofile(MP.."/steam_engine/boiler.lua") +--dofile(MP.."/steam_engine/cylinder.lua") +--dofile(MP.."/steam_engine/flywheel.lua") +--dofile(MP.."/steam_engine/gearbox.lua") +--dofile(MP.."/steam_engine/consumer.lua") dofile(MP.."/electric/electric_cable.lua") dofile(MP.."/electric/test.lua") +dofile(MP.."/electric/generator.lua") --dofile(MP.."/fermenter/biogas_pipe.lua") diff --git a/steam_engine/consumer.lua b/steam_engine/consumer.lua new file mode 100644 index 0000000..1c966b0 --- /dev/null +++ b/steam_engine/consumer.lua @@ -0,0 +1,141 @@ +--[[ + + TechAge + ======= + + Copyright (C) 2019 Joachim Stolberg + + LGPLv2.1+ + See LICENSE.txt for more information + + TA2 Consumer + +]]-- + +-- for lazy programmers +local S = function(pos) if pos then return minetest.pos_to_string(pos) end end +local P = minetest.string_to_pos +local M = minetest.get_meta + +local POWER_CONSUME = 4 + + +local function swap_node(pos, name) + local node = minetest.get_node(pos) + if node.name == name then + return + end + node.name = name + minetest.swap_node(pos, node) +end + +-- To be able to check if power connection is on the +-- correct node side (mem.power_dir == in_dir) +local function valid_power_dir(pos, mem, in_dir) + return mem.power_dir == in_dir +end + +local function turn_on(pos, in_dir, on) + if on then + swap_node(pos, "techage:consumer_on") + else + swap_node(pos, "techage:consumer") + end +-- end +end + +minetest.register_node("techage:consumer", { + description = "TechAge Consumer", + tiles = { + -- up, down, right, left, back, front + 'techage_filling_ta2.png^techage_frame_ta2.png', + 'techage_filling_ta2.png^techage_frame_ta2.png', + 'techage_filling_ta2.png^techage_frame_ta2.png', + 'techage_filling_ta2.png^techage_frame_ta2.png^techage_axle_clutch.png', + 'techage_filling_ta2.png^techage_frame_ta2.png', + 'techage_filling_ta2.png^techage_frame_ta2.png', + }, + techage = { + turn_on = turn_on, + power_consumption = techage.consumer_power_consumption, + power_network = techage.Axle, + power_consume = POWER_CONSUME, + animated_power_network = true, + power_side = "L", + valid_power_dir = valid_power_dir, + + }, + + after_place_node = function(pos, placer) + local mem = techage.consumer_after_place_node(pos, placer) + mem.power_consume = POWER_CONSUME + end, + + after_tube_update = techage.consumer_after_tube_update, + after_dig_node = techage.consumer_after_dig_node, + + paramtype2 = "facedir", + groups = {choppy=2, cracky=2, crumbly=2}, + is_ground_content = false, + sounds = default.node_sound_wood_defaults(), +}) + +minetest.register_node("techage:consumer_on", { + description = "TechAge Consumer", + tiles = { + -- up, down, right, left, back, front + 'techage_filling_ta2.png^techage_frame_ta2.png', + 'techage_filling_ta2.png^techage_frame_ta2.png', + { + image = "techage_filling4_ta2.png^techage_frame4_ta2.png^techage_appl_compressor4.png", + backface_culling = false, + animation = { + type = "vertical_frames", + aspect_w = 32, + aspect_h = 32, + length = 0.6, + }, + }, + 'techage_filling_ta2.png^techage_frame_ta2.png^techage_axle_clutch.png', + { + image = "techage_filling4_ta2.png^techage_frame4_ta2.png^techage_appl_compressor4.png", + backface_culling = false, + animation = { + type = "vertical_frames", + aspect_w = 32, + aspect_h = 32, + length = 0.6, + }, + }, + { + image = "techage_filling4_ta2.png^techage_frame4_ta2.png^techage_appl_compressor4.png", + backface_culling = false, + animation = { + type = "vertical_frames", + aspect_w = 32, + aspect_h = 32, + length = 0.6, + }, + }, + }, + techage = { + turn_on = turn_on, + power_consumption = techage.consumer_power_consumption, + power_network = techage.Axle, + power_consume = POWER_CONSUME, + animated_power_network = true, + valid_power_dir = valid_power_dir, + }, + + after_place_node = techage.consumer_after_place_node, + after_tube_update = techage.consumer_after_tube_update, + after_dig_node = techage.consumer_after_dig_node, + + paramtype2 = "facedir", + groups = {not_in_creative_inventory=1}, + diggable = false, + drop = "techage:consumer", + is_ground_content = false, + sounds = default.node_sound_wood_defaults(), +}) + diff --git a/steam_engine/flywheel.lua b/steam_engine/flywheel.lua index ea7d255..3ca9e40 100644 --- a/steam_engine/flywheel.lua +++ b/steam_engine/flywheel.lua @@ -21,7 +21,7 @@ local M = minetest.get_meta local MP = minetest.get_modpath("tubelib2") local I,_ = dofile(MP.."/intllib.lua") -local CYCLE_TIME = 10 +local CYCLE_TIME = 8 local POWER = 8 local function swap_node(pos, name) @@ -33,30 +33,38 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end -local function turn_on(pos, dir, on) - print("jou") - if on then - swap_node(pos, "techage:flywheel_on") - if not minetest.get_node_timer(pos):is_started() then - minetest.get_node_timer(pos):start(CYCLE_TIME) - end - else - swap_node(pos, "techage:flywheel") - if minetest.get_node_timer(pos):is_started() then - minetest.get_node_timer(pos):stop() - end - end -end +--local function turn_on(pos, dir, on) +-- print("jou") +-- if on then +-- swap_node(pos, "techage:flywheel_on") +-- if not minetest.get_node_timer(pos):is_started() then +-- minetest.get_node_timer(pos):start(CYCLE_TIME) +-- end +-- else +-- swap_node(pos, "techage:flywheel") +-- if minetest.get_node_timer(pos):is_started() then +-- minetest.get_node_timer(pos):stop() +-- end +-- end +--end local function try_to_start(pos, on) print("try_to_start", S(pos)) if on then if techage.generator_on(pos, POWER) then + swap_node(pos, "techage:flywheel_on") + if not minetest.get_node_timer(pos):is_started() then + minetest.get_node_timer(pos):start(CYCLE_TIME) + end return true end else techage.generator_off(pos) end + swap_node(pos, "techage:flywheel") + if minetest.get_node_timer(pos):is_started() then + minetest.get_node_timer(pos):stop() + end return false end @@ -91,9 +99,7 @@ end local function node_timer(pos, elapsed) - local mem = tubelib2.get_mem(pos) - techage.generator_on(pos, POWER, techage.Axle) - return true + return try_to_start(pos, true) end minetest.register_node("techage:flywheel", { @@ -112,7 +118,7 @@ minetest.register_node("techage:flywheel", { power_consumption = techage.generator_power_consumption, power_consume = 0, animated_power_network = true, - turn_on = turn_on, + --turn_on = turn_on, try_to_start = try_to_start, }, @@ -179,7 +185,7 @@ minetest.register_node("techage:flywheel_on", { power_consumption = techage.generator_power_consumption, power_consume = 0, animated_power_network = true, - turn_on = turn_on, + --turn_on = turn_on, try_to_start = try_to_start, }, diff --git a/textures/tubelib_distributor_active_frame.png b/textures/techage_distributor_active_frame.png similarity index 100% rename from textures/tubelib_distributor_active_frame.png rename to textures/techage_distributor_active_frame.png diff --git a/textures/techage_inv_button_error.png b/textures/techage_inv_button_error.png new file mode 100644 index 0000000..e4b0d1d Binary files /dev/null and b/textures/techage_inv_button_error.png differ diff --git a/textures/techage_inv_button_off.png b/textures/techage_inv_button_off.png new file mode 100644 index 0000000..355fee5 Binary files /dev/null and b/textures/techage_inv_button_off.png differ diff --git a/textures/techage_inv_button_on.png b/textures/techage_inv_button_on.png new file mode 100644 index 0000000..a10aa50 Binary files /dev/null and b/textures/techage_inv_button_on.png differ diff --git a/textures/techage_inv_button_standby.png b/textures/techage_inv_button_standby.png new file mode 100644 index 0000000..a13b201 Binary files /dev/null and b/textures/techage_inv_button_standby.png differ diff --git a/textures/techage_inv_button_warning.png b/textures/techage_inv_button_warning.png new file mode 100644 index 0000000..33ec686 Binary files /dev/null and b/textures/techage_inv_button_warning.png differ