File: push.py

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

#pushdown if need
if (match & (not alreadymatched)):
    op = event
    while (op.Env):
        op=op.Env
    map = op.Map
    map.TriggerConnected(connected,1,Crossfire.WhoAmI())
    event.Value=1
if ( (not match) & alreadymatched):
    op = event
    while (op.Env):
        op=op.Env
    map = op.Map
    map.TriggerConnected(connected,0,Crossfire.WhoAmI())
    event.Value=0