Allow FunctionMachines to act as a "helper" for another marker and implement use of these in FogMachines. Helper FunctionMachines can provide on-trigger messages for FogMachines, and pre-trigger warning messages.
Also includes three FogMachine-specific helpers: warning_machine, trigger_machine and tw_machine, providing simple and easy access to warning/trigger messages.
Signed-off-by: Robert Vollmert <rvollmert@gmx.net>
CPGFXEIHLYNGWTPJGZOP4XJ3YEWLH5BINXNMLJ2GWNATIOTFG5HAC
-- * "helper": A function machine that can be linked into other lua markers
-- and machines. It is not triggered independantly, but called by the "parent"
-- marker, though always with the same marker_table parameter as other
-- machines. May take further parameters, see the parent's documentation.
--
dgn.register_listener(dgn.dgn_event_type('turn'), marker)
--dgn.register_listener(dgn.dgn_event_type('entered_level'), marker)
if (self.marker_type == "helper") then
return
else
dgn.register_listener(dgn.dgn_event_type('turn'), marker)
end
-- helper: A FunctionMachine helper marker. Will be called whenever the countdown
-- is activated, and whenever the fog machine is reset. It will be called
-- with the FogMachine's marker, a string containing the event ("decrement",
-- "trigger"), the actual event object, and a copy of the FogMachine itself.
-- See the section "Messages for fog machines" at the end of the file.
--
-------------------------------------------------------------------------------
-- Messages for fog machines.
--
-- * warning_machine: Takes three parameters: turns, cantsee_message, and,
-- optionally, see_message. Turns is the value of player turns before to
-- trigger the message before the fog machine is fired. If only see_message
-- is provided, the message will only be printed if the player can see the
-- fog machine. If only cantsee_mesage is provided, the message will be
-- displayed regardless. In combination, the message will be different
-- depending on whether or not the player can see the marker. By default, the
-- message will be displaying using the "warning" channel.
--
-- * trigger_machine: Takes three parameters: cantsee_message, see_message, and,
-- optionally, channel. The functionality is identical to a warning_machine,
-- only the message is instead displayed (or not displayed) when the fog machine
-- is triggered. The message channel can be provided.
--
-- * tw_machine: Combines the above two message machines, providing warning messages
-- as well as messages when triggered. Takes the parameters: warn_turns,
-- warning_cantsee_message, trigger_cantsee_message, trigger_channel,
-- trigger_see_message, warning_see_message. Parameters work as described above.
--
-- In all instances, the "cantsee" form of the message parameter cannot be null,
-- and for warning and dual trigger/warning machines, the turns parameter cannot
-- be null. All other parameters are considered optional.
function warning_machine (trns, cantsee_mesg, see_mesg)
if trns == nil or (see_mesg == nil and cantsee_mesg == nil) then
error("WarningMachine requires turns and message!")
end
local function warning_func (marker, mtable, m2, event_name, event, fm)
local countdown = fm.countdown
if event_name == "decrement" and countdown <= mtable.turns then
if mtable.warning_done ~= true then
if mtable.see_message ~= nil and you.see_cell(marker:pos()) then
crawl.mpr(mtable.see_message, "warning")
elseif mtable.cantsee_message ~= nil then
crawl.mpr(mtable.cantsee_message, "warning")
end
mtable.warning_done = true
end
elseif event_name == "trigger" then
mtable.warning_done = false
end
end
pars = {marker_type = "helper"}
pars.marker_params = {see_message = see_mesg, cantsee_message = cantsee_mesg,
turns = trns * 10, warning_done = false}
pars.func = warning_func
return FunctionMachine:new(pars)
end
function trigger_machine (cantsee_mesg, see_mesg, chan)
if see_mesg == nil and cantsee_mesg == nil then
error("Triggermachine requires a message!")
end
local function trigger_func (marker, mtable, m2, event_name, event, fm)
local countdown = fm.countdown
if event_name == "trigger" then
channel = mtable.channel or ""
if mtable.see_message ~= nil and you.see_cell(marker:pos()) then
crawl.mpr(mtable.see_message, channel)
elseif mtable.cantsee_message ~= nil then
crawl.mpr(mtable.cantsee_message, channel)
end
end
end
pars = {marker_type = "helper"}
pars.marker_params = {channel = chan or nil, see_message = see_mesg, cantsee_message = cantsee_mesg}
pars.func = trigger_func
return FunctionMachine:new(pars)
end
function tw_machine (warn_turns, warn_cantsee_message, trig_cantsee_message, trig_channel,
trig_see_message, warn_see_message)
if warn_turns == nil or (warn_see_message == nil and warn_cantsee_message == nil)
or (trig_see_message == nil and trig_cantsee_message == nil) then
error("TWMachine needs warning turns, warning message and triggeing message.")
end
local function tw_func (marker, mtable, m2, event_name, event, fm)
local countdown = fm.countdown
if event_name == "decrement" and countdown <= mtable.warning_turns then
if mtable.warning_done ~= true then
if mtable.warning_see_message ~= nil and you.see_cell(marker:pos()) then
crawl.mpr(mtable.warning_see_message, "warning")
elseif mtable.warning_cantsee_message ~= nil then
crawl.mpr(mtable.warning_cantsee_message, "warning")
end
mtable.warning_done = true
end
elseif event_name == "trigger" then
mtable.warning_done = false
channel = mtable.trigger_channel or ""
if mtable.trigger_see_message ~= nil and you.see_cell(marker:pos()) then
crawl.mpr(mtable.trigger_see_message, channel)
elseif mtable.trigger_cantsee_message ~= nil then
crawl.mpr(mtable.trigger_cantsee_message, channel)
end
end
end
pars = {marker_type = "helper"}
pars.marker_params = {warning_see_message = warn_see_message, warning_cantsee_message = warn_cantsee_message,
warning_turns = warn_turns * 10, warning_done = false, trigger_see_message = trig_see_message,
trigger_cantsee_message = trig_cantsee_message, trigger_channel = trig_channel or nil}
pars.func = tw_func
return FunctionMachine:new(pars)
end