Crossfire Server, Trunk
push.py
Go to the documentation of this file.
1 # push.py
2 #
3 # Copyright 2007 by David Delbecq
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 #
20 #
21 # This script make the event it is attached to (not global!)
22 # trigger a connected value at specific moment of year/day.
23 # It will behave as if a button was "pushed" when entering period
24 # and "release" after the period. Typical use is to use it as
25 # event_time on a living object (that objects that triggers
26 # time events).
27 # This script ensure button remains pushed for all period, and
28 # get release after period. This works even is map is loaded
29 # in middle of period or map gets released from memory and put in
30 # cache. In those case, event will just ensure that "current" state
31 # correspond to expected state and correct status if needed.
32 # Note: the event must be inside an object
33 # which can find a path, using inventory, to a toplavel
34 # object that is on a Map.
35 #
36 # exemple, to make an "push" starting at Morning and ending after Noon:
37 #
38 # arch event_time
39 # title Python
40 # slaying /python/tod/push.py
41 # msg
42 # {
43 # "connected":"69",
44 # "when":["Morning","Noon"],
45 # "match":"one"
46 # }
47 # endmsg
48 # end
49 # parameters are separated by comas. First one
50 # is connected value to trigger, other ones are
51 # one or more periods where state must become "pushed"
52 import Crossfire
53 import string
54 from CFTimeOfDay import TimeOfDay
55 import cjson
56 event = Crossfire.WhatIsEvent()
57 parameters = cjson.decode(event.Message)
58 alreadymatched = (event.Value!=0)
59 connected = int(parameters["connected"])
60 inverse = "inverse" in parameters and parameters["inverse"] == True
61 match = False
62 if not "match" in parameters:
63  Crossfire.Log(Crossfire.LogError,"Script push_period.py didn't get a 'match' parameter. Only got %s" %parameters)
64 elif parameters["match"].lower() == "one":
65  match=TimeOfDay().matchAny(parameters["when"]) != inverse
66 elif parameters["match"].lower() == "all":
67  match=TimeOfDay().matchAll(parameters["when"]) != inverse
68 else:
69  Crossfire.Log(Crossfire.LogError,"Script push_period.py didn't get a 'match' parameter. Only got %s" %parameters)
70 
71 #pushdown if need
72 if (match & (not alreadymatched)):
73  op = event
74  while (op.Env):
75  op=op.Env
76  map = op.Map
77  map.TriggerConnected(connected,1,Crossfire.WhoAmI())
78  event.Value=1
79 if ( (not match) & alreadymatched):
80  op = event
81  while (op.Env):
82  op=op.Env
83  map = op.Map
84  map.TriggerConnected(connected,0,Crossfire.WhoAmI())
85  event.Value=0
make_face_from_files.int
int
Definition: make_face_from_files.py:26
CFTimeOfDay.TimeOfDay
Definition: CFTimeOfDay.py:3