Crossfire Server, Trunk
sleep.py
Go to the documentation of this file.
1 # sleep.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 # Uses JSON notation for parameters
21 #
22 # This script makes the monster it is attached to (use EVENT_TIME to attach it)
23 # sleep at certain periods of the day/month/year. Note that it will
24 # triggers only once per period change. That mean a sleeping monster awaken
25 # by a PC will not all of sudden go to sleep. Also, remember that most moster
26 # awaken and attack when a player is nearby. So effects of this script might be
27 # limited.
28 #
29 # exemple
30 #
31 # arch event_time
32 # title Python
33 # slaying /python/tod/sleep.py
34 # msg
35 # {
36 # "when":["Dawn","Night"]
37 # "match":"one"
38 # }
39 # endmsg
40 # arch beholder
41 # end
42 # end
43 #
44 
45 import Crossfire
46 import string
47 from CFTimeOfDay import TimeOfDay
48 import cjson
49 event = Crossfire.WhatIsEvent()
50 parameters = cjson.decode(event.Message)
51 alreadymatched = (event.Value!=0)
52 inverse = "inverse" in parameters and parameters["inverse"] == True
53 match = False
54 if not "match" in parameters:
55  Crossfire.Log(Crossfire.LogError,"Script sleep.py didn't get a 'match' parameter. Only got %s" %parameters)
56 elif parameters["match"].lower() == "one":
57  match=TimeOfDay().matchAny(parameters["when"]) != inverse
58 elif parameters["match"].lower() == "all":
59  match=TimeOfDay().matchAll(parameters["when"]) != inverse
60 else:
61  Crossfire.Log(Crossfire.LogError,"Script sleep.py didn't get a 'match' parameter. Only got %s" %parameters)
62 
63 if ( match != alreadymatched ):
64  Crossfire.Log(Crossfire.LogDebug, "sleep")
65  event = Crossfire.WhatIsEvent()
66  current = Crossfire.WhoAmI()
67  if (current):
68  if (alreadymatched):
69  event.Value=0
70  current.Sleeping=0
71  Crossfire.Log(Crossfire.LogDebug, "Awaken %s" %current.Name)
72  else:
73  event.Value=1
74  current.Sleeping=1
75  Crossfire.Log(Crossfire.LogDebug, "Put %s to sleep" %current.Name)
CFTimeOfDay.TimeOfDay
Definition: CFTimeOfDay.py:3