summaryrefslogtreecommitdiff
path: root/Tyard/init_code.lua
blob: fe220dab747423c801ba2f9c3e4a5c5334a3520c (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
--Environment Code
F.error = function(set)
	local error_indicator = POS(-4025,13,-2671)
	if set ~= nil then
		setstate(error_indicator,(set and "on") or "off")
	end
	return (getstate(error_indicator) == "on") or false
end

F.yard_active = function(set) --if set == true then yard = active
	local yard_indicator = POS(-4025,14,-2659)
	if set ~= nil then
		setstate(yard_indicator,(set and "on") or "off")
	end
	return (getstate(yard_indicator) == "on") or false
end

F.dir = function(set) -- if set == true then dir = pointing north
	local dir_indicator = POS(-4025,13,-2665)
	if set ~= nil then
		setstate(dir_indicator,(set and "on") or "off")
	end
	return (getstate(dir_indicator) == "on") or false
end

F.get_rc_safe = function()
	return get_rc() or ""
end

F.has_rc = function(query,rc_list) -- query eg: "rc1"
	if rc_list == "" or query == nil or query=="" then return false end
	if not rc_list then rc_list = F.get_rc_safe() end
	for word in rc_list:gmatch("[^%s]+") do
		if word == query then return true end
	end
	print(F.get_rc_safe())
	return false
end

F.remove_rc = function(rc_list,arrow_mode) -- rc_list eg: {"rc1","rc2"}
	-- rc_list MUST be a table of rc codes to remove
	-- eg: {"rc1","rc2"}
	-- Arrow Modes:
	-- true: with arrow direction
	-- false: against arrow direction
	-- nil: ignores arrow direction
	
	if not atc_id then return false end
	if (arrow_mode == nil) or (atc_arrow == arrow_mode) then
		local rc = F.get_rc_safe()
		rc_list = rc_list or {}
		local rc_remove = {}
		for _,v in pairs(rc_list) do
			rc_remove[v] = true
		end
		-- remove codes from train's rc
		local reinsert = {}
		for token in rc:gmatch("[^%s]+") do
			if not rc_remove[token] then
				table.insert(reinsert,token)
			end
		end
		-- insert new string to train's rc
		set_rc(table.concat(reinsert," "))
	end
	print(F.get_rc_safe())
	return reinsert
end

F.remove_rc_match = function(rc_list) -- rc_list eg: "rc_%d+"
	local rm = {}
	for v in F.get_rc_safe():gmatch("("..rc_list..")") do
		table.insert(rm,v)
	end
	F.remove_rc(rm)
	print(F.get_rc_safe())
	return rm
end

F.add_rc = function(rc_list) -- rc_list eg {"rc1","rc2"} OR "rc1 rc2"
	if not atc_id then return false end
	if type(rc_list) == "table" then
		rc_list = table.concat(rc_list," ")
	end
	set_rc(F.get_rc_safe().." "..rc_list)
	print(F.get_rc_safe())
	return true
end