Lua automation - initial incomplete coding

master
orwell96 2017-02-02 00:02:11 +01:00
parent 78e936bea7
commit f6d1157ba1
8 changed files with 67 additions and 52 deletions

View File

@ -134,7 +134,7 @@ minetest.register_node(nodename, {
^- the height value of this rail that is saved in the path. usually the median of rely1 and rely2.
can_dig=function(pos)
return not advtrains.is_train_at_pos(pos)
return not advtrains.get_train_at_pos(pos)
end,
after_dig_node=function(pos)
advtrains.invalidate_all_paths()

View File

@ -8,8 +8,8 @@ function atc.load_data(data)
local temp = data and data.controllers or {}
--transcode atc controller data to node hashes: table access times for numbers are far less than for strings
for pts, data in pairs(temp) do
if type(pts)="string" then
pts=minetest.hash_node_position(minetest.pos_to_string(pts))
if type(pts)=="string" then
pts=minetest.hash_node_position(minetest.string_to_pos(pts))
end
atc.controllers[pts] = data
end
@ -28,7 +28,7 @@ end
--general
function atc.send_command(pos)
local pts=minetest.hash_node_position(ppos)
local pts=minetest.hash_node_position(pos)
if atc.controllers[pts] then
--atprint("Called send_command at "..pts)
local train_id = advtrains.detector.on_node[pts]
@ -88,7 +88,7 @@ advtrains.register_tracks("default", {
after_dig_node=function(pos)
advtrains.invalidate_all_paths()
advtrains.ndb.clear(pos)
local pts=minetest.hash_node_position(ppos)
local pts=minetest.hash_node_position(pos)
atc.controllers[pts]=nil
end,
on_receive_fields = function(pos, formname, fields, player)

View File

@ -208,6 +208,10 @@ minetest.register_abm({
if (nodeid~=node.name or param2~=node.param2) then
atprint("nodedb: lbm replaced", pos, "with nodeid", nodeid, "param2", param2, "cid is", cid)
minetest.swap_node(pos, {name=nodeid, param2 = param2})
local ndef=minetest.registered_nodes[nodeid]
if ndef and ndef.on_updated_from_nodedb then
ndef.on_updated_from_nodedb(pos, node)
end
end
end
else

View File

@ -1,6 +1,6 @@
--advtrains by orwell96
--signals.lua
for r,f in pairs({on="off", off="on"}) do
for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", als="green"}}) do
advtrains.trackplacer.register_tracktype("advtrains:retrosignal", "")
advtrains.trackplacer.register_tracktype("advtrains:signal", "")
@ -32,12 +32,12 @@ for r,f in pairs({on="off", off="on"}) do
save_in_nodedb=1,
},
mesecons = {effector = {
["action_"..f] = function (pos, node)
advtrains.ndb.swap_node(pos, {name = "advtrains:retrosignal_"..f..rotation, param2 = node.param2})
["action_"..f.as] = function (pos, node)
advtrains.ndb.swap_node(pos, {name = "advtrains:retrosignal_"..f.as..rotation, param2 = node.param2})
end
}},
on_rightclick=function(pos, node, clicker)
advtrains.ndb.swap_node(pos, {name = "advtrains:retrosignal_"..f..rotation, param2 = node.param2})
advtrains.ndb.swap_node(pos, {name = "advtrains:retrosignal_"..f.as..rotation, param2 = node.param2})
end,
})
advtrains.trackplacer.add_worked("advtrains:retrosignal", r, rotation, nil)
@ -65,12 +65,20 @@ for r,f in pairs({on="off", off="on"}) do
light_source = 1,
sunlight_propagates=true,
mesecons = {effector = {
["action_"..f] = function (pos, node)
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f..rotation, param2 = node.param2})
["action_"..f.as] = function (pos, node)
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f.as..rotation, param2 = node.param2})
end
}},
luaautomation = {
getstate = f.ls,
setstate = function(pos, node, newstate)
if newstate == f.als then
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f.as..rotation, param2 = node.param2})
end
end,
},
on_rightclick=function(pos, node, clicker)
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f..rotation, param2 = node.param2})
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f.as..rotation, param2 = node.param2})
end,
})
advtrains.trackplacer.add_worked("advtrains:signal", r, rotation, nil)

View File

