diff options
Diffstat (limited to 'advtrains')
189 files changed, 3525 insertions, 0 deletions
diff --git a/advtrains/atc.lua b/advtrains/atc.lua new file mode 100644 index 0000000..0afddb0 --- /dev/null +++ b/advtrains/atc.lua @@ -0,0 +1,4 @@ +--atc.lua +--registers and controls the ATC system + +--(simple)mesecon detector rails diff --git a/advtrains/couple.lua b/advtrains/couple.lua new file mode 100644 index 0000000..a846c8b --- /dev/null +++ b/advtrains/couple.lua @@ -0,0 +1,154 @@ +--couple.lua +--defines couple entities. + +--advtrains:discouple +--set into existing trains to split them when punched. +--they are attached to the wagons. +--[[fields +wagon + +wagons keep their couple entity minetest-internal id inside the field discouple_id. if it refers to nowhere, they will spawn a new one if player is near +]] +local print=function(t, ...) minetest.log("action", table.concat({t, ...}, " ")) minetest.chat_send_all(table.concat({t, ...}, " ")) end + + +minetest.register_entity("advtrains:discouple", { + visual="sprite", + textures = {"advtrains_discouple.png"}, + collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5}, + visual_size = {x=1, y=1}, + initial_sprite_basepos = {x=0, y=0}, + + is_discouple=true, + on_activate=function(self, staticdata) + if staticdata=="DISCOUPLE" then + --couple entities have no right to exist further... + self.object:remove() + return + end + self.object:set_armor_groups({immortal=1}) + end, + get_staticdata=function() return "DISCOUPLE" end, + on_punch=function(self, player) + --only if player owns at least one wagon next to this + local own=player:get_player_name() + if self.wagon.owner and self.wagon.owner~=own then + local train=advtrains.trains[self.wagon.train_id] + local nextwgn_id=train.trainparts[self.wagon.pos_in_trainparts-1] + for aoi, le in pairs(minetest.luaentities) do + if le and le.is_wagon then + if le.unique_id==nextwgn_id then + if le.owner and le.owner~=own then + minetest.chat_send_player(own, "You need to own at least one neighboring wagon to destroy this couple.") + return + end + end + end + end + end + advtrains.split_train_at_wagon(self.wagon)--found in trainlogic.lua + end, + on_step=function(self, dtime) + local t=os.clock() + if not self.wagon then + self.object:remove() + return + end + --getyaw seems to be a reliable method to check if an object is loaded...if it returns nil, it is not. + if not self.wagon.object:getyaw() then + self.object:remove() + return + end + local velocityvec=self.wagon.object:getvelocity() + self.updatepct_timer=(self.updatepct_timer or 0)-dtime + if not self.old_velocity_vector or not vector.equals(velocityvec, self.old_velocity_vector) or self.updatepct_timer<=0 then--only send update packet if something changed + local flipsign=self.wagon.wagon_flipped and -1 or 1 + self.object:setpos(vector.add(self.wagon.object:getpos(), {y=0, x=-math.sin(self.wagon.object:getyaw())*self.wagon.wagon_span*flipsign, z=math.cos(self.wagon.object:getyaw())*self.wagon.wagon_span*flipsign})) + self.object:setvelocity(velocityvec) + self.updatepct_timer=2 + end + printbm("discouple_step", t) + end, +}) + +--advtrains:couple +--when two trains overlap with their end-positions, this entity will be spawned and both trains set its id into appropiate fields for them to know when to free them again. The entity will destroy automatically when it recognizes that any of the trains left the common position. +--[[fields +train_id_1 +train_id_2 +train1_is_backpos +train2_is_backpos +]] + + +minetest.register_entity("advtrains:couple", { + visual="sprite", + textures = {"advtrains_couple.png"}, + collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5}, + visual_size = {x=1, y=1}, + initial_sprite_basepos = {x=0, y=0}, + + is_couple=true, + on_activate=function(self, staticdata) + if staticdata=="COUPLE" then + --couple entities have no right to exist further... + self.object:remove() + return + end + end, + get_staticdata=function(self) return "COUPLE" end, + on_rightclick=function(self) + if not self.train_id_1 or not self.train_id_2 then return end + + local id1, id2=self.train_id_1, self.train_id_2 + + if self.train1_is_backpos and not self.train2_is_backpos then + advtrains.do_connect_trains(id1, id2) + --case 2 (second train is front) + elseif self.train2_is_backpos and not self.train1_is_backpos then + advtrains.do_connect_trains(id2, id1) + --case 3 + elseif self.train1_is_backpos and self.train2_is_backpos then + advtrains.invert_train(id2) + advtrains.do_connect_trains(id1, id2) + --case 4 + elseif not self.train1_is_backpos and not self.train2_is_backpos then + advtrains.invert_train(id1) + advtrains.do_connect_trains(id1, id2) + end + self.object:remove() + end, + on_step=function(self, dtime) + local t=os.clock() + if not self.train_id_1 or not self.train_id_2 then print("wtf no train ids?")return end + local train1=advtrains.trains[self.train_id_1] + local train2=advtrains.trains[self.train_id_2] + if not train1 or not train2 or not train1.path or not train2.path or not train1.index or not train2.index then + self.object:remove() + return + end + + local tp1 + if not self.train1_is_backpos then + tp1=advtrains.get_real_index_position(train1.path, train1.index) + else + tp1=advtrains.get_real_index_position(train1.path, advtrains.get_train_end_index(train1)) + end + local tp2 + if not self.train2_is_backpos then + tp2=advtrains.get_real_index_position(train2.path, train2.index) + 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 + self.object:remove() + return + else + local pos_median=advtrains.pos_median(tp1, tp2) + if not vector.equals(pos_median, self.object:getpos()) then + self.object:setpos(pos_median) + end + end + printbm("couple step", t) + end, +}) diff --git a/advtrains/crafting.lua b/advtrains/crafting.lua new file mode 100644 index 0000000..5ba12ce --- /dev/null +++ b/advtrains/crafting.lua @@ -0,0 +1,71 @@ +--advtrains by orwell96, see readme.txt and license.txt +--crafting.lua +--registers crafting recipes + +--tracks +minetest.register_craft({ + output = 'advtrains:dtrack_placer 50', + recipe = { + {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, + {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, + {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, + }, +}) +minetest.register_craft({ + type = "shapeless", + output = 'advtrains:dtrack_slopeplacer 2', + recipe = { + "advtrains:dtrack_placer", + "advtrains:dtrack_placer", + "default:gravel", + }, +}) + +minetest.register_craft({ + output = 'advtrains:dtrack_bumper_placer 2', + recipe = { + {'default:wood', 'dye:red'}, + {'default:steel_ingot', 'default:steel_ingot'}, + {'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, + }, +}) +minetest.register_craft({ + type="shapeless", + output = 'advtrains:dtrack_detector_off_placer', + recipe = { + "advtrains:dtrack_placer", + "mesecons:wire_00000000_off" + }, +}) +--signals +minetest.register_craft({ + output = 'advtrains:retrosignal_off 2', + recipe = { + {'dye:red', 'default:steel_ingot', 'default:steel_ingot'}, + {'', '', 'default:steel_ingot'}, + {'', '', 'default:steel_ingot'}, + }, +}) +minetest.register_craft({ + output = 'advtrains:signal_off 2', + recipe = { + {'', 'dye:red', 'default:steel_ingot'}, + {'', 'dye:dark_green', 'default:steel_ingot'}, + {'', '', 'default:steel_ingot'}, + }, +}) + +--trackworker +minetest.register_craft({ + output = 'advtrains:trackworker', + recipe = { + {'default:diamond'}, + {'screwdriver:screwdriver'}, + {'default:steel_ingot'}, + }, +}) + + + +--misc_nodes +--crafts for platforms see misc_nodes.lua diff --git a/advtrains/damage.lua b/advtrains/damage.lua new file mode 100644 index 0000000..b39fe67 --- /dev/null +++ b/advtrains/damage.lua @@ -0,0 +1,26 @@ +--damage.lua +--a globalstep that damages players overrolled by trains. + +advtrains.player_to_train_mapping={} + +local tmr=0 +minetest.register_globalstep(function(dtime) + tmr=tmr-dtime + if tmr<=0 then + + for _, player in pairs(minetest.get_connected_players()) do + local pos=player:getpos() + for _, object in pairs(minetest.get_objects_inside_radius(pos, 1)) do + local le=object:get_luaentity() + if le and le.is_wagon and le.initialized and le:train() then + if (not advtrains.player_to_train_mapping[player:get_player_name()] or le.train_id~=advtrains.player_to_train_mapping[player:get_player_name()]) and math.abs(le:train().velocity)>2 then + --player:punch(object, 1000, {damage={fleshy=3*math.abs(le:train().velocity)}}) + player:set_hp(player:get_hp()-math.abs(le:train().velocity)-3) + end + end + end + end + + tmr=0.5 + end +end) diff --git a/advtrains/debugitems.lua b/advtrains/debugitems.lua new file mode 100644 index 0000000..b3164ff --- /dev/null +++ b/advtrains/debugitems.lua @@ -0,0 +1,36 @@ +minetest.register_tool("advtrains:tunnelborer", +{ + description = "tunnelborer", + groups = {cracky=1}, -- key=name, value=rating; rating=1..3. + inventory_image = "drwho_screwdriver.png", + wield_image = "drwho_screwdriver.png", + stack_max = 1, + range = 7.0, + + on_place = function(itemstack, placer, pointed_thing) + + end, + --[[ + ^ Shall place item and return the leftover itemstack + ^ default: minetest.item_place ]] + on_use = function(itemstack, user, pointed_thing) + if pointed_thing.type=="node" then + for x=-1,1 do + for y=-1,1 do + for z=-1,1 do + minetest.remove_node(vector.add(pointed_thing.under, {x=x, y=y, z=z})) + end + end + end + end + end, +--[[ +^ default: nil +^ Function must return either nil if no item shall be removed from +inventory, or an itemstack to replace the original itemstack. +e.g. itemstack:take_item(); return itemstack +^ Otherwise, the function is free to do what it wants. +^ The default functions handle regular use cases. +]] +} +) diff --git a/advtrains/depends.txt b/advtrains/depends.txt new file mode 100644 index 0000000..20aa884 --- /dev/null +++ b/advtrains/depends.txt @@ -0,0 +1,2 @@ +default +mesecons?
\ No newline at end of file diff --git a/advtrains/description.txt b/advtrains/description.txt new file mode 100644 index 0000000..ecc5d58 --- /dev/null +++ b/advtrains/description.txt @@ -0,0 +1,8 @@ +Advanced Trains v1.3.0, by orwell and contributors. Also see readme. +Good-looking, realistic trains for minetest. + +For crafting recipes, see manual.pdf + +Website: http://advtrains.bleipb.de/ +Manual: https://github.com/orwell96/advtrains/blob/master/manual.pdf +Forum : https://forum.minetest.net/viewtopic.php?f=11&t=14726
\ No newline at end of file diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua new file mode 100644 index 0000000..6a8175f --- /dev/null +++ b/advtrains/helpers.lua @@ -0,0 +1,249 @@ +--advtrains by orwell96, see readme.txt
+local print=function(t) minetest.log("action", t) minetest.chat_send_all(t) end
+
+advtrains.dir_trans_tbl={
+ [0]={x=0, z=1},
+ [1]={x=1, z=2},
+ [2]={x=1, z=1},
+ [3]={x=2, z=1},
+ [4]={x=1, z=0},
+ [5]={x=2, z=-1},
+ [6]={x=1, z=-1},
+ [7]={x=1, z=-2},
+ [8]={x=0, z=-1},
+ [9]={x=-1, z=-2},
+ [10]={x=-1, z=-1},
+ [11]={x=-2, z=-1},
+ [12]={x=-1, z=0},
+ [13]={x=-2, z=1},
+ [14]={x=-1, z=1},
+ [15]={x=-1, z=2},
+}
+
+function advtrains.dirCoordSet(coord, dir)
+ local x,z
+ if advtrains.dir_trans_tbl[dir] then
+ x,z=advtrains.dir_trans_tbl[dir].x, advtrains.dir_trans_tbl[dir].z
+ else
+ error("advtrains: in helpers.lua/dirCoordSet() given dir="..(dir or "nil"))
+ end
+ return {x=coord.x+x, y=coord.y, z=coord.z+z}
+end
+function advtrains.dirToCoord(dir)
+ return advtrains.dirCoordSet({x=0, y=0, z=0}, dir)
+end
+
+function advtrains.maxN(list, expectstart)
+ local n=expectstart or 0
+ while list[n] do
+ n=n+1
+ end
+ return n-1
+end
+
+function advtrains.minN(list, expectstart)
+ local n=expectstart or 0
+ while list[n] do
+ n=n-1
+ end
+ return n+1
+end
+
+--vertical_transmit:
+--[[
+rely1, rely2 tell to which height the connections are pointed to. 1 means it will go up the next node
+
+]]
+
+function advtrains.conway(midreal, prev, traintype)--in order prev,mid,return
+ local mid=advtrains.round_vector_floor_y(midreal)
+ local drives_on=advtrains.all_traintypes[traintype].drives_on
+
+ if not advtrains.get_rail_info_at(advtrains.round_vector_floor_y(prev), traintype) then
+ return nil
+ end
+
+ local midnode_ok, middir1, middir2, midrely1, midrely2=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(mid), traintype)
+ if not midnode_ok then
+ return nil
+ end
+
+ local next, chkdir, chkrely, y_offset
+ y_offset=0
+ --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
+ next=advtrains.dirCoordSet(mid, middir1)
+ if midrely1>=1 then
+ next.y=next.y+1
+ --print("[advtrains]found midrely1 to be >=1: next is now "..(next and minetest.pos_to_string(next) or "nil"))
+ y_offset=1
+ end
+ chkdir=middir1
+ chkrely=midrely1
+ --print("[advtrains]dir2 applied next pos:",minetest.pos_to_string(next),"(chkdir is ",chkdir,")")
+ 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
+ next=advtrains.dirCoordSet(mid, middir2)--dir2 wird überprüft, alles gut.
+ if midrely2>=1 then
+ next.y=next.y+1
+ --print("[advtrains]found midrely2 to be >=1: next is now "..(next and minetest.pos_to_string(next) or "nil"))
+ y_offset=1
+ end
+ chkdir=middir2
+ chkrely=midrely2
+ --print("[advtrains] dir2 applied next pos:",minetest.pos_to_string(next),"(chkdir is ",chkdir,")")
+ end
+ --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!")
+ return nil
+ end
+
+ local nextnode_ok, nextdir1, nextdir2, nextrely1, nextrely2, nextrailheight=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(next), traintype)
+
+ --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!")
+ 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), traintype)
+ if(not nextnode_ok) then
+ --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!")
+ 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), traintype)
+ 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!")
+ 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)
+ return nil
+ end
+ end
+
+ --print("[advtrains]conway found rail.")
+ return vector.add(advtrains.round_vector_floor_y(next), {x=0, y=nextrailheight, z=0}), chkdir
+end
+--TODO use this
+function advtrains.oppd(dir)
+ return ((dir+8)%16)
+end
+
+function advtrains.round_vector_floor_y(vec)
+ return {x=math.floor(vec.x+0.5), y=math.floor(vec.y), z=math.floor(vec.z+0.5)}
+end
+
+function advtrains.yawToDirection(yaw, conn1, conn2)
+ if not conn1 or not conn2 then
+ error("given nil to yawToDirection: conn1="..(conn1 or "nil").." conn2="..(conn1 or "nil"))
+ end
+ local yaw1=math.pi*(conn1/4)
+ local yaw2=math.pi*(conn2/4)
+ if advtrains.minAngleDiffRad(yaw, yaw1)<advtrains.minAngleDiffRad(yaw, yaw2) then--change to > if weird behavior
+ return conn2
+ else
+ return conn1
+ end
+end
+
+function advtrains.minAngleDiffRad(r1, r2)
+ local try1=r2-r1
+ local try2=(r2+2*math.pi)-r1
+ local try3=r2-(r1+2*math.pi)
+ if math.min(math.abs(try1), math.abs(try2), math.abs(try3))==math.abs(try1) then
+ return try1
+ end
+ if math.min(math.abs(try1), math.abs(try2), math.abs(try3))==math.abs(try2) then
+ return try2
+ end
+ if math.min(math.abs(try1), math.abs(try2), math.abs(try3))==math.abs(try3) then
+ return try3
+ end
+end
+function advtrains.dumppath(path)
+ if not path then print("dumppath: no path(nil)") return end
+ local min=advtrains.minN(path)
+ local max=advtrains.maxN(path)
+ for i=min, max do print("["..i.."] "..(path[i] and minetest.pos_to_string(path[i]) or "nil")) end
+end
+
+function advtrains.merge_tables(a, ...)
+ local new={}
+ for _,t in ipairs({a,...}) do
+ for k,v in pairs(t) do new[k]=v end
+ end
+ return new
+end
+function advtrains.yaw_from_3_positions(prev, curr, next)
+ local pts=minetest.pos_to_string
+ --print("p3 "..pts(prev)..pts(curr)..pts(next))
+ local prev2curr=math.atan2((curr.x-prev.x), (prev.z-curr.z))
+ local curr2next=math.atan2((next.x-curr.x), (curr.z-next.z))
+ --print("y3 "..(prev2curr*360/(2*math.pi)).." "..(curr2next*360/(2*math.pi)))
+ return prev2curr+(advtrains.minAngleDiffRad(prev2curr, curr2next)/2)
+end
+function advtrains.get_wagon_yaw(front, first, second, back, pct)
+ local pts=minetest.pos_to_string
+ --print("p "..pts(front)..pts(first)..pts(second)..pts(back))
+ local y2=advtrains.yaw_from_3_positions(second, first, front)
+ local y1=advtrains.yaw_from_3_positions(back, second, first)
+ --print("y "..(y1*360/(2*math.pi)).." "..(y2*360/(2*math.pi)))
+ return y1+advtrains.minAngleDiffRad(y1, y2)*pct
+end
+function advtrains.get_real_index_position(path, index)
+ if not path or not index then return end
+
+ local first_pos=path[math.floor(index)]
+ local second_pos=path[math.floor(index)+1]
+
+ if not first_pos or not second_pos then return nil end
+
+ local factor=index-math.floor(index)
+ local actual_pos={x=first_pos.x-(first_pos.x-second_pos.x)*factor, y=first_pos.y-(first_pos.y-second_pos.y)*factor, z=first_pos.z-(first_pos.z-second_pos.z)*factor,}
+ return actual_pos
+end
+function advtrains.pos_median(pos1, pos2)
+ return {x=pos1.x-(pos1.x-pos2.x)*0.5, y=pos1.y-(pos1.y-pos2.y)*0.5, z=pos1.z-(pos1.z-pos2.z)*0.5}
+end
+function advtrains.abs_ceil(i)
+ return math.ceil(math.abs(i))*math.sign(i)
+end
+
+function advtrains.serialize_inventory(inv)
+ local ser={}
+ local liszts=inv:get_lists()
+ for lisztname, liszt in pairs(liszts) do
+ ser[lisztname]={}
+ for idx, item in ipairs(liszt) do
+ local istring=item:to_string()
+ if istring~="" then
+ ser[lisztname][idx]=istring
+ end
+ end
+ end
+ return minetest.serialize(ser)
+end
+function advtrains.deserialize_inventory(sers, inv)
+ local ser=minetest.deserialize(sers)
+ if ser then
+ inv:set_lists(ser)
+ return true
+ end
+ return false
+end
diff --git a/advtrains/init.lua b/advtrains/init.lua new file mode 100644 index 0000000..41b624e --- /dev/null +++ b/advtrains/init.lua @@ -0,0 +1,42 @@ +--advtrains + +advtrains={} + +advtrains.modpath = minetest.get_modpath("advtrains") + +--print=function(text) +-- minetest.log("action", tostring(text) or "<non-string>") +--end +print = function(t) minetest.log("action", t) minetest.chat_send_all(t) end + +dofile(advtrains.modpath.."/helpers.lua"); +dofile(advtrains.modpath.."/debugitems.lua"); + +advtrains.meseconrules = +{{x=0, y=0, z=-1}, + {x=1, y=0, z=0}, + {x=-1, y=0, z=0}, + {x=0, y=0, z=1}, + {x=1, y=1, z=0}, + {x=1, y=-1, z=0}, + {x=-1, y=1, z=0}, + {x=-1, y=-1, z=0}, + {x=0, y=1, z=1}, + {x=0, y=-1, z=1}, + {x=0, y=1, z=-1}, + {x=0, y=-1, z=-1}, + {x=0, y=-2, z=0}} +dofile(advtrains.modpath.."/trainlogic.lua") +dofile(advtrains.modpath.."/trainhud.lua") +dofile(advtrains.modpath.."/trackplacer.lua") +dofile(advtrains.modpath.."/tracks.lua") +dofile(advtrains.modpath.."/atc.lua") +dofile(advtrains.modpath.."/wagons.lua") + +dofile(advtrains.modpath.."/pseudoload.lua") +dofile(advtrains.modpath.."/couple.lua") +dofile(advtrains.modpath.."/damage.lua") + +dofile(advtrains.modpath.."/signals.lua") +dofile(advtrains.modpath.."/misc_nodes.lua") +dofile(advtrains.modpath.."/crafting.lua") diff --git a/advtrains/misc_nodes.lua b/advtrains/misc_nodes.lua new file mode 100644 index 0000000..93829f0 --- /dev/null +++ b/advtrains/misc_nodes.lua @@ -0,0 +1,67 @@ +--all nodes that do not fit in any other category + +function advtrains.register_platform(preset) + local ndef=minetest.registered_nodes[preset] + if not ndef then + minetest.log("warning", "[advtrains] register_platform couldn't find preset node "..preset) + return + end + local btex=ndef.tiles + if type(btex)=="table" then + btex=btex[1] + end + local desc=ndef.description or "" + local nodename=string.match(preset, ":(.+)$") + minetest.register_node("advtrains:platform_low_"..nodename, { + description = desc.." Platform (low)", + tiles = {btex.."^advtrains_platform.png", btex, btex, btex, btex, btex}, + groups = {cracky = 1, not_blocking_trains = 1, platform=1}, + sounds = default.node_sound_stone_defaults(), + drawtype = "nodebox", + node_box = { + type = "fixed", + fixed = { + {-0.5, -0.1, -0.1, 0.5, 0 , 0.5}, + {-0.5, -0.5, 0 , 0.5, -0.1, 0.5} + }, + }, + paramtype2="facedir", + paramtype = "light", + sunlight_propagates = true, + }) + minetest.register_node("advtrains:platform_high_"..nodename, { + description = desc.." Platform (high)", + tiles = {btex.."^advtrains_platform.png", btex, btex, btex, btex, btex}, + groups = {cracky = 1, not_blocking_trains = 1, platform=2}, + sounds = default.node_sound_stone_defaults(), + drawtype = "nodebox", + node_box = { + type = "fixed", + fixed = { + {-0.5, 0.3, -0.1, 0.5, 0.5, 0.5}, + {-0.5, -0.5, 0 , 0.5, 0.3, 0.5} + }, + }, + paramtype2="facedir", + paramtype = "light", + sunlight_propagates = true, + }) + minetest.register_craft({ + type="shapeless", + output = "advtrains:platform_high_"..nodename.." 4", + recipe = { + "dye:yellow", preset, preset + }, + }) + minetest.register_craft({ + type="shapeless", + output = "advtrains:platform_low_"..nodename.." 4", + recipe = { + "dye:yellow", preset + }, + }) +end + + +advtrains.register_platform("default:stonebrick") +advtrains.register_platform("default:sandstonebrick") diff --git a/advtrains/models/advtrains_dtrack_bumper_st.b3d b/advtrains/models/advtrains_dtrack_bumper_st.b3d Binary files differnew file mode 100644 index 0000000..a6d9745 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_bumper_st.b3d diff --git a/advtrains/models/advtrains_dtrack_bumper_st_30.b3d b/advtrains/models/advtrains_dtrack_bumper_st_30.b3d Binary files differnew file mode 100644 index 0000000..5f5b3f4 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_bumper_st_30.b3d diff --git a/advtrains/models/advtrains_dtrack_bumper_st_45.b3d b/advtrains/models/advtrains_dtrack_bumper_st_45.b3d Binary files differnew file mode 100644 index 0000000..f13ae75 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_bumper_st_45.b3d diff --git a/advtrains/models/advtrains_dtrack_bumper_st_60.b3d b/advtrains/models/advtrains_dtrack_bumper_st_60.b3d Binary files differnew file mode 100644 index 0000000..59a2285 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_bumper_st_60.b3d diff --git a/advtrains/models/advtrains_dtrack_cr.b3d b/advtrains/models/advtrains_dtrack_cr.b3d Binary files differnew file mode 100644 index 0000000..159717e --- /dev/null +++ b/advtrains/models/advtrains_dtrack_cr.b3d diff --git a/advtrains/models/advtrains_dtrack_cr_30.b3d b/advtrains/models/advtrains_dtrack_cr_30.b3d Binary files differnew file mode 100644 index 0000000..09cdb1f --- /dev/null +++ b/advtrains/models/advtrains_dtrack_cr_30.b3d diff --git a/advtrains/models/advtrains_dtrack_cr_45.b3d b/advtrains/models/advtrains_dtrack_cr_45.b3d Binary files differnew file mode 100644 index 0000000..176da81 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_cr_45.b3d diff --git a/advtrains/models/advtrains_dtrack_cr_60.b3d b/advtrains/models/advtrains_dtrack_cr_60.b3d Binary files differnew file mode 100644 index 0000000..00313c8 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_cr_60.b3d diff --git a/advtrains/models/advtrains_dtrack_detector_st.b3d b/advtrains/models/advtrains_dtrack_detector_st.b3d Binary files differnew file mode 100644 index 0000000..893c8cd --- /dev/null +++ b/advtrains/models/advtrains_dtrack_detector_st.b3d diff --git a/advtrains/models/advtrains_dtrack_detector_st_30.b3d b/advtrains/models/advtrains_dtrack_detector_st_30.b3d Binary files differnew file mode 100644 index 0000000..2e59e93 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_detector_st_30.b3d diff --git a/advtrains/models/advtrains_dtrack_detector_st_45.b3d b/advtrains/models/advtrains_dtrack_detector_st_45.b3d Binary files differnew file mode 100644 index 0000000..c73166f --- /dev/null +++ b/advtrains/models/advtrains_dtrack_detector_st_45.b3d diff --git a/advtrains/models/advtrains_dtrack_detector_st_60.b3d b/advtrains/models/advtrains_dtrack_detector_st_60.b3d Binary files differnew file mode 100644 index 0000000..b6bc9af --- /dev/null +++ b/advtrains/models/advtrains_dtrack_detector_st_60.b3d diff --git a/advtrains/models/advtrains_dtrack_st.b3d b/advtrains/models/advtrains_dtrack_st.b3d Binary files differnew file mode 100644 index 0000000..f3e2753 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_st.b3d diff --git a/advtrains/models/advtrains_dtrack_st_30.b3d b/advtrains/models/advtrains_dtrack_st_30.b3d Binary files differnew file mode 100644 index 0000000..7a35c8d --- /dev/null +++ b/advtrains/models/advtrains_dtrack_st_30.b3d diff --git a/advtrains/models/advtrains_dtrack_st_45.b3d b/advtrains/models/advtrains_dtrack_st_45.b3d Binary files differnew file mode 100644 index 0000000..b2a1702 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_st_45.b3d diff --git a/advtrains/models/advtrains_dtrack_st_60.b3d b/advtrains/models/advtrains_dtrack_st_60.b3d Binary files differnew file mode 100644 index 0000000..0a59f77 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_st_60.b3d diff --git a/advtrains/models/advtrains_dtrack_swlcr.b3d b/advtrains/models/advtrains_dtrack_swlcr.b3d Binary files differnew file mode 100644 index 0000000..1adc23f --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swlcr.b3d diff --git a/advtrains/models/advtrains_dtrack_swlcr_30.b3d b/advtrains/models/advtrains_dtrack_swlcr_30.b3d Binary files differnew file mode 100644 index 0000000..7d8373b --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swlcr_30.b3d diff --git a/advtrains/models/advtrains_dtrack_swlcr_45.b3d b/advtrains/models/advtrains_dtrack_swlcr_45.b3d Binary files differnew file mode 100644 index 0000000..9679b9e --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swlcr_45.b3d diff --git a/advtrains/models/advtrains_dtrack_swlcr_60.b3d b/advtrains/models/advtrains_dtrack_swlcr_60.b3d Binary files differnew file mode 100644 index 0000000..3efc924 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swlcr_60.b3d diff --git a/advtrains/models/advtrains_dtrack_swlst.b3d b/advtrains/models/advtrains_dtrack_swlst.b3d Binary files differnew file mode 100644 index 0000000..93841a4 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swlst.b3d diff --git a/advtrains/models/advtrains_dtrack_swlst_30.b3d b/advtrains/models/advtrains_dtrack_swlst_30.b3d Binary files differnew file mode 100644 index 0000000..e9a90c7 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swlst_30.b3d diff --git a/advtrains/models/advtrains_dtrack_swlst_45.b3d b/advtrains/models/advtrains_dtrack_swlst_45.b3d Binary files differnew file mode 100644 index 0000000..49c707c --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swlst_45.b3d diff --git a/advtrains/models/advtrains_dtrack_swlst_60.b3d b/advtrains/models/advtrains_dtrack_swlst_60.b3d Binary files differnew file mode 100644 index 0000000..c9a6ffe --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swlst_60.b3d diff --git a/advtrains/models/advtrains_dtrack_swrcr.b3d b/advtrains/models/advtrains_dtrack_swrcr.b3d Binary files differnew file mode 100644 index 0000000..ee29b62 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swrcr.b3d diff --git a/advtrains/models/advtrains_dtrack_swrcr_30.b3d b/advtrains/models/advtrains_dtrack_swrcr_30.b3d Binary files differnew file mode 100644 index 0000000..ba065e1 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swrcr_30.b3d diff --git a/advtrains/models/advtrains_dtrack_swrcr_45.b3d b/advtrains/models/advtrains_dtrack_swrcr_45.b3d Binary files differnew file mode 100644 index 0000000..7f9dc43 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swrcr_45.b3d diff --git a/advtrains/models/advtrains_dtrack_swrcr_60.b3d b/advtrains/models/advtrains_dtrack_swrcr_60.b3d Binary files differnew file mode 100644 index 0000000..b8ffa61 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swrcr_60.b3d diff --git a/advtrains/models/advtrains_dtrack_swrst.b3d b/advtrains/models/advtrains_dtrack_swrst.b3d Binary files differnew file mode 100644 index 0000000..0b3e7ad --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swrst.b3d diff --git a/advtrains/models/advtrains_dtrack_swrst_30.b3d b/advtrains/models/advtrains_dtrack_swrst_30.b3d Binary files differnew file mode 100644 index 0000000..4aea19b --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swrst_30.b3d diff --git a/advtrains/models/advtrains_dtrack_swrst_45.b3d b/advtrains/models/advtrains_dtrack_swrst_45.b3d Binary files differnew file mode 100644 index 0000000..4182fe5 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swrst_45.b3d diff --git a/advtrains/models/advtrains_dtrack_swrst_60.b3d b/advtrains/models/advtrains_dtrack_swrst_60.b3d Binary files differnew file mode 100644 index 0000000..6d2c891 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_swrst_60.b3d diff --git a/advtrains/models/advtrains_dtrack_vst1.b3d b/advtrains/models/advtrains_dtrack_vst1.b3d Binary files differnew file mode 100644 index 0000000..c9d7427 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_vst1.b3d diff --git a/advtrains/models/advtrains_dtrack_vst1_45.b3d b/advtrains/models/advtrains_dtrack_vst1_45.b3d Binary files differnew file mode 100644 index 0000000..14d438c --- /dev/null +++ b/advtrains/models/advtrains_dtrack_vst1_45.b3d diff --git a/advtrains/models/advtrains_dtrack_vst2.b3d b/advtrains/models/advtrains_dtrack_vst2.b3d Binary files differnew file mode 100644 index 0000000..c128650 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_vst2.b3d diff --git a/advtrains/models/advtrains_dtrack_vst2_45.b3d b/advtrains/models/advtrains_dtrack_vst2_45.b3d Binary files differnew file mode 100644 index 0000000..263276d --- /dev/null +++ b/advtrains/models/advtrains_dtrack_vst2_45.b3d diff --git a/advtrains/models/advtrains_dtrack_vst31.b3d b/advtrains/models/advtrains_dtrack_vst31.b3d Binary files differnew file mode 100644 index 0000000..df0f383 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_vst31.b3d diff --git a/advtrains/models/advtrains_dtrack_vst32.b3d b/advtrains/models/advtrains_dtrack_vst32.b3d Binary files differnew file mode 100644 index 0000000..01d2978 --- /dev/null +++ b/advtrains/models/advtrains_dtrack_vst32.b3d diff --git a/advtrains/models/advtrains_dtrack_vst33.b3d b/advtrains/models/advtrains_dtrack_vst33.b3d Binary files differnew file mode 100644 index 0000000..7fe418d --- /dev/null +++ b/advtrains/models/advtrains_dtrack_vst33.b3d diff --git a/advtrains/models/advtrains_modernwagon.b3d b/advtrains/models/advtrains_modernwagon.b3d Binary files differnew file mode 100644 index 0000000..aacddca --- /dev/null +++ b/advtrains/models/advtrains_modernwagon.b3d diff --git a/advtrains/models/advtrains_retrosignal_off.b3d b/advtrains/models/advtrains_retrosignal_off.b3d Binary files differnew file mode 100644 index 0000000..3d231dd --- /dev/null +++ b/advtrains/models/advtrains_retrosignal_off.b3d diff --git a/advtrains/models/advtrains_retrosignal_off_30.b3d b/advtrains/models/advtrains_retrosignal_off_30.b3d Binary files differnew file mode 100644 index 0000000..da258e1 --- /dev/null +++ b/advtrains/models/advtrains_retrosignal_off_30.b3d diff --git a/advtrains/models/advtrains_retrosignal_off_45.b3d b/advtrains/models/advtrains_retrosignal_off_45.b3d Binary files differnew file mode 100644 index 0000000..338224a --- /dev/null +++ b/advtrains/models/advtrains_retrosignal_off_45.b3d diff --git a/advtrains/models/advtrains_retrosignal_off_60.b3d b/advtrains/models/advtrains_retrosignal_off_60.b3d Binary files differnew file mode 100644 index 0000000..c560ca1 --- /dev/null +++ b/advtrains/models/advtrains_retrosignal_off_60.b3d diff --git a/advtrains/models/advtrains_retrosignal_on.b3d b/advtrains/models/advtrains_retrosignal_on.b3d Binary files differnew file mode 100644 index 0000000..3d19439 --- /dev/null +++ b/advtrains/models/advtrains_retrosignal_on.b3d diff --git a/advtrains/models/advtrains_retrosignal_on_30.b3d b/advtrains/models/advtrains_retrosignal_on_30.b3d Binary files differnew file mode 100644 index 0000000..98f8a92 --- /dev/null +++ b/advtrains/models/advtrains_retrosignal_on_30.b3d diff --git a/advtrains/models/advtrains_retrosignal_on_45.b3d b/advtrains/models/advtrains_retrosignal_on_45.b3d Binary files differnew file mode 100644 index 0000000..414e121 --- /dev/null +++ b/advtrains/models/advtrains_retrosignal_on_45.b3d diff --git a/advtrains/models/advtrains_retrosignal_on_60.b3d b/advtrains/models/advtrains_retrosignal_on_60.b3d Binary files differnew file mode 100644 index 0000000..a51529a --- /dev/null +++ b/advtrains/models/advtrains_retrosignal_on_60.b3d diff --git a/advtrains/models/advtrains_signal.b3d b/advtrains/models/advtrains_signal.b3d Binary files differnew file mode 100644 index 0000000..7f69560 --- /dev/null +++ b/advtrains/models/advtrains_signal.b3d diff --git a/advtrains/models/advtrains_signal_30.b3d b/advtrains/models/advtrains_signal_30.b3d Binary files differnew file mode 100644 index 0000000..0b949a7 --- /dev/null +++ b/advtrains/models/advtrains_signal_30.b3d diff --git a/advtrains/models/advtrains_signal_45.b3d b/advtrains/models/advtrains_signal_45.b3d Binary files differnew file mode 100644 index 0000000..ccaebf4 --- /dev/null +++ b/advtrains/models/advtrains_signal_45.b3d diff --git a/advtrains/models/advtrains_signal_60.b3d b/advtrains/models/advtrains_signal_60.b3d Binary files differnew file mode 100644 index 0000000..cf41e6d --- /dev/null +++ b/advtrains/models/advtrains_signal_60.b3d diff --git a/advtrains/models/advtrains_track_cr.b3d b/advtrains/models/advtrains_track_cr.b3d Binary files differnew file mode 100644 index 0000000..b0f5e4b --- /dev/null +++ b/advtrains/models/advtrains_track_cr.b3d diff --git a/advtrains/models/advtrains_track_st.b3d b/advtrains/models/advtrains_track_st.b3d Binary files differnew file mode 100644 index 0000000..10b5d90 --- /dev/null +++ b/advtrains/models/advtrains_track_st.b3d diff --git a/advtrains/models/advtrains_track_st_45.b3d b/advtrains/models/advtrains_track_st_45.b3d Binary files differnew file mode 100644 index 0000000..32505a1 --- /dev/null +++ b/advtrains/models/advtrains_track_st_45.b3d diff --git a/advtrains/models/blender/engine-with-animation.blend b/advtrains/models/blender/engine-with-animation.blend Binary files differnew file mode 100644 index 0000000..2649587 --- /dev/null +++ b/advtrains/models/blender/engine-with-animation.blend diff --git a/advtrains/models/blender/gleis/infos/Grafik_weiche.png b/advtrains/models/blender/gleis/infos/Grafik_weiche.png Binary files differnew file mode 100644 index 0000000..6674eb3 --- /dev/null +++ b/advtrains/models/blender/gleis/infos/Grafik_weiche.png diff --git a/advtrains/models/blender/gleis/infos/breite b/advtrains/models/blender/gleis/infos/breite new file mode 100644 index 0000000..b2862c6 --- /dev/null +++ b/advtrains/models/blender/gleis/infos/breite @@ -0,0 +1 @@ +1435mm diff --git a/advtrains/models/blender/gleis/licence.txt~ b/advtrains/models/blender/gleis/licence.txt~ new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/advtrains/models/blender/gleis/licence.txt~ diff --git a/advtrains/models/blender/gleis/rail_my.blend b/advtrains/models/blender/gleis/rail_my.blend Binary files differnew file mode 100644 index 0000000..3c741bc --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my.blend diff --git a/advtrains/models/blender/gleis/rail_my.blend1 b/advtrains/models/blender/gleis/rail_my.blend1 Binary files differnew file mode 100644 index 0000000..6322856 --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my.blend1 diff --git a/advtrains/models/blender/gleis/rail_my_rt.blend b/advtrains/models/blender/gleis/rail_my_rt.blend Binary files differnew file mode 100644 index 0000000..8372a0c --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my_rt.blend diff --git a/advtrains/models/blender/gleis/rail_my_rt.blend1 b/advtrains/models/blender/gleis/rail_my_rt.blend1 Binary files differnew file mode 100644 index 0000000..7d0978e --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my_rt.blend1 diff --git a/advtrains/models/blender/gleis/rail_my_rt_final.blend b/advtrains/models/blender/gleis/rail_my_rt_final.blend Binary files differnew file mode 100644 index 0000000..6a9a088 --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my_rt_final.blend diff --git a/advtrains/models/blender/gleis/rail_my_rt_final.blend1 b/advtrains/models/blender/gleis/rail_my_rt_final.blend1 Binary files differnew file mode 100644 index 0000000..1f346ae --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my_rt_final.blend1 diff --git a/advtrains/models/blender/gleis/rail_my_rt_switches.blend b/advtrains/models/blender/gleis/rail_my_rt_switches.blend Binary files differnew file mode 100644 index 0000000..fdce427 --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my_rt_switches.blend diff --git a/advtrains/models/blender/gleis/rail_my_rt_switches.blend1 b/advtrains/models/blender/gleis/rail_my_rt_switches.blend1 Binary files differnew file mode 100644 index 0000000..04370ba --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my_rt_switches.blend1 diff --git a/advtrains/models/blender/gleis/rail_my_rt_switches2.blend b/advtrains/models/blender/gleis/rail_my_rt_switches2.blend Binary files differnew file mode 100644 index 0000000..9562e4d --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my_rt_switches2.blend diff --git a/advtrains/models/blender/gleis/rail_my_rt_switches2.blend1 b/advtrains/models/blender/gleis/rail_my_rt_switches2.blend1 Binary files differnew file mode 100644 index 0000000..07a155f --- /dev/null +++ b/advtrains/models/blender/gleis/rail_my_rt_switches2.blend1 diff --git a/advtrains/models/blender/gleis/rail_together.blend b/advtrains/models/blender/gleis/rail_together.blend Binary files differnew file mode 100644 index 0000000..cff51ea --- /dev/null +++ b/advtrains/models/blender/gleis/rail_together.blend diff --git a/advtrains/models/blender/gleis/rail_together.png b/advtrains/models/blender/gleis/rail_together.png Binary files differnew file mode 100644 index 0000000..386cd5c --- /dev/null +++ b/advtrains/models/blender/gleis/rail_together.png diff --git a/advtrains/models/blender/gleis/rail_together2.png b/advtrains/models/blender/gleis/rail_together2.png Binary files differnew file mode 100644 index 0000000..3effe34 --- /dev/null +++ b/advtrains/models/blender/gleis/rail_together2.png diff --git a/advtrains/models/blender/gleis/rail_together3.png b/advtrains/models/blender/gleis/rail_together3.png Binary files differnew file mode 100644 index 0000000..4a183c1 --- /dev/null +++ b/advtrains/models/blender/gleis/rail_together3.png diff --git a/advtrains/models/blender/gleis/rail_verticals.blend b/advtrains/models/blender/gleis/rail_verticals.blend Binary files differnew file mode 100644 index 0000000..45c37e1 --- /dev/null +++ b/advtrains/models/blender/gleis/rail_verticals.blend diff --git a/advtrains/models/blender/gleis/rail_verticals.blend1 b/advtrains/models/blender/gleis/rail_verticals.blend1 Binary files differnew file mode 100644 index 0000000..320f30b --- /dev/null +++ b/advtrains/models/blender/gleis/rail_verticals.blend1 diff --git a/advtrains/models/blender/gleis/rail_verticals3.blend b/advtrains/models/blender/gleis/rail_verticals3.blend Binary files differnew file mode 100644 index 0000000..5d62959 --- /dev/null +++ b/advtrains/models/blender/gleis/rail_verticals3.blend diff --git a/advtrains/models/blender/gleis/rail_verticals3.blend1 b/advtrains/models/blender/gleis/rail_verticals3.blend1 Binary files differnew file mode 100644 index 0000000..893f2ea --- /dev/null +++ b/advtrains/models/blender/gleis/rail_verticals3.blend1 diff --git a/advtrains/models/blender/gleis/rail_verticals45.blend b/advtrains/models/blender/gleis/rail_verticals45.blend Binary files differnew file mode 100644 index 0000000..a1589e3 --- /dev/null +++ b/advtrains/models/blender/gleis/rail_verticals45.blend diff --git a/advtrains/models/blender/gleis/rail_verticals45.blend1 b/advtrains/models/blender/gleis/rail_verticals45.blend1 Binary files differnew file mode 100644 index 0000000..6b44d1c --- /dev/null +++ b/advtrains/models/blender/gleis/rail_verticals45.blend1 diff --git a/advtrains/models/blender/gleis/rail_verticals45.blend11 b/advtrains/models/blender/gleis/rail_verticals45.blend11 Binary files differnew file mode 100644 index 0000000..48316e8 --- /dev/null +++ b/advtrains/models/blender/gleis/rail_verticals45.blend11 diff --git a/advtrains/models/blender/gleis/texturen/rail.png b/advtrains/models/blender/gleis/texturen/rail.png Binary files differnew file mode 100644 index 0000000..8478cf6 --- /dev/null +++ b/advtrains/models/blender/gleis/texturen/rail.png diff --git a/advtrains/models/blender/gleis/texturen/uv.png b/advtrains/models/blender/gleis/texturen/uv.png Binary files differnew file mode 100644 index 0000000..fb0af3e --- /dev/null +++ b/advtrains/models/blender/gleis/texturen/uv.png diff --git a/advtrains/models/blender/industrial/industry_loc.blend b/advtrains/models/blender/industrial/industry_loc.blend Binary files differnew file mode 100644 index 0000000..1a6cb2b --- /dev/null +++ b/advtrains/models/blender/industrial/industry_loc.blend diff --git a/advtrains/models/blender/industrial/industry_loc.blend1 b/advtrains/models/blender/industrial/industry_loc.blend1 Binary files differnew file mode 100644 index 0000000..af54c3f --- /dev/null +++ b/advtrains/models/blender/industrial/industry_loc.blend1 diff --git a/advtrains/models/blender/industrial/industry_loc.blend11 b/advtrains/models/blender/industrial/industry_loc.blend11 Binary files differnew file mode 100644 index 0000000..a089a65 --- /dev/null +++ b/advtrains/models/blender/industrial/industry_loc.blend11 diff --git a/advtrains/models/blender/industrial/industry_oil_ wagong.blend b/advtrains/models/blender/industrial/industry_oil_ wagong.blend Binary files differnew file mode 100644 index 0000000..30bde51 --- /dev/null +++ b/advtrains/models/blender/industrial/industry_oil_ wagong.blend diff --git a/advtrains/models/blender/industrial/industry_oil_ wagong.blend1 b/advtrains/models/blender/industrial/industry_oil_ wagong.blend1 Binary files differnew file mode 100644 index 0000000..849971d --- /dev/null +++ b/advtrains/models/blender/industrial/industry_oil_ wagong.blend1 diff --git a/advtrains/models/blender/industrial/industry_tree_wagong.blend b/advtrains/models/blender/industrial/industry_tree_wagong.blend Binary files differnew file mode 100644 index 0000000..23de208 --- /dev/null +++ b/advtrains/models/blender/industrial/industry_tree_wagong.blend diff --git a/advtrains/models/blender/industrial/industry_tree_wagong.blend1 b/advtrains/models/blender/industrial/industry_tree_wagong.blend1 Binary files differnew file mode 100644 index 0000000..bf2c9ed --- /dev/null +++ b/advtrains/models/blender/industrial/industry_tree_wagong.blend1 diff --git a/advtrains/models/blender/industrial/industry_tree_wagong.png b/advtrains/models/blender/industrial/industry_tree_wagong.png Binary files differnew file mode 100644 index 0000000..87109dd --- /dev/null +++ b/advtrains/models/blender/industrial/industry_tree_wagong.png diff --git a/advtrains/models/blender/locomotive.blend b/advtrains/models/blender/locomotive.blend Binary files differnew file mode 100644 index 0000000..8fd6059 --- /dev/null +++ b/advtrains/models/blender/locomotive.blend diff --git a/advtrains/models/blender/magnet_track.blend b/advtrains/models/blender/magnet_track.blend Binary files differnew file mode 100644 index 0000000..0ab14e7 --- /dev/null +++ b/advtrains/models/blender/magnet_track.blend diff --git a/advtrains/models/blender/mbb/advtrains_wagon_japan.xcf b/advtrains/models/blender/mbb/advtrains_wagon_japan.xcf Binary files differnew file mode 100644 index 0000000..4fc88c0 --- /dev/null +++ b/advtrains/models/blender/mbb/advtrains_wagon_japan.xcf diff --git a/advtrains/models/blender/mbb/modern_japanlocomotive.blend b/advtrains/models/blender/mbb/modern_japanlocomotive.blend Binary files differnew file mode 100644 index 0000000..6eb8f3d --- /dev/null +++ b/advtrains/models/blender/mbb/modern_japanlocomotive.blend diff --git a/advtrains/models/blender/mbb/modern_japanlocomotive.blend1 b/advtrains/models/blender/mbb/modern_japanlocomotive.blend1 Binary files differnew file mode 100644 index 0000000..a2e745d --- /dev/null +++ b/advtrains/models/blender/mbb/modern_japanlocomotive.blend1 diff --git a/advtrains/models/blender/mbb/modern_japanlocomotive.png b/advtrains/models/blender/mbb/modern_japanlocomotive.png Binary files differnew file mode 100644 index 0000000..b80d16f --- /dev/null +++ b/advtrains/models/blender/mbb/modern_japanlocomotive.png diff --git a/advtrains/models/blender/mbb/modern_japanlocomotive.xcf b/advtrains/models/blender/mbb/modern_japanlocomotive.xcf Binary files differnew file mode 100644 index 0000000..bb28534 --- /dev/null +++ b/advtrains/models/blender/mbb/modern_japanlocomotive.xcf diff --git a/advtrains/models/blender/mbb/modern_locomotive.blend b/advtrains/models/blender/mbb/modern_locomotive.blend Binary files differnew file mode 100644 index 0000000..8e72233 --- /dev/null +++ b/advtrains/models/blender/mbb/modern_locomotive.blend diff --git a/advtrains/models/blender/mbb/modern_wagong.blend b/advtrains/models/blender/mbb/modern_wagong.blend Binary files differnew file mode 100644 index 0000000..f32710a --- /dev/null +++ b/advtrains/models/blender/mbb/modern_wagong.blend diff --git a/advtrains/models/blender/mbb/modern_wagong.blend1 b/advtrains/models/blender/mbb/modern_wagong.blend1 Binary files differnew file mode 100644 index 0000000..376831c --- /dev/null +++ b/advtrains/models/blender/mbb/modern_wagong.blend1 diff --git a/advtrains/models/blender/mbb/modern_wagong_uv.blend b/advtrains/models/blender/mbb/modern_wagong_uv.blend Binary files differnew file mode 100644 index 0000000..4c10fb3 --- /dev/null +++ b/advtrains/models/blender/mbb/modern_wagong_uv.blend diff --git a/advtrains/models/blender/mbb/modern_wagong_uv.blend1 b/advtrains/models/blender/mbb/modern_wagong_uv.blend1 Binary files differnew file mode 100644 index 0000000..ce77416 --- /dev/null +++ b/advtrains/models/blender/mbb/modern_wagong_uv.blend1 diff --git a/advtrains/models/blender/mbb/modern_wagong_uv.png b/advtrains/models/blender/mbb/modern_wagong_uv.png Binary files differnew file mode 100644 index 0000000..68018ea --- /dev/null +++ b/advtrains/models/blender/mbb/modern_wagong_uv.png diff --git a/advtrains/models/blender/mbb/prellbock.blend b/advtrains/models/blender/mbb/prellbock.blend Binary files differnew file mode 100644 index 0000000..1c20ab2 --- /dev/null +++ b/advtrains/models/blender/mbb/prellbock.blend diff --git a/advtrains/models/blender/mbb/prellbock.blend1 b/advtrains/models/blender/mbb/prellbock.blend1 Binary files differnew file mode 100644 index 0000000..981538e --- /dev/null +++ b/advtrains/models/blender/mbb/prellbock.blend1 diff --git a/advtrains/models/blender/mbb/prellbock.uv.png b/advtrains/models/blender/mbb/prellbock.uv.png Binary files differnew file mode 100644 index 0000000..7730753 --- /dev/null +++ b/advtrains/models/blender/mbb/prellbock.uv.png diff --git a/advtrains/models/blender/mbb/regio-lokomotive.blend b/advtrains/models/blender/mbb/regio-lokomotive.blend Binary files differnew file mode 100644 index 0000000..210e60e --- /dev/null +++ b/advtrains/models/blender/mbb/regio-lokomotive.blend diff --git a/advtrains/models/blender/mbb/regio-waron.blend b/advtrains/models/blender/mbb/regio-waron.blend Binary files differnew file mode 100644 index 0000000..19b07ce --- /dev/null +++ b/advtrains/models/blender/mbb/regio-waron.blend diff --git a/advtrains/models/blender/mbb/retrosignal.blend b/advtrains/models/blender/mbb/retrosignal.blend Binary files differnew file mode 100644 index 0000000..b795215 --- /dev/null +++ b/advtrains/models/blender/mbb/retrosignal.blend diff --git a/advtrains/models/blender/mbb/retrosignal.blend1 b/advtrains/models/blender/mbb/retrosignal.blend1 Binary files differnew file mode 100644 index 0000000..94af470 --- /dev/null +++ b/advtrains/models/blender/mbb/retrosignal.blend1 diff --git a/advtrains/models/blender/mbb/signal(2)_hoch.blend b/advtrains/models/blender/mbb/signal(2)_hoch.blend Binary files differnew file mode 100644 index 0000000..70391f6 --- /dev/null +++ b/advtrains/models/blender/mbb/signal(2)_hoch.blend diff --git a/advtrains/models/blender/mbb/signal(2)_unten.blend b/advtrains/models/blender/mbb/signal(2)_unten.blend Binary files differnew file mode 100644 index 0000000..4e917bc --- /dev/null +++ b/advtrains/models/blender/mbb/signal(2)_unten.blend diff --git a/advtrains/models/blender/mbb/signal.blend b/advtrains/models/blender/mbb/signal.blend Binary files differnew file mode 100644 index 0000000..135c7b4 --- /dev/null +++ b/advtrains/models/blender/mbb/signal.blend diff --git a/advtrains/models/blender/mbb/signal.blend1 b/advtrains/models/blender/mbb/signal.blend1 Binary files differnew file mode 100644 index 0000000..63a8c99 --- /dev/null +++ b/advtrains/models/blender/mbb/signal.blend1 diff --git a/advtrains/models/blender/mbb/signal.png b/advtrains/models/blender/mbb/signal.png Binary files differnew file mode 100644 index 0000000..779cdda --- /dev/null +++ b/advtrains/models/blender/mbb/signal.png diff --git a/advtrains/models/blender/mbb/signal.xcf b/advtrains/models/blender/mbb/signal.xcf Binary files differnew file mode 100644 index 0000000..f667739 --- /dev/null +++ b/advtrains/models/blender/mbb/signal.xcf diff --git a/advtrains/models/blender/mbb/signal2.png b/advtrains/models/blender/mbb/signal2.png Binary files differnew file mode 100644 index 0000000..7af4e2d --- /dev/null +++ b/advtrains/models/blender/mbb/signal2.png diff --git a/advtrains/models/blender/mbb/tr-logo.png b/advtrains/models/blender/mbb/tr-logo.png Binary files differnew file mode 100644 index 0000000..9bc0f10 --- /dev/null +++ b/advtrains/models/blender/mbb/tr-logo.png diff --git a/advtrains/models/blender/mbb/weichenhebel1.blend b/advtrains/models/blender/mbb/weichenhebel1.blend Binary files differnew file mode 100644 index 0000000..70b997d --- /dev/null +++ b/advtrains/models/blender/mbb/weichenhebel1.blend diff --git a/advtrains/models/blender/mbb/weichenhebel2.blend b/advtrains/models/blender/mbb/weichenhebel2.blend Binary files differnew file mode 100644 index 0000000..c1c3aac --- /dev/null +++ b/advtrains/models/blender/mbb/weichenhebel2.blend diff --git a/advtrains/models/blender/newlocomotive.blend b/advtrains/models/blender/newlocomotive.blend Binary files differnew file mode 100644 index 0000000..858882b --- /dev/null +++ b/advtrains/models/blender/newlocomotive.blend diff --git a/advtrains/models/blender/newlocomotive.blend1 b/advtrains/models/blender/newlocomotive.blend1 Binary files differnew file mode 100644 index 0000000..4b8b24f --- /dev/null +++ b/advtrains/models/blender/newlocomotive.blend1 diff --git a/advtrains/models/blender/newlocomotive_uvs.png b/advtrains/models/blender/newlocomotive_uvs.png Binary files differnew file mode 100644 index 0000000..879be65 --- /dev/null +++ b/advtrains/models/blender/newlocomotive_uvs.png diff --git a/advtrains/models/blender/newwagon.blend b/advtrains/models/blender/newwagon.blend Binary files differnew file mode 100644 index 0000000..1cedc55 --- /dev/null +++ b/advtrains/models/blender/newwagon.blend diff --git a/advtrains/models/blender/newwagon.blend1 b/advtrains/models/blender/newwagon.blend1 Binary files differnew file mode 100644 index 0000000..eb30fde --- /dev/null +++ b/advtrains/models/blender/newwagon.blend1 diff --git a/advtrains/models/blender/newwagon.png b/advtrains/models/blender/newwagon.png Binary files differnew file mode 100644 index 0000000..812bc32 --- /dev/null +++ b/advtrains/models/blender/newwagon.png diff --git a/advtrains/models/blender/subway-train.blend b/advtrains/models/blender/subway-train.blend Binary files differnew file mode 100644 index 0000000..690d87b --- /dev/null +++ b/advtrains/models/blender/subway-train.blend diff --git a/advtrains/models/blender/subway-train.blend1 b/advtrains/models/blender/subway-train.blend1 Binary files differnew file mode 100644 index 0000000..86d1f92 --- /dev/null +++ b/advtrains/models/blender/subway-train.blend1 diff --git a/advtrains/models/blender/subway-train.png b/advtrains/models/blender/subway-train.png Binary files differnew file mode 100644 index 0000000..d0b9731 --- /dev/null +++ b/advtrains/models/blender/subway-train.png diff --git a/advtrains/models/blender/subway-train.xcf b/advtrains/models/blender/subway-train.xcf Binary files differnew file mode 100644 index 0000000..775b87c --- /dev/null +++ b/advtrains/models/blender/subway-train.xcf diff --git a/advtrains/models/blender/trackplane.blend b/advtrains/models/blender/trackplane.blend Binary files differnew file mode 100644 index 0000000..79365f7 --- /dev/null +++ b/advtrains/models/blender/trackplane.blend diff --git a/advtrains/models/blender/trackvertical1.blend b/advtrains/models/blender/trackvertical1.blend Binary files differnew file mode 100644 index 0000000..fca6742 --- /dev/null +++ b/advtrains/models/blender/trackvertical1.blend diff --git a/advtrains/models/blender/trackvertical1.blend1 b/advtrains/models/blender/trackvertical1.blend1 Binary files differnew file mode 100644 index 0000000..b146b56 --- /dev/null +++ b/advtrains/models/blender/trackvertical1.blend1 diff --git a/advtrains/models/blender/trackvertical1.png b/advtrains/models/blender/trackvertical1.png Binary files differnew file mode 100644 index 0000000..a998dcb --- /dev/null +++ b/advtrains/models/blender/trackvertical1.png diff --git a/advtrains/models/blender/trackvertical2.blend b/advtrains/models/blender/trackvertical2.blend Binary files differnew file mode 100644 index 0000000..a066b84 --- /dev/null +++ b/advtrains/models/blender/trackvertical2.blend diff --git a/advtrains/models/blender/trackvertical2.png b/advtrains/models/blender/trackvertical2.png Binary files differnew file mode 100644 index 0000000..43142f9 --- /dev/null +++ b/advtrains/models/blender/trackvertical2.png diff --git a/advtrains/models/oldmodels/locomotive.b3d b/advtrains/models/oldmodels/locomotive.b3d Binary files differnew file mode 100644 index 0000000..5e16be3 --- /dev/null +++ b/advtrains/models/oldmodels/locomotive.b3d diff --git a/advtrains/models/oldmodels/trackvertical1.b3d b/advtrains/models/oldmodels/trackvertical1.b3d Binary files differnew file mode 100644 index 0000000..5620f60 --- /dev/null +++ b/advtrains/models/oldmodels/trackvertical1.b3d diff --git a/advtrains/models/oldmodels/trackvertical2.b3d b/advtrains/models/oldmodels/trackvertical2.b3d Binary files differnew file mode 100644 index 0000000..22dbb20 --- /dev/null +++ b/advtrains/models/oldmodels/trackvertical2.b3d diff --git a/advtrains/models/trackplane.b3d b/advtrains/models/trackplane.b3d Binary files differnew file mode 100644 index 0000000..b4728c3 --- /dev/null +++ b/advtrains/models/trackplane.b3d diff --git a/advtrains/pseudoload.lua b/advtrains/pseudoload.lua new file mode 100644 index 0000000..8511811 --- /dev/null +++ b/advtrains/pseudoload.lua @@ -0,0 +1,180 @@ +local print=function(t) minetest.log("action", t) minetest.chat_send_all(t) end + +--pseudoload.lua +--responsible for keeping up a database of all rail nodes existant in the world, regardless of whether the mapchunk is loaded. + +advtrains.trackdb={} +--trackdb[tt][y][x][z]={conn1, conn2, rely1, rely2, railheight} +--serialization format: +--(2byte x)(2byte y)(2byte z)(4bits conn1, 4bits conn2)[(plain rely1)|(plain rely2)|(plain railheight)]\n +--[] may be missing if 0,0,0 + +--load initially + +--[[ TODO temporary outcomment + +--delayed since all traintypes need to be registered +minetest.after(0, function() + +for tt, _ in pairs(advtrains.all_traintypes) do + local pl_fpath=minetest.get_worldpath().."/advtrains_trackdb_"..tt + advtrains.trackdb[tt]={} + local file, err = io.open(pl_fpath, "r") + if not file then + local er=err or "Unknown Error" + print("[advtrains]Failed loading advtrains trackdb save file "..er) + else + --custom format to save memory + while true do + local xbytes=file:read(2) + if not xbytes or #xbytes<2 then + break --eof reached + end + print(xbytes) + local ybytes=file:read(2) + local zbytes=file:read(2) + local x=(string.byte(xbytes[1])-128)*256+(string.byte(xbytes[2])) + local y=(string.byte(ybytes[1])-128)*256+(string.byte(ybytes[2])) + local z=(string.byte(zbytes[1])-128)*256+(string.byte(zbytes[2])) + + local conn1=string.byte(file:read(1)) + local conn1=string.byte(file:read(1)) + + if not advtrains.trackdb[tt][y] then advtrains.trackdb[tt][y]={} end + if not advtrains.trackdb[tt][y][x] then advtrains.trackdb[tt][y][x]={} end + + local rest=file.read("*l") + if rest~="" then + local rely1, rely2, railheight=string.match(rest, "([^|]+)|([^|]+)|([^|]+)") + if rely1 and rely2 and railheight then + advtrains.trackdb[tt][y][x][z]={ + conn1=conn1, conn2=conn2, + rely1=rely1, rely2=rely2, + railheight=railheight + } + else + advtrains.trackdb[tt][y][x][z]={ + conn1=conn1, conn2=conn2 + } + end + else + advtrains.trackdb[tt][y][x][z]={ + conn1=conn1, conn2=conn2 + } + end + end + file:close() + end +end + +--end minetest.after +end) + +function advtrains.save_trackdb() + for tt, _ in pairs(advtrains.all_traintypes) do + local pl_fpath=minetest.get_worldpath().."/advtrains_trackdb_"..tt + local file, err = io.open(pl_fpath, "w") + if not file then + local er=err or "Unknown Error" + print("[advtrains]Failed saving advtrains trackdb save file "..er) + else + --custom format to save memory + for y,tyl in pairs(advtrains.trackdb[tt]) do + for x,txl in pairs(tyl) do + for z,rail in pairs(txl) do + print("write "..x.." "..y.." "..z.." "..minetest.serialize(rail)) + file:write(string.char(math.floor(x/256)+128)..string.char((x%256))) + file:write(string.char(math.floor(y/256)+128)..string.char((y%256))) + file:write(string.char(math.floor(z/256)+128)..string.char((z%256))) + file:write(string.char(rail.conn1)) + file:write(string.char(rail.conn2)) + if (rail.rely1 and rail.rely1~=0) or (rail.rely2 and rail.rely2~=0) or (rail.railheight and rail.railheight~=0) then + file:write(rail.rely1.."|"..rail.rely2.."|"..rail.railheight) + end + file:write("\n") + end + end + end + file:close() + end + end +end +]]--end temp outcomment +advtrains.trackdb={} +advtrains.fpath_tdb=minetest.get_worldpath().."/advtrains_trackdb" +local file, err = io.open(advtrains.fpath_tdb, "r") +if not file then + local er=err or "Unknown Error" + print("[advtrains]Failed loading advtrains save file "..er) +else + local tbl = minetest.deserialize(file:read("*a")) + if type(tbl) == "table" then + advtrains.trackdb=tbl + end + file:close() +end +function advtrains.save_trackdb() + local datastr = minetest.serialize(advtrains.trackdb) + if not datastr then + minetest.log("error", "[advtrains] Failed to serialize trackdb data!") + return + end + local file, err = io.open(advtrains.fpath_tdb, "w") + if err then + return err + end + file:write(datastr) + file:close() +end + +--get_node with pseudoload. +--returns: +--true, conn1, conn2, rely1, rely2, railheight in case everything's right. +--false if it's not a rail or the train does not drive on this rail, but it is loaded or +--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, traintype) + local node=minetest.get_node_or_nil(pos) + 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]) + if dbe then + return true, dbe.conn1, dbe.conn2, dbe.rely1 or 0, dbe.rely2 or 0, dbe.railheight or 0 + else + return nil + end + end + local nodename=node.name + if(not advtrains.is_track_and_drives_on(nodename, advtrains.all_traintypes[traintype].drives_on)) then + return false + end + local conn1, conn2, rely1, rely2, railheight=advtrains.get_track_connections(node.name, node.param2) + + --already in trackdb? + local rdp=vector.round(pos) + if not (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]) then--TODO is this necessary? + if not advtrains.trackdb[traintype] then advtrains.trackdb[traintype]={} end + if not advtrains.trackdb[traintype][rdp.y] then advtrains.trackdb[traintype][rdp.y]={} end + if not advtrains.trackdb[traintype][rdp.y][rdp.x] then advtrains.trackdb[traintype][rdp.y][rdp.x]={} end + advtrains.trackdb[traintype][rdp.y][rdp.x][rdp.z]={ + conn1=conn1, conn2=conn2, + rely1=rely1, rely2=rely2, + railheight=railheight + } + end + + return true, conn1, conn2, rely1, rely2, railheight +end +function advtrains.reset_trackdb_position(pos) + local rdp=vector.round(pos) + for tt, _ in pairs(advtrains.all_traintypes) do + if not advtrains.trackdb[tt] then advtrains.trackdb[tt]={} end + if not advtrains.trackdb[tt][rdp.y] then advtrains.trackdb[tt][rdp.y]={} end + if not advtrains.trackdb[tt][rdp.y][rdp.x] then advtrains.trackdb[tt][rdp.y][rdp.x]={} end + advtrains.trackdb[tt][rdp.y][rdp.x][rdp.z]=nil + advtrains.get_rail_info_at(pos, tt)--to restore it. + end +end + + diff --git a/advtrains/readme.txt b/advtrains/readme.txt new file mode 100644 index 0000000..ad093bd --- /dev/null +++ b/advtrains/readme.txt @@ -0,0 +1,23 @@ +
+## ADVTRAINS ## realistic trains in Minetest!
+by orwell96 and contributors(see below)
+
+For up-to-date information, visit https://forum.minetest.net/viewtopic.php?f=9&t=14726 +
+
+Manual: +If manual.pdf is not present (which is the case when you downloaded the zip file), see https://github.com/orwell96/advtrains/blob/master/manual.pdf
+
+License of code: LGPL 2.1
+License of media: CC-BY-NC-SA 3.0
+
+Contributions:
+
+Gravel Texture : from Minetest Game
+Initial rail model/texture : DS-minetest
+Models for signals/bumpers : mbb
+Steam engine / wagon texture: mbb +Industrial engine/wagons : mbb +Inventory images : mbb +Small code contributions : NaruTrey / gpcf +Mod Description : hajo
\ No newline at end of file diff --git a/advtrains/signals.lua b/advtrains/signals.lua new file mode 100644 index 0000000..8be65e0 --- /dev/null +++ b/advtrains/signals.lua @@ -0,0 +1,76 @@ +--advtrains by orwell96 +--signals.lua +for r,f in pairs({on="off", off="on"}) do + + advtrains.trackplacer.register_tracktype("advtrains:retrosignal", "") + advtrains.trackplacer.register_tracktype("advtrains:signal", "") + + for rotid, rotation in ipairs({"", "_30", "_45", "_60"}) do + local crea=1 + if rotid==1 and r=="off" then crea=0 end + + minetest.register_node("advtrains:retrosignal_"..r..rotation, { + drawtype = "mesh", + paramtype="light", + paramtype2="facedir", + walkable = false, + selection_box = { + type = "fixed", + fixed = {-1/4, -1/2, -1/4, 1/4, 2, 1/4}, + }, + mesh = "advtrains_retrosignal_"..r..rotation..".b3d", + tiles = {"advtrains_retrosignal.png"}, + inventory_image="advtrains_retrosignal_inv.png", + drop="advtrains:retrosignal_off", + description="Lampless Signal ("..r..rotation..")", + on_rightclick=switchfunc, + sunlight_propagates=true, + groups = { + choppy=3, + not_blocking_trains=1, + not_in_creative_inventory=crea, + }, + mesecons = {effector = { + ["action_"..f] = function (pos, node) + minetest.swap_node(pos, {name = "advtrains:retrosignal_"..f..rotation, param2 = node.param2}) + end + }}, + on_rightclick=function(pos, node, clicker) + minetest.swap_node(pos, {name = "advtrains:retrosignal_"..f..rotation, param2 = node.param2}) + end, + }) + advtrains.trackplacer.add_worked("advtrains:retrosignal", r, rotation, nil) + minetest.register_node("advtrains:signal_"..r..rotation, { + drawtype = "mesh", + paramtype="light", + paramtype2="facedir", + walkable = false, + selection_box = { + type = "fixed", + fixed = {-1/4, -1/2, -1/4, 1/4, 2, 1/4}, + }, + mesh = "advtrains_signal"..rotation..".b3d", + tiles = {"advtrains_signal_"..r..".png"}, + inventory_image="advtrains_signal_inv.png", + drop="advtrains:signal_off", + description="Signal ("..r..rotation..")", + on_rightclick=switchfunc, + groups = { + choppy=3, + not_blocking_trains=1, + not_in_creative_inventory=crea, + }, + light_source = 1, + sunlight_propagates=true, + mesecons = {effector = { + ["action_"..f] = function (pos, node) + minetest.swap_node(pos, {name = "advtrains:signal_"..f..rotation, param2 = node.param2}) + end + }}, + on_rightclick=function(pos, node, clicker) + minetest.swap_node(pos, {name = "advtrains:signal_"..f..rotation, param2 = node.param2}) + end, + }) + advtrains.trackplacer.add_worked("advtrains:signal", r, rotation, nil) + end +end diff --git a/advtrains/textures/advtrains_couple.png b/advtrains/textures/advtrains_couple.png Binary files differnew file mode 100644 index 0000000..9e997e4 --- /dev/null +++ b/advtrains/textures/advtrains_couple.png diff --git a/advtrains/textures/advtrains_discouple.png b/advtrains/textures/advtrains_discouple.png Binary files differnew file mode 100644 index 0000000..b27c4fb --- /dev/null +++ b/advtrains/textures/advtrains_discouple.png diff --git a/advtrains/textures/advtrains_dtrack_bumper_placer.png b/advtrains/textures/advtrains_dtrack_bumper_placer.png Binary files differnew file mode 100644 index 0000000..27191fe --- /dev/null +++ b/advtrains/textures/advtrains_dtrack_bumper_placer.png diff --git a/advtrains/textures/advtrains_dtrack_detector_placer.png b/advtrains/textures/advtrains_dtrack_detector_placer.png Binary files differnew file mode 100644 index 0000000..e6c6ad6 --- /dev/null +++ b/advtrains/textures/advtrains_dtrack_detector_placer.png diff --git a/advtrains/textures/advtrains_dtrack_placer.png b/advtrains/textures/advtrains_dtrack_placer.png Binary files differnew file mode 100644 index 0000000..c49777c --- /dev/null +++ b/advtrains/textures/advtrains_dtrack_placer.png diff --git a/advtrains/textures/advtrains_dtrack_rail.png b/advtrains/textures/advtrains_dtrack_rail.png Binary files differnew file mode 100644 index 0000000..568e533 --- /dev/null +++ b/advtrains/textures/advtrains_dtrack_rail.png diff --git a/advtrains/textures/advtrains_dtrack_rail_detector_on.png b/advtrains/textures/advtrains_dtrack_rail_detector_on.png Binary files differnew file mode 100644 index 0000000..3ae0dac --- /dev/null +++ b/advtrains/textures/advtrains_dtrack_rail_detector_on.png diff --git a/advtrains/textures/advtrains_dtrack_slopeplacer.png b/advtrains/textures/advtrains_dtrack_slopeplacer.png Binary files differnew file mode 100644 index 0000000..1d456b0 --- /dev/null +++ b/advtrains/textures/advtrains_dtrack_slopeplacer.png diff --git a/advtrains/textures/advtrains_newlocomotive.xcf b/advtrains/textures/advtrains_newlocomotive.xcf Binary files differnew file mode 100644 index 0000000..9ad5187 --- /dev/null +++ b/advtrains/textures/advtrains_newlocomotive.xcf diff --git a/advtrains/textures/advtrains_platform.png b/advtrains/textures/advtrains_platform.png Binary files differnew file mode 100644 index 0000000..5ba9663 --- /dev/null +++ b/advtrains/textures/advtrains_platform.png diff --git a/advtrains/textures/advtrains_retrosignal.png b/advtrains/textures/advtrains_retrosignal.png Binary files differnew file mode 100644 index 0000000..141198d --- /dev/null +++ b/advtrains/textures/advtrains_retrosignal.png diff --git a/advtrains/textures/advtrains_retrosignal_inv.png b/advtrains/textures/advtrains_retrosignal_inv.png Binary files differnew file mode 100644 index 0000000..1036594 --- /dev/null +++ b/advtrains/textures/advtrains_retrosignal_inv.png diff --git a/advtrains/textures/advtrains_signal_inv.png b/advtrains/textures/advtrains_signal_inv.png Binary files differnew file mode 100644 index 0000000..ed64ed9 --- /dev/null +++ b/advtrains/textures/advtrains_signal_inv.png diff --git a/advtrains/textures/advtrains_signal_off.png b/advtrains/textures/advtrains_signal_off.png Binary files differnew file mode 100644 index 0000000..8046e52 --- /dev/null +++ b/advtrains/textures/advtrains_signal_off.png diff --git a/advtrains/textures/advtrains_signal_on.png b/advtrains/textures/advtrains_signal_on.png Binary files differnew file mode 100644 index 0000000..5228bb3 --- /dev/null +++ b/advtrains/textures/advtrains_signal_on.png diff --git a/advtrains/textures/advtrains_track_cr.png b/advtrains/textures/advtrains_track_cr.png Binary files differnew file mode 100644 index 0000000..40f0cc5 --- /dev/null +++ b/advtrains/textures/advtrains_track_cr.png diff --git a/advtrains/textures/advtrains_track_cr_45.png b/advtrains/textures/advtrains_track_cr_45.png Binary files differnew file mode 100644 index 0000000..54966b3 --- /dev/null +++ b/advtrains/textures/advtrains_track_cr_45.png diff --git a/advtrains/textures/advtrains_track_placer.png b/advtrains/textures/advtrains_track_placer.png Binary files differnew file mode 100644 index 0000000..03e17ed --- /dev/null +++ b/advtrains/textures/advtrains_track_placer.png diff --git a/advtrains/textures/advtrains_track_st.png b/advtrains/textures/advtrains_track_st.png Binary files differnew file mode 100644 index 0000000..5ad7e4f --- /dev/null +++ b/advtrains/textures/advtrains_track_st.png diff --git a/advtrains/textures/advtrains_track_st_45.png b/advtrains/textures/advtrains_track_st_45.png Binary files differnew file mode 100644 index 0000000..63b4c96 --- /dev/null +++ b/advtrains/textures/advtrains_track_st_45.png diff --git a/advtrains/textures/advtrains_track_swlcr.png b/advtrains/textures/advtrains_track_swlcr.png Binary files differnew file mode 100644 index 0000000..d9b5c0b --- /dev/null +++ b/advtrains/textures/advtrains_track_swlcr.png diff --git a/advtrains/textures/advtrains_track_swlcr_45.png b/advtrains/textures/advtrains_track_swlcr_45.png Binary files differnew file mode 100644 index 0000000..f098fc9 --- /dev/null +++ b/advtrains/textures/advtrains_track_swlcr_45.png diff --git a/advtrains/textures/advtrains_track_swlst.png b/advtrains/textures/advtrains_track_swlst.png Binary files differnew file mode 100644 index 0000000..314bd2d --- /dev/null +++ b/advtrains/textures/advtrains_track_swlst.png diff --git a/advtrains/textures/advtrains_track_swlst_45.png b/advtrains/textures/advtrains_track_swlst_45.png Binary files differnew file mode 100644 index 0000000..765d0ec --- /dev/null +++ b/advtrains/textures/advtrains_track_swlst_45.png diff --git a/advtrains/textures/advtrains_track_swrcr.png b/advtrains/textures/advtrains_track_swrcr.png Binary files differnew file mode 100644 index 0000000..f74e1bc --- /dev/null +++ b/advtrains/textures/advtrains_track_swrcr.png diff --git a/advtrains/textures/advtrains_track_swrcr_45.png b/advtrains/textures/advtrains_track_swrcr_45.png Binary files differnew file mode 100644 index 0000000..fa432aa --- /dev/null +++ b/advtrains/textures/advtrains_track_swrcr_45.png diff --git a/advtrains/textures/advtrains_track_swrst.png b/advtrains/textures/advtrains_track_swrst.png Binary files differnew file mode 100644 index 0000000..06ea29e --- /dev/null +++ b/advtrains/textures/advtrains_track_swrst.png diff --git a/advtrains/textures/advtrains_track_swrst_45.png b/advtrains/textures/advtrains_track_swrst_45.png Binary files differnew file mode 100644 index 0000000..be477b7 --- /dev/null +++ b/advtrains/textures/advtrains_track_swrst_45.png diff --git a/advtrains/textures/advtrains_trackworker.png b/advtrains/textures/advtrains_trackworker.png Binary files differnew file mode 100644 index 0000000..b50bcae --- /dev/null +++ b/advtrains/textures/advtrains_trackworker.png diff --git a/advtrains/textures/drwho_screwdriver.png b/advtrains/textures/drwho_screwdriver.png Binary files differnew file mode 100644 index 0000000..b50bcae --- /dev/null +++ b/advtrains/textures/drwho_screwdriver.png diff --git a/advtrains/textures/larger rails.xcf b/advtrains/textures/larger rails.xcf Binary files differnew file mode 100644 index 0000000..42ae592 --- /dev/null +++ b/advtrains/textures/larger rails.xcf diff --git a/advtrains/trackplacer.lua b/advtrains/trackplacer.lua new file mode 100644 index 0000000..eebf2ae --- /dev/null +++ b/advtrains/trackplacer.lua @@ -0,0 +1,282 @@ +--trackplacer.lua +--holds code for the track-placing system. the default 'track' item will be a craftitem that places rails as needed. this will neither place or change switches nor place vertical rails. + +--all new trackplacer code +local tp={ + tracks={} +} + +function tp.register_tracktype(nnprefix, n_suffix) + tp.tracks[nnprefix]={ + default=n_suffix, + single_conn={}, + double_conn={}, + --keys:conn1_conn2 (example:1_4) + --values:{name=x, param2=x} + twcycle={}, + twrotate={},--indexed by suffix, list, tells order of rotations + modify={} + } +end +function tp.add_double_conn(nnprefix, suffix, rotation, conns) + local nodename=nnprefix.."_"..suffix..rotation + for i=0,3 do + tp.tracks[nnprefix].double_conn[((conns.conn1+4*i)%16).."_"..((conns.conn2+4*i)%16)]={name=nodename, param2=i} + tp.tracks[nnprefix].double_conn[((conns.conn2+4*i)%16).."_"..((conns.conn1+4*i)%16)]={name=nodename, param2=i} + end + tp.tracks[nnprefix].modify[nodename]=true +end +function tp.add_single_conn(nnprefix, suffix, rotation, conns) + local nodename=nnprefix.."_"..suffix..rotation + for i=0,3 do + tp.tracks[nnprefix].single_conn[((conns.conn1+4*i)%16)]={name=nodename, param2=i} + tp.tracks[nnprefix].single_conn[((conns.conn2+4*i)%16)]={name=nodename, param2=i} + end + tp.tracks[nnprefix].modify[nodename]=true +end + +function tp.add_worked(nnprefix, suffix, rotation, cycle_follows) + tp.tracks[nnprefix].twcycle[suffix]=cycle_follows + if not tp.tracks[nnprefix].twrotate[suffix] then tp.tracks[nnprefix].twrotate[suffix]={} end + table.insert(tp.tracks[nnprefix].twrotate[suffix], rotation) +end + + +--[[ + rewrite algorithm. + selection criteria: these will never be changed or even selected: + - tracks being already connected on both sides + - tracks that are already connected on one side but are not bendable to the desired position + the following situations can occur: + 1. there are two more than two rails around + 1.1 there is one or more subset(s) that can be directly connected + -> choose the first possibility + 2.2 not + -> choose the first one and orient straight + 2. there's exactly 1 rail around + -> choose and orient straight + 3. there's no rail around + -> set straight +]] +function tp.find_already_connected(pos)--TODO vertical calculations(check node below) + local function istrackandbc(pos, conn) + local cnode=minetest.get_node(advtrains.dirCoordSet(pos, conn)) + local bconn=(conn+8)%16 + if advtrains.is_track_and_drives_on(cnode.name, advtrains.all_tracktypes) then + local cconn1, cconn2=advtrains.get_track_connections(cnode.name, cnode.param2) + return cconn1==bconn or cconn2==bconn + end + return false + end + local dnode=minetest.get_node(pos) + local dconn1, dconn2=advtrains.get_track_connections(dnode.name, dnode.param2) + local t={[true]="true", [false]="false"} + if istrackandbc(pos, dconn1) and istrackandbc(pos, dconn2) then return dconn1, dconn2 + elseif istrackandbc(pos, dconn1) then return dconn1 + elseif istrackandbc(pos, dconn2) then return dconn2 + end + return nil +end +function tp.rail_and_can_be_bent(originpos, conn, nnpref) + local pos=advtrains.dirCoordSet(originpos, conn) + local newdir=(conn+8)%16 + local node=minetest.get_node(pos) + local tr=tp.tracks[nnpref] + if not advtrains.is_track_and_drives_on(node.name, advtrains.all_tracktypes) then + return false + end + --rail at other end? + local adj1, adj2=tp.find_already_connected(pos) + if adj1 and adj2 then + return false--dont destroy existing track + elseif adj1 and not adj2 then + if tr.double_conn[adj1.."_"..newdir] then + return true--if exists, connect new rail and old end + end + return false + else + if tr.single_conn[newdir] then--just rotate old rail to right orientation + return true + end + return false + end +end +function tp.bend_rail(originpos, conn, nnpref) + local pos=advtrains.dirCoordSet(originpos, conn) + local newdir=(conn+8)%16 + local node=minetest.get_node(pos) + local tr=tp.tracks[nnpref] + --is rail already connected? no need to bend. + local conn1, conn2=advtrains.get_track_connections(node.name, node.param2) + if newdir==conn1 or newdir==conn2 then + return + end + --rail at other end? + local adj1, adj2=tp.find_already_connected(pos) + if adj1 and adj2 then + return false--dont destroy existing track + elseif adj1 and not adj2 then + if tr.double_conn[adj1.."_"..newdir] then + minetest.set_node(pos, tr.double_conn[adj1.."_"..newdir]) + return true--if exists, connect new rail and old end + end + return false + else + if tr.single_conn[newdir] then--just rotate old rail to right orientation + minetest.set_node(pos, tr.single_conn[newdir]) + return true + end + return false + end +end +function tp.placetrack(pos, nnpref) + --1. find all rails that are likely to be connected + local tr=tp.tracks[nnpref] + local p_rails={} + for i=0,15 do + if tp.rail_and_can_be_bent(pos, i, nnpref) then + p_rails[#p_rails+1]=i + end + end + if #p_rails==0 then + minetest.set_node(pos, {name=nnpref.."_"..tr.default}) + elseif #p_rails==1 then + tp.bend_rail(pos, p_rails[1], nnpref) + minetest.set_node(pos, tr.single_conn[p_rails[1]]) + else + --iterate subsets + for k1, conn1 in ipairs(p_rails) do + for k2, conn2 in ipairs(p_rails) do + if k1~=k2 then + if (tr.double_conn[conn1.."_"..conn2]) then + tp.bend_rail(pos, conn1, nnpref) + tp.bend_rail(pos, conn2, nnpref) + minetest.set_node(pos, tr.double_conn[conn1.."_"..conn2]) + return + end + end + end + end + --not found + tp.bend_rail(pos, p_rails[1], nnpref) + minetest.set_node(pos, tr.single_conn[p_rails[1]]) + end +end + + +function tp.register_track_placer(nnprefix, imgprefix, dispname) + minetest.register_craftitem(nnprefix.."_placer",{ + description = dispname, + inventory_image = imgprefix.."_placer.png", + wield_image = imgprefix.."_placer.png", + groups={}, + on_place = function(itemstack, placer, pointed_thing) + local name = placer:get_player_name() + if not name then + return itemstack + end + if pointed_thing.type=="node" then + local pos=pointed_thing.above + local upos=pointed_thing.under + if minetest.is_protected(pos,name) and minetest.is_protected(upos,name) then + return itemstack + end + if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to + and minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable then + tp.placetrack(pos, nnprefix) + if not minetest.setting_getbool("creative_mode") then + itemstack:take_item() + end + end + end + return itemstack + end, + }) +end + + + +minetest.register_craftitem("advtrains:trackworker",{ + description = "Track Worker Tool\n\nLeft-click: change rail type (straight/curve/switch)\nRight-click: rotate rail/bumper/signal/etc.", + groups = {cracky=1}, -- key=name, value=rating; rating=1..3. + inventory_image = "advtrains_trackworker.png", + wield_image = "advtrains_trackworker.png", + stack_max = 1, + on_place = function(itemstack, placer, pointed_thing) + local name = placer:get_player_name() + if not name then + return + end + if pointed_thing.type=="node" then + local pos=pointed_thing.under + if minetest.is_protected(pos, name) then + return + end + 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 + + local nnprefix, suffix, rotation=string.match(node.name, "^(.+)_([^_]+)(_[^_]+)$") + --print(node.name.."\npattern recognizes:"..nodeprefix.." / "..railtype.." / "..rotation) + if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twrotate[suffix] then + nnprefix, suffix=string.match(node.name, "^(.+)_([^_]+)$") + rotation = "" + if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twrotate[suffix] then + minetest.chat_send_player(placer:get_player_name(), "This node can't be rotated using the trackworker!") + return + end + end + local modext=tp.tracks[nnprefix].twrotate[suffix] + + if rotation==modext[#modext] then --increase param2 + minetest.set_node(pos, {name=nnprefix.."_"..suffix..modext[1], param2=(node.param2+1)%4}) + return + else + local modpos + for k,v in pairs(modext) do if v==rotation then modpos=k end end + if not modpos then + minetest.chat_send_player(placer:get_player_name(), "This node can't be rotated using the trackworker!") + return + end + minetest.set_node(pos, {name=nnprefix.."_"..suffix..modext[modpos+1], param2=node.param2}) + end + advtrains.invalidate_all_paths() + end + end, + on_use=function(itemstack, user, pointed_thing) + local name = user:get_player_name() + if not name then + return + end + if pointed_thing.type=="node" then + local pos=pointed_thing.under + local node=minetest.get_node(pos) + if minetest.is_protected(pos, name) then + return + 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 + local nnprefix, suffix, rotation=string.match(node.name, "^(.+)_([^_]+)(_[^_]+)$") + --print(node.name.."\npattern recognizes:"..nodeprefix.." / "..railtype.." / "..rotation) + if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twcycle[suffix] then + nnprefix, suffix=string.match(node.name, "^(.+)_([^_]+)$") + rotation = "" + if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twcycle[suffix] then + minetest.chat_send_player(user:get_player_name(), "This node can't be changed using the trackworker!") + return + end + end + local nextsuffix=tp.tracks[nnprefix].twcycle[suffix] + minetest.set_node(pos, {name=nnprefix.."_"..nextsuffix..rotation, param2=node.param2}) + --invalidate trains + advtrains.invalidate_all_paths() + else + print(name, dump(tp.tracks)) + end + end, +}) + +--putting into right place +advtrains.trackplacer=tp diff --git a/advtrains/tracks.lua b/advtrains/tracks.lua new file mode 100644 index 0000000..99dbb27 --- /dev/null +++ b/advtrains/tracks.lua @@ -0,0 +1,693 @@ +--advtrains by orwell96, see readme.txt
+
+--dev-time settings:
+--EDIT HERE
+--If the old non-model rails on straight tracks should be replaced by the new...
+--false: no
+--true: yes
+advtrains.register_replacement_lbms=false
+
+--[[TracksDefinition
+nodename_prefix
+texture_prefix
+description
+common={}
+straight={}
+straight45={}
+curve={}
+curve45={}
+lswitchst={}
+lswitchst45={}
+rswitchst={}
+rswitchst45={}
+lswitchcr={}
+lswitchcr45={}
+rswitchcr={}
+rswitchcr45={}
+vert1={
+ --you'll probably want to override mesh here
+}
+vert2={
+ --you'll probably want to override mesh here
+}
+]]--
+advtrains.all_tracktypes={}
+
+--definition preparation
+local function conns(c1, c2, r1, r2, rh, rots) return {conn1=c1, conn2=c2, rely1=r1, rely2=r2, railheight=rh} end
+
+local t_30deg={
+ regstep=1,
+ variant={
+ st=conns(0,8),
+ cr=conns(0,7),
+ swlst=conns(0,8),
+ swlcr=conns(0,7),
+ swrst=conns(0,8),
+ swrcr=conns(0,9),
+ vst1=conns(8,0,0,0.5,0.25),
+ vst2=conns(8,0,0.5,1,0.75),
+ vst31=conns(8,0,0,0.33,0.16),
+ vst32=conns(8,0,0.33,0.66,0.5),
+ vst33=conns(8,0,0.66,1,0.83),
+ },
+ description={
+ st="straight",
+ cr="curve",
+ swlst="left switch (straight)",
+ swlcr="left switch (curve)",
+ swrst="right switch (straight)",
+ swrcr="right switch (curve)",
+ vst1="steep uphill 1/2",
+ vst2="steep uphill 2/2",
+ vst31="uphill 1/3",
+ vst32="uphill 2/3",
+ vst33="uphill 3/3",
+ },
+ switch={
+ swlst="swlcr",
+ swlcr="swlst",
+ swrst="swrcr",
+ swrcr="swrst",
+ },
+ switchmc={
+ swlst="on",
+ swlcr="off",
+ swrst="on",
+ swrcr="off",
+ },
+ regtp=true,
+ trackplacer={
+ st=true,
+ cr=true,
+ },
+ tpsingle={
+ st=true,
+ },
+ tpdefault="st",
+ trackworker={
+ ["swrcr"]="st",
+ ["swrst"]="st",
+ ["st"]="cr",
+ ["cr"]="swlst",
+ ["swlcr"]="swrcr",
+ ["swlst"]="swrst",
+ },
+ regsp=true,
+ slopenodes={
+ vst1=true, vst2=true,
+ vst31=true, vst32=true, vst33=true,
+ },
+ slopeplacer={
+ [2]={"vst1", "vst2"},
+ [3]={"vst31", "vst32", "vst33"},
+ max=3,--highest entry
+ },
+ slopeplacer_45={
+ [2]={"vst1_45", "vst2_45"},
+ max=2,
+ },
+ rotation={"", "_30", "_45", "_60"},
+ increativeinv={},
+}
+local t_30deg_straightonly={
+ regstep=1,
+ variant={
+ st=conns(0,8),
+ },
+ description={
+ st="straight",
+ },
+ switch={
+ },
+ switchmc={
+ },
+ regtp=true,
+ trackplacer={
+ },
+ tpsingle={
+ },
+ tpdefault="st",
+ trackworker={
+ ["st"]="st",
+ },
+ slopenodes={},
+ rotation={"", "_30", "_45", "_60"},
+ increativeinv={st},
+}
+local t_30deg_straightonly_noplacer={
+ regstep=1,
+ variant={
+ st=conns(0,8),
+ },
+ description={
+ st="straight",
+ },
+ switch={
+ },
+ switchmc={
+ },
+ regtp=false,
+ trackplacer={
+ },
+ tpsingle={
+ },
+ tpdefault="st",
+ trackworker={
+ ["st"]="st",
+ },
+ slopenodes={},
+ rotation={"", "_30", "_45", "_60"},
+ increativeinv={st},
+}
+local t_45deg={
+ regstep=2,
+ variant={
+ st=conns(0,8),
+ cr=conns(0,6),
+ swlst=conns(0,8),
+ swlcr=conns(0,6),
+ swrst=conns(0,8),
+ swrcr=conns(0,10),
+ vst1=conns(8,0,0,0.5,0.25),
+ vst2=conns(8,0,0.5,1,0.75),
+ },
+ description={
+ st="straight",
+ cr="curve",
+ swlst="left switch (straight)",
+ swlcr="left switch (curve)",
+ swrst="right switch (straight)",
+ swrcr="right switch (curve)",
+ vst1="vertical lower node",
+ vst2="vertical upper node",
+ },
+ switch={
+ swlst="swlcr",
+ swlcr="swlst",
+ swrst="swrcr",
+ swrcr="swrst",
+ },
+ switchmc={
+ swlst="on",
+ swlcr="off",
+ swrst="on",
+ swrcr="off",
+ },
+ regtp=true,
+ trackplacer={
+ st=true,
+ cr=true,
+ },
+ tpsingle={
+ st=true,
+ },
+ tpdefault="st",
+ trackworker={
+ ["swrcr"]="st",
+ ["swrst"]="st",
+ ["st"]="cr",
+ ["cr"]="swlst",
+ ["swlcr"]="swrcr",
+ ["swlst"]="swrst",
+ },
+ slopenodes={},
+ rotation={"", "_45"},
+ increativeinv={vst1=true, vst2=true}
+}
+
+--definition format: ([] optional)
+--[[{
+ nodename_prefix
+ texture_prefix
+ [shared_texture]
+ models_prefix
+ models_suffix (with dot)
+ [shared_model]
+ formats={
+ st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2
+ (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all)
+ }
+ 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)
+ if advtrains.is_train_at_pos(pos) then return end
+ minetest.set_node(pos, {name=def.nodename_prefix.."_"..suffix_target, param2=node.param2})
+ advtrains.invalidate_all_paths()
+ advtrains.reset_trackdb_position(pos)
+ end
+ return switchfunc, {effector = {
+ ["action_"..mesecon_state] = switchfunc,
+ rules=advtrains.meseconrules
+ }}
+ end
+ local function make_overdef(suffix, rotation, conns, switchfunc, mesecontbl, in_creative_inv, drop_slope)
+ local img_suffix=suffix..rotation
+ return {
+ mesh = def.shared_model or (def.models_prefix.."_"..img_suffix..def.models_suffix),
+ tiles = {def.shared_texture or (def.texture_prefix.."_"..img_suffix..".png")},
+ --inventory_image = def.texture_prefix.."_"..img_suffix..".png",
+ --wield_image = def.texture_prefix.."_"..img_suffix..".png",
+ description=def.description.."("..preset.description[suffix]..rotation..")",
+ connect1=conns.conn1,
+ connect2=conns.conn2,
+ rely1=conns.rely1 or 0,
+ rely2=conns.rely2 or 0,
+ railheight=conns.railheight or 0,
+ on_rightclick=switchfunc,
+ groups = {
+ attached_node=1,
+ ["advtrains_track_"..tracktype]=1,
+ dig_immediate=2,
+ not_in_creative_inventory=(not in_creative_inv and 1 or nil),
+ not_blocking_trains=1,
+ },
+ mesecons=mesecontbl,
+ drop = increativeinv and def.nodename_prefix.."_"..suffix..rotation or (drop_slope and def.nodename_prefix.."_slopeplacer" or def.nodename_prefix.."_placer"),
+ }
+ end
+ local function cycle_conns(conns, rotid)
+ local add=(rotid-1)*preset.regstep
+ return {
+ conn1=(conns.conn1+add)%16,
+ conn2=(conns.conn2+add)%16,
+ rely1=conns.rely1 or 0,
+ rely2=conns.rely2 or 0,
+ railheight=conns.railheight or 0,
+ }
+ end
+ local common_def=advtrains.merge_tables({
+ description = def.description,
+ drawtype = "mesh",
+ paramtype="light",
+ paramtype2="facedir",
+ walkable = false,
+ selection_box = {
+ type = "fixed",
+ fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
+ },
+ rely1=0,
+ rely2=0,
+ railheight=0,
+ drop=def.nodename_prefix.."_placer",
+ can_dig=function(pos)
+ return not advtrains.is_train_at_pos(pos)
+ end,
+ after_dig_node=function(pos)
+ advtrains.invalidate_all_paths()
+ advtrains.reset_trackdb_position(pos)
+ end,
+ after_place_node=function(pos)
+ advtrains.reset_trackdb_position(pos)
+ end,
+ }, def.common or {})
+ --make trackplacer base def
+ advtrains.trackplacer.register_tracktype(def.nodename_prefix, preset.tpdefault)
+ if preset.regtp then
+ advtrains.trackplacer.register_track_placer(def.nodename_prefix, def.texture_prefix, def.description)
+ end
+ if preset.regsp then
+ advtrains.slope.register_placer(def, preset)
+ end
+ 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
+ if preset.switch[suffix] then
+ switchfunc, mesecontbl=make_switchfunc(preset.switch[suffix]..rotation, preset.switchmc[suffix])
+ end
+ local adef={}
+ if def.get_additional_definiton then
+ adef=def.get_additional_definiton(def, preset, suffix, rotation)
+ end
+
+ minetest.register_node(def.nodename_prefix.."_"..suffix..rotation, advtrains.merge_tables(
+ common_def,
+ make_overdef(
+ suffix, rotation,
+ cycle_conns(conns, rotid),
+ switchfunc, mesecontbl, preset.increativeinv[suffix], preset.slopenodes[suffix]
+ ),
+ adef
+ )
+ )
+ --trackplacer
+ if preset.regtp then
+ if preset.trackplacer[suffix] then
+ advtrains.trackplacer.add_double_conn(def.nodename_prefix, suffix, rotation, cycle_conns(conns, rotid))
+ end
+ if preset.tpsingle[suffix] then
+ advtrains.trackplacer.add_single_conn(def.nodename_prefix, suffix, rotation, cycle_conns(conns, rotid))
+ end
+ end
+ advtrains.trackplacer.add_worked(def.nodename_prefix, suffix, rotation, preset.trackworker[suffix])
+ end
+ end
+ end
+ table.insert(advtrains.all_tracktypes, tracktype)
+end
+
+
+function advtrains.is_track_and_drives_on(nodename, drives_on)
+ if not minetest.registered_nodes[nodename] then
+ return false
+ end
+ local nodedef=minetest.registered_nodes[nodename]
+ for k,v in ipairs(drives_on) do
+ if nodedef.groups["advtrains_track_"..v] then
+ return true
+ end
+ end
+ return false
+end
+
+function advtrains.get_track_connections(name, param2)
+ local nodedef=minetest.registered_nodes[name]
+ if not nodedef then print("[advtrains] get_track_connections couldn't find nodedef for nodename "..(name or "nil")) return 0, 8, 0, 0, 0 end
+ local noderot=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
+end
+
+--detector code
+--holds a table with nodes on which trains are on.
+
+advtrains.detector = {}
+advtrains.detector.on_node = {}
+advtrains.detector.on_node_restore = {}
+--set if paths were invalidated before. tells trainlogic.lua to call advtrains.detector.finalize_restore()
+advtrains.detector.clean_step_before = false
+
+--when train enters a node, call this
+--The entry already being contained in advtrains.detector.on_node_restore will not trigger an on_train_enter event on the node. (when path is reset, this is saved).
+function advtrains.detector.enter_node(pos, train_id)
+ local pts = minetest.pos_to_string(advtrains.round_vector_floor_y(pos))
+ --print("enterNode "..pts.." "..train_id)
+ if not advtrains.detector.on_node[pts] then
+ advtrains.detector.on_node[pts]=train_id
+ if advtrains.detector.on_node_restore[pts] then
+ advtrains.detector.on_node_restore[pts]=nil
+ else
+ advtrains.detector.call_enter_callback(advtrains.round_vector_floor_y(pos), train_id)
+ end
+ end
+end
+function advtrains.detector.leave_node(pos, train_id)
+ local pts = minetest.pos_to_string(advtrains.round_vector_floor_y(pos))
+ --print("leaveNode "..pts.." "..train_id)
+ if advtrains.detector.on_node[pts] then
+ advtrains.detector.call_leave_callback(advtrains.round_vector_floor_y(pos), train_id)
+ advtrains.detector.on_node[pts]=nil
+ end
+end
+--called immediately before invalidating paths
+function advtrains.detector.setup_restore()
+ --print("setup_restore")
+ advtrains.detector.on_node_restore = advtrains.detector.on_node
+ advtrains.detector.on_node = {}
+end
+--called one step after invalidating paths, when all trains have restored their path and called enter_node for their contents.
+function advtrains.detector.finalize_restore()
+ --print("finalize_restore")
+ for pts, train_id in pairs(advtrains.detector.on_node_restore) do
+ --print("called leave callback "..pts.." "..train_id)
+ advtrains.detector.call_leave_callback(minetest.string_to_pos(pts), train_id)
+ end
+ advtrains.detector.on_node_restore = {}
+end
+function advtrains.detector.call_enter_callback(pos, train_id)
+ --print("instructed to call enter calback")
+
+ local node = minetest.get_node(pos) --this spares the check if node is nil, it has a name in any case
+ local mregnode=minetest.registered_nodes[node.name]
+ if mregnode and mregnode.advtrains and mregnode.advtrains.on_train_enter then
+ mregnode.advtrains.on_train_enter(pos, train_id)
+ end
+end
+function advtrains.detector.call_leave_callback(pos, train_id)
+ --print("instructed to call leave calback")
+
+ local node = minetest.get_node(pos) --this spares the check if node is nil, it has a name in any case
+ local mregnode=minetest.registered_nodes[node.name]
+ if mregnode and mregnode.advtrains and mregnode.advtrains.on_train_leave then
+ mregnode.advtrains.on_train_leave(pos, train_id)
+ end
+end
+
+-- slope placer. Defined in register_tracks.
+--crafted with rail and gravel
+local sl={}
+function sl.register_placer(def, preset)
+ minetest.register_craftitem(def.nodename_prefix.."_slopeplacer",{
+ description = def.description.." Slope",
+ inventory_image = def.texture_prefix.."_slopeplacer.png",
+ wield_image = def.texture_prefix.."_slopeplacer.png",
+ groups={},
+ on_place = sl.create_slopeplacer_on_place(def, preset)
+ })
+end
+--(itemstack, placer, pointed_thing)
+function sl.create_slopeplacer_on_place(def, preset)
+ return function(istack, player, pt)
+ if not pt.type=="node" then
+ minetest.chat_send_player(player:get_player_name(), "Can't place: not pointing at node")
+ return istack
+ end
+ local pos=pt.above
+ if not pos then
+ minetest.chat_send_player(player:get_player_name(), "Can't place: not pointing at node")
+ return istack
+ end
+ local node=minetest.get_node(pos)
+ if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to then
+ minetest.chat_send_player(player:get_player_name(), "Can't place: space occupied!")
+ return istack
+ end
+ if minetest.is_protected(pos, player:get_player_name()) then
+ minetest.chat_send_player(player:get_player_name(), "Can't place: protected position!")
+ return istack
+ end
+ --determine player orientation (only horizontal component)
+ --get_look_horizontal may not be available
+ local yaw=player.get_look_horizontal and player:get_look_horizontal() or (player:get_look_yaw() - math.pi/2)
+
+ --rounding unit vectors is a nice way for selecting 1 of 8 directions since sin(30°) is 0.5.
+ dirvec={x=math.floor(math.sin(-yaw)+0.5), y=0, z=math.floor(math.cos(-yaw)+0.5)}
+ --translate to direction to look up inside the preset table
+ local param2, rot45=({
+ [-1]={
+ [-1]=2,
+ [0]=3,
+ [1]=3,
+ },
+ [0]={
+ [-1]=2,
+ [1]=0,
+ },
+ [1]={
+ [-1]=1,
+ [0]=1,
+ [1]=0,
+ },
+ })[dirvec.x][dirvec.z], dirvec.x~=0 and dirvec.z~=0
+ local lookup=preset.slopeplacer
+ if rot45 then lookup=preset.slopeplacer_45 end
+
+ --go unitvector forward and look how far the next node is
+ local step=1
+ while step<=lookup.max do
+ local node=minetest.get_node(vector.add(pos, dirvec))
+ --next node solid?
+ if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to or minetest.is_protected(pos, player:get_player_name()) then
+ --do slopes of this distance exist?
+ if lookup[step] then
+ if minetest.setting_getbool("creative_mode") or istack:get_count()>=step then
+ --start placing
+ local placenodes=lookup[step]
+ while step>0 do
+ minetest.set_node(pos, {name=def.nodename_prefix.."_"..placenodes[step], param2=param2})
+ if not minetest.setting_getbool("creative_mode") then
+ istack:take_item()
+ end
+ step=step-1
+ pos=vector.subtract(pos, dirvec)
+ end
+ else
+ minetest.chat_send_player(player:get_player_name(), "Can't place: Not enough slope items left ("..step.." required)")
+ end
+ else
+ minetest.chat_send_player(player:get_player_name(), "Can't place: There's no slope of length "..step)
+ end
+ return istack
+ end
+ step=step+1
+ pos=vector.add(pos, dirvec)
+ end
+ minetest.chat_send_player(player:get_player_name(), "Can't place: no supporting node at upper end.")
+ return itemstack
+ end
+end
+
+advtrains.slope=sl
+
+--END code, BEGIN definition
+--definition format: ([] optional)
+--[[{
+ nodename_prefix
+ texture_prefix
+ [shared_texture]
+ models_prefix
+ models_suffix (with dot)
+ [shared_model]
+ formats={
+ st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2
+ (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all)
+ }
+ common={} change something on common rail appearance
+}]]
+
+advtrains.register_tracks("regular", {
+ nodename_prefix="advtrains:track",
+ texture_prefix="advtrains_track",
+ shared_model="trackplane.b3d",
+ description="Deprecated Track",
+ formats={vst1={}, vst2={}},
+}, t_45deg)
+
+
+advtrains.register_tracks("default", {
+ nodename_prefix="advtrains:dtrack",
+ texture_prefix="advtrains_dtrack",
+ models_prefix="advtrains_dtrack",
+ models_suffix=".b3d",
+ shared_texture="advtrains_dtrack_rail.png",
+ description="Track",
+ formats={vst1={true, false, true}, vst2={true, false, true}, vst31={true}, vst32={true}, vst33={true}},
+}, t_30deg)
+
+--bumpers
+advtrains.register_tracks("default", {
+ nodename_prefix="advtrains:dtrack_bumper",
+ texture_prefix="advtrains_dtrack_bumper",
+ models_prefix="advtrains_dtrack_bumper",
+ models_suffix=".b3d",
+ shared_texture="advtrains_dtrack_rail.png",
+ description="Bumper",
+ formats={},
+}, t_30deg_straightonly)
+--legacy bumpers
+for _,rot in ipairs({"", "_30", "_45", "_60"}) do
+ minetest.register_alias("advtrains:dtrack_bumper"..rot, "advtrains:dtrack_bumper_st"..rot)
+end
+
+if mesecon then
+ advtrains.register_tracks("default", {
+ nodename_prefix="advtrains:dtrack_detector_off",
+ texture_prefix="advtrains_dtrack_detector",
+ models_prefix="advtrains_dtrack_detector",
+ models_suffix=".b3d",
+ shared_texture="advtrains_dtrack_rail.png",
+ description="Detector Rail",
+ formats={},
+ get_additional_definiton = function(def, preset, suffix, rotation)
+ return {
+ mesecons = {
+ receptor = {
+ state = mesecon.state.off,
+ rules = advtrains.meseconrules
+ }
+ },
+ advtrains = {
+ on_train_enter=function(pos, train_id)
+ minetest.swap_node(pos, {name="advtrains:dtrack_detector_on".."_"..suffix..rotation, param2=minetest.get_node(pos).param2})
+ mesecon.receptor_on(pos, advtrains.meseconrules)
+ end
+ }
+ }
+ end
+ }, t_30deg_straightonly)
+ advtrains.register_tracks("default", {
+ nodename_prefix="advtrains:dtrack_detector_on",
+ texture_prefix="advtrains_dtrack_detector",
+ models_prefix="advtrains_dtrack_detector",
+ models_suffix=".b3d",
+ shared_texture="advtrains_dtrack_rail_detector_on.png",
+ description="Detector(on)(you hacker you)",
+ formats={},
+ get_additional_definiton = function(def, preset, suffix, rotation)
+ return {
+ mesecons = {
+ receptor = {
+ state = mesecon.state.on,
+ rules = advtrains.meseconrules
+ }
+ },
+ advtrains = {
+ on_train_leave=function(pos, train_id)
+ minetest.swap_node(pos, {name="advtrains:dtrack_detector_off".."_"..suffix..rotation, param2=minetest.get_node(pos).param2})
+ mesecon.receptor_off(pos, advtrains.meseconrules)
+ end
+ }
+ }
+ end
+ }, t_30deg_straightonly_noplacer)
+end
+--TODO legacy
+--I know lbms are better for this purpose
+for name,rep in pairs({swl_st="swlst", swr_st="swrst", swl_cr="swlcr", swr_cr="swrcr", }) do
+ minetest.register_abm({
+ -- In the following two fields, also group:groupname will work.
+ nodenames = {"advtrains:track_"..name},
+ interval = 1.0, -- Operation interval in seconds
+ chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this
+ action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep, param2=node.param2}) end,
+ })
+ minetest.register_abm({
+ -- In the following two fields, also group:groupname will work.
+ nodenames = {"advtrains:track_"..name.."_45"},
+ interval = 1.0, -- Operation interval in seconds
+ chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this
+ action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep.."_45", param2=node.param2}) end,
+ })
+end
+
+if advtrains.register_replacement_lbms then
+minetest.register_lbm({
+ name = "advtrains:ramp_replacement_1",
+-- In the following two fields, also group:groupname will work.
+ nodenames = {"advtrains:track_vert1"},
+ action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst1", param2=(node.param2+2)%4}) end,
+})
+minetest.register_lbm({
+ name = "advtrains:ramp_replacement_1",
+-- -- In the following two fields, also group:groupname will work.
+ nodenames = {"advtrains:track_vert2"},
+ action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst2", param2=(node.param2+2)%4}) end,
+})
+ minetest.register_abm({
+ name = "advtrains:st_rep_1",
+ -- In the following two fields, also group:groupname will work.
+ nodenames = {"advtrains:track_st"},
+ interval=1,
+ chance=1,
+ action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st", param2=node.param2}) end,
+ })
+ minetest.register_lbm({
+ name = "advtrains:st_rep_1",
+ -- -- In the following two fields, also group:groupname will work.
+ nodenames = {"advtrains:track_st_45"},
+ action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st_45", param2=node.param2}) end,
+ })
+end
+
+
+
+
+
+
+
+
diff --git a/advtrains/trainhud.lua b/advtrains/trainhud.lua new file mode 100644 index 0000000..aead246 --- /dev/null +++ b/advtrains/trainhud.lua @@ -0,0 +1,109 @@ +--trainhud.lua: holds all the code for train controlling + +advtrains.hud = {} + +minetest.register_on_leaveplayer(function(player) +advtrains.hud[player:get_player_name()] = nil +end) + +local mletter={[1]="F", [-1]="R", [0]="N"} + +function advtrains.on_control_change(pc, train, flip) + if pc.sneak then + if pc.up then + train.tarvelocity = advtrains.all_traintypes[train.traintype].max_speed or 10 + end + if pc.down then + train.tarvelocity = 0 + end + if pc.left then + train.tarvelocity = 4 + end + if pc.right then + train.tarvelocity = 8 + end + if pc.jump then + train.brake = true + --0: released, 1: brake and pressed, 2: released and brake, 3: pressed and brake + if not train.brake_hold_state or train.brake_hold_state==0 then + train.brake_hold_state = 1 + elseif train.brake_hold_state==2 then + train.brake_hold_state = 3 + end + elseif train.brake_hold_state==1 then + train.brake_hold_state = 2 + elseif train.brake_hold_state==3 then + train.brake = false + train.brake_hold_state = 0 + end + --shift+use:see wagons.lua + else + if pc.up then + train.tarvelocity = train.tarvelocity + 1 + end + if pc.down then + if train.velocity>0 then + train.tarvelocity = math.max(train.tarvelocity - 1, 0) + else + train.movedir = -train.movedir + end + end + if train.brake_hold_state~=2 then + train.brake = false + end + if pc.jump then + train.brake = true + end + if pc.aux1 then + --horn + end + end +end +function advtrains.update_driver_hud(pname, train, flip) + advtrains.set_trainhud(pname, advtrains.hud_train_format(train, flip)) +end +function advtrains.clear_driver_hud(pname) + advtrains.set_trainhud(pname, "") +end + +function advtrains.set_trainhud(name, text) + local hud = advtrains.hud[name] + local player=minetest.get_player_by_name(name) + if not player then + return + end + if not hud then + hud = {} + advtrains.hud[name] = hud + hud.id = player:hud_add({ + hud_elem_type = "text", + name = "ADVTRAINS", + number = 0xFFFFFF, + position = {x=0.5, y=0.7}, + offset = {x=0, y=0}, + text = text, + scale = {x=200, y=60}, + alignment = {x=0, y=0}, + }) + hud.oldText=text + return + elseif hud.oldText ~= text then + player:hud_change(hud.id, "text", text) + hud.oldText=text + end +end +function advtrains.hud_train_format(train, flip) + local fct=flip and -1 or 1 + if not train or not train.traintype then return "" end + + local max=advtrains.all_traintypes[train.traintype].max_speed or 10 + local vel=advtrains.abs_ceil(train.velocity) + local tvel=advtrains.abs_ceil(train.tarvelocity) + local topLine, firstLine, secondLine + + topLine=train.traintype.." ["..mletter[fct*train.movedir].."] "..(train.brake and "="..( train.brake_hold_state==2 and "^" or "" ).."B=" or "") + firstLine="Speed: |"..string.rep("+", vel)..string.rep("_", max-vel)..">" + secondLine="Target: |"..string.rep("+", tvel)..string.rep("_", max-tvel)..">" + + return topLine.."\n"..firstLine.."\n"..secondLine +end diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua new file mode 100644 index 0000000..5e3936f --- /dev/null +++ b/advtrains/trainlogic.lua @@ -0,0 +1,895 @@ +--trainlogic.lua +--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 print=function() end + +local benchmark=false +--printbm=function(str, t) print("[advtrains]"..str.." "..((os.clock()-t)*1000).."ms") end +local bm={} +local bmlt=0 +local bmsteps=0 +local bmstepint=200 +printbm=function(action, ta) + if not benchmark then return end + local t=(os.clock()-ta)*1000 + if not bm[action] then + bm[action]=t + else + bm[action]=bm[action]+t + end + bmlt=bmlt+t +end +function endstep() + if not benchmark then return end + bmsteps=bmsteps-1 + if bmsteps<=0 then + bmsteps=bmstepint + for key, value in pairs(bm) do + minetest.chat_send_all(key.." "..(value/bmstepint).." ms avg.") + end + minetest.chat_send_all("Total time consumed by all advtrains actions per step: "..(bmlt/bmstepint).." ms avg.") + bm={} + bmlt=0 + end +end + +--TODO: these values need to be integrated when i remove traintypes. +advtrains.train_accel_force=2--per second and divided by number of wagons +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.all_traintypes={} +function advtrains.register_train_type(name, drives_on, max_speed) + advtrains.all_traintypes[name]={} + advtrains.all_traintypes[name].drives_on=drives_on + advtrains.all_traintypes[name].max_speed=max_speed or 10 +end + + +advtrains.trains={} +advtrains.wagon_save={} + +--load initially +advtrains.fpath=minetest.get_worldpath().."/advtrains" +local file, err = io.open(advtrains.fpath, "r") +if not file then + local er=err or "Unknown Error" + print("[advtrains]Failed loading advtrains save file "..er) +else + local tbl = minetest.deserialize(file:read("*a")) + if type(tbl) == "table" then + advtrains.trains=tbl + end + file:close() +end +advtrains.fpath_ws=minetest.get_worldpath().."/advtrains_wagon_save" +local file, err = io.open(advtrains.fpath_ws, "r") +if not file then + local er=err or "Unknown Error" + print("[advtrains]Failed loading advtrains save file "..er) +else + local tbl = minetest.deserialize(file:read("*a")) + if type(tbl) == "table" then + advtrains.wagon_save=tbl + end + file:close() +end + + +advtrains.save = function() + --print("[advtrains]saving") + advtrains.invalidate_all_paths() + local datastr = minetest.serialize(advtrains.trains) + if not datastr then + minetest.log("error", "[advtrains] Failed to serialize train data!") + return + end + local file, err = io.open(advtrains.fpath, "w") + if err then + return err + end + file:write(datastr) + file:close() + + -- update wagon saves + for _,wagon in pairs(minetest.luaentities) do + if wagon.is_wagon and wagon.initialized then + wagon:get_staticdata() + end + end + --cross out userdata + for w_id, data in pairs(advtrains.wagon_save) do + data.name=nil + data.object=nil + if data.driver then + data.driver_name=data.driver:get_player_name() + data.driver=nil + else + data.driver_name=nil + end + if data.discouple then + data.discouple.object:remove() + data.discouple=nil + end + end + --print(dump(advtrains.wagon_save)) + datastr = minetest.serialize(advtrains.wagon_save) + if not datastr then + minetest.log("error", "[advtrains] Failed to serialize train data!") + return + end + file, err = io.open(advtrains.fpath_ws, "w") + if err then + return err + end + file:write(datastr) + file:close() + + advtrains.save_trackdb() +end +minetest.register_on_shutdown(advtrains.save) + +advtrains.save_and_audit_timer=advtrains.audit_interval +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 + printbm("saving", t) + end + --regular train step + local t=os.clock() + for k,v in pairs(advtrains.trains) do + advtrains.train_step(k, v, dtime) + end + + --see tracks.lua + if advtrains.detector.clean_step_before then + advtrains.detector.finalize_restore() + end + + printbm("trainsteps", t) + endstep() +end) + +function advtrains.train_step(id, train, dtime) + + --TODO check for all vars to be present + if not train.velocity then + train.velocity=0 + end + if not train.movedir or (train.movedir~=1 and train.movedir~=-1) then + train.movedir=1 + end + --very unimportant thing: check if couple is here + if train.couple_eid_front and (not minetest.luaentities[train.couple_eid_front] or not minetest.luaentities[train.couple_eid_front].is_couple) then train.couple_eid_front=nil end + if train.couple_eid_back and (not minetest.luaentities[train.couple_eid_back] or not minetest.luaentities[train.couple_eid_back].is_couple) then train.couple_eid_back=nil end + + --skip certain things (esp. collision) when not moving + local train_moves=(train.velocity~=0) + + --if not train.last_pos then advtrains.trains[id]=nil return end + + if not advtrains.pathpredict(id, train) then + print("pathpredict failed(returned false)") + train.velocity=0 + train.tarvelocity=0 + return + end + + local path=advtrains.get_or_create_path(id, train) + if not path then + train.velocity=0 + train.tarvelocity=0 + print("train has no path for whatever reason") + return + end + + local train_end_index=advtrains.get_train_end_index(train) + --apply off-track handling: + local front_off_track=train.max_index_on_track and train.index>train.max_index_on_track + local back_off_track=train.min_index_on_track and train_end_index<train.min_index_on_track + if front_off_track and back_off_track then--allow movement in both directions + if train.tarvelocity>1 then train.tarvelocity=1 end + elseif front_off_track then--allow movement only backward + if train.movedir==1 and train.tarvelocity>0 then train.tarvelocity=0 end + if train.movedir==-1 and train.tarvelocity>1 then train.tarvelocity=1 end + elseif back_off_track then--allow movement only forward + if train.movedir==-1 and train.tarvelocity>0 then train.tarvelocity=0 end + if train.movedir==1 and train.tarvelocity>1 then train.tarvelocity=1 end + end + + --update advtrains.detector + if not train.detector_old_index then + train.detector_old_index = math.floor(train_end_index) + train.detector_old_end_index = math.floor(train_end_index) + end + local ifo, ifn, ibo, ibn = train.detector_old_index, math.floor(train.index), train.detector_old_end_index, math.floor(train_end_index) + if ifn>ifo then + for i=ifo, ifn do + if path[i] then + advtrains.detector.enter_node(path[i], id) + end + end + elseif ifn<ifo then + for i=ifn, ifo do + if path[i] then + advtrains.detector.leave_node(path[i], id) + end + end + end + if ibn<ibo then + for i=ibn, ibn do + if path[i] then + advtrains.detector.enter_node(path[i], id) + end + end + elseif ibn>ibo then + for i=ibo, ibn do + if path[i] then + advtrains.detector.leave_node(path[i], id) + end + end + end + train.detector_old_index = math.floor(train.index) + train.detector_old_end_index = math.floor(train_end_index) + + 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 + local coll_grace=1 + if train.movedir==1 then + collpos=advtrains.get_real_index_position(path, train.index-coll_grace) + else + collpos=advtrains.get_real_index_position(path, train_end_index+coll_grace) + end + if collpos then + 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})) + if advtrains.detector.on_node[testpts] and advtrains.detector.on_node[testpts]~=id then + --collides + 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 + 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))) + + local should_check=false + for _,p in ipairs(minetest.get_connected_players()) do + should_check=should_check or ((vector.distance(ori_pos, p:getpos())<node_range)) + end + if should_check then + --it is better to iterate luaentites only once + --print("check_trainpartload") + local found_uids={} + for _,wagon in pairs(minetest.luaentities) do + if wagon.is_wagon and wagon.initialized and wagon.train_id==id then + if found_uids[wagon.unique_id] then + --duplicate found, delete it + if wagon.object then wagon.object:remove() end + else + found_uids[wagon.unique_id]=true + end + end + end + --print("found_uids: "..dump(found_uids)) + --now iterate trainparts and check. then cross them out to see if there are wagons over for any reason + for pit, w_id in ipairs(train.trainparts) do + if found_uids[w_id] then + --print(w_id.." still loaded") + elseif advtrains.wagon_save[w_id] then + --print(w_id.." not loaded, but save available") + --spawn a new and initialize it with the properties from wagon_save + local le=minetest.env:add_entity(ori_pos, advtrains.wagon_save[w_id].entity_name):get_luaentity() + le:init_from_wagon_save(w_id) + else + print(w_id.." not loaded and no save available") + --what the hell... + table.remove(train.trainparts, pit) + end + end + end + train.check_trainpartload=2 + end + + + --handle collided_with_env + if train.recently_collided_with_env then + train.tarvelocity=0 + if not train_moves then + train.recently_collided_with_env=false--reset status when stopped + end + end + if train.locomotives_in_train==0 then + train.tarvelocity=0 + end + --make brake adjust the tarvelocity if necessary + if train.brake and (math.ceil(train.velocity)-1)<train.tarvelocity then + train.tarvelocity=math.max((math.ceil(train.velocity)-1), 0) + end + --apply tarvel(but with physics in mind!) + if train.velocity~=train.tarvelocity then + local applydiff=0 + local mass=#train.trainparts + local diff=train.tarvelocity-train.velocity + if diff>0 then--accelerating, force will be brought on only by locomotives. + --print("accelerating with default force") + applydiff=(math.min((advtrains.train_accel_force*train.locomotives_in_train*dtime)/mass, math.abs(diff))) + else--decelerating + if front_off_track or back_off_track or train.recently_collided_with_env then --every wagon has a brake, so not divided by mass. + --print("braking with emergency force") + applydiff= -(math.min((advtrains.train_emerg_force*dtime), math.abs(diff))) + elseif train.brake then + --print("braking with default force") + --no math.min, because it can grow beyond tarvelocity, see up there + --dont worry, it will never fall below zero. + applydiff= -((advtrains.train_brake_force*dtime)) + else + --print("roll") + applydiff= -(math.min((advtrains.train_roll_force*dtime), math.abs(diff))) + end + end + train.last_accel=(applydiff*train.movedir) + train.velocity=math.min(math.max( train.velocity+applydiff , 0), advtrains.all_traintypes[train.traintype].max_speed) + else + train.last_accel=0 + end + + --move + --TODO 3,5 + 0.7 + train.index=train.index and train.index+(((train.velocity*train.movedir)/(train.path_dist[math.floor(train.index)] or 1))*dtime) or 0 + +end + + +--structure of train table: +--[[ +trains={ + [train_id]={ + trainparts={ + [n]=wagon_id + } + path={path} + velocity + tarvelocity + index + trainlen + path_inv_level + last_pos | + last_dir | for pathpredicting. + } +} +--a wagon itself has the following properties: +wagon={ + unique_id + train_id + pos_in_train (is index difference, including train_span stuff) + pos_in_trainparts (is index in trainparts tabel of trains) +} +inherited by metatable: +wagon_proto={ + wagon_span +} +]] + +--returns new id +function advtrains.create_new_train_at(pos, pos_prev, traintype) + local newtrain_id=os.time()..os.clock() + while advtrains.trains[newtrain_id] do newtrain_id=os.time()..os.clock() end--ensure uniqueness(will be unneccessary) + + advtrains.trains[newtrain_id]={} + advtrains.trains[newtrain_id].last_pos=pos + advtrains.trains[newtrain_id].last_pos_prev=pos_prev + advtrains.trains[newtrain_id].traintype=traintype + advtrains.trains[newtrain_id].tarvelocity=0 + advtrains.trains[newtrain_id].velocity=0 + advtrains.trains[newtrain_id].trainparts={} + return newtrain_id +end + +--returns false on failure. handle this case! +function advtrains.pathpredict(id, train) + + --print("pos ",x,y,z) + --::rerun:: + if not train.index then train.index=0 end + if not train.path or #train.path<2 then + if not train.last_pos then + --no chance to recover + print("[advtrains]train hasn't saved last-pos, removing train.") + advtrains.train[id]=nil + return false + end + + local node_ok=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(train.last_pos), train.traintype) + + if node_ok==nil then + --block not loaded, do nothing + return nil + elseif node_ok==false then + print("[advtrains]no track here, (fail) removing train.") + advtrains.trains[id]=nil + return false + end + + if not train.last_pos_prev then + --no chance to recover + print("[advtrains]train hasn't saved last-pos_prev, removing train.") + advtrains.trains[id]=nil + return false + end + + local prevnode_ok=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(train.last_pos_prev), train.traintype) + + if prevnode_ok==nil then + --block not loaded, do nothing + return nil + elseif prevnode_ok==false then + print("[advtrains]no track at prev, (fail) removing train.") + advtrains.trains[id]=nil + return false + end + + train.index=(train.restore_add_index or 0)+(train.savedpos_off_track_index_offset or 0) + --restore_add_index is set by save() to prevent trains hopping to next round index. should be between -0.5 and 0.5 + --savedpos_off_track_index_offset is set if train went off track. see below. + train.path={} + train.path_dist={} + train.path[0]=train.last_pos + train.path[-1]=train.last_pos_prev + train.path_dist[-1]=vector.distance(train.last_pos, train.last_pos_prev) + end + + local pregen_front=2 + local pregen_back=2 + if train.velocity>0 then + if train.movedir>0 then + pregen_front=2+math.ceil(train.velocity*0.15) --assumes server step of 0.1 seconds, +50% tolerance + else + pregen_back=2+math.ceil(train.velocity*0.15) + end + end + + + local maxn=advtrains.maxN(train.path) + 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.traintype) + if conway then + train.path[maxn+1]=conway + train.max_index_on_track=maxn + 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) + 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) + 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.traintype) + if conway then + train.path[minn-1]=conway + train.min_index_on_track=minn + 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) + 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]) + minn=advtrains.minN(train.path) + end + if not train.min_index_on_track then train.min_index_on_track=0 end + if not train.max_index_on_track then train.max_index_on_track=0 end + + --make pos/yaw available for possible recover calls + if train.max_index_on_track<train.index then --whoops, train went too far. the saved position will be the last one that lies on a track, and savedpos_off_track_index_offset will hold how far to go from here + 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)) + 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)) + 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)] + end + return train.path +end +function advtrains.get_train_end_index(train) + return advtrains.get_real_path_index(train, train.trainlen or 2)--this function can be found inside wagons.lua since it's more related to wagons. we just set trainlen as pos_in_train +end + +function advtrains.get_or_create_path(id, train) + if not train.path then return advtrains.pathpredict(id, train) end + return train.path +end + +function advtrains.add_wagon_to_train(wagon, train_id, index) + local train=advtrains.trains[train_id] + if index then + table.insert(train.trainparts, index, wagon.unique_id) + else + table.insert(train.trainparts, wagon.unique_id) + end + --this is not the usual case!!! + --we may set initialized because the wagon has no chance to step() + wagon.initialized=true + --TODO is this art or can we throw it away? + advtrains.update_trainpart_properties(train_id) +end +function advtrains.update_trainpart_properties(train_id, invert_flipstate) + local train=advtrains.trains[train_id] + local rel_pos=0 + local count_l=0 + for i, w_id in ipairs(train.trainparts) do + local any_loaded=false + for _,wagon in pairs(minetest.luaentities) do + if wagon.is_wagon and wagon.initialized and wagon.unique_id==w_id then + rel_pos=rel_pos+wagon.wagon_span + wagon.train_id=train_id + wagon.pos_in_train=rel_pos + wagon.pos_in_trainparts=i + wagon.old_velocity_vector=nil + if wagon.is_locomotive then + count_l=count_l+1 + end + if invert_flipstate then + wagon.wagon_flipped = not wagon.wagon_flipped + end + rel_pos=rel_pos+wagon.wagon_span + any_loaded=true + end + end + if not any_loaded then + print("update_trainpart_properties wagon "..w_id.." not loaded, ignoring it.") + end + end + train.trainlen=rel_pos + train.locomotives_in_train=count_l +end + +function advtrains.split_train_at_wagon(wagon) + --get train + local train=advtrains.trains[wagon.train_id] + local real_pos_in_train=advtrains.get_real_path_index(train, wagon.pos_in_train) + local pos_for_new_train=advtrains.get_or_create_path(wagon.train_id, train)[math.floor(real_pos_in_train+wagon.wagon_span)] + local pos_for_new_train_prev=advtrains.get_or_create_path(wagon.train_id, train)[math.floor(real_pos_in_train-1+wagon.wagon_span)] + + --before doing anything, check if both are rails. else do not allow + if not pos_for_new_train then + print("split_train: pos_for_new_train not set") + return false + end + local node_ok=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(pos_for_new_train), train.traintype) + if not node_ok then + print("split_train: pos_for_new_train "..minetest.pos_to_string(advtrains.round_vector_floor_y(pos_for_new_train_prev)).." not loaded or is not a rail") + return false + end + + if not train.last_pos_prev then + print("split_train: pos_for_new_train_prev not set") + return false + end + + local prevnode_ok=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(pos_for_new_train), train.traintype) + if not prevnode_ok then + print("split_train: pos_for_new_train_prev "..minetest.pos_to_string(advtrains.round_vector_floor_y(pos_for_new_train_prev)).." not loaded or is not a rail") + return false + end + + --create subtrain + local newtrain_id=advtrains.create_new_train_at(pos_for_new_train, pos_for_new_train_prev, train.traintype) + local newtrain=advtrains.trains[newtrain_id] + --insert all wagons to new train + for k,v in ipairs(train.trainparts) do + if k>=wagon.pos_in_trainparts then + table.insert(newtrain.trainparts, v) + train.trainparts[k]=nil + end + end + --update train parts + advtrains.update_trainpart_properties(wagon.train_id)--atm it still is the desierd id. + advtrains.update_trainpart_properties(newtrain_id) + train.tarvelocity=0 + newtrain.velocity=train.velocity + newtrain.tarvelocity=0 +end + +--there are 4 cases: +--1/2. F<->R F<->R regular, put second train behind first +--->frontpos of first train will match backpos of second +--3. F<->R R<->F flip one of these trains, take the other as new train +--->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)<couple_spawnradius then + advtrains.spawn_couple_if_neccessary(backpos1, frontpos2, id1, id2, true, false) + --case 2 (second train is front) + elseif vector.distance(frontpos1, backpos2)<couple_spawnradius then + advtrains.spawn_couple_if_neccessary(backpos2, frontpos1, id2, id1, true, false) + --case 3 + elseif vector.distance(backpos2, backpos1)<couple_spawnradius then + advtrains.spawn_couple_if_neccessary(backpos1, backpos2, id1, id2, true, true) + --case 4 + elseif vector.distance(frontpos2, frontpos1)<couple_spawnradius then + advtrains.spawn_couple_if_neccessary(frontpos1, frontpos2, id1, id2, false, false) + end + end +end +--true when trains are facing each other. needed on colliding. +-- check done by iterating paths and checking their direction +--returns nil when not on the same track at all OR when required path items are not generated. this distinction may not always be needed. +function advtrains.trains_facing(train1, train2) + local sr_pos=train1.path[math.floor(train1.index)] + local sr_pos_p=train1.path[math.floor(train1.index)-1] + + for i=advtrains.minN(train2.path), advtrains.maxN(train2.path) do + if vector.equals(sr_pos, train2.path[i]) then + if train2.path[i+1] and vector.equals(sr_pos_p, train2.path[i+1]) then return true end + if train2.path[i-1] and vector.equals(sr_pos_p, train2.path[i-1]) then return false end + return nil + end + end + return nil +end + +--order of trains may be irrelevant in some cases. check opposite cases. TODO does this work? +--pos1 and pos2 are just needed to form a median. +function advtrains.spawn_couple_if_neccessary(pos1, pos2, tid1, tid2, train1_is_backpos, train2_is_backpos) + --print("spawn_couple_if_neccessary..."..dump({pos1=pos1, pos2=pos2, train1_is_backpos=train1_is_backpos, train2_is_backpos=train2_is_backpos})) + local train1=advtrains.trains[tid1] + local train2=advtrains.trains[tid2] + local t1_has_couple + if train1_is_backpos then + t1_has_couple=train1.couple_eid_back + else + t1_has_couple=train1.couple_eid_front + end + local t2_has_couple + if train2_is_backpos then + t2_has_couple=train2.couple_eid_back + else + t2_has_couple=train2.couple_eid_front + end + + if t1_has_couple and t2_has_couple then + if t1_has_couple~=t2_has_couple then--what the hell + if minetest.object_refs[t2_has_couple] then minetest.object_refs[t2_has_couple]:remove() end + if train2_is_backpos then + train2.couple_eid_back=t1_has_couple + else + train2.couple_eid_front=t1_has_couple + end + end + --[[elseif t1_has_couple and not t2_has_couple then + if train2_is_backpos then + train2.couple_eid_back=t1_has_couple + else + train2.couple_eid_front=t1_has_couple + end + elseif not t1_has_couple and t2_has_couple then + if train1_is_backpos then + train1.couple_eid_back=t2_has_couple + else + train1.couple_eid_front=t2_has_couple + end]] + else + local pos=advtrains.pos_median(pos1, pos2) + local obj=minetest.add_entity(pos, "advtrains:couple") + if not obj then print("failed creating object") return end + local le=obj:get_luaentity() + le.train_id_1=tid1 + le.train_id_2=tid2 + le.train1_is_backpos=train1_is_backpos + le.train2_is_backpos=train2_is_backpos + --find in object_refs + for aoi, compare in pairs(minetest.object_refs) do + if compare==obj then + if train1_is_backpos then + train1.couple_eid_back=aoi + else + train1.couple_eid_front=aoi + end + if train2_is_backpos then + train2.couple_eid_back=aoi + else + train2.couple_eid_front=aoi + end + end + end + end +end + +function advtrains.do_connect_trains(first_id, second_id) + local first_wagoncnt=#advtrains.trains[first_id].trainparts + local second_wagoncnt=#advtrains.trains[second_id].trainparts + + for _,v in ipairs(advtrains.trains[second_id].trainparts) do + table.insert(advtrains.trains[first_id].trainparts, v) + end + --kick it like physics (with mass being #wagons) + local new_velocity=((advtrains.trains[first_id].velocity*first_wagoncnt)+(advtrains.trains[second_id].velocity*second_wagoncnt))/(first_wagoncnt+second_wagoncnt) + advtrains.trains[second_id]=nil + advtrains.update_trainpart_properties(first_id) + advtrains.trains[first_id].velocity=new_velocity + advtrains.trains[first_id].tarvelocity=0 +end + +function advtrains.invert_train(train_id) + local train=advtrains.trains[train_id] + + local old_path=advtrains.get_or_create_path(train_id, train) + train.path={} + train.index= - advtrains.get_train_end_index(train) + train.velocity=-train.velocity + train.tarvelocity=-train.tarvelocity + for k,v in pairs(old_path) do + train.path[-k]=v + end + local old_trainparts=train.trainparts + train.trainparts={} + for k,v in ipairs(old_trainparts) do + table.insert(train.trainparts, 1, v)--notice insertion at first place + end + advtrains.update_trainpart_properties(train_id, true) +end + +function advtrains.is_train_at_pos(pos) + --print("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 + --print("istrainat: checking "..le.train_id) + checked_trains[le.train_id]=true + local path=advtrains.get_or_create_path(le.train_id, le:train()) + if path then + --print("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 + --print("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 +end +function advtrains.invalidate_all_paths() + --print("invalidating all paths") + for k,v in pairs(advtrains.trains) do + if v.index then + v.restore_add_index=v.index-math.floor(v.index+0.5) + end + v.path=nil + v.path_dist=nil + v.index=nil + v.min_index_on_track=nil + v.max_index_on_track=nil + + advtrains.detector.setup_restore() + v.detector_old_index=nil + v.detector_old_end_index=nil + end +end + +--not blocking trains group +function advtrains.train_collides(node) + if node and minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].walkable then + if not minetest.registered_nodes[node.name].groups.not_blocking_trains then + return true + end + end + return false +end + +local nonblocknodes={ + "default:fence_wood", + "default:fence_acacia_wood", + "default:fence_aspen_wood", + "default:fence_pine_wood", + "default:fence_junglewood", + "default:torch", + + "default:sign_wall", + "signs:sign_wall", + "signs:sign_wall_blue", + "signs:sign_wall_brown", + "signs:sign_wall_orange", + "signs:sign_wall_green", + "signs:sign_yard", + "signs:sign_wall_white_black", + "signs:sign_wall_red", + "signs:sign_wall_white_red", + "signs:sign_wall_yellow", + "signs:sign_post", + "signs:sign_hanging", + + +} +minetest.after(0, function() + for _,name in ipairs(nonblocknodes) do + if minetest.registered_nodes[name] then + minetest.registered_nodes[name].groups.not_blocking_trains=1 + end + end +end) diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua new file mode 100644 index 0000000..03eae34 --- /dev/null +++ b/advtrains/wagons.lua @@ -0,0 +1,607 @@ +--atan2 counts angles clockwise, minetest does counterclockwise
+--local print=function(t) minetest.log("action", t) minetest.chat_send_all(t) end
+local print=function() end
+
+local wagon={
+ collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
+ --physical = true,
+ visual = "mesh",
+ mesh = "wagon.b3d",
+ visual_size = {x=3, y=3},
+ textures = {"black.png"},
+ is_wagon=true,
+ wagon_span=1,--how many index units of space does this wagon consume
+ has_inventory=false,
+}
+
+
+
+function wagon:on_rightclick(clicker)
+ if not self:ensure_init() then return end
+ if not clicker or not clicker:is_player() then
+ return
+ end
+ if clicker:get_player_control().aux1 then
+ --advtrains.dumppath(self:train().path)
+ --minetest.chat_send_all("at index "..(self:train().index or "nil"))
+ --advtrains.invert_train(self.train_id)
+ minetest.chat_send_all(dump(self:train()))
+ return
+ end
+ local no=self:get_seatno(clicker:get_player_name())
+ if no then
+ self:get_off(no)
+ else
+ self:show_get_on_form(clicker:get_player_name())
+ end
+end
+
+function wagon:train()
+ return advtrains.trains[self.train_id]
+end
+
+--[[about 'initalized':
+ when initialized is false, the entity hasn't got any data yet and should wait for these to be set before doing anything
+ when loading an existing object (with staticdata), it will be set
+ when instanciating a new object via add_entity, it is not set at the time on_activate is called.
+ then, wagon:initialize() will be called
+
+ wagon will save only uid in staticdata, no serialized table
+]]
+function wagon:on_activate(sd_uid, dtime_s)
+ print("[advtrains][wagon "..((sd_uid and sd_uid~="" and sd_uid) or "no-id").."] activated")
+ self.object:set_armor_groups({immortal=1})
+ if sd_uid and sd_uid~="" then
+ --legacy
+ --expect this to be a serialized table and handle
+ if minetest.deserialize(sd_uid) then
+ self:init_from_wagon_save(minetest.deserialize(sd_uid).unique_id)
+ else
+ self:init_from_wagon_save(sd_uid)
+ end
+ end
+ self.entity_name=self.name
+
+ --duplicates?
+ for ao_id,wagon in pairs(minetest.luaentities) do
+ if wagon.is_wagon and wagon.initialized and wagon.unique_id==self.unique_id and wagon~=self then--i am a duplicate!
+ print("[advtrains][wagon "..((sd_uid and sd_uid~="" and sd_uid) or "no-id").."] duplicate found(ao_id:"..ao_id.."), removing")
+ self.object:remove()
+ minetest.after(0.5, function() advtrains.update_trainpart_properties(self.train_id) end)
+ return
+ end
+ end
+
+ if self.custom_on_activate then
+ self:custom_on_activate(staticdata_table, dtime_s)
+ end
+end
+
+function wagon:get_staticdata()
+ if not self:ensure_init() then return end
+ print("[advtrains][wagon "..((self.unique_id and self.unique_id~="" and self.unique_id) or "no-id").."]: saving to wagon_save")
+ --serialize inventory, if it has one
+ if self.has_inventory then
+ local inv=minetest.get_inventory({type="detached", name="advtrains_wgn_"..self.unique_id})
+ self.ser_inv=advtrains.serialize_inventory(inv)
+ end
+ --save to table before being unloaded
+ advtrains.wagon_save[self.unique_id]=advtrains.merge_tables(self)
+ advtrains.wagon_save[self.unique_id].entity_name=self.name
+ advtrains.wagon_save[self.unique_id].name=nil
+ advtrains.wagon_save[self.unique_id].object=nil
+ return self.unique_id
+end
+--returns: uid of wagon
+function wagon:init_new_instance(train_id, properties)
+ self.unique_id=os.time()..os.clock()
+ self.train_id=train_id
+ for k,v in pairs(properties) do
+ if k~="name" and k~="object" then
+ self[k]=v
+ end
+ end
+ self:init_shared()
+ self.initialized=true
+ print("init_new_instance "..self.unique_id.." ("..self.train_id..")")
+ return self.unique_id
+end
+function wagon:init_from_wagon_save(uid)
+ if not advtrains.wagon_save[uid] then
+ self.object:remove()
+ return
+ end
+ self.unique_id=uid
+ for k,v in pairs(advtrains.wagon_save[uid]) do
+ if k~="name" and k~="object" then
+ self[k]=v
+ end
+ end
+ if not self.train_id or not self:train() then
+ self.object:remove()
+ return
+ end
+ self:init_shared()
+ self.initialized=true
+ minetest.after(1, function() self:reattach_all() end)
+ print("init_from_wagon_save "..self.unique_id.." ("..self.train_id..")")
+ advtrains.update_trainpart_properties(self.train_id)
+end
+function wagon:init_shared()
+ if self.has_inventory then
+ local uid_noptr=self.unique_id..""
+ --to be used later
+ local inv=minetest.create_detached_inventory("advtrains_wgn_"..self.unique_id, {
+ allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
+ return count
+ end,
+ allow_put = function(inv, listname, index, stack, player)
+ return stack:get_count()
+ end,
+ allow_take = function(inv, listname, index, stack, player)
+ return stack:get_count()
+ end
+ })
+ if self.ser_inv then
+ advtrains.deserialize_inventory(self.ser_inv, inv)
+ end
+ if self.inventory_list_sizes then
+ for lst, siz in pairs(self.inventory_list_sizes) do
+ inv:set_size(lst, siz)
+ end
+ end
+ end
+end
+function wagon:ensure_init()
+ if self.initialized then return true end
+ self.object:setvelocity({x=0,y=0,z=0})
+ return false
+end
+
+-- Remove the wagon
+function wagon:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
+ if not self:ensure_init() then return end
+ if not puncher or not puncher:is_player() then
+ return
+ end
+ if self.owner and puncher:get_player_name()~=self.owner then
+ minetest.chat_send_player(puncher:get_player_name(), "This wagon is owned by "..self.owner..", you can't destroy it.")
+ return
+ end
+
+ if minetest.setting_getbool("creative_mode") then
+ if not self:destroy() then return end
+
+ local inv = puncher:get_inventory()
+ if not inv:contains_item("main", self.name) then
+ inv:add_item("main", self.name)
+ end
+ else
+ local pc=puncher:get_player_control()
+ if not pc.sneak then
+ minetest.chat_send_player(puncher:get_player_name(), "Warning: If you destroy this wagon, you only get some steel back! If you are sure, shift-leftclick the wagon.")
+ return
+ end
+
+ if not self:destroy() then return end
+
+ local inv = puncher:get_inventory()
+ for _,item in ipairs(self.drops or {self.name}) do
+ inv:add_item("main", item)
+ end
+ end
+end
+function wagon:destroy()
+ --some rules:
+ -- you get only some items back
+ -- single left-click shows warning
+ -- shift leftclick destroys
+ -- not when a driver is inside
+
+ for _,_ in pairs(self.seatp) do
+ return
+ end
+
+ if self.custom_may_destroy then
+ if not self.custom_may_destroy(self, puncher, time_from_last_punch, tool_capabilities, direction) then
+ return
+ end
+ end
+ if self.custom_on_destroy then
+ self.custom_on_destroy(self, puncher, time_from_last_punch, tool_capabilities, direction)
+ end
+
+ print("[advtrains][wagon "..((self.unique_id and self.unique_id~="" and self.unique_id) or "no-id").."]: destroying")
+
+ self.object:remove()
+
+ table.remove(self:train().trainparts, self.pos_in_trainparts)
+ advtrains.update_trainpart_properties(self.train_id)
+ advtrains.wagon_save[self.unique_id]=nil
+ if self.discouple then self.discouple.object:remove() end--will have no effect on unloaded objects
+ return true
+end
+
+
+function wagon:on_step(dtime)
+ if not self:ensure_init() then return end
+
+ local t=os.clock()
+ local pos = self.object:getpos()
+
+ if not pos then
+ print("["..self.unique_id.."][fatal] missing position (object:getpos() returned nil)")
+ return
+ end
+
+ self.entity_name=self.name
+
+ --is my train still here
+ if not self.train_id or not self:train() then
+ print("[advtrains][wagon "..self.unique_id.."] missing train_id, destroying")
+ self.object:remove()
+ return
+ elseif not self.initialized then
+ self.initialized=true
+ end
+ if not self.seatp then
+ self.seatp={}
+ end
+
+ --custom on_step function
+ if self.custom_on_step then
+ self:custom_on_step(self, dtime)
+ end
+
+ --driver control
+ for seatno, seat in ipairs(self.seats) do
+ if seat.driving_ctrl_access then
+ local driver=self.seatp[seatno] and minetest.get_player_by_name(self.seatp[seatno])
+ local get_off_pressed=false
+ if driver and driver:get_player_control_bits()~=self.old_player_control_bits then
+ local pc=driver:get_player_control()
+
+ advtrains.on_control_change(pc, self:train(), self.wagon_flipped)
+ if pc.aux1 and pc.sneak then
+ get_off_pressed=true
+ end
+
+ self.old_player_control_bits=driver:get_player_control_bits()
+ end
+ if driver then
+ if get_off_pressed then
+ self:get_off(seatno)
+ else
+ advtrains.update_driver_hud(driver:get_player_name(), self:train(), self.wagon_flipped)
+ end
+ end
+ end
+ end
+
+ local gp=self:train()
+
+ --DisCouple
+ if self.pos_in_trainparts and self.pos_in_trainparts>1 then
+ if gp.velocity==0 then
+ if not self.discouple or not self.discouple.object:getyaw() then
+ local object=minetest.add_entity(pos, "advtrains:discouple")
+ if object then
+ local le=object:get_luaentity()
+ le.wagon=self
+ --box is hidden when attached, so unuseful.
+ --object:set_attach(self.object, "", {x=0, y=0, z=self.wagon_span*10}, {x=0, y=0, z=0})
+ self.discouple=le
+ else
+ print("Couldn't spawn DisCouple")
+ end
+ end
+ else
+ if self.discouple and self.discouple.object:getyaw() then
+ self.discouple.object:remove()
+ end
+ end
+ end
+ --for path to be available. if not, skip step
+ if not advtrains.get_or_create_path(self.train_id, gp) then
+ self.object:setvelocity({x=0, y=0, z=0})
+ return
+ end
+ if not self.pos_in_train then
+ --why ever. but better continue next step...
+ advtrains.update_trainpart_properties(self.train_id)
+ return
+ end
+
+ local index=advtrains.get_real_path_index(self:train(), self.pos_in_train)
+ --print("trainindex "..gp.index.." wagonindex "..index)
+
+ --position recalculation
+ local first_pos=gp.path[math.floor(index)]
+ local second_pos=gp.path[math.floor(index)+1]
+ if not first_pos or not second_pos then
+ --print("[advtrains] object "..self.unique_id.." path end reached!")
+ self.object:setvelocity({x=0,y=0,z=0})
+ return
+ end
+
+ --checking for environment collisions(a 3x3 cube around the center)
+ if not gp.recently_collided_with_env then
+ local collides=false
+ for x=-1,1 do
+ for y=0,2 do
+ for z=-1,1 do
+ local node=minetest.get_node_or_nil(vector.add(first_pos, {x=x, y=y, z=z}))
+ if (advtrains.train_collides(node)) then
+ collides=true
+ end
+ end
+ end
+ end
+ if collides then
+ gp.recently_collided_with_env=true
+ gp.velocity=-0.5*gp.velocity
+ gp.tarvelocity=0
+ end
+ end
+
+ --FIX: use index of the wagon, not of the train.
+ local velocity=(gp.velocity*gp.movedir)/(gp.path_dist[math.floor(index)] or 1)
+ local acceleration=(gp.last_accel or 0)/(gp.path_dist[math.floor(index)] or 1)
+ local factor=index-math.floor(index)
+ local actual_pos={x=first_pos.x-(first_pos.x-second_pos.x)*factor, y=first_pos.y-(first_pos.y-second_pos.y)*factor, z=first_pos.z-(first_pos.z-second_pos.z)*factor,}
+ local velocityvec={x=(first_pos.x-second_pos.x)*velocity*-1, z=(first_pos.z-second_pos.z)*velocity*-1, y=(first_pos.y-second_pos.y)*velocity*-1}
+ local accelerationvec={x=(first_pos.x-second_pos.x)*acceleration*-1, z=(first_pos.z-second_pos.z)*acceleration*-1, y=(first_pos.y-second_pos.y)*acceleration*-1}
+
+ --some additional positions to determine orientation
+ local aposfwd=gp.path[math.floor(index+2)]
+ local aposbwd=gp.path[math.floor(index-1)]
+
+ local yaw
+ if aposfwd and aposbwd then
+ yaw=advtrains.get_wagon_yaw(aposfwd, second_pos, first_pos, aposbwd, factor)+math.pi--TODO remove when cleaning up
+ else
+ yaw=math.atan2((first_pos.x-second_pos.x), (second_pos.z-first_pos.z))
+ end
+ if self.wagon_flipped then
+ yaw=yaw+math.pi
+ end
+
+ self.updatepct_timer=(self.updatepct_timer or 0)-dtime
+ if not self.old_velocity_vector
+ or not vector.equals(velocityvec, self.old_velocity_vector)
+ or not self.old_acceleration_vector
+ or not vector.equals(accelerationvec, self.old_acceleration_vector)
+ or self.old_yaw~=yaw
+ or self.updatepct_timer<=0 then--only send update packet if something changed
+ self.object:setpos(actual_pos)
+ self.object:setvelocity(velocityvec)
+ self.object:setacceleration(accelerationvec)
+ self.object:setyaw(yaw)
+ self.updatepct_timer=2
+ if self.update_animation then
+ self:update_animation(gp.velocity)
+ end
+ end
+
+
+ self.old_velocity_vector=velocityvec
+ self.old_acceleration_vector=accelerationvec
+ self.old_yaw=yaw
+ printbm("wagon step", t)
+end
+
+function advtrains.get_real_path_index(train, pit)
+ local pos_in_train_left=pit
+ local index=train.index
+ if pos_in_train_left>(index-math.floor(index))*(train.path_dist[math.floor(index)] or 1) then
+ pos_in_train_left=pos_in_train_left - (index-math.floor(index))*(train.path_dist[math.floor(index)] or 1)
+ index=math.floor(index)
+ while pos_in_train_left>(train.path_dist[index-1] or 1) do
+ pos_in_train_left=pos_in_train_left - (train.path_dist[index-1] or 1)
+ index=index-1
+ end
+ index=index-(pos_in_train_left/(train.path_dist[index-1] or 1))
+ else
+ index=index-(pos_in_train_left/(train.path_dist[math.floor(index-1)] or 1))
+ end
+ return index
+end
+
+function wagon:get_on(clicker, seatno)
+ if not self.seatp then
+ self.seatp={}
+ end
+ if not self.seats[seatno] then return end
+ if self.seatp[seatno] and self.seatp[seatno]~=clicker:get_player_name() then
+ self:get_off(seatno)
+ end
+ self.seatp[seatno] = clicker:get_player_name()
+ advtrains.player_to_train_mapping[clicker:get_player_name()]=self.train_id
+ clicker:set_attach(self.object, "", self.seats[seatno].attach_offset, {x=0,y=0,z=0})
+ clicker:set_eye_offset(self.seats[seatno].view_offset, self.seats[seatno].view_offset)
+end
+function wagon:get_off_plr(pname)
+ local no=self:get_seatno(pname)
+ if no then
+ self:get_off(no)
+ end
+end
+function wagon:get_seatno(pname)
+ for no, cont in pairs(self.seatp) do
+ if cont==pname then
+ return no
+ end
+ end
+ return nil
+end
+function wagon:get_off(seatno)
+ if not self.seatp[seatno] then return end
+ local pname = self.seatp[seatno]
+ local clicker = minetest.get_player_by_name(pname)
+ advtrains.player_to_train_mapping[pname]=nil
+ advtrains.clear_driver_hud(pname)
+ if clicker then
+ clicker:set_detach()
+ clicker:set_eye_offset({x=0,y=0,z=0}, {x=0,y=0,z=0})
+ local objpos=advtrains.round_vector_floor_y(self.object:getpos())
+ local yaw=self.object:getyaw()
+ local isx=(yaw < math.pi/4) or (yaw > 3*math.pi/4 and yaw < 5*math.pi/4) or (yaw > 7*math.pi/4)
+ --abuse helper function
+ for _,r in ipairs({-1, 1}) do
+ local p=vector.add({x=isx and r or 0, y=0, z=not isx and r or 0}, objpos)
+ if minetest.get_item_group(minetest.get_node(p).name, "platform")>0 then
+ minetest.after(0.2, function() clicker:setpos({x=p.x, y=p.y+1, z=p.z}) end)
+ end
+ end
+ end
+ self.seatp[seatno]=nil
+end
+function wagon:show_get_on_form(pname)
+ if not self.initialized then return end
+ if #self.seats==0 then
+ if self.has_inventory and self.get_inventory_formspec then
+ minetest.show_formspec(pname, "advtrains_inv_"..self.unique_id, self:get_inventory_formspec())
+ end
+ return
+ end
+ local form, comma="size[5,8]label[0.5,0.5;Select seat:]textlist[0.5,1;4,6;seat;", ""
+ for seatno, seattbl in ipairs(self.seats) do
+ local addtext, colorcode="", ""
+ if self.seatp and self.seatp[seatno] then
+ colorcode="#FF0000"
+ addtext=" ("..self.seatp[seatno]..")"
+ end
+ form=form..comma..colorcode..seattbl.name..addtext
+ comma=","
+ end
+ form=form..";0,false]"
+ if self.has_inventory and self.get_inventory_formspec then
+ form=form.."button_exit[1,7;3,1;inv;Show Inventory]"
+ end
+ minetest.show_formspec(pname, "advtrains_geton_"..self.unique_id, form)
+end
+minetest.register_on_player_receive_fields(function(player, formname, fields)
+ local uid=string.match(formname, "^advtrains_geton_(.+)$")
+ if uid then
+ for _,wagon in pairs(minetest.luaentities) do
+ if wagon.is_wagon and wagon.initialized and wagon.unique_id==uid then
+ if fields.inv then
+ if wagon.has_inventory and wagon.get_inventory_formspec then
+ minetest.show_formspec(player:get_player_name(), "advtrains_inv_"..uid, wagon:get_inventory_formspec())
+ end
+ elseif fields.seat then
+ local val=minetest.explode_textlist_event(fields.seat)
+ if val and val.type~="INV" and not self.seatp[player:get_player_name()] then
+ --get on
+ wagon:get_on(player, val.index)
+ --will work with the new close_formspec functionality. close exactly this formspec.
+ minetest.show_formspec(player:get_player_name(), formname, "")
+ end
+ end
+ end
+ end
+ end
+end)
+function wagon:reattach_all()
+ if not self.seatp then self.seatp={} end
+ for seatno, pname in pairs(self.seatp) do
+ local p=minetest.get_player_by_name(pname)
+ if p then
+ self:get_on(p ,seatno)
+ end
+ end
+end
+minetest.register_on_joinplayer(function(player)
+ for _,wagon in pairs(minetest.luaentities) do
+ if wagon.is_wagon and wagon.initialized then
+ wagon:reattach_all()
+ end
+ end
+end)
+
+function advtrains.register_wagon(sysname, traintype, prototype, desc, inv_img)
+ setmetatable(prototype, {__index=wagon})
+ minetest.register_entity(":advtrains:"..sysname,prototype)
+
+ minetest.register_craftitem(":advtrains:"..sysname, {
+ description = desc,
+ inventory_image = inv_img,
+ wield_image = inv_img,
+ stack_max = 1,
+
+ on_place = function(itemstack, placer, pointed_thing)
+ if not pointed_thing.type == "node" then
+ return
+ end
+
+ local node=minetest.env:get_node_or_nil(pointed_thing.under)
+ if not node then print("[advtrains]Ignore at placer position") return itemstack end
+ local nodename=node.name
+ if(not advtrains.is_track_and_drives_on(nodename, advtrains.all_traintypes[traintype].drives_on)) then
+ print("[advtrains]no track here, not placing.")
+ return itemstack
+ end
+ local conn1=advtrains.get_track_connections(node.name, node.param2)
+ local id=advtrains.create_new_train_at(pointed_thing.under, advtrains.dirCoordSet(pointed_thing.under, conn1), traintype)
+
+ local ob=minetest.env:add_entity(pointed_thing.under, "advtrains:"..sysname)
+ if not ob then
+ print("[advtrains]couldn't add_entity, aborting")
+ end
+ local le=ob:get_luaentity()
+
+ le.owner=placer:get_player_name()
+ le.infotext=desc..", owned by "..placer:get_player_name()
+
+ local wagon_uid=le:init_new_instance(id, {})
+
+ advtrains.add_wagon_to_train(le, id)
+ if not minetest.setting_getbool("creative_mode") then
+ itemstack:take_item()
+ end
+ return itemstack
+
+ end,
+ })
+end
+advtrains.register_train_type("steam", {"regular", "default"})
+
+--[[advtrains.register_wagon("blackwagon", "steam",{textures = {"black.png"}})
+advtrains.register_wagon("bluewagon", "steam",{textures = {"blue.png"}})
+advtrains.register_wagon("greenwagon", "steam",{textures = {"green.png"}})
+advtrains.register_wagon("redwagon", "steam",{textures = {"red.png"}})
+advtrains.register_wagon("yellowwagon", "steam",{textures = {"yellow.png"}})
+]]
+
+--[[
+ wagons can define update_animation(self, velocity) if they have a speed-dependent animation
+ this function will be called when the velocity vector changes or every 2 seconds.
+]]
+
+
+advtrains.register_train_type("electric", {"regular", "default"}, 20)
+
+
+
+advtrains.register_train_type("subway", {"default"}, 15)
+
+advtrains.register_wagon("subway_wagon", "subway",{
+ mesh="advtrains_subway_train.b3d",
+ textures = {"advtrains_subway_train.png"},
+ seats = {
+ {
+ name="Default Seat (driver stand)",
+ attach_offset={x=0, y=10, z=0},
+ view_offset={x=0, y=6, z=0},
+ driving_ctrl_access=true,
+ },
+ },
+ visual_size = {x=1, y=1},
+ wagon_span=1.8,
+ collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},
+ is_locomotive=true,
+ drops={"default:steelblock 4"},
+}, "Subway Passenger Wagon", "advtrains_subway_train_inv.png")
+
+
+
|