Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
fg_util Namespace Reference

Functions

def MaxNumFlightgroupsInSystem
 
def MinNumFlightgroupsInSystem
 
def MaxNumBasesInSystem
 
def MinNumBasesInSystem
 
def MakeFactionKey
 
def MakeFGKey
 
def MakeStarSystemFGKey
 
def MakeStarSystemFactionFGKey
 
def ShipListOffset
 
def PerShipDataSize
 
def AllFactions
 
def ReadBaseNameList
 
def GetRandomFGNames
 
def GetRandomBaseName
 
def TweakFGNames
 
def maxTweakFGOffset
 
def WriteStringList
 
def ReadStringList
 
def AllFlightgroups
 
def NumAllFlightgroups
 
def RandomFlightgroup
 
def ListToPipe
 
def CheckFG
 
def PurgeZeroShips
 
def NumShipsInFG
 
def GetDamageInFGPool
 
def SetDamageInFGPool
 
def DeleteFG
 
def AllFG
 
def SystemsWithFactionFGs
 
def DeleteAllFG
 
def DeleteAllFGFromAllSystems
 
def DeleteLegacyFGs
 
def DeleteLegacyFGLeftovers
 
def HasLegacyFGFormat
 
def FGSystem
 
def TransferFG
 
def AddShipsToFG
 
def RemoveShipFromFG
 
def BaseFGInSystemName
 
def AllFGsInSystem
 
def FGsInSystem
 
def BaseFGInSystem
 
def BaseFG
 
def NumFactionFGsInSystem
 
def CountFactionShipsInSystem
 
def LandedShipsInFG
 
def ShipsInFG
 
def CapshipInFG
 
def RandomShipIn
 
def minIndex
 
def launchBaseOrbit
 
def launchSingleBase
 
def launchBaseStuck
 
def launchBase
 
def zeros
 
def launchBases
 
def randDirection
 
def incr_by_abs
 
def FGsLaunchedInCurrentSystemAsSet
 
def FGsLaunchedInCurrentSystem
 
def filterLaunchedFGs
 
def launchUnits
 
def DefaultNumShips
 
def GetShipsInFG
 
def LaunchLandShip
 
def LaunchShip
 
def LandShip
 
def AllShips
 
def CheckAllShips
 
def SortedAllShips
 
def getFgLeaderType
 

Variables

tuple ccp = VS.getCurrentPlayer()
 
tuple _tweaktuple = ('Squadron','Prime','Arc','Alpha','Aleph','Beta','Quadratis','Zeta','X','Plus','Blade','Delta','Dash','Xprime','Gamma','Hydris','Dual','Tri','Quad','Penta','Hex','Octo','Deca','Octate')
 

Function Documentation

def fg_util.AddShipsToFG (   fgname,
  faction,
  typenumbertuple,
  starsystem 
)

Definition at line 429 of file fg_util.py.

References debug.debug, FGSystem(), debug.info, MakeFGKey(), ShipListOffset(), and WriteStringList().

430 def AddShipsToFG (fgname,faction,typenumbertuple,starsystem):
431  key = MakeFGKey(fgname,faction)
432  len = Director.getSaveStringLength (ccp,key)
433  if (len<ShipListOffset()):
434  debug.debug('adding new fg %r of %s to %s', fgname, typenumbertuple, starsystem)
435  WriteStringList(ccp,key,_MakeFGString( starsystem,typenumbertuple) )
436  _AddFGToSystem (fgname,faction,starsystem)
437  _AddFGToFactionList (fgname,faction)
438  debug.info('adding new fg %r of %s to %s', fgname, typenumbertuple, starsystem)
439  else:
440  debug.info('adding old fg %r of %s to %s', fgname, typenumbertuple, FGSystem(fgname,faction))
441  _AddShipsToKnownFG(key,typenumbertuple)
def fg_util.AllFactions ( )

Definition at line 61 of file fg_util.py.

61 
62 def AllFactions ():
63  facs =[]
64  for i in xrange (VS.GetNumFactions()):
65  facs.append(VS.GetFactionName(i))
66  return facs
67 
68 basenamelist={}
69 flightgroupnamelist={}
70 genericalphabet=['Alpha','Beta','Gamma','Delta','Epsilon','Zeta','Phi','Omega']
def fg_util.AllFG (   faction)

Definition at line 354 of file fg_util.py.

355 def AllFG(faction):
356  return ReadStringList (ccp,MakeFactionKey (faction))
def fg_util.AllFGsInSystem (   faction,
  system 
)

Definition at line 491 of file fg_util.py.

References MakeStarSystemFactionFGKey(), and ReadStringList().

492 def AllFGsInSystem(faction,system):
493  key = MakeStarSystemFactionFGKey(system,faction)
494  return ReadStringList(ccp,key)
def fg_util.AllFlightgroups (   faction)

Definition at line 175 of file fg_util.py.

References ReadStringList().

176 def AllFlightgroups (faction):
177  key = MakeFactionKey (faction)
178  return ReadStringList(ccp,key)
def fg_util.AllShips (   faction,
  offset = 1 
)

Definition at line 843 of file fg_util.py.

844 def AllShips (faction,offset=1):
845  ret=[]
846  for i in AllFlightgroups (faction):
847  ret+=ShipsInFG (i,faction,offset)
848  return ret
def fg_util.BaseFG (   faction,
  system 
)

Definition at line 509 of file fg_util.py.

References BaseFGInSystem(), and BaseFGInSystemName().

