0


I am trying to control weather a trigger_delay is fired or not based on the state of a door. This is to prevent the delay from opening a door that is already closed.

Problem: When a door is opened the trigger_delay is fired immediately, not after the delay duration.

  1. When a door is first opened the delay is triggered and permitted
  2. If the user then closes the door, the second delay trigger is interrupted, but the first one still goes through. This causes the door to still be opened when it was closed.

Here's the code I have so far:

if doors==nil then doors={} end
totalDoors = 35     -- total number of doors in the map
bInterrupt = false  -- controls if the trigger will be permitted
bDebug = true       -- set true to print values to console for testing

-- naming convention:
-- doors:  d1, d2, d3...
-- trigger_delay: td1, td2, td3...

function initArray(m)
    local array = {}
    for i = 1, m do
        array[i] = {d = "d"..i, td = "td"..i, open = false}
    end
    return array
end

doors=initArray(totalDoors)

addhook("triggerentity","TriggerUsed")
function TriggerUsed(x, y)  -- entity triggered
    bInterrupt = false      -- dont interrupt unless we tell it to
    sName = entity(x,y,"name")
    -- loop thru doors array
    for i = 1, totalDoors do
        aDelay = doors[i].td
        aDoor = doors[i].d
        if sName == aDelay then             -- the trigger is a trigger_delay
            if bDebug == true then -- print name and state as string to console
                if doors[i].open == true then sState = "Yes" end
                if doors[i].open == false then sState = "No" end
                print(sName.." was triggered: Door Open? :"..sState)
            end
            if doors[i].open == false then  -- door is closed
                bInterrupt = true           -- dont open it
                i = totalDoors              -- exit the loop and continue
                if bDebug == true then print("***"..sName.." : door is already closed!***") end
            else                            -- the door is open
                bInterrupt = false          -- let it close
                i = totalDoors              -- exit the loop and continue
                if bDebug == true then print("***"..sName.." : door was OPEN, closing...***") end
            end
        elseif sName == aDoor then          -- the trigger is a door
            bState = entity(x,y,"state")
            if bDebug == true then -- print name and state as string to console
                if bState == true then sState = "Yes" end
                if bState == false then sState = "No" end
                print(sName.." was triggered: Door Open? :"..sState)
            end
            if bState == true then          -- the door is open
                bInterrupt = false          -- allow it to close
                doors[i].open = false       -- set array item state to closed
                i = totalDoors              -- exit the loop and continue
                if bDebug == true then print(sName.." was OPEN, closing..") end
            else                            -- the door is closed
                bInterrupt = false          -- allow it to open
                doors[i].open = true        -- set array item state to open
                i = totalDoors              -- exit the loop and continue
                if bDebug == true then print(sName.." was CLOSED, openning...") end
            end
        end
    end

    -- should the trigger be triggered or not?
    if bInterrupt == true then
        if bDebug == true then print(sName.." was Interrupted") end
        return 1    -- do not trigger
    else
        if bDebug == true then print(sName.." was permitted") end
        return 0    -- trigger as usual
    end
end

With this code if I open and then close a door it produced the following results:

[10:11:54] d6 was triggered: Door Open? :No          <- I opened the door
[10:11:54] d6 was CLOSED, openning...
[10:11:54] d6 was permitted
[10:11:54] s6 was permitted
[10:11:54] td6 was triggered: Door Open? :Yes
[10:11:54] ***td6 : door was OPEN, closing...***
[10:11:54] td6 was permitted
[10:11:54] d6 was triggered: Door Open? :Yes          <- I closed the door
[10:11:54] d6 was OPEN, closing..
[10:11:54] d6 was permitted
[10:11:54] s6 was permitted
[10:11:54] td6 was triggered: Door Open? :No
[10:11:54] ***td6 : door is already closed!***
[10:11:54] td6 was Interrupted                    <- the second delay is canceled
[10:11:57] d6 was triggered: Door Open? :No       <- the first delay still opens
[10:11:57] d6 was CLOSED, openning...                the door
[10:11:57] d6 was permitted
[10:11:57] s6 was permitted

Any idea on how to do this properly? It seems it only detects the state when the delay is first triggered and not after the delay interval. Any help would be appreciated.

flag offensive
asked 2010-07-27 14:36:55.137091
5
add comment
2 Answers:
0


This is a bit arcane since CS2D normally have self propagating triggers, but here's the code:

Assuming that we have 1 starttrigger, one td (triggerdelay), and one ctd (trigger_delay that triggers td). We have an obstacle that we'll name door and a button that we'll name stopctd:

  • start -> td
  • td -> ctd,door
  • ctd -> td
  • stopctd -> None

The following code should work for one instance of the above scenario, you can extrapolate from this:

addhook("trigger", "tr1")

stop = false
doors = 0
function tr1(dst, src)
    if dst == "stopctd" then
        if stop then
            parse("trigger td")
        else
            if doors%s == 0 then parse("trigger doors") end
        end

        stop = not stop
    elseif not stop and dst == "doors" then
        doors = doors + 1
    elseif stop and dst == "doors" then
        return 1
    end
end
permanent link | flag offensive
answered 2010-07-27 21:28:49.222896
41
add comment
0


Thanks for the response, Lee. I think I found an easier way to do it. I just removed the trigger_delays altogether and use the "second" hook to track the doors in the array.

It's working great! Here's the code:

-- Door Auto-Close Controller
-- By VaiN

-- This script will close a door after iInterval seconds
-- if the door is open

if doors==nil then doors={} end
totalDoors = 35     -- total number of doors in the map
iInterval = 3

function initArray(m)
    local array = {}
    for i = 1, m do
        array[i] = {d = "d"..i, open = false, time = 0}
    end
    return array
end

doors=initArray(totalDoors)

addhook("second", "DoorTimer")
function DoorTimer()
    -- loop thru all doors and decrement the time value
    -- if it is zero and the door is open, then close it
    for i = 1, totalDoors do
        if doors[i].time > 0 then
            doors[i].time = doors[i].time - 1
        elseif doors[i].time == 0 and doors[i].open == true then
            -- close the door
            parse("trigger d"..i) -- trigger the door
            parse("trigger s"..i) -- trigger the sound effect
            print("Door: "..doors[i].d.." was closed by the timer")
        end
    end
end

addhook("triggerentity","TriggerUsed")
function TriggerUsed(x,y)                   -- entity triggered
    sName = entity(x,y,"name")
    -- loop thru doors array
    for i = 1, totalDoors do
        if sName == doors[i].d then         -- the trigger is a door
            bState = entity(x,y,"state")
            if bState == false then
                print("Door: "..doors[i].d.." was opened")
                doors[i].open = true
                doors[i].time = iInterval   -- add iInterval seconds to the time
            else
                print("Door: "..doors[i].d.." was closed")
                doors[i].open = false
                doors[i].time = 0           -- reset the time
            end
            break                           -- stop looping thru array
        end
    end
end
permanent link | flag offensive
answered 2010-07-28 16:17:25.935203
5
add comment
Your answer:
You are now not logged in but you can answer first and then login
toggle preview



Tags:

× 2
× 2
× 1

Asked: 1 year, 6 months ago

Seen: 1,085 times

Last updated: 1 year, 6 months ago

Made with Django.