| 1 |
# CFMapTransformer.py - CFMapTransformer class |
| 2 |
# |
| 3 |
# Copyright (C) 2007 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 |
# The author can be reached via e-mail at [email protected] |
| 20 |
# |
| 21 |
# Small helper class. Instanciate with |
| 22 |
# transform = CFMapTransformer(key) |
| 23 |
# or |
| 24 |
# transform = CFMapTransformer(key, map) |
| 25 |
# default map value is to use The map of current event. |
| 26 |
# |
| 27 |
# The key is used to keep track of transform. Different transforms |
| 28 |
# on same map should use different key to prevent mixup |
| 29 |
|
| 30 |
# transformAll(criteria,whatTo) |
| 31 |
# criteria can be a single String or a list, |
| 32 |
# whatTo can be a single String or a list. |
| 33 |
# This will scan the whole map for item's whose |
| 34 |
# "name" are in the list of criteria and transform |
| 35 |
# the to one of the whatTo objects, choosen randomly. |
| 36 |
# original object is kept in inventory of newly created |
| 37 |
# item and can be restored from there |
| 38 |
# untransformAll() |
| 39 |
# this no argument method cancel all changes that have been |
| 40 |
# made by transformAll |
| 41 |
import Crossfire |
| 42 |
import random |
| 43 |
|
| 44 |
|
| 45 |
def GetObjectMap(o): |
| 46 |
while (o.Env): |
| 47 |
o=o.Env |
| 48 |
if (o.Map): |
| 49 |
return o.Map |
| 50 |
return None |
| 51 |
|
| 52 |
def MakeIdentifier(key): |
| 53 |
m = Crossfire.CreateObjectByName("force") |
| 54 |
m.Name= key |
| 55 |
m.Speed=0 |
| 56 |
return m |
| 57 |
|
| 58 |
class CFMapTransformer: |
| 59 |
key = None |
| 60 |
cfmap = None |
| 61 |
|
| 62 |
def __init__(self,key,cfmap=None): |
| 63 |
self.key=key |
| 64 |
if (cfmap): |
| 65 |
self.cfmap=cfmap |
| 66 |
else: |
| 67 |
o = Crossfire.WhoAmI() |
| 68 |
while (o.Env): |
| 69 |
o = o.Env |
| 70 |
self.cfmap = o.Map |
| 71 |
|
| 72 |
def transformAll(self, criteria, whatTo): |
| 73 |
if (not isinstance (criteria,list)): |
| 74 |
criteria = [criteria] |
| 75 |
for x in range (self.cfmap.Width): |
| 76 |
for y in range (self.cfmap.Height): |
| 77 |
top = self.cfmap.ObjectAt(x,y) |
| 78 |
while (top): |
| 79 |
#print "testing %s at (%d %d) for match against criteria %s" %(top.Name,x,y,criteria) |
| 80 |
next = top.Above |
| 81 |
if (set([top.Name]) & set (criteria) ): |
| 82 |
#print "matched" |
| 83 |
#do the replace |
| 84 |
if isinstance(whatTo,list): |
| 85 |
ob = Crossfire.CreateObjectByName(whatTo[random.randint(0,len(whatTo)-1)]) |
| 86 |
else: |
| 87 |
ob = Crossfire.CreateObjectByName(whatTo) |
| 88 |
force = MakeIdentifier(self.key) |
| 89 |
force.InsertInto(ob) |
| 90 |
top.InsertInto(ob) |
| 91 |
ob.Pickable=False |
| 92 |
self.cfmap.Insert(ob,x,y) |
| 93 |
#handle living stuff by freezing them |
| 94 |
force.WriteKey("inside_speed","%f" %top.Speed,1) |
| 95 |
top.Speed = 0 |
| 96 |
top=next |
| 97 |
|
| 98 |
def untransformAll(self): |
| 99 |
for x in range(self.cfmap.Width): |
| 100 |
for y in range(self.cfmap.Height): |
| 101 |
top = self.cfmap.ObjectAt(x,y) |
| 102 |
while (top): |
| 103 |
#print "checking at (%d,%d) if %s need a restore" %(x,y,top.Name) |
| 104 |
next = top.Above |
| 105 |
match = False |
| 106 |
inv = top.Inventory |
| 107 |
torestore = None |
| 108 |
while(inv): |
| 109 |
#print " checking inventory item %s" %inv.Name |
| 110 |
if (inv.Type == Crossfire.Type.FORCE) and (inv.Name == self.key): |
| 111 |
oldspeed = float(inv.ReadKey("inside_speed")) |
| 112 |
#print "i found the force, luke" |
| 113 |
match = True |
| 114 |
elif (inv.Type != Crossfire.Type.EVENT_CONNECTOR): |
| 115 |
#print "found what to restore" |
| 116 |
torestore = inv |
| 117 |
inv = inv.Below |
| 118 |
if match and (torestore != None): |
| 119 |
#print "found something to restore" |
| 120 |
torestore.Speed = oldspeed |
| 121 |
self.cfmap.Insert(torestore,x,y) |
| 122 |
top.Remove() |
| 123 |
top=next |