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