aboutsummaryrefslogtreecommitdiff
path: root/textures/advtrains_signal_inv.png
blob: ed64ed91127a3b3efd5d1ff285f1c32262d8713c (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 20 00 00 00 20 08 06 00 00 00 73 7a 7a .PNG........IHDR.............szz
0020 f4 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 e0 09 0d 0f 0e 23 12 52 10 4f 00 00 02 e5 49 ............tIME......#.R.O....I
0060 44 41 54 58 c3 ed 97 bd 6b 14 41 18 87 9f 99 fd 4a 6e 77 f3 61 02 46 31 28 44 88 04 0b f5 40 49 DATX....k.A.....Jnw.a.F1(D....@I
0080 40 b4 d1 42 51 52 a8 85 f9 13 2c 6d fd 03 2c 6c 23 06 1b 11 14 6d 2c ac ec 82 a0 39 50 16 d2 88 @..BQR....,m..,l#....m,....9P...
00a0 58 08 49 4c 11 72 e4 62 92 fb da bd dd 1d 9b 53 2f c9 5e 2e 97 bb eb 32 30 cd ec cc fb 3e f3 7b X.IL.r.b.......S/.^....20....>.{
00c0 df 9d 79 47 90 d0 ee 4f 4f f1 fa c1 2b 26 a7 ee f6 ff fa b1 fc 08 45 0a f0 81 2c 20 aa d3 04 50 ..yG...OO...+&........E...,....P
00e0 00 d6 6b c6 00 f2 c0 46 75 4c 02 8b 9e 97 f9 96 4e 8f e3 79 99 5d be f4 24 80 ef cf 7f 02 b0 ba ..k....FuL......N..y.]..$.......
0100 94 7d 81 e2 36 ad b5 62 3a 3d 7e c3 f3 32 1f 93 20 44 bd 55 97 ae 5c 7e 12 6e 85 0f 85 14 2d 79 .}..6..b:=~..2...D.U..\~.n....-y
0120 57 4a 01 6c 01 63 9e 97 59 de f9 7d 97 f5 74 7a 1c e0 1e f0 16 c0 71 5c a4 94 f5 0c 27 8c d7 18 WJ.l.c..Y..}..tz......q\....'...
0140 17 50 2a 15 09 c3 90 6a 58 ae 7a 5e 66 be 76 be 4c b0 21 80 89 ff 46 92 15 10 42 24 76 29 6b bb .P*....jX.z^f.v.L.!...F...B$v)k.
0160 ac 5d df 0b bc 4f a7 c7 bb 1b 01 00 a8 bd 64 15 42 a0 54 4c 04 c4 52 36 13 91 61 40 6b 98 84 8d .]...O........d.B.TL..R6..a@k...
0180 62 ba b6 b6 4a 3e a8 30 ea 97 19 48 d9 2c f4 1f c1 34 ad ba 61 d9 6b 63 7a a3 9d 26 39 97 e5 12 b...J>.0...H.,...4..a.kcz..&9...
01a0 d3 cb 8b 5c 28 95 d0 80 37 27 4e 32 73 7c 98 41 db 69 68 63 67 6b 4a bf 28 8a c8 57 42 ee e4 d6 ...\(...7'N2s|.A.ihcgkJ.(..WB...
01c0 38 5b 2a 51 00 36 81 5b cb 8b d8 7e 99 28 8a ea a4 54 9b 00 a4 94 98 a9 14 23 7e 79 9b 96 0a 98 8[*Q.6.[...~.(...T.......#~y....
01e0 f8 bd 5e 07 80 f6 02 1c ed 1f e0 6b 6f 1f c6 0e 80 b1 62 01 4d d7 3b 0b 00 a0 c5 31 f3 23 a3 cc ..^........ko.....b.M.;....1.#..
0200 f5 f4 11 48 49 a0 69 2c d8 2e 2f 4f 8f 22 0f 70 68 e9 07 3a de 34 9d a7 e7 2f 32 5c d8 22 52 b0 ...HI.i,../O.".ph..:.4.../2\."R.
0220 64 3b 08 29 11 b4 1d a0 be c1 58 4a 16 dd de 83 c9 b8 df 10 88 d6 ae 81 ce e4 c0 21 c0 21 40 b3 d;.)......XJ...............!.!@.
0240 67 7d c7 7e c3 28 8a 28 97 8b 04 41 80 65 75 d1 d5 d5 9d 58 b8 74 44 01 a5 20 8e 63 4c d3 c2 b2 g}.~.(.(...A.eu....X.tD....cL...
0260 2c d6 d7 73 e4 72 d9 5d d7 b1 52 aa 0a a5 da 07 10 86 15 b2 d9 15 4c d3 c4 30 4c 6c db 65 68 e8 ,..s.r.]..R...........L..0Ll.eh.
0280 18 be ef 13 04 7e 67 15 50 4a 91 cb ad 61 59 d6 b6 31 4d d3 71 9c 1e 82 20 68 3a 27 9a bc 0b 14 .....~g.PJ...aY..1M.q....h:'....
02a0 b6 ed 24 ee 34 95 4a 21 a5 b6 9f aa e8 e0 0a 08 21 71 1c 17 d7 ed 61 73 73 83 38 8e 11 42 10 86 ..$.4.J!........!q....ass.8..B..
02c0 15 0c c3 40 d3 b4 f6 fe 05 7f 2b dd 9d 61 d0 75 03 c7 d1 28 14 0a 00 d8 b6 d3 b0 f2 69 ef 75 5c ...@......+..a.u...(........i.u\
02e0 55 c3 75 7b f6 7c 23 74 14 a0 55 c7 8d 00 34 da d7 62 a5 c8 0b 81 aa 3e 5c 55 a3 24 54 c0 27 14 U.u{.|#t..U...4..b.....>\U.$T.'.
0300 cf 52 83 5d 5f 5a f5 6e 18 c6 4c 10 f8 a7 80 33 c0 39 a0 b8 ef 9a f9 da f5 9b 93 61 25 7a 07 88 .R.]_Z.n..L....3.9.........a%z..
0320 48 84 ff d0 23 42 a4 90 d8 ba bb ed f9 25 a5 8c f2 f9 ad cf 42 08 03 58 a9 2a fc 78 76 f6 c3 5c H...#B.......%......B..X.*.xv..\
0340 3d 1f 7f 00 3b aa 0a 95 b8 1f 19 ac 00 00 00 00 49 45 4e 44 ae 42 60 82 =...;...........IEND.B`.
n195' href='#n195'>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
--trainlogic.lua
--controls train entities stuff about connecting/disconnecting/colliding trains and other things

local print=function(t, ...) minetest.log("action", table.concat({t, ...}, " ")) minetest.chat_send_all(table.concat({t, ...}, " ")) end

local benchmark=false
--printbm=function(str, t) print("[advtrains]"..str.." "..((os.clock()-t)*1000).."ms") end
local bm={}
local bmlt=0
local bmsteps=0
local bmstepint=200
printbm=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

advtrains.train_accel_force=2--per second and divided by number of wagons
advtrains.train_brake_force=3--per second, not divided by number of wagons
advtrains.train_emerg_force=10--for emergency brakes(when going off track)

advtrains.audit_interval=30

advtrains.all_traintypes={}
function advtrains.register_train_type(name, drives_on, max_speed)
	advtrains.all_traintypes[name]={}
	advtrains.all_traintypes[name].drives_on=drives_on
	advtrains.all_traintypes[name].max_speed=max_speed or 10
end


advtrains.trains={}
advtrains.wagon_save={}

--load initially
advtrains.fpath=minetest.get_worldpath().."/advtrains"
local file, err = io.open(advtrains.fpath, "r")
if not file then
	local er=err or "Unknown Error"
	print("[advtrains]Failed loading advtrains save file "..er)
else
	local tbl = minetest.deserialize(file:read("*a"))
	if type(tbl) == "table" then
		advtrains.trains=tbl
	end
	file:close()
end
advtrains.fpath_ws=minetest.get_worldpath().."/advtrains_wagon_save"
local file, err = io.open(advtrains.fpath_ws, "r")
if not file then
	local er=err or "Unknown Error"
	print("[advtrains]Failed loading advtrains save file "..er)
else
	local tbl = minetest.deserialize(file:read("*a"))
	if type(tbl) == "table" then
		advtrains.wagon_save=tbl
	end
	file:close()
end


advtrains.save = function()
	--print("[advtrains]saving")
	advtrains.invalidate_all_paths()
	local datastr = minetest.serialize(advtrains.trains)
	if not datastr then
		minetest.log("error", "[advtrains] Failed to serialize train data!")
		return
	end
	local file, err = io.open(advtrains.fpath, "w")
	if err then
		return err
	end
	file:write(datastr)
	file:close()
	
	-- update wagon saves
	for _,wagon in pairs(minetest.luaentities) do
		if wagon.is_wagon and wagon.initialized then
			advtrains.wagon_save[wagon.unique_id]=advtrains.merge_tables(wagon)--so, will only copy non_metatable elements
		end
	end
	--cross out userdata
	for w_id, data in pairs(advtrains.wagon_save) do
		data.name=nil
		data.object=nil
		if data.driver then
			data.driver_name=data.driver:get_player_name()
			data.driver=nil
		else
			data.driver_name=nil
		end
		if data.discouple then
			data.discouple.object:remove()
			data.discouple=nil
		end
	end
	--print(dump(advtrains.wagon_save))
	datastr = minetest.serialize(advtrains.wagon_save)
	if not datastr then
		minetest.log("error", "[advtrains] Failed to serialize train data!")
		return
	end
	file, err = io.open(advtrains.fpath_ws, "w")
	if err then
		return err
	end
	file:write(datastr)
	file:close()
	
	advtrains.save_trackdb()
end
minetest.register_on_shutdown(advtrains.save)

advtrains.save_and_audit_timer=advtrains.audit_interval
minetest.register_globalstep(function(dtime)
	advtrains.save_and_audit_timer=advtrains.save_and_audit_timer-dtime
	if advtrains.save_and_audit_timer<=0 then
		local t=os.clock()
		--print("[advtrains] audit step")
		--clean up orphaned trains
		for k,v in pairs(advtrains.trains) do
			--advtrains.update_trainpart_properties(k)
			if #v.trainparts==0 then
				advtrains.trains[k]=nil
			end
		end
		--save
		advtrains.save()
		advtrains.save_and_audit_timer=advtrains.audit_interval
		printbm("saving", t)
	end
	--regular train step
	local t=os.clock()
	for k,v in pairs(advtrains.trains) do
		advtrains.train_step(k, v, dtime)
	end
	printbm("trainsteps", t)
	endstep()
end)

function advtrains.train_step(id, train, dtime)
	
	--TODO check for all vars to be present
	if not train.velocity then
		train.velocity=0
	end
	--very unimportant thing: check if couple is here
	if train.couple_eid_front and (not minetest.luaentities[train.couple_eid_front] or not minetest.luaentities[train.couple_eid_front].is_couple) then train.couple_eid_front=nil end
	if train.couple_eid_back and (not minetest.luaentities[train.couple_eid_back] or not minetest.luaentities[train.couple_eid_back].is_couple) then train.couple_eid_back=nil end
	
	--skip certain things (esp. collision) when not moving
	local train_moves=(train.velocity~=0)
	
	--if not train.last_pos then advtrains.trains[id]=nil return end
	
	if not advtrains.pathpredict(id, train) then 
		print("pathpredict failed(returned false)")
		train.velocity=0
		train.tarvelocity=0
		return
	end
	
	local path=advtrains.get_or_create_path(id, train)
	if not path then
		train.velocity=0
		train.tarvelocity=0
		print("train has no path for whatever reason")
		return 
	end
	
	local train_end_index=advtrains.get_train_end_index(train)
	--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
	if front_off_track and back_off_track then--allow movement in both directions
		if train.tarvelocity>1 then train.tarvelocity=1 end
		if train.tarvelocity<-1 then train.tarvelocity=-1 end
	elseif front_off_track then--allow movement only backward
		if train.tarvelocity>0 then train.tarvelocity=0 end
		if train.tarvelocity<-1 then train.tarvelocity=-1 end
	elseif back_off_track then--allow movement only forward
		if train.tarvelocity>1 then train.tarvelocity=1 end
		if train.tarvelocity<0 then train.tarvelocity=0 end
	end
	
	if train_moves then
		--check for collisions by finding objects
		--front
		local search_radius=4
		
		--coupling
		local couple_outward=1
		local posfront=advtrains.get_real_index_position(path, train.index+couple_outward)
		local posback=advtrains.get_real_index_position(path, train_end_index-couple_outward)
		for _,pos in ipairs({posfront, posback}) do
			if pos then
				local objrefs=minetest.get_objects_inside_radius(pos, search_radius)
				for _,v in pairs(objrefs) do
					local le=v:get_luaentity()
					if le and le.is_wagon and le.initialized and le.train_id~=id then
						advtrains.try_connect_trains(id, le.train_id)
					end
				end
			end
		end
		--new train collisions (only search in the direction of the driving train)
		local coll_search_radius=2
		local coll_grace=0
		local collpos
		if train.velocity>0 then
			collpos=advtrains.get_real_index_position(path, train.index-coll_grace)
		elseif train.velocity<0 then
			collpos=advtrains.get_real_index_position(path, train_end_index+coll_grace)
		end
		if collpos then
			local objrefs=minetest.get_objects_inside_radius(collpos, coll_search_radius)
			for _,v in pairs(objrefs) do
				local le=v:get_luaentity()
				if le and le.is_wagon and le.initialized and le.train_id~=id then
					train.recently_collided_with_env=true
					train.velocity=-0.5*train.velocity
					train.tarvelocity=0
				end
			end
		end
	end
	--check for any trainpart entities if they have been unloaded. do this only if train is near a player, to not spawn entities into unloaded areas
	train.check_trainpartload=(train.check_trainpartload or 0)-dtime
	local node_range=(math.max((minetest.setting_get("active_block_range") or 0),1)*16)
	if train.check_trainpartload<=0 and posfront and posback then
		--print(minetest.pos_to_string(posfront))
		local should_check=false
		for _,p in ipairs(minetest.get_connected_players()) do
			should_check=should_check or ((vector.distance(posfront, p:getpos())<node_range) and (vector.distance(posback, p:getpos())<node_range))
		end
		if should_check then
			--it is better to iterate luaentites only once
			--print("check_trainpartload")
			local found_uids={}
			for _,wagon in pairs(minetest.luaentities) do
				if wagon.is_wagon and wagon.initialized and wagon.train_id==id then
					if found_uids[wagon.unique_id] then
						--duplicate found, delete it
						if wagon.object then wagon.object:remove() end
					else
						found_uids[wagon.unique_id]=true
					end
				end
			end
			--print("found_uids: "..dump(found_uids))
			--now iterate trainparts and check. then cross them out to see if there are wagons over for any reason
			for pit, w_id in ipairs(train.trainparts) do
				if found_uids[w_id] then
					--print(w_id.." still loaded")
				elseif advtrains.wagon_save[w_id] then
					--print(w_id.." not loaded, but save available")
					--spawn a new and initialize it with the properties from wagon_save
					local le=minetest.env:add_entity(posfront, advtrains.wagon_save[w_id].entity_name):get_luaentity()
					for k,v in pairs(advtrains.wagon_save[w_id]) do
						le[k]=v
					end
					advtrains.wagon_save[w_id].name=nil
					advtrains.wagon_save[w_id].object=nil
				else
					print(w_id.." not loaded and no save available")
					--what the hell...
					table.remove(train.trainparts, pit)
				end
			end
		end
		train.check_trainpartload=10
	end
	
	
	--handle collided_with_env
	if train.recently_collided_with_env then
		train.tarvelocity=0
		if not train_moves then
			train.recently_collided_with_env=false--reset status when stopped
		end
	end
	if train.locomotives_in_train==0 then
		train.tarvelocity=0
	end
	--apply tarvel(but with physics in mind!)
	if train.velocity~=train.tarvelocity then
		local applydiff=0
		local mass=#train.trainparts
		local diff=math.abs(train.tarvelocity)-math.abs(train.velocity)
		if diff>0 then--accelerating, force will be brought on only by locomotives.
			--print("accelerating with default force")
			applydiff=(math.min((advtrains.train_accel_force*train.locomotives_in_train*dtime)/mass, math.abs(diff)))
		else--decelerating
			if front_off_track or back_off_track or train.recently_collided_with_env then --every wagon has a brake, so not divided by mass.
				--print("braking with emergency force")
				applydiff=(math.min((advtrains.train_emerg_force*dtime), math.abs(diff)))
			else
				--print("braking with default force")
				applydiff=(math.min((advtrains.train_brake_force*dtime), math.abs(diff)))
			end
		end
		train.last_accel=(applydiff*math.sign(train.tarvelocity-train.velocity))
		train.velocity=train.velocity+train.last_accel
	else
		train.last_accel=0
	end
	
	--move
	train.index=train.index and train.index+((train.velocity/(train.path_dist[math.floor(train.index)] or 1))*dtime) or 0
	
end

--the 'leader' concept has been overthrown, we won't rely on MT's "buggy object management"
--structure of train table:
--[[
trains={
	[train_id]={
		trainparts={
			[n]=wagon_id
		}
		path={path}
		velocity
		tarvelocity
		index
		trainlen
		path_inv_level
		last_pos       |
		last_dir       | for pathpredicting.
	}
}
--a wagon itself has the following properties:
wagon={
	unique_id
	train_id
	pos_in_train (is index difference, including train_span stuff)
	pos_in_trainparts (is index in trainparts tabel of trains)
}
inherited by metatable:
wagon_proto={
	wagon_span
}
]]

--returns new id
function advtrains.create_new_train_at(pos, pos_prev, traintype)
	local newtrain_id=os.time()..os.clock()
	while advtrains.trains[newtrain_id] do newtrain_id=os.time()..os.clock() end--ensure uniqueness(will be unneccessary)
	
	advtrains.trains[newtrain_id]={}
	advtrains.trains[newtrain_id].last_pos=pos
	advtrains.trains[newtrain_id].last_pos_prev=pos_prev
	advtrains.trains[newtrain_id].traintype=traintype
	advtrains.trains[newtrain_id].tarvelocity=0
	advtrains.trains[newtrain_id].velocity=0
	advtrains.trains[newtrain_id].trainparts={}
	return newtrain_id
end

--returns false on failure. handle this case!
function advtrains.pathpredict(id, train)
	
	--print("pos ",x,y,z)
	--::rerun::
	if not train.index then train.index=0 end
	if not train.path or #train.path<2 then
		if not train.last_pos then
			--no chance to recover
			print("[advtrains]train hasn't saved last-pos, removing train.")
			advtrains.train[id]=nil
			return false
		end
		
		local node_ok=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(train.last_pos), train.traintype)
		
		if node_ok==nil then
			--block not loaded, do nothing
			return nil
		elseif node_ok==false then
			print("[advtrains]no track here, (fail) removing train.")
			advtrains.trains[id]=nil
			return false
		end
		
		if not train.last_pos_prev then
			--no chance to recover
			print("[advtrains]train hasn't saved last-pos_prev, removing train.")
			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.traintype)
		
		if prevnode_ok==nil then
			--block not loaded, do nothing
			return nil
		elseif prevnode_ok==false then
			print("[advtrains]no track at prev, (fail) removing train.")
			advtrains.trains[id]=nil
			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)
	end
	
	local maxn=advtrains.maxN(train.path)
	while (maxn-train.index) < 2 do--pregenerate
		--print("[advtrains]maxn conway for ",maxn,minetest.pos_to_string(path[maxn]),maxn-1,minetest.pos_to_string(path[maxn-1]))
		local conway=advtrains.conway(train.path[maxn], train.path[maxn-1], train.traintype)
		if conway then
			train.path[maxn+1]=conway
			train.max_index_on_track=maxn
		else
			--do as if nothing has happened and preceed with path