Fix sending empty ATC commands

This commit is contained in:
orwell96 2019-02-05 10:51:55 +01:00
parent badd915121
commit 8b8c5a2c1f
1 changed files with 58 additions and 35 deletions

View File

@ -2,6 +2,9 @@
--registers and controls the ATC system --registers and controls the ATC system
local atc={} local atc={}
local eval_conditional
-- ATC persistence table. advtrains.atc is created by init.lua when it loads the save file. -- ATC persistence table. advtrains.atc is created by init.lua when it loads the save file.
atc.controllers = {} atc.controllers = {}
function atc.load_data(data) function atc.load_data(data)
@ -50,8 +53,18 @@ function atc.send_command(pos, par_tid)
atwarn("ATC rail at", pos, ": Rail not on train's path! Can't determine arrow direction. Assuming +!") atwarn("ATC rail at", pos, ": Rail not on train's path! Can't determine arrow direction. Assuming +!")
end end
atc.train_set_command(train, atc.controllers[pts].command, iconnid==1) local command = atc.controllers[pts].command
atprint("Sending ATC Command to", train_id, ":", atc.controllers[pts].command, "iconnid=",iconnid) command = eval_conditional(command, iconnid==1, train.velocity)
if not command then command="" end
command=string.match(command, "^%s*(.*)$")
if command == "" then
atprint("Sending ATC Command to", train_id, ": Not modifying, conditional evaluated empty.")
return true
end
atc.train_set_command(train, command, iconnid==1)
atprint("Sending ATC Command to", train_id, ":", command, "iconnid=",iconnid)
return true return true
else else
@ -219,41 +232,33 @@ local matchptn={
end, end,
} }
function atc.execute_atc_command(id, train) eval_conditional = function(command, arrow, speed)
--strip whitespaces
local command=string.match(train.atc_command, "^%s*(.*)$")
if string.match(command, "^%s*$") then
train.atc_command=nil
return
end
--conditional statement? --conditional statement?
local is_cond, cond_applies, compare local is_cond, cond_applies, compare
local cond, rest=string.match(command, "^I([%+%-])(.+)$") local cond, rest=string.match(command, "^I([%+%-])(.+)$")
if cond then if cond then
is_cond=true is_cond=true
if cond=="+" then if cond=="+" then
cond_applies=train.atc_arrow cond_applies=arrow
end end
if cond=="-" then if cond=="-" then
cond_applies=not train.atc_arrow cond_applies=not arrow
end end
else else
cond, compare, rest=string.match(command, "^I([<>]=?)([0-9]+)(.+)$") cond, compare, rest=string.match(command, "^I([<>]=?)([0-9]+)(.+)$")
if cond and compare then if cond and compare then
is_cond=true is_cond=true
if cond=="<" then if cond=="<" then
cond_applies=train.velocity<tonumber(compare) cond_applies=speed<tonumber(compare)
end end
if cond==">" then if cond==">" then
cond_applies=train.velocity>tonumber(compare) cond_applies=speed>tonumber(compare)
end end
if cond=="<=" then if cond=="<=" then
cond_applies=train.velocity<=tonumber(compare) cond_applies=speed<=tonumber(compare)
end end
if cond==">=" then if cond==">=" then
cond_applies=train.velocity>=tonumber(compare) cond_applies=speed>=tonumber(compare)
end end
end end
end end
@ -267,8 +272,7 @@ function atc.execute_atc_command(id, train)
while nest>=0 do while nest>=0 do
if pos>#rest then if pos>#rest then
atwarn(sid(id), attrans("ATC command syntax error: I statement not closed: @1",command)) atwarn(sid(id), attrans("ATC command syntax error: I statement not closed: @1",command))
atc.train_reset_command(train, true) return ""
return
end end
local char=string.sub(rest, pos, pos) local char=string.sub(rest, pos, pos)
if char=="I" then if char=="I" then
@ -289,24 +293,43 @@ function atc.execute_atc_command(id, train)
command=string.sub(rest, elsepos+1, pos-2)..string.sub(rest, pos) command=string.sub(rest, elsepos+1, pos-2)..string.sub(rest, pos)
end end
atprint("Result: "..command) atprint("Result: "..command)
train.atc_command=command end
atc.execute_atc_command(id, train) return command
end
function atc.execute_atc_command(id, train)
--strip whitespaces
local command=string.match(train.atc_command, "^%s*(.*)$")
if string.match(command, "^%s*$") then
train.atc_command=nil
return return
else end
for pattern, func in pairs(matchptn) do
local match=string.match(command, "^"..pattern) train.atc_command = eval_conditional(command, train.atc_arrow, train.velocity)
if match then
local patlen=func(id, train, match) if not train.atc_command then return end
command=string.match(train.atc_command, "^%s*(.*)$")
atprint("Executing: "..string.sub(command, 1, patlen))
if string.match(command, "^%s*$") then
train.atc_command=string.sub(command, patlen+1) train.atc_command=nil
if train.atc_delay<=0 and not train.atc_wait_finish then return
--continue (recursive, cmds shouldn't get too long, and it's a end-recursion.) end
atc.execute_atc_command(id, train)
end for pattern, func in pairs(matchptn) do
return local match=string.match(command, "^"..pattern)
if match then
local patlen=func(id, train, match)
atprint("Executing: "..string.sub(command, 1, patlen))
train.atc_command=string.sub(command, patlen+1)
if train.atc_delay<=0 and not train.atc_wait_finish then
--continue (recursive, cmds shouldn't get too long, and it's a end-recursion.)
atc.execute_atc_command(id, train)
end end
return
end end
end end
atwarn(sid(id), attrans("ATC command parse error: Unknown command: @1", command)) atwarn(sid(id), attrans("ATC command parse error: Unknown command: @1", command))