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

Functions

def isLandable
 
def isBase
 
def findPlayerNum
 
def isAsteroid
 
def moveOutOfPlayerPath
 
def getUnitFullName
 
def getSignificant
 
def inSystem
 
def getPlanet
 
def getPlanetList
 
def getJumpPoint
 
def obsolete_getNearestEnemy
 
def obsolete_getThreatOrEnemyInRange
 
def setPreciseTargetShip
 
def getMinDistFrom
 
def minimumSigDistApart
 
def getUnitByName
 
def getUnitByFgIDFromNumber
 
def getUnitByFgID
 
def setTargetShip
 
def removeFg
 
def TfgCloak
 
def TfgisNull
 
def TfgHeadCount
 
def setTfgDirective
 
def TfgJumpTo
 
def getUnitSequenceBackwards
 
def approachAndDock
 
def faceTaget
 
def facingAngleToUnit
 
def getSignedVelocity
 
def changeDirectionAndThrust
 
def getShieldPercent
 

Variables

tuple profiling_level = int(VS.vsConfig("general","profiling","0"))
 

Function Documentation

def unit.approachAndDock (   ship,
  station 
)

Definition at line 298 of file unit.py.

References sre_parse.max, Vector.SafeNorm(), Vector.Scale(), and Vector.Sub().

299 def approachAndDock (ship,station):
300  # if ship is more than 500m away it will set
301  distance = ship.getDistance(station) #+station.rSize()
302  if (distance>=500):
303  # orientate the nose of the ship towards the station
304  vec = Vector.Sub(station.Position(),ship.Position())
305  ship.SetOrientation((1,0,0),vec)
306  # set velocity proportional to distance from player
307  vec = max(10,Vector.Scale(Vector.SafeNorm(vec),distance/10))
308  ship.SetVelocity(vec)
309  # if ship has approached the station until 500m then stop it
310  if (distance<500):
311  ship.SetVelocity((0,0,0))
312  # this is also needed to stop rotation of the ship
313  ship.SetAngularVelocity((0,0,0))
314  # request clearance and dock seems to have no effect
315  station.RequestClearance(ship)
316  ship.Dock(station)
317  # emulate docking by making the ship disappear
318  ship.Kill()
319  ship.setNull()
320  return 1
321 
# orientate the nose of the ship towards the station
def unit.changeDirectionAndThrust (   unit,
  angular,
  thrust 
)

Definition at line 342 of file unit.py.

343 def changeDirectionAndThrust(unit, angular, thrust):
344  import Vector
345  import vsrandom
346  unit.SetAngularVelocity(angular)
347  unit.LongitudinalThrust(thrust)
348  return 1
349 
350 # returns the shield percent for a ship
# ships with dual shields will return 0.5 with full shields
def unit.faceTaget (   ship,
  target 
)

Definition at line 322 of file unit.py.

References Vector.Sub().

323 def faceTaget (ship,target):
324  vec = Vector.Sub(target.Position(),ship.Position())
325  ship.SetOrientation((1,0,0),vec)
326 
327 # returns the facing angle between unit 1 and unit 2
328 # when unit 1 is facing unit 2 the return value is 0
# when unit 1 is completely turned away from unit 2 the return value is pi (~3.14)
def unit.facingAngleToUnit (   unit1,
  unit2 
)

Definition at line 329 of file unit.py.

References Vector.Dot(), Vector.SafeNorm(), and Vector.Sub().

330 def facingAngleToUnit(unit1,unit2):
331  vec = Vector.Sub(unit2.Position(),unit1.Position())
332  dot = Vector.Dot(Vector.SafeNorm(unit1.GetOrientation()[2]),Vector.SafeNorm(vec))
333  angle = VS.acos(dot)
334  return angle
335 
336 # signed velocity is negative when the thrust is reverse
# otherwise velocity is positive
def unit.findPlayerNum (   un)

Definition at line 20 of file unit.py.

20 
21 def findPlayerNum (un):
22  for i in range (VS.getNumPlayers()):
23  if (VS.getPlayerX(i)==VS.getPlayer()):
24  return i
25  return 0
def unit.getJumpPoint (   whichsignificant)

Definition at line 137 of file unit.py.

