aboutsummaryrefslogtreecommitdiff
path: root/advtrains_line_automation/textures/advtrains_dtrack_stop_placer.png
blob: 0d1c7694ca19be3f4454eeaec1de847c6f700244 (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 38 00 00 00 39 08 06 00 00 00 63 da e8 .PNG........IHDR...8...9.....c..
0020 bb 00 00 00 06 62 4b 47 44 00 ff 00 ff 00 ff a0 bd a7 93 00 00 00 09 70 48 59 73 00 00 0b 13 00 .....bKGD..............pHYs.....
0040 00 0b 13 01 00 9a 9c 18 00 00 00 07 74 49 4d 45 07 e2 0b 1d 10 0e 0c c9 e8 6c af 00 00 04 63 49 ............tIME.........l....cI
0060 44 41 54 68 de ed da 5d 4c 5b 65 18 07 f0 ff db 16 4a 0a cc 41 4b dd 49 74 65 d0 e0 e2 6c b3 e1 DATh...]L[e......J..AK.Ite...l..
0080 dc 32 68 9b 9a a9 31 6c 93 60 1c 17 08 d1 64 99 b3 e2 cc b2 39 a3 92 2c 84 0b 34 c6 1b e2 fc 58 .2h...1l.`....d.....9..,..4....X
00a0 16 b2 25 ea cd 4c b6 69 74 13 3b 49 95 32 24 c6 35 d1 0c a2 89 3a ea c5 09 6d 85 ad c0 a0 d5 ed ..%..L.it.;I.2$.5....:...m......
00c0 f5 a2 9e ee f4 f4 f4 fb 83 41 de f7 0a 72 c8 e9 fb cb f3 7f 9f a7 a7 94 70 1c 87 d5 bc 14 58 e5 .........A...r..........p.....X.
00e0 8b 01 19 90 01 19 90 01 19 90 01 19 90 01 19 90 01 19 90 01 19 90 01 19 90 01 19 90 01 19 90 01 ................................
0100 0b b8 b6 3d b2 95 16 1b a8 2a c6 8b bc 72 b0 9b 5a ad 16 00 c0 f5 d9 eb 74 ff 01 07 59 91 c0 33 ...=.....*...r..Z.......t...Y..3
0120 3d b5 14 00 46 7e b9 01 00 20 86 0e 08 b0 f9 b9 79 7c e3 74 e2 e6 cd c5 95 19 d1 b6 1d e5 74 ca =...F~..........y|.t..........t.
0140 7f 1b 20 04 c4 d0 01 5b e7 07 b0 5a 2d 98 9f 9b c7 d9 b3 e7 10 f8 3b 80 87 1b 1b 31 37 17 5c 99 .......[...Z-.........;....17.\.
0160 c0 75 55 0a fc a5 de 03 62 ee 97 85 f9 7c 7e 54 ae 59 03 87 e3 45 74 75 76 14 ed 2c 92 6c fe 7d .uU.....b....|~T.Y...Etuv..,.l.}
0180 26 44 51 88 a3 34 8a 4f b6 ec c6 27 1f 9f 06 01 c1 b4 cf 07 a3 d1 88 ea ea 2a 84 42 61 8c 7f 76 &DQ..4.O...'.............*.Ba..v
01a0 08 ef 7d 31 4b ee 5a 60 db 8e 72 da 64 d2 c2 a0 57 e2 dd 21 2d 5e 3d 72 18 44 41 a2 30 e9 72 8f ..}1K.Z`..r.d...W..!-^=r.DA.0.r.
01c0 b8 10 0a 85 51 fa 6b 2f 40 29 c4 67 b4 18 50 55 b6 51 ac 35 5b 70 d4 7c a7 62 94 52 d0 ff 01 84 ....Q.k/@).g..PU.Q.5[p.|.b.R....
01e0 dc d9 f7 f0 e9 17 c0 cf dc 02 00 34 9b 75 30 e8 95 b0 9a ee 11 2e d3 42 43 93 56 30 9d 28 4a 17 ...........4.u0........BC.V0.(J.
0200 a5 34 06 c8 71 1c 01 00 47 4b 25 95 42 8b 51 d1 84 40 71 14 dd 37 5a 92 c2 c6 c7 81 ed db e5 5f .4..q...GK%.B.Q..@q..7Z........_
0220 a0 ff c0 83 31 1b 2f 36 34 69 05 c5 03 5a 98 63 8d 8d 5b d0 d9 f5 7c ca 1b 8f bc df 14 dd b8 b0 ....1./64i...Z.c..[...|.........
0240 f9 e5 80 12 8e e3 52 46 51 80 09 5d f1 f0 91 a3 29 6f ec 68 ab 8f db 78 26 50 6f 00 98 e2 73 87 ......RFQ..]....)o.h...x&Po...s.
0260 92 97 9e 36 52 95 92 60 af bd 26 b2 11 53 7f b4 2b 4a 61 e2 76 bf f3 b1 27 e2 a2 f8 e6 89 89 e8 ...6R..`..&..S..+Ja.v...'.......
0280 ef 7d fb 1a 00 00 fc cc ad 9c a0 e9 54 f4 d4 e0 49 aa 29 d7 c0 e5 fa 1e 1f 7e 74 82 c4 55 50 b8 .}..........T...I.)......~t..UP.
02a0 79 89 4a 01 d5 7d bb 50 52 52 02 93 69 93 2c ac b3 eb 39 22 fc 7d ba 1b 2f 14 f4 d4 e0 49 5a 51 y.J..}.PRR..i.,...9".}../....IZQ
02c0 59 11 b9 36 e2 86 4e ab c5 b1 de 3e 92 f0 0c be f1 fa 6b d4 ed 1e 45 eb 53 7b 52 0e 68 9e e7 69 Y..6..N....>......k...E.S{R.h..i
02e0 df be 06 e4 0a f5 06 22 d7 a7 f8 d4 50 af 9f 62 bd 0e 58 a8 eb 81 14 16 0a 87 e1 f1 78 70 e1 e2 ......."....P..b..X.........xp..
0300 10 49 da 64 da db 9f a1 76 9b 0d 35 fa 9a b8 01 2d b7 71 f1 66 f2 59 51 9e e7 a9 f4 4c b7 ee 1f .I.d....v..5....-.q.f.YQ....L...
0320 90 85 a9 d5 6a 4c 4f fb 60 b3 59 10 f0 fb d1 7d f0 10 49 ab 8b 8a a3 1b 3d a3 05 86 5a ba 2f 83 ....jLO.`.Y....}..I.....=...Z./.
0340 10 12 33 4f bf be f0 65 52 98 dd 6e 03 01 c1 4f 57 ae e0 ad b7 df 21 19 bf 55 cb 17 54 e8 8c 72 ..3O...eR..n...OW.....!..U..T..r
0360 d0 63 83 bf c5 cc 53 4a 29 86 2e 7e 95 36 6c 8b f2 8c 6c 33 ca e8 bd 68 21 2a 2a 8e a2 00 14 2a .c....SJ)..~.6l...l3...h!**....*
0380 36 3c ec 42 59 99 1a 55 6b d7 26 84 4d ba 8e cb 36 23 85 82 60 e0 fc 0c c9 ea 69 22 9f 50 eb cb 6<.BY..Uk.&.M...6#..`.....i".P..
03a0 63 d1 9f e5 a2 38 7a 79 0c cd cd 4d 09 a3 28 6d 46 b5 f7 aa f0 e7 f4 bf 08 ff 73 1b 97 7e f8 23 c....8zy...M..(mF.........s..~.#
03c0 bb c7 a5 54 50 b9 28 ca 41 bd 7e 8a 67 7b c7 12 9e 31 8f c7 83 0d 1b ea e2 a2 98 6c bc cc 06 97 ...TP.(.A.~.g{...1.........l....
03e0 60 32 56 e1 7e 9d 12 3f ff 1e cc 0d 98 6b 45 53 75 45 bb dd 06 a7 f3 12 b4 5a ad 6c 14 01 60 f4 `2V.~..?.....kESuE.......Z.l..`.
0400 6a 10 03 e7 67 a2 f7 df 6b ad a4 e1 a5 20 3e ff 31 d2 a1 48 3e bf 2f 9a 2e 54 6e 40 a7 ea 8a d2 j...g...k.....>.1..H>./..Tn@....
0420 ae 2b 8d a2 6b b2 94 e4 ed 89 3e 53 a8 d7 1f 81 6a b7 f5 64 d4 15 93 8d 97 89 6b 0b 31 51 fc f4 .+..k.....>S....j..d......k.1Q..
0440 bb a5 e2 01 13 41 bf f5 ed 84 46 a3 c9 b8 2b a6 13 c5 bc 7e 26 93 2d b4 bc ae 15 9b 37 9b b1 b8 .....A....F...+....~&.-.....7...
0460 b8 94 74 40 8b 9b 51 ba 51 5c 56 20 00 d4 1a d6 d3 fa 7a 23 36 6e 7c 20 ad 28 0a d0 89 6b 0b d0 ..t@..Q.Q\V.......z#6n|..(...k..
0480 57 97 61 6b 43 45 d2 28 2e 3b 50 58 26 d3 43 74 f7 ae 16 4c ba 8e cb 36 23 69 14 01 e0 f1 4d 61 W.akCE.(.;PX&.Ct...L...6#i....Ma
04a0 ea bc 5a 9a d5 33 21 59 ce 6f dd 8b cf 68 fb a3 fa 9c a2 78 57 02 f3 19 c5 84 40 9d 76 1d 56 f3 ..Z..3!Y.o...h.....xW.....@.v.V.
04c0 fa 0f 90 a6 de 01 97 53 46 e5 00 00 00 00 49 45 4e 44 ae 42 60 82 .......SF.....IEND.B`.
n276'>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 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
--trainlogic.lua
--controls train entities stuff about connecting/disconnecting/colliding trains and other things


local benchmark=false
local bm={}
local bmlt=0
local bmsteps=0
local bmstepint=200
atprintbm=function(action, ta)
	if not benchmark then return end
	local t=(os.clock()-ta)*1000
	if not bm[action] then
		bm[action]=t
	else
		bm[action]=bm[action]+t
	end
	bmlt=bmlt+t
end
function endstep()
	if not benchmark then return end
	bmsteps=bmsteps-1
	if bmsteps<=0 then
		bmsteps=bmstepint
		for key, value in pairs(bm) do
			minetest.chat_send_all(key.." "..(value/bmstepint).." ms avg.")
		end
		minetest.chat_send_all("Total time consumed by all advtrains actions per step: "..(bmlt/bmstepint).." ms avg.")
		bm={}
		bmlt=0
	end
end

--acceleration for lever modes (trainhud.lua), per wagon
local t_accel_all={
	[0] = -10,
	[1] = -3,
	[2] = -0.5,
	[4] = 0.5,
}
--acceleration per engine
local t_accel_eng={
	[0] = 0,
	[1] = 0,
	[2] = 0,
	[4] = 1.5,
}

advtrains.mainloop_trainlogic=function(dtime)
	--build a table of all players indexed by pts. used by damage and door system.
	advtrains.playersbypts={}
	for _, player in pairs(minetest.get_connected_players()) do
		if not advtrains.player_to_train_mapping[player:get_player_name()] then
			--players in train are not subject to damage
			local ptspos=minetest.pos_to_string(vector.round(player:getpos()))
			advtrains.playersbypts[ptspos]=player
		end
	end
	--regular train step
	-- do in two steps: 
	--  a: predict path and add all nodes to the advtrains.detector.on_node table
	--  b: check for collisions based on these data
	--  (and more)
	local t=os.clock()
	advtrains.detector.on_node={}
	for k,v in pairs(advtrains.trains) do
		advtrains.atprint_context_tid=sid(k)
		advtrains.atprint_context_tid_full=k
		advtrains.train_step_a(k, v, dtime)
	end
	for k,v in pairs(advtrains.trains) do
		advtrains.atprint_context_tid=sid(k)
		advtrains.atprint_context_tid_full=k
		advtrains.train_step_b(k, v, dtime)
	end
	
	advtrains.atprint_context_tid=nil
	advtrains.atprint_context_tid_full=nil
	
	atprintbm("trainsteps", t)
	endstep()
end

minetest.register_on_joinplayer(function(player)
	return advtrains.pcall(function()
		local pname=player:get_player_name()
		local id=advtrains.player_to_train_mapping[pname]
		if id then
			local train=advtrains.trains[id]
			if not train then advtrains.player_to_train_mapping[pname]=nil return end
			--set the player to the train position.
			--minetest will emerge the area and load the objects, which then will call reattach_all().
			--because player is in mapping, it will not be subject to dying.
			player:setpos(train.last_pos_prev)
			--independent of this, cause all wagons of the train which are loaded to reattach their players
			--needed because already loaded wagons won't call reattach_all()
			for _,wagon in pairs(minetest.luaentities) do
				if wagon.is_wagon and wagon.initialized and wagon.train_id==id then
					wagon:reattach_all()
				end
			end
		end
	end)
end)

minetest.register_on_dieplayer(function(player)
	return advtrains.pcall(function()
		local pname=player:get_player_name()
		local id=advtrains.player_to_train_mapping[pname]
		if id then
			local train=advtrains.trains[id]
			if not train then advtrains.player_to_train_mapping[pname]=nil return end
			for _,wagon in pairs(minetest.luaentities) do
				if wagon.is_wagon and wagon.initialized and wagon.train_id==id then
					--when player dies, detach him from the train
					--call get_off_plr on every wagon since we don't know which one he's on.
					wagon:get_off_plr(pname)
				end
			end
		end
	end)
end)
--[[
train step structure:


- legacy stuff
- preparing the initial path and creating index
- setting node coverage old indices
- handle velocity influences:
	- off-track
	- atc
	- player controls
	- environment collision
- update index = move
- create path
- call stay_node on all old positions to register train there, for collision system
- do less important stuff such as checking trainpartload or removing

-- break --
- Call enter_node and leave_node callbacks (required here because stay_node needs to be called on all trains first)
- handle train collisions

]]

function advtrains.train_step_a(id, train, dtime)
	--atprint("--- runcnt ",advtrains.mainloop_runcnt,": index",train.index,"end_index", train.end_index,"| max_iot", train.max_index_on_track, "min_iot", train.min_index_on_track, "<> pe_min", train.path_extent_min,"pe_max", train.path_extent_max)
	if train.min_index_on_track then
		assert(math.floor(train.min_index_on_track)==train.min_index_on_track)
	end
	--- 1. not exactly legacy. required now because of saving ---
	if not train.drives_on or not train.max_speed then
		advtrains.update_trainpart_properties(id)
	end
	--TODO check for all vars to be present
	if not train.velocity then
		train.velocity=0
	end
	if not train.movedir or (train.movedir~=1 and train.movedir~=-1) then
		train.movedir=1
	end
	--- 2. prepare initial path and index if needed ---
	if not train.index then train.index=0 end
	if not train.path then
		if not train.last_pos then
			--no chance to recover
			atwarn("Unable to restore train ",id,": missing last_pos")
			advtrains.trains[id]=nil
			return false
		end
		
		local node_ok=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(train.last_pos), train.drives_on)
		
		if node_ok==nil then
			--block not loaded, do nothing
			atprint("last_pos", advtrains.round_vector_floor_y(train.last_pos), "not loaded and not in ndb, waiting")
			return nil
		elseif node_ok==false then
			atprint("Unable to restore train ",id,": No rail at train's position")
			return false
		end
		
		if not train.last_pos_prev then
			--no chance to recover
			atwarn("Unable to restore train ",id,": missing last_pos_prev")
			advtrains.trains[id]=nil
			return false
		end
		
		local prevnode_ok=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(train.last_pos_prev), train.drives_on)
		
		if prevnode_ok==nil then
			--block not loaded, do nothing
			atprint("last_pos_prev", advtrains.round_vector_floor_y(train.last_pos_prev), "not loaded and not in ndb, waiting")
			return nil
		elseif prevnode_ok==false then
			atprint("Unable to restore train ",id,": No rail at train's position")
			return false
		end
		
		train.index=(train.restore_add_index or 0)+(train.savedpos_off_track_index_offset or 0)
		--restore_add_index is set by save() to prevent trains hopping to next round index. should be between -0.5 and 0.5
		--savedpos_off_track_index_offset is set if train went off track. see below.
		train.path={}
		train.path_dist={}
		train.path[0]=train.last_pos
		train.path[-1]=train.last_pos_prev
		train.path_dist[-1]=vector.distance(train.last_pos, train.last_pos_prev)
		train.path_extent_min=-1
		train.path_extent_max=0
		train.min_index_on_track=-1
		train.max_index_on_track=0
		
		--[[
		Bugfix for trains randomly ignoring ATC rails:
		- Paths have been invalidated. 1 gets executed and ensures an initial path
		- 2a sets train end index. The problem is that path_dist is not known for the whole path, so train end index will be nearly trainlen
		- Since the detector indices are also unknown, they get set to the new (wrong) train_end_index. Enter_node calls are not executed for the nodes that lie in between real end_index and trainlen.
		- The next step, mistake is recognized, train leaves some positions. From there, everything works again.
		To overcome this, we will generate the full required path here so that path_dist is available for get_train_end_index(). 
		]]
		advtrains.pathpredict(id, train)
	end
	
	--- 2a. set train.end_index which is required in different places, IF IT IS NOT SET YET by STMT afterwards. ---
	---   table entry to avoid triple recalculation ---
	if not train.end_index then
		train.end_index=advtrains.get_train_end_index(train)
	end
	
	--- 2b. set node coverage old indices ---
	
	train.detector_old_index = atround(train.index)
	train.detector_old_end_index = atround(train.end_index)
	
	--- 3. handle velocity influences ---
	local train_moves=(train.velocity~=0)
	local tarvel_cap
	
	if train.recently_collided_with_env then
		tarvel_cap=0
		train.active_control=false
		if not train_moves then
			train.recently_collided_with_env=nil--reset status when stopped
		end
	end
	if train.locomotives_in_train==0 then
		tarvel_cap=0
	end
	
	--- 3a. this can be useful for debugs/warnings and is used for check_trainpartload ---
	local t_info, train_pos=sid(id), train.path[atround(train.index)]
	if train_pos then
		t_info=t_info.." @"..minetest.pos_to_string(train_pos)
		--atprint("train_pos:",train_pos)
	end
	
	--apply off-track handling:
	local front_off_track=train.max_index_on_track and train.index>train.max_index_on_track
	local back_off_track=train.min_index_on_track and train.end_index<train.min_index_on_track
	local pprint
	
	if front_off_track and back_off_track then--allow movement in both directions
		tarvel_cap=1
	elseif front_off_track then--allow movement only backward
		if train.movedir==1 then
			tarvel_cap=0
		end
		if train.movedir==-1 then
			tarvel_cap=1
		end
	elseif back_off_track then--allow movement only forward
		if train.movedir==1 then
			tarvel_cap=1
		end
		if train.movedir==-1 then
			tarvel_cap=0
		end
	end
	
	--interpret ATC command and apply auto-lever control when not actively controlled