summaryrefslogtreecommitdiff
path: root/Tyard/init_code.lua
blob: a90efdf3762844d11c2186548b56e31b3fbb75cb (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
--Environment Code
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)
	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
	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
	return reinsert
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)
	return true
end