home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 56 / CDPowerplay56Disc2.iso / demos / blade / data1.cab / Program_Executable_Files / Lib / Select.py < prev    next >
Encoding:
Text File  |  2000-10-27  |  8.6 KB  |  294 lines

  1. import Bladex
  2. import Reference
  3. import netgame
  4.  
  5.  
  6.  
  7. TRUE=(1==1)
  8. FALSE=(1!=1)
  9.  
  10. if netgame.GetNetState() == 2:
  11.     MAX_VISIBLE_ENEMIES_DISTANCE = 80000
  12.     MAX_SELECTION_DISTANCE=8000.0    
  13. else:
  14. ##    me=Bladex.GetEntity("Player1")
  15. ##    chartype = Bladex.GetCharType(me.CharType,me.CharTypeExt)
  16. ##    MAX_VISIBLE_ENEMIES_DISTANCE=chartype.MaxCombatDist
  17.     MAX_SELECTION_DISTANCE=8000.0
  18.  
  19.  
  20.  
  21.  
  22. AUTOSELECT_PERIOD=0.2
  23. Gpj=None
  24.  
  25. def SqDistanceToGpj(entity):
  26.     global Gpj
  27.     return(Gpj.SQDistance2(entity))
  28.  
  29. def GetSelectionData(entity_name):
  30.     entity=Bladex.GetEntity(entity_name)
  31.     if(Reference.EntitiesSelectionData.has_key(entity_name)):
  32.         return(Reference.EntitiesSelectionData[entity_name])
  33.     elif(Reference.DefaultSelectionData.has_key(entity.Kind)):
  34.         return(Reference.DefaultSelectionData[entity.Kind])
  35.     else: return(None)
  36.  
  37. def IsValidForSelection(entity_entry):
  38.     if(entity_entry[1]):
  39.         entity_name=entity_entry[0]
  40.         entity=Bladex.GetEntity(entity_name)
  41.         if(entity):
  42.             parent_name=entity.Parent;
  43.             if(parent_name!=None):
  44.                 if(parent_name!=""):
  45.                     parent=Bladex.GetEntity(parent_name)
  46.                     if(parent.Person):
  47.                         return(FALSE)
  48.         else:
  49.             return(FALSE)
  50.         if(SqDistanceToGpj(entity)>entity_entry[1][1]*entity_entry[1][1]):
  51.             return(FALSE)
  52.     else:
  53.         return(FALSE)
  54.     return(TRUE)
  55.  
  56. def GetMoreInterestingEntity(pj,list):
  57.     target=None
  58.     priority=0.0
  59.     sqdistance=MAX_SELECTION_DISTANCE*MAX_SELECTION_DISTANCE
  60.     for x in list:
  61.         if(priority<x[1][0] or (priority==x[1][0] and sqdistance>SqDistanceToGpj(Bladex.GetEntity(x[0])))):
  62.             target=x
  63.             priority=x[1][0]
  64.             sqdistance=SqDistanceToGpj(Bladex.GetEntity(x[0]))
  65.     return(target)
  66.  
  67.  
  68. def GetNextMoreInterestingEntity(pj,list,sentry):
  69.     target=None
  70.     priority=0.0
  71.     sqdistance=MAX_SELECTION_DISTANCE*MAX_SELECTION_DISTANCE
  72.  
  73.     spriority=sentry[1][0]
  74.     ssqdistance=SqDistanceToGpj(Bladex.GetEntity(sentry[0]))
  75.     
  76.     for x in list:
  77.         if(priority<x[1][0] or (priority==x[1][0] and sqdistance>SqDistanceToGpj(Bladex.GetEntity(x[0])))):
  78.             if(spriority>x[1][0] or (spriority==x[1][0] and ssqdistance<SqDistanceToGpj(Bladex.GetEntity(x[0])))):
  79.                 target=x
  80.                 priority=x[1][0]
  81.                 sqdistance=SqDistanceToGpj(Bladex.GetEntity(x[0]))
  82.     return(target)
  83.  
  84. def GetFromList(entity_entry,list):
  85.     for x in list:
  86.         if(x[0]==entity_entry[0]):
  87.             return(entity_entry)
  88.     return(None)
  89.  
  90. def GetNextFromList(entity_entry,list):
  91.     for x in range(1,len(list)):
  92.         if(list[x-1][0]==entity_entry[0]):
  93.             return(list[x])
  94.     return(None)
  95.  
  96. def SelectNext(pj):
  97.     global Gpj
  98.     Gpj=pj
  99.     head_pos=pj.Rel2AbsPoint(0.0,0.0,0.0,"Head")
  100.     pj_dir=pj.Rel2AbsVector(0.0,-1.0,0.0)
  101.     list=Bladex.GetObjectEntitiesVisibleFrom(head_pos,MAX_SELECTION_DISTANCE,pj_dir,0.0)
  102.     list=map(None,list,map(GetSelectionData,list))
  103.     list=filter(IsValidForSelection,list)
  104.  
  105.     if(pj.Data.selection_locked and pj.Data.selected_entity):
  106.         target=GetFromList(pj.Data.selected_entity,list)
  107.         if(not target):
  108.             pj.Data.selection_locked=FALSE
  109.             target=GetMoreInterestingEntity(pj,list)
  110.         else:
  111.             target=GetNextMoreInterestingEntity(pj,list,pj.Data.selected_entity)
  112.             if(not target):
  113.                 target=GetMoreInterestingEntity(pj,list)
  114.     else:
  115.         pj.Data.selection_locked=FALSE
  116.         target=GetMoreInterestingEntity(pj,list)
  117.     
  118.     pj.Data.selected_entity=target
  119.  
  120.     if(target):
  121.         pj.LookAtEntity(target[0])
  122.     else:
  123.         pj.LookAtEntity("")
  124.         pj.StopLooking()
  125.  
  126.  
  127. def AutoSelect(pj):
  128.     global Gpj
  129.     Gpj=pj
  130.     head_pos=pj.Rel2AbsPoint(0.0,0.0,0.0) #,"Head")
  131.     pj_dir=pj.Rel2AbsVector(0.0,-1.0,0.0)
  132.     list=Bladex.GetObjectEntitiesVisibleFrom(head_pos,MAX_SELECTION_DISTANCE,pj_dir,0.0)
  133.     list=map(None,list,map(GetSelectionData,list))
  134.     list=filter(IsValidForSelection,list)
  135.  
  136.     if(pj.Data.selection_locked and pj.Data.selected_entity):
  137.         target=GetFromList(pj.Data.selected_entity,list)
  138.         if(not target):
  139.             pj.Data.selection_locked=FALSE
  140.             target=GetMoreInterestingEntity(pj,list)
  141.     else:
  142.         pj.Data.selection_locked=FALSE
  143.         target=GetMoreInterestingEntity(pj,list)
  144.     
  145.     pj.Data.selected_entity=target
  146.  
  147.  
  148. def GetEnemiesScorerData(entity_name):
  149.     entity=Bladex.GetEntity(entity_name)
  150.     if(Reference.EnemiesScorerData.has_key(entity_name)):
  151.         return(Reference.EnemiesScorerData[entity_name])
  152.     elif(Reference.EnemiesDefaultScorerData.has_key(entity.MeshName)):
  153.         return(Reference.EnemiesDefaultScorerData[entity.MeshName])
  154.     else:
  155.         if(Reference.EnemiesDefaultScorerData.has_key(entity.Kind)):
  156.             return(Reference.EnemiesDefaultScorerData[entity.Kind])
  157.         else:
  158.             return(None)
  159.  
  160. def Distance2Pj(enemy_entry):
  161.     global Gpj
  162.     enemy=Bladex.GetEntity(enemy_entry[0])
  163.     return(Gpj.SQDistance2(enemy))
  164.  
  165.  
  166. def ShortEnemiesList(pj):
  167.     global Gpj
  168.     Gpj=pj    
  169.     list=pj.Data.visible_enemies
  170.     if(len(list)):
  171.         distances=map(Distance2Pj,list)
  172.         for i in range(len(list)-1):
  173.             for j in range(len(list)-1):
  174.                 if(distances[j]>distances[j+1]):
  175.                     tmp=distances[j]
  176.                     distances[j]=distances[j+1]
  177.                     distances[j+1]=tmp
  178.                     tmp=list[j]
  179.                     list[j]=list[j+1]
  180.                     list[j+1]=tmp
  181.                 
  182.  
  183.  
  184. def SelectNextEnemy(pj):
  185.     chartype = Bladex.GetCharType(pj.CharType,pj.CharTypeExt)
  186.     MAX_VISIBLE_ENEMIES_DISTANCE=chartype.MaxCombatDist
  187.     head_pos=pj.Rel2AbsPoint(0.0,0.0,0.0) #,"Head")
  188.     pj_dir=pj.Rel2AbsVector(0.0,-1.0,0.0)
  189.     list=Bladex.GetEnemiesVisibleFrom(head_pos,MAX_VISIBLE_ENEMIES_DISTANCE,pj_dir,0.0)
  190.     list=map(None,list,map(GetEnemiesScorerData,list))
  191.     pj.Data.visible_enemies=list
  192.     if(len(pj.Data.visible_enemies)):
  193.         ShortEnemiesList(pj)
  194.         if(pj.Data.enemy_locked and pj.Data.selected_enemy):
  195.             target=GetNextFromList(pj.Data.selected_enemy,list)
  196.             if(not target):
  197.                 pj.Data.enemy_locked=FALSE
  198.                 pj.Data.selected_enemy=pj.Data.visible_enemies[0]
  199.             else:
  200.                 pj.Data.selected_enemy=target
  201.         else:
  202.             pj.Data.selected_enemy=pj.Data.visible_enemies[0]
  203.     else:
  204.         pj.Data.enemy_locked=FALSE
  205.         pj.Data.selected_enemy=None
  206.  
  207.  
  208. import pdb
  209.  
  210. def GetVisibleEnemies(pj):
  211.     head_pos=pj.Rel2AbsPoint(0.0,0.0,0.0) #,"Head")
  212.     
  213.     if ( pj.AnimName=="g_back" or pj.AnimName=="g2h_back") and pj.AnmPos>0.6:    
  214.         dir_y=1.0        
  215.     else:
  216.         dir_y=-1.0
  217.     chartype = Bladex.GetCharType(pj.CharType,pj.CharTypeExt)
  218.     MAX_VISIBLE_ENEMIES_DISTANCE=chartype.MaxCombatDist
  219.     pj_dir=pj.Rel2AbsVector(0.0,dir_y,0.0)
  220.     list=Bladex.GetEnemiesVisibleFrom(head_pos,MAX_VISIBLE_ENEMIES_DISTANCE,pj_dir,0.0)
  221.     list=map(None,list,map(GetEnemiesScorerData,list))
  222.     pj.Data.visible_enemies=list
  223.     if(len(pj.Data.visible_enemies)):
  224.         ShortEnemiesList(pj)
  225.         if(pj.Data.enemy_locked and pj.Data.selected_enemy):
  226.             target=GetFromList(pj.Data.selected_enemy,list)
  227.             if(not target):
  228.                 pj.Data.enemy_locked=FALSE
  229.                 pj.Data.selected_enemy=pj.Data.visible_enemies[0]
  230.         else:
  231.             pj.Data.selected_enemy=pj.Data.visible_enemies[0]
  232.     else:
  233.         pj.Data.enemy_locked=FALSE
  234.         pj.Data.selected_enemy=None
  235.  
  236.  
  237.  
  238. def PeriodicAutoSelect(pj_name, look_for_enemies=1, autoselect_period=AUTOSELECT_PERIOD):
  239.     import DefaultSelectionData
  240.  
  241.     pj=Bladex.GetEntity(pj_name) # Puede ser None
  242.     
  243.     if pj and pj.Data.auto_select:
  244.         AutoSelect(pj)
  245.         GetVisibleEnemies(pj)
  246.     
  247.         if(pj.Data.selected_enemy):
  248.             time_flag=FALSE
  249.             try:
  250.                 if Bladex.GetTime()-me.Data.time_deactive_enemy>Bladex.GetAecGap():
  251.                     time_flag=TRUE
  252.             except:
  253.                 time_flag=TRUE
  254.  
  255.             if not pj.Data.enemy_locked and Bladex.GetAutoEngageCombat() and time_flag and not pj.Data.previous_visible_enemies:
  256.                 DefaultSelectionData.SelectEnemy()
  257.             pj.LookAtEntity(pj.Data.selected_enemy[0])
  258.             pj.Data.previous_visible_enemies=TRUE
  259.         else:
  260.             pj.Data.previous_visible_enemies=FALSE
  261.             if(pj.Data.selected_entity):
  262.                 pj.LookAtEntity(pj.Data.selected_entity[0])
  263.                 if(pj.Data.selected_entity and (not pj.Data.selection_locked) and (pj.Data.selected_entity[1][0]>6)):
  264.                     DefaultSelectionData.SelectObject()
  265.             else:
  266.                 pj.LookAtEntity("")
  267.                 pj.StopLooking()
  268.  
  269.  
  270.  
  271.  
  272.         Bladex.AddScheduledFunc(Bladex.GetTime()+autoselect_period,PeriodicAutoSelect,(pj_name,look_for_enemies,autoselect_period),"PeriodicAutoSelect"+pj_name)
  273.  
  274.  
  275. def TurnOnAutoSelect(pj_name, look_for_enemies=1, autoselect_period=AUTOSELECT_PERIOD):
  276.     pj=Bladex.GetEntity(pj_name)
  277.     pj.Data.selected_entity=None
  278.     pj.Data.selection_locked=FALSE
  279.     pj.Data.visible_enemies=()
  280.     pj.Data.selected_enemy=None
  281.     pj.Data.enemy_locked=FALSE
  282.     pj.Data.auto_select=TRUE
  283.     pj.Data.previous_visible_enemies=FALSE
  284.  
  285.     # Make sure that we don't have multiple  versions running at the same time
  286.     Bladex.RemoveScheduledFunc("TurnOnAutoSelect"+pj_name)
  287.     Bladex.RemoveScheduledFunc("PeriodicAutoSelect"+pj_name)
  288.  
  289.     Bladex.AddScheduledFunc(Bladex.GetTime()+autoselect_period,PeriodicAutoSelect,(pj_name,look_for_enemies,autoselect_period),"TurnOnAutoSelect"+pj_name)
  290.  
  291. def TurnOffAutoSelect(pj_name):
  292.     pj=Bladex.GetEntity(pj_name)
  293.     pj.Data.auto_select=FALSE
  294.