forked from PilzAdam/carts
/
init.lua
818 lines (726 loc) · 26.4 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
dofile(minetest.get_modpath("carts").."/functions.lua")
--
-- Cart entity
--
local cart = {
physical = false,
collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
visual = "mesh",
mesh = "cart.x",
visual_size = {x=1, y=1},
textures = {"cart.png"},
driver = nil,
velocity = {x=0, y=0, z=0},
old_pos = nil,
old_velocity = nil,
pre_stop_dir = nil,
MAX_V = 8, -- Limit of the velocity --
TARGET_TOUR_V = 4.5, -- target touring velocity
railcount=0, --stores how many rails a cart has passed over
--ignorekeypos stores a position where we changed direction because the
--player was pressing left or right keys. once we have changed direction,
--we want to ignore those keys until you have changed to a different rail
--(otherwise you end up reversing)
ignorekeypos=nil, -- so will not keep turning when pressing an arrow on track fork
lockyaw=false, -- is player view locked to cart.
yawtarget=nil, -- target yaw for player view when locked to cart
YAW_STEP=math.pi/12 -- step to turn player view when view locked to cart
--smaller YAW_STEP makes for smoother camera turning. BUT, takes much longer and can fall behind
}
function cart:on_rightclick(clicker)
if not clicker or not clicker:is_player() then
return
end
if self.driver and clicker == self.driver then
self.driver = nil
clicker:set_detach()
elseif not self.driver then
self.driver = clicker
clicker:set_attach(self.object, "", {x=0,y=5,z=0}, {x=0,y=0,z=0})
end
end
function cart:on_activate(staticdata, dtime_s)
self.object:set_armor_groups({immortal=1})
if staticdata then
local tmp = minetest.deserialize(staticdata)
if tmp then
self.velocity = tmp.velocity
end
if tmp and tmp.pre_stop_dir then
self.pre_stop_dir = tmp.pre_stop_dir
end
end
self.old_pos = self.object:getpos()
self.old_velocity = self.velocity
end
function cart:get_staticdata()
return minetest.serialize({
velocity = self.velocity,
pre_stop_dir = self.pre_stop_dir,
})
end
-- Remove the cart if holding a tool or accelerate it
function cart:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
if not puncher or not puncher:is_player() then
return
end
if puncher:get_player_control().sneak then
print("cart: end pos="..cart_func:pos_to_string(vector.round(self.object:getpos())).." railcount="..self.railcount)
self.object:remove()
local inv = puncher:get_inventory()
if minetest.setting_getbool("creative_mode") then
if not inv:contains_item("main", "carts:cart") then
inv:add_item("main", "carts:cart")
end
else
inv:add_item("main", "carts:cart")
end
return
end
--patch by spillz to allow punching a cart you are riding.
--if puncher == self.driver then
if puncher == self.driver and (math.abs(self.velocity.x)>1 or math.abs(self.velocity.z)>1) then
return
end
local d = cart_func:velocity_to_dir(direction)
if d.x==0 and d.z==0 then
local fd = minetest.dir_to_facedir(puncher:get_look_dir())
if fd == 0 then
d.x = 1
elseif fd == 1 then
d.z = -1
elseif fd == 2 then
d.x = -1
elseif fd == 3 then
d.z = 1
end
end
local s = self.velocity
if time_from_last_punch > tool_capabilities.full_punch_interval then
time_from_last_punch = tool_capabilities.full_punch_interval
end
local f = 4*(time_from_last_punch/tool_capabilities.full_punch_interval)
local v = {x=s.x+d.x*f, y=s.y, z=s.z+d.z*f}
if math.abs(v.x) < 6 and math.abs(v.z) < 6 then
self.velocity = v
else
if math.abs(self.velocity.x) < 6 and math.abs(v.x) >= 6 then
self.velocity.x = 6*cart_func:get_sign(self.velocity.x)
end
if math.abs(self.velocity.z) < 6 and math.abs(v.z) >= 6 then
self.velocity.z = 6*cart_func:get_sign(self.velocity.z)
end
end
end
-- Returns the direction as a unit vector
function cart:get_rail_direction(pos, dir)
--print("get_rail_direction top pos="..cart_func:pos_to_string(pos).." dir="..cart_func:pos_to_string(dir))
local fwd=cart_func.v3:copy(dir)
fwd.y=0
local up=cart_func.v3:copy(dir)
up.y=1
local down=cart_func.v3:copy(dir)
down.y=-1
-- figure out left and right
local left={x=0,y=0,z=0}
local right={x=0,y=0,z=0}
if dir.z ~= 0 and dir.x == 0 then
left.x=-dir.z --left is opposite sign from z
right.x=dir.z --right is same sign as z
elseif dir.x ~= 0 and dir.z == 0 then
left.z=dir.x --left is same sign as x
right.z=-dir.x --right is opposite sign from x
end --left and right
local leftdown=cart_func.v3:copy(left)
leftdown.y=-1
local rightdown=cart_func.v3:copy(right)
rightdown.y=-1
local ignorekeys=false
--ignorekeypos stores a position where we changed direction because the
--player was pressing left or right keys. once we have changed direction,
--we want to ignore those keys until you have changed to a different rail
--(otherwise you end up reversing)
--I use the ignorekeys boolean to make the key logic more readable
if self.ignorekeypos then --ignorekeypos was set
if cart_func.v3:equal(self.ignorekeypos,pos) then
ignorekeys=true --if still at same position, ignore left and right keys
else
self.ignorekeypos=nil --if ignorekeypos was set but pos does not match anymore, clear it
end
end
local ctrl=nil
if self.driver and not ignorekeys then
ctrl = self.driver:get_player_control()
end
--if player is pressing left or right arrows, check left or right first
if ctrl and ctrl.left then --left key pressed, check left first
if cart_func:check_rail_in_direction(pos,left) then
self.ignorekeypos=cart_func.v3:copy(pos) --ignore keys until pos changes
return left
elseif cart_func:check_rail_in_direction(pos,leftdown) then
self.ignorekeypos=cart_func.v3:copy(pos)
return leftdown
end
elseif ctrl and ctrl.right then --right key pressed, check right first
if cart_func:check_rail_in_direction(pos,right) then
self.ignorekeypos=cart_func.v3:copy(pos)
return right
elseif cart_func:check_rail_in_direction(pos,rightdown) then
self.ignorekeypos=cart_func.v3:copy(pos)
return rightdown
end
end --ctrl.left
--now for normal checks
if cart_func:check_rail_in_direction(pos,fwd) then
return fwd
elseif cart_func:check_rail_in_direction(pos,down) then
return down
elseif cart_func:check_rail_in_direction(pos,up) then
return up
elseif (not ctrl or not ctrl.left) --only check left if we didnt above
and cart_func:check_rail_in_direction(pos,left) then
return left
elseif (not ctrl or not ctrl.left)
and cart_func:check_rail_in_direction(pos,leftdown) then
return leftdown
elseif (not ctrl or not ctrl.right) --only check right if we didnt above
and cart_func:check_rail_in_direction(pos,right) then
return right
elseif (not ctrl or not ctrl.right)
and cart_func:check_rail_in_direction(pos,rightdown) then
return rightdown
else
return {x=0, y=0, z=0}
end --normal rail checks
end --get_rail_direction
function cart:calc_rail_direction(pos, vel)
local velocity = cart_func.v3:copy(vel)
local p = cart_func.v3:copy(pos)
if cart_func:is_int(p.x) and cart_func:is_int(p.z) then
local dir = cart_func:velocity_to_dir(velocity)
local dir_old = cart_func.v3:copy(dir)
dir = self:get_rail_direction(cart_func.v3:round(p), dir)
local v = math.max(math.abs(velocity.x), math.abs(velocity.z))
velocity = {
x = v * dir.x,
y = v * dir.y,
z = v * dir.z,
}
--if cart_func.v3:equal(velocity, {x=0, y=0, z=0}) then
if cart_func.v3:equal(velocity, {x=0, y=0, z=0}) and not cart_func:is_rail(p) then
--this loop tries to find the rail if we lost it moving up or down.
--it is almost always within 2 nodes, but SOMETIMES 3 or more away, and if you
--dont check those, then the cart gets stuck above or below the rail.
local upordown=1
if vel.y<0 then upordown=-1 end
for i=1,2 do
local deltay=0
while deltay<6 do
deltay=deltay+1
if cart_func:is_rail(cart_func.v3:add(p, {x=0, y=deltay*upordown, z=0})) then
p = cart_func.v3:add(p, {x=0, y=deltay*upordown, z=0})
--print("cart: off rails"..i.." old_pos="..cart_func:pos_to_string(self.old_pos).." old_vel="..cart_func:pos_to_string(old_velocity).." pos in="..cart_func:pos_to_string(pos).." vel in="..cart_func:pos_to_string(vel).." dir="..cart_func:pos_to_string(dir))
--print(" v="..v.." velocity="..cart_func:pos_to_string(velocity).." rail found at p="..cart_func:pos_to_string(p).." deltay="..deltay*upordown)
return self:calc_rail_direction(p,vel)
end --if
end --while
upordown=-upordown --if that direction failed, try the other direction.
end --for
--if we got through all of that
--print("cart: off rails old_pos="..cart_func:pos_to_string(self.old_pos).." old_vel="..cart_func:pos_to_string(old_velocity).." pos in="..cart_func:pos_to_string(pos).." vel in="..cart_func:pos_to_string(vel).." dir="..cart_func:pos_to_string(dir))
--print(" v="..v.." velocity="..cart_func:pos_to_string(velocity))
return {x=0, y=0, z=0}, p
end --hack
if not cart_func.v3:equal(dir, dir_old) then
return velocity, cart_func.v3:round(p)
end
end
return velocity, p
end --calc_rail_direction
function cart:on_step(dtime)
local pos = self.object:getpos()
local dir = cart_func:velocity_to_dir(self.velocity)
--minetest.chat_send_all("cart: on_step top pos="..cart_func:pos_to_string(pos).." dir="..cart_func:pos_to_string(dir).." vel="..cart_func:pos_to_string(self.velocity))
--print("cart: on_step top pos="..cart_func:pos_to_string(pos).." dir="..cart_func:pos_to_string(dir).." vel="..cart_func:pos_to_string(self.velocity))
--count rails to put in debug.txt
if self.old_pos then
local cmp_old=vector.round(self.old_pos)
local cmp_new=vector.round(pos)
if cmp_old.x~=cmp_new.x or cmp_old.y~=cmp_new.y or cmp_old.z~=cmp_new.z then
self.railcount=self.railcount+1
--local a = tonumber(minetest.env:get_meta(pos):get_string("cart_acceleration"))
--local railtype=""
--if a and a>0 then railtype="power" end
--minetest.chat_send_all("-- cart pos="..cart_func:pos_to_string(vector.round(pos)).." railcount="..self.railcount.." vel="..cart_func:pos_to_string(self.velocity).." "..railtype)
end
end
--check to see if we should lock or unlock player view to cart
local ctrl=nil
if self.driver then
ctrl = self.driver:get_player_control()
--if user pressed jump while driving cart, and lockyaw not set, then lock view to cart
if ctrl and ctrl.jump and not self.lockyaw then
self.lockyaw=true
self.yawtarget=self.object:getyaw()
minetest.chat_send_player(self.driver:get_player_name(),"cart: player view locked to cart, hit SNEAK to unlock")
--if user pressed sneak while driving cart, and lockyaw is set, then unlock view from cart
elseif ctrl and ctrl.sneak and self.lockyaw then
self.lockyaw=false
minetest.chat_send_player(self.driver:get_player_name(),"cart: player view NOT locked to cart, hit JUMP to lock")
end
end --check lockyaw if self.driver
if not cart_func.v3:equal(self.velocity, {x=0,y=0,z=0}) then
self.pre_stop_dir = cart_func:velocity_to_dir(self.velocity)
end
-- Stop the cart if the velocity is nearly 0
-- Only if on a flat railway
if dir.y == 0 then
if math.abs(self.velocity.x) < 0.1 and math.abs(self.velocity.z) < 0.1 then
-- Start the cart if powered from mesecons
local a = tonumber(minetest.env:get_meta(pos):get_string("cart_acceleration"))
if a and a ~= 0 then
if self.pre_stop_dir and cart_func.v3:equal(self:get_rail_direction(self.object:getpos(), self.pre_stop_dir), self.pre_stop_dir) then
self.velocity = {
x = self.pre_stop_dir.x * 0.2,
y = self.pre_stop_dir.y * 0.2,
z = self.pre_stop_dir.z * 0.2,
}
self.old_velocity = self.velocity
return
end
for _,y in ipairs({0,-1,1}) do
for _,z in ipairs({1,-1}) do
if cart_func.v3:equal(self:get_rail_direction(self.object:getpos(), {x=0, y=y, z=z}), {x=0, y=y, z=z}) then
self.velocity = {
x = 0,
y = 0.2*y,
z = 0.2*z,
}
self.old_velocity = self.velocity
return
end
end
for _,x in ipairs({1,-1}) do
if cart_func.v3:equal(self:get_rail_direction(self.object:getpos(), {x=x, y=y, z=0}), {x=x, y=y, z=0}) then
self.velocity = {
x = 0.2*x,
y = 0.2*y,
z = 0,
}
self.old_velocity = self.velocity
return
end
end
end
end
self.velocity = {x=0, y=0, z=0}
self.object:setvelocity(self.velocity)
self.old_velocity = self.velocity
self.old_pos = self.object:getpos()
return
end
end
--
-- Set the new moving direction
--
-- Recalcualte the rails that are passed since the last server step
local old_dir = cart_func:velocity_to_dir(self.old_velocity)
if old_dir.x ~= 0 then
local sign = cart_func:get_sign(pos.x-self.old_pos.x)
while true do
if sign ~= cart_func:get_sign(pos.x-self.old_pos.x) or pos.x == self.old_pos.x then
break
end
self.old_pos.x = self.old_pos.x + cart_func:get_sign(pos.x-self.old_pos.x)*0.1
self.old_pos.y = self.old_pos.y + cart_func:get_sign(pos.x-self.old_pos.x)*0.1*old_dir.y
self.old_velocity, self.old_pos = self:calc_rail_direction(self.old_pos, self.old_velocity)
old_dir = cart_func:velocity_to_dir(self.old_velocity)
if not cart_func.v3:equal(cart_func:velocity_to_dir(self.old_velocity), dir) then
self.velocity = self.old_velocity
pos = self.old_pos
self.object:setpos(self.old_pos)
break
end
end
elseif old_dir.z ~= 0 then
local sign = cart_func:get_sign(pos.z-self.old_pos.z)
while true do
if sign ~= cart_func:get_sign(pos.z-self.old_pos.z) or pos.z == self.old_pos.z then
break
end
self.old_pos.z = self.old_pos.z + cart_func:get_sign(pos.z-self.old_pos.z)*0.1
self.old_pos.y = self.old_pos.y + cart_func:get_sign(pos.z-self.old_pos.z)*0.1*old_dir.y
self.old_velocity, self.old_pos = self:calc_rail_direction(self.old_pos, self.old_velocity)
old_dir = cart_func:velocity_to_dir(self.old_velocity)
if not cart_func.v3:equal(cart_func:velocity_to_dir(self.old_velocity), dir) then
self.velocity = self.old_velocity
pos = self.old_pos
self.object:setpos(self.old_pos)
break
end
end
end
-- Calculate the new step
self.velocity, pos = self:calc_rail_direction(pos, self.velocity)
self.object:setpos(pos)
dir = cart_func:velocity_to_dir(self.velocity)
-- Accelerate or decelerate the cart according to the pitch and acceleration of the rail node
local a = tonumber(minetest.env:get_meta(pos):get_string("cart_acceleration"))
if not a then
a = 0
end
--touring rail adjustments
--this needs to be redone, if I moved this to AFTER the gravity section,
--then there would be no need for the messy logic to adjust for gravity here.
local t = tonumber(minetest.env:get_meta(pos):get_string("cart_touring_velocity"))
if not t then t=0 end
if t>0 then
local vx=math.abs(self.velocity.x)
local vy=math.abs(self.velocity.y)
local vz=math.abs(self.velocity.z)
-- make v the largest of the 3 velocities
local v=vx
if vy>v then v=vy end
if vz>v then v=vz end
--
local diff=0
local acelordecl=0
if v>t then
diff=v-t
acelordecl=-1
elseif v<t then
diff=t-v
acelordecl=1
end --v>t
--adjust for grav
if self.velocity.y<0 then --going downhill so grav will acel by extra 0.13
--if we are decel then add an extra 0.13 to how much we need to decel
--if we are accel then subtract an extra 0.13 from how much we need to acel
diff=diff-(0.13*acelordecl)
elseif self.velocity.y>0 then --going uphill grav will decl by extra 0.10
--if we are decel then subtract 0.1 from how much we need to decel
--if we are acel then add 0.1 to how much we need to acel
diff=diff+(0.1*acelordecl)
end -- self.velocity.y<0
--so now diff is the difference between cart velocity (after this turns grav)
--and our target touring velocity
if diff<a then --we dont want to over acel or decel
a=diff
elseif diff>a*4 then
a=a*2 --if big difference, play catchup fast!
elseif diff>a*3 then
a=a*1.5 --if big difference, play catchup fast!
end --diff<a
a=a*acelordecl
end -- if t>0
--check if down arrow is being pressed (hand break)
if self.driver then
local ctrl = self.driver:get_player_control()
if ctrl and ctrl.down then
a=a-0.1 --same as uphill
end --if hand break
end --if self.driver
if self.velocity.y < 0 then
self.velocity = {
x = self.velocity.x + (a+0.13)*cart_func:get_sign(self.velocity.x),
y = self.velocity.y + (a+0.13)*cart_func:get_sign(self.velocity.y),
z = self.velocity.z + (a+0.13)*cart_func:get_sign(self.velocity.z),
}
elseif self.velocity.y > 0 then
self.velocity = {
x = self.velocity.x + (a-0.1)*cart_func:get_sign(self.velocity.x),
y = self.velocity.y + (a-0.1)*cart_func:get_sign(self.velocity.y),
z = self.velocity.z + (a-0.1)*cart_func:get_sign(self.velocity.z),
}
else
self.velocity = {
x = self.velocity.x + (a-0.03)*cart_func:get_sign(self.velocity.x),
y = self.velocity.y + (a-0.03)*cart_func:get_sign(self.velocity.y),
z = self.velocity.z + (a-0.03)*cart_func:get_sign(self.velocity.z),
}
-- Place the cart exactly on top of the rail
if cart_func:is_rail(cart_func.v3:round(pos)) then
self.object:setpos({x=pos.x, y=math.floor(pos.y+0.5), z=pos.z})
pos = self.object:getpos()
end
end
-- Dont switch moving direction
-- Only if on flat railway
if dir.y == 0 then
if cart_func:get_sign(dir.x) ~= cart_func:get_sign(self.velocity.x) then
self.velocity.x = 0
end
if cart_func:get_sign(dir.y) ~= cart_func:get_sign(self.velocity.y) then
self.velocity.y = 0
end
if cart_func:get_sign(dir.z) ~= cart_func:get_sign(self.velocity.z) then
self.velocity.z = 0
end
end
-- Allow only one moving direction (multiply the other one with 0)
dir = cart_func:velocity_to_dir(self.velocity)
self.velocity = {
x = math.abs(self.velocity.x) * dir.x,
y = self.velocity.y,
z = math.abs(self.velocity.z) * dir.z,
}
-- Move cart exactly on the rail
if dir.x ~= 0 and not cart_func:is_int(pos.z) then
pos.z = math.floor(0.5+pos.z)
self.object:setpos(pos)
elseif dir.z ~= 0 and not cart_func:is_int(pos.x) then
pos.x = math.floor(0.5+pos.x)
self.object:setpos(pos)
end
-- Limit the velocity
if math.abs(self.velocity.x) > self.MAX_V then
self.velocity.x = self.MAX_V*cart_func:get_sign(self.velocity.x)
end
if math.abs(self.velocity.y) > self.MAX_V then
self.velocity.y = self.MAX_V*cart_func:get_sign(self.velocity.y)
end
if math.abs(self.velocity.z) > self.MAX_V then
self.velocity.z = self.MAX_V*cart_func:get_sign(self.velocity.z)
end
self.object:setvelocity(self.velocity)
self.old_pos = self.object:getpos()
self.old_velocity = cart_func.v3:copy(self.velocity)
local oldyaw=self.object:getyaw()
if dir.x < 0 then
self.object:setyaw(math.pi/2)
elseif dir.x > 0 then
self.object:setyaw(3*math.pi/2)
elseif dir.z < 0 then
self.object:setyaw(math.pi)
elseif dir.z > 0 then
self.object:setyaw(0)
end
local newyaw=self.object:getyaw()
--now if driver and lockyaw, change drivers viewing direction.
if self.driver and self.lockyaw then
if oldyaw~=newyaw then
self.yawtarget=newyaw --set new target
end
local playeryaw=self.driver:get_look_yaw()-1.57
if playeryaw<0 then playeryaw=playeryaw+(math.pi*2) end
if self.yawtarget and playeryaw ~= self.yawtarget then
local diff = self.yawtarget - playeryaw
if diff>math.pi then
diff=diff-(2*math.pi)
elseif diff<(-math.pi) then
diff=diff+(2*math.pi)
end
yawdir=cart_func:get_sign(diff)
local step=self.YAW_STEP
if math.abs(diff)<=self.YAW_STEP then
step=diff
self.yawtarget=nil
end
local setyaw=playeryaw+(step*yawdir)
self.driver:set_look_yaw(setyaw)
end --move yaw
end --lockyaw set
if dir.y == -1 then
self.object:set_animation({x=1, y=1}, 1, 0)
elseif dir.y == 1 then
self.object:set_animation({x=2, y=2}, 1, 0)
else
self.object:set_animation({x=0, y=0}, 1, 0)
end
end --on_step
minetest.register_entity("carts:cart", cart)
minetest.register_craftitem("carts:cart", {
description = "Minecart",
inventory_image = minetest.inventorycube("cart_top.png", "cart_side.png", "cart_side.png"),
wield_image = "cart_side.png",
on_place = function(itemstack, placer, pointed_thing)
if not pointed_thing.type == "node" then
return
end
if cart_func:is_rail(pointed_thing.under) then
local obj=minetest.env:add_entity(pointed_thing.under, "carts:cart")
print("cart: begin pos="..cart_func:pos_to_string(vector.round(obj:getpos())))
minetest.chat_send_player(placer:get_player_name(),"cart: right click to ride, left click to push, SNEAK-left click to put back in inventory, JUMP to lock view to cart, SNEAK to unlock view from cart, LEFT and RIGHT ARROWS to switch tracks, DOWN ARROW to apply hand break")
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
return itemstack
elseif cart_func:is_rail(pointed_thing.above) then
minetest.env:add_entity(pointed_thing.above, "carts:cart")
print("cart: begin pos="..cart_func:pos_to_string(self.object:getpos()))
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
return itemstack
end
end,
})
minetest.register_craft({
output = "carts:cart",
recipe = {
{"", "", ""},
{"default:steel_ingot", "", "default:steel_ingot"},
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
},
})
--
-- Mesecon support
--
minetest.register_node(":default:rail", {
description = "Rail",
drawtype = "raillike",
tiles = {"default_rail.png", "default_rail_curved.png", "default_rail_t_junction.png", "default_rail_crossing.png"},
inventory_image = "default_rail.png",
wield_image = "default_rail.png",
paramtype = "light",
is_ground_content = true,
walkable = false,
selection_box = {
type = "fixed",
-- but how to specify the dimensions for curved and sideways rails?
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups = {bendy=2,snappy=1,dig_immediate=2,attached_node=1,rail=1,connect_to_raillike=1},
})
minetest.register_node("carts:powerrail", {
description = "Powered Rail",
drawtype = "raillike",
tiles = {"carts_rail_pwr.png", "carts_rail_curved_pwr.png", "carts_rail_t_junction_pwr.png", "carts_rail_crossing_pwr.png"},
inventory_image = "carts_rail_pwr.png",
wield_image = "carts_rail_pwr.png",
paramtype = "light",
is_ground_content = true,
walkable = false,
selection_box = {
type = "fixed",
-- but how to specify the dimensions for curved and sideways rails?
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups = {bendy=2,snappy=1,dig_immediate=2,attached_node=1,rail=1,connect_to_raillike=1},
after_place_node = function(pos, placer, itemstack)
if not mesecon then
minetest.env:get_meta(pos):set_string("cart_acceleration", "0.5")
end
end,
mesecons = {
effector = {
action_on = function(pos, node)
minetest.env:get_meta(pos):set_string("cart_acceleration", "0.5")
end,
action_off = function(pos, node)
minetest.env:get_meta(pos):set_string("cart_acceleration", "0")
end,
},
},
})
minetest.register_node("carts:brakerail", {
description = "Brake Rail",
drawtype = "raillike",
tiles = {"carts_rail_brk.png", "carts_rail_curved_brk.png", "carts_rail_t_junction_brk.png", "carts_rail_crossing_brk.png"},
inventory_image = "carts_rail_brk.png",
wield_image = "carts_rail_brk.png",
paramtype = "light",
is_ground_content = true,
walkable = false,
selection_box = {
type = "fixed",
-- but how to specify the dimensions for curved and sideways rails?
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups = {bendy=2,snappy=1,dig_immediate=2,attached_node=1,rail=1,connect_to_raillike=1},
after_place_node = function(pos, placer, itemstack)
if not mesecon then
minetest.env:get_meta(pos):set_string("cart_acceleration", "-0.2")
end
end,
mesecons = {
effector = {
action_on = function(pos, node)
minetest.env:get_meta(pos):set_string("cart_acceleration", "-0.2")
end,
action_off = function(pos, node)
minetest.env:get_meta(pos):set_string("cart_acceleration", "0")
end,
},
},
})
minetest.register_node("carts:touringrail", {
description = "Touring Rail",
drawtype = "raillike",
tiles = {"carts_rail_tour.png", "carts_rail_curved_tour.png", "carts_rail_t_junction_tour.png", "carts_rail_crossing_tour.png"},
inventory_image = "carts_rail_tour.png",
wield_image = "carts_rail_tour.png",
paramtype = "light",
is_ground_content = true,
walkable = false,
selection_box = {
type = "fixed",
-- but how to specify the dimensions for curved and sideways rails?
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups = {bendy=2,snappy=1,dig_immediate=2,attached_node=1,rail=1,connect_to_raillike=1},
after_place_node = function(pos, placer, itemstack)
if not mesecon then
minetest.env:get_meta(pos):set_string("cart_acceleration", "0.5")
minetest.env:get_meta(pos):set_string("cart_touring_velocity", cart.TARGET_TOUR_V)
end
end,
mesecons = {
effector = {
action_on = function(pos, node)
minetest.env:get_meta(pos):set_string("cart_acceleration", "0.5")
end,
action_off = function(pos, node)
minetest.env:get_meta(pos):set_string("cart_acceleration", "0")
end,
},
},
})
minetest.register_craft({
output = "carts:powerrail 2",
recipe = {
{"default:steel_ingot", "default:mese_crystal_fragment", "default:steel_ingot"},
{"default:steel_ingot", "default:stick", "default:steel_ingot"},
{"default:steel_ingot", "", "default:steel_ingot"},
}
})
minetest.register_craft({
output = "carts:powerrail 2",
recipe = {
{"default:steel_ingot", "", "default:steel_ingot"},
{"default:steel_ingot", "default:stick", "default:steel_ingot"},
{"default:steel_ingot", "default:mese_crystal_fragment", "default:steel_ingot"},
}
})
minetest.register_craft({
output = "carts:brakerail 2",
recipe = {
{"default:steel_ingot", "default:coal_lump", "default:steel_ingot"},
{"default:steel_ingot", "default:stick", "default:steel_ingot"},
{"default:steel_ingot", "", "default:steel_ingot"},
}
})
minetest.register_craft({
output = "carts:brakerail 2",
recipe = {
{"default:steel_ingot", "", "default:steel_ingot"},
{"default:steel_ingot", "default:stick", "default:steel_ingot"},
{"default:steel_ingot", "default:coal_lump", "default:steel_ingot"},
}
})
minetest.register_craft({
output = "carts:touringrail 7",
recipe = {
{"default:steel_ingot", "default:coal_lump", "default:steel_ingot"},
{"default:steel_ingot", "default:stick", "default:steel_ingot"},
{"default:steel_ingot", "default:mese_crystal_fragment", "default:steel_ingot"},
}
})
minetest.register_craft({
output = "carts:touringrail 7",
recipe = {
{"default:coal_lump"},
{"carts:powerrail"},
{"carts:powerrail"},
}
})