aboutsummaryrefslogtreecommitdiff
path: root/src/util/quicktune.cpp
blob: 37d4933de5605b0a69b37839250ff6219da1432d (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
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
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "quicktune.h"
#include "threading/mutex_auto_lock.h"
#include "util/string.h"

std::string QuicktuneValue::getString()
{
	switch(type){
	case QVT_NONE:
		return "(none)";
	case QVT_FLOAT:
		return ftos(value_QVT_FLOAT.current);
	}
	return "<invalid type>";
}
void QuicktuneValue::relativeAdd(float amount)
{
	switch(type){
	case QVT_NONE:
		break;
	case QVT_FLOAT:
		value_QVT_FLOAT.current += amount * (value_QVT_FLOAT.max - value_QVT_FLOAT.min);
		if(value_QVT_FLOAT.current > value_QVT_FLOAT.max)
			value_QVT_FLOAT.current = value_QVT_FLOAT.max;
		if(value_QVT_FLOAT.current < value_QVT_FLOAT.min)
			value_QVT_FLOAT.current = value_QVT_FLOAT.min;
		break;
	}
}

static std::map<std::string, QuicktuneValue> g_values;
static std::vector<std::string> g_names;
std::mutex *g_mutex = NULL;

static void makeMutex()
{
	if(!g_mutex){
		g_mutex = new std::mutex();
	}
}

std::vector<std::string> getQuicktuneNames()
{
	return g_names;
}

QuicktuneValue getQuicktuneValue(const std::string &name)
{
	makeMutex();
	MutexAutoLock lock(*g_mutex);
	std::map<std::string, QuicktuneValue>::iterator i = g_values.find(name);
	if(i == g_values.end()){
		QuicktuneValue val;
		val.type = QVT_NONE;
		return val;
	}
	return i->second;
}

void setQuicktuneValue(const std::string &name, const QuicktuneValue &val)
{
	makeMutex();
	MutexAutoLock lock(*g_mutex);
	g_values[name] = val;
	g_values[name].modified = true;
}

void updateQuicktuneValue(const std::string &name, QuicktuneValue &val)
{
	makeMutex();
	MutexAutoLock lock(*g_mutex);
	std::map<std::string, QuicktuneValue>::iterator i = g_values.find(name);
	if(i == g_values.end()){
		g_values[name] = val;
		g_names.push_back(name);
		return;
	}
	QuicktuneValue &ref = i->second;
	if(ref.modified)
		val = ref;
	else{
		ref = val;
		ref.modified = false;
	}
}

>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
-- 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 = <sigd>,
	aspect = <signal aspect>,--note, might change in future
}
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"
			if k==#route and not tmp_lcks then
				img = "at_il_route_end.png"
			end
			routemarker(context, c_sigd.p, "rte"..k, img, yaw, route.name.." #"..k)
		end
		-- display locks
		for pts, state in pairs(v.locks) do
			local pos = minetest.string_to_pos(pts)
			routesprite(context, pos, "fix"..k..pts, "at_il_route_lock.png", "Fixed in state '"..state.."' by route "..route.name.." until segment #"..k.." is freed.")
		end
	end
	
	-- The presence of tmp_lcks tells us that we are displaying during route programming.
	if tmp_lcks then
		-- display route end markers at appropriate places (check next TS, if it exists)
		local terminal = c_sigd
		if terminal then
			local term_tcbs = advtrains.interlocking.db.get_tcbs(terminal)
			if term_tcbs.ts_id then
				local over_ts = advtrains.interlocking.db.get_ts(term_tcbs.ts_id)
				for i, sigd in ipairs(over_ts.tc_breaks) do
					if not vector.equals(sigd.p, terminal.p) then
						local yaw = 0
						local node_ok, conns, rhe = advtrains.get_rail_info_at(sigd.p, advtrains.all_tracktypes)
						if node_ok then
							yaw = advtrains.dir_to_angle(conns[otherside(sigd.s)].c)
						end
						routemarker(context, sigd.p, "rteterm"..i, "at_il_route_end.png", yaw, route.name.." Terminal "..i)
					end
				end
			end
		end
	-- display locks set by player		
		for pts, state in pairs(tmp_lcks) do
			local pos = minetest.string_to_pos(pts)
			routesprite(context, pos, "fixp"..pts, "at_il_route_lock_edit.png", "Fixed in state '"..state.."' by route "..route.name.." (punch to unfix)",
				function() clear_lock(tmp_lcks, pname, pts) end)
		end
	end
