forked from jordan4ibanez/trains-OLD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.lua
More file actions
372 lines (334 loc) · 13.1 KB
/
functions.lua
File metadata and controls
372 lines (334 loc) · 13.1 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
--I have no idea where I got this function from
function distance( x1, y1, x2, y2 )
local dx = x1 - x2
local dy = y1 - y2
return math.sqrt ( dx * dx + dy * dy )
end
--http://stackoverflow.com/questions/2705793/how-to-get-number-of-entries-in-a-lua-table
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
-- Round 'v' to 'p' decimal places https://love2d.org/forums/viewtopic.php?f=4&t=146#p1508
function round(v, p)
local scale = math.pow(10, p or 0);
local res = math.floor(math.abs(v) * scale + 0.5) / scale;
if v < 0 then
res = -res;
end;
return res;
end;
function force_load_train(self)
local chunkpos = self.object:getpos()
minetest.forceload_block(chunkpos)
minetest.forceload_block({x=chunkpos.x-5, y=chunkpos.y, z=chunkpos.z})
minetest.forceload_block({x=chunkpos.x+5, y=chunkpos.y, z=chunkpos.z})
minetest.forceload_block({x=chunkpos.x, y=chunkpos.y, z=chunkpos.z-5})
minetest.forceload_block({x=chunkpos.x, y=chunkpos.y, z=chunkpos.z+5})
end
train_functions = {}
function train_functions.activate(self,staticdata)
self.object:set_armor_groups({immortal=1})
if self.id == nil then
self.id = train_id
end
--deserialization
if staticdata then
local data = minetest.deserialize(staticdata)
if data then
self.gear = data.gear
self.lastpos = data.lastpos
self.stop = data.stop
self.position = data.position
self.direction = data.direction
self.id = data.id
end
end
end
function train_functions.remove_entities(self, hitter)
--have this remove the table in the train tables
self.object:remove()
self.cotruck.object:remove()
if train_table[self.id] ~= nil then
if train_table[self.id][3] ~= nil then
train_table[self.id][3].object:remove()
end
end
end
function train_functions.serialize(self)
self.tmp = {
gear = self.gear,
lastpos = self.lastpos,
stop = self.stop,
position = self.position,
id = self.id,
direction = self.direction,
}
end
--this cannot exceed 1 block per tick or the mod will become messed up
function train_on_track(self,position)
--check if cotruck exists or if needed to be stopped - do this here to prevent unneeded cpu usage - this is also for very long cars
if self.cotruck.object:get_luaentity() == nil then
print("stop and force load it's area")
return
end
if self.stop == true then
return
end
local pos = self.object:getpos()
local pos = {x=round(pos.x, round_to),y=round(pos.y, round_to),z=round(pos.z, round_to)}--make it nice and even -- move this to the end as well so it is displayed perfectly
local center = {x=math.floor(pos.x + 0.5),y=math.floor(pos.y + 0.5),z=math.floor(pos.z + 0.5)}
--0 and 2 are z 1 and 3 are x
--a and c are z b and d are x
--e is always current pos (think engine)
--check clockwise
local a = minetest.get_node_group(minetest.get_node({x=pos.x,y=pos.y,z=pos.z+prediction_distance}).name, "track")
local b = minetest.get_node_group(minetest.get_node({x=pos.x+prediction_distance,y=pos.y,z=pos.z}).name, "track")
local c = minetest.get_node_group(minetest.get_node({x=pos.x,y=pos.y,z=pos.z-prediction_distance}).name, "track")
local d = minetest.get_node_group(minetest.get_node({x=pos.x-prediction_distance,y=pos.y,z=pos.z}).name, "track")
local e = minetest.get_node_group(minetest.get_node(pos).name, "track")
local a2 = minetest.get_node({x=pos.x,y=pos.y,z=pos.z+prediction_distance}).param2
local b2 = minetest.get_node({x=pos.x+prediction_distance,y=pos.y,z=pos.z}).param2
local c2 = minetest.get_node({x=pos.x,y=pos.y,z=pos.z-prediction_distance}).param2
local d2 = minetest.get_node({x=pos.x-prediction_distance,y=pos.y,z=pos.z}).param2
local e2 = minetest.get_node(pos).param2
--print("a:"..a.."|b:"..b.."|c:"..c.."|d:"..d.."|e:"..e.."|a2:"..a2.."|b2:"..b2.."|c2:"..c2.."|d2:"..d2.."|e2:"..e2) --massive debug
--check if track in front of you while on a straight
if e == 1 then
--z related track
if e2 == 0 then
if self.direction == 0 then
if a == 1 then
if a2 == 0 then
self.object:moveto({x=pos.x,y=pos.y,z=pos.z+train_speed}, false)
end
end
elseif self.direction == 2 then
if c == 1 then
if c2 == 0 then
self.object:moveto({x=pos.x,y=pos.y,z=pos.z-train_speed}, false)
end
end
end
-- x related track
elseif e2 == 1 then
if self.direction == 1 then
if b == 1 then
if b2 == 1 then
self.object:moveto({x=pos.x+train_speed,y=pos.y,z=pos.z}, false)
end
end
elseif self.direction == 3 then
if d == 1 then
if d2 == 1 then
self.object:moveto({x=pos.x-train_speed,y=pos.y,z=pos.z}, false)
end
end
end
end
--check if turn is matching
--z related straight track
if e2 == 0 then
if self.direction == 0 then
if a == 2 then
if a2 == 2 or a2 == 3 then
self.object:moveto({x=pos.x,y=pos.y,z=pos.z+train_speed}, false)
end
end
elseif self.direction == 2 then
if c == 2 then
if c2 == 0 or c2 == 1 then
self.object:moveto({x=pos.x,y=pos.y,z=pos.z-train_speed}, false)
end
end
end
--x related straight track
elseif e2 == 1 then
if self.direction == 1 then
if b == 2 then
if b2 == 0 or b2 == 3 then
self.object:moveto({x=pos.x+train_speed,y=pos.y,z=pos.z}, false)
end
end
elseif self.direction == 3 then
if d == 2 then
if d2 == 1 or d2 == 2 then
self.object:moveto({x=pos.x-train_speed,y=pos.y,z=pos.z}, false)
end
end
end
end
--handle turning and turn prediction
elseif e == 2 then
if e2 == 0 then
if self.direction == 0 then
if (a == 1 and a2 == 0) or (a == 2 and (a2 == 2 or a2 == 3)) then
self.object:moveto({x=center.x,y=pos.y,z=pos.z+train_speed}, false)
end
elseif self.direction == 1 then
if math.abs(round(pos.x-center.x, round_to)) > 0 then
self.object:moveto({x=pos.x+train_speed,y=pos.y,z=pos.z}, false)
elseif math.abs(round(pos.x-center.x, round_to)) == 0 then
self.direction = 0
self.object:moveto({x=center.x,y=pos.y,z=pos.z+train_speed}, false)
end
elseif self.direction == 2 then
if math.abs(round(pos.z-center.z, round_to)) > 0 then
self.object:moveto({x=pos.x,y=pos.y,z=pos.z-train_speed}, false)
elseif math.abs(round(pos.z-center.z, round_to)) == 0 then
self.direction = 3
self.object:moveto({x=pos.x-train_speed,y=pos.y,z=center.z}, false)
end
elseif self.direction == 3 then
if (d == 1 and d2 == 1) or (d == 2 and (d2 == 1 or d2 == 2)) then
self.object:moveto({x=pos.x-train_speed,y=pos.y,z=center.z}, false)
end
end
elseif e2 == 1 then
if self.direction == 0 then
if (a == 1 and a2 == 0) or (a == 2 and (a2 == 2 or a2 == 3)) then
self.object:moveto({x=center.x,y=pos.y,z=pos.z+train_speed}, false)
end
elseif self.direction == 1 then
if (b == 1 and b2 == 1) or (b == 2 and (b2 == 0 or b2 == 3)) then
self.object:moveto({x=pos.x+train_speed,y=pos.y,z=center.z}, false)
end
elseif self.direction == 2 then
if math.abs(round(pos.z-center.z, round_to)) > 0 then
self.object:moveto({x=pos.x,y=pos.y,z=pos.z-train_speed}, false)
elseif math.abs(round(pos.z-center.z, round_to)) == 0 then
self.direction = 1
self.object:moveto({x=pos.x+train_speed,y=pos.y,z=center.z}, false)
end
elseif self.direction == 3 then
if math.abs(round(pos.x-center.x, round_to)) > 0 then
self.object:moveto({x=pos.x-train_speed,y=pos.y,z=pos.z}, false)
elseif math.abs(round(pos.x-center.x, round_to)) == 0 then
self.direction = 0
self.object:moveto({x=center.x,y=pos.y,z=pos.z+train_speed}, false)
end
end
elseif e2 == 2 then
if self.direction == 0 then
if math.abs(round(pos.z-center.z, round_to)) > 0 then
self.object:moveto({x=pos.x,y=pos.y,z=pos.z+train_speed}, false)
elseif math.abs(round(pos.z-center.z, round_to)) == 0 then
self.direction = 1
self.object:moveto({x=pos.x+train_speed,y=pos.y,z=center.z}, false)
end
elseif self.direction == 1 then
if (b == 1 and b2 == 1) or (b == 2 and (b2 == 0 or b2 == 3)) then
self.object:moveto({x=pos.x+train_speed,y=pos.y,z=center.z}, false)
end
elseif self.direction == 2 then
if (c == 1 and c2 == 0) or (c == 2 and (c2 == 0 or c2 == 1)) then
self.object:moveto({x=center.x,y=pos.y,z=pos.z-train_speed}, false)
end
elseif self.direction == 3 then
if math.abs(round(pos.x-center.x, round_to)) > 0 then
self.object:moveto({x=pos.x-train_speed,y=pos.y,z=pos.z}, false)
elseif math.abs(round(pos.x-center.x, round_to)) == 0 then
self.direction = 2
self.object:moveto({x=center.x,y=pos.y,z=pos.z-train_speed}, false)
end
end
elseif e2 == 3 then
if self.direction == 0 then
if math.abs(round(pos.z-center.z, round_to)) > 0 then
self.object:moveto({x=pos.x,y=pos.y,z=pos.z+train_speed}, false)
elseif math.abs(round(pos.z-center.z, round_to)) == 0 then
self.direction = 3
self.object:moveto({x=pos.x-train_speed,y=pos.y,z=center.z}, false)
end
elseif self.direction == 1 then
if math.abs(round(pos.x-center.x, round_to)) > 0 then
self.object:moveto({x=pos.x+train_speed,y=pos.y,z=pos.z}, false)
elseif math.abs(round(pos.x-center.x, round_to)) == 0 then
self.direction = 2
self.object:moveto({x=center.x,y=pos.y,z=pos.z-train_speed}, false)
end
elseif self.direction == 2 then
if (c == 1 and c2 == 0) or (c == 2 and (c2 == 0 or c2 == 1)) then
self.object:moveto({x=center.x,y=pos.y,z=pos.z-train_speed}, false)
end
elseif self.direction == 3 then
if (d == 1 and d2 == 1) or (d == 2 and (d2 == 1 or d2 == 2)) then
self.object:moveto({x=pos.x-train_speed,y=pos.y,z=center.z}, false)
end
end
end
end
--set the lastpos so you can use it to equal the distance between the trucks when they stop
local lastpos = self.object:getpos()
self.lastpos = {x=round(pos.x, round_to),y=round(pos.y, round_to),z=round(pos.z, round_to)}
--make sure that everything is even and absolute
local newpos = self.object:getpos()
local newpos = {x=round(newpos.x, round_to),y=round(newpos.y, round_to),z=round(newpos.z, round_to)}
self.object:setpos(newpos)
--then check if other truck is stopped, if other truck doesn't exist, stop -- this needs a simple value added for single entity cars and engines
if self.cotruck ~= nil then
if newpos.x == pos.x and newpos.y == pos.y and newpos.z == pos.z and self.stop == false then
self.cotruck.stop = true
else
self.cotruck.stop = false
end
elseif self.cotruck == nil then
self.stop = true
end
end
--[[
DOCUMENTATION ON TURNS
0 = +x and -z
1 = -x and -z
2 = -x and +z
3 = +x and +z
a = z+1
b = x+1
c = z-1
d = x-1
entity direction
0+ 2- = z
1+ 3- = x
rails
0 = z
1 = x
]]--
--[[
You found casper, the friendly bug.
.,,cccd$$$$$$$$$$$ccc,
,cc$$$$$$$$$$$$$$$$$$$$$$$$$cc,
,d$$$$$$$$$$$$$$$$"J$$$$$$$$$$$$$$c,
d$$$$$$$$$$$$$$$$$$,$" ,,`?$$$$$$$$$$$$L
,$$$$$$$$$$$$$$$$$$$$$',J$$$$$$$$$$$$$$$$$b
,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$i `$h
$$$$$$$$$$$$$$$$$$$$$$$$$P' "$$$$$$$$$$$h $$
;$$$$$$$$$$$$$$$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