home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2001 January / Game.EXE_01_2001.iso / demos / Blade of Darkness / data1.cab / Program_Executable_Files / Scripts / PickInit.py < prev    next >
Encoding:
Text File  |  2000-11-16  |  6.5 KB  |  311 lines

  1.  
  2.  
  3.  
  4. import Bladex
  5. import copy_reg
  6. import types
  7.  
  8.  
  9. GlobalModulesCache=None
  10. GlobalFunctionsCache=None
  11. GlobalCFunctionsCache=None
  12.  
  13. def GetGlobalsAux():
  14.     import sys
  15.     try:
  16.         1 + ''
  17.     except:
  18.         frame = sys.exc_info()[2].tb_frame.f_back
  19.  
  20.     while frame:
  21.         globs=frame.f_globals
  22.         frame=frame.f_back
  23.  
  24.     return globs
  25.  
  26.  
  27. def GetGlobalsAux2(req_type):
  28. ##    global GlobalModulesCache
  29. ##    global GlobalFunctionsCache
  30. ##    global GlobalCFunctionsCache
  31. ##
  32. ##    if req_type==types.ModuleType and GlobalModulesCache:
  33. ##        return GlobalModulesCache
  34. ##    elif (req_type==types.FunctionType or req_type==types.MethodType) and GlobalFunctionsCache:
  35. ##        return GlobalFunctionsCache
  36. ##    elif req_type==types.BuiltinFunctionType and GlobalCFunctionsCache:
  37. ##        return GlobalCFunctionsCache
  38.  
  39.     g=GetGlobalsAux()
  40.     elems=[]
  41.     for i in g.items():
  42.         if type(i[1])==req_type:
  43.             elems.append(i)
  44.  
  45. ##    if req_type==types.ModuleType:
  46. ##        GlobalModulesCache=elems
  47. ##    elif req_type==types.FunctionType or req_type==types.MethodType:
  48. ##        GlobalFunctionsCache=elems
  49. ##    elif req_type==types.BuiltinFunctionType:
  50. ##        GlobalCFunctionsCache=elems
  51.     return elems
  52.  
  53.  
  54. def ConstSound(sound_name,sound_file,volume,base_volume,min_distance,max_distance,scale,send_notify):
  55.   s=Bladex.CreateSound(sound_file,sound_name)
  56. ##  print s
  57.   if not s:
  58.     return
  59.   s.Volume=volume
  60.   s.BaseVolume=base_volume
  61.   s.MinDistance=min_distance
  62.   s.MaxDistance=max_distance
  63.   s.Scale=scale
  64.   s.SendNotify=send_notify
  65.   return s
  66.  
  67.  
  68. def RedSound(s):
  69.   return ConstSound,(s.Name,"",s.Volume,s.BaseVolume,s.MinDistance,s.MaxDistance,s.Scale,s.SendNotify)
  70.  
  71.  
  72. def RegisterPickSound():
  73.   #Creo uno cualquiera.  ┐Revisar?
  74.   gmadlig=Bladex.CreateSound('../../sounds/golpe-madera-mediana.wav', 'GolpeMaderaMediana')
  75.  
  76.   copy_reg.pickle(type(gmadlig),RedSound,ConstSound)
  77.  
  78.  
  79.  
  80.  
  81.  
  82. def ConstEntity(ent_name):
  83.   e=Bladex.GetEntity(ent_name)
  84.   return e
  85.  
  86.  
  87. def RedEntity(e):
  88.  
  89.   if e:
  90.     try:
  91.       return ConstEntity,(e.Name,)
  92.     except:
  93.       print "PickInit.RedEntity() can not get entity name"
  94.       return ConstEntity,("Invalid Entity",)
  95.  
  96.   return ConstEntity,("Invalid Entity",)
  97.  
  98.  
  99. def RegisterPickEntity():
  100.   #Creo uno cualquiera.  ┐Revisar?
  101.   #gmadlig=Bladex.CreateEntity('PickEntity','Entity Spot',0,0,0)
  102.   gmadlig=Bladex.GetEntity(0)
  103.  
  104.   copy_reg.pickle(type(gmadlig),RedEntity,ConstEntity)
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. def FindFunctionAux(module,fun_name):
  116.  
  117.   if module.__dict__.has_key(fun_name):
  118.     return module.__dict__[fun_name]
  119.   return None
  120.  
  121.  
  122.  
  123. def ConstFunction(fun_name,lib_name):
  124.  
  125.   funcs=GetGlobalsAux2(types.FunctionType)
  126.   for i in funcs:
  127.     if i[1].func_name==fun_name:
  128.       return i[1]
  129.  
  130.   # La busco en los modulos
  131.   global_mods=GetGlobalsAux2(types.ModuleType)
  132.   # Primero miro si hay concordancia con lib_name
  133.   for i in global_mods:
  134.       if i[0]==lib_name and i[1].__dict__.has_key(fun_name):
  135.           return i[1].__dict__[fun_name]
  136.  
  137.   for i in global_mods:
  138.     func=FindFunctionAux(i[1],fun_name)
  139.     if func:
  140.         return func
  141.  
  142.   print "Warning, can't find global function '",fun_name,"'",lib_name,"'"
  143.   return None
  144.  
  145.  
  146.  
  147. def RedFunction(f):
  148.   import GameStateAux
  149.   s=GameStateAux.GetFunctionFile(f)
  150.  
  151.   return ConstFunction,(f.func_name,s)
  152.  
  153.  
  154. def RegisterPickFunction():
  155.   import types
  156.  
  157.   copy_reg.pickle(types.FunctionType,RedFunction,ConstFunction)
  158.  
  159.  
  160.  
  161.  
  162.  
  163. def ConstMethod(obj_id,method_name):
  164.   import types
  165.   import ObjStore
  166.  
  167.   try:
  168.     obj=ObjStore.ObjectsStore[obj_id]
  169.     assign_func=eval("obj."+method_name)
  170.     return assign_func
  171.   except Exception,exc:
  172. ##    import pdb
  173. ##    pdb.set_trace()
  174.     print "PickInit.ConstMethod() can not find method",obj_id,method_name
  175.     if ObjStore.ObjectsStore.has_key(obj_id):
  176.         print "Object ",obj_id,"exists. -> ",ObjStore.ObjectsStore[obj_id]
  177.     else:
  178.         print "Object ",obj_id,"does not exist."
  179.         print ObjStore.ObjectsStore
  180.  
  181.     print "Exception",exc
  182.     return None
  183.  
  184. def RedMethod(f):
  185.   try:
  186.     return ConstMethod,(f.im_class.persistent_id(f.im_self),f.im_func.func_name)
  187.   except:
  188.     print "PickInit.RedMethod() can not register method",f
  189.     return ConstMethod,(None,None)
  190.  
  191.  
  192. def RegisterPickMethod():
  193.   import types
  194.  
  195.   copy_reg.pickle(types.MethodType,RedMethod,ConstMethod)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. def ConstCFunction(fun_name,ent_name):
  203.  
  204.   print "ConstCFunction: '",fun_name,"','",ent_name,"'"
  205.   if ent_name: #Si es una entidad
  206.     import Bladex
  207.     ent=Bladex.GetEntity(ent_name)
  208.     if ent:
  209.         assign_func=eval("ent."+fun_name)
  210.         return assign_func
  211.     else: # La entidad ha desaparecido.
  212.         print "Can not find entity",ent_name
  213.         return None
  214.  
  215.   # La busco en funciones C
  216.   funcs=GetGlobalsAux2(types.BuiltinFunctionType)
  217.   for i in funcs:
  218.     if i[1].__name__==fun_name:
  219.       return i[1]
  220.   # La busco en los modulos
  221.   # Primero en los de Blade
  222.   import Bladex
  223.   import Traps_C
  224.   import B3DLib
  225.   mods=(Bladex,B3DLib,Traps_C)
  226.   for i in mods:
  227.     func=FindFunctionAux(i,fun_name)
  228.     if func:
  229.         return func
  230.   # Y luego en los otros
  231.   global_mods=GetGlobalsAux2(types.ModuleType)
  232.   for i in global_mods:
  233.     if i not in mods:
  234.         func=FindFunctionAux(i[1],fun_name)
  235.         if func:
  236.             return func
  237.  
  238.   print "Warning, can't find global function",fun_name
  239.   return None
  240.  
  241.  
  242.  
  243. def RedCFunction(f):
  244.   if f.__self__: # Asume que es una entidad
  245.     return ConstCFunction,(f.__name__,f.__self__.Name)
  246.   else:
  247.     return ConstCFunction,(f.__name__,None)
  248.  
  249.  
  250. def RegisterPickCFunction():
  251.   import types
  252.  
  253.   copy_reg.pickle(types.BuiltinFunctionType,RedCFunction,ConstCFunction)
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271. def ConstSector(sec_idx):
  272.   e=Bladex.GetSector(sec_idx)
  273.   return e
  274.  
  275. def RedSector(s):
  276.   return ConstSector,(s.Index,)
  277.  
  278.  
  279. def RegisterPickSector():
  280.   gmadlig=Bladex.GetSector(0)
  281.   if not gmadlig:
  282.       print "ERROR in RegisterPickSector()"
  283.       return
  284.  
  285.   copy_reg.pickle(type(gmadlig),RedSector,ConstSector)
  286.  
  287.  
  288.  
  289.  
  290. def ClearCaches():
  291.   global GlobalModulesCache
  292.   global GlobalFunctionsCache
  293.   global GlobalCFunctionsCache
  294.  
  295.   GlobalModulesCache=None
  296.   GlobalFunctionsCache=None
  297.   GlobalCFunctionsCache=None
  298.  
  299.  
  300.  
  301.  
  302. def Init():
  303.   ClearCaches()
  304.   RegisterPickSound()
  305.   RegisterPickEntity()
  306.   RegisterPickFunction()
  307.   RegisterPickSector()
  308.   RegisterPickMethod()
  309.   RegisterPickCFunction()
  310.   print "Executed PickInit.Init()"
  311.