@ -230,7 +230,7 @@ minetest.register_craftitem("advtrains:trackworker",{
local node=minetest.get_node(pos)
--if not advtrains.is_track_and_drives_on(minetest.get_node(pos).name, advtrains.all_tracktypes) then return end
if advtrains.is_train_at_pos(pos) then return end
if advtrains.get_train_at_pos(pos) then return end
local nnprefix, suffix, rotation=string.match(node.name, "^(.+)_([^_]+)(_[^_]+)$")
--atprint(node.name.."\npattern recognizes:"..nodeprefix.." / "..railtype.." / "..rotation)
@ -272,7 +272,7 @@ minetest.register_craftitem("advtrains:trackworker",{
end
--if not advtrains.is_track_and_drives_on(minetest.get_node(pos).name, advtrains.all_tracktypes) then return end
if advtrains.is_train_at_pos(pos) then return end
if advtrains.get_train_at_pos(pos) then return end
local nnprefix, suffix, rotation=string.match(node.name, "^(.+)_([^_]+)(_[^_]+)$")
--atprint(node.name.."\npattern recognizes:"..nodeprefix.." / "..railtype.." / "..rotation)
if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twcycle[suffix] then

View File

@ -77,6 +77,12 @@ ap.t_30deg={
swrst="on",
swrcr="off",
},
switchst={
swlst="st",
swlcr="cr",
swrst="st",
swrcr="cr",
},
regtp=true,
trackplacer={
st=true,
@ -195,6 +201,12 @@ ap.t_45deg={
swrst="on",
swrcr="off",
},
switchst={
swlst="st",
swlcr="cr",
swrst="st",
swrcr="cr",
},
regtp=true,
trackplacer={
st=true,
@ -233,16 +245,26 @@ advtrains.trackpresets = ap
common={} change something on common rail appearance
}]]
function advtrains.register_tracks(tracktype, def, preset)
local function make_switchfunc(suffix_target, mesecon_state)
local switchfunc=function(pos, node)
advtrains.ndb.swap_node(pos, {name=def.nodename_prefix.."_"..suffix_target, param2=node.param2})
local function make_switchfunc(suffix_target, mesecon_state, is_state)
local switchfunc=function(pos, node, newstate)
if newstate~=is_state then
advtrains.ndb.swap_node(pos, {name=def.nodename_prefix.."_"..suffix_target, param2=node.param2})
end
end
return switchfunc, {effector = {
["action_"..mesecon_state] = switchfunc,
rules=advtrains.meseconrules
}}
local mesec
if mesecon_state then -- if mesecons is not wanted, do not.
mesec = {effector = {
["action_"..mesecon_state] = switchfunc,
rules=advtrains.meseconrules
}}
end
return switchfunc, mesec,
{
getstate = is_state,
setstate = switchfunc,
}
end
local function make_overdef(suffix, rotation, conns, switchfunc, mesecontbl, in_creative_inv, drop_slope)
local function make_overdef(suffix, rotation, conns, switchfunc, mesecontbl, luaautomation, in_creative_inv, drop_slope)
local img_suffix=suffix..rotation
return {
mesh = def.shared_model or (def.models_prefix.."_"..img_suffix..def.models_suffix),
@ -266,6 +288,7 @@ function advtrains.register_tracks(tracktype, def, preset)
not_blocking_trains=1,
},
mesecons=mesecontbl,
luaautomation=luaautomation,
drop = increativeinv and def.nodename_prefix.."_"..suffix..rotation or (drop_slope and def.nodename_prefix.."_slopeplacer" or def.nodename_prefix.."_placer"),
}
end
@ -294,7 +317,7 @@ function advtrains.register_tracks(tracktype, def, preset)
railheight=0,
drop=def.nodename_prefix.."_placer",
can_dig=function(pos)
return not advtrains.is_train_at_pos(pos)
return not advtrains.get_train_at_pos(pos)
end,
after_dig_node=function(pos)
advtrains.invalidate_all_paths()
@ -315,9 +338,9 @@ function advtrains.register_tracks(tracktype, def, preset)
for suffix, conns in pairs(preset.variant) do
for rotid, rotation in ipairs(preset.rotation) do
if not def.formats[suffix] or def.formats[suffix][rotid] then
local switchfunc, mesecontbl
local switchfunc, mesecontbl, luaautomation
if preset.switch[suffix] then
switchfunc, mesecontbl=make_switchfunc(preset.switch[suffix]..rotation, preset.switchmc[suffix])
switchfunc, mesecontbl, luaautomation=make_switchfunc(preset.switch[suffix]..rotation, preset.switchmc[suffix], preset.switchst[suffix])
end
local adef={}
if def.get_additional_definiton then
@ -329,7 +352,7 @@ function advtrains.register_tracks(tracktype, def, preset)
make_overdef(
suffix, rotation,
cycle_conns(conns, rotid),
switchfunc, mesecontbl, preset.increativeinv[suffix], preset.slopenodes[suffix]
switchfunc, mesecontbl, luaautomation, preset.increativeinv[suffix], preset.slopenodes[suffix]
),
adef
)

View File

@ -754,31 +754,11 @@ function advtrains.invert_train(train_id)
advtrains.update_trainpart_properties(train_id, true)
end
function advtrains.is_train_at_pos(pos)
--atprint("istrainat: pos "..minetest.pos_to_string(pos))
local checked_trains={}
local objrefs=minetest.get_objects_inside_radius(pos, 2)
for _,v in pairs(objrefs) do
local le=v:get_luaentity()
if le and le.is_wagon and le.initialized and le.train_id and not checked_trains[le.train_id] then
--atprint("istrainat: checking "..le.train_id)
checked_trains[le.train_id]=true
local path=le:train().path
if path then
--atprint("has path")
for i=math.floor(advtrains.get_train_end_index(le:train())+0.5),math.floor(le:train().index+0.5) do
if path[i] then
--atprint("has pathitem "..i.." "..minetest.pos_to_string(path[i]))
if vector.equals(advtrains.round_vector_floor_y(path[i]), pos) then
return true
end
end
end
end
end
end
return false
function advtrains.get_train_at_pos(pos)
local ph=minetest.hash_node_position(advtrains.round_vector_floor_y(pos))
return advtrains.detector.on_node[ph]
end
function advtrains.invalidate_all_paths()
--atprint("invalidating all paths")
for k,v in pairs(advtrains.trains) do

View File

@ -25,7 +25,7 @@ local function create_map_form_with_bg(d)
local lbl={}
for pts, tid in pairs(advtrains.detector.on_node) do
local pos=minetest.string_to_pos(pts)
local pos=minetest.get_pos_from_hash(pts)
form=form.."box["..(edge_x*(pos.x-minx))..","..(form_z-(edge_z*(pos.z-minz)))..";"..len_x..","..len_z..";red]"
lbl[sid(tid)]=pos
end
@ -52,7 +52,7 @@ local function create_map_form(d)
if x>=minx and x<=maxx then
for z,y in pairs(itx) do
if z>=minz and z<=maxz then
local adn=advtrains.detector.on_node[minetest.pos_to_string({x=x, y=y, z=z})]
local adn=advtrains.detector.on_node[minetest.hash_node_position({x=x, y=y, z=z})]
local color="gray"
if adn then
color="red"