Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
unit.py
Go to the documentation of this file.
1 import VS
2 import debug
3 import Vector
4 
5 #statistics - for profiling (disabled in release version)
6 try:
7  profiling_level = int(VS.vsConfig("general","profiling","0"))
8 except:
9  profiling_level = 0
10 
11 def isLandable (un):
12  if (un):
13  return un.isDockableUnit()
14 
15 def isBase (un):
16  unit_fgid = un.getFlightgroupName()
17  retval = unit_fgid=="Base"
18  return retval and not un.isSun()
19 
20 def findPlayerNum (un):
21  for i in range (VS.getNumPlayers()):
22  if (VS.getPlayerX(i)==VS.getPlayer()):
23  return i
24  return 0
25 
26 def isAsteroid (un):
27  unit_fgid = un.getFlightgroupName()
28  retval = unit_fgid=="Asteroid"
29  return retval
30 
32  def reposition(un,playa,min_distance,vel):
33  import Vector
34  import vsrandom
36  if vel[0]==0 and vel[1]==0 and vel[2]==0:
37  vel=(1,0,0)
38  dir=Vector.Scale(Vector.ScaledCross(ex,vel),min_distance)
39  print "offsetting you a few meters to the "+str(dir)
40  un.SetPosition(Vector.Add(playa.Position(),dir))
41  import faction_ships
42  playa=VS.getPlayer()
43  min_distance=100
44  min_forward_distance=1300
45  try:
46  min_distance=faction_ships.min_distance
47  min_forward_distance=faction_ships.min_forward_distance
48  except:
49  print "badness no faction_ships.min_distance"
50  dis=un.getDistance(playa)
51  import Vector
52  vel=playa.GetVelocity()
53  vel=Vector.SafeNorm(vel)
54  if (dis<min_distance):
55  reposition(un,playa,min_distance,vel)
56  dir=Vector.SafeNorm(Vector.Sub(playa.Position(),un.Position()))
57  if (dis<min_forward_distance and Vector.Dot(dir,vel)>.8):
58  reposition(un,playa,min_distance,vel)
59 
60 def getUnitFullName(un,inenglish=False):
61  thename=(un.getFullname())#.replace("_"," ")
62  snumber=str(un.getFgSubnumber())
63  fg=un.getFlightgroupName()#.replace("_"," ")
64  if un.isPlanet():
65  thename=un.getName()
66  elif fg=='Base':
67  if un.getFullname().capitalize()==un.getName().capitalize():
68  thename=thename+" "+snumber
69  elif fg!='Shadow' and fg!='':
70  if inenglish:
71  thename=un.getFactionName().replace("_"," ")+" "+thename+ " "+snumber +' in the '+fg+' flightgroup'
72  else:
73  thename=thename+":"+fg+' <'+snumber+'>'
74  else:
75  thename=thename+":"+fg+' <'+snumber+'>'
76  return thename
77 
78 def getSignificant (whichsignificant, landable_only, capship_only):
79  import vsrandom
80  if not (landable_only or capship_only):
81  rez = getPlanetList(True) #Gets all significants
82  else:
83  rez = []
84  i = VS.getUnitList()
85  while i.notDone():
86  un = i.current()
87  if (capship_only and isBase(un)) or (not capship_only and isLandable(un)):
88  rez.append(un)
89  i.advance()
90  if (len(rez)==0):
91  if (capship_only):
92  return getSignificant(whichsignificant,landable_only,0)
93  elif(landable_only):
94  return getSignificant (whichsignificant,0,0)
95  else:
96  debug.warn("no significants in system "+VS.getSystemFile())
97  return VS.getPlayer()
98  return rez[vsrandom.randrange(0,len(rez))]
99 
100  #this one terminates if fewer than so many planets exist with null
101 
102 def inSystem (unit):
103  i=VS.getUnitList ()
104  while (i.notDone()):
105  if (i.current()==unit):
106  return 1
107  i.advance()
108  return 0
109 
110 def getPlanet (whichsignificant, sig):
111  i = VS.getUnitList()
112  if sig:
113  i.advanceNSignificant(whichsignificant)
114  else:
115  i.advanceNPlanet(whichshignificant)
116  if i.isDone():
117  return VS.Unit()
118  else:
119  return i.current()
120 
121 def getPlanetList (sig):
122  res = []
123  i = VS.getUnitList()
124  if i.notDone():
125  if sig:
126  i.advanceNSignificant(0)
127  else:
128  i.advanceNPlanet(0)
129  while i.notDone():
130  res.append(i.current())
131  if sig:
132  i.advanceSignificant()
133  else:
134  i.advancePlanet()
135  return res
136 
137 def getJumpPoint(whichsignificant):
138  i = VS.getUnitList()
139  i.advanceNJumppoint(whichsignificant)
140  if i.isDone():
141  un = VS.Unit()
142  un.setNull()
143  return un
144  else:
145  return i.current()
146 
147 def obsolete_getNearestEnemy(my_unit,range):
148  i = VS.getUnitList()
149  min_dist=9999999.0
150  min_enemy=VS.Unit()
151  while(i.notDone()):
152  un=i.current()
153  unit_pos=un.Position()
154  dist=my_unit.getMinDis(unit_pos)
155  relation=my_unit.getRelation(unit)
156  if(relation<0.0):
157  if((my_unit==unit) and (dist<range) and (dist<min_dist)):
158  min_dist=dist
159  min_enemy=unit
160  i.advance()
161  if(min_enemy):
162  other_fgid=min_enemy.getFgID()
163  return min_enemy
164 
166  threat=un.getThreat()
167  if(threat.isNull()):
168  threat=obsolete_getNearestEnemy(un,range)
169  return threat
170 
171 def setPreciseTargetShip (which_fgid, target_unit):
172  if (target_unit):
173  i = VS.getUnitList()
174  while i.notDone():
175  un = i.current()
176  unit_fgid=un.getFgID()
177  if(unit_fgid[:len(which_fgid)]==which_fgid):
178  un.SetTarget(target_unit)
179  i.advance()
180 
181 def getMinDistFrom(sig1,siglist=None):
182  siglist=siglist or getPlanetList(0)
183  mindist=100000000000000000000000000000000000000000000.0
184  for sig2 in siglist:
185  tempdist = sig1.getSignificantDistance(sig2)
186  if (tempdist<mindist and tempdist>0.0):
187  mindist=tempdist
188  return mindist
189 
191  siglist=getPlanetList(0)
192  i=0
193  mindist=100000000000000000000000000000000000000000000.0
194  ave=0.0
195  for sig1 in siglist:
196  tempdist = getMinDistFrom (sig1,siglist)
197  if (ave<0.9):
198  mindist = tempdist
199  else:
200  mindist += tempdist
201  ave+=1.0
202  if (ave!=0.0):
203  mindist = mindist/ave
204  return mindist
205 
206 def getUnitByName (name):
207  return VS.getUnitByName(name) or VS.getUnit(0)
208 
209 def getUnitByFgIDFromNumber(fgid, ship_nr):
210  i = VS.getUnitList()
211  i.advanceN(ship_nr)
212  found_unit = VS.Unit()
213  while i.notDone() and not found_unit:
214  un = i.current()
215  unit_fgid=un.getFgID()
216  if(unit_fgid==fgid):
217  found_unit=un
218  i.advance()
219  return found_unit
220 
221 def getUnitByFgID(fgid):
222  return getUnitByFgIDFromNumber(fgid,0)
223 
224 def setTargetShip(which_fgid,target_fgid):
225  target_unit=getUnitByFgID(target_fgid)
226  setPreciseTargetShip(which_fgid,target_unit)
227 
228 def removeFg(which_fgid):
229  i = VS.getUnitList()
230  klist = []
231  while i.notDone():
232  un = i.current()
233  unit_fgid=un.getFgID()
234  if(unit_fgid[:len(which_fgid)]==which_fgid):
235  klist.append(un)
236  i.advance()
237  for un in klist:
238  un.Kill()
239 
240 # A collection of functions useful for dealing with flightgroup tuples.
241 # Added as used.
242 # For more information on the types of veriables used,
243 # see the VS python doc in the manual.
244 
245 # Cloaks or uncloaks (1 or 0) a flightgroup tuple (tup).
246 def TfgCloak(state,tup):
247  num = len(tup)
248  for i in range(num):
249  tup[i].Cloak(state)
250  num = num + 1
251 
252 # Tells us if a tup is null
253 def TfgisNull(tup):
254  num = 0
255  for i in tup:
256  if (i):
257  num = num + 1
258  if num == 0:
259  return 1
260  else:
261  return 0
262 
263 # Returns an integer value of the number of ships in the tup. Not sure if it takes notice of null state.
264 def TfgHeadCount(tup):
265  num = 0
266  for i in tup:
267  if (i):
268  num = num + 1
269  return num
270 
271 # Sets a whole tupled flightgroup on a target.
272 def setTfgDirective(tup,tgt,dir):
273  num = len(tup)
274  for i in range(num):
275  tup[i].SetTarget(tgt)
276  tup[i].setFgDirective(dir)
277 
278 # Jumps a whole fg tuple using the JumpTo command.
279 def TfgJumpTo(tup,system):
280  num = len(tup)
281  for i in range(num):
282  tup[i].JumpTo(system)
283 
285  rez1 = []
286  rez2 = []
287  i = VS.getUnitList()
288  while i.notDone():
289  rez1.append(i.current())
290  i.advance()
291  i = len(rez1)-1
292  while (i>=0):
293  rez2.append(rez1[i])
294  i-=1
295  return rez2
296 
297 # the ship will approach the station until 500 meters and then dock
298 def approachAndDock (ship,station):
299  # if ship is more than 500m away it will set
300  distance = ship.getDistance(station) #+station.rSize()
301  if (distance>=500):
302  # orientate the nose of the ship towards the station
303  vec = Vector.Sub(station.Position(),ship.Position())
304  ship.SetOrientation((1,0,0),vec)
305  # set velocity proportional to distance from player
306  vec = max(10,Vector.Scale(Vector.SafeNorm(vec),distance/10))
307  ship.SetVelocity(vec)
308  # if ship has approached the station until 500m then stop it
309  if (distance<500):
310  ship.SetVelocity((0,0,0))
311  # this is also needed to stop rotation of the ship
312  ship.SetAngularVelocity((0,0,0))
313  # request clearance and dock seems to have no effect
314  station.RequestClearance(ship)
315  ship.Dock(station)
316  # emulate docking by making the ship disappear
317  ship.Kill()
318  ship.setNull()
319  return 1
320 
321 # orientate the nose of the ship towards the station
322 def faceTaget (ship,target):
323  vec = Vector.Sub(target.Position(),ship.Position())
324  ship.SetOrientation((1,0,0),vec)
325 
326 # returns the facing angle between unit 1 and unit 2
327 # when unit 1 is facing unit 2 the return value is 0
328 # when unit 1 is completely turned away from unit 2 the return value is pi (~3.14)
329 def facingAngleToUnit(unit1,unit2):
330  vec = Vector.Sub(unit2.Position(),unit1.Position())
331  dot = Vector.Dot(Vector.SafeNorm(unit1.GetOrientation()[2]),Vector.SafeNorm(vec))
332  angle = VS.acos(dot)
333  return angle
334 
335 # signed velocity is negative when the thrust is reverse
336 # otherwise velocity is positive
338  velocity = Vector.Dot(Vector.SafeNorm(unit.GetOrientation()[2]),unit.GetVelocity())
339  return velocity
340 
341 # change the direction and thrust forward at the same time
342 def changeDirectionAndThrust(unit, angular, thrust):
343  import Vector
344  import vsrandom
345  unit.SetAngularVelocity(angular)
346  unit.LongitudinalThrust(thrust)
347  return 1
348 
349 # returns the shield percent for a ship
350 # ships with dual shields will return 0.5 with full shields
352  shield = (unit.FShieldData() + unit.RShieldData() + unit.LShieldData() + unit.BShieldData()) / 4
353  return shield