510 def BaseFG(faction,system):
511  if (BaseFGInSystem(faction,system)):
512  return LandedShipsInFG (BaseFGInSystemName(system),faction)
513  return []
def fg_util.BaseFGInSystem (   faction,
  system 
)

Definition at line 502 of file fg_util.py.

References AllFGsInSystem(), and BaseFGInSystemName().

503 def BaseFGInSystem(faction,system):
504  ret = AllFGsInSystem(faction,system)
505  basefg = BaseFGInSystemName(system)
506  if (basefg in ret):
507  return 1
508  return 0
def fg_util.BaseFGInSystemName (   system)

Definition at line 488 of file fg_util.py.

489 def BaseFGInSystemName (system):
490  return 'Base_'+system
def fg_util.CapshipInFG (   fg,
  fac 
)

Definition at line 552 of file fg_util.py.

References faction_ships.isCapital(), MakeFGKey(), PerShipDataSize(), and ShipListOffset().

553 def CapshipInFG(fg,fac):
554  key = MakeFGKey(fg,fac)
555  for num in xrange(ShipListOffset(),Director.getSaveStringLength(ccp,key),PerShipDataSize()):
556  import faction_ships
557  shipinquestion=Director.getSaveString(ccp,key,num)
558  if (faction_ships.isCapital(shipinquestion)):
559  return shipinquestion
560  return None
def fg_util.CheckAllShips (   faction)

Definition at line 849 of file fg_util.py.

References AllFGsInSystem(), AllFlightgroups(), debug.error, FGSystem(), and locale.str().

850 def CheckAllShips(faction):
851  for i in AllFlightgroups(faction):
852  sys = FGSystem(i,faction)
853  fgsin=AllFGsInSystem(faction,sys)
854  if (not i in fgsin):
855  debug.error('error '+str(fgsin) + i+' not in system '+ sys)
def fg_util.CheckFG (   fgname,
  faction 
)

Definition at line 275 of file fg_util.py.

References debug.debug, PerShipDataSize(), and ShipListOffset().

276 def CheckFG (fgname,faction):
277  key = MakeFGKey (fgname,faction)
278  leg = Director.getSaveStringLength (ccp,key)
279  totalships=0
280  try:
281  # Check ship counts
282  for i in xrange (ShipListOffset()+1,leg,PerShipDataSize()):
283  shipshere=Director.getSaveString(ccp,key,i)
284  totalships+=int(shipshere)
285  temp=Director.getSaveString(ccp,key,i+1)
286  if (temp!=shipshere):
287  debug.debug('correcting flightgroup %r to have right landed ships', fgname)
288  Director.putSaveString(ccp,key,i+1,shipshere)#set num actives to zero
289  #DEBUG ONLY if (totalships!=int(Director.getSaveString(ccp,key,0))):
290  # debug.debug('mismatch on flightgroup '+fgname+' faction '+faction)
291  # return 0
292  except:
293  debug.debug('nonint reading on flightgroup %r faction %r', fgname, faction)
294  return 0
295  return 1
def fg_util.CountFactionShipsInSystem (   faction,
  system 
)

Definition at line 518 of file fg_util.py.

References debug.debug, debug.error, PerShipDataSize(), and ShipListOffset().

519 def CountFactionShipsInSystem(faction,system):
520  count=0
521  st=''
522  for fgs in FGsInSystem (faction,system):
523  st+=fgs+' '
524  ships=ReadStringList (ccp,MakeFGKey (fgs,faction))
525  for num in xrange(ShipListOffset()+2,len(ships),PerShipDataSize()):
526  try:
527  count+= int(ships[num])
528  except:
529  debug.error('number ships '+ships[num] + ' not read')
530  debug.debug('OFFICIALCOUNT %s is %s', st, count)
531  return count
def fg_util.DefaultNumShips ( )

Definition at line 765 of file fg_util.py.

References vsrandom.randrange().

766 def DefaultNumShips():
767  import vsrandom
768  diff=VS.GetDifficulty()
769  if (diff>.9):
770  return vsrandom.randrange(1,5)
771  if (diff>.5):
772  return vsrandom.randrange(1,4)
773  if (diff>.2):
774  return vsrandom.randrange(1,3)
775  if (vsrandom.randrange(0,4)==0):
776  return 2
777  return 1
def fg_util.DeleteAllFG (   faction)

Definition at line 366 of file fg_util.py.

References AllFG(), debug.debug, DeleteFG(), and SystemsWithFactionFGs().

367 def DeleteAllFG (faction):
368  # Watch the ordering of operations! (won't work in most other permutations)
369  debug.debug("DeleteAllFG from %s", faction)
370  sysspan = SystemsWithFactionFGs(faction)
371  for fgname in AllFG(faction):
372  DeleteFG(fgname,faction,1)
373  for starsystem in sysspan:
374  _RemoveAllFGFromSystem(faction,starsystem)
375  _RemoveAllFGFromFactionList(faction)
def fg_util.DeleteAllFGFromAllSystems ( )

Definition at line 376 of file fg_util.py.

References AllFactions(), and DeleteAllFG().

378  for faction in AllFactions():
379  DeleteAllFG(faction)
def fg_util.DeleteFG (   fgname,
  faction,
  deferaux = 0 
)

Definition at line 344 of file fg_util.py.

References ShipListOffset().

