aboutsummaryrefslogtreecommitdiff
path: root/advtrains/debugitems.lua
blob: e672308c5df28d3730201a3658207390c4a0f485 (plain)
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
minetest.register_tool("advtrains:tunnelborer",
{
	description = "tunnelborer",
	groups = {cracky=1}, -- key=name, value=rating; rating=1..3.
	inventory_image = "drwho_screwdriver.png",
	wield_image = "drwho_screwdriver.png",
	stack_max = 1,
	range = 7.0,
		
	on_place = function(itemstack, placer, pointed_thing)
	
	end,
	--[[
	^ Shall place item and return the leftover itemstack
	^ default: minetest.item_place ]]
	on_use = function(itemstack, user, pointed_thing)
		if pointed_thing.type=="node" then
			for x=-1,1 do
				for y=-1,1 do
					for z=-1,1 do
						minetest.remove_node(vector.add(pointed_thing.under, {x=x, y=y, z=z}))
					end
				end
			end
		end
	end,
}
)

minetest.register_chatcommand("atyaw",
	{
        params = "angledeg conn1 conn2", 
        description = "", 
        func = function(name, param)
			local angledegs, conn1s, conn2s = string.match(param, "^(%S+)%s(%S+)%s(%S+)$")
			if angledegs and conn1s and conn2s then
				local angledeg, conn1, conn2 = angledegs+0,conn1s+0,conn2s+0
				local yaw = angledeg*math.pi/180
				local yaw1 = advtrains.dir_to_angle(conn1)
				local yaw2 = advtrains.dir_to_angle(conn2)
				local adiff1 = advtrains.minAngleDiffRad(yaw, yaw1)
				local adiff2 = advtrains.minAngleDiffRad(yaw, yaw2)
				
				atdebug("yaw1",atfloor(yaw1*180/math.pi))
				atdebug("yaw2",atfloor(yaw2*180/math.pi))
				atdebug("dif1",atfloor(adiff1*180/math.pi))
				atdebug("dif2",atfloor(adiff2*180/math.pi))
				
				minetest.chat_send_all(advtrains.yawToAnyDir(yaw))
				return true, advtrains.yawToDirection(yaw, conn1, conn2)
			end
        end,
})
9' href='#n299'>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
-- Route programming system