end


local player_rte_prog = {}

function advtrains.interlocking.init_route_prog(pname, sigd)
	if not minetest.check_player_privs(pname, "interlocking") then
		minetest.chat_send_player(pname, "Insufficient privileges to use this!")
		return
	end
	player_rte_prog[pname] = {
		origin = sigd,
		route = {
			name = "PROG["..pname.."]",
		},
		tmp_lcks = {},
	}
	advtrains.interlocking.visualize_route(sigd, player_rte_prog[pname].route, "prog_"..pname, player_rte_prog[pname].tmp_lcks, pname)
	minetest.chat_send_player(pname, "Route programming mode active. Punch TCBs to add route segments, punch turnouts to lock them.")
end

local function get_last_route_item(origin, route)
	if #route == 0 then
		return origin
	end
	return route[#route].next
end

local function do_advance_route(pname, rp, sigd, tsname)
	table.insert(rp.route, {next = sigd, locks = rp.tmp_lcks})
	rp.tmp_lcks = {}
	chat(pname, "Added track section '"..tsname.."' to the route.")
end

local function finishrpform(pname)
	local rp = player_rte_prog[pname]
	if not rp then return end
	
	local form = "size[7,6]label[0.5,0.5;Finish programming route]"
	local terminal = get_last_route_item(rp.origin, rp.route)
	if terminal then
		local term_tcbs = advtrains.interlocking.db.get_tcbs(terminal)
		
		if term_tcbs.signal then
			form = form .. "label[0.5,1.5;Route ends at signal:]"
			form = form .. "label[0.5,2  ;"..term_tcbs.signal_name.."]"
		else
			form = form .. "label[0.5,1.5;WARNING: Route does not end at a signal.]"
			form = form .. "label[0.5,2  ;Routes should in most cases end at signals.]"
			form = form .. "label[0.5,2.5;Cancel if you are unsure!]"
		end
	else
		form = form .. "label[0.5,1.5;Route leads into]"
		form = form .. "label[0.5,2  ;non-interlocked area]"
	end
	form = form.."field[0.8,3.5;5.2,1;name;Enter Route Name;]"
	form = form.."button_exit[0.5,4.5;  5,1;save;Save Route]"
	
	
	minetest.show_formspec(pname, "at_il_routepf", form)
end


local function check_advance_valid(tcbpos, rp)
	-- track circuit break, try to advance route over it
	local lri = get_last_route_item(rp.origin, rp.route)
	if not lri then
		return false, false
	end
	
	local is_endpoint = false
	
	local this_sigd, this_ts, adv_side
	
	if vector.equals(lri.p, tcbpos) then
		-- If the player just punched the last TCB again, it's of course possible to
		-- finish the route here (although it can't be advanced by here.
		-- Fun fact: you can now program routes that end exactly where they begin :)
		is_endpoint = true
		this_sigd = lri
	else
		-- else, we need to check whether this TS actually borders
		local start_tcbs = advtrains.interlocking.db.get_tcbs(lri)
		if not start_tcbs.ts_id then
			return false, false
		end
		
		this_ts = advtrains.interlocking.db.get_ts(start_tcbs.ts_id)
		for _,sigd in ipairs(this_ts.tc_breaks) do
			if vector.equals(sigd.p, tcbpos) then
				adv_side = otherside(sigd.s)
			end
		end
		if not adv_side then
			-- this TCB is not bordering to the section
			return false, false