345 def DeleteFG(fgname,faction,deferaux=0):
346  key = MakeFGKey (fgname,faction)
347  leg = Director.getSaveStringLength (ccp,key)
348  if (leg>=ShipListOffset()):
349  starsystem=Director.getSaveString(ccp,key,1)
350  if not deferaux:
351  _RemoveFGFromSystem(fgname,faction,starsystem)
352  _RemoveFGFromFactionList(fgname,faction)
353  WriteStringList (ccp,key,[])
def fg_util.DeleteLegacyFGLeftovers ( )

Definition at line 390 of file fg_util.py.

References universe.AllSystems(), DeleteLegacyFGs(), ShowProgress.setProgressBar(), and ShowProgress.setProgressMessage().

392  # Legacy cleanup
393  allsys = AllSystems()
394  numsys = len(allsys)
395  count = 0
396  oldpct = 0
397  for sys in allsys:
398  count += 1
399  pct = count*100/len(allsys)
400  if pct != oldpct:
401  ShowProgress.setProgressBar("loading",pct/100.)
402  ShowProgress.setProgressMessage("loading","Resetting old universe (%d%%)" % (pct))
403  oldpct = pct
404  DeleteLegacyFGs(sys)
def fg_util.DeleteLegacyFGs (   sys)

Definition at line 380 of file fg_util.py.

References DeleteFG(), MakeStarSystemFGKey(), ReadStringList(), and dospath.split().

381 def DeleteLegacyFGs(sys):
382  key = MakeStarSystemFGKey(sys)
383  factionfglists = ReadStringList(ccp,key)
384  for i in xrange(len(factionfglists)):
385  faction = VS.GetFactionName(i)
386  fglist = factionfglists[i].split('|')
387  for fg in fglist:
388  DeleteFG(fg,faction)
389 
def fg_util.FGsInSystem (   faction,
  system 
)

Definition at line 495 of file fg_util.py.

References AllFGsInSystem(), and BaseFGInSystemName().

496 def FGsInSystem(faction,system):
497  ret = AllFGsInSystem(faction,system)
498  basefg = BaseFGInSystemName(system)
499  if (basefg in ret):
500  del ret[ret.index(basefg)]
501  return ret
def fg_util.FGsLaunchedInCurrentSystem ( )

Definition at line 686 of file fg_util.py.

References FGsLaunchedInCurrentSystemAsSet().

689  rv = []
690  for fg in rvset:
691  rv.append(fg)
692  return rv
def fg_util.FGsLaunchedInCurrentSystemAsSet ( )

Definition at line 678 of file fg_util.py.

680  rvset = {}
681  iter = VS.getUnitList()
682  while iter.notDone():
683  un = iter.next()
684  rvset[un.getFlightgroupName()] = 1
685  return rvset
def fg_util.FGSystem (   fgname,
  faction 
)

Definition at line 412 of file fg_util.py.

References debug.debug, and MakeFGKey().

413 def FGSystem (fgname,faction):
414  key = MakeFGKey(fgname,faction)
415  len = Director.getSaveStringLength(ccp,key)
416  if (len>1):
417  return Director.getSaveString(ccp,key,1)
418  else:
419  debug.debug('%r for %r already died, in no system', fgname, faction)
return 'nil'
def fg_util.filterLaunchedFGs (   fglist)

Definition at line 693 of file fg_util.py.

References FGsLaunchedInCurrentSystemAsSet().

694 def filterLaunchedFGs(fglist):
696  rv = []
697  for fg in fglist:
698  if not fg in launched:
699  rv.append(fg)
700  return rv
def fg_util.GetDamageInFGPool (   fgname,
  faction 
)

Definition at line 326 of file fg_util.py.

References debug.debug.

327 def GetDamageInFGPool (fgname,faction):
328  key = MakeFGKey (fgname,faction)
329  len = Director.getSaveStringLength (ccp,key)
330  if (len<3):
331  return 0
332  else:
333  try:
334  return int(Director.getSaveString(ccp,key,2))
335  except:
336  debug.debug("nonfatal: flightgroup without size")
337  return 0
def fg_util.getFgLeaderType (   fgname,
  faction 
)

Definition at line 866 of file fg_util.py.

References faction_ships.getRandomFighter(), and ShipsInFG().

867 def getFgLeaderType(fgname,faction):
868 # print "wah " +str(ShipsInFG(fgname,faction))
869  l = ShipsInFG(fgname,faction)
870  if (len(l)):
871  if (len(l[0])):
872  return l[0][0];
873  import faction_ships
874  return faction_ships.getRandomFighter(faction)
def fg_util.GetRandomBaseName (   n,
  faction 
)

Definition at line 118 of file fg_util.py.

References debug.error, ReadBaseNameList(), and locale.str().

119 def GetRandomBaseName (n,faction):
120  global basecounter
121  retval=[]
122  global basenamelist
123  try:
124  import seedrandom
125  if (not (faction in basenamelist)):
126  basenamelist[faction]=ReadBaseNameList(faction+'_base')
127  retval = basenamelist[faction][basecounter:basecounter+n]
128  while len(retval)<n:
129  basecounter=n-len(retval)
130  retval = retval + basenamelist[faction][:basecounter]
131  basecounter = basecounter % len(basenamelist[faction])
132  except:
133  debug.error('uhoh base lsit wrong')
134  retval=[]
135  for i in xrange (n):
136  retval+=[str(n)]
137  n+=1
138  return retval
139 
140 origfgoffset=0
def fg_util.GetRandomFGNames (   numflightgroups,
  faction 
)

Definition at line 103 of file fg_util.py.

References ReadBaseNameList(), and locale.str().