--[[
Progamming routes:
1. Select "program new route" in the signalling dialog
-> route_start marker will appear to designate route-program mode
2. Do those actions in any order:
A. punch a TCB marker node to proceed route along this TCB. This will only work if
	this is actually a TCB bordering the current TS, and will place a
	route_set marker and shift to the next TS
B. right-click a turnout to switch it (no impact to route programming
C. punch a turnout (or some other passive component) to fix its state (toggle)
	for the route. A sprite telling "Route Fix" will show that fact.
3. To complete route setting, use the chat command '/at_program_route <route name>'.
	The last punched TCB will get a 'route end' marker
	The end of a route should be at another signal facing the same direction as the entrance signal,
	however this is not enforced and left up to the signal engineer (the programmer)
	
The route visualization will also be used to visualize routes after they have been programmed.
]]--


-- table with objectRefs
local markerent = {}

minetest.register_entity("advtrains_interlocking:routemarker", {
	visual = "mesh",
	mesh = "trackplane.b3d",
	textures = {"at_il_route_set.png"},
	collisionbox = {-1,-0.5,-1, 1,-0.4,1},
	visual_size = {x=10, y=10},
	on_punch = function(self)
		self.object:remove()
	end,
	get_staticdata = function() return "STATIC" end,
	on_activate = function(self, sdata) if sdata=="STATIC" then self.object:remove() end end,
	static_save = false,
})


-- Spawn or update a route marker entity
-- pos: position where this is going to be
-- key: something unique to determine which entity to remove if this was set before
-- img: texture
local function routemarker(context, pos, key, img, yaw, itex)
	if not markerent[context] then
		markerent[context] = {}
	end
	if markerent[context][key] then
		markerent[context][key]:remove()
	end
	
	local obj = minetest.add_entity(vector.add(pos, {x=0, y=0.3, z=0}), "advtrains_interlocking:routemarker")
	if not obj then return end
	obj:set_yaw(yaw)
	obj:set_properties({
		infotext = itex,
		textures = {img},
	})
	
	markerent[context][key] = obj
end

minetest.register_entity("advtrains_interlocking:routesprite", {
	visual = "sprite",
	textures = {"at_il_turnout_free.png"},
	collisionbox = {-0.2,-0.2,-0.2, 0.2,0.2,0.2},
	visual_size = {x=1, y=1},
	on_punch = function(self)
		if self.callback then
			self.callback()
		end
		self.object:remove()
	end,
	get_staticdata = function() return "STATIC" end,
	on_activate = function(self, sdata) if sdata=="STATIC" then self.object:remove() end end,
	static_save = false,
})


-- Spawn or update a route sprite entity
-- pos: position where this is going to be
-- key: something unique to determine which entity to remove if this was set before
-- img: texture
local function routesprite(context, pos, key, img, itex, callback)
	if not markerent[context] then
		markerent[context] = {}
	end
	if markerent[context][key] then
		markerent[context][key]:remove()
	end
	
	local obj = minetest.add_entity(vector.add(pos, {x=0, y=0, z=0}), "advtrains_interlocking:routesprite")
	if not obj then return end
	obj:set_properties({
		infotext = itex,
		textures = {img},
	})
	
	if callback then
		obj:get_luaentity().callback = callback
	end
	
	markerent[context][key] = obj
end

--[[
Route definition:
route = {
	name = <string>
	[n] = {
		next = <sigd>, -- of the next (note: next) TCB on the route
		locks = {<pts> = "state"} -- route locks of this route segment
	}
	terminal = 
}
The first item in the TCB path (namely i=0) is always the start signal of this route,
so this is left out.
All subsequent entries, starting from 1, contain:
- all route locks of the segment on TS between the (i-1). and the i. TCB
- the next TCB signal describer in proceeding direction of the route.
'Terminal' once again repeats the "next" entry of the last route segment.
It is needed for distant signal aspect determination. If it is not set,
the distant signal aspect is determined as DANGER.
]]--

local function chat(pname, message)
	minetest.chat_send_player(pname, "[Route programming] "..message)
end
local function clear_lock(locks, pname, pts)
	locks[pts] = nil
	chat(pname, pts.." is no longer affected when this route is set.")
end

local function otherside(s)
	if s==1 then return 2 else return 1 end
end

function advtrains.interlocking.clear_visu_context(context)
	if not markerent[context] then return end
	for key, obj in pairs(markerent[context]) do
		obj:remove()
	end
	markerent[context] = nil
end

-- visualize route. 'context' is a string that identifies the context of this visualization
-- e.g. prog_<player> or vis_<pts> for later visualizations
-- last 2 parameters are only to be used in the context of route programming!
function advtrains.interlocking.visualize_route(origin, route, context, tmp_lcks, pname)
	advtrains.interlocking.clear_visu_context(context)
	
	local oyaw = 0
	local onode_ok, oconns, orhe = advtrains.get_rail_info_at(origin.p, advtrains.all_tracktypes)
	if onode_ok then
		oyaw = advtrains.dir_to_angle(oconns[origin.s].c)
	end
	routemarker(context, origin.p, "rte_origin", "at_il_route_start.png", oyaw, route.name)
	
	local c_sigd = origin
	for k,v in ipairs(route) do
		c_sigd = v.next
		-- display route path
		-- Final "next" marker can be EOI, thus undefined. This is legitimate.
		if c_sigd then
			local yaw = 0
			local node_ok, conns, rhe = advtrains.get_rail_info_at(c_sigd.p, advtrains.all_tracktypes)
			if node_ok then
				yaw = advtrains.dir_to_angle(conns[c_sigd.s].c)
			end
			local img = "at_il_route_set.png"