Crossfire Server, Trunk
python.py
Go to the documentation of this file.
1 import Crossfire
2 import random
3 #import CFLog
4 
5 Crossfire.SetReturnValue( 1 )
6 
7 whoami=Crossfire.WhoAmI()
8 who = Crossfire.WhoIsActivator()
9 
10 def do_help():
11  help = ''
12  help += 'Usage: say <test name>\nAvailable tests:\n'
13  help += ' - arch: archetypes-related tests\n'
14  help += ' - maps: maps-related tests\n'
15  help += ' - party: party-related tests\n'
16  help += ' - region: party-related tests\n'
17  help += ' - ref: some checks on objects references\n'
18  help += ' - mark: marked item\n'
19  help += ' - memory: storage-related tests\n'
20  help += ' - time: time of day tests\n'
21  help += ' - timer: timer activation test\n'
22  help += ' - timer_kill: kill specified timer\n'
23  help += ' - misc: other tests\n'
24  help += ' - exp\n'
25  help += ' - const: constants and such\n'
26  help += ' - move\n'
27  help += ' - bed\n'
28  help += ' - readkey\n'
29  help += ' - writekey\n'
30  help += ' - speed\n'
31  help += ' - owner\n'
32  help += ' - friendlylist\n'
33  help += ' - create\n'
34  help += ' - directory\n'
35  help += ' - event\n'
36  help += ' - light\n'
37  help += ' - attacktype\n'
38  help += ' - players\n'
39  help += ' - checkinv\n'
40  help += ' - face\n'
41  help += ' - anim\n'
42  help += ' - hook\n'
43  help += ' - checkinventory\n'
44  help += ' - nosave\n'
45  help += ' - move_to\n'
46  help += ' - attr: object-attribute tests'
47 
48  whoami.Say(help)
49 
50 def do_arch():
51  archs = Crossfire.GetArchetypes()
52  whoami.Say('%d archetypes'%len(archs))
53  which = random.randint(0,len(archs))
54  arch = archs[which]
55  whoami.Say('random = %s'%arch.Name)
56  head = ''
57  more = ''
58  next = ''
59  if (arch.Head):
60  head = arch.Head.Name
61  if (arch.More):
62  more = arch.More.Name
63  if (arch.Next):
64  next = arch.Next.Name
65  whoami.Say(' head = %s, more = %s, clone = %s, next = %s'%(head, more, arch.Clone.Name, next))
66 
67  arch = who.Archetype
68  whoami.Say('your archetype is %s'%arch.Name)
69 
70 def do_maps():
71  whoami.Say('Current map is %s'%who.Map.Name)
72  maps = Crossfire.GetMaps()
73  whoami.Say('%d maps loaded:'%len(maps))
74  list = '\n'
75  for map in maps:
76  list += '%s [%d] -> %d players\n'%(map.Name, map.Unique, map.Players)
77  whoami.Say(list)
78 #activator=Crossfire.WhoIsActivator()
79  whoami.Say('this map is %s, size %d, %d'%(whoami.Map.Name, whoami.Map.Width, whoami.Map.Height))
80  if (len(topic) > 1):
81  flag = 0
82  if len(topic) > 2:
83  flag = int(topic[2])
84  ready = Crossfire.ReadyMap(topic[1], flag)
85  if (ready):
86  whoami.Say('ok, loaded %d map %s'%(flag,ready.Name))
87  else:
88  whoami.Say('can\'t load %d map %s'%(flag,topic[1]))
89 
90 def do_party():
91  parties = Crossfire.GetParties()
92  whoami.Say('%d parties'%len(parties))
93  for party in parties:
94  whoami.Say('%s (%s)'%(party.Name, party.Password))
95  players = party.GetPlayers()
96  for player in players:
97  whoami.Say(' %s'%player.Name)
98  if len(parties) >= 2:
99  who.Party = parties[1]
100  whoami.Say('changed your party!')
101 
102 def do_region():
103  msg = 'Known regions, region for current map is signaled by ***\n'
104  cur = whoami.Map.Region
105  msg += 'This map\'s region is %s (msg: %s)\n'%(cur.Name, cur.Message)
106  regions = Crossfire.GetRegions()
107  msg += ('%d regions\n'%len(regions))
108  for region in regions:
109  if cur == region:
110  msg += ('*** %s - %s\n'%(region.Name,region.Longname))
111  else:
112  msg += ('%s - %s\n'%(region.Name,region.Longname))
113  parent = cur.GetParent()
114  if parent:
115  msg += ('Parent is %s\n'%parent.Name)
116  else:
117  msg += ('Region without parent\n')
118 
119  msg += "Jail: %s (%d,%d)"%(cur.JailPath, cur.JailX, cur.JailY)
120 
121  whoami.Say(msg)
122 
124  who2 = Crossfire.WhoIsOther()
125  who3 = Crossfire.WhoAmI()
126  who = 0
127  who2 = 0
128  who3 = 0
129  whoami.Say('let\'s hope no reference crash!')
130 
131 def do_marker():
132  obj = who.MarkedItem
133  if obj:
134  whoami.Say(' your marked item is: %s'%obj.Name)
135  mark = obj.Below
136  else:
137  whoami.Say(' no marked item')
138  mark = who.Inventory
139  while (mark) and (mark.Invisible):
140  mark = mark.Below
141  who.MarkedItem = mark
142  whoami.Say('Changed marked item!')
143 
144 def do_memory():
145  whoami.Say('Value save test')
146  dict = Crossfire.GetPrivateDictionary()
147  if 's' in dict:
148  x = dict['s']
149  whoami.Say(' x was %d'%x)
150  x = x + 1
151  else:
152  x = 0
153  whoami.Say(' new x')
154 
155  dict['s'] = x
156 
157 
158 def do_resist():
159  whoami.Say('Resistance test')
160  for r in range(25):
161  whoami.Say(' %d -> %d'%(r,who.GetResist(r)))
162 
163 def do_basics():
164  msg = 'Basic test\n'
165  msg += ' your type is %d\n'%who.Type
166  msg += ' your race is %s\n'%who.Race
167  msg += ' your level is %d\n'%who.Level
168  msg += ' your nrof is %d\n'%who.Quantity
169  msg += ' your weight is %d\n'%who.Weight
170  msg += ' your name is %s\n'%who.Name
171  msg += ' your archname is %s\n'%who.ArchName
172  msg += ' your title is %s\n'%who.Title
173  msg += ' your ip is %s\n'%who.IP
174  msg += ' my name is %s\n'%whoami.Name
175  msg += ' your permanent exp is %d\n' % who.PermExp()
176  whoami.Say(msg)
177 
178 def do_time():
179  cftime = Crossfire.GetTime()
180  whoami.Say('Year: %d'%cftime[0])
181  whoami.Say('Month: %d'%cftime[1])
182  whoami.Say('Day: %d'%cftime[2])
183  whoami.Say('Hour: %d'%cftime[3])
184  whoami.Say('Minute: %d'%cftime[4])
185  whoami.Say('Day of week: %d'%cftime[5])
186  whoami.Say('Week of year: %d'%cftime[6])
187  whoami.Say('Season: %d'%cftime[7])
188 
189 def do_timer():
190  id = whoami.CreateTimer(3,1)
191  if id >= 0:
192  whoami.Say('The countdown started with a 3 second delay, timerid = %d'%id)
193  else:
194  whoami.Say('Timer failure: %d'%id)
195 
197  if ( len(topic) < 2 ):
198  whoami.Say('Kill which timer?')
199  else:
200  timer = int(topic[1])
201  res = Crossfire.DestroyTimer(timer)
202  whoami.Say('Timer %d removed with code %d'%(timer,res))
203 
204 def do_misc():
205  inv = whoami.Inventory
206  if inv != 0:
207  whoami.Say("First inv = %s"%inv.Name)
208  whoami.Say("Inv.Env = %s"%inv.Env.Name)
209  else:
210  whoami.Say("Empty inv??")
211 
212  if len(topic) > 1:
213  map = Crossfire.MapHasBeenLoaded(topic[1])
214  if map:
215  whoami.Say('map %s is loaded, size = %d, %d'%(map.Name, map.Width, map.Height))
216  else:
217  whoami.Say('map %s is not loaded'%topic[1])
218 
220  whoami.Say('You have:')
221  inv = who.Inventory
222  while inv:
223  whoami.Say('%s (type = %d, subtype = %d)'%(inv.Name, inv.Type, inv.Subtype))
224  inv = inv.Below
225 
226 def do_exp():
227  if ( len(topic) < 2 ):
228  whoami.Say("Your exp is %d, perm is %d, mult is %d"%(who.Exp, who.PermExp, who.ExpMul))
229  whoami.Say("Syntax is: exp <value> [option] [skill]")
230  else:
231  value = int(topic[1])
232  skill = ""
233  arg = 0
234  if ( len(topic) > 2 ):
235  arg = int(topic[2])
236  if ( len(topic) > 3):
237  i = 3
238  while ( i < len(topic) ):
239  skill = skill + topic[i] + ' '
240  i = i + 1
241  skill = skill.rstrip()
242  who.AddExp(value, skill, arg)
243  whoami.Say("ok, added %d exp to %s"%(value,skill))
244 
245 def do_const():
246  ret = '\n'
247  ret += "%s => %d\n"%(Crossfire.DirectionName[Crossfire.Direction.NORTH],Crossfire.Direction.NORTH)
248  ret += "Player type => %d\n"%Crossfire.Type.PLAYER
249  ret += "Move Fly High => %d\n"%Crossfire.Move.FLY_HIGH
250  ret += "MessageFlag NDI_BLUE => %d\n"%Crossfire.MessageFlag.NDI_BLUE
251  ret += "CostFlag F_NO_BARGAIN => %d\n"%Crossfire.CostFlag.NOBARGAIN
252  ret += "AttackMovement PETMOVE => %d\n"%Crossfire.AttackMovement.PETMOVE
253  whoami.Say(ret)
254 
255 def dump_move(title, move):
256  moves = [
257  Crossfire.Move.WALK,
258  Crossfire.Move.FLY_LOW,
259  Crossfire.Move.FLY_HIGH,
260  Crossfire.Move.FLYING,
261  Crossfire.Move.SWIM,
262  Crossfire.Move.BOAT ]
263  s = title + ':'
264  for t in moves:
265  if move & t:
266  s = s + ' ' + Crossfire.MoveName[t]
267  return s
268 
269 def do_move():
270  whoami.Say(dump_move("movetype", who.MoveType))
271 
272 def do_bed():
273  whoami.Say("bed = %s at (%d, %d)"%(who.BedMap, who.BedX, who.BedY))
274  whoami.Say("changing to +1 -1")
275  who.BedX = who.BedX + 1
276  who.BedY = who.BedY - 1
277  whoami.Say("bed = %s at (%d, %d)"%(who.BedMap, who.BedX, who.BedY))
278  whoami.Say("resetting.")
279  who.BedX = who.BedX - 1
280  who.BedY = who.BedY + 1
281 
283  if (len(topic) < 2):
284  whoami.Say('read what key?')
285  return
286  whoami.Say('key %s = %s'%(topic[1], who.ReadKey(topic[1])))
287 
289  if (len(topic) < 3):
290  whoami.Say('syntax is writekey key add_update [value]')
291  return
292  val = ''
293  if (len(topic) > 3):
294  val = topic[3]
295 
296  whoami.Say('writekey returned %d'%who.WriteKey(topic[1], val, int(topic[2])))
297 
298 def do_speed():
299  whoami.Say('Your speed is %f and your speed_left %f'%(who.Speed, who.SpeedLeft))
300 # who.Speed = 0.2
301  who.SpeedLeft = -50
302  whoami.Say('Changed your speed, now %f and %f'%(who.Speed, who.SpeedLeft))
303 
304 def do_owner():
305  whoami.Say('Not implemented.')
306 
308  friends = Crossfire.GetFriendlyList()
309  for ob in friends:
310  if (ob.Owner):
311  n = ob.Owner.Name
312  else:
313  n = ''
314  whoami.Say(' - %s (%s)'%(ob.Name, n))
315 
316 def do_create():
317  first = Crossfire.CreateObjectByName('gem')
318  if (first):
319  whoami.Say('created gem: %s'%first.Name)
320  first.Teleport(whoami.Map, 2, 2)
321  second = Crossfire.CreateObjectByName('diamond')
322  if (second):
323  whoami.Say('created diamond: %s'%second.Name)
324  second.Teleport(whoami.Map, 2, 2)
325 
327  whoami.Say('map = %s'%Crossfire.MapDirectory())
328  whoami.Say('unique = %s'%Crossfire.UniqueDirectory())
329  whoami.Say('temp = %s'%Crossfire.TempDirectory())
330  whoami.Say('config = %s'%Crossfire.ConfigDirectory())
331  whoami.Say('local = %s'%Crossfire.LocalDirectory())
332  whoami.Say('player = %s'%Crossfire.PlayerDirectory())
333  whoami.Say('data = %s'%Crossfire.DataDirectory())
334  whoami.Say('scriptname = %s'%Crossfire.ScriptName())
335 
336 def do_event():
337  whoami.Say('event title = %s' %Crossfire.WhatIsEvent().Title)
338  whoami.Say('event slaying = %s' %Crossfire.WhatIsEvent().Slaying)
339  whoami.Say('event msg = %s' %Crossfire.WhatIsEvent().Message)
340 
341 def do_light():
342  whoami.Say('current light: %d'%whoami.Map.Light)
343  if (len(topic) > 1):
344  chg = int(topic[1])
345  whoami.Map.ChangeLight(chg)
346  whoami.Say('new light: %d'%whoami.Map.Light)
347 
349  att = [ Crossfire.AttackType.FIRE, Crossfire.AttackType.COLD, Crossfire.AttackType.ELECTRICITY ]
350  whoami.Say('Your attacktype are:')
351  for at in att:
352  if ( at & Crossfire.WhoIsActivator().AttackType == at):
353  whoami.Say(Crossfire.AttackTypeName[ at ])
354 
356  players = Crossfire.GetPlayers()
357  whoami.Say('Players logged in:')
358  for pl in players:
359  whoami.Say(' - %s'%pl.Name)
360 
362  if len(topic) > 1:
363  what = topic[1]
364  else:
365  what = 'force'
366  find = who.CheckInventory(what)
367  if find:
368  whoami.Say('Found %s in your inventory.'%find.Name)
369  else:
370  whoami.Say('Can\'t find %s in your inventory.'%what)
371 
372 def do_face():
373  obj = whoami.Map.ObjectAt(4, 4)
374  if len(topic) == 1:
375  whoami.Say('Face is %s'%obj.Face)
376  return
377 
378  face = topic[1]
379 
380  try:
381  obj.Face = face
382  whoami.Say('Face changed to %s'%face)
383  except:
384  whoami.Say('Invalid face %s'%face)
385 
386 def do_anim():
387  obj = whoami.Map.ObjectAt(4, 4).Above
388  if len(topic) == 1:
389  whoami.Say('Animation is %s'%obj.Anim)
390  return
391 
392  anim = topic[1]
393  try:
394  obj.Anim = anim
395  whoami.Say('Animation changed to %s'%anim)
396  except:
397  whoami.Say('Invalid animation %s'%anim)
398 
399 def do_hook():
400  item = whoami.Map.CreateObject('food', 0, 0)
401  whoami.Say('Created item.')
402  item2 = whoami.Map.ObjectAt(0, 0)
403  while item2.Above:
404  item2 = item2.Above
405  if item != item2:
406  whoami.Say('Not the same items!')
407  item.Remove()
408  whoami.Say('Trying to access removed item, exception coming')
409  try:
410  item2.Quantity = 1
411  whoami.Say('No exception! Error!')
412  except:
413  whoami.Say('Exception came, ok')
414 
416  if len(topic) == 1:
417  whoami.Say('use: checkinventory <item''s name>')
418  return
419 
420  what = ' '.join(topic[1:])
421  item = who.CheckInventory(what)
422  if item != None:
423  whoami.Say('found item: ' + item.Name)
424  else:
425  whoami.Say('did not find anything matching ' + what)
426 
428  item = whoami.Map.CreateObject('food', 2, 1)
429  item.NoSave = 1
430  whoami.Say('no_save set, the food should not be saved')
431 
433  if whoami.X == 2 and whoami.Y == 2:
434  whoami.WriteKey('dest_x', '0', 1)
435  whoami.WriteKey('dest_y', '4', 1)
436  else:
437  whoami.WriteKey('dest_x', '2', 1)
438  whoami.WriteKey('dest_y', '2', 1)
439 
440 def do_attr():
441  if len(topic) < 2:
442  whoami.Say('Usage: attr name [value], if value is omitted display the value')
443  return
444 
445  if len(topic) == 2:
446  val = getattr(whoami, topic[1])
447  # Not sure of the value of the attribute, so just use Python's type conversion by forcing to str
448  whoami.Say('my %s is %s'%(topic[1], str(val)))
449  return
450 
451  try:
452  setattr(whoami, topic[1], topic[2])
453  except:
454  try:
455  setattr(whoami, topic[1], int(topic[2]))
456  except:
457  try:
458  setattr(whoami, topic[1], float(topic[2]))
459  except:
460  whoami.Say("sorry, I don't know how to set this attribute...")
461 
463  if whoami.ReadKey('dest_x') != '' or whoami.ReadKey('dest_y') != '':
464  return
465 
466  topic = Crossfire.WhatIsMessage().split()
467  #whoami.Say('topic = %s'%topic)
468  #whoami.Say('topic[0] = %s'%topic[0])
469  if topic[0] == 'arch':
470  do_arch()
471  elif topic[0] == 'maps':
472  do_maps()
473  elif topic[0] == 'party':
474  do_party()
475  elif topic[0] == 'region':
476  do_region()
477  elif topic[0] == 'mark':
478  do_marker()
479  elif topic[0] == 'ref':
480  do_activator()
481  elif topic[0] == 'memory':
482  do_memory()
483  elif topic[0] == 'resist':
484  do_resist()
485  elif topic[0] == 'basics':
486  do_basics()
487  elif topic[0] == 'time':
488  do_time()
489  elif topic[0] == 'timer':
490  do_timer()
491  elif topic[0] == 'timer_kill':
492  do_timer_kill()
493  elif topic[0] == 'misc':
494  do_misc()
495  elif topic[0] == 'exp':
496  do_exp()
497  elif topic[0] == 'const':
498  do_const()
499  elif topic[0] == 'move':
500  do_move()
501  elif topic[0] == 'inv':
502  do_inventory()
503  elif topic[0] == 'bed':
504  do_bed()
505  elif topic[0] == 'readkey':
506  do_readkey()
507  elif topic[0] == 'writekey':
508  do_writekey()
509  elif topic[0] == 'speed':
510  do_speed()
511  elif topic[0] == 'owner':
512  do_owner()
513  elif topic[0] == 'friendlylist':
515  elif topic[0] == 'create':
516  do_create()
517  elif topic[0] == 'directory':
518  do_directory()
519  elif topic[0] == 'event':
520  do_event()
521  elif topic[0] == 'light':
522  do_light()
523  elif topic[0] == 'attacktype':
524  do_attacktype()
525  elif topic[0] == 'players':
526  do_players()
527  elif topic[0] == 'checkinv':
528  do_checkinv()
529  elif topic[0] == 'anim':
530  do_anim()
531  elif topic[0] == 'face':
532  do_face()
533  elif topic[0] == 'hook':
534  do_hook()
535  elif topic[0] == 'checkinventory':
537  elif topic[0] == 'nosave':
538  do_no_save()
539  elif topic[0] == 'move_to':
540  do_move_to()
541  elif topic[0] == 'attr':
542  do_attr()
543  else:
544  do_help()
545 
547  x = whoami.ReadKey('dest_x')
548  y = whoami.ReadKey('dest_y')
549  if x == '' or y == '':
550  return
551  x = int(x)
552  y = int(y)
553  result = whoami.MoveTo(x, y)
554  if (result == 0):
555  whoami.WriteKey('dest_x', '', 1)
556  whoami.WriteKey('dest_y', '', 1)
557  whoami.Say("I'm there")
558  elif (result == 2):
559  whoami.Say('blocked...')
560 
561 event = Crossfire.WhatIsEvent()
562 if event.Subtype == Crossfire.EventType.SAY:
563  topic = Crossfire.WhatIsMessage().split()
564  handle_say()
565 elif event.Subtype == Crossfire.EventType.TIME:
566  handle_time()
python.do_friendlylist
def do_friendlylist()
Definition: python.py:307
python.do_checkinv
def do_checkinv()
Definition: python.py:361
python.do_light
def do_light()
Definition: python.py:341
python.do_attacktype
def do_attacktype()
Definition: python.py:348
python.do_readkey
def do_readkey()
Definition: python.py:282
python.do_party
def do_party()
Definition: python.py:90
python.dump_move
def dump_move(title, move)
Definition: python.py:255
python.do_region
def do_region()
Definition: python.py:102
python.do_create
def do_create()
Definition: python.py:316
python.do_check_inventory
def do_check_inventory()
Definition: python.py:415
python.do_owner
def do_owner()
Definition: python.py:304
python.do_activator
def do_activator()
Definition: python.py:123
python.do_resist
def do_resist()
Definition: python.py:158
python.do_help
def do_help()
Definition: python.py:10
python.do_move
def do_move()
Definition: python.py:269
python.do_misc
def do_misc()
Definition: python.py:204
python.do_timer
def do_timer()
Definition: python.py:189
make_face_from_files.str
str
Definition: make_face_from_files.py:30
python.do_inventory
def do_inventory()
Definition: python.py:219
python.do_hook
def do_hook()
Definition: python.py:399
python.do_directory
def do_directory()
Definition: python.py:326
python.do_face
def do_face()
Definition: python.py:372
python.do_writekey
def do_writekey()
Definition: python.py:288
python.do_exp
def do_exp()
Definition: python.py:226
python.do_arch
def do_arch()
Definition: python.py:50
python.handle_time
def handle_time()
Definition: python.py:546
python.do_memory
def do_memory()
Definition: python.py:144
python.do_no_save
def do_no_save()
Definition: python.py:427
python.do_anim
def do_anim()
Definition: python.py:386
python.do_basics
def do_basics()
Definition: python.py:163
python.do_time
def do_time()
Definition: python.py:178
python.do_attr
def do_attr()
Definition: python.py:440
make_face_from_files.int
int
Definition: make_face_from_files.py:32
python.handle_say
def handle_say()
Definition: python.py:462
python.do_speed
def do_speed()
Definition: python.py:298
python.do_marker
def do_marker()
Definition: python.py:131
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2608
python.do_players
def do_players()
Definition: python.py:355
python.do_const
def do_const()
Definition: python.py:245
python.do_timer_kill
def do_timer_kill()
Definition: python.py:196
python.do_move_to
def do_move_to()
Definition: python.py:432
python.do_bed
def do_bed()
Definition: python.py:272
python.do_maps
def do_maps()
Definition: python.py:70
python.do_event
def do_event()
Definition: python.py:336