forked from minetest-mods/mesecons
Add overheat protection and lots of bugfixes once again
This commit is contained in:
parent
0abfa674d6
commit
f557b6c363
|
@ -17,7 +17,8 @@ minetest.register_node(nodename, {
|
||||||
"jeija_microcontroller_top_"..tostring(d)..tostring(c)..tostring(b)..tostring(a)..".png",
|
"jeija_microcontroller_top_"..tostring(d)..tostring(c)..tostring(b)..tostring(a)..".png",
|
||||||
"jeija_microcontroller_sides.png",
|
"jeija_microcontroller_sides.png",
|
||||||
},
|
},
|
||||||
inventory_image = "jeija_microcontroller_top_0000.png",
|
--inventory_image = "jeija_microcontroller_top_0000.png",
|
||||||
|
|
||||||
sunlight_propagates = true,
|
sunlight_propagates = true,
|
||||||
paramtype = "light",
|
paramtype = "light",
|
||||||
walkable = true,
|
walkable = true,
|
||||||
|
@ -43,7 +44,7 @@ minetest.register_node(nodename, {
|
||||||
"field[0.256,0.5;8,1;code;Code:;]"..
|
"field[0.256,0.5;8,1;code;Code:;]"..
|
||||||
"button_exit[3,0.5;2,2;program;Program]")
|
"button_exit[3,0.5;2,2;program;Program]")
|
||||||
meta:set_string("infotext", "Unprogrammed Microcontroller")
|
meta:set_string("infotext", "Unprogrammed Microcontroller")
|
||||||
meta:set_string("heat", "0")
|
meta:set_int("heat", 0)
|
||||||
local r = ""
|
local r = ""
|
||||||
for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0"
|
for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0"
|
||||||
meta:set_string("eeprom", r)
|
meta:set_string("eeprom", r)
|
||||||
|
@ -56,11 +57,11 @@ minetest.register_node(nodename, {
|
||||||
meta:set_string("formspec", "size[8,2]"..
|
meta:set_string("formspec", "size[8,2]"..
|
||||||
"field[0.256,0.5;8,1;code;Code:;"..fields.code.."]"..
|
"field[0.256,0.5;8,1;code;Code:;"..fields.code.."]"..
|
||||||
"button_exit[3,0.5;2,2;program;Program]")
|
"button_exit[3,0.5;2,2;program;Program]")
|
||||||
meta:set_string("heat", "0")
|
meta:set_int("heat", 0)
|
||||||
reset_yc (pos)
|
yc_reset (pos)
|
||||||
update_yc(pos)
|
update_yc(pos)
|
||||||
end
|
end
|
||||||
end
|
end,
|
||||||
})
|
})
|
||||||
local rules={}
|
local rules={}
|
||||||
if (a == 1) then table.insert(rules, {x = -1, y = 0, z = 0}) end
|
if (a == 1) then table.insert(rules, {x = -1, y = 0, z = 0}) end
|
||||||
|
@ -83,7 +84,7 @@ minetest.register_craft({
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
function reset_yc(pos)
|
function yc_reset(pos)
|
||||||
yc_action(pos, {a=false, b=false, c=false, d=false})
|
yc_action(pos, {a=false, b=false, c=false, d=false})
|
||||||
local meta = minetest.env:get_meta(pos)
|
local meta = minetest.env:get_meta(pos)
|
||||||
local r = ""
|
local r = ""
|
||||||
|
@ -93,15 +94,12 @@ end
|
||||||
|
|
||||||
function update_yc(pos)
|
function update_yc(pos)
|
||||||
local meta = minetest.env:get_meta(pos)
|
local meta = minetest.env:get_meta(pos)
|
||||||
local timer = minetest.env:get_node_timer(pos)
|
yc_heat(meta)
|
||||||
if (timer:get_elapsed()~=nil) then
|
minetest.after(0.5, yc_cool, meta)
|
||||||
if (timer:get_elapsed() < 0.05) then
|
if (yc_overheat(meta)) then
|
||||||
h = tonumber(meta:get_string("heat"))
|
minetest.env:remove_node(pos)
|
||||||
if h==nil then return nil end
|
minetest.after(0.2, yc_overheat_off, pos) --wait for pending parsings
|
||||||
meta:set_string("heat", tostring(h + 1))
|
minetest.env:add_item(pos, "mesecons_microcontroller:microcontroller0000")
|
||||||
else
|
|
||||||
meta:set_string("heat", "0");
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local code = meta:get_string("code")
|
local code = meta:get_string("code")
|
||||||
|
@ -114,9 +112,11 @@ function update_yc(pos)
|
||||||
meta:set_string("infotext", "Working Microcontroller")
|
meta:set_string("infotext", "Working Microcontroller")
|
||||||
end
|
end
|
||||||
timer = minetest.env:get_node_timer(pos) --action places a new node!
|
timer = minetest.env:get_node_timer(pos) --action places a new node!
|
||||||
timer:start(1)
|
timer:start(0.5)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--Code Parsing
|
||||||
function yc_code_remove_commentary(code)
|
function yc_code_remove_commentary(code)
|
||||||
for i = 1, #code do
|
for i = 1, #code do
|
||||||
if code:sub(i, i) == ":" then
|
if code:sub(i, i) == ":" then
|
||||||
|
@ -130,6 +130,7 @@ function parse_yccode(code, pos)
|
||||||
local endi = 1
|
local endi = 1
|
||||||
local Lreal = yc_get_real_portstates(pos)
|
local Lreal = yc_get_real_portstates(pos)
|
||||||
local Lvirtual = yc_get_virtual_portstates(pos)
|
local Lvirtual = yc_get_virtual_portstates(pos)
|
||||||
|
if Lvirtual == nil then return nil end
|
||||||
local c
|
local c
|
||||||
local eeprom = minetest.env:get_meta(pos):get_string("eeprom")
|
local eeprom = minetest.env:get_meta(pos):get_string("eeprom")
|
||||||
while true do
|
while true do
|
||||||
|
@ -224,6 +225,20 @@ function yc_parse_get_eeprom_param(cond, starti)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function yc_skip_to_endif(code, starti)
|
||||||
|
local i = starti
|
||||||
|
local s = false
|
||||||
|
while s ~= nil and s~= "" do
|
||||||
|
s = code:sub(i, i)
|
||||||
|
if s == ";" then
|
||||||
|
return i + 1
|
||||||
|
end
|
||||||
|
i = i + 1
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
--Commands
|
||||||
function yc_command_on(params, L)
|
function yc_command_on(params, L)
|
||||||
local rules = {}
|
local rules = {}
|
||||||
for i, port in ipairs(params) do
|
for i, port in ipairs(params) do
|
||||||
|
@ -255,6 +270,7 @@ function yc_command_sbi(params, eeprom, L)
|
||||||
return new_eeprom
|
return new_eeprom
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--If
|
||||||
function yc_command_if(code, starti, L, eeprom)
|
function yc_command_if(code, starti, L, eeprom)
|
||||||
local cond, endi = yc_command_if_getcondition(code, starti)
|
local cond, endi = yc_command_if_getcondition(code, starti)
|
||||||
if cond == nil then return nil end
|
if cond == nil then return nil end
|
||||||
|
@ -268,6 +284,7 @@ function yc_command_if(code, starti, L, eeprom)
|
||||||
return result, endi --endi from local cond, endi = yc_command_if_getcondition(code, starti)
|
return result, endi --endi from local cond, endi = yc_command_if_getcondition(code, starti)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--Condition parsing
|
||||||
function yc_command_if_getcondition(code, starti)
|
function yc_command_if_getcondition(code, starti)
|
||||||
i = starti
|
i = starti
|
||||||
s = nil
|
s = nil
|
||||||
|
@ -376,6 +393,7 @@ function yc_command_parsecondition(cond, L, eeprom)
|
||||||
return cond
|
return cond
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--Virtual-Hardware functions
|
||||||
function yc_eeprom_read(number, eeprom)
|
function yc_eeprom_read(number, eeprom)
|
||||||
if number == nil then return nil, nil end
|
if number == nil then return nil, nil end
|
||||||
value = eeprom:sub(number, number)
|
value = eeprom:sub(number, number)
|
||||||
|
@ -383,11 +401,12 @@ function yc_eeprom_read(number, eeprom)
|
||||||
return value, endi
|
return value, endi
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--Real I/O functions
|
||||||
function yc_action(pos, L) --L-->Lvirtual
|
function yc_action(pos, L) --L-->Lvirtual
|
||||||
Lv = yc_get_virtual_portstates(pos)
|
Lv = yc_get_virtual_portstates(pos)
|
||||||
local meta = minetest.env:get_meta(pos)
|
local meta = minetest.env:get_meta(pos)
|
||||||
local code = meta:get_string("code")
|
local code = meta:get_string("code")
|
||||||
local heat = meta:get_string("heat")
|
local heat = meta:get_int("heat")
|
||||||
local eeprom = meta:get_string("eeprom")
|
local eeprom = meta:get_string("eeprom")
|
||||||
local infotext = meta:get_string("infotext")
|
local infotext = meta:get_string("infotext")
|
||||||
local formspec = meta:get_string("formspec")
|
local formspec = meta:get_string("formspec")
|
||||||
|
@ -399,7 +418,7 @@ function yc_action(pos, L) --L-->Lvirtual
|
||||||
minetest.env:add_node(pos, {name=name})
|
minetest.env:add_node(pos, {name=name})
|
||||||
local meta = minetest.env:get_meta(pos)
|
local meta = minetest.env:get_meta(pos)
|
||||||
meta:set_string("code", code)
|
meta:set_string("code", code)
|
||||||
meta:set_string("heat", heat)
|
meta:set_int("heat", heat)
|
||||||
meta:set_string("eeprom", eeprom)
|
meta:set_string("eeprom", eeprom)
|
||||||
meta:set_string("infotext", infotext)
|
meta:set_string("infotext", infotext)
|
||||||
meta:set_string("formspec", formspec)
|
meta:set_string("formspec", formspec)
|
||||||
|
@ -408,21 +427,24 @@ function yc_action(pos, L) --L-->Lvirtual
|
||||||
end
|
end
|
||||||
|
|
||||||
function yc_action_setports(pos, L, Lv)
|
function yc_action_setports(pos, L, Lv)
|
||||||
|
|
||||||
local name = "mesecons_microcontroller:microcontroller"
|
local name = "mesecons_microcontroller:microcontroller"
|
||||||
|
local rules
|
||||||
if Lv.a ~= L.a then
|
if Lv.a ~= L.a then
|
||||||
rules = mesecon:get_rules(name.."0001")
|
rules = mesecon:get_rules(name.."0001")
|
||||||
if L.a == true then mesecon:receptor_on(pos, rules)
|
if L.a == true then mesecon:receptor_on(pos, rules)
|
||||||
else mesecon:receptor_off(pos, rules) end
|
else mesecon:receptor_off(pos, rules) end
|
||||||
elseif Lv.b ~= L.b then
|
end
|
||||||
|
if Lv.b ~= L.b then
|
||||||
rules = mesecon:get_rules(name.."0010")
|
rules = mesecon:get_rules(name.."0010")
|
||||||
if L.b == true then mesecon:receptor_on(pos, rules)
|
if L.b == true then mesecon:receptor_on(pos, rules)
|
||||||
else mesecon:receptor_off(pos, rules) end
|
else mesecon:receptor_off(pos, rules) end
|
||||||
elseif Lv.c ~= L.c then
|
end
|
||||||
|
if Lv.c ~= L.c then
|
||||||
rules = mesecon:get_rules(name.."0100")
|
rules = mesecon:get_rules(name.."0100")
|
||||||
if L.c == true then mesecon:receptor_on(pos, rules)
|
if L.c == true then mesecon:receptor_on(pos, rules)
|
||||||
else mesecon:receptor_off(pos, rules) end
|
else mesecon:receptor_off(pos, rules) end
|
||||||
elseif Lv.d ~= L.d then
|
end
|
||||||
|
if Lv.d ~= L.d then
|
||||||
rules = mesecon:get_rules(name.."1000")
|
rules = mesecon:get_rules(name.."1000")
|
||||||
if L.d == true then mesecon:receptor_on(pos, rules)
|
if L.d == true then mesecon:receptor_on(pos, rules)
|
||||||
else mesecon:receptor_off(pos, rules) end
|
else mesecon:receptor_off(pos, rules) end
|
||||||
|
@ -466,7 +488,7 @@ function yc_get_virtual_portstates(pos)
|
||||||
return Lvirtual
|
return Lvirtual
|
||||||
end
|
end
|
||||||
|
|
||||||
function yc_merge_portstates(Lreal, Lvirtual) --TODO
|
function yc_merge_portstates(Lreal, Lvirtual)
|
||||||
local L = {a=false, b=false, c=false, d=false}
|
local L = {a=false, b=false, c=false, d=false}
|
||||||
if Lvirtual.a or Lreal.a then L.a = true end
|
if Lvirtual.a or Lreal.a then L.a = true end
|
||||||
if Lvirtual.b or Lreal.b then L.b = true end
|
if Lvirtual.b or Lreal.b then L.b = true end
|
||||||
|
@ -475,21 +497,45 @@ function yc_merge_portstates(Lreal, Lvirtual) --TODO
|
||||||
return L
|
return L
|
||||||
end
|
end
|
||||||
|
|
||||||
function yc_skip_to_endif(code, starti)
|
--"Overheat" protection
|
||||||
local i = starti
|
function yc_heat(meta)
|
||||||
local s = false
|
h = meta:get_int("heat")
|
||||||
while s ~= nil and s~= "" do
|
if h ~= nil then
|
||||||
s = code:sub(i, i)
|
meta:set_int("heat", h + 1)
|
||||||
if s == ";" then
|
|
||||||
return i + 1
|
|
||||||
end
|
end
|
||||||
i = i + 1
|
|
||||||
end
|
end
|
||||||
return nil
|
|
||||||
|
function yc_cool(meta)
|
||||||
|
h = meta:get_int("heat")
|
||||||
|
if h ~= nil then
|
||||||
|
meta:set_int("heat", h - 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function yc_overheat(meta)
|
||||||
|
h = meta:get_int("heat")
|
||||||
|
if h == nil then return true end -- if nil the overheat
|
||||||
|
if h>30 then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function yc_overheat_off(pos)
|
||||||
|
rules = mesecon:get_rules("mesecons_microcontroller:microcontroller1111")
|
||||||
|
mesecon:receptor_off(pos, rules);
|
||||||
end
|
end
|
||||||
|
|
||||||
mesecon:register_on_signal_change(function(pos, node)
|
mesecon:register_on_signal_change(function(pos, node)
|
||||||
if string.find(node.name, "mesecons_microcontroller:microcontroller")~=nil then
|
if string.find(node.name, "mesecons_microcontroller:microcontroller")~=nil then
|
||||||
minetest.after(0.5, update_yc, pos)
|
update_yc(pos)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
minetest.register_on_dignode(function(pos, node)
|
||||||
|
if string.find(node.name, "mesecons_microcontroller:microcontroller") then
|
||||||
|
rules = mesecon:get_rules(node.name)
|
||||||
|
mesecon:receptor_off(pos, rules)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user