From 987cd5b7f0ef86884858a107cc915f548675f2a8 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Sat, 31 Dec 2016 15:16:19 +0100 Subject: spawn couple on train collision also fix empty trains hanging around for too long --- advtrains/couple.lua | 2 +- advtrains/pseudoload.lua | 2 +- advtrains/trainlogic.lua | 190 ++++++++++++++++++++--------------------------- 3 files changed, 83 insertions(+), 111 deletions(-) diff --git a/advtrains/couple.lua b/advtrains/couple.lua index a846c8b..675bed5 100644 --- a/advtrains/couple.lua +++ b/advtrains/couple.lua @@ -140,7 +140,7 @@ minetest.register_entity("advtrains:couple", { else tp2=advtrains.get_real_index_position(train2.path, advtrains.get_train_end_index(train2)) end - if not tp1 or not tp2 or not (vector.distance(tp1,tp2)<0.5) then + if not tp1 or not tp2 or not (vector.distance(tp1,tp2)<1.5) then self.object:remove() return else diff --git a/advtrains/pseudoload.lua b/advtrains/pseudoload.lua index 3f4f321..677cd14 100644 --- a/advtrains/pseudoload.lua +++ b/advtrains/pseudoload.lua @@ -176,7 +176,7 @@ function advtrains.reset_trackdb_position(pos) 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 advtrains.trackdb[rdp.y][rdp.x][rdp.z]=nil - advtrains.get_rail_info_at(pos)--to restore it. + advtrains.get_rail_info_at(pos, advtrains.all_tracktypes)--to restore it. end diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index bb38fce..1050d62 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -2,6 +2,7 @@ --controls train entities stuff about connecting/disconnecting/colliding trains and other things local print=function(t, ...) minetest.log("action", table.concat({t, ...}, " ")) minetest.chat_send_all(table.concat({t, ...}, " ")) end +local sid=function(id) return string.sub(id, -4) end --local print=function() end local benchmark=false @@ -130,15 +131,7 @@ minetest.register_globalstep(function(dtime) advtrains.save_and_audit_timer=advtrains.save_and_audit_timer-dtime if advtrains.save_and_audit_timer<=0 then local t=os.clock() - --print("[advtrains] audit step") - --clean up orphaned trains - for k,v in pairs(advtrains.trains) do - --advtrains.update_trainpart_properties(k) - if #v.trainparts==0 then - print("[advtrains][train "..k.."] has empty trainparts, removing.") - advtrains.trains[k]=nil - end - end + --save advtrains.save() advtrains.save_and_audit_timer=advtrains.audit_interval @@ -244,26 +237,17 @@ function advtrains.train_step(id, train, dtime) train.detector_old_index = math.floor(train.index) train.detector_old_end_index = math.floor(train_end_index) + --remove? + if #train.trainparts==0 then + print("[advtrains][train "..sid(id).."] has empty trainparts, removing.") + advtrains.detector.leave_node(path[train.detector_old_index], id) + advtrains.trains[id]=nil + return + end + if train_moves then --check for collisions by finding objects - --front - local search_radius=4 - --coupling - local couple_outward=1 - local posfront=advtrains.get_real_index_position(path, train.index+couple_outward) - local posback=advtrains.get_real_index_position(path, train_end_index-couple_outward) - for _,pos in ipairs({posfront, posback}) do - if pos then - local objrefs=minetest.get_objects_inside_radius(pos, search_radius) - for _,v in pairs(objrefs) do - local le=v:get_luaentity() - if le and le.is_wagon and le.initialized and le.train_id~=id then - advtrains.try_connect_trains(id, le.train_id) - end - end - end - end --heh, new collision again. --this time, based on NODES and the advtrains.detector.on_node table. local collpos @@ -277,24 +261,29 @@ function advtrains.train_step(id, train, dtime) local rcollpos=advtrains.round_vector_floor_y(collpos) for x=-1,1 do for z=-1,1 do - local testpts=minetest.pos_to_string(vector.add(rcollpos, {x=x, y=0, z=z})) + 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 + end end end end end --check for any trainpart entities if they have been unloaded. do this only if train is near a player, to not spawn entities into unloaded areas + --todo function will be taken by update_trainpart_properties train.check_trainpartload=(train.check_trainpartload or 0)-dtime local node_range=(math.max((minetest.setting_get("active_block_range") or 0),1)*16) if train.check_trainpartload<=0 then local ori_pos=advtrains.get_real_index_position(path, train.index) --not much to calculate - print("[advtrains][train "..id.."] at "..minetest.pos_to_string(vector.round(ori_pos))) + --print("[advtrains][train "..id.."] at "..minetest.pos_to_string(vector.round(ori_pos))) local should_check=false for _,p in ipairs(minetest.get_connected_players()) do @@ -664,38 +653,7 @@ end --->backpos's will match --4. R<->F F<->R flip one of these trains and take it as new parent --->frontpos's will match -function advtrains.try_connect_trains(id1, id2) - local train1=advtrains.trains[id1] - local train2=advtrains.trains[id2] - if not train1 or not train2 then return end - if not train1.path or not train2.path then return end - if #train1.trainparts==0 or #train2.trainparts==0 then return end - - local frontpos1=advtrains.get_real_index_position(train1.path, train1.index) - local backpos1=advtrains.get_real_index_position(train1.path, advtrains.get_train_end_index(train1)) - --couple logic - --if train1.traintype==train2.traintype then - local frontpos2=advtrains.get_real_index_position(train2.path, train2.index) - local backpos2=advtrains.get_real_index_position(train2.path, advtrains.get_train_end_index(train2)) - - if not frontpos1 or not frontpos2 or not backpos1 or not backpos2 then return end - - local couple_spawnradius=0.7 - --case 1 (first train is front) - if vector.distance(frontpos2, backpos1)