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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
--clipboard = {trainlen = number, [n] = {type = string, flipped = bool, properties = {}, bike_painter_livery = string, custom_info = <optional custom data>}
-- 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
-- Get current translator
local S = advtrains.translate
minetest.register_tool("advtrains:copytool", {
description = S("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, S("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, S("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, S("The clipboard is empty."));
return
end
clipboard = minetest.deserialize(clipboard)
if (clipboard.wagons == nil) then
minetest.chat_send_player(pname, S("The clipboard is empty."));
return
end
local wagons = {}
local n = 1
for _, clip_data in pairs(clipboard.wagons) do
wagons[n] = advtrains.create_wagon(clip_data.type, pname)
local wagon = advtrains.wagons[wagons[n]]
wagon.wagon_flipped = clip_data.wagon_flipped
-- Paste relevant wagon properties
wagon.whitelist = clip_data.properties.whitelist
wagon.roadnumber = clip_data.properties.roadnumber
wagon.fc = clip_data.properties.fc
wagon.fcind = clip_data.properties.fcind
-- Paste bike painter livery information
if clip_data.bike_painter_livery then
wagon.livery = clip_data.bike_painter_livery
end
-- Paste any optional custom data
if clip_data.custom_info then
local wagon_proto = advtrains.wagon_prototypes[wagon.type or wagon.entity_name]
if wagon_proto and wagon_proto.custom_paste_to_wagon_from_clipboard then
wagon_proto.custom_paste_to_wagon_from_clipboard(wagon, clip_data.custom_info)
end
end
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, S("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(), S("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(), S("No such wagon: @1.", le.id))
return
end
local train = advtrains.trains[wagon.train_id]
if (not train) then
minetest.chat_send_player(user:get_player_name(), S("No such train: @1.", 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,
properties = {
-- Copy relevant wagon properties
whitelist = wagon.whitelist,
roadnumber = wagon.roadnumber,
fc = wagon.fc,
fcind = wagon.fcind,
},
type = wagon.type
}
-- Copy bike painter livery information
if wagon.livery and type(wagon.livery) == "string" then
-- As a convenience for older wagon mods, copy the livery string that can be set
-- by some mods that support using the bike painter. Note that this will result in
-- incomplete or missing livery information being copied for some mods that use
-- additional fields for livery information. In general, all mods that support
-- customizing liveries should provide the necessary implementations of
-- custom_copy_from_wagon_to_clipboard() and custom_paste_to_wagon_from_clipboard()
-- in their wagon's prototype to support their liveries being properly copied with
-- this tool. It is beyond the scope of advtrains to support livery schemes defined
-- in other mods.
-- As with code that supports the bike painter elsewhere in advtrains, this code
-- should be considered temporary since it may be removed in the future.
clipboard.wagons[n].bike_painter_livery = wagon.livery
end
-- Copy any custom data (for liveries or other uses) if defined in the wagon's prototype
local wagon_proto = advtrains.wagon_prototypes[wagon.type or wagon.entity_name]
if wagon_proto and wagon_proto.custom_copy_from_wagon_to_clipboard and wagon_proto.custom_paste_to_wagon_from_clipboard then
-- Use wagon_proto.custom_copy_from_wagon_to_clipboard() and
-- wagon_proto.custom_paste_to_wagon_from_clipboard() if and only if both are
-- defined. They should always be defined in tandem for a wagon when custom liveries
-- or other mod specific data need to be copied by this tool. The content of
-- custom_info is not relevant to the copy tool. It is treated as a "black box".
clipboard.wagons[n].custom_info = wagon_proto.custom_copy_from_wagon_to_clipboard(wagon)
end
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, S("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(), S("Train copied."))
return itemstack
end
})
|