From 5aad0930d1ddd63d9021d833a9379b4a01bd8414 Mon Sep 17 00:00:00 2001 From: Gabriel PĂ©rez-Cerezo Date: Sat, 10 Oct 2020 23:28:42 +0200 Subject: Squashed 'advtrains/' content from commit 3256c27 git-subtree-dir: advtrains git-subtree-split: 3256c2778d626548541bcdfabf3026f781a2287c --- advtrains/copytool.lua | 185 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 advtrains/copytool.lua (limited to 'advtrains/copytool.lua') diff --git a/advtrains/copytool.lua b/advtrains/copytool.lua new file mode 100644 index 0000000..dc18081 --- /dev/null +++ b/advtrains/copytool.lua @@ -0,0 +1,185 @@ +--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) + return advtrains.pcall(function() + 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_and_drives_on(nodename, {default=true})) 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, "You don't have the train_operator privilege.") + 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, "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, "The clipboard is empty."); + return + end + clipboard = minetest.deserialize(clipboard) + if (clipboard.wagons == nil) then + minetest.chat_send_player(pname, "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 +}) \ No newline at end of file -- cgit v1.2.3