aboutsummaryrefslogtreecommitdiff
path: root/advtrains
diff options
context:
space:
mode:
authororwell96 <mono96.mml@gmail.com>2017-02-02 00:02:11 +0100
committerorwell96 <mono96.mml@gmail.com>2017-02-02 00:02:11 +0100
commitf6d1157ba1b3bc8519b7c01f6b5974b0f9f5cae1 (patch)
tree9b8196b017264a0cc56d72f2a29af7bd43d87ce2 /advtrains
parent78e936bea7831e9a0d253b72ca8a63da558c9e81 (diff)
downloadadvtrains-f6d1157ba1b3bc8519b7c01f6b5974b0f9f5cae1.tar.gz
advtrains-f6d1157ba1b3bc8519b7c01f6b5974b0f9f5cae1.tar.bz2
advtrains-f6d1157ba1b3bc8519b7c01f6b5974b0f9f5cae1.zip
Lua automation - initial incomplete coding
Diffstat (limited to 'advtrains')
-rw-r--r--advtrains/advtrains/api_doc.txt2
-rw-r--r--advtrains/advtrains/atc.lua8
-rw-r--r--advtrains/advtrains/nodedb.lua4
-rw-r--r--advtrains/advtrains/signals.lua22
-rw-r--r--advtrains/advtrains/trackplacer.lua4
-rw-r--r--advtrains/advtrains/tracks.lua47
-rw-r--r--advtrains/advtrains/trainlogic.lua28
-rw-r--r--advtrains/advtrains_itrainmap/init.lua4
8 files changed, 67 insertions, 52 deletions
diff --git a/advtrains/advtrains/api_doc.txt b/advtrains/advtrains/api_doc.txt
index af9c071..09b3cc1 100644
--- a/advtrains/advtrains/api_doc.txt
+++ b/advtrains/advtrains/api_doc.txt
@@ -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()
diff --git a/advtrains/advtrains/atc.lua b/advtrains/advtrains/atc.lua
index 7c2cec0..85c62d8 100644
--- a/advtrains/advtrains/atc.lua
+++ b/advtrains/advtrains/atc.lua
@@ -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)
diff --git a/advtrains/advtrains/nodedb.lua b/advtrains/advtrains/nodedb.lua
index 0a8c1e8..e3ed56d 100644
--- a/advtrains/advtrains/nodedb.lua
+++ b/advtrains/advtrains/nodedb.lua
@@ -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
diff --git a/advtrains/advtrains/signals.lua b/advtrains/advtrains/signals.lua
index d521c8a..b475d3c 100644
--- a/advtrains/advtrains/signals.lua
+++ b/advtrains/advtrains/signals.lua
@@ -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)
diff --git a/advtrains/advtrains/trackplacer.lua b/advtrains/advtrains/trackplacer.lua
index d1045aa..e5e3340 100644
--- a/advtrains/advtrains/trackplacer.lua
+++ b/advtrains/advtrains/trackplacer.lua
@@ -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
diff --git a/advtrains/advtrains/tracks.lua b/advtrains/advtrains/tracks.lua
index 2c5c121..cde2b35 100644
--- a/advtrains/advtrains/tracks.lua
+++ b/advtrains/advtrains/tracks.lua
@@ -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
)
diff --git a/advtrains/advtrains/trainlogic.lua b/advtrains/advtrains/trainlogic.lua
index 1bc732d..534b2b3 100644
--- a/advtrains/advtrains/trainlogic.lua
+++ b/advtrains/advtrains/trainlogic.lua
@@ -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
diff --git a/advtrains/advtrains_itrainmap/init.lua b/advtrains/advtrains_itrainmap/init.lua
index 60ee944..db1c40b 100644
--- a/advtrains/advtrains_itrainmap/init.lua
+++ b/advtrains/advtrains_itrainmap/init.lua
@@ -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"