104 def GetRandomFGNames (numflightgroups, faction):
105  global flightgroupnamelist
106  if (not (faction in flightgroupnamelist)):
107  flightgroupnamelist[faction]=ReadBaseNameList(faction)
108  if numflightgroups < 0:
109  numflightgroups = len(flightgroupnamelist[faction])
110  additional=[]
111  if (numflightgroups>len(flightgroupnamelist[faction])):
112  for i in xrange (numflightgroups-len(flightgroupnamelist)):
113  additional.append(str(i))
114  if (len(additional)+len(flightgroupnamelist[faction])==0):
115  flightgroupnamelist[faction]=ReadBaseNameList(faction)
116  return additional+flightgroupnamelist[faction]
117 basecounter=0
def fg_util.GetShipsInFG (   fgname,
  faction 
)

Definition at line 778 of file fg_util.py.

References DefaultNumShips(), debug.error, MakeFGKey(), PerShipDataSize(), vsrandom.random(), vsrandom.randrange(), and ShipListOffset().

779 def GetShipsInFG(fgname,faction):
780  ships = ReadStringList (ccp,MakeFGKey(fgname,faction))
781  if (len(ships)<=ShipListOffset()):
782  return []
783  try:
784  count=int(ships[0])
785  except:
786  debug.error('bad flightgroup record %s', ships)
787  launchnum = DefaultNumShips()
788  if (launchnum>count):
789  launchnum=count
790 
791  nent = (len(ships) - ShipListOffset()) / PerShipDataSize()
792  retn = [0] * nent
793  for i in xrange(_prob_round(launchnum*(0.7+vsrandom.random()+0.3))):
794  which = vsrandom.randrange(count)
795  for j in xrange(nent):
796  pos = j*PerShipDataSize()+ShipListOffset()
797  which -= int(ships[pos+2])
798  if which <= 0:
799  retn[j] += 1
800  break
801  ret = []
802  for i in xrange(nent):
803  if retn[i]:
804  pos = i*PerShipDataSize()+ShipListOffset()
805  ret.append((ships[pos],retn[i]))
806  return ret
def fg_util.HasLegacyFGFormat ( )

Definition at line 405 of file fg_util.py.

References universe.AllSystems(), and MakeStarSystemFGKey().

406 def HasLegacyFGFormat():
407  for starsystem in AllSystems():
408  key = MakeStarSystemFGKey(starsystem)
409  if Director.getSaveStringLength(ccp,key)>0:
410  return 1
411  return 0
def fg_util.incr_by_abs (   num,
  val 
)

Definition at line 672 of file fg_util.py.

References debug.debug.

673 def incr_by_abs(num,val):
674  debug.debug("A: %s or %s", num+val, num-val)
675  if (num>0):
676  return num+val
677  return num-val
def fg_util.LandedShipsInFG (   fgname,
  faction 
)

Definition at line 544 of file fg_util.py.

References ShipsInFG().

545 def LandedShipsInFG(fgname,faction):
return ShipsInFG(fgname,faction,2)
def fg_util.LandShip (   fgname,
  faction,
  typ,
  num = 1 
)

Definition at line 840 of file fg_util.py.

References LaunchLandShip().

841 def LandShip (fgname,faction,typ,num=1):
842  LaunchLandShip(fgname,faction,typ,-num)
def fg_util.launchBase (   type,
  num,
  faction,
  system,
  sig_units,
  numfighters 
)

Definition at line 624 of file fg_util.py.

References debug.debug, seedrandom.interleave(), launchBaseStuck(), minIndex(), seedrandom.rand(), seedrandom.seed(), and seedrandom.seedstring().

625 def launchBase (type,num,faction,system,sig_units,numfighters):
626  import seedrandom
627  debug.debug('launching base %s', type)
629  'faction',
630  'system'])))
631  if (len(sig_units)):
632  for i in xrange (num):
633  one=seedrandom.rand()
634  two=seedrandom.rand()
635  three=seedrandom.rand()
636  indices = [one%len(sig_units),
637  two%len(sig_units),
638  three%len(sig_units)];
639  which = minIndex(numfighters,indices)
640  if (sig_units[which].isJumppoint()):
641  numfighters[which]+=20
642  else:
643  numfighters[which]+=1
644  launchSingleBase (type,faction,sig_units[which])
645  else:
646  for i in xrange(num):
647  launchBaseStuck(type,faction)
def fg_util.launchBaseOrbit (   type,
  faction,
  loc,
  orbitradius,
  orbitspeed,
  unit 
)

Definition at line 578 of file fg_util.py.

References Vector.Add(), BaseFGInSystemName(), Vector.Mag(), Vector.Scale(), vsrandom.uniform(), and Vector.Vector().