138 def getJumpPoint(whichsignificant):
139  i = VS.getUnitList()
140  i.advanceNJumppoint(whichsignificant)
141  if i.isDone():
142  un = VS.Unit()
143  un.setNull()
144  return un
145  else:
146  return i.current()
def unit.getMinDistFrom (   sig1,
  siglist = None 
)

Definition at line 181 of file unit.py.

References getPlanetList().

182 def getMinDistFrom(sig1,siglist=None):
183  siglist=siglist or getPlanetList(0)
184  mindist=100000000000000000000000000000000000000000000.0
185  for sig2 in siglist:
186  tempdist = sig1.getSignificantDistance(sig2)
187  if (tempdist<mindist and tempdist>0.0):
188  mindist=tempdist
189  return mindist
def unit.getPlanet (   whichsignificant,
  sig 
)

Definition at line 110 of file unit.py.

111 def getPlanet (whichsignificant, sig):
112  i = VS.getUnitList()
113  if sig:
114  i.advanceNSignificant(whichsignificant)
115  else:
116  i.advanceNPlanet(whichshignificant)
117  if i.isDone():
118  return VS.Unit()
119  else:
120  return i.current()
def unit.getPlanetList (   sig)

Definition at line 121 of file unit.py.

122 def getPlanetList (sig):
123  res = []
124  i = VS.getUnitList()
125  if i.notDone():
126  if sig:
127  i.advanceNSignificant(0)
128  else:
129  i.advanceNPlanet(0)
130  while i.notDone():
131  res.append(i.current())
132  if sig:
133  i.advanceSignificant()
134  else:
135  i.advancePlanet()
136  return res
def unit.getShieldPercent (   unit)

Definition at line 351 of file unit.py.

352 def getShieldPercent(unit):
353  shield = (unit.FShieldData() + unit.RShieldData() + unit.LShieldData() + unit.BShieldData()) / 4
354  return shield
def unit.getSignedVelocity (   unit)

Definition at line 337 of file unit.py.

References Vector.Dot(), and Vector.SafeNorm().

338 def getSignedVelocity(unit):
339  velocity = Vector.Dot(Vector.SafeNorm(unit.GetOrientation()[2]),unit.GetVelocity())
340  return velocity
341 
# change the direction and thrust forward at the same time
def unit.getSignificant (   whichsignificant,
  landable_only,
  capship_only 
)

Definition at line 78 of file unit.py.

References getPlanetList(), isBase(), isLandable(), vsrandom.randrange(), and debug.warn.

78 
79 def getSignificant (whichsignificant, landable_only, capship_only):
80  import vsrandom
81  if not (landable_only or capship_only):
82  rez = getPlanetList(True) #Gets all significants
83  else:
84  rez = []
85  i = VS.getUnitList()
86  while i.notDone():
87  un = i.current()
88  if (capship_only and isBase(un)) or (not capship_only and isLandable(un)):
89  rez.append(un)
90  i.advance()
91  if (len(rez)==0):
92  if (capship_only):
93  return getSignificant(whichsignificant,landable_only,0)
94  elif(landable_only):
95  return getSignificant (whichsignificant,0,0)
96  else:
97  debug.warn("no significants in system "+VS.getSystemFile())
98  return VS.getPlayer()
99  return rez[vsrandom.randrange(0,len(rez))]
100 
101  #this one terminates if fewer than so many planets exist with null
def unit.getUnitByFgID (   fgid)

Definition at line 221 of file unit.py.

References getUnitByFgIDFromNumber().

222 def getUnitByFgID(fgid):
223  return getUnitByFgIDFromNumber(fgid,0)
def unit.getUnitByFgIDFromNumber (   fgid,
  ship_nr 
)

Definition at line 209 of file unit.py.

210 def getUnitByFgIDFromNumber(fgid, ship_nr):
211  i = VS.getUnitList()
212  i.advanceN(ship_nr)
213  found_unit = VS.Unit()
214  while i.notDone() and not found_unit:
215  un = i.current()
216  unit_fgid=un.getFgID()
217  if(unit_fgid==fgid):
218  found_unit=un
219  i.advance()
220  return found_unit
def unit.getUnitByName (   name)

Definition at line 206 of file unit.py.

207 def getUnitByName (name):
208  return VS.getUnitByName(name) or VS.getUnit(0)
def unit.getUnitFullName (   un,
  inenglish = False 
)

