| 1 |
# replace.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 |
# This script make the object is is attached to swap at |
| 22 |
# given periods of day with a specifc object in the event's inventory |
| 23 |
# To use it, give this event's parameter the name of |
| 24 |
# period where the swap is active. Put in the inventiry the swapped object |
| 25 |
# The swap can occur for objects in map and for object in other object |
| 26 |
# |
| 27 |
# exemple |
| 28 |
# |
| 29 |
# arch event_time |
| 30 |
# title Python |
| 31 |
# slaying /python/tod/replace_all_periods.py |
| 32 |
# msg |
| 33 |
# { |
| 34 |
# "when":["Morning","The Season of the Blizzard"] |
| 35 |
# "match":"all" |
| 36 |
# } |
| 37 |
# endmsg |
| 38 |
# arch beholder |
| 39 |
# end |
| 40 |
# end |
| 41 |
# |
| 42 |
|
| 43 |
import Crossfire |
| 44 |
import string |
| 45 |
from CFTimeOfDay import TimeOfDay |
| 46 |
import cjson |
| 47 |
event = Crossfire.WhatIsEvent() |
| 48 |
parameters = cjson.decode(event.Message) |
| 49 |
alreadymatched = (event.Value!=0) |
| 50 |
inverse = "inverse" in parameters and parameters["inverse"] == True |
| 51 |
match = False |
| 52 |
if not "match" in parameters: |
| 53 |
Crossfire.Log(Crossfire.LogError,"Script replace_period.py didn't get a 'match' parameter. Only got %s" %parameters) |
| 54 |
elif parameters["match"].lower() == "one": |
| 55 |
match=TimeOfDay().matchAny(parameters["when"]) != inverse |
| 56 |
elif parameters["match"].lower() == "all": |
| 57 |
match=TimeOfDay().matchAll(parameters["when"]) != inverse |
| 58 |
else: |
| 59 |
Crossfire.Log(Crossfire.LogError,"Script replace_period.py didn't get a 'match' parameter. Only got %s" %parameters) |
| 60 |
|
| 61 |
if ( match != alreadymatched ): |
| 62 |
#Crossfire.Log(Crossfire.LogDebug, "replace_all_periods") |
| 63 |
event = Crossfire.WhatIsEvent() |
| 64 |
current = Crossfire.WhoAmI() |
| 65 |
future = event.Inventory |
| 66 |
# we do not want to repace a monster/anything useable in game by a subevent on the event. |
| 67 |
# seems for technical reasons, EVENT object have a destroy subevent. So let's just |
| 68 |
# ignore subevents |
| 69 |
while ( (future != None) & (future.Type == Crossfire.Type.EVENT_CONNECTOR)): |
| 70 |
future=future.Below |
| 71 |
if (future): |
| 72 |
#if (future.Below): |
| 73 |
#Crossfire.Log(Crossfire.LogDebug, "future.Below is %s" %future.Below.Name) |
| 74 |
#Crossfire.Log(Crossfire.LogDebug, "current is %s, future is %s, event is %s" %(current.Name, future.Name, event.Name)) |
| 75 |
if (current.Env): |
| 76 |
#Crossfire.Log(Crossfire.LogDebug, "env mode") |
| 77 |
env = current.Env |
| 78 |
future.InsertInto(env) |
| 79 |
event.InsertInto(future) |
| 80 |
current.InsertInto(event) |
| 81 |
if (alreadymatched): |
| 82 |
event.Value=0 |
| 83 |
else: |
| 84 |
event.Value=1 |
| 85 |
elif (current.Map): |
| 86 |
#Crossfire.Log(Crossfire.LogDebug, "Map mode") |
| 87 |
mymap = current.Map |
| 88 |
x = current.X |
| 89 |
y = current.Y |
| 90 |
#Crossfire.Log(Crossfire.LogDebug, "inserting future %s in map" %future.Name) |
| 91 |
mymap.Insert(future,x,y) |
| 92 |
#Crossfire.Log(Crossfire.LogDebug, "inserting event %s in future" %event.Name) |
| 93 |
event.InsertInto(future) |
| 94 |
#Crossfire.Log(Crossfire.LogDebug, "inserting current %s in event" %current.Name) |
| 95 |
current.InsertInto(event) |
| 96 |
if (alreadymatched): |
| 97 |
event.Value=0 |
| 98 |
else: |
| 99 |
event.Value=1 |
| 100 |
#else: |
| 101 |
#Crossfire.Log(Crossfire.LogDebug, "neither env object nor map found") |