579 def launchBaseOrbit(type,faction,loc,orbitradius,orbitspeed,unit):
580  #orbitradius*=2
581  import Vector
582  import dynamic_universe
583  R = Vector.Vector(vsrandom.uniform(1.25*orbitradius,orbitradius),
584  vsrandom.uniform(1.25*orbitradius,orbitradius),
585  vsrandom.uniform(1.25*orbitradius,orbitradius))
586  RMag = Vector.Mag(R)
587  T = Vector.Vector(vsrandom.uniform(.5*orbitradius,orbitradius),
588  vsrandom.uniform(.75*orbitradius,.85*orbitradius),
589  vsrandom.uniform(.5*orbitradius,orbitradius))
590  S = Vector.Cross (T,R)
591 
592  S = Vector.Scale(S,
593  vsrandom.uniform (1.5*orbitradius,orbitradius)
594  /Vector.Mag(S))
595  SMag = Vector.Mag(S)
596  bas=VS.launch("Base",type,faction,"unit","default",1,1,Vector.Add(loc,R),'')
597  nam=GetRandomBaseName (1,faction);
598  R = Vector.Scale (R,(RMag+2.0*bas.rSize())/RMag)
599  S = Vector.Scale (S,(SMag+2.0*bas.rSize())/SMag)
600  bas.orbit (unit,orbitspeed,R,S,(0.0,0.0,0.0))
601  #bas.SetPosition(Vector.Add(loc,R))
602  dynamic_universe.TrackLaunchedShip (BaseFGInSystemName(VS.getSystemFile()),
603  faction,
604  type,
605  bas)
def fg_util.launchBases (   sys)

Definition at line 651 of file fg_util.py.

References launchBase(), universe.significantUnits(), and zeros().

652 def launchBases(sys):
653  import universe
654  print 'Launching bases for '+sys
655  fac = VS.GetGalaxyFaction(sys)
656  fgs = BaseFG (fac,sys)
657  sig_units = universe.significantUnits()
658  shipcount=zeros(len(sig_units))
659  for fg in fgs:
660  launchBase(fg[0],fg[1],fac,sys,sig_units,shipcount)
def fg_util.launchBaseStuck (   type,
  faction 
)

Definition at line 616 of file fg_util.py.

617 def launchBaseStuck (type,faction):
618  un=VS.getPlayer()
619  maxspeed=100.1
620  if (un):
621  maxspeed=un.maxAfterburnerSpeed()+30.1
622  un.setNull();
623  launchBaseOrbit (type,faction,un.Position(),maxspeed*180,0,un)
def fg_util.LaunchLandShip (   fgname,
  faction,
  typ,
  numlaunched = 1 
)

Definition at line 807 of file fg_util.py.

References debug.debug, debug.error, PerShipDataSize(), ShipListOffset(), and locale.str().

808 def LaunchLandShip(fgname,faction,typ,numlaunched=1):
809  key = MakeFGKey (fgname,faction)
810  ships=ReadStringList (ccp,key)
811  debug.debug('LaunchLandShip: fg:%s fac:%s typ:%s, num:%s', fgname, faction, typ, numlaunched)
812  for num in xrange (ShipListOffset(),len(ships),PerShipDataSize()):
813  if (typ == ships[num]):
814  try:
815  ntobegin=int(ships[num+1])
816  nactive=int(ships[num+2])
817 
818  debug.debug("attempting launch for ship %s, begin %s, act %s)",
819  typ, ntobegin, nactive)
820 
821  nactive-=numlaunched
822  # Happens regularly -Patrick
823  # In the first system, nactive seems to always be 0 for all ships.
824  # In other systems, this isn't always true.
825  # This doesn't really seem to matter, though.
826  # Klauss: turned debug.error into debug.debug if it happens so often
827  # to clean up non-debug logs
828  if (nactive<0):
829  nactive=0
830  debug.debug('error more ships launched than in FG %s', fgname)
831  if (nactive>ntobegin):
832  nactive=ntobegin
833  debug.debug('error ships %s landed that never launched', typ)
834  Director.putSaveString(ccp,key,num+2,str(nactive))
835  except:
836  debug.error('error in FG data (str->int)')
def fg_util.LaunchShip (   fgname,
  faction,
  typ,
  num = 1 
)

Definition at line 837 of file fg_util.py.

838 def LaunchShip (fgname,faction,typ,num=1):
839  LaunchLandShip (fgname,faction,typ,num)
def fg_util.launchSingleBase (   type,
  faction,
  sig 
)

Definition at line 606 of file fg_util.py.

References debug.debug.

607 def launchSingleBase (type,faction,sig):
608  radpct = VS.getPlanetRadiusPercent()
609  sigrsize=sig.rSize()
610  radpct = sigrsize*(1+radpct)
611  if radpct<200000000.+sigrsize:
612  radpct=200000000.+sigrsize
613  speed = vsrandom.uniform (0,50)
614  debug.debug('Lauching %s base %s by sig %s (%s)', faction, type, sig.getName(), sig.getFullname())
615  launchBaseOrbit (type,faction,sig.Position(),radpct,speed,sig)
def fg_util.launchUnits (   sys)

Definition at line 701 of file fg_util.py.

References debug.debug, FGsInSystem(), filterLaunchedFGs(), faction_ships.getMaxFactions(), incr_by_abs(), debug.info, faction_ships.intToFaction(), launch_recycle.launch_types_around(), Vector.Mag(), vsrandom.randrange(), ShipsInFG(), universe.significantUnits(), and vsrandom.uniform().

