--clipboard = {trainlen = number, [n] = {type = string, flipped = bool, } -- Yaw is in radians. There are 2pi rad in a circle. North is the 0 point and the angle increases anticlockwise. -- 4.712389 = 1.5pi; sin(1.5pi) = -1 -- 7.853981 = 2.5pi; sin(2.5pi) = 1 minetest.register_tool("advtrains:copytool", { description = attrans("Train copy/paste tool\n\nLeft-click: copy train\nRight-click: paste train"), inventory_image = "advtrains_copytool.png", wield_image = "advtrains_copytool.png", stack_max = 1, -- Paste: Take the clipboard and the player yaw, and attempt to place a new train in the world. -- The front of the train is used as the start of the new train and it proceeds backwards from -- the direction of travel. on_place = function(itemstack, placer, pointed_thing) if ((not pointed_thing.type == "node") or (not placer.get_player_name)) then return end local pname = placer:get_player_name() local node=minetest.get_node_or_nil(pointed_thing.under) if not node then atprint("[advtrains]Ignore at placer position") return itemstack end local nodename=node.name if(not advtrains.is_track(nodename)) then atprint("no track here, not placing.") return itemstack end if not minetest.check_player_privs(placer, {train_operator = true }) then minetest.chat_send_player(pname, S("You do not have the @1 privilege.", "train_operator")) return itemstack end if not minetest.check_player_privs(placer, {train_admin = true }) and minetest.is_protected(pointed_thing.under, placer:get_player_name()) then return itemstack end local tconns=advtrains.get_track_connections(node.name, node.param2) local yaw = placer:get_look_horizontal() local plconnid = advtrains.yawToClosestConn(yaw, tconns) local prevpos = advtrains.get_adjacent_rail(pointed_thing.under, tconns, plconnid, {default=true}) if not prevpos then minetest.chat_send_player(pname, attrans("The track you are trying to place the wagon on is not long enough.")) return end local meta = itemstack:get_meta() if not meta then minetest.chat_send_player(pname, attrans("The clipboard couldn't access the metadata. Paste failed.")) return end local clipboard = meta:get_string("clipboard") if (clipboard == "") then minetest.chat_send_player(pname, attrans("The clipboard is empty.")); return end clipboard = minetest.deserialize(clipboard) if (clipboard.wagons == nil) then minetest.chat_send_player(pname, attrans("The clipboard is empty.")); return end local wagons = {} local n = 1 for _, wagonProto in pairs(clipboard.wagons) do local wagon = advtrains.create_wagon(wagonProto.type, pname) advtrains.wagons[wagon].wagon_flipped = wagonProto.wagon_flipped wagons[n] = wagon n = n + 1 end local id=advtrains.create_new_train_at(pointed_thing.under, plconnid, 0, wagons) local train = advtrains.trains[id] train.off_track = train.end_index 0.5) then -- towards the end of the rain clipboard.wagons = flip_clipboard(clipboard.wagons) --minetest.chat_send_player(user:get_player_name(), "Flipped train: Rake") end end local meta = itemstack:get_meta() if not meta then minetest.chat_send_player(pname, attrans("The clipboard couldn't access the metadata. Copy failed.")) return end meta:set_string("clipboard", minetest.serialize(clipboard)) minetest.chat_send_player(user:get_player_name(), attrans("Train copied.")) return itemstack end })