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