From 1f9a9062e022c401826155efd3cd45a479d50c15 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Wed, 25 Oct 2017 10:51:19 +0200 Subject: Replace many math.floor(x+0.5) calls (or math.floor calls that should be those) by custom atround() function --- advtrains/helpers.lua | 6 +++++- advtrains/init.lua | 2 +- advtrains/trainlogic.lua | 42 +++++++++++++++++++++--------------------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index 91c0c1c..d7b977e 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -48,6 +48,10 @@ function advtrains.minN(list, expectstart) return n+1 end +function atround(number) + return math.floor(number+0.5) +end + --vertical_transmit: --[[ rely1, rely2 tell to which height the connections are pointed to. 1 means it will go up the next node @@ -80,7 +84,7 @@ function advtrains.conway(midreal, prev, drives_on)--in order prev,mid,return end --dir2??? local cor2=advtrains.dirCoordSet(mid, middir1)--<<<< - if math.floor(cor2.x+0.5)==math.floor(prev.x+0.5) and math.floor(cor2.z+0.5)==math.floor(prev.z+0.5) then + if atround(cor2.x)==atround(prev.x) and atround(cor2.z)==atround(prev.z) then next=advtrains.dirCoordSet(mid, middir2)--dir2 wird überprüft, alles gut. if midrely2>=1 then next.y=next.y+1 diff --git a/advtrains/init.lua b/advtrains/init.lua index 6f81a40..8991025 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -251,7 +251,7 @@ advtrains.avt_save = function(remove_players_from_wagons) local v=advtrains.merge_tables(train) --then invalidate if v.index then - v.restore_add_index=v.index-math.floor(v.index+0.5) + v.restore_add_index=v.index-math.floor(v.index+1) end v.path=nil v.path_dist=nil diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 6a0e61a..64b40f1 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -223,8 +223,8 @@ function advtrains.train_step_a(id, train, dtime) --- 2b. set node coverage old indices --- - train.detector_old_index = math.floor(train.index) - train.detector_old_end_index = math.floor(train.end_index) + train.detector_old_index = atround(train.index) + train.detector_old_end_index = atround(train.end_index) --- 3. handle velocity influences --- local train_moves=(train.velocity~=0) @@ -240,7 +240,7 @@ function advtrains.train_step_a(id, train, dtime) end --- 3a. this can be useful for debugs/warnings and is used for check_trainpartload --- - local t_info, train_pos=sid(id), train.path[math.floor(train.index)] + local t_info, train_pos=sid(id), train.path[atround(train.index)] if train_pos then t_info=t_info.." @"..minetest.pos_to_string(train_pos) --atprint("train_pos:",train_pos) @@ -347,19 +347,19 @@ function advtrains.train_step_a(id, train, dtime) --- 5a. make pos/yaw available for possible recover calls --- if train.max_index_on_tracktrain.index then --whoops, train went even more far. same behavior - train.savedpos_off_track_index_offset=train.index-train.min_index_on_track + train.savedpos_off_track_index_offset=atround(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] atprint("train is off-track (back), last positions kept at", train.last_pos, "/", 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)] - train.last_pos_prev=train.path[math.floor(train.index-0.5)] + train.last_pos=train.path[math.floor(train.index+1)] + train.last_pos_prev=train.path[math.floor(train.index)] end --- 5b. Remove path items that are no longer used --- @@ -367,10 +367,10 @@ function advtrains.train_step_a(id, train, dtime) local path_pregen_keep=20 local offtrack_keep=4 local gen_front_keep= path_pregen_keep - local gen_back_keep= math.floor(- train.trainlen - path_pregen_keep) + local gen_back_keep= atround(- train.trainlen - path_pregen_keep) - local delete_min=math.min(train.max_index_on_track - offtrack_keep, math.floor(train.index)+gen_back_keep) - local delete_max=math.max(train.min_index_on_track + offtrack_keep, math.floor(train.index)+gen_front_keep) + local delete_min=math.min(train.max_index_on_track - offtrack_keep, atround(train.index)+gen_back_keep) + local delete_max=math.max(train.min_index_on_track + offtrack_keep, atround(train.index)+gen_front_keep) if train.path_extent_mininv_radius and vector.distance(pos, cmp2)>inv_radius then exec=false end @@ -951,7 +951,7 @@ function advtrains.invalidate_all_paths(pos) if exec then --TODO duplicate code in init.lua avt_save()! if v.index then - v.restore_add_index=v.index-math.floor(v.index+0.5) + v.restore_add_index=v.index-math.floor(v.index+1) end v.path=nil v.path_dist=nil -- cgit v1.2.3