summaryrefslogtreecommitdiff
path: root/util/colors.txt
Commit message (Expand)AuthorAge
* Update minetestmapper.py to support the current map format (and previous ones)Perttu Ahola2012-06-08
* Add apple color to minetestmapper and fix cactus colorPerttu Ahola2011-09-26
* Updated colors.txt (which is used by minetestmapper)Kahrl2011-09-15
* * support for content types extension in minetestmapperWolfgang Fellger2011-07-30
* added minetestmapper in utils/Perttu Ahola2011-06-25
2'>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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 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
local aj_cache = {}
local aj_strout = {}

local aj_tostring

--[[ Notes on the pattern matching functions:
- Patterns can have multiple captures (e.g. coordinates). These captures
are passed starting from the second argument.
- The commands after the match are passed as the first argument. If the
command involves waiting, it should:
  - Set train.atc_command to the first argument passed to the function,
  - End with "do return end", and
  - Return true as the second argument
- The function should return a string to be compiled as the first result.
- In case of an error, the function should return nil as the first
argument and the error message as the second argument.
]]
local matchptn = {
	["A([01FT])"] = function(_, match)
		return string.format(
			"train.ars_disable=%s",
			(match=="0" or match=="F") and "true" or "false")
	end,
	["BB"] = function()
		return [[do
			train.atc_brake_target = -1
			train.tarvelocity = 0
		end]]
	end,
	["B([0-9]+)"] = function(_, match)
		return string.format([[do
			train.atc_brake_target = %s
			if not train.tarvelocity or train.tarvelocity > train.atc_brake_target then
				train.tarvelocity = train.atc_brake_target
			end
		end]], match)
	end,
	["D([0-9]+)"] = function(cont, match)
		return string.format([[do
			train.atc_delay = %s
			train.atc_command = %q
			return
		end]], match, cont), true
	end,
	["(%bI;)"] = function(cont, match)
		local i = 2
		local l = #match
		local epos
		while (i<l) do
			local b, e, c = string.find(match,"([IE])",i)
			if c == "I" then
				b, e = string.find(match,"%bI;", b)
				-- This is unlikely to happen since the %b pattern is balanced
				if not (b and e) then return nil, "Malformed nested I statement" end
				i = e+1
			else
				epos = b
				break
			end
		end
		local endp = string.find(match,"[WDI]") and true
		local cond = string.match(match,"^I([+-])")
		if cond then
			local vtrue = string.sub(match, 3, epos and epos-1 or -2)
			local vfalse = epos and string.sub(match, epos+1, -2)
			local cstr = (cond == "-") and "not" or ""
			local tstr, err = aj_tostring(vtrue, 1, cont)
			if not tstr then return nil, err end
			if vfalse then
				local fstr, err = aj_tostring(vfalse, 1, cont)
				if not fstr then return nil, err end
				return string.format("if %s train.atc_arrow then %s else %s end",
					cstr, tstr, fstr)
			else
				return string.format("if %s train.atc_arrow then %s end", cstr, tstr)
			end
		else
			local op, ref = string.match(match,"^I([<>]=?)([0-9]+)")
			if not op then
				return nil, "Invalid I statement"
			end
			local spos = 2+#op+#ref
			local vtrue = string.sub(match, spos, epos and epos-1 or -2)
			local vfalse = epos and string.sub(match, epos+1, -2)
			local cstr = string.format("train.velocity %s %s", op, ref)
			local tstr = aj_tostring(vtrue, 1, cont)
			if vfalse then
				local fstr, err = aj_tostring(vfalse, 1, cont)
				if not fstr then return nil, err end
				return string.format("if %s then %s else %s end", cstr, tstr, fstr)
			else
				return string.format("if %s then %s end", cstr, tstr)
			end
		end
	end,
	["K"] = function()
		return [=[do
			if train.door_open == 0 then
				atwarn(sid(id),attrans("ATC Kick command warning: Doors are closed"))
			elseif train.velocity>0 then
				atwarn(sid(id),attrans("ATC Kick command warning: Train moving"))
			else
				local tp = train.trainparts
				for i = 1, #tp do
					local data = advtrains.wagons[tp[i]]
					local obj = advtrains.wagon_objects[tp[i]]
					if data and obj then
						local ent = obj:get_luaentity()
						if ent then
							for seatno, seat in pairs(ent.seats) do
								if data.seatp[seatno] and not ent:is_driver_stand(seat) then
									ent:get_off(seatno)
								end
							end
						end
					end
				end
			end
		end]=]
	end,
	["O([LR])"] = function(_, match)
		local tt = {L = -1, R = 1}
		return string.format("train.door_open = %d*(train.atc_arrow and 1 or -1)",tt[match])
	end,
	["OC"] = function()
		return "train.door_open = 0"
	end,
	["R"] = function()
		return [[
			if train.velocity<=0 then
				advtrains.invert_train(id)
				advtrains.train_ensure_init(id, train)
			else
				atwarn(sid(id),attrans("ATC Reverse command warning: didn't reverse train, train moving!"))
			end]]
	end,
	["SM"] = function()
		return "train.tarvelocity=train.max_speed"
	end,
	["S([0-9]+)"] = function(_, match)
		return string.format("train.tarvelocity=%s",match)
	end,
	["W"] = function(cont)
		return string.format([[do
			train.atc_wait_finish=true
			train.atc_command=%q
			return
		end]], cont), true
	end,
}

local function aj_tostring_single(cmd, pos, cont)
	if not pos then pos = 1 end
	for pattern, func in pairs(matchptn) do
		local match = {string.find(cmd, "^"..pattern, pos)}
		if match[1] then
			local e = match[2]
			match[2] = string.sub(cmd, e+1)..(cont or "")
			return e+1, func(unpack(match, 2))
		end
	end
	return nil
end

aj_tostring = function(cmd, pos, cont)
	if not pos then pos = 1 end
	local t = {}
	local endp = false
	while pos <= #cmd do
		if string.match(cmd,"^%s+$", pos) then break end
		local _, e = string.find(cmd, "^%s+", pos)
		if e then pos = e+1 end
		local nxt, str
		nxt, str, endp = aj_tostring_single(cmd, pos, cont or "")
		if not str then
			return nil, (endp or "Invalid command or malformed I statement: "..string.sub(cmd,pos))
		end
		t[#t+1] = str
		pos = nxt
		if endp then
			break
		end
	end
	return table.concat(t,"\n"), pos
end

local function aj_compile(cmd)
	local c = aj_cache[cmd]
	if c then
		if type(c) == "function" then
			return c, aj_strout[cmd]
		else
			return nil, c
		end
	else
		local str, err = aj_tostring(cmd)
		if not str then
			aj_cache[cmd] = err
			return nil, err
		end
		str = string.format([[return function(id, train)
			%s
			train.atc_command=nil
		end]], str)
		local f, e = loadstring(str)
		if not f then
			aj_cache[cmd] = e
			return nil, e
		end
		f = f()
		aj_cache[cmd] = f
		aj_strout[cmd] = str
		return f, str
	end
end

local function aj_execute(id,train)
	if not train.atc_command then return end
	local func, err = aj_compile(train.atc_command)
	if func then return func(id,train) end
	return nil, err
end

return {
	compile = aj_compile,
	execute = aj_execute
}