Definition at line 60 of file unit.py.

References string.capitalize(), pydoc.replace(), and locale.str().

60 
61 def getUnitFullName(un,inenglish=False):
62  thename=(un.getFullname())#.replace("_"," ")
63  snumber=str(un.getFgSubnumber())
64  fg=un.getFlightgroupName()#.replace("_"," ")
65  if un.isPlanet():
66  thename=un.getName()
67  elif fg=='Base':
68  if un.getFullname().capitalize()==un.getName().capitalize():
69  thename=thename+" "+snumber
70  elif fg!='Shadow' and fg!='':
71  if inenglish:
72  thename=un.getFactionName().replace("_"," ")+" "+thename+ " "+snumber +' in the '+fg+' flightgroup'
73  else:
74  thename=thename+":"+fg+' <'+snumber+'>'
75  else:
76  thename=thename+":"+fg+' <'+snumber+'>'
77  return thename
def unit.getUnitSequenceBackwards ( )

Definition at line 284 of file unit.py.

286  rez1 = []
287  rez2 = []
288  i = VS.getUnitList()
289  while i.notDone():
290  rez1.append(i.current())
291  i.advance()
292  i = len(rez1)-1
293  while (i>=0):
294  rez2.append(rez1[i])
295  i-=1
296  return rez2
297 
# the ship will approach the station until 500 meters and then dock
def unit.inSystem (   unit)

Definition at line 102 of file unit.py.

103 def inSystem (unit):
104  i=VS.getUnitList ()
105  while (i.notDone()):
106  if (i.current()==unit):
107  return 1
108  i.advance()
109  return 0
def unit.isAsteroid (   un)

Definition at line 26 of file unit.py.

26 
27 def isAsteroid (un):
28  unit_fgid = un.getFlightgroupName()
29  retval = unit_fgid=="Asteroid"
30  return retval
def unit.isBase (   un)

Definition at line 15 of file unit.py.

15 
16 def isBase (un):
17  unit_fgid = un.getFlightgroupName()
18  retval = unit_fgid=="Base"
19  return retval and not un.isSun()
def unit.isLandable (   un)

Definition at line 11 of file unit.py.

11 
12 def isLandable (un):
13  if (un):
14  return un.isDockableUnit()
def unit.minimumSigDistApart ( )

Definition at line 190 of file unit.py.

References getPlanetList().

191 def minimumSigDistApart():
192  siglist=getPlanetList(0)
193  i=0
194  mindist=100000000000000000000000000000000000000000000.0
195  ave=0.0
196  for sig1 in siglist:
197  tempdist = getMinDistFrom (sig1,siglist)
198  if (ave<0.9):
199  mindist = tempdist
200  else:
201  mindist += tempdist
202  ave+=1.0
203  if (ave!=0.0):
204  mindist = mindist/ave
205  return mindist
def unit.moveOutOfPlayerPath (   un)

Definition at line 31 of file unit.py.

References Vector.Add(), Vector.Dot(), Vector.SafeNorm(), Vector.Scale(), Vector.ScaledCross(), locale.str(), Vector.Sub(), and vsrandom.uniform().

31 
32 def moveOutOfPlayerPath(un):
33  def reposition(un,playa,min_distance,vel):
34  import Vector
35  import vsrandom
37  if vel[0]==0 and vel[1]==0 and vel[2]==0:
38  vel=(1,0,0)
39  dir=Vector.Scale(Vector.ScaledCross(ex,vel),min_distance)
40  print "offsetting you a few meters to the "+str(dir)
41  un.SetPosition(Vector.Add(playa.Position(),dir))
42  import faction_ships
43  playa=VS.getPlayer()
44  min_distance=100
45  min_forward_distance=1300
46  try:
47  min_distance=faction_ships.min_distance
48  min_forward_distance=faction_ships.min_forward_distance
49  except:
50  print "badness no faction_ships.min_distance"
51  dis=un.getDistance(playa)
52  import Vector
53  vel=playa.GetVelocity()
54  vel=Vector.SafeNorm(vel)
55  if (dis<min_distance):
56  reposition(un,playa,min_distance,vel)
57  dir=Vector.SafeNorm(Vector.Sub(playa.Position(),un.Position()))
58  if (dis<min_forward_distance and Vector.Dot(dir,vel)>.8):
59  reposition(un,playa,min_distance,vel)
def unit.obsolete_getNearestEnemy (   my_unit,
  range 
)

