aboutsummaryrefslogtreecommitdiff
path: root/advtrains/copytool.lua
blob: 0c1cdfe9241640368cc4d5f84df08899348c1492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
--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_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<train.path_trk_b
			if (train.off_track) then
				minetest.chat_send_player(pname, "Back of train would end up off track, cancelling.")
				advtrains.remove_train(id)
				return
			end
			train.text_outside = clipboard.text_outside
			train.text_inside = clipboard.text_inside
			train.routingcode = clipboard.routingcode
			train.line = clipboard.line

			return
		end,
	-- Copy: Take the pointed-at train and put it on the clipboard
	on_use = function(itemstack, user, pointed_thing)
		if not user:get_player_name() then return end
		if (pointed_thing.type ~= "object") then return end

		local le = pointed_thing.ref:get_luaentity()
		if (le == nil) then
			minetest.chat_send_player(user:get_player_name(), "No such lua entity!")
			return
		end

		local wagon = advtrains.wagons[le.id]
		if (not (le.id and advtrains.wagons[le.id])) then
			minetest.chat_send_player(user:get_player_name(), string.format("No such wagon: %s", le.id))
			return
		end

		local train = advtrains.trains[wagon.train_id]
		if (not train) then
			minetest.chat_send_player(user:get_player_name(), string.format("No such train: %s", wagon.train_id))
			return
		end

		-- Record the train length. The paste operation should require this much free space.
		local clipboard = {
			trainlen = math.ceil(train.trainlen),
			text_outside = train.text_outside,
			text_inside = train.text_inside,
			routingcode = train.routingcode,
			line = train.line,
			wagons = {}
		}
		local trainLength = math.ceil(train.trainlen)

		local n = 1
		for _, wagonid in pairs(train.trainparts) do
			local wagon = advtrains.wagons[wagonid]
			clipboard.wagons[n] = {
				wagon_flipped = wagon.wagon_flipped,
				type = wagon.type
			}
			n = n + 1
		end
		

		local function flip_clipboard(wagon_clipboard)
			local flipped = {}
			local numWagons = #wagon_clipboard
			for k, v in ipairs(wagon_clipboard) do
				local otherSide = (numWagons-k)+1
				flipped[otherSide] = v
				local wagon = flipped[otherSide]
				wagon.wagon_flipped = not wagon.wagon_flipped
			end
			return flipped
		end

		local function is_loco(wagon_id)
			local wagon = advtrains.wagons[wagon_id]
			if (not wagon) then return false end
			local wagon_proto = advtrains.wagon_prototypes[wagon.type or wagon.entity_name]
			if wagon_proto and wagon_proto.is_locomotive then
				return true
			end
			return false
		end

		-- Decide on a new 'front of train' and possibly flip the train.
		-- Locomotive on one end = loco-hauled, that end is front;
		-- if (advtrains.wagons[train.trainparts[1]].is_locomotive) then -- do nothing, train is already in right order
		local numWagons = #train.trainparts
		local backLoco = train.trainparts[numWagons]
		backLoco = is_loco(backLoco)
		local frontLoco = train.trainparts[1]
		frontLoco = is_loco(frontLoco)
		if ((backLoco) and (not frontLoco)) then
			clipboard.wagons = flip_clipboard(clipboard.wagons)
			--minetest.chat_send_player(user:get_player_name(), "Flipped train: Loco-hauled")
		end
		-- locomotives on both ends = train is push-pull / multi-unit, has no front, do nothing
		-- no locomotives on ends = rake of wagons, front is end closest to where player copied.
		if ((not frontLoco) and (not backLoco)) then

			if (wagon.pos_in_trainparts / numWagons > 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
})
an> "Unconfigured Track Circuit Break, right-click to assign.") end, on_rightclick = function(pos, node, player) local pname = player:get_player_name() if not minetest.check_player_privs(pname, "interlocking") then minetest.chat_send_player(pname, "Insufficient privileges to use this!") return end local meta = minetest.get_meta(pos) local tcbpts = meta:get_string("tcb_pos") if tcbpts ~= "" then local tcbpos = minetest.string_to_pos(tcbpts) local tcb = ildb.get_tcb(tcbpos) if tcb then advtrains.interlocking.show_tcb_form(tcbpos, pname) else minetest.chat_send_player(pname, "This TCB has been removed. Please dig marker.") end else --unconfigured minetest.chat_send_player(pname, "Configuring TCB: Please punch the rail you want to assign this TCB to.") players_assign_tcb[pname] = pos end end, --on_punch = function(pos, node, player) -- local meta = minetest.get_meta(pos) -- local tcbpts = meta:get_string("tcb_pos") -- if tcbpts ~= "" then -- local tcbpos = minetest.string_to_pos(tcbpts) -- advtrains.interlocking.show_tcb_marker(tcbpos) -- end --end, can_dig = function(pos, player) if player == nil then return false end local pname = player:get_player_name() -- Those markers can only be dug when all adjacent TS's are set -- as EOI. local meta = minetest.get_meta(pos) local tcbpts = meta:get_string("tcb_pos") if tcbpts ~= "" then if not minetest.check_player_privs(pname, "interlocking") then minetest.chat_send_player(pname, "Insufficient privileges to use this!") return end local tcbpos = minetest.string_to_pos(tcbpts) local tcb = ildb.get_tcb(tcbpos) if not tcb then return true end for connid=1,2 do if tcb[connid].ts_id or tcb[connid].signal then minetest.chat_send_player(pname, "Can't remove TCB: Both sides must have no track section and no signal assigned!") return false end if not ildb.may_modify_tcbs(tcb[connid]) then minetest.chat_send_player(pname, "Can't remove TCB: Side "..connid.." forbids modification (shouldn't happen).") return false end end end return true end, after_dig_node = function(pos, oldnode, oldmetadata, player) if not oldmetadata or not oldmetadata.fields then return end local tcbpts = oldmetadata.fields.tcb_pos if tcbpts and tcbpts ~= "" then local tcbpos = minetest.string_to_pos(tcbpts) local success = ildb.remove_tcb(tcbpos) if success and player then minetest.chat_send_player(player:get_player_name(), "TCB has been removed.") else minetest.chat_send_player(player:get_player_name(), "Failed to remove TCB!") minetest.set_node(pos, oldnode) local meta = minetest.get_meta(pos) meta:set_string("tcb_pos", minetest.pos_to_string(tcbpos)) end end end, }) minetest.register_on_punchnode(function(pos, node, player, pointed_thing) local pname = player:get_player_name() if not minetest.check_player_privs(pname, "interlocking") then return end -- TCB assignment local tcbnpos = players_assign_tcb[pname] if tcbnpos then if vector.distance(pos, tcbnpos)<=20 then local node_ok, conns, rhe = advtrains.get_rail_info_at(pos, advtrains.all_tracktypes) if node_ok and #conns == 2 then local ok = ildb.create_tcb(pos) if not ok then minetest.chat_send_player(pname, "Configuring TCB: TCB already exists at this position! It has now been re-assigned.") end ildb.sync_tcb_neighbors(pos, 1) ildb.sync_tcb_neighbors(pos, 2) local meta = minetest.get_meta(tcbnpos) meta:set_string("tcb_pos", minetest.pos_to_string(pos)) meta:set_string("infotext", "TCB assigned to "..minetest.pos_to_string(pos)) minetest.chat_send_player(pname, "Configuring TCB: Successfully configured TCB") else minetest.chat_send_player(pname, "Configuring TCB: This is not a normal two-connection rail! Aborted.") end else minetest.chat_send_player(pname, "Configuring TCB: Node is too far away. Aborted.") end players_assign_tcb[pname] = nil end -- Signal assignment local sigd = players_assign_signal[pname] if sigd then if vector.distance(pos, sigd.p)<=50 then local is_signal = minetest.get_item_group(node.name, "advtrains_signal") >= 2 if is_signal then local ndef = minetest.registered_nodes[node.name] if ndef and ndef.advtrains and ndef.advtrains.set_aspect then local tcbs = ildb.get_tcbs(sigd) if tcbs then tcbs.signal = pos if not tcbs.signal_name then tcbs.signal_name = "Signal at "..minetest.pos_to_string(sigd.p) end if not tcbs.routes then tcbs.routes = {} end ildb.set_sigd_for_signal(pos, sigd) minetest.chat_send_player(pname, "Configuring TCB: Successfully assigned signal.") advtrains.interlocking.show_ip_form(pos, pname, true) else minetest.chat_send_player(pname, "Configuring TCB: Internal error, TCBS doesn't exist. Aborted.") end else minetest.chat_send_player(pname, "Configuring TCB: Cannot use static signals for routesetting. Aborted.") end else minetest.chat_send_player(pname, "Configuring TCB: Not a compatible signal. Aborted.") end else minetest.chat_send_player(pname, "Configuring TCB: Node is too far away. Aborted.") end players_assign_signal[pname] = nil end end) -- TCB Form local function mktcbformspec(tcbs, btnpref, offset, pname) local form = "" local ts if tcbs.ts_id then ts = ildb.get_ts(tcbs.ts_id) end if ts then form = form.."label[0.5,"..offset..";Side "..btnpref..": "..minetest.formspec_escape(ts.name).."]" form = form.."button[0.5,"..(offset+0.5)..";5,1;"..btnpref.."_gotots;Show track section]" if ildb.may_modify_tcbs(tcbs) then -- Note: the security check to prohibit those actions is located in database.lua in the corresponding functions. form = form.."button[0.5,"..(offset+1.5)..";2.5,1;"..btnpref.."_update;Update near TCBs]" form = form.."button[3 ,"..(offset+1.5)..";2.5,1;"..btnpref.."_remove;Remove from section]" end else tcbs.ts_id = nil form = form.."label[0.5,"..offset..";Side "..btnpref..": ".."End of interlocking]" form = form.."button[0.5,"..(offset+0.5)..";5,1;"..btnpref.."_makeil;Create Interlocked Track Section]" --if tcbs.section_free then --form = form.."button[0.5,"..(offset+1.5)..";5,1;"..btnpref.."_setlocked;Section is free]" --else --form = form.."button[0.5,"..(offset+1.5)..";5,1;"..btnpref.."_setfree;Section is blocked]" --end end if tcbs.signal then form = form.."button[0.5,"..(offset+2.5)..";5,1;"..btnpref.."_sigdia;Signalling]" else form = form.."button[0.5,"..(offset+2.5)..";5,1;"..btnpref.."_asnsig;Assign a signal]" end return form end function advtrains.interlocking.show_tcb_form(pos, pname) if not minetest.check_player_privs(pname, "interlocking") then minetest.chat_send_player(pname, "Insufficient privileges to use this!") return end local tcb = ildb.get_tcb(pos) if not tcb then return end local form = "size[6,9] label[0.5,0.5;Track Circuit Break Configuration]" form = form .. mktcbformspec(tcb[1], "A", 1, pname) form = form .. mktcbformspec(tcb[2], "B", 5, pname) minetest.show_formspec(pname, "at_il_tcbconfig_"..minetest.pos_to_string(pos), form) advtrains.interlocking.show_tcb_marker(pos) end --helper: length of nil table is 0 local function nlen(t) if not t then return 0 end return #t end minetest.register_on_player_receive_fields(function(player, formname, fields) local pname = player:get_player_name() if not minetest.check_player_privs(pname, "interlocking") then return end local pts = string.match(formname, "^at_il_tcbconfig_(.+)$") local pos if pts then pos = minetest.string_to_pos(pts) end if pos and not fields.quit then local tcb = ildb.get_tcb(pos) if not tcb then return end local f_gotots = {fields.A_gotots, fields.B_gotots} local f_update = {fields.A_update, fields.B_update} local f_remove = {fields.A_remove, fields.B_remove} local f_makeil = {fields.A_makeil, fields.B_makeil} local f_setlocked = {fields.A_setlocked, fields.B_setlocked} local f_setfree = {fields.A_setfree, fields.B_setfree} local f_asnsig = {fields.A_asnsig, fields.B_asnsig} local f_sigdia = {fields.A_sigdia, fields.B_sigdia} for connid=1,2 do local tcbs = tcb[connid] if tcbs.ts_id then if f_gotots[connid] then advtrains.interlocking.show_ts_form(tcbs.ts_id, pname) return end if f_update[connid] then ildb.sync_tcb_neighbors(pos, connid) end if f_remove[connid] then ildb.remove_from_interlocking({p=pos, s=connid}) end else if f_makeil[connid] then -- try sinc_tcb_neighbors first ildb.sync_tcb_neighbors(pos, connid) -- if that didn't work, create new section if not tcbs.ts_id then ildb.create_ts({p=pos, s=connid}) ildb.sync_tcb_neighbors(pos, connid) end end -- non-interlocked if f_setfree[connid] then tcbs.section_free = true end if f_setlocked[connid] then tcbs.section_free = nil end end if f_asnsig[connid] and not tcbs.signal then minetest.chat_send_player(pname, "Configuring TCB: Please punch the signal to assign.") players_assign_signal[pname] = {p=pos, s=connid} minetest.close_formspec(pname, formname) return end if f_sigdia[connid] and tcbs.signal then advtrains.interlocking.show_signalling_form({p=pos, s=connid}, pname) return end end advtrains.interlocking.show_tcb_form(pos, pname) end end) -- TS Formspec -- textlist selection temporary storage local ts_pselidx = {} function advtrains.interlocking.show_ts_form(ts_id, pname, sel_tcb) if not minetest.check_player_privs(pname, "interlocking") then minetest.chat_send_player(pname, "Insufficient privileges to use this!") return end local ts = ildb.get_ts(ts_id)