diff options
Diffstat (limited to 'advtrains')
-rw-r--r-- | advtrains/advtrains/helpers.lua | 22 | ||||
-rw-r--r-- | advtrains/advtrains/pseudoload.lua | 9 | ||||
-rw-r--r-- | advtrains/advtrains/tracks.lua | 9 | ||||
-rw-r--r-- | advtrains/advtrains/trainlogic.lua | 46 |
4 files changed, 48 insertions, 38 deletions
diff --git a/advtrains/advtrains/helpers.lua b/advtrains/advtrains/helpers.lua index 1a02621..7ee7e71 100644 --- a/advtrains/advtrains/helpers.lua +++ b/advtrains/advtrains/helpers.lua @@ -58,11 +58,7 @@ rely1, rely2 tell to which height the connections are pointed to. 1 means it wil function advtrains.conway(midreal, prev, drives_on)--in order prev,mid,return
local mid=advtrains.round_vector_floor_y(midreal)
- if not advtrains.get_rail_info_at(advtrains.round_vector_floor_y(prev), drives_on) then
- return nil
- end
-
- local midnode_ok, middir1, middir2, midrely1, midrely2=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(mid), drives_on)
+ local midnode_ok, middir1, middir2, midrely1, midrely2=advtrains.get_rail_info_at(mid, drives_on)
if not midnode_ok then
return nil
end
@@ -72,7 +68,7 @@ function advtrains.conway(midreal, prev, drives_on)--in order prev,mid,return --print("[advtrains] in order mid1,mid2",middir1,middir2)
--try if it is dir1
local cor1=advtrains.dirCoordSet(mid, middir2)--<<<<
- if math.floor(cor1.x+0.5)==math.floor(prev.x+0.5) and math.floor(cor1.z+0.5)==math.floor(prev.z+0.5) then--this was previous
+ if cor1.x==prev.x and cor1.z==prev.z then--this was previous
next=advtrains.dirCoordSet(mid, middir1)
if midrely1>=1 then
next.y=next.y+1
@@ -99,7 +95,7 @@ function advtrains.conway(midreal, prev, drives_on)--in order prev,mid,return --print("[advtrains]dir applied next pos: "..(next and minetest.pos_to_string(next) or "nil").."(chkdir is "..(chkdir or "nil")..", y-offset "..y_offset..")")
--is there a next
if not next then
- --print("[advtrains]in conway: no next rail(nil), returning!")
+ print("[advtrains]in conway: no next rail(nil), returning!")
return nil
end
@@ -107,31 +103,31 @@ function advtrains.conway(midreal, prev, drives_on)--in order prev,mid,return --is it a rail?
if(not nextnode_ok) then
- --print("[advtrains]in conway: next "..minetest.pos_to_string(next).." not a rail, trying one node below!")
+ print("[advtrains]in conway: next "..minetest.pos_to_string(next).." not a rail, trying one node below!")
next.y=next.y-1
y_offset=y_offset-1
nextnode_ok, nextdir1, nextdir2, nextrely1, nextrely2, nextrailheight=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(next), drives_on)
if(not nextnode_ok) then
- --print("[advtrains]in conway: one below "..minetest.pos_to_string(next).." is not a rail either, returning!")
+ print("[advtrains]in conway: one below "..minetest.pos_to_string(next).." is not a rail either, returning!")
return nil
end
end
--is this next rail connecting to the mid?
if not ( (((nextdir1+8)%16)==chkdir and nextrely1==chkrely-y_offset) or (((nextdir2+8)%16)==chkdir and nextrely2==chkrely-y_offset) ) then
- --print("[advtrains]in conway: next "..minetest.pos_to_string(next).." not connecting, trying one node below!")
+ print("[advtrains]in conway: next "..minetest.pos_to_string(next).." not connecting, trying one node below!")
next.y=next.y-1
y_offset=y_offset-1
nextnode_ok, nextdir1, nextdir2, nextrely1, nextrely2, nextrailheight=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(next), drives_on)
if(not nextnode_ok) then
- --print("[advtrains]in conway: (at connecting if check again) one below "..minetest.pos_to_string(next).." is not a rail either, returning!")
+ print("[advtrains]in conway: (at connecting if check again) one below "..minetest.pos_to_string(next).." is not a rail either, returning!")
return nil
end
if not ( (((nextdir1+8)%16)==chkdir and nextrely1==chkrely) or (((nextdir2+8)%16)==chkdir and nextrely2==chkrely) ) then
- --print("[advtrains]in conway: one below "..minetest.pos_to_string(next).." rail not connecting, returning!")
- --print("[advtrains] in order mid1,2,next1,2,chkdir "..middir1.." "..middir2.." "..nextdir1.." "..nextdir2.." "..chkdir)
+ print("[advtrains]in conway: one below "..minetest.pos_to_string(next).." rail not connecting, returning!")
+ print("[advtrains] in order mid1,2,next1,2,chkdir "..middir1.." "..middir2.." "..nextdir1.." "..nextdir2.." "..chkdir)
return nil
end
end
diff --git a/advtrains/advtrains/pseudoload.lua b/advtrains/advtrains/pseudoload.lua index 677cd14..14db58f 100644 --- a/advtrains/advtrains/pseudoload.lua +++ b/advtrains/advtrains/pseudoload.lua @@ -134,11 +134,11 @@ end --nil if the node is neither loaded nor in trackdb --the distraction between false and nil will be needed only in special cases.(train initpos) function advtrains.get_rail_info_at(pos, drives_on) - local node=minetest.get_node_or_nil(pos) + local rdp=advtrains.round_vector_floor_y(pos) + local node=minetest.get_node_or_nil(rdp) if not node then --try raildb - local rdp=vector.round(pos) - local dbe=(advtrains.trackdb[traintype] and advtrains.trackdb[traintype][rdp.y] and advtrains.trackdb[traintype][rdp.y][rdp.x] and advtrains.trackdb[traintype][rdp.y][rdp.x][rdp.z]) + local dbe=(advtrains.trackdb[rdp.y] and advtrains.trackdb[rdp.y][rdp.x] and advtrains.trackdb[rdp.y][rdp.x][rdp.z]) if dbe then for tt,_ in pairs(drives_on) do if not dbe.tracktype or tt==dbe.tracktype then @@ -156,7 +156,6 @@ function advtrains.get_rail_info_at(pos, drives_on) local conn1, conn2, rely1, rely2, railheight, tracktype=advtrains.get_track_connections(node.name, node.param2) --already in trackdb? - local rdp=vector.round(pos) if not (advtrains.trackdb and advtrains.trackdb[rdp.y] and advtrains.trackdb[rdp.y][rdp.x] and advtrains.trackdb[rdp.y][rdp.x][rdp.z]) then--TODO is this necessary? if not advtrains.trackdb then advtrains.trackdb={} end if not advtrains.trackdb[rdp.y] then advtrains.trackdb[rdp.y]={} end @@ -171,7 +170,7 @@ function advtrains.get_rail_info_at(pos, drives_on) return true, conn1, conn2, rely1, rely2, railheight end function advtrains.reset_trackdb_position(pos) - local rdp=vector.round(pos) + local rdp=advtrains.round_vector_floor_y(pos) if not advtrains.trackdb then advtrains.trackdb={} end if not advtrains.trackdb[rdp.y] then advtrains.trackdb[rdp.y]={} end if not advtrains.trackdb[rdp.y][rdp.x] then advtrains.trackdb[rdp.y][rdp.x]={} end diff --git a/advtrains/advtrains/tracks.lua b/advtrains/advtrains/tracks.lua index beb0bbc..c20be70 100644 --- a/advtrains/advtrains/tracks.lua +++ b/advtrains/advtrains/tracks.lua @@ -375,7 +375,14 @@ function advtrains.get_track_connections(name, param2) if not param2 then noderot=0 end
if noderot > 3 then print("[advtrains] get_track_connections: rail has invaild param2 of "..noderot) noderot=0 end
- return (nodedef.connect1 + 4 * noderot)%16, (nodedef.connect2 + 4 * noderot)%16, nodedef.rely1 or 0, nodedef.rely2 or 0, nodedef.railheight or 0
+ local tracktype
+ for k,_ in pairs(nodedef.groups) do
+ local tt=string.match(k, "^advtrains_track_(.+)$")
+ if tt then
+ tracktype=tt
+ end
+ end
+ return (nodedef.connect1 + 4 * noderot)%16, (nodedef.connect2 + 4 * noderot)%16, nodedef.rely1 or 0, nodedef.rely2 or 0, nodedef.railheight or 0, tracktype
end
--detector code
diff --git a/advtrains/advtrains/trainlogic.lua b/advtrains/advtrains/trainlogic.lua index eb45500..0ad7874 100644 --- a/advtrains/advtrains/trainlogic.lua +++ b/advtrains/advtrains/trainlogic.lua @@ -36,7 +36,7 @@ advtrains.train_brake_force=3--per second, not divided by number of wagons advtrains.train_roll_force=0.5--per second, not divided by number of wagons, acceleration when rolling without brake advtrains.train_emerg_force=10--for emergency brakes(when going off track) -advtrains.audit_interval=30 +advtrains.audit_interval=10 advtrains.trains={} @@ -70,7 +70,7 @@ end advtrains.save = function() - --print("[advtrains]saving") + print("[advtrains]saving") advtrains.invalidate_all_paths() local datastr = minetest.serialize(advtrains.trains) if not datastr then @@ -261,14 +261,18 @@ function advtrains.train_step(id, train, dtime) local testpos=vector.add(rcollpos, {x=x, y=0, z=z}) local testpts=minetest.pos_to_string(testpos) if advtrains.detector.on_node[testpts] and advtrains.detector.on_node[testpts]~=id then - --collides - advtrains.spawn_couple_on_collide(id, testpos, advtrains.detector.on_node[testpts], train.movedir==-1) - - train.recently_collided_with_env=true - train.velocity=0.5*train.velocity - train.movedir=train.movedir*-1 - train.tarvelocity=0 - + if advtrains.trains[advtrains.detector.on_node[testpts]] then + --collides + advtrains.spawn_couple_on_collide(id, testpos, advtrains.detector.on_node[testpts], train.movedir==-1) + + train.recently_collided_with_env=true + train.velocity=0.5*train.velocity + train.movedir=train.movedir*-1 + train.tarvelocity=0 + else + --unexistant train left in this place + advtrains.detector.on_node[testpts]=nil + end end end end @@ -422,6 +426,7 @@ function advtrains.pathpredict(id, train) if node_ok==nil then --block not loaded, do nothing + print("[advtrains]last_pos not available") return nil elseif node_ok==false then print("[advtrains]no track here, (fail) removing train.") @@ -440,6 +445,7 @@ function advtrains.pathpredict(id, train) if prevnode_ok==nil then --block not loaded, do nothing + print("[advtrains]prev not available") return nil elseif prevnode_ok==false then print("[advtrains]no track at prev, (fail) removing train.") @@ -468,7 +474,7 @@ function advtrains.pathpredict(id, train) end - local maxn=advtrains.maxN(train.path) + local maxn=train.max_index_on_track or 0 while (maxn-train.index) < pregen_front do--pregenerate --print("[advtrains]maxn conway for ",maxn,minetest.pos_to_string(path[maxn]),maxn-1,minetest.pos_to_string(path[maxn-1])) local conway=advtrains.conway(train.path[maxn], train.path[maxn-1], train.drives_on) @@ -478,14 +484,14 @@ function advtrains.pathpredict(id, train) else --do as if nothing has happened and preceed with path --but do not update max_index_on_track - --print("over-generating path max to index "..maxn+1) + print("over-generating path max to index "..(maxn+1).." (position "..minetest.pos_to_string(train.path[maxn]).." )") train.path[maxn+1]=vector.add(train.path[maxn], vector.subtract(train.path[maxn], train.path[maxn-1])) end train.path_dist[maxn]=vector.distance(train.path[maxn+1], train.path[maxn]) maxn=advtrains.maxN(train.path) end - local minn=advtrains.minN(train.path) + local minn=train.min_index_on_track or 0 while (train.index-minn) < (train.trainlen or 0) + pregen_back do --post_generate. has to be at least trainlen. (we let go of the exact calculation here since this would be unuseful here) --print("[advtrains]minn conway for ",minn,minetest.pos_to_string(path[minn]),minn+1,minetest.pos_to_string(path[minn+1])) local conway=advtrains.conway(train.path[minn], train.path[minn+1], train.drives_on) @@ -495,7 +501,7 @@ function advtrains.pathpredict(id, train) else --do as if nothing has happened and preceed with path --but do not update min_index_on_track - --print("over-generating path min to index "..minn-1) + print("over-generating path min to index "..(minn-1).." (position "..minetest.pos_to_string(train.path[minn]).." )") train.path[minn-1]=vector.add(train.path[minn], vector.subtract(train.path[minn], train.path[minn+1])) end train.path_dist[minn-1]=vector.distance(train.path[minn], train.path[minn-1]) @@ -509,12 +515,12 @@ function advtrains.pathpredict(id, train) train.savedpos_off_track_index_offset=train.index-train.max_index_on_track train.last_pos=train.path[train.max_index_on_track] train.last_pos_prev=train.path[train.max_index_on_track-1] - --print("train is off-track (front), last positions kept at "..minetest.pos_to_string(train.last_pos).." / "..minetest.pos_to_string(train.last_pos_prev)) + print("train is off-track (front), last positions kept at "..minetest.pos_to_string(train.last_pos).." / "..minetest.pos_to_string(train.last_pos_prev)) elseif train.min_index_on_track+1>train.index then --whoops, train went even more far. same behavior train.savedpos_off_track_index_offset=train.index-train.min_index_on_track train.last_pos=train.path[train.min_index_on_track+1] train.last_pos_prev=train.path[train.min_index_on_track] - --print("train is off-track (back), last positions kept at "..minetest.pos_to_string(train.last_pos).." / "..minetest.pos_to_string(train.last_pos_prev)) + print("train is off-track (back), last positions kept at "..minetest.pos_to_string(train.last_pos).." / "..minetest.pos_to_string(train.last_pos_prev)) else --regular case train.savedpos_off_track_index_offset=nil train.last_pos=train.path[math.floor(train.index+0.5)] @@ -547,7 +553,7 @@ end function advtrains.update_trainpart_properties(train_id, invert_flipstate) local train=advtrains.trains[train_id] train.drives_on=advtrains.all_tracktypes - train.max_speed=100 + train.max_speed=20 local rel_pos=0 local count_l=0 for i, w_id in ipairs(train.trainparts) do @@ -680,6 +686,8 @@ function advtrains.spawn_couple_on_collide(id1, pos, id2, t1_is_backpos) local train1=advtrains.trains[id1] local train2=advtrains.trains[id2] + if not train1 or not train2 then return end + local found for i=advtrains.minN(train1.path), advtrains.maxN(train1.path) do if vector.equals(train1.path[i], pos) then @@ -696,9 +704,9 @@ function advtrains.spawn_couple_on_collide(id1, pos, id2, t1_is_backpos) local t2_is_backpos print("End positions: "..minetest.pos_to_string(frontpos2)..minetest.pos_to_string(backpos2)) - if vector.equals(frontpos2, pos) then + if vector.distance(frontpos2, pos)<2 then t2_is_backpos=false - elseif vector.equals(backpos2, pos) then + elseif vector.distance(backpos2, pos)<2 then t2_is_backpos=true else print("Err: not a endpos") |