-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
456 lines (400 loc) · 14.8 KB
/
init.lua
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
--to make the track wood seperate from the metal, you'll have to make a texture for each side
--make rails generate with the fire code 000000
--make the train travel around itself, under it's own power
--make the train serialize and deserialize it's data
--have the item id on the
--eventually have the train number auto decrease and increase with how many vehicles physically exist in the world
--force load chunks with the trucks directions --an entire active train or car unless it stops
--instead of just crafting it, have a rail house that works as a huge crafting table, unmineable, and then have the doors open when the train is done crafting, and make it roll out
--have the train derail if the collision box of the body comes into contact with a node
--make sure that it is placed on a solid piece of track without turns
--make the train be able to go backwards
--make the train animation based on speed
--store last position of cotruck, if the same as last then stop
--make mobs get picked up by minecarts
--create some kind of thing to slow down the wheels based on how far apart they are
--have it scan the tracks when coming out of a turn to check if it is wide enough to turn
--diagnal track
--turn tables
--train lifts
--for smoother trains only change velocity when comparing the self.track with the one in front and it's different, ONLY CHECK NODE WHEN MATH.FLOOR( POS + 0.5) IS DIFFERENT FROM self.pos!
--[[ IMPORTANT!
--save all entities to a table in world file
--save all trains to file on shut down then force load them on startup so trains are attonomous
depending how small the train is, use variable speeds 0.005 0.01 0.015 etc
]]--
dofile(minetest.get_modpath("trains").."/functions.lua")
--these variables will be part of the api eventually
--round to nearest for the position, make sure that the speed you're setting it at matches to this decimal place. 2 = tenths, 3 = hundreths,
--speed 0.1 = round_to 1, speed 0.25 = round_to 2, etc
--set it to round_to 0 train speed 1.0 for craziness
--in this you can do as many train speeds as you'd like
--the lower the acceleratin the the faster it accelerates, think 0-60
--make sure to center the node when the gear changes
round_to = 2
train_speed = 0.01
--whole numbers only
truck_distance = 7
--keep this at 0.5 or spooky stuff will happen
prediction_distance = 0.5
--do this to allow multiple entities to work as one
train_table = {}
print("---------")
local file=io.open(minetest.get_worldpath().."/train_id.txt","r")
if file ~= nil then
print("Reading TRAIN ID file")
train_id = file:read("*all")
file:close()
train_id = tonumber(train_id)
else
--file:close()
print("Writing TRAIN ID file")
local file=io.open(minetest.get_worldpath().."/train_id.txt","w")
file:write("0")
file:close(file)
train_id = 0
end
print("---------")
print("")
print("")
print("Current train ID: "..train_id)
print("")
print("")
print("---------")
--These both have the ability to load or set table items because one might load before the other.
--########################################################################################################################################################
minetest.register_entity("trains:truck_front",
{
hp_max = 1,
physical = true,
weight = 5,
visual = "mesh",
mesh = "cart.x",
visual_size = {x=1, y=1},
textures = {"cart.png"},
collisionbox = {-0.395,-0.4,-0.395, 0.395,0.5,0.395},
collide_with_objects = false,
groups = {"immortal"},
is_visible = true,
cotruck = nil,
--speeds and rounding
speed = {train_speed_1,train_speed_2,train_speed_3,train_speed_4},--this will be changed to api.train speed when the api is created
round = {round_to_1,round_to_2,round_to_3,round_to_4},--make the track function see how many speeds there are and work with that
--serialized variables
gear = 0,
lastpos = nil,
stop = false,
position = "front",
id = nil,
direction = nil,
--deserialization
on_activate = function(self,staticdata)
train_functions.activate(self,staticdata)
--grouping
if not train_table[self.id] then
train_table[self.id] = {}
end
train_table[self.id][1] = self.object:get_luaentity()
if train_table[self.id][2] ~= nil then
self.cotruck = train_table[self.id][2]
train_table[self.id][2].cotruck = train_table[self.id][1]
end
end,
--remove entire train
on_punch = function(self, hitter)
train_functions.remove_entities(self, hitter)
end,
--serialization and grouping
get_staticdata = function(self)
train_functions.serialize(self)
return minetest.serialize(self.tmp)
end,
--physics stuff
on_step = function(self)
force_load_train(self)
train_on_track(self)
end
})
minetest.register_entity("trains:truck_rear",
{
hp_max = 1,
physical = true,
weight = 5,
visual = "mesh",
mesh = "cart.x",
visual_size = {x=1, y=1},
textures = {"cart.png"},
collisionbox = {-0.395,-0.4,-0.395, 0.395,0.5,0.395},
collide_with_objects = false,
groups = {"immortal"},
is_visible = true,
cotruck = nil,
--speeds and rounding
speed = {train_speed_1,train_speed_2,train_speed_3,train_speed_4},--this will be changed to api.train speed when the api is created
round = {round_to_1,round_to_2,round_to_3,round_to_4},--make the track function see how many speeds there are and work with that
--serialized variables
gear = 0,
lastpos = nil,
stop = false,
position = "rear",
id = nil,
direction = nil,
--deserialization
on_activate = function(self,staticdata)
train_functions.activate(self,staticdata)
--grouping
if not train_table[self.id] then
train_table[self.id] = {}
end
train_table[self.id][2] = self.object:get_luaentity()
if train_table[self.id][1] ~= nil then
self.cotruck = train_table[self.id][1]
train_table[self.id][1].cotruck = train_table[self.id][2]
end
end,
--remove entire train
on_punch = function(self, hitter)
train_functions.remove_entities(self, hitter)
end,
--serialization and grouping
get_staticdata = function(self)
train_functions.serialize(self)
return minetest.serialize(self.tmp)
end,
--physics stuff
on_step = function(self)
force_load_train(self)
train_on_track(self)
end
})
--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
minetest.register_entity("trains:engine",
{
hp_max = 1,
physical = true,
weight = 5,
collisionbox = {-0.395,-0.4,-0.395, 0.395,0.5,0.395},
mesh = "train.x",
visual = "mesh",
visual_size = {x=2, y=2},
tiles = {"default_dirt.png","default_dirt.png","default_dirt.png","default_dirt.png","default_dirt.png","default_dirt.png","default_dirt.png","default_dirt.png","default_dirt.png","default_dirt.png",},
groups = {"immortal"},
is_visible = true,
--remove entire train
on_punch = function(self, hitter)
--reverse positions then directions
local pos1 = train_table[self.id][1].object:getpos()
local pos2 = train_table[self.id][2].object:getpos()
local pos1 = {x=round(pos1.x, round_to),y=round(pos1.y, round_to),z=round(pos1.z, round_to)}
local pos2 = {x=round(pos2.x, round_to),y=round(pos2.y, round_to),z=round(pos2.z, round_to)}
train_table[self.id][1].object:setpos(pos2)
train_table[self.id][2].object:setpos(pos1)
if train_table[self.id][1].direction == 0 then
train_table[self.id][1].direction = 2
elseif train_table[self.id][1].direction == 1 then
train_table[self.id][1].direction = 3
elseif train_table[self.id][1].direction == 2 then
train_table[self.id][1].direction = 0
elseif train_table[self.id][1].direction == 3 then
train_table[self.id][1].direction = 1
end
if train_table[self.id][2].direction == 0 then
train_table[self.id][2].direction = 2
elseif train_table[self.id][2].direction == 1 then
train_table[self.id][2].direction = 3
elseif train_table[self.id][2].direction == 2 then
train_table[self.id][2].direction = 0
elseif train_table[self.id][2].direction == 3 then
train_table[self.id][2].direction = 1
end
end,
--serialized variables
id = nil,
direction = nil,
--deserialization
on_activate = function(self,staticdata)
self.object:set_armor_groups({immortal=1})
--set everything up
if self.id == nil then
self.id = train_id
end
--grouping
if not train_table[self.id] then
train_table[self.id] = {}
end
train_table[self.id][3] = self.object:get_luaentity()
--deserialization
if staticdata then
local data = minetest.deserialize(staticdata)
if data then
self.direction = data.direction
self.id = data.id
end
end
end,
--serialization and grouping
get_staticdata = function(self)
self.tmp = {
direction = self.direction,
id = self.id,
}
return minetest.serialize(self.tmp)
end,
on_step = function(self)
if train_table[self.id] ~= nil then
if train_table[self.id][1] ~= nil and train_table[self.id][2] ~= nil then
--this is pilzadam's mob code, nice work pilzadam, this wouldn't be possible without you!
--https://github.com/PilzAdam/mobs/blob/master/api.lua#L262
--position
local pos1 = train_table[self.id][1].object:getpos()
local pos2 = train_table[self.id][2].object:getpos()
print(round(math.abs(distance(pos1.x, pos1.z, pos2.x, pos2.z )),1))
local cpos = self.object:getpos()
local pos = {x=(pos2.x+pos1.x)/2, y=cpos.y, z=(pos2.z+pos1.z)/2}
self.object:moveto(pos, false)
--yaw
local vec = {x=pos2.x-pos1.x, y=0, z=pos2.z-pos1.z}
local yaw = math.atan(vec.z/vec.x)+math.pi/2
if pos2.x < pos1.x then
yaw = yaw + math.pi
end
self.object:setyaw(yaw)
end
end
end,
})
--########################################################################################################################################################
minetest.register_craftitem("trains:train", {
description = "Train",
inventory_image = "heli_inv.png",
wield_image = "heli_inv.png",
wield_scale = {x=1, y=1, z=1},
on_place = function(itemstack, placer, pointed_thing)
local pos = pointed_thing.under
if pointed_thing.type ~= "node" then
return
end
if minetest.get_node(pointed_thing.above).name ~= "air" then
return
end
if minetest.get_node_group(minetest.get_node(pointed_thing.under).name, "track") == 0 then
return
end
if minetest.get_node(pointed_thing.under).param2 == 0 then
--check if all straight rail
for i = -(truck_distance/2),(truck_distance/2) do
if minetest.get_node({x=pos.x,y=pos.y,z=pos.z+i}).name == "trains:track_straight" and minetest.get_node({x=pos.x,y=pos.y,z=pos.z+i}).param2 == 0 then
--do nothing
else
minetest.chat_send_player(placer:get_player_name(), "There needs to be at least "..truck_distance.." straight tracks to place this train")
return
end
end
train_id = train_id + 1
--this needs to be fixed to use rounding
local posib = {0,2}
local test = posib[math.random(1,2)]
if test == 0 then
local front = minetest.add_entity({x=pos.x,y=pos.y,z=pos.z+(truck_distance/2)}, "trains:truck_front")
local rear = minetest.add_entity({x=pos.x,y=pos.y,z=pos.z-(truck_distance/2)}, "trains:truck_rear")
front:get_luaentity().direction = test
rear:get_luaentity().direction = test
elseif test == 2 then
local front = minetest.add_entity({x=pos.x,y=pos.y,z=pos.z-(truck_distance/2)}, "trains:truck_front")
local rear = minetest.add_entity({x=pos.x,y=pos.y,z=pos.z+(truck_distance/2)}, "trains:truck_rear")
front:get_luaentity().direction = test
rear:get_luaentity().direction = test
end
local engine = minetest.add_entity({x=pos.x,y=pos.y+1,z=pos.z}, "trains:engine")
print("Train ID is now "..train_id)
local file=io.open(minetest.get_worldpath().."/train_id.txt","w")
file:write(train_id)
file:close(file)
end
if minetest.get_node(pointed_thing.under).param2 == 1 then
--check if all straight rail
for i = -(truck_distance/2),(truck_distance/2) do
if minetest.get_node({x=pos.x+i,y=pos.y,z=pos.z}).name == "trains:track_straight" and minetest.get_node({x=pos.x+i,y=pos.y,z=pos.z}).param2 == 1 then
--do nothing
else
minetest.chat_send_player(placer:get_player_name(), "There needs to be at least "..truck_distance.." straight tracks to place this train")
return
end
end
train_id = train_id + 1
local posib = {1,3}
local test = posib[math.random(1,2)]
if test == 1 then
local front = minetest.add_entity({x=pos.x+(truck_distance/2),y=pos.y,z=pos.z}, "trains:truck_front")
local rear = minetest.add_entity({x=pos.x-(truck_distance/2),y=pos.y,z=pos.z}, "trains:truck_rear")
front:get_luaentity().direction = test
rear:get_luaentity().direction = test
elseif test == 3 then
local front = minetest.add_entity({x=pos.x-(truck_distance/2),y=pos.y,z=pos.z}, "trains:truck_front")
local rear = minetest.add_entity({x=pos.x+(truck_distance/2),y=pos.y,z=pos.z}, "trains:truck_rear")
front:get_luaentity().direction = test
rear:get_luaentity().direction = test
end
local engine = minetest.add_entity({x=pos.x,y=pos.y+1,z=pos.z}, "trains:engine")
print("Train ID is now "..train_id)
local file=io.open(minetest.get_worldpath().."/train_id.txt","w")
file:write(train_id)
file:close(file)
end
end,
})
minetest.register_node("trains:track_straight", {
description = "Railroad Track",
tiles = {"default_steel_block.png"},
groups = {cracky=3,track=1},
--sounds = default.node_sound_stone_defaults(),
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
is_ground_content = true,
on_construct = function(pos)
if minetest.get_node(pos).param2 == 2 then
minetest.set_node(pos, {name="trains:track_straight", param2 = 0})
elseif minetest.get_node(pos).param2 == 3 then
minetest.set_node(pos, {name="trains:track_straight", param2 = 1})
end
end,
node_box = {
type = "fixed",
fixed = {
--rails
{0.4, -0.4, -0.5, 0.5, -0.2, 0.5},
{-0.5, -0.4, -0.5, -0.4, -0.2, 0.5},
--sleeper
{-0.7, -0.5, -0.2, 0.7, -0.4, 0.2},
},
},
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -0.2, 0.5},
},
})
minetest.register_node("trains:track_turn", {
description = "Railroad Track",
tiles = {"default_steel_block.png"},
groups = {cracky=3,track=2},
--sounds = default.node_sound_stone_defaults(),
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
is_ground_content = true,
node_box = {
type = "fixed",
fixed = {
{0.4, -0.4, -0.5, 0.5, -0.2, 0.5},
{-0.5, -0.4, -0.5, 0.5, -0.2, -0.4},
{-0.5, -0.4, 0.4, -0.4, -0.2, 0.5},
--sleeper
{-0.7, -0.5, -0.2, 0.7, -0.4, 0.2},
},
},
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -0.2, 0.5},
},
})