702 def launchUnits(sys):
703  debug.info("Launching units for %s", sys)
704 
705  import faction_ships
706  import launch_recycle
707  import universe
708  sig_units=universe.significantUnits()
709  ownerfac= VS.GetGalaxyFaction(sys)
710  jumpcount=0
711  planetcount=0
712 # asteroidcount=0
713  basecount=0
714  farlen=0
715  for sig in sig_units:
716  if sig.isJumppoint():
717  jumpcount+=1
718  elif sig.isPlanet():
719  planetcount+=1
720 # elif sig.isAsteroid():
721 # asteroidcount+=1
722  else:
723  basecount+=1
724  tmplen=Vector.Mag(sig.Position())
725  if tmplen>farlen:
726  farlen=tmplen
727 
728  for factionnum in xrange(0,faction_ships.getMaxFactions()-1):
729  faction=faction_ships.intToFaction(factionnum)
730  fglist=filterLaunchedFGs(FGsInSystem(faction,sys))
731  isHostile=VS.GetRelation(ownerfac,faction)<0
732  isForeign=faction.find(ownerfac)==-1
733 
734  if isForeign:
735  if basecount+jumpcount:
736  frac=len(fglist)/float(basecount+jumpcount)
737  else:
738  frac=0.0
739  else:
740  if basecount+planetcount+jumpcount:
741  frac=len(fglist)/float(planetcount+basecount+jumpcount)
742  else:
743  frac=0.0
744  if isHostile:
745  for flightgroup in fglist:
746  X=incr_by_abs(vsrandom.uniform(-1.0,1.0),1)*farlen
747  Y=incr_by_abs(vsrandom.uniform(-1.0,1.0),1)*farlen
748  Z=incr_by_abs(vsrandom.uniform(-1.0,1.0),1)*farlen
749  XYZ = (X,Y,Z)
750  typenumbers=ShipsInFG(flightgroup,faction)
751  debug.debug("Really Far Apart around %s and 10000",XYZ)
752  debug.debug(" launching %s for %s at %s", typenumbers, faction, XYZ)
753  launch_recycle.launch_types_around(flightgroup,faction,typenumbers,'default',1,XYZ,0,'','',1,10000)
754  else:
755  for flightgroup in fglist:
756  #jp = sig.isJumppoint()
757  #if sig.isPlanet() or not isForeign:
758  sig = sig_units[vsrandom.randrange(0,len(sig_units))]
759  typenumbers=ShipsInFG(flightgroup,faction)
760  debug.debug(" launching %s for %s", typenumbers, faction)
761 
762  launch_recycle.launch_types_around(flightgroup,faction,typenumbers,'default',sig.rSize()*vsrandom.randrange(10,100),sig,0,'','',1,10000)
763 
764 
def fg_util.ListToPipe (   tup)

Definition at line 191 of file fg_util.py.

References MakeFactionKey(), MakeStarSystemFactionFGKey(), PerShipDataSize(), ShipListOffset(), locale.str(), and WriteStringList().

192 def ListToPipe (tup):
193  fina=''
194  if (len(tup)):
195  fina=tup[0]
196  for i in xrange (1,len(tup)):
197  fina+='|'+tup[i]
198  return fina
def fg_util.MakeFactionKey (   faction)

Definition at line 40 of file fg_util.py.

References locale.str().

40 
41 def MakeFactionKey (faction):
42  return 'FF:'+str(VS.GetFactionIndex(faction))
def fg_util.MakeFGKey (   fgname,
  faction 
)

Definition at line 43 of file fg_util.py.

References locale.str().

43 
44 def MakeFGKey (fgname,faction):
45  return 'FG:'+str(fgname)+'|'+str(VS.GetFactionIndex(faction))
def fg_util.MakeStarSystemFactionFGKey (   starsystem,
  faction 
)

Definition at line 49 of file fg_util.py.

49 
50 def MakeStarSystemFactionFGKey(starsystem,faction):
51  skey = MakeStarSystemFGKey (starsystem)
52  index = VS.GetFactionIndex (faction)
53  key = "%s[%d]" % (skey,index)
54  return key
def fg_util.MakeStarSystemFGKey (   starsystem)

Definition at line 46 of file fg_util.py.

References locale.str().

46 
47 def MakeStarSystemFGKey (starsystem):
48  return 'SS:'+str(starsystem)
def fg_util.MaxNumBasesInSystem ( )

Definition at line 34 of file fg_util.py.

34 
36  return 10
def fg_util.MaxNumFlightgroupsInSystem (   syst)

Definition at line 22 of file fg_util.py.

22 
24  if syst in faction_ships.max_flightgroups:
25  return faction_ships.max_flightgroups[syst]
26  else:
27  return 3
def fg_util.maxTweakFGOffset ( )

Definition at line 151 of file fg_util.py.

152 def maxTweakFGOffset():
153  return len(_tweaktuple)
def fg_util.minIndex (   vals,
  indices 
)

Definition at line 569 of file fg_util.py.

570 def minIndex (vals,indices):
571  mini = None
572  minv = None
573  for i in indices:
574  if (mini == None) or (minv > vals[i]):
575  mini = i
576  minv = vals[i]
577  return mini or 0
def fg_util.MinNumBasesInSystem ( )

Definition at line 37 of file fg_util.py.

37 
39  return 0
def fg_util.MinNumFlightgroupsInSystem (   syst)

Definition at line 28 of file fg_util.py.

28 
30  if syst in faction_ships.min_flightgroups:
31  return faction_ships.min_flightgroups[syst]
32  else:
33  return 1
def fg_util.NumAllFlightgroups (   faction)

Definition at line 179 of file fg_util.py.

References MakeFactionKey().

180 def NumAllFlightgroups (faction):
181  key = MakeFactionKey(faction)
182  return Director.getSaveStringLength(ccp,key)
def fg_util.NumFactionFGsInSystem (   faction,
  system 
)

Definition at line 514 of file fg_util.py.

References MakeStarSystemFactionFGKey().

515 def NumFactionFGsInSystem(faction,system):
516  key = MakeStarSystemFactionFGKey(system,faction)
517  return Director.getSaveStringLength(ccp,key)
def fg_util.NumShipsInFG (   fgname,
  faction 
)