Definition at line 147 of file unit.py.

148 def obsolete_getNearestEnemy(my_unit,range):
149  i = VS.getUnitList()
150  min_dist=9999999.0
151  min_enemy=VS.Unit()
152  while(i.notDone()):
153  un=i.current()
154  unit_pos=un.Position()
155  dist=my_unit.getMinDis(unit_pos)
156  relation=my_unit.getRelation(unit)
157  if(relation<0.0):
158  if((my_unit==unit) and (dist<range) and (dist<min_dist)):
159  min_dist=dist
160  min_enemy=unit
161  i.advance()
162  if(min_enemy):
163  other_fgid=min_enemy.getFgID()
164  return min_enemy
def unit.obsolete_getThreatOrEnemyInRange (   un,
  range 
)

Definition at line 165 of file unit.py.

References obsolete_getNearestEnemy().

167  threat=un.getThreat()
168  if(threat.isNull()):
169  threat=obsolete_getNearestEnemy(un,range)
170  return threat
def unit.removeFg (   which_fgid)

Definition at line 228 of file unit.py.

229 def removeFg(which_fgid):
230  i = VS.getUnitList()
231  klist = []
232  while i.notDone():
233  un = i.current()
234  unit_fgid=un.getFgID()
235  if(unit_fgid[:len(which_fgid)]==which_fgid):
236  klist.append(un)
237  i.advance()
238  for un in klist:
239  un.Kill()
240 
241 # A collection of functions useful for dealing with flightgroup tuples.
242 # Added as used.
243 # For more information on the types of veriables used,
244 # see the VS python doc in the manual.
245 
# Cloaks or uncloaks (1 or 0) a flightgroup tuple (tup).
def unit.setPreciseTargetShip (   which_fgid,
  target_unit 
)

Definition at line 171 of file unit.py.

172 def setPreciseTargetShip (which_fgid, target_unit):
173  if (target_unit):
174  i = VS.getUnitList()
175  while i.notDone():
176  un = i.current()
177  unit_fgid=un.getFgID()
178  if(unit_fgid[:len(which_fgid)]==which_fgid):
179  un.SetTarget(target_unit)
180  i.advance()
def unit.setTargetShip (   which_fgid,
  target_fgid 
)

Definition at line 224 of file unit.py.

References getUnitByFgID(), and setPreciseTargetShip().

225 def setTargetShip(which_fgid,target_fgid):
226  target_unit=getUnitByFgID(target_fgid)
227  setPreciseTargetShip(which_fgid,target_unit)
def unit.setTfgDirective (   tup,
  tgt,
  dir 
)

Definition at line 272 of file unit.py.

273 def setTfgDirective(tup,tgt,dir):
274  num = len(tup)
275  for i in range(num):
276  tup[i].SetTarget(tgt)
277  tup[i].setFgDirective(dir)
278 
# Jumps a whole fg tuple using the JumpTo command.
def unit.TfgCloak (   state,
  tup 
)

Definition at line 246 of file unit.py.

247 def TfgCloak(state,tup):
248  num = len(tup)
249  for i in range(num):
250  tup[i].Cloak(state)
251  num = num + 1
252 
# Tells us if a tup is null
def unit.TfgHeadCount (   tup)

Definition at line 264 of file unit.py.

265 def TfgHeadCount(tup):
266  num = 0
267  for i in tup:
268  if (i):
269  num = num + 1
270  return num
271 
# Sets a whole tupled flightgroup on a target.
def unit.TfgisNull (   tup)

Definition at line 253 of file unit.py.

254 def TfgisNull(tup):
255  num = 0
256  for i in tup:
257  if (i):
258  num = num + 1
259  if num == 0:
260  return 1
261  else:
262  return 0
263 
# Returns an integer value of the number of ships in the tup. Not sure if it takes notice of null state.
def unit.TfgJumpTo (   tup,
  system 
)

Definition at line 279 of file unit.py.

280 def TfgJumpTo(tup,system):
281  num = len(tup)
282  for i in range(num):
283  tup[i].JumpTo(system)

Variable Documentation

int profiling_level = int(VS.vsConfig("general","profiling","0"))

Definition at line 7 of file unit.py.