From 10ea9b896b140ecc5ed4e53e7ee1daeaa4efa46e Mon Sep 17 00:00:00 2001 From: orwell96 Date: Tue, 5 Sep 2023 20:46:18 +0200 Subject: Make the track registration less manual again to prevent duplicate code (but keep the template table in the track mod, as it's supposed to be) --- advtrains/trackplacer.lua | 2 +- advtrains/tracks.lua | 1 - advtrains_train_track/init.lua | 327 +++++++++++++++++++++-------------------- 3 files changed, 171 insertions(+), 159 deletions(-) diff --git a/advtrains/trackplacer.lua b/advtrains/trackplacer.lua index 9d63199..71eb79c 100644 --- a/advtrains/trackplacer.lua +++ b/advtrains/trackplacer.lua @@ -49,7 +49,7 @@ function tp.register_candidate(tpg, name, ndef, as_single, as_double) for i=0,3 do if as_double then g.double[rotate(c1,i*4).."_"..rotate(c2,i*4)] = {name=name, param2=i} - if not is_symmmetrical then + if not is_symmetrical then g.double[rotate(c2,i*4).."_"..rotate(c1,i*4)] = {name=name, param2=i} -- if the track is unsymmetric (e.g. a curve), we may require the "wrong" orientation to fill a gap. end diff --git a/advtrains/tracks.lua b/advtrains/tracks.lua index dc2d909..46ceaf8 100644 --- a/advtrains/tracks.lua +++ b/advtrains/tracks.lua @@ -213,7 +213,6 @@ function advtrains.register_node_4rot(ori_name, ori_ndef, definition_mangling_fu end -- register node - atdebug("Registering: ",name, ndef) minetest.register_node(":"..name, ndef) -- if this has the track_place_group set, register as a candidate for the track_place_group diff --git a/advtrains_train_track/init.lua b/advtrains_train_track/init.lua index 55b1367..6195b5b 100755 --- a/advtrains_train_track/init.lua +++ b/advtrains_train_track/init.lua @@ -1,172 +1,185 @@ -- advtrains_train_track --- rewritten to work with advtrains 2.5 track system +-- rewritten to work with advtrains 2.5 track system, but mimics the "old" template-based track registration +-- Also, since 2.5, all tracks are moved here, even the ATC, LuaATC and Interlocking special tracks local function conns(c1, c2, r1, r2) return {{c=c1, y=r1}, {c=c2, y=r2}} end local function conns3(c1, c2, c3, r1, r2, r3) return {{c=c1, y=r1}, {c=c2, y=r2}, {c=c3, y=r3}} end -local common_def = { - drawtype = "mesh", - paramtype = "light", - paramtype2 = "facedir", - walkable = false, - selection_box = { - type = "fixed", - fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, - }, - - mesh_suffix = ".b3d", - tiles = { "advtrains_dtrack_shared.png" }, - - groups = { - advtrains_track=1, - advtrains_track_default=1, - dig_immediate=2, - --not_in_creative_inventory=1, - }, - - can_dig = advtrains.track_can_dig_callback, - after_dig_node = advtrains.track_update_callback, - after_place_node = advtrains.track_update_callback, - - drop = "advtrains:dtrack_placer" -} - --- Normal tracks, straight and curved -advtrains.register_node_4rot("advtrains:dtrack_st", - advtrains.merge_tables(common_def, { - description=attrans("Track Straight"), - mesh_prefix="advtrains_dtrack_st", - at_conns = conns(0,8), - advtrains = { - trackworker_next_var = "advtrains:dtrack_cr", - track_place_group = "advtrains:dtrack", - track_place_single = true, - }, - }) -) - -advtrains.register_node_4rot("advtrains:dtrack_cr", - advtrains.merge_tables(common_def, { - description=attrans("Track Curve"), - mesh_prefix="advtrains_dtrack_cr", - at_conns = conns(0,7), - advtrains = { - trackworker_next_var = "advtrains:dtrack_swlst", - track_place_group = "advtrains:dtrack", - }, - }) -) +local function register(reg) + for sgi, sgrp in ipairs(reg.sgroups) do + -- prepare the state map if we need it later + local state_map = {} + if sgrp.turnout then + for vn,var in pairs(sgrp.variants) do + local name = reg.base .. "_" .. vn + state_map[var.state] = name + end + end + -- iterate through each of the variants + for vn,var in pairs(sgrp.variants) do + local name = reg.base .. "_" .. vn + local ndef = { + description = reg.description .. " " .. vn, + drawtype = "mesh", + paramtype = "light", + paramtype2 = "facedir", + walkable = false, + selection_box = { + type = "fixed", + fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, + }, + + mesh_prefix=reg.mprefix.."_"..vn, + mesh_suffix = ".b3d", + tiles = { "advtrains_dtrack_shared.png" }, + + groups = { + advtrains_track=1, + advtrains_track_default=1, + dig_immediate=2, + --not_in_creative_inventory=1, + }, + + at_conns = sgrp.conns, + at_conn_map = var.conn_map, + + can_dig = advtrains.track_can_dig_callback, + after_dig_node = advtrains.track_update_callback, + after_place_node = advtrains.track_update_callback, + + advtrains = { + trackworker_next_var = reg.base .. "_" .. var.next_var + } + } + -- drop field + if reg.register_placer then + ndef.drop = reg.base.."_placer" + else + ndef.drop = reg.drop + end + -- if variant is suitable for autoplacing (trackplacer) + if var.track_place then + ndef.advtrains.track_place_group = reg.base + ndef.advtrains.track_place_single = var.track_place_single + end + -- turnout handling + -- if the containing group was a turnout group, the containing state_map will be used + if sgrp.turnout then + ndef.on_rightclick = advtrains.state_node_on_rightclick_callback + ndef.advtrains.node_state = var.state + ndef.advtrains.node_next_state = var.next_state + ndef.advtrains.node_state_map = state_map + end + -- use advtrains-internal function to register the 4 rotations of the node, to make our life easier + --atdebug("Registering: ",name, ndef) -- for debugging it can be useful to output what is being registered + advtrains.register_node_4rot(name, ndef) + end + end + if reg.register_placer then + local tpgrp = reg.base + minetest.register_craftitem(":advtrains:dtrack_placer", { + description = reg.description, + inventory_image = reg.mprefix.."_placer.png", + wield_image = reg.mprefix.."_placer.png", + groups={advtrains_trackplacer=1, digtron_on_place=1}, + liquids_pointable = false, + on_place = function(itemstack, placer, pointed_thing) + local name = placer:get_player_name() + if not name then + return itemstack, false + end + if pointed_thing.type=="node" then + local pos=pointed_thing.above + local upos=vector.subtract(pointed_thing.above, {x=0, y=1, z=0}) + if not advtrains.check_track_protection(pos, name) then + return itemstack, false + end + if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to then + local s = minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable + if s then + -- minetest.chat_send_all(nnprefix) + local yaw = placer:get_look_horizontal() + advtrains.trackplacer.place_track(pos, tpgrp, name, yaw) + if not advtrains.is_creative(name) then + itemstack:take_item() + end + end + end + end + return itemstack, true + end, + }) + end +end --- simple turnouts left and right -local stm_left = { - st = "advtrains:dtrack_swlst", - cr = "advtrains:dtrack_swlcr", -} -advtrains.register_node_4rot("advtrains:dtrack_swlst", - advtrains.merge_tables(common_def, { - description=attrans("Track Turnout Left Straight"), - mesh_prefix="advtrains_dtrack_swlst", - at_conns = conns3(0,8,7), - at_conn_map = {2,1,1}, - on_rightclick = advtrains.state_node_on_rightclick_callback, - advtrains = { - node_state = "st", - node_next_state = "cr", - node_state_map = stm_left, - trackworker_next_var = "advtrains:dtrack_swrst" +-- normal dtrack +register({ + base = "advtrains:dtrack", + mprefix = "advtrains_dtrack", + description = attrans("Track"), + + sgroups = { -- integer-indexed table, we don't need a key here + -- inside are "variant" tables + { + variants = { + st = { + next_var = "cr", + track_place = true, + track_place_single = true, + }, + }, + conns = conns(0,8), }, - }) -) - -advtrains.register_node_4rot("advtrains:dtrack_swlcr", - advtrains.merge_tables(common_def, { - description=attrans("Track Turnout Left Curve"), - mesh_prefix="advtrains_dtrack_swlcr", - at_conns = conns3(0,8,7), -- note: conns must stay identical - at_conn_map = {3,1,1}, -- now points to curve branch - on_rightclick = advtrains.state_node_on_rightclick_callback, - advtrains = { - node_state = "cr", - node_next_state = "st", - node_state_map = stm_left, - trackworker_next_var = "advtrains:dtrack_swrcr" + { + variants = { + cr = { + next_var = "swlst", + track_place = true, + }, + }, + conns = conns(0,7), }, - }) -) - -local stm_right = { - st = "advtrains:dtrack_swrst", - cr = "advtrains:dtrack_swrcr", -} - -advtrains.register_node_4rot("advtrains:dtrack_swrst", - advtrains.merge_tables(common_def, { - description=attrans("Track Turnout Right Straight"), - mesh_prefix="advtrains_dtrack_swrst", - at_conns = conns3(0,8,9), - at_conn_map = {2,1,1}, - on_rightclick = advtrains.state_node_on_rightclick_callback, - advtrains = { - node_state = "st", - node_next_state = "cr", - node_state_map = stm_right, - trackworker_next_var = "advtrains:dtrack_st" + { + turnout = true, + variants = { + swlst = { + next_var = "swrst", + conn_map = {2,1,1}, + state = "st", + next_state = "cr", + }, + swlcr = { + next_var = "swrcr", + conn_map = {3,1,1}, + state = "cr", + next_state = "st", + }, + }, + conns = conns3(0,8,7), }, - }) -) - -advtrains.register_node_4rot("advtrains:dtrack_swrcr", - advtrains.merge_tables(common_def, { - description=attrans("Track Turnout Right Curve"), - mesh_prefix="advtrains_dtrack_swrcr", - at_conns = conns3(0,8,9), -- note: conns must stay identical - at_conn_map = {3,1,1}, -- now points to curve branch - on_rightclick = advtrains.state_node_on_rightclick_callback, - advtrains = { - node_state = "cr", - node_next_state = "st", - node_state_map = stm_right, - trackworker_next_var = "advtrains:dtrack_st" + { + turnout = true, + variants = { + swrst = { + next_var = "st", + conn_map = {2,1,1}, + state = "st", + next_state = "cr", + }, + swrcr = { + next_var = "st", + conn_map = {3,1,1}, + state = "cr", + next_state = "st", + }, + }, + conns = conns3(0,8,9), }, - }) -) - --- register placer item -minetest.register_craftitem(":advtrains:dtrack_placer", { - description = attrans("Track"), - inventory_image = "advtrains_dtrack_placer.png", - wield_image = "advtrains_dtrack_placer.png", - groups={advtrains_trackplacer=1, digtron_on_place=1}, - liquids_pointable = false, - on_place = function(itemstack, placer, pointed_thing) - local name = placer:get_player_name() - if not name then - return itemstack, false - end - if pointed_thing.type=="node" then - local pos=pointed_thing.above - local upos=vector.subtract(pointed_thing.above, {x=0, y=1, z=0}) - if not advtrains.check_track_protection(pos, name) then - return itemstack, false - end - if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to then - local s = minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable - if s then --- minetest.chat_send_all(nnprefix) - local yaw = placer:get_look_horizontal() - advtrains.trackplacer.place_track(pos, "advtrains:dtrack", name, yaw) - if not advtrains.is_creative(name) then - itemstack:take_item() - end - end - end - end - return itemstack, true - end, + }, + register_placer = true, }) - ---TODO restore mesecons! \ No newline at end of file +---------------------- \ No newline at end of file -- cgit v1.2.3