Definition at line 314 of file fg_util.py.

References debug.debug.

315 def NumShipsInFG (fgname,faction):
316  key = MakeFGKey (fgname,faction)
317  len = Director.getSaveStringLength (ccp,key)
318  if (len==0):
319  return 0
320  else:
321  try:
322  return int(Director.getSaveString(ccp,key,0))
323  except:
324  debug.debug("fatal: flightgroup without size")
325  return 0
def fg_util.PerShipDataSize ( )

Definition at line 58 of file fg_util.py.

58 
59 def PerShipDataSize ():
60  return 3
def fg_util.PurgeZeroShips (   faction)

Definition at line 296 of file fg_util.py.

References debug.debug, and MakeFactionKey().

297 def PurgeZeroShips (faction):
298  return #perf problem...but we need it for persistent bases...so now it's in C++, not python---means this whole file is duplicated
299  key=MakeFactionKey(faction)
300  len=Director.getSaveStringLength (ccp,key)
301  i=0
302  debug.debug("purging 0 ships start")
303  while i<len:
304  curfg=Director.getSaveString(ccp,key,i)
305  CheckFG (curfg,faction)
306  #numships=NumShipsInFG(curfg,faction)
307  #commented to allow 0 ship flightgroups for future reinforcement
308  #if (numships==0):
309  # DeleteFG(curfg,faction)
310  # i-=1
311  # len-=1
312  i+=1
313  debug.debug("purging 0 ships end")
def fg_util.randDirection ( )

Definition at line 661 of file fg_util.py.

References vsrandom.uniform().

662 def randDirection():
663  leng=2
664  while leng>1 or leng<.00001:
665  X = vsrandom.uniform(-1,1);
666  Y = vsrandom.uniform(-1,1);
667  Z = vsrandom.uniform(-1,1);
668  leng=X*X+Y*Y+Z*Z
669  import VS
670  leng=VS.sqrt(leng)
671  return (X/leng,Y/leng,Z/leng)
def fg_util.RandomFlightgroup (   faction)

Definition at line 183 of file fg_util.py.

References MakeFactionKey(), and vsrandom.randrange().

184 def RandomFlightgroup (faction):
185  key = MakeFactionKey(faction)
186  i = Director.getSaveStringLength(ccp,MakeFactionKey(faction))
187  if (i==0):
188  return ''
189  import vsrandom
190  return Director.getSaveString(ccp,key,vsrandom.randrange(0,i))
def fg_util.RandomShipIn (   fg,
  fac 
)

Definition at line 561 of file fg_util.py.

References MakeFGKey(), PerShipDataSize(), vsrandom.randrange(), and ShipListOffset().

562 def RandomShipIn (fg,fac):
563  key = MakeFGKey(fg,fac)
564  len = Director.getSaveStringLength(ccp,key)-ShipListOffset()
565  len = int(len/PerShipDataSize())
566  if (len>0):
567  return Director.getSaveString(ccp,key,ShipListOffset()+PerShipDataSize()*vsrandom.randrange(0,len))
568  return ''
def fg_util.ReadBaseNameList (   faction)

Definition at line 71 of file fg_util.py.

References debug.debug, string.rstrip(), and vsrandom.shuffle().

71 
72 def ReadBaseNameList(faction):
73  bnl=[]
74  debug.debug('reading base names %s', faction)
75  filename = 'universe/fgnames/'+faction+'.txt'
76  try:
77  f = open (filename,'r')
78  bnl = f.readlines()
79  f.close()
80  except:
81  try:
82  f = open ('../'+filename,'r')
83  bnl = f.readlines()
84  f.close()
85  except:
86  try:
87  f = open ('../universe/names.txt','r')
88  bnl = f.readlines()
89  f.close()
90  except:
91  try:
92  f = open ('universe/names.txt','r')
93  bnl = f.readlines()
94  f.close()
95  except:
96  global genericalphabet
97  bnl=genericalphabet
98  for i in xrange(len(bnl)):
99  bnl[i]=bnl[i].rstrip()#.decode('utf8','ignore')
100  import vsrandom
101  vsrandom.shuffle(bnl)
102  return bnl
def fg_util.ReadStringList (   cp,
  key 
)

Definition at line 168 of file fg_util.py.

169 def ReadStringList (cp,key):
170  siz = Director.getSaveStringLength (cp,key)
171  tup =[]
172  for i in xrange (siz):
173  tup.append(Director.getSaveString(cp,key,i))
174  return tup
def fg_util.RemoveShipFromFG (   fgname,
  faction,
  type,
  numkill = 1,
  landed = 0 
)

Definition at line 442 of file fg_util.py.

References debug.debug, DeleteFG(), debug.error, PerShipDataSize(), ShipListOffset(), and locale.str().

443 def RemoveShipFromFG (fgname,faction,type,numkill=1,landed=0):
444  key = MakeFGKey (fgname,faction)
445  leg = Director.getSaveStringLength (ccp,key)
446  debug.debug("Scanning %d units...", leg)
447  for i in xrange (ShipListOffset()+1,leg,PerShipDataSize()):
448  if (Director.getSaveString(ccp,key,i-1)==str(type)):
449  debug.debug("Removing unit %s", type)
450  numships=0
451  numlandedships=0
452  try:
453  numships = int (Director.getSaveString (ccp,key,i))
454  numlandedships=int (Director.getSaveString (ccp,key,i+1))
455  except:
456  debug.error("unable to get savestring %s from FG %s %s %s", i, fgname, faction, type)
457  if (numships>numkill):
458  numships-=numkill
459  if (numships<numlandedships):
460  if (landed==0):
461  debug.debug('trying to remove launched ship %s but all are landed', type)
462  landed=1
463  return 0#failur
464  Director.putSaveString (ccp,key,i,str(numships))
465  if (landed and numlandedships>0):
466  Director.putSaveString(ccp,key,i+1,str(numlandedships-numkill))
467  else:
468  numkill=numships
469  numships=0
470  for j in xrange (i-1,i+PerShipDataSize()-1):
471  Director.eraseSaveString(ccp,key,i-1)
472  if (numships>=0):
473  try:
474  totalnumships = int(Director.getSaveString(ccp,key,0))
475  totalnumships -=numkill
476  if (totalnumships>=0):
477  Director.putSaveString(ccp,key,0,str(totalnumships))
478  if(totalnumships==0):
479  debug.debug("Removing %s FG %r", faction, fgname)
480  DeleteFG(fgname,faction)
481  else:
482  debug.error('error...removing too many ships')
483  except:
484  debug.error('error, flight record %r corrupt', fgname)
485  return numkill
486  debug.debug('cannot find ship to delete in %s fg %r', faction, fgname)
487  return 0
def fg_util.SetDamageInFGPool (   fgname,
  faction,
  num 
)

Definition at line 338 of file fg_util.py.

References locale.str().

339 def SetDamageInFGPool (fgname,faction,num):
340  key = MakeFGKey (fgname,faction)
341  len = Director.getSaveStringLength (ccp,key)
342  if (len>2):
343  Director.putSaveString(ccp,key,2,str(num))
def fg_util.ShipListOffset ( )

Definition at line 55 of file fg_util.py.

55 
56 def ShipListOffset ():
57  return 3
def fg_util.ShipsInFG (   fgname,
  faction,
  offset = 1 
)

Definition at line 546 of file fg_util.py.

References MakeFGKey(), PerShipDataSize(), and ShipListOffset().

547 def ShipsInFG(fgname,faction,offset=1):
548  ships = ReadStringList (ccp,MakeFGKey(fgname,faction))
549  rez=[]
550  for num in xrange (ShipListOffset(),len(ships),PerShipDataSize()):
551  rez.append((ships[num],int(ships[num+offset])))
return rez
def fg_util.SortedAllShips (   faction,
  offset = 1 
)

Definition at line 856 of file fg_util.py.

References ShipsInFG().

857 def SortedAllShips (faction,offset=1):
858  ret={}
859  for i in AllFlightgroups (faction):
860  for j in ShipsInFG(i,faction,offset):
861  if j[0] in ret:
862  ret[j[0]]+=j[1]
863  else:
864  ret[j[0]]=j[1]
865  return ret
def fg_util.SystemsWithFactionFGs (   faction)

Definition at line 357 of file fg_util.py.

References AllFG(), and FGSystem().

358 def SystemsWithFactionFGs(faction):
359  sysSpan = {}
360  rv = []
361  for fgname in AllFG(faction):
362  sysSpan[FGSystem(fgname,faction)] = 1
363  for starsystem in sysSpan:
364  rv.append(starsystem)
365  return rv
def fg_util.TransferFG (   fgname,
  faction,
  tosys 
)

Definition at line 420 of file fg_util.py.

References MakeFGKey().

421 def TransferFG (fgname,faction,tosys):
422  key = MakeFGKey(fgname,faction)
423  len = Director.getSaveStringLength(ccp,key)
424  if (len>1):
425  starsystem=Director.getSaveString(ccp,key,1)
426  _RemoveFGFromSystem(fgname,faction,starsystem)
427  _AddFGToSystem(fgname,faction,tosys)
428  Director.putSaveString(ccp,key,1,tosys)
def fg_util.TweakFGNames (   origfgnames)

Definition at line 141 of file fg_util.py.

References locale.str().

142 def TweakFGNames (origfgnames):
143  global origfgoffset
144  tweaker=str(origfgoffset)
145  if (origfgoffset<len(_tweaktuple)):
146  tweaker = _tweaktuple[origfgoffset]
147  rez=[]
148  for i in origfgnames:
149  rez.append (i+'_'+tweaker)
150  return rez
def fg_util.WriteStringList (   cp,
  key,
  tup 
)

Definition at line 154 of file fg_util.py.

155 def WriteStringList(cp,key,tup):
156  siz = Director.getSaveStringLength (cp,key)
157  s_size=siz;
158  lentup= len(tup)
159  if (lentup<siz):
160  siz=lentup
161  for i in xrange(siz):
162  Director.putSaveString(cp,key,i,tup[i])
163  for i in xrange (s_size,lentup):
164  Director.pushSaveString(cp,key,tup[i])
165  while s_size > lentup:
166  Director.eraseSaveString(cp,key,s_size-1)
167  s_size -= 1
def fg_util.zeros (   le)

Definition at line 648 of file fg_util.py.

649 def zeros (le):
650  return [0 for i in xrange(le)]

Variable Documentation

tuple _tweaktuple = ('Squadron','Prime','Arc','Alpha','Aleph','Beta','Quadratis','Zeta','X','Plus','Blade','Delta','Dash','Xprime','Gamma','Hydris','Dual','Tri','Quad','Penta','Hex','Octo','Deca','Octate')

Definition at line 15 of file fg_util.py.

tuple ccp = VS.getCurrentPlayer()

Definition at line 14 of file fg_util.py.