Crossfire Server, Trunk
trigger.py
Go to the documentation of this file.
1 # trigger.py -- triggered connections from CFPython
2 #
3 # CFPython provides map.TriggerConnected() which triggers a connection once.
4 # However, in some circumstances, what you actually want is an "open" followed
5 # by a "close" after a delay (like 'arch trigger'). Many maps work around this
6 # with clever lightning walls, but this should not be necessary.
7 #
8 # Usage: In your script, in place of:
9 # Crossfire.WhoAmI().Map.TriggerConnected(con, 1)
10 # Write:
11 # import misc.trigger
12 # misc.trigger.trigger(Crossfire.WhoAmI(), con)
13 #
14 # Bugs:
15 # - The timer created by CreateTimer does not survive across map resets, so
16 # if the map happens to swap before the gate is un-triggered, the gate will
17 # stay open.
18 
19 import Crossfire
20 
21 extra_debug = False
22 
23 def flip_pol(pol):
24  if pol != 0:
25  return 0
26  else:
27  return 1
28 
29 def trigger(ob, con, pol=1, delay=5):
30  # add delayed close
31  trigger_delay(ob, con, flip_pol(pol), delay)
32 
33  # now just open the gate
34  if extra_debug:
35  Crossfire.Log(Crossfire.LogDebug, "Triggering connection %d on %s" % (con, ob.Map.Path))
36  ob.Map.TriggerConnected(con, pol)
37 
38 def trigger_delay(ob, con, pol, delay):
39  timer = Crossfire.CreateObjectByName("event_timer")
40  timer.Title = "Python"
41  timer.Name = "%d %d" % (con, pol)
42  timer.Slaying = "/python/misc/trigger.py" # path to this script
43  timer.InsertInto(ob)
44  ob.CreateTimer(delay, 1) # in seconds
45 
47  ev = Crossfire.WhatIsEvent()
48  if ev.Subtype == Crossfire.EventType.TIMER:
49  parts = ev.Name.strip().split(" ")
50  try:
51  con = int(parts[0])
52  pol = int(parts[1])
53  if extra_debug:
54  Crossfire.Log(Crossfire.LogDebug, "Un-triggering connection %d on %s" % (con, ev.Env.Map.Path))
55  ev.Env.Map.TriggerConnected(con, pol)
56  except Exception as e:
57  Crossfire.Log(Crossfire.LogError, "Error parsing delayed trigger on %s: %s" % (ev.Env.Map.Path, e))
58  ev.Remove()
59 
trigger.do_delayed_trigger
def do_delayed_trigger()
Definition: trigger.py:46
trigger.flip_pol
def flip_pol(pol)
Definition: trigger.py:23
trigger.trigger
def trigger(ob, con, pol=1, delay=5)
Definition: trigger.py:29
trigger.trigger_delay
def trigger_delay(ob, con, pol, delay)
Definition: trigger.py:38
make_face_from_files.int
int
Definition: make_face_from_files.py:32
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2606