Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
random_encounters.py
Go to the documentation of this file.
1 import vsrandom
2 import faction_ships
3 import launch_recycle
4 import launch
5 import VS
6 import unit
7 import sys
8 import adventure
9 import news
10 import universe
11 import fg_util
12 import dynamic_battle
13 import dj_lib
14 import debug
15 import generate_dyn_universe
16 import ShowProgress
17 
19  class playerdata:
25  self.prob_phase+=1;
26  return self.prob_amplitude*(.6+.4*VS.cos ((self.prob_phase*3.1415926536*2)/self.prob_period))
27  def __init__(self,sig_distance,det_distance):
28  debug.debug("init playerdata")
29  try:
31  except:
32  import sys
33  debug.debug(str(sys.exc_info()[0])+str(sys.exc_info()[1]))
34  self.quests=[]
35  self.curquest=0
36  self.last_ship=0
37  self.curmode=0
38  self.lastmode=0
39  self.lastsys=""
40  self.sig_container=VS.Unit()
41  self.significant_distance=sig_distance
42  self.detection_distance=det_distance
44  debug.debug("done playerdat")
45  def __init__(self, sigdis, detectiondis, gendis, minnships, gennships, unitprob, enemyprob, capprob, capdist):
46  unitprob=1
47  debug.debug("init random enc")
48 
49  self.capship_gen_distance=capdist
50  # player_num=player
51  self.enprob = enemyprob
52  self.fighterprob = unitprob
53 
54  self.det_distance = detectiondis
55  self.sig_distance = sigdis
56  self.players=[]
57  self.generation_distance=gendis
58  self.min_num_ships=minnships
59  self.gen_num_ships=gennships
60  self.capship_prob=capprob
61  self.cur_player=0
62  self.sig_distance_table = {"enigma_sector/heavens_gate":(2000,4000,.4)}
63  self.spawning=0
64  # Cache format is (Faction,System,Probability[Faction],sum)
65  self.probability_cache = (0,'',[],0)
66  debug.debug("end random enc")
67  def AddPlayer (self):
68 # debug.debug("begin add player")
70 # debug.debug("add player")
71 
72  def NewSystemHousekeeping(self,oldsystem,newsystem):
73  print oldsystem,' IS NOW AT ',newsystem
74  print oldsystem.split('/')[0]
75  if oldsystem.split('/')[0]=='Special':
76  print 'Showing splash screen!'
78  fg_util.launchBases(newsystem)
79  fg_util.launchUnits(newsystem)
80  news.newNews()
81  newquest = adventure.newAdventure (self.cur_player,oldsystem,newsystem)
82  if (newquest):
83  self.cur.quests+=[newquest]
84  else:
85  self.RestoreDroneMission()
88  VS.hideSplashScreen()
90  qdf=adventure.persistentAdventure (self.cur_player)
91  if (qdf):
92  self.cur.quests+=[qdf]
94  sysfile = VS.getSystemFile()
95  self.cur.GeneratePhaseAndAmplitude()
96  if sysfile in self.sig_distance_table:
97  self.cur.significant_distance = self.sig_distance_table[sysfile][0]
98  self.cur.detection_distance = self.sig_distance_table[sysfile][1]
99  self.cur.prob_amplitude = self.sig_distance_table[sysfile][2]
100  return
101  minsig = unit.minimumSigDistApart()
102  if (self.sig_distance>minsig*0.15):
103  self.cur.significant_distance=minsig*0.15
104  else:
105  self.cur.significant_distance=self.sig_distance
106  if (self.det_distance>minsig*0.2):
107  self.cur.detection_distance=minsig*0.2
108  else:
109  self.cur.detection_distance=self.det_distance
110 
111  debug.debug("resetting sigdist=%f detdist=%f" % (self.cur.significant_distance,self.cur.detection_distance))
112 
113  def SetEnemyProb (self,enp):
114  self.enprob = enp
115 
116 
117  def AsteroidNear (self,uni, how):
118  i = VS.getUnitList()
119  dd = self.cur.detection_distance
120  while i.notDone():
121  un = i.current()
122  if (uni.getSignificantDistance(un)<how):
123  if (unit.isAsteroid (un)):
124  debug.debug("asty near")
125  return 1
126  i.advance()
127  return 0
128  def TrueEnProb(self,enprob):
129  ret=1
130  nam = VS.numActiveMissions()
131  while (nam>0):
132  ret*=(1-enprob)
133  nam-=1
134  debug.debug(1-ret)
135  return 1-ret;
136 
137  def launch_near(self,un, forceLaunch=False):
138  if (VS.GetGameTime()<10 and not forceLaunch):
139  debug.debug("hola!")
140  return
141  cursys=VS.getSystemFile()
142  cursysfaction=VS.GetGalaxyFaction(cursys)
143 
144  # Computing probability numbers is relatively expensive,
145  # accessing multiple times FG/Galaxy/Universe API.
146  # Although with some optimization it becomes acceptable on
147  # a director loop, this is still much better: we compute
148  # probabilities roughly once per system entry, and then just
149  # launch a single flightgroup per director execution (to spread
150  # launch load in time and make it less intrusive on gameplay -
151  # ie, stutter less noticeably). Outside code will take care
152  # of ceasing to call this function when no more flightgroups
153  # are needed.
154 
155  # Validate probability cache
156  if (cursysfaction != self.probability_cache[0]) \
157  or (cursys != self.probability_cache[1]) \
158  or (faction_ships.getMaxFactions() != len(self.probability_cache[2])):
159  debug.debug('Probability numbers:')
160  psum = 0
161  probs = []
162  for factionnum in range(faction_ships.getMaxFactions()):
163  faction=faction_ships.intToFaction(factionnum)
164  num=fg_util.NumFactionFGsInSystem(faction,cursys) # will count bases... but... much quicker.
165  if num==1:
166  #cannot accept counting bases in this case...
167  num=len(fg_util.FGsInSystem(faction,cursys))
168 
169  avg=float(num)/float(fg_util.MaxNumFlightgroupsInSystem(cursys))#/float(numsigs)
170  fortress_level=0
171  if cursys in faction_ships.fortress_systems:
172  foretress_level=faction_ships.fortress_systems[cursys]
173  avg*=(not (VS.GetRelation(cursysfaction,faction)<0 and cursys in faction_ships.fortress_systems))*fortress_level+(1-fortress_level)
174  debug.debug('Chance for %s ship: %g'%(faction, avg))
175  probs.append(avg)
176  psum += avg
177  self.probability_cache = (cursysfaction,cursys,probs,psum)
178  else:
179  debug.debug('Probability numbers cached.')
180 
181  # Launch a single random flightgroup in the bunch
183  p = 0
184  for factionnum in range(len(self.probability_cache[2])):
185  p += self.probability_cache[2][factionnum]
186  if (p>=rnd):
187  #now we know that we will generate some ships!
188  faction=faction_ships.intToFaction(factionnum)
189  fglist=fg_util.FGsInSystem(faction,cursys)
190  for k in range(10): #try 10 times
191  flightgroup=fglist[vsrandom.randrange(len(fglist))]
192  typenumbers=fg_util.GetShipsInFG(flightgroup,faction)
193  if not len(typenumbers):
194  continue
195  debug.debug('FG Name: "%s", ShipTypes: %s'%(flightgroup,str(typenumbers)))
196  launch_recycle.launch_types_around(flightgroup,faction,typenumbers,'default',self.generation_distance*vsrandom.random()*0.9,un,self.generation_distance*vsrandom.random()*2,'','',100)
197  break
198 
199  # Update DJ
200  dj_lib.PlayMusik(0,dj_lib.HOSTILE_NEWLAUNCH_DISTANCE)
201 
203  num_ships=0
204  leadah = uni.getFlightgroupLeader ()
205  i = VS.getUnitList()
206  dd = self.cur.detection_distance
207  while i.notDone():
208  un = i.current()
209  if (uni.getSignificantDistance(un)<dd*1.6):
210  if ((not un.isSignificant()) and (not un.isSun())):
211  unleadah = un.getFlightgroupLeader ()
212  if (leadah!=unleadah):
213  num_ships+=1
214  i.advance()
215  return num_ships>=n
216 
217  def SetModeZero(self):
218  self.cur.last_ship=0
219  self.cur.curmode=0
220  self.cur.sig_container.setNull()
221  for q in self.cur.quests:
222  q.NoSignificantsNear()
223 
224  def SetModeOne (self,significant):
225  self.cur.last_ship=0
226  self.cur.curmode=1
227  self.cur.sig_container=significant
228  cursys = VS.getSystemFile()
229  oldsys = self.cur.lastsys==cursys
230  if (not oldsys):
231  self.NewSystemHousekeeping(self.cur.lastsys,cursys)
232  self.cur.lastsys=cursys
233  for q in self.cur.quests:
234  q.SignificantsNear(self.cur.sig_container)
235 # import dynamic_battle
236 # dynamic_battle.UpdateCombatTurn()
237 
238  def decideMode(self):
239  myunit=VS.getPlayerX(self.cur_player)
240  if (not myunit):
241  self.SetModeZero()
242  return myunit
243  significant_unit = self.cur.sig_container
244 # un=VS.getUnit(0);
245 # i=0
246 # while (un):
247 # debug.debug(un.getName())
248 # i+=1
249 # un= VS.getUnit(i)
250 
251  if (not significant_unit):
252  un=VS.getUnit(self.cur.last_ship)
253  if (self.DifferentSystemP()):
254  un.setNull()
255  if (not un):
256  self.SetModeZero()
257  else:
258  sd = self.cur.significant_distance
259  if ((un.getSignificantDistance(myunit)<sd) and (un.isSignificant())):
260  self.SetModeOne (un)
261  return un
262  self.cur.last_ship+=1
263  return VS.Unit()
264  else:
265  #significant_unit is something.... lets see what it is
266  cursys = VS.getSystemFile()
267  if (self.DifferentSystemP()):
268  debug.debug("different")
269  self.SetModeZero()
270  significant_unit.setNull ()
271  else:
272  dd = self.cur.detection_distance
273  if (myunit.getSignificantDistance (significant_unit)>dd):
274  self.SetModeZero ()
275  return VS.Unit()
276  else:
277  return significant_unit
278  return significant_unit
279  def DifferentSystemP(self):
280  cursys=VS.getSystemFile()
281  if (cursys==self.cur.lastsys):
282  return 0
283  self.NewSystemHousekeeping(self.cur.lastsys,cursys)
284  self.cur.lastsys=cursys
285  return 1
286  def Execute(self):
289  if (self.cur_player>=len(self.players)):
290  self.AddPlayer()
291  self.cur=self.players[self.cur_player]
292  if (self.cur.curquest<len(self.cur.quests)):
293  if (self.cur.quests[self.cur.curquest].Execute()):
294  self.cur.curquest+=1
295  else:
296  del self.cur.quests[self.cur.curquest]
297  else:
298  self.cur.curquest=0
299  un = self.decideMode ()
300  if (self.cur.curmode!=self.cur.lastmode):
301  #lastmode=curmode#processed this event don't process again if in critical zone
302  if ((self.spawning or (vsrandom.random()<(self.fighterprob*self.cur.UpdatePhaseAndAmplitude()))) and un):
303  debug.debug("curmodechange %d in progress" % (self.cur.curmode))
304  if (VS.vsConfig("physics","spawn_units_close","false")=="true" and not self.atLeastNInsignificantUnitsNear (un,self.min_num_ships)):
305  #determine whether to launch more ships next to significant thing based on ships in that range
306  debug.debug("launch near")
307  self.launch_near (VS.getPlayerX(self.cur_player))
308  self.spawning=1
309  else:
310  self.cur.lastmode=self.cur.curmode
311  self.spawning=0
312  debug.debug("curmodechange %d ended" % (self.cur.curmode))
313  else:
314  self.cur.lastmode=self.cur.curmode
315  self.spawning=0
316  debug.debug("curmodechange %d ended" % (self.cur.curmode))
317  self.cur_player+=1
318  if (self.cur_player>=VS.getNumPlayers()):
319  self.cur_player=0
320  VS.setMissionOwner(self.cur_player)
321 
322 debug.debug("